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 "graphparser.h"
29 #include "avfiltergraph.h"
30 #include "parseutils.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, AVClass *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 ctx the filtergraph context
89 * @param put here a filter context in case of successful creation and configuration, NULL otherwise.
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, AVClass *log_ctx)
104 snprintf(inst_name, sizeof(inst_name), "Filter %d %s", index, filt_name);
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_destroy(*filt_ctx);
126 if (!strcmp(filt_name, "scale") && !strstr(args, "flags")) {
127 snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
128 args, ctx->scale_sws_opts);
132 if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
133 av_log(log_ctx, AV_LOG_ERROR,
134 "Error initializing filter '%s' with args '%s'\n", filt_name, args);
142 * Parse a string of the form FILTER_NAME[=PARAMS], and create a
143 * corresponding filter instance which is added to graph with
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, AVClass *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 static void free_inout(AVFilterInOut *head)
176 AVFilterInOut *next = head->next;
183 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
187 while (*links && strcmp((*links)->name, label))
188 links = &((*links)->next);
198 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
200 element->next = *inouts;
204 static int link_filter_inouts(AVFilterContext *filt_ctx,
205 AVFilterInOut **curr_inputs,
206 AVFilterInOut **open_inputs, AVClass *log_ctx)
208 int pad = filt_ctx->input_count, ret;
211 AVFilterInOut *p = *curr_inputs;
213 av_log(log_ctx, AV_LOG_ERROR,
214 "Not enough inputs specified for the \"%s\" filter.\n",
215 filt_ctx->filter->name);
216 return AVERROR(EINVAL);
219 *curr_inputs = (*curr_inputs)->next;
222 if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
227 p->filter_ctx = filt_ctx;
229 insert_inout(open_inputs, p);
234 av_log(log_ctx, AV_LOG_ERROR,
235 "Too many inputs specified for the \"%s\" filter.\n",
236 filt_ctx->filter->name);
237 return AVERROR(EINVAL);
240 pad = filt_ctx->output_count;
242 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
244 return AVERROR(ENOMEM);
245 currlinkn->filter_ctx = filt_ctx;
246 currlinkn->pad_idx = pad;
247 insert_inout(curr_inputs, currlinkn);
253 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
254 AVFilterInOut **open_outputs, AVClass *log_ctx)
258 while (**buf == '[') {
259 char *name = parse_link_name(buf, log_ctx);
260 AVFilterInOut *match;
263 return AVERROR(EINVAL);
265 /* First check if the label is not in the open_outputs list */
266 match = extract_inout(name, open_outputs);
271 /* Not in the list, so add it as an input */
272 if (!(match = av_mallocz(sizeof(AVFilterInOut))))
273 return AVERROR(ENOMEM);
275 match->pad_idx = pad;
278 insert_inout(curr_inputs, match);
280 *buf += strspn(*buf, WHITESPACES);
287 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
288 AVFilterInOut **open_inputs,
289 AVFilterInOut **open_outputs, AVClass *log_ctx)
293 while (**buf == '[') {
294 char *name = parse_link_name(buf, log_ctx);
295 AVFilterInOut *match;
297 AVFilterInOut *input = *curr_inputs;
298 *curr_inputs = (*curr_inputs)->next;
301 return AVERROR(EINVAL);
303 /* First check if the label is not in the open_inputs list */
304 match = extract_inout(name, open_inputs);
307 if ((ret = link_filter(input->filter_ctx, input->pad_idx,
308 match->filter_ctx, match->pad_idx, log_ctx)) < 0)
310 av_free(match->name);
315 /* Not in the list, so add the first input as a open_output */
317 insert_inout(open_outputs, input);
319 *buf += strspn(*buf, WHITESPACES);
326 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
327 AVFilterInOut *open_inputs,
328 AVFilterInOut *open_outputs, AVClass *log_ctx)
333 AVFilterInOut *curr_inputs = NULL;
336 AVFilterContext *filter;
337 filters += strspn(filters, WHITESPACES);
339 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
342 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
345 if (filter->input_count == 1 && !curr_inputs && !index) {
346 /* First input can be omitted if it is "[in]" */
347 const char *tmp = "[in]";
348 if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
352 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
355 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
359 filters += strspn(filters, WHITESPACES);
362 if (chr == ';' && curr_inputs) {
363 av_log(log_ctx, AV_LOG_ERROR,
364 "Could not find a output to link when parsing \"%s\"\n",
366 ret = AVERROR(EINVAL);
370 } while (chr == ',' || chr == ';');
373 av_log(log_ctx, AV_LOG_ERROR,
374 "Unable to parse graph description substring: \"%s\"\n",
376 ret = AVERROR(EINVAL);
380 if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
381 /* Last output can be omitted if it is "[out]" */
382 const char *tmp = "[out]";
383 if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
391 avfilter_graph_free(graph);
392 free_inout(open_inputs);
393 free_inout(open_outputs);
394 free_inout(curr_inputs);