Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Introductory

Currently, the C3V Ubuntu rootfs have included the FFmpeg 4.4.4 prebuilt for version 20.04/22.04 and FFmpeg 6.1.1 for version 24.04 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

Code Block
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 :

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

Get source code:

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

...

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

3 . Build ffmpeg FFmpeg and install it:

Gst source and extract it.

...

4 . Apply code patch for supporting C3V codec

Patch The patch files are below. Please download files and copy files it to the root path of ffmpeg ~/code folder extracted in step 3C3V rootfs.

View file
name0001-swscale-x86-yuv2rgb-Fix-build-without-SSSE3.patch

View file
name0002-avcodec-vaapi_h264-skip-decode-if-pic-has-no-slices.patch

View file
name0003-libavutil-Fix-mips-build.patch

View file
name0004-configure-add-extralibs-to-extralibs_xxx.patch

View file
name0005-v4l2-hw-codec-support.patch

View file
name0006-v4l2-enc-add-spspps-to-each-idr.patchffmpeg-4.4.4-patch.7z

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

Code Block
7z x ffmpeg-4.4.4-patch.7z
Info

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

Code Block
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 < 0006../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"

...

6 . Set environment variable to run ffmpeg FFmpeg

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

Code Block
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 logitech the Logitech c270 usb USB webcam. When it is plugged in the C3V usb USB port, you will see the video device in /dev.

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

...

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

...

Code Block
v4l2-ctl --list-formats-ext --device /dev/video0

...

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

Code Block
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.

Code Block
sudp pat install mplayer

...

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

Code Block
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 

Command 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.

...

Code Block
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.1 are the same as for FFmpeg 4.4.4, here is the patch for FFmpeg 6.1.1.

View file
nameffmpeg-6.1-patch.7z

Note

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