首页 > 其他分享 >ffmpeg结构体解析-AVClass 和 AVOption

ffmpeg结构体解析-AVClass 和 AVOption

时间:2024-06-06 16:34:23浏览次数:13  
标签:const ffmpeg AVClass AVOption offset class struct

AVClass

先来看 AVClass 的结构如下:

/**
 * Describe the class of an AVClass context structure. That is an
 * arbitrary struct of which the first field is a pointer to an
 * AVClass struct (e.g. AVCodecContext, AVFormatContext etc.).
 */
typedef struct AVClass {
    /**
     * The name of the class; usually it is the same name as the
     * context structure type to which the AVClass is associated.
     */
    const char* class_name;

    /**
     * A pointer to a function which returns the name of a context
     * instance ctx associated with the class.
     */
    const char* (*item_name)(void* ctx);

    /**
     * a pointer to the first option specified in the class if any or NULL
     *
     * @see av_set_default_options()
     */
    const struct AVOption *option;

    /**
     * LIBAVUTIL_VERSION with which this structure was created.
     * This is used to allow fields to be added without requiring major
     * version bumps everywhere.
     */

    int version;

    /**
     * Offset in the structure where log_level_offset is stored.
     * 0 means there is no such variable
     */
    int log_level_offset_offset;

    /**
     * Offset in the structure where a pointer to the parent context for
     * logging is stored. For example a decoder could pass its AVCodecContext
     * to eval as such a parent context, which an av_log() implementation
     * could then leverage to display the parent context.
     * The offset can be NULL.
     */
    int parent_log_context_offset;

    /**
     * Return next AVOptions-enabled child or NULL
     */
    void* (*child_next)(void *obj, void *prev);

    /**
     * Return an AVClass corresponding to the next potential
     * AVOptions-enabled child.
     *
     * The difference between child_next and this is that
     * child_next iterates over _already existing_ objects, while
     * child_class_next iterates over _all possible_ children.
     */
    const struct AVClass* (*child_class_next)(const struct AVClass *prev);

    /**
     * Category used for visualization (like color)
     * This is only set if the category is equal for all objects using this class.
     * available since version (51 << 16 | 56 << 8 | 100)
     */
    AVClassCategory category;

    /**
     * Callback to return the category.
     * available since version (51 << 16 | 59 << 8 | 100)
     */
    AVClassCategory (*get_category)(void* ctx);

    /**
     * Callback to return the supported/allowed ranges.
     * available since version (52.12)
     */
    int (*query_ranges)(struct AVOptionRanges **, void *obj, const char *key, int flags);
} AVClass;

根据 API 的意思,AVClass 就是用于描述任意包含 AVClass 的一个 Context 的信息类,并且会出现在该 Context 类的第一个字段。然后看下 AVClass 结构体的字段如下:

  • const char* class_name:表示Context 类的名称,一般和描述的 Context 类的类名/结构体名一致

  • const char* (item_name)(void ctx):可以返回该 Context 类实例的一个函数

  • const struct AVOption *option:AcOption 数组

  • version:创建该结构体的版本号

  • log_level_offset_offset:表示 level_offset_offset 字段在这个结构体的偏移量,如果没有这个字段则为0

  • parent_log_context_offset:同上,表示的是 Parent Context 的 level_offset_offset 字段在这个结构体的偏移量

  • void* (*child_next)(void *obj, void *prev):一个返回下一个 AVOption 的函数指针

  • const struct AVClass* (*child_class_next)(const struct AVClass *prev)

  • AVClassCategory category:表示 AVClass 的类型,是一组枚举类型的值,见后面备注。

  • AVClassCategory (get_category)(void ctx):

  • int (*query_ranges)(struct AVOptionRanges **, void *obj, const char *key, int flags):

 

在 AVFormatContext 或者 AVCodecContext 等类里面,第一个字段都是 AVClass 指针,以AVFormatContext示例,如下:


