x264码率控制概述(2009年8月)

A qualitative overview of x264's ratecontrol methods
By Loren Merritt

x264码率控制概述(2009年8月)

x264's ratecontrol is based on libavcodec's, and is mostly empirical. But I can retroactively propose the following theoretical points which underlie most of the algorithms:

- You want the movie to be somewhere approaching constant quality. However, constant quality does not mean constant PSNR nor constant QP. Details are less noticeable in high-complexity or high-motion scenes, so you can get away with somewhat higher QP for the same perceived quality.
稳定的画质并不意味着相同的PSNR和相同的QP。复杂度高、快速运动的场景中,用较高的QP也可以获得较好的画质。

- On the other hand, you get more quality per bit if you spend those bits in scenes where motion compensation works well: A given artifact may stick around several seconds in a low-motion scene, and you only have to fix it in one frame to improve the quality of the whole scene.
把码率资源用於运动补偿可以获得更好的画质。

- Both of the above are correlated with the number of bits it takes to encode a frame at a given QP.
- Given one encoding of a frame, we can predict the number of bits needed to encode it at a different QP. This prediction gets less accurate if the QPs are far apart.
编好一帧之后,我们可以预测用另一个QP编码该帧时需要的的Bit数,如果QP的差值较大,则这种预测会不太准确。

- The importance of a frame depends on the number of other frames that are predicted from it. Hence I-frames get reduced QP depending on the number and complexity of following inter-frames, disposable B-frames get higher QP than P-frames, and referenced B-frames are between P-frames and disposable B-frames.
如果一帧被参考的次数越多,那么这一帧就越重要。因此,编I帧时要用较小的QP,不用於参考的B帧则采用比P帧的QP更大的QP,可以用於参考的B帧的QP则介于不用於参考的B帧的QP和P帧的QP之间。

The modes:

    2pass:
Given some data about each frame of a 1st pass (e.g. generated by 1pass ABR, below), we try to choose QPs to maximize quality while matching a specified total size. This is separated into 3 parts:
在第一步获得每帧的一些信息之后,第二步则在满足规定大小的前提下选择适当的QP获得最好的画质。第二步又分为3小步。

(1) Before starting the 2nd pass, select the relative number of bits to allocate between frames. This pays no attention to the total size of the encode. The default formula, empirically selected to balance between the 1st 2 theoretical points, is "complexity ** 0.6", where complexity is defined to be the bit size of the frame at a constant QP (estimated from the 1st pass).
开始之前,不考虑总的大小,使用公式为每一帧分配Bit数. 缺省使用的公式是 “复杂度**0.6”(基于经验来平衡最先的两个理论点),其中复杂度被定义为该帧用一个固定QP编码时用的总的Bit数。

(2) Scale the results of (1) to fill the requested total size. Optional: Impose VBV limitations. Due to nonlinearities in the frame size predictor and in VBV, this is an iterative process.
调整上一步中为每一帧分配的大小,使得总的大小符合预期值。可选的增加VBV限制,由于VBV在预测帧大小时表现的非线性特征,这个过程是一个递归的过程。

(3) Now start encoding. After each frame, update future QPs to compensate for mispredictions in size. If the 2nd pass is consistently off from the predicted size (usually because we use slower compression options than the 1st pass), then we multiply all future frames' qscales by the reciprocal of the error. Additionally, there is a short-term compensation to prevent us from deviating too far from the desired size near the beginning (when we don't have much data for the global compensation) and near the end (when global doesn't have time to react).
开始编码。每编完一帧,更新下一帧采用的QP以弥补与预期大小的偏差。如果持续偏离预期的大小(通常由于使用了比1pass更慢的压缩选项导致),那么我们就用误差的倒数和以后的帧的QScale相乘,进行一个修正,防止在编码开始时或者编码结束时偏离预期的大小太多。

    1pass, average bitrate:
The goal is the same as in 2pass, but here we don't have the benefit of a previous encode, so all ratecontrol must be done during the encode.
1pass,平均码率模式
这种模式的目标和2pass是一样的,但是不像2pass那样有前一次编码的统计信息可用,所以所有的码率控制工作都必须在一次编码的过程中完成。

(1) This is the same as in 2pass, except that instead of estimating complexity from a previous encode, we run a fast motion estimation algo over a half-resolution version of the frame, and use the SATD residuals (these are also used in the decision between P- and B-frames). Also, we don't know the size or complexity of the following GOP, so I-frame bonus is based on the past.
除了不能使用前一次编码的每一帧的复杂度信息之外,这一步和2pass是一样的。我们将在半分辨率的情况下对帧进行运动估计,并使用SATD残差来计算复杂度(该信息也用于决定编码时使用的帧类型)。而且,由于不知道下一个GOP的复杂度或者大小,所以I帧该比P帧和B帧多分配多少的码率资源也是基于已编码的帧来决定的。

(2) We don't know the complexities of future frames, so we can only scale based on the past. The scaling factor is chosen to be the one that would have resulted in the desired bitrate if it had been applied to all frames so far.
由于无法预知未编码的帧的复杂度,所以只能根据已编码的帧的复杂度进行调整。如果所有已编码的帧采用某个比例因子进行调整后正好能达到要求的码率,那么就采用这个比例因子调整接下来要编码的帧。

(3) Overflow compensation is the same as in 2pass. By tuning the strength of compensation, you can get anywhere from near the quality of 2pass (but unpredictable size, like +- 10%) to reasonably strict filesize but lower quality.
处理溢出补偿的方式和2pass是一样的。通过调整补偿的力度,可以获得近似2pass的质量(但是大小不可预知,会有大约+-10%的误差),也可在较低质量的情况下符合预期的大小。




    1pass, constant bitrate (VBV compliant):
1pass,变码率模式

(1) Same as ABR.
与1pass平均码率模式的第一步相同

(2) Scaling factor is based on a local average (dependent on VBV buffer size) instead of all past frames.
比例因子采用一个基于VBV的平均数

(3) Overflow compensation is stricter, and has an additional term to hard limit the QPs if the VBV is near empty. Note that no hard limit is done for a full VBV, so CBR may use somewhat less than the requested bitrate. Note also that if a frame violates VBV constraints despite the best efforts of prediction, it is not re-encoded.
处理溢出补偿的方式更严苛,且在VBV快耗尽的时候,对QP的调整会有一个额外的限制。需要注意由于VBV空闲的时候没有前面的限制

    1pass, constant ratefactor:
1pass,常量比例因子

(1) Same as ABR.
与1pass平均码率模式的第一步相同

(2) The scaling factor is a constant based on the --crf argument.
比例因子由--crf参数指定

(3) No overflow compensation is done.
不处理溢出补偿

    constant quantizer:
常量QP模式
QPs are simply based on frame type.
QP由帧类型决定

    all modes:
H.264 allows each macroblock to have a different QP. x264 does not do so. Ratecontrol returns one QP which is used for the whole frame.
H.264标准允许每个宏块拥有自己的QP,x264没有这么做,而是一帧所有的宏块使用相同的一个QP。

你可能感兴趣的:(x264码率控制概述(2009年8月))