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 && ctx->scale_sws_opts) {
128 snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
129 args, ctx->scale_sws_opts);
133 if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
134 av_log(log_ctx, AV_LOG_ERROR,
135 "Error initializing filter '%s' with args '%s'\n", filt_name, args);
143 * Parse a string of the form FILTER_NAME[=PARAMS], and create a
144 * corresponding filter instance which is added to graph with
147 * @param filt_ctx Pointer that is set to the created and configured filter
148 * context on success, set to NULL on failure.
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
158 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
159 int index, void *log_ctx)
162 char *name = av_get_token(buf, "=,;[\n");
167 opts = av_get_token(buf, "[],;\n");
170 ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
176 AVFilterInOut *avfilter_inout_alloc(void)
178 return av_mallocz(sizeof(AVFilterInOut));
181 void avfilter_inout_free(AVFilterInOut **inout)
184 AVFilterInOut *next = (*inout)->next;
185 av_freep(&(*inout)->name);
191 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
195 while (*links && (!(*links)->name || strcmp((*links)->name, label)))
196 links = &((*links)->next);
208 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
210 element->next = *inouts;
214 static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
216 while (*inouts && (*inouts)->next)
217 inouts = &((*inouts)->next);
222 (*inouts)->next = *element;
226 static int link_filter_inouts(AVFilterContext *filt_ctx,
227 AVFilterInOut **curr_inputs,
228 AVFilterInOut **open_inputs, void *log_ctx)
232 for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
233 AVFilterInOut *p = *curr_inputs;
236 *curr_inputs = (*curr_inputs)->next;
238 } else if (!(p = av_mallocz(sizeof(*p))))
239 return AVERROR(ENOMEM);
242 ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
248 p->filter_ctx = filt_ctx;
250 append_inout(open_inputs, &p);
255 av_log(log_ctx, AV_LOG_ERROR,
256 "Too many inputs specified for the \"%s\" filter.\n",
257 filt_ctx->filter->name);
258 return AVERROR(EINVAL);
261 pad = filt_ctx->nb_outputs;
263 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
265 return AVERROR(ENOMEM);
266 currlinkn->filter_ctx = filt_ctx;
267 currlinkn->pad_idx = pad;
268 insert_inout(curr_inputs, currlinkn);
274 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
275 AVFilterInOut **open_outputs, void *log_ctx)
277 AVFilterInOut *parsed_inputs = NULL;
280 while (**buf == '[') {
281 char *name = parse_link_name(buf, log_ctx);
282 AVFilterInOut *match;
285 return AVERROR(EINVAL);
287 /* First check if the label is not in the open_outputs list */
288 match = extract_inout(name, open_outputs);
293 /* Not in the list, so add it as an input */
294 if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
296 return AVERROR(ENOMEM);
299 match->pad_idx = pad;
302 append_inout(&parsed_inputs, &match);
304 *buf += strspn(*buf, WHITESPACES);
308 append_inout(&parsed_inputs, curr_inputs);
309 *curr_inputs = parsed_inputs;
314 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
315 AVFilterInOut **open_inputs,
316 AVFilterInOut **open_outputs, void *log_ctx)
320 while (**buf == '[') {
321 char *name = parse_link_name(buf, log_ctx);
322 AVFilterInOut *match;
324 AVFilterInOut *input = *curr_inputs;
327 return AVERROR(EINVAL);
330 av_log(log_ctx, AV_LOG_ERROR,
331 "No output pad can be associated to link label '%s'.\n", name);
333 return AVERROR(EINVAL);
335 *curr_inputs = (*curr_inputs)->next;
337 /* First check if the label is not in the open_inputs list */
338 match = extract_inout(name, open_inputs);
341 if ((ret = link_filter(input->filter_ctx, input->pad_idx,
342 match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
346 av_free(match->name);
351 /* Not in the list, so add the first input as a open_output */
353 insert_inout(open_outputs, input);
355 *buf += strspn(*buf, WHITESPACES);
362 static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
364 char *p = strchr(*buf, ';');
366 if (strncmp(*buf, "sws_flags=", 10))
370 av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
371 return AVERROR(EINVAL);
374 *buf += 4; // keep the 'flags=' part
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);
385 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
386 AVFilterInOut **inputs,
387 AVFilterInOut **outputs)
389 int index = 0, ret = 0;
392 AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
394 filters += strspn(filters, WHITESPACES);
396 if ((ret = parse_sws_flags(&filters, graph)) < 0)
400 AVFilterContext *filter;
401 filters += strspn(filters, WHITESPACES);
403 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
405 if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
409 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
412 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
416 filters += strspn(filters, WHITESPACES);
419 if (chr == ';' && curr_inputs)
420 append_inout(&open_outputs, &curr_inputs);
422 } while (chr == ',' || chr == ';');
425 av_log(graph, AV_LOG_ERROR,
426 "Unable to parse graph description substring: \"%s\"\n",
428 ret = AVERROR(EINVAL);
432 append_inout(&open_outputs, &curr_inputs);
435 *inputs = open_inputs;
436 *outputs = open_outputs;
440 for (; graph->filter_count > 0; graph->filter_count--)
441 avfilter_free(graph->filters[graph->filter_count - 1]);
442 av_freep(&graph->filters);
443 avfilter_inout_free(&open_inputs);
444 avfilter_inout_free(&open_outputs);
445 avfilter_inout_free(&curr_inputs);
453 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
454 AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
459 AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
460 AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
461 AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
463 if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
466 /* First input can be omitted if it is "[in]" */
467 if (inputs && !inputs->name)
468 inputs->name = av_strdup("in");
469 for (cur = inputs; cur; cur = cur->next) {
471 av_log(log_ctx, AV_LOG_ERROR,
472 "Not enough inputs specified for the \"%s\" filter.\n",
473 cur->filter_ctx->filter->name);
474 ret = AVERROR(EINVAL);
477 if (!(match = extract_inout(cur->name, &open_outputs)))
479 ret = avfilter_link(match->filter_ctx, match->pad_idx,
480 cur->filter_ctx, cur->pad_idx);
481 avfilter_inout_free(&match);
486 /* Last output can be omitted if it is "[out]" */
487 if (outputs && !outputs->name)
488 outputs->name = av_strdup("out");
489 for (cur = outputs; cur; cur = cur->next) {
491 av_log(log_ctx, AV_LOG_ERROR,
492 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
494 ret = AVERROR(EINVAL);
497 if (!(match = extract_inout(cur->name, &open_inputs)))
499 ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
500 match->filter_ctx, match->pad_idx);
501 avfilter_inout_free(&match);
508 for (; graph->filter_count > 0; graph->filter_count--)
509 avfilter_free(graph->filters[graph->filter_count - 1]);
510 av_freep(&graph->filters);
512 avfilter_inout_free(&inputs);
513 avfilter_inout_free(&outputs);
514 /* clear open_in/outputs only if not passed as parameters */
515 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
516 else avfilter_inout_free(&open_inputs);
517 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
518 else avfilter_inout_free(&open_outputs);
522 int index = 0, ret = 0;
525 AVFilterInOut *curr_inputs = NULL;
526 AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
527 AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
529 if ((ret = parse_sws_flags(&filters, graph)) < 0)
533 AVFilterContext *filter;
534 const char *filterchain = filters;
535 filters += strspn(filters, WHITESPACES);
537 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
540 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
543 if (filter->input_count == 1 && !curr_inputs && !index) {
544 /* First input pad, assume it is "[in]" if not specified */
545 const char *tmp = "[in]";
546 if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
550 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
553 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
557 filters += strspn(filters, WHITESPACES);
560 if (chr == ';' && curr_inputs) {
561 av_log(log_ctx, AV_LOG_ERROR,
562 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
564 ret = AVERROR(EINVAL);
568 } while (chr == ',' || chr == ';');
571 av_log(log_ctx, AV_LOG_ERROR,
572 "Unable to parse graph description substring: \"%s\"\n",
574 ret = AVERROR(EINVAL);
579 /* Last output pad, assume it is "[out]" if not specified */
580 const char *tmp = "[out]";
581 if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
587 /* clear open_in/outputs only if not passed as parameters */
588 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
589 else avfilter_inout_free(&open_inputs);
590 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
591 else avfilter_inout_free(&open_outputs);
592 avfilter_inout_free(&curr_inputs);
595 for (; graph->filter_count > 0; graph->filter_count--)
596 avfilter_free(graph->filters[graph->filter_count - 1]);
597 av_freep(&graph->filters);