You can call hi_mpi_vpc_get_process_result in the same thread as hi_mpi_vpc_crop, or start a new thread. Multiple threads can improve efficiency, but require inter-thread synchronization.
Call aclrtGetRunMode to obtain the run mode of the software stack. If the run mode is ACL_HOST and the image data output by VPC 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 the data does not need to be displayed on the host, it can be used as the input of model inference. For details about model inference, see Model Inference and Dynamic AIPP Model Inference.
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. Initialize AscendCL.
aclRet = aclInit(nullptr);
//2. Allocate runtime resources including the device and context in sequence.
aclrtContext g_context;
aclRet = aclrtSetDevice(0);
aclRet = aclrtCreateContext(&g_context, 0);
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);
//3. Initialize the media data processing system.
int32_t ret = hi_mpi_sys_init();
//4. Create a channel.
hi_vpc_chn chnId;
hi_vpc_chn_attr stChnAttr;
ret = hi_mpi_vpc_sys_create_chn(&chnId, &stChnAttr);
//5. Perform image cropping.
//5.1 Construct a struct for storing input image information.
hi_vpc_pic_info inputPic;
inputPic.picture_width = 1920;
inputPic.picture_height = 1080;
inputPic.picture_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420;
inputPic.picture_width_stride = 1920;
inputPic.picture_height_stride = 1080;
inputPic.picture_buffer_size = inputPic.picture_width_stride * inputPic.picture_height_stride * 3 / 2;
//5.2 Prepare input image data.
//Allocate the device buffer for media data processing.
ret = hi_mpi_dvpp_malloc(0, &inputPic.picture_address, inputPic.picture_buffer_size);
//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* inputAddr = nullptr;
//Allocate the host buffer.
aclRet = aclrtMallocHost(&inputAddr, inputPic.picture_buffer_size);
//Load the input image into the buffer. The ReadPicFile function is defined by the user.
ReadPicFile(picName, inputAddr, inputPic.picture_buffer_size);
//Transfer data.
aclRet = aclrtMemcpy(inputPic.picture_address, inputPic.picture_buffer_size, inputAddr,
inputPic.picture_buffer_size, ACL_MEMCPY_HOST_TO_DEVICE);
//Free the buffer in a timely manner after data transfer is complete.
aclrtFreeHost(inputAddr);
inputAddr = nullptr;
} else {
//Load the input image into the buffer. The ReadPicFile function is defined by the user.
ReadPicFile(picName, inputPic.picture_address, inputPic.picture_buffer_size);
}
//5.3 Construct a struct for storing output image information.
//This parameter indicates the number of crop ROIs.
uint32_t multiCount = 1;
//The size of the cropRegionInfos array must be consistent with the number of crop ROIs.
hi_vpc_crop_region_info cropRegionInfos[1];
hi_vpc_pic_info outputPic;
for (uint32_t i = 0; i < multiCount; i++) {
outputPic.picture_width = 960;
outputPic.picture_height = 540;
outputPic.picture_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420;
outputPic.picture_width_stride = 960;
outputPic.picture_height_stride = 540;
outputPic.picture_buffer_size = outputPic.picture_width_stride * outputPic.picture_height_stride * 3 / 2;
ret = hi_mpi_dvpp_malloc(0, &outputPic.picture_address, outputPic.picture_buffer_size);
//Initialize the buffer.
if (runMode == ACL_HOST) {
aclRet = aclrtMemset(outputPic.picture_address, outputPic.picture_buffer_size, 0, outputPic.picture_buffer_size);
} else {
memset(outputPic.picture_address, 0, outputPic.picture_buffer_size);
}
//Crop a 960 x 540 ROI from the input image with the upper-left corner as the origin.
cropRegionInfos[i].dest_pic_info = outputPic;
cropRegionInfos[i].crop_region.left_offset = 0;
cropRegionInfos[i].crop_region.top_offset = 0;
cropRegionInfos[i].crop_region.crop_width = 960;
cropRegionInfos[i].crop_region.crop_height = 540;
}
//5.4 Call the image cropping API.
uint32_t taskID = 0;
ret = hi_mpi_vpc_crop(chnId, &inputPic, cropRegionInfos, 1, &taskID, -1);
//5.5 Wait until the task processing is complete. The output image data is stored in the buffer specified by outputPic.picture_address.
uint32_t taskIDResult = taskID;
ret = hi_mpi_vpc_get_process_result(chnId, taskIDResult, -1);
//5.6 If the run mode is ACL_HOST and the image data output by VPC 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 the data does not need to be displayed on the host, it can be used as the input of model inference.
if (g_runMode == ACL_HOST) {
hi_vpc_pic_info outputPicHost = outputPic;
aclRet = aclrtMallocHost(&outputPicHost.picture_address, outputPic.picture_buffer_size);
aclRet = aclrtMemcpy(outputPicHost.picture_address, outputPic.picture_buffer_size, outputPic.picture_address,outputPic.picture_buffer_size, ACL_MEMCPY_DEVICE_TO_HOST);
// ......
//Free the host buffer after the data on the host is used.
aclrtFreeHost(outputPicHost.picture_address);
outputPicHost.picture_address = nullptr;
} else {
//You can directly use the output image data of VPC in the buffer specified by outputPic.picture_address.
// ......
}
//5.7 Free the input and output buffers.
ret = hi_mpi_dvpp_free(inputPic.picture_address);
inputPic.picture_address = nullptr;
for (uint32_t i = 0; i < multiCount; i++) {
hi_mpi_dvpp_free(cropRegionInfos[i].dest_pic_info.picture_address);
cropRegionInfos[i].dest_pic_info.picture_address = nullptr;
}
//6. Destroy the channel.
ret = hi_mpi_vpc_destroy_chn(chnId);
//7. Deinitialize the media data processing system.
ret = hi_mpi_sys_exit();
//8. Release runtime resources including the context and device in sequence.
aclRet = aclrtDestroyContext(g_context);
aclRet = aclrtResetDevice(0);
//9. Deinitialize AscendCL.
aclRet = aclFinalize();
// ....