ffmpeg learning source code compilation, NVIDIA hardware acceleration

Posted by nishanthc12 on Fri, 11 Feb 2022 22:21:05 +0100

When using CPU for soft coding and decoding, the CPU efficiency is low and the occupation is high. Using hardware acceleration can significantly reduce the occupation of CPU. See blog ffmpeg learning (16) AVDevice usage . Here, take the h264 codec acceleration using NVIDIA gpu as an example, which is similar to other platforms.

1. Winows hardware acceleration support

Download the official precompiled ffmpeg and lib development packages directly on the windows platform, which have supported NVIDIA graphics card hardware acceleration. Use the command ffmpeg exe -hide_ Banner true - codecs | findstr h264, the query result is as follows

It can be seen that NVIDIA hardware supports H264 decoders_ Cuvid, encoder has h264_nvenc,nvenc_h264,nvenc.

2. Hardware acceleration support under linux

After using sudo apt get install ffmpeg, hardware acceleration is not supported by default. Check the command ffmpeg -hide_banner true -codecs | grep h264, the result is as follows

There is no codec name with nv or cu similar to that under windows.
In order to use the hardware acceleration function, you need to compile from the source code and add hardware acceleration support.

Compile ffnvcodec

ffnvcodec needs to be prepared for ffmpeg acceleration on nvidia hardware.

git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers 
sudo make install 

Compiling environment

sudo apt-get install build-essential yasm cmake libnuma1 libnuma-dev

When compiling the ffmpeg source code later, you can reinstall it according to the missing items.

ffmpeg compilation

You can use the GIT command git clone https://git.ffmpeg.org/ffmpeg.git /ffmpeg pulls the latest ffmpeg branch, here with ffmpeg N4 Version 0.2 as an example
Compile and install commands

./configure --enable-shared --enable-nonfree --enable-cuda-sdk --enable-libnpp \
--extra-cflags=-I/usr/local/cuda/include \
--extra-ldflags=-L/usr/local/cuda/lib64 \
--prefix=/home/wanggao/software/FFmpeg-n4.0.2/install

make -j8
sudo make install

Other complete compilation options

./configure --enable-shared --enable-nonfree --enable-cuda-sdk --enable-libnpp \
--extra-cflags=-I/usr/local/cuda/include \
--extra-ldflags=-L/usr/local/cuda/lib64 \
--prefix=/home/wanggao/software/FFmpeg-n4.0.2/install \
\
--disable-doc \
--disable-htmlpages \
--disable-podpages \
--disable-txtpages \
--disable-manpages \
\
--enable-gpl \
--enable-libx264 \
--enable-encoder=libx264 \
--enable-decoder=h264 \

Note: the above command line compiles all the contents, resulting in a large library. You can compile the required modules according to the specific situation.
First use – disable all to disable all modules, and then add – enable xxxxx to enable xxxxx modules
H.264 support is added above.

After compiling, command ffmpeg - hide again_ Banner true - codecs | grep 264 view support

There is obviously a specific encoder H264 for nvidia hardware_ Cuvid, decoder nvenc and h264_nvenc.

View relevant codec information, such as h264_cuvid information, as follows

Check encoder H264 again_ The information of nvenc (including preset, profile, level and other coding parameters) only lists the supported pixel coding, as shown in the figure below

3. Hardware acceleration and problem solving under linux

The following code can be used in the ffmpeg source code

AVCodec *enc = avcodec_find_encoder_by_name("h264_nvenc");

AVCodec *dec = avcodec_find_decoder_by_name("h264_cuvid");

During normal operation, it is found that the cpu occupation is reduced and gpu occupation will occur.

Possible problems

In the encoding program, set the encoder h264_nvenc, run the program and report an error

[h264_nvenc @ 0x7fad9030f100] Driver does not support the required nvenc API version. Required: 11.0 Found: 9.0
[h264_nvenc @ 0x7fad9030f100] The minimum required Nvidia driver for nvenc is 390.25 or newer

This error clearly indicates that the driver version is not supported. We can specify the requirements in the NV codec headers / readme file

FFmpeg version of headers required to interface with Nvidias codec APIs.

Corresponds to Video Codec SDK version 11.0.10.

Minimum required driver versions:
Linux: 455.28 or newer
Windows: 456.71 or newer

Check the gpu model of the driver version of this machine. The driver version is 418.67, which is lower than the required version. The driver needs to be updated.

However, we have noticed that the name of the machine's graphics card is incomplete. First, use lspci | grep -i vga to check the PCI ID number of the graphics card, which is NVIDIA Corporation Device 1e04 (rev a1).

Via NVIDIA graphics card PCI ID query
list http://pci-ids.ucw.cz/read/PC/10de
query http://pci-ids.ucw.cz/mods/PC/10de?action=help?help=pci

After knowing the type of graphics card, go to the official website to download and install the appropriate version of the driver.

Install the driver, test the code, and everything is OK.

Topics: ffmpeg