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. 源码分析
稍后补充