Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Version History

« Previous Version 7 Current »

Introductory

Currently, the C3V Ubuntu rootfs has included the FFmpeg 4.4.4 prebuilt for version 20/22 and FFmpeg 6.1 for version 24 by default. It carries the commonly used elements and hardware media codec functions. If you don’t want to use it, disable the install in the file <C3V_PRJ>/linux/rootfs/initramfs/build_disk.sh before running make config.

The prebuilt FFmpeg is located in the path <C3V_PRJ>/linux/rootfs/initramfs/ubuntu/prebuilt-app-xxx. If your C3V system doesn’t have it or you feel the provided does not meet your needs, you can recompile it as below.


Compile and install FFmpeg to the target version 4.4.4 with C3V Codec supported:

1 . Install the building package

sudo apt update
sudo apt install -y build-essential meson ninja-build python3-pip bison libglib2.0-dev flex git
sudo apt install -y libmp3lame-dev libx264-dev libopus-dev libvpx-dev libssl-dev libfdk-aac-dev libv4l-dev
sudo apt install -y nasm libsdl2-dev libxcb-xfixes0-dev libxcb-shm0-dev libsctp-dev libfdk-aac-dev liblz-dev liblzma-dev
sudo pip3 install --upgrade meson

2 . Build and install Openh264 :

Openh264 is a free software library for real-time encoding and decoding of video streams in the H.264/MPEG-4 AVC format. You can do this step if you want to use it with the FFmpeg.

Get source code:

mkdir code
cd code
git clone https://github.com/cisco/openh264.git

Compile and install:

cd openh264
meson build --prefix=/usr
sudo ninja -C build/ install

3 . Build FFmpeg and install it:

Gst source and extract it.

cd ~/code
wget https://ffmpeg.org/releases/ffmpeg-4.4.4.tar.xz
tar -xf ffmpeg-4.4.4.tar.xz

4 . Apply code patch for supporting C3V codec

The patch files are below. Please download and copy it to the root ~/code folder in C3V rootfs.

Extract patch files: patch files will be extracted in ~/code/ffmpeg

7z x ffmpeg-4.4.4-patch.7z

The patches in this document may not be up to date, if you are using the latest code of C3V, you can get the newest patch in the directory <C3V_PRJ>/linux/rootfs/initramfs/buildroot/package/ffmpeg directly.

Apply patch

cd ~/code/ffmpeg-4.4.4
patch -p1 < ../ffmpeg/0001-swscale-x86-yuv2rgb-Fix-build-without-SSSE3.patch
patch -p1 < ../ffmpeg/0002-avcodec-vaapi_h264-skip-decode-if-pic-has-no-slices.patch
patch -p1 < ../ffmpeg/0003-libavutil-Fix-mips-build.patch
patch -p1 < ../ffmpeg/0004-configure-add-extralibs-to-extralibs_xxx.patch
patch -p1 < ../ffmpeg/0005-Fixes-assembling-with-binutils-as-2.41.patch
patch -p1 < ../ffmpeg/0006-v4l2-hw-codec-support.patch
patch -p1 < ../ffmpeg/0007-v4l2-enc-add-spspps-to-each-idr.patch
patch -p1 < ../ffmpeg/0008-use-v4l2-codec-default-if-find-by-id.patch
patch -p1 < ../ffmpeg/0009-add-options-to-hw-codec-to-use-dma-copy.patch

5 . Configure, build, and install in prefix path “/home/sunplus/code/install-ffmpeg"

mkdir build
cd build
../configure --prefix=/home/sunplus/code/install-ffmpeg \
--enable-static --enable-shared --enable-gpl --enable-nonfree \
--enable-sdl --enable-ffplay --disable-optimizations \
--enable-libv4l2 --enable-libopus --enable-openssl \
--enable-libfdk-aac --enable-libopenh264 --enable-libx264 \
--enable-libmp3lame \
--enable-debug \
--extra-cflags=-g

make -j4
make install

6 . Set environment variable to run FFmpeg

Because the FFmpeg install path is/home/sunplus/code/install-ffmpeg, it needs to set LD_LIBRARY_PATH and PATH to execute FFmpeg.

export LD_LIBRARY_PATH=/home/sunplus/code/install-ffmpeg/lib:/usr/local/lib/aarch64-linux-gnu:$LD_LIBRARY_PATH
export PATH=/home/sunplus/code/install-ffmpeg/bin:$PATH

7 . Running cmd example:

Encode the video frame of the webcam to be an H264 video file.

I use the Logitech c270 USB webcam. When plugged in the C3V USB port, you will see the video device in /dev.

In my case, the c270 USB webcam device is video0.

image-20240826-105829.png

You can check the video format support list with “v4l-ctl” app. It can be installed by below command.

sudo apt install v4l-utils

Check command is below:

v4l2-ctl --list-formats-ext --device /dev/video0
image-20240826-110449.png

I use YUYV422 640x480 30fps video format to be the encoded video source. The action command is below:

ffmpeg -f v4l2 -input_format yuyv422 -framerate 30 -video_size 640x480 -i /dev/video0 -vcodec h264_v4l2m2m -vframes 100 output.h264

If you want to play the encode file “output.h264”, you can use the mplayer. It can be installed by the below command.

sudp pat install mplayer

Then output.h264 can be played by

mplayer output.h264

You can also record video and separate the record file by time with fixed video length. The action command is below:

ffmpeg -f v4l2 -input_format yuyv422 -framerate 30 -video_size 640x480 -i /dev/video0 -vcodec h264_v4l2m2m -spspps_to_idr 1 -f segment -segment_time 60 -map 0 -reset_timestamps 1 -strftime 1 -preset ultrafast -crf 23 output_%Y-%m-%d_%H:%M:%S.h264 

The command argument -spspps_to_idr 1 will automatically add SPS and PPS to the second and subsequent recorded video files to prevent playback issues.

Decode H.264 to YUV

ffmpeg -benchmark -y -vcodec h264_v4l2m2m -i output.h264 ffout.yuv

JPEG Encode

ffmpeg -benchmark -y -pix_fmt nv12 -s 640x480 -i ffout.yuv -vcodec jpeg_v4l2m2m -vframes 5 ffout_%03d.jpg

JPEG Decode

ffmpeg -benchmark -y -vcodec jpeg_v4l2m2m -i ffout_001.jpg -vframes 1 ffout_001.yuv

7 . Other Version

The steps for installing FFmpeg 6.1 are the same as for FFmpeg 4.4.4, here is the patch for FFmpeg 6.1.

If you are using Ubuntu version 24, it is recommended to use FFmpeg 6.1, otherwise when installing some packages such as OpenCV via apt, it will download FFmpe 6.1 packages to replace the lower version already installed.

  • No labels