omen23 / ffmpeg-ffnvcodec-explanation

How to get FFmpeg to export the needed symbols on (K)ubuntu cosmic/disco (and similar distros) so OBS and MPV can use NVENC and NVDEC by installing the FFmpeg Nvidia dynamic linker code
39 stars 7 forks source link

FFmpeg lists hardware accelerators but can't use them #4

Closed catb0t closed 4 years ago

catb0t commented 4 years ago

I have used these instructions successfully before on Ubuntu 18.10, but this time I am getting a problem I can't work around.

$ uname -a 
Linux oryx-pro 5.0.0-29-generic #31-Ubuntu SMP Thu Sep 12 13:05:32 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Pop!_OS 19.04
Release:    19.04
Codename:   disco

Installing version 435 instead of 430, because that's where my system packages were already:

sudo apt install nvidia-driver-435 xserver-xorg-video-nvidia-435 nvidia-utils-435 nvidia-kernel-source-435 nvidia-kernel-common-435 nvidia-dkms-435 nvidia-compute-utils-435 libnvidia-ifr1-435 libnvidia-gl-435 libnvidia-fbc1-435 libnvidia-encode-435 libnvidia-decode-435 libnvidia-cfg1-435 libnvidia-compute-435

Using the following additions to ffmpeg-4.1.3/debian/rules:

--enable-nvenc --enable-nvdec --enable-libdrm --enable-vdpau --enable-vaapi --enable-cuvid --enable-ffnvcodec --enable-nonfree

debuild is successful and the "Programs:" section at the beginning contains the lines

Enabled decoders: 
hevc
adpcm_sbpro_2       dfa         hevc_cuvid
adpcm_sbpro_3       dirac           hevc_v4l2m2m
adpcm_sbpro_4       dnxhd           hnm4_video
...
Enabled encoders: 
nvenc           tiff
ffv1            nvenc_h264      truehd
ffvhuff         nvenc_hevc  
...
Enabled hwaccels:
h263_vaapi      mpeg1_vdpau     vc1_vaapi
h264_nvdec      mpeg1_xvmc      vc1_vdpau
h264_vaapi      mpeg2_nvdec     vp8_nvdec
h264_vdpau      mpeg2_vaapi     vp8_vaapi
hevc_nvdec      mpeg2_vdpau     vp9_nvdec
hevc_vaapi      mpeg2_xvmc      vp9_vaapi
hevc_vdpau      mpeg4_nvdec     wmv3_nvdec
mjpeg_nvdec     mpeg4_vaapi     wmv3_vaapi
mjpeg_vaapi     mpeg4_vdpau     wmv3_vdpau
mpeg1_nvdec     vc1_nvdec

dpkg -i *.deb only has the expected errors:

Errors were encountered while processing:
 libavcodec58_4.1.3-0ubuntu1_amd64.deb
 libavfilter7_4.1.3-0ubuntu1_amd64.deb

Because of rm libav{filter,codec}-extra*.

the resulting ffmpeg apparently has all the hardware accelerators:

$ ffmpeg -hide_banner -hwaccels
Hardware acceleration methods:
vdpau
cuda
vaapi
drm
cuvid

But that is a lie:

$ ffmpeg -hide_banner -encoders | grep nv
<no output> 
$ ffmpeg -hide_banner -encoders | grep cuvid
<no output>

OBS agrees that this ffmpeg doesn't have any hardware accelerated capabilities.

As another piece of evidence, ffmpeg's banner includes build flags such as --enable-nvenc, etc, but also includes this line, which might be related to the problem?

WARNING: library configuration mismatch

Some sources say that this message is due to mismatched versions of libraries upon which ffmpeg relies.

I haven't built any of those from source or checked their versions, but they're all only from the official repositories, not PPAs, I'm quite sure.

Do you have any pointers for me?

omen23 commented 4 years ago

FFmpeg will automatically detect the ffnvcodec-headers — extract from ./configure --help:

  The following libraries provide various hardware acceleration features:
  --disable-amf            disable AMF video encoding code [autodetect]
  --disable-audiotoolbox   disable Apple AudioToolbox code [autodetect]
  --enable-cuda-sdk        enable CUDA features that require the CUDA SDK [no]
  --disable-cuvid          disable Nvidia CUVID support [autodetect]
  --disable-d3d11va        disable Microsoft Direct3D 11 video acceleration code [autodetect]
  --disable-dxva2          disable Microsoft DirectX 9 video acceleration code [autodetect]
  --disable-ffnvcodec      disable dynamically linked Nvidia code [autodetect]
  --enable-libdrm          enable DRM code (Linux) [no]
  --enable-libmfx          enable Intel MediaSDK (AKA Quick Sync Video) code via libmfx [no]
  --enable-libnpp          enable Nvidia Performance Primitives-based code [no]
  --enable-mmal            enable Broadcom Multi-Media Abstraction Layer (Raspberry Pi) via MMAL [no]
  --disable-nvdec          disable Nvidia video decoding acceleration (via hwaccel) [autodetect]
  --disable-nvenc          disable Nvidia video encoding code [autodetect]
  --enable-omx             enable OpenMAX IL code [no]
  --enable-omx-rpi         enable OpenMAX IL code for Raspberry Pi [no]
  --enable-rkmpp           enable Rockchip Media Process Platform code [no]
  --disable-v4l2-m2m       disable V4L2 mem2mem code [autodetect]
  --disable-vaapi          disable Video Acceleration API (mainly Unix/Intel) code [autodetect]
  --disable-vdpau          disable Nvidia Video Decode and Presentation API for Unix code [autodetect]
  --disable-videotoolbox   disable VideoToolbox code [autodetect]

