H.264帧编码原理简述
|Last edited: 2024-5-1

帧内预测(intra-prediction)

 
帧内预测(Intra-prediction)是视频编码中的一个重要概念,它是从已编码的帧中形成预测的。在视频压缩编码中,通常使用三种类型的帧:I帧、P帧和B帧。I帧是关键帧,它只使用帧内预测。P帧和B帧则会根据情况使用帧内预测或帧间预测。
帧内预测是从当前帧的已编码部分形成预测的。这个过程可以被视为一种精确的自我编码。首先,需要决定帧的类型。如果是I帧,它只使用帧内预测。如果是P帧或B帧,它会根据帧内预测和帧间预测的成本为每个宏块选择一种预测方式。其针对一帧(或者不满一帧)的数据,进行编码。编码时所参考的数据就是这一帧,换言之,解码也就只需要这一帧数据。这种编码方式也被用于图片的编码中,例如,jpeg 图片的编码方式就和 H.264 的帧内编码方式极度相似。
预测的过程是这样的:首先,将原始数据减去预测值,得到的就是残差数据。然后,对残差数据进行离散余弦变换(DCT)和量化。在编码部分,会进行zigzag扫描,并使用预测来减小一些头信息的大小。对于DCT和量化的结果,还会进行反向计算。然后,将预测值和反向计算的结果加在一起,这就完成了精确的自我编码。
帧内预测使用受限的数据进行下一次的帧内预测。在添加到参考列表并被帧间预测使用之前,会使用环路滤波器来减少块效应失真。这就是帧内预测的基本概念和过程。

残差的意义

在视频编码中,残差数据是原始数据和预测数据之间的差异。这个概念来自于预测模型,其中预测值通常不会完全准确,因此会有一些“残差”或者说是误差。
在我们对视频帧进行编码时,我们通常会尝试预测每个像素的值,然后计算出预测值和实际值之间的差异,这就是残差。这样做的好处是,残差数据通常会比原始像素数据有更多的零值,这使得它们更容易被压缩和编码。
例如,如果我们预测一个像素的值为200,而实际值为205,那么残差就是5。在编码这个残差值时,我们只需要存储这个小的差异,而不是完整的像素值。这大大减少了需要存储的信息量,从而实现了数据的压缩。
在视频编码中,我们通常会对残差数据进行进一步的处理,比如离散余弦变换(DCT)和量化,然后再进行编码。这些步骤都是为了进一步压缩数据,减少所需的存储空间和带宽。下面是原始的图像和计算残差后可视化的图像。
notion image
notion image
那为什么会出现残差呢,是因为预测的不准,我们来看下实际是如何预测的。

预测模式

在 H264 中,图像通过YUV表示,“Y”表示明亮度(Luminance、Luma),“U”和“V”则是色度、浓度(Chrominance、Chroma),和一般的RGB和HSV不太一样。
在帧内预测模式下,预测块P是基于先前编码和重构的块生成的,并在编码当前块之前从当前块中减去。对于亮度(luma)样本,每个4×4块或16×16宏块都会形成一个预测块P。每个4×4亮度块有九种可选的预测模式,每个16×16亮度块有四种模式,色度(chroma)组件也有四种模式。编码器通常会选择使预测块P和待编码块之间的差异最小的预测模式。
还有一种特殊的编码方式I PCM,I PCM是一种特殊的帧内编码模式,它允许编码器直接传输图像样本的值(无需预测或转换)。在一些特殊情况下(例如,异常的图像内容和/或非常低的量化参数),这种模式可能比通常的帧内预测、变换、量化和熵编码过程更有效率。包括I PCM选项可以在不限制解码图像质量的情况下,对编码宏块中可能包含的位数设置绝对限制。

亮度

对于亮度,有以下9种预测模式
notion image
 
notion image
 
我们可以认为,对于一个宏块,其中的亮度通过左和上的像素是可以拟合 成以上9种模式中的一种,图片中可以很清晰的看到基本上每一个块都拟合了上面9种模式中的一种。但是缺陷是什么呢,就是这种预测之后会失真。可以看实际图和预测图中的区别:
notion image
notion image
可以很清晰的看出两者的区别,这样就需要通过存储残差信息来弥补和实际图中的差别
 
