文章目录

  • 前言
  • 正文
  • 1. libmfx / iHD 路线
  • 前置安装
  • 编译安装ffmpeg
  • 2. VAAPI / i965 路线


前言

参考官方文档:http://trac.ffmpeg.org/wiki/Hardware/QuickSync

Intel的核显,通常以genX划分代际,内部俗称GEN。
根据硬件可分为
VAAPI / i965 路线,ffmpeg中对应-hwaccel vaapi ,适用于gen5~gen9.5
libmfx / iHD 路线,ffmpeg中对应硬编解码器h264_qsv ,适用于gen8+

正文

1. libmfx / iHD 路线

前置安装

sudo apt update
sudo apt install autoconf libtool libdrm-dev xorg xorg-dev openbox libx11-dev libgl1-mesa-glx libgl1-mesa-dev

1)下载编译安装libav,对应问题2
https://github.com/intel/libva git clone https://github.com/intel/libva.git
下载后

./autogen.sh
make -j6
sudo make install  

# 2.16.0
apt install meson 
在meson.build目录执行
meson setup build
cd build
ninja
sudo ninja install
# if delete
sudo ninja uninstall

2)下载编译安装gmmlib
https://github.com/intel/gmmlib git clone https://github.com/intel/gmmlib.git

mkdir build && cd build
 cmake -DCMAKE_BUILD_TYPE= Release ..
make -j6
sudo make install

3)下载编译安装 Intel® Media Driver for VAAPI
https://github.com/intel/media-driver git clone https://github.com/intel/media-driver.git

mkdir build && cd build
cmake ..
make -j"$(nproc)"
sudo make install

这个贼慢,建议保存编译好的so,免得再来一次

-- Install configuration: ""
-- Installing: /usr/local/lib/dri/iHD_drv_video.so
-- Installing: /usr/local/lib/libigfxcmrt.so.7.2.0
-- Installing: /usr/local/lib/libigfxcmrt.so.7
-- Installing: /usr/local/lib/libigfxcmrt.so
-- Installing: /usr/local/include/igfxcmrt/cm_rt.h
-- Installing: /usr/local/include/igfxcmrt/cm_rt_g8.h
-- Installing: /usr/local/include/igfxcmrt/cm_rt_g9.h
-- Installing: /usr/local/include/igfxcmrt/cm_rt_g10.h
-- Installing: /usr/local/include/igfxcmrt/cm_rt_g11.h
-- Installing: /usr/local/include/igfxcmrt/cm_rt_g12_tgl.h
-- Installing: /usr/local/include/igfxcmrt/cm_hw_vebox_cmd_g10.h
-- Installing: /usr/local/include/igfxcmrt/cm_rt_def_os.h
-- Installing: /usr/local/include/igfxcmrt/cm_rt_api_os.h
-- Installing: /usr/local/include/igfxcmrt/cm_rt_extension.h
-- Installing: /usr/local/lib/pkgconfig/igfxcmrt.pc

[] No package ‘libva-x11’ found
sudo apt install libva-x11-2

需要将driver配置export出来给libva用,这一步非常关键

export LIBVA_DRIVER_NAME=iHD
export LIBVA_DRIVERS_PATH=/usr/local/lib/dri

然后用vainfo来验证

vainfo

如果没有报错,恭喜你,闯过了整个编译环节中最难的一关,我花了整整5个小时

4)下载编译安装MediaSDK,对应问题1
https://github.com/Intel-Media-SDK/MediaSDK git clone https://github.com/Intel-Media-SDK/MediaSDK.git

下载后

mkdir build && cd build
cmake ..
make -j6
sudo make install

MediaSDK安装时注意这一条
– Installing: /opt/intel/mediasdk/lib/pkgconfig/libmfxhw64.pc

.pc文件并没有放到pkg-config能找到的那几个环境路径中,所以这里
临时export libmfxhw64.pc的位置,方便ffmpeg编译
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/intel/mediasdk/lib/pkgconfig/

否则依然会找不着libmfx

或者将这句加入~/.bashrc让其一直生效

测试是否安装成功(这里一定要是h264文件,否则报错)

$ ./sample_decode h264 -i '/xxxx/1.h264' 
libva info: VA-API version 1.9.0
libva info: User environment variable requested driver 'iHD'
libva info: Trying to open /usr/local/lib/dri/iHD_drv_video.so
libva info: Found init function __vaDriverInit_1_9
libva info: va_openDriver() returns 0
Decoding Sample Version 8.4.27.0


Input video	AVC 
Output format	NV12
Input:
  Resolution	1280x720
  Crop X,Y,W,H	0,0,1280,720
Output:
  Resolution	1280x720
Frame rate	30.00
Memory type		system
MediaSDK impl		hw
MediaSDK version	1.33

Decoding started
Frame number:  868, fps: 1066.751, fread_fps: 0.000, fwrite_fps: 0.000
Decoding finished

编译安装ffmpeg

经验证,这个ffmpeg不能装其他版本,否则qsv使用时会报错, 得下载这个版本
https://github.com/Intel-FFmpeg-Plugin/Intel_FFmpeg_plugins

sudo apt-get install autoconf automake bzip2 nasm yasm

进入ffmpeg目录

./configure --enable-shared \
              --disable-x86asm \
              --disable-lzma \
              --enable-pic \
              --extra-cflags=-fPIC \
              --extra-cxxflags=-fPIC \
              --enable-libmfx \
              --enable-nonfree \
              --enable-encoder=h264_qsv \
              --enable-decoder=h264_qsv \
              --enable-encoder=hevc_qsv \
              --enable-decoder=h264_qsv \
              --enable-decoder=hevc_qsv \
              --disable-libx264 --disable-libx265 \
              --disable-cuvid  --disable-nvenc \
              --prefix=./install

