[PATCH 3/5] Implement add_filter() and use it in configure_filters(). Factorize.

Stefano Sabatini stefano.sabatini-lala
Sun Jul 25 12:26:52 CEST 2010


---
 ffmpeg.c |   56 ++++++++++++++++++++++++++++++--------------------------
 1 files changed, 30 insertions(+), 26 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index 9a2eced..1737b72 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -400,32 +400,48 @@ static AVFilter output_filter =
     .outputs   = (AVFilterPad[]) {{ .name = NULL }},
 };
 
+static int add_filter(AVFilterGraph *graph_ctx, AVFilterContext **filter_ctx,
+                      const char *name, const char *inst_name, const char *args, void *opaque,
+                      AVFilterContext *last_filter_ctx)
+{
+    AVFilter *filter;
+    int ret;
+
+    if (!(filter = avfilter_get_by_name(name)))
+        return AVERROR(EINVAL);
+    if ((ret = avfilter_open(filter_ctx, filter, inst_name)) < 0)
+        return ret;
+    if ((ret = avfilter_init_filter(*filter_ctx, args, opaque)) < 0)
+        return ret;
+    if (last_filter_ctx) {
+        if ((ret = avfilter_link(last_filter_ctx, 0, *filter_ctx, 0)) < 0)
+            return ret;
+    }
+    avfilter_graph_add_filter(graph_ctx, *filter_ctx);
+
+    return 0;
+}
+
 static int configure_filters(AVInputStream *ist, AVOutputStream *ost)
 {
-    AVFilterContext *last_filter, *filter;
+    AVFilterContext *last_filter;
     /** filter graph containing all filters including input & output */
     AVCodecContext *codec = ost->st->codec;
     AVCodecContext *icodec = ist->st->codec;
     char args[255];
     int ret;
 
-    graph = av_mallocz(sizeof(AVFilterGraph));
-
-    if ((ret = avfilter_open(&ist->input_video_filter, avfilter_get_by_name("buffer"), "src")) < 0)
-        return ret;
-    if ((ret = avfilter_open(&ist->output_video_filter, &output_filter, "out")) < 0)
+    if ((ret = avfilter_register(&output_filter)) < 0)
         return ret;
+    graph = av_mallocz(sizeof(AVFilterGraph));
 
     snprintf(args, 255, "%d:%d:%d", ist->st->codec->width,
              ist->st->codec->height, ist->st->codec->pix_fmt);
-    if ((ret = avfilter_init_filter(ist->input_video_filter, args, NULL)) < 0)
-        return ret;
-    if ((ret = avfilter_init_filter(ist->output_video_filter, NULL, &codec->pix_fmt)) < 0)
+    if ((ret = add_filter(graph, &ist->input_video_filter, "buffer", "src", args, NULL, NULL)) < 0)
         return ret;
 
-    /* add input and output filters to the overall graph */
-    avfilter_graph_add_filter(graph, ist->input_video_filter);
-    avfilter_graph_add_filter(graph, ist->output_video_filter);
+    if ((ret = add_filter(graph, &ist->output_video_filter, "ffmpeg_output", "out", NULL, &codec->pix_fmt, NULL)) < 0)
+        return ret;
 
     last_filter = ist->input_video_filter;
 
@@ -433,14 +449,8 @@ static int configure_filters(AVInputStream *ist, AVOutputStream *ost)
         snprintf(args, 255, "%d:%d:%d:%d", ost->leftBand, ost->topBand,
                  codec->width,
                  codec->height);
-        if ((ret = avfilter_open(&filter, avfilter_get_by_name("crop"), NULL)) < 0)
-            return ret;
-        if ((ret = avfilter_init_filter(filter, args, NULL)) < 0)
-            return ret;
-        if ((ret = avfilter_link(last_filter, 0, filter, 0)) < 0)
+        if ((ret = add_filter(graph, &last_filter, "crop", NULL, args, NULL, last_filter)) < 0)
             return ret;
-        last_filter = filter;
-        avfilter_graph_add_filter(graph, last_filter);
     }
 
     if((codec->width !=
@@ -450,14 +460,8 @@ static int configure_filters(AVInputStream *ist, AVOutputStream *ost)
                  codec->width,
                  codec->height,
                  (int)av_get_int(sws_opts, "sws_flags", NULL));
-        if ((ret = avfilter_open(&filter, avfilter_get_by_name("scale"), NULL)) < 0)
-            return ret;
-        if ((ret = avfilter_init_filter(filter, args, NULL)) < 0)
-            return ret;
-        if ((ret = avfilter_link(last_filter, 0, filter, 0)) < 0)
+        if ((ret = add_filter(graph, &last_filter, "scale", NULL, args, NULL, last_filter)) < 0)
             return ret;
-        last_filter = filter;
-        avfilter_graph_add_filter(graph, last_filter);
     }
 
     snprintf(args, sizeof(args), "flags=0x%X", (int)av_get_int(sws_opts, "sws_flags", NULL));
-- 
1.7.0.4


--WhfpMioaduB5tiZL--



More information about the ffmpeg-devel mailing list