今天看一下ffmpeg里面avformat_open_input这个函数,我个人认为这个函数算是在ffmpeg中最重要的函数了,因为其实在执行了这个函数之后,基本上所有的文件信息都就出来了。比如这一段流里面有几股流,每一股流都是什么信息基本都可以看到了。尤其当你是从内存里面读取数据的时候,这个函数就更加的重要了。因为这个函数里面有好几重嵌套,所以我在函数上有一些有加了注释,我会重点讲讲重要的函数。PS:当前我使用的ffmpeg的版本是3.4.7,另外我是以从内存读取数据来重点看的,而不是读取文件来看。
int avformat_open_input(AVFormatContext **ps, const char *filename,
AVInputFormat *fmt, AVDictionary **options)
{
AVFormatContext *s = *ps;
int i, ret = 0;
//不知道什么作用的变量
AVDictionary *tmp = NULL;
ID3v2ExtraMeta *id3v2_extra_meta = NULL;
//检查变量
if (!s && !(s = avformat_alloc_context()))
return AVERROR(ENOMEM);
if (!s->av_class) {
av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
return AVERROR(EINVAL);
}
//判断是不是输入的格式已经有了 如果有了,直接写上,后续就不用再查找了。这里书输入数据的一些信息
if (fmt)
s->iformat = fmt;
//暂时不知道作用,但是这块可以先跳过
if (options)
av_dict_copy(&tmp, *options, 0);
//判断是否又输入数据的变量
if (s->pb) // must be before any goto fail
s->flags |= AVFMT_FLAG_CUSTOM_IO;
//设置所有的选项到一个对象中。暂时不知道作用,但是这块可以先跳过
if ((ret = av_opt_set_dict(s, &tmp)) < 0)
goto fail;
//把文件名称写入
av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
//非常重要的函数,获取了很多信息,需要进一步分析
if ((ret = init_input(s, filename, &tmp)) < 0)
goto fail;
//得到了预测到的的格式的分数。内容主要还是再init_input中
s->probe_score = ret;
//拷贝允许的协议
if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
if (!s->protocol_whitelist) {
ret = AVERROR(ENOMEM);
goto fail;
}
}
//拷贝不允许的协议
if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) {
s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist);
if (!s->protocol_blacklist) {
ret = AVERROR(ENOMEM);
goto fail;
}
}
//这个就是使用刚刚在判断s->iformat = fmt;这个设置的时候可以用
if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
ret = AVERROR(EINVAL);
goto fail;
}
//暂时不知道作用,但是这块可以先跳过
avio_skip(s->pb, s->skip_initial_bytes);
/*检查文件名以防需要图像编号。*/
/* Check filename in case an image number is expected. */
if (s->iformat->flags & AVFMT_NEEDNUMBER) {
if (!av_filename_number_test(filename)) {
ret = AVERROR(EINVAL);
goto fail;
}
}
s->duration = s->start_time = AV_NOPTS_VALUE;
//私有数据处理
/* Allocate private data. */
if (s->iformat->priv_data_size > 0) {
if (!(s->priv_data = av_mallocz(s->iforma

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