//avformat.h
typedef struct AVFormatContext {
/**
* A class for logging and @ref avoptions. Set by avformat_alloc_context().
* Exports (de)muxer private options if they exist.
*/
const AVClass *av_class;
//省略其它代码

根据API的介绍,AVClass 就是一个用于打印log 和AVOption的引用,由avformat_alloc_context() 函数创建。怎么理解这个引用呢?

就是说,AVClass 建立起了 AVOption 和 Context 之间的桥梁。

AVOption 结构体
那么这样一说,必须先来看下 AVOption 这个结构体。

先看 API 对 AVOption 结构体的说明,如下:

* AVOptions provide a generic system to declare options on arbitrary structs
* ("objects"). An option can have a help text, a type and a range of possible
* values. Options may then be enumerated, read and written to.
*
* @section avoptions_implement Implementing AVOptions
* This section describes how to add AVOptions capabilities to a struct.
*
* All AVOptions-related information is stored in an AVClass. Therefore
* the first member of the struct should be a pointer to an AVClass describing it.
* The option field of the AVClass must be set to a NULL-terminated static array
* of AVOptions. Each AVOption must have a non-empty name, a type, a default
* value and for number-type AVOptions also a range of allowed values. It must
* also declare an offset in bytes from the start of the struct, where the field
* associated with this AVOption is located. Other fields in the AVOption struct
* should also be set when applicable, but are not required.

AVOption 提供了一种泛型系统用于描述任意结构体的 Option。所有 AVOption 相关的信息都是存储在 AVClass 里面的。因此所以AVOption 描述的结构体的第一个字段必定是 AVClass,其次AVClass 必须包含一个静态的 AVOption 数组。

AVOption 的定义在 libutils/opts.h 头文件中,如下:

/**
 * AVOption
 */
typedef struct AVOption {
    const char *name;

    /**
     * short English help text
     * @todo What about other languages?
     */
    const char *help;

    /**
     * The offset relative to the context structure where the option
     * value is stored. It should be 0 for named constants.
     */
    int offset;
    enum AVOptionType type;

    /**
     * the default value for scalar options
     */
    union {
        int64_t i64;
        double dbl;
        const char *str;
        /* TODO those are unused now */
        AVRational q;
    } default_val;
    double min;                 ///< minimum valid value for the option
    double max;                 ///< maximum valid value for the option

    int flags;
#define AV_OPT_FLAG_ENCODING_PARAM  1   ///< a generic parameter which can be set by the user for muxing or encoding
#define AV_OPT_FLAG_DECODING_PARAM  2   ///< a generic parameter which can be set by the user for demuxing or decoding
#define AV_OPT_FLAG_AUDIO_PARAM     8
#define AV_OPT_FLAG_VIDEO_PARAM     16
#define AV_OPT_FLAG_SUBTITLE_PARAM  32
/**
 * The option is intended for exporting values to the caller.
 */
#define AV_OPT_FLAG_EXPORT          64
/**
 * The option may not be set through the AVOptions API, only read.
 * This flag only makes sense when AV_OPT_FLAG_EXPORT is also set.
 */
#define AV_OPT_FLAG_READONLY        128
#define AV_OPT_FLAG_BSF_PARAM       (1<<8) ///< a generic parameter which can be set by the user for bit stream filtering
#define AV_OPT_FLAG_RUNTIME_PARAM   (1<<15) ///< a generic parameter which can be set by the user at runtime
#define AV_OPT_FLAG_FILTERING_PARAM (1<<16) ///< a generic parameter which can be set by the user for filtering
#define AV_OPT_FLAG_DEPRECATED      (1<<17) ///< set if option is deprecated, users should refer to AVOption.help text for more information
#define AV_OPT_FLAG_CHILD_CONSTS    (1<<18) ///< set if option constants can also reside in child objects
//FIXME think about enc-audio, ... style flags

    /**
     * The logical unit to which the option belongs. Non-constant
     * options and corresponding named constants share the same
     * unit. May be NULL.
     */
    const char *unit;
} AVOption;

来看下 AVOption 结构体的字段:

const char *name:Context Struc 结构体的字段名称

const char *help:帮助说明

int offset:表示该字段在结构体中的偏移量,一般可以使用 C标准库的 offsetof(type, member-designator) 计算该字体在结构体中的偏移量

enum AVOptionType type:枚举值,表示该字段的类型,后面讲解

default_val: 默认数值,可以是 int,double,字符串任意类型。

min max:这两个字段表示取值范围,限定了该字段的取值范围

int flags:类型标记位,取值如描述

const char *unit:该选项所属的逻辑单元,可以为空



作者:kotlon
链接:https://www.jianshu.com/p/25a087619b7c
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


标签:const,ffmpeg,AVClass,AVOption,offset,class,struct
From: https://www.cnblogs.com/lizishaoye/p/18235529

相关文章

  • Android视频开发入门: VideoView、MediaPlayer、 FFmpeg、exoplayer...
    现在,视频功能是越来越普遍的需求。本文将提供一个关于Android视频开发的入门指南,帮助读者快速掌握视频播放、录制和处理等基本功能。1、概述在Android平台上,视频开发主要涉及以下几个方面:视频播放与控制视频录制与处理视频编解码与格式转换视频流媒体与直播接下来,我......
  • 无缝融合:使用 Python 和 PyFFmpeg 合并视频的完整指南
    前言在当今数字化时代,视频内容无处不在。从社交媒体到在线教育,视频已经成为我们生活中不可或缺的一部分。但是,有时候我们可能需要将多个视频片段合并成一个,创造出更丰富、更有吸引力的内容。而今天,我们将向您展示如何使用Python和PyFFmpeg工具实现这一目标。准备工作:安装P......
  • 音频剪裁大师:使用 Python 和 ffmpeg 分割音频的完整指南
    前言在音频处理中,有时候我们需要对音频文件进行分割,提取其中的部分内容以满足特定需求。而Python提供了许多强大的工具和库来实现这一目标,其中ffmpeg是一个功能强大的工具,它不仅支持音频分割,还能进行音频转码、合并、提取等操作。本文将介绍如何使用Python和ffmpeg来分......
  • Linux ffmpeg 离线安装
    linux版本下载地址:http://www.ffmpeg.org/releases/4.3.1 下载地址:http://ffmpeg.org/releases/ffmpeg-4.3.1.tar.gz 配套组件yasm下载http://www.tortall.net/projects/yasm/releases/http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz yasm安......
  • YOLOv8输出视频.avi有损转.mp4(使用ffmpeg)
    问题:在使用YOLOv8模型直接推理视频后,存储的视频文件格式默认为.avi格式,且推理出的视频占用空间巨大,亲测500多M的视频推理完保存的结果视频有25个多G,此时当视频在服务器上时,想预览就需要下载至本地,对于这么大的视频要耗费大量时间。解决办法:可以使用ffmpeg视频处理工具对.avi格......
  • FFmpeg开发笔记(二十六)Linux环境安装ZLMediaKit实现视频推流
    ​《FFmpeg开发实战:从零基础到短视频上线》一书在第10章介绍了轻量级流媒体服务器MediaMTX,通过该工具可以测试RTSP/RTMP等流媒体协议的推拉流。不过MediaMTX的功能实在是太简单了,无法应用于真实直播的生产环境,真正能用于生产环境的流媒体服务器还要看SRS或者ZLMediaKit。ZLMedia......
  • ffmpeg编码之实现YUV转换成H264
    方法1:命令转换#转换ffmpeg-s720*1280-pix_fmtyuv420p-iinput.yuv-vcodeclibx264-b:v4096k-bf0-g10-r30output.h264#播放ffplayoutput.h264方法2:代码转换 main.c#include"libavutil/log.h"#include"libavutil/avutil.h"#include......
  • 语音合成与文字语音互转 Baidu.AI+ffmpeg
    语音和文本互转安装Baidu.AIInstall-PackageBaidu.AI将文本合成语音将文本合成为语音文件主要使用百度云API中的Tts类,该类是语音合成的交互类,为使用语音合成的开发人员提供了一系列的交互方法。Tts类中提供了一个Synthnesis方法,哟过来将文本合成语音,其语法如下:参数......
  • FFmpeg开发笔记(二十五)Linux环境给FFmpeg集成libwebp
    ​《FFmpeg开发实战:从零基础到短视频上线》一书介绍了JPEG、PNG、GIF等图片格式,以及如何通过FFmpeg把视频画面转存为这些格式。除了上述这些常见的图片格式,还有较新的WebP格式,它由VP8视频标准派生而来,VP8演进的视频格式叫做WebM,图片格式则叫WebP。若想让FFmpeg支持WebP图片的编......
  • ffmpeg使用方法
    合成视频ffmpeg-framerate25-iout/%04d.jpeg-c:vlibx264-r25-pix_fmtyuv420p-t40out_temp.mp4 截取指定坐标位置的视频ffmpeg-iin.avi-vfcrop=w=500:h=500:x=980:y=120out.mp4截取指定时长的音频ffmpeg-iinput.wav-ss00:00:00-t10-ccopyoutpu......