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 VencCallBacker.

For details about video decoding APIs, see VideoDecoder.

API Calling Process

Define the output data combination mode as required, refer to VdecCallBacker 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.

The following figure shows the process of calling APIs of video decoding.

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 mx_init().
  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 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 decode API to decode the video.
  7. Perform deinitialization by calling mx_deinit().

Sample Code

The following is a code example of key steps, which is for reference only and cannot be directly copied for execution.
 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
import os
import numpy as np
import time
from mindx.sdk import base
from mindx.sdk.base import Image, ImageProcessor
from mindx.sdk.base import VideoDecoder, VideoDecodeConfig, VdecCallBacker

decoded_data_list = []  
# Video decoding callback function
def vdec_callback(decodedImage, channelId, frameId):  
    # Save the decoded Image class to the list.
    decoded_data_list.append(decodedImage)    

def process():     
    # Initialize the VdecCallBacker class and register the callback function.
    vdecCallBacker = VdecCallBacker()  
    vdecCallBacker.registerVdecCallBack(vdec_callback)  
    # Initialize the VideoDecodeConfig class and set parameters.
    vdecConfig = VideoDecodeConfig()  
    vdecConfig.skipInterval = 0  
    vdecConfig.inputVideoFormat = base.h264_main_level  
    vdecConfig.outputImageFormat = base.nv12  
    vdecConfig.width = 1920  
    vdecConfig.height = 1080  
    # Initialize the VideoDecoder.
    videoDecoder = VideoDecoder(vdecConfig, vdecCallBacker, device_id, channel_id)  
    # Obtain the name of the video frame file to be decoded.
    srcDataList = ["frame-{}.data".format(i) for i in range(100)]  
    # Obtain frames repeatedly for decoding.
    for i, fileName in enumerate(srcDataList):  
    # Read the video frame data and save it to the file.
    file = np.fromfile(fileName, dtype='uint8')  
    # Decode video frame data.
    videoDecoder.decode(file, i) 

if __name__ == "__main__":
    base.mx_init()    # Initialize resources.
    process()       
    base.mx_deinit()  # Deinitialize resources.