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