FFMPeg代码分析:AVCodec结构体以及编解码器的查找和加载

2023-11-23 04:58

本文主要是介绍FFMPeg代码分析:AVCodec结构体以及编解码器的查找和加载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

书接上回。在调用av_find_stream_info函数分析媒体文件并找到其中的视频流之后,视频流的相关信息被存放在了AVFormatContext结构体实例中。此时AVCodecContext实例所保存的AVCodec仍然为空。该结构体的定义如下:

typedef struct AVCodec {const char *name;//codec名称,如果是解码HEVC的文件,那就是"hevc"const char *long_name;//codec全名,"HEVC(High Efficient Video Coding)"enum AVMediaType type;//当前codec针对的媒体类型,在此为AVMEDIA_TYPE_VIDEOenum AVCodecID id;//codec_id,检索codec的依据/*** Codec capabilities.* see CODEC_CAP_**/int capabilities;const AVRational *supported_framerates; //<span style="font-family: Arial, Helvetica, sans-serif;">当前codec支持的码率</span>const enum AVPixelFormat *pix_fmts;     //<span style="font-family: Arial, Helvetica, sans-serif;">当前codec支持的像素格式</span>const int *supported_samplerates;       //<span style="font-family: Arial, Helvetica, sans-serif;">当前codec支持的采样速率(audio)</span>const enum AVSampleFormat *sample_fmts; //<span style="font-family: Arial, Helvetica, sans-serif;">当前codec支持的采样格式(audio)</span>const uint64_t *channel_layouts;         //<span style="font-family: Arial, Helvetica, sans-serif;">当前codec支持的声道数</span>
#if FF_API_LOWRESuint8_t max_lowres;                     ///< maximum value for lowres supported by the decoder, no direct access, use av_codec_get_max_lowres()
#endifconst AVClass *priv_class;              ///< AVClass for the private contextconst AVProfile *profiles;              ///< array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN}/****************************************************************** No fields below this line are part of the public API. They* may not be used outside of libavcodec and can be changed and* removed at will.* New public fields should be added right above.******************************************************************/int priv_data_size;struct AVCodec *next;/*** @name Frame-level threading support functions* @{*//*** If defined, called on thread contexts when they are created.* If the codec allocates writable tables in init(), re-allocate them here.* priv_data will be set to a copy of the original.*/int (*init_thread_copy)(AVCodecContext *);/*** Copy necessary context variables from a previous thread context to the current one.* If not defined, the next thread will start automatically; otherwise, the codec* must call ff_thread_finish_setup().** dst and src will (rarely) point to the same context, in which case memcpy should be skipped.*/int (*update_thread_context)(AVCodecContext *dst, const AVCodecContext *src);/** @} *//*** Private codec-specific defaults.*/const AVCodecDefault *defaults;/*** Initialize codec static data, called from avcodec_register().*/void (*init_static_data)(struct AVCodec *codec);int (*init)(AVCodecContext *);int (*encode_sub)(AVCodecContext *, uint8_t *buf, int buf_size,const struct AVSubtitle *sub);/*** Encode data to an AVPacket.** @param      avctx          codec context* @param      avpkt          output AVPacket (may contain a user-provided buffer)* @param[in]  frame          AVFrame containing the raw data to be encoded* @param[out] got_packet_ptr encoder sets to 0 or 1 to indicate that a*                            non-empty packet was returned in avpkt.* @return 0 on success, negative error code on failure*/int (*encode2)(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame,int *got_packet_ptr);int (*decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt);int (*close)(AVCodecContext *);/*** Flush buffers.* Will be called when seeking*/void (*flush)(AVCodecContext *);
} AVCodec;
在先前的demo中,该结构体实例的codec_id为AV_CODEC_ID_HEVC,表明源文件所包含的视频流应采用HEVC解码器进行解码。函数avcodec_find_decoder通过这个codec_id搜索解码器并返回给一个AVCodec实例,实现代码如下:

AVCodec *avcodec_find_decoder(enum AVCodecID id)
{return find_encdec(id, 0);
}
static AVCodec *find_encdec(enum AVCodecID id, int encoder)
{AVCodec *p, *experimental = NULL;p = first_avcodec;id= remap_deprecated_codec_id(id);while (p) {if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&p->id == id) {if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {experimental = p;} elsereturn p;}p = p->next;}return experimental;
}
int av_codec_is_decoder(const AVCodec *codec)
{return codec && codec->decode;
}
在find_encdec函数中可以看出,各个编解码器以链表的形式连接。通过遍历链表的方法找到id与输入id相同的AVCodec并返回。HEVC的解码器也由一个结构体ff_hevc_decoder实现,这个结构体的定义如下(hevc.c):

AVCodec ff_hevc_decoder = {.name                  = "hevc",.long_name             = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),.type                  = AVMEDIA_TYPE_VIDEO,.id                    = AV_CODEC_ID_HEVC,.priv_data_size        = sizeof(HEVCContext),.priv_class            = &hevc_decoder_class,.init                  = hevc_decode_init,.close                 = hevc_decode_free,.decode                = hevc_decode_frame,.flush                 = hevc_decode_flush,.update_thread_context = hevc_update_thread_context,.init_thread_copy      = hevc_init_thread_copy,.capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
};

这篇关于FFMPeg代码分析:AVCodec结构体以及编解码器的查找和加载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


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

相关文章

C++作用域和标识符查找规则详解

《C++作用域和标识符查找规则详解》在C++中,作用域(Scope)和标识符查找(IdentifierLookup)是理解代码行为的重要概念,本文将详细介绍这些规则,并通过实例来说明它们的工作原理,需... 目录作用域标识符查找规则1. 普通查找(Ordinary Lookup)2. 限定查找(Qualif

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示