天天看点

H264 Format 方式

一、概况介绍

h264有两种封装,

一种是annexb模式(Byte stream format),传统模式,有startcode,SPS和PPS是在ES中

一种是mp4模式(ISO Base Media File Format),一般mp4、mkv、flv会有,没有startcode,SPS和PPS以及其它信息被封装在container中,每一个frame前面是这个frame的长度

H264 Format 方式

    lengthSizeMinusOne indicates the length in bytes of the NALUnitLength field in an AVC video

sample or AVC parameter set sample of the associated stream minus one.

很多解码器只支持annexb这种模式,因此需要将mp4做转换:

在ffmpeg中用h264_mp4toannexb_filter可以做转换

实现:

注册filter

avcbsfc = av_bitstream_filter_init("h264_mp4toannexb");

转换bitstream

av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,

                               AVCodecContext *avctx, const char *args,

                     uint8_t **poutbuf, int *poutbuf_size,

                     const uint8_t *buf, int buf_size, int keyframe)

二、ISO_14496-15 : ISO Base Media File Format 1、使用“avc1”标记来表示使用这种format格式;

2、Format定义 这种封装格式有两种ES类型: video ES:包含所有的slice data 和 SEI 和 acess unit等 nal; Parameter set ES:包含 pps和 sps nal;

AVC可以使用以上一种或者两种类型: Video elementary stream only:sps、pps放在mp4的track描述 box中,AVC sample中不包含 sps和pps信息;

H264 Format 方式

Video elementary stream and parameter set elementary stream: In this case, sequence and   picture parameter set NAL units shall be transmitted only in the parameter set elementary stream and  shall neither be present in the sample descriptions nor the AVC samples of the video elementary  stream.

H264 Format 方式

3、video sample的format结构:

video sample使用大小描述frame:

H264 Format 方式

每个nal unit这样描述: Length: 代表后面nal unit的字节长度。length可以使用1、2、4字节。长度包含 一个nal的header,以及后面的rbsp payload长度。 nal unit:包含一个字节的nal的header,还有后面的rbsp。

4、AVC decoder configuration record

    这个结构体包含 描述 sample 长度的“字节数目”,还有parameter sets。 aligned(8) class AVCDecoderConfigurationRecord {     unsigned int(8) configurationVersion = 1;     unsigned int(8) AVCProfileIndication;     unsigned int(8) profile_compatibility;     unsigned int(8) AVCLevelIndication;     bit(6) reserved = ‘111111’b;     unsigned int(2) lengthSizeMinusOne;     bit(3) reserved = ‘111’b;     unsigned int(5) numOfSequenceParameterSets;     for (i=0; i< numOfSequenceParameterSets; i++) {         unsigned int(16) sequenceParameterSetLength ;         bit(8*sequenceParameterSetLength) sequenceParameterSetNALUnit;     }         unsigned int(8) numOfPictureParameterSets;     for (i=0; i< numOfPictureParameterSets; i++) {         unsigned int(16) pictureParameterSetLength;         bit(8*pictureParameterSetLength) pictureParameterSetNALUnit;     }     } lengthSizeMinusOne:indicates the length in bytes of the NALUnitLength field in an AVC video   sample or AVC parameter set sample of the associated stream minus one.

三、Annex B  Byte stream format      字节流格式,这个是绝大部分编码器的默认输出格式,就是每个帧的开头的3~4个字节是H264的start_code,0x00000001或者0x000001,即NALU数据+开始前缀(00000001或000001)

      一共有两种起始码:3字节的0x000001和4字节的0x00000001

       3字节的0x000001只有一种场合下使用,就是一个完整的帧被编为多个slice的时候,包含这些slice的nalu使用3字节起始码。其余场合都是4字节的。

继续阅读