H264 macroblock level rate control method for hardware implementation
The H.264 hardware coding method, which uses a 5-stage pipeline to calculate quantization parameters, solves the problems of high complexity and high resource consumption in hardware implementation, simplifies the hardware implementation process, and allows for flexible configuration in different scenarios.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- ALLWINNER TECH CO LTD
- Filing Date
- 2018-07-10
- Publication Date
- 2026-05-29
AI Technical Summary
Existing H.264 rate control algorithms suffer from high complexity and resource consumption in hardware implementation, especially in macroblock-level rate control, making it difficult to effectively solve the chicken-and-egg paradox.
H.264 hardware encoding is implemented using a 5-stage pipeline, including the IME module, FME module, IPTQ module, DBK module, and EE module. The quantization parameter is calculated in stages. First, the coarse quantization parameter coarse_qp is calculated, and then the precise quantization parameter mb_qp is calculated based on the texture characteristics and MAD value of the macroblock, which simplifies the hardware implementation process.
While ensuring coding performance, it greatly simplifies the complexity of hardware implementation, reduces resource consumption, and allows for flexible configuration to adapt to different scenarios.
Smart Images

Figure CN108900840B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image algorithm coding, specifically a hardware-implemented H264 macroblock-level bitrate control method. Background Technology
[0002] In video transmission, the encoded and compressed bitstream information often needs to be transmitted over a network with limited and variable bandwidth. In order to make the encoded video stream adapt well to the bandwidth and avoid the receiving end from overflowing the bitstream buffer due to excessively high instantaneous bitrate or stuttering due to excessively low bitrate, it is necessary to control the bitrate of the encoded video stream during the video encoding process. This will enable a relatively stable output video stream under certain bandwidth constraints, while obtaining good video quality.
[0003] In H.264 rate control algorithms, the simultaneous use of quantization parameters in both rate control and rate distortion optimization stages leads to the chicken-and-egg paradox: when performing rate distortion optimization on the current macroblock, the MAD (Modal Amount) of the current macroblock needs to be calculated first, and then the quantization parameter QP (Quantization Parameter) of the current macroblock can be obtained through the rate distortion optimization algorithm. However, the MAD of the current macroblock can only be obtained by selecting the optimal coding mode for each macroblock using the H.264 coding algorithm, given that the quantization parameter QP is known. This results in the chicken-and-egg paradox. To address this issue, the official H.264 standard organization, JVT, adopted a superior rate control proposal, JVT-G012. This proposal uses a linear MAD model to predict the MAD value of the current macroblock from the MAD value of the corresponding macroblock in the previous frame, thus avoiding the chicken-and-egg paradox.
[0004] Although the JVT-G012 algorithm effectively solves the chicken-and-egg paradox in rate control during H.264 encoding, it is software-based and not hardware-friendly. In particular, using this rate control algorithm at the macroblock level makes hardware implementation very complex and resource-intensive. Moreover, hardware design typically employs pipelined design, and related parameters are dependent on each other, further increasing the difficulty of hardware implementation. Summary of the Invention
[0005] To address the aforementioned problems in the prior art, this invention provides a hardware-implemented H264 macroblock-level bitrate control method, which includes a 5-stage pipeline for H264 hardware encoding, specifically comprising an IME module, an FME module, an IPTQ module, a DBK module, an EE module, and a WBK module.
[0006] Furthermore, the IME module primarily implements rate control and integer pixel search. Rate control calculates a coarse quantization parameter, coarse_qp, for the current macroblock. This quantization parameter is only used by the IME and FME modules. After determining the macroblock type through mode decision-making, the macroblock's quantization parameter is recalculated in the IPTQ module. The calculation of coarse_qp is based on the texture of the current macroblock. For macroblocks with complex textures and rich details, the quantization parameter is appropriately increased based on slice_qp; for macroblocks with simple and smooth textures, the quantization parameter is appropriately decreased based on slice_qp. The texture calculation algorithm for the current macroblock is relatively simple and does not depend on surrounding macroblocks, making it convenient for hardware implementation. Integer pixel search is used to find the block that best matches the current macroblock from the reference frame within the P / B frame. There are many commonly used search algorithms; for ease of hardware implementation, Diamond Search (DS) can be used. After integer pixel search, the optimal block type for the current macroblock's inter type and the optimal integer pixel mv information corresponding to each block can be obtained.
[0007] Furthermore, the FME module primarily implements luminance intra prediction, pixel-level search, mode decision, and transform quantization, inverse quantization, inverse transform, and reconstruction. The quantization parameter used in this step is the coarse_qp calculated by the IME module. Luminance intra prediction mainly performs intra-frame predictions of intra4x4, intra8x8, and intra16x16 on the luminance data of the current block, selecting the optimal prediction mode when the current macroblock is encoded as the intra type. Pixel-level search, based on the previous level integer pixel search, performs more refined 1 / 2 and 1 / 4 searches to find the optimal mv information corresponding to each block type. The mode decision algorithm compares the cost of encoding as the intra macroblock type and the inter macroblock type, selecting the minimum cost as the final encoding type for the current macroblock. Transform quantization and inverse quantization are an inverse pair of operations. Transform quantization performs integer DCT transform and quantization on the difference value obtained by subtracting the predicted value from the original pixel of the current macroblock to obtain the residual data to be entropy encoded. Inverse quantization and inverse transform perform inverse quantization and inverse DCT transform on this residual data. Adding the predicted value, the reconstructed data of the luminance component of the current macroblock can be obtained, and thus the mad under the current coarse quantization parameter coarse_qp can be obtained.
[0008] Furthermore, the IPTQ module primarily implements macroblock QP calculation, intra prediction, chroma inter interpolation, transform quantization, inverse quantization and inverse transform, and reconstruction. After pipeline processing by the preceding two-level IME and FME modules, the macroblock type of the current macroblock has been finalized, and the mad value of the macroblock under the quantization parameter coarse_qp can be obtained. At this point, the more accurate quantization parameter mb_qp of the macroblock is recalculated based on this mad value. This mb_qp value is the true quantization parameter of the macroblock, and it is used for calculations in the IPTQ module and the subsequent DBK and EE modules. Intra prediction is initiated when the current macroblock is determined to be of the intra macroblock type. The intra type luminance prediction value is recalculated using the new quantization parameter mb_qp, and the optimal intra prediction mode for the chroma block needs to be calculated simultaneously. Chromaticity interpolation is initiated when the current macroblock is determined to be of type inter. It obtains the chromaticity MV information based on the luminance MV information, reads the corresponding chromaticity reference frame data from the reference frame based on this MV information, and completes the interpolation operation to obtain the chromaticity component interpolation result. Transform quantization and inverse quantization / inverse transform are an inverse pair of operations. Transform quantization performs integer DCT transform and quantization on the difference value obtained by subtracting the predicted value from the original pixels of the current macroblock, obtaining the residual data to be entropy encoded. Inverse quantization and inverse transform perform inverse quantization and inverse DCT transform on this residual data, and adding the predicted value yields the reconstructed data of the current macroblock.
[0009] Furthermore, the DBK module and EE module mainly implement in-loop filtering of the current macroblock and CABAC or CAVLC entropy coding of the residual data.
[0010] Furthermore, the WBK module mainly implements the write-back of encoded data, including reconstructed data and encoded bitstream data, and outputs it to an external DDR memory.
[0011] The beneficial effects of the present invention are: (1) The present invention provides a hardware-implemented H264 macroblock-level bitrate control method. In the hardware implementation of macroblock-level bitrate control, the calculation of the macroblock quantization parameter QP is divided into two levels. First, a coarse quantization parameter coarse_qp is calculated. This coarse quantization parameter coarse_qp is used for calculation in the IME module and FME module. After the FME module obtains the macroblock type of the current macroblock through mode decision, the mad value of the macroblock under the quantization parameter coarse_qp can be obtained. The more accurate quantization parameter mb_qp of the macroblock is recalculated based on the mad. This quantization parameter mb_qp is used for calculation in the IPTQ module and the subsequent DBK and EE modules. By dividing the calculation of the quantization parameter QP for macroblock-level bitstream control into two levels, the first two pipeline stages use the coarse quantization parameter coarse_qp, and the last three pipeline stages use the more accurate quantization parameter mb_qp, which greatly facilitates the hardware implementation while ensuring coding performance.
[0012] (2) When calculating the coarse quantization parameter coarse_qp, the calculation is simple based on the texture of the current macroblock and does not depend on the surrounding macroblocks, which is convenient for hardware implementation. Attached Figure Description
[0013] Figure 1 This is a schematic diagram of the H264 encoded macroblock pipeline in this invention.
[0014] Figure 2 This is a framework diagram of the H264 encoding algorithm in this invention.
[0015] Figure 3 This is a schematic diagram of the H264 encoded pixel-by-pixel search algorithm in this invention.
[0016] The present invention will be further described below with reference to the accompanying drawings and embodiments. Detailed Implementation
[0017] In this invention, to reduce complexity and facilitate hardware implementation, the H.264 encoding algorithm is implemented using a 5-stage pipeline, such as... Figure 1As shown, the pipeline consists of the IME module, FME module, IPTQ module, DBK module, EE module, and WBK module, with the DBK and EE modules operating in the same pipeline. At any given time, each module in the five pipelines is working, but processing different macroblocks. When the IME module begins its integer-pixel search for the nth macroblock, the FME module has just begun its sub-pixel search for the (n-1)th macroblock. The IPTQ module begins processing the (n-2)th macroblock. The DBK and EE modules then begin in-loop filtering and entropy coding operations on the (n-3)th macroblock. Finally, the WBK module writes back the data encoded from the (n-4)th macroblock. Only at this stage of the pipeline is the encoding of a macroblock truly completed. The algorithm functions implemented at each pipeline stage are shown in [link to pipeline description]. Figure 2 As shown, a detailed description follows.
[0018] (1) IME module
[0019] The IME module is at stage 1 of the pipeline, and its main algorithmic functions include rate control and integer pixel search. Before macroblock pipeline starts, the quantization parameter QP of the current macroblock needs to be obtained through the rate control algorithm. This quantization parameter QP is used to calculate the optimal matching point in the subsequent integer pixel search. Since calculating the quantization parameter QP of the current macroblock in the H.264 officially adopted stream control proposal JVT-G012 is too complex and unsuitable for hardware implementation, this invention uses a simplified rate control algorithm to calculate the QP of the current macroblock.
[0020] First, calculate the mean value y_avg of the current macroblock's luminance component and the sum of the absolute values of each pixel after subtracting the mean value y_sum. The calculation formula is as follows:
[0021]
[0022]
[0023] The same algorithm is used to obtain the mean values u_avg and v_avg of the chrominance components, as well as the sum of the absolute values u_sum and v_sum of the chrominance components after subtracting the mean. The calculation formulas are as follows:
[0024]
[0025]
[0026]
[0027]
[0028] The total sum of the absolute values of the three components y, u, and v is obtained by summing the values of the components y, u, and v. The formula is as follows:
[0029]
[0030] The total accumulated value, `total_sum`, reflects whether the current macroblock is a smooth block or a texture block. If it's a texture block, the calculated `total_sum` will be larger. By comparing it with three thresholds, the QP of the current macroblock is appropriately increased based on `slice_qp`. If it's a smooth block, `total_sum` will be smaller. Similarly, by comparing it with the three thresholds, the QP of the current macroblock is appropriately decreased based on `slice_qp`, thus dynamically adjusting the macroblock's quantization parameter, `QP`. The calculation formula is as follows:
[0031] if (total_sum <= RC_MB_TH0)
[0032] {
[0033] coarse_qp = slice_qp - RC_MB_QP_OFFSET0;
[0034] }
[0035] else if (total_sum<= RC_MB_TH1)
[0036] {
[0037] coarse_qp = slice_qp - RC_MB_QP_OFFSET1;
[0038] }
[0039] else if (total_sum<= RC_MB_TH2)
[0040] {
[0041] coarse_qp = slice_qp - RC_MB_QP_OFFSET2;
[0042] }
[0043] else if (total_sum<= RC_MB_TH3)
[0044] {
[0045] coarse_qp = slice_qp + RC_MB_QP_OFFSET3;
[0046] }
[0047] else if (total_sum<= RC_MB_TH4)
[0048] {
[0049] coarse_qp = slice_qp + RC_MB_QP_OFFSET4;
[0050] }
[0051] else if (total_sum<= RC_MB_TH5)
[0052] {
[0053] coarse_qp = slice_qp + RC_MB_QP_OFFSET5;
[0054] }
[0055] else
[0056] {
[0057] coarse_qp = slice_qp + RC_MB_QP_OFFSET6;
[0058] }
[0059] Here, `slice_qp` represents the quantization parameters of the current slice calculated by the software. The thresholds RC_MB_TH0 to RC_MB_TH5 and the offsets RC_MB_QP_OFFSET0 to RC_MB_QP_OFFSET6 are configured to the hardware via registers, allowing for flexible hardware configuration to adapt to different scenarios. In typical application scenarios, experiments have shown that the thresholds RC_MB_TH0 to RC_MB_TH5 can be configured to the following values:
[0060] RC_MB_TH0 = (unsigned int ) (0.4 * last_pic_avg_sum)
[0061] RC_MB_TH1 = (unsigned int ) (0.6 * last_pic_avg_sum)
[0062] RC_MB_TH2 = (unsigned int ) (0.8 * last_pic_avg_sum)
[0063] RC_MB_TH3 = (unsigned int ) (1.2 * last_pic_avg_sum)
[0064] RC_MB_TH4 = (unsigned int ) (1.6 * last_pic_avg_sum)
[0065] RC_MB_TH5 = (unsigned int ) (2.0 * last_pic_avg_sum)
[0066] Where last_pic_avg_sum is the average of the total_sum of all macroblocks in the previous frame.
[0067] RC_MB_QP_OFFSET0 to RC_MB_QP_OFFSET6 can be configured to the following values:
[0068] RC_MB_QP_OFFSET0 = 3
[0069] RC_MB_QP_OFFSET1 = 2
[0070] RC_MB_QP_OFFSET2 = 1
[0071] RC_MB_QP_OFFSET3 = 0
[0072] RC_MB_QP_OFFSET4 = 1
[0073] RC_MB_QP_OFFSET5 = 2
[0074] RC_MB_QP_OFFSET6 = 3
[0075] The quantization parameter QP obtained from the rate control calculation at this stage is a coarse coarse QP, not the actual macroblock quantization parameter QP. It is only used in the IME and FME modules. The actual macroblock quantization parameter mb_qp is recalculated in the IPTQ module based on the macroblock's mad.
[0076] Integer pixel search is used to find the block that best matches the current macroblock from the reference frame within a P / B frame. Commonly used search algorithms include Full Search (FS), Three-Step Search (TSS), New Three-Step Search (NTSS), Four-Step Search (FSS), and Diamond Search (DS). For ease of hardware implementation, Diamond Search (DS) can be used; this algorithm is relatively universal and will not be elaborated upon here. After integer pixel search, the optimal block type of the current macroblock (inter type) and the optimal integer pixel MV information (bestmv_ime_x, bestmv_ime_y) corresponding to each block are obtained.
[0077] (2) FME module
[0078] The FME module is located at stage 2 of the pipeline. Its main algorithmic functions include luminance intra-prediction, pixel-level search, mode decision, and transform quantization, inverse quantization, inverse transform, and reconstruction. Luminance intra-prediction performs intra-frame prediction on the luminance data of the current block using intra4x4, intra8x8, and intra16x16 methods. Based on cost = satd + lamda * bit, it selects the minimum cost as the optimal prediction mode when the current macroblock is encoded as intra type. Pixel-level search, building upon the integer-pixel search in the previous pipeline, more accurately matches each block with the reference frame. It first finds eight surrounding 1 / 2-pixel points centered on the integer pixel (bestmv_ime_x, bestmv_ime_y). Figure 3 As shown, a pixel-by-pixel search is performed to obtain the optimal 1 / 2 pixel matching point. The judgment criterion is also cost = satd + lamda * bit. The 1 / 2 pixel point with the smallest cost is selected as the optimal matching point. If the 1 / 2 pixel point in the upper left corner is optimal, then the four surrounding 1 / 4 pixel points are found using this point as the center, and a new round of pixel-by-pixel search is performed. The judgment criterion is exactly the same as that of the 1 / 2 pixel search, thus finding the optimal matching point and obtaining the optimal mv information corresponding to this block type. The above 1 / 2 search and 1 / 4 search are performed for each block to obtain the optimal pixel-by-pixel mv information (bestmv_fme_x, bestmv_fme_y) of all blocks under the current macroblock type inter. The cost of all inter block types is accumulated as the cost of the current macroblock type inter. The mode decision algorithm compares the optimal cost of encoding as intra macroblock type with the cost of encoding as inter macroblock type, and selects the minimum cost as the final encoding type of the current macroblock. Transform quantization and inverse quantization are an inverse pair of operations. Transform quantization performs integer DCT transform and quantization on the difference value obtained by subtracting the predicted value from the original pixel of the current macroblock to obtain the residual data to be entropy encoded. Inverse quantization and inverse transform perform inverse quantization and inverse DCT transform on this residual data. Adding the predicted value, the reconstructed data of the luminance component of the current macroblock can be obtained, and thus the mad under the current coarse quantization parameter coarse_qp can be obtained.
[0079] (3) IPTQ module
[0080] The IPTQ module is the third stage of the pipeline, and its main algorithmic functions include macroblock QP calculation, intra prediction, chroma inter inter interpolation, transform quantization, inverse quantization and inverse transform, and reconstruction. After processing by the preceding two stages of the IME and FME modules, the macroblock type of the current macroblock is finally determined. Simultaneously, if it is determined to be of the intra type, the optimal prediction mode (intra4x4, intra8x8, or intra16x16) is also finally determined; if it is determined to be of the inter type, the inter block mode is also finally determined. After determining the macroblock type, the MAD value of the current macroblock under the current coarse quantization parameter coarse_qp can be calculated using the following formula:
[0081]
[0082] Where pred(i,j) is the intra prediction value when the current macroblock is encoded as intra type, or the result obtained by each block based on the optimal mv interpolation when encoded as inter type.
[0083] After obtaining the MAD value of the macroblock, the quantization parameter QP of the macroblock can be calculated based on the MAD. The calculation formula is as follows:
[0084] if (mad <= RC_MB_MAD_TH0)
[0085] {
[0086] mb_qp = slice_qp - RC_MB_QP_DELTA0;
[0087] }
[0088] else if (mad <= RC_MB_MAD_TH1)
[0089] {
[0090] mb_qp = slice_qp - RC_MB_QP_DELTA1;
[0091] }
[0092] else if (mad <= RC_MB_MAD_TH2)
[0093] {
[0094] mb_qp = slice_qp - RC_MB_QP_DELTA2;
[0095] }
[0096] else if(mad<= RC_MB_MAD_TH3)
[0097] {
[0098] mb_qp = slice_qp - RC_MB_QP_DELTA3;
[0099] }
[0100] else if(mad<= RC_MB_MAD_TH4)
[0101] {
[0102] mb_qp = slice_qp - RC_MB_QP_DELTA4;
[0103] }
[0104] else if(mad<= RC_MB_MAD_TH5)
[0105] {
[0106] mb_qp = slice_qp + RC_MB_QP_DELTA5;
[0107] }
[0108] else if(mad<= RC_MB_MAD_TH6)
[0109] {
[0110] mb_qp = slice_qp + RC_MB_QP_DELTA6;
[0111] }
[0112] else if(mad<= RC_MB_MAD_TH7)
[0113] {
[0114] mb_qp = slice_qp + RC_MB_QP_DELTA7;
[0115] }
[0116] else if(mad<= RC_MB_MAD_TH8)
[0117] {
[0118] mb_qp = slice_qp + RC_MB_QP_DELTA8;
[0119] }
[0120] else
[0121] {
[0122] mb_qp = slice_qp + RC_MB_QP_DELTA9;
[0123] }
[0124] The thresholds RC_MB_MAD_TH0 to RC_MB_MAD_TH8 and the offset values of the quantization parameters RC_MB_QP_DELTA0 to RC_MB_QP_DELTA9 are all configured to the hardware via registers, allowing the hardware to adapt to different scenarios for flexible configuration. In typical application scenarios, experiments have shown that the thresholds RC_MB_MAD_TH0 to RC_MB_MAD_TH8 can be configured to the following values:
[0125] RC_MB_ MAD_TH0 = (unsigned int ) (0.1 * last_pic_avg_mad)
[0126] RC_MB_ MAD_TH1 = (unsigned int ) (0.2 * last_pic_avg_mad)
[0127] RC_MB_ MAD_TH2 = (unsigned int ) (0.3 * last_pic_avg_mad)
[0128] RC_MB_ MAD_TH3 = (unsigned int ) (0.4 * last_pic_avg_mad)
[0129] RC_MB_ MAD_TH4 = (unsigned int ) (0.5 * last_pic_avg_mad)
[0130] RC_MB_ MAD_TH5 = (unsigned int ) (2.0 * last_pic_avg_mad)
[0131] RC_MB_ MAD_TH6 = (unsigned int ) (3.0 * last_pic_avg_mad)
[0132] RC_MB_ MAD_TH7 = (unsigned int ) (4.0 * last_pic_avg_mad)
[0133] RC_MB_ MAD_TH8 = (unsigned int ) (6.0 * last_pic_avg_mad)
[0134] Where last_pic_avg_mad is the average mad value of all macroblocks in the previous frame.
[0135] RC_MB_QP_DELTA0 to RC_MB_QP_DELTA9 can be configured to the following values:
[0136] RC_MB_QP_DELTA0 = 5
[0137] RC_MB_QP_DELTA1 = 4
[0138] RC_MB_QP_DELTA2 = 3
[0139] RC_MB_QP_DELTA3 = 2
[0140] RC_MB_QP_DELTA4 = 1
[0141] RC_MB_QP_DELTA5 = 0
[0142] RC_MB_QP_DELTA6 = 1
[0143] RC_MB_QP_DELTA7 = 2
[0144] RC_MB_QP_DELTA8 = 3
[0145] RC_MB_QP_DELTA9 = 4
[0146] The QP calculated in this step is the true quantization parameter QP of the macroblock, and this QP is used for calculation in subsequent pipeline processing.
[0147] Intra prediction is initiated when the current macroblock is determined to be of the intra macroblock type. It recalculates the intra type luminance prediction value using the new quantization parameter mb_qp, and simultaneously calculates the optimal intra prediction mode for the chrominance block. Chromaticity interpolation is initiated when the current macroblock is determined to be of the inter macroblock type. It obtains the chrominance MV information from the luminance MV information, reads the chrominance reference frame data at the corresponding position from the reference frame based on this MV information, and completes the interpolation operation to obtain the chrominance component interpolation result. Transform quantization and inverse quantization / inverse transform are an inverse pair of operations. Transform quantization performs integer DCT transform and quantization on the difference value obtained by subtracting the predicted value from the original pixels of the current macroblock, obtaining the residual data to be entropy encoded. Inverse quantization and inverse transform perform inverse quantization and inverse DCT transform on this residual data, and adding the predicted value yields the reconstructed data of the current macroblock.
[0148] (4) DBK module and EE module
[0149] The DBK module and the EE module are both in the 4th stage of the pipeline. They respectively complete the in-loop filtering of the current macroblock and the CABAC or CAVLC entropy encoding of the residual data according to the H264 algorithm process.
[0150] (5) WBK module
[0151] The WBK module is the last stage of the pipeline. It mainly writes back the encoded data, including the reconstructed YUV data and the encoded bitstream data, and outputs it to the external DDR memory.
[0152] The above description is only a preferred embodiment of the present invention. It should be noted that those skilled in the art can make several improvements without departing from the principle of the present invention, and these improvements should also be considered within the scope of protection of the present invention.
Claims
1. A hardware-implemented H.264 macroblock-level bitrate control method, characterized in that: The implementation of H264 hardware encoding is achieved through a 5-stage pipeline, specifically including the IME module in stage 1, the FME module in stage 2, the IPTQ module in stage 3, the DBK and EE modules in stage 4, and the WBK module in the last stage. The IME module mainly implements bitrate control and integer pixel search. The FME module mainly implements brightness intra prediction, pixel-by-pixel search, mode decision-making, as well as transform quantization, inverse quantization, inverse transform, and reconstruction. The IPTQ module mainly implements macroblock QP calculation, intra prediction, chrominance inter interpolation, transform quantization, inverse quantization and inverse transform, and reconstruction. The DBK and EE modules mainly implement in-loop filtering of the current macroblock and CABAC or CAVLC entropy coding of the residual data. The WBK module is mainly responsible for writing back the encoded data, including the reconstructed data and the encoded bitstream data, and outputting it to the external DDR memory. When implementing macroblock-level bitrate control in hardware, the calculation of the macroblock quantization parameter QP is divided into two levels. First, the IME module calculates the coarse quantization parameter coarse_qp, and then the IPTQ module calculates the more precise quantization parameter mb_qp. The coarse quantization parameter coarse_qp is only used in the IME module and FME module. The precise quantization parameter is used in the IPTQ module, DBK module, and EE module. The coarse quantization parameter *coarse_qp* is calculated based on the texture of the current macroblock. For macroblocks with complex textures and rich details, the quantization parameter is appropriately increased based on *slice_qp*, while for macroblocks with simple and smooth textures, the quantization parameter is appropriately decreased based on *slice_qp*. Here, *slice_qp* is the quantization parameter of the current macroblock calculated by the software. The more precise quantization parameter mb_qp is calculated based on the coarse quantization parameter coarse_qp. After the macroblock type has been finally determined and the macroblock's mad value has been obtained, the macroblock's quantization parameter is recalculated based on the macroblock's mad value to obtain the more precise quantization parameter mb_qp.