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