VCodecV4L2 C++ library v2.1.0

Ultra low-latency video encoding library for Raspberry PI platform. Simple interface for frame-by-frame H264 and JPEG hardware encoding with configurable parameters.

Overview

VCodecV4L2 C++ library designed for hardware H264 and JPEG video encoding on Raspberry PI platform (4B and Zero W2). VCodecV4L2 class inherits interface and data structures form open source VCodec library and also includes Logger open source library. VCodecV4L2 uses V4L2 API. Version 2.1.0 supports only Raspberry PI 4 and Raspberry Zero W2 platforms (tested on Raspbian OS 64 bit). The library provides simple programming interface to be implemented in different C++ projects for Raspberry PI without complexity of V4L2 API. The libraries is a CMake project and supplied as source code only. Encoding time on Raspberry PI 4B and Raspberry Zero W2 for different video resolution per frame:

  • RPI 4, H264 codec, 1920x1080 - 12 msec, 1280x720 - 5.4 msec, 640x512 - 2.5 msec.

  • RPI 4, JPEG codec, 1920x1080 - 25 msec, 1280x720 - 13 msec, 640x512 - 5.5 msec.

  • Zero W2, H264 codec, 1920x1080 - 19.5 msec, 1280x720 - 8.7 msec, 640x512 - 2.8 msec.

  • Zero W2, JPEG codec, 1920x1080 - 33.4 msec, 1280x720 - 15.7 msec, 640x512 - 5.1 msec.

Interface

VCodecV4L2 library provides simple interface hardware H264 and JPEG video encoding without complexity of V4L2 API:

class VCodecV4L2 : public VCodec {
public:
    /// Encode video frame.
    bool transcode(Frame& src, Frame& dst);
    /// Set parameter value.
    bool setParam(VCodecParam id, float value);
    /// Get parameter value.
    float getParam(VCodecParam id);
    /// Execute command.
    bool executeCommand(VCodecCommand id);
};

Downloads

  • VCodecV4L2 C++ library programmer’s manual: DOWNLOAD

  • Test application for Raspbian OS 64bit bullseye: DOWNLOAD

How to use

Source code bellow is simple application which encodes video frames (NV12 pixel format, pseudo code used) by H264 codec and writes results to file:

#include "VCodecV4L2.h"
using namespace cr::video;

int main(void) {
    // Create codec object and set parameters.
    VCodecV4L2 codec;
    codec.setParam(VCodecParam::BITRATE_KBPS, 2500);
    codec.setParam(VCodecParam::GOP, 30);
    codec.setParam(VCodecParam::FPS, 30);
    codec.setParam(VCodecParam::H264_PROFILE, 0);
    // Create input NV12 and output H264 frames.
    Frame dstH264; dstH264.fourcc = Fourcc::H264;
    Frame srcNv12;
    // Create output file.
    FILE *out = fopen("out.h264", "w+b");
    // Encode and record 200 frames.
    for (int n = 0; n < 200; ++n) {
        // Get NV12 frame pseudo code.
        srcNv12 = getNextFrame();
        // Encode.
        if (!codec.transcode(srcNv12, dstH264))
            continue;
        // Write to file.
        fwrite(dstH264.data, dstH264.size, 1, out);
    }
    // Close file.
    fclose(outputFile);
    return 1;
}

Price and Terms

The VCodecV4L2 C++ library is supplied under a license in source code. Library pricing is available upon request info@constantrobotics.com.