avcodec Video Processing Library

repository·master·Indexed 41 days ago

https://github.com/ireader/avcodec

A video processing library providing H.264 bitstream parsing and encoding capabilities. It supports H.264 encoding via X264 and OpenH264, as well as video encoding and decoding integration with FFMPEG.

Tokens
886
Snippets
1
Records
2
Agent score
2%

What's inside avcodec

  1. Overview of avcodec capabilities

    master

    The avcodec library provides several core functionalities for handling H.264 video streams:

    1. H.264 bit stream parsing: Analyzing existing H.264 streams.
    2. H.264 encoding via X264: High-performance encoding using the X264 library.
    3. H.264 encoding via OpenH264: Encoding using the OpenH264 library.
    4. FFMPEG video encode/decode: Integration with FFMPEG for broader video processing capabilities.
  2. Encode H.264 video using X264 or OpenH264

    master

    To encode video, you must first define an h264_parameter_t structure to configure the encoder (profile, level, resolution, format, etc.), then create an encoder instance using x264enc_create or openh264enc_create.

    Once the encoder is initialized, you feed it raw video frames using the picture_t structure via the x264enc_input (or openh264enc_input) function. Finally, retrieve the encoded packets using x264enc_getpacket (or openh264enc_getpacket) and write the pkt.data to a file.

    h264_parameter_t param;
    param.profile = H264_PROFILE_BASELINE;
    param.level = H264_LEVEL_2_0;
    param.width = 352;
    param.height = 288;
    param.format = PICTURE_YUV420;
    param.frame_rate = 25000;
    param.gop_size = 50;
    param.bitrate = 500000;
    
    // Create the encoder instance
    void* h264 = x264enc_create(& param);
    // Alternatively for OpenH264:
    // void* h264 = openh264enc_create(& param);
    
    // Prepare a raw video frame
    picture_t pic;
    memset(&pic, 0, sizeof(pic));
    pic.format = PICTURE_YUV420;
    pic.width = 352;
    pic.height = 288;
    pic.pts = i * 40;
    pic.dts = i * 40;
    pic.flags = 0; // Use AVPACKET_FLAG_KEY to force IDR frame
    pic.data[0] = s_yuv;
    pic.linesize[0] = pic.width;
    pic.data[1] = pic.data[0] + pic.linesize[0] * pic.height;
    pic.linesize[1] = pic.width / 2;
    pic.data[2] = pic.data[1] + pic.linesize[1] * pic.height / 2;
    pic.linesize[2] = pic.width / 2;
    
    // Input the frame to the encoder
    assert(x264enc_input(h264, &pic) > 0);
    // Alternatively for OpenH264:
    // assert(openh264enc_input(h264, &pic) > 0);
    
    // Retrieve the encoded packet
    avpacket_t pkt;
    memset(&pkt, 0, sizeof(pkt));
    assert(x264enc_getpacket(h264, &pkt) > 0);
    // Alternatively for OpenH264:
    // assert(1 == openh264enc_getpacket(h264, &pkt));
    
    // Save the encoded data to a file
    assert(pkt.bytes == fwrite(pkt.data, 1, pkt.bytes, wfp));