[FFmpeg-cvslog] fftools/ffmpeg_filter: add a function for creating a filtergraph

Anton Khirnov git at videolan.org
Mon Apr 24 18:03:16 EEST 2023


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Tue Apr 18 14:11:03 2023 +0200| [8b56480652c83603a877c8e72c20c30c7de5b9cf] | committer: Anton Khirnov

fftools/ffmpeg_filter: add a function for creating a filtergraph

Code creating a new filtergraph is currently duplicated in 3 places.
This commit unifies it and moves towards making filtergraphs more
self-contained.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=8b56480652c83603a877c8e72c20c30c7de5b9cf
---

 fftools/ffmpeg.h        |  8 ++++++++
 fftools/ffmpeg_filter.c | 18 +++++++++++++-----
 fftools/ffmpeg_opt.c    | 14 +++++---------
 3 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index c9d499efdd..b181d433b0 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -803,6 +803,14 @@ int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
 int ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par);
 int ifilter_has_all_input_formats(FilterGraph *fg);
 
+/**
+ * Create a new filtergraph in the global filtergraph list.
+ *
+ * @param graph_desc Graph description; an av_malloc()ed string, filtergraph
+ *                   takes ownership of it.
+ */
+FilterGraph *fg_create(char *graph_desc);
+
 int ffmpeg_parse_options(int argc, char **argv);
 
 void enc_stats_write(OutputStream *ost, EncStats *es,
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index ea182089b1..7b3d9a490f 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -188,15 +188,26 @@ static OutputFilter *ofilter_alloc(FilterGraph *fg)
     return ofilter;
 }
 
+FilterGraph *fg_create(char *graph_desc)
+{
+    FilterGraph *fg;
+
+    fg = ALLOC_ARRAY_ELEM(filtergraphs, nb_filtergraphs);
+    fg->index      = nb_filtergraphs - 1;
+    fg->graph_desc = graph_desc;
+
+    return fg;
+}
+
 int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
 {
-    FilterGraph *fg = av_mallocz(sizeof(*fg));
+    FilterGraph *fg;
     OutputFilter *ofilter;
     InputFilter  *ifilter;
 
+    fg = fg_create(NULL);
     if (!fg)
         report_and_exit(AVERROR(ENOMEM));
-    fg->index = nb_filtergraphs;
 
     ofilter      = ofilter_alloc(fg);
     ofilter->ost = ost;
@@ -212,9 +223,6 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
     if (!ifilter->frame_queue)
         report_and_exit(AVERROR(ENOMEM));
 
-    GROW_ARRAY(filtergraphs, nb_filtergraphs);
-    filtergraphs[nb_filtergraphs - 1] = fg;
-
     ist_filter_add(ist, ifilter, 1);
 
     return 0;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index f811d38b9c..c99263b6a5 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1106,26 +1106,22 @@ static int opt_audio_qscale(void *optctx, const char *opt, const char *arg)
 
 static int opt_filter_complex(void *optctx, const char *opt, const char *arg)
 {
-    FilterGraph *fg = ALLOC_ARRAY_ELEM(filtergraphs, nb_filtergraphs);
-
-    fg->index      = nb_filtergraphs - 1;
-    fg->graph_desc = av_strdup(arg);
-    if (!fg->graph_desc)
+    char *graph_desc = av_strdup(arg);
+    if (!graph_desc)
         return AVERROR(ENOMEM);
 
+    fg_create(graph_desc);
+
     return 0;
 }
 
 static int opt_filter_complex_script(void *optctx, const char *opt, const char *arg)
 {
-    FilterGraph *fg;
     char *graph_desc = file_read(arg);
     if (!graph_desc)
         return AVERROR(EINVAL);
 
-    fg = ALLOC_ARRAY_ELEM(filtergraphs, nb_filtergraphs);
-    fg->index      = nb_filtergraphs - 1;
-    fg->graph_desc = graph_desc;
+    fg_create(graph_desc);
 
     return 0;
 }



More information about the ffmpeg-cvslog mailing list