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 "graphparser.h"
28 #include "avfiltergraph.h"
29 #include "parseutils.h"
31 #define WHITESPACES " \n\t"
33 static int link_filter(AVFilterContext *src, int srcpad,
34 AVFilterContext *dst, int dstpad,
38 if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
39 av_log(log_ctx, AV_LOG_ERROR,
40 "Cannot create the link %s:%d -> %s:%d\n",
41 src->filter->name, srcpad, dst->filter->name, dstpad);
49 * Parse the name of a link, which has the format "[linkname]".
51 * @return a pointer (that need to be freed after use) to the name
54 static char *parse_link_name(const char **buf, AVClass *log_ctx)
56 const char *start = *buf;
60 name = av_get_token(buf, "]");
63 av_log(log_ctx, AV_LOG_ERROR,
64 "Bad (empty?) label found in the following: \"%s\".\n", start);
68 if (*(*buf)++ != ']') {
69 av_log(log_ctx, AV_LOG_ERROR,
70 "Mismatched '[' found in the following: \"%s\".\n", start);
78 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
79 const char *filt_name, const char *args,
82 AVFilterContext *filt_ctx;
88 snprintf(inst_name, sizeof(inst_name), "Filter %d %s", index, filt_name);
90 filt = avfilter_get_by_name(filt_name);
93 av_log(log_ctx, AV_LOG_ERROR,
94 "No such filter: '%s'\n", filt_name);
98 filt_ctx = avfilter_open(filt, inst_name);
100 av_log(log_ctx, AV_LOG_ERROR,
101 "Error creating filter '%s'\n", filt_name);
105 if (avfilter_graph_add_filter(ctx, filt_ctx) < 0) {
106 avfilter_destroy(filt_ctx);
110 if (!strcmp(filt_name, "scale") && !strstr(args, "flags")) {
111 snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
112 args, ctx->scale_sws_opts);
116 if (avfilter_init_filter(filt_ctx, args, NULL)) {
117 av_log(log_ctx, AV_LOG_ERROR,
118 "Error initializing filter '%s' with args '%s'\n", filt_name, args);
126 * Parse "filter=params"
128 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
129 int index, AVClass *log_ctx)
132 char *name = av_get_token(buf, "=,;[\n");
133 AVFilterContext *ret;
137 opts = av_get_token(buf, "[],;\n");
140 ret = create_filter(graph, index, name, opts, log_ctx);
146 static void free_inout(AVFilterInOut *head)
149 AVFilterInOut *next = head->next;
156 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
160 while (*links && strcmp((*links)->name, label))
161 links = &((*links)->next);
171 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
173 element->next = *inouts;
177 static int link_filter_inouts(AVFilterContext *filter,
178 AVFilterInOut **curr_inputs,
179 AVFilterInOut **open_inputs, AVClass *log_ctx)
181 int pad = filter->input_count;
184 AVFilterInOut *p = *curr_inputs;
186 av_log(log_ctx, AV_LOG_ERROR,
187 "Not enough inputs specified for the \"%s\" filter.\n",
188 filter->filter->name);
192 *curr_inputs = (*curr_inputs)->next;
195 if (link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
202 insert_inout(open_inputs, p);
207 av_log(log_ctx, AV_LOG_ERROR,
208 "Too many inputs specified for the \"%s\" filter.\n",
209 filter->filter->name);
213 pad = filter->output_count;
215 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
216 currlinkn->filter = filter;
217 currlinkn->pad_idx = pad;
218 insert_inout(curr_inputs, currlinkn);
224 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
225 AVFilterInOut **open_outputs, AVClass *log_ctx)
229 while (**buf == '[') {
230 char *name = parse_link_name(buf, log_ctx);
231 AVFilterInOut *match;
236 /* First check if the label is not in the open_outputs list */
237 match = extract_inout(name, open_outputs);
242 /* Not in the list, so add it as an input */
243 match = av_mallocz(sizeof(AVFilterInOut));
245 match->pad_idx = pad;
248 insert_inout(curr_inputs, match);
250 *buf += strspn(*buf, WHITESPACES);
257 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
258 AVFilterInOut **open_inputs,
259 AVFilterInOut **open_outputs, AVClass *log_ctx)
263 while (**buf == '[') {
264 char *name = parse_link_name(buf, log_ctx);
265 AVFilterInOut *match;
267 AVFilterInOut *input = *curr_inputs;
268 *curr_inputs = (*curr_inputs)->next;
273 /* First check if the label is not in the open_inputs list */
274 match = extract_inout(name, open_inputs);
277 if (link_filter(input->filter, input->pad_idx,
278 match->filter, match->pad_idx, log_ctx) < 0)
280 av_free(match->name);
285 /* Not in the list, so add the first input as a open_output */
287 insert_inout(open_outputs, input);
289 *buf += strspn(*buf, WHITESPACES);
296 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
297 AVFilterInOut *open_inputs,
298 AVFilterInOut *open_outputs, AVClass *log_ctx)
303 AVFilterInOut *curr_inputs = NULL;
306 AVFilterContext *filter;
307 filters += strspn(filters, WHITESPACES);
309 if (parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
312 filter = parse_filter(&filters, graph, index, log_ctx);
317 if (filter->input_count == 1 && !curr_inputs && !index) {
318 /* First input can be omitted if it is "[in]" */
319 const char *tmp = "[in]";
320 if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
324 if (link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
327 if (parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
331 filters += strspn(filters, WHITESPACES);
334 if (chr == ';' && curr_inputs) {
335 av_log(log_ctx, AV_LOG_ERROR,
336 "Could not find a output to link when parsing \"%s\"\n",
341 } while (chr == ',' || chr == ';');
344 av_log(log_ctx, AV_LOG_ERROR,
345 "Unable to parse graph description substring: \"%s\"\n",
350 if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
351 /* Last output can be omitted if it is "[out]" */
352 const char *tmp = "[out]";
353 if (parse_outputs(&tmp, &curr_inputs, &open_inputs,
354 &open_outputs, log_ctx) < 0)
361 avfilter_graph_destroy(graph);
362 free_inout(open_inputs);
363 free_inout(open_outputs);
364 free_inout(curr_inputs);