Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7771271
  • 博文数量: 701
  • 博客积分: 2150
  • 博客等级: 上尉
  • 技术积分: 13233
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-29 16:28
个人简介

天行健,君子以自强不息!

文章分类

全部博文(701)

文章存档

2019年(2)

2018年(12)

2017年(76)

2016年(120)

2015年(178)

2014年(129)

2013年(123)

2012年(61)

分类: 高性能计算

2018-12-28 11:08:54

4. Filter的结构体关系图与定义

4.1 结构体间的关系图

filter涉及的结构体,主要包括:
> FilterGraph, AVFilterGraph
> InputFilter, InputStream, OutputFilter, OutputStream
> AVFilter, AVFilterContext
> AVFilterLink
> AVFilterPad;

它们之间的类关系如下图所示:


图5:滤镜结构体关系图

从上图可以看到,FFmpeg的滤镜相关的结构体三层组成:
1) filtergraph层
由结构体 FilterGraph, AVFilterGraph组成;
其中,
FilterGraph, 包含一个InputFilter, 它指示了整个Graph的第一个滤镜,并指示了InputStream, 从而作为整个Graph的输入;
包含一个OutputFilter, 它指示了整个Graph的最后一个滤镜,并指示了OutputStream,从而作为整个Graph的输出;
包含一个AVFilterGraph的实例,它指示的是组成本graph的filter;

2) filterchain层
它由AVFilter, AVFilterContext, AVFilterLink, AVFilterPad组成;
其中,AVFilterContext是AVFilter的实例;
而filter之间是用 AVFilterLink进行连接,意思是,滤镜之间并不是直接相连的,是通过AVFilterLink进行连接;
AVFilterContext 通过AVFilterLink进行连接后,就组成了Filterchain。
而AVFilterContext与AVFilterLink之间的AVFilterPad是直接相连的,对应的关系是
AVFilterContext的output_pad 连接它下游AVFilterLink的 srcpad;
AVFilterContext的input_pad 连接它上游AVFilterLink的 dstpad;

3) filter层
由AVFilterContext, AVFilterPad组成;
其中AVFilterContext是真正进行数据处理的滤镜实体;

AVFilterPad用于AVFilterContext之间的callback(回调):
第一个AVFilterContext的outputs[0]指针,指向第一个AVFilterLink,这个AVFilterLink的dst指针,指向第二个AVFilterContext。
如果在前一个AVFilterContext调用 outputs[0]->dstpad->filter_frame(Frame* input_frame1),
那其实就意味着,第一个过滤器,可以把处理好的一个frame(名字为input_frame1),可以通过这个调用,传递给第二个过滤器的input_pads的filter_frame函数。
而第二个过滤器,里面就是用户自己实现的filter_frame(),以对数据进行处理;




以 ./ffmpeg_g -i INPUT.mp4 -vn -acodec libfdk_aac -filter_complex "aresample=osf=s16,denoise" -ar 16000 -ac 1 -y OUTPUT.mp4 为例:
它们的实际数据流程如下图所示:

图6:-filter_complex "aresample=osf=s16,denoise"的滤镜实例图

从上图可以看到,虽然我们只使用了两个滤镜,但构建出来的filterchain实际上包含有:五个filter, 和四个link;
是因为ffmpeg系统会自动为用户设置的滤镜添加上必要的输入、输出滤镜,并将它们连接起来,组成一个完整的filterchain;
其中,ffmpeg默认是有3个filter的!分别是 "graph_0_in_0_0","format_out_0_0", "out_0_0"
它们对应的滤镜类型是: “buffersrc”, “format”, “buffersink”。
它们对应的源码位于: libavfilter/buffersrc.c libavfilter/vf_format.c(对应视频格式) libavfilter/format.c(对应音频格式) , libavfilter/buffersink.c


上图中:
蓝色和绿色连线 是 AVFilterContext 与 AVFilterLink的连接关系 ;
红色连线 是 AVFilterPad的连接关系;
灰色连线 是 组成AVFilterGraph的AVFilterContext;

上图中,上半部分是filtergraph层:
左边连接的是InputFilter,用来引入filtergraph的输入流;
右边连接的是OutputFilter,用来导出filtergraph的输出流;
并且指示了组成本filtergraph的所有filter;

上图中,下半部分是filterchain层:
AVFilterContext与AVFilterLink是一种双向链表指针的关系,见图左的蓝色和绿色连线所示;
即 AVFilterContext的 outputs 指向 它的下游 AVFilterLink;
而 下游的AVFilterLink的 src 批向它上游的 AVFilterContext;

