【FFmpeg】avformat_alloc_output_context2函数

news2024/10/5 20:23:15

【FFmpeg】avformat_alloc_output_context2函数

  • 1.avformat_alloc_output_context2
    • 1.1 初始化AVFormatContext(avformat_alloc_context)
    • 1.2 格式猜测(av_guess_format)
      • 1.2.1 遍历可用的fmt(av_muxer_iterate)
      • 1.2.2 name匹配检查(av_match_name)
      • 1.2.3 扩展匹配检查(av_match_ext)
  • 2.小结

参考:
FFmpeg源代码简单分析:avformat_alloc_output_context2()

示例工程:
【FFmpeg】调用ffmpeg库实现264软编
【FFmpeg】调用ffmpeg库实现264软解
【FFmpeg】调用ffmpeg库进行RTMP推流和拉流
【FFmpeg】调用ffmpeg库进行SDL2解码后渲染

流程分析:
【FFmpeg】编码链路上主要函数的简单分析
【FFmpeg】解码链路上主要函数的简单分析

结构体分析:
【FFmpeg】AVCodec结构体
【FFmpeg】AVCodecContext结构体
【FFmpeg】AVStream结构体
【FFmpeg】AVFormatContext结构体
【FFmpeg】AVIOContext结构体
【FFmpeg】AVPacket结构体

函数分析:
【FFmpeg】avformat_open_input函数
【FFmpeg】avformat_find_stream_info函数

avformat_alloc_output_context2内函数调用关系如下
在这里插入图片描述

1.avformat_alloc_output_context2

函数的主要功能是分配一个输出的context,利用输入的format和filename来确定output format,主要工作流程为:
(1)初始化AVFormatContext(avformat_alloc_context)
(2)如果没有指定输出format,需要根据输入信息来猜测一个format(av_guess_format)
(3)根据输出的format,来对AVFormatContext中的priv_data初始化

int avformat_alloc_output_context2(AVFormatContext **avctx, const AVOutputFormat *oformat,
                                   const char *format, const char *filename)
{
    AVFormatContext *s = avformat_alloc_context();
    int ret = 0;

    *avctx = NULL;
    if (!s)
        goto nomem;
	// 没有指定输出format
    if (!oformat) {
    	// 但是指定了格式名称,例如rtp,flv,udp等,则进行输出format的猜测
        if (format) {
            oformat = av_guess_format(format, NULL, NULL);
            if (!oformat) {
                av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not known.\n", format);
                ret = AVERROR(EINVAL);
                goto error;
            }
        } else {	// 也没有指定格式名称,使用filename进行输出format的猜测
            oformat = av_guess_format(NULL, filename, NULL);
            if (!oformat) {
                ret = AVERROR(EINVAL);
                av_log(s, AV_LOG_ERROR,
                       "Unable to choose an output format for '%s'; "
                       "use a standard extension for the filename or specify "
                       "the format manually.\n", filename);
                goto error;
            }
        }
    }
	// 根据确定的输出format,对AVFormatContext中priv_data信息进行初始化
    s->oformat = oformat;
    if (ffofmt(s->oformat)->priv_data_size > 0) {
        s->priv_data = av_mallocz(ffofmt(s->oformat)->priv_data_size);
        if (!s->priv_data)
            goto nomem;
        if (s->oformat->priv_class) {
            *(const AVClass**)s->priv_data= s->oformat->priv_class;
            av_opt_set_defaults(s->priv_data);
        }
    } else
        s->priv_data = NULL;

    if (filename) {
        if (!(s->url = av_strdup(filename)))
            goto nomem;

    }
    *avctx = s;
    return 0;
nomem:
    av_log(s, AV_LOG_ERROR, "Out of memory\n");
    ret = AVERROR(ENOMEM);
error:
    avformat_free_context(s);
    return ret;
}

1.1 初始化AVFormatContext(avformat_alloc_context)

函数用于初始化AVFormatContext结构体

AVFormatContext *avformat_alloc_context(void)
{
    FFFormatContext *const si = av_mallocz(sizeof(*si));
    AVFormatContext *s;

    if (!si)
        return NULL;

    s = &si->pub;
    // 初始化函数指针
    s->av_class = &av_format_context_class;
    s->io_open  = io_open_default;
    s->io_close2= io_close2_default;
	
    av_opt_set_defaults(s);
	// 分配packet的空间
    si->pkt = av_packet_alloc();
    si->parse_pkt = av_packet_alloc();
    if (!si->pkt || !si->parse_pkt) {
        avformat_free_context(s);
        return NULL;
    }

#if FF_API_LAVF_SHORTEST
    si->shortest_end = AV_NOPTS_VALUE;
#endif

    return s;
}

