如何用 SPS 和 PPS 数据填充 AVCodecContext 的 'extradata' 字段?

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

问题来了:当用ffmpeg解码H264码流时,我可以得到SPS和PPS的原始数据,但我不知道如何将它们填充到[=的extradata字段中14=]。没有 extradata,我无法正确解码帧。每次调用 avcodec_decodec_video2 时,return 值为正,但 got_picture 标志始终为 zero

我正在处理的流如下所示:

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

您提到的数据是一个字节流,其中包含用于 SPS 和 PPS 的 NAL 单元。 extradata 反过来需要一个指向 AVC 解码器配置记录的指针,这是您具有额外格式的数据。

有关详细信息,请参阅 MPEG-4 第 15 部分 "Advanced Video Coding (AVC) file format" 第 5.2.4.1 节。

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