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, AVClass *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 ctx the filtergraph context
87 * @param put here a filter context in case of successful creation and configuration, NULL otherwise.
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, AVClass *log_ctx)
102 snprintf(inst_name, sizeof(inst_name), "Filter %d %s", index, filt_name);
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") && !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 put here a pointer to the created filter context on
145 * success, NULL otherwise
146 * @param buf pointer to the buffer to parse, *buf will be updated to
147 * point to the char next after the parsed string
148 * @param index an index which is assigned to the created filter
149 * instance, and which is supposed to be unique for each filter
150 * instance added to the filtergraph
151 * @return 0 in case of success, a negative AVERROR code otherwise
153 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
154 int index, AVClass *log_ctx)
157 char *name = av_get_token(buf, "=,;[\n");
162 opts = av_get_token(buf, "[],;\n");
165 ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
171 static void free_inout(AVFilterInOut *head)
174 AVFilterInOut *next = head->next;
181 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
185 while (*links && strcmp((*links)->name, label))
186 links = &((*links)->next);
196 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
198 element->next = *inouts;
202 static int link_filter_inouts(AVFilterContext *filt_ctx,
203 AVFilterInOut **curr_inputs,
204 AVFilterInOut **open_inputs, AVClass *log_ctx)
206 int pad = filt_ctx->input_count, ret;
209 AVFilterInOut *p = *curr_inputs;
211 av_log(log_ctx, AV_LOG_ERROR,
212 "Not enough inputs specified for the \"%s\" filter.\n",
213 filt_ctx->filter->name);
214 return AVERROR(EINVAL);
217 *curr_inputs = (*curr_inputs)->next;
220 if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
225 p->filter_ctx = filt_ctx;
227 insert_inout(open_inputs, p);
232 av_log(log_ctx, AV_LOG_ERROR,
233 "Too many inputs specified for the \"%s\" filter.\n",
234 filt_ctx->filter->name);
235 return AVERROR(EINVAL);
238 pad = filt_ctx->output_count;
240 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
242 return AVERROR(ENOMEM);
243 currlinkn->filter_ctx = filt_ctx;
244 currlinkn->pad_idx = pad;
245 insert_inout(curr_inputs, currlinkn);
251 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
252 AVFilterInOut **open_outputs, AVClass *log_ctx)
256 while (**buf == '[') {
257 char *name = parse_link_name(buf, log_ctx);
258 AVFilterInOut *match;
261 return AVERROR(EINVAL);
263 /* First check if the label is not in the open_outputs list */
264 match = extract_inout(name, open_outputs);
269 /* Not in the list, so add it as an input */
270 if (!(match = av_mallocz(sizeof(AVFilterInOut))))
271 return AVERROR(ENOMEM);
273 match->pad_idx = pad;
276 insert_inout(curr_inputs, match);
278 *buf += strspn(*buf, WHITESPACES);
285 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
286 AVFilterInOut **open_inputs,
287 AVFilterInOut **open_outputs, AVClass *log_ctx)
291 while (**buf == '[') {
292 char *name = parse_link_name(buf, log_ctx);
293 AVFilterInOut *match;
295 AVFilterInOut *input = *curr_inputs;
297 av_log(log_ctx, AV_LOG_ERROR,
298 "No output pad can be associated to link label '%s'.\n",
300 return AVERROR(EINVAL);
302 *curr_inputs = (*curr_inputs)->next;
305 return AVERROR(EINVAL);
307 /* First check if the label is not in the open_inputs list */
308 match = extract_inout(name, open_inputs);
311 if ((ret = link_filter(input->filter_ctx, input->pad_idx,
312 match->filter_ctx, match->pad_idx, log_ctx)) < 0)
314 av_free(match->name);
319 /* Not in the list, so add the first input as a open_output */
321 insert_inout(open_outputs, input);
323 *buf += strspn(*buf, WHITESPACES);
330 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
331 AVFilterInOut *open_inputs,
332 AVFilterInOut *open_outputs, AVClass *log_ctx)
337 AVFilterInOut *curr_inputs = NULL;
340 AVFilterContext *filter;
341 const char *filterchain = filters;
342 filters += strspn(filters, WHITESPACES);
344 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
347 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
350 if (filter->input_count == 1 && !curr_inputs && !index) {
351 /* First input can be omitted if it is "[in]" */
352 const char *tmp = "[in]";
353 if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
357 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
360 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
364 filters += strspn(filters, WHITESPACES);
367 if (chr == ';' && curr_inputs) {
368 av_log(log_ctx, AV_LOG_ERROR,
369 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
371 ret = AVERROR(EINVAL);
375 } while (chr == ',' || chr == ';');
378 av_log(log_ctx, AV_LOG_ERROR,
379 "Unable to parse graph description substring: \"%s\"\n",
381 ret = AVERROR(EINVAL);
385 if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
386 /* Last output can be omitted if it is "[out]" */
387 const char *tmp = "[out]";
388 if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
396 avfilter_graph_free(graph);
397 free_inout(open_inputs);
398 free_inout(open_outputs);
399 free_inout(curr_inputs);