3 * copyright (c) 2007 Bobby Bingham
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "avfiltergraph.h"
29 typedef struct AVFilterGraph {
30 unsigned filter_count;
31 AVFilterContext **filters;
34 static void uninit(AVFilterContext *ctx)
36 GraphContext *graph = ctx->priv;
38 for(; graph->filter_count > 0; graph->filter_count --)
39 avfilter_destroy(graph->filters[graph->filter_count - 1]);
40 av_freep(&graph->filters);
43 void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
45 GraphContext *graph = graphctx->priv;
47 graph->filters = av_realloc(graph->filters,
48 sizeof(AVFilterContext*) * ++graph->filter_count);
49 graph->filters[graph->filter_count - 1] = filter;
52 int avfilter_graph_config_links(AVFilterContext *graphctx)
54 GraphContext *graph = graphctx->priv;
57 for(i = 0; i < graph->filter_count; i ++) {
58 for(j = 0; j < graph->filters[i]->input_count; j ++)
59 if(avfilter_config_link(graph->filters[i]->inputs[j]))
66 static AVFilterContext *create_filter_with_args(const char *filt, void *opaque)
69 char *filter = av_strdup(filt); /* copy - don't mangle the input string */
73 if((args = strchr(filter, '='))) {
74 /* ensure we at least have a name */
81 av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
82 name, args ? args : "(none)");
84 if((ret = avfilter_create_by_name(name, NULL))) {
85 if(avfilter_init_filter(ret, args, opaque)) {
86 av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
87 avfilter_destroy(ret);
90 } else av_log(NULL, AV_LOG_ERROR, "error creating filter!\n");
101 static int graph_load_chain(AVFilterContext *graphctx,
102 unsigned count, char **filter_list, void **opaque,
103 AVFilterContext **first, AVFilterContext **last)
106 AVFilterContext *filters[2] = {NULL,NULL};
108 for(i = 0; i < count; i ++) {
111 if(opaque) op = opaque[i];
114 if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
117 if(first) *first = filters[1];
119 if(avfilter_link(filters[0], 0, filters[1], 0)) {
120 av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
124 avfilter_graph_add_filter(graphctx, filters[1]);
125 filters[0] = filters[1];
128 if(last) *last = filters[1];
133 if(first) *first = NULL;
134 if(last) *last = NULL;
138 static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
139 AVFilterContext **first,
140 AVFilterContext **last)
146 strings = av_malloc(sizeof(char *));
147 strings[0] = av_strdup(str);
149 filt = strchr(strings[0], ',');
150 for(count = 1; filt; count ++) {
151 if(filt == strings[count-1]) {
156 strings = av_realloc(strings, sizeof(char *) * (count+1));
157 strings[count] = filt + 1;
159 filt = strchr(strings[count], ',');
162 ret = graph_load_chain(ctx, count, strings, NULL, first, last);
171 static int init(AVFilterContext *ctx, const char *args, void *opaque)
173 AVFilterContext **filters = opaque;
180 return graph_load_chain_from_string(ctx, args, filters, filters + 1);
186 .author = "Bobby Bingham",
188 .priv_size = sizeof(GraphContext),
193 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
194 .outputs = (AVFilterPad[]) {{ .name = NULL, }},