Bit rate control (II) : DETAILS of CRF

Constant Rate Factor (CRF) has been introduced in the previous article. This article further introduces the principle of CRF. CRF is the default bitrate control mode for X264 and X265. CRF can also be used in libvpx. CRF ranges from 0 to 51. The smaller the value, the better the quality, the lower the compression rate. The larger the value, the higher the compression rate, the lower the quality.

CRF does not attempt to achieve a specific bit rate in its bit rate control, but to maintain a stable quality. The size of the stream will be determined by the complexity of the source video.

For X264, the CRF value ranges from 18 to 28. The default value is 23.

ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4
Copy the code

For X265, the default value is 28.

ffmpeg -i input.mp4 -c:v libx265 -crf 28 output.mp4
Copy the code

Libvpx has no default value and its CRF value ranges from 0 to 63. For 1080P videos, the recommended value is 31.

ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 31 -b:v 0 output.mkv
Copy the code

If you are not sure what CRF value you want, you can start with the default value and try it out. Reduce CRF if the quality is lower than expected and increase CRF if the file is too large. Each time CRF increases/decreases by 6, the file size is halved/doubled.

CRF should be used in offline scenarios for optimal results.

How does the bit rate change?

In order to give you an intuitive understanding of the relation between the bit rate and CRF of video with different resolutions, the relation between the average bit rate (MBit/s) and CRF of four 1-minute videos with different complexity is given below. The coder is X264.

 

It can be seen that the bit rate and CRF of videos with different resolutions meet the logarithmic relationship.

CRF vs CQP

CQP mode will keep the QP of each frame unchanged during encoding. For example, if QP=18 is set, the QP of each frame of the whole sequence will be 18 (there will be QP offset according to different frame types, but the effect is not significant). CRF keeps the quality constant by dynamically adjusting the QP of each frame. For example, if the encoding is set to CRF=18, the QP may be increased to 20 for frames with more motion and reduced to 16 for frames with less motion.

Below is the change in the number of bits per frame when QP and CRF are equal to 17 and 23.

 

You can see that CRF always has fewer bits than QP, meaning that CRF saves bits while preserving mass.

Why is exercise so important?

The human eye perceives more detail in stationary objects than in moving ones. Thus, encoders can compress moving objects more (removing more detail) and stationary objects less (preserving more detail).

The human visual system is distracted by motion, and moving objects spend less time on the screen, so less distortion is detected. While a stationary object stays on the screen for long enough time to observe, and cannot be distracted, so it can detect more distortion.

What evaluation indicators are used?

If you compare video quality using simple metrics like PSNR, you’ll probably find that CRF is lower than CQP. However, human eyes can find that the quality of CRF is no lower than or even higher than that of CQP. This is because indicators such as PSNR do not consider perceived quality, only the statistical results of each frame. Subjective evaluation can be done using VQM or VMAF.

Translated from: slhck.info/video/2017/…

If you are interested, please pay attention to wechat public account Video Coding