[FFmpeg-devel] [PATCH 4/4] Add avfilter_graph_insert_filter().
Stefano Sabatini
stefano.sabatini-lala
Thu Oct 14 12:57:29 CEST 2010
---
ffmpeg.c | 34 +++++++++++-----------------------
ffplay.c | 14 ++++++--------
libavfilter/avfiltergraph.c | 42 ++++++++++++++++++++++++++++++++----------
libavfilter/avfiltergraph.h | 19 +++++++++++++++++++
4 files changed, 68 insertions(+), 41 deletions(-)
diff --git a/ffmpeg.c b/ffmpeg.c
index c90375a..6231eb5 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -375,22 +375,18 @@ static int configure_filters(AVInputStream *ist, AVOutputStream *ost)
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, &ffsink, "out")) < 0)
- return ret;
-
snprintf(args, 255, "%d:%d:%d:%d:%d", ist->st->codec->width,
ist->st->codec->height, ist->st->codec->pix_fmt,
ist->st->time_base.num, ist->st->time_base.den);
- if ((ret = avfilter_init_filter(ist->input_video_filter, args, NULL)) < 0)
- return ret;
- if ((ret = avfilter_init_filter(ist->output_video_filter, NULL, &ffsink_ctx)) < 0)
+ if ((ret = avfilter_graph_insert_filter(&filter, avfilter_get_by_name("buffer"),
+ "src", args, NULL, graph, NULL)) < 0)
return ret;
+ ist->input_video_filter = filter;
- /* 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 = avfilter_graph_insert_filter(&filter, &ffsink,
+ "out", NULL, &ffsink_ctx, graph, NULL)) < 0)
+ return ret;
+ ist->output_video_filter = filter;
last_filter = ist->input_video_filter;
@@ -398,14 +394,10 @@ static int configure_filters(AVInputStream *ist, AVOutputStream *ost)
snprintf(args, 255, "%d:%d:%d:%d",
codec->width, codec->height,
ost->leftBand, ost->topBand);
- 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 = avfilter_graph_insert_filter(&filter, avfilter_get_by_name("crop"),
+ NULL, args, NULL, graph, last_filter)) < 0)
return ret;
last_filter = filter;
- avfilter_graph_add_filter(graph, last_filter);
}
if((codec->width !=
@@ -415,14 +407,10 @@ 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 = avfilter_graph_insert_filter(&filter, avfilter_get_by_name("scale"),
+ NULL, args, NULL, graph, 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));
diff --git a/ffplay.c b/ffplay.c
index 9db160e..848332e 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -1819,12 +1819,12 @@ static int video_thread(void *arg)
snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%d", sws_flags);
graph->scale_sws_opts = av_strdup(sws_flags_str);
- if (avfilter_open(&filt_src, &input_filter, "src") < 0) goto the_end;
- if (avfilter_open(&filt_out, &ffsink , "out") < 0) goto the_end;
-
- if(avfilter_init_filter(filt_src, NULL, is)) goto the_end;
- if(avfilter_init_filter(filt_out, NULL, &ffsink_ctx)) goto the_end;
-
+ if ((ret = avfilter_graph_insert_filter(&filt_src, &input_filter,
+ "src", NULL, is, graph, NULL)) < 0)
+ goto the_end;
+ if ((ret = avfilter_graph_insert_filter(&filt_out, &ffsink,
+ "out", NULL, &ffsink_ctx, graph, NULL)) < 0)
+ goto the_end;
if(vfilters) {
AVFilterInOut *outputs = av_malloc(sizeof(AVFilterInOut));
@@ -1846,8 +1846,6 @@ static int video_thread(void *arg)
} else {
if(avfilter_link(filt_src, 0, filt_out, 0) < 0) goto the_end;
}
- avfilter_graph_add_filter(graph, filt_src);
- avfilter_graph_add_filter(graph, filt_out);
if (avfilter_graph_config(graph, NULL))
goto the_end;
diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index baffc51..4d122c3 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -47,6 +47,30 @@ int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
return 0;
}
+int avfilter_graph_insert_filter(AVFilterContext **filt_ctx, AVFilter *filt,
+ const char *name, const char *args, void *opaque,
+ AVFilterGraph *graph_ctx,
+ AVFilterContext *prev_filt_ctx)
+{
+ int ret;
+
+ if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
+ goto fail;
+ if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
+ goto fail;
+ if (prev_filt_ctx && (ret = avfilter_link(prev_filt_ctx, 0, *filt_ctx, 0)) < 0)
+ goto fail;
+ if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
+ goto fail;
+ return 0;
+
+fail:
+ if (*filt_ctx)
+ avfilter_destroy(*filt_ctx);
+ *filt_ctx = NULL;
+ return ret;
+}
+
int avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
{
AVFilterContext *filt;
@@ -107,7 +131,7 @@ AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
{
- int i, j;
+ int i, j, ret;
int scaler_count = 0;
char inst_name[30];
@@ -133,17 +157,15 @@ static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
/* couldn't merge format lists. auto-insert scale filter */
snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
scaler_count++);
- avfilter_open(&scale, avfilter_get_by_name("scale"), inst_name);
snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
- if(!scale || scale->filter->init(scale, scale_args, NULL) ||
- avfilter_insert_filter(link, scale, 0, 0)) {
- avfilter_destroy(scale);
- return -1;
- }
- if (avfilter_graph_add_filter(graph, scale) < 0)
- return -1;
+ if ((ret = avfilter_graph_insert_filter(&scale, avfilter_get_by_name("scale"),
+ inst_name, scale_args, NULL,
+ graph, NULL)) < 0)
+ return ret;
+ if ((ret = avfilter_insert_filter(link, scale, 0, 0)) < 0)
+ return ret;
scale->filter->query_formats(scale);
if (((link = scale-> inputs[0]) &&
diff --git a/libavfilter/avfiltergraph.h b/libavfilter/avfiltergraph.h
index efb9cc0..6c9cf33 100644
--- a/libavfilter/avfiltergraph.h
+++ b/libavfilter/avfiltergraph.h
@@ -48,6 +48,25 @@ AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name);
int avfilter_graph_add_filter(AVFilterGraph *graphctx, AVFilterContext *filter);
/**
+ * Insert a filter context into an existing graph.
+ *
+ * A filter context is created from the filter filt and inited using
+ * the parameter args and opaque.
+ * If prev_filt_ctx is non-NULL, the created filter context is linked
+ * after it.
+ *
+ * @param filt_ctx put here the created filter context in case of
+ * success, NULL otherwise
+ * @param name the instance name to give to the created filter context
+ * @param graph_ctx the filter graph
+ * @return a negative error AVERROR error code in case of failure
+ */
+int avfilter_graph_insert_filter(AVFilterContext **filt_ctx, AVFilter *filt,
+ const char *name, const char *args, void *opaque,
+ AVFilterGraph *graph_ctx,
+ AVFilterContext *prev_filt_ctx);
+
+/**
* Check for the validity of graph.
*
* A graph is considered valid if all its input and output pads are
--
1.7.1
More information about the ffmpeg-devel
mailing list