graphparser: fix the order of connecting unlabeled links.
[libav.git] / libavfilter / graphparser.c
CommitLineData
27afb09d
VS
1/*
2 * filter graph parser
3fa77bde
SS
3 * Copyright (c) 2008 Vitor Sessak
4 * Copyright (c) 2007 Bobby Bingham
27afb09d 5 *
2912e87a 6 * This file is part of Libav.
27afb09d 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
27afb09d
VS
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 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
27afb09d
VS
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
2912e87a 19 * License along with Libav; if not, write to the Free Software
27afb09d
VS
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <ctype.h>
24#include <string.h>
25
372e2884 26#include "libavutil/avstring.h"
27afb09d
VS
27#include "avfilter.h"
28#include "avfiltergraph.h"
29
fd548e5b
SS
30#define WHITESPACES " \n\t"
31
d2874a9d
SS
32/**
33 * Link two filters together.
34 *
35 * @see avfilter_link()
36 */
9710beaf 37static int link_filter(AVFilterContext *src, int srcpad,
3a70bb2d 38 AVFilterContext *dst, int dstpad,
64abd375 39 void *log_ctx)
27afb09d 40{
99ac59ca
SS
41 int ret;
42 if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
3a70bb2d 43 av_log(log_ctx, AV_LOG_ERROR,
bb90d855 44 "Cannot create the link %s:%d -> %s:%d\n",
9710beaf 45 src->filter->name, srcpad, dst->filter->name, dstpad);
99ac59ca 46 return ret;
27afb09d
VS
47 }
48
49 return 0;
50}
51
27afb09d 52/**
94b2120d
SS
53 * Parse the name of a link, which has the format "[linkname]".
54 *
55 * @return a pointer (that need to be freed after use) to the name
56 * between parenthesis
27afb09d 57 */
64abd375 58static char *parse_link_name(const char **buf, void *log_ctx)
27afb09d 59{
22260824 60 const char *start = *buf;
bd80b349 61 char *name;
27afb09d
VS
62 (*buf)++;
63
dd04911c 64 name = av_get_token(buf, "]");
27afb09d 65
f219eee5 66 if (!name[0]) {
3a70bb2d 67 av_log(log_ctx, AV_LOG_ERROR,
22260824 68 "Bad (empty?) label found in the following: \"%s\".\n", start);
27afb09d 69 goto fail;
22260824 70 }
27afb09d 71
f219eee5 72 if (*(*buf)++ != ']') {
3a70bb2d 73 av_log(log_ctx, AV_LOG_ERROR,
22260824 74 "Mismatched '[' found in the following: \"%s\".\n", start);
85cb8af7 75 fail:
bd80b349 76 av_freep(&name);
22260824 77 }
bd80b349
VS
78
79 return name;
27afb09d
VS
80}
81
98137a1a
SS
82/**
83 * Create an instance of a filter, initialize and insert it in the
84 * filtergraph in *ctx.
85 *
24c9baba 86 * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
98137a1a
SS
87 * @param ctx the filtergraph context
88 * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
89 * @param filt_name the name of the filter to create
90 * @param args the arguments provided to the filter during its initialization
91 * @param log_ctx the log context to use
c58572f8 92 * @return 0 in case of success, a negative AVERROR code otherwise
98137a1a 93 */
c58572f8 94static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
64abd375 95 const char *filt_name, const char *args, void *log_ctx)
8e74c889 96{
e4a5f397 97 AVFilter *filt;
8e74c889 98 char inst_name[30];
b5049814 99 char tmp_args[256];
c58572f8 100 int ret;
8e74c889 101
3bf3fc0e 102 snprintf(inst_name, sizeof(inst_name), "Parsed filter %d %s", index, filt_name);
8e74c889 103
e4a5f397 104 filt = avfilter_get_by_name(filt_name);
5e600185 105
f219eee5 106 if (!filt) {
8e74c889 107 av_log(log_ctx, AV_LOG_ERROR,
bb90d855 108 "No such filter: '%s'\n", filt_name);
c58572f8 109 return AVERROR(EINVAL);
8e74c889
VS
110 }
111
c58572f8
SS
112 ret = avfilter_open(filt_ctx, filt, inst_name);
113 if (!*filt_ctx) {
8e74c889 114 av_log(log_ctx, AV_LOG_ERROR,
bb90d855 115 "Error creating filter '%s'\n", filt_name);
c58572f8 116 return ret;
8e74c889
VS
117 }
118
c58572f8 119 if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
24de0edb 120 avfilter_free(*filt_ctx);
c58572f8 121 return ret;
64b164f4 122 }
8e74c889 123
f80b381b 124 if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags")) {
b5049814
BC
125 snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
126 args, ctx->scale_sws_opts);
127 args = tmp_args;
128 }
129
c58572f8 130 if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
8e74c889 131 av_log(log_ctx, AV_LOG_ERROR,
bb90d855 132 "Error initializing filter '%s' with args '%s'\n", filt_name, args);
c58572f8 133 return ret;
8e74c889
VS
134 }
135
c58572f8 136 return 0;
8e74c889
VS
137}
138
f5cbde2e 139/**
55b2a9ba
SS
140 * Parse a string of the form FILTER_NAME[=PARAMS], and create a
141 * corresponding filter instance which is added to graph with
142 * create_filter().
143 *
96c1e6d4
DB
144 * @param filt_ctx Pointer that is set to the created and configured filter
145 * context on success, set to NULL on failure.
55b2a9ba
SS
146 * @param filt_ctx put here a pointer to the created filter context on
147 * success, NULL otherwise
148 * @param buf pointer to the buffer to parse, *buf will be updated to
149 * point to the char next after the parsed string
150 * @param index an index which is assigned to the created filter
151 * instance, and which is supposed to be unique for each filter
152 * instance added to the filtergraph
153 * @return 0 in case of success, a negative AVERROR code otherwise
f5cbde2e 154 */
688b9dad 155static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
64abd375 156 int index, void *log_ctx)
f5cbde2e 157{
ba3fed2f 158 char *opts = NULL;
15a316c0 159 char *name = av_get_token(buf, "=,;[\n");
c58572f8 160 int ret;
f5cbde2e 161
f219eee5 162 if (**buf == '=') {
f5cbde2e 163 (*buf)++;
15a316c0 164 opts = av_get_token(buf, "[],;\n");
12849837 165 }
f5cbde2e 166
688b9dad 167 ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
64b164f4
VS
168 av_free(name);
169 av_free(opts);
688b9dad 170 return ret;
f5cbde2e
VS
171}
172
27afb09d
VS
173static void free_inout(AVFilterInOut *head)
174{
f219eee5 175 while (head) {
55672c83 176 AVFilterInOut *next = head->next;
64b164f4 177 av_free(head->name);
27afb09d
VS
178 av_free(head);
179 head = next;
180 }
181}
182
c9987633
VS
183static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
184{
185 AVFilterInOut *ret;
c9987633 186
d7bcc71d 187 while (*links && (!(*links)->name || strcmp((*links)->name, label)))
f6557d5e 188 links = &((*links)->next);
c9987633 189
f6557d5e 190 ret = *links;
c9987633 191
d7bcc71d 192 if (ret) {
f6557d5e 193 *links = ret->next;
d7bcc71d
AK
194 ret->next = NULL;
195 }
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 205
d7bcc71d
AK
206static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
207{
208 while (*inouts && (*inouts)->next)
209 inouts = &((*inouts)->next);
210
211 if (!*inouts)
212 *inouts = *element;
213 else
214 (*inouts)->next = *element;
215 *element = NULL;
216}
217
ed581e65 218static int link_filter_inouts(AVFilterContext *filt_ctx,
7f9b3266 219 AVFilterInOut **curr_inputs,
64abd375 220 AVFilterInOut **open_inputs, void *log_ctx)
c9987633 221{
4e781c25 222 int pad, ret;
c9987633 223
4e781c25 224 for (pad = 0; pad < filt_ctx->input_count; pad++) {
7f9b3266 225 AVFilterInOut *p = *curr_inputs;
c9987633 226
d7bcc71d
AK
227 if (p)
228 *curr_inputs = (*curr_inputs)->next;
229 else if (!(p = av_mallocz(sizeof(*p))))
230 return AVERROR(ENOMEM);
4d11beb2 231
7313132b
SS
232 if (p->filter_ctx) {
233 if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
684ade49 234 return ret;
64b164f4 235 av_free(p->name);
c9987633
VS
236 av_free(p);
237 } else {
7313132b 238 p->filter_ctx = filt_ctx;
c9987633 239 p->pad_idx = pad;
7f9b3266 240 insert_inout(open_inputs, p);
c9987633
VS
241 }
242 }
243
f219eee5 244 if (*curr_inputs) {
c9987633
VS
245 av_log(log_ctx, AV_LOG_ERROR,
246 "Too many inputs specified for the \"%s\" filter.\n",
ed581e65 247 filt_ctx->filter->name);
684ade49 248 return AVERROR(EINVAL);
c9987633
VS
249 }
250
ed581e65 251 pad = filt_ctx->output_count;
f219eee5 252 while (pad--) {
c956dd43 253 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
fbd97184
SS
254 if (!currlinkn)
255 return AVERROR(ENOMEM);
7313132b 256 currlinkn->filter_ctx = filt_ctx;
c9987633 257 currlinkn->pad_idx = pad;
7f9b3266 258 insert_inout(curr_inputs, currlinkn);
c9987633
VS
259 }
260
261 return 0;
262}
263
7f9b3266 264static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
64abd375 265 AVFilterInOut **open_outputs, void *log_ctx)
27afb09d 266{
4e781c25 267 AVFilterInOut *parsed_inputs = NULL;
c9987633 268 int pad = 0;
c9987633 269
f219eee5 270 while (**buf == '[') {
bd80b349 271 char *name = parse_link_name(buf, log_ctx);
b2ac16da 272 AVFilterInOut *match;
22260824 273
f219eee5 274 if (!name)
42e7f6d7 275 return AVERROR(EINVAL);
22260824 276
7f9b3266
SS
277 /* First check if the label is not in the open_outputs list */
278 match = extract_inout(name, open_outputs);
cf4f7d38 279
f219eee5 280 if (match) {
64fbf5e2 281 av_free(name);
0de3407b
VS
282 } else {
283 /* Not in the list, so add it as an input */
00b3ca3c
SS
284 if (!(match = av_mallocz(sizeof(AVFilterInOut))))
285 return AVERROR(ENOMEM);
c880791f 286 match->name = name;
c880791f 287 match->pad_idx = pad;
cf4f7d38 288 }
e97908ee 289
4e781c25 290 append_inout(&parsed_inputs, &match);
e97908ee 291
fd548e5b 292 *buf += strspn(*buf, WHITESPACES);
c9987633 293 pad++;
27afb09d 294 }
cf4f7d38 295
4e781c25
AK
296 append_inout(&parsed_inputs, curr_inputs);
297 *curr_inputs = parsed_inputs;
298
27afb09d
VS
299 return pad;
300}
301
7f9b3266
SS
302static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
303 AVFilterInOut **open_inputs,
64abd375 304 AVFilterInOut **open_outputs, void *log_ctx)
9710beaf 305{
0cc8b659 306 int ret, pad = 0;
c9987633 307
f219eee5 308 while (**buf == '[') {
bd80b349 309 char *name = parse_link_name(buf, log_ctx);
c9987633
VS
310 AVFilterInOut *match;
311
7f9b3266 312 AVFilterInOut *input = *curr_inputs;
668673f1
SS
313 if (!input) {
314 av_log(log_ctx, AV_LOG_ERROR,
315 "No output pad can be associated to link label '%s'.\n",
316 name);
317 return AVERROR(EINVAL);
318 }
7f9b3266 319 *curr_inputs = (*curr_inputs)->next;
443c10ef 320
f219eee5 321 if (!name)
0cc8b659 322 return AVERROR(EINVAL);
c9987633 323
7f9b3266
SS
324 /* First check if the label is not in the open_inputs list */
325 match = extract_inout(name, open_inputs);
c9987633 326
f219eee5 327 if (match) {
7313132b
SS
328 if ((ret = link_filter(input->filter_ctx, input->pad_idx,
329 match->filter_ctx, match->pad_idx, log_ctx)) < 0)
0cc8b659 330 return ret;
64b164f4
VS
331 av_free(match->name);
332 av_free(name);
c9987633 333 av_free(match);
7baa6210 334 av_free(input);
0de3407b 335 } else {
7f9b3266 336 /* Not in the list, so add the first input as a open_output */
7baa6210 337 input->name = name;
7f9b3266 338 insert_inout(open_outputs, input);
c9987633 339 }
fd548e5b 340 *buf += strspn(*buf, WHITESPACES);
c9987633 341 pad++;
8095a014 342 }
9710beaf 343
c9987633
VS
344 return pad;
345}
9710beaf 346
d7bcc71d
AK
347#if FF_API_GRAPH_AVCLASS
348#define log_ctx graph
349#else
350#define log_ctx NULL
351#endif
352int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
353 AVFilterInOut **inputs,
354 AVFilterInOut **outputs)
27afb09d 355{
c24f76b9 356 int index = 0, ret;
27afb09d 357 char chr = 0;
27afb09d 358
d7bcc71d 359 AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
27afb09d 360
27afb09d 361 do {
9710beaf 362 AVFilterContext *filter;
fd548e5b 363 filters += strspn(filters, WHITESPACES);
27afb09d 364
c24f76b9 365 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
27afb09d
VS
366 goto fail;
367
c24f76b9 368 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
22260824
VS
369 goto fail;
370
27afb09d 371
c24f76b9 372 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
c9987633 373 goto fail;
da790674 374
c24f76b9
SS
375 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
376 log_ctx)) < 0)
e84f0b62
VS
377 goto fail;
378
fd548e5b 379 filters += strspn(filters, WHITESPACES);
27afb09d 380 chr = *filters++;
27afb09d 381
d7bcc71d
AK
382 if (chr == ';' && curr_inputs)
383 append_inout(&open_outputs, &curr_inputs);
c9987633 384 index++;
f219eee5 385 } while (chr == ',' || chr == ';');
27afb09d 386
fd51ff16 387 if (chr) {
78471234
SS
388 av_log(log_ctx, AV_LOG_ERROR,
389 "Unable to parse graph description substring: \"%s\"\n",
390 filters - 1);
c24f76b9 391 ret = AVERROR(EINVAL);
78471234
SS
392 goto fail;
393 }
394
d7bcc71d 395 append_inout(&open_outputs, &curr_inputs);
27afb09d 396
d7bcc71d
AK
397 *inputs = open_inputs;
398 *outputs = open_outputs;
27afb09d
VS
399 return 0;
400
401 fail:
e8e5dde7
SS
402 for (; graph->filter_count > 0; graph->filter_count--)
403 avfilter_free(graph->filters[graph->filter_count - 1]);
404 av_freep(&graph->filters);
7f9b3266
SS
405 free_inout(open_inputs);
406 free_inout(open_outputs);
407 free_inout(curr_inputs);
d7bcc71d
AK
408
409 *inputs = NULL;
410 *outputs = NULL;
411
412 return ret;
413}
414#undef log_ctx
415
416int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
417 AVFilterInOut *open_inputs,
418 AVFilterInOut *open_outputs, void *log_ctx)
419{
420 int ret;
421 AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
422
423 if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
424 goto fail;
425
426 /* First input can be omitted if it is "[in]" */
427 if (inputs && !inputs->name)
428 inputs->name = av_strdup("in");
429 for (cur = inputs; cur; cur = cur->next) {
430 if (!cur->name) {
431 av_log(log_ctx, AV_LOG_ERROR,
432 "Not enough inputs specified for the \"%s\" filter.\n",
433 cur->filter_ctx->filter->name);
434 ret = AVERROR(EINVAL);
435 goto fail;
436 }
437 if (!(match = extract_inout(cur->name, &open_outputs)))
438 continue;
439 ret = avfilter_link(match->filter_ctx, match->pad_idx,
440 cur->filter_ctx, cur->pad_idx);
441 free_inout(match);
442 if (ret < 0)
443 goto fail;
444 }
445
446 /* Last output can be omitted if it is "[out]" */
447 if (outputs && !outputs->name)
448 outputs->name = av_strdup("out");
449 for (cur = outputs; cur; cur = cur->next) {
450 if (!cur->name) {
451 av_log(log_ctx, AV_LOG_ERROR,
452 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
453 filters);
454 ret = AVERROR(EINVAL);
455 goto fail;
456 }
457 if (!(match = extract_inout(cur->name, &open_inputs)))
458 continue;
459 ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
460 match->filter_ctx, match->pad_idx);
461 free_inout(match);
462 if (ret < 0)
463 goto fail;
464 }
465
466 fail:
467 if (ret < 0) {
468 for (; graph->filter_count > 0; graph->filter_count--)
469 avfilter_free(graph->filters[graph->filter_count - 1]);
470 av_freep(&graph->filters);
471 }
472 free_inout(inputs);
473 free_inout(outputs);
474 free_inout(open_inputs);
475 free_inout(open_outputs);
c24f76b9 476 return ret;
27afb09d 477}