Merge variable initialization and declaration
[libav.git] / libavfilter / graphparser.c
CommitLineData
27afb09d
VS
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
6a0c770b 26#include "graphparser.h"
27afb09d
VS
27#include "avfilter.h"
28#include "avfiltergraph.h"
29
9710beaf 30static int link_filter(AVFilterContext *src, int srcpad,
3a70bb2d
VS
31 AVFilterContext *dst, int dstpad,
32 AVClass *log_ctx)
27afb09d 33{
9710beaf 34 if(avfilter_link(src, srcpad, dst, dstpad)) {
3a70bb2d 35 av_log(log_ctx, AV_LOG_ERROR,
9710beaf
VS
36 "cannot create the link %s:%d -> %s:%d\n",
37 src->filter->name, srcpad, dst->filter->name, dstpad);
27afb09d
VS
38 return -1;
39 }
40
41 return 0;
42}
43
d424e138 44static int consume_whitespace(const char *buf)
27afb09d 45{
d424e138 46 return strspn(buf, " \n\t");
27afb09d
VS
47}
48
49/**
27afb09d
VS
50 * Consumes a string from *buf.
51 * @return a copy of the consumed string, which should be free'd after use
52 */
53static char *consume_string(const char **buf)
54{
2255026d 55 char *out = av_malloc(strlen(*buf) + 1);
93b7a0f6 56 char *ret = out;
27afb09d 57
d424e138 58 *buf += consume_whitespace(*buf);
27afb09d 59
93b7a0f6 60 do{
a64821f4 61 char c = *(*buf)++;
93b7a0f6
VS
62 switch (c) {
63 case '\\':
d835680e 64 *out++ = *(*buf)++;
27afb09d 65 break;
93b7a0f6 66 case '\'':
a64821f4 67 while(**buf && **buf != '\'')
d835680e 68 *out++ = *(*buf)++;
a64821f4 69 if(**buf) (*buf)++;
93b7a0f6
VS
70 break;
71 case 0:
d7ff2297
VS
72 case ']':
73 case '[':
93b7a0f6
VS
74 case '=':
75 case ',':
dbec351d 76 case ';':
a78f2ccd
VS
77 case ' ':
78 case '\n':
d835680e 79 *out++ = 0;
93b7a0f6
VS
80 break;
81 default:
d835680e 82 *out++ = c;
93b7a0f6
VS
83 }
84 } while(out[-1]);
27afb09d 85
a64821f4 86 (*buf)--;
d424e138 87 *buf += consume_whitespace(*buf);
a78f2ccd 88
27afb09d
VS
89 return ret;
90}
91
92/**
ffac8784 93 * Parse "[linkname]"
69fa7e80
VS
94 * @param name a pointer (that need to be free'd after use) to the name between
95 * parenthesis
27afb09d 96 */
bd80b349 97static char *parse_link_name(const char **buf, AVClass *log_ctx)
27afb09d 98{
22260824 99 const char *start = *buf;
bd80b349 100 char *name;
27afb09d
VS
101 (*buf)++;
102
bd80b349 103 name = consume_string(buf);
27afb09d 104
bd80b349 105 if(!name[0]) {
3a70bb2d 106 av_log(log_ctx, AV_LOG_ERROR,
22260824 107 "Bad (empty?) label found in the following: \"%s\".\n", start);
27afb09d 108 goto fail;
22260824 109 }
27afb09d 110
22260824 111 if(*(*buf)++ != ']') {
3a70bb2d 112 av_log(log_ctx, AV_LOG_ERROR,
22260824 113 "Mismatched '[' found in the following: \"%s\".\n", start);
85cb8af7 114 fail:
bd80b349 115 av_freep(&name);
22260824 116 }
bd80b349
VS
117
118 return name;
27afb09d
VS
119}
120
8e74c889
VS
121static 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
5e600185
VS
132 filterdef = avfilter_get_by_name(name);
133
134 if(!filterdef) {
8e74c889
VS
135 av_log(log_ctx, AV_LOG_ERROR,
136 "no such filter: '%s'\n", name);
137 return NULL;
138 }
139
5e600185
VS
140 filt = avfilter_open(filterdef, inst_name);
141 if(!filt) {
8e74c889
VS
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 return NULL;
149
150 if(avfilter_init_filter(filt, args, NULL)) {
151 av_log(log_ctx, AV_LOG_ERROR,
152 "error initializing filter '%s' with args '%s'\n", name, args);
153 return NULL;
154 }
155
156 return filt;
157}
158
f5cbde2e
VS
159/**
160 * Parse "filter=params"
f5cbde2e
VS
161 */
162static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
163 int index, AVClass *log_ctx)
164{
ba3fed2f 165 char *opts = NULL;
f5cbde2e
VS
166 char *name = consume_string(buf);
167
12849837 168 if(**buf == '=') {
f5cbde2e
VS
169 (*buf)++;
170 opts = consume_string(buf);
12849837 171 }
f5cbde2e
VS
172
173 return create_filter(graph, index, name, opts, log_ctx);
174}
175
27afb09d
VS
176static void free_inout(AVFilterInOut *head)
177{
487c0e67 178 while(head) {
55672c83 179 AVFilterInOut *next = head->next;
27afb09d
VS
180 av_free(head);
181 head = next;
182 }
183}
184
c9987633
VS
185static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
186{
187 AVFilterInOut *ret;
c9987633 188
fec2e513 189 while(*links && strcmp((*links)->name, label))
f6557d5e 190 links = &((*links)->next);
c9987633 191
f6557d5e 192 ret = *links;
c9987633 193
fec2e513 194 if(ret)
f6557d5e 195 *links = ret->next;
c9987633
VS
196
197 return ret;
198}
199
e97908ee
VS
200static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
201{
202 element->next = *inouts;
203 *inouts = element;
204}
c9987633
VS
205
206static int link_filter_inouts(AVFilterContext *filter,
207 AVFilterInOut **currInputs,
208 AVFilterInOut **openLinks, AVClass *log_ctx)
209{
90ed076c 210 int pad = filter->input_count;
c9987633 211
4fd9d074 212 while(pad--) {
d835680e 213 AVFilterInOut *p = *currInputs;
eee68d96 214 *currInputs = (*currInputs)->next;
c9987633
VS
215 if(!p) {
216 av_log(log_ctx, AV_LOG_ERROR,
217 "Not enough inputs specified for the \"%s\" filter.\n",
089d3714 218 filter->filter->name);
c9987633
VS
219 return -1;
220 }
221
222 if(p->filter) {
223 if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
224 return -1;
c9987633
VS
225 av_free(p);
226 } else {
c9987633
VS
227 p->filter = filter;
228 p->pad_idx = pad;
e97908ee 229 insert_inout(openInputs, p);
c9987633
VS
230 }
231 }
232
c9987633
VS
233 if(*currInputs) {
234 av_log(log_ctx, AV_LOG_ERROR,
235 "Too many inputs specified for the \"%s\" filter.\n",
089d3714 236 filter->filter->name);
c9987633
VS
237 return -1;
238 }
239
240 pad = filter->output_count;
4fd9d074 241 while(pad--) {
c956dd43 242 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
c9987633
VS
243 currlinkn->type = LinkTypeOut;
244 currlinkn->filter = filter;
245 currlinkn->pad_idx = pad;
e97908ee 246 insert_inout(currInputs, currlinkn);
c9987633
VS
247 }
248
249 return 0;
250}
251
c9987633
VS
252static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
253 AVFilterInOut **openLinks, AVClass *log_ctx)
27afb09d 254{
c9987633 255 int pad = 0;
c9987633 256
487c0e67 257 while(**buf == '[') {
bd80b349 258 char *name = parse_link_name(buf, log_ctx);
b2ac16da 259 AVFilterInOut *match;
22260824 260
a4501887 261 if(!name)
22260824 262 return -1;
22260824 263
c9987633 264 /* First check if the label is not in the openLinks list */
b2ac16da 265 match = extract_inout(name, openLinks);
cf4f7d38 266
b2ac16da 267 if(match) {
c9987633
VS
268 /* A label of a open link. Make it one of the inputs of the next
269 filter */
d9d086d9 270 if(match->type != LinkTypeOut) {
cf4f7d38 271 av_log(log_ctx, AV_LOG_ERROR,
b2ac16da 272 "Label \"%s\" appears twice as input!\n", match->name);
cf4f7d38
VS
273 return -1;
274 }
0de3407b
VS
275 } else {
276 /* Not in the list, so add it as an input */
c880791f
VS
277 match = av_mallocz(sizeof(AVFilterInOut));
278 match->name = name;
279 match->type = LinkTypeIn;
280 match->pad_idx = pad;
cf4f7d38 281 }
e97908ee 282
c880791f 283 insert_inout(currInputs, match);
e97908ee 284
d424e138 285 *buf += consume_whitespace(*buf);
c9987633 286 pad++;
27afb09d 287 }
cf4f7d38 288
27afb09d
VS
289 return pad;
290}
291
c9987633
VS
292static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
293 AVFilterInOut **openLinks, AVClass *log_ctx)
9710beaf 294{
c9987633
VS
295 int pad = 0;
296
487c0e67 297 while(**buf == '[') {
bd80b349 298 char *name = parse_link_name(buf, log_ctx);
c9987633
VS
299 AVFilterInOut *match;
300
443c10ef
VS
301 AVFilterInOut *input = *currInputs;
302 *currInputs = (*currInputs)->next;
303
c9987633
VS
304 if(!name)
305 return -1;
306
307 /* First check if the label is not in the openLinks list */
308 match = extract_inout(name, openLinks);
309
0de3407b 310 if(match) {
c9987633 311 /* A label of a open link. Link it. */
d9d086d9 312 if(match->type != LinkTypeIn) {
c9987633
VS
313 av_log(log_ctx, AV_LOG_ERROR,
314 "Label \"%s\" appears twice as output!\n", match->name);
315 return -1;
316 }
317
7baa6210 318 if(link_filter(input->filter, input->pad_idx,
c9987633
VS
319 match->filter, match->pad_idx, log_ctx) < 0)
320 return -1;
321 av_free(match);
7baa6210 322 av_free(input);
0de3407b
VS
323 } else {
324 /* Not in the list, so add the first input as a openLink */
7baa6210
VS
325 input->next = *openLinks;
326 input->type = LinkTypeOut;
327 input->name = name;
e97908ee 328 insert_inout(openOutputs, input);
c9987633 329 }
d424e138 330 *buf += consume_whitespace(*buf);
c9987633 331 pad++;
8095a014 332 }
9710beaf 333
c9987633
VS
334 return pad;
335}
9710beaf 336
92973a04 337int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
74c2f1fb 338 AVFilterInOut *openLinks, AVClass *log_ctx)
27afb09d 339{
27afb09d
VS
340 int index = 0;
341 char chr = 0;
27afb09d 342
d835680e 343 AVFilterInOut *currInputs = NULL;
27afb09d 344
27afb09d 345 do {
9710beaf 346 AVFilterContext *filter;
d424e138 347 filters += consume_whitespace(filters);
27afb09d 348
5b31015a 349 if(parse_inputs(&filters, &currInputs, &openLinks, log_ctx) < 0)
27afb09d
VS
350 goto fail;
351
5e600185
VS
352 filter = parse_filter(&filters, graph, index, log_ctx);
353
354 if(!filter)
22260824
VS
355 goto fail;
356
c9987633 357 if(filter->input_count == 1 && !currInputs && !index) {
c185fc5d 358 /* First input can be ommitted if it is "[in]" */
c9987633 359 const char *tmp = "[in]";
5b31015a 360 if(parse_inputs(&tmp, &currInputs, &openLinks, log_ctx))
27afb09d 361 goto fail;
27afb09d
VS
362 }
363
c9987633
VS
364 if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
365 goto fail;
da790674 366
5b31015a 367 if(parse_outputs(&filters, &currInputs, &openLinks, log_ctx))
e84f0b62
VS
368 goto fail;
369
d424e138 370 filters += consume_whitespace(filters);
27afb09d 371 chr = *filters++;
27afb09d 372
d9d086d9 373 if(chr == ';' && currInputs) {
c9987633
VS
374 av_log(log_ctx, AV_LOG_ERROR,
375 "Could not find a output to link when parsing \"%s\"\n",
376 filters - 1);
377 goto fail;
27afb09d 378 }
c9987633 379 index++;
487c0e67 380 } while(chr == ',' || chr == ';');
27afb09d 381
c9987633 382 if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
c185fc5d 383 /* Last output can be ommitted if it is "[out]" */
c9987633
VS
384 const char *tmp = "[out]";
385 if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
27afb09d 386 goto fail;
27afb09d
VS
387 }
388
389 return 0;
390
391 fail:
27afb09d 392 avfilter_destroy_graph(graph);
c9987633
VS
393 free_inout(openLinks);
394 free_inout(currInputs);
27afb09d
VS
395 return -1;
396}