Sample Code

Principles

Sample Code

You can view the complete code in Sample Overview.

Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code sample of key steps for your reference only.

//1. Obtain the run mode of the software stack. Different run modes lead to different API call sequences (for example, whether data transfer is required).
aclrtRunMode runMode;
aclError aclRet = aclrtGetRunMode(&runMode);

//2. Initialize AscendCL.
aclRet = aclInit(nullptr);

//3. Allocate runtime resources including the device and context in sequence.
aclrtContext g_context;
aclRet = aclrtSetDevice(0);
aclRet = aclrtCreateContext(&g_context, 0);

//4. Initialize the media data processing system.
int32_t ret = hi_mpi_sys_init();

//5. Create a channel.
hi_vdec_chn chnId;
hi_vdec_chn_attr chnAttr;
hi_pic_buf_attr buf_attr{1920, 1080, 0, 8, HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420, HI_COMPRESS_MODE_NONE};

chnAttr.type = HI_PT_H264;
chnAttr.mode = HI_VDEC_SEND_MODE_FRAME;
chnAttr.pic_width = 1920;
chnAttr.pic_height = 1080;
chnAttr.stream_buf_size = 1920 * 1080 * 3 / 2;
chnAttr.frame_buf_cnt = 16;
chnAttr.frame_buf_size = hi_vdec_get_pic_buf_size(HI_PT_H264, &buf_attr);
chnAttr.video_attr.ref_frame_num = 12;
chnAttr.video_attr.temporal_mvp_en = HI_TRUE;
chnAttr.video_attr.tmv_buf_size = hi_vdec_get_tmv_buf_size(HI_PT_H264, 1920, 1080);

ret = hi_mpi_vdec_create_chn(chnId, &chnAttr);

//6. Set channel attributes.
hi_vdec_chn_param chnParam;
ret = hi_mpi_vdec_get_chn_param(chnId, &chnParam);

chnParam.video_param.dec_mode = HI_VIDEO_DEC_MODE_IPB;        
chnParam.video_param.compress_mode = HI_COMPRESS_MODE_HFBC;        
chnParam.video_param.video_format = HI_VIDEO_FORMAT_TILE_64x16;        
chnParam.display_frame_num = 3;
chnParam.video_param.out_order = HI_VIDEO_OUT_ORDER_DISPLAY;

ret = hi_mpi_vdec_set_chn_param(chnId, &chnParam);

//7. The decoder starts receiving streams.
ret = hi_mpi_vdec_start_recv_stream(chnId);

//8. Send streams.
//8.1 Allocate input buffer.
uint8_t* inputAddr = nullptr;
//inputsize indicates the buffer size occupied by each stream. The following uses 1024 bytes as an example. You need to calculate the actual buffer size.
int32_t inputSize = 1024;
ret = hi_mpi_dvpp_malloc(0, &inputAddr, inputSize);

//If the run mode is ACL_HOST, allocate the host buffer, load the input stream data into the host buffer, and call aclrtMemcpy to transfer the host stream data to the device. After the data transfer is complete, free the host buffer in a timely manner. In other modes, directly load the input stream data into the device buffer.
if (runMode == ACL_HOST) {
    void* hostInputAddr = nullptr;
    //Allocate the host buffer.
    aclRet = aclrtMallocHost(&hostInputAddr, inputSize);
    //Load the input stream into the buffer. The ReadStreamFile function is defined by the user.
    ReadStreamFile(streamName, hostInputAddr, inputSize);
    //Transfer data.
    aclRet = aclrtMemcpy(inputAddr, inputSize, hostInputAddr, inputSize, ACL_MEMCPY_HOST_TO_DEVICE);
    //Free the buffer in a timely manner after data transfer is complete.
    aclrtFreeHost(hostInputAddr );
    hostInputAddr = nullptr;
} else {
   //Load the input stream into the buffer. The ReadStreamFile function is defined by the user.
    ReadStreamFile(streamName, inputAddr, inputSize);
}

