Use Intel QuickSync with ffmpeg to have hardware accelerated video decoding and encoding – CentOS 7.4.1708

Author:

This howto shows how you can use ffmpeg to hardware accelerate the video decoding and encoding if you have the proper Intel processor. Some of the Intel processors have GPU unit in them, which can be used to decode and encode video streams and it is pretty fast! High end Intel CPU could encode around 20 h264 (using faster preset) video streams on a single processor mini server as SUPERMICRO system using X11SSV-M4F.
Here are the steps how to enable the hardware options and what and how to install the software to hardware encode a video stream successful under linux – CentOS 7 distro.

For the first four steps just follow the howto “Install Intel Media Studio SDK and GPU driver from Intel Media Server Studio 2018 R1 – CentOS 7 (7.4.1708)” Here we include only the headers of the steps, because they are important part of this howto.
Installing Media Server Studio your system will get validated stack of software by Intel to build application against Media SDK interface:

  • libmfx.a dispatcher – the API for your application
  • libmfxhw64.so hardware implementation
  • related libraries (including OpenCL)
  • specific versions for libva and libdrm
  • user mode driver
  • modified i915 graphics driver module
  • kernel patches

Here are the steps:

STEP 1) Enable internal graphics in your system.

We have an article on the topic – Enable internal graphics in SUPERMICRO servers

STEP 2) Install the right CentOS 7 – 7.4.1708

We have an article on the topic – Howto do a netinstall of CentOS 7 (7.4.1708) – minimal server installation

STEP 3) Download Intel Media Server Studio 2018 R1

We have an article on the topic – Download, what is in the package Intel Media Server Studio 2018 R1 and which Linux distro it supports

STEP 4) Install Intel Media Studio SDK from Intel Media Server Studio 2018 R1

“Install Intel Media Studio SDK and GPU driver from Intel Media Server Studio 2018 R1 – CentOS 7 (7.4.1708)”

STEP 5) Tune pkgconfig libmfx (Intel library, installed with the Media Server Studio)

Create a file:

/usr/lib64/pkgconfig/libmfx.pc

with the following content:

prefix=/opt/intel/mediasdk
exec_prefix=${prefix}
libdir=${prefix}/lib/lin_x64
includedir=${prefix}/include
 
Name: libmfx
Description: Intel Media SDK
Version: 16.8
Libs: -L${libdir} -lmfx -lva -lstdc++ -ldl -lva-drm -ldrm
Cflags: -I${includedir} -I/usr/include/libdrm

STEP 6) Move the Media SDK include files in a mfx sub-directory

This is required because ffmpeg expect these files to be in a sub-directory

[root@srv ~]# mkdir /opt/intel/mediasdk/include/mfx
[root@srv ~]# cp /opt/intel/mediasdk/include/*.h /opt/intel/mediasdk/include/mfx/

STEP 7) Install yasm – a dependency for the ffmpeg software.

[root@srv ~]# wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
[root@srv ~]# tar xzf yasm-1.3.0.tar.gz
[root@srv ~]# cd yasm-1.3.0
[root@srv yasm-1.3.0]# export CFLAGS="-O2 -march=native -msse3 -fomit-frame-pointer -pipe"
[root@srv yasm-1.3.0]# export CXXFLAGS="${CFLAGS}"
[root@srv yasm-1.3.0]# export CHOST="x86_64-pc-linux-gnu"
[root@srv yasm-1.3.0]# export LDFLAGS="-Wl,-O1"
[root@srv yasm-1.3.0]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
....
....
....
[root@srv yasm-1.3.0]# make -j8
gcc -std=gnu99  -I.  \
  -c -o genperf.o `test -f tools/genperf/genperf.c || echo './'`tools/genperf/genperf.c
gcc -std=gnu99  -I.  \
  -c -o gp-perfect.o `test -f tools/genperf/perfect.c || echo './'`tools/genperf/perfect.c
gcc -std=gnu99  -I.  \
  -c -o gp-phash.o `test -f libyasm/phash.c || echo './'`libyasm/phash.c
gcc -std=gnu99  -I.  \
  -c -o gp-xmalloc.o `test -f libyasm/xmalloc.c || echo './'`libyasm/xmalloc.c
gcc -std=gnu99  -I.  \
  -c -o gp-xstrdup.o `test -f libyasm/xstrdup.c || echo './'`libyasm/xstrdup.c
gcc -std=gnu99  -I.  \
  -c -o re2c-main.o `test -f tools/re2c/main.c || echo './'`tools/re2c/main.c
gcc -std=gnu99  -I.  \
  -c -o re2c-code.o `test -f tools/re2c/code.c || echo './'`tools/re2c/code.c