4.2 FilterGraph 滤镜图结构体定义


  1. typedef struct FilterGraph {
  2.     int index;
  3.     const char *graph_desc;

  4.     AVFilterGraph *graph; // 指向它的实例
  5.     int reconfiguration;

  6.     InputFilter **inputs;
  7.     int nb_inputs;
  8.     OutputFilter **outputs;
  9.     int nb_outputs;
  10. } FilterGraph;

4.3 AVFilterGraph 滤镜图结构体定义


  1. typedef struct AVFilterGraph {
  2.     const AVClass *av_class;
  3.     AVFilterContext **filters; // 本 滤镜图 包含的所有 filter, 包括系统自生成的
  4.     unsigned nb_filters;

  5.     char *scale_sws_opts; ///< sws options to use for the auto-inserted scale filters
  6. #if FF_API_LAVR_OPTS
  7.     attribute_deprecated char *resample_lavr_opts; ///< libavresample options to use for the auto-inserted resample filters
  8. #endif

  9.     /**
  10.      * Type of multithreading allowed for filters in this graph. A combination
  11.      * of AVFILTER_THREAD_* flags.
  12.      *
  13.      * May be set by the caller at any point, the setting will apply to all
  14.      * filters initialized after that. The default is allowing everything.
  15.      *
  16.      * When a filter in this graph is initialized, this field is combined using
  17.      * bit AND with AVFilterContext.thread_type to get the final mask used for
  18.      * determining allowed threading types. I.e. a threading type needs to be
  19.      * set in both to be allowed.
  20.      */
  21.     int thread_type;

  22.     /**
  23.      * Maximum number of threads used by filters in this graph. May be set by
  24.      * the caller before adding any filters to the filtergraph. Zero (the
  25.      * default) means that the number of threads is determined automatically.
  26.      */
  27.     int nb_threads;

  28.     /**
  29.      * Opaque object for libavfilter internal use.
  30.      */
  31.     AVFilterGraphInternal *internal;

  32.     /**
  33.      * Opaque user data. May be set by the caller to an arbitrary value, e.g. to
  34.      * be used from callbacks like @ref AVFilterGraph.execute.
  35.      * Libavfilter will not touch this field in any way.
  36.      */
  37.     void *opaque;

  38.     /**
  39.      * This callback may be set by the caller immediately after allocating the
  40.      * graph and before adding any filters to it, to provide a custom
  41.      * multithreading implementation.
  42.      *
  43.      * If set, filters with slice threading capability will call this callback
  44.      * to execute multiple jobs in parallel.
  45.      *
  46.      * If this field is left unset, libavfilter will use its internal
  47.      * implementation, which may or may not be multithreaded depending on the
  48.      * platform and build options.
  49.      */
  50.     avfilter_execute_func *execute;

  51.     char *aresample_swr_opts; ///< swr options to use for the auto-inserted aresample filters, Access ONLY through AVOptions

  52.     /**
  53.      * Private fields
  54.      *
  55.      * The following fields are for internal use only.
  56.      * Their type, offset, number and semantic can change without notice.
  57.      */

  58.     AVFilterLink **sink_links;
  59.     int sink_links_count;

  60.     unsigned disable_auto_convert;
  61. } AVFilterGraph;

4.4. 视频滤镜类型的宏定义


  1. /**
  2.  * 滤镜输入源的个数不能单由成员变量 AVFilter.inputs 来确定。
  3.  * 即依赖于 options 的支持与否,这个滤镜在初始化时可以添加额外的输入源;
  4.  */
  5. #define AVFILTER_FLAG_DYNAMIC_INPUTS (1 << 0)
  6. /**
  7.  * 滤镜输出目的的个数不能单由成员变量 AVFilter.outputs来确定。
  8.  * 即依赖于 options 的支持与否,这个滤镜在初始化时可以添加额外的输出目的
  9.  */
  10. #define AVFILTER_FLAG_DYNAMIC_OUTPUTS (1 << 1)
  11. /**
  12.  * 这个滤镜通过将一帧划分成多个部分,然后使用多线程来并行处理它们;
  13.  */
  14. #define AVFILTER_FLAG_SLICE_THREADS (1 << 2)
  15. /**
  16.  * 有些滤镜支持一个一般意义上的"enable"表达式选项。它可以用来在时间轴上开启或关闭滤镜。
  17.  * 如果滤镜支持这个选项就是设置这个标志。
  18.  * 当 enable 表达式为假时,默认的无操作的 filter_frame()函数将被调用,它会替换掉在每个输入pad上定义的回调函数 filter_frame();
  19.  * 因此,这一帧不做任何处理地透传到下一个滤镜;
  20.  */
  21. #define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC (1 << 16)
  22. /**
  23.  * 和 AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC一样,
  24.  * 有些滤镜希望在 enable表达式为假时,调用自己的 filter_frame()回调函数。
  25.  * 例如,依据AVFilterCOntext->is_disable值,这个滤镜会使用它自己的filter_frame()回调来关闭它的处理。
  26. */
  27. #define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL (1 << 17)
  28. /**
  29.  * 用来测试滤镜是否支持时间轴功能(internally or generically)的掩码
  30.  */
  31. #define AVFILTER_FLAG_SUPPORT_TIMELINE (AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL)