1.2 格式猜测(av_guess_format)

该函数用于猜测一个format,类型为AVOutputFormat,

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;
    // 遍历每个可用的fmt
    while ((fmt = av_muxer_iterate(&i))) {
        score = 0;
        // 如果当前封装格式匹配,则置信度增加100
        if (fmt->name && short_name && av_match_name(short_name, fmt->name))
            score += 100;
        // 如果fmt的mime类型与mime_type匹配,则置信度增加10
        if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
            score += 10;
        if (filename && fmt->extensions &&
            av_match_ext(filename, fmt->extensions)) {
            // 如果扩展名匹配,则置信度增加5
            score += 5;
        }
        if (score > score_max) {
            score_max = score;
            fmt_found = fmt;
        }
    }
    return fmt_found;
}

其中,fmt中name、mime_type和extension的定义如下(以FLV格式为例)。在新版本FFmpeg中,FLV级别封装的格式不再是AVOutputFormat而是FFOutputFormat,但是FFOutputFormat之中的信息可以转换成AVOutputFormat,下面的.p就是AVOutputFormat的指针。对于FLV格式而言,name=“flv”,mime_type=“video/x-flv”,extensions=“flv”

const FFOutputFormat ff_flv_muxer = {
    .p.name         = "flv",
    .p.long_name    = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
    .p.mime_type    = "video/x-flv",
    .p.extensions   = "flv",
    .priv_data_size = sizeof(FLVContext),
    .p.audio_codec  = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
    .p.video_codec  = AV_CODEC_ID_FLV1,
    .init           = flv_init,
    .write_header   = flv_write_header,
    .write_packet   = flv_write_packet,
    .write_trailer  = flv_write_trailer,
    .deinit         = flv_deinit,
    .check_bitstream= flv_check_bitstream,
    .p.codec_tag    = (const AVCodecTag* const []) {
                          flv_video_codec_ids, flv_audio_codec_ids, 0
                      },
    .p.flags        = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
                      AVFMT_TS_NONSTRICT,
    .p.priv_class   = &flv_muxer_class,
};

1.2.1 遍历可用的fmt(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 FFOutputFormat *f = NULL;
    uintptr_t tmp;
	// 从muxer的list中获取一个fmt,list当中有180个muxer
    if (i < size) {
        f = muxer_list[i];
    } else if (tmp = atomic_load_explicit(&outdev_list_intptr, memory_order_relaxed)) {
        const FFOutputFormat *const *outdev_list = (const FFOutputFormat *const *)tmp;
        f = outdev_list[i - size];
    }
	// 如果找到了fmt,i = i + 1
    if (f) {
        *opaque = (void*)(i + 1);
        return &f->p;
    }
    return NULL;
}

1.2.2 name匹配检查(av_match_name)

/**
 * Match instances of a name in a comma-separated list of names.
 * List entries are checked from the start to the end of the names list,
 * the first match ends further processing. If an entry prefixed with '-'
 * matches, then 0 is returned. The "ALL" list entry is considered to
 * match all names.
 *
 * @param name  Name to look for.
 * @param names List of names.
 * @return 1 on match, 0 otherwise.
 */
// 在逗号分隔的名称列表中匹配名称的实例(字符串匹配)
// 从名字列表的开始到结束检查列表条目,第一个匹配结束进一步处理。
//		如果匹配前缀为'-'的条目,则返回0。“ALL”列表项被认为匹配所有名称
int av_match_name(const char *name, const char *names)
{
    const char *p;
    size_t len, namelen;

    if (!name || !names)
        return 0;

    namelen = strlen(name);
    while (*names) {
        int negate = '-' == *names;
        p = strchr(names, ','); // 因为有多个,分隔的字号
        if (!p)
            p = names + strlen(names);
        names += negate;
        len = FFMAX(p - names, namelen);
        if (!av_strncasecmp(name, names, len) || !strncmp("ALL", names, FFMAX(3, p - names)))
            return !negate;
        names = p + (*p == ',');
    }
    return 0;
}

1.2.3 扩展匹配检查(av_match_ext)

函数会提取出扩展名,然后调用av_match_name进行扩展名的匹配

