]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
Allow code to pass data to filters it creates.
[ffmpeg] / libavfilter / avfiltergraph.c
1 /*
2  * Filter graphs
3  * copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <string.h>
23 #include <stddef.h>
24
25 #include "avstring.h"
26 #include "avfilter.h"
27 #include "avfiltergraph.h"
28
29 struct AVFilterGraph {
30     unsigned filter_count;
31     AVFilterContext **filters;
32 };
33
34 AVFilterGraph *avfilter_create_graph(void)
35 {
36     return av_mallocz(sizeof(AVFilterGraph));
37 }
38
39 static void destroy_graph_filters(AVFilterGraph *graph)
40 {
41     for(; graph->filter_count > 0; graph->filter_count --)
42         avfilter_destroy(graph->filters[graph->filter_count - 1]);
43     av_freep(&graph->filters);
44 }
45
46 void avfilter_destroy_graph(AVFilterGraph *graph)
47 {
48     destroy_graph_filters(graph);
49     av_free(graph);
50 }
51
52 void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
53 {
54     graph->filters = av_realloc(graph->filters,
55                                 sizeof(AVFilterContext*) * ++graph->filter_count);
56     graph->filters[graph->filter_count - 1] = filter;
57 }
58
59 static AVFilterContext *create_filter_with_args(const char *filt, void *opaque)
60 {
61     AVFilterContext *ret;
62     char *filter = av_strdup(filt); /* copy - don't mangle the input string */
63     char *name, *args;
64
65     name = filter;
66     if((args = strchr(filter, '='))) {
67         /* ensure we at least have a name */
68         if(args == filter)
69             goto fail;
70
71         *args ++ = 0;
72     }
73
74     av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
75            name, args ? args : "(none)");
76
77     if((ret = avfilter_create_by_name(name, NULL))) {
78         if(avfilter_init_filter(ret, args, opaque)) {
79             av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
80             avfilter_destroy(ret);
81             goto fail;
82         }
83     } else av_log(NULL, AV_LOG_ERROR, "error creating filter!\n");
84
85     return ret;
86
87 fail:
88     av_free(filter);
89     return NULL;
90 }
91
92 int avfilter_graph_load_chain(AVFilterGraph *graph,
93                               unsigned count, char **filter_list, void **opaque,
94                               AVFilterContext **first, AVFilterContext **last)
95 {
96     unsigned i;
97     AVFilterContext *filters[2] = {NULL,NULL};
98
99     for(i = 0; i < count; i ++) {
100         void *op;
101
102         if(opaque) op = opaque[i];
103         else       op = NULL;
104
105         if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
106             goto fail;
107         if(i == 0) {
108             if(first) *first = filters[1];
109         } else {
110             if(avfilter_link(filters[0], 0, filters[1], 0)) {
111                 av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
112                 goto fail;
113             }
114         }
115         avfilter_graph_add_filter(graph, filters[1]);
116         filters[0] = filters[1];
117     }
118
119     if(last) *last = filters[1];
120     return 0;
121
122 fail:
123     destroy_graph_filters(graph);
124     if(first) *first = NULL;
125     if(last)  *last  = NULL;
126     return -1;
127 }
128