对于更大的块,可以用更大的8x8或者16x16的预测方法,实际上差不太多。一般来说,H.264/AVC支持在16x16和4x4的块上进行预测。这两种预测模式被称为宏块预测(16x16)和子块预测(4x4)。
然而,H.264/AVC还支持8x8变换。这是一种可选的特性,可用于替代默认的4x4变换。在某些情况下,使用8x8变换可以提供更好的编码效率。对于宏块预测,只有4种方式,而由于8x8变换是子块预测的替代,所以8x8变换也有9种。
 
notion image
红色是16x16,紫色是8x8,蓝色是4x4,
notion image
16x16的4种模式

色度

对于色度,有4种模式,实际的情况和亮度的16x16的4种模式是一样的,所以图也是一样的
在H.264/AVC编码中,色度(chroma)的预测通常在8x8的块上进行。这是因为在许多情况下,色度信号的采样率比亮度信号的采样率低,这被称为色度子采样(chroma subsampling)。例如,在常见的4:2:0子采样模式中,每四个亮度样本对应一个色度样本。因此,一个16x16的亮度块对应一个8x8的色度块。
色彩採樣在 4×2 個陣列中每 8 個像素發生一次。
4 列像素的亮度 (Y) 通道被記錄下來,因此第一個數字是 4。 第二個數字表示從第一行像素記錄的色差信號 (CbCr) 數量。 第三個數字表示從第二行像素記錄的色差信號 (CbCr) 數量。
notion image
notion image
在流分析软件中,16x16的色度块实际上是对原始视频的一个视觉化表示。在4:2:0色度子采样模式中,一个16x16的亮度宏块对应一个8x8的色度块。然而,为了在流分析软件中对视频进行可视化,这些8x8的色度块可能会被扩大到16x16的大小以匹配亮度宏块的大小。这样做是为了更方便地展示和比较亮度和色度的数据。这只是一种可视化的方式,实际的色度数据仍然是在8x8的块上进行预测和编码的。这是因为在4:2:0色度子采样模式中,色度信号的分辨率是亮度信号的一半。
notion image
切换到色度模式,可以很清楚的看到是8x8的模式
至于编码器要挑选哪种模式,这个要根据SAE来判断

SAE

SAE指的是"Sum of Absolute Errors",即绝对误差之和。这是一种常见的误差度量方式,用于评估预测或编码操作的效果。
在视频编码中,我们通常需要对原始视频数据进行预测,以减少需要编码和传输的数据量。例如,我们可能会预测一个块的像素值,然后只编码预测值和实际值之间的差异(称为预测误差或残差)。这样可以大大减少需要编码的数据量,从而提高编码效率。
SAE就是用于评估这种预测效果的一种度量。它是预测误差的绝对值之和。如果SAE值较小,那么说明预测效果较好,因为预测误差较小。反之,如果SAE值较大,那么说明预测效果较差。
notion image
8的SAE最低,所以编码器会选择8
 
具体怎么算出来的,就无所谓了,太复杂了,不是做编码器没必要了解

帧间预测(inter-prediction)

帧间预测是一种利用视频帧之间的时间相关性来减少冗余的方法。在这种方法中,编码器会从一个或多个先前编码的帧(或场)中创建预测模型,然后用这个模型来预测当前帧的内容。预测的结果和实际的内容之间的差异(称为预测误差或残差)将被编码和传输。
帧间预测主要依赖于运动补偿。运动补偿是一种估计和补偿视频帧之间运动的方法。在这种方法中,编码器会为每个块或子块找到一个运动向量,这个向量表示了这个块在先前帧中的对应位置。然后,编码器会用这个位置的内容来预测当前块的内容。

树形结构的运动补偿

