Introduction to x265 Rate Control Algorithm
The rate control in x265 is the same as x264's implementation, which is mostly empirical. It includes one two-pass and three one-pass modes(ABR, CRF and CQP). We describe ABR and CRF modes below.
Average Bitrate(ABR)
This is a one-pass scheme which produces near-constant quality within a specified file size. The ABR mode is implemented as follows:
- Run a fast motion estimation algorithm over a half-resolution version of current frame and then get SATD[i].
- Calculate blurred complexity of current frame according to SATD[i].
blurredComplexity [i] = cplxsum [i] / ( cplxcount [i] )
cplxsum [i] = cplxsum [i - 1] ∗ 0.5 + SATD [i]
cplxcount [i] = cplxcount [i - 1] ∗ 0.5 + 1 - Calculate qscale of current frame according to blurredComplexity [i] .
qscale [i] = blurredComplexity [i] ^ (1 - qcomp) - Clip qscale [i] twice in order to get it more accurate.
qscale [i] = qscale[i] / rateFactor[i]
qscale [i] = qscale[i] ∗ overflow
rateFactor [i] = wanted_bits_windows[i] / cplxsum[i]
overflow = clip3f(1 + (totalBits - wanted_bits) / abr_buffer, 0.5, 2) - Call clipQscale function to guarantee that the buffer is in a reasonable state by the end of the lookahead.
- Calculate QP according to qscale.
QP = 12 + 6 * log2 (qscale[i] / 0.85)
Constant Rate Factor(CRF)
The CRF mode is a one-pass mode that is optimal if the user specifies quality instead of bitrate. It is the same as ABR, except that the scaling factor is a constant and no overflow compensation is done. The steps are given below:
- Run a fast motion estimation algorithm over a half-resolution version of current frame and then get SATD[i].
- Calculate blurred complexity of current frame according to SATD[i].
blurredComplexity [i] = cplxsum [i] / ( cplxcount [i] )
cplxsum [i] = cplxsum [i - 1] ∗ 0.5 + SATD [i]
cplxcount [i] = cplxcount [i - 1] ∗ 0.5 + 1 - Calculate qscale of current frame according to blurredComplexity [i].
qscale [i] = blurredComplexity [i] ^ (1 - qcomp) - Scale qscale [i] with a constant.
qscale [i] = qscale[i] / rateFactor - Call clipQscale function to guarantee that the buffer is in a reasonable state by the end of the lookahead.
- Calculate QP according to qscale.
QP = 12 + 6 * log2 (qscale[i] / 0.85)