4.5. AVFilter 结构体定义详解


  1. /**
  2.  * 滤镜定义。
  3.  * 它定义了滤镜与外部进行交互的pad(管脚,这个概念来自集成电脑,表示滤镜和外部交互的接口),
  4.  * 以及和本滤镜进行交互的所有回调函数;
  5.  */
  6. typedef struct AVFilter {
  7.     /* 滤镜名。必须是非空且唯一的; */
  8.     const char *name;

  9.     /* 滤镜的描述,可以为空。 */
  10.     const char *description;

  11.     /**
  12.      * Pad : 输入Pad列表,以零元素结束, 它是和它的上一个AVFilterLink的 dstpad 相连的;
  13.      * 如果没有(静态的)输入,则将其设置为空。
  14.      * 如果滤镜是 AVFILTER_FLAG_DYNAMIC_INPUTS 类型的实例,则在这个列表中可能有多个输入
  15.      */
  16.     const AVFilterPad *inputs;
  17.     /**
  18.      * Pad : 输出Pad列表,以零元素结束, 它是和它的下一个AVFilterLink的 srcpad 相连的;
  19.      * 如果没有(静态的)输出,则将其设置为空。
  20.      * 如果滤镜是 AVFILTER_FLAG_DYNAMIC_OUTPUTS 类型的实例,则在这个列表中可能有多个输出
  21.      */
  22.     const AVFilterPad *outputs;

  23.     /**
  24.      * 私有数据类,用于声明滤镜私有的 AVoptions 结构体;
  25.      * 如果滤镜没有任何可选项则将这个值初始化为NULL;
  26.      * 如果这个值不为空,那么滤镜私有数据的第一个成员变量必须是 AVClass指针;
  27.      */
  28.     const AVClass *priv_class;

  29.     /**
  30.      * 滤镜类型标志 , 其值为 AVFILTER_FLAG_* 宏定义
  31.      */
  32.     int flags;

  33.     /*****************************************************************
  34.      * 下面的变量不是公开的API,它们不能被libavfilter的外部使用
  35.      *****************************************************************/

  36.     /**
  37.      * 回调函数: 滤镜预初始化函数
  38.      * 当滤镜的 context 被分配后,将立即调用这个回调函数,来分配空间和初始化子对象。
  39.      * 如果 这个回调函数不为空, 当遇到空间分配失败时将调用 uninit 回调函数;
  40.      * 返回值: 0, 表示成功
  41.      * AVERROR,表示失败,但是这个错误码会被删除,并被调用代码视为 ENOMEM 。
  42.      */
  43.     int (*preinit)(AVFilterContext *ctx);

  44.     /**
  45.      * 回调函数: 滤镜初始化函数
  46.      * 在滤镜的整个生命周期中,这个回调函数只会在 所有可选项被设置后,滤镜链建立及格式协调之前 被调用 一次。
  47.      * 滤镜本身的基本初始化可以放在这里,
  48.      * ----- 意思是只是初始化滤镜本身的状态,结构体,及本滤镜专用的参数(如输入参数再计算得到的参数)可以放在这里;
  49.      * 如果 这些初始化需要使用到本滤镜外的(如上下游滤镜,系统)参数,则需要把这样的初始化放在 config_input()中;
  50.      * NOTE:输入参数的默认值是在 static const AVOption xxxx_options[] 中设置的,不需要再单独设置;
  51.      *
  52.      * 如果滤镜是多输入/输出类型的,那么应该在这里基于提供的可选项创建这些 inputs/outputs。
  53.       * 对于滤镜来说,当这个回调被调用后,就不能再有滤镜的inputs/outputs的改变了。
  54.      *
  55.       * 这个回调函数它假设滤镜链是有效的,或者帧参数是有效的
  56.       * AVFilter.uninit 指针指向的 uninit 函数 保证了即使初始化失败时 ,这个函数将会被调用 , 因此失败时这个回调函数不需要清空
  57.       *
  58.       * 返回值: 0, 表示成功; 负数AVERROR, 表示失败。
  59.      
  60.      */
  61.     int (*init)(AVFilterContext *ctx);

  62.     /**
  63.      * Should be set instead of @ref AVFilter.init "init" by the filters that
  64.      * want to pass a dictionary of AVOptions to nested contexts that are
  65.      * allocated during init.
  66.      *
  67.      * On return, the options dict should be freed and replaced with one that
  68.      * contains all the options which could not be processed by this filter (or
  69.      * with NULL if all the options were processed).
  70.      *
  71.      * Otherwise the semantics is the same as for @ref AVFilter.init "init".
  72.      * 回调函数: 这个变量是被滤镜设置,用来代替 AVFilter.init "init" 回调函数,
  73.      * 它会传递一个 AVOptions的字典 给 嵌套的 context (它是在初始化进分配的)
  74.      * 返回时,这个字典选项应当被释放,且被一个包含所有选项的
  75.      */
  76.     int (*init_dict)(AVFilterContext *ctx, AVDictionary **options);

  77.     /**
  78.      * 回调函数: 滤镜释放函数 , 它必须在滤镜释放前仅调用一次。
  79.      * 在这个回调函数中,应当释放滤镜的所有申请的内存,指针引用等。
  80.      * 但它不需要去释放 AVFilterContext.priv 内存空间本身;
  81.      * 即便 AVFilter.init "init" 函数没有被调用或调用失败,这个函数也可能会被调用。
  82.      * 因此,它必须能处理这种情况
  83.      */
  84.     void (*uninit)(AVFilterContext *ctx);

  85.     /**
  86.      * 回调函数: 检查本滤镜输入列表和输出列表支持的格式。
  87.      * 它是在滤镜初始化后(这时输入列表和输出列表都固定了),格式协商之前被调用。它可以被调用多次。
  88.      *
  89.      * 在这个回调函数中, 必须对每一个输入link 设置 AVFilterLink.out_formats, 对每一个输出link 设置 AVFilterLink.in_formats,
  90.      * 列出这个滤镜的link 支持的 像素/样本格式列表。
  91.      * 对于音频 link, 这个滤镜还需要设置的参数有:
  92.      * @ref AVFilterLink.in_samplerates "in_samplerates" /
  93.      * @ref AVFilterLink.out_samplerates "out_samplerates" and
  94.      * @ref AVFilterLink.in_channel_layouts "in_channel_layouts" /
  95.      * @ref AVFilterLink.out_channel_layouts "out_channel_layouts"
  96.      *
  97.      * 如果滤镜只有一个输入,那这个回调函数可以设置为空,
  98.      * 在这种情况下, libavfilter假设它支持所有的输入格式,并在输出时保留它们
  99.      *
  100.      * 返回值: 0, 表示成功; 负数,对应为AVERROR 码
  101.      */
  102.     int (*query_formats)(AVFilterContext *);

  103.     int priv_size; ///< 滤镜分配的私有数据的大小

  104.     int flags_internal; ///< avfilter内部使用的额外的标志

  105.     /**
  106.      * 它是被滤镜注册系统使用的,任何其它代码都不需要去操作它
  107.      */
  108.     struct AVFilter *next;

  109.     /**
  110.      * 让滤镜实例执行一个命令;
  111.      * 参数: cmd , 要执行的命令,为简化处理,所有命令必须仅为字母数字;
  112.      * arg , 命令的参数
  113.      * res , 大小为 res_size的buffer, 用于存放滤镜的返回。当命令不支持时,不需要改变它。
  114.      * flags , 如果设置为 AVFILTER_CMD_FLAG_FAST, 且这个命令是有时间消耗的时,这个滤镜将会不执行命令
  115.      * 返回值: >=0, 表示成功。 否则为错误码,其中,AVERROR(ENOSYS) 为滤镜不支持这个命令
  116.      */
  117.     int (*process_command)(AVFilterContext *, const char *cmd, const char *arg, char *res, int res_len, int flags);

  118.     /**
  119.      * 回调函数: 滤镜初始化函数,它用来替代 init()回调函数。
  120.      * 参数中可以包含用户提供的参数 : opaque是用于传输二进制数据
  121.      */
  122.     int (*init_opaque)(AVFilterContext *ctx, void *opaque);

  123.     /**
  124.      * Filter activation function.
  125.      *
  126.      * Called when any processing is needed from the filter, instead of any
  127.      * filter_frame and request_frame on pads.
  128.      *
  129.      * The function must examine inlinks and outlinks and perform a single
  130.      * step of processing. If there is nothing to do, the function must do
  131.      * nothing and not return an error. If more steps are or may be
  132.      * possible, it must use ff_filter_set_ready() to schedule another
  133.      * activation.
  134.      * 回调函数: 滤镜激活函数
  135.      * 当有处理需要这个滤镜时,就会调用这个函数。
  136.      */
  137.     int (*activate)(AVFilterContext *ctx);
  138. } AVFilter;

