Press "Enter" to skip to content

最近对视频编码的一些研究心得

从某知名视频网站下载了一个小视频, 1080P 25帧的, 比特率是2600kbps, 格式为H.264/AVC, 视频长度为5分钟, 才98M的体积. 某天心血来潮想把它转换为H.265/HEVC格式的视频, 试了无数次才发现, 用现有的工具, 转换出来的H.265/HEVC格式的视频, 体积比它大的, 可能还没有它清晰, 体积比它小的, 清晰度就差更多了. 传说中的H.265/HEVC的优势哪里去了?

1, 一些专有名词

Constant QP (CQP)

Constant Quantization Parameter, 恒定量化编码模式, 也称 CQ (constant quantizer)模式, 英文解释为The quantization parameter defines how much information to discard from a given block of pixels (a Macroblock), 此参数控制每个宏块(Macroblock)的压缩量. 此值越大, 表示要丢弃的Macroblock就越多(压缩率越大), 视频体积越小, 同时视频质量越差. 一般是用GPU转码时, 才会有CQP选项. 不建议使用此模式, 原因如下

Setting a fixed QP means that the resulting bitrate will be varying strongly depending on each scene’s complexity, and it will result in rather inefficient encodes for your input video. You may waste space and you have no control of the actual bitrate.

Average Bitrate (ABR)

平均比特率, 这个不多说了. 不建议使用此模式. 老外的分析如下

One of the main x264 developers himself says you should never use it. Why? As the encoder doesn’t know exactly what’s ahead in time, it will have to guess how to reach that bitrate. This means that the rate itself will vary, especially at the beginning of the clip, and at some point reach the target. Especially for HAS-type streaming, this leads to huge quality variations within short segments.

Constant Bitrate (CBR)

固定比特率, 这个也不多说, 多用于在线直播/流媒体方面.

Constant Rate Factor (CRF)

恒定速率因子编码模式. 是x264和x265编码器的默认质量(和速率控制)设置方式, 有些地方也称之为 constant quality (注意跟上面的 constant quantizer 是两码事). 在H.264和H.265中, CRF的范围是0到51(类似于QP).

跟CBR相反, CRF是一种类似于CQP的编码模式, 它会感知视频的运动变化, 来随时调整QP的值. 是一种比较推荐的方式.

Constant Rate Factor is a little more sophisticated than Constant QP. 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.

B帧(B-frame)

双向前后帧. 可以使用之前和之后的视频帧中的信息来预测如何将数据保存在宏块或编码树单元块中. Can use information from earlier and later video frames to predict how to save data in macro blocks or coding tree unit blocks

2, 一些分析

与静止物体相比,人眼对静止物体的感知更多。因此,视频编码器可以在物体移动时施加更多的压缩(丢弃更多细节),而在物体静止时应用更少的压缩(保留更多细节)。

用外行的话来说,这是因为您的视觉系统会因发生的一切而“分散注意力”,并且在屏幕上没有足够的时间来查看较大的压缩效果。从技术上讲,高速运动“掩盖”了压缩伪影(例如阻塞)的存在。另一方面,当框架没有很多运动时,您(简单地说)将有更多时间查看图像,并且没有任何东西可以分散您的注意力或掩盖任何伪影,因此您需要框架尽可能少地压缩。在低速运动时,压缩伪影会变得更加突出(在视觉上显而易见),从而分散注意力。

您可能会问,Constant QP最终是否真的不是更好的质量?不,感知到的质量是相同的,但是本质上,它通过在您真正不会注意到的区域进行较少的压缩来浪费空间。

主观上来说,正常人类看到的CRF跟CQP版本相同或更佳。它最少地压缩您看到细节最多的部分,而多数情况下压缩您看到细节最少的部分。这意味着,尽管以PSNR(信噪比)客观衡量的平均质量略有下降,但可感知的视频质量却有所上升。

对于不同的视频,不同的CRF级别会导致不同的比特率。 (实际上,除非您进一步了解该来源,否则YouTube无法正常估算给定CRF的最终比特率。)

例如,如果您设置CRF 23,则对于一个源,最终可能为1,500 kBit / s,而对于另一个源,最终可能仅为1,000 kBit / s。但是,它们在质量上应该看起来相同。

请注意,如果您的CRF太高(例如,如果您使用的CRF为30),您将会看到在高速运动时出现阻塞,因为这些部分的比特率将太低。因此 , 请根据所需的质量级别选择CRF。

3, GPU还是CPU?

GPU video encoding is generally optimised for high-bitrate streaming rather than playback. GPU encoding will not use B-Frames and will use a constant bitrate.
x264, x265, xvid, etc… is used more in playback and storage where multipass encoding is used to optimise dynamic bitrates so areas with low changes (blank screen) use a very low bit rate and heavy changes (particle effects, rain, etc…) have a very high bitrate with an overall targeted average bitrate is achieved. The targeted bitrate is usually optimised for storage.

老外的一段分析, 意思是, GPU编码是用于高比特率场景而优化的, 例如游戏流媒体(实时渲染)等, 能在高比特率条件下产生高质量的画面, 但它不是为playback而优化的. GPU编码没有B帧, 且会使用固定的比特率. 因此使用GPU编码的视频体积都非常大, 也不便于存储.

GPU编码的另外一个优势是性能, 从而可以让CPU来完成其它任务. 虽然GPU编码在速度上优于x264和x265, 但是,在图像质量方面,x264和x265具有用于专业视频编码的更复杂的功能集,并且可以在较低的比特率下产生更好的画面质量。

4, 推荐设定值

知名开源转码工具Handbrake网站给出了推荐的CRF值

RF 18-22 for 480p/576p Standard Definition, 推荐20
RF 19-23 for 720p High Definition
RF 20-24 for 1080p Full High Definition, 推荐22
RF 22-28 for 2160p 4K Ultra High Definition, 推荐24

参考文档:
Adjusting quality from HandBrake Documentation
CRF Guide (Constant Rate Factor in x264, x265 and libvpx)
Understanding Rate Control Modes (x264, x265, vpx)
https://www.winxdvd.com/video-converter/handbrake-output-file-size-larger-than-input.htm

 

One Comment

  1. 心灵博客 2019-12-04

    我也经常一个视频编码好多次,调整各项参数,到现在都没研究出一套文件又小,画质有清晰的参数。

Leave a Reply

Your email address will not be published. Required fields are marked *