API Call Sequence

For the Ascend 310 AI Processor, this function is not supported in the current version.

For the Ascend 910 AI Processor, this function is not supported in the current version.

If a YUV image needs to be encoded into a JPEG image during app development, ensure that your app contains the code logic for such image encoding. For details about the API call sequence, see API Call Sequence. For details about the JPEGE functions, see JPEGE Functions and Restrictions.

Figure 1 API call sequence
The current system supports encoding YUV images into JPEG images. The key APIs are described as follows:
  1. Call hi_mpi_sys_init to initialize the media data processing system.
  2. Call hi_mpi_venc_create_chn to create a channel.

    After creating a channel, you can set advanced encoding parameters such as the scene mode and stream controller as required. For details, see hi_mpi_venc_set_jpeg_param to hi_mpi_venc_compact_jpeg_tables.

  3. Call hi_mpi_venc_get_fd to obtain the file descriptor corresponding to the channel ID.
  4. Call hi_mpi_sys_create_epoll to create a DVPP epoll instance.
  5. Call hi_mpi_sys_ctl_epoll to add the file descriptor corresponding to the encoding channel to the epoll instance.

    Skip this step if the select or poll function is used.

  6. Call hi_mpi_venc_start_chn to notify the channel to start encoding.
  7. Call hi_mpi_dvpp_malloc to allocate the device buffer to store the input data.
  8. Start a user-mode thread and call hi_mpi_sys_wait_epoll to wait for the epoll instance to complete encoding.
  9. Call hi_mpi_venc_send_frame to feed your stream to the encoder.
  10. Once the encoding is complete, the hi_mpi_sys_wait_epoll, select, or poll function returns a value. Call hi_mpi_venc_query_status to query the encoding status and call hi_mpi_venc_get_stream to obtain the encoding result.
  11. Call hi_mpi_venc_release_stream to free the buffer in time when the encoding result is no longer needed. Otherwise, no more encoding task can be performed because the encoding buffer is used up.
  12. Call hi_mpi_dvpp_free to free the input buffer.
  13. When the source images have been completely fed, call hi_mpi_venc_stop_chn to stop the channel from receiving new images.
  14. Call hi_mpi_sys_ctl_epoll to delete the encoding channel file descriptor from the epoll instance.
  15. After all encoding tasks are complete, call hi_mpi_venc_destroy_chn to destroy the encoding channel and free the memory.
  16. Call hi_mpi_sys_close_epoll to destroy the DVPP epoll instance.
  17. Call hi_mpi_sys_exit to deinitialize the media data processing system.
The output buffer can be managed by DVPP internally or by users.
  • Managed by DVPP: Call hi_mpi_venc_send_frame to send source images for encoding.

    When creating a channel by calling hi_mpi_venc_create_chn, you must properly set hi_venc_chn_attr.venc_attr.buf_size. For details about the parameter description, see hi_venc_attr.

    In this mode, the JPEG header of the output data does not contain the COM comment field, and the data length is smaller. However, you need to copy the output data from the buffer returned by DVPP to the specified memory.

  • Managed by users: Call hi_mpi_venc_send_jpege_frame to send source images for encoding.

    When creating a channel by calling hi_mpi_venc_create_chn, set hi_venc_chn_attr.venc_attr.buf_size to 0 (for details about the parameter description, see hi_venc_attr). Call hi_mpi_venc_get_jpege_predicted_size to predict the output buffer size and call hi_mpi_dvpp_malloc or hi_mpi_dvpp_free to allocate or free the output buffer.

    In this mode, the output buffer address is set when hi_mpi_venc_send_jpege_frame is called. The output data is directly stored in the buffer set by the user. Compared with the other mode, this mode does not require you to copy the output data from the buffer returned by DVPP to the specified memory. However, the JPEG header of the output data may contain the COM comment field (whose length ranges from 4 bytes to 19 bytes), and thus the data length is larger.