4.6 AVFilterContext 滤镜实例的结构体


  1. /** An instance of a filter */
  2. struct AVFilterContext {
  3.     const AVClass *av_class; ///< needed for av_log() and filters common options

  4.     const AVFilter *filter; ///< 指明本实例是哪个AVFilter的实例

  5.     char *name; ///< 当前滤镜实例的名称

  6.     AVFilterPad *input_pads; ///< 输入Pad的数组 array of input pads
  7.     AVFilterLink **inputs; ///< 输入link的指针数组 array of pointers to input links
  8.     unsigned nb_inputs; ///< 输入Pad的个数 number of input pads

  9.     AVFilterPad *output_pads; ///< 输出Pad的数组 array of output pads
  10.     AVFilterLink **outputs; ///< 输出link的指针数组 array of pointers to output links
  11.     unsigned nb_outputs; ///< 输出Pad的个数 number of output pads

  12.     void *priv; ///< 本滤镜使用的私有数据 private data for use by the filter

  13.     struct AVFilterGraph *graph; ///< 指明本滤镜是属于哪个filtergraph

  14.     /**
  15.      * Type of multithreading being allowed/used. A combination of
  16.      * AVFILTER_THREAD_* flags.
  17.      *
  18.      * May be set by the caller before initializing the filter to forbid some
  19.      * or all kinds of multithreading for this filter. The default is allowing
  20.      * everything.
  21.      *
  22.      * When the filter is initialized, this field is combined using bit AND with
  23.      * AVFilterGraph.thread_type to get the final mask used for determining
  24.      * allowed threading types. I.e. a threading type needs to be set in both
  25.      * to be allowed.
  26.      *
  27.      * After the filter is initialized, libavfilter sets this field to the
  28.      * threading type that is actually used (0 for no multithreading).
  29.      */
  30.     int thread_type;

  31.     /**
  32.      * An opaque struct for libavfilter internal use.
  33.      */
  34.     AVFilterInternal *internal;

  35.     struct AVFilterCommand *command_queue;

  36.     char *enable_str; ///< enable expression string
  37.     void *enable; ///< parsed expression (AVExpr*)
  38.     double *var_values; ///< variable values for the enable expression
  39.     int is_disabled; ///< the enabled state from the last expression evaluation

  40.     /**
  41.      * For filters which will create hardware frames, sets the device the
  42.      * filter should create them in. All other filters will ignore this field:
  43.      * in particular, a filter which consumes or processes hardware frames will
  44.      * instead use the hw_frames_ctx field in AVFilterLink to carry the
  45.      * hardware context information.
  46.      */
  47.     AVBufferRef *hw_device_ctx;

  48.     /**
  49.      * Max number of threads allowed in this filter instance.
  50.      * If <= 0, its value is ignored.
  51.      * Overrides global number of threads set per filter graph.
  52.      */
  53.     int nb_threads;

  54.     /**
  55.      * Ready status of the filter.
  56.      * A non-0 value means that the filter needs activating;
  57.      * a higher value suggests a more urgent activation.
  58.      */
  59.     unsigned ready;

  60.     /**
  61.      * Sets the number of extra hardware frames which the filter will
  62.      * allocate on its output links for use in following filters or by
  63.      * the caller.
  64.      *
  65.      * Some hardware filters require all frames that they will use for
  66.      * output to be defined in advance before filtering starts. For such
  67.      * filters, any hardware frame pools used for output must therefore be
  68.      * of fixed size. The extra frames set here are on top of any number
  69.      * that the filter needs internally in order to operate normally.
  70.      *
  71.      * This field must be set before the graph containing this filter is
  72.      * configured.
  73.      */
  74.     int extra_hw_frames;
  75. };

