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