h264_metadata: Add option to delete filler data
[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
27afb09d 23#include <string.h>
1d9c2dc8 24#include <stdio.h>
27afb09d 25
372e2884 26#include "libavutil/avstring.h"
1d9c2dc8 27#include "libavutil/mem.h"
27afb09d 28#include "avfilter.h"
27afb09d 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, "]");
9d3b752f
VG
65 if (!name)
66 goto fail;
27afb09d 67
f219eee5 68 if (!name[0]) {
3a70bb2d 69 av_log(log_ctx, AV_LOG_ERROR,
22260824 70 "Bad (empty?) label found in the following: \"%s\".\n", start);
27afb09d 71 goto fail;
22260824 72 }
27afb09d 73
f219eee5 74 if (*(*buf)++ != ']') {
3a70bb2d 75 av_log(log_ctx, AV_LOG_ERROR,
22260824 76 "Mismatched '[' found in the following: \"%s\".\n", start);
85cb8af7 77 fail:
bd80b349 78 av_freep(&name);
22260824 79 }
bd80b349
VS
80
81 return name;
27afb09d
VS
82}
83
285fe5b0
LB
84#define TMP_ARGS_SIZE 256
85
86static void append_sws_flags(const char **args, const char *sws_opts, char *tmp)
87{
88 int nb_opts = 0;
89 const char *separator = ":";
90 const char *opt = *args;
91
92 if (strstr(*args, "flags"))
93 return;
94
95 if (strstr(*args, "="))
96 separator = ":flags=";
97
98 while ((opt = strstr(opt, ":")) && *opt) {
99 av_log(NULL, AV_LOG_INFO, "opts '%s' \n", opt);
100 if (nb_opts > 2) {
101 return;
102 }
103 nb_opts++;
104 opt++;
105 }
106
107 opt = strstr(sws_opts, "flags=");
108 if (opt && strlen(opt) > 6)
109 opt += 6;
110 else
111 opt = sws_opts;
112
113 snprintf(tmp, TMP_ARGS_SIZE, "%s%s%s",
114 *args, separator, opt);
115
116 *args = tmp;
117}
118
98137a1a
SS
119/**
120 * Create an instance of a filter, initialize and insert it in the
121 * filtergraph in *ctx.
122 *
24c9baba 123 * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
98137a1a
SS
124 * @param ctx the filtergraph context
125 * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
126 * @param filt_name the name of the filter to create
127 * @param args the arguments provided to the filter during its initialization
128 * @param log_ctx the log context to use
c58572f8 129 * @return 0 in case of success, a negative AVERROR code otherwise
98137a1a 130 */
c58572f8 131static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
64abd375 132 const char *filt_name, const char *args, void *log_ctx)
8e74c889 133{
96a47364 134 const AVFilter *filt;
8e74c889 135 char inst_name[30];
285fe5b0 136 char tmp_args[TMP_ARGS_SIZE];
c58572f8 137 int ret;
8e74c889 138
3bf3fc0e 139 snprintf(inst_name, sizeof(inst_name), "Parsed filter %d %s", index, filt_name);
8e74c889 140
e4a5f397 141 filt = avfilter_get_by_name(filt_name);
5e600185 142
f219eee5 143 if (!filt) {
8e74c889 144 av_log(log_ctx, AV_LOG_ERROR,
bb90d855 145 "No such filter: '%s'\n", filt_name);
c58572f8 146 return AVERROR(EINVAL);
8e74c889
VS
147 }
148
bc1a985b 149 *filt_ctx = avfilter_graph_alloc_filter(ctx, filt, inst_name);
c58572f8 150 if (!*filt_ctx) {
8e74c889 151 av_log(log_ctx, AV_LOG_ERROR,
bb90d855 152 "Error creating filter '%s'\n", filt_name);
bc1a985b 153 return AVERROR(ENOMEM);
64b164f4 154 }
8e74c889 155
285fe5b0 156 if (!strcmp(filt_name, "scale") && args &&
6e3c13a5 157 ctx->scale_sws_opts) {
285fe5b0 158 append_sws_flags(&args, ctx->scale_sws_opts, tmp_args);
b5049814
BC
159 }
160
48a5adab
AK
161 ret = avfilter_init_str(*filt_ctx, args);
162 if (ret < 0) {
8e74c889 163 av_log(log_ctx, AV_LOG_ERROR,
c22263d3
AK
164 "Error initializing filter '%s'", filt_name);
165 if (args)
166 av_log(log_ctx, AV_LOG_ERROR, " with args '%s'", args);
167 av_log(log_ctx, AV_LOG_ERROR, "\n");
cffecc0e 168 avfilter_free(*filt_ctx);
c58572f8 169 return ret;
8e74c889
VS
170 }
171
c58572f8 172 return 0;
8e74c889
VS
173}
174
f5cbde2e 175/**
55b2a9ba
SS
176 * Parse a string of the form FILTER_NAME[=PARAMS], and create a
177 * corresponding filter instance which is added to graph with
178 * create_filter().
179 *
96c1e6d4
DB
180 * @param filt_ctx Pointer that is set to the created and configured filter
181 * context on success, set to NULL on failure.
55b2a9ba
SS
182 * @param filt_ctx put here a pointer to the created filter context on
183 * success, NULL otherwise
184 * @param buf pointer to the buffer to parse, *buf will be updated to
185 * point to the char next after the parsed string
186 * @param index an index which is assigned to the created filter
187 * instance, and which is supposed to be unique for each filter
188 * instance added to the filtergraph
189 * @return 0 in case of success, a negative AVERROR code otherwise
f5cbde2e 190 */
688b9dad 191static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
64abd375 192 int index, void *log_ctx)
f5cbde2e 193{
ba3fed2f 194 char *opts = NULL;
15a316c0 195 char *name = av_get_token(buf, "=,;[\n");
c58572f8 196 int ret;
f5cbde2e 197
f219eee5 198 if (**buf == '=') {
f5cbde2e 199 (*buf)++;
15a316c0 200 opts = av_get_token(buf, "[],;\n");
12849837 201 }
f5cbde2e 202
688b9dad 203 ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
64b164f4
VS
204 av_free(name);
205 av_free(opts);
688b9dad 206 return ret;
f5cbde2e
VS
207}
208
91d3cbe0 209AVFilterInOut *avfilter_inout_alloc(void)
27afb09d 210{
91d3cbe0
SS
211 return av_mallocz(sizeof(AVFilterInOut));
212}
213
214void avfilter_inout_free(AVFilterInOut **inout)
215{
216 while (*inout) {
217 AVFilterInOut *next = (*inout)->next;
218 av_freep(&(*inout)->name);
219 av_freep(inout);
220 *inout = next;
27afb09d
VS
221 }
222}
223
c9987633
VS
224static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
225{
226 AVFilterInOut *ret;
c9987633 227
d7bcc71d 228 while (*links && (!(*links)->name || strcmp((*links)->name, label)))
f6557d5e 229 links = &((*links)->next);
c9987633 230
f6557d5e 231 ret = *links;
c9987633 232
d7bcc71d 233 if (ret) {
f6557d5e 234 *links = ret->next;
d7bcc71d
AK
235 ret->next = NULL;
236 }
c9987633
VS
237
238 return ret;
239}
240
e97908ee
VS
241static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
242{
243 element->next = *inouts;
244 *inouts = element;
245}
c9987633 246
d7bcc71d
AK
247static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
248{
249 while (*inouts && (*inouts)->next)
250 inouts = &((*inouts)->next);
251
252 if (!*inouts)
253 *inouts = *element;
254 else
255 (*inouts)->next = *element;
256 *element = NULL;
257}
258
ed581e65 259static int link_filter_inouts(AVFilterContext *filt_ctx,
7f9b3266 260 AVFilterInOut **curr_inputs,
64abd375 261 AVFilterInOut **open_inputs, void *log_ctx)
c9987633 262{
4e781c25 263 int pad, ret;
c9987633 264
9baeff95 265 for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
7f9b3266 266 AVFilterInOut *p = *curr_inputs;
c9987633 267
aff01de6 268 if (p) {
d7bcc71d 269 *curr_inputs = (*curr_inputs)->next;
aff01de6
AK
270 p->next = NULL;
271 } else if (!(p = av_mallocz(sizeof(*p))))
d7bcc71d 272 return AVERROR(ENOMEM);
4d11beb2 273
7313132b 274 if (p->filter_ctx) {
285b706b 275 ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
64b164f4 276 av_free(p->name);
c9987633 277 av_free(p);
285b706b
JG
278 if (ret < 0)
279 return ret;
c9987633 280 } else {
7313132b 281 p->filter_ctx = filt_ctx;
c9987633 282 p->pad_idx = pad;
7af99a01 283 append_inout(open_inputs, &p);
c9987633
VS
284 }
285 }
286
f219eee5 287 if (*curr_inputs) {
c9987633
VS
288 av_log(log_ctx, AV_LOG_ERROR,
289 "Too many inputs specified for the \"%s\" filter.\n",
ed581e65 290 filt_ctx->filter->name);
684ade49 291 return AVERROR(EINVAL);
c9987633
VS
292 }
293
9baeff95 294 pad = filt_ctx->nb_outputs;
f219eee5 295 while (pad--) {
c956dd43 296 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
fbd97184
SS
297 if (!currlinkn)
298 return AVERROR(ENOMEM);
7313132b 299 currlinkn->filter_ctx = filt_ctx;
c9987633 300 currlinkn->pad_idx = pad;
7f9b3266 301 insert_inout(curr_inputs, currlinkn);
c9987633
VS
302 }
303
304 return 0;
305}
306
7f9b3266 307static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
64abd375 308 AVFilterInOut **open_outputs, void *log_ctx)
27afb09d 309{
4e781c25 310 AVFilterInOut *parsed_inputs = NULL;
c9987633 311 int pad = 0;
c9987633 312
f219eee5 313 while (**buf == '[') {
bd80b349 314 char *name = parse_link_name(buf, log_ctx);
b2ac16da 315 AVFilterInOut *match;
22260824 316
f219eee5 317 if (!name)
42e7f6d7 318 return AVERROR(EINVAL);
22260824 319
7f9b3266
SS
320 /* First check if the label is not in the open_outputs list */
321 match = extract_inout(name, open_outputs);
cf4f7d38 322
f219eee5 323 if (match) {
64fbf5e2 324 av_free(name);
0de3407b
VS
325 } else {
326 /* Not in the list, so add it as an input */
285b706b
JG
327 if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
328 av_free(name);
00b3ca3c 329 return AVERROR(ENOMEM);
285b706b 330 }
c880791f 331 match->name = name;
c880791f 332 match->pad_idx = pad;
cf4f7d38 333 }
e97908ee 334
4e781c25 335 append_inout(&parsed_inputs, &match);
e97908ee 336
fd548e5b 337 *buf += strspn(*buf, WHITESPACES);
c9987633 338 pad++;
27afb09d 339 }
cf4f7d38 340
4e781c25
AK
341 append_inout(&parsed_inputs, curr_inputs);
342 *curr_inputs = parsed_inputs;
343
27afb09d
VS
344 return pad;
345}
346
7f9b3266
SS
347static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
348 AVFilterInOut **open_inputs,
64abd375 349 AVFilterInOut **open_outputs, void *log_ctx)
9710beaf 350{
0cc8b659 351 int ret, pad = 0;
c9987633 352
f219eee5 353 while (**buf == '[') {
bd80b349 354 char *name = parse_link_name(buf, log_ctx);
c9987633
VS
355 AVFilterInOut *match;
356
7f9b3266 357 AVFilterInOut *input = *curr_inputs;
285b706b
JG
358
359 if (!name)
360 return AVERROR(EINVAL);
361
668673f1
SS
362 if (!input) {
363 av_log(log_ctx, AV_LOG_ERROR,
285b706b
JG
364 "No output pad can be associated to link label '%s'.\n", name);
365 av_free(name);
668673f1
SS
366 return AVERROR(EINVAL);
367 }
7f9b3266 368 *curr_inputs = (*curr_inputs)->next;
443c10ef 369
7f9b3266
SS
370 /* First check if the label is not in the open_inputs list */
371 match = extract_inout(name, open_inputs);
c9987633 372
f219eee5 373 if (match) {
7313132b 374 if ((ret = link_filter(input->filter_ctx, input->pad_idx,
285b706b
JG
375 match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
376 av_free(name);
0cc8b659 377 return ret;
285b706b 378 }
64b164f4
VS
379 av_free(match->name);
380 av_free(name);
c9987633 381 av_free(match);
7baa6210 382 av_free(input);
0de3407b 383 } else {
7f9b3266 384 /* Not in the list, so add the first input as a open_output */
7baa6210 385 input->name = name;
7f9b3266 386 insert_inout(open_outputs, input);
c9987633 387 }
fd548e5b 388 *buf += strspn(*buf, WHITESPACES);
c9987633 389 pad++;
8095a014 390 }
9710beaf 391
c9987633
VS
392 return pad;
393}
9710beaf 394
12e7e1d0
AK
395static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
396{
397 char *p = strchr(*buf, ';');
398
399 if (strncmp(*buf, "sws_flags=", 10))
400 return 0;
401
402 if (!p) {
8d900aa4 403 av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
12e7e1d0
AK
404 return AVERROR(EINVAL);
405 }
406
407 *buf += 4; // keep the 'flags=' part
408
409 av_freep(&graph->scale_sws_opts);
410 if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
411 return AVERROR(ENOMEM);
412 av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
413
414 *buf = p + 1;
415 return 0;
416}
417
d7bcc71d
AK
418int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
419 AVFilterInOut **inputs,
420 AVFilterInOut **outputs)
27afb09d 421{
c24f76b9 422 int index = 0, ret;
27afb09d 423 char chr = 0;
27afb09d 424
d7bcc71d 425 AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
27afb09d 426
12e7e1d0
AK
427 filters += strspn(filters, WHITESPACES);
428
429 if ((ret = parse_sws_flags(&filters, graph)) < 0)
430 goto fail;
431
27afb09d 432 do {
9710beaf 433 AVFilterContext *filter;
fd548e5b 434 filters += strspn(filters, WHITESPACES);
27afb09d 435
8d900aa4 436 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
27afb09d
VS
437 goto fail;
438
8d900aa4 439 if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
22260824
VS
440 goto fail;
441
27afb09d 442
8d900aa4 443 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
c9987633 444 goto fail;
da790674 445
c24f76b9 446 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
8d900aa4 447 graph)) < 0)
e84f0b62
VS
448 goto fail;
449
fd548e5b 450 filters += strspn(filters, WHITESPACES);
27afb09d 451 chr = *filters++;
27afb09d 452
d7bcc71d
AK
453 if (chr == ';' && curr_inputs)
454 append_inout(&open_outputs, &curr_inputs);
c9987633 455 index++;
f219eee5 456 } while (chr == ',' || chr == ';');
27afb09d 457
fd51ff16 458 if (chr) {
8d900aa4 459 av_log(graph, AV_LOG_ERROR,
78471234
SS
460 "Unable to parse graph description substring: \"%s\"\n",
461 filters - 1);
c24f76b9 462 ret = AVERROR(EINVAL);
78471234
SS
463 goto fail;
464 }
465
d7bcc71d 466 append_inout(&open_outputs, &curr_inputs);
27afb09d 467
d7bcc71d
AK
468 *inputs = open_inputs;
469 *outputs = open_outputs;
27afb09d
VS
470 return 0;
471
472 fail:
1565cbc6
AK
473 while (graph->nb_filters)
474 avfilter_free(graph->filters[0]);
e8e5dde7 475 av_freep(&graph->filters);
91d3cbe0
SS
476 avfilter_inout_free(&open_inputs);
477 avfilter_inout_free(&open_outputs);
478 avfilter_inout_free(&curr_inputs);
d7bcc71d
AK
479
480 *inputs = NULL;
481 *outputs = NULL;
482
483 return ret;
484}
d7bcc71d
AK
485
486int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
487 AVFilterInOut *open_inputs,
488 AVFilterInOut *open_outputs, void *log_ctx)
489{
490 int ret;
491 AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
492
493 if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
494 goto fail;
495
496 /* First input can be omitted if it is "[in]" */
497 if (inputs && !inputs->name)
498 inputs->name = av_strdup("in");
499 for (cur = inputs; cur; cur = cur->next) {
500 if (!cur->name) {
501 av_log(log_ctx, AV_LOG_ERROR,
502 "Not enough inputs specified for the \"%s\" filter.\n",
503 cur->filter_ctx->filter->name);
504 ret = AVERROR(EINVAL);
505 goto fail;
506 }
507 if (!(match = extract_inout(cur->name, &open_outputs)))
508 continue;
509 ret = avfilter_link(match->filter_ctx, match->pad_idx,
510 cur->filter_ctx, cur->pad_idx);
91d3cbe0 511 avfilter_inout_free(&match);
d7bcc71d
AK
512 if (ret < 0)
513 goto fail;
514 }
515
516 /* Last output can be omitted if it is "[out]" */
517 if (outputs && !outputs->name)
518 outputs->name = av_strdup("out");
519 for (cur = outputs; cur; cur = cur->next) {
520 if (!cur->name) {
521 av_log(log_ctx, AV_LOG_ERROR,
522 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
523 filters);
524 ret = AVERROR(EINVAL);
525 goto fail;
526 }
527 if (!(match = extract_inout(cur->name, &open_inputs)))
528 continue;
529 ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
530 match->filter_ctx, match->pad_idx);
91d3cbe0 531 avfilter_inout_free(&match);
d7bcc71d
AK
532 if (ret < 0)
533 goto fail;
534 }
535
536 fail:
537 if (ret < 0) {
1565cbc6
AK
538 while (graph->nb_filters)
539 avfilter_free(graph->filters[0]);
d7bcc71d
AK
540 av_freep(&graph->filters);
541 }
91d3cbe0
SS
542 avfilter_inout_free(&inputs);
543 avfilter_inout_free(&outputs);
544 avfilter_inout_free(&open_inputs);
545 avfilter_inout_free(&open_outputs);
c24f76b9 546 return ret;
27afb09d 547}