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, context, and stream in sequence.
aclrtContext context_;
aclrtStream stream_;
aclrtSetDevice(0);
aclrtCreateContext(&context_, 0);
aclrtCreateStream(&stream_);
//3. Specify cropAreas_ and pasteAreas_, which are of type acldvppRoiConfig.
acldvppRoiConfig *cropAreas_[2], pasteAreas_[2];
cropAreas_[0] = acldvppCreateRoiConfig(512, 711, 512, 711);
cropAreas_[1] = acldvppCreateRoiConfig(512, 711, 512, 711);
pasteAreas_[0] = acldvppCreateRoiConfig(16, 215, 16, 215);
pasteAreas_[1] = acldvppCreateRoiConfig(16, 215, 16, 215);
//4. Create description of the data processing channel. dvppChannelDesc_ is of type acldvppChannelDesc.
dvppChannelDesc_ = acldvppCreateChannelDesc();
//5. Create a data processing channel.
aclError ret = acldvppCreateChannel(dvppChannelDesc_);
//6. Allocate input buffer (run mode specific).
//Call aclrtGetRunMode to obtain the run mode of the software stack. If ACL_DEVICE is returned, allocate and use the device buffer. If ACL_HOST is returned, transfer the input image data to the device by using the aclrtMemcpy call. After the data transfer is complete, the buffer needs to be freed in a timely manner.
aclrtRunMode runMode;
ret = aclrtGetRunMode(&runMode);
//inputPicWidth and inputPicHeight respectively indicate the width and height of an image after alignment. The following uses a YUV420SP image as an example.
uint32_t vpcInBufferSize = inputPicWidth * inputPicHeight * 3 / 2;
if (runMode == ACL_HOST) {
//Allocate host buffer vpcInHostBuffer.
void* vpcInHostBuffer = nullptr;
vpcInHostBuffer = malloc(vpcInBufferSize);
//Load the input image into the buffer. The ReadPicFile function is defined by the user.
ReadPicFile(picName, vpcInHostBuffer, vpcInBufferSize);
//Allocate the device buffer vpcInDevBuffer_.
aclRet = acldvppMalloc(&vpcInDevBuffer_, vpcInBufferSize);
//Transfer the image data from the host to the device by using the aclrtMemcpy call.
aclRet = aclrtMemcpy(vpcInDevBuffer_, vpcInBufferSize, vpcInHostBuffer, vpcInBufferSize, ACL_MEMCPY_HOST_TO_DEVICE);
//Free the buffer in a timely manner after data transfer is complete.
free(vpcInHostBuffer);
} else {
//Allocate input buffer vpcInDevBuffer_ on the device.
ret = acldvppMalloc(&vpcInDevBuffer_, vpcInBufferSize);
//Load the input image into the buffer. The ReadPicFile function is defined by the user.
ReadPicFile(picName, vpcInDevBuffer_, vpcInBufferSize);
}
//7. Allocate output buffer vpcOutBufferDev_. The buffer size vpcOutBufferSize_ is calculated based on the formula.
//outputPicWidth and outputPicHeight respectively indicate the width and height after image alignment. The following uses a YUV420SP image as an example.
uint32_t vpcOutBufferSize_ = outputPicWidth * outputPicHeight * 3 / 2;
ret = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_)
//8. Create the description of the input image and set the attribute values.
//In this sample, the decoding output buffer is used as the input of image cropping and pasting. vpcInputDesc_ is of type acldvppPicDesc.
vpcInputBatchDesc_ = acldvppCreateBatchPicDesc(1);
vpcInputDesc_ = acldvppGetPicDesc(vpcInputBatchDesc_, 0);
acldvppSetPicDescData(vpcInputDesc_, decodeOutBufferDev_);
acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
acldvppSetPicDescWidth(vpcInputDesc_, inputWidth_);
acldvppSetPicDescHeight(vpcInputDesc_, inputHeight_);
acldvppSetPicDescWidthStride(vpcInputDesc_, jpegOutWidthStride);
acldvppSetPicDescHeightStride(vpcInputDesc_, jpegOutHeightStride);
acldvppSetPicDescSize(vpcInputDesc_, jpegOutBufferSize);
//9. Create the description of the output image and set the attribute values.
//If the cropped and pasted output image is used as the input for model inference, the output width and height must meet the requirements of the model.
//vpcOutputDesc_ is of type acldvppPicDesc.
vpcOutputBatchDesc_ = acldvppCreateBatchPicDesc(2);
for (index=0; index<2; ++index){
vecOutPtr_.push_back(vpcOutBufferDev_);
vpcOutputDesc_ = acldvppGetPicDesc(vpcInputBatchDesc_, index);
acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_);
acldvppSetPicDescFormat(vpcOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
acldvppSetPicDescWidth(vpcOutputDesc_, dvppOutWidth);
acldvppSetPicDescHeight(vpcOutputDesc_, dvppOutHeight);
acldvppSetPicDescWidthStride(vpcOutputDesc_, dvppOutWidthStride);
acldvppSetPicDescHeightStride(vpcOutputDesc_, dvppOutHeightStride);
acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_);
}
//10. Create an roiNums. Each image corresponds to the number of images to be cropped and pasted.
uint32_ttotalNum = 0;
std::unique_ptr<uint32_t[]> roiNums(new (std::nothrow) uint32_t[1]);
roiNums[0]=2;
//11. Perform asynchronous cropping and pasting, and call aclrtSynchronizeStream to wait for the stream tasks to complete.
ret = acldvppVpcBatchCropAndPasteAsync(dvppChannelDesc_, vpcInputBatchDesc_, roiNums.get(), 1,
vpcOutputBatchDesc_, cropAreas_, pasteAreas_, stream_);
ret = aclrtSynchronizeStream(stream_);
//12. After cropping and pasting are complete, destroy the allocations, including the description of the input and output image, input and output buffer, channel description, and channel.
acldvppDestroyRoiConfig(cropAreas_[0]);
acldvppDestroyRoiConfig(cropAreas_[1]);
acldvppDestroyRoiConfig(pasteAreas_[0]);
acldvppDestroyRoiConfig(pasteAreas_[1]);
(void)acldvppFree(vpcInDevBuffer_);
for(index=0; index<2; ++index){
if (runMode == ACL_HOST) {
//In this mode, the processing result is on the device. Therefore, you need to call the memory copy API to transfer the result data, and then free the device buffer.
//Allocate host buffer vpcOutHostBuffer.
void* vpcOutHostBuffer = nullptr;
vpcOutHostBuffer = malloc(vpcOutBufferSize_);
//Transfer the processing result of the device to the host by using the aclrtMemcpy call.
aclRet = aclrtMemcpy(vpcOutHostBuffer, vpcOutBufferSize_, vpcOutBufferDev_, vpcOutBufferSize_, ACL_MEMCPY_DEVICE_TO_HOST);
//Free the input and output buffer on the device.
(void)acldvppFree(vpcOutBufferDev_);
//Free the buffer after the data is used.
free(vpcOutHostBuffer);
} else {
//The process is running on the device, and the processing result is also on the device. Free the device buffer when the data is no longer needed.
(void)acldvppFree(vpcOutBufferDev_);
}
}
acldvppDestroyBatchPicDesc(vpcInputDesc_);
acldvppDestroyBatchPicDesc(vpcOutputDesc_);
acldvppDestroyChannel(dvppChannelDesc_);
(void)acldvppDestroyChannelDesc(dvppChannelDesc_);
dvppChannelDesc_ = nullptr;
//13. Release runtime resources including the stream, context, and device in sequence.
aclrtDestroyStream(stream_);
aclrtDestroyContext(context_);
aclrtResetDevice(0);
//14. Deinitialize AscendCL.
aclRet = aclFinalize();
// ....