OrangePI: H264 HW encoding with ffmpeg

Last days I’ve been experimenting with various features of OPI. One thing I’ve always been interested in is HW encoding. There is some information on linux sunxi wiki on this topic ([1] [2]), which say that it’s not something straightforward and opensource.

Fortunately, there is one page which shortly mentions ffmpeg implementation which interested me. In this repo I’ve found another fork which states compatibility with Allwinner H3. After compilation, I found it works.

Next I wanted to port it to recent ffmpeg but the first repo was just commited files without proper git history. So I needed to find out, what is changed compared to mainline ffmpeg. For this I needed to find out the version it was forked from. The compiled binary from that repo showed “N-65839-g156858b” but commit 156858b doesn’t exist in ffmpeg main tree. So I started to traverse back from master by following command:

$ git checkout HEAD~1 && git describe --tags --match N
Previous HEAD position was 948b54763b... avcodec/lagarith: Fix runtime error: left shift of 1 by 31 places cannot be represented in type 'int'
HEAD is now at 620b452a11... avcodec/aacdec_fixed: Fix multiple runtime error: shift exponent 127 is too large for 32-bit type 'int'
N-86239-g620b452a11

Until I found this version:

HEAD is now at 8495c6086d... vidstabutils: improve documentation
N-65837-g8495c6086d

Diff showed only some differences – this is the base version:

$ diff -r -x *.pc -x .git FFmpeg FFmpeg-Cedrus
Only in FFmpeg: .config
Only in FFmpeg: config.asm
Only in FFmpeg: config.fate
Only in FFmpeg: config.h
Only in FFmpeg: config.log
Only in FFmpeg: config.mak
Only in FFmpeg-Cedrus: description-pak
Only in FFmpeg/doc: config.texi
Only in FFmpeg-Cedrus: doc-pak
Only in FFmpeg-Cedrus: ffmpeg_2-1_armhf.deb
Only in FFmpeg: .gitignore
diff -r -x '*.pc' -x .git FFmpeg/libavcodec/allcodecs.c FFmpeg-Cedrus/libavcodec/allcodecs.c
533a534
> REGISTER_ENCODER(CEDRUS264, cedrus264);
Only in FFmpeg-Cedrus/libavcodec/arm/sunxi: h264enc.c
Only in FFmpeg-Cedrus/libavcodec/arm/sunxi: h264enc.h
Only in FFmpeg-Cedrus/libavcodec/arm/sunxi: ve.c
Only in FFmpeg-Cedrus/libavcodec/arm/sunxi: ve.c-ORG
Only in FFmpeg-Cedrus/libavcodec/arm/sunxi: ve.h
Only in FFmpeg-Cedrus/libavcodec/arm/sunxi: ve.h-ORG
Only in FFmpeg-Cedrus/libavcodec: cedrus264.c
Only in FFmpeg-Cedrus/libavcodec: cedrus264.c-ORG
diff -r -x '*.pc' -x .git FFmpeg/libavcodec/Makefile FFmpeg-Cedrus/libavcodec/Makefile
765a766
> OBJS-$(CONFIG_CEDRUS264_ENCODER) += cedrus264.o arm/sunxi/ve.o
Only in FFmpeg/libavutil: avconfig.h
Only in FFmpeg-Cedrus: README2

I copied relevant files which were only in FFmpeg-Cedrus, updated different files and created a new branch cedrus264 in mainline ffmpeg. Then I needed to apply patches from second repo to make it work on H3. After compilation, I’ve had working binary from mainline sources. Then I rebased that branch to recent ffmpeg, fixed some issue and the result is here:I tested it on my OPI and OPI Zero and it works on both. Now I’m considering to buy some CSI camera to test real world encoding performance.

You can get updated sources in my repo: https://github.com/danielkucera/FFmpeg/tree/cedrus264 (Don’t forget to switch to branch cedrus264).

Looking forward to your comments.

21 thoughts on “OrangePI: H264 HW encoding with ffmpeg”

  1. Do You have use with lvk373a for interface encoding vídeo and áudio for YouTube ir Facebook live?

  2. 640×480 at 19 FPS does not really look too fast. Seems it not useful for real time encoding ;(

  3. konecne nejaky progress pre hw akceleraciu pre tie sproste proprietary chipy… len sa smejem kam takym sposobom dosli intel, amd, arm, … vsetci su vulnerable to meltdown/spectre… zatialco risc-v, co je open-source, sa na tom vsetkom moze dobre zabavat…

  4. Hi,
    I bought a bpi-zero and I’d like to use it to transcode my LKV373A 3.0 (still thanks for awesome work about it) using hw acceleration. I compiled from your source cedrus264 branch but when I try to use it I receive this error:
    cedrus264 @ 0x14c0270] VE Open error.
    Error initializing output stream 0:0 — Error while opening encoder for output stream #0:0 – maybe incorrect parameters such as bit_rate, rate, width or height
    It’s like it wants to start a custom *video engine* but systematically fails.
    Any advice? maybe bpi-zero hw is not suitable?

    Thanks.

    1. simple test command I’m trying is
      sudo ./ffmpeg -i “udp://239.255.42.42:5004” -pix_fmt nv12 -c:v cedrus264 -acodec libmp3lame -ar 44100 -rtmp_live live -f null –

              1. Which one? I tried someone but /dev/cedar_dev device doesn’t exist, and FFmpeg encoding with cedrus264 doesn’t work accordingly.

  5. Hi, thank you for your guide.
    I made success to build ffmpeg with cedrus264 enalbed on my orangepi plus(AP is H3).
    But when I tried to test but failed with below error. Could you help me to correct it?

    I am doing on Armbian-bionic with Kernel 4.14.70. Here is the result of uname -a

    Linux orangepiplus 4.14.70-sunxi #265 SMP Wed Sep 19 10:01:19 CEST 2018 armv7l armv7l armv7l GNU/Linux

    VE Open error.
    Error initializing output stream 0:0 — Error while opening encoder for output stream #0:0 – maybe incorrect parameters such as bit_rate, rate, width or height

      1. Have you any success? I am trying to use the latest armbian, re-compiled ffmpeg with cedrus, but without /dev/cedar_dev it doesn’t work. I tried to compile and install all cedar kernel modules I found in the internet, but with no luck. Have you any recipe to use a hardware encoder?

  6. Hi are you available to get this working as paid consultant for nano pi neo2. I posted this on upwork.
    We have an Nano Pi NEO2 https://wiki.friendlyelec.com/wiki/index.php/NanoPi_NEO2 and need ffmpeg to work with GPU decode and GPU Encode. You will be build ffmpeg so that hardware decode and encode work with mpeg2 udp video input and udp output h.264 video. We will supply test streams and hardware via remote access. You can start with mpeg2 test file.

    You will select the version of linux we use, kernel, and version of ffmpeg.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.