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