[FFmpeg-devel] [PATCH 2/3] avfilter/af_aformat, formats: Unify freeing AVFilterFormatsConfig

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Fri Sep 11 02:49:10 EEST 2020


AVFilterFormats and AVFilterChannelLayouts always exist together, namely
in the aformat filter as well as in AVFilterLinks. Now that they are
grouped together in a single structure in AVFilterLinks, the same can be
done in aformat and then this structure can be freed on its own,
replacing the freeing functions for AVFilterFormats and
AVFilterChannelLayouts structures (which still exist as static functions
in formats.c).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
Btw: There are several quite common combinations of functions like e.g.
ff_make_format_list() and ff_formats_ref(). Should they be factored out
in new functions or not?

 libavfilter/af_aformat.c    |  26 ++++-----
 libavfilter/avfilter.c      |   8 +--
 libavfilter/avfiltergraph.c |   8 +--
 libavfilter/formats.c       | 105 +++++++++++++++++++-----------------
 libavfilter/formats.h       |  13 ++---
 5 files changed, 76 insertions(+), 84 deletions(-)

diff --git a/libavfilter/af_aformat.c b/libavfilter/af_aformat.c
index e669f2de83..a8dbdd8147 100644
--- a/libavfilter/af_aformat.c
+++ b/libavfilter/af_aformat.c
@@ -36,9 +36,7 @@
 typedef struct AFormatContext {
     const AVClass   *class;
 
-    AVFilterFormats *formats;
-    AVFilterFormats *sample_rates;
-    AVFilterChannelLayouts *channel_layouts;
+    AVFilterFormatsConfig config;
 
     char *formats_str;
     char *sample_rates_str;
@@ -100,11 +98,11 @@ static av_cold int init(AVFilterContext *ctx)
 {
     AFormatContext *s = ctx->priv;
 
-    PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
+    PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->config.formats,
                   ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
-    PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
+    PARSE_FORMATS(s->sample_rates_str, int, s->config.samplerates, ff_add_format,
                   get_sample_rate, 0, "sample rate");
-    PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
+    PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->config.channel_layouts,
                   ff_add_channel_layout, av_get_channel_layout, 0,
                   "channel layout");
 
@@ -115,9 +113,7 @@ static av_cold void uninit(AVFilterContext *ctx)
 {
     AFormatContext *s = ctx->priv;
 
-    ff_formats_unref(&s->formats);
-    ff_formats_unref(&s->sample_rates);
-    ff_channel_layouts_unref(&s->channel_layouts);
+    ff_filter_formats_config_unref(&s->config);
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -125,19 +121,19 @@ static int query_formats(AVFilterContext *ctx)
     AFormatContext *s = ctx->priv;
     int ret;
 
-    ret = ff_set_common_formats(ctx, s->formats ? s->formats :
+    ret = ff_set_common_formats(ctx, s->config.formats ? s->config.formats :
                                             ff_all_formats(AVMEDIA_TYPE_AUDIO));
-    s->formats = NULL;
+    s->config.formats = NULL;
     if (ret < 0)
         return ret;
-    ret = ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
+    ret = ff_set_common_samplerates(ctx, s->config.samplerates ? s->config.samplerates :
                                                      ff_all_samplerates());
-    s->sample_rates = NULL;
+    s->config.samplerates = NULL;
     if (ret < 0)
         return ret;
-    ret = ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
+    ret = ff_set_common_channel_layouts(ctx, s->config.channel_layouts ? s->config.channel_layouts :
                                                             ff_all_channel_counts());
-    s->channel_layouts = NULL;
+    s->config.channel_layouts = NULL;
     return ret;
 }
 
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index 8ff22c71e3..06cf564270 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -746,12 +746,8 @@ static void free_link(AVFilterLink *link)
 
     av_buffer_unref(&link->hw_frames_ctx);
 
-    ff_formats_unref(&link->incfg.formats);
-    ff_formats_unref(&link->outcfg.formats);
-    ff_formats_unref(&link->incfg.samplerates);
-    ff_formats_unref(&link->outcfg.samplerates);
-    ff_channel_layouts_unref(&link->incfg.channel_layouts);
-    ff_channel_layouts_unref(&link->outcfg.channel_layouts);
+    ff_filter_formats_config_unref(&link->incfg);
+    ff_filter_formats_config_unref(&link->outcfg);
     avfilter_link_free(&link);
 }
 
diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index f6b572b3de..831fc2b985 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -727,12 +727,8 @@ static int pick_format(AVFilterLink *link, AVFilterLink *ref)
             link->channels = av_get_channel_layout_nb_channels(link->channel_layout);
     }
 
