当前位置: 首页 > news >正文

FFmpeg 基本API avformat_alloc_context 函数内部调用流程分析

1、avformat_alloc_context 函数定义

avformat_alloc_context 是 FFmpeg 库中的一个函数,用于分配并初始化一个 AVFormatContext 结构体的指针。在使用 FFmpeg 进行音视频处理时,通常需要先调用这个函数来创建一个 AVFormatContext 对象。
函数原型如下:

AVFormatContext *avformat_alloc_context(void)
{// 1. 分配FFFormatContext结构体FFFormatContext *const si = av_mallocz(sizeof(*si));AVFormatContext *s;// 2. 初始化FFFormatContext结构体s = &si->pub;s->av_class = &av_format_context_class;// 3. 设置默认的io_open和io_close2函数s->io_open  = io_open_default;s->io_close2= io_close2_default;// 4. 设置默认的选项av_opt_set_defaults(s);// 5. 分配AVPacket结构体si->pkt = av_packet_alloc();si->parse_pkt = av_packet_alloc();return s;
}

在使用完 AVFormatContext 对象后,可以通过调用 void avformat_free_context(AVFormatContext *s) 函数来释放分配的内存。

2、avformat_alloc_context分配主要流程

avformat_alloc_context() 关键流程:

  • 分配 AVFormatContext
  • 初始化内部成员(AVIOContext 为 NULL)
  • 设置默认参数:start_time, duration, packet_size, probesize, max_analyze_duration 等
  • 分配 AVFormatInternal 并初始化子字段
  • 分配 AVStreamList 等辅助结构(部分版本在 open 时延后)

3、avformat_alloc_context与avformat_open_input() / avformat_free_context() 的关系

  • avformat_alloc_context():只分配并做最小初始化。
  • avformat_open_input(&ic, url, iformat, options):
    • 如果 *ic 为 NULL,avformat_open_input() 会内部调用 avformat_alloc_context() 来创建。
    • avformat_open_input() 会做真正的“打开和初始化”:探测格式、分配 pb(如果必要)、分配 demuxer 私有数据(ic->priv_data / iformat->priv_data)、调用 read_header 回调并填充 streams 等。
    • 如果 avformat_open_input() 失败,它会清理分配的资源(包括释放 ic,除非你传入已分配的 ic 并指定了不同的行为——实现细节随版本)。因此常见用法是把 ic 初始化为 NULL,直接把 &ic 交给 avformat_open_input(),让它负责分配与回滚。
  • avformat_free_context(AVFormatContext *s):释放 AVFormatContext 和已分配资源(streams、metadata、pb 等),适用于你只调用了 avformat_alloc_context() 或你想手动释放。另外 avformat_close_input() 是更常用的对输入 context 的清理方式(它会调用 avformat_free_context() 并关闭 I/O)

4、avformat_alloc_context初始化参数列表

AVFormatContext的初始化列表参数定义在 libavformat\options_table.h avformat_options中,其中比较重要如下:

{"probesize", "set probing size", OFFSET(probesize), AV_OPT_TYPE_INT64, {.i64 = 5000000 }, 32, INT64_MAX, D},
{"formatprobesize", "number of bytes to probe file format", OFFSET(format_probesize), AV_OPT_TYPE_INT, {.i64 = PROBE_BUF_MAX}, 0, INT_MAX-1, D},

暂时无法在飞书文档外展示此内容

5、avformat_alloc_context分配主要时序图

主要时序图

http://www.dtcms.com/a/486706.html

相关文章:

  • ubuntu系统中ffmpeg+x264简易编译安装指南
  • FLAC to MP3 批量转换 Python
  • 开源鸿蒙6.1和8.1版本被确定为LTS建议版本,最新路标正式发布!-转自开源鸿蒙OpenHarmony社区
  • linux sdl图形编程之helloworld.
  • 开发一个网站系统报价电子商务网站建设试卷及答案
  • 瑞芯微算法环境搭建(2)------编译opencv
  • 计算机视觉(opencv)——人脸网格关键点检测
  • 自己做网站投入编程培训机构需要哪些证件
  • AXI总线的基础知识
  • 【泛微OA】泛微OA平台实现计算具体的天数
  • 「深度学习笔记1」深度学习全面解析:从基本概念到未来趋势
  • puppeteer 生成pdf,含动态目录,目录带页码
  • 深度学习的卷积神经网络中医舌诊断病系统-ResNet50与VGG16方法的比较研究
  • 如何查看网站空间wordpress图片多选
  • Android的Activity与intent知识点
  • 一、前置基础(MVC学习前提)_核心特性_【C# MVC 前置】委托与事件:从 “小区通知” 看懂 MVC 过滤器的底层逻辑
  • 网站建设年度汇报详情页设计理念怎么写
  • Spring Boot项目中Maven引入依赖常见报错问题解决
  • Android 13 启动的时候会显示一下logo,很不友好
  • seo网站快速排名外包关键词seo排名优化如何
  • Linux防火墙:核心机制与安全实践全解析
  • SX1261IMLTRT射频收发器Semtech赋能远距离物联网连接的低功耗射频芯片IC
  • Flutter---坐标网格图标
  • 邢台做网站多少钱洪泽网站建设
  • 整体设计 逻辑系统程序 之30 定稿V1 之1 含 4 套程序架构、三式模型与时空约束体系
  • 基于单片机的智能洗衣机的设计与实现(论文+源码)
  • 【ComfyUI】SDXL Revision 文本提示实现参考图像概念迁移生成
  • Zabbix 模板、监控项、图形指南
  • 个人如何做网站软件湛江免费制作网站
  • 嘉兴公司网站模板建站建设厅官方网站