ffmpeg中AVCodecContext等数据结构中extradata成员的数据格式及其设置

1、引用AVCodecContext中对该数据成员的解释

/**
     * some codecs need / can use extradata like Huffman tables.
     * MJPEG: Huffman tables
     * rv10: additional flags
     * MPEG-4: global headers (they can be in the bitstream or here)
     * The allocated memory should be AV_INPUT_BUFFER_PADDING_SIZE bytes larger
     * than extradata_size to avoid problems if it is read with the bitstream reader.
     * The bytewise contents of extradata must not depend on the architecture or CPU endianness.
     * - encoding: Set/allocated/freed by libavcodec.
     * - decoding: Set/allocated/freed by user.
     */
    uint8_t *extradata;
    int extradata_size;

可见它针对不同的情况有不同的格式,而比较常用的情况就是我们对视频流进行写入文件操作时(某些情况下,如通过NV12格式编码的视频流数据),或则解码视频文件时需要我们去设置。此时extradata作为一个global headers,主要保存SPS、PPS等信息,下面就针对此种情况进行说明。


2、格式规定

MPEG-4 Part 15 "Advanced Video Coding (AVC) file format" section 5.2.4.1 的规定如下:

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; 
 } 
}


引用一个MP4文件中包含sps、pps数据的数据段

其中有一个sps块(e1 & 1F),长度为26字节(0x00 1a),后面就是这个序列参数集的真正内容;

一个pps块(01  & FF),长度为4字节(0x04),后面就是这个图像参数集的真正类容

如何提取提取数据参见:http://blog.csdn.net/a812073479/article/details/74716476


3、extradata的设置

      (1)直接设置原始的PPS、SPS数据

               数据格式如下(黄色部分为SPS数据、红色部分为PPS数据)


               将这部分数直接送给extradata的前面即可,后面填充AV_INPUT_BUFFER_PADDING_SIZE 个字节的0数据

      (2)某些情况下需要按照文档中定义的那样来设置

              参考链接:https://stackoverflow.com/questions/15263458/h-264-muxed-to-mp4-using-libavformat-not-playing-back

                                  https://devtalk.nvidia.com/default/topic/718718/-howto-h-264-mp4-container/?offset=1

猜你喜欢

转载自blog.csdn.net/a812073479/article/details/74721119