[FFmpeg-devel] [PATCH 5/5] Implement add_filter() and use it in configure_filters(). Factorize.
Stefano Sabatini
stefano.sabatini-lala
Sun Jul 25 12:44:41 CEST 2010
---
ffmpeg.c | 58 ++++++++++++++++++++++++++++++----------------------------
1 files changed, 30 insertions(+), 28 deletions(-)
diff --git a/ffmpeg.c b/ffmpeg.c
index 194fdce..6d29d78 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -399,32 +399,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 (!(*filter_ctx = avfilter_open(filter, inst_name)))
+ return AVERROR(EINVAL);
+ 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;
+ if ((ret = avfilter_register(&output_filter)) < 0)
+ return ret;
graph = av_mallocz(sizeof(AVFilterGraph));
- if (!(ist->input_video_filter = avfilter_open(avfilter_get_by_name("buffer"), "src")))
- return AVERROR(EINVAL);
- if (!(ist->output_video_filter = avfilter_open(&output_filter, "out")))
- return AVERROR(EINVAL);
-
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;
@@ -432,15 +448,8 @@ static int configure_filters(AVInputStream *ist, AVOutputStream *ost)
snprintf(args, 255, "%d:%d:%d:%d", ost->leftBand, ost->topBand,
codec->width,
codec->height);
- filter = avfilter_open(avfilter_get_by_name("crop"), NULL);
- if (!filter)
- return AVERROR(EINVAL);
- if ((ret = avfilter_init_filter(filter, args, NULL)) < 0)
+ if ((ret = add_filter(graph, &last_filter, "crop", NULL, args, NULL, last_filter)) < 0)
return ret;
- if ((ret = avfilter_link(last_filter, 0, filter, 0)) < 0)
- return ret;
- last_filter = filter;
- avfilter_graph_add_filter(graph, last_filter);
}
if((codec->width !=
@@ -450,15 +459,8 @@ static int configure_filters(AVInputStream *ist, AVOutputStream *ost)
codec->width,
codec->height,
(int)av_get_int(sws_opts, "sws_flags", NULL));
- filter = avfilter_open(avfilter_get_by_name("scale"), NULL);
- if (!filter)
- return AVERROR(EINVAL);
- 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
More information about the ffmpeg-devel
mailing list