Introduction of x264 Code Rate control method

Source: Internet
Author: User

Transferred from: http://www.bubuko.com/infodetail-471698.html

1. X264 explicit support for a pass-rate control method 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;
Bitrate and QP do not have default values, once set they indicate to follow the corresponding code rate control method to encode, CRF has a default value of 23, there is no encoding control settings when the CRF default value 23来 encoded.

General recommendations for use:
Cqp–not recommended anymore unless you know it
Crf–good for one pass when the "priority" is quality and the "file Size/bit rate are not really a concern
1 pass Abr–good for streaming purposes or targeting a bit rate when two-pass is unfeasible
2 Pass Vbr–good for targeting a bit rate if you had the time to spend on both passes (though the first pass can be qui Te fast) and is writing to a file

2. CQP, constant QP. No saved value
The simplest method of rate control, each frame of the image is encoded according to a specific QP, the amount of data after each frame encoding is unknown.
The parameter qp_constant is set to QP for P frames. I,b frame of QP According to F_ip_factor, F_pb_factor, calculated.
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 multiple B frames are continuous, the QP increases.

The QP range of the x264 YUV420 format 8-bit sampling is [0, 51]. The smaller the QP value, the better the coding visual quality. The qp=0 is a distortion-free encoding.
In the study of coding algorithms, the general will choose Cqp method, set QP for 24,28,32,36,40, etc. (generally choose 4 QP value), coded to get the RD curve, and then compare the advantages and disadvantages of the algorithm.
With the same visual quality, the CQP encoded output file will be larger than the CRF mode. Generally speaking, CRF can replace the CQP method, but CQP will run faster because it doesn't need to be predicted at all.
The important levels of the frame are: IDR frame > I frame > P frame > Do reference B frame > do not reference B-frame. QP can be increased in turn.

Qpmin, default value: 0. Defines the minimum quantization value that the X264 can use. The smaller the quantization value, the better the output video quality.
When QP is less than a certain value, the quality of the encoded output is very similar to the original block, and it is not necessary to continue to lower the QP.
If you turn on the adaptive Quantizer (which is on by default), it is not recommended to increase the value of qpmin because it reduces the visual quality of the smooth background area.
Qpmax, default value: 51. Defines the maximum quantization value that can be used by the X264. The default value of 51 is the maximum quantization value available for use in the H. S specification.
If you want to control the minimum quality of the X264 output, you can set this value to a smaller number.
Qpmin and Qpmax are effective under the Crf,abr method and are too low to set Qpmax, which may cause ABR bitrate control to fail. Adjusting this parameter is not recommended.
Qpstep, Default value: 4. Sets the maximum variation amplitude of the quantization value between frames of two.
QP changes between frames, the macro-block QP unchanged in the frame, the output code rate is unknown, each frame output visual quality changes (high QP low bit rate of the case will be more obvious).

3. CRF, Constant rate Factor (bitrate coefficient) default value 23
CQP is the goal of a certain quantization value, bitrate is the size of an output file as the target, while the CRF is the output "visual quality" as the target.
CRF can provide the same visual quality as QP, but the file is smaller, and the CRF achieves this by reducing the quality of those "less important" frames.
"Less important" means frames that are too expensive to detect with the naked eye, such as complex or fast-moving scenes. The saved bitrate is allocated to other more efficient frames.
In the X264 encoder internal CRF and bitrate adopt the same adjustment strategy, except that it does not follow a specific output code rate.
It also adjusts the output visual quality by changing the QP values of different important level frames (i,p,b types), as well as different macro block types (high-speed motion, complex textures, flat areas) within the frame.
And the range of the QP is the same as the range of the RF [0, 51]. 0 is lossless, 23 is default, and 51 is the lowest quality. The same trend as QP. RF Value plus 6, the output code rate is approximately halved, minus 6, the output code rate doubled.
Subjectively speaking, the 18~28 is a reasonable range, 18 is often considered visually to be approximately lossless.
The change of QP between frames, the change of the macro-block QP in the frame, the unknown output code rate, the visual quality of each frame is basically constant.

4. ABR, constant average target bitrate. To choose this rate control method, the bitrate must be set first.
The unit of bitrate in X264 is Kbps (K bit per sec). --bitrate 128 refers to the set target bitrate of 128Kbps, so that the amount of data for 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. Each pixel is assigned an average of less than 0.1 bits after encoding.
The corresponding technique of ABR is CBR,VBR. These rate control techniques are first used in audio coding, which is the problem of how to solve the optimal bit rate of audio coding.
CBR code bit rate is basically kept constant at the target bitrate, which facilitates streaming playback.
The disadvantage of CBR is that complex scene code rate is not enough, simple scene code rate is wasted, so the visual quality of encoded content is not stable. Usually at lower bit rates, this change in quality is more pronounced.

VBR encoding allocates a large QP to a simple scene, allocates a smaller qp for a complex scene, and obtains a basic stable output visual quality.
Compared to CBR, the output of VBR is much better than CBR in the same file size, which facilitates media download and local storage.
The disadvantage of VBR is that the output code stream size is not controllable. At the same time, there is no advantage for content with constant complexity, such as news broadcasts.

ABR codes assign fewer bits to a simple scene, leaving enough bits to produce high-quality, complex parts. So that the finite number of bits can be allocated reasonably between different scenes, which is similar to VBR.
At the same time, the ABR distribution code rate, so that in a certain period, the average code rate toward the target bitrate, so that 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.

There are three factors that can be used to analyze video encoding bitrate control:
1. Visual quality stability, conducive to visual subjective quality;
2. Instant output bitrate, equivalent to the number of output bits per frame encoding;
3. Output video file size controllable, facilitate transmission, storage.
Compare these three code rate control methods as follows:

# Visual quality stability Instant output bitrate output file size
CBR unstable constant controllable
VBR stable change is not controllable
The basic stability change of ABR is controllable (real-time code rate change, but the average rate is approaching the target rate for a period)

5. In the current X264 version (version 142), the ABR needs to be aware of two settings, 1.fps;2. Output frame pts calculation.
1. FPs. The ABR estimates the average amount of data per frame based on the frame rate, bitrate/fps the average one frame of data.
When the input video source is YUV data, it is necessary to explicitly specify the correct frame rate--fps, otherwise the X264 will be calculated with the default 25fps, it is possible to control the target bitrate.
2. PTS calculation. The ABR algorithm uses a different frame of PTS as the distance between frames, and if no PTS value is set for the output frame, X264 will report "Non-strictly-monotonic PTS" warning.
Encoded video file code rate is very small, not up to bitrate settings, while the video quality is very poor, almost all mosaic. There are two ways to solve this problem:
A. Set param.b_vfr_input = 0, at which point the distance between frames is calculated according to FPS instead of timebase,timestamps.
B_vfr_input=1 is the X264 default setting, the Zerolatency has a =0 configuration.
B. Actively increase the pic_out.i_pts++ after decoding; Statement.
Both methods can be used at the same time. ABR needs to be set bitrate, fps, and param.b_vfr_input = 0.

Introduction of x264 Code Rate control method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.