JPEGD
The JPEG decoder (JPEGD) can decode .jpg, .jpeg, .JPG, and .JPEG images into YUV images. For details about the JPEGD function and restrictions, see Functions and Restrictions.
This section describes the API call sequence of JPEGD, and sample code is also provided to help you better understand the sequence.
API Call Sequence
If JPEG image decoding is involved during application development, the application must contain the code logic for image decoding. For details about the API call sequence of image decoding, see API Call Sequence.
The current system supports .jpg, .jpeg, .JPG, and .JPEG image decoding. Different source image encoding formats lead to different output formats. The key APIs are described as follows:
- Call the acl.media.dvpp_create_channel_desc API to create channel description.
- Call the acl.media.dvpp_create_channel API to create a stream for image data processing.
- Before decoding JPEG images, call acl.media.dvpp_malloc to allocate memory on the device for storing the input or output data.
Before allocating output buffer, call the acl.media.dvpp_jpeg_predict_dec_size API to estimate the output buffer size required for the decoded JPEG image based on the buffer for storing JPEG image data.
- Call the asynchronous API acl.media.dvpp_jpeg_decode_async for decoding.
For asynchronous APIs, the acl.rt.synchronize_stream API needs to be called to block program running until all tasks in the specified stream are complete.
- After the decoding is complete, call the acl.media.dvpp_free API to free the input and output memory in a timely manner.
- Call the acl.media.dvpp_destroy_channel API to destroy the image data processing channel.
Call the acl.media.dvpp_destroy_channel_desc API to destroy the channel description after the image data processing channel is destroyed.
Sample Code
You can obtain the complete sample code by referring to Obtaining a Sample.
Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code snippet of key steps only, which is not ready to use.
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 |
import acl # ...... # 1. Perform initialization. ret = acl.init() # 2. Allocate runtime resources. # 3. Create a description of the data processing channel. self.dvpp_channel_desc is of type acldvppChannelDesc. self.dvpp_channel_desc = acl.media.dvpp_create_channel_desc() # 4. Create a data processing channel. ret = acl.media.dvpp_create_channel(self.dvpp_channel_desc) # 5. Allocate buffers. # Load the YUV image data. # Allocate the device buffer inputDevBuff. # Copy image data from the host to the device by calling acl.rt.memcpy. After data copy is complete, free the host memory in a timely manner. np_jpg = np.fromfile(self.path, dtype=np.byte) self.in_buffer_size = np_jpg.itemsize * np_jpg.size bytes_data = np_jpg.tobytes() np_jpg_ptr = acl.util.bytes_to_ptr(bytes_data) self.in_buffer_dev, ret = acl.media.dvpp_malloc(self.in_buffer_size) ret = acl.rt.memcpy(self.in_buffer_dev, self.in_buffer_size, np_jpg_ptr, self.in_buffer_size, 1) # 6 Calculate and allocate the output device buffer self.out_buffer_dev in 16-pixel-aligned mode to store the encoded output data. height_stride = ((h + 1) // 2) * 2 width_stride = ((w + 15) // 16) * 16 # malloc for output self.out_buffer_size = (width_stride * height_stride * 3) // 2 self.out_buffer_dev, ret = acl.media.dvpp_malloc(self.out_buffer_size) # 7. Create the description of the decoded image and set the attribute values. # Set the output buffer and size to the picture description for data placeholder. self.pic_desc = acl.media.dvpp_create_pic_desc() ret = acl.media.dvpp_set_pic_desc_data(self.pic_desc, self.out_buffer_dev) ret = acl.media.dvpp_set_pic_desc_size(self.pic_desc, self.out_buffer_size) ret = acl.media.dvpp_set_pic_desc_format( self.pic_desc, PIXEL_FORMAT_YUV_SEMIPLANAR_420) ret = acl.media.dvpp_set_pic_desc_width(self.pic_desc, w) ret = acl.media.dvpp_set_pic_desc_height(self.pic_desc, h) ret = acl.media.dvpp_set_pic_desc_width_stride(self.pic_desc, width_stride) ret = acl.media.dvpp_set_pic_desc_height_stride(self.pic_desc, height_stride) # 8. Perform asynchronous decoding and call acl.rt.synchronize_stream to block the host until all tasks in the specified stream are complete. ret = acl.media.dvpp_jpeg_decode_async(self.dvpp_channel_desc, self.in_buffer_dev, self.in_buffer_size, self.pic_desc, self.stream) ret = acl.rt.synchronize_stream(self.stream) # 9. After the decoding is complete, destroy the allocations, including the description of the output image, output buffer, description of the channel as well as the channel. ret = acl.media.dvpp_destroy_pic_desc(self.pic_desc) ret = acl.media.dvpp_free(self.in_buffer_dev) ret = acl.media.dvpp_free(self.out_buffer_dev) ret = acl.media.dvpp_destroy_channel(self.dvpp_channel_desc) ret = acl.media.dvpp_destroy_channel_desc(self.dvpp_channel_desc) # 10. Deallocate runtime resources. # Deinitialization ret = acl.finalize() # ...... |