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_pngd_chn chnId;
hi_pngd_chn_attr chnAttr; // hi_pngd_chn_attr is reserved.
ret = hi_mpi_pngd_create_chn(chnId, &chnAttr);

//6. Send streams.
//6.1 Allocate input buffer.
uint8_t* inputAddr = nullptr;
//inputsize indicates the buffer size occupied by the input image. 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 image data into the host buffer, and call aclrtMemcpy to transfer the host image 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 image data into the device buffer.
if (runMode == ACL_HOST) {
    void* hostInputAddr = nullptr;
    //Allocate the host buffer.
    aclRet = aclrtMallocHost(&hostInputAddr, inputSize);
    //Load the input image into the buffer. The ReadStreamFile function is defined by the user.
    ReadStreamFile(fileName, hostInputAddr, inputSize);
    //Transfer data.
    aclRet = aclrtMemcpy(inputAddr, inputSize, hostInputAddr, inputSize, ACL_MEMCPY_HOST_TO_DEVICE);    
} else {
    //Load the input image into the buffer. The ReadStreamFile function is defined by the user.
    ReadStreamFile(fileName, inputAddr, inputSize);
}

//6.2 Construct a struct for storing input image information.
hi_img_stream stStream{};
hi_img_info stImgInfo{};
stStream.pts = 0;
if (g_runMode == ACL_HOST) {
    stStream.addr  = (uint8_t *)hostInputAddr;
} else {
    stStream.addr  = (uint8_t *)inputAddr;
}
stStream.len  = inputSize;
stStream.type = HI_PT_PNG;

ret = hi_mpi_png_get_image_info(&stStream, &stImgInfo);
if (g_runMode == ACL_HOST) {
    //If the data on the host is not used, free the host buffer in a timely manner.
   aclrtFreeHost(hostInputAddr);
    hostInputAddr = nullptr;
}
stStream.addr = (uint8_t *)inputAddr;

//6.3 Construct the struct for storing output image information and allocate output buffer.
hi_pic_info outPicInfo{};
void *outBuffer = nullptr;
outPicInfo.picture_width  = stImgInfo.width;
outPicInfo.picture_height = stImgInfo.height;
outPicInfo.picture_width_stride  = stImgInfo.width_stride;
outPicInfo.picture_height_stride = stImgInfo.height_stride;
outPicInfo.picture_buffer_size   = stImgInfo.img_buf_size;
outPicInfo.picture_format = HI_PIXEL_FORMAT_UNKNOWN;

ret = hi_mpi_dvpp_malloc(0, &outBuffer, outPicInfo.buffer_size);

outPicInfo.picture_address = (uint64_t)outBuffer;

//6.4 Send the input image to be decoded.
ret = hi_mpi_pngd_send_stream(chnId, &stream, &outPicInfo, 0);

//7. Receive the decoding result.
//7.1 Obtain the decoding result.
hi_pic_info picInfo;
hi_img_stream stream;
ret = hi_mpi_pngd_get_image_data(chnId, &picInfo, &stream, 0);
if (ret == HI_SUCCESS) { // Decode success
    printf("[%s][%d] Chn %u GetFrame Success, Decode Success \n",__FUNCTION__, __LINE__, chnId);
} else if (ret == HI_ERR_PNGD_BUF_EMPTY){ // Decoding
    printf("[%s][%d] Chn %u Decoding, try again \n",__FUNCTION__, __LINE__, chnId);
} else { // Decode fail
    printf("[%s][%d] Chn %u GetFrame Success, Decode Fail \n",__FUNCTION__, __LINE__, chnId);
}

//7.2 If the run mode is ACL_HOST and the image data output by PNGD 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 PNGD in the buffer specified by outputPic.picture_address.
    // ......
}

//7.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);

//8. Destroy the channel.
ret = hi_mpi_pngd_destroy_chn(chnId);

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

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

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

// ....