[FFmpeg-devel] [PATCH 03/10] lavfi: move AVFilterLink.m{ax, in}_samples to FilterLink

Anton Khirnov anton at khirnov.net
Sun Aug 11 17:42:04 EEST 2024


Also, document who sets these fields and when.
---
 libavfilter/af_firequalizer.c |  4 +++-
 libavfilter/af_lv2.c          |  3 ++-
 libavfilter/af_replaygain.c   |  6 ++++--
 libavfilter/avfilter.c        | 13 +++++++------
 libavfilter/avfilter.h        | 15 ---------------
 libavfilter/buffersink.c      |  5 +++--
 libavfilter/filters.h         | 21 +++++++++++++++++++++
 7 files changed, 40 insertions(+), 27 deletions(-)

diff --git a/libavfilter/af_firequalizer.c b/libavfilter/af_firequalizer.c
index 5108edca48..b889872775 100644
--- a/libavfilter/af_firequalizer.c
+++ b/libavfilter/af_firequalizer.c
@@ -26,6 +26,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/tx.h"
 #include "avfilter.h"
+#include "filters.h"
 #include "internal.h"
 #include "audio.h"
 
@@ -725,6 +726,7 @@ static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *g
 
 static int config_input(AVFilterLink *inlink)
 {
+    FilterLink *l = ff_filter_link(inlink);
     AVFilterContext *ctx = inlink->dst;
     FIREqualizerContext *s = ctx->priv;
     float iscale, scale = 1.f;
@@ -824,7 +826,7 @@ static int config_input(AVFilterLink *inlink)
            inlink->sample_rate, inlink->ch_layout.nb_channels, s->analysis_rdft_len, s->rdft_len, s->fir_len, s->nsamples_max);
 
     if (s->fixed)
-        inlink->min_samples = inlink->max_samples = s->nsamples_max;
+        l->min_samples = l->max_samples = s->nsamples_max;
 
     return generate_kernel(ctx, SELECT_GAIN(s), SELECT_GAIN_ENTRY(s));
 }
diff --git a/libavfilter/af_lv2.c b/libavfilter/af_lv2.c
index a5980d5e9c..f9425a5828 100644
--- a/libavfilter/af_lv2.c
+++ b/libavfilter/af_lv2.c
@@ -34,6 +34,7 @@
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "avfilter.h"
+#include "filters.h"
 #include "formats.h"
 #include "internal.h"
 
@@ -381,7 +382,7 @@ static int config_output(AVFilterLink *outlink)
         (lilv_plugin_has_feature(s->plugin, s->powerOf2BlockLength) ||
          lilv_plugin_has_feature(s->plugin, s->fixedBlockLength) ||
          lilv_plugin_has_feature(s->plugin, s->boundedBlockLength))) {
-        AVFilterLink *inlink = ctx->inputs[0];
+        FilterLink *inlink = ff_filter_link(ctx->inputs[0]);
 
         inlink->min_samples = inlink->max_samples = 4096;
     }
diff --git a/libavfilter/af_replaygain.c b/libavfilter/af_replaygain.c
index 266121e2c0..e7b0330e6c 100644
--- a/libavfilter/af_replaygain.c
+++ b/libavfilter/af_replaygain.c
@@ -30,6 +30,7 @@
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "avfilter.h"
+#include "filters.h"
 #include "formats.h"
 #include "internal.h"
 
