• ffmpeg源码阅读之avformat_alloc_output_context2


    整体结构流程

    请添加图片描述

    核心逻辑

    通过读源码发现核心的处理逻辑是av_guess_format函数,这里就根据核心逻辑来阅读,其余的基本是是在做判断和赋值

    av_guess_format阅读分析

    步骤1(先看头文件)
    /**
     * Return the output format in the list of registered output formats
     * which best matches the provided parameters, or return NULL if
     * there is no match.
     *
     * @param short_name if non-NULL checks if short_name matches with the
     *                   names of the registered formats
     * @param filename   if non-NULL checks if filename terminates with the
     *                   extensions of the registered formats
     * @param mime_type  if non-NULL checks if mime_type matches with the
     *                   MIME type of the registered formats
     */
    const AVOutputFormat *av_guess_format(const char *short_name,const char *filename,const char *mime_type);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    说明

    1.根据注释信息,此函数返回AVOutputFormat的指针,通过注册的 ”output formats“列表进行匹配,没有匹配上就返回NULL
    2.思考:这里说的"注册列表"指的是啥?(下面做解释)

    步骤2(看函数定义)
    const AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
                                          const char *mime_type)
    {
        const AVOutputFormat *fmt = NULL;
        const AVOutputFormat *fmt_found = NULL;
        void *i = 0;
        int score_max, score;
    
        /* specific test for image sequences */
    #if CONFIG_IMAGE2_MUXER
        if (!short_name && filename &&
            av_filename_number_test(filename) &&
            ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
            return av_guess_format("image2", NULL, NULL);
        }
    #endif
        /* Find the proper file type. */
        score_max = 0;
        while ((fmt = av_muxer_iterate(&i))) {
            score = 0;
            if (fmt->name && short_name && av_match_name(short_name, fmt->name))
                score += 100;
            if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
                score += 10;
            if (filename && fmt->extensions &&
                av_match_ext(filename, fmt->extensions)) {
                score += 5;
            }
            if (score > score_max) {
                score_max = score;
                fmt_found = fmt;
            }
        }
        return fmt_found;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    从此函数中可以发现干活的核心处理函数是 “av_muxer_iterate”;从这个我们就可以大概猜测,此函数做了两件事情:

    1. 取出”注册列表“中的AVOutputFormat的指针.
    2. 对参数进行的值进行递增.

    查看av_muxer_iterate函数定义

    const AVOutputFormat *av_muxer_iterate(void **opaque)
    {
        static const uintptr_t size = sizeof(muxer_list)/sizeof(muxer_list[0]) - 1;
        uintptr_t i = (uintptr_t)*opaque;
        const AVOutputFormat *f = NULL;
        uintptr_t tmp;
    
        if (i < size) {
            f = muxer_list[i];
        } else if (tmp = atomic_load_explicit(&outdev_list_intptr, memory_order_relaxed)) {
            const AVOutputFormat *const *outdev_list = (const AVOutputFormat *const *)tmp;
            f = outdev_list[i - size];
        }
    
        if (f)
            *opaque = (void*)(i + 1);
        return f;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    原来注册列表就是一个数组,里面存放的就是解析和封装不同”format“的指针,这是想看muxer_list里面是些什么?但是源码文件里面这个却没有找到就很奇怪,先埋个坑,后面找到什么原因在纪录。反正基础逻辑就是"注册列表"就是一个数组,至于这个数组什么时候出现,我猜测是在链接的时候指向的。

  • 相关阅读:
    ceph 004 纠删码池 修改参数 cephx认证
    QMake中的预变量和库链接方式
    第八章 字符输入输出和输入验证
    完成Zookeeper集群部署
    Vue3 源码阅读(5):响应式系统 —— Vue2 中的 watch 和 computed
    Maven 介绍
    Go 常量为什么只支持基本数据类型?
    hbuildx mac离线安装插件
    数字藏品值得探究,依然是广阔的大海播
    9月前三大海外“债主”分别减持美债,”美债还完全吗?
  • 原文地址:https://blog.csdn.net/weixin_44742767/article/details/127962210