]> git.sesse.net Git - ffmpeg/blob - libavfilter/graphparser.c
794501ebe0bf1a8611d69172edcd389f34274db5
[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         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             av_free(name);
285         } else {
286             /* Not in the list, so add it as an input */
287             match = av_mallocz(sizeof(AVFilterInOut));
288             match->name    = name;
289             match->type    = LinkTypeIn;
290             match->pad_idx = pad;
291         }
292
293         insert_inout(currInputs, match);
294
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->name);
332             av_free(name);
333             av_free(match);
334             av_free(input);
335         } else {
336             /* Not in the list, so add the first input as a openLink */
337             input->next = *openLinks;
338             input->type = LinkTypeOut;
339             input->name = name;
340             insert_inout(openOutputs, input);
341         }
342         *buf += consume_whitespace(*buf);
343         pad++;
344     }
345
346     return pad;
347 }
348
349 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
350                          AVFilterInOut *openLinks, AVClass *log_ctx)
351 {
352     int index = 0;
353     char chr = 0;
354
355     AVFilterInOut *currInputs = NULL;
356
357     do {
358         AVFilterContext *filter;
359         filters += consume_whitespace(filters);
360
361         if(parse_inputs(&filters, &currInputs, &openLinks, log_ctx) < 0)
362             goto fail;
363
364         filter = parse_filter(&filters, graph, index, log_ctx);
365
366         if(!filter)
367             goto fail;
368
369         if(filter->input_count == 1 && !currInputs && !index) {
370             /* First input can be ommitted if it is "[in]" */
371             const char *tmp = "[in]";
372             if(parse_inputs(&tmp, &currInputs, &openLinks, log_ctx))
373                 goto fail;
374         }
375
376         if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
377             goto fail;
378
379         if(parse_outputs(&filters, &currInputs, &openLinks, log_ctx))
380             goto fail;
381
382         filters += consume_whitespace(filters);
383         chr = *filters++;
384
385         if(chr == ';' && currInputs) {
386             av_log(log_ctx, AV_LOG_ERROR,
387                    "Could not find a output to link when parsing \"%s\"\n",
388                    filters - 1);
389             goto fail;
390         }
391         index++;
392     } while(chr == ',' || chr == ';');
393
394     if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
395         /* Last output can be ommitted if it is "[out]" */
396         const char *tmp = "[out]";
397         if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
398             goto fail;
399     }
400
401     return 0;
402
403  fail:
404     avfilter_destroy_graph(graph);
405     free_inout(openLinks);
406     free_inout(currInputs);
407     return -1;
408 }