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);
50 * @param name a pointer (that need to be free'd after use) to the name between
53 static char *parse_link_name(const char **buf, AVClass *log_ctx)
55 const char *start = *buf;
59 name = av_get_token(buf, "]");
62 av_log(log_ctx, AV_LOG_ERROR,
63 "Bad (empty?) label found in the following: \"%s\".\n", start);
67 if (*(*buf)++ != ']') {
68 av_log(log_ctx, AV_LOG_ERROR,
69 "Mismatched '[' found in the following: \"%s\".\n", start);
77 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
78 const char *filt_name, const char *args,
81 AVFilterContext *filt_ctx;
87 snprintf(inst_name, sizeof(inst_name), "Filter %d %s", index, filt_name);
89 filt = avfilter_get_by_name(filt_name);
92 av_log(log_ctx, AV_LOG_ERROR,
93 "No such filter: '%s'\n", filt_name);
97 filt_ctx = avfilter_open(filt, inst_name);
99 av_log(log_ctx, AV_LOG_ERROR,
100 "Error creating filter '%s'\n", filt_name);
104 if (avfilter_graph_add_filter(ctx, filt_ctx) < 0) {
105 avfilter_destroy(filt_ctx);
109 if (!strcmp(filt_name, "scale") && !strstr(args, "flags")) {
110 snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
111 args, ctx->scale_sws_opts);
115 if (avfilter_init_filter(filt_ctx, args, NULL)) {
116 av_log(log_ctx, AV_LOG_ERROR,
117 "Error initializing filter '%s' with args '%s'\n", filt_name, args);
125 * Parse "filter=params"
127 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
128 int index, AVClass *log_ctx)
131 char *name = av_get_token(buf, "=,;[\n");
132 AVFilterContext *ret;
136 opts = av_get_token(buf, "[],;\n");
139 ret = create_filter(graph, index, name, opts, log_ctx);
145 static void free_inout(AVFilterInOut *head)
148 AVFilterInOut *next = head->next;
155 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
159 while (*links && strcmp((*links)->name, label))
160 links = &((*links)->next);
170 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
172 element->next = *inouts;
176 static int link_filter_inouts(AVFilterContext *filter,
177 AVFilterInOut **curr_inputs,
178 AVFilterInOut **open_inputs, AVClass *log_ctx)
180 int pad = filter->input_count;
183 AVFilterInOut *p = *curr_inputs;
185 av_log(log_ctx, AV_LOG_ERROR,
186 "Not enough inputs specified for the \"%s\" filter.\n",
187 filter->filter->name);
191 *curr_inputs = (*curr_inputs)->next;
194 if (link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
201 insert_inout(open_inputs, p);
206 av_log(log_ctx, AV_LOG_ERROR,
207 "Too many inputs specified for the \"%s\" filter.\n",
208 filter->filter->name);
212 pad = filter->output_count;
214 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
215 currlinkn->filter = filter;
216 currlinkn->pad_idx = pad;
217 insert_inout(curr_inputs, currlinkn);
223 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
224 AVFilterInOut **open_outputs, AVClass *log_ctx)
228 while (**buf == '[') {
229 char *name = parse_link_name(buf, log_ctx);
230 AVFilterInOut *match;
235 /* First check if the label is not in the open_outputs list */
236 match = extract_inout(name, open_outputs);
241 /* Not in the list, so add it as an input */
242 match = av_mallocz(sizeof(AVFilterInOut));
244 match->pad_idx = pad;
247 insert_inout(curr_inputs, match);
249 *buf += strspn(*buf, WHITESPACES);
256 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
257 AVFilterInOut **open_inputs,
258 AVFilterInOut **open_outputs, AVClass *log_ctx)
262 while (**buf == '[') {
263 char *name = parse_link_name(buf, log_ctx);
264 AVFilterInOut *match;
266 AVFilterInOut *input = *curr_inputs;
267 *curr_inputs = (*curr_inputs)->next;
272 /* First check if the label is not in the open_inputs list */
273 match = extract_inout(name, open_inputs);
276 if (link_filter(input->filter, input->pad_idx,
277 match->filter, match->pad_idx, log_ctx) < 0)
279 av_free(match->name);
284 /* Not in the list, so add the first input as a open_output */
286 insert_inout(open_outputs, input);
288 *buf += strspn(*buf, WHITESPACES);
295 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
296 AVFilterInOut *open_inputs,
297 AVFilterInOut *open_outputs, AVClass *log_ctx)
302 AVFilterInOut *curr_inputs = NULL;
305 AVFilterContext *filter;
306 filters += strspn(filters, WHITESPACES);
308 if (parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
311 filter = parse_filter(&filters, graph, index, log_ctx);
316 if (filter->input_count == 1 && !curr_inputs && !index) {
317 /* First input can be omitted if it is "[in]" */
318 const char *tmp = "[in]";
319 if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
323 if (link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
326 if (parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
330 filters += strspn(filters, WHITESPACES);
333 if (chr == ';' && curr_inputs) {
334 av_log(log_ctx, AV_LOG_ERROR,
335 "Could not find a output to link when parsing \"%s\"\n",
340 } while (chr == ',' || chr == ';');
343 av_log(log_ctx, AV_LOG_ERROR,
344 "Unable to parse graph description substring: \"%s\"\n",
349 if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
350 /* Last output can be omitted if it is "[out]" */
351 const char *tmp = "[out]";
352 if (parse_outputs(&tmp, &curr_inputs, &open_inputs,
353 &open_outputs, log_ctx) < 0)
360 avfilter_graph_destroy(graph);
361 free_inout(open_inputs);
362 free_inout(open_outputs);
363 free_inout(curr_inputs);