sine
Posts: 14
Joined: Tue Nov 26, 2019 8:13 am

how to compile ffmpeg with hardware acceleration support

Wed May 20, 2020 7:23 am

hello
i am trying to do some live video streaming(h264) transcoding on RPI 4b , could not figure out how to enable ffmpeg hardware accelerated encoding/decoding , is there a tutorial for that ?
which to choose among h264_mmal  & h264_v4l2m2m ?

thank you

gkreidl
Posts: 6349
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: how to compile ffmpeg with hardware acceleration support

Wed May 20, 2020 8:21 am

The version from the (Foundation) repository has it all built in.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

User avatar
Ram.Sh
Posts: 145
Joined: Wed Oct 26, 2016 9:14 pm

Re: how to compile ffmpeg with hardware acceleration support

Wed May 20, 2020 11:07 am

gkreidl wrote: The version from the (Foundation) repository has it all built in.
I have the same need for an updated ffmpeg version, I was in contact with the ffmpeg listing group following an error when running ffmpeg on my RPI4 Buster edition, this is the reply I got:
> ffmpeg version 4.1

This is more than eighteen months old.
I don't know if this makes any difference for your problem but it is certainly a strong reason not to spend too much time on it.

Carl Eugen
i tried without any success to compile ffmpeg on the pi using different scripts which all failed in some point of the process, I'd be more than grateful for a working ffmpeg installation script for the PI if anyone has one.

thank you all in advance!

User avatar
Ram.Sh
Posts: 145
Joined: Wed Oct 26, 2016 9:14 pm

Re: how to compile ffmpeg with hardware acceleration support

Wed May 20, 2020 12:24 pm

following this tutorial i have successfully updated ffmpeg to the version:

Code: Select all

ffmpeg version git-2020-05-20-86822cf Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 8 (Raspbian 8.3.0-6+rpi1)
  configuration: --extra-cflags=-I/usr/local/include --extra-ldflags=-L/usr/local/lib --extra-libs='-lpthread -lm' --arch=armel --enable-gmp --enable-gpl --enable-libaom --enable-libass --enable-libdav1d --enable-libdrm --enable-libfdk-aac --enable-libfreetype --enable-libkvazaar --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr --enable-libssh --enable-libvorbis --enable-libvpx --enable-libzimg --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-mmal --enable-nonfree --enable-omx --enable-omx-rpi --enable-version3 --target-os=linux --enable-pthreads --enable-openssl --enable-hardcoded-tables
  libavutil      56. 46.100 / 56. 46.100
  libavcodec     58. 86.100 / 58. 86.100
  libavformat    58. 43.100 / 58. 43.100
  libavdevice    58.  9.103 / 58.  9.103
  libavfilter     7. 82.100 /  7. 82.100
  libswscale      5.  6.101 /  5.  6.101
  libswresample   3.  6.100 /  3.  6.100
  libpostproc    55.  6.100 / 55.  6.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
would it be possible to copy and share the compiled ffmpeg from one PI to another without repeating the process?

gkreidl
Posts: 6349
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: how to compile ffmpeg with hardware acceleration support

Wed May 20, 2020 1:43 pm

I' quite sure that this version is missing the rpi H265 hardware decoder.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

pboh
Posts: 13
Joined: Wed Oct 31, 2018 2:26 pm

Re: how to compile ffmpeg with hardware acceleration support

Fri May 22, 2020 12:02 am

Have had a lot of back and forth trying to figure out which of a few similar threads is best suited for my question, and maybe this is the winner :)

I'm trying to compile FFMPEG-4.2.2 from @popcornmix's gbm repo at: https://github.com/popcornmix/xbmc/tree ... get/ffmpeg, and have had two problems, one of which I've resolved but want to be sure I've done it correctly. For added context, I'm using an RPI4B 4Gb with a freshly installed and updated (to kernel 5.4.40-v8+) Raspbian.

My intention has been to use the @popcornmix patched ffmpeg 4.2.2 version in conjunction with Kodi 19, so I have merged the current versions of the gbm branch from @popcornmix and the master from xbmc so that I inherit the fixed up CMakeList files and patches. However, I've also attempted to compile ffmpeg myself completely outside of the kodi build process and have had the exact same experience. If anyone has any ideas, I'd really appreciate a nudge in the right direction!

Issue 1 [Resolved]: compiler complains it can't find an include. Fixed by creating a symlink 'drm' in /usr/include/libdrm. (just not sure if I was supposed to?)

Code: Select all

CC      libavutil/md5.o
src/libavutil/hwcontext_drm.c:24:10: fatal error: drm/drm_fourcc.h: No such file
 or directory
 #include <drm/drm_fourcc.h>