....
....
....
[root@srv yasm-1.3.0]# make install
make  install-recursive
make[1]: Entering directory `/root/yasm-1.3.0'
....
....
....
[root@srv yasm-1.3.0]#
[root@srv yasm-1.3.0]# ldconfig
[root@srv yasm-1.3.0]# cd ../
[root@srv ~]#

STEP 8) Download ffmpeg version 2.8.X and compile it

it is important to use this branch 2.8.X because we’ve tested with the others 3.2.X, 3.3.X, 3.4.X and there were errors and we unable to use the the GPU unit to accelerate our video decoding and encoding. Here is the different ffmpeg release https://ffmpeg.org/download.html#releases or you can use the github to download https://github.com/FFmpeg/FFmpeg/releases

[root@srv ~]# wget https://www.ffmpeg.org/releases/ffmpeg-2.8.14.tar.bz2
[root@srv ~]# tar xjf ffmpeg-2.8.14.tar.bz2
[root@srv ~]# cd ffmpeg-2.8.14
[root@srv ffmpeg-2.8.14]# export CFLAGS="-O2 -march=native -msse3 -fomit-frame-pointer -pipe"
[root@srv ffmpeg-2.8.14]# export CXXFLAGS="${CFLAGS}"
[root@srv ffmpeg-2.8.14]# export CHOST="x86_64-pc-linux-gnu"
[root@srv ffmpeg-2.8.14]# export LDFLAGS="-Wl,-O1"
[root@srv ffmpeg-2.8.14]# ./configure --enable-libmfx --prefix=/usr/local/ffmpeg-qsv
install prefix            /usr/local/ffmpeg-qsv
source path               .
C compiler                gcc
C library                 glibc
ARCH                      x86 (generic)
big-endian                no
runtime cpu detection     yes
yasm                      yes
MMX enabled               yes
MMXEXT enabled            yes
3DNow! enabled            yes
3DNow! extended enabled   yes
SSE enabled               yes
SSSE3 enabled             yes
AVX enabled               yes
XOP enabled               yes
FMA3 enabled              yes
FMA4 enabled              yes
i686 features enabled     yes
CMOV is fast              yes
EBX available             yes
EBP available             yes
debug symbols             yes
strip symbols             yes
optimize for size         no
optimizations             yes
static                    yes
shared                    no
postprocessing support    no
new filter support        yes
network support           yes
threading support         pthreads
safe bitstream reader     yes
SDL support               no
opencl enabled            no
texi2html enabled         no
perl enabled              yes
pod2man enabled           yes
makeinfo enabled          no
makeinfo supports HTML    no

Enabled programs:
ffmpeg                  ffprobe                 ffserver

External libraries:
iconv                   lzma                    zlib
libmfx

Enabled decoders:
aac                     ayuv                    frwu
aac_fixed               bethsoftvid             g2m
aac_latm                bfi                     g723_1
aasc                    bink                    g729
ac3                     binkaudio_dct           gif
ac3_fixed               binkaudio_rdft          gsm
adpcm_4xm               bintext                 gsm_ms
adpcm_adx               bmp                     h261
adpcm_afc               bmv_audio               h263
adpcm_ct                bmv_video               h263i
adpcm_dtk               brender_pix             h263p
adpcm_ea                c93                     h264
adpcm_ea_maxis_xa       cavs                    h264_qsv
adpcm_ea_r1             ccaption                hap
adpcm_ea_r2             cdgraphics              hevc
adpcm_ea_r3             cdxl                    hevc_qsv
adpcm_ea_xas            cinepak                 hnm4_video
adpcm_g722              cljr                    hq_hqa
adpcm_g726              cllc                    hqx
adpcm_g726le            comfortnoise            huffyuv
adpcm_ima_amv           cook                    iac
adpcm_ima_apc           cpia                    idcin
adpcm_ima_dk3           cscd                    idf
adpcm_ima_dk4           cyuv                    iff_byterun1
adpcm_ima_ea_eacs       dca                     iff_ilbm
adpcm_ima_ea_sead       dds                     imc
adpcm_ima_iss           dfa                     indeo2
adpcm_ima_oki           dirac                   indeo3
adpcm_ima_qt            dnxhd                   indeo4
adpcm_ima_rad           dpx                     indeo5
adpcm_ima_smjpeg        dsd_lsbf                interplay_dpcm
adpcm_ima_wav           dsd_lsbf_planar         interplay_video
adpcm_ima_ws            dsd_msbf                jacosub
adpcm_ms                dsd_msbf_planar         jpeg2000
adpcm_sbpro_2           dsicinaudio             jpegls
adpcm_sbpro_3           dsicinvideo             jv
adpcm_sbpro_4           dss_sp                  kgv1
adpcm_swf               dvbsub                  kmvc
adpcm_thp               dvdsub                  lagarith
adpcm_thp_le            dvvideo                 loco
adpcm_vima              dxa                     mace3
adpcm_xa                dxtory                  mace6
adpcm_yamaha            eac3                    mdec
aic                     eacmv                   metasound
alac                    eamad                   microdvd
alias_pix               eatgq                   mimic
als                     eatgv                   mjpeg
amrnb                   eatqi                   mjpegb
amrwb                   eightbps                mlp
amv                     eightsvx_exp            mmvideo
anm                     eightsvx_fib            motionpixels
ansi                    escape124               movtext
ape                     escape130               mp1
apng                    evrc                    mp1float
ass                     exr                     mp2
asv1                    ffv1                    mp2float
asv2                    ffvhuff                 mp3
atrac1                  ffwavesynth             mp3adu
atrac3                  fic                     mp3adufloat
atrac3p                 flac                    mp3float
aura                    flashsv                 mp3on4
aura2                   flashsv2                mp3on4float
avrn                    flic                    mpc7
avrp                    flv                     mpc8
avs                     fourxm                  mpeg1video
avui                    fraps                   mpeg2_qsv
mpeg2video              prores                  tscc2
mpeg4                   prores_lgpl             tta
mpegvideo               ptx                     twinvq
mpl2                    qcelp                   txd
msa1                    qdm2                    ulti
msmpeg4v1               qdraw                   utvideo
msmpeg4v2               qpeg                    v210
msmpeg4v3               qtrle                   v210x
msrle                   r10k                    v308
mss1                    r210                    v408
mss2                    ra_144                  v410
msvideo1                ra_288                  vb
mszh                    ralf                    vble
mts2                    rawvideo                vc1
mvc1                    realtext                vc1_qsv
mvc2                    rl2                     vc1image
mxpeg                   roq                     vcr1
nellymoser              roq_dpcm                vima
nuv                     rpza                    vmdaudio
on2avc                  rv10                    vmdvideo
opus                    rv20                    vmnc
paf_audio               rv30                    vorbis
paf_video               rv40                    vp3
pam                     s302m                   vp5
pbm                     sami                    vp6
pcm_alaw                sanm                    vp6a
pcm_bluray              sgi                     vp6f
pcm_dvd                 sgirle                  vp7
pcm_f32be               shorten                 vp8
pcm_f32le               sipr                    vp9
pcm_f64be               smackaud                vplayer
pcm_f64le               smacker                 vqa
pcm_lxf                 smc                     wavpack
pcm_mulaw               smvjpeg                 webp
pcm_s16be               snow                    webvtt
pcm_s16be_planar        sol_dpcm                wmalossless
pcm_s16le               sonic                   wmapro
pcm_s16le_planar        sp5x                    wmav1
pcm_s24be               srt                     wmav2
pcm_s24daud             ssa                     wmavoice
pcm_s24le               stl                     wmv1
pcm_s24le_planar        subrip                  wmv2
pcm_s32be               subviewer               wmv3
pcm_s32le               subviewer1              wmv3image
pcm_s32le_planar        sunrast                 wnv1
pcm_s8                  svq1                    ws_snd1
pcm_s8_planar           svq3                    xan_dpcm
pcm_u16be               tak                     xan_wc3
pcm_u16le               targa                   xan_wc4
pcm_u24be               targa_y216              xbin
pcm_u24le               tdsc                    xbm
pcm_u32be               text                    xface
pcm_u32le               theora                  xl
pcm_u8                  thp                     xsub
pcm_zork                tiertexseqvideo         xwd
pcx                     tiff                    y41p
pgm                     tmv                     yop
pgmyuv                  truehd                  yuv4
pgssub                  truemotion1             zero12v
pictor                  truemotion2             zerocodec
pjs                     truespeech              zlib
png                     tscc                    zmbv
ppm

Enabled encoders:
a64multi                huffyuv                 png
a64multi5               jpeg2000                ppm
aac                     jpegls                  prores
ac3                     ljpeg                   prores_aw
ac3_fixed               mjpeg                   prores_ks
adpcm_adx               movtext                 qtrle
adpcm_g722              mp2                     r10k
adpcm_g726              mp2fixed                r210
adpcm_ima_qt            mpeg1video              ra_144
adpcm_ima_wav           mpeg2_qsv               rawvideo
adpcm_ms                mpeg2video              roq
adpcm_swf               mpeg4                   roq_dpcm
adpcm_yamaha            msmpeg4v2               rv10
alac                    msmpeg4v3               rv20
alias_pix               msvideo1                s302m
amv                     nellymoser              sgi
apng                    pam                     snow
ass                     pbm                     sonic
asv1                    pcm_alaw                sonic_ls
asv2                    pcm_f32be               srt
avrp                    pcm_f32le               ssa
avui                    pcm_f64be               subrip
ayuv                    pcm_f64le               sunrast
bmp                     pcm_mulaw               svq1
cinepak                 pcm_s16be               targa
cljr                    pcm_s16be_planar        tiff
comfortnoise            pcm_s16le               tta
dca                     pcm_s16le_planar        utvideo
dnxhd                   pcm_s24be               v210
dpx                     pcm_s24daud             v308
dvbsub                  pcm_s24le               v408
dvdsub                  pcm_s24le_planar        v410
dvvideo                 pcm_s32be               vorbis
eac3                    pcm_s32le               wavpack
ffv1                    pcm_s32le_planar        webvtt
ffvhuff                 pcm_s8                  wmav1
flac                    pcm_s8_planar           wmav2
flashsv                 pcm_u16be               wmv1
flashsv2                pcm_u16le               wmv2
flv                     pcm_u24be               xbm
g723_1                  pcm_u24le               xface
gif                     pcm_u32be               xsub
h261                    pcm_u32le               xwd
h263                    pcm_u8                  y41p
h263p                   pcx                     yuv4
h264_qsv                pgm                     zlib
hevc_qsv                pgmyuv                  zmbv

Enabled hwaccels:
h263_vaapi              hevc_vaapi              vc1_qsv
h264_qsv                mpeg2_qsv               vc1_vaapi
h264_vaapi              mpeg2_vaapi             wmv3_vaapi
hevc_qsv                mpeg4_vaapi

Enabled parsers:
aac                     dvdsub                  mpegvideo
aac_latm                flac                    opus
ac3                     g729                    png
adx                     gsm                     pnm
bmp                     h261                    rv30
cavsvideo               h263                    rv40
cook                    h264                    tak
dca                     hevc                    vc1
dirac                   mjpeg                   vorbis
dnxhd                   mlp                     vp3
dpx                     mpeg4video              vp8
dvbsub                  mpegaudio               vp9
dvd_nav

Enabled demuxers:
aa                      g723_1                  mpjpeg
aac                     g729                    mpl2
ac3                     gif                     mpsub
act                     gsm                     msnwc_tcp
adf                     gxf                     mtv
adp                     h261                    mv
adx                     h263                    mvi
aea                     h264                    mxf
afc                     hevc                    mxg
aiff                    hls                     nc
amr                     hnm                     nistsphere
anm                     ico                     nsv
apc                     idcin                   nut
ape                     idf                     nuv
apng                    iff                     ogg
aqtitle                 ilbc                    oma
asf                     image2                  paf
asf_o                   image2_alias_pix        pcm_alaw
ass                     image2_brender_pix      pcm_f32be
ast                     image2pipe              pcm_f32le
au                      image_bmp_pipe          pcm_f64be
avi                     image_dds_pipe          pcm_f64le
avr                     image_dpx_pipe          pcm_mulaw
avs                     image_exr_pipe          pcm_s16be
bethsoftvid             image_j2k_pipe          pcm_s16le
bfi                     image_jpeg_pipe         pcm_s24be
bfstm                   image_jpegls_pipe       pcm_s24le
bink                    image_pictor_pipe       pcm_s32be
bintext                 image_png_pipe          pcm_s32le
bit                     image_qdraw_pipe        pcm_s8
bmv                     image_sgi_pipe          pcm_u16be
boa                     image_sunrast_pipe      pcm_u16le
brstm                   image_tiff_pipe         pcm_u24be
c93                     image_webp_pipe         pcm_u24le
caf                     ingenient               pcm_u32be
cavsvideo               ipmovie                 pcm_u32le
cdg                     ircam                   pcm_u8
cdxl                    iss                     pjs
cine                    iv8                     pmp
concat                  ivf                     pva
data                    jacosub                 pvf
daud                    jv                      qcp
dfa                     latm                    r3d
dirac                   live_flv                rawvideo
dnxhd                   lmlm4                   realtext
dsf                     loas                    redspark
dsicin                  lrc                     rl2
dss                     lvf                     rm
dts                     lxf                     roq
dtshd                   m4v                     rpl
dv                      matroska                rsd
dvbsub                  mgsts                   rso
dxa                     microdvd                rtp
ea                      mjpeg                   rtsp
ea_cdata                mlp                     sami
eac3                    mlv                     sap
epaf                    mm                      sbg
ffm                     mmf                     sdp
ffmetadata              mov                     sdr2
filmstrip               mp3                     segafilm
flac                    mpc                     shorten
flic                    mpc8                    siff
flv                     mpegps                  sln
fourxm                  mpegts                  smacker
frm                     mpegtsraw               smjpeg
g722                    mpegvideo               smush
sol                     tmv                     wav
sox                     truehd                  wc3
spdif                   tta                     webm_dash_manifest
srt                     tty                     webvtt
stl                     txd                     wsaud
str                     vc1                     wsvqa
subviewer               vc1t                    wtv
subviewer1              vivo                    wv
sup                     vmd                     xa
swf                     vobsub                  xbin
tak                     voc                     xmv
tedcaptions             vplayer                 xwma
thp                     vqf                     yop
tiertexseq              w64                     yuv4mpegpipe

Enabled muxers:
a64                     ipod                    pcm_s32be
ac3                     ircam                   pcm_s32le
adts                    ismv                    pcm_s8
adx                     ivf                     pcm_u16be
aiff                    jacosub                 pcm_u16le
amr                     latm                    pcm_u24be
apng                    lrc                     pcm_u24le
asf                     m4v                     pcm_u32be
asf_stream              matroska                pcm_u32le
ass                     matroska_audio          pcm_u8
ast                     md5                     psp
au                      microdvd                rawvideo
avi                     mjpeg                   rm
avm2                    mkvtimestamp_v2         roq
bit                     mlp                     rso
caf                     mmf                     rtp
cavsvideo               mov                     rtp_mpegts
crc                     mp2                     rtsp
dash                    mp3                     sap
data                    mp4                     segment
daud                    mpeg1system             singlejpeg
dirac                   mpeg1vcd                smjpeg
dnxhd                   mpeg1video              smoothstreaming
dts                     mpeg2dvd                sox
dv                      mpeg2svcd               spdif
eac3                    mpeg2video              spx
f4v                     mpeg2vob                srt
ffm                     mpegts                  stream_segment
ffmetadata              mpjpeg                  swf
filmstrip               mxf                     tee
flac                    mxf_d10                 tg2
flv                     mxf_opatom              tgp
framecrc                null                    truehd
framemd5                nut                     uncodedframecrc
g722                    oga                     vc1
g723_1                  ogg                     vc1t
gif                     oma                     voc
gxf                     opus                    w64
h261                    pcm_alaw                wav
h263                    pcm_f32be               webm
h264                    pcm_f32le               webm_chunk
hds                     pcm_f64be               webm_dash_manifest
hevc                    pcm_f64le               webp
hls                     pcm_mulaw               webvtt
ico                     pcm_s16be               wtv
ilbc                    pcm_s16le               wv
image2                  pcm_s24be               yuv4mpegpipe
image2pipe              pcm_s24le

Enabled protocols:
async                   hls                     rtmpt
cache                   http                    rtp
concat                  httpproxy               srtp
crypto                  icecast                 subfile
data                    md5                     tcp
ffrtmphttp              mmsh                    udp
file                    mmst                    udplite
ftp                     pipe                    unix
gopher                  rtmp

Enabled filters:
acrossfade              dctdnoiz                paletteuse
adelay                  deband                  pan
adrawgraph              decimate                perms
aecho                   deflate                 pixdesctest
aeval                   dejudder                psnr
aevalsrc                deshake                 qp
afade                   detelecine              random
aformat                 dilation                removegrain
ainterleave             drawbox                 removelogo
allpass                 drawgraph               replaygain
allrgb                  drawgrid                reverse
allyuv                  dynaudnorm              rgbtestsrc
alphaextract            earwax                  rotate
alphamerge              edgedetect              scale
amerge                  elbg                    scale2ref
amix                    equalizer               select
amovie                  erosion                 sendcmd
anull                   extractplanes           separatefields
anullsink               fade                    setdar
anullsrc                fftfilt                 setfield
apad                    field                   setpts
aperms                  fieldmatch              setsar
aphasemeter             fieldorder              settb
aphaser                 flanger                 showcqt
aresample               format                  showfreqs
areverse                fps                     showinfo
aselect                 framepack               showpalette
asendcmd                framerate               showspectrum
asetnsamples            framestep               showvolume
asetpts                 gradfun                 showwaves
asetrate                haldclut                showwavespic
asettb                  haldclutsrc             shuffleplanes
ashowinfo               hflip                   sidechaincompress
asplit                  highpass                signalstats
astats                  histogram               silencedetect
astreamsync             hqx                     silenceremove
atadenoise              hstack                  sine
atempo                  hue                     smptebars
atrim                   idet                    smptehdbars
avectorscope            il                      split
bandpass                inflate                 ssim
bandreject              interleave              swapuv
bass                    join                    tblend
bbox                    lenscorrection          telecine
biquad                  life                    testsrc
blackdetect             lowpass                 thumbnail
blend                   lut                     tile
cellauto                lut3d                   transpose
channelmap              lutrgb                  treble
channelsplit            lutyuv                  trim
chorus                  mandelbrot              unsharp
codecview               mergeplanes             vectorscope
color                   movie                   vflip
colorbalance            negate                  vignette
colorchannelmixer       noformat                volume
colorkey                noise                   volumedetect
colorlevels             null                    vstack
compand                 nullsink                w3fdif
concat                  nullsrc                 waveform
copy                    overlay                 xbr
crop                    pad                     yadif
curves                  palettegen              zoompan
dcshift

Enabled bsfs:
aac_adtstoasc           imx_dump_header         mpeg4_unpack_bframes
chomp                   mjpeg2jpeg              noise
dump_extradata          mjpega_dump_header      remove_extradata
h264_mp4toannexb        mov2textsub             text2movsub
hevc_mp4toannexb        mp3_header_decompress

Enabled indevs:
dv1394                  lavfi                   v4l2
fbdev                   oss

Enabled outdevs:
fbdev                   oss                     v4l2

License: LGPL version 2.1 or later
Creating config.mak, config.h, and doc/config.texi...
[root@conv11 ffmpeg-2.8.14]# make -j 8
CC      libavdevice/oss.o
CC      libavdevice/alldevices.o
CC      libavdevice/avdevice.o
CC      libavdevice/lavfi.o
CC      libavdevice/dv1394.o
....
....
....
LD      ffmpeg_g
LD      ffprobe_g
LD      ffserver_g
CP      ffserver
STRIP   ffserver
CP      ffprobe
CP      ffmpeg
STRIP   ffprobe
STRIP   ffmpeg
[root@conv11 ffmpeg-2.8.14]# make install
INSTALL doc/ffmpeg.1
INSTALL doc/ffprobe.1
INSTALL doc/ffserver.1
INSTALL doc/ffmpeg-all.1
....
....
....
INSTALL libavutil/tea.h
INSTALL libavutil/lzo.h
INSTALL libavutil/avconfig.h
INSTALL libavutil/ffversion.h
INSTALL libavutil/libavutil.pc
[root@conv11 ffmpeg-2.8.14]#

And now you have a ffmpeg installation build against Intel API libmfx dispatcher, which could benefit from the hardware acceleration in decoding and encoding video streams. Thus using a Intel QuickSync technology.

STEP 9) ffmpeg example – how to use the GPU hardware decoding and encoding

[root@conv11 ffmpeg-2.8.14]# cd
[root@conv11 ~]# wget http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_60fps_stereo_abl.mp4
[root@conv11 ~]# /usr/local/ffmpeg-qsv/bin/ffmpeg -vcodec h264_qsv -i bbb_sunflower_1080p_60fps_stereo_abl.mp4 -vcodec h264_qsv -b:v 600k -maxrate 1200K -s 1280x720 -aspect 16:9 -preset medium -maxrate 500K -g 50 -strict -2 -c:a aac -b:a 96k -ar 44100 -f mpegts -threads 0 output.mp4
ffmpeg version 2.8.14 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-16)
  configuration: --enable-libmfx --prefix=/usr/local/ffmpeg-qsv
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
libva info: VA-API version 1.0.0
libva info: va_getDriverName() returns 0
libva info: User requested driver 'iHD'
libva info: Trying to open /opt/intel/mediasdk/lib64/iHD_drv_video.so
libva info: Found init function __vaDriverInit_1_0
libva info: va_openDriver() returns 0
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'bbb_sunflower_1080p_60fps_stereo_abl.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 1
    compatible_brands: isomavc1
    creation_time   : 2013-12-16 18:03:30
    title           : Big Buck Bunny, Sunflower version
    artist          : Blender Foundation 2008, Janus Bager Kristensen 2013
    comment         : Creative Commons Attribution 3.0 - http://bbb3d.renderfarming.net
    genre           : Animation
    composer        : Sacha Goedegebure
  Duration: 00:10:34.53, start: 0.000000, bitrate: 6490 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), nv12, 1920x2160, 6005 kb/s, 60 fps, 60 tbr, 60k tbn, 60k tbc (default)
    Metadata:
      creation_time   : 2013-12-16 18:03:30
      handler_name    : GPAC ISO Video Handler
    Stream #0:1(und): Audio: mp3 (mp4a / 0x6134706D), 48000 Hz, stereo, s16p, 160 kb/s (default)
    Metadata:
      creation_time   : 2013-12-16 18:03:37
      handler_name    : GPAC ISO Audio Handler
    Stream #0:2(und): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, 5.1(side), fltp, 320 kb/s (default)
    Metadata:
      creation_time   : 2013-12-16 18:03:37
      handler_name    : GPAC ISO Audio Handler
    Side data:
      audio service type: main
libva info: VA-API version 1.0.0
libva info: va_getDriverName() returns 0
libva info: User requested driver 'iHD'
libva info: Trying to open /opt/intel/mediasdk/lib64/iHD_drv_video.so
libva info: Found init function __vaDriverInit_1_0
libva info: va_openDriver() returns 0
Output #0, mpegts, to 'output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 1
    compatible_brands: isomavc1
    composer        : Sacha Goedegebure
    title           : Big Buck Bunny, Sunflower version
    artist          : Blender Foundation 2008, Janus Bager Kristensen 2013
    comment         : Creative Commons Attribution 3.0 - http://bbb3d.renderfarming.net
    genre           : Animation
    encoder         : Lavf56.40.101
    Stream #0:0(und): Video: h264 (h264_qsv), nv12, 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 600 kb/s, 60 fps, 90k tbn, 60 tbc (default)
    Metadata:
      creation_time   : 2013-12-16 18:03:30
      handler_name    : GPAC ISO Video Handler
      encoder         : Lavc56.60.100 h264_qsv
    Stream #0:1(und): Audio: aac, 44100 Hz, 5.1(side), fltp, 96 kb/s (default)
    Metadata:
      creation_time   : 2013-12-16 18:03:37
      handler_name    : GPAC ISO Audio Handler
      encoder         : Lavc56.60.100 aac
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (h264_qsv) -> h264 (h264_qsv))
  Stream #0:2 -> #0:1 (ac3 (native) -> aac (native))
Press [q] to stop, [?] for help
libva info: VA-API version 1.0.0
libva info: va_getDriverName() returns 0
libva info: User requested driver 'iHD'
libva info: Trying to open /opt/intel/mediasdk/lib64/iHD_drv_video.so
libva info: Found init function __vaDriverInit_1_0
libva info: va_openDriver() returns 0
[h264_qsv @ 0x3587360] A decode call did not consume any dataitrate=1162.5kbits/s    
    Last message repeated 14 times
frame=38072 fps=126 q=-0.0 Lsize=   90023kB time=00:10:34.48 bitrate=1162.3kbits/s    
video:71572kB audio:7436kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 13.942102%
[root@conv11 ~]# /usr/local/ffmpeg-qsv/bin/ffmpeg -i output.mp4
ffmpeg version 2.8.14 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-16)
  configuration: --enable-libmfx --prefix=/usr/local/ffmpeg-qsv
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
Input #0, mpegts, from 'output.mp4':
  Duration: 00:10:34.57, start: 1.400000, bitrate: 1162 kb/s
  Program 1 
    Metadata:
      service_name    : Big Buck Bunny, Sunflower version
      service_provider: FFmpeg
    Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 60 fps, 60 tbr, 90k tbn, 120 tbc
    Stream #0:1[0x101](und): Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, 5.1, fltp, 100 kb/s

You can see ffmpeg is using VA-API and /opt/intel/mediasdk/lib64/iHD_drv_video.so for video acceleration encoding.

* Live stream decoding and encoding with hardware acceleration and outputting multiple output

The input stream is from a multicast source and the output is a wowza server

/usr/local/ffmpeg-qsv/bin/ffmpeg -vcodec h264_qsv -i 'udp://239.10.10.1:5000?fifo_size=1000000&overrun_nonfatal=1' -filter_complex '[0:v]yadif=1:0:0,split=3[v1][v2][v3];[v1]scale=1280:720,framerate=50[out1];[v2]scale=960:540,framerate=25[out2];[v3]scale=640:360,framerate=25[out3]' -map '[out1]' -map '[out2]' -map '[out3]' -map 0:a -streamid 0:0x151 -streamid 1:0x150 -streamid 2:0x149 -streamid 3:0x140 -vcodec h264_qsv -preset medium -maxrate:0 1900K -bufsize:0 2400K -b:v:0 1500k -maxrate:1 1200K -bufsize:1 1000K -b:v:1 1000k -maxrate:2 900K -bufsize:2 1000K -b:v:2 600k -g 50 -strict -2 -c:a aac -b:a 96k -ar 44100 -reorder_queue_size 32 -threads 8 -fflags +igndts -copyts -f mpegts 'udp://10.10.10.10:30000?pkt_size=2632&buffer_size=20000000&fifo_size=2000000'
ffmpeg version 2.8.14 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-16)
  configuration: --enable-libmfx --prefix=/usr/local/ffmpeg-qsv3
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
[h264_qsv @ 0x21e6960] non-existing PPS 0 referenced
libva info: VA-API version 1.0.0
libva info: va_getDriverName() returns 0
libva info: User requested driver 'iHD'
libva info: Trying to open /opt/intel/mediasdk/lib64/iHD_drv_video.so
libva info: Found init function __vaDriverInit_1_0
libva info: va_openDriver() returns 0
    Last message repeated 1 times
Input #0, mpegts, from 'udp://239.10.10.33:5000?fifo_size=1000000&overrun_nonfatal=1':
  Duration: N/A, start: 1375.213422, bitrate: N/A
  Program 74 
    Metadata:
      service_name    : Test 1 HD
      service_provider: Test
    Stream #0:0[0x734]: Video: h264 (High) ([27][0][0][0] / 0x001B), nv12, 1920x1080, 25 fps, 25 tbr, 90k tbn, 90k tbc
    Stream #0:1[0x744]: Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 256 kb/s
[Parsed_framerate_7 @ 0x2413620] time base:1/180000 -> 1/180000 exact:1
[Parsed_framerate_7 @ 0x2413620] fps -> fps:25/1 scene score:7.000000 interpolate start:15 end:240
[Parsed_framerate_5 @ 0x2438d20] time base:1/180000 -> 1/180000 exact:1
[Parsed_framerate_5 @ 0x2438d20] fps -> fps:25/1 scene score:7.000000 interpolate start:15 end:240
[Parsed_framerate_3 @ 0x2934640] time base:1/180000 -> 1/180000 exact:1
[Parsed_framerate_3 @ 0x2934640] fps -> fps:50/1 scene score:7.000000 interpolate start:15 end:240
libva info: VA-API version 1.0.0
libva info: va_getDriverName() returns 0
libva info: User requested driver 'iHD'
libva info: Trying to open /opt/intel/mediasdk/lib64/iHD_drv_video.so
libva info: Found init function __vaDriverInit_1_0
libva info: va_openDriver() returns 0
libva info: VA-API version 1.0.0
libva info: va_getDriverName() returns 0
libva info: User requested driver 'iHD'
libva info: Trying to open /opt/intel/mediasdk/lib64/iHD_drv_video.so
libva info: Found init function __vaDriverInit_1_0
libva info: va_openDriver() returns 0
libva info: VA-API version 1.0.0
libva info: va_getDriverName() returns 0
libva info: User requested driver 'iHD'
libva info: Trying to open /opt/intel/mediasdk/lib64/iHD_drv_video.so
libva info: Found init function __vaDriverInit_1_0
libva info: va_openDriver() returns 0
Output #0, mpegts, to 'udp://10.10.10.10:30000?pkt_size=2632&buffer_size=20000000&fifo_size=2000000':
  Metadata:
    encoder         : Lavf56.40.101
    Stream #0:0: Video: h264 (h264_qsv), nv12, 1280x720, q=2-31, 1500 kb/s, 50 fps, 90k tbn, 50 tbc
    Metadata:
      encoder         : Lavc56.60.100 h264_qsv
    Stream #0:1: Video: h264 (h264_qsv), nv12, 960x540, q=2-31, 1000 kb/s, 25 fps, 90k tbn, 25 tbc
    Metadata:
      encoder         : Lavc56.60.100 h264_qsv
    Stream #0:2: Video: h264 (h264_qsv), nv12, 640x360, q=2-31, 600 kb/s, 25 fps, 90k tbn, 25 tbc
    Metadata:
      encoder         : Lavc56.60.100 h264_qsv
    Stream #0:3: Audio: aac, 44100 Hz, stereo, fltp, 96 kb/s
    Metadata:
      encoder         : Lavc56.60.100 aac
Stream mapping:
  Stream #0:0 (h264_qsv) -> yadif (graph 0)
  framerate (graph 0) -> Stream #0:0 (h264_qsv)
  framerate (graph 0) -> Stream #0:1 (h264_qsv)
  framerate (graph 0) -> Stream #0:2 (h264_qsv)
  Stream #0:1 -> #0:3 (mp2 (native) -> aac (native))
Press [q] to stop, [?] for help
libva info: VA-API version 1.0.0
libva info: va_getDriverName() returns 0
libva info: User requested driver 'iHD'
libva info: Trying to open /opt/intel/mediasdk/lib64/iHD_drv_video.so
libva info: Found init function __vaDriverInit_1_0
libva info: va_openDriver() returns 0
[h264_qsv @ 0x21ea740] A decode call did not consume any data=00:23:14.04 bitrate=  46.6kbits/s    
frame=  937 fps= 59 q=-0.0 Lq=-0.0 q=-0.0 size=    8267kB time=00:23:14.57 bitrate=  48.6kbits/s    
video:7225kB audio:228kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 10.923792%

* Troubleshooting

If you get a build error like this, you skipped the (STEP 6) Move the Media SDK include files in a mfx sub-directory. This is required because ffmpeg expect these files to be in a sub-directory. Just move the files in a sub-directory mfx as it is shown in (STEP 6)

gcc -Wl,-O1 -Wl,--as-needed -Wl,-z,noexecstack -o /tmp/ffconf.7IhNLjmP/test /tmp/ffconf.7IhNLjmP/test.o -lm -ldl -llzma -lz -pthread -pthread
use_pkg_config libmfx libmfx mfx/mfxvideo.h MFXInit
check_pkg_config libmfx libmfx mfx/mfxvideo.h MFXInit
pkg-config --exists --print-errors libmfx
check_func_headers mfx/mfxvideo.h MFXInit -I/opt/intel/mediasdk/include -I/usr/include/libdrm -L/opt/intel/mediasdk/lib/lin_x64 -lmfx -lva -lstdc++ -ldl -lva-drm -ldrm
check_ld cc -I/opt/intel/mediasdk/include -I/usr/include/libdrm -L/opt/intel/mediasdk/lib/lin_x64 -lmfx -lva -lstdc++ -ldl -lva-drm -ldrm
check_cc -I/opt/intel/mediasdk/include -I/usr/include/libdrm -L/opt/intel/mediasdk/lib/lin_x64
BEGIN /tmp/ffconf.7IhNLjmP/test.c
    1   #include <mfx/mfxvideo.h>
    2   #include <stdint.h>
    3   long check_MFXInit(void) { return (long) MFXInit; }
    4   int main(void) { int ret = 0;
    5    ret |= ((intptr_t)check_MFXInit) & 0xFFFF;
    6   return ret; }
END /tmp/ffconf.7IhNLjmP/test.c
gcc -D_ISOC99_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -O2 -march=native -msse3 -fomit-frame-pointer -pipe -std=c11 -fomit-frame-pointer -pthread -I/opt/intel/mediasdk/include -I/usr/include/libdrm -L/opt/intel/mediasdk/lib/lin_x64 -c -o /tmp/ffconf.7IhNLjmP/test.o /tmp/ffconf.7IhNLjmP/test.c
/tmp/ffconf.7IhNLjmP/test.c:1:26: fatal error: mfx/mfxvideo.h: No such file or directory
 #include <mfx/mfxvideo.h>
                          ^
compilation terminated.
require libmfx mfx/mfxvideo.h MFXInit -llibmfx
check_lib libmfx mfx/mfxvideo.h MFXInit -llibmfx
check_func_headers mfx/mfxvideo.h MFXInit -llibmfx
check_ld cc -llibmfx
check_cc
BEGIN /tmp/ffconf.7IhNLjmP/test.c
    1   #include <mfx/mfxvideo.h>
    2   #include <stdint.h>
    3   long check_MFXInit(void) { return (long) MFXInit; }
    4   int main(void) { int ret = 0;
    5    ret |= ((intptr_t)check_MFXInit) & 0xFFFF;
    6   return ret; }
END /tmp/ffconf.7IhNLjmP/test.c
gcc -D_ISOC99_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -O2 -march=native -msse3 -fomit-frame-pointer -pipe -std=c11 -fomit-frame-pointer -pthread -c -o /tmp/ffconf.7IhNLjmP/test.o /tmp/ffconf.7IhNLjmP/test.c
/tmp/ffconf.7IhNLjmP/test.c:1:26: fatal error: mfx/mfxvideo.h: No such file or directory
 #include <mfx/mfxvideo.h>
                          ^
compilation terminated.
ERROR: libmfx not found

2 thoughts on “Use Intel QuickSync with ffmpeg to have hardware accelerated video decoding and encoding – CentOS 7.4.1708”

  1. Great tutorial! I’ve been working on this problem for a while now.

    I wasn’t able to get hevc_qsv to work. Any suggestions?

    1. No we don’t use hevc_qsv and haven’t tried it, but you can try newer version of ffmpeg. Recently we discovered that ffmpeg 3.2 (downloaded from https://www.ffmpeg.org/) can replace here ffmpeg 2.8 and I hear it works with hevc_qsv, probably we are going to edit this article and create a new one with ffmpeg 3.2 and hevc_qsv.

Leave a Reply

Your email address will not be published. Required fields are marked *