Cross-Compiling FFmpeg for Rockchip

FFmpeg Rockchip encapsulates the hardware-accelerated video codec library MPP from Rockchip, allowing for easier use of Rockchip’s hardware-accelerated video encoding and decoding capabilities through FFmpeg’s command line and API. FFmpeg Rockchip depends on Rockchip’s MPP and librga libraries, so we first need to cross-compile the MPP and librga libraries.

  • Compilation Environment

The compilation system is Fedora 43, and the cross-compilation toolchain uses the system’s built-in packages aarch64-linux-gnu-gcc and clang. The compilation uses sysroot, which utilizes the arm64 sysroot from the WebRTC source library. The method to obtain it is:

cd webrtc-checkout/src/build/linux/sysroot_scripts./install-sysroot.py --arch arm64 

It will automatically download the sysroot. Of course, the network must be accessible; otherwise, it cannot be downloaded.

  • MPP Compilation

Download the official source code:

git clone https://github.com/rockchip-linux/mpp.git

Modify build/linux/aarch64/arm.linux.cross.cmake to add sysroot

SET(sysroot webrtc-checkout/src/build/linux/debian_bullseye_arm64-sysroot)                                                                                                                     set(CMAKE_SYSROOT ${sysroot})

Modify CMakeLists.txt to add the include directory of sysroot

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -DNDEBUG -I${sysroot}/usr/include/aarch64-linux-gnu")                                                                                                          set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -DNDEBUG -I${sysroot}/usr/include/aarch64-linux-gnu") 

Then compile and install to the specified path.

  • librga Compilation

Download the community-maintained source code

git clone -b jellyfin-rga --depth=1 https://github.com/nyanmisaka/rk-mirrors.git rkrga

Initially, I compiled using the gcc toolchain, but there were always linking errors with the compiled libraries. I couldn’t find the cause of the errors, but switching to clang resolved the linking issues. Modify toolchains/toolchain_linux.cmake to add sysroot and change to clang compilation

SET(sysroot webrtc-checkout/src/build/linux/debian_bullseye_arm64-sysroot)                                                                                                                     set(CMAKE_SYSROOT ${sysroot})                                                                                                                                                                                                                                                                                                                                                               SET(CMAKE_C_COMPILER /usr/bin/clang)                                                                                                                                                                           SET(CMAKE_CXX_COMPILER /usr/bin/clang)                                                                                                                                                                         SET(CMAKE_SYSTEM_PROCESSOR "armv8-a")                                                                                                                                                                                                                                                                                                                                                                                        set(triple aarch64-linux-gnu)                                                                                                                                                                                  set(CMAKE_C_COMPILER_TARGET ${triple})                                                                                                                                                                         set(CMAKE_CXX_COMPILER_TARGET ${triple})   

Modify CMakeLists.txt to add the include directory of sysroot

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -pthread -I${sysroot}/usr/include/aarch64-linux-gnu -I${sysroot}/usr/include/c++/10  -I${sysroot}/usr/include/aarch64-linux-gnu/c++/10")                set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -pthread -lm -Wno-unused-command-line-argument -I${sysroot}/usr/include/aarch64-linux-gnu")  

Then compile and install to the specified path.

  • FFmpeg Rockchip Compilation

Download the source code

git clone --depth=1 https://github.com/nyanmisaka/ffmpeg-rockchip.git ffmpeg

Compilation configuration

SYSROOT=webrtc-checkout/src/build/linux/debian_bullseye_arm64-sysroot TARGET=aarch64-linux-gnu
pkg_config=$(which pkg-config) PKG_CONFIG_PATH=/path/to/mpp/lib/pkgconfig 
./configure --prefix=/home/lisi/work/rock/sysroot/usr --enable-cross-compile --cross-prefix=aarch64-linux-gnu- 
--arch=aarch64 --target-os=linux --sysroot=$SYSROOT --extra-cflags="-I$SYSROOT/usr/include/$TARGET -I$SYSROOT/usr/include/libdrm" 
--extra-ldflags="-L$SYSROOT/usr/lib/$TARGET" --enable-gpl --enable-version3 --enable-libdrm --enable-rkmpp --enable-rkrga 
--disable-static --enable-shared

<span>Note that PKG_CONFIG_PATH should be configured to the installation location of the mpp and librga compiled libraries. After installing librga, there is no pc file, so I wrote a librga.pc file similar to mpp's pc file.</span>

exec_prefix=${prefix}                                                                                                                                                                                           libdir=${prefix}/lib                                                                                                                                                                                            includedir=${prefix}/include                                                                                                                                                                                    Name: librga                                                                                                                                                                                                    Description: librga                                                                                                                                                                                             Requires.private:                                                                                                                                                                                               Version: 1.0                                                                                                                                                                                                    Libs: -L${libdir} -lrga -lstdc++                                                                                                                                                                                Libs.private:                                                                                                                                                                                                   Cflags: -I${includedir}

After configuring, you can compile and install<span> FFmpeg Rockchip.</span>

Leave a Comment