]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavfilter/graphparser.c
103887b11b827c8ca3b7db5a578aad25ab6718c4
[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 "avfilter.h"
27 #include "avfiltergraph.h"
28
29 /**
30  * For use in av_log
31  */
32 static const char *log_name(void *p)
33 {
34     return "Filter parser";
35 }
36
37 static const AVClass filter_parser_class = {
38     "Filter parser",
39     log_name
40 };
41
42 static const AVClass *log_ctx = &filter_parser_class;
43
44 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
45                                       const char *name, const char *args)
46 {
47     AVFilterContext *filt;
48
49     AVFilter *filterdef;
50     char inst_name[30];
51
52     snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
53
54     if(!(filterdef = avfilter_get_by_name(name))) {
55         av_log(&log_ctx, AV_LOG_ERROR,
56                "no such filter: '%s'\n", name);
57         return NULL;
58     }
59
60     if(!(filt = avfilter_open(filterdef, inst_name))) {
61         av_log(&log_ctx, AV_LOG_ERROR,
62                "error creating filter '%s'\n", name);
63         return NULL;
64     }
65
66     if(avfilter_graph_add_filter(ctx, filt) < 0)
67         return NULL;
68
69     if(avfilter_init_filter(filt, args, NULL)) {
70         av_log(&log_ctx, AV_LOG_ERROR,
71                "error initializing filter '%s' with args '%s'\n", name, args);
72         return NULL;
73     }
74
75     return filt;
76 }
77
78 static int link_filter(AVFilterContext *src, int srcpad,
79                        AVFilterContext *dst, int dstpad)
80 {
81     if(avfilter_link(src, srcpad, dst, dstpad)) {
82         av_log(&log_ctx, AV_LOG_ERROR,
83                "cannot create the link %s:%d -> %s:%d\n",
84                src->filter->name, srcpad, dst->filter->name, dstpad);
85         return -1;
86     }
87
88     return 0;
89 }
90
91 static void consume_whitespace(const char **buf)
92 {
93     *buf += strspn(*buf, " \n\t");
94 }
95
96 /**
97  * Consumes a string from *buf.
98  * @return a copy of the consumed string, which should be free'd after use
99  */
100 static char *consume_string(const char **buf)
101 {
102     char *out = av_malloc(strlen(*buf) + 1);
103     char *ret = out;
104
105     consume_whitespace(buf);
106
107     do{
108         char c = *(*buf)++;
109         switch (c) {
110         case '\\':
111             *out++= *(*buf)++;
112             break;
113         case '\'':
114             while(**buf && **buf != '\'')
115                 *out++= *(*buf)++;
116             if(**buf) (*buf)++;
117             break;
118         case 0:
119         case ']':
120         case '[':
121         case '=':
122         case ',':
123         case ' ':
124         case '\n':
125             *out++= 0;
126             break;
127         default:
128             *out++= c;
129         }
130     } while(out[-1]);
131
132     (*buf)--;
133     consume_whitespace(buf);
134
135     return ret;
136 }
137
138 /**
139  * Parse "[linkname]"
140  * @arg name a pointer (that need to be free'd after use) to the name between
141  *           parenthesis
142  */
143 static void parse_link_name(const char **buf, char **name)
144 {
145     const char *start = *buf;
146     (*buf)++;
147
148     *name = consume_string(buf);
149
150     if(!*name[0]) {
151         av_log(&log_ctx, AV_LOG_ERROR,
152                "Bad (empty?) label found in the following: \"%s\".\n", start);
153         goto fail;
154     }
155
156     if(*(*buf)++ != ']') {
157         av_log(&log_ctx, AV_LOG_ERROR,
158                "Mismatched '[' found in the following: \"%s\".\n", start);
159     fail:
160         av_freep(name);
161     }
162 }
163
164 /**
165  * Parse "filter=params"
166  * @arg name a pointer (that need to be free'd after use) to the name of the
167  *           filter
168  * @arg ars  a pointer (that need to be free'd after use) to the args of the
169  *           filter
170  */
171 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
172 {
173     char *name, *opts;
174     name = consume_string(buf);
175
176     if(**buf == '=') {
177         (*buf)++;
178         opts = consume_string(buf);
179     } else {
180         opts = NULL;
181     }
182
183     return create_filter(graph, index, name, opts);
184 }
185
186 enum LinkType {
187     LinkTypeIn,
188     LinkTypeOut,
189 };
190
191 /**
192  * A linked-list of the inputs/outputs of the filter chain.
193  */
194 typedef struct AVFilterInOut {
195     enum LinkType type;
196     char *name;
197     AVFilterContext *filter;
198     int pad_idx;
199
200     struct AVFilterInOut *next;
201 } AVFilterInOut;
202
203 static void free_inout(AVFilterInOut *head)
204 {
205     while (head) {
206         AVFilterInOut *next = head->next;
207         av_free(head);
208         head = next;
209     }
210 }
211
212 /**
213  * Process a link. This funcion looks for a matching label in the *inout
214  * linked list. If none is found, it adds this link to the list.
215  */
216 static int handle_link(char *name, AVFilterInOut **inout, int pad,
217                        enum LinkType type, AVFilterContext *filter)
218 {
219     AVFilterInOut *p = *inout;
220
221     for (; p && strcmp(p->name, name); p = p->next);
222
223     if(!p) {
224         // First label apearence, add it to the linked list
225         AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
226
227         inoutn->name    = name;
228         inoutn->type    = type;
229         inoutn->filter  = filter;
230         inoutn->pad_idx = pad;
231         inoutn->next    = *inout;
232         *inout = inoutn;
233          return 0;
234     }
235
236     if(p->type == LinkTypeIn && type == LinkTypeOut) {
237         if(link_filter(filter, pad, p->filter, p->pad_idx) < 0)
238             goto fail;
239     } else if(p->type == LinkTypeOut && type == LinkTypeIn) {
240         if(link_filter(p->filter, p->pad_idx, filter, pad) < 0)
241             goto fail;
242     } else {
243         av_log(&log_ctx, AV_LOG_ERROR,
244                "Two links named '%s' are either both input or both output\n",
245                name);
246         goto fail;
247     }
248
249     p->filter = NULL;
250
251     return 0;
252  fail:
253     return -1;
254 }
255
256
257 /**
258  * Parse "[a1][link2] ... [etc]"
259  */
260 static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
261                         enum LinkType type, AVFilterContext *filter)
262 {
263     while (**buf == '[') {
264         char *name;
265
266         parse_link_name(buf, &name);
267
268         if(!name)
269             return -1;
270
271         handle_link(name, inout, pad++, type, filter);
272         consume_whitespace(buf);
273     }
274     return pad;
275 }
276
277 static const char *skip_inouts(const char *buf)
278 {
279     while (*buf == '[') {
280         buf += strcspn(buf, "]") + 1;
281         consume_whitespace(&buf);
282     }
283     return buf;
284 }
285
286
287 /**
288  * Parse a string describing a filter graph.
289  */
290 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
291                          AVFilterContext *in, int inpad,
292                          AVFilterContext *out, int outpad)
293 {
294     AVFilterInOut *inout=NULL;
295     AVFilterInOut  *head=NULL;
296
297     int index = 0;
298     char chr = 0;
299     int pad = 0;
300     int has_out = 0;
301
302     AVFilterContext *last_filt = NULL;
303
304     do {
305         AVFilterContext *filter;
306         int oldpad = pad;
307         const char *inouts;
308
309         consume_whitespace(&filters);
310         inouts = filters;
311
312         // We need to parse the inputs of the filter after we create it, so
313         // skip it by now
314         filters = skip_inouts(filters);
315
316         if(!(filter = parse_filter(&filters, graph, index)))
317             goto fail;
318
319         pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
320
321         if(pad < 0)
322             goto fail;
323
324         // If the first filter has an input and none was given, it is
325         // implicitly the input of the whole graph.
326         if(pad == 0 && filter->input_count == 1) {
327             if(link_filter(in, inpad, filter, 0))
328                 goto fail;
329         }
330
331         if(chr == ',') {
332             if(link_filter(last_filt, oldpad, filter, 0) < 0)
333                 goto fail;
334         }
335
336         pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
337
338         if(pad < 0)
339             goto fail;
340
341         consume_whitespace(&filters);
342
343         chr = *filters++;
344         index++;
345         last_filt = filter;
346     } while (chr == ',' || chr == ';');
347
348     head = inout;
349     // Process remaining labels. Only inputs and outputs should be left.
350     for (; inout; inout = inout->next) {
351         if(!inout->filter)
352             continue; // Already processed
353
354         if(!strcmp(inout->name, "in")) {
355             if(link_filter(in, inpad, inout->filter, inout->pad_idx))
356                 goto fail;
357
358         } else if(!strcmp(inout->name, "out")) {
359             has_out = 1;
360
361             if(link_filter(inout->filter, inout->pad_idx, out, outpad))
362                 goto fail;
363
364         } else {
365             av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
366                    inout->name);
367                 goto fail;
368         }
369     }
370
371     free_inout(head);
372
373     if(!has_out) {
374         if(link_filter(last_filt, pad, out, outpad))
375             goto fail;
376     }
377
378     return 0;
379
380  fail:
381     free_inout(head);
382     avfilter_destroy_graph(graph);
383     return -1;
384 }