-    ff_formats_unref(&link->incfg.formats);
-    ff_formats_unref(&link->outcfg.formats);
-    ff_formats_unref(&link->incfg.samplerates);
-    ff_formats_unref(&link->outcfg.samplerates);
-    ff_channel_layouts_unref(&link->incfg.channel_layouts);
-    ff_channel_layouts_unref(&link->outcfg.channel_layouts);
+    ff_filter_formats_config_unref(&link->incfg);
+    ff_filter_formats_config_unref(&link->outcfg);
 
     return 0;
 }
diff --git a/libavfilter/formats.c b/libavfilter/formats.c
index c54aff688f..98ff82a887 100644
--- a/libavfilter/formats.c
+++ b/libavfilter/formats.c
@@ -255,6 +255,55 @@ int ff_merge_channel_layouts(AVFilterChannelLayouts *a,
     return 1;
 }
 
+#define FIND_REF_INDEX(ref, idx)            \
+do {                                        \
+    int i;                                  \
+    for (i = 0; i < (*ref)->refcount; i ++) \
+        if((*ref)->refs[i] == ref) {        \
+            idx = i;                        \
+            break;                          \
+        }                                   \
+} while (0)
+
+#define FORMATS_UNREF(ref, list)                                   \
+do {                                                               \
+    int idx = -1;                                                  \
+                                                                   \
+    if (!ref || !*ref)                                             \
+        return;                                                    \
+                                                                   \
+    FIND_REF_INDEX(ref, idx);                                      \
+                                                                   \
+    if (idx >= 0) {                                                \
+        memmove((*ref)->refs + idx, (*ref)->refs + idx + 1,        \
+            sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
+        --(*ref)->refcount;                                        \
+    }                                                              \
+    if (!(*ref)->refcount) {                                       \
+        av_free((*ref)->list);                                     \
+        av_free((*ref)->refs);                                     \
+        av_free(*ref);                                             \
+    }                                                              \
+    *ref = NULL;                                                   \
+} while (0)
+
+static void formats_unref(AVFilterFormats **ref)
+{
+    FORMATS_UNREF(ref, formats);
+}
+
+static void channel_layouts_unref(AVFilterChannelLayouts **ref)
+{
+    FORMATS_UNREF(ref, channel_layouts);
+}
+
+void ff_filter_formats_config_unref(AVFilterFormatsConfig *cfg)
+{
+    formats_unref(&cfg->formats);
+    formats_unref(&cfg->samplerates);
+    channel_layouts_unref(&cfg->channel_layouts);
+}
+
 int ff_fmt_is_in(int fmt, const int *fmts)
 {
     const int *p;
@@ -332,14 +381,14 @@ do {                                                        \
 
 int ff_add_format(AVFilterFormats **avff, int64_t fmt)
 {
-    ADD_FORMAT(avff, fmt, ff_formats_unref, int, formats, nb_formats);
+    ADD_FORMAT(avff, fmt, formats_unref, int, formats, nb_formats);
     return 0;
 }
 
 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
 {
     av_assert1(!(*l && (*l)->all_layouts));
-    ADD_FORMAT(l, channel_layout, ff_channel_layouts_unref, uint64_t, channel_layouts, nb_channel_layouts);
+    ADD_FORMAT(l, channel_layout, channel_layouts_unref, uint64_t, channel_layouts, nb_channel_layouts);
     return 0;
 }
 
@@ -461,54 +510,12 @@ AVFilterChannelLayouts *ff_all_channel_counts(void)
 
 int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
 {
-    FORMATS_REF(f, ref, ff_channel_layouts_unref);
+    FORMATS_REF(f, ref, channel_layouts_unref);
 }
 
 int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
 {
-    FORMATS_REF(f, ref, ff_formats_unref);
-}
-
-#define FIND_REF_INDEX(ref, idx)            \
-do {                                        \
-    int i;                                  \
-    for (i = 0; i < (*ref)->refcount; i ++) \
-        if((*ref)->refs[i] == ref) {        \
-            idx = i;                        \
-            break;                          \
-        }                                   \
-} while (0)
-
-#define FORMATS_UNREF(ref, list)                                   \
-do {                                                               \
-    int idx = -1;                                                  \
-                                                                   \
-    if (!ref || !*ref)                                             \
-        return;                                                    \
-                                                                   \
-    FIND_REF_INDEX(ref, idx);                                      \
-                                                                   \
-    if (idx >= 0) {                                                \
-        memmove((*ref)->refs + idx, (*ref)->refs + idx + 1,        \
-            sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
-        --(*ref)->refcount;                                        \
-    }                                                              \
-    if (!(*ref)->refcount) {                                       \
-        av_free((*ref)->list);                                     \
-        av_free((*ref)->refs);                                     \
-        av_free(*ref);                                             \
-    }                                                              \
-    *ref = NULL;                                                   \
-} while (0)
-
-void ff_formats_unref(AVFilterFormats **ref)
-{
-    FORMATS_UNREF(ref, formats);
-}
-
-void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
-{
-    FORMATS_UNREF(ref, channel_layouts);
+    FORMATS_REF(f, ref, formats_unref);
 }
 
 #define FORMATS_CHANGEREF(oldref, newref)       \
@@ -570,14 +577,14 @@ int ff_set_common_channel_layouts(AVFilterContext *ctx,
                                   AVFilterChannelLayouts *channel_layouts)
 {
     SET_COMMON_FORMATS(ctx, channel_layouts,
-                       ff_channel_layouts_ref, ff_channel_layouts_unref);
+                       ff_channel_layouts_ref, channel_layouts_unref);
 }
 
 int ff_set_common_samplerates(AVFilterContext *ctx,
                               AVFilterFormats *samplerates)
 {
     SET_COMMON_FORMATS(ctx, samplerates,
-                       ff_formats_ref, ff_formats_unref);
+                       ff_formats_ref, formats_unref);
 }
 
 /**
@@ -588,7 +595,7 @@ int ff_set_common_samplerates(AVFilterContext *ctx,
 int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
 {
     SET_COMMON_FORMATS(ctx, formats,
-                       ff_formats_ref, ff_formats_unref);
+                       ff_formats_ref, formats_unref);
 }
 
 int ff_default_query_formats(AVFilterContext *ctx)
diff --git a/libavfilter/formats.h b/libavfilter/formats.h
index 0520bf42a9..8a7dbb2f97 100644
--- a/libavfilter/formats.h
+++ b/libavfilter/formats.h
@@ -191,11 +191,6 @@ av_warn_unused_result
 int ff_channel_layouts_ref(AVFilterChannelLayouts *f,
                            AVFilterChannelLayouts **ref);
 
-/**
- * Remove a reference to a channel layouts list.
- */
-void ff_channel_layouts_unref(AVFilterChannelLayouts **ref);
-
 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
                                   AVFilterChannelLayouts **newref);
 
@@ -261,9 +256,11 @@ av_warn_unused_result
 int ff_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref);
 
 /**
- * If *ref is non-NULL, remove *ref as a reference to the format list
+ * If cfg->formats is non-NULL, remove it as a reference to the format list
  * it currently points to, deallocates that list if this was the last
- * reference, and sets *ref to NULL.
+ * reference, and sets cfg->formats to NULL.
+ *
+ * Also does the same for cfg->sample_rates and cfg->channel_layouts.
  *
  *         Before                                 After
  *   ________                               ________         NULL
@@ -275,7 +272,7 @@ int ff_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref);
  *  | |____| |    | |____|                 | |____| |    | |____|
  *  |________|    |_____________________   |________|    |_____________________
  */
-void ff_formats_unref(AVFilterFormats **ref);
+void ff_filter_formats_config_unref(AVFilterFormatsConfig *cfg);
 
 /**
  *         Before                                 After
-- 
2.20.1



More information about the ffmpeg-devel mailing list