CRF Guide (Constant Rate Factor in x264, x265 and Libvpx)
CRF Guide (Constant Rate Factor in x264, x265 and Libvpx)
For x264, sane values are between 18 and 28. The default is 23, so you can use this as a starting
point.
For libvpx, there is no default, and CRF can range between 0 and 63. 31 is recommended for
1080p HD video:
If you’re unsure about what CRF to use, begin with the default and change it according to your
subjective impression of the output. Is the quality good enough? No? Then set a lower CRF. Is the
file size too high? Choose a higher CRF. A change of ±6 should result in about half/double the file
size, although your results might vary.
You should use CRF encoding primarly for offline file storage, in order to achieve the most optimal
encodes. For other applications, other rate control modes are recommended. In video streaming,
for example, CRF can be used in a constrained/capped mode to prevent bitrate spikes.
What bitrates will I get?
To give you an estimation of the bitrates to be expected for clips with different resolutions, here’s a
figure showing the average bitrate in MBit/s for four one-minute video clips with different encoding
complexity, encoded with x264 and different CRF values:
As you can see, the maximum rate can range between 10 and 25 MBit/s for a 2160p encode at
CRF 23. For other CRFs and resolutions, the rates vary accordingly. You can clearly see the
logarithmic relationship between CRF and bitrate.
Constant Rate Factor is a little more sophisticated than that. It will compress different frames by
different amounts, thus varying the QP as necessary to maintain a certain level
of perceived quality. It does this by taking motion into account. A constant QP encode at QP=18
will stay at QP=18 regardless of the frame (there is some small offset for different frame types, but
it is negligible here). Constant Rate Factor at CRF=18 will increase the QP to, say, 20, for high
motion frames (compressing them more) and lower it down to 16 for low motion parts of the
sequence. This will essentially change the bitrate allocation over time.
For example, here is a figure (from another post of mine) that shows how the bitrate changes for
two video clips encoded at different levels (17, 23) of constant QP or CRF:
The line for CRF is always lower than the line for CQP; it means that the encoder can save bits,
while retaining perceptual quality, whereas with CQP, you waste a little bit of space. This effect is
quite pronounced in the first video clip, for example.
In layperson’s terms, this is because your visual system will be “distracted” by everything going on,
and won’t have the image on screen for enough time to see the heavier compression. Slightly
more technically speaking, high motion “masks” the presence of compression artifacts like
blocking. On the other hand, when a frame doesn’t have a lot of motion, you will (simply put) have
more time to look at the image, and there will be nothing to distract you or mask any artifacts, so
you want the frame to be as little compressed as possible. With low motion, compression artifacts
become more salient (visually apparent) and thus more distracting.
You may ask if constant QP isn’t really better quality in the end? No, the perceived quality is the
same, but essentially it wastes space by compressing less in areas you really won’t notice.
Practically speaking, many people always use CRF for single-pass encodes and argue there is no
reason to ever use CQP. Another good argument for using CRF is that it is the default rate control
mode chosen by the developers of x264 and x265.
This is also another argument against using simple metrics such as PSNR or SSIM to judge video
quality: they cannot take into account perceptual effects like motion, since they only look at
individual frames. More perceptually-based metrics like VQM or VMAF are better choices for
evaluating video degradations.
CRF takes care of this problem: with different videos, different CRF levels result in different
bitrates. (In fact, you cannot reliably estimate what the resulting bitrate for a given CRF will be,
unless you know more about that source, which is what YouTube is doing.)
For example, if you set CRF 23, you may end up with 1,500 kBit/s for one source, but only 1,000
kBit/s for the other. They should look the same in terms of quality, though. With CRF, you are
saying “use whatever bitrate is necessary to preserve this much detail.” It’s not a 1-to-1 thing.
The cloud encoding service Bitmovin also uses CRF to gauge the complexity of a clip before
encoding it.
Note that if your CRF is too high—for example if you use a CRF of 30—you’re going to see
blocking on high-motion because the bitrate in these parts will simply be too low. The encoder will
use a QP of (for example) 32 for the more complex parts, which is way too heavy a quantizer. As
mentioned in the beginning, choose the CRF depending on what level of quality you want.
Streaming nowadays is done a little more cleverly. YouTube or Netflix are using 2-pass or even 3-
pass algorithms, where in the latter, a CRF encode for a given source determines the best bitrate
at which to 2-pass encode your stream. They can make sure that enough bitrate is reserved for
complex scenes while not exceeding your bandwidth.
You can learn more about rate control modes in another post of mine.