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"
28 #include "avfiltergraph.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, "]");
67 av_log(log_ctx, AV_LOG_ERROR,
68 "Bad (empty?) label found in the following: \"%s\".\n", start);
72 if (*(*buf)++ != ']') {
73 av_log(log_ctx, AV_LOG_ERROR,
74 "Mismatched '[' found in the following: \"%s\".\n", start);
83 * Create an instance of a filter, initialize and insert it in the
84 * filtergraph in *ctx.
86 * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
87 * @param ctx the filtergraph context
88 * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
89 * @param filt_name the name of the filter to create
90 * @param args the arguments provided to the filter during its initialization
91 * @param log_ctx the log context to use
92 * @return 0 in case of success, a negative AVERROR code otherwise
94 static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
95 const char *filt_name, const char *args, void *log_ctx)
102 snprintf(inst_name, sizeof(inst_name), "Parsed_%s_%d", filt_name, index);
104 filt = avfilter_get_by_name(filt_name);
107 av_log(log_ctx, AV_LOG_ERROR,
108 "No such filter: '%s'\n", filt_name);
109 return AVERROR(EINVAL);
112 ret = avfilter_open(filt_ctx, filt, inst_name);
114 av_log(log_ctx, AV_LOG_ERROR,
115 "Error creating filter '%s'\n", filt_name);
119 if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
120 avfilter_free(*filt_ctx);
124 if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags")) {
125 snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
126 args, ctx->scale_sws_opts);
130 if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
131 av_log(log_ctx, AV_LOG_ERROR,
132 "Error initializing filter '%s' with args '%s'\n", filt_name, args);
140 * Parse a string of the form FILTER_NAME[=PARAMS], and create a
141 * corresponding filter instance which is added to graph with
144 * @param filt_ctx Pointer that is set to the created and configured filter
145 * context on success, set to NULL on failure.
146 * @param filt_ctx put here a pointer to the created filter context on
147 * success, NULL otherwise
148 * @param buf pointer to the buffer to parse, *buf will be updated to
149 * point to the char next after the parsed string
150 * @param index an index which is assigned to the created filter
151 * instance, and which is supposed to be unique for each filter
152 * instance added to the filtergraph
153 * @return 0 in case of success, a negative AVERROR code otherwise
155 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
156 int index, void *log_ctx)
159 char *name = av_get_token(buf, "=,;[\n");
164 opts = av_get_token(buf, "[],;\n");
167 ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
173 AVFilterInOut *avfilter_inout_alloc(void)
175 return av_mallocz(sizeof(AVFilterInOut));
178 void avfilter_inout_free(AVFilterInOut **inout)
181 AVFilterInOut *next = (*inout)->next;
182 av_freep(&(*inout)->name);
188 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
192 while (*links && (!(*links)->name || strcmp((*links)->name, label)))
193 links = &((*links)->next);
205 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
207 element->next = *inouts;
211 static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
213 while (*inouts && (*inouts)->next)
214 inouts = &((*inouts)->next);
219 (*inouts)->next = *element;
223 static int link_filter_inouts(AVFilterContext *filt_ctx,
224 AVFilterInOut **curr_inputs,
225 AVFilterInOut **open_inputs, void *log_ctx)
229 for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
230 AVFilterInOut *p = *curr_inputs;
233 *curr_inputs = (*curr_inputs)->next;
235 } else if (!(p = av_mallocz(sizeof(*p))))
236 return AVERROR(ENOMEM);
239 if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
244 p->filter_ctx = filt_ctx;
246 append_inout(open_inputs, &p);
251 av_log(log_ctx, AV_LOG_ERROR,
252 "Too many inputs specified for the \"%s\" filter.\n",
253 filt_ctx->filter->name);
254 return AVERROR(EINVAL);
257 pad = filt_ctx->nb_outputs;
259 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
261 return AVERROR(ENOMEM);
262 currlinkn->filter_ctx = filt_ctx;
263 currlinkn->pad_idx = pad;
264 insert_inout(curr_inputs, currlinkn);
270 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
271 AVFilterInOut **open_outputs, void *log_ctx)
273 AVFilterInOut *parsed_inputs = NULL;
276 while (**buf == '[') {
277 char *name = parse_link_name(buf, log_ctx);
278 AVFilterInOut *match;
281 return AVERROR(EINVAL);
283 /* First check if the label is not in the open_outputs list */
284 match = extract_inout(name, open_outputs);
289 /* Not in the list, so add it as an input */
290 if (!(match = av_mallocz(sizeof(AVFilterInOut))))
291 return AVERROR(ENOMEM);
293 match->pad_idx = pad;
296 append_inout(&parsed_inputs, &match);
298 *buf += strspn(*buf, WHITESPACES);
302 append_inout(&parsed_inputs, curr_inputs);
303 *curr_inputs = parsed_inputs;
308 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
309 AVFilterInOut **open_inputs,
310 AVFilterInOut **open_outputs, void *log_ctx)
314 while (**buf == '[') {
315 char *name = parse_link_name(buf, log_ctx);
316 AVFilterInOut *match;
318 AVFilterInOut *input = *curr_inputs;
320 av_log(log_ctx, AV_LOG_ERROR,
321 "No output pad can be associated to link label '%s'.\n",
323 return AVERROR(EINVAL);
325 *curr_inputs = (*curr_inputs)->next;
328 return AVERROR(EINVAL);
330 /* First check if the label is not in the open_inputs list */
331 match = extract_inout(name, open_inputs);
334 if ((ret = link_filter(input->filter_ctx, input->pad_idx,
335 match->filter_ctx, match->pad_idx, log_ctx)) < 0)
337 av_free(match->name);
342 /* Not in the list, so add the first input as a open_output */
344 insert_inout(open_outputs, input);
346 *buf += strspn(*buf, WHITESPACES);
353 #if FF_API_GRAPH_AVCLASS
354 #define log_ctx graph
359 static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
361 char *p = strchr(*buf, ';');
363 if (strncmp(*buf, "sws_flags=", 10))
367 av_log(log_ctx, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
368 return AVERROR(EINVAL);
371 *buf += 4; // keep the 'flags=' part
373 av_freep(&graph->scale_sws_opts);
374 if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
375 return AVERROR(ENOMEM);
376 av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
382 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
383 AVFilterInOut **inputs,
384 AVFilterInOut **outputs)
386 int index = 0, ret = 0;
389 AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
391 filters += strspn(filters, WHITESPACES);
393 if ((ret = parse_sws_flags(&filters, graph)) < 0)
397 AVFilterContext *filter;
398 filters += strspn(filters, WHITESPACES);
400 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
403 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
406 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
409 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
413 filters += strspn(filters, WHITESPACES);
416 if (chr == ';' && curr_inputs)
417 append_inout(&open_outputs, &curr_inputs);
419 } while (chr == ',' || chr == ';');
422 av_log(log_ctx, AV_LOG_ERROR,
423 "Unable to parse graph description substring: \"%s\"\n",
425 ret = AVERROR(EINVAL);
429 append_inout(&open_outputs, &curr_inputs);
432 *inputs = open_inputs;
433 *outputs = open_outputs;
437 for (; graph->filter_count > 0; graph->filter_count--)
438 avfilter_free(graph->filters[graph->filter_count - 1]);
439 av_freep(&graph->filters);
440 avfilter_inout_free(&open_inputs);
441 avfilter_inout_free(&open_outputs);
442 avfilter_inout_free(&curr_inputs);
451 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
452 AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
457 AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
458 AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
459 AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
461 if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
464 /* First input can be omitted if it is "[in]" */
465 if (inputs && !inputs->name)
466 inputs->name = av_strdup("in");
467 for (cur = inputs; cur; cur = cur->next) {
469 av_log(log_ctx, AV_LOG_ERROR,
470 "Not enough inputs specified for the \"%s\" filter.\n",
471 cur->filter_ctx->filter->name);
472 ret = AVERROR(EINVAL);
475 if (!(match = extract_inout(cur->name, &open_outputs)))
477 ret = avfilter_link(match->filter_ctx, match->pad_idx,
478 cur->filter_ctx, cur->pad_idx);
479 avfilter_inout_free(&match);
484 /* Last output can be omitted if it is "[out]" */
485 if (outputs && !outputs->name)
486 outputs->name = av_strdup("out");
487 for (cur = outputs; cur; cur = cur->next) {
489 av_log(log_ctx, AV_LOG_ERROR,
490 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
492 ret = AVERROR(EINVAL);
495 if (!(match = extract_inout(cur->name, &open_inputs)))
497 ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
498 match->filter_ctx, match->pad_idx);
499 avfilter_inout_free(&match);
506 for (; graph->filter_count > 0; graph->filter_count--)
507 avfilter_free(graph->filters[graph->filter_count - 1]);
508 av_freep(&graph->filters);
510 avfilter_inout_free(&inputs);
511 avfilter_inout_free(&outputs);
512 /* clear open_in/outputs only if not passed as parameters */
513 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
514 else avfilter_inout_free(&open_inputs);
515 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
516 else avfilter_inout_free(&open_outputs);
520 int index = 0, ret = 0;
523 AVFilterInOut *curr_inputs = NULL;
524 AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
525 AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
528 AVFilterContext *filter;
529 const char *filterchain = filters;
530 filters += strspn(filters, WHITESPACES);
532 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
535 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
538 if (filter->input_count == 1 && !curr_inputs && !index) {
539 /* First input pad, assume it is "[in]" if not specified */
540 const char *tmp = "[in]";
541 if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
545 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
548 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
552 filters += strspn(filters, WHITESPACES);
555 if (chr == ';' && curr_inputs) {
556 av_log(log_ctx, AV_LOG_ERROR,
557 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
559 ret = AVERROR(EINVAL);
563 } while (chr == ',' || chr == ';');
566 av_log(log_ctx, AV_LOG_ERROR,
567 "Unable to parse graph description substring: \"%s\"\n",
569 ret = AVERROR(EINVAL);
574 /* Last output pad, assume it is "[out]" if not specified */
575 const char *tmp = "[out]";
576 if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
582 /* clear open_in/outputs only if not passed as parameters */
583 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
584 else avfilter_inout_free(&open_inputs);
585 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
586 else avfilter_inout_free(&open_outputs);
587 avfilter_inout_free(&curr_inputs);
590 for (; graph->filter_count > 0; graph->filter_count--)
591 avfilter_free(graph->filters[graph->filter_count - 1]);
592 av_freep(&graph->filters);