FFMPeg代码分析:AVFormatContext结构体

来源:互联网 发布:mblock编程语言 编辑:程序博客网 时间:2024/05/18 06:27

转自:http://blog.csdn.net/shaqoneal/article/details/16879951

从先前的demo中可以看到,进入main函数所定义的第一个变量就是AVFormatContext的指针:

[cpp] view plaincopy
  1. int main(int argc, char *argv[])  
  2. {  
  3.     AVFormatContext *pFormatCtx = NULL;    
  4.         ....  
  5. }  
而且,往下看就会知道这个结构体将贯穿函数始终,avformat_open_input、av_find_stream_info、av_read_frame、av_close_input_file都需要这个结构作为参数。该结构在FFMPEG中的作用是解封装,即根据视频文件容器的后缀名(.mp4, .avi, .flv等)分析其中编码过的音频和视频流的保存方式并将其提取以供解码器进行解码。该结构的部分定义如下:
[cpp] view plaincopy
  1. typedef struct AVFormatContext  
  2. {      
  3.     const AVClass *av_class;  
  4.     struct AVInputFormat *iformat;  
  5.     struct AVOutputFormat *oformat;      
  6.     void *priv_data;  
  7.     AVIOContext *pb;  
  8.     int ctx_flags;  
  9.     unsigned int nb_streams;  
  10.     AVStream **streams;  
  11.     char filename[1024];  
  12.     int64_t start_time;  
  13.     int64_t duration;  
  14.     int bit_rate;  
  15.     unsigned int packet_size;  
  16.     int max_delay;  
  17.     int flags;  
  18. #define AVFMT_FLAG_GENPTS       0x0001 ///< Generate missing pts even if it requires parsing future frames.  
  19. #define AVFMT_FLAG_IGNIDX       0x0002 ///< Ignore index.  
  20. #define AVFMT_FLAG_NONBLOCK     0x0004 ///< Do not block when reading packets from input.  
  21. #define AVFMT_FLAG_IGNDTS       0x0008 ///< Ignore DTS on frames that contain both DTS & PTS  
  22. #define AVFMT_FLAG_NOFILLIN     0x0010 ///< Do not infer any values from other values, just return what is stored in the container  
  23. #define AVFMT_FLAG_NOPARSE      0x0020 ///< Do not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the fillin code works on frames and no parsing -> no frames. Also seeking to frames can not work if parsing to find frame boundaries has been disabled  
  24. #define AVFMT_FLAG_NOBUFFER     0x0040 ///< Do not buffer frames when possible  
  25. #define AVFMT_FLAG_CUSTOM_IO    0x0080 ///< The caller has supplied a custom AVIOContext, don't avio_close() it.  
  26. #define AVFMT_FLAG_DISCARD_CORRUPT  0x0100 ///< Discard frames marked corrupted  
  27. #define AVFMT_FLAG_FLUSH_PACKETS    0x0200 ///< Flush the AVIOContext every packet.  
  28. #define AVFMT_FLAG_MP4A_LATM    0x8000 ///< Enable RTP MP4A-LATM payload  
  29. #define AVFMT_FLAG_SORT_DTS    0x10000 ///< try to interleave outputted packets by dts (using this flag can slow demuxing down)  
  30. #define AVFMT_FLAG_PRIV_OPT    0x20000 ///< Enable use of private options by delaying codec open (this could be made default once all code is converted)  
  31. #define AVFMT_FLAG_KEEP_SIDE_DATA 0x40000 ///< Don't merge side data but keep it separate.  
  32.   
  33.       
  34.     unsigned int probesize;  
  35.   
  36.     int max_analyze_duration;  
  37.   
  38.     const uint8_t *key;  
  39.     int keylen;  
  40.   
  41.     unsigned int nb_programs;  
  42.     AVProgram **programs;  
  43.       
  44.     enum AVCodecID video_codec_id;  
  45.       
  46.     enum AVCodecID audio_codec_id;  
  47.   
  48.     enum AVCodecID subtitle_codec_id;  
  49.   
  50.     unsigned int max_index_size;  
  51.      
  52.     unsigned int max_picture_buffer;  
  53.      
  54.     unsigned int nb_chapters;  
  55.     AVChapter **chapters;  
  56.   
  57.     AVDictionary *metadata;  
  58.   
  59.     int64_t start_time_realtime;  
  60.   
  61.     int fps_probe_size;  
  62.    
  63.     int error_recognition;  
  64.    
  65.     AVIOInterruptCB interrupt_callback;  
  66.   
  67.     int debug;  
  68. #define FF_FDEBUG_TS        0x0001  
  69.   
  70.     int ts_id;  
  71.   
  72.     int audio_preload;  
  73.   
  74.     int max_chunk_duration;  
  75.    
  76.     int max_chunk_size;  
  77.   
  78.     int use_wallclock_as_timestamps;  
  79.   
  80.     int avoid_negative_ts;  
  81.   
  82.     int avio_flags;  
  83.   
  84.     enum AVDurationEstimationMethod duration_estimation_method;  
  85.   
  86.     unsigned int skip_initial_bytes;  
  87.   
  88.     unsigned int correct_ts_overflow;  
  89.   
  90.     int seek2any;  
  91.   
  92.     int flush_packets;  
  93.   
  94.     int probe_score;  
  95. } AVFormatContext;  
该定义中部分成员变量的作用,其中与解码相关的由红色标出:
const AVClass *av_class:用于记录日志和设置AVOption的类,由avformat_alloc_context()设置。AVClass类中保存了包含该成员的类名、对象名和AVOption实例等信息;

AVInputFormat *iformat / AVOutputFormat *oformat:输入/输出格式,或为输入或为输出,这两个不能同时存在;分别用于编码和解码。AVOutputFormat类保存了文件名、扩展名、默认音视频和字幕解码器等信息;

void *priv_data:私有数据,只有iformat/oformat.priv_class非0时有效;

AVIOContext *pb:指向IO数据的缓存的指针;

int ctx_flags:媒体流信息;

unsigned int nb_streams:包含的媒体流的数量

AVStream **streams:指向实际输入/输出数据的指针

char filename[1024]:输入或输出的文件名

int64_t start_time / int64_t duration:媒体的开始时间和持续时长,一般由AVStream **streams导出;

int bit_rate:视频码率,由ffmpeg自动计算;

unsigned int packet_size:包尺寸;

int max_delay:最大延迟;

unsigned int probesize:在解码时用于探测的数据的大小,编码时不使用;

int max_analyze_duration:允许输入数据在avformat_find_stream_info()中分析的最长时间;

const uint8_t *key:关键字;

int keylen:关键字长;

unsigned int nb_programs:包含节目的数量;

AVProgram **programs:指向实际节目的指针;

enum AVCodecID video_codec_id:指定的视频解码器ID;由用户指定;

enum AVCodecID audio_codec_id:指定的音频解码器ID;由用户指定;

enum AVCodecID subtitle_codec_id:指定的字幕解码器ID;由用户指定;

unsigned int max_index_size:用于检索码流的索引值的最大字长;

unsigned int max_picture_buffer:图像缓存的最大尺寸;

unsigned int nb_chapters:章节数目;

AVChapter **chapters:指向章节数据的指针;

AVDictionary *metadata:元数据;

int64_t start_time_realtime;:码流开始的实际时间,以毫秒为单位;

int fps_probe_size:表明使用了多少帧用于格式检测;

int error_recognition:错误检测的阈值;

AVIOInterruptCB interrupt_callback:在IO层用户自定义的回调结构;

int ts_id:传输流的ID;

unsigned int skip_initial_bytes:打开媒体流时跳过的字节数;

int seek2any:是否可以移动到任意一帧的位置;

int flush_packets:每处理一个packet之后刷新io上下文;

int probe_score:格式探测的分值,上限为AVPROBE_SCORE_MAX;


指向元数据的结构体AVDictionary由两部分组成:统计计数和指向AVDictionaryEntry的指针,也就是说AVDictionary可以说是AVDictionaryEntry的封装。

[cpp] view plaincopy
  1. struct AVDictionary   
  2. {  
  3.     int count;  
  4.     AVDictionaryEntry *elems;  
  5. };  
  6. typedef struct AVDictionaryEntry  
  7.  {  
  8.     char *key;  
  9.     char *value;  
  10. } AVDictionaryEntry;  

了解了AVFormatContext的构造之后,可以再来看看它是如何被初始化的了。在函数avformat_open_input中,AVFormatContext的初始化由avformat_alloc_context()及其调用的函数实现:

[cpp] view plaincopy
  1. AVFormatContext *avformat_alloc_context(void)  
  2. {  
  3.     AVFormatContext *ic;  
  4.     ic = av_malloc(sizeof(AVFormatContext));  
  5.     if (!ic) return ic;  
  6.     avformat_get_context_defaults(ic);  
  7.     return ic;  
  8. }  
  9. static void avformat_get_context_defaults(AVFormatContext *s)  
  10. {  
  11.     memset(s, 0, sizeof(AVFormatContext));  
  12.   
  13.     s->av_class = &av_format_context_class;  
  14.   
  15.     av_opt_set_defaults(s);  
  16. }  
  17. void av_opt_set_defaults(void *s)  
  18. {  
  19. #if FF_API_OLD_AVOPTIONS  
  20.     av_opt_set_defaults2(s, 0, 0);  
  21. }  
  22. void av_opt_set_defaults2(void *s, int mask, int flags)  
  23. {  
  24. #endif  
  25.     const AVClass *class = *(AVClass **)s;  
  26.     const AVOption *opt = NULL;  
  27.     while ((opt = av_opt_next(s, opt)) != NULL) {  
  28. #if FF_API_OLD_AVOPTIONS  
  29.         if ((opt->flags & mask) != flags)  
  30.             continue;  
  31. #endif  
  32.         switch (opt->type) {  
  33.             case AV_OPT_TYPE_CONST:  
  34.                 /* Nothing to be done here */  
  35.             break;  
  36.             case AV_OPT_TYPE_FLAGS:  
  37.             case AV_OPT_TYPE_INT:  
  38.             case AV_OPT_TYPE_INT64:  
  39.             case AV_OPT_TYPE_DURATION:  
  40.             case AV_OPT_TYPE_CHANNEL_LAYOUT:  
  41.                 av_opt_set_int(s, opt->name, opt->default_val.i64, 0);  
  42.             break;  
  43.             case AV_OPT_TYPE_DOUBLE:  
  44.             case AV_OPT_TYPE_FLOAT: {  
  45.                 double val;  
  46.                 val = opt->default_val.dbl;  
  47.                 av_opt_set_double(s, opt->name, val, 0);  
  48.             }  
  49.             break;  
  50.             case AV_OPT_TYPE_RATIONAL: {  
  51.                 AVRational val;  
  52.                 val = av_d2q(opt->default_val.dbl, INT_MAX);  
  53.                 av_opt_set_q(s, opt->name, val, 0);  
  54.             }  
  55.             break;  
  56.             case AV_OPT_TYPE_COLOR:  
  57.             case AV_OPT_TYPE_STRING:  
  58.             case AV_OPT_TYPE_IMAGE_SIZE:  
  59.             case AV_OPT_TYPE_VIDEO_RATE:  
  60.                 av_opt_set(s, opt->name, opt->default_val.str, 0);  
  61.                 break;  
  62.             case AV_OPT_TYPE_PIXEL_FMT:  
  63. #if LIBAVUTIL_VERSION_MAJOR < 53  
  64.                 if (class->version && class->version < AV_VERSION_INT(52, 10, 100))  
  65.                     av_opt_set(s, opt->name, opt->default_val.str, 0);  
  66.                 else  
  67. #endif  
  68.                     av_opt_set_pixel_fmt(s, opt->name, opt->default_val.i64, 0);  
  69.                 break;  
  70.             case AV_OPT_TYPE_SAMPLE_FMT:  
  71. #if LIBAVUTIL_VERSION_MAJOR < 53  
  72.                 if (class->version && class->version < AV_VERSION_INT(52, 10, 100))  
  73.                     av_opt_set(s, opt->name, opt->default_val.str, 0);  
  74.                 else  
  75. #endif  
  76.                     av_opt_set_sample_fmt(s, opt->name, opt->default_val.i64, 0);  
  77.                 break;  
  78.             case AV_OPT_TYPE_BINARY:  
  79.                 /* Cannot set default for binary */  
  80.             break;  
  81.             default:  
  82.                 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);  
  83.         }  
  84.     }  
  85. }  

可以看到,在av_opt_set_defaults2()函数中,首先会调用av_opt_next(s, opt),并根据opt->type的不同值来进行下一步的设置。av_opt_next()的代码:
[cpp] view plaincopy
  1. const AVOption *av_opt_next(void *obj, const AVOption *last)  
  2. {  
  3.     AVClass *class = *(AVClass**)obj;  
  4.     if (!last && class && class->option && class->option[0].name)  
  5.         return class->option;  
  6.     if (last && last[1].name)  
  7.         return ++last;  
  8.     return NULL;  
  9. }  
由于初始化时opt=NULL,因此该函数返回AVFormatContext::AVClass::option,并在下一次调用的时候作为参数传入。在options_table.h中可以看出该函数将avformat_options[]表中的设置数据逐条地返回给调用者,并通过其type进行下一步操作。以av_opt_set_int()为例:
[cpp] view plaincopy
  1. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)  
  2. {  
  3.     return set_number(obj, name, 1, 1, val, search_flags);  
  4. }  
  5. static int set_number(void *obj, const char *name, double num, int den, int64_t intnum, int search_flags)  
  6. {  
  7.     void *dst, *target_obj;  
  8.     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);  
  9.   
  10.     if (!o || !target_obj)  
  11.         return AVERROR_OPTION_NOT_FOUND;  
  12.   
  13.     dst = ((uint8_t*)target_obj) + o->offset;  
  14.     return write_number(obj, o, dst, num, den, intnum);  
  15. }  
由此我们知道了,AVFormatContext结构体中AVOption的元素通过这些函数,由options_table.h中的预定义表生成。
0 0
原创粉丝点击