H264 Encode HW

[中文]

1. Function overview

h264 coding is mainly used to encode YUV422 data through hardware, output h264 image data, and base on baseline profile.

2. Develop materials

Currently avdk provides a detailed sdk interface, refer to the file path:./ap/middleware/driver/h264

3. Interface description

Adjust h264 encode image quailty api: bk_h264_set_base_config(compress_ratio_t *config)

struct paramters explain: compress_ratio_t

typedef struct
{
    uint8_t init_qp;    /*The encode init QP, range[0, 51]*/
    uint8_t i_min_qp;   /*The minimum qp of I frame, range[0, 51]*/
    uint8_t i_max_qp;   /*The maximum qp of I frame, range[0, 51]*/
    uint8_t p_min_qp;   /*The minimum qp of P frame, range[0, 51]*/
    uint8_t p_max_qp;   /*The maximum qp of P frame, range[0, 51]*/
} h264_qp_t;

During H.264 encoding, I-frames and P-frames each have their own fixed quantization parameter (QP) values.
A larger QP value results in more significant compression, while a smaller QP value leads to finer detail.
The initial QP is the fixed quantization value used at the beginning of compression.
Subsequently, both the QP for I-frames and P-frames are automatically adjusted within their respective ranges:
I-frames operate within the range of [i_min_qp, i_max_qp], and P-frames operate within the range of [p_min_qp, p_max_qp].

typedef struct {
    yuv_mode_t mode;    /*invalid parameter*/
    h264_qp_t qp;       /*adjust h264 qp*/
    uint8_t  enable;    /*enable adjust func*/
    uint16_t jpeg_up;   /*invalid parameter*/
    uint16_t jpeg_low;  /*invalid parameter*/
    uint16_t imb_bits;  /*The size of I frame marco-block, unit byte, The value more larger, the decode ratio more smaller, the image quality more heigher, the output frame size more bigger, range[1, 4095]*/
    uint16_t pmb_bits;  /*The size of P frame marco-block, unit byte, The value more larger, the decode ratio more smaller, the image quality more heigher, the output frame size more bigger, range[1, 4095]*/
} compress_ratio_t;

Note

In addition to ensuring the values are within the valid range, it’s crucial that the maximum value for I/P frame encoding is greater than its minimum value for the settings to take effect. These interfaces allow you to adjust image quality, but be aware that increasing image quality will inevitably increase data volume. Currently, the maximum output space for one frame of yuv422 data after encoding is 64KB, if you adjust the image quality.

Regenerate IDR frames: During encoding, the process always cycles through GOPs (Group of Pictures) - one I-frame followed by N P-frames, where N is controlled by a macro setting. To allow for restarting a new GOP cycle at any time, we provide an interface to begin a new GOP with the next frame.

bk_err_t bk_h264_soft_reset(void);

Here are a few options for translating that sentence into English, depending on the nuance you want to convey. They all aim to capture the idea that you don’t directly use the low-level driver, but instead use higher-level component interfaces:

- For UVC door lock solution, api: ``h264_jdec_pipeline_regenerate_idr_frame``
- For DVP door lock solution, api: ``bk_dvp_h264_idr_reset``

4. Code gear adjust

The default SDK provides the configuration of h264 encoded gear adjustment, which is defined by the configuration macro: CONFIG_H264_QUALITY_LEVEL, which is described as follows:

  • Value range: [0, 3], define three gears, the values are 1/2/3, corresponding to the h264 compressed image quality from low to high, the clearer the image is.

  • If CONFIG_H264_QUALITY_LEVEL=0, the default value is used instead of the three-gear parameter. The default value reference path: .\bk_idk\middleware\soc\bk7258\hal\h264_default_config.h, you can change the default value to achieve the desired effect.

  • The default SDK gear is defined in the middle gear, CONFIG_H264_QUALITY_LEVEL=2. You can change this value in project config.

5.Adjustment of P-Frame Count

Under the default SDK configuration, the number of P-frames per GOP in H264 encoding is 5, which can be adjusted through the macro: CONFIG_H264_P_FRAME_CNT. The valid range is [0, 1023]. Increasing the number of P-frames can lead to a reduction in the overall bitrate of the encoded output.

6.Suggestions for Adjusting Image Quality

The SDK provides three predefined H264 image quality levels, which can be controlled through the macro: CONFIG_H264_QUALITY_LEVEL. Adjusting image quality primarily involves parameters within the structure of: compress_ratio_t.

Debugging Steps:

1.Adjust the number of P-frames: Increase the number of P-frames based on the variation in image content to reduce bitrate.

2.Adjust the frame buffer size: The default is 64K. Adjust it as needed to prevent I-frames from being too large and causing the output to fail. It is recommended to set the CONFIG_H264_FRAME_SIZE to 102400 (100K).

3.Configure the H264 image quality level: Use CONFIG_H264_QUALITY_LEVEL=0 to use the SDK’s original encoding parameters.

4.Real-time compression rate adjustment: Monitor image quality and bitrate simultaneously, and use a controlled variable method to change one parameter at a time.

5.Obtain/configure encoding parameters: Use the command-line tool to get encoding parameters. To obtain: media h264 get_config, and to configure: media compress h264 init_qp iframe_max_qp pframe_max_qp num_ibits num_pbits.

6.Adjust the initial quantization parameter (init_qp): Start from 1 and increase by 5 each time, with a maximum value of no more than 51 until the image does not show obvious macroblock motion.

7.Adjust the I-frame encoding parameter (num_ibits): Increase this value by 20 each time until the image quality is satisfactory, but be cautious not to increase it too much to avoid increased bitrate, with a suggested maximum value of no more than 200.

8.Adjust the P-frame encoding parameter (num_pbits): Adjust this similarly, but be cautious not to increase it too much to avoid increased bitrate; a suggested maximum value is no more than 150.

9.Repeat steps 7-8: Continue adjusting until the best parameters are found.

10.Adjust the maximum quantization parameters for I-frames and P-frames (iframe_max_qp and pframe_max_qp): Reducing these values can improve image quality, but will also increase bitrate. These should not be set lower than 32.

11.Adjust the frame buffer size: After achieving the final effect, if the I-frame size does not exceed 64K, you can revert it to the default value CONFIG_H264_FRAME_SIZE=65536 (64K).

Through these steps, you can optimize the number of P-frames and image quality in the H264 encoding process to meet specific application requirements.