简介: CSDN博客专家、《Android系统多媒体进阶实战》作者
博主新书推荐:《Android系统多媒体进阶实战》🚀
Android Audio工程师专栏地址:Audio工程师进阶系列【原创干货持续更新中...... 】🚀
Android多媒体专栏地址:多媒体系统工程师系列【原创干货持续更新中...... 】🚀
专题一 二:AAOS车载系统+AOSP14系统攻城狮入门视频实战课 🚀
专题三:Android14 Binder之HIDL与AIDL通信实战课 🚀
专题四:Android15快速自定义与集成音效实战课 🚀
专题五:Android15音频策略实战课 🚀
专题六:Android15音频性能实战课(无声/杂音/断音/爆音实战案例) 🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
更多原创,欢迎关注:Android系统攻城狮

🍉🍉🍉文章目录🍉🍉🍉
🌻1.前言
本篇目的:理解
av_codec_iterate()如何逐个返回当前FFmpeg构建中的编解码器描述对象,并通过遍历程序筛选音视频编码器和解码器。
FFmpeg应用有时需要展示当前可用的编解码器、按媒体类型筛选实现,或检查某个构建是否包含特定编码器。av_codec_iterate()提供遍历入口,返回的是const AVCodec描述对象。
它只负责枚举编解码器,不会创建AVCodecContext、打开编解码器或处理媒体数据。
#mermaid-svg-ugmxgSa0vgeRR20V{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ugmxgSa0vgeRR20V .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ugmxgSa0vgeRR20V .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ugmxgSa0vgeRR20V .error-icon{fill:#552222;}#mermaid-svg-ugmxgSa0vgeRR20V .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ugmxgSa0vgeRR20V .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ugmxgSa0vgeRR20V .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ugmxgSa0vgeRR20V .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ugmxgSa0vgeRR20V .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ugmxgSa0vgeRR20V .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ugmxgSa0vgeRR20V .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ugmxgSa0vgeRR20V .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ugmxgSa0vgeRR20V .marker.cross{stroke:#333333;}#mermaid-svg-ugmxgSa0vgeRR20V svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ugmxgSa0vgeRR20V p{margin:0;}#mermaid-svg-ugmxgSa0vgeRR20V .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ugmxgSa0vgeRR20V .cluster-label text{fill:#333;}#mermaid-svg-ugmxgSa0vgeRR20V .cluster-label span{color:#333;}#mermaid-svg-ugmxgSa0vgeRR20V .cluster-label span p{background-color:transparent;}#mermaid-svg-ugmxgSa0vgeRR20V .label text,#mermaid-svg-ugmxgSa0vgeRR20V span{fill:#333;color:#333;}#mermaid-svg-ugmxgSa0vgeRR20V .node rect,#mermaid-svg-ugmxgSa0vgeRR20V .node circle,#mermaid-svg-ugmxgSa0vgeRR20V .node ellipse,#mermaid-svg-ugmxgSa0vgeRR20V .node polygon,#mermaid-svg-ugmxgSa0vgeRR20V .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ugmxgSa0vgeRR20V .rough-node .label text,#mermaid-svg-ugmxgSa0vgeRR20V .node .label text,#mermaid-svg-ugmxgSa0vgeRR20V .image-shape .label,#mermaid-svg-ugmxgSa0vgeRR20V .icon-shape .label{text-anchor:middle;}#mermaid-svg-ugmxgSa0vgeRR20V .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ugmxgSa0vgeRR20V .rough-node .label,#mermaid-svg-ugmxgSa0vgeRR20V .node .label,#mermaid-svg-ugmxgSa0vgeRR20V .image-shape .label,#mermaid-svg-ugmxgSa0vgeRR20V .icon-shape .label{text-align:center;}#mermaid-svg-ugmxgSa0vgeRR20V .node.clickable{cursor:pointer;}#mermaid-svg-ugmxgSa0vgeRR20V .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ugmxgSa0vgeRR20V .arrowheadPath{fill:#333333;}#mermaid-svg-ugmxgSa0vgeRR20V .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ugmxgSa0vgeRR20V .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ugmxgSa0vgeRR20V .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ugmxgSa0vgeRR20V .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ugmxgSa0vgeRR20V .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ugmxgSa0vgeRR20V .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ugmxgSa0vgeRR20V .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ugmxgSa0vgeRR20V .cluster text{fill:#333;}#mermaid-svg-ugmxgSa0vgeRR20V .cluster span{color:#333;}#mermaid-svg-ugmxgSa0vgeRR20V div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ugmxgSa0vgeRR20V .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ugmxgSa0vgeRR20V rect.text{fill:none;stroke-width:0;}#mermaid-svg-ugmxgSa0vgeRR20V .icon-shape,#mermaid-svg-ugmxgSa0vgeRR20V .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ugmxgSa0vgeRR20V .icon-shape p,#mermaid-svg-ugmxgSa0vgeRR20V .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ugmxgSa0vgeRR20V .icon-shape .label rect,#mermaid-svg-ugmxgSa0vgeRR20V .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ugmxgSa0vgeRR20V .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ugmxgSa0vgeRR20V .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ugmxgSa0vgeRR20V :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 空迭代状态
av_codec_iterate
返回一个AVCodec
检查类型和编码方向
找到目标音视频编解码器
🌻2.应用场景和用法
2.1枚举并筛选编解码器
遍历接口返回当前构建编入编解码器列表的项目。调用者可以根据AVCodec.type筛选视频、音频或字幕类型,再用av_codec_is_decoder()和av_codec_is_encoder()判断支持的方向。
| 场景 | 遍历后的筛选方式 | 结果 |
|---|---|---|
| 显示可用视频解码器 | 类型为AVMEDIA_TYPE_VIDEO且支持解码 |
得到视频解码器列表 |
| 显示可用音频编码器 | 类型为AVMEDIA_TYPE_AUDIO且支持编码 |
得到音频编码器列表 |
| 检查编解码器方向 | 分别调用编码和解码方向判断接口 | 判断实现属于编码器、解码器或两者 |
| 调试构建能力 | 遍历后查找名称或Codec ID | 确认当前构建中是否存在目标实现 |
遍历结果表示当前FFmpeg构建可见的实现集合,不代表运行时所有外部硬件或系统编码能力。若只想按Codec ID或名称查找单个实现,通常应使用avcodec_find_decoder()、avcodec_find_encoder()或名称查找接口。
2.2迭代参数与返回语义
函数原型如下:
c
const AVCodec *
av_codec_iterate (void **opaque);
| 参数或返回值 | 含义 |
|---|---|
opaque |
保存遍历位置的游标指针,开始遍历时必须指向NULL |
| 返回值 | 下一个AVCodec描述对象;遍历结束时返回NULL |
最小循环如下:
c
void *opaque = NULL;
const AVCodec *codec;
while ((codec = av_codec_iterate (&opaque))) {
/* 检查codec描述信息 */
}
opaque由FFmpeg更新,调用者不应解析或修改其内部值。它只是迭代位置,不需要单独释放。返回的AVCodec属于FFmpeg的编解码器描述信息,调用者也不负责释放它。
🌻3.调用流程剖析
3.1游标映射到编解码器列表
实现从opaque读取当前位置,并将其转换成列表索引。随后从内部codec_list取出对应的编解码器描述对象。
#mermaid-svg-EygdSq1OBBHBVJ3O{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-EygdSq1OBBHBVJ3O .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-EygdSq1OBBHBVJ3O .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-EygdSq1OBBHBVJ3O .error-icon{fill:#552222;}#mermaid-svg-EygdSq1OBBHBVJ3O .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-EygdSq1OBBHBVJ3O .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-EygdSq1OBBHBVJ3O .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-EygdSq1OBBHBVJ3O .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-EygdSq1OBBHBVJ3O .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-EygdSq1OBBHBVJ3O .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-EygdSq1OBBHBVJ3O .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-EygdSq1OBBHBVJ3O .marker{fill:#333333;stroke:#333333;}#mermaid-svg-EygdSq1OBBHBVJ3O .marker.cross{stroke:#333333;}#mermaid-svg-EygdSq1OBBHBVJ3O svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-EygdSq1OBBHBVJ3O p{margin:0;}#mermaid-svg-EygdSq1OBBHBVJ3O .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-EygdSq1OBBHBVJ3O .cluster-label text{fill:#333;}#mermaid-svg-EygdSq1OBBHBVJ3O .cluster-label span{color:#333;}#mermaid-svg-EygdSq1OBBHBVJ3O .cluster-label span p{background-color:transparent;}#mermaid-svg-EygdSq1OBBHBVJ3O .label text,#mermaid-svg-EygdSq1OBBHBVJ3O span{fill:#333;color:#333;}#mermaid-svg-EygdSq1OBBHBVJ3O .node rect,#mermaid-svg-EygdSq1OBBHBVJ3O .node circle,#mermaid-svg-EygdSq1OBBHBVJ3O .node ellipse,#mermaid-svg-EygdSq1OBBHBVJ3O .node polygon,#mermaid-svg-EygdSq1OBBHBVJ3O .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-EygdSq1OBBHBVJ3O .rough-node .label text,#mermaid-svg-EygdSq1OBBHBVJ3O .node .label text,#mermaid-svg-EygdSq1OBBHBVJ3O .image-shape .label,#mermaid-svg-EygdSq1OBBHBVJ3O .icon-shape .label{text-anchor:middle;}#mermaid-svg-EygdSq1OBBHBVJ3O .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-EygdSq1OBBHBVJ3O .rough-node .label,#mermaid-svg-EygdSq1OBBHBVJ3O .node .label,#mermaid-svg-EygdSq1OBBHBVJ3O .image-shape .label,#mermaid-svg-EygdSq1OBBHBVJ3O .icon-shape .label{text-align:center;}#mermaid-svg-EygdSq1OBBHBVJ3O .node.clickable{cursor:pointer;}#mermaid-svg-EygdSq1OBBHBVJ3O .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-EygdSq1OBBHBVJ3O .arrowheadPath{fill:#333333;}#mermaid-svg-EygdSq1OBBHBVJ3O .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-EygdSq1OBBHBVJ3O .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-EygdSq1OBBHBVJ3O .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-EygdSq1OBBHBVJ3O .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-EygdSq1OBBHBVJ3O .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-EygdSq1OBBHBVJ3O .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-EygdSq1OBBHBVJ3O .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-EygdSq1OBBHBVJ3O .cluster text{fill:#333;}#mermaid-svg-EygdSq1OBBHBVJ3O .cluster span{color:#333;}#mermaid-svg-EygdSq1OBBHBVJ3O div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-EygdSq1OBBHBVJ3O .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-EygdSq1OBBHBVJ3O rect.text{fill:none;stroke-width:0;}#mermaid-svg-EygdSq1OBBHBVJ3O .icon-shape,#mermaid-svg-EygdSq1OBBHBVJ3O .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-EygdSq1OBBHBVJ3O .icon-shape p,#mermaid-svg-EygdSq1OBBHBVJ3O .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-EygdSq1OBBHBVJ3O .icon-shape .label rect,#mermaid-svg-EygdSq1OBBHBVJ3O .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-EygdSq1OBBHBVJ3O .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-EygdSq1OBBHBVJ3O .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-EygdSq1OBBHBVJ3O :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
否
调用者传入opaque游标
转换为列表索引
当前位置是否有codec
返回AVCodec并推进游标
返回NULL结束遍历
列表中还有未返回项时,函数把游标推进到下一位置,并返回当前的AVCodec。到达列表结束标记后,函数返回NULL。内部静态数据初始化通过一次性初始化机制执行;每次遍历需要使用独立的游标变量。
3.2从枚举到目标编解码器
av_codec_iterate()不接收媒体类型或编解码方向作为参数,因此不会替调用者筛选结果。遍历后需要读取codec->type,并调用方向判断接口。
FFmpeg内部的按Codec ID查找逻辑也会遍历同一编解码器列表,再依据编码方向和ID筛选。对于应用层来说,枚举适合生成完整列表;查找单个解码器则优先调用avcodec_find_decoder()。
🌻4.实战案例
4.1准备迭代游标和统计变量
程序使用void *opaque = NULL启动迭代,并分别统计音频、视频类型的编码器和解码器数量。
4.2遍历并检查编解码器属性
每次调用取得一个AVCodec,程序根据type和方向接口判断是否计数。字幕、数据等其他媒体类型不计入本案例的音视频统计。
4.3完整代码
c
#include <stdio.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
int
main (void)
{
void *opaque = NULL;
const AVCodec *codec;
unsigned int audio_decoders = 0;
unsigned int audio_encoders = 0;
unsigned int video_decoders = 0;
unsigned int video_encoders = 0;
/* opaque为NULL时从编解码器列表起点开始 */
while ((codec = av_codec_iterate (&opaque))) {
int is_decoder;
int is_encoder;
if (codec->name == NULL)
continue;
/* 编码和解码能力需要分别判断 */
is_decoder = av_codec_is_decoder (codec);
is_encoder = av_codec_is_encoder (codec);
if (codec->type == AVMEDIA_TYPE_AUDIO) {
audio_decoders += is_decoder;
audio_encoders += is_encoder;
} else if (codec->type == AVMEDIA_TYPE_VIDEO) {
video_decoders += is_decoder;
video_encoders += is_encoder;
}
/* 输出每个音视频编解码器的类型和方向 */
if (codec->type == AVMEDIA_TYPE_AUDIO
|| codec->type == AVMEDIA_TYPE_VIDEO) {
const char *type_name =
av_get_media_type_string (codec->type);
printf ("%s\t%s\t%s%s\n",
codec->name,
type_name ? type_name : "unknown",
is_decoder ? "decoder " : "",
is_encoder ? "encoder" : "");
}
}
/* 汇总当前构建中枚举到的音视频实现数量 */
printf ("audio decoders: %u, audio encoders: %u\n",
audio_decoders, audio_encoders);
printf ("video decoders: %u, video encoders: %u\n",
video_decoders, video_encoders);
return 0;
}
🌻5.总结
av_codec_iterate()使用调用者提供的游标逐个返回当前构建中的AVCodec描述对象,筛选、创建上下文和打开编解码器由调用者或其他API负责。