纠结概念的人不少,这是个好事,但有时用实验的方法自己亲自分析一下会更好. 一次刻骨铭心的体验胜过千百次的说教, 闲话少扯,进入正题.
在MPEG2中,有个重要的概念叫GOP(group of pictures),假设编码的帧类型为:IBBPBBPBBPBBIBBPBBPBBPBBI..., 那么这个IBBPBBPBBPBB就叫一个GOP. 由于误差会积累,但MPEG2中的I帧可以阻断误差的积累,也就是说,在MPEG中I帧后面的帧永远不会参考I帧前面的帧,也就是说,一个GOP中的帧永远不会参考前一个GOP中的帧.(另外说句题外话:B帧可以参考下一个GOP的I帧,但在MPEG2中,B帧不会作为参考帧,所以B帧不会导致误差积累)
在H.264中就不同了.很多人说,在H.264中没有I帧这个概念了,当然这也是有道理的,标准中的确没有这么叫,但是,为了方便,也可以延续I帧这个概念,那么H.264中什么叫I帧呢?
定义:H.264中的I帧是指帧中的宏块都是采用帧内预测方式,在H.264中有两种I帧: 普通I帧和IDR帧(特殊I帧).
在H.264中,是IDR帧阻断了误差的积累, IDR帧后面的帧都不能参考该IDR帧前面的帧. 在H.264中,普通的I帧并没有阻断误差的积累,那就是说普通I帧后面的帧就可以参考该I帧之前的帧么?事实正是如此. 下面用H.264visa加以分析验证.
将foreman编码成 IDR BBPBBPBBPBBPBBIBBPBBPBBPBBI...,用H.264visa对码流进行分析. 分析第16帧(P帧)的某一宏块,该宏块信息为:
==== MB 61(6, 5) ====
Location : (96, 80),
Slice No. : 0
Slice Type : P Slice
MB Type : (3)P_8x8
NumMbPart : 4
MbPartSize : (8, 8)
Subblock Type:
+-----------------+----------------+
| (2)P_L0_4x8 | (2)P_L0_4x8 |
+-----------------+----------------+
| (2)P_L0_4x8 | (2)P_L0_4x8 |
+-----------------+----------------+
****** Inter Info ******
Block(0, 0):
(0,0) L0=MV(63,-7),POC:24,refIdx:0,DecNo: 11
(1,0) L0=MV(71,-5),POC:24,refIdx:0,DecNo: 11
Block(1, 0):
(0,0) L0=MV(31, 3),POC: 6,refIdx:3,DecNo: 2
(1,0) L0=MV(28, 4),POC: 6,refIdx:3,DecNo: 2
Block(0, 1):
(0,0) L0=MV(66,-6),POC:24,refIdx:0,DecNo: 11
(1,0) L0=MV(69,-3),POC:24,refIdx:0,DecNo: 11
Block(1, 1):
(0,0) L0=MV(-32,18),POC: 0,refIdx:4,DecNo: 1
(1,0) L0=MV(-29,10),POC: 0,refIdx:4,DecNo: 1
易知,该宏块中一个小块的实际运动矢量为(7, 1), 该小块参考了第4帧(P帧),也就是说第16帧参考了第4帧,而这两帧分居不同的GOP之中,可见P帧确实跨越参考了普通I帧前面的帧. 下面来进一步证实. 第16帧中这个宏块中运动矢量为(7,1)的小块对应的预测像素值为:
====================== Y Data ======================
+----------------+----------------+----------------+----------------+
|135,134,134,134,|133,130,149,165,|165,186,201,195,|192,178,159,140,|
|134,134,136,137,|157,162,178,187,|175,184,189,188,|185,177,165,150,|
|133,139,148,156,|179,183,186,190,|171,181,184,182,|180,175,165,150,|
|129,141,154,162,|160,166,168,173,|165,174,176,173,|171,171,164,151,|
+----------------+----------------+----------------+----------------+
|119,123,123,123,|123,135,145,152,|160,167,169,167,|165,165,165,153,|
|100, 97, 83, 72,| 79,102,126,134,|150,155,158,157,|153,153,158,151,|
| 95, 84, 69, 54,| 58, 72, 96,114,|136,143,145,146,|143,142,141,140,|
|120,100, 82, 70,| 65, 59, 73, 98,|119,126,129,131,|126,125,126,126,|
+----------------+----------------+----------------+----------------+
|139,126,108,103,|109, 95, 81, 88,|108,111,109,111,|119,115,105, 92,|
|152,144,130,116,|109,106,102,106,|107,108,107,108,|107,103, 97, 95,|
|154,145,135,119,|109,104,101, 99,|106,104,105,104,|107,102,100,107,|
|158,154,146,128,|107, 96, 93, 95,|105,103,103,104,|108,110,111,115,|
+----------------+----------------+----------------+----------------+
|172,171,170,144,|121,101, 90, 91,|103, 99,103,104,|109,114,119,122,|
|164,164,164,157,|141,119,100, 95,|103, 97,104,106,|110,115,120,124,|
|137,138,136,142,|128,129,121,113,|108,108,111,114,|111,116,123,127,|
|128,125,125,123,|116,113,113,110,|114,115,116,117,|113,118,124,128,|
+----------------+----------------+----------------+----------------+
根据运动矢量(7,1)去第4帧的重建帧找预测块,果然就找到了,如下:
====================== Y Data ======================
+----------------+----------------+----------------+----------------+
|159,180,194,183,|166,147,133,125,|121,122,124,128,|130,136,127,145,|
|174,190,200,192,|178,159,140,127,|122,124,126,130,|133,135,127,116,|
|177,184,186,185,|177,165,150,130,|123,126,129,131,|134,137,136,117,|
|172,182,183,180,|175,165,150,132,|124,126,130,133,|134,135,135,119,|
+----------------+----------------+----------------+----------------+
|165,173,172,171,|171,164,151,132,|121,125,128,132,|134,135,137,116,|
|159,167,166,165,|165,165,153,139,|115,120,125,130,|133,134,136,112,|
|147,152,154,153,|153,158,151,140,|110,116,122,128,|131,134,131,107,|
|135,141,142,143,|142,141,140,127,|108,112,118,126,|130,134,131,105,|
+----------------+----------------+----------------+----------------+
|116,122,125,126,|125,126,126,118,|106,109,116,124,|128,134,131,105,|
|104,111,112,113,|112,113,113,109,|106,106,115,123,|128,132,134,104,|
|104,109,110,112,|113,113,112,111,|109,106,113,120,|128,130,132,103,|
|104,107,108,111,|113,117,121,122,|117,111,112,120,|128,129,124,103,|
+----------------+----------------+----------------+----------------+
|104,105,108,112,|115,119,125,125,|123,120,118,122,|127,129,125,103,|
|103,106,109,116,|120,123,126,127,|127,125,125,122,|127,127,120,100,|
|103,106,110,118,|123,126,128,129,|130,128,127,126,|126,124,115, 99,|
|104,106,111,118,|125,129,130,131,|130,130,128,128,|128,123,110, 98,|
+----------------+----------------+----------------+----------------+
再次总结:在H.264中,I帧分为普通I帧和IDR帧(特殊I帧); 在H.264中,是IDR帧阻断了误差的积累, IDR帧后面的帧都不能参考该IDR帧前面的帧, 普通的I帧并没有阻断误差的积累,普通I帧后面的帧可以参考该I帧之前的帧. 在MPEG2中,I帧阻断了误差的积累,I帧后面的帧不可以参考该I帧之前的帧. 从这个意义上说,H.264中的IDR帧颇有MPEG2中I帧的味道.