@@ -349,6 +350,7 @@ static int query_formats(AVFilterContext *ctx)
 
 static int config_input(AVFilterLink *inlink)
 {
+    FilterLink *l = ff_filter_link(inlink);
     AVFilterContext *ctx = inlink->dst;
     ReplayGainContext *s = ctx->priv;
     int i;
@@ -366,8 +368,8 @@ static int config_input(AVFilterLink *inlink)
 
     s->yule_hist_i   = 20;
     s->butter_hist_i = 4;
-    inlink->min_samples =
-    inlink->max_samples = inlink->sample_rate / 20;
+    l->min_samples =
+    l->max_samples = inlink->sample_rate / 20;
 
     return 0;
 }
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index 80c9cf7b51..75e9bf6724 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -1077,14 +1077,15 @@ static int samples_ready(FilterLinkInternal *link, unsigned min)
 static int take_samples(FilterLinkInternal *li, unsigned min, unsigned max,
                         AVFrame **rframe)
 {
-    AVFilterLink *link = &li->l.pub;
+    FilterLink *l = &li->l;
+    AVFilterLink *link = &l->pub;
     AVFrame *frame0, *frame, *buf;
     unsigned nb_samples, nb_frames, i, p;
     int ret;
 
     /* Note: this function relies on no format changes and must only be
        called with enough samples. */
-    av_assert1(samples_ready(li, link->min_samples));
+    av_assert1(samples_ready(li, l->min_samples));
     frame0 = frame = ff_framequeue_peek(&li->fifo, 0);
     if (!li->fifo.samples_skipped && frame->nb_samples >= min && frame->nb_samples <= max) {
         *rframe = ff_framequeue_take(&li->fifo);
@@ -1142,8 +1143,8 @@ static int ff_filter_frame_to_filter(AVFilterLink *link)
     int ret;
 
     av_assert1(ff_framequeue_queued_frames(&li->fifo));
-    ret = link->min_samples ?
-          ff_inlink_consume_samples(link, link->min_samples, link->max_samples, &frame) :
+    ret = li->l.min_samples ?
+          ff_inlink_consume_samples(link, li->l.min_samples, li->l.max_samples, &frame) :
           ff_inlink_consume_frame(link, &frame);
     av_assert1(ret);
     if (ret < 0) {
@@ -1218,8 +1219,8 @@ static int ff_filter_activate_default(AVFilterContext *filter)
     }
 
     for (i = 0; i < filter->nb_inputs; i++) {
-        if (samples_ready(ff_link_internal(filter->inputs[i]),
-                          filter->inputs[i]->min_samples)) {
+        FilterLinkInternal *li = ff_link_internal(filter->inputs[i]);
+        if (samples_ready(li, li->l.min_samples)) {
             return ff_filter_frame_to_filter(filter->inputs[i]);
         }
     }
diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h
index a34e61f23c..2624b0cfca 100644
--- a/libavfilter/avfilter.h
+++ b/libavfilter/avfilter.h
@@ -625,21 +625,6 @@ struct AVFilterLink {
      */
     AVRational frame_rate;
 
-    /**
-     * Minimum number of samples to filter at once. If filter_frame() is
-     * called with fewer samples, it will accumulate them in fifo.
-     * This field and the related ones must not be changed after filtering
-     * has started.
-     * If 0, all related fields are ignored.
-     */
-    int min_samples;
-
-    /**
-     * Maximum number of samples to filter at once. If filter_frame() is
-     * called with more samples, it will split them.
-     */
-    int max_samples;
-
     /**
      * Number of past frames sent through the link.
      */
diff --git a/libavfilter/buffersink.c b/libavfilter/buffersink.c
index e05bd0a573..2c1fa4c293 100644
--- a/libavfilter/buffersink.c
+++ b/libavfilter/buffersink.c
@@ -119,7 +119,8 @@ static int get_frame_internal(AVFilterContext *ctx, AVFrame *frame, int flags, i
 
 int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
 {
-    return get_frame_internal(ctx, frame, flags, ctx->inputs[0]->min_samples);
+    return get_frame_internal(ctx, frame, flags,
+                              ff_filter_link(ctx->inputs[0])->min_samples);
 }
 
 int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
@@ -163,7 +164,7 @@ static int activate(AVFilterContext *ctx)
 
 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
 {
-    AVFilterLink *inlink = ctx->inputs[0];
+    FilterLink *inlink = ff_filter_link(ctx->inputs[0]);
 
     inlink->min_samples = inlink->max_samples = frame_size;
 }
diff --git a/libavfilter/filters.h b/libavfilter/filters.h
index 2c856fead7..11064aee13 100644
--- a/libavfilter/filters.h
+++ b/libavfilter/filters.h
@@ -41,8 +41,29 @@
  */
 typedef struct FilterLink {
     AVFilterLink pub;
+
+    /**
+     * Minimum number of samples to filter at once.
+     *
+     * May be set by the link destination filter in its config_props().
+     * If 0, all related fields are ignored.
+     */
+    int min_samples;
+
+    /**
+     * Maximum number of samples to filter at once. If filter_frame() is
+     * called with more samples, it will split them.
+     *
+     * May be set by the link destination filter in its config_props().
+     */
+    int max_samples;
 } FilterLink;
 
+static inline FilterLink* ff_filter_link(AVFilterLink *link)
+{
+    return (FilterLink*)link;
+}
+
 /**
  * Mark a filter ready and schedule it for activation.
  *
-- 
2.43.0



More information about the ffmpeg-devel mailing list