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