在視頻編碼序列中,主要有三種編碼幀:I幀、P幀、B幀,如下圖3.1所示。
● I幀即Intra-coded picture(幀內(nèi)編碼圖像幀),不參考其他圖像幀,只利用本幀的信息進(jìn)行編碼
● P幀即Predictive-coded Picture(預(yù)測(cè)編碼圖像幀),利用之前的I幀或P幀,采用運(yùn)動(dòng)預(yù)測(cè)的方式進(jìn)行幀間預(yù)測(cè)編碼
● B幀即Bidirectionally predicted picture(雙向預(yù)測(cè)編碼圖像幀),提供最高的壓縮比,它既需要之前的圖
像幀(I幀或P幀),也需要后來(lái)的圖像幀(P幀),采用運(yùn)動(dòng)預(yù)測(cè)的方式進(jìn)行幀間雙向預(yù)測(cè)編碼
在視頻編碼序列中,GOP即Group of picture(圖像組),指兩個(gè)I幀之間的距離,Reference(參考周期)指兩個(gè)P幀之間的距離(如下圖3.1)。一個(gè)I幀所占用的字節(jié)數(shù)大于一個(gè)P幀,一個(gè)P幀所占用的字節(jié)數(shù)大于一個(gè)B幀(如下圖3.1所示)。
圖3.1 I、P、B幀示意圖
所以在碼率不變的前提下,GOP值越大,P、B幀的數(shù)量會(huì)越多,平均每個(gè)I、P、B幀所占用的字節(jié)數(shù)就越多,也就更容易獲取較好的圖像質(zhì)量;Reference越大,B幀的數(shù)量越多,同理也更容易獲得較好的圖像質(zhì)量。
需要說(shuō)明的是,通過(guò)提高GOP值來(lái)提高圖像質(zhì)量是有限度的,在遇到場(chǎng)景切換的情況時(shí),H.264編碼器會(huì)自動(dòng)強(qiáng)制插入一個(gè)I幀,此時(shí)實(shí)際的GOP值被縮短了。另一方面,在一個(gè)GOP中,P、B幀是由I幀預(yù)測(cè)得到的,當(dāng)I幀的圖像質(zhì)量比較差時(shí),會(huì)影響到一個(gè)GOP中后續(xù)P、B幀的圖像質(zhì)量,直到下一個(gè)GOP開(kāi)始才有可能得以恢復(fù),所以GOP值也不宜設(shè)置過(guò)大。
同時(shí),由于P、B幀的復(fù)雜度大于I幀,所以過(guò)多的P、B幀會(huì)影響編碼效率,使編碼效率降低。另外,過(guò)長(zhǎng)的GOP還會(huì)影響Seek操作的響應(yīng)速度,由于P、B幀是由前面的I或P幀預(yù)測(cè)得到的,所以Seek操作需要直接定位,解碼某一個(gè)P或B幀時(shí),需要先解碼得到本GOP內(nèi)的I幀及之前的N個(gè)預(yù)測(cè)幀才可以,GOP值越長(zhǎng),需要解碼的預(yù)測(cè)幀就越多,seek響應(yīng)的時(shí)間也越長(zhǎng)。
x264最近的更新加入了兩個(gè)關(guān)于keyframe的參數(shù),–open-gop和–keyint infinite。
所謂open-gop,就是指允許類似IBBPBBIBB的frame結(jié)構(gòu),它的作用在于提高低keyint設(shè)置時(shí)的壓縮率。
–keyint infinite的作用和open-gop正好相反,它為了最大化的利用壓縮率存在的。開(kāi)啟infinite的open-gop后,除非scene-cut自動(dòng)判定需要加入keyframe,一般情況下都不會(huì)加入keyframe。
眾所周知,過(guò)于頻繁的keyframe存在會(huì)降低壓縮率,而一定頻率的keyframe又能保證GOP間的B和P幀擁有良好的預(yù)測(cè)精度。同時(shí)keyframe的存在又保證了編碼視頻的可seek能力這在某些應(yīng)用場(chǎng)合又是非常關(guān)鍵的。所以keyframe的選擇是一個(gè)因人而異,因應(yīng)用場(chǎng)合不同而變化的參數(shù)。
由于open-gop開(kāi)啟/keyint infinite與open-gop關(guān)閉/keyint infinite結(jié)果一模一樣,所以不在列出了。這也可以理解,因?yàn)閮烧邔?duì)keyframe的控制作用是完全相反的。
從上面的結(jié)果不難看出,開(kāi)啟open-gop保持現(xiàn)有keyint的設(shè)定,或者完全無(wú)視open-gop直 接上極端的無(wú)窮keyint interval,都能對(duì)最終編碼視頻的質(zhì)量有所提升(opengop的提升很有限)。至于兩者選何者好就要根據(jù)實(shí)際編碼視頻的質(zhì)量,以及可播放性來(lái)確定 了,如果是極端ep的終極質(zhì)量追求者,看片子幾乎不seek,那就選擇keyint infinite吧。
MPEG的格式支持open GOP或者close GOP格式。Close GOP是指幀間的預(yù)測(cè)都是在GOP中進(jìn)行的。而使用open GOP,后一個(gè)GOP會(huì)參考前一個(gè)GOP的信息。使用這種方式就大大降低了碼率。
from: http://www.videohelp.com
GOP: Group Of Pictures
A Group Of Pictures (GOP) consists of all the pictures that follow a GOP header before another GOP header.
The GOP layer allows random access because the first picture after the GOP header is an Intra picture that means that it doesn't need any reference to any other picture.
The GOP layer is optional, i.e. it's not mandatory to put any GOP header in the bitstream. In the header there is also the timecode of the first picture of the GOP to be displayed.
The decoding process, as the GOP header is immediately followed by an Intra picture, can begin at that point of the bitstream. Anyway it's possible that some B pictures, following such I_picture in the bitstream, have references coming from the previous GOP and can't be correctly decoded. In this case the GOP is called an Open GOP because some references from the previous GOP exist; if a random access to such a GOP is performed, some B_pictures shouldn't be displayed .
A GOP is called a Closed GOP when either there are no B_pictures immediately following the first I_picture or such B_pictures haven't any references coming from the previous GOP (in this case a GOP header flag must be set).
the GOP length is the period (often expressed in frames) by which an Intra frame occurs. It must be noticed that such a value cannot be found in the bitstream and it is unnecessary to the decoding process. Furthermore it isn't specified any fixed period for the Intra frame. As the presence of the Intra frames is quite important for many applications, it is the encoder that has to provide them, while the decoder has only to work with all the valid bitstreams.
例子:
- 非封閉GOP: I1 P2 B3 B4 P5 B6 B7
I8 B9 B10 P11 B11 B12 P13 B14 B15
I16 B16 B17 P18 B19 B20 P21 B22 B23
第一個(gè)GOP length為9-2幀,從第二個(gè)GOP開(kāi)始為9幀
I1 P2 B3 B4 P5 B6 B7
I8 P9 B10 B11 P12 B13 B14
I15 P16 B17 B18 P19 B20 B21
GOP length = 9-2;