如何在Linux上运行硬件加速qsv,看这一篇就够了。
前置准备工作
1、检查linux 的版本,最好使用Ubuntu20.04 使用该版本最便捷也比较稳定。如果使用Ubuntu18.04及以下的版本很多依赖库需要自行编译。
2、检查内核版本 va-api要求Kernel 4.14.16及以上。
uname -ar
3、更新Linux 软件包管理
sudo apt update
3、检查是否安装gcc 与 cmake 开发环境 其中 cmake 的版本要求在3.x以上否则需要更新会报错。
sudo apt install gcc
sudo apt install cmake
sudo apt-get install pkg-config
4、检测是否具备核显能力,查看系统是否检测到GPU 或者直接去英特尔官网搜索
lspci | grep VGA
这里基本上不会有什么问题出现。编译过程中提示缺什么包就安装什么包
更详细步骤可参考:https://zhuanlan.zhihu.com/p/62246545
编译Intel Media SDK 库文件
这个是最关键的也是最重要的编译库 。根据要求需要libva、libva-utils 、Gmmlib、Griver 前提编译的依赖库
1、下载ibva、libva-utils 、Gmmlib、Griver 等编译库
sudo apt-get install -y libmfx1 libmfx-tools libva-dev libmfx-dev intel-media-va-driver-non-free vainfo
2、添加Linux环境环境变量
环境配置:
vim ~/.bashrc
#末尾添加
export LIBVA_DRIVER_NAME=iHD
3、检查用Vainfo 检查是否配置成功
sudo vainfo
如果出现没报错说明编译成功Inter Driver有检测核显
4、下载编译 Intel Media SDK 库。解压文件
[Intel Mdeida SDK 下载:https://github.com/Intel-Media-SDK/MediaSDK/releases]
5、编译 Intel Media SDK库文件进入源文件
mkdir build &&cd build
cmake . .
make -j8 && make install
6、再次添加Linux 环境变量
vim ~/.bashrc
export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig/
export LIBVA_DRIVERS_PATH=/usr/lib/x86_64-linux-gnu/dri/
export LD_LIBRARY_PATH=/opt/intel/mediasdk/lib/
编译 ffmpeg 文件
1、安装环境
sudo apt install yasm
2、下载ffmpeg 文件 [ ffmpeg下载地址:https://www.ffmpeg.org/download.html]
3、进入源文件夹中,创建build文件夹
mkdir build && cd build
./configure --arch=x86_64 --disable-yasm --enable-vaapi --enable-libmfx --extra-cflags=-fPIC --extra-cxxflags=-fPIC --prefix=“输出目录”
4、进入输出目录bin中检查是否成功
./ffmpeg -hwaccels
5、运行可执行文件是否报错
ffmpeg -y -hwaccel qsv -c:v h264_qsv -i 1.mp4 -q:v 30 -c:v h264_qsv 2.mp4
链接ffmpeg静态库
进入源文件夹中下载的ffmpeg 源码的版本最好编译源码使用的版本一致不然很有可能会出问题。
vim CMakeLists.txt
复制修改四个地方的目录位置粘贴
cmake_minimum_required(VERSION 3.10)
project(OpenCVTest)
message("project root path is ${CMAKE_CURRENT_SOURCE_DIR}")
set(CMAKE_CXX_FLAGS "-pthread -g" )
set(ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
set(OPENCV_LIB_PATH "/home/mo/project/lib //源文件库文件位置")
set(LDFLAGS "${LDFLAGS} -lva")
macro(OPENCV_IMPORT_LIBS LibsList LibName LibPaths)
message(STATUS "import libs ${LibName}!")
add_library(${LibName} STATIC IMPORTED GLOBAL)
set_target_properties(${LibName} PROPERTIES IMPORTED_LOCATION ${LibPaths})
list(APPEND ${LibsList} ${LibName})
endmacro(OPENCV_IMPORT_LIBS LibsList LibName LibPaths)
include_directories(${ROOT_PATH}/include //头文件位置)
include_directories(/home/mo/buildA/include //静态库头头文件位置)
OPENCV_IMPORT_LIBS(OPENCV_LIBS opencv_world "${OPENCV_LIB_PATH}/libopencv_world.a")
OPENCV_IMPORT_LIBS(OPENCV_LIBS opencv_llmlmf "${OPENCV_LIB_PATH}/libIlmImf.a")
OPENCV_IMPORT_LIBS(OPENCV_LIBS opencv_libjpeg "${OPENCV_LIB_PATH}/liblibjpeg-turbo.a")
OPENCV_IMPORT_LIBS(OPENCV_LIBS opencv_liblibopenjp2 "${OPENCV_LIB_PATH}/liblibopenjp2.a")
OPENCV_IMPORT_LIBS(OPENCV_LIBS opencv_libpng "${OPENCV_LIB_PATH}/liblibpng.a")
OPENCV_IMPORT_LIBS(OPENCV_LIBS opencv_libtiff "${OPENCV_LIB_PATH}/liblibtiff.a")
#OPENCV_IMPORT_LIBS(OPENCV_LIBS opencv_libjasper "${OPENCV_LIB_PATH}/liblibjasper.a")
OPENCV_IMPORT_LIBS(OPENCV_LIBS opencv_libnotify "${OPENCV_LIB_PATH}/libittnotify.a")
OPENCV_IMPORT_LIBS(OPENCV_LIBS opencv_libwebp "${OPENCV_LIB_PATH}/liblibwebp.a")
OPENCV_IMPORT_LIBS(OPENCV_LIBS opencv_libquirc "${OPENCV_LIB_PATH}/libquirc.a")
OPENCV_IMPORT_LIBS(OPENCV_LIBS opencv_libzlib "${OPENCV_LIB_PATH}/libzlib.a")
OPENCV_IMPORT_LIBS(OPENCV_LIBS opencv_liblibprotobuf "${OPENCV_LIB_PATH}/liblibprotobuf.a")
file(GLOB WRAPPER_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp
)
add_executable(OpenCVTest ${WRAPPER_SRCS})
target_link_libraries(OpenCVTest
${OPENCV_LIBS}
-ldl
)
target_link_directories(${PROJECT_NAME} PUBLIC /home/mo/buildA/lib //静态库位置)
target_link_libraries(${PROJECT_NAME}
-Wl,--start-group
libavutil.a
libswscale.a
libswresample.a
libavcodec.a
libavformat.a
libavfilter.a
libavdevice.a
-Wl,--end-group
-lmfx
-lz
-lva
-lX11
-lvdpau
-llzma
-ldrm
-lva
-lva-drm
-lva-x11
pthread
)
7、完成运行。
参考:
https://blog.csdn.net/m0_38101947/article/details/130100490
https://www.freesion.com/article/34851134007/
可能出现的问题
1、提示 [h264_qsv @ 0x7fadd00a3140] Selected ratecontrol mode is unsupported下qsv 需要改变编码模式,不支持固定码率
2、提示[h264_qsv @ 0x7f71dc0a3780] Error during encoding: undefined behavior (-16) 可能ffmpeg 版本与源码的版本不一致导致
3、如果 运行 ffmpeg 提示XXX .so cannot open shared object file: No such file or directory 需要编辑
vim /etc/ld.so.conf.d/ffmpeg.conf
#添加到最后一行(ffmpeg 库位置)
/monchickey/ffmpeg/lib
sudo ldconfig
4、提示 undefined reference to lzma_stream_decoder
undefined reference to zma_code
in function vdpau_device_create
错误。
参考 :FFmepeg在编译执行文件时遇到的问题及解决方法