make -j"$(nproc)"
make install

测试

$ ./install/bin/ffmpeg -codecs | grep qsv
ffmpeg version 4.2.4 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 6.5.0 (Ubuntu 6.5.0-2ubuntu1~18.04) 20181026
  configuration: --enable-shared --disable-x86asm --disable-lzma --enable-pic --extra-cflags=-fPIC --extra-cxxflags=-fPIC --enable-libmfx --enable-nonfree --enable-encoder=h264_qsv --enable-decoder=h264_qsv --prefix=./install
  WARNING: library configuration mismatch
  avutil      configuration: --enable-shared --prefix=/usr/local/ffmpeg --enable-gpl --enable-libx264 --enable-cuda-sdk --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
  avcodec     configuration: --enable-shared --prefix=/usr/local/ffmpeg --enable-gpl --enable-libx264 --enable-cuda-sdk --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
  avformat    configuration: --enable-shared --prefix=/usr/local/ffmpeg --enable-gpl --enable-libx264 --enable-cuda-sdk --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
  avdevice    configuration: --enable-shared --prefix=/usr/local/ffmpeg --enable-gpl --enable-libx264 --enable-cuda-sdk --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
  avfilter    configuration: --enable-shared --prefix=/usr/local/ffmpeg --enable-gpl --enable-libx264 --enable-cuda-sdk --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
  swscale     configuration: --enable-shared --prefix=/usr/local/ffmpeg --enable-gpl --enable-libx264 --enable-cuda-sdk --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
  swresample  configuration: --enable-shared --prefix=/usr/local/ffmpeg --enable-gpl --enable-libx264 --enable-cuda-sdk --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
  libavutil      56. 31.100 / 56. 22.100
  libavcodec     58. 54.100 / 58. 35.100
  libavformat    58. 29.100 / 58. 20.100
  libavdevice    58.  8.100 / 58.  5.100
  libavfilter     7. 57.100 /  7. 40.101
  libswscale      5.  5.100 /  5.  3.100
  libswresample   3.  5.100 /  3.  3.100

没有发现h264_qsv 还出现了 library configuration mismatch , 这是因为这个版本与系统中的版本冲突了,卸载原来的版本后就可以找到h264_qsv了

$ ./install/bin/ffmpeg -codecs | grep qsv
ffmpeg version 4.2.4 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 6.5.0 (Ubuntu 6.5.0-2ubuntu1~18.04) 20181026
  configuration: --enable-shared --disable-x86asm --disable-lzma --enable-pic --extra-cflags=-fPIC --extra-cxxflags=-fPIC --enable-libmfx --enable-nonfree --enable-encoder=h264_qsv --enable-decoder=h264_qsv --prefix=./install
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
 DEV.LS h264                 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_v4l2m2m h264_qsv h264_cuvid ) (encoders: h264_nvenc h264_qsv h264_v4l2m2m h264_vaapi nvenc nvenc_h264 )
 DEV.L. hevc                 H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_qsv hevc_cuvid ) (encoders: nvenc_hevc hevc_nvenc hevc_qsv hevc_vaapi )
 DEVIL. mjpeg                Motion JPEG (decoders: mjpeg mjpeg_cuvid ) (encoders: mjpeg mjpeg_qsv mjpeg_vaapi )
 DEV.L. mpeg2video           MPEG-2 video (decoders: mpeg2video mpegvideo mpeg2_v4l2m2m mpeg2_qsv mpeg2_cuvid ) (encoders: mpeg2video mpeg2_qsv mpeg2_vaapi )
 D.V.L. vc1                  SMPTE VC-1 (decoders: vc1 vc1_qsv vc1_v4l2m2m vc1_cuvid )
 DEV.L. vp8                  On2 VP8 (decoders: vp8 vp8_v4l2m2m vp8_cuvid vp8_qsv ) (encoders: vp8_v4l2m2m vp8_vaapi )

可能发生的错误
[问题1] ERROR: libmfx not found
折腾了久不知道咋装,最后在ffmpeg-wiki上找到了答案。trac.ffmpeg.org/wiki/Hardware/QuickSync

  1. 如果是Ubuntu19以上可以在线直接安装
sudo apt get install libmfx-dev

2)下载编译安装
很多文章都很旧了,根本下不到那些东东。还得看官方的
https://github.com/Intel-Media-SDK/MediaSDK

下载后

mkdir build && cd build
cmake ..
make -j6
sudo make install

[问题2]
Requested ‘libva >= 1.5.0’ but version of libva is 1.1.0
需要下载编译更高版本的libva
https://github.com/intel/libva 下载后

./autogen.sh
make -j6
sudo make install

接着编译安装MediaSDK即可

[问题3]
离奇的问题,当ffmpeg的configure正常执行后,发现decoder中没有发现h264_qsv或者 发现了h264_qsv但是最后编译出来的ffmpeg中并没有支持h264_qsv
并出现 library configuration missmatch字样

原因:ffmpeg冲突

2. VAAPI / i965 路线

sudo apt install vainfo

上来直接安装vainfo,会直接安装va和i965.结束
ffmpeg在运行时缺了一些库,
sudo apt install libxcb-shape0 libXv*

如果是中途改道的像上面这样,就参考下面
步骤类似上面,3-4步的mediasdk不要,替换为安装i965 driver
下载地址:
https://github.com/intel/intel-vaapi-driver

然后ffmpeg编译时指定开启

./configure --disable-static --enable-shared --enable-gpl --enable-version3 --disable-debug --enable-vaapi --disable-x86asm --prefix=./install