ffmpeg源代码分析之avformat_open_input

运行avformat_open_input函数是ffmpeg初始化之后做的第一件事。从大面意思上来讲,函数作用是打开输入流,但是流具体是如何打开的,具体做了哪些事情,我们慢慢来分析研究下:

<pre name="code" class="cpp">int avformat_open_input(AVFormatContext **ps, const char *filename,
                        AVInputFormat *fmt, AVDictionary **options)
{
    AVFormatContext *s = *ps;
    int ret = 0;
    AVDictionary *tmp = NULL;
    ID3v2ExtraMeta *id3v2_extra_meta = NULL;//保存MP3的一些信息,比如作者,风格,年代什么的

    //如果s没有初始化,则创建一个
    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);
    }
    //如果指定了fmt,直接进行赋值
    if (fmt)
        s->iformat = fmt;

    if (options)
        av_dict_copy(&tmp, *options, 0);

    if ((ret = av_opt_set_dict(s, &tmp)) < 0)//没有设置option参数,该步可以跳过
        goto fail;
   
    //探测输入流的格式是此步实现的。
    if ((ret = init_input(s, filename, &tmp)) < 0)
        goto fail;
.....
}

 

/* Open input file and probe the format if necessary. */
static int init_input(AVFormatContext *s, const char *filename,
                      AVDictionary **options)
{
    int ret;
    AVProbeData pd = { filename, NULL, 0 };//使用AVProbeData进行探测,里面的参数是文件名
    int score = AVPROBE_SCORE_RETRY;

    //调用者已经指定了Pb(一般不会这么做),除非自己去实现让ffmpeg去内存中获取数据
    if (s->pb) {                                          //AVFormatContext中的AVIOContext *pb是否已经初始化,里面保存着对底层数据的读写操作。
        s->flags |= AVFMT_FLAG_CUSTOM_IO;                 //如果我们自己指定了pb,会执行到这,此处代表自己的IO
        if (!s->iformat)                                  //如果iformat没有指定,需要进行iformat探测和文件格式探测
            //指定了pb但是没指定iformat,以pb读取媒体数据进行探测     
            return av_probe_input_buffer2(s->pb, &s->iformat, filename, s, 0, s->format_probesize);
        else if (s->iformat->flags & AVFMT_NOFILE)
            av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
                                      "will be ignored with AVFMT_NOFILE format.\n");
        return 0;
    }

    /*常规来讲会执行到这儿,首先如果指定了iformat,并且不需要文件,直接就返回
    *如果没有指定iformat,就根据文件后缀猜测iformat
    */
    if ((s->iformat && s->iformat->f
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值