[FFmpeg-devel] [PATCH 08/10] avfilter: Avoid allocation of AVFilterInternal

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Wed Aug 11 04:17:51 EEST 2021


This can be achieved by allocating it together with AVFilterContext,
with the public AVFilterContext as first element in the new structure.
Given that said structure is now more than just the internal, it has
been renamed to FFFilterContext.
Accessing it is not type-safe, so this unsafety has been confined to
a single function.

This break ABI due to the removal of the AVFilterContext.internal
pointer.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavfilter/avfilter.c | 15 ++++++---------
 libavfilter/avfilter.h |  7 -------
 libavfilter/internal.h | 12 +++++++++---
 3 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index 7e48ff14c5..6925d99659 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -623,14 +623,16 @@ static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, voi
 AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
 {
     AVFilterContext *ret;
+    FFFilterContext *ctx;
     int preinited = 0;
 
     if (!filter)
         return NULL;
 
-    ret = av_mallocz(sizeof(AVFilterContext));
-    if (!ret)
+    ctx = av_mallocz(sizeof(*ctx));
+    if (!ctx)
         return NULL;
+    ret = &ctx->pub;
 
     ret->av_class = &avfilter_class;
     ret->filter   = filter;
@@ -652,10 +654,7 @@ AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
         av_opt_set_defaults(ret->priv);
     }
 
-    ret->internal = av_mallocz(sizeof(*ret->internal));
-    if (!ret->internal)
-        goto err;
-    ret->internal->execute = default_execute;
+    ctx->execute = default_execute;
 
     ret->nb_inputs = avfilter_pad_count(filter->inputs);
     if (ret->nb_inputs ) {
@@ -695,7 +694,6 @@ err:
         av_freep(&ret->priv);
     }
     av_opt_free(ret);
-    av_freep(&ret->internal);
     av_free(ret);
     return NULL;
 }
@@ -759,7 +757,6 @@ void avfilter_free(AVFilterContext *filter)
     av_expr_free(filter->enable);
     filter->enable = NULL;
     av_freep(&filter->var_values);
-    av_freep(&filter->internal);
     av_free(filter);
 }
 
@@ -870,7 +867,7 @@ int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
         ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
         filtergraph(ctx->graph)->thread_execute) {
         ctx->thread_type       = AVFILTER_THREAD_SLICE;
-        ctx->internal->execute = filtergraph(ctx->graph)->thread_execute;
+        filtercontext(ctx)->execute = filtergraph(ctx->graph)->thread_execute;
     } else {
         ctx->thread_type = 0;
     }
diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h
index 4436eb7d2d..26a849f955 100644
--- a/libavfilter/avfilter.h
+++ b/libavfilter/avfilter.h
@@ -327,8 +327,6 @@ typedef struct AVFilter {
  */
 #define AVFILTER_THREAD_SLICE (1 << 0)
 
-typedef struct AVFilterInternal AVFilterInternal;
-
 /** An instance of a filter */
 struct AVFilterContext {
     const AVClass *av_class;        ///< needed for av_log() and filters common options
@@ -367,11 +365,6 @@ struct AVFilterContext {
      */
     int thread_type;
 
-    /**
-     * An opaque struct for libavfilter internal use.
-     */
-    AVFilterInternal *internal;
-
     struct AVFilterCommand *command_queue;
 
     char *enable_str;               ///< enable expression string
diff --git a/libavfilter/internal.h b/libavfilter/internal.h
index 48a911f082..040ce6dee8 100644
--- a/libavfilter/internal.h
+++ b/libavfilter/internal.h
@@ -134,14 +134,20 @@ static av_always_inline FFFilterGraph *filtergraph(AVFilterGraph *graph)
     return (FFFilterGraph*)graph;
 }
 
-struct AVFilterInternal {
+typedef struct FFFilterContext {
+    AVFilterContext        pub;
     avfilter_execute_func *execute;
-};
+} FFFilterContext;
+
+static av_always_inline FFFilterContext *filtercontext(AVFilterContext *ctx)
+{
+    return (FFFilterContext*)ctx;
+}
 
 static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func,
                                               void *arg, int *ret, int nb_jobs)
 {
-    return ctx->internal->execute(ctx, func, arg, ret, nb_jobs);
+    return filtercontext(ctx)->execute(ctx, func, arg, ret, nb_jobs);
 }
 
 /**
-- 
2.30.2



More information about the ffmpeg-devel mailing list