H.264 编码实验
一、解码得到YUV文件
将自行选择两个264文件进行解码,得到相应的YUV文件。
使用jm_vc8中的ldecod,修改项目中的路径和文件名,输入H.264文件,输出YUV文件,输出YUV文件可用YUVViewer播放。
转换前:
![截屏2020-07-31下午4.02.53](https://tva1.sinaimg.cn/large/007S8ZIlgy1gha7s1gf3dj312a02imxh.jpg)
转换后:
![截屏2020-07-31下午4.13.31](https://tva1.sinaimg.cn/large/007S8ZIlgy1gha83207ikj31c005mgms.jpg)
二、将YUV文件重新编码后分析
1、将上述两个视频序列编码为.264文件
固定码率,以不同的GOP长度及形状编码
-
GOP=15,2B帧;GOP=12,2B帧,GOP=9,2B帧
-
GOP=4,1B帧;GOP=12,无B帧;GOP=1,全I帧
修改lencod下的encoder.cfg配置文件,涉及参数如下:
(以GOP=15,2B帧为例)
InputFile = "128x128_dec.yuv" # Input sequence
FramesToBeEncoded = 50 # Number of frames to be coded
SourceWidth = 128 # Source frame width
SourceHeight = 128 # Source frame height
OutputWidth = 128 # Output frame width
OutputHeight = 128 # Output frame height
ReconFile = "128x128_rec.yuv" # Reconstruction YUV file
OutputFile = "128x128_rec_GOP15-2B.264" # Bitstream
IntraPeriod = 15 # Period of I-pictures (0=only first)
IDRPeriod = 0 # Period of IDR pictures (0=only first)
NumberBFrames = 2 # Number of B coded frames inserted (0=not used)
RateControlEnable = 0 # 0 Disable, 1 Enable
通过修改IDR帧周期设置GOP组长度。
NumberBFrames表示P帧前插入B帧的个数。
编码后视频序列信息如下:
![截屏2020-08-02上午10.44.43](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghc9wn8fhyj310o0pm43x.jpg)
相同的GOP长度及形状,不同的码率
- 例:1000kb/s, 800kb/s,400kb/s,…
(以1000kbps为例)
InputFile = "flyer_converted_dec.yuv" # Input sequence
InputHeaderLength = 0 # If the inputfile has a header, state it's length in byte here
StartFrame = 0 # Start frame for encoding. (0-N)
FramesToBeEncoded = 30 # Number of frames to be coded
OutputFile = "flyer_converted_rec_1000kbps.264" # Bitstream
IntraPeriod = 10 # Period of I-pictures (0=only first)
IDRPeriod = 0 # Period of IDR pictures (0=only first)
RateControlEnable = 1 # 0 Disable, 1 Enable
Bitrate = 1000000 # Bitrate(bps) -- 可变 --
RCUpdateMode = 2 # Rate Control type. Modes supported :
# 0 = original JM rate control,
# 1 = rate control that is applied to all frames regardless of the slice type,
# 2 = original plus intelligent QP selection for I and B slices (including Hierarchical),
# 3 = original + hybrid quadratic rate control for I and B slice using bit rate statistics
#
编码后视频序列信息如下:
![截屏2020-08-04上午11.19.27](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghem2efo4pj310o0f440q.jpg)
2、用码流分析软件检查所生成的码流中各种编码模式和运动矢量等信息
分析编码后的flyer_converted_rec_800kbps.264文件
![截屏2020-08-05上午10.31.42](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghfqay5uwvj310s0suk0l.jpg)
![截屏2020-08-05上午10.30.34](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghfqabkw6pj30jk0d0t95.jpg)
该帧为码流中的第一帧,帧内Slice Type为I-Slice。I-slice中的宏块类型只能是I宏块类型,标记宏块类型为I_16x16_1_0_0。
![截屏2020-08-05上午10.40.37](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghfqk8kpy1j31100smdp1.jpg)
Frame2 中Slice Type均为B-Slice。B-slice中包含了I宏块类型与B宏块类型,图中蓝色部分宏块类型为B宏块类型,红色部分宏块类型为I宏块。
分析标记宏块的编码模式:
![截屏2020-08-05上午11.25.16](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghfruwiaprj310o0t244l.jpg)
宏块类型为B_skip,被分割成4个部分,分割后的块大小为8x8。
MV代表每个8x8块的运动矢量。
参考图像索引refIdx是以8x8块为单位的,每个块参考图像索引refidx均为0。
B_Skip类型宏块(200503版标准,表7-14最后一行): 无像素残差,无运动矢量残差(MVD)。解码时,通过Direct预测模式(时间或空间)计算出前、后向MV后,直接利用前、后向MV得到像素预测值。像素重构值=像素预测值。
L0:前向预测;L1:后向预测;Bi:双向预测。
有 B 图像的场合。POC 表示的是图像显示顺序。
![截屏2020-08-05上午11.55.26](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghfsq9a0hcj310c0rewjc.jpg)
宏块类型为I_8x8,调用亮度样点的Intra_8x8预测过程。色度样点采用8x8 Chroma DC方式预测。
本过程的输入是来自相邻宏块或宏块对的 Intra4x4PredMode (如果可用)的值或 Intra8x8PredMode(如果可用) 的值。 本过程的输出是 8x8 亮度样点阵列,它们是宏块 pred L 的预测样点的 16x16 亮度样点阵列的一部分。
![截屏2020-08-05下午7.04.08](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghg549h5lgj310m0sq47n.jpg)
Frame4中Slice Type均为P-Slice。P-slice中包含了I宏块类型与P宏块类型,图中绿色部分宏块类型为B宏块类型,红色部分宏块类型为I宏块。
分析标记宏块的编码模式:
![截屏2020-08-05下午7.07.59](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghg586fecgj30zs0qugq4.jpg)
宏块类型为P_Skip,采用前向预测编码,运动矢量为(0,0)。
与B-Skip类似,P-Skip既不传送像素残差,也不传送运动矢量残差(在这种情况下,像素残差和运动矢量残差必定都为零,所以根本不需要传送)。编码端除了传送一些标识该宏块是P-Skip宏块的很少量的bit外,不在需要传送关于该宏块的其他信息
运动矢量信息:
B帧 | P帧 |
---|---|
![]() | ![]() |
绿色矢量线表示利用前向预测,红色矢量线表示利用后向预测。
从图中可以看出,像素信息基本保持不变的宏块运动矢量为0,像素信息明显变化的宏块运动矢量不为0,相邻宏块的运动矢量方向大小相似。
以一块类型为B_L0_16x16的宏块为例分析宏块内具体像素信息:
残差数据 - Residual
![截屏2020-08-06下午1.06.26](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghh0imqpb4j30z30u00ux.jpg)
预测数据 - Predicted
![截屏2020-08-06下午1.06.12](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghh0nffs36j30u00ygtbb.jpg)
Pre-LoopFilter
![截屏2020-08-06下午1.06.04](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghh1yjw6chj30u00yktbc.jpg)
观察后可知,Residual+Predicted=Pre-LoopFilter。
3、用播放器观看所生成码流的质量
1000kbps | 800kbps | 400kbps |
---|---|---|
![]() | ![]() | ![]() |
4、生成率失真曲线
![image-20200806125138638](https://tva1.sinaimg.cn/large/007S8ZIlgy1ghh0n7cu0fj30k20c2di4.jpg)
由率失真曲线可知,GOP组结构相同时,比特率越大,编码后文件失真越小;比特率相同时,GOP越短,编码后文件失真越大。