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"
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 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
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 *filt_name, const char *args, void *log_ctx)
101 char *tmp_args = NULL;
104 snprintf(inst_name, sizeof(inst_name), "Parsed_%s_%d", filt_name, index);
106 filt = avfilter_get_by_name(filt_name);
109 av_log(log_ctx, AV_LOG_ERROR,
110 "No such filter: '%s'\n", filt_name);
111 return AVERROR(EINVAL);
114 *filt_ctx = avfilter_graph_alloc_filter(ctx, filt, inst_name);
116 av_log(log_ctx, AV_LOG_ERROR,
117 "Error creating filter '%s'\n", filt_name);
118 return AVERROR(ENOMEM);
121 if (!strcmp(filt_name, "scale") && (!args || !strstr(args, "flags")) &&
122 ctx->scale_sws_opts) {
124 tmp_args = av_asprintf("%s:%s",
125 args, ctx->scale_sws_opts);
127 return AVERROR(ENOMEM);
130 args = ctx->scale_sws_opts;
133 ret = avfilter_init_str(*filt_ctx, args);
135 av_log(log_ctx, AV_LOG_ERROR,
136 "Error initializing filter '%s'", filt_name);
138 av_log(log_ctx, AV_LOG_ERROR, " with args '%s'", args);
139 av_log(log_ctx, AV_LOG_ERROR, "\n");
140 avfilter_free(*filt_ctx);
149 * Parse a string of the form FILTER_NAME[=PARAMS], and create a
150 * corresponding filter instance which is added to graph with
153 * @param filt_ctx Pointer that is set to the created and configured filter
154 * context on success, set to NULL on failure.
155 * @param filt_ctx put here a pointer to the created filter context on
156 * success, NULL otherwise
157 * @param buf pointer to the buffer to parse, *buf will be updated to
158 * point to the char next after the parsed string
159 * @param index an index which is assigned to the created filter
160 * instance, and which is supposed to be unique for each filter
161 * instance added to the filtergraph
162 * @return >= 0 in case of success, a negative AVERROR code otherwise
164 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
165 int index, void *log_ctx)
168 char *name = av_get_token(buf, "=,;[\n");
173 opts = av_get_token(buf, "[],;\n");
176 ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
182 AVFilterInOut *avfilter_inout_alloc(void)
184 return av_mallocz(sizeof(AVFilterInOut));
187 void avfilter_inout_free(AVFilterInOut **inout)
190 AVFilterInOut *next = (*inout)->next;
191 av_freep(&(*inout)->name);
197 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
201 while (*links && (!(*links)->name || strcmp((*links)->name, label)))
202 links = &((*links)->next);
214 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
216 element->next = *inouts;
220 static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
222 while (*inouts && (*inouts)->next)
223 inouts = &((*inouts)->next);
228 (*inouts)->next = *element;
232 static int link_filter_inouts(AVFilterContext *filt_ctx,
233 AVFilterInOut **curr_inputs,
234 AVFilterInOut **open_inputs, void *log_ctx)
238 for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
239 AVFilterInOut *p = *curr_inputs;
242 *curr_inputs = (*curr_inputs)->next;
244 } else if (!(p = av_mallocz(sizeof(*p))))
245 return AVERROR(ENOMEM);
248 ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
254 p->filter_ctx = filt_ctx;
256 append_inout(open_inputs, &p);
261 av_log(log_ctx, AV_LOG_ERROR,
262 "Too many inputs specified for the \"%s\" filter.\n",
263 filt_ctx->filter->name);
264 return AVERROR(EINVAL);
267 pad = filt_ctx->nb_outputs;
269 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
271 return AVERROR(ENOMEM);
272 currlinkn->filter_ctx = filt_ctx;
273 currlinkn->pad_idx = pad;
274 insert_inout(curr_inputs, currlinkn);
280 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
281 AVFilterInOut **open_outputs, void *log_ctx)
283 AVFilterInOut *parsed_inputs = NULL;
286 while (**buf == '[') {
287 char *name = parse_link_name(buf, log_ctx);
288 AVFilterInOut *match;
291 return AVERROR(EINVAL);
293 /* First check if the label is not in the open_outputs list */
294 match = extract_inout(name, open_outputs);
299 /* Not in the list, so add it as an input */
300 if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
302 return AVERROR(ENOMEM);
305 match->pad_idx = pad;
308 append_inout(&parsed_inputs, &match);
310 *buf += strspn(*buf, WHITESPACES);
314 append_inout(&parsed_inputs, curr_inputs);
315 *curr_inputs = parsed_inputs;
320 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
321 AVFilterInOut **open_inputs,
322 AVFilterInOut **open_outputs, void *log_ctx)
326 while (**buf == '[') {
327 char *name = parse_link_name(buf, log_ctx);
328 AVFilterInOut *match;
330 AVFilterInOut *input = *curr_inputs;
333 return AVERROR(EINVAL);
336 av_log(log_ctx, AV_LOG_ERROR,
337 "No output pad can be associated to link label '%s'.\n", name);
339 return AVERROR(EINVAL);
341 *curr_inputs = (*curr_inputs)->next;
343 /* First check if the label is not in the open_inputs list */
344 match = extract_inout(name, open_inputs);
347 if ((ret = link_filter(input->filter_ctx, input->pad_idx,
348 match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
352 av_freep(&match->name);
357 /* Not in the list, so add the first input as an open_output */
359 insert_inout(open_outputs, input);
361 *buf += strspn(*buf, WHITESPACES);
368 static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
370 char *p = strchr(*buf, ';');
372 if (strncmp(*buf, "sws_flags=", 10))
376 av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
377 return AVERROR(EINVAL);
380 *buf += 4; // keep the 'flags=' part
382 av_freep(&graph->scale_sws_opts);
383 if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
384 return AVERROR(ENOMEM);
385 av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
391 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
392 AVFilterInOut **inputs,
393 AVFilterInOut **outputs)
395 int index = 0, ret = 0;
398 AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
400 filters += strspn(filters, WHITESPACES);
402 if ((ret = parse_sws_flags(&filters, graph)) < 0)
406 AVFilterContext *filter;
407 filters += strspn(filters, WHITESPACES);
409 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
411 if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
415 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
418 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
422 filters += strspn(filters, WHITESPACES);
425 if (chr == ';' && curr_inputs)
426 append_inout(&open_outputs, &curr_inputs);
428 } while (chr == ',' || chr == ';');
431 av_log(graph, AV_LOG_ERROR,
432 "Unable to parse graph description substring: \"%s\"\n",
434 ret = AVERROR(EINVAL);
438 append_inout(&open_outputs, &curr_inputs);
441 *inputs = open_inputs;
442 *outputs = open_outputs;
446 while (graph->nb_filters)
447 avfilter_free(graph->filters[0]);
448 av_freep(&graph->filters);
449 avfilter_inout_free(&open_inputs);
450 avfilter_inout_free(&open_outputs);
451 avfilter_inout_free(&curr_inputs);
459 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
460 AVFilterInOut *open_inputs,
461 AVFilterInOut *open_outputs, void *log_ctx)
464 AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
466 if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
469 /* First input can be omitted if it is "[in]" */
470 if (inputs && !inputs->name)
471 inputs->name = av_strdup("in");
472 for (cur = inputs; cur; cur = cur->next) {
474 av_log(log_ctx, AV_LOG_ERROR,
475 "Not enough inputs specified for the \"%s\" filter.\n",
476 cur->filter_ctx->filter->name);
477 ret = AVERROR(EINVAL);
480 if (!(match = extract_inout(cur->name, &open_outputs)))
482 ret = avfilter_link(match->filter_ctx, match->pad_idx,
483 cur->filter_ctx, cur->pad_idx);
484 avfilter_inout_free(&match);
489 /* Last output can be omitted if it is "[out]" */
490 if (outputs && !outputs->name)
491 outputs->name = av_strdup("out");
492 for (cur = outputs; cur; cur = cur->next) {
494 av_log(log_ctx, AV_LOG_ERROR,
495 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
497 ret = AVERROR(EINVAL);
500 if (!(match = extract_inout(cur->name, &open_inputs)))
502 ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
503 match->filter_ctx, match->pad_idx);
504 avfilter_inout_free(&match);
511 while (graph->nb_filters)
512 avfilter_free(graph->filters[0]);
513 av_freep(&graph->filters);
515 avfilter_inout_free(&inputs);
516 avfilter_inout_free(&outputs);
517 avfilter_inout_free(&open_inputs);
518 avfilter_inout_free(&open_outputs);
522 int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
523 AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
526 int index = 0, ret = 0;
529 AVFilterInOut *curr_inputs = NULL;
530 AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
531 AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
533 if ((ret = parse_sws_flags(&filters, graph)) < 0)
537 AVFilterContext *filter;
538 const char *filterchain = filters;
539 filters += strspn(filters, WHITESPACES);
541 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
544 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
547 if (filter->nb_inputs == 1 && !curr_inputs && !index) {
548 /* First input pad, assume it is "[in]" if not specified */
549 const char *tmp = "[in]";
550 if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
554 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
557 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
561 filters += strspn(filters, WHITESPACES);
564 if (chr == ';' && curr_inputs) {
565 av_log(log_ctx, AV_LOG_ERROR,
566 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
568 ret = AVERROR(EINVAL);
572 } while (chr == ',' || chr == ';');
575 av_log(log_ctx, AV_LOG_ERROR,
576 "Unable to parse graph description substring: \"%s\"\n",
578 ret = AVERROR(EINVAL);
583 /* Last output pad, assume it is "[out]" if not specified */
584 const char *tmp = "[out]";
585 if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
591 /* clear open_in/outputs only if not passed as parameters */
592 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
593 else avfilter_inout_free(&open_inputs);
594 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
595 else avfilter_inout_free(&open_outputs);
596 avfilter_inout_free(&curr_inputs);
599 while (graph->nb_filters)
600 avfilter_free(graph->filters[0]);
601 av_freep(&graph->filters);