4.7 AVFilterLink : 链接结构体定义


  1. libavfilter/avfilter.h
  2. /**
  3.  * 两个滤镜之间的链接;
  4.  * 如果两个滤镜是有链接的,则这个链接结构体包含有指向源滤镜与目的滤镜的指针,
  5.  * 及 源滤镜的输出Pad, 和 目的滤镜的输入Pad 指针;
  6.  * 另外,这个链接还包含有这个两个滤镜经过协商并匹配成功的参数,如图像的分辨率,格式等;
  7.  *
  8.  * 应用程序不能直接去访问链接结构体,而是使用 buffersrc 和 buffersink API 来访问,如:
  9.  *
  10.  */
  11. struct AVFilterLink {
  12.     AVFilterContext *src; ///< 指向和它相连的上游的源滤镜
  13.     AVFilterPad *srcpad; ///< 源Pad , 和上游源滤镜的 output_pad 相连

  14.     AVFilterContext *dst; ///< 指向和它相连的下游的目的滤镜
  15.     AVFilterPad *dstpad; ///< 目的Pad, 和下游目目的滤镜的 innput_pad 相连

  16.     enum AVMediaType type; ///< Link的媒体类型

  17.     /* 视频相关参数 */
  18.     int w; ///< agreed upon image width
  19.     int h; ///< agreed upon image height
  20.     AVRational sample_aspect_ratio; ///< agreed upon sample aspect ratio
  21.     /* 音频相关参数 */
  22.     uint64_t channel_layout; ///< channel layout of current buffer (see libavutil/channel_layout.h)
  23.     int sample_rate; ///< samples per second

  24.     int format; ///< 协商后并匹配成功的媒体格式 ID

  25.     /**
  26.      * 定义将通过此链接的 帧/样本的PTS表示用的 时钟基,
  27.      * 在配置阶段,每个滤镜应当只能改变输出的时钟基,而输入的时钟基是不可以更改的
  28.      */
  29.     AVRational time_base;

  30.     /*****************************************************************
  31.      * 下面的所有成员属性与函数都不是公开API,它们不能被 libavfilter的外部使用
  32.      *****************************************************************
  33.      */
  34.     /**
  35.      * 输入滤镜与输出滤镜支持的格式、通道布局、采样率列表,这些列表用是用于实际的格式协商。
  36.      * 当协商成功后,匹配的格式与通道布局将会更新上面的 format 、channel_layout 、sample_rate 成员属性;
  37.      */
  38.     AVFilterFormats *in_formats;
  39.     AVFilterFormats *out_formats;

  40.     AVFilterFormats *in_samplerates;
  41.     AVFilterFormats *out_samplerates;

  42.     struct AVFilterChannelLayouts *in_channel_layouts;
  43.     struct AVFilterChannelLayouts *out_channel_layouts;

  44.     /**
  45.      * 仅用于音频;
  46.      * 输出滤镜会设置它为一个非零值,用于请求将有设定数量的样本缓冲区发送给它。
  47.      * 此时,对应输入Pad的 AVFilterPad.needs_fifo 也要做设置;
  48.      * EOF之前的最后一个buffer将会使用静音数据填充;
  49.      */
  50.     int request_samples;

  51.     /** stage of the initialization of the link properties (dimensions, etc) */
  52.     enum {
  53.         AVLINK_UNINIT = 0, ///< not started
  54.         AVLINK_STARTINIT, ///< started, but incomplete
  55.         AVLINK_INIT ///< complete
  56.     } init_state;

  57.     /**
  58.      * 滤镜所属的滤镜图;
  59.      */
  60.     struct AVFilterGraph *graph;

  61.     /**
  62.      * 本链接的当前时间戳,由最近的帧定义,单位是 上面的 time_base;
  63.      */
  64.     int64_t current_pts;

  65.     /**
  66.      * 本链接的当前时间戳,由最近的帧定义,单位是 AV_TIME_BASE (即ffmpeg内部使用的时钟基)
  67.      */
  68.     int64_t current_pts_us;

  69.     /**
  70.      * Index in the age array.
  71.      */
  72.     int age_index;

  73.     /**
  74.      * 本滤镜链的流的帧率,当帧率是未知或可变时,设为 {1/0};
  75.      * 如果设置为 {0/0}, 将会自动从源滤镜的输入复制得到;
  76.      *
  77.      * 源滤镜应当将其设置为实际帧率的最佳估值。
  78.      * 如果源滤镜帧率是未知或是可变的,则将其设置为 {1/0}.
  79.      * 如果有必要,滤镜应当在它的处理函数中更新这个值;
  80.      * Sink型滤镜可以用它来设置默认的输出帧率;
  81.       * 它和 AVStream中 的 r_frame_rate 类似;
  82.      */
  83.     AVRational frame_rate;

  84.     /**
  85.      * Buffer partially filled with samples to achieve a fixed/minimum size.
  86.      *
  87.      */
  88.     AVFrame *partial_buf;

  89.     /**
  90.      * Size of the partial buffer to allocate.Must be between min_samples and max_samples.
  91.      *
  92.      */
  93.     int partial_buf_size;

  94.     /**
  95.      * 滤镜一次能处理的最小样本数。
  96.      * 如果调用 filter_frame() 函数 时 的样本数小于这个值时,那这个函数将会在 partial_buf中将这些样本累积起来。
  97.      * 这个属性值 及 相关的属性在滤镜初始化完成后就不能再更改了;
  98.      * 如果设置为零,则所有相关的属性都被忽略;
  99.      */
  100.     int min_samples;

  101.     /**
  102.      * 滤镜一次能处理的最大样本数。
  103.      * 如果调用 filter_frame() 函数时的 样本数大于这个值时,那这个函数要将样本切分后再处理;
  104.      */
  105.     int max_samples;

  106.     /**
  107.      * 音频通道的个数
  108.      */
  109.     int channels;

  110.     /**
  111.      * Link processing flags. 链接处理标志
  112.      */
  113.     unsigned flags;

  114.     /**
  115.      * 通过链接的输入的帧数,输出的帧数;
  116.      */
  117.     int64_t frame_count_in, frame_count_out;

  118.     /**
  119.      * A pointer to a FFFramePool struct.
  120.      */
  121.     void *frame_pool;

  122.     /**
  123.      * True if a frame is currently wanted on the output of this filter.
  124.      * Set when ff_request_frame() is called by the output,
  125.      * cleared when a frame is filtered.
  126.      * 如果当前帧希望从滤镜输出,则此值为真。
  127.      */
  128.     int frame_wanted_out;

  129.     /**
  130.      * For hwaccel pixel formats, this should be a reference to the
  131.      * AVHWFramesContext describing the frames.
  132.      */
  133.     AVBufferRef *hw_frames_ctx;

  134. #ifndef FF_INTERNAL_FIELDS

  135.     /**
  136.      * Internal structure members.
  137.      * The fields below this limit are internal for libavfilter's use
  138.      * and must in no way be accessed by applications.
  139.      */
  140.     char reserved[0xF000];

  141. #else /* FF_INTERNAL_FIELDS */

  142.     /**
  143.      * Queue of frames waiting to be filtered.
  144.      */
  145.     FFFrameQueue fifo;

  146.     /**
  147.      * If set, the source filter can not generate a frame as is.
  148.      * The goal is to avoid repeatedly calling the request_frame() method on
  149.      * the same link.
  150.      */
  151.     int frame_blocked_in;

  152.     /**
  153.      * Link input status.
  154.      * If not zero, all attempts of filter_frame will fail with the
  155.      * corresponding code.
  156.      */
  157.     int status_in;

  158.     /**
  159.      * Timestamp of the input status change.
  160.      */
  161.     int64_t status_in_pts;

  162.     /**
  163.      * Link output status.
  164.      * If not zero, all attempts of request_frame will fail with the
  165.      * corresponding code.
  166.      */
  167.     int status_out;

  168. #endif /* FF_INTERNAL_FIELDS */

  169. };

