ffmpeg Intel硬件加速總結


0. 概述

FFmpeg可通過Intel的GPU進行加速,加速器的名稱是Intel Quick Sync Video,簡稱qsv,支持該加速器的Intel處理器的型號是帶有Intel Iris graphics GPU或 HD graphics GPU的CPU,目前幾乎所有Core i系列的CPU和小部分Xeon系列的CPU(E3-1285v5)有GPU,市面上常見的高密度媒體處理服務器便是基於此CPU+GPU的方式實現多路媒體處理計算任務,需要注意的是作為SoC組成部分的GPU是不能虛擬化的,無法通過Xen、Ctrix、KVM或VMware利用GPU計算資源。

1. 安裝驅動與SDK

1.1 前期准備

(1). 添加用戶組 
usermod -a -G video root 
(2). 查看集成顯核情況 
lspci -nn -s 0:02.0 
如果回車顯示

00:02.0 VGA compatible controller [0300]: Intel Corporation Sky Lake Integrated Graphics [8086:193b] (rev 09)
  • 1

說明集成顯核存在,其中193b是graphics device ID,如果看不到此信息的話需要更改BIOS的設置

1.2 SDK安裝

官方提供的SDK安裝方式分為CentOS7.2 Gold安裝(即官方針對該版本操作系統寫好安裝腳本的簡易安裝)以及Generic安裝(需要手動進行),由於本文使用Ubuntu16.04操作系統,故用第二種方式 
SDK安裝又稱為user-mode driver(UMD)安裝 
(1). 首先將mediaserverstudio/SDK/Generic/intel-linux-media-ocl-generic/目錄下的etc、opt、usr文件夾分別拷貝至系統的/etc、/opt、/usr目錄下 
(2). 確定/usr目錄下此前沒有其他版本的libdrm和libva庫,否則應刪除 
(3). 安裝libxcb1-dev(提供X window protocol access)和libpciaccess-dev庫(提供PCI access),這兩個庫均可直接通過apt-get install安裝,在安裝之前建議先通過apt-get build-dep安裝它們的依賴庫 
(4). 進入/opt/intel/mediasdk/opensource/libdrm/2.4.66-55964/libdrm-2.4.66目錄下安裝libdrm庫。DRM即direct rendering manager,可以在linux系統下提供對圖形硬件的訪問。安裝步驟很簡單,./configure、make、make install三步即可 
(5). 進入/opt/intel/mediasdk/opensource/libva/1.67.0.pre1-55964/libva-1.67.0.pre1目錄下安裝libva庫。VA即Video Accerleration,是一個開源的提供硬件編碼的模塊。安裝步驟很簡單,./configure、make、make install三步即可 
(6). 在/etc/environment文件添加中添加下列語句 
LD_LIBRARY_PATH=/usr/local/lib;/usr/lib64 
LIBVA_DRIVER_NAME=iHD 
LIBVA_DRIVERS_PATH=/opt/intel/mediasdk/lib64

1.3 驅動安裝