Issue 2: have seen this or similar reported in other threads on this and other sites but can't triangulate on the solution. When compiling, make process errors out at a linker step. I only included a snippet because the rest is largely indicative of what's pasted there. Happy to paste more.

Code: Select all

LD	ffmpeg_g
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_uncached: error
: undefined reference to 'vcsm_malloc_cache'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_uncached: error
: undefined reference to 'vcsm_vc_hdl_from_hdl'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_uncached: error
: undefined reference to 'vcsm_lock'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_uncached: error
: undefined reference to 'vcsm_vc_addr_from_hdl'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_uncached: error
: undefined reference to 'vcsm_unlock_ptr'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_uncached: error
: undefined reference to 'vcsm_free'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_cached: error: 
undefined reference to 'vcsm_malloc_cache'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_cached: error: 
undefined reference to 'vcsm_vc_hdl_from_hdl'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_cached: error: 
undefined reference to 'vcsm_lock'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_cached: error: 
undefined reference to 'vcsm_vc_addr_from_hdl'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_cached: error: 
undefined reference to 'vcsm_unlock_ptr'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_cached: error: 
undefined reference to 'vcsm_free'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_free: error: undefined
 reference to 'vcsm_unlock_ptr'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_free: error: undefined
 reference to 'vcsm_free'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function rpi_mem_gpu_uninit: error:
 undefined reference to 'vcsm_exit'
I've tried copying bcm_host.pc, mmal.pc, and vcsm.pc from their initial homes in /opt to a pkgconfig directory and specifying it via PKG_CONFIG_PATH. I've also renamed the /opt/vc/include/{EGL|GLES2|GLES} directories to contain a hyphen per one of the suggestions in another thread. No joy either way. I feel like I'm monkeying around too much with the make opts and I'm finding myself out of rabbit holes to go down. I'm guessing I'm missing something w.r.t. the relationship between the new kernel, how or where it provides the graphics drivers, and what the patched ffmpeg code is looking for. Apologies ahead of time if this should be posted elsewhere.

