## 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:

1. Run a fast motion estimation algorithm over a half-resolution version of current frame and then get SATD[i].
2. 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
3. Calculate qscale of current frame according to blurredComplexity [i] .
qscale [i] =  blurredComplexity [i] ^ (1 - qcomp)
4. 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)
5. Call clipQscale function to guarantee that the buffer is in a reasonable state by the end of the lookahead.
6. 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:

1. Run a fast motion estimation algorithm over a half-resolution version of current frame and then get SATD[i].
2. 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
3. Calculate qscale of current frame according to blurredComplexity [i].
qscale [i] =  blurredComplexity [i] ^ (1 - qcomp)
4. Scale qscale [i] with a constant.
qscale [i] =  qscale[i] / rateFactor
5. Call clipQscale function to guarantee that the buffer is in a reasonable state by the end of the lookahead.
6. Calculate QP according to qscale.
QP = 12 + 6 * log2 (qscale[i] / 0.85)

posted on 2016-04-26 20:23  lakeone  阅读(3295)  评论(0编辑  收藏  举报