3 * Copyright (c) 2008 Vitor Sessak
4 * Copyright (c) 2007 Bobby Bingham
6 * This file is part of FFmpeg.
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.
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.
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
26 #include "libavutil/avstring.h"
27 #include "libavutil/mem.h"
30 #define WHITESPACES " \n\t\r"
33 * Link two filters together.
35 * @see avfilter_link()
37 static int link_filter(AVFilterContext *src, int srcpad,
38 AVFilterContext *dst, int dstpad,
42 if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
43 av_log(log_ctx, AV_LOG_ERROR,
44 "Cannot create the link %s:%d -> %s:%d\n",
45 src->filter->name, srcpad, dst->filter->name, dstpad);
53 * Parse the name of a link, which has the format "[linkname]".
55 * @return a pointer (that need to be freed after use) to the name
58 static char *parse_link_name(const char **buf, void *log_ctx)
60 const char *start = *buf;
64 name = av_get_token(buf, "]");
69 av_log(log_ctx, AV_LOG_ERROR,
70 "Bad (empty?) label found in the following: \"%s\".\n", start);
74 if (*(*buf)++ != ']') {
75 av_log(log_ctx, AV_LOG_ERROR,
76 "Mismatched '[' found in the following: \"%s\".\n", start);
85 * Create an instance of a filter, initialize and insert it in the
86 * filtergraph in *ctx.
88 * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
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 name the name of the filter to create, can be filter name or filter_name\@id as instance name
92 * @param args the arguments provided to the filter during its initialization
93 * @param log_ctx the log context to use
94 * @return >= 0 in case of success, a negative AVERROR code otherwise
96 static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
97 const char *name, const char *args, void *log_ctx)
101 const char *inst_name = NULL, *filt_name = NULL;
102 char *tmp_args = NULL;
105 av_strlcpy(name2, name, sizeof(name2));
107 for (k = 0; name2[k]; k++) {
108 if (name2[k] == '@' && name[k+1]) {
117 snprintf(name2, sizeof(name2), "Parsed_%s_%d", name, index);
122 filt = avfilter_get_by_name(filt_name);
125 av_log(log_ctx, AV_LOG_ERROR,
126 "No such filter: '%s'\n", filt_name);
127 return AVERROR(EINVAL);
130 *filt_ctx = avfilter_graph_alloc_filter(ctx, filt, inst_name);
132 av_log(log_ctx, AV_LOG_ERROR,
133 "Error creating filter '%s'\n", filt_name);
134 return AVERROR(ENOMEM);
137 if (!strcmp(filt_name, "scale") && (!args || !strstr(args, "flags")) &&
138 ctx->scale_sws_opts) {
140 tmp_args = av_asprintf("%s:%s",
141 args, ctx->scale_sws_opts);
143 return AVERROR(ENOMEM);
146 args = ctx->scale_sws_opts;
149 ret = avfilter_init_str(*filt_ctx, args);
151 av_log(log_ctx, AV_LOG_ERROR,
152 "Error initializing filter '%s'", filt_name);
154 av_log(log_ctx, AV_LOG_ERROR, " with args '%s'", args);
155 av_log(log_ctx, AV_LOG_ERROR, "\n");
156 avfilter_free(*filt_ctx);
165 * Parse a string of the form FILTER_NAME[=PARAMS], and create a
166 * corresponding filter instance which is added to graph with
169 * @param filt_ctx Pointer that is set to the created and configured filter
170 * context on success, set to NULL on failure.
171 * @param filt_ctx put here a pointer to the created filter context on
172 * success, NULL otherwise
173 * @param buf pointer to the buffer to parse, *buf will be updated to
174 * point to the char next after the parsed string
175 * @param index an index which is assigned to the created filter
176 * instance, and which is supposed to be unique for each filter
177 * instance added to the filtergraph
178 * @return >= 0 in case of success, a negative AVERROR code otherwise
180 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
181 int index, void *log_ctx)
184 char *name = av_get_token(buf, "=,;[");
189 opts = av_get_token(buf, "[],;");
192 ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
198 AVFilterInOut *avfilter_inout_alloc(void)
200 return av_mallocz(sizeof(AVFilterInOut));
203 void avfilter_inout_free(AVFilterInOut **inout)
206 AVFilterInOut *next = (*inout)->next;
207 av_freep(&(*inout)->name);
213 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
217 while (*links && (!(*links)->name || strcmp((*links)->name, label)))
218 links = &((*links)->next);
230 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
232 element->next = *inouts;
236 static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
238 while (*inouts && (*inouts)->next)
239 inouts = &((*inouts)->next);
244 (*inouts)->next = *element;
248 static int link_filter_inouts(AVFilterContext *filt_ctx,
249 AVFilterInOut **curr_inputs,
250 AVFilterInOut **open_inputs, void *log_ctx)
254 for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
255 AVFilterInOut *p = *curr_inputs;
258 *curr_inputs = (*curr_inputs)->next;
260 } else if (!(p = av_mallocz(sizeof(*p))))
261 return AVERROR(ENOMEM);
264 ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
270 p->filter_ctx = filt_ctx;
272 append_inout(open_inputs, &p);
277 av_log(log_ctx, AV_LOG_ERROR,
278 "Too many inputs specified for the \"%s\" filter.\n",
279 filt_ctx->filter->name);
280 return AVERROR(EINVAL);
283 pad = filt_ctx->nb_outputs;
285 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
287 return AVERROR(ENOMEM);
288 currlinkn->filter_ctx = filt_ctx;
289 currlinkn->pad_idx = pad;
290 insert_inout(curr_inputs, currlinkn);
296 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
297 AVFilterInOut **open_outputs, void *log_ctx)
299 AVFilterInOut *parsed_inputs = NULL;
302 while (**buf == '[') {
303 char *name = parse_link_name(buf, log_ctx);
304 AVFilterInOut *match;
307 return AVERROR(EINVAL);
309 /* First check if the label is not in the open_outputs list */
310 match = extract_inout(name, open_outputs);
315 /* Not in the list, so add it as an input */
316 if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
318 return AVERROR(ENOMEM);
321 match->pad_idx = pad;
324 append_inout(&parsed_inputs, &match);
326 *buf += strspn(*buf, WHITESPACES);
330 append_inout(&parsed_inputs, curr_inputs);
331 *curr_inputs = parsed_inputs;
336 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
337 AVFilterInOut **open_inputs,
338 AVFilterInOut **open_outputs, void *log_ctx)
342 while (**buf == '[') {
343 char *name = parse_link_name(buf, log_ctx);
344 AVFilterInOut *match;
346 AVFilterInOut *input = *curr_inputs;
349 return AVERROR(EINVAL);
352 av_log(log_ctx, AV_LOG_ERROR,
353 "No output pad can be associated to link label '%s'.\n", name);
355 return AVERROR(EINVAL);
357 *curr_inputs = (*curr_inputs)->next;
359 /* First check if the label is not in the open_inputs list */
360 match = extract_inout(name, open_inputs);
363 if ((ret = link_filter(input->filter_ctx, input->pad_idx,
364 match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
368 av_freep(&match->name);
373 /* Not in the list, so add the first input as an open_output */
375 insert_inout(open_outputs, input);
377 *buf += strspn(*buf, WHITESPACES);
384 static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
386 char *p = strchr(*buf, ';');
388 if (strncmp(*buf, "sws_flags=", 10))
392 av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
393 return AVERROR(EINVAL);
396 *buf += 4; // keep the 'flags=' part
398 av_freep(&graph->scale_sws_opts);
399 if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
400 return AVERROR(ENOMEM);
401 av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
407 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
408 AVFilterInOut **inputs,
409 AVFilterInOut **outputs)
411 int index = 0, ret = 0;
414 AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
416 filters += strspn(filters, WHITESPACES);
418 if ((ret = parse_sws_flags(&filters, graph)) < 0)
422 AVFilterContext *filter;
423 filters += strspn(filters, WHITESPACES);
425 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
427 if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
431 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
434 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
438 filters += strspn(filters, WHITESPACES);
441 if (chr == ';' && curr_inputs)
442 append_inout(&open_outputs, &curr_inputs);
444 } while (chr == ',' || chr == ';');
447 av_log(graph, AV_LOG_ERROR,
448 "Unable to parse graph description substring: \"%s\"\n",
450 ret = AVERROR(EINVAL);
454 append_inout(&open_outputs, &curr_inputs);
457 *inputs = open_inputs;
458 *outputs = open_outputs;
462 while (graph->nb_filters)
463 avfilter_free(graph->filters[0]);
464 av_freep(&graph->filters);
465 avfilter_inout_free(&open_inputs);
466 avfilter_inout_free(&open_outputs);
467 avfilter_inout_free(&curr_inputs);
475 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
476 AVFilterInOut *open_inputs,
477 AVFilterInOut *open_outputs, void *log_ctx)
480 AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
482 if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
485 /* First input can be omitted if it is "[in]" */
486 if (inputs && !inputs->name)
487 inputs->name = av_strdup("in");
488 for (cur = inputs; cur; cur = cur->next) {
490 av_log(log_ctx, AV_LOG_ERROR,
491 "Not enough inputs specified for the \"%s\" filter.\n",
492 cur->filter_ctx->filter->name);
493 ret = AVERROR(EINVAL);
496 if (!(match = extract_inout(cur->name, &open_outputs)))
498 ret = avfilter_link(match->filter_ctx, match->pad_idx,
499 cur->filter_ctx, cur->pad_idx);
500 avfilter_inout_free(&match);
505 /* Last output can be omitted if it is "[out]" */
506 if (outputs && !outputs->name)
507 outputs->name = av_strdup("out");
508 for (cur = outputs; cur; cur = cur->next) {
510 av_log(log_ctx, AV_LOG_ERROR,
511 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
513 ret = AVERROR(EINVAL);
516 if (!(match = extract_inout(cur->name, &open_inputs)))
518 ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
519 match->filter_ctx, match->pad_idx);
520 avfilter_inout_free(&match);
527 while (graph->nb_filters)
528 avfilter_free(graph->filters[0]);
529 av_freep(&graph->filters);
531 avfilter_inout_free(&inputs);
532 avfilter_inout_free(&outputs);
533 avfilter_inout_free(&open_inputs);
534 avfilter_inout_free(&open_outputs);
538 int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
539 AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
542 int index = 0, ret = 0;
545 AVFilterInOut *curr_inputs = NULL;
546 AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
547 AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
549 if ((ret = parse_sws_flags(&filters, graph)) < 0)
553 AVFilterContext *filter;
554 const char *filterchain = filters;
555 filters += strspn(filters, WHITESPACES);
557 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
560 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
563 if (filter->nb_inputs == 1 && !curr_inputs && !index) {
564 /* First input pad, assume it is "[in]" if not specified */
565 const char *tmp = "[in]";
566 if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
570 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
573 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
577 filters += strspn(filters, WHITESPACES);
580 if (chr == ';' && curr_inputs) {
581 av_log(log_ctx, AV_LOG_ERROR,
582 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
584 ret = AVERROR(EINVAL);
588 } while (chr == ',' || chr == ';');
591 av_log(log_ctx, AV_LOG_ERROR,
592 "Unable to parse graph description substring: \"%s\"\n",
594 ret = AVERROR(EINVAL);
599 /* Last output pad, assume it is "[out]" if not specified */
600 const char *tmp = "[out]";
601 if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
607 /* clear open_in/outputs only if not passed as parameters */
608 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
609 else avfilter_inout_free(&open_inputs);
610 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
611 else avfilter_inout_free(&open_outputs);
612 avfilter_inout_free(&curr_inputs);
615 while (graph->nb_filters)
616 avfilter_free(graph->filters[0]);
617 av_freep(&graph->filters);