驅動安裝又名kernel-mode driver(KMD)安裝 
(1). 在www.kernel.org下載linux kernel 4.4.66版本,將/opt/intel/mediasdk/opensource/patches/kmd/4.4.66/intel-kernel-patchs.tar.bz2拷貝到解壓得到的linux-4.4.66目錄下,通過下面的命令為linux kernel加入補丁 
for i in intel-kernel-patches/*.patch; do patch -p1 < $i; done 
(2). 使用下面的命令build kernel 
make olddefconfig 
make -j 8 
make modules_install 
make install 
重啟電腦 
檢測方法:lsmod | grep ‘i915’ 
如果顯示

i915                 1277952  6
i2c_algo_bit           16384  1 i915
drm_kms_helper        135168  1 i915
drm                   356352  7 i915,drm_kms_helper
video                  40960  1 i915
  • 1
  • 2
  • 3
  • 4
  • 5

說明驅動安裝成功

1.4 HEVC插件安裝

在mediaserverstudioprofessional/hevcevaluation目錄下,直接運行install.sh腳本按提示安裝即可

2. Sample測試

2.1 Sample編譯

進入sample目錄 
perl build.pl –cmake=intel64.make.debug –build 
如果出現如下問題:

Not searching for unused variables given on the command line.
-- Intel(R) Media SDK /opt/intel/mediasdk/include, /opt/intel/mediasdk/lib/lin_x64/libmfx.a was found here /opt/intel/mediasdk -- Intel(R) Media SDK /opt/intel/mediasdk/lib/lin_x64 will be used -- Checking for module 'libva-x11>=0.33' -- CMake Error at /usr/share/cmake-3.5/Modules/FindPkgConfig.cmake:367 (message): A required package was not found Call Stack (most recent call first):  /usr/share/cmake-3.5/Modules/FindPkgConfig.cmake:532 (_pkg_check_modules_internal)  /workspace/ffmpeg_linux/MediaServerStudioProfessionalEvaluation2017/MediaSamples_Linux_2017/builder/FindPackages.cmake:387 (pkg_check_modules) CMakeLists.txt:24 (include) -- Configuring incomplete, errors occurred! See also "/workspace/ffmpeg_linux/MediaServerStudioProfessionalEvaluation2017/MediaSamples_Linux_2017/samples/__cmake/intel64.make.debug/CMakeFiles/CMakeOutput.log". make: *** 沒有指明目標並且找不到 makefile。 停止。 [ intel64.make.debug State: FAIL ] 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

則需要 
apt-get install pciaccess 
apt-get install libva-x11-1 
apt-get install libx11*

2.2 Sample測試

可以用2.1自己編譯的Sample,在MediaServerStudioProfessionalEvaluation2017/MediaSamples_Linux_2017/samples/_bin/x64下 
也可以用安裝時帶的Sample,在/opt/intel/mediasdk/samples

(1). 例子1 
./sample_encode h265 -i BQTerrace_1920x1080_60.yuv -o 1.265 -w 1920 -h 1080 
如果出現如下問題:

plugin_loader.h :166 [ERROR] Failed to load plugin from GUID, sts=-9: { 0x6f, 0xad, 0xc7, 0x91, 0xa0, 0xc2, 0xeb, 0x47, 0x9a, 0xb6, 0xdc, 0xd5, 0xea, 0x9d, 0xa3, 0x47 } (Intel (R) Media SDK HW plugin for HEVC ENCODE) Default plugin cannot be loaded (possibly you have to define plugin explicitly) Return on error: error code -3, /home/lab_msdk/buildAgentDir/buildAgent_MediaSDK4/git/mdp_msdk-samples/samples/sample_encode/src/pipeline_encode.cpp 1127 Return on error: error code -3, /home/lab_msdk/buildAgentDir/buildAgent_MediaSDK4/git/mdp_msdk-samples/samples/sample_encode/src/sample_encode.cpp 859 Frame number: 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

hevc需要加-p參數 
打開/opt/intel/mediasdk/plugins/plugins.cfg看到

[HEVC_Decoder_15dd936825ad475ea34e35f3f54217a6] GUID = 15dd936825ad475ea34e35f3f54217a6 PluginVersion = 1 APIVersion = 275 Path = /opt/intel/mediasdk/plugins/libmfx_hevcd_sw64.so Type = 1 CodecID = HEVC Default = 0 [HEVC_Encoder_e5400a06c74d41f5b12d430bbaa23d0b] GUID = e5400a06c74d41f5b12d430bbaa23d0b PluginVersion = 1 APIVersion = 275 Path = /opt/intel/mediasdk/plugins/libmfx_hevce_gacc64.so Type = 2 CodecID = HEVC Default = 0 [HEVC_Encoder_2fca99749fdb49aeb121a5b63ef568f7] GUID = 2fca99749fdb49aeb121a5b63ef568f7 PluginVersion = 1 APIVersion = 275 Path = /opt/intel/mediasdk/plugins/libmfx_hevce_sw64.so Type = 2 CodecID = HEVC Default = 0/2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

里面包含GUID

./sample_encode h264 -i BQTerrace_1920x1080_60.yuv -o 1.264 -w 1920 -h 1080 -f 60
  • 1

運行正常,並顯示

`libva info: VA-API version 0.99.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_0_32 libva info: va_openDriver() returns 0 Encoding Sample Version 7.0.16053497 Input file format YUV420 Output video AVC Source picture: Resolution 1920x1088 Crop X,Y,W,H 0,0,1920,1080 Destination picture: Resolution 1920x1088 Crop X,Y,W,H 0,0,1920,1080 Frame rate 60.00 Bit rate(Kbps) 4703 Gop size 0 Ref dist 0 Ref number 1 Idr Interval 0 Target usage balanced Memory type system Media SDK impl hw Media SDK version 1.19 Processing started Frame number: 601
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

(2). 例子2

./sample_multi_transcode -i::h264 ../content/test_stream.264 -o::h264 out.h264 -hw -la
  • 1

顯示

Multi Transcoding Sample Version 7.0.16053497 libva info: VA-API version 0.99.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_0_32 libva info: va_openDriver() returns 0 Pipeline surfaces number (DecPool): 59 MFX HARDWARE Session 0 API ver 1.19 parameters: Input video: AVC Output video: AVC Session 0 was NOT joined with other sessions Transcoding started .. Transcoding finished Common transcoding time is 0.19 sec MFX session 0 transcoding PASSED: Processing time: 0.19 sec Number of processed frames: 101 The test PASSED
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

該例子運行的正確,表明: 
1. iHD驅動安裝正確 
2. libva路徑正確

./sample_multi_transcode -i::h264 ../content/test_stream.264 -o::h265 out.h265 -hw -la
  • 1

如果出現問題:

Multi Transcoding Sample Version 7.0.16053497 libva info: VA-API version 0.99.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_0_32 libva info: va_openDriver() returns 0 plugin_loader.h :166 [ERROR] Failed to load plugin from GUID, sts=-9: { 0x6f, 0xad, 0xc7, 0x91, 0xa0, 0xc2, 0xeb, 0x47, 0x9a, 0xb6, 0xdc, 0xd5, 0xea, 0x9d, 0xa3, 0x47 } (Intel (R) Media SDK HW plugin for HEVC ENCODE) Default plugin cannot be loaded (possibly you have to define plugin explicitly) Return on error: error code -3, /home/lab_msdk/buildAgentDir/buildAgent_MediaSDK4/git/mdp_msdk-samples/samples/sample_multi_transcode/src/pipeline_transcode.cpp392 Return on error: error code -3, /home/lab_msdk/buildAgentDir/buildAgent_MediaSDK4/git/mdp_msdk-samples/samples/sample_multi_transcode/src/pipeline_transcode.cpp2886 Return on error: error code -3, /home/lab_msdk/buildAgentDir/buildAgent_MediaSDK4/git/mdp_msdk-samples/samples/sample_multi_transcode/src/sample_multi_transcode.cpp 307
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

則說明本機存在多個版本的libdrm和libva 
解決方法 
將/usr中的libdrm*和libva*都刪除 
find /usr -name “libdrm*” | xargs sudo rm –rf 
find /usr -name “libva*” | xargs sudo rm –rf 
apt-get install libva-* 
apt-get install安裝在了這個目錄下: 
/usr/lib/x86_64-linux-gnu/

plugin一直沒有正常安裝成功

3. ffmpeg結合

3.1 ffmpeg編譯

3.1.1 前期工作

(1). 
/opt/intel/mediasdk/include中新建文件夾mfx,並把所有.h拷貝到該文件夾中 
(2). 
mkdir -p /opt/intel/mediasdk/lib64/pkgconfig 
gedit /opt/intel/mediasdk/lib64/pkgconfig/libmfx.pc 
寫了如下內容:

prefix=/opt/intel/mediasdk exec_prefix=${prefix} libdir=${prefix}/lib/lin_x64 includedir=${prefix}/include Name: libmfx Description: Intel Media Server Studio SDK Version: 16.4.2 Libs: -L${libdir} -lmfx -lva -lstdc++ -ldl -lva-drm -ldrm Cflags: -I${includedir} -I/usr/include/libdrm
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

並復制/usr/lib64/pkgconfig/ 
在bashrc中添加

export MFX_HOME=/opt/intel/mediasdk/ export PKG_CONFIG_PATH=/usr/lib64/pkgconfig/
  • 1
  • 2

3.1.2 configure命令

./configure \
    --enable-version3 \ --enable-libvpx \ --enable-libfdk-aac \ --enable-libmp3lame \ --enable-libx264 \ --enable-libmfx \ --extra-cflags="-I/opt/intel/mediasdk/include" \ --extra-ldflags="-L/opt/intel/mediasdk/lib/lin_x64" \ --extra-libs=-lmfx \ --extra-libs=-ldl \ --enable-librtmp \ --enable-shared \ --enable-gpl \ --enable-postproc \ --enable-nonfree \ --enable-avfilter \ --enable-pthreads
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

如果出現如下錯誤

ERROR: libmfx not found using pkg-config If you think configure made a mistake, make sure you are using the latest version from Git. If the latest version fails, report the problem to the ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net. Include the log file "config.log" produced by configure as this will help solve the problem. config.log顯示: Package libmfx was not found in the pkg-config search path. Perhaps you should add the directory containing `libmfx.pc' to the PKG_CONFIG_PATH environment variable No package 'libmfx' found ERROR: libmfx not found using pkg-config
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

則說明沒做前期工作!!!

編譯時可以 | tee config.out,然后grep ‘qsv’ config.out來檢查是否有qsv

3.1.2 make

如果出現編譯錯誤

LD  ffmpeg_g
libavcodec/libavcodec.so:對‘MFXVideoUSER_Load’未定義的引用
collect2: error: ld returned 1 exit status Makefile:131: recipe for target 'ffmpeg_g' failed make: *** [ffmpeg_g] Error 1
  • 1
  • 2
  • 3
  • 4
  • 5

說明configure沒有加如下兩行

--extra-libs=-lmfx \ --extra-libs=-ldl \
  • 1
  • 2

最終編譯通過,運行 ffmpeg -codecs | grep ‘qsv’,如果得到以下顯示

 DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_qsv ) (encoders: h264_qsv h264_vaapi )
  • 1

說明編譯成功! 
3.2 ffmpeg測試 
ffmpeg -codecs|grep qsv 
顯示

DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_qsv ) (encoders: libx264 libx264rgb h264_qsv h264_vaapi ) DEV.L. hevc H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_qsv ) (encoders: hevc_qsv hevc_vaapi ) DEV.L. mpeg2video MPEG-2 video (decoders: mpeg2video mpegvideo mpeg2_qsv ) (encoders: mpeg2video mpeg2_qsv ) D.V.L. vc1 SMPTE VC-1 (decoders: vc1 vc1_qsv )
  • 1
  • 2
  • 3
  • 4

3.3 編解碼器使用方法

265編碼測試 
(1). ffmpeg -s 1920x1080 -pix_fmt yuv420p -i BQTerrace_1920x1080_60.yuv -vcodec hevc_qsv -load_plugins e5400a06c74d41f5b12d430bbaa23d0b -r 60 2_60.265 
(2). ffmpeg -s 1920x1080 -pix_fmt yuv420p -i BQTerrace_1920x1080_60.yuv -vcodec hevc_qsv -load_plugins e5400a06c74d41f5b12d430bbaa23d0b -r 30 2_30.265 
264編碼測試 
(3). ffmpeg -s 1920x1080 -pix_fmt yuv420p -i BQTerrace_1920x1080_60.yuv -vcodec h264_qsv -r 60 2_60.264 
(4). ffmpeg -s 1920x1080 -pix_fmt yuv420p -i BQTerrace_1920x1080_60.yuv -vcodec h264_qsv -r 30 2_30.264 
264轉265 
(5). ffmpeg -i 1_60.264 -vcodec hevc_qsv -load_plugins e5400a06c74d41f5b12d430bbaa23d0b -r 60 2_60_264to265.265 
(6). ffmpeg -i 1_30.264 -vcodec hevc_qsv -load_plugins e5400a06c74d41f5b12d430bbaa23d0b -r 30 2_30_264to265.265 
265轉264 
(7). ffmpeg -i 1_60.265 -vcodec h264_qsv -r 60 2_60_265to264.264 
(8). ffmpeg -i 1_30.265 -vcodec h264_qsv -r 30 2_30_265to264.264

3.4 程序開發使用方法

av_find_encoder_by_name(“h264_qsv”); 
av_find_encoder_by_name(“h265_qsv”);

4. 輔助工具

(1). apt-get install intel-gpu-tools 
intel_gpu_top 類似與top 
intel_gpu_abrt 
intel_gpu_time

(2). 
/opt/intel/mediasdk/tools/metrics_monitor/sample 
./run.sh

5. 實測結果

稍后補充

6. 源碼分析

稍后補充


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM