Saturday, 18 January 2014

x264 CRF Guide

Along with x264 came a new rate control method Constant Rate Factor(CRF) to me this is one of the best ways to encode videos. Unfortunately there isn't a lot of data on how CRF actually work and the results it creates. The reason for this is because it is based off of Constant Quantizer(QP) to understand this one must know how QP works first.

Constant Quantizer(QP)
Quantization works by breaking large input sets into smaller ones by removing information that might not be as relevant. A small example of this is, instead of storing data like: 28.56 we store it  28.56204234672.

So QP work by keeping the information it throws away consistent, thus resulting in a constant quality. In the H263 (xvid,divx, mpeg part 4, 3ivx) days a QP of 1 = 100% quality and every step was 5 less, so a QP 3 would be 90% and QP 5 = 85% and so on.

In H264 a QP 0 = 100% and there are 51 steps suggesting that every step is roughly 2% quality loss of course that is all mathematical and in the real world we deal with perceptual. And that is where CRF comes in. 

Constant Rate Factor
CRF works similarly to QP in that it tries and mimics the perceptual quality output of QP, such that a QP 21 would look to the human eye near identical to a CRF 21, it does this by reducing information in areas the human eye can't perceive and redistributes them in other areas that might need more data.

As you can see the images have a very similar image quality even during playback but has a significant difference in file-size:
QP: 2.6 GB
CRF: 2.1 GB(24% smaller)

Resolution scaling

So lets take a look at how CRF scales with different resolution sizes. The y-axis represents file size and the x-axis the vertical lines from 96x52p to 1920x1040p. The first step is 5% pixels of max and the second is 10%, the 3rd 15% and so on until 100% of/or 1920x1040.
As you can see the there is a slight indication that the amount of data needed to represent an image with CRF scales slightly exponentially in regards to the number of pixels.
And if we look at the encoding performance above we can see that as the resolution increases there is a sharp drop in encoding speed that tapers of has the resolution reaches the higher end.

So what is a good CRF value? It mainly depends on what you want, if you want a near identical copy then 18, High Quality 19-21.

But lets see how various CRF values impact the compression of a file. y-axis is file-size and x-axis is the CRF value
Again an exponential trend occurs, the lower the CRF value the significantly larger the file-size. As for image quality its hard to see the exact difference between still images when you compare the visual quality of CRF value with each other, you only notice a significant difference when its in motion. However I would suggest no higher value than 25.

CRF 21

CRF 25

CRF 30

No comments:

Post a Comment