Before you sudo dpkg -i *.deb – you should rm -f libavcodec-extra* libavfilter-extra*

omen23 commented 4 years ago

I just installed nvidia-driver-435 and before pulled the new ffnvcodec headers (and installed them), updated the manual and rebootet but did not build ffmpeg anew and its all working...

$ ffmpeg -decoders | grep cuvid
 V..... h264_cuvid           Nvidia CUVID H264 decoder (codec h264)
 V..... hevc_cuvid           Nvidia CUVID HEVC decoder (codec hevc)
 V..... mjpeg_cuvid          Nvidia CUVID MJPEG decoder (codec mjpeg)
 V..... mpeg1_cuvid          Nvidia CUVID MPEG1VIDEO decoder (codec mpeg1video)
 V..... mpeg2_cuvid          Nvidia CUVID MPEG2VIDEO decoder (codec mpeg2video)
 V..... mpeg4_cuvid          Nvidia CUVID MPEG4 decoder (codec mpeg4)
 V..... vc1_cuvid            Nvidia CUVID VC1 decoder (codec vc1)
 V..... vp8_cuvid            Nvidia CUVID VP8 decoder (codec vp8)
 V..... vp9_cuvid            Nvidia CUVID VP9 decoder (codec vp9)
$ ffmpeg -encoders | grep nvenc*
 V..... h264_nvenc           NVIDIA NVENC H.264 encoder (codec h264)
 V..... nvenc                NVIDIA NVENC H.264 encoder (codec h264)
 V..... nvenc_h264           NVIDIA NVENC H.264 encoder (codec h264)
 V..... nvenc_hevc           NVIDIA NVENC hevc encoder (codec hevc)
 V..... hevc_nvenc           NVIDIA NVENC hevc encoder (codec hevc)
$ obs
...
info: ---------------------------------
warning: Failed to load 'en-US' text for module: 'decklink-ouput-ui.so'
libDeckLinkAPI.so: cannot open shared object file: No such file or directory
info: No blackmagic support
info: NVENC supported
info: FFMPEG VAAPI supported
info: VLC found, VLC video source enabled
info: ---------------------------------
info:   Loaded Modules:
info:     libobs-linuxbrowser.so
info:     vlc-video.so
info:     text-freetype2.so
info:     rtmp-services.so
info:     obs-x264.so
info:     obs-transitions.so
info:     obs-outputs.so
info:     obs-libfdk.so
info:     obs-filters.so
info:     obs-ffmpeg.so
info:     linux-v4l2.so
info:     linux-pulseaudio.so
info:     linux-jack.so
info:     linux-decklink.so
info:     linux-capture.so
info:     linux-alsa.so
info:     image-source.so
info:     frontend-tools.so
info:     decklink-ouput-ui.so
info: ---------------------------------
info: ==== Startup complete ===============================================
...

mpv ....mp4
Playing: ....mp4
 (+) Video --vid=1 (*) (h264 1280x532 30.000fps)
 (+) Audio --aid=1 (*) (aac 2ch 48000Hz)
Using hardware decoding (nvdec).
AO: [pulse] 48000Hz stereo 2ch float
VO: [gpu] 1280x532 cuda[nv12]
AV: 00:00:04 / 02:37:00 (0%) A-V:  0.000

Exiting... (Quit)
omen23 commented 4 years ago

You only need --enable-nonfree in your rules for --libfdk-aac

catb0t commented 4 years ago

The error from libavcodec / libavfilter actually happens regardless of whether they were removed. So something else is going on but I don't know what

$ rm libavcodec-extra* libavfilter-extra*

removed 'libavcodec-extra_4.1.3-0ubuntu1_all.deb'
removed 'libavcodec-extra58_4.1.3-0ubuntu1_amd64.deb'
removed 'libavcodec-extra58-dbgsym_4.1.3-0ubuntu1_amd64.ddeb'
removed 'libavfilter-extra_4.1.3-0ubuntu1_all.deb'
removed 'libavfilter-extra7_4.1.3-0ubuntu1_amd64.deb'
removed 'libavfilter-extra7-dbgsym_4.1.3-0ubuntu1_amd64.ddeb'

$ sudo dpkg -i *.deb
(Reading database ... 490489 files and directories currently installed.)
Preparing to unpack ffmpeg_4.1.3-0ubuntu1_amd64.deb ...
Unpacking ffmpeg (7:4.1.3-0ubuntu1) over (7:4.1.3-0ubuntu1) ...
Preparing to unpack ffmpeg-doc_4.1.3-0ubuntu1_all.deb ...
Unpacking ffmpeg-doc (7:4.1.3-0ubuntu1) over (7:4.1.3-0ubuntu1) ...
dpkg: regarding libavcodec58_4.1.3-0ubuntu1_amd64.deb containing libavcodec58:amd64:
 libavcodec-extra58:amd64 conflicts with libavcodec58
  libavcodec58:amd64 (version 7:4.1.3-0ubuntu1) is to be installed.

dpkg: error processing archive libavcodec58_4.1.3-0ubuntu1_amd64.deb (--install):
 conflicting packages - not installing libavcodec58:amd64
Preparing to unpack libavcodec-dev_4.1.3-0ubuntu1_amd64.deb ...
Unpacking libavcodec-dev:amd64 (7:4.1.3-0ubuntu1) over (7:4.1.3-0ubuntu1) ...
Preparing to unpack libavdevice58_4.1.3-0ubuntu1_amd64.deb ...
Unpacking libavdevice58:amd64 (7:4.1.3-0ubuntu1) over (7:4.1.3-0ubuntu1) ...
Preparing to unpack libavdevice-dev_4.1.3-0ubuntu1_amd64.deb ...
Unpacking libavdevice-dev:amd64 (7:4.1.3-0ubuntu1) over (7:4.1.3-0ubuntu1) ...
dpkg: regarding libavfilter7_4.1.3-0ubuntu1_amd64.deb containing libavfilter7:amd64:
 libavfilter-extra7:amd64 conflicts with libavfilter7
  libavfilter7:amd64 (version 7:4.1.3-0ubuntu1) is to be installed.

dpkg: error processing archive libavfilter7_4.1.3-0ubuntu1_amd64.deb (--install):
 conflicting packages - not installing libavfilter7:amd64
[...]
Errors were encountered while processing:
 libavcodec58_4.1.3-0ubuntu1_amd64.deb
 libavfilter7_4.1.3-0ubuntu1_amd64.deb
catb0t commented 4 years ago

Taking all of your suggestions, I still get no output from ffmpeg -decoders | grep cuvid etc

omen23 commented 4 years ago

can you post the output of apt-cache policy libavcodec-extra58 and libavfilter-extra7

$ apt-cache policy libavcodec-extra58
libavcodec-extra58:
  Installed: (none)
  Candidate: 7:4.1.3-0ubuntu1
  Version table:
     7:4.1.3-0ubuntu1 500
        500 http://archive.ubuntu.com/ubuntu disco-updates/universe amd64 Packages
        500 http://archive.ubuntu.com/ubuntu disco-security/universe amd64 Packages
     7:4.1.1-1 500
        500 http://archive.ubuntu.com/ubuntu disco/universe amd64 Packages

$ apt-cache policy libavfilter-extra7
libavfilter-extra7:
  Installed: (none)
  Candidate: 7:4.1.3-0ubuntu1
  Version table:
     7:4.1.3-0ubuntu1 500
        500 http://archive.ubuntu.com/ubuntu disco-updates/universe amd64 Packages
        500 http://archive.ubuntu.com/ubuntu disco-security/universe amd64 Packages
     7:4.1.1-1 500
        500 http://archive.ubuntu.com/ubuntu disco/universe amd64 Packages

It seems you have the extra packages installed, or idk. If you do, remove all the packages, and do a clean FFmpeg install.

omen23 commented 4 years ago

sudo apt-get remove ffmpeg ffmpeg-doc libavcodec-dev libavcodec58 libavfilter7 libavfilter-dev libavformat58 libavformat-dev libavresample4 libavresample-dev libavutil-dev libavutil56 libavdevice58 libavdevice-dev libswscale5 libswscale-dev libswresample3 libswresample-dev libpostproc55 libpostproc-dev libavfilter-extra7 libavcodec-extra58 Then pull the newest ffnvcodec-headers from git and install them (make && sudo make install), reboot and then build FFmpeg and you can decide if you wanna use the extra versions or the none extra ones – just not both.

catb0t commented 4 years ago

They were installed; thanks for your help. Btw, --enable-nonfree is also for --enable-libnpp, NIVIDIA's Performance Primitives, and I'm linking against the CUDA SDK.