天天看點

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位元組的。

繼續閱讀