July 24, 2024

RaspberryPI 4 hardware acceleration 100% working

Finally elucidated how to make this work effectively with latest firmware on ARM64.

I wrote an article some time ago about FFMPEG streaming with hardware acceleration in RaspberryPI4. While the method is still valid and working, there are two drawbacks:

  1. It’s only working for a legacy Raspian operating system which may not have latest bug fixes and features
  2. The hardware codec is only supported for the 32 bits version of this operating system

However, lately on my Raspberry where I had installed this solution I started to have some issues with the stability of the system and after a restart, RaspberryPI OS did no longer boot. I suspect there was something faulty because of this legacy version and I decided to install the newer 64bit version and try to find some way to use hardware acceleration for RTSP streaming.

What I tried

ffmpeg -f v4l2 -i /dev/video0 -c:v libx264 -c:a aac -preset medium -tune zerolatency -b:v 500k -f rtsp rtsp://output-url

The command above is the first one I tried after installing ffmpeg. It works well, but if you check the processor with the top command you can notice the it’s hyped at over 100%. Although the Raspberry seemingly had no stability issue, it’s not elegant and effective to leave it like that, especially if you know the device supports hardware accelerated decoding. So I decided not to give up 😁

To make it work, I tried to look into a lot of articles, Github issues and so forth… Many of them suggested to use some Bellagio library and recompile ffmpeg with OMX support. I managed to recompile, but still I wasn’t able to run any hardware accelerated stream.

What works

Long story short, using the approach above is simply a dead end. The whole idea is that ffmpeg which can be installed from the apt repositories is already compiled with support for the h264_v4l2m2m codec, which works with hardware acceleration out of the box! You just need to amend the ffmpeg line a bit:

fmpeg -input_format yuv420p -f v4l2 -framerate 30 -video_size 1920x1080 -i /dev/video0 -c:v h264_v4l2m2m -r 30  -f rtsp rtsp://output-url

This way, the processor usage stays low benefiting from the acceleration. You can see in the screenshot below the CPU usage for an ffmpeg process which is already running for some time.

Conclusion

Some solutions to different problems are in fact simpler than expected. What I learned is that whenever something gives you headaches and every possible way leads to a dead end, the trick is to unplug and try to view the situation from a broader holistic perspective. The approach may be doomed from the start so there is no reason to ramble around for a solution.

In this case, the solution was to use a different codec which has this hardware acceleration supported. The tricky part is that it’s not straightforward and the pieces you may find on Internet are vague. This made the investigation quite a hassle, but the satisfaction is worth it. So cheer up, there is a solution for those interested in this hardware streaming subject and I hope it works for you 🥳

Thanks for reading, I hope you found this article useful and interesting. If you have any suggestions don’t hesitate to contact me. If you found my content useful please consider a small donation. Any support is greatly appreciated! Cheers  😉

Hi there 👋
It’s nice to meet you.

Sign up to receive useful content in your inbox, every month.

Spam is a waste of time, I prefer something creative! Read the privacy policy for more info.

afivan

Enthusiast adventurer, software developer with a high sense of creativity, discipline and achievement. I like to travel, I like music and outdoor sports. Because I have a broken ligament, I prefer safer activities like running or biking. In a couple of years, my ambition is to become a good technical lead with entrepreneurial mindset. From a personal point of view, I’d like to establish my own family, so I’ll have lots of things to do, there’s never time to get bored 😂

View all posts by afivan →