//8.2 Allocate output buffer.
uint8_t* outputAddr = nullptr;
//The size of the buffer occupied by the output image data is related to the format of the output image.
int32_t outputSize = 1920 * 1080 * 3 / 2;
ret = hi_mpi_dvpp_malloc(0, &outputAddr, outputSize);

//8.3 Construct a struct for storing an input stream.
hi_vdec_stream stream;
stream.addr = inputAddr;
stream.len = inputSize;
stream.end_of_frame = HI_TRUE;
stream.end_of_stream = HI_FALSE;
stream.need_display = HI_TRUE;

//8.4 Construct a struct for storing an output result.
hi_vdec_pic_info outPicInfo;
outPicInfo.width = 1920;
outPicInfo.height = 1080;
outPicInfo.width_stride = 1920;
outPicInfo.height_stride = 1080;
outPicInfo.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420;
outPicInfo.vir_addr = outputAddr;
outPicInfo.buffer_size = outputSize;

//8.5 Send a stream.
ret = hi_mpi_vdec_send_stream(chnId, &stream, &outPicInfo, 0);

//9. Receive the decoding result.
//9.1 Obtain the decoding result.
hi_video_frame_info frame;    
hi_vdec_stream stream;
hi_vdec_supplement_info stSupplement;
ret = hi_mpi_vdec_get_frame(chnId, &frame, &stSupplement, &stream, 0);
if (ret == HI_SUCCESS) {
   decResult = frame.v_frame.frame_flag;
   if (decResult == 0) { // 0: Decode success
       printf("[%s][%d] Chn %u GetFrame Success, Decode Success \n",__FUNCTION__, __LINE__, chnId);
   } elseif (decResult == 1) { // 1: Decoding fails
       printf("[%s][%d] Chn %u GetFrame Success, Decode Fail \n",__FUNCTION__, __LINE__, chnId);
   } elseif (decResult == 2) { // 2:This result is returned for the second field of
       printf("[%s][%d] Chn %u GetFrame Success, No Picture \n", __FUNCTION__, __LINE__, chnId);
   } elseif (decResult == 3) { // 3: Reference frame number set error
       printf("[%s][%d] Chn %u GetFrame Success, RefFrame Num Error \n",__FUNCTION__, __LINE__, chnId);
   } elseif (decResult == 4) { // 4: Reference frame size set error
       printf("[%s][%d] Chn %u GetFrame Success, RefFrame Size Error \n",__FUNCTION__, __LINE__, chnId);
  }
}

//9.2 If the run mode is ACL_HOST and the image data output by VDEC needs to be displayed on the host, allocate the host buffer and call aclrtMemcpy to transfer the output image data from the device to the host.
if (g_runMode == ACL_HOST) {
    void* hostOutputAddr = nullptr;
    aclRet = aclrtMallocHost(&hostOutputAddr, outputSize);
    aclRet = aclrtMemcpy(hostOutputAddr, outputSize, frame.v_frame.virt_addr[0], outputSize, ACL_MEMCPY_DEVICE_TO_HOST);
    // ......
    //After data is used, free the buffer in a timely manner.
    aclrtFreeHost(hostOutputAddr);
    hostOutputAddr = nullptr;
} else {
    //You can directly use the output image data of VDEC in the buffer specified by frame.v_frame.virt_addr[0].
    // ......
}

//9.3 Free the input and output buffers.
ret = hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]);
ret = hi_mpi_dvpp_free(stream.addr);

//9.4 Release resources.
ret = hi_mpi_vdec_release_frame(chnId, &frame);

//10. The decoder stops receiving streams.
ret = hi_mpi_vdec_stop_recv_stream(chnId);

//11. Destroy the channel.
ret = hi_mpi_vdec_destroy_chn(chnId);

//12. Deinitialize the media data processing system.
ret = hi_mpi_sys_exit();

//13. Release runtime resources including the context and device in sequence.
aclRet = aclrtDestroyContext(g_context);
aclRet = aclrtResetDevice(0);

//14. Deinitialize AscendCL.
aclRet = aclFinalize();

// ....