Video Decoding

Function Description

You can implement video decoding by constructing an instance of the VideoDecoder class. For details about the decoding configuration items and restrictions, see VideoDecodeConfig.

Video decoding supports customized output data formats. You can use a customized callback function to input the decoding configuration items to use the decoded data. For details, see VideoDecodeCallBack.

For details about video decoding APIs, see VideoDecoder.

API Calling Process

Define the output data combination mode as required, refer to VideoDecodeCallBack to define the callback function, and input decoding configuration items. Instantiate the VideoDecoder class, and call the member function of Decode to complete decoding and obtain data.

Process of calling APIs of video decoding is as follows:

Figure 1 Process of calling APIs of video decoding

Vision SDK provides the VideoDecoder class for video decoding. 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 decoded, frameId of the current decoded 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 VideoDecodeCallBack format. The output is optional in the function.
    • You are advised to perform customized operations to obtain the decoded data, and do not perform complex operations in the callback function.
  4. Construct video decoding configuration items.

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

  5. Instantiate the video decoding class.

    Input the configured VideoDecodeConfig to the constructor API to instantiate the video decoding class.

  6. Call the decoding API.
    • When an instantiated decoder calls the Decode API for the first time, the system checks whether the scenario is a pre-allocation scenario.
    • In this case, call the Decode API to pre-allocate output memory to prevent API calling failure.
  7. 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
// Initialization
MxBase::MxInit();
{
    // Define the decoded 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 decoded data to the customized structure.
    APP_ERROR CallBackVdec(Image& decodedImage, uint32_t channelId, uint32_t frameId, void* userData)
    {
      FrameImage* decodedVec = static_cast<FrameImage*>(userData);
      decodedVec->image = decodedImage;
      decodedVec->channelId = channelId;
      decodedVec->frameId = frameId;
    }
    // Construct the decoding configuration items.
    MxBase::VideoDecodeConfig config;
    VideoDecodeCallBack cPtr = CallBackVdec;
    config.width = 1920;
    config.height = 1080;
    config.callbackFunc = cPtr;
    config.skipInterval = 0;
    config.inputVideoFormat = StreamFormat::H264_MAIN_LEVEL;
    config.outputImageFormat = ImageFormat::YUV_SP_420;
    // Instantiate the decoding class.
    MxBase::VideoDecoder videoDecoder(config, deviceId, channelId);
    // Perform the decoding operation.
    ret = videoDecoder.Decode(dataPtr, dataSize, frameId, &decodedFrame); 
}
// Deinitialization
MxBase::MxDeInit();