Use tables symetry to reduce their size by half.
[ffmpeg-lucabe.git] / libavfilter / graphparser.c
blob25c5c4d25890c4d1144be1691fbf95a347b1f97c
1 /*
2 * filter graph parser
3 * copyright (c) 2008 Vitor Sessak
4 * copyright (c) 2007 Bobby Bingham
6 * This file is part of FFmpeg.
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.
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.
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
23 #include <ctype.h>
24 #include <string.h>
26 #include "graphparser.h"
27 #include "avfilter.h"
28 #include "avfiltergraph.h"
30 static int link_filter(AVFilterContext *src, int srcpad,
31 AVFilterContext *dst, int dstpad,
32 AVClass *log_ctx)
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;
41 return 0;
44 static int consume_whitespace(const char *buf)
46 return strspn(buf, " \n\t");
49 /**
50 * Consumes a string from *buf.
51 * @return a copy of the consumed string, which should be free'd after use
53 static char *consume_string(const char **buf)
55 char *out = av_malloc(strlen(*buf) + 1);
56 char *ret = out;
58 *buf += consume_whitespace(*buf);
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;
84 } while(out[-1]);
86 (*buf)--;
87 *buf += consume_whitespace(*buf);
89 return ret;
92 /**
93 * Parse "[linkname]"
94 * @param name a pointer (that need to be free'd after use) to the name between
95 * parenthesis
97 static char *parse_link_name(const char **buf, AVClass *log_ctx)
99 const char *start = *buf;
100 char *name;
101 (*buf)++;
103 name = consume_string(buf);
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;
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);
118 return name;
121 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
122 const char *name, const char *args,
123 AVClass *log_ctx)
125 AVFilterContext *filt;
127 AVFilter *filterdef;
128 char inst_name[30];
130 snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
132 filterdef = avfilter_get_by_name(name);
134 if(!filterdef) {
135 av_log(log_ctx, AV_LOG_ERROR,
136 "no such filter: '%s'\n", name);
137 return NULL;
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;
147 if(avfilter_graph_add_filter(ctx, filt) < 0) {
148 avfilter_destroy(filt);
149 return NULL;
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;
158 return filt;
162 * Parse "filter=params"
164 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
165 int index, AVClass *log_ctx)
167 char *opts = NULL;
168 char *name = consume_string(buf);
169 AVFilterContext *ret;
171 if(**buf == '=') {
172 (*buf)++;
173 opts = consume_string(buf);
176 ret = create_filter(graph, index, name, opts, log_ctx);
177 av_free(name);
178 av_free(opts);
179 return ret;
182 static void free_inout(AVFilterInOut *head)
184 while(head) {
185 AVFilterInOut *next = head->next;
186 av_free(head->name);
187 av_free(head);
188 head = next;
192 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
194 AVFilterInOut *ret;
196 while(*links && strcmp((*links)->name, label))
197 links = &((*links)->next);
199 ret = *links;
201 if(ret)
202 *links = ret->next;
204 return ret;
207 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
209 element->next = *inouts;
210 *inouts = element;
213 static int link_filter_inouts(AVFilterContext *filter,
214 AVFilterInOut **currInputs,
215 AVFilterInOut **openInputs, AVClass *log_ctx)
217 int pad = filter->input_count;
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;
228 *currInputs = (*currInputs)->next;
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);
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;
249 pad = filter->output_count;
250 while(pad--) {
251 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
252 currlinkn->filter = filter;
253 currlinkn->pad_idx = pad;
254 insert_inout(currInputs, currlinkn);
257 return 0;
260 static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
261 AVFilterInOut **openOutputs, AVClass *log_ctx)
263 int pad = 0;
265 while(**buf == '[') {
266 char *name = parse_link_name(buf, log_ctx);
267 AVFilterInOut *match;
269 if(!name)
270 return -1;
272 /* First check if the label is not in the openOutputs list */
273 match = extract_inout(name, openOutputs);
275 if(match) {
276 av_free(name);
277 } else {
278 /* Not in the list, so add it as an input */
279 match = av_mallocz(sizeof(AVFilterInOut));
280 match->name = name;
281 match->pad_idx = pad;
284 insert_inout(currInputs, match);
286 *buf += consume_whitespace(*buf);
287 pad++;
290 return pad;
293 static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
294 AVFilterInOut **openInputs,
295 AVFilterInOut **openOutputs, AVClass *log_ctx)
297 int pad = 0;
299 while(**buf == '[') {
300 char *name = parse_link_name(buf, log_ctx);
301 AVFilterInOut *match;
303 AVFilterInOut *input = *currInputs;
304 *currInputs = (*currInputs)->next;
306 if(!name)
307 return -1;
309 /* First check if the label is not in the openInputs list */
310 match = extract_inout(name, openInputs);
312 if(match) {
313 if(link_filter(input->filter, input->pad_idx,
314 match->filter, match->pad_idx, log_ctx) < 0)
315 return -1;
316 av_free(match->name);
317 av_free(name);
318 av_free(match);
319 av_free(input);
320 } else {
321 /* Not in the list, so add the first input as a openOutput */
322 input->name = name;
323 insert_inout(openOutputs, input);
325 *buf += consume_whitespace(*buf);
326 pad++;
329 return pad;
332 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
333 AVFilterInOut *openInputs,
334 AVFilterInOut *openOutputs, AVClass *log_ctx)
336 int index = 0;
337 char chr = 0;
339 AVFilterInOut *currInputs = NULL;
341 do {
342 AVFilterContext *filter;
343 filters += consume_whitespace(filters);
345 if(parse_inputs(&filters, &currInputs, &openOutputs, log_ctx) < 0)
346 goto fail;
348 filter = parse_filter(&filters, graph, index, log_ctx);
350 if(!filter)
351 goto fail;
353 if(filter->input_count == 1 && !currInputs && !index) {
354 /* First input can be ommitted if it is "[in]" */
355 const char *tmp = "[in]";
356 if(parse_inputs(&tmp, &currInputs, &openOutputs, log_ctx) < 0)
357 goto fail;
360 if(link_filter_inouts(filter, &currInputs, &openInputs, log_ctx) < 0)
361 goto fail;
363 if(parse_outputs(&filters, &currInputs, &openInputs, &openOutputs,
364 log_ctx) < 0)
365 goto fail;
367 filters += consume_whitespace(filters);
368 chr = *filters++;
370 if(chr == ';' && currInputs) {
371 av_log(log_ctx, AV_LOG_ERROR,
372 "Could not find a output to link when parsing \"%s\"\n",
373 filters - 1);
374 goto fail;
376 index++;
377 } while(chr == ',' || chr == ';');
379 if(openInputs && !strcmp(openInputs->name, "out") && currInputs) {
380 /* Last output can be ommitted if it is "[out]" */
381 const char *tmp = "[out]";
382 if(parse_outputs(&tmp, &currInputs, &openInputs,
383 &openOutputs, log_ctx) < 0)
384 goto fail;
387 return 0;
389 fail:
390 avfilter_destroy_graph(graph);
391 free_inout(openInputs);
392 free_inout(openOutputs);
393 free_inout(currInputs);
394 return -1;