/**
 * @file
 * Format register and lookup
 */

int av_match_ext(const char *filename, const char *extensions)
{
    const char *ext;

    if (!filename)
        return 0;

    ext = strrchr(filename, '.');
    if (ext)
        return av_match_name(ext + 1, extensions);
    return 0;
}

2.小结

avformat_alloc_output_context2函数是FFmpeg使用过程中重要的函数,它创建了输出的AVFormatContext,能够用于数据的输出,需要注意的是,新版本的FFmpeg中对上层格式的封装不再使用AVOutputFormat而是使用FFOutputFormat,将AVOutputFormat封装到了FFOutputFormat之中

CSDN : https://blog.csdn.net/weixin_42877471
Github : https://github.com/DoFulangChen

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1871403.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Bad owner or permissions on C:\\Users\\username/.ssh/config > 过程试图写入的管道不存在。

使用windows连接远程服务器出现Bad owner or permissions 错误 问题&#xff1a; 需要修复文件权限 SSH 配置文件应具有受限权限以防止未经授权的访问 确保只有用户对该.ssh/config文件具有读取权限 解决方案&#xff1a; 在windows下打开命令行&#xff0c;通过以下命令打开文…

Spring Cloud Alibaba之负载均衡组件Ribbon

一、什么是负载均衡&#xff1f; &#xff08;1&#xff09;概念&#xff1a; 在基于微服务架构开发的系统里&#xff0c;为了能够提升系统应对高并发的能力&#xff0c;开发人员通常会把具有相同业务功能的模块同时部署到多台的服务器中&#xff0c;并把访问业务功能的请求均…

Kubernetes之 资源管理

系列文章目录 Kubernetes之 资源管理 文章目录 系列文章目录前言一、资源管理介绍二、YAML语言介绍 1.1.YAML语法&#xff1a;2.读入数据总结 一、资源管理介绍 在kubernetes中&#xff0c;所有的内容都抽象为资源&#xff0c;用户需要通过操作资源来管理kubernetes。 1. kub…

SMTP 转发器/中继

设置中继邮件服务器 我将设置一个邮件服务器&#xff0c;该服务器稍后将用作 SMTP 中继服务器。首先&#xff0c;在 Digital Ocean 中创建了一个新的 Ubuntu Droplet&#xff1a; Postfix MTA 安装在droplet上&#xff0c;并带有&#xff1a; apt-get install postfix 在pos…

序列检测器(Moore型)

目录 描述 输入描述&#xff1a; 输出描述&#xff1a; 参考代码 描述 请用Moore型状态机实现序列“1101”从左至右的不重叠检测。 电路的接口如下图所示。当检测到“1101”&#xff0c;Y输出一个时钟周期的高电平脉冲。 接口电路图如下&#xff1a; 输入描述&#xff1a…

【机器学习300问】132、自注意力机制(Self-Attention)和传统注意力机制(Attention)的区别?

最近学习注意力机制的时候&#xff0c;发现相同的概念很多&#xff0c;有必要给这些概念做一下区分&#xff0c;不然后续的学习可能会混成一团。本文先区分一下自注意力机制和传统注意力机制。我会先直接给出它们之间有何区别的结论&#xff0c;然后通过一个例子来说明。 【机…

阿里云服务器通过镜像下hunggingface上的模型

参考连接https://blog.csdn.net/lanlinjnc/article/details/136709225 https://www.bilibili.com/video/BV1VT421X7xe/?spm_id_from333.337.search-card.all.click&vd_source1ba257184239f03bd3caf4c6cab427e4 pip install -U huggingface_hub# 建议将上面这一行写入 ~/…

Ubuntu Nvidia GPU驱动安装和故障排除

去官网 菜单列表下载&#xff0c;或者直接下载驱动 wget https://cn.download.nvidia.com/XFree86/Linux-x86_64/550.54.14/NVIDIA-Linux-x86_64-550.54.14.run 安装驱动 /data/install/NVIDIA-Linux-x86_64-550.54.14.run 执行命令&#xff0c;显示GPU情况 出错处理&…

Android开发系列(十一)Jetpack Compose之Dialog

