How to fill 'extradata' field of AVCodecContext with SPS and PPS data?

此生再无相见时 提交于 2019-12-09 13:20:55

问题


Here is the problem: When decoding H264 stream with ffmpeg, I can obtain raw data of SPS and PPS but I have no idea how to fill them into the extradata field of AVCodecContext. Without extradata, I can't decode frames properly. Every time I call avcodec_decodec_video2, the return value is positive but the got_picture flag is always zero. Can anyone kindly help me with this issue?

The stream I am dealing with looks like this:

[0x67]...[0x68]...[0x61]...[0x61]...  .......  [0x61]...[0x67]...[0x68]...  ......

Thanks in advance!


回答1:


The data you mentioned is a byte stream holding NAL units for SPS and PPS. extradata in turn expects a pointer to AVC decoder configuration record, which is the data you have with extra formatting.

See MPEG-4 Part 15 "Advanced Video Coding (AVC) file format" section 5.2.4.1 for details.

5.2.4.1.1 Syntax 

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



回答2:


c_v->extradata = (uint8_t*)av_malloc(flvBufSize + FF_INPUT_BUFFER_PADDING_SIZE);
                                    if(!c_v->extradata){
                                        loge("Could not av_malloc extradata");
                                    }
                                    logv("extradata_size =%d", c_v->extradata_size);
                                    c_v->extradata_size = flvBufSize;
                                    memcpy(c_v->extradata, avpkt.data, avpkt.size);

                                    if (avcodec_open2(c_v, codec_v, NULL) < 0) {
                                        loge("could not open video codec");                             
                                    }


来源:https://stackoverflow.com/questions/29790334/how-to-fill-extradata-field-of-avcodeccontext-with-sps-and-pps-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!