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
27 #include "libavutil/avstring.h"
28 #include "libavutil/mem.h"
30 #include "avfiltergraph.h"
32 #define WHITESPACES " \n\t"
35 * Link two filters together.
37 * @see avfilter_link()
39 static int link_filter(AVFilterContext *src, int srcpad,
40 AVFilterContext *dst, int dstpad,
44 if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
45 av_log(log_ctx, AV_LOG_ERROR,
46 "Cannot create the link %s:%d -> %s:%d\n",
47 src->filter->name, srcpad, dst->filter->name, dstpad);
55 * Parse the name of a link, which has the format "[linkname]".
57 * @return a pointer (that need to be freed after use) to the name
60 static char *parse_link_name(const char **buf, void *log_ctx)
62 const char *start = *buf;
66 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)
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 ret = avfilter_open(filt_ctx, filt, inst_name);
116 av_log(log_ctx, AV_LOG_ERROR,
117 "Error creating filter '%s'\n", filt_name);
121 if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
122 avfilter_free(*filt_ctx);
126 if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags")) {
127 snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
128 args, ctx->scale_sws_opts);
132 if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
133 av_log(log_ctx, AV_LOG_ERROR,
134 "Error initializing filter '%s' with args '%s'\n", filt_name, args);
142 * Parse a string of the form FILTER_NAME[=PARAMS], and create a
143 * corresponding filter instance which is added to graph with
146 * @param filt_ctx Pointer that is set to the created and configured filter
147 * context on success, set to NULL on failure.
148 * @param filt_ctx put here a pointer to the created filter context on
149 * success, NULL otherwise
150 * @param buf pointer to the buffer to parse, *buf will be updated to
151 * point to the char next after the parsed string
152 * @param index an index which is assigned to the created filter
153 * instance, and which is supposed to be unique for each filter
154 * instance added to the filtergraph
155 * @return 0 in case of success, a negative AVERROR code otherwise
157 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
158 int index, void *log_ctx)
161 char *name = av_get_token(buf, "=,;[\n");
166 opts = av_get_token(buf, "[],;\n");
169 ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
175 AVFilterInOut *avfilter_inout_alloc(void)
177 return av_mallocz(sizeof(AVFilterInOut));
180 void avfilter_inout_free(AVFilterInOut **inout)
183 AVFilterInOut *next = (*inout)->next;
184 av_freep(&(*inout)->name);
190 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
194 while (*links && (!(*links)->name || strcmp((*links)->name, label)))
195 links = &((*links)->next);
207 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
209 element->next = *inouts;
213 static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
215 while (*inouts && (*inouts)->next)
216 inouts = &((*inouts)->next);
221 (*inouts)->next = *element;
225 static int link_filter_inouts(AVFilterContext *filt_ctx,
226 AVFilterInOut **curr_inputs,
227 AVFilterInOut **open_inputs, void *log_ctx)
231 for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
232 AVFilterInOut *p = *curr_inputs;
235 *curr_inputs = (*curr_inputs)->next;
237 } else if (!(p = av_mallocz(sizeof(*p))))
238 return AVERROR(ENOMEM);
241 if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
246 p->filter_ctx = filt_ctx;
248 append_inout(open_inputs, &p);
253 av_log(log_ctx, AV_LOG_ERROR,
254 "Too many inputs specified for the \"%s\" filter.\n",
255 filt_ctx->filter->name);
256 return AVERROR(EINVAL);
259 pad = filt_ctx->nb_outputs;
261 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
263 return AVERROR(ENOMEM);
264 currlinkn->filter_ctx = filt_ctx;
265 currlinkn->pad_idx = pad;
266 insert_inout(curr_inputs, currlinkn);
272 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
273 AVFilterInOut **open_outputs, void *log_ctx)
275 AVFilterInOut *parsed_inputs = NULL;
278 while (**buf == '[') {
279 char *name = parse_link_name(buf, log_ctx);
280 AVFilterInOut *match;
283 return AVERROR(EINVAL);
285 /* First check if the label is not in the open_outputs list */
286 match = extract_inout(name, open_outputs);
291 /* Not in the list, so add it as an input */
292 if (!(match = av_mallocz(sizeof(AVFilterInOut))))
293 return AVERROR(ENOMEM);
295 match->pad_idx = pad;
298 append_inout(&parsed_inputs, &match);
300 *buf += strspn(*buf, WHITESPACES);
304 append_inout(&parsed_inputs, curr_inputs);
305 *curr_inputs = parsed_inputs;
310 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
311 AVFilterInOut **open_inputs,
312 AVFilterInOut **open_outputs, void *log_ctx)
316 while (**buf == '[') {
317 char *name = parse_link_name(buf, log_ctx);
318 AVFilterInOut *match;
320 AVFilterInOut *input = *curr_inputs;
322 av_log(log_ctx, AV_LOG_ERROR,
323 "No output pad can be associated to link label '%s'.\n",
325 return AVERROR(EINVAL);
327 *curr_inputs = (*curr_inputs)->next;
330 return AVERROR(EINVAL);
332 /* First check if the label is not in the open_inputs list */
333 match = extract_inout(name, open_inputs);
336 if ((ret = link_filter(input->filter_ctx, input->pad_idx,
337 match->filter_ctx, match->pad_idx, log_ctx)) < 0)
339 av_free(match->name);
344 /* Not in the list, so add the first input as a open_output */
346 insert_inout(open_outputs, input);
348 *buf += strspn(*buf, WHITESPACES);
355 static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
357 char *p = strchr(*buf, ';');
359 if (strncmp(*buf, "sws_flags=", 10))
363 av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
364 return AVERROR(EINVAL);
367 *buf += 4; // keep the 'flags=' part
369 av_freep(&graph->scale_sws_opts);
370 if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
371 return AVERROR(ENOMEM);
372 av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
378 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
379 AVFilterInOut **inputs,
380 AVFilterInOut **outputs)
382 int index = 0, ret = 0;
385 AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
387 filters += strspn(filters, WHITESPACES);
389 if ((ret = parse_sws_flags(&filters, graph)) < 0)
393 AVFilterContext *filter;
394 filters += strspn(filters, WHITESPACES);
396 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
398 if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
402 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
405 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
409 filters += strspn(filters, WHITESPACES);
412 if (chr == ';' && curr_inputs)
413 append_inout(&open_outputs, &curr_inputs);
415 } while (chr == ',' || chr == ';');
418 av_log(graph, AV_LOG_ERROR,
419 "Unable to parse graph description substring: \"%s\"\n",
421 ret = AVERROR(EINVAL);
425 append_inout(&open_outputs, &curr_inputs);
428 *inputs = open_inputs;
429 *outputs = open_outputs;
433 for (; graph->filter_count > 0; graph->filter_count--)
434 avfilter_free(graph->filters[graph->filter_count - 1]);
435 av_freep(&graph->filters);
436 avfilter_inout_free(&open_inputs);
437 avfilter_inout_free(&open_outputs);
438 avfilter_inout_free(&curr_inputs);
446 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
447 AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
452 AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
453 AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
454 AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
456 if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
459 /* First input can be omitted if it is "[in]" */
460 if (inputs && !inputs->name)
461 inputs->name = av_strdup("in");
462 for (cur = inputs; cur; cur = cur->next) {
464 av_log(log_ctx, AV_LOG_ERROR,
465 "Not enough inputs specified for the \"%s\" filter.\n",
466 cur->filter_ctx->filter->name);
467 ret = AVERROR(EINVAL);
470 if (!(match = extract_inout(cur->name, &open_outputs)))
472 ret = avfilter_link(match->filter_ctx, match->pad_idx,
473 cur->filter_ctx, cur->pad_idx);
474 avfilter_inout_free(&match);
479 /* Last output can be omitted if it is "[out]" */
480 if (outputs && !outputs->name)
481 outputs->name = av_strdup("out");
482 for (cur = outputs; cur; cur = cur->next) {
484 av_log(log_ctx, AV_LOG_ERROR,
485 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
487 ret = AVERROR(EINVAL);
490 if (!(match = extract_inout(cur->name, &open_inputs)))
492 ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
493 match->filter_ctx, match->pad_idx);
494 avfilter_inout_free(&match);
501 for (; graph->filter_count > 0; graph->filter_count--)
502 avfilter_free(graph->filters[graph->filter_count - 1]);
503 av_freep(&graph->filters);
505 avfilter_inout_free(&inputs);
506 avfilter_inout_free(&outputs);
507 /* clear open_in/outputs only if not passed as parameters */
508 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
509 else avfilter_inout_free(&open_inputs);
510 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
511 else avfilter_inout_free(&open_outputs);
515 int index = 0, ret = 0;
518 AVFilterInOut *curr_inputs = NULL;
519 AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
520 AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
523 AVFilterContext *filter;
524 const char *filterchain = filters;
525 filters += strspn(filters, WHITESPACES);
527 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
530 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
533 if (filter->input_count == 1 && !curr_inputs && !index) {
534 /* First input pad, assume it is "[in]" if not specified */
535 const char *tmp = "[in]";
536 if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
540 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
543 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
547 filters += strspn(filters, WHITESPACES);
550 if (chr == ';' && curr_inputs) {
551 av_log(log_ctx, AV_LOG_ERROR,
552 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
554 ret = AVERROR(EINVAL);
558 } while (chr == ',' || chr == ';');
561 av_log(log_ctx, AV_LOG_ERROR,
562 "Unable to parse graph description substring: \"%s\"\n",
564 ret = AVERROR(EINVAL);
569 /* Last output pad, assume it is "[out]" if not specified */
570 const char *tmp = "[out]";
571 if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
577 /* clear open_in/outputs only if not passed as parameters */
578 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
579 else avfilter_inout_free(&open_inputs);
580 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
581 else avfilter_inout_free(&open_outputs);
582 avfilter_inout_free(&curr_inputs);
585 for (; graph->filter_count > 0; graph->filter_count--)
586 avfilter_free(graph->filters[graph->filter_count - 1]);
587 av_freep(&graph->filters);