background
In order to reduce CPU usage and improve system access capabilities, the encoding and decoding module needs to be moved to the GPU for processing. The default release version of ffmpeg does not support GPU acceleration, so ffmpeg needs to be recompiled to support GPU hardware acceleration.
ffmpeg official compilation guide
https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
reader
Readers of this article must have some experience in using Linux. Operations such as regular software installation are not described in this document.
the term
ffmpeg: open source streaming media encoding and decoding and other processing tools.
Dependent components
git: version management tool, used to obtain program source code;
g++: compiler for c++ programs;
cmake: compilation management tool for c++ programs;
ffmpeg: main function is to pull rtsp video stream. If this component is not added, opencv can only extract the rtsp video stream from this component. The source code of this tool can be obtained from github.
cuda runtime library: The underlying dependency library released by NVIDIA for using GPU, which can be obtained from the NVIDIA official website.
Video_Codec_SDK: The video encoding and decoding library provided by NVIDIA, which can be obtained from the NVIDIA official website.
Install cuda
Install toolkit
Download cuda toolkit from NVIDIA official website.
sh cuda_11.4.4_470.82.01_linux.run//Do not install the driver. There are many online tutorials on driver installation, you can refer to it.
Install nv-codec-header
Install nvidia and compile dependent header files. Make will generate a .pc file. Make install will install the .pc file in the system path. You can use pkg-config to find
git clone http://git.videolan.org/git/ffmpeg/nv -codec-headers.git
make
make install
Install nasm
sudo apt-get install nasm
Install x264
git clone --depth 1 https://code.videolan.org/videolan/x264.git
PKG_CONFIG_PATH="/usr/local/x264/pkgconfig" ./configure --prefix="/usr/local/x264" --bindir="/usr/local/x264/bin" --enable-static --enable-pic && \
make & make install
Install x265
wget -O x265.tar.bz2 https://bitbucket.org/multicoreware/x265_git/get/master.tar.bz2
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="/usr/local/x265" -DENABLE_SHARED=off ../../source
make & make install
ffmpeg compile
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/x264/lib/pkgconfig:/usr/local/x265/lib/pkgconfig
./configure --prefix="/usr/local/ffmpeg/" --extra-cflags="-I/usr/local/cuda/include" --extra-ldflags="-L/usr/local/cuda/lib64" --extra-libs="-lpthread -lm" --bindir="/usr/local/ffmpeg/bin" --enable-shared --enable-ffplay --enable-cuda --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --enable-gpl --enable-libx264 --enable-libx265
make
make install
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/ffmpeg/lib
in conclusion
ffmpeg supports hardware decoding (completed), but does not support hardware pixel format conversion; that is, the pixel format output by ffmpeg after hardware decoding is NV12, and if you want to convert, you must use the CPU.