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