Dialogs是在应用程序中显示一些额外信息或进行用户交互的常见功能。Jetpack Compose中的Dialog可以通过使用AlertDialog组件来创建。 基本用法 下面通过示例来了解Dialog的使用。 OptIn(ExperimentalMaterial3Api::class) Composable fun AlertDialogExample(onDismissReques…

vue3用自定义指令实现按钮权限

1&#xff0c;编写permission.ts文件 在src/utils/permission.ts import type { Directive } from "vue"; export const permission:Directive{// 在绑定元素的父组件被挂载后调用mounted(el,binding){// el&#xff1a;指令所绑定的元素&#xff0c;可以用来直接操…

从文章到视频:如何用ChatGPT打造自媒体全能内容

在当今自媒体时代&#xff0c;内容创作的多样性和多元化成为了吸引和保持观众注意力的关键。无论是文章、视频还是音频内容&#xff0c;创作者们都需要灵活运用各种形式来触达不同的受众群体。ChatGPT作为一种先进的AI语言模型&#xff0c;能够为自媒体创作者提供强大的支持&am…

通过代理从ARDUINO IDE直接下载开发板包

使用免费代理 实现ARDUINO IDE2.3.2 下载ESP8266/ESP32包 免费代理 列表 测试代理是否可用的 网站 有时&#xff0c;代理是可用的&#xff0c;但依然有可能找不到开发板管理器的资料包。 可以多换几个代理试试。 代理的配置 文件 -> 首选项 -> 网络 进入后做如下配置…

OpenCV报错已解决:Vector析构异常OpencvAssert CrtlsValidHeapPointer

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《C干货基地》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! 引入 在使用OpenCV进行图像处理时&#xff0c;我们可能会遇到Vector析构异常OpencvAssert CrtlsValidHeapPointer的问题。本文将…

电脑开机就一直在开机界面转圈,怎么回事?

前言 前段时间小白去给一位朋友修电脑。她说这个电脑很奇怪&#xff0c;有时候开机很快就进入电脑界面&#xff0c;但有时候开机一直在那转圈&#xff0c;半天也不见进入。 Windows7系统的小伙伴应该也有遇到过类似的问题&#xff0c;就是电脑一直在Windows的logo界面&#xf…

39 - 安全技术与防火墙

39、安全技术和防火墙 一、安全技术 入侵检测系统&#xff1a;特点是不阻断网络访问&#xff0c;主要是提供报警和事后监督。不主动介入&#xff0c;默默看着你&#xff08;监控&#xff09;。 入侵防御系统&#xff1a;透明模式工作&#xff0c;数据包&#xff0c;网络监控…

MySQL8 新特性——公用表表达式用法

MySQL8 新特性——公用表表达式用法_mysql ctes-CSDN博客 1.普通公用表表达式 MySQL8 新特性——公用表表达式用法 在MySQL 8.0及更高版本中&#xff0c;引入了公用表表达式&#xff08;Common Table Expressions&#xff0c;CTEs&#xff09;&#xff0c;它是一种方便且可重…

生信实证系列Vol.15:如何用AlphaFold2,啪,一键预测100+蛋白质结构

"结构就是功能"——蛋白质的工作原理和作用取决于其3D形状。 2020年末&#xff0c;基于深度神经网络的AlphaFold2&#xff0c;一举破解了困扰生物学界长达五十年之久的“蛋白质折叠”难题&#xff0c;改变了科学研究的游戏规则&#xff0c;可以从蛋白质序列直接预测…

AI Agent项目实战(02)-对话情感优化

1 使用prompt设计agent性格与行为 添加系统 prompt&#xff1a; self.SYSTEMPL """你是一个非常厉害的算命先生&#xff0c;你叫JavaEdge人称Edge大师。以下是你的个人设定:1. 你精通阴阳五行&#xff0c;能够算命、紫薇斗数、姓名测算、占卜凶吉&#xff0c…

高中数学:不等式-常见题型解题技巧

一、“1”的代换 练习 例题1 例题2 解 二、基本不等式中的“变形” 就是&#xff0c;一般情况下&#xff0c;我们在题目中&#xff0c;是不能够直接使用基本不等式进行求解的。 而是要对条件等式进行变形&#xff0c;满足基本不等式的使用条件 练习 例题1 解析 两边同…

13 Redis-- MySQL 和 Redis 的数据一致性

Redis-- MySQL 和 Redis 的数据一致性 先抛一下结论&#xff1a;在满足实时性的条件下&#xff0c;不存在两者完全保存一致的方案&#xff0c;只有最终一致性方案。 不好的方案&#xff1a;先写 MS&#xff0c;再写 Redis 例如 &#xff1a;A请求更新数据为10&#xff0c;B…