]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavfilter/graphparser.c
Factor common code out of if
[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
183
184     while(*links && strcmp((*links)->name, label))
185         links= &((*links)->next);
186
187     ret= *links;
188
189     if(ret)
190         *links= ret->next;
191
192     return ret;
193 }
194
195
196 static int link_filter_inouts(AVFilterContext *filter,
197                               AVFilterInOut **currInputs,
198                               AVFilterInOut **openLinks, AVClass *log_ctx)
199 {
200     AVFilterInOut *p;
201     int pad = 0;
202
203     pad = filter->input_count;
204     while(pad--) {
205         p = *currInputs;
206         if(!p) {
207             av_log(log_ctx, AV_LOG_ERROR,
208                    "Not enough inputs specified for the \"%s\" filter.\n",
209                    filter->name);
210             return -1;
211         }
212
213         if(p->filter) {
214             if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
215                 return -1;
216             *currInputs = (*currInputs)->next;
217             av_free(p);
218         } else {
219             *currInputs = (*currInputs)->next;
220             p->filter = filter;
221             p->pad_idx = pad;
222             p->next = *openLinks;
223             *openLinks = p;
224         }
225     }
226
227
228     if(*currInputs) {
229         av_log(log_ctx, AV_LOG_ERROR,
230                "Too many inputs specified for the \"%s\" filter.\n",
231                filter->name);
232         return -1;
233     }
234
235     pad = filter->output_count;
236     while(pad--) {
237         AVFilterInOut *currlinkn = av_malloc(sizeof(AVFilterInOut));
238         currlinkn->name    = NULL;
239         currlinkn->type    = LinkTypeOut;
240         currlinkn->filter  = filter;
241         currlinkn->pad_idx = pad;
242         currlinkn->next    = *currInputs;
243         *currInputs = currlinkn;
244     }
245
246     return 0;
247 }
248
249 /**
250  * Parse "filter=params"
251  * @arg name a pointer (that need to be free'd after use) to the name of the
252  *           filter
253  * @arg ars  a pointer (that need to be free'd after use) to the args of the
254  *           filter
255  */
256 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
257                                      int index, AVClass *log_ctx)
258 {
259     char *opts;
260     char *name = consume_string(buf);
261
262     if(**buf == '=') {
263         (*buf)++;
264         opts = consume_string(buf);
265     } else {
266         opts = NULL;
267     }
268
269     return create_filter(graph, index, name, opts, log_ctx);
270 }
271
272 static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
273                         AVFilterInOut **openLinks, AVClass *log_ctx)
274 {
275     int pad = 0;
276
277     while (**buf == '[') {
278         char *name;
279         AVFilterInOut *link_to_add;
280         AVFilterInOut *match;
281
282         parse_link_name(buf, &name, log_ctx);
283
284         if(!name)
285             return -1;
286
287         /* First check if the label is not in the openLinks list */
288         match = extract_inout(name, openLinks);
289
290         if(match) {
291             /* A label of a open link. Make it one of the inputs of the next
292                filter */
293             if (match->type != LinkTypeOut) {
294                 av_log(log_ctx, AV_LOG_ERROR,
295                        "Label \"%s\" appears twice as input!\n", match->name);
296                 return -1;
297             }
298
299             link_to_add = match;
300         } else {
301             /* Not in the list, so add it as an input */
302             link_to_add = av_malloc(sizeof(AVFilterInOut));
303
304             link_to_add->name    = name;
305             link_to_add->type    = LinkTypeIn;
306             link_to_add->filter  = NULL;
307             link_to_add->pad_idx = pad;
308         }
309         link_to_add->next = *currInputs;
310         *currInputs = link_to_add;
311         consume_whitespace(buf);
312         pad++;
313     }
314
315     return pad;
316 }
317
318 static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
319                          AVFilterInOut **openLinks, AVClass *log_ctx)
320 {
321     int pad = 0;
322
323     while (**buf == '[') {
324         char *name;
325         AVFilterInOut *match;
326
327         AVFilterInOut *input = *currInputs;
328         *currInputs = (*currInputs)->next;
329
330         parse_link_name(buf, &name, log_ctx);
331
332         if(!name)
333             return -1;
334
335         /* First check if the label is not in the openLinks list */
336         match = extract_inout(name, openLinks);
337
338         if(match) {
339             /* A label of a open link. Link it. */
340             if (match->type != LinkTypeIn) {
341                 av_log(log_ctx, AV_LOG_ERROR,
342                        "Label \"%s\" appears twice as output!\n", match->name);
343                 return -1;
344             }
345
346             if(link_filter(input->filter, input->pad_idx,
347                            match->filter, match->pad_idx, log_ctx) < 0)
348                 return -1;
349             av_free(match);
350             av_free(input);
351         } else {
352             /* Not in the list, so add the first input as a openLink */
353             input->next = *openLinks;
354             input->type = LinkTypeOut;
355             input->name = name;
356             *openLinks = input;
357         }
358         consume_whitespace(buf);
359         pad++;
360     }
361
362     return pad;
363 }
364
365 /**
366  * Parse a string describing a filter graph.
367  */
368 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
369                          AVFilterContext *in, int inpad,
370                          AVFilterContext *out, int outpad,
371                          AVClass *log_ctx)
372 {
373     int index = 0;
374     char chr = 0;
375     int pad = 0;
376
377     AVFilterInOut *currInputs=NULL;
378     AVFilterInOut *openLinks  = av_malloc(sizeof(AVFilterInOut));
379
380     openLinks->name = "in";
381     openLinks->filter = in;
382     openLinks->type = LinkTypeOut;
383     openLinks->pad_idx = inpad;
384     openLinks->next = av_malloc(sizeof(AVFilterInOut));
385
386     openLinks->next->name = "out";
387     openLinks->next->filter = out;
388     openLinks->next->type = LinkTypeIn;
389     openLinks->next->pad_idx = outpad;
390     openLinks->next->next = NULL;
391
392     do {
393         AVFilterContext *filter;
394         consume_whitespace(&filters);
395
396         pad = parse_inputs(&filters, &currInputs, &openLinks, log_ctx);
397
398         if(pad < 0)
399             goto fail;
400
401         if(!(filter = parse_filter(&filters, graph, index, log_ctx)))
402             goto fail;
403
404         if(filter->input_count == 1 && !currInputs && !index) {
405             // First input can be ommitted if it is "[in]"
406             const char *tmp = "[in]";
407             pad = parse_inputs(&tmp, &currInputs, &openLinks, log_ctx);
408             if (pad < 0)
409                 goto fail;
410         }
411
412         if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
413             goto fail;
414
415         pad = parse_outputs(&filters, &currInputs, &openLinks, log_ctx);
416
417         if(pad < 0)
418             goto fail;
419
420         consume_whitespace(&filters);
421         chr = *filters++;
422
423         if (chr == ';' && currInputs) {
424             av_log(log_ctx, AV_LOG_ERROR,
425                    "Could not find a output to link when parsing \"%s\"\n",
426                    filters - 1);
427             goto fail;
428         }
429         index++;
430     } while (chr == ',' || chr == ';');
431
432     if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
433         // Last output can be ommitted if it is "[out]"
434         const char *tmp = "[out]";
435         if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
436             goto fail;
437     }
438
439     return 0;
440
441  fail:
442     avfilter_destroy_graph(graph);
443     free_inout(openLinks);
444     free_inout(currInputs);
445     return -1;
446 }