FFmpeg 获取H264流中的sps pps

来源:互联网 发布:php 采集微信文章内容 编辑:程序博客网 时间:2024/05/16 15:04

原文地址:http://blog.csdn.net/ren65432/article/details/43448781

FFmpeg 获取H264流中的sps pps

原创 2015年02月03日 13:45:15

H.264的SPS和PPS串,包含了初始化H.264解码器所需要的信息参数,包括编码所用的profile,level,图像的宽和高,deblock滤波器等。

(1)avcC的数据结构:

[cpp] view plain copy
  1. aligned(8) class AVCDecoderConfigurationRecord {     
  2.  unsigned int(8) configurationVersion = 1;     
  3.  unsigned int(8) AVCProfileIndication;     
  4.  unsigned int(8) profile_compatibility;     
  5.  unsigned int(8) AVCLevelIndication;     
  6.  bit(6) reserved = '111111'b;     
  7.  unsigned int(2) lengthSizeMinusOne;     
  8.  bit(3) reserved = '111'b;     
  9.  unsigned int(5) numOfSequenceParameterSets;     
  10. for (i=0; i< numOfSequenceParameterSetsispan>    
  11.  unsigned int(16) sequenceParameterSetLength ;     
  12.  bit(8*sequenceParameterSetLength) sequenceParameterSetNALUnit;     
  13.  }     
  14.  unsigned int(8) numOfPictureParameterSets;     
  15. for (i=0; i< numOfPictureParameterSetsispan>    
  16.  unsigned int(16) pictureParameterSetLength;     
  17.  bit(8*pictureParameterSetLength) pictureParameterSetNALUnit;     
  18.  }     
  19. }     

avcC的数据结构对应sps和pps流。


(2) FFmpeg 如何获取sps和pps

ffmpeg获取sps和pps非常简单。avcC数据结构对应于AVFormatContext->streams[H264Index]->codec->extradata。

代码如下:

[cpp] view plain copy
  1. if ((ret = avformat_open_input(&ic, InputFileName, NULL, NULL)) < 0)  
  2.     {  
  3.         xprintf->Trace(0,"******** Decode avformat_open_input() Function result=%d",ret);  
  4.         return ret;  
  5.     }  
  6.   
  7.     if ((ret = avformat_find_stream_info(ic, NULL)) < 0)   
  8.     {    
  9.         xprintf->Trace(0,"******** Decode avformat_find_stream_info() Function result=%d ",ret);  
  10.         avformat_close_input(&ic);    
  11.         return ret;    
  12.     }    
  13.   
  14.     for (int i=0;i<ic->streams[0]->codec->extradata_size;i++)  
  15.     {  
  16.         printf("%x ",ic->streams[0]->codec->extradata[i]);  
  17.     }  


对应上面的avcC结构体我们知道:

第7,8位, 为sps长度(0,18)即为24。

接下来的24位为sps数据,(67,64,0,20,ac,b2,0,a0,b,76,2,20,0,0,3,0,20,0,0,c,81,e3,6,49)。

[cpp] view plain copy
  1. numOfPictureParameterSets, 为1。  
接下来2位为pps长度:(0,6) 即为6。

接下来6位为pps数据,(68,eb,c3,cb,22,c0)。


原创粉丝点击