]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavfilter/graphparser.c
ea2270f935cb8ca4f01f7bd72be32373b2bc0ffd
[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  * @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         avfilter_destroy(filt);
149         return NULL;
150     }
151
152     if(avfilter_init_filter(filt, args, NULL)) {
153         av_log(log_ctx, AV_LOG_ERROR,
154                "error initializing filter '%s' with args '%s'\n", name, args);
155         return NULL;
156     }
157
158     return filt;
159 }
160
161 /**
162  * Parse "filter=params"
163  */
164 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
165                                      int index, AVClass *log_ctx)
166 {
167     char *opts = NULL;
168     char *name = consume_string(buf);
169     AVFilterContext *ret;
170
171     if(**buf == '=') {
172         (*buf)++;
173         opts = consume_string(buf);
174     }
175
176     ret = create_filter(graph, index, name, opts, log_ctx);
177     av_free(name);
178     av_free(opts);
179     return ret;
180 }
181
182 static void free_inout(AVFilterInOut *head)
183 {
184     while(head) {
185         AVFilterInOut *next = head->next;
186         av_free(head->name);
187         av_free(head);
188         head = next;
189     }
190 }
191
192 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
193 {
194     AVFilterInOut *ret;
195
196     while(*links && strcmp((*links)->name, label))
197         links = &((*links)->next);
198
199     ret = *links;
200
201     if(ret)
202         *links = ret->next;
203
204     return ret;
205 }
206
207 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
208 {
209     element->next = *inouts;
210     *inouts = element;
211 }
212
213 static int link_filter_inouts(AVFilterContext *filter,
214                               AVFilterInOut **currInputs,
215                               AVFilterInOut **openLinks, AVClass *log_ctx)
216 {
217     int pad = filter->input_count;
218
219     while(pad--) {
220         AVFilterInOut *p = *currInputs;
221         if(!p) {
222             av_log(log_ctx, AV_LOG_ERROR,
223                    "Not enough inputs specified for the \"%s\" filter.\n",
224                    filter->filter->name);
225             return -1;
226         }
227
228         *currInputs = (*currInputs)->next;
229
230         if(p->filter) {
231             if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
232                 return -1;
233             av_free(p->name);
234             av_free(p);
235         } else {
236             p->filter = filter;
237             p->pad_idx = pad;
238             insert_inout(openInputs, p);
239         }
240     }
241
242     if(*currInputs) {
243         av_log(log_ctx, AV_LOG_ERROR,
244                "Too many inputs specified for the \"%s\" filter.\n",
245                filter->filter->name);
246         return -1;
247     }
248
249     pad = filter->output_count;
250     while(pad--) {
251         AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
252         currlinkn->type    = LinkTypeOut;
253         currlinkn->filter  = filter;
254         currlinkn->pad_idx = pad;
255         insert_inout(currInputs, currlinkn);
256     }
257
258     return 0;
259 }
260
261 static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
262                         AVFilterInOut **openLinks, AVClass *log_ctx)
263 {
264     int pad = 0;
265
266     while(**buf == '[') {
267         char *name = parse_link_name(buf, log_ctx);
268         AVFilterInOut *match;
269
270         if(!name)
271             return -1;
272
273         /* First check if the label is not in the openLinks list */
274         match = extract_inout(name, openLinks);
275
276         if(match) {
277             /* A label of a open link. Make it one of the inputs of the next
278                filter */
279             if(match->type != LinkTypeOut) {
280                 av_log(log_ctx, AV_LOG_ERROR,
281                        "Label \"%s\" appears twice as input!\n", match->name);
282                 return -1;
283             }
284         } else {
285             /* Not in the list, so add it as an input */
286             match = av_mallocz(sizeof(AVFilterInOut));
287             match->name    = name;
288             match->type    = LinkTypeIn;
289             match->pad_idx = pad;
290         }
291
292         insert_inout(currInputs, match);
293
294         *buf += consume_whitespace(*buf);
295         pad++;
296     }
297
298     return pad;
299 }
300
301 static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
302                          AVFilterInOut **openLinks, AVClass *log_ctx)
303 {
304     int pad = 0;
305
306     while(**buf == '[') {
307         char *name = parse_link_name(buf, log_ctx);
308         AVFilterInOut *match;
309
310         AVFilterInOut *input = *currInputs;
311         *currInputs = (*currInputs)->next;
312
313         if(!name)
314             return -1;
315
316         /* First check if the label is not in the openLinks list */
317         match = extract_inout(name, openLinks);
318
319         if(match) {
320             /* A label of a open link. Link it. */
321             if(match->type != LinkTypeIn) {
322                 av_log(log_ctx, AV_LOG_ERROR,
323                        "Label \"%s\" appears twice as output!\n", match->name);
324                 return -1;
325             }
326
327             if(link_filter(input->filter, input->pad_idx,
328                            match->filter, match->pad_idx, log_ctx) < 0)
329                 return -1;
330             av_free(match->name);
331             av_free(name);
332             av_free(match);
333             av_free(input);
334         } else {
335             /* Not in the list, so add the first input as a openLink */
336             input->next = *openLinks;
337             input->type = LinkTypeOut;
338             input->name = name;
339             insert_inout(openOutputs, input);
340         }
341         *buf += consume_whitespace(*buf);
342         pad++;
343     }
344
345     return pad;
346 }
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
354     AVFilterInOut *currInputs = NULL;
355
356     do {
357         AVFilterContext *filter;
358         filters += consume_whitespace(filters);
359
360         if(parse_inputs(&filters, &currInputs, &openLinks, log_ctx) < 0)
361             goto fail;
362
363         filter = parse_filter(&filters, graph, index, log_ctx);
364
365         if(!filter)
366             goto fail;
367
368         if(filter->input_count == 1 && !currInputs && !index) {
369             /* First input can be ommitted if it is "[in]" */
370             const char *tmp = "[in]";
371             if(parse_inputs(&tmp, &currInputs, &openLinks, log_ctx))
372                 goto fail;
373         }
374
375         if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
376             goto fail;
377
378         if(parse_outputs(&filters, &currInputs, &openLinks, log_ctx))
379             goto fail;
380
381         filters += consume_whitespace(filters);
382         chr = *filters++;
383
384         if(chr == ';' && currInputs) {
385             av_log(log_ctx, AV_LOG_ERROR,
386                    "Could not find a output to link when parsing \"%s\"\n",
387                    filters - 1);
388             goto fail;
389         }
390         index++;
391     } while(chr == ',' || chr == ';');
392
393     if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
394         /* Last output can be ommitted if it is "[out]" */
395         const char *tmp = "[out]";
396         if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
397             goto fail;
398     }
399
400     return 0;
401
402  fail:
403     avfilter_destroy_graph(graph);
404     free_inout(openLinks);
405     free_inout(currInputs);
406     return -1;
407 }