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 static AVFilterContext *create_filter_with_args(const char *filt, void *opaque)
55 char *filter = av_strdup(filt); /* copy - don't mangle the input string */
59 if((args = strchr(filter, '='))) {
60 /* ensure we at least have a name */
67 av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
68 name, args ? args : "(none)");
70 if((ret = avfilter_create_by_name(name, NULL))) {
71 if(avfilter_init_filter(ret, args, opaque)) {
72 av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
73 avfilter_destroy(ret);
76 } else av_log(NULL, AV_LOG_ERROR, "error creating filter!\n");
85 static int graph_load_chain(AVFilterContext *graphctx,
86 unsigned count, char **filter_list, void **opaque,
87 AVFilterContext **first, AVFilterContext **last)
90 AVFilterContext *filters[2] = {NULL,NULL};
92 for(i = 0; i < count; i ++) {
95 if(opaque) op = opaque[i];
98 if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
101 if(first) *first = filters[1];
103 if(avfilter_link(filters[0], 0, filters[1], 0)) {
104 av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
108 avfilter_graph_add_filter(graphctx, filters[1]);
109 filters[0] = filters[1];
112 if(last) *last = filters[1];
117 if(first) *first = NULL;
118 if(last) *last = NULL;
122 static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
123 AVFilterContext **first,
124 AVFilterContext **last)
130 strings = av_malloc(sizeof(char *));
131 strings[0] = av_strdup(str);
133 filt = strchr(strings[0], ',');
134 for(count = 1; filt; count ++) {
135 if(filt == strings[count-1]) {
140 strings = av_realloc(strings, sizeof(char *) * (count+1));
141 strings[count] = filt + 1;
143 filt = strchr(strings[count], ',');
146 ret = graph_load_chain(ctx, count, strings, NULL, first, last);
155 static int init(AVFilterContext *ctx, const char *args, void *opaque)
157 AVFilterContext **filters = opaque;
164 return graph_load_chain_from_string(ctx, args, filters, filters + 1);
170 .author = "Bobby Bingham",
172 .priv_size = sizeof(GraphContext),
177 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
178 .outputs = (AVFilterPad[]) {{ .name = NULL, }},