JPEGE
This section describes the API call sequence of JPEGE, and sample code is also provided to help you better understand the process.
The JPEG encoder (JPEGE) encodes YUV images into .jpg images. For details about the JPEGE function and restrictions, see DVPP Media Acceleration Library.
API Call Sequence

The current system supports YUV to JPG image encoding. The key APIs are described as follows:
- Call aclInit to initialize the system.
- Call aclrtSetDevice to specify the compute device.
- aclrtCreateStream: creates a stream.
- Call acldvppCreateChannel to create a channel for image data processing.
Before creating a channel, call <cf id="Bold">acldvppCreateChannelDesc</cf> to create the channel description.
- Call acldvppCreateJpegeConfig to create image encoding configuration data.
- Before JPEG image encoding, call acldvppMalloc to allocate memory on the device for storing the input or output data.
Before allocating output memory, call <cf id="Bold">acldvppJpegPredictEncSize</cf> to predict the required output allocation size based on the input image description and the image encoding configuration.
The actual output buffer size may be different from that estimated by calling acldvppJpegPredictEncSize. To obtain the actual output buffer size after encoding, call the output parameter size of acldvppJpegEncodeAsync.
- Call the asynchronous API acldvppJpegEncodeAsync for encoding.
For asynchronous APIs, call aclrtSynchronizeStream to block program running until all tasks in the specified stream are complete.
- Call acldvppDestroyJpegeConfig to destroy the image encoding configuration data.
- After the encoding is complete, call acldvppFree to free the input and output memory in a timely manner.
- Call acldvppDestroyChannel to destroy the image data processing channel.
After destroying the channel, call <cf id="Bold">acldvppDestroyChannelDesc</cf> to destroy the channel description.
- aclrtDestroyStream: destroys a stream.
- Call aclrtResetDevice to reset the device and free the resources on the device.
- Call aclFinalize to deinitialize the system and free the resources used by the acl API in the process.
Sample Code
The following is a code example of the key steps of JPEGE image encoding. It is for reference only and cannot be directly copied for compilation and running. After APIs are called, you need to add exception handling branches and record error logs and info logs.
You can click vpc_jpeg_resnet50_imagenet_classification to obtain the sample.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | // 1. Create the description of the image data processing channel. dvppChannelDesc_ is of the acldvppChannelDesc type. dvppChannelDesc_ = acldvppCreateChannelDesc(); // 2. Create a channel for image data processing. aclError ret = acldvppCreateChannel(dvppChannelDesc_); // 3. Allocate input buffer (differentiated by running status). // 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_. ret = acldvppMalloc(&inDevBuffer_, PicBufferSize); // Transfer the input image data to the device by using the aclrtMemcpy call. ret = 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); } // 4. Create the description of the input image 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_); // 5. Create image encoding configuration data and set the 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); // 6. Allocate the output buffer and device buffer encodeOutBufferDev_ to store the encoded output data. uint32_t outBufferSize= 0; ret = acldvppJpegPredictEncSize(encodeInputDesc_, jpegeConfig_, &outBufferSize); ret = acldvppMalloc(&encodeOutBufferDev_, outBufferSize); // 7. Perform asynchronous encoding and call aclrtSynchronizeStream to block program running until all tasks in the specified stream are complete. ret = acldvppJpegEncodeAsync(dvppChannelDesc_, encodeInputDesc_, encodeOutBufferDev_, &outBufferSize, jpegeConfig_, stream_); ret = aclrtSynchronizeStream(stream_); // 8. After the encoding is complete, release resources, including the description of the input/output image, input/output buffer, channel description, and 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. ret = aclrtMemcpy(outputHostBuffer, outBufferSize, encodeOutBufferDev_, outBufferSize, ACL_MEMCPY_DEVICE_TO_HOST); // Free the input and output buffer on the device. (void)acldvppFree(inDevBuffer_); (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(inDevBuffer_); (void)acldvppFree(encodeOutBufferDev_); } acldvppDestroyChannel(dvppChannelDesc_); (void)acldvppDestroyChannelDesc(dvppChannelDesc_); dvppChannelDesc_ = nullptr; .... |