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);
75 av_log(log_ctx, AV_LOG_ERROR,
76 "Mismatched '[' found in the following: \"%s\".\n", start);
87 * Create an instance of a filter, initialize and insert it in the
88 * filtergraph in *ctx.
90 * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
91 * @param ctx the filtergraph context
92 * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
93 * @param name the name of the filter to create, can be filter name or filter_name\@id as instance name
94 * @param args the arguments provided to the filter during its initialization
95 * @param log_ctx the log context to use
96 * @return >= 0 in case of success, a negative AVERROR code otherwise
98 static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
99 const char *name, const char *args, void *log_ctx)
101 const AVFilter *filt;
103 const char *inst_name = NULL, *filt_name = NULL;
104 char *tmp_args = NULL;
107 av_strlcpy(name2, name, sizeof(name2));
109 for (k = 0; name2[k]; k++) {
110 if (name2[k] == '@' && name[k+1]) {
119 snprintf(name2, sizeof(name2), "Parsed_%s_%d", name, index);
124 filt = avfilter_get_by_name(filt_name);
127 av_log(log_ctx, AV_LOG_ERROR,
128 "No such filter: '%s'\n", filt_name);
129 return AVERROR(EINVAL);
132 *filt_ctx = avfilter_graph_alloc_filter(ctx, filt, inst_name);
134 av_log(log_ctx, AV_LOG_ERROR,
135 "Error creating filter '%s'\n", filt_name);
136 return AVERROR(ENOMEM);
139 if (!strcmp(filt_name, "scale") && (!args || !strstr(args, "flags")) &&
140 ctx->scale_sws_opts) {
142 tmp_args = av_asprintf("%s:%s",
143 args, ctx->scale_sws_opts);
145 return AVERROR(ENOMEM);
148 args = ctx->scale_sws_opts;
151 ret = avfilter_init_str(*filt_ctx, args);
153 av_log(log_ctx, AV_LOG_ERROR,
154 "Error initializing filter '%s'", filt_name);
156 av_log(log_ctx, AV_LOG_ERROR, " with args '%s'", args);
157 av_log(log_ctx, AV_LOG_ERROR, "\n");
158 avfilter_free(*filt_ctx);
167 * Parse a string of the form FILTER_NAME[=PARAMS], and create a
168 * corresponding filter instance which is added to graph with
171 * @param filt_ctx Pointer that is set to the created and configured filter
172 * context on success, set to NULL on failure.
173 * @param filt_ctx put here a pointer to the created filter context on
174 * success, NULL otherwise
175 * @param buf pointer to the buffer to parse, *buf will be updated to
176 * point to the char next after the parsed string
177 * @param index an index which is assigned to the created filter
178 * instance, and which is supposed to be unique for each filter
179 * instance added to the filtergraph
180 * @return >= 0 in case of success, a negative AVERROR code otherwise
182 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
183 int index, void *log_ctx)
186 char *name = av_get_token(buf, "=,;[");
190 return AVERROR(ENOMEM);
194 opts = av_get_token(buf, "[],;");
197 return AVERROR(ENOMEM);
201 ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
207 AVFilterInOut *avfilter_inout_alloc(void)
209 return av_mallocz(sizeof(AVFilterInOut));
212 void avfilter_inout_free(AVFilterInOut **inout)
215 AVFilterInOut *next = (*inout)->next;
216 av_freep(&(*inout)->name);
222 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
226 while (*links && (!(*links)->name || strcmp((*links)->name, label)))
227 links = &((*links)->next);
239 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
241 element->next = *inouts;
245 static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
247 while (*inouts && (*inouts)->next)
248 inouts = &((*inouts)->next);
253 (*inouts)->next = *element;
257 static int link_filter_inouts(AVFilterContext *filt_ctx,
258 AVFilterInOut **curr_inputs,
259 AVFilterInOut **open_inputs, void *log_ctx)
263 for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
264 AVFilterInOut *p = *curr_inputs;
267 *curr_inputs = (*curr_inputs)->next;
269 } else if (!(p = av_mallocz(sizeof(*p))))
270 return AVERROR(ENOMEM);
273 ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
279 p->filter_ctx = filt_ctx;
281 append_inout(open_inputs, &p);
286 av_log(log_ctx, AV_LOG_ERROR,
287 "Too many inputs specified for the \"%s\" filter.\n",
288 filt_ctx->filter->name);
289 return AVERROR(EINVAL);
292 pad = filt_ctx->nb_outputs;
294 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
296 return AVERROR(ENOMEM);
297 currlinkn->filter_ctx = filt_ctx;
298 currlinkn->pad_idx = pad;
299 insert_inout(curr_inputs, currlinkn);
305 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
306 AVFilterInOut **open_outputs, void *log_ctx)
308 AVFilterInOut *parsed_inputs = NULL;
311 while (**buf == '[') {
312 char *name = parse_link_name(buf, log_ctx);
313 AVFilterInOut *match;
316 avfilter_inout_free(&parsed_inputs);
317 return AVERROR(EINVAL);
320 /* First check if the label is not in the open_outputs list */
321 match = extract_inout(name, open_outputs);
326 /* Not in the list, so add it as an input */
327 if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
328 avfilter_inout_free(&parsed_inputs);
330 return AVERROR(ENOMEM);
333 match->pad_idx = pad;
336 append_inout(&parsed_inputs, &match);
338 *buf += strspn(*buf, WHITESPACES);
342 append_inout(&parsed_inputs, curr_inputs);
343 *curr_inputs = parsed_inputs;
348 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
349 AVFilterInOut **open_inputs,
350 AVFilterInOut **open_outputs, void *log_ctx)
354 while (**buf == '[') {
355 char *name = parse_link_name(buf, log_ctx);
356 AVFilterInOut *match;
358 AVFilterInOut *input = *curr_inputs;
361 return AVERROR(EINVAL);
364 av_log(log_ctx, AV_LOG_ERROR,
365 "No output pad can be associated to link label '%s'.\n", name);
367 return AVERROR(EINVAL);
369 *curr_inputs = (*curr_inputs)->next;
371 /* First check if the label is not in the open_inputs list */
372 match = extract_inout(name, open_inputs);
375 ret = link_filter(input->filter_ctx, input->pad_idx,
376 match->filter_ctx, match->pad_idx, log_ctx);
377 av_freep(&match->name);
384 /* Not in the list, so add the first input as an open_output */
386 insert_inout(open_outputs, input);
388 *buf += strspn(*buf, WHITESPACES);
395 static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
397 char *p = strchr(*buf, ';');
399 if (strncmp(*buf, "sws_flags=", 10))
403 av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
404 return AVERROR(EINVAL);
407 *buf += 4; // keep the 'flags=' part
409 av_freep(&graph->scale_sws_opts);
410 if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
411 return AVERROR(ENOMEM);
412 av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
418 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
419 AVFilterInOut **inputs,
420 AVFilterInOut **outputs)
422 int index = 0, ret = 0;
425 AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
427 filters += strspn(filters, WHITESPACES);
429 if ((ret = parse_sws_flags(&filters, graph)) < 0)
433 AVFilterContext *filter;
434 filters += strspn(filters, WHITESPACES);
436 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
438 if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
442 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
445 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
449 filters += strspn(filters, WHITESPACES);
452 if (chr == ';' && curr_inputs)
453 append_inout(&open_outputs, &curr_inputs);
455 } while (chr == ',' || chr == ';');
458 av_log(graph, AV_LOG_ERROR,
459 "Unable to parse graph description substring: \"%s\"\n",
461 ret = AVERROR(EINVAL);
465 append_inout(&open_outputs, &curr_inputs);
468 *inputs = open_inputs;
469 *outputs = open_outputs;
473 while (graph->nb_filters)
474 avfilter_free(graph->filters[0]);
475 av_freep(&graph->filters);
476 avfilter_inout_free(&open_inputs);
477 avfilter_inout_free(&open_outputs);
478 avfilter_inout_free(&curr_inputs);
486 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
487 AVFilterInOut *open_inputs,
488 AVFilterInOut *open_outputs, void *log_ctx)
491 AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
493 if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
496 /* First input can be omitted if it is "[in]" */
497 if (inputs && !inputs->name)
498 inputs->name = av_strdup("in");
499 for (cur = inputs; cur; cur = cur->next) {
501 av_log(log_ctx, AV_LOG_ERROR,
502 "Not enough inputs specified for the \"%s\" filter.\n",
503 cur->filter_ctx->filter->name);
504 ret = AVERROR(EINVAL);
507 if (!(match = extract_inout(cur->name, &open_outputs)))
509 ret = avfilter_link(match->filter_ctx, match->pad_idx,
510 cur->filter_ctx, cur->pad_idx);
511 avfilter_inout_free(&match);
516 /* Last output can be omitted if it is "[out]" */
517 if (outputs && !outputs->name)
518 outputs->name = av_strdup("out");
519 for (cur = outputs; cur; cur = cur->next) {
521 av_log(log_ctx, AV_LOG_ERROR,
522 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
524 ret = AVERROR(EINVAL);
527 if (!(match = extract_inout(cur->name, &open_inputs)))
529 ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
530 match->filter_ctx, match->pad_idx);
531 avfilter_inout_free(&match);
538 while (graph->nb_filters)
539 avfilter_free(graph->filters[0]);
540 av_freep(&graph->filters);
542 avfilter_inout_free(&inputs);
543 avfilter_inout_free(&outputs);
544 avfilter_inout_free(&open_inputs);
545 avfilter_inout_free(&open_outputs);
549 int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
550 AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
553 int index = 0, ret = 0;
556 AVFilterInOut *curr_inputs = NULL;
557 AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
558 AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
560 if ((ret = parse_sws_flags(&filters, graph)) < 0)
564 AVFilterContext *filter;
565 const char *filterchain = filters;
566 filters += strspn(filters, WHITESPACES);
568 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
571 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
574 if (filter->nb_inputs == 1 && !curr_inputs && !index) {
575 /* First input pad, assume it is "[in]" if not specified */
576 const char *tmp = "[in]";
577 if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
581 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
584 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
588 filters += strspn(filters, WHITESPACES);
591 if (chr == ';' && curr_inputs) {
592 av_log(log_ctx, AV_LOG_ERROR,
593 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
595 ret = AVERROR(EINVAL);
599 } while (chr == ',' || chr == ';');
602 av_log(log_ctx, AV_LOG_ERROR,
603 "Unable to parse graph description substring: \"%s\"\n",
605 ret = AVERROR(EINVAL);
610 /* Last output pad, assume it is "[out]" if not specified */
611 const char *tmp = "[out]";
612 if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
618 /* clear open_in/outputs only if not passed as parameters */
619 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
620 else avfilter_inout_free(&open_inputs);
621 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
622 else avfilter_inout_free(&open_outputs);
623 avfilter_inout_free(&curr_inputs);
626 while (graph->nb_filters)
627 avfilter_free(graph->filters[0]);
628 av_freep(&graph->filters);