树形结构的运动补偿是一种灵活的运动补偿方法。在这种方法中,每个宏块(16x16的亮度样本)可以被分割成多种方式,如一块16x16的宏块,两块16x8的块,两块8x16的块,或者四块8x8的块。如果选择了8x8模式,那么每个8x8的子宏块还可以进一步分割,如一块8x8的子宏块,两块8x4的子宏块,两块4x8的子宏块,或者四块4x4的子宏块。这种分割方式可以为每个宏块提供大量的组合可能性。
notion image
每个分割或子宏块都需要一个单独的运动向量。这个运动向量必须被编码和传输,而且分割的选择也必须被编码进压缩比特流。因此,分割大小的选择对压缩性能有显著的影响。一般来说,对于帧的均匀区域,选择大的分割大小比较合适;而对于详细的区域,选择小的分割大小可能更有利。
notion image
色度分量(Cb和Cr)在宏块中的分辨率是亮度分量(luma)的一半。每个色度块的分割方式和亮度分量相同,但是分割大小的水平和垂直分辨率正好是亮度分量的一半。例如,一个8x16的亮度分割对应一个4x8的色度分割;一个8x4的亮度分割对应一个4x2的色度分割,等等。每个运动向量的水平和垂直分量在应用到色度块时都会被减半。这和前面说的一样。
在实际的编码过程中,编码器会选择最佳的分割大小。例如,对于帧中变化不大的区域(残差看起来是灰色的),可以选择16x16的分割;而对于有详细运动的区域(残差看起来是黑色或白色的),小的分割可能更有效。
notion image

运动补偿插值

在运动补偿中,我们需要从参考帧中找到一个与当前块相同大小的区域来进行预测。这个区域的位置由运动向量决定,运动向量在亮度分量上具有四分之一像素的精度,在色度分量上具有八分之一像素的精度。然而,参考帧中并不存在这种精度的样本,因此需要通过插值来生成这些样本。简单说,运动补偿是通过运动向量来实现的,运动向量描述了参考画面中的区域与当前画面块之间的位置偏移。不过,这个偏移量可能是一个小数,这意味着我们需要预测的画面块可能位于参考画面的像素之间。这时,我们就需要通过插值方法来生成这些不存在的像素。
插值的过程如下:
  1. 首先,生成亮度分量中位于整数位置样本之间的半像素样本。这些样本是通过有限冲激响应(FIR)滤波器进行插值得到的,滤波器的权重为(1/32, -5/32, 5/8, 5/8, -5/32, 1/32)。例如,半像素样本b是通过六个水平整数样本E, F, G, H, I和J计算得到的。
    1. notion image
  1. 一旦计算出所有与整数样本水平和垂直相邻的半像素样本,就可以通过插值生成剩余的半像素位置的样本。
  1. 当所有的半像素样本都生成后,就可以通过线性插值生成四分之一像素位置的样本。
    1. notion image
  1. 对于色度分量,由于采样率是4:2:0,所以需要生成八分之一像素间隔的插值样本。这些样本是通过线性插值得到的,每个子样本位置a是相邻的四个整数样本位置A、B、C和D的线性组合。
 

运动向量计算

在视频编码中,运动向量(Motion Vector, MV)的主要作用是进行运动补偿预测(Motion Compensation Prediction)。这是一种利用视频序列中帧与帧之间的时间相关性来减少冗余的技术。
具体来说,当我们对一帧中的一个宏块(Macroblock, MB)或分区(Partition)进行编码时,我们可以在前一帧或后一帧中找到一个相似的宏块或分区作为参考,然后计算出这两个宏块或分区之间的运动向量。这个运动向量就表示了当前宏块或分区相对于参考宏块或分区的位置移动。
在视频编码中,对每个分区进行运动向量的编码可能会消耗大量的位数,特别是当选择较小的分区大小时。由于相邻分区的运动向量通常高度相关,因此每个运动向量都是根据附近已编码分区的向量进行预测的。首先形成一个预测向量MVp(MV predictor),它是基于之前计算的运动向量形成的,然后编码并传输当前向量与预测向量之间的差值MVD(MV Delta)。
预测向量MVp的形成方法取决于运动补偿分区的大小以及附近向量的可用性。假设E是当前的宏块、宏块分区或子宏块分区,A是E左边的分区或子分区,B是E上方的分区或子分区,C是E右上方的分区或子宏块分区。如果E左边有多个分区,选择最上面的一个作为A。如果E上方有多个分区,选择最左边的一个作为B。当没有C时,就用D。
notion image
对于运动向量的预测和编码,规则如下:
  1. 对于除16×8和8×16分区大小外的传输分区,预测向量MVp是分区A、B和C的运动向量的中值。
  1. 对于16×8的分区,上部16×8分区的预测向量MVp是根据B预测的,下部16×8分区的预测向量MVp是根据A预测的。
  1. 对于8×16的分区,左侧8×16分区的预测向量MVp是根据A预测的,右侧8×16分区的预测向量MVp是根据C预测的。
  1. 对于跳过的宏块,生成一个16×16的预测向量MVp,就像上述情况1一样(即,就像块以16×16的Inter模式编码一样)。
如果图6.19中显示的一个或多个先前传输的块不可用(例如,如果它在当前切片之外),则相应地修改预测向量MVp的选择。在解码器中,以同样的方式形成预测向量MVp,并将其加到解码的向量差MVD中。在跳过的宏块的情况下,没有解码的向量差,使用预测向量MVp作为运动向量生成运动补偿宏块。
简单说就是
  1. 如果所有考虑的块都存在,并且它们的运动向量都相同,那么这个相同的运动向量就被用作MV predictor(MVp)。
  1. 否则,编码器会分别计算这些块的运动向量的平均值,并将结果四舍五入到最近的整数,这个结果就被用作MV predictor(MVp)。
 
notion image
notion image
在码流分析软件里右键,分析选中块的数据:
  • Final MV (最终运动矢量): 宏块的最终运动矢量是水平方向移动2个像素,垂直方向移动6个像素。
  • refIdx (参考帧索引): 当前宏块的运动矢量是相对于索引为0的参考帧计算的。
  • MV Delta (运动矢量差): 运动矢量相对于预测值的变化,在这里,水平方向变化了1个像素,垂直方向没有变化。
  • MV predictor (运动矢量预测器): 用于预测当前宏块运动矢量的值,在这里是水平方向1个像素,垂直方向6个像素。
在视频编码中,为了节省位数,我们通常不直接编码运动向量,而是编码运动向量与预测向量的差值(MVD)。因为相邻的宏块的运动向量通常是高度相关的,所以这个差值通常会很小,需要的位数也就更少。
 
 
运动向量是用来进行运动补偿预测的,它表示的是宏块或分区在参考帧中的相对位置移动。但即使运动向量完全准确,也并不能保证所有的像素都能完全匹配。因为在视频序列中,除了物体的运动,还可能存在光照变化、物体形状的变化等因素,这些都可能导致即使运动补偿预测完全准确,也仍然存在一些像素的误差。
这些误差需要通过预测误差(residual)来进行纠正。预测误差是原始宏块与运动补偿预测宏块之间的差,它反映了运动补偿预测无法预测的部分。预测误差需要进行变换编码,然后与运动向量一起发送,以便在解码端进行解码和重建。
 
 
 
 
 
下面我将详细介绍每个组件。首先,我想谈谈它们是如何决定帧类型的。我使用码流 I 来帮助我分析比特流。这是 B 帧。红色代表 I 帧,蓝色代表 B 帧、P 帧,绿色代表 B 帧。那么,它是如何确定的呢?I 帧由两个因素决定。在没有明显变化的情况下,它由精确参数 GOP 大小决定。如果 GOP 大小为 100,那么每 100 个 GOP 就会产生一个 I 帧。当检测到事物发生重大变化时,无论 GOP 的精确大小如何,它都会立即插入一个 I 帧。我们再来看看视频。当事物发生变化时,它会插入一个 I 帧。我想你现在对如何决定帧类型有了一个简单的了解。GOP 是一个精确的参数。但如何计算事物的重大变化呢?对于当前帧,它会计算整个帧的帧间预测和帧内预测成本。如果帧内预测成本较低,说明它无法很好地预测上一编码帧,因此会检测到事物的变化,从而插入一个 I 帧。让我们看下一张幻灯片。你可能会问,在精确决定帧类型的过程中,它会进行帧间预测和帧内预测。但在此之后,它还会进行内部和内部预测。那么,两者有什么区别呢?这个部分的预测只是决定帧类型,与最终的比特流无关,因此需要足够快地进行精确预测。这一阶段的预测计算基于较低分辨率的帧。例如,原始宽度为 1920,低分辨率帧的宽度为 960。
好吧,除此之外,我觉得它做得太过分了,没有选择性分区模式。本来是有很多分区模式的,但这两种模式都能快速做出决策。现在我们来谈谈内部预测。它根据当前帧中先前编码的部分进行预测。总共有九种预测模式。前三种模式 前三种模式 前三种模式 垂直模式、水平模式和直流模式。为了帮助大家更好地理解,我绘制了这张表格。对于垂直模式,它使用上述数值。对于水平模式,则使用实时值。16 x 16 微块可分为 16 个 4x4 块。它会计算每个分区模式的 SATD (sum absolute transform distortion),并选择每个分区块中 SATD 最小的模式。计算所有预测模式的 SATD,选择 SATD 最小的预测模式。例如,为了计算 4x4 分区模式的 SATD,需要对每个 4x4 块使用 9 种预测模式,并选择 SATD 最小的预测模式。总共需要计算 16 次。它们的总和就是 4x4 分区模式的 SATD。让我们来看一个实际例子。这是视频的一帧。我也用这个流眼来帮助我进行分析。让我们来看看这两个部分。天空的分割模式为 16 x 16 和 8 x 8。对于细节部分,16 x 16 和 8 x 8 的分割模式不能很好地发挥作用,因此使用了 4x4 的分割模式。你可能会问,为什么不是所有的分区模式都使用 4x4 模式呢?因为它需要更多的计算资源。只有在真正需要时,它才需要使用 4x4 模式。为了避免使用小分区模式,在计算 4x4 模式的 SATD 时,它会添加一个小惩罚。当它计算 4x4 模式的 SATD 时,会增加较大的惩罚。我已经讲完了 I 帧的内部预测。下面我们来看 p 帧的预测。这就比较复杂了,因为对于每个宏块,它都需要决定使用区间预测还是内部预测。因此,它需要比较区间预测和内部预测的 SATD,并选择 SATD 最小的那个。在前面的幻灯片中,我已经谈到了内部预测。内部预测最重要的是运动估计。这就是选择一个预测区域并生成一个预测块。当前分区的位置与预测区域之间的偏移量就是运动矢量。为了进行良好的运动估算,估算以四分之一像素为基础。四分之一像素是通过相邻符号之间的插值产生的。这意味着有更多的像素和更多的分区模式。它需要计算每个分割模式的 SATD,并选择 SATD 最小的模式。让我们来看看真实视频帧中分区模式的选择。画面右侧是天空。没有太阳。让我们看看实际视频帧中的分区模式选择。右侧没有太多运动。大部分分区模式都是 16 x 16 分区模式。在画面的左侧,有一个人在骑自行车。为了很好地预测运动,大部分分割模式都是 8 乘 8 模式。现在我们知道如何进行预测了。我们用原始帧减去预测值,然后得到残差数据。残差数据是什么样的呢?让我们将其可视化。
从视频中我们可以发现,在运动较多的地方,预测并不准确。因此,该部分的残留数据较大。我们再来看看。当运动量较大时,预测效果不佳。这会导致残留数据增大。最后一段视频是残留数据,这部分视频是原始视频。现在我们来进行转换。对于每个 16 x 16 的宏块,它会进行 16 次 4 x 4 的 DCT 变换。从图中我们可以发现还有另一种变换。第二次变换应用于第一次变换的最低直流频率系数。这些直流值往往高度相关,第二次变换可以提高编码性能。直流系数是相同的。直流系数块使用 4 乘 4 的硬标记变换进行进一步变换。然后对 4×4 直流块和 16×16 交流块进行量化。这就是精确的正向变换量化。这就是反变换和反量化的精确性。它与上述精度非常相似。变换和量化之后,需要进行编码。首先,需要编码。它使用帧的分区模式和量化值等信息,但并不直接进行编码。为了提高编码性能,它使用预测来减少比特。对于 I 帧,它有三种分区模式。它使用上块和左块分区模式的最小值进行预测。如果预测值相同,则无需对该模式进行编码。对于 P 帧,它需要预测块的数量。对于 P-Frame,需要预测块数。对于 P-Frame,需要预测块的数量。运动矢量、预测矢量是左上方和左下方区块矢量的中值矢量。它只需要对实际向量和预测向量之间的差值进行编码。至于量化值的编码,只需编码实际量化值和最后一个块量化值之间的差值。在完成精确的变换和量化后,它会以之字形顺序扫描系数块,然后进行编码。这部分已经完成,下面来谈谈循环滤波器。在将构建的帧添加到参考列表之前,需要使用循环滤波器。滤波器可以平滑块边缘,改善解码帧的外观。这通常会提高压缩性能,因为滤波后的图像比块状和滤波后的图像更适合再现原始帧。从这两幅图中,我们可以发现这幅图比这幅图更适合。H. 264 编码的整个过程就介绍到这里,感谢您的收听。
 
1. 264 is a method and format for video compression, the precise of converting video into a format that takes up less capacity, and it is an industry standard. The presentation well describes all the components in this figure. Before talking about the whole precise of encoding in this figure, let me give the definition of some concepts. So first, what is I-frame, P-frame, and B-frame? I-frame does not depend on any frames, it uses intra-prediction to encode the frame. P-frame and B-frame can use the previous encoded frame as reference, so it can use inter-prediction to encode. These two figures are generated by stream I, I also use this bitstream analyzer to analyze. Red column represents I-frame, blue column represents B-frame. So we can find that P-frame uses the last frame. We can see that the previous frame used P-frame and B-frame, both are these frames. In the previous slide, I have shown you how to use these three frames to do inter-prediction. I-frame does not depend on any frame. I have just mentioned two terms, inter-prediction and intra-prediction, what do they mean? Intra-prediction form the prediction from the coded frame. It is used by P-frame and B-frame. Intra-prediction form the prediction from the previously coded parts from the current frame. Let me give the brief introduction. introduction of the precise self-encoding. This is the current frame which is waiting to be encoded. First it needs to decide the frame type. If it is I-frame, it only uses intra-prediction. If it is P or B-frame, it uses intra-prediction or inter-prediction for each macro block, depending on the cost of intra-prediction and intra-prediction. Manager original data minus prediction is the residual data. It does DCT and quantization for the residual data. As for the coding part, it does zigzag and also uses the prediction to reduce the size of some header information. For the result of DCT and quantization, it also do the reverse calculation for it. After that, it adds the prediction and the reverse calculation result together. This precise self-encoding. Intra-prediction uses the restricted data for the next intra-prediction. Before adding to the reference list and being used by inter-prediction, it uses loop filter to reduce blocking distortion. I will talk about each component in detail. First I want to talk about how that they decide the frame type. I use stream I to help me to analyze the bitstream. This is the B-frame. Red color represents I-frame, blue color represents B-frame, P-frame, green color represents B-frame. So, how does it determine this? I-frame is determined by two factors. When there is no significant change of thing, it is determined by precise parameter GOP size. If GOP size is 100, every 100 of them will generate an I-frame. When a significant change of thing is detected, it inserts an I-frame instantly, regardless of the precise GOP size. Let's see the video again. When the thing changes, it inserts an I-frame. I think now you have a brief understanding how to decide the frame type. GOP is a precise parameter. But how to calculate the significant change of thing? For the current frame, it calculates the cost of inter- and intra-prediction for the whole frame. If the cost of intra-prediction is less, it means that it cannot predict from the previous coded frame well, so it detects the change of thing, so that an I-frame is inserted. Let's see the next slide. You may ask, in the precise of deciding the frame type, it does inter- and intra-prediction. But after that, it also does the intra- and intra-prediction. So, what is the difference? The prediction in this component is only about deciding the frame type, it is not related to the final bitstream, so it needs to make this precise fast enough. The calculation of the prediction of this stage is based on lower-resolution frames. For example, the original width is 1920, the width of lower-resolution frame is 960. Okay. So, apart from that, I think it goes too far, there is no selective partition mode. Originally, there are many partition modes, but both of them make the decision fast. Now let's talk about the intra-prediction. It forms the prediction from the previously coded part from the current frame. Totally, it has nine prediction modes. For the first three modes. For the first three modes. For the first three modes. For the vertical, horizontal, and DC. I draw this table to help you understand this most. For the vertical mode, it uses the above values. For the horizontal mode, it uses the live values. The 16 x 16 micro block can be divided into 16 4x4 block. It calculates the STTD for each partition mode and choose the mode with the smallest STTD. of each partition block. It calculates STD for all the prediction modes and choose the prediction mode with the smallest STD. For example, in order to calculate STD for 4x4 partition mode, it needs to use 9 prediction modes for each 4x4 block and choose the prediction mode with the smallest STD. Totally, it needs to calculate 16 times. The sum of them is STD of the 4x4 partition mode. Let's see an example in reality. This is a frame of the video. I also use this stream eye to help me analyze. Let's have a look at these two parts. The partition mode of the skies is 16 by 16 and 8 by 8. For the detailed part, 16 and 8 by 8 partition mode cannot work well, so it uses 4x4 partition mode instead. You may wonder why does not all the partition modes use 4x4 mode? Because it needs more calculation resource. It needs to use 4x4 mode only when it really needs it. In order to avoid using the small partition mode, when it calculates STD for 4x4 mode, it adds a small punishment. When it calculates STD for 4x4 mode, it adds a larger punishment. I have finished talking about the intra-prediction for I-frame. Let's come to the prediction for p-frame. It is more complicated because for each macroblock, it needs to decide to use inter-prediction or intra-prediction. So it needs to compare the STD for inter and intra-prediction and choose the one with the smallest STD. In the previous slides, I have talked about the intra-prediction. The important thing for intra-prediction is about motion estimation. This is about selecting a prediction region and generating a prediction block. The offset between the position of the current partition and the prediction region is the motion vector. In order to make good motion estimation, the estimation is based on quarter pixel. The quarter pixel is generated by interpolation between adjacent symbols. It means that there are more pixels and there are more partition modes. It needs to calculate the STD for each partition mode and choose a mode with the smallest STD. Let's see the selection of partition modes in the real video frame. At the right hand side of the frame, it is sky. There is no sun. Let's see the selection of partition modes in the real video frame. At the right hand So there is not so much motion. Most of the partition modes are 16 by 16 partition mode. In the left-hand side of the frame, there is a person riding bicycle. In order to make the good prediction of the motion, most of the partition modes are 8 by 8 mode. Now we know how to do prediction. We use original frame minus prediction, then we get the residual data. What does residual data look like? Let's visualize it. From the video, we can find that the prediction is not accurate in where there is much motion. So the residual data in that part is larger. So let's see it again. When there is much motion, the prediction is not good. It leads to the larger residual data. The last video is residual data, and this part of video is the original video. Now let's come to the translation. For each 16 by 16 macroblock, it does 16 times 4 by 4 DCT transform for it. From the figure, we can find that there is the other transform. The second transform is applied to the lowest of DC frequency coefficients of the first transform. These DC values tend to be highly correlated, and the second transform improves the coding performance. The DC coefficients are the same. The DC coefficient block is further transformed using a 4 by 4 hard-marked transform. Then it does quantization for 4 by 4 DC block and 16 by 16 AC block. This is the precise of forward transform quantization. And this is the precise of inverse transform and inverse quantization. It is very similar to the above precise. After transform and quantization, it needs to do coding. First, it needs to encode the code. It uses the frame's information like partition mode and quantization value, but it does not directly encode them. In order to improve the coding performance, it uses prediction to reduce the bits. For I Frame, it has three partition modes. It uses minimal value of the upper and left blocks partition mode to predict. If it is the same, it does not need to encode the mode. For P-Frame, it needs to predict the number of blocks. For P-Frame, it needs to predict the number of blocks. For P-Frame, it needs to predict the number of blocks. The motion vector, the prediction vector is the median vector of the upper and left blocks vector. It only needs to encode the difference between the actual vector and prediction vector. As for encoding the quantization values, it only encodes the difference between the actual quantization and last block quantization. After finishing the precise of transform and quantization, it scans the coefficient blocks in a zigzag order, then encodes. We have finished this part, let's talk about the loop filter. Before adding the constructed frame into the reference list, this needs to use loop filter. The filter smooths block edges, improving the appearance of the decoded frame. This generally improves compression performance because the filtered image is more fit for reproduction of the original frame than a blocky and filtered image. From these two figures, we can find that this figure is more fit for than this figure. So that's all for the whole process of H. 264 encoding, thanks for listening.