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