Intel Media SDK H264 encoder GOP setting

1 I帧,P帧,B帧,IDR帧,NAL单元

I frame:帧内编码帧,又称intra picture,I 帧通常是每个 GOP(MPEG 所使用的一种视频压缩技术)的第一个帧,经过适度地压缩,做为随机访问的参考点,可以当成图象。I帧可以看成是一个图像经过压缩后的产物;

P frame: 前向预测编码帧,又称predictive-frame,通过充分将低于图像序列中前面已编码帧的时间冗余信息来压缩传输数据量的编码图像,也叫预测帧;

B frame: 双向预测内插编码帧,又称bi-directional interpolated prediction frame,既考虑与源图像序列前面已编码帧,也顾及源图像序列后面已编码帧之间的时间冗余信息来压缩传输数据量的编码图像,也叫双向预测帧;

IDR frame:I和IDR帧都是使用帧内预测的,在编码和解码中为了方便,要首个I帧和其他I帧区别开,把第一个I帧叫IDR,这样就方便控制编码和解码流程,所以IDR帧一定是I帧,但I帧不一定是IDR帧;IDR帧的作用是立刻刷新,使错误不致传播,从IDR帧开始,重新算一个新的序列开始编码。

NAL单元:全称Network Abstract Layer,即网络抽象层,在H.264/AVC视频编码标准中,整个系统框架被分为了两个层面:视频编码层面(VCL)和网络抽象层面(NAL)。其中,前者负责有效表示视频数据的内容,而后者则负责格式化数据并提供头信息,以保证数据适合各种信道和存储介质上的传输。因此我们平时的每帧数据就是一个NAL单元(SPS与PPS除外)。在实际的H264数据帧中,往往帧前面带有00 00 00 01 或 00 00 01分隔符,一般来说编码器编出的首帧数据为PPS与SPS,接着为I帧。

2 GOP(Group of pictures)

所谓GOP,意思是画面组,一个GOP就是一组连续的画面。GOP结构一般涉及两个数字,例如,M=3,N=12。第一个数字M指定I帧和P帧之间的距离,第二个数字N指定两个I帧之间的距离:及画面组的大小。对于上面的例子M=3,N=12,GOP结构表示为:IBBPBBPBBPBBI。在一个GOP内I frame的解码不依赖于任何的其它帧,而p frame的解码则依赖于其前面的I frame或者P frame,B frame的解码则依赖于其前的最近的一个I frame或者P frame 及其后的最近的一个P frame。

3 H264 encoder GOP setting

Intel Media SDK Encoding Sample 用法如下

sample_encode.exe h264 -i video.yuv -w 640 -h 480 -o out.mkv -hw -d3d -mkv -b 1000 -f 30

sample_encode.exe h264 -i video.yuv -w 640 -h 480 -o out.mp4 -hw -d3d -mux -b 1000 -f 30

编码参数包括:输入格式、帧速率、输出比特率、输入视频流宽高、输出视频流宽高等等。这里我们发现,比没有设置I、B、P帧信息的相关参数,也就是说Demo中并没有开发这样的参数设置。通过查看Intel Media SDK的Guide,发现Intel有提供这样的参数让我们可以调整I、B、P的构成,具体就是GopOptFlag:
GopOptFlag
Description
    The GopOptFlag enumerator itemizes special properties in the GOP (Group of Pictures) sequence.
Name/Description
    MFX_GOP_CLOSED
    B-frames of the first B-interval can never reference the previous GOP
    MFX_GOP_STRICT
    The encoder must strictly follow the given GOP structure as defined by parameter GopPicSize, 
    GopRefDist etc in the mfxVideoParam structure. Otherwise, the encoder can adapt the GOP structure
    for better efficiency, whose range is constrained by parameter GopPicSize and GopRefDist etc

而GopOptFlag所在的位置是:
mfxVideoParam -> mfxInfoMFX -> GopOptFlag
此外还需要利用到的相关参数可以参考mfxInfoMFX的说明,主要包括:

mfxU16 GopPicSize;
mfxU16 GopRefDist;
mfxU16 GopOptFlag;
mfxU16 IdrInterval;

GopPicSize
    Number of pictures within the current GOP (Group of Pictures); if GopPicSize=0, then the GOP size is unspecified.
    If GopPicSize=1, only I-frames are used. 
    当前GOP中画面的个数,若GopPicSize=0,则认为GOP尺寸未指定,若GopPicSize=1,则将只使用I帧

GopRefDist
    Distance between I- or P- key frames; if it is zero, the GOP structure is unspecified. Note: 
    If GopRefDist = 1, there are no B-frames used. 
    I或P关键帧之间的距离;若为零,则认为GOP结构未指定,若GopRefDist=1,则将不使用B帧

GopOptFlag
    ORs of the GopOptFlag enumerator indicate the additional flags for the GOP specification; 

IdrInterval
    the sequence header before every Nth I-frame. If IdrInterval=0(default), SDK inserts the sequence header once at the beginning of the stream
    对于H264,IdrInterval指定了IDR帧的间隔,单位为I帧;若IdrInterval=0,则每个I帧均为IDR帧。若IdrInterval=1,则每隔一个I帧为IDR帧,以此类推。对于
    MPEG2, IdrInterval定义了序列头间隔,单位为I帧,若IdrInterval=N,SDK将在每第N个I帧之前插入序列头;若IdrInterval=0(默认),SDK将在流开头一次
    性插入序列头。

举例说明下:

                                                                                                                 图 1

                                                                                                                  图 2

                                                                                                                                 图 3

以上图1和图2中红色表示I帧蓝色表示P帧绿色表示B帧,其中图2和图3是同一个H264文件,图3可体现IdrInterval = 1的作用,即两个Idr帧间隔一个I帧,所以也可以把IDR帧看做是SPS和PPS后面第一个I帧!

参考:

http://www.cnblogs.com/cslunatic/p/3565984.html

http://en.wikipedia.org/wiki/Group_of_pictures

http://blog.csdn.net/jtujtujtu/article/details/6565287

posted @ 2014-08-15 14:46  weiwei22844  阅读(2936)  评论(0编辑  收藏  举报