使用cpu進(jìn)行軟編解碼時(shí),cpu效率低并且占用高。使用硬件加速,能夠明顯降低CPU的占用,參看博客 ffmpeg學(xué)習(xí)(16)AVDevice使用。 這里以使用英偉達(dá)gpu進(jìn)行h264編解碼加速為例說明,其他平臺(tái)類似。
1、winodws硬件加速支持
在windows平臺(tái)直接下載官方的預(yù)編譯ffmpeg、lib開發(fā)包,都已經(jīng)支持了英偉達(dá)顯卡硬件加速。使用命令ffmpeg.exe -hide_banner true -codecs |findstr h264
,查詢結(jié)果如下
可以看到英偉達(dá)硬件支持的解碼器有 h264_cuvid,編碼器有h264_nvenc、nvenc_h264、nvenc。
2、linux下硬件加速支持
使用sudo apt get install ffmpeg
后是默認(rèn)不支持的硬件加速的,查看支持使用命令 ffmpeg -hide_banner true -codecs | grep h264
,結(jié)果如下
并沒有出現(xiàn)同windows下類似帶有nv或者cu字樣的的編解碼器名稱。
為了使用硬件加速功能,需要從源代碼進(jìn)行編譯,添加硬件加速支持。
編譯ffnvcodec
再nvidia硬件上進(jìn)行ffmpeg加速,需要準(zhǔn)備ffnvcodec。
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
sudo make install
編譯環(huán)境
sudo apt-get install build-essential yasm cmake libnuma1 libnuma-dev
后面編譯ffmpeg源碼時(shí)可以根據(jù)缺少項(xiàng)再安裝。
ffmpeg編譯
可以使用git命令 git clone https://git.ffmpeg.org/ffmpeg.git /ffmpeg
拉取最新的ffmpeg分支,這里以ffmpeg n4.0.2版本為例
編譯與安裝命令
./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
其他完整的編譯選項(xiàng)
./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 \
注意:上述命令行編譯了所有內(nèi)容,導(dǎo)致編譯生成的庫較大,可以根據(jù)具體情況編譯需要的模塊。
先使用 –disable-all 禁用所有模塊,再添加 –enable-xxxxx 開啟xxxxx模塊
上面添加了H.264的支持。
編譯完成后,再次命令 ffmpeg -hide_banner true -codecs | grep 264
查看支持情況
這里明顯對(duì)nvidia的硬件有了特定的編碼器 h264_cuvid、解碼器nvenc和h264_nvenc。
查看相關(guān)編解碼器的信息,例如查看解碼器h264_cuvid信息,如下
再查看編碼器h264_nvenc的信息(還包含preset、profile、level以及其他更多編碼參數(shù)),僅列出支持的像素編碼,如下圖
3、linux下硬件加速使用、問題解決
在ffmpeg源代碼中可以使用 如下代碼
AVCodec *enc = avcodec_find_encoder_by_name("h264_nvenc");
AVCodec *dec = avcodec_find_decoder_by_name("h264_cuvid");
正常運(yùn)行時(shí),發(fā)現(xiàn)cpu占用降低,會(huì)有g(shù)pu的占用。
可能出現(xiàn)的的問題
在編碼程序中,設(shè)定編碼器h264_nvenc,運(yùn)行程序報(bào)錯(cuò)
[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
這種錯(cuò)誤明確提示驅(qū)動(dòng)版本不支持。我們可以在nv-codec-headers/README
文件中有要求說明
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
查看本機(jī)的驅(qū)動(dòng)版本已經(jīng)gpu型號(hào),驅(qū)動(dòng)版本為 418.67, 是低于要求版本的,需要更新驅(qū)動(dòng)。
但是我們注意到,機(jī)器的顯卡名稱不全。首先使用 lspci | grep -i vga
查看顯卡的PCI ID號(hào),為 NVIDIA Corporation Device 1e04 (rev a1)。
通過可以通過英偉達(dá)顯卡PCI ID查詢
列表 http://pci-ids.ucw.cz/read/PC/10de
查詢 http://pci-ids.ucw.cz/mods/PC/10de?action=help?help=pci
知道了顯卡類型,之后就是去官網(wǎng)下載安裝合適版本驅(qū)動(dòng)了。文章來源:http://www.zghlxwxcb.cn/news/detail-408225.html
安裝驅(qū)動(dòng),測(cè)試代碼,一切OK。文章來源地址http://www.zghlxwxcb.cn/news/detail-408225.html
到了這里,關(guān)于ffmpeg學(xué)習(xí) 源代碼編譯、英偉達(dá)硬件加速的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!