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

相关文章

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

Python 迭代器和生成器概念及场景分析

《Python迭代器和生成器概念及场景分析》yield是Python中实现惰性计算和协程的核心工具,结合send()、throw()、close()等方法,能够构建高效、灵活的数据流和控制流模型,这... 目录迭代器的介绍自定义迭代器省略的迭代器生产器的介绍yield的普通用法yield的高级用法yidle

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当