I did include one of my current attempts at the kodi version of make options (https://pastebin.com/bUfwVRJrwhich is an amalgamation of all the hard work @Rascas and folks have made available, but again, have tried to install/compile ffmpeg by itself without kodi even being on the system (using the ffmpeg compile-time flags stolen from the scripts in the kodi directory.

Thanks so much for any direction!

dorian-ny
Posts: 66
Joined: Fri Nov 01, 2019 11:29 pm

Re: how to compile ffmpeg with hardware acceleration support

Thu May 28, 2020 10:28 pm

The c files have nothing to do with the linker.
There are 2 possibilities. Either the library its trying to link to is not in the search path. Can be added by putting the path in $LD_LIBRARY_PATH
or adding a file with it to /etc/ld.so.conf.d/
The second is that its trying to link to a library that is missing that function. In which case you either have to recompile that library with the function or disable that call in the code

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9276
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: how to compile ffmpeg with hardware acceleration support

Fri May 29, 2020 11:32 am

The library you're after is normally in /opt/vc/lib/libvcsm.so
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

pboh
Posts: 13
Joined: Wed Oct 31, 2018 2:26 pm

Re: how to compile ffmpeg with hardware acceleration support

Mon Jun 01, 2020 4:00 pm

Thank you both! This was the guidance I needed. All sorted and up and running now.

sine
Posts: 14
Joined: Tue Nov 26, 2019 8:13 am

Re: how to compile ffmpeg with hardware acceleration support

Thu Jun 04, 2020 1:53 am

pboh wrote:
Mon Jun 01, 2020 4:00 pm
Thank you both! This was the guidance I needed. All sorted and up and running now.

hello @pboh
is it performing good ?
i need to run 2 ffmpeg instances at same time for a long running task(video transcoding/re-streaming), do you think RPI 4b with 4gb memory could handle this scenario , could you share the cpu/gpu/temperature status while running ffmpeg?
i don't have a RPI 4b yet , but i am thinking of getting one for my job.

thank you

pboh
Posts: 13
Joined: Wed Oct 31, 2018 2:26 pm

Re: how to compile ffmpeg with hardware acceleration support

Thu Jun 04, 2020 3:25 pm

sine wrote:
Thu Jun 04, 2020 1:53 am
pboh wrote:
Mon Jun 01, 2020 4:00 pm
Thank you both! This was the guidance I needed. All sorted and up and running now.

hello @pboh
is it performing good ?
i need to run 2 ffmpeg instances at same time for a long running task(video transcoding/re-streaming), do you think RPI 4b with 4gb memory could handle this scenario , could you share the cpu/gpu/temperature status while running ffmpeg?
i don't have a RPI 4b yet , but i am thinking of getting one for my job.

thank you
Heya - sorry, but I can't yet advise. I should have been clear that the guidance above both simplified and helped me complete the compile process with executables that run (both popcornmix's gbm branch and Rascas' leia-pi4 builds), but I can't yet play video with the kms driver loaded. Because it's only Kodi I've been using as my barometer I've been hesitant to add more details to this thread since the subject was contained to ffmpeg. But generally speaking, with KMS on, the newest kernel, and kodi/ffmpeg compiled with the libraries recommended above and mentioned in other threads, I get a black screen in Kodi but audio works just fine. The codec in use shows as "mmal-<Xxx>' (depending). With FKMS, I get ffmpeg-h264 or whatever. Still combing other threads to try and chase the rabbit. If there's an ffmpeg specific test you'd like me to run I'm happy to do it, but I've used it only as a black box that's part of Kodi :)

sine
Posts: 14
Joined: Tue Nov 26, 2019 8:13 am

Re: how to compile ffmpeg with hardware acceleration support

Fri Jun 05, 2020 7:01 am

pboh wrote:
Thu Jun 04, 2020 3:25 pm
sine wrote:
Thu Jun 04, 2020 1:53 am
pboh wrote:
Mon Jun 01, 2020 4:00 pm
Thank you both! This was the guidance I needed. All sorted and up and running now.

hello @pboh
is it performing good ?
i need to run 2 ffmpeg instances at same time for a long running task(video transcoding/re-streaming), do you think RPI 4b with 4gb memory could handle this scenario , could you share the cpu/gpu/temperature status while running ffmpeg?
i don't have a RPI 4b yet , but i am thinking of getting one for my job.

thank you
Heya - sorry, but I can't yet advise. I should have been clear that the guidance above both simplified and helped me complete the compile process with executables that run (both popcornmix's gbm branch and Rascas' leia-pi4 builds), but I can't yet play video with the kms driver loaded. Because it's only Kodi I've been using as my barometer I've been hesitant to add more details to this thread since the subject was contained to ffmpeg. But generally speaking, with KMS on, the newest kernel, and kodi/ffmpeg compiled with the libraries recommended above and mentioned in other threads, I get a black screen in Kodi but audio works just fine. The codec in use shows as "mmal-<Xxx>' (depending). With FKMS, I get ffmpeg-h264 or whatever. Still combing other threads to try and chase the rabbit. If there's an ffmpeg specific test you'd like me to run I'm happy to do it, but I've used it only as a black box that's part of Kodi :)

@pboh thank you

my use case requires 2 ffmpeg instances(hw accelerated preferably) to work together , and it has to be running 7x 24 ,
both will be doing some heavy-lifting work simultaneously,wondering if Rpi 4b 4gb/8gb could handle this scenario ,
otherwise i will have to wait for Rpi 4b+ or maybe Rpi 5 , or seeking other alternatives.

it would be a huge help to me if you could kindly run a test on your machine and share the results .
could you please run 2 ffmpeg instances simultaneously for a while (30 mins),

one for re-streaming: ./ffmpeg -re -i "udp://127.0.0.1:1234?fifo_size=1000000&overrun_nonfatal=1" -c:v libx264 -crf 34 -c:a aac -b:v 900k -b:a 64k -maxrate 1M -bufsize 2M -flush_packets 0 -f mpegts "udp://127.0.0.1:1235?pkt_size=1316&buffer_size=65535"

another one for transcoding live video stream : ./ffmpeg -re -i rtmp://202.69.69.180:443/webcast/bshdlive-pc -c:v libx264 -c:a aac -crf 34 -b:v 900k -b:a 64k -maxrate 1M -bufsize 2M -flush_packets 0 -f mpegts "udp://127.0.0.1:1234?pkt_size=1316&buffer_size=65535"

please replace "libx264" of commands above with an enabled hw accelerated codec , i am mainly concerned about the hardware capabilities of Rpi 4b, e.g. memory/cpu/gpu usage , and core temperature .

thank you so much

User avatar
graysky
Posts: 101
Joined: Fri Apr 05, 2013 11:43 am
Location: /run/user/1000
Contact: Website

Re: how to compile ffmpeg with hardware acceleration support

Mon Jul 20, 2020 6:22 pm

pboh wrote:
Mon Jun 01, 2020 4:00 pm
Thank you both! This was the guidance I needed. All sorted and up and running now.
I am getting very similar errors building the popcornmix/gbm kodi branch. Wondering if you could elaborate a bit on what you did to fix it?

Code: Select all

...
AR    libavutil/libavutil.a
AR    libavformat/libavformat.a
AR    libavcodec/libavcodec.a
LD    ffmpeg_g
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_uncached: error: undefined reference to 'vcsm_malloc_cache'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_uncached: error: undefined reference to 'vcsm_vc_hdl_from_hdl'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_uncached: error: undefined reference to 'vcsm_lock'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_uncached: error: undefined reference to 'vcsm_vc_addr_from_hdl'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_uncached: error: undefined reference to 'vcsm_unlock_ptr'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_uncached: error: undefined reference to 'vcsm_free'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_cached: error: undefined reference to 'vcsm_malloc_cache'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_cached: error: undefined reference to 'vcsm_vc_hdl_from_hdl'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_cached: error: undefined reference to 'vcsm_lock'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_cached: error: undefined reference to 'vcsm_vc_addr_from_hdl'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_cached: error: undefined reference to 'vcsm_unlock_ptr'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_malloc_cached: error: undefined reference to 'vcsm_free'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_free: error: undefined reference to 'vcsm_unlock_ptr'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function gpu_free: error: undefined reference to 'vcsm_free'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function rpi_mem_gpu_uninit: error: undefined reference to 'vcsm_exit'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function rpi_mem_gpu_init: error: undefined reference to 'vcsm_init_ex'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function rpi_mem_gpu_init: error: undefined reference to 'vcsm_init_ex'
libavcodec/libavcodec.a(rpi_mem.o):rpi_mem.c:function rpi_cache_flush_execute: error: undefined reference to 'vcsm_clean_invalid2'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function gpu_init.constprop.0: error: undefined reference to 'vcsm_init'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function gpu_init.constprop.0: error: undefined reference to 'vcsm_malloc_cache'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function gpu_init.constprop.0: error: undefined reference to 'vcsm_vc_hdl_from_hdl'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function gpu_init.constprop.0: error: undefined reference to 'vcsm_lock'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function gpu_init.constprop.0: error: undefined reference to 'vcsm_vc_addr_from_hdl'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function gpu_init.constprop.0: error: undefined reference to 'vcsm_malloc_cache'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function gpu_init.constprop.0: error: undefined reference to 'vcsm_vc_hdl_from_hdl'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function gpu_init.constprop.0: error: undefined reference to 'vcsm_lock'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function gpu_init.constprop.0: error: undefined reference to 'vcsm_vc_addr_from_hdl'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function gpu_init.constprop.0: error: undefined reference to 'vcsm_unlock_ptr'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function gpu_init.constprop.0: error: undefined reference to 'vcsm_free'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function gpu_unref: error: undefined reference to 'vcsm_exit'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function vpu_qpu_wait: error: undefined reference to 'vcsm_exit'
libavcodec/libavcodec.a(rpi_qpu.o):rpi_qpu.c:function vpu_qpu_term: error: undefined reference to 'vcsm_exit'
libavcodec/libavcodec.a(rpi_zc.o):rpi_zc.c:function zc_pool_delete_v: error: undefined reference to 'vcsm_unlock_hdl'
libavcodec/libavcodec.a(rpi_zc.o):rpi_zc.c:function zc_pool_free_v: error: undefined reference to 'vcsm_unlock_hdl'
libavcodec/libavcodec.a(rpi_zc.o):rpi_zc.c:function zc_pool_buf_alloc: error: undefined reference to 'vcsm_unlock_hdl'
libavcodec/libavcodec.a(rpi_zc.o):rpi_zc.c:function zc_pool_buf_alloc: error: undefined reference to 'vcsm_unlock_hdl'
collect2: error: ld returned 1 exit status
distcc[31695] ERROR: compile (null) on localhost failed
make[6]: *** [/build/kodi-rbp-git/src/kodi-build/build/ffmpeg/src/ffmpeg/Makefile:114: ffmpeg_g] Error 1
make[5]: *** [CMakeFiles/ffmpeg.dir/build.make:132: ffmpeg-prefix/src/ffmpeg-stamp/ffmpeg-build] Error 2
make[4]: *** [CMakeFiles/Makefile2:95: CMakeFiles/ffmpeg.dir/all] Error 2
make[3]: *** [Makefile:149: all] Error 2
make[2]: *** [CMakeFiles/ffmpeg.dir/build.make:134: build/ffmpeg/src/ffmpeg-stamp/ffmpeg-build] Error 2
make[1]: *** [CMakeFiles/Makefile2:4534: CMakeFiles/ffmpeg.dir/all] Error 2
make: *** [Makefile:160: all] Error 2
Complete build log: https://gist.github.com/graysky2/ad280b ... c7661559e6

Return to “Graphics, sound and multimedia”