PNGD

This section describes the API call sequence of PNGD, and sample code is also provided to help you better understand the process.

The PNG decoder (PNGD) decodes images in PNG format. For details about the PNGD function and restrictions, see Functions and Restrictions.

API Call Sequence

If PNG 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.

Figure 1 PNG image decoding

The current system supports PNG image decoding and output of RGB and RGBA images. The key APIs are described as follows:

  1. Call the acl.media.dvpp_create_channel_desc API to create channel description.
  2. Call the acl.media.dvpp_create_channel API to create a stream for image data processing.
  3. Before decoding PNG images, call acl.media.dvpp_malloc to allocate memory on the device for storing the input or output data.

    Before allocating output buffer, you can call the acl.media.dvpp_png_predict_dec_size API to estimate the size of the output buffer required for the decoded PNG image based on the buffer for storing PNG image data.

  4. Call the asynchronous API of acl.media.dvpp_png_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.

  5. After the decoding is complete, call the acl.media.dvpp_free API to free the input and output memory in a timely manner.
  6. Call the acl.media.dvpp_destroy_channel API to destroy the image data processing channel.

    Call acl.media.dvpp_destroy_channel_desc to destroy the channel description after the channel is destroyed.

Sample Code

After the API calls, add an exception handling branch and specify log printing of different levels (such as ERROR_LOG and INFO_LOG). 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
import acl
# ......

# 1. Initialize.
ACL_CONFIG_PATH = "../src/acl.json"
ret = acl.init(ACL_CONFIG_PATH)

# 2. Allocate runtime resources.

# 3. Create the description of the data processing channel. dvpp_channel_desc is of type acldvppChannelDesc.
dvpp_channel_desc = acl.media.dvpp_create_channel_desc()

# 4. Create a data processing channel.
ret = acl.media.dvpp_create_channel(dvpp_channel_desc)

# 5. Allocate the input buffer.
pic_name = "../data/test_png_dec.png"
np_png = np.fromfile(pic_name, dtype=np.byte)
in_buffer_size = np_png.itemsize * np_png.size
bytes_data = np_png.tobytes()
np_png_ptr = acl.util.bytes_to_ptr(bytes_data)
in_dev_buffer, ret = acl.media.dvpp_malloc(in_buffer_size)
ACL_MEMCPY_HOST_TO_DEVICE = 1
ret = acl.rt.memcpy(in_dev_buffer, in_buffer_size, np_png_ptr, in_buffer_size,
    ACL_MEMCPY_HOST_TO_DEVICE)

# 6. Allocate a decoding output buffer.
PIXEL_FORMAT_RGB_888 = 12
out_buffer_size, ret = acl.media.dvpp_png_predict_dec_size(np_png_ptr, in_buffer_size,
    PIXEL_FORMAT_RGB_888)
out_dev_buffer, ret = acl.media.dvpp_malloc(out_buffer_size)

# 7. Create the description of the decoded image and set the attribute values.
output_desc = acl.media.dvpp_create_pic_desc()
width, height, components, ret = acl.media.dvpp_png_get_image_info(np_png_ptr, in_buffer_size)
ret = acl.media.dvpp_set_pic_desc_data(output_desc, out_dev_buffer)
ret = acl.media.dvpp_set_pic_desc_size(output_desc, out_buffer_size)
ret = acl.media.dvpp_set_pic_desc_format(output_desc, PIXEL_FORMAT_RGB_888)

# 8. Perform asynchronous decoding and call acl.rt.synchronize_stream to wait for the stream tasks to complete.
ret = acl.media.dvpp_png_decode_async(dvpp_channel_desc, in_dev_buffer, in_buffer_size,
    output_desc, stream)
ret = acl.rt.synchronize_stream(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(output_desc)
ret = acl.media.dvpp_free(in_dev_buffer)
ret = acl.media.dvpp_free(out_dev_buffer)
ret = acl.media.dvpp_destroy_channel(dvpp_channel_desc)
ret = acl.media.dvpp_destroy_channel_desc(dvpp_channel_desc)

# 10. Destroy runtime allocations.
# Deinitialization
ret = acl.finalize()
# ....