]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavfilter/graphparser.c
Make two variables const
[frescor/ffmpeg.git] / libavfilter / graphparser.c
1 /*
2  * filter graph parser
3  * copyright (c) 2008 Vitor Sessak
4  * copyright (c) 2007 Bobby Bingham
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <ctype.h>
24 #include <string.h>
25
26 #include "avfilter.h"
27 #include "avfiltergraph.h"
28
29 /**
30  * For use in av_log
31  */
32 static const char *log_name(void *p)
33 {
34     return "Filter parser";
35 }
36
37 static const AVClass filter_parser_class = {
38     "Filter parser",
39     log_name
40 };
41
42 static const AVClass *log_ctx = &filter_parser_class;
43
44 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
45                                       const char *name, const char *args)
46 {
47     AVFilterContext *filt;
48
49     AVFilter *filterdef;
50     char inst_name[30];
51
52     snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
53
54     if(!(filterdef = avfilter_get_by_name(name))) {
55         av_log(&log_ctx, AV_LOG_ERROR,
56                "no such filter: '%s'\n", name);
57         return NULL;
58     }
59
60     if(!(filt = avfilter_open(filterdef, inst_name))) {
61         av_log(&log_ctx, AV_LOG_ERROR,
62                "error creating filter '%s'\n", name);
63         return NULL;
64     }
65
66     if(avfilter_graph_add_filter(ctx, filt) < 0)
67         return NULL;
68
69     if(avfilter_init_filter(filt, args, NULL)) {
70         av_log(&log_ctx, AV_LOG_ERROR,
71                "error initializing filter '%s' with args '%s'\n", name, args);
72         return NULL;
73     }
74
75     return filt;
76 }
77
78 static int link_filter(AVFilterContext *src, int srcpad,
79                        AVFilterContext *dst, int dstpad)
80 {
81     if(avfilter_link(src, srcpad, dst, dstpad)) {
82         av_log(&log_ctx, AV_LOG_ERROR,
83                "cannot create the link %s:%d -> %s:%d\n",
84                src->filter->name, srcpad, dst->filter->name, dstpad);
85         return -1;
86     }
87
88     return 0;
89 }
90
91 static void consume_whitespace(const char **buf)
92 {
93     *buf += strspn(*buf, " \n\t");
94 }
95
96 /**
97  * Consumes a string from *buf.
98  * @return a copy of the consumed string, which should be free'd after use
99  */
100 static char *consume_string(const char **buf)
101 {
102     char *out = av_malloc(strlen(*buf) + 1);
103     const char *in = *buf;
104     char *ret = out;
105
106     consume_whitespace(buf);
107
108     do{
109         char c = *in++;
110         switch (c) {
111         case '\\':
112             *out++= *in++;
113             break;
114         case '\'':
115             while(*in && *in != '\'')
116                 *out++= *in++;
117             if(*in) in++;
118             break;
119         case 0:
120         case ']':
121         case '[':
122         case '=':
123         case ',':
124             *out++= 0;
125             break;
126         default:
127             *out++= c;
128         }
129     } while(out[-1]);
130
131     *buf = in-1;
132     return ret;
133 }
134
135 /**
136  * Parse "[linkname]"
137  * @arg name a pointer (that need to be free'd after use) to the name between
138  *           parenthesis
139  */
140 static void parse_link_name(const char **buf, char **name)
141 {
142     const char *start = *buf;
143     (*buf)++;
144
145     *name = consume_string(buf);
146
147     if(!*name[0]) {
148         av_log(&log_ctx, AV_LOG_ERROR,
149                "Bad (empty?) label found in the following: \"%s\".\n", start);
150         goto fail;
151     }
152
153     if(*(*buf)++ != ']') {
154         av_log(&log_ctx, AV_LOG_ERROR,
155                "Mismatched '[' found in the following: \"%s\".\n", start);
156         goto fail;
157     }
158
159     return;
160
161  fail:
162     av_freep(name);
163 }
164
165 /**
166  * Parse "filter=params"
167  * @arg name a pointer (that need to be free'd after use) to the name of the
168  *           filter
169  * @arg ars  a pointer (that need to be free'd after use) to the args of the
170  *           filter
171  */
172 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
173 {
174     char *name, *opts;
175     name = consume_string(buf);
176
177     if(**buf == '=') {
178         (*buf)++;
179         opts = consume_string(buf);
180     } else {
181         opts = NULL;
182     }
183
184     return create_filter(graph, index, name, opts);
185 }
186
187 enum LinkType {
188     LinkTypeIn,
189     LinkTypeOut,
190 };
191
192 /**
193  * A linked-list of the inputs/outputs of the filter chain.
194  */
195 typedef struct AVFilterInOut {
196     enum LinkType type;
197     char *name;
198     AVFilterContext *filter;
199     int pad_idx;
200
201     struct AVFilterInOut *next;
202 } AVFilterInOut;
203
204 static void free_inout(AVFilterInOut *head)
205 {
206     while (head) {
207         AVFilterInOut *next = head->next;
208         av_free(head);
209         head = next;
210     }
211 }
212
213 /**
214  * Parse "[a1][link2] ... [etc]"
215  */
216 static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
217                         enum LinkType type, AVFilterContext *filter)
218 {
219     while (**buf == '[') {
220         AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
221         parse_link_name(buf, &inoutn->name);
222
223         if (!inoutn->name) {
224             av_free(inoutn);
225             return -1;
226         }
227
228         inoutn->type = type;
229         inoutn->filter = filter;
230         inoutn->pad_idx = pad++;
231         inoutn->next = *inout;
232         *inout = inoutn;
233     }
234     return pad;
235 }
236
237 static const char *skip_inouts(const char *buf)
238 {
239     while (*buf == '[')
240         buf += strcspn(buf, "]") + 1;
241
242     return buf;
243 }
244
245
246 /**
247  * Parse a string describing a filter graph.
248  */
249 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
250                          AVFilterContext *in, int inpad,
251                          AVFilterContext *out, int outpad)
252 {
253     AVFilterInOut *inout=NULL;
254     AVFilterInOut  *head=NULL;
255
256     int index = 0;
257     char chr = 0;
258     int pad = 0;
259     int has_out = 0;
260
261     AVFilterContext *last_filt = NULL;
262
263     consume_whitespace(&filters);
264
265     do {
266         AVFilterContext *filter;
267         int oldpad = pad;
268         const char *inouts = filters;
269
270         // We need to parse the inputs of the filter after we create it, so
271         // skip it by now
272         filters = skip_inouts(filters);
273
274         if(!(filter = parse_filter(&filters, graph, index)))
275             goto fail;
276
277         pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
278
279         if(pad < 0)
280             goto fail;
281
282         // If the first filter has an input and none was given, it is
283         // implicitly the input of the whole graph.
284         if(pad == 0 && filter->input_count == 1) {
285             if(link_filter(in, inpad, filter, 0))
286                 goto fail;
287         }
288
289         if(chr == ',') {
290             if(link_filter(last_filt, oldpad, filter, 0) < 0)
291                 goto fail;
292         }
293
294         pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
295         chr = *filters++;
296         index++;
297         last_filt = filter;
298     } while (chr == ',' || chr == ';');
299
300     head = inout;
301     for (; inout != NULL; inout = inout->next) {
302         if(inout->filter == NULL)
303             continue; // Already processed
304
305         if(!strcmp(inout->name, "in")) {
306             if(link_filter(in, inpad, inout->filter, inout->pad_idx))
307                 goto fail;
308
309         } else if(!strcmp(inout->name, "out")) {
310             has_out = 1;
311
312             if(link_filter(inout->filter, inout->pad_idx, out, outpad))
313                 goto fail;
314
315         } else {
316             AVFilterInOut *p, *src, *dst;
317             for (p = inout->next;
318                  p && strcmp(p->name,inout->name); p = p->next);
319
320             if(!p) {
321                 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
322                        inout->name);
323                 goto fail;
324             }
325
326             if(p->type == LinkTypeIn && inout->type == LinkTypeOut) {
327                 src = inout;
328                 dst = p;
329             } else if(p->type == LinkTypeOut && inout->type == LinkTypeIn) {
330                 src = p;
331                 dst = inout;
332             } else {
333                 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
334                        inout->name);
335                 goto fail;
336             }
337
338             if(link_filter(src->filter, src->pad_idx, dst->filter, dst->pad_idx) < 0)
339                 goto fail;
340
341             src->filter = NULL;
342             dst->filter = NULL;
343         }
344     }
345
346     free_inout(head);
347
348     if(!has_out) {
349         if(link_filter(last_filt, pad, out, outpad))
350             goto fail;
351     }
352
353     return 0;
354
355  fail:
356     free_inout(head);
357     avfilter_destroy_graph(graph);
358     return -1;
359 }