The actual size may be different from the size predicated by using the acldvppJpegPredictEncSize call. To obtain the actual output allocation size, inspect the output size of the acldvppJpegEncodeAsync call.
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, context, and stream in sequence.
aclrtContext context_;
aclrtStream stream_;
aclrtSetDevice(0);
aclrtCreateContext(&context_, 0);
aclrtCreateStream(&stream_);
//3. Create description of the data processing channel. dvppChannelDesc_ is of type acldvppChannelDesc.
dvppChannelDesc_ = acldvppCreateChannelDesc();
//4. Create a data processing channel.
aclRet = acldvppCreateChannel(dvppChannelDesc_);
//5. 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 PicBufferSize = inputPicWidth * inputPicHeight * 3 / 2;
if (runMode == ACL_HOST) {
//Allocate host buffer vpcInHostBuffer.
void* vpcInHostBuffer = nullptr;
vpcInHostBuffer = malloc(PicBufferSize);
//Load the input image into the buffer. The ReadPicFile function is defined by the user.
ReadPicFile(picName, vpcInHostBuffer, PicBufferSize);
//Allocate the device buffer inDevBuffer_.
aclRet = acldvppMalloc(&inDevBuffer_, PicBufferSize);
//Transfer the input image data to the device by using the aclrtMemcpy call.
aclRet = aclrtMemcpy(inDevBuffer_, PicBufferSize, vpcInHostBuffer, PicBufferSize, ACL_MEMCPY_HOST_TO_DEVICE);
//Free the buffer in a timely manner after data transfer is complete.
free(vpcInHostBuffer);
} else {
//Allocate input buffer inDevBuffer_ on the device.
ret = acldvppMalloc(&inDevBuffer_, PicBufferSize);
//Load the input image into the buffer. The ReadPicFile function is defined by the user.
ReadPicFile(picName, inDevBuffer_, PicBufferSize);
}
//6. Create the description of the input image to be encoded and set the attribute values.
//encodeInputDesc_ is of type acldvppPicDesc.
encodeInputDesc_ = acldvppCreatePicDesc();
acldvppSetPicDescData(encodeInputDesc_, reinterpret_cast<void *>(inDevBuffer_));
acldvppSetPicDescFormat(encodeInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
acldvppSetPicDescWidth(encodeInputDesc_, inputWidth_);
acldvppSetPicDescHeight(encodeInputDesc_, inputHeight_);
acldvppSetPicDescWidthStride(encodeInputDesc_, encodeInWidthStride);
acldvppSetPicDescHeightStride(encodeInputDesc_, encodeInHeightStride);
acldvppSetPicDescSize(encodeInputDesc_, inDevBufferSizeE_);
//7. Create image encoding configuration data and set encoding quality.
//The encoding level range is [0, 100]. The quality of level 0 is similar to that of level 100. A smaller value in the range (1, 100) indicates poorer output image quality.
jpegeConfig_ = acldvppCreateJpegeConfig();
acldvppSetJpegeConfigLevel(jpegeConfig_, 100);
//8. Allocate output buffer and device buffer encodeOutBufferDev_ to store the encoded output data.
uint32_t outBufferSize= 0;
ret = acldvppJpegPredictEncSize(encodeInputDesc_, jpegeConfig_, &outBufferSize);
ret = acldvppMalloc(&outBufferDev_, outBufferSize)
//9. Perform asynchronous encoding and call aclrtSynchronizeStream to wait for the stream tasks to complete.
aclRet = acldvppJpegEncodeAsync(dvppChannelDesc_, encodeInputDesc_, encodeOutBufferDev_,
&outBufferSize, jpegeConfig_, stream_);
aclRet = aclrtSynchronizeStream(stream_);
//10. Destroy the allocations, including the description of the input and output image, input and output buffer, channel description, channel.
acldvppDestroyPicDesc(encodeInputDesc_);
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 outputHostBuffer.
void* outputHostBuffer = nullptr;
outputHostBuffer = malloc(outBufferSize_);
//Transfer the processing result of the device to the host by using the aclrtMemcpy call.
aclRet = aclrtMemcpy(outputHostBuffer, outBufferSize_, encodeOutBufferDev_, outBufferSize_, ACL_MEMCPY_DEVICE_TO_HOST);
//Free the input and output buffer on the device.
(void)acldvppFree(inputDevBuff);
(void)acldvppFree(encodeOutBufferDev_);
//Free the buffer after the data is used.
free(outputHostBuffer);
} 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(inputDevBuff);
(void)acldvppFree(encodeOutBufferDev_);
}
acldvppDestroyChannel(dvppChannelDesc_);
(void)acldvppDestroyChannelDesc(dvppChannelDesc_);
dvppChannelDesc_ = nullptr;
//11. Release runtime resources including the stream, context, and device in sequence.
aclrtDestroyStream(stream_);
aclrtDestroyContext(context_);
aclrtResetDevice(0);
//12. Deinitialize AscendCL.
aclRet = aclFinalize();
// ....