4.8. AVFilterPad结构体定义


  1. /**
  2.  * @addtogroup lavu_media Media Type
  3.  * @brief Media Type
  4.  */

  5. enum AVMediaType {
  6.     AVMEDIA_TYPE_UNKNOWN = -1, ///< Usually treated as AVMEDIA_TYPE_DATA
  7.     AVMEDIA_TYPE_VIDEO,
  8.     AVMEDIA_TYPE_AUDIO,
  9.     AVMEDIA_TYPE_DATA, ///< Opaque data information usually continuous
  10.     AVMEDIA_TYPE_SUBTITLE,
  11.     AVMEDIA_TYPE_ATTACHMENT, ///< Opaque data information usually sparse
  12.     AVMEDIA_TYPE_NB
  13. };


  14. /**
  15.   * 一个 filter pad 就是一个滤镜的输入/输出端口
  16.  */
  17. struct AVFilterPad {
  18.     /**
  19.      * Pad名称, 在输入列表,或输出列表内部必须是唯一的,但输入列表,输出列表之间可以重名。
  20.      * 如果 这个 Pad 不会被以名称引用,则其值可以为NULL。
  21.      */
  22.     const char *name;

  23.     /**
  24.      * AVFilterPad 类型
  25.      */
  26.     enum AVMediaType type;

  27.     /**
  28.      * 获得一个视频 buffer的回调函数,如果为空,则滤镜系统会使用默认的 ff_default_get_video_buffer();
  29.      * 只对输入的视频 Pad 有效
  30.      */
  31.     AVFrame *(*get_video_buffer)(AVFilterLink *link, int w, int h);

  32.     /**
  33.      * 获得一个音频 buffer的回调函数,如果为空,则滤镜系统会使用默认的 ff_default_get_audio_buffer();
  34.      * 只对输入的 音频 Pad 有效
  35.      */
  36.     AVFrame *(*get_audio_buffer)(AVFilterLink *link, int nb_samples);
  37.     /**
  38.      * 调用滤镜进行处理的回调函数:当滤镜收到一帧 音频/视频 数据时,就要调用它进行处理。
  39.      * 它是滤镜处理的真正入口 , 只在输入 pad中使用;
  40.      *
  41.      * 返回值: >=0, 表示成功,负数,为AVERROR的错误。
  42.     * 这个函数必须确保 当滤镜的处理出现错误时未被不合适地引用,且不会将错误传递到下一个滤镜
  43.      */
  44.     int (*filter_frame)(AVFilterLink *link, AVFrame *frame);

  45.     /**
  46.      * Frame poll callback. This returns the number of immediately available
  47.      * samples. It should return a positive value if the next request_frame()
  48.      * is guaranteed to return one frame (with no delay).
  49.      *
  50.      * Defaults to just calling the source poll_frame() method.
  51.      *
  52.      * Output pads only.
  53.      * 回调函数: 帧轮询的回调函数, 它返回当前有效的样本个数; 只对输出Pad有效;
  54.      * 如果下一个 request_frame()函数是有帧数据返回,则它应该返回一个正数值;
  55.      */
  56.     int (*poll_frame)(AVFilterLink *link);

  57.     /**
  58.      * Frame request callback. A call to this should result in some progress
  59.      * towards producing output over the given link. This should return zero
  60.      * on success, and another value on error.
  61.      *
  62.      * Output pads only.
  63.      * 回调函数: 帧请求回调函数。只对输出Pad有效;
  64.      * 如果调用 了这个回调函数,应当在给定的link上生成经过处理后的输出数据;
  65.      * 返回值: 0,表示成功;否则为错误值;
  66.      */
  67.     int (*request_frame)(AVFilterLink *link);

  68.     /**
  69.      * 回调函数: 用于link的配置的回调函数
  70.      * 对于 输出 Pad, 它应当设置 link的属性,如 width/height等。
  71.      * 不要在这里设置 格式 属性;
  72.      * 因为格式属性是由滤镜系统调用这个回调函数之前,调用query_formats() 回调函数在滤镜间进行协调得到的。
  73.      * 对于输入 pad, 这个回调函数是用检查 link的属性,并由此更新滤镜内部的相关状态;
  74.      *
  75.      * 对于既有输入,又有输出的滤镜,这个回调函数在成功时返回零,其它返回值为出错。
  76.      */
  77.     int (*config_props)(AVFilterLink *link);

  78.     /**
  79.      * 这个滤镜需要有一个 FIFO插入它的输入link,通常是因为这个滤镜会有一个延迟的动作。
  80.      * 只在输入pad中使用
  81.      */
  82.     int needs_fifo;

  83.     /**
  84.      * 这个滤镜需要从它的输入 link中得到一个可写的帧,如果有需要,会复制数据buffer
  85.      * 只在输入pad中使用
  86.      */
  87.     int needs_writable;
  88. };





阅读(3926) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~