]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavfilter/graphparser.c
Simplify consume_string() as Michael suggested
[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                                       char *name, 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));
103     const char *in = *buf;
104     char *ret = out;
105
106     consume_whitespace(buf);
107
108     do{
109         char c = *in++;
110         switch (c) {
111         case '\\':
112             *out++= *in++;
113             break;
114         case '\'':
115             while(*in && *in != '\'')
116                 *out++= *in++;
117             if(*in) in++;
118             break;
119         case 0:
120         case ')':
121         case '(':
122         case '=':
123         case ',':
124             *out++= 0;
125             break;
126         default:
127             *out++= c;
128         }
129     } while(out[-1]);
130
131     *buf = in-1;
132     return ret;
133 }
134
135 /**
136  * Parse "(linkname)"
137  * @arg name a pointer (that need to be free'd after use) to the name between
138  *           parenthesis
139  */
140 static void parse_link_name(const char **buf, char **name)
141 {
142     (*buf)++;
143
144     *name = consume_string(buf);
145
146     if (!*name[0])
147         goto fail;
148
149     if (*(*buf)++ != ')')
150         goto fail;
151
152     return;
153  fail:
154     av_freep(name);
155     av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
156 }
157
158 /**
159  * Parse "filter=params"
160  * @arg name a pointer (that need to be free'd after use) to the name of the
161  *           filter
162  * @arg ars  a pointer (that need to be free'd after use) to the args of the
163  *           filter
164  */
165 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
166 {
167     char *name, *opts;
168     name = consume_string(buf);
169
170     if (**buf == '=') {
171         (*buf)++;
172         opts = consume_string(buf);
173     } else {
174         opts = NULL;
175     }
176
177     return create_filter(graph, index, name, opts);
178 }
179
180 enum LinkType {
181     LinkTypeIn,
182     LinkTypeOut,
183 };
184
185 /**
186  * A linked-list of the inputs/outputs of the filter chain.
187  */
188 typedef struct AVFilterInOut {
189     enum LinkType type;
190     char *name;
191     AVFilterContext *instance;
192     int pad_idx;
193
194     struct AVFilterInOut *next;
195 } AVFilterInOut;
196
197 static void free_inout(AVFilterInOut *head)
198 {
199     while (head) {
200         AVFilterInOut *next;
201         next = head->next;
202         av_free(head);
203         head = next;
204     }
205 }
206
207 /**
208  * Parse "(a1)(link2) ... (etc)"
209  */
210 static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
211                         enum LinkType type, AVFilterContext *instance)
212 {
213     int pad = firstpad;
214     while (**buf == '(') {
215         AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
216         parse_link_name(buf, &inoutn->name);
217         inoutn->type = type;
218         inoutn->instance = instance;
219         inoutn->pad_idx = pad++;
220         inoutn->next = *inout;
221         *inout = inoutn;
222     }
223     return pad;
224 }
225
226 static const char *skip_inouts(const char *buf)
227 {
228     while (*buf == '(') {
229         buf += strcspn(buf, ")");
230         buf++;
231     }
232     return buf;
233 }
234
235
236 /**
237  * Parse a string describing a filter graph.
238  */
239 int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
240 {
241     AVFilterInOut *inout=NULL;
242     AVFilterInOut  *head=NULL;
243
244     int index = 0;
245     char chr = 0;
246     int pad = 0;
247     int has_out = 0;
248
249     AVFilterContext *last_filt = NULL;
250
251     consume_whitespace(&filters);
252
253     do {
254         AVFilterContext *filter;
255         int oldpad = pad;
256         const char *inouts = filters;
257
258         // We need to parse the inputs of the filter after we create it, so
259         // skip it by now
260         filters = skip_inouts(filters);
261
262         if (!(filter = parse_filter(&filters, graph, index)))
263             goto fail;
264
265         pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
266
267         // If the first filter has an input and none was given, it is
268         // implicitly the input of the whole graph.
269         if (pad == 0 && filter->input_count == 1) {
270             if(link_filter(in, inpad, filter, 0))
271                 goto fail;
272         }
273
274         if(chr == ',') {
275             if (link_filter(last_filt, oldpad, filter, 0) < 0)
276                 goto fail;
277
278         }
279         pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
280         chr = *filters++;
281         index++;
282         last_filt = filter;
283     } while (chr == ',' || chr == ';');
284
285     head = inout;
286     for (; inout != NULL; inout = inout->next) {
287         if (inout->instance == NULL)
288             continue; // Already processed
289
290         if (!strcmp(inout->name, "in")) {
291             if(link_filter(in, inpad, inout->instance, inout->pad_idx))
292                 goto fail;
293
294         } else if (!strcmp(inout->name, "out")) {
295             has_out = 1;
296
297             if(link_filter(inout->instance, inout->pad_idx, out, outpad))
298                 goto fail;
299
300         } else {
301             AVFilterInOut *p, *src, *dst;
302             for (p = inout->next;
303                  p && strcmp(p->name,inout->name); p = p->next);
304
305             if (!p) {
306                 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
307                        inout->name);
308                 goto fail;
309             }
310
311             if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
312                 src = inout;
313                 dst = p;
314             } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
315                 src = p;
316                 dst = inout;
317             } else {
318                 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
319                        inout->name);
320                 goto fail;
321             }
322
323             if (link_filter(src->instance, src->pad_idx, dst->instance, dst->pad_idx) < 0)
324                 goto fail;
325
326             src->instance = NULL;
327             dst->instance = NULL;
328         }
329     }
330
331     free_inout(head);
332
333     if (!has_out) {
334         if(link_filter(last_filt, pad, out, outpad))
335             goto fail;
336     }
337
338     return 0;
339
340  fail:
341     free_inout(head);
342     avfilter_destroy_graph(graph);
343     return -1;
344 }