1.AVFormatContext(IO格式上下文,用于读写)
avformat_alloc_context 分配一个结构AVFormatContext。
avformat_alloc_context调用avformat_get_context_defaults来初始化AVFormatContext。
avformat_get_context_defaults 填充了 av_class,io_open,io_close字段。最后调用 av_opt_set_defaults来填充option。
static const AVClass av_format_context_class= {
.class_name = "AVFormatContext",
.item_name = format_to_name,
.option = avformat_options,
.version = LIBAVUTIL_VERSION_INT,
.child_next = format_child_next,
.child_class_next = format_child_class_next,
.category = AV_CLASS_CATEGORY_MUXER,
.get_category = get_category,
}
avformat_options的定义在libavformat/options_table.h中。
static void avformat_get_context_defaults(AVFormatContext *s)
{
memset(s, 0, sizeof(AVFormatContext));
s->av_class = &av_format_context_class;
s->io_open = io_open_default;
s->io_close = io_close_default;
av_opt_set_defaults(s);
}
v_opt_set_defaults。调用了av_opt_set_defaults2;
av_opt_set_defaults2调用av_opt_next来进行分析,如果存在,这返回option。(AVOption可以包含名称,简短的帮助信息,取值等等)
const AVOption *av_opt_next(const void *obj, const AVOption *last)
{
const AVClass *class;
if (!obj)
return NULL;
class = *(const AVClass**)obj;(obj指向对象的字段)
if (!last && class && class->option && class->option[0].name)
return class->option;
if (last && last[1].name)
return ++last;
return NULL;
}
2. 打开文件函数 avformat_open_input:模块 libavformat/utils.c
声明:int avformat_open_input(AVFormatContext **ps, const char *filename,AVInputFormat *fmt, AVDictionary **options)
如果按照默认调用
avformat_open_input调用 init_input ,init_input会调用AVFormatContext的io_open。
如果options!=NULL
调用av_opt_set_dict,会遍历*options,然后调用av_opt_set来设置*ps的av_class的option字段。
av_opt_set_dict遍历AVDictionary的每个值,av_opt_set获取对应的option,来设置。