从学龄前开始解读FFMPEG代码 之 avformat_open_input函数 一

在avformat_open_input()开始学龄前学习想说的话

这一函数的内容整体下来是非常的长的,所以需要捋清楚整个学习流程的过程也需要花费一点功夫。这也是整个学龄前学习过程中碰到的第一个较为复杂的函数。可能需要把整个函数拆开为多篇文章来进行说明。

avformat_open_input()的定义

首先来看一下这个函数的定义和实现吧。它的定义在libavformat/avformat.h文件中,内容如下:

/**
 * Open an input stream and read the header. The codecs are not opened.
 * The stream must be closed with avformat_close_input().
 *
 * @param ps Pointer to user-supplied AVFormatContext (allocated by avformat_alloc_context).
 *           May be a pointer to NULL, in which case an AVFormatContext is allocated by this
 *           function and written into ps.
 *           Note that a user-supplied AVFormatContext will be freed on failure.
 * @param url URL of the stream to open.
 * @param fmt If non-NULL, this parameter forces a specific input format.
 *            Otherwise the format is autodetected.
 * @param options  A dictionary filled with AVFormatContext and demuxer-private options.
 *                 On return this parameter will be destroyed and replaced with a dict containing
 *                 options that were not found. May be NULL.
 *
 * @return 0 on success, a negative AVERROR on failure.
 *
 * @note If you want to use custom IO, preallocate the format context and set its pb field.
 */
int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options);

在定义注释中,提到了,该函数是用于打开一个码流并阅读码流header的。在调用这个函数的时候,编解码码器codec并没有打开,打开的只是文件。在关闭时则必须使用avformat_close_input(),也就是说打开函数和关闭函数几乎总是成对出现的。

open函数拥有四个参数,第一个参数是一个AVFormatContext指针,允许传入空指针,在传入空指针时该AVFormatContext指针则是由这个open函数来分配空间并进行写入。

第二个参数是需要打开的视频码流的url,该url在说明AVFormatContext中曾经提到的成员变量一样,用的词是url而不是filename,因为可以使用该url来打开网络视频流或者普通文件都是可以的。

第三个参数是一个AVInputFormat指针,可以为空(在打开视频流的时候基本都是设置为空,所以在注释中特别说明的是不为空的情况),在不为空时该参数则是强制指定使用的输入格式,一般情况下都可以让函数自己去进行检测判断就行,所以一般情况设置为空。

第四个参数则是一个AVDictionary,存储了一些(也可以啥都不存)AVOption用于设置打开文件并解封装时候的特殊设置要求。函数完成工作并返回时,该参数会被改变销毁,返回没有找到的option选项。所以在使用open函数打开视频码流时,可以在执行完成之后,对传参的AVDictionary进行一次检查,如果不是空,那么代表传入的有些option没有找到,没有完成参数的传递。

avformat_open_input()的实现

该函数的具体实现代码则是在libavformat/utils.c中,下面贴一下具体实现。(在源码的基础上加了注释分隔线,以方便学龄前的学习理解

int avformat_open_input(AVFormatContext **ps, const char *filename,
                        ff_const59 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;

    if (</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值