1. One code rate control method explicitly supported by X264 is: ABR, CQP, CRF. The default method is CRF. The priority of these three methods is ABR> CQP> CRF.
if (bitrate) rc_method = ABR;
else if (qp || qp_constant) rc_method = CQP;
else rc_method = CRF;
There are no default values for bitrate and QP. Once they are set, they are encoded according to the corresponding rate control method. CRF has a default value of 23, and if there is no setting for encoding control, it is encoded according to CRF default value of 23.
General usage suggestions:
CQP-generally not recommended, this mode is used in some algorithm verification work
CRF-suitable for use in scenarios where the encoding quality is concerned and the output file size or bit rate is not too important. Generally, CRF is used for network compression.
1 pass ABR – suitable for real-time applications where streaming media or target bit rates are limited.
2 pass VBR-suitable for non-real-time applications with target bit rate limitation and time for secondary encoding.
2. CQP, constant QP. No default value
The simplest rate control method, each frame of image is coded according to a specific QP, how much data is encoded after each frame is unknown.
The parameter qp_constant sets the QP of the P frame. The QP of I and B frames is calculated according to f_ip_factor and f_pb_factor.
rc-> ip_offset = 6.0 * log2f (h-> param.rc.f_ip_factor);
rc-> pb_offset = 6.0 * log2f (h-> param.rc.f_pb_factor);
rc-> qp_constant [SLICE_TYPE_P] = h-> param.rc.i_qp_constant;
rc-> qp_constant [SLICE_TYPE_I] = x264_clip3 (h-> param.rc.i_qp_constant-rc-> ip_offset + 0.5, 0, QP_MAX);
rc-> qp_constant [SLICE_TYPE_B] = x264_clip3 (h-> param.rc.i_qp_constant + rc-> pb_offset + 0.5, 0, QP_MAX);
When there are multiple consecutive B frames, QP will increase gradually. x264 YUV420 format The QP range for 8-bit samples is [0, 51]. The smaller the QP value, the better the coding visual quality. QP = 0 is distortion-free coding.
When studying the coding algorithm, the CQP method is generally used, and the QP is set to 24, 28, 32, 36, 40, etc. (4 QP values are generally selected), the RD curve is obtained by coding, and then the algorithm is compared with the pros and cons.
With the same visual quality, CQP encoded output files will be larger than CRF mode. In general, CRF can replace the CQP method, but CQP runs faster because it does not require prediction at all.
The importance levels of frames are: IDR frame> I frame> P frame> B frame for reference> B frame for no reference. QP can be sequentially increased.
QPmin, default value: 0. Defines the minimum quantization value that X264 can use. The smaller the quantization value, the better the output video quality.
When the QP is less than a certain value, the quality of the encoded macroblock is very close to the original block, and it is not necessary to continue to lower the QP at this time.
If the adaptive quantizer is turned on (by default), it is not recommended to increase the QPmin value, as this will reduce the visual quality of the smooth background area.
QPmax, default: 51. Defines the maximum quantization value that X264 can use. The default value of 51 is the maximum quantization value available in the H.264 specification.
If you want to control the minimum quality of X264 output, you can set this value smaller.
QPmin and QPmax are effective under CRF and ABR methods. Setting QPmax too low may cause ABR code rate control failure. It is not recommended to adjust this parameter.
QPstep, default: 4. Sets the maximum variation of the quantization value between two frames.
The QP changes between frames, the macroblock QP in the frame does not change, the output bit rate is unknown, and the output visual quality of each frame changes (higher QP and lower bit rate will be more obvious).
3. CRF, constant Rate Factor default value 23
CQP targets a certain quantized value, bitrate targets a certain output file size, and CRF targets a certain output "visual quality".
CRF can provide the same visual quality as QP, but with smaller files, CRF does this by reducing the quality of those "less important" frames.
"Less important" means frames that are too expensive and difficult to detect with the naked eye, such as complex or high-speed scenes. The saved bit rate is allocated to other more efficient frames.
CRF and bitrate use the same adjustment strategy inside the X264 encoder, except that it does not follow a specific output bit rate.
It also adjusts the visual quality of the output by changing the QP values of different important level frames (I, P, B types) and different macroblock types (high-speed motion, complex textures, flat areas) within the frame.
The range of RF is the same as the range of QP [0, 51]. Among them, 0 is lossless mode, 23 is the default, and 51 is the worst quality. Same trend as QP. If the RF value is increased by 6, the output bit rate is reduced by about half; if it is decreased by 6, the output bit rate is doubled.
Subjectively speaking, 18 ~ 28 is a reasonable range, and 18 is often considered to be approximately lossless visually.
Inter-frame QP changes, intra-frame macroblock QP changes, the output bit rate is unknown, and the output visual quality of each frame is basically constant.
4. ABR, constant average target bit rate. To choose this bit rate control method, you must first set the bitrate.
The unit of bitrate in X264 is Kbps (K bit per second). --Bitrate 128 refers to setting the target code rate to 128Kbps, so the amount of data in one second is 128/8 bits = 16K bytes.
If the input is [email protected], it is equivalent to 128Kbit / (352 * 288 * 15) = 0.086 bits per pixel. After encoding, each pixel is allocated less than 0.1 bits on average.
The corresponding technologies of ABR are CBR and VBR. These bit rate control technologies are first adopted in audio coding, which is to solve the problem of what bit rate is optimal for audio coding.
The bit rate of the CBR encoding code remains basically constant at the target bit rate, which is conducive to streaming playback. The disadvantage of CBR is that the code rate of complex scenes is not enough, and the code rate of simple scenes is wasted, so the visual quality of the encoded content is unstable. This change in quality is usually more pronounced at lower bit rates.
VBR encoding assigns a larger QP for simple scenes and a smaller QP for complex scenes, and obtains basically stable output visual quality. Compared with CBR, under the same file size, the output of VBR is much better than CBR, which is conducive to media download and local storage. The disadvantage of VBR is that the size of the output code stream is uncontrollable. At the same time, there is no advantage for constant complexity content (such as news broadcasts).
ABR coding allocates fewer bits for simple scenes, leaving enough bits for generating high-quality complex parts. This allows a limited number of bits to be reasonably allocated between different scenarios, which is similar to VBR. At the same time, the ABR allocates the bit rate, so that the average bit rate approaches the target bit rate in a certain period of time. This can control the output file size, which is similar to CBR. Therefore, ABR can be considered as a compromise optimization scheme for CBR and VBR.
Analyze video encoding bit rate control through three factors:
1. Visual quality stability is conducive to visual subjective quality;
2. Instant output bit rate, which is equivalent to the number of encoded output bits per frame;
3. The output video file size can be controlled, which is convenient for transmission and storage.
The comparison of these three rate control methods is as follows:
# Visual quality stability Instant output bit rate Output file size
CBR unstable unstable controllable
VBR stable change uncontrollable
ABR is basically stable and controllable (immediate bit rate changes, but the average bit rate approaches the target bit rate for a period of time)
In the current X264 version (version 142), when choosing ABR, you need to pay attention to two settings, 1.fps; 2. pts calculation of output frame.
1. fps. ABR will estimate the average amount of data per frame according to the frame rate, and bitrate / fps is the average amount of data per frame.
When the input video source is YUV data, you need to explicitly specify the correct frame rate-fps, otherwise X264 will use the default 25fps to calculate and may not control the set target bit rate.
2. pts calculation. In the ABR algorithm, the pts of different frames are used as the inter-frame distance. If the pts value of the output frame is not set, X264 will report a "non-strictly-monotonic PTS" warning.
The bit rate of the encoded video file is very small, it cannot reach the bitrate setting, and the video quality is very poor, almost all of which are mosaic. There are two ways to solve this problem:
a. Set param.b_vfr_input = 0, then use fps instead of timebase, timestamps to calculate the distance between frames
b. Actively update pts after decoding, pic_out.i_pts ++;
Both of these methods can avoid the problem of ABR code rate control failure.
X264 rate control summary 1-ABR, CQP, CRF