/
Video Codec V4L2 Usage with C3V system

Video Codec V4L2 Usage with C3V system

Introduction

This document is about the usage of hardware video codec with V4L2 framework on the C3V platform.

Before use, please ensure you already know how to compile the Buildroot and Ubuntu system for C3V platform.

Environment

The kernel needs to do some config to support the video codec V4L2 driver.

Kernel Suport

Run the command make kconfig to configure the kernel driver.

video hardware codec driver

Device Drivers > Multimedia support > Media drivers > Video hardware codec

image-20240125-054103.png

Currently, most boards already support this by default, you can follow the above method to check if there is enabled.

Get the Patches and Build

To use the video codec v4l2 framework in GStreamer and FFmpeg, we need to make some changes to the source code to adapt it. And we will provide the patches for the changes.

The patches of the GStreamer and FFmpeg can be obtained from GitHub with the C3V source code.

  • For the buildroot system:

    • GStreamer patches: “<code_root>/linux/rootfs/initramfs/buildroot/package/gstreamer1/gst1-plugins-good”.

    • FFmpeg patches “<code_root>/linux/rootfs/initramfs/buildroot/package/ffmpeg” .

After make config, use make bconfig to config the packages and use make buildroot to compile them. The patches will be applied automatically during buildroot building.

If you change the source code and want to rebuild the packages, just run the above two commands to rebuild the buildroot. The updated libs will be installed to the rootfs directory automatically. You can run make to pack it to the ISP BIN file, or you can just copy the libs to the C3V platform for using.

Alterbatuvely, run make <pkg-name>-rebuild under “<code_root>/linux/rootfs/initramfs/buildroot” to recompile the target package only, such as

make ffmpeg-rebuild make gst1-plugins-good-rebuild make gstreamer1-rebuild

The “<pkg-name>” can be find under the path “<code_root>/linux/rootfs/initramfs/buildroot/package”. In this way, the updated libs will not be installed to the rootfs directory automatically, you need copy it the by yourself if you want to pack them to the ISP BIN.

Use

Here just introduce the basic usage with default parameters for H.264 and JPEG. If you need to change the codec parameters, please follow the usage of GStreamer and FFmpeg.

V4L2 Sample

Test sample:

This is a V4L2 video codec test sample to test the codec functions without the media framework such as the GStreamer and FFmpeg.

The test needs a YUV source file, you can use the GStreamer to generate a fake one, or you can use the V4L2 utils to get it from the sensor:

gst-launch-1.0 -v videotestsrc horizontal-speed=15 num_buffers=100 ! "video/x-raw,width=1920,height=1080,format=NV12,framerate=30/1" ! filesink location=1080p.yuv
image-20240125-054309.png

H.264 Encode

./v4l2_vsi_sample ifile --key 0 --name 1080p.yuv --size 1920 1080 --framenum 100 --fmt nv12 encoder --level 11 --fmt h264 --key 1 --source 0 ofile --key 2 --source 1 --name stream.h264

The command will encode the file 1080p.yuv to a file called stream.h264.

The file can be played by PC tools like PotPlayer and VLC media player.

H.264 Decode

The command will decode the file stream.h264 to a file called test.yuv.

The file can be played by PC tools like Elecard YUV viewer and YuvEye.

Due to the driver's 16-byte alignment of the YUV, the height of the YUV output from the 1080p decode is 1088. However, the GStreamer and FFmpeg have undergone internal processing, so the final output height is still 1080.

GStreamer Command Line

H.264 Encode

Fake live stream:

Local file:

H.264 Decode

JPEG Encode

JPEG Decode

FFmpeg Command Line

H.264 Encode

H.264 Decode

JPEG Encode

JPEG Decode

GStreamer Coding

Only sample code is provided here, please make sure you already know how to code with GStreamer.

H.264 Encode

Normally, the filter “h264parse” needs to follow the encoder to adpat to different application scenarios, such as MP4 recoding or network transmission.

H.264 Decode

JPEG Encode

JPEG Decode

FFmpeg Coding

Only sample code is provided here, please make sure you already know how to code with FFmpeg.

H.264 Encode

H.264 Decode

JPEG Encode

JPEG Decode

Reference

https://sunplus.atlassian.net/wiki/x/A4CVgw

Related content