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) {
123 tmp_args = av_asprintf("%s:%s",
124 args, ctx->scale_sws_opts);
126 return AVERROR(ENOMEM);
130 ret = avfilter_init_str(*filt_ctx, args);
132 av_log(log_ctx, AV_LOG_ERROR,
133 "Error initializing filter '%s'", filt_name);
135 av_log(log_ctx, AV_LOG_ERROR, " with args '%s'", args);
136 av_log(log_ctx, AV_LOG_ERROR, "\n");
137 avfilter_free(*filt_ctx);
146 * Parse a string of the form FILTER_NAME[=PARAMS], and create a
147 * corresponding filter instance which is added to graph with
150 * @param filt_ctx Pointer that is set to the created and configured filter
151 * context on success, set to NULL on failure.
152 * @param filt_ctx put here a pointer to the created filter context on
153 * success, NULL otherwise
154 * @param buf pointer to the buffer to parse, *buf will be updated to
155 * point to the char next after the parsed string
156 * @param index an index which is assigned to the created filter
157 * instance, and which is supposed to be unique for each filter
158 * instance added to the filtergraph
159 * @return >= 0 in case of success, a negative AVERROR code otherwise
161 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
162 int index, void *log_ctx)
165 char *name = av_get_token(buf, "=,;[\n");
170 opts = av_get_token(buf, "[],;\n");
173 ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
179 AVFilterInOut *avfilter_inout_alloc(void)
181 return av_mallocz(sizeof(AVFilterInOut));
184 void avfilter_inout_free(AVFilterInOut **inout)
187 AVFilterInOut *next = (*inout)->next;
188 av_freep(&(*inout)->name);
194 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
198 while (*links && (!(*links)->name || strcmp((*links)->name, label)))
199 links = &((*links)->next);
211 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
213 element->next = *inouts;
217 static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
219 while (*inouts && (*inouts)->next)
220 inouts = &((*inouts)->next);
225 (*inouts)->next = *element;
229 static int link_filter_inouts(AVFilterContext *filt_ctx,
230 AVFilterInOut **curr_inputs,
231 AVFilterInOut **open_inputs, void *log_ctx)
235 for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
236 AVFilterInOut *p = *curr_inputs;
239 *curr_inputs = (*curr_inputs)->next;
241 } else if (!(p = av_mallocz(sizeof(*p))))
242 return AVERROR(ENOMEM);
245 ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
251 p->filter_ctx = filt_ctx;
253 append_inout(open_inputs, &p);
258 av_log(log_ctx, AV_LOG_ERROR,
259 "Too many inputs specified for the \"%s\" filter.\n",
260 filt_ctx->filter->name);
261 return AVERROR(EINVAL);
264 pad = filt_ctx->nb_outputs;
266 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
268 return AVERROR(ENOMEM);
269 currlinkn->filter_ctx = filt_ctx;
270 currlinkn->pad_idx = pad;
271 insert_inout(curr_inputs, currlinkn);
277 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
278 AVFilterInOut **open_outputs, void *log_ctx)
280 AVFilterInOut *parsed_inputs = NULL;
283 while (**buf == '[') {
284 char *name = parse_link_name(buf, log_ctx);
285 AVFilterInOut *match;
288 return AVERROR(EINVAL);
290 /* First check if the label is not in the open_outputs list */
291 match = extract_inout(name, open_outputs);
296 /* Not in the list, so add it as an input */
297 if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
299 return AVERROR(ENOMEM);
302 match->pad_idx = pad;
305 append_inout(&parsed_inputs, &match);
307 *buf += strspn(*buf, WHITESPACES);
311 append_inout(&parsed_inputs, curr_inputs);
312 *curr_inputs = parsed_inputs;
317 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
318 AVFilterInOut **open_inputs,
319 AVFilterInOut **open_outputs, void *log_ctx)
323 while (**buf == '[') {
324 char *name = parse_link_name(buf, log_ctx);
325 AVFilterInOut *match;
327 AVFilterInOut *input = *curr_inputs;
330 return AVERROR(EINVAL);
333 av_log(log_ctx, AV_LOG_ERROR,
334 "No output pad can be associated to link label '%s'.\n", name);
336 return AVERROR(EINVAL);
338 *curr_inputs = (*curr_inputs)->next;
340 /* First check if the label is not in the open_inputs list */
341 match = extract_inout(name, open_inputs);
344 if ((ret = link_filter(input->filter_ctx, input->pad_idx,
345 match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
349 av_freep(&match->name);
354 /* Not in the list, so add the first input as a open_output */
356 insert_inout(open_outputs, input);
358 *buf += strspn(*buf, WHITESPACES);
365 static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
367 char *p = strchr(*buf, ';');
369 if (strncmp(*buf, "sws_flags=", 10))
373 av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
374 return AVERROR(EINVAL);
377 *buf += 4; // keep the 'flags=' part
379 av_freep(&graph->scale_sws_opts);
380 if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
381 return AVERROR(ENOMEM);
382 av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
388 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
389 AVFilterInOut **inputs,
390 AVFilterInOut **outputs)
392 int index = 0, ret = 0;
395 AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
397 filters += strspn(filters, WHITESPACES);
399 if ((ret = parse_sws_flags(&filters, graph)) < 0)
403 AVFilterContext *filter;
404 filters += strspn(filters, WHITESPACES);
406 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
408 if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
412 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
415 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
419 filters += strspn(filters, WHITESPACES);
422 if (chr == ';' && curr_inputs)
423 append_inout(&open_outputs, &curr_inputs);
425 } while (chr == ',' || chr == ';');
428 av_log(graph, AV_LOG_ERROR,
429 "Unable to parse graph description substring: \"%s\"\n",
431 ret = AVERROR(EINVAL);
435 append_inout(&open_outputs, &curr_inputs);
438 *inputs = open_inputs;
439 *outputs = open_outputs;
443 while (graph->nb_filters)
444 avfilter_free(graph->filters[0]);
445 av_freep(&graph->filters);
446 avfilter_inout_free(&open_inputs);
447 avfilter_inout_free(&open_outputs);
448 avfilter_inout_free(&curr_inputs);
456 #if HAVE_INCOMPATIBLE_LIBAV_ABI || !FF_API_OLD_GRAPH_PARSE
457 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
458 AVFilterInOut *open_inputs,
459 AVFilterInOut *open_outputs, void *log_ctx)
462 AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
464 if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
467 /* First input can be omitted if it is "[in]" */
468 if (inputs && !inputs->name)
469 inputs->name = av_strdup("in");
470 for (cur = inputs; cur; cur = cur->next) {
472 av_log(log_ctx, AV_LOG_ERROR,
473 "Not enough inputs specified for the \"%s\" filter.\n",
474 cur->filter_ctx->filter->name);
475 ret = AVERROR(EINVAL);
478 if (!(match = extract_inout(cur->name, &open_outputs)))
480 ret = avfilter_link(match->filter_ctx, match->pad_idx,
481 cur->filter_ctx, cur->pad_idx);
482 avfilter_inout_free(&match);
487 /* Last output can be omitted if it is "[out]" */
488 if (outputs && !outputs->name)
489 outputs->name = av_strdup("out");
490 for (cur = outputs; cur; cur = cur->next) {
492 av_log(log_ctx, AV_LOG_ERROR,
493 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
495 ret = AVERROR(EINVAL);
498 if (!(match = extract_inout(cur->name, &open_inputs)))
500 ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
501 match->filter_ctx, match->pad_idx);
502 avfilter_inout_free(&match);
509 while (graph->nb_filters)
510 avfilter_free(graph->filters[0]);
511 av_freep(&graph->filters);
513 avfilter_inout_free(&inputs);
514 avfilter_inout_free(&outputs);
515 avfilter_inout_free(&open_inputs);
516 avfilter_inout_free(&open_outputs);
519 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
520 AVFilterInOut **inputs, AVFilterInOut **outputs,
523 return avfilter_graph_parse_ptr(graph, filters, inputs, outputs, log_ctx);
527 int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
528 AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
531 int index = 0, ret = 0;
534 AVFilterInOut *curr_inputs = NULL;
535 AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
536 AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
538 if ((ret = parse_sws_flags(&filters, graph)) < 0)
542 AVFilterContext *filter;
543 const char *filterchain = filters;
544 filters += strspn(filters, WHITESPACES);
546 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
549 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
552 if (filter->nb_inputs == 1 && !curr_inputs && !index) {
553 /* First input pad, assume it is "[in]" if not specified */
554 const char *tmp = "[in]";
555 if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
559 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
562 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
566 filters += strspn(filters, WHITESPACES);
569 if (chr == ';' && curr_inputs) {
570 av_log(log_ctx, AV_LOG_ERROR,
571 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
573 ret = AVERROR(EINVAL);
577 } while (chr == ',' || chr == ';');
580 av_log(log_ctx, AV_LOG_ERROR,
581 "Unable to parse graph description substring: \"%s\"\n",
583 ret = AVERROR(EINVAL);
588 /* Last output pad, assume it is "[out]" if not specified */
589 const char *tmp = "[out]";
590 if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
596 /* clear open_in/outputs only if not passed as parameters */
597 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
598 else avfilter_inout_free(&open_inputs);
599 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
600 else avfilter_inout_free(&open_outputs);
601 avfilter_inout_free(&curr_inputs);
604 while (graph->nb_filters)
605 avfilter_free(graph->filters[0]);
606 av_freep(&graph->filters);