Read extended channel configuration when extended AOT is BSAC.
[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 121static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
e4a5f397 122 const char *filt_name, const char *args,
8e74c889
VS
123 AVClass *log_ctx)
124{
e4a5f397 125 AVFilterContext *filt_ctx;
8e74c889 126
e4a5f397 127 AVFilter *filt;
8e74c889
VS
128 char inst_name[30];
129
130 snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
131
e4a5f397 132 filt = avfilter_get_by_name(filt_name);
5e600185 133
e4a5f397 134 if(!filt) {
8e74c889 135 av_log(log_ctx, AV_LOG_ERROR,
e4a5f397 136 "no such filter: '%s'\n", filt_name);
8e74c889
VS
137 return NULL;
138 }
139
e4a5f397
SS
140 filt_ctx = avfilter_open(filt, inst_name);
141 if(!filt_ctx) {
8e74c889 142 av_log(log_ctx, AV_LOG_ERROR,
e4a5f397 143 "error creating filter '%s'\n", filt_name);
8e74c889
VS
144 return NULL;
145 }
146
e4a5f397
SS
147 if(avfilter_graph_add_filter(ctx, filt_ctx) < 0) {
148 avfilter_destroy(filt_ctx);
8e74c889 149 return NULL;
64b164f4 150 }
8e74c889 151
e4a5f397 152 if(avfilter_init_filter(filt_ctx, args, NULL)) {
8e74c889 153 av_log(log_ctx, AV_LOG_ERROR,
e4a5f397 154 "error initializing filter '%s' with args '%s'\n", filt_name, args);
8e74c889
VS
155 return NULL;
156 }
157
e4a5f397 158 return filt_ctx;
8e74c889
VS
159}
160
f5cbde2e
VS
161/**
162 * Parse "filter=params"
f5cbde2e
VS
163 */
164static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
165 int index, AVClass *log_ctx)
166{
ba3fed2f 167 char *opts = NULL;
f5cbde2e 168 char *name = consume_string(buf);
64b164f4 169 AVFilterContext *ret;
f5cbde2e 170
12849837 171 if(**buf == '=') {
f5cbde2e
VS
172 (*buf)++;
173 opts = consume_string(buf);
12849837 174 }
f5cbde2e 175
64b164f4
VS
176 ret = create_filter(graph, index, name, opts, log_ctx);
177 av_free(name);
178 av_free(opts);
179 return ret;
f5cbde2e
VS
180}
181
27afb09d
VS
182static void free_inout(AVFilterInOut *head)
183{
487c0e67 184 while(head) {
55672c83 185 AVFilterInOut *next = head->next;
64b164f4 186 av_free(head->name);
27afb09d
VS
187 av_free(head);
188 head = next;
189 }
190}
191
c9987633
VS
192static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
193{
194 AVFilterInOut *ret;
c9987633 195
fec2e513 196 while(*links && strcmp((*links)->name, label))
f6557d5e 197 links = &((*links)->next);
c9987633 198
f6557d5e 199 ret = *links;
c9987633 200
fec2e513 201 if(ret)
f6557d5e 202 *links = ret->next;
c9987633
VS
203
204 return ret;
205}
206
e97908ee
VS
207static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
208{
209 element->next = *inouts;
210 *inouts = element;
211}
c9987633
VS
212
213static int link_filter_inouts(AVFilterContext *filter,
7f9b3266
SS
214 AVFilterInOut **curr_inputs,
215 AVFilterInOut **open_inputs, AVClass *log_ctx)
c9987633 216{
90ed076c 217 int pad = filter->input_count;
c9987633 218
4fd9d074 219 while(pad--) {
7f9b3266 220 AVFilterInOut *p = *curr_inputs;
c9987633
VS
221 if(!p) {
222 av_log(log_ctx, AV_LOG_ERROR,
223 "Not enough inputs specified for the \"%s\" filter.\n",
089d3714 224 filter->filter->name);
c9987633
VS
225 return -1;
226 }
227
7f9b3266 228 *curr_inputs = (*curr_inputs)->next;
4d11beb2 229
c9987633
VS
230 if(p->filter) {
231 if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
232 return -1;
64b164f4 233 av_free(p->name);
c9987633
VS
234 av_free(p);
235 } else {
c9987633
VS
236 p->filter = filter;
237 p->pad_idx = pad;
7f9b3266 238 insert_inout(open_inputs, p);
c9987633
VS
239 }
240 }
241
7f9b3266 242 if(*curr_inputs) {
c9987633
VS
243 av_log(log_ctx, AV_LOG_ERROR,
244 "Too many inputs specified for the \"%s\" filter.\n",
089d3714 245 filter->filter->name);
c9987633
VS
246 return -1;
247 }
248
249 pad = filter->output_count;
4fd9d074 250 while(pad--) {
c956dd43 251 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
c9987633
VS
252 currlinkn->filter = filter;
253 currlinkn->pad_idx = pad;
7f9b3266 254 insert_inout(curr_inputs, currlinkn);
c9987633
VS
255 }
256
257 return 0;
258}
259
7f9b3266
SS
260static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
261 AVFilterInOut **open_outputs, AVClass *log_ctx)
27afb09d 262{
c9987633 263 int pad = 0;
c9987633 264
487c0e67 265 while(**buf == '[') {
bd80b349 266 char *name = parse_link_name(buf, log_ctx);
b2ac16da 267 AVFilterInOut *match;
22260824 268
a4501887 269 if(!name)
22260824 270 return -1;
22260824 271
7f9b3266
SS
272 /* First check if the label is not in the open_outputs list */
273 match = extract_inout(name, open_outputs);
cf4f7d38 274
b2ac16da 275 if(match) {
64fbf5e2 276 av_free(name);
0de3407b
VS
277 } else {
278 /* Not in the list, so add it as an input */
c880791f
VS
279 match = av_mallocz(sizeof(AVFilterInOut));
280 match->name = name;
c880791f 281 match->pad_idx = pad;
cf4f7d38 282 }
e97908ee 283
7f9b3266 284 insert_inout(curr_inputs, match);
e97908ee 285
d424e138 286 *buf += consume_whitespace(*buf);
c9987633 287 pad++;
27afb09d 288 }
cf4f7d38 289
27afb09d
VS
290 return pad;
291}
292
7f9b3266
SS
293static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
294 AVFilterInOut **open_inputs,
295 AVFilterInOut **open_outputs, AVClass *log_ctx)
9710beaf 296{
c9987633
VS
297 int pad = 0;
298
487c0e67 299 while(**buf == '[') {
bd80b349 300 char *name = parse_link_name(buf, log_ctx);
c9987633
VS
301 AVFilterInOut *match;
302
7f9b3266
SS
303 AVFilterInOut *input = *curr_inputs;
304 *curr_inputs = (*curr_inputs)->next;
443c10ef 305
c9987633
VS
306 if(!name)
307 return -1;
308
7f9b3266
SS
309 /* First check if the label is not in the open_inputs list */
310 match = extract_inout(name, open_inputs);
c9987633 311
0de3407b 312 if(match) {
7baa6210 313 if(link_filter(input->filter, input->pad_idx,
c9987633
VS
314 match->filter, match->pad_idx, log_ctx) < 0)
315 return -1;
64b164f4
VS
316 av_free(match->name);
317 av_free(name);
c9987633 318 av_free(match);
7baa6210 319 av_free(input);
0de3407b 320 } else {
7f9b3266 321 /* Not in the list, so add the first input as a open_output */
7baa6210 322 input->name = name;
7f9b3266 323 insert_inout(open_outputs, input);
c9987633 324 }
d424e138 325 *buf += consume_whitespace(*buf);
c9987633 326 pad++;
8095a014 327 }
9710beaf 328
c9987633
VS
329 return pad;
330}
9710beaf 331
86a47378 332int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
7f9b3266
SS
333 AVFilterInOut *open_inputs,
334 AVFilterInOut *open_outputs, AVClass *log_ctx)
27afb09d 335{
27afb09d
VS
336 int index = 0;
337 char chr = 0;
27afb09d 338
7f9b3266 339 AVFilterInOut *curr_inputs = NULL;
27afb09d 340
27afb09d 341 do {
9710beaf 342 AVFilterContext *filter;
d424e138 343 filters += consume_whitespace(filters);
27afb09d 344
7f9b3266 345 if(parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
27afb09d
VS
346 goto fail;
347
5e600185
VS
348 filter = parse_filter(&filters, graph, index, log_ctx);
349
350 if(!filter)
22260824
VS
351 goto fail;
352
7f9b3266 353 if(filter->input_count == 1 && !curr_inputs && !index) {
e916c2ac 354 /* First input can be omitted if it is "[in]" */
c9987633 355 const char *tmp = "[in]";
7f9b3266 356 if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
27afb09d 357 goto fail;
27afb09d
VS
358 }
359
7f9b3266 360 if(link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
c9987633 361 goto fail;
da790674 362
7f9b3266 363 if(parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
e2b5fdaf 364 log_ctx) < 0)
e84f0b62
VS
365 goto fail;
366
d424e138 367 filters += consume_whitespace(filters);
27afb09d 368 chr = *filters++;
27afb09d 369
7f9b3266 370 if(chr == ';' && curr_inputs) {
c9987633
VS
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;
27afb09d 375 }
c9987633 376 index++;
487c0e67 377 } while(chr == ',' || chr == ';');
27afb09d 378
fd51ff16 379 if (chr) {
78471234
SS
380 av_log(log_ctx, AV_LOG_ERROR,
381 "Unable to parse graph description substring: \"%s\"\n",
382 filters - 1);
383 goto fail;
384 }
385
7f9b3266 386 if(open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
e916c2ac 387 /* Last output can be omitted if it is "[out]" */
c9987633 388 const char *tmp = "[out]";
7f9b3266
SS
389 if(parse_outputs(&tmp, &curr_inputs, &open_inputs,
390 &open_outputs, log_ctx) < 0)
27afb09d 391 goto fail;
27afb09d
VS
392 }
393
394 return 0;
395
396 fail:
d7dbe558 397 avfilter_graph_destroy(graph);
7f9b3266
SS
398 free_inout(open_inputs);
399 free_inout(open_outputs);
400 free_inout(curr_inputs);
27afb09d
VS
401 return -1;
402}