Video Encoding

Function Description

You can implement video encoding by constructing an instance of the VideoEncoder class. For details about the configuration items, restrictions, and support of the encoding function, see the data structure description in VideoEncodeConfig.

Video encoding supports customized output data formats. You can use a customized callback function to input the encoding configuration items to use the encoded data. For details, see VideoEncodeCallBack.

For details about video encoding APIs, see VideoEncoder.

API Calling Process

Define the output data combination mode as required, refer to VideoEncodeCallBack to define the callback function, and input encoding configuration items. Instantiate the VideoEncoder class, and call the member function of Encode to complete encoding and obtain data.

Process of calling APIs of video encoding is as follows:

Figure 1 Process of calling APIs of video encoding

Vision SDK provides the VideoEncoder class for video encoding. The key steps are described as follows:

  1. Perform global initialization by calling MxInit().
  2. Define the output data combination mode.
    • The output data includes the Image class data obtained after video frames are encoded, frameId of the current encoded frame, and channelId.
    • Determine the preceding data to be obtained as required.
  3. Define the output callback function.
    • Define the callback function based on the data to be obtained and assemble the customized data in the function.
    • The input parameter of the callback function is fixed in VideoEncodeCallBack format. The output is optional in the function.
    • You are advised to use the custom userData to receive the video encoding callback result, and not to perform complex operations in the callback function. Otherwise, the callback thread is suspended, and the video encoding speed becomes slow.
  4. Construct video encoding configuration items.

    For details about the configuration items and restrictions, see the data structure description of VideoEncodeConfig.

  5. Instantiate the video encoding class.

    Input the configured VideoEncodeConfig to the constructor API to instantiate the video encoding class.

  6. Deinitialize the initialized global resources by calling MxDeInit().

Sample Code

The following is a code example of key steps, which is for reference only and cannot be directly copied for compilation or running.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Initialization
MxBase::MxInit();
{
    // Define the encoded data to be received.
    struct FrameImage {
        Image image;                 // Video frame object
        uint32_t frameId = 0;        // Video frame ID
        uint32_t channelId = 0;      // Video processing channel ID
    };

    // Define the callback function for transferring the encoded data to the customized structure.
    APP_ERROR CallBackVenc(std::shared_ptr<uint8_t>& outDataPtr, uint32_t& outDataSize, uint32_t& channelId,
                           uint32_t& frameId, void* userData)
    {
        Image image(outDataPtr, outDataSize, -1, Size(1920, 1080));
        FrameImage* encodedVec = static_cast<FrameImage*>(userData);
        FrameImage frameImage;
        encodedVec.image = image;
        encodedVec.channelId = channelId;
        encodedVec.frameId = frameId;
        return APP_ERR_OK;
    }
    // Construct the encoding configuration items.
    MxBase::VideoEncodeConfig vEncodeConfig;
    VideoEncodeCallBack cPtr2 = CallBackVenc;
    vEncodeConfig.callbackFunc = cPtr2;
    vEncodeConfig.width = 1920;  // Input width
    vEncodeConfig.height = 1080;   // Input height
    vEncodeConfig.keyFrameInterval = 1;  // I-frame interval
    vEncodeConfig.srcRate = 30;   // Input stream frame rate
    vEncodeConfig.maxBitRate = 30000;  // Output bit rate
    vEncodeConfig.ipProp = 70;  // Output bit rate
    vEncodeConfig.displayRate = 40;  // [1, 120]  // Display rate of the output video
    vEncodeConfig.rcMode = 0;  // cbr 0 or 1, vbr 2, avbr 3, qvbr 4, cvbr 5 # Bit rate control mode
    vEncodeConfig.sceneMode = 0;  // 0: a scene in which the camera does not move or moves periodically and continuously; H.264 and H.265 are supported. 1: a motion scene in which the bit rate is high; H.265 is supported.
    vEncodeConfig.shortTermStatsTime = 40; // Short-term statistical time of the bit rate (in seconds). The value range is [1, 120]. It takes effect when rcMode is set to 5.
    vEncodeConfig.longTermStatsTime = 240;  // Long-term statistical time of the bit rate (in minutes). The value range is [1, 1440]. It takes effect when rcMode is set to 5.
    vEncodeConfig.longTermMaxBitRate = 200;  // Maximum long-term output bit rate of the encoder, in kbps. The value range is [2, max_bit_rate]. It takes effect when rcMode is set to 5
    vEncodeConfig.longTermMinBitRate = 1;  // Minimum long-term output bit rate of the encoder, in kbps. The value range is [0, long_term_max_bit_rate]. It takes effect when rcMode is set to 5
    vEncodeConfig.statsTime = 1;  // [1, 60]
    vEncodeConfig.thresholdI = {0, 0, 0, 0, 3, 3, 5, 5, 8, 8, 8, 15, 15, 20, 25, 25}; // Length: 16; value range: [0, 255].
    vEncodeConfig.thresholdP = {0, 0, 0, 0, 3, 3, 5, 5, 8, 8, 8, 15, 15, 20, 25, 25}; // Length: 16; value range: [0, 255].
    vEncodeConfig.thresholdB = {0, 0, 0, 0, 3, 3, 5, 5, 8, 8, 8, 15, 15, 20, 25, 25}; // Length: 16; value range: [0, 255].
    vEncodeConfig.direction = 8; // [0, 16] # Addition or subtraction direction control during texture-based macroblock-level bit rate control
    vEncodeConfig.rowQpDelta = 1; //[0, 10] # Maximum row-level adjustment range within one frame. The unit is the macroblock row. A greater adjustment amplitude signifies a wider allowable QP row-level adjustment range and a more stable bit rate.
    vEncodeConfig.firstFrameStartQp = 32; // Start QP value of the first frame.
    // Instantiate the encoding class.
    MxBase::VideoEncoder videoEncoder(vEncodeConfig, deviceId);
    // Perform the encoding operation.
    FrameImage encodedFrame;

    for (size_t i = 0; i < inputFrameList.size(); i++) {
        ret = videoEncoder.Encode(image, frameId, &encodedFrame);
    }
}
// Deinitialization
MxBase::MxDeInit();