]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavfilter/graphparser.c
Cosmetics: more function reordering
[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 "graphparser.h"
27 #include "avfilter.h"
28 #include "avfiltergraph.h"
29
30 static int link_filter(AVFilterContext *src, int srcpad,
31                        AVFilterContext *dst, int dstpad,
32                        AVClass *log_ctx)
33 {
34     if(avfilter_link(src, srcpad, dst, dstpad)) {
35         av_log(log_ctx, AV_LOG_ERROR,
36                "cannot create the link %s:%d -> %s:%d\n",
37                src->filter->name, srcpad, dst->filter->name, dstpad);
38         return -1;
39     }
40
41     return 0;
42 }
43
44 static int consume_whitespace(const char *buf)
45 {
46     return strspn(buf, " \n\t");
47 }
48
49 /**
50  * Consumes a string from *buf.
51  * @return a copy of the consumed string, which should be free'd after use
52  */
53 static char *consume_string(const char **buf)
54 {
55     char *out = av_malloc(strlen(*buf) + 1);
56     char *ret = out;
57
58     *buf += consume_whitespace(*buf);
59
60     do{
61         char c = *(*buf)++;
62         switch (c) {
63         case '\\':
64             *out++ = *(*buf)++;
65             break;
66         case '\'':
67             while(**buf && **buf != '\'')
68                 *out++ = *(*buf)++;
69             if(**buf) (*buf)++;
70             break;
71         case 0:
72         case ']':
73         case '[':
74         case '=':
75         case ',':
76         case ';':
77         case ' ':
78         case '\n':
79             *out++ = 0;
80             break;
81         default:
82             *out++ = c;
83         }
84     } while(out[-1]);
85
86     (*buf)--;
87     *buf += consume_whitespace(*buf);
88
89     return ret;
90 }
91
92 /**
93  * Parse "[linkname]"
94  * @arg name a pointer (that need to be free'd after use) to the name between
95  *           parenthesis
96  */
97 static void parse_link_name(const char **buf, char **name, AVClass *log_ctx)
98 {
99     const char *start = *buf;
100     (*buf)++;
101
102     *name = consume_string(buf);
103
104     if(!*name[0]) {
105         av_log(log_ctx, AV_LOG_ERROR,
106                "Bad (empty?) label found in the following: \"%s\".\n", start);
107         goto fail;
108     }
109
110     if(*(*buf)++ != ']') {
111         av_log(log_ctx, AV_LOG_ERROR,
112                "Mismatched '[' found in the following: \"%s\".\n", start);
113     fail:
114         av_freep(name);
115     }
116 }
117
118 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
119                                       const char *name, const char *args,
120                                       AVClass *log_ctx)
121 {
122     AVFilterContext *filt;
123
124     AVFilter *filterdef;
125     char inst_name[30];
126
127     snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
128
129     if(!(filterdef = avfilter_get_by_name(name))) {
130         av_log(log_ctx, AV_LOG_ERROR,
131                "no such filter: '%s'\n", name);
132         return NULL;
133     }
134
135     if(!(filt = avfilter_open(filterdef, inst_name))) {
136         av_log(log_ctx, AV_LOG_ERROR,
137                "error creating filter '%s'\n", name);
138         return NULL;
139     }
140
141     if(avfilter_graph_add_filter(ctx, filt) < 0)
142         return NULL;
143
144     if(avfilter_init_filter(filt, args, NULL)) {
145         av_log(log_ctx, AV_LOG_ERROR,
146                "error initializing filter '%s' with args '%s'\n", name, args);
147         return NULL;
148     }
149
150     return filt;
151 }
152
153 /**
154  * Parse "filter=params"
155  * @arg name a pointer (that need to be free'd after use) to the name of the
156  *           filter
157  * @arg ars  a pointer (that need to be free'd after use) to the args of the
158  *           filter
159  */
160 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
161                                      int index, AVClass *log_ctx)
162 {
163     char *opts;
164     char *name = consume_string(buf);
165
166     if(**buf == '=') {
167         (*buf)++;
168         opts = consume_string(buf);
169     } else {
170         opts = NULL;
171     }
172
173     return create_filter(graph, index, name, opts, log_ctx);
174 }
175
176 static void free_inout(AVFilterInOut *head)
177 {
178     while(head) {
179         AVFilterInOut *next = head->next;
180         av_free(head);
181         head = next;
182     }
183 }
184
185 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
186 {
187     AVFilterInOut *ret;
188
189     while(*links && strcmp((*links)->name, label))
190         links = &((*links)->next);
191
192     ret = *links;
193
194     if(ret)
195         *links = ret->next;
196
197     return ret;
198 }
199
200
201 static int link_filter_inouts(AVFilterContext *filter,
202                               AVFilterInOut **currInputs,
203                               AVFilterInOut **openLinks, AVClass *log_ctx)
204 {
205     int pad = 0;
206
207     pad = filter->input_count;
208     while(pad--) {
209         AVFilterInOut *p = *currInputs;
210         *currInputs = (*currInputs)->next;
211         if(!p) {
212             av_log(log_ctx, AV_LOG_ERROR,
213                    "Not enough inputs specified for the \"%s\" filter.\n",
214                    filter->name);
215             return -1;
216         }
217
218         if(p->filter) {
219             if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
220                 return -1;
221             av_free(p);
222         } else {
223             p->filter = filter;
224             p->pad_idx = pad;
225             p->next = *openLinks;
226             *openLinks = p;
227         }
228     }
229
230
231     if(*currInputs) {
232         av_log(log_ctx, AV_LOG_ERROR,
233                "Too many inputs specified for the \"%s\" filter.\n",
234                filter->name);
235         return -1;
236     }
237
238     pad = filter->output_count;
239     while(pad--) {
240         AVFilterInOut *currlinkn = av_malloc(sizeof(AVFilterInOut));
241         currlinkn->name    = NULL;
242         currlinkn->type    = LinkTypeOut;
243         currlinkn->filter  = filter;
244         currlinkn->pad_idx = pad;
245         currlinkn->next    = *currInputs;
246         *currInputs = currlinkn;
247     }
248
249     return 0;
250 }
251
252 static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
253                         AVFilterInOut **openLinks, AVClass *log_ctx)
254 {
255     int pad = 0;
256
257     while(**buf == '[') {
258         char *name;
259         AVFilterInOut *link_to_add;
260         AVFilterInOut *match;
261
262         parse_link_name(buf, &name, log_ctx);
263
264         if(!name)
265             return -1;
266
267         /* First check if the label is not in the openLinks list */
268         match = extract_inout(name, openLinks);
269
270         if(match) {
271             /* A label of a open link. Make it one of the inputs of the next
272                filter */
273             if(match->type != LinkTypeOut) {
274                 av_log(log_ctx, AV_LOG_ERROR,
275                        "Label \"%s\" appears twice as input!\n", match->name);
276                 return -1;
277             }
278
279             link_to_add = match;
280         } else {
281             /* Not in the list, so add it as an input */
282             link_to_add = av_malloc(sizeof(AVFilterInOut));
283
284             link_to_add->name    = name;
285             link_to_add->type    = LinkTypeIn;
286             link_to_add->filter  = NULL;
287             link_to_add->pad_idx = pad;
288         }
289         link_to_add->next = *currInputs;
290         *currInputs = link_to_add;
291         *buf += consume_whitespace(*buf);
292         pad++;
293     }
294
295     return pad;
296 }
297
298 static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
299                          AVFilterInOut **openLinks, AVClass *log_ctx)
300 {
301     int pad = 0;
302
303     while(**buf == '[') {
304         char *name;
305         AVFilterInOut *match;
306
307         AVFilterInOut *input = *currInputs;
308         *currInputs = (*currInputs)->next;
309
310         parse_link_name(buf, &name, log_ctx);
311
312         if(!name)
313             return -1;
314
315         /* First check if the label is not in the openLinks list */
316         match = extract_inout(name, openLinks);
317
318         if(match) {
319             /* A label of a open link. Link it. */
320             if(match->type != LinkTypeIn) {
321                 av_log(log_ctx, AV_LOG_ERROR,
322                        "Label \"%s\" appears twice as output!\n", match->name);
323                 return -1;
324             }
325
326             if(link_filter(input->filter, input->pad_idx,
327                            match->filter, match->pad_idx, log_ctx) < 0)
328                 return -1;
329             av_free(match);
330             av_free(input);
331         } else {
332             /* Not in the list, so add the first input as a openLink */
333             input->next = *openLinks;
334             input->type = LinkTypeOut;
335             input->name = name;
336             *openLinks = input;
337         }
338         *buf += consume_whitespace(*buf);
339         pad++;
340     }
341
342     return pad;
343 }
344
345 /**
346  * Parse a string describing a filter graph.
347  */
348 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
349                          AVFilterInOut *openLinks, AVClass *log_ctx)
350 {
351     int index = 0;
352     char chr = 0;
353     int pad = 0;
354
355     AVFilterInOut *currInputs = NULL;
356
357     do {
358         AVFilterContext *filter;
359         filters += consume_whitespace(filters);
360
361         pad = parse_inputs(&filters, &currInputs, &openLinks, log_ctx);
362
363         if(pad < 0)
364             goto fail;
365
366         if(!(filter = parse_filter(&filters, graph, index, log_ctx)))
367             goto fail;
368
369         if(filter->input_count == 1 && !currInputs && !index) {
370             // First input can be ommitted if it is "[in]"
371             const char *tmp = "[in]";
372             pad = parse_inputs(&tmp, &currInputs, &openLinks, log_ctx);
373             if(pad < 0)
374                 goto fail;
375         }
376
377         if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
378             goto fail;
379
380         pad = parse_outputs(&filters, &currInputs, &openLinks, log_ctx);
381
382         if(pad < 0)
383             goto fail;
384
385         filters += consume_whitespace(filters);
386         chr = *filters++;
387
388         if(chr == ';' && currInputs) {
389             av_log(log_ctx, AV_LOG_ERROR,
390                    "Could not find a output to link when parsing \"%s\"\n",
391                    filters - 1);
392             goto fail;
393         }
394         index++;
395     } while(chr == ',' || chr == ';');
396
397     if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
398         // Last output can be ommitted if it is "[out]"
399         const char *tmp = "[out]";
400         if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
401             goto fail;
402     }
403
404     return 0;
405
406  fail:
407     avfilter_destroy_graph(graph);
408     free_inout(openLinks);
409     free_inout(currInputs);
410     return -1;
411 }