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"
34 * Link two filters together.
36 * @see avfilter_link()
38 static int link_filter(AVFilterContext *src, int srcpad,
39 AVFilterContext *dst, int dstpad,
43 if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
44 av_log(log_ctx, AV_LOG_ERROR,
45 "Cannot create the link %s:%d -> %s:%d\n",
46 src->filter->name, srcpad, dst->filter->name, dstpad);
54 * Parse the name of a link, which has the format "[linkname]".
56 * @return a pointer (that need to be freed after use) to the name
59 static char *parse_link_name(const char **buf, AVClass *log_ctx)
61 const char *start = *buf;
65 name = av_get_token(buf, "]");
68 av_log(log_ctx, AV_LOG_ERROR,
69 "Bad (empty?) label found in the following: \"%s\".\n", start);
73 if (*(*buf)++ != ']') {
74 av_log(log_ctx, AV_LOG_ERROR,
75 "Mismatched '[' found in the following: \"%s\".\n", start);
83 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
84 const char *filt_name, const char *args,
87 AVFilterContext *filt_ctx;
93 snprintf(inst_name, sizeof(inst_name), "Filter %d %s", index, filt_name);
95 filt = avfilter_get_by_name(filt_name);
98 av_log(log_ctx, AV_LOG_ERROR,
99 "No such filter: '%s'\n", filt_name);
103 filt_ctx = avfilter_open(filt, inst_name);
105 av_log(log_ctx, AV_LOG_ERROR,
106 "Error creating filter '%s'\n", filt_name);
110 if (avfilter_graph_add_filter(ctx, filt_ctx) < 0) {
111 avfilter_destroy(filt_ctx);
115 if (!strcmp(filt_name, "scale") && !strstr(args, "flags")) {
116 snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
117 args, ctx->scale_sws_opts);
121 if (avfilter_init_filter(filt_ctx, args, NULL)) {
122 av_log(log_ctx, AV_LOG_ERROR,
123 "Error initializing filter '%s' with args '%s'\n", filt_name, args);
131 * Parse "filter=params"
133 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
134 int index, AVClass *log_ctx)
137 char *name = av_get_token(buf, "=,;[\n");
138 AVFilterContext *ret;
142 opts = av_get_token(buf, "[],;\n");
145 ret = create_filter(graph, index, name, opts, log_ctx);
151 static void free_inout(AVFilterInOut *head)
154 AVFilterInOut *next = head->next;
161 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
165 while (*links && strcmp((*links)->name, label))
166 links = &((*links)->next);
176 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
178 element->next = *inouts;
182 static int link_filter_inouts(AVFilterContext *filter,
183 AVFilterInOut **curr_inputs,
184 AVFilterInOut **open_inputs, AVClass *log_ctx)
186 int pad = filter->input_count;
189 AVFilterInOut *p = *curr_inputs;
191 av_log(log_ctx, AV_LOG_ERROR,
192 "Not enough inputs specified for the \"%s\" filter.\n",
193 filter->filter->name);
197 *curr_inputs = (*curr_inputs)->next;
200 if (link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
207 insert_inout(open_inputs, p);
212 av_log(log_ctx, AV_LOG_ERROR,
213 "Too many inputs specified for the \"%s\" filter.\n",
214 filter->filter->name);
218 pad = filter->output_count;
220 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
221 currlinkn->filter = filter;
222 currlinkn->pad_idx = pad;
223 insert_inout(curr_inputs, currlinkn);
229 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
230 AVFilterInOut **open_outputs, AVClass *log_ctx)
234 while (**buf == '[') {
235 char *name = parse_link_name(buf, log_ctx);
236 AVFilterInOut *match;
241 /* First check if the label is not in the open_outputs list */
242 match = extract_inout(name, open_outputs);
247 /* Not in the list, so add it as an input */
248 match = av_mallocz(sizeof(AVFilterInOut));
250 match->pad_idx = pad;
253 insert_inout(curr_inputs, match);
255 *buf += strspn(*buf, WHITESPACES);
262 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
263 AVFilterInOut **open_inputs,
264 AVFilterInOut **open_outputs, AVClass *log_ctx)
268 while (**buf == '[') {
269 char *name = parse_link_name(buf, log_ctx);
270 AVFilterInOut *match;
272 AVFilterInOut *input = *curr_inputs;
273 *curr_inputs = (*curr_inputs)->next;
278 /* First check if the label is not in the open_inputs list */
279 match = extract_inout(name, open_inputs);
282 if (link_filter(input->filter, input->pad_idx,
283 match->filter, match->pad_idx, log_ctx) < 0)
285 av_free(match->name);
290 /* Not in the list, so add the first input as a open_output */
292 insert_inout(open_outputs, input);
294 *buf += strspn(*buf, WHITESPACES);
301 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
302 AVFilterInOut *open_inputs,
303 AVFilterInOut *open_outputs, AVClass *log_ctx)
308 AVFilterInOut *curr_inputs = NULL;
311 AVFilterContext *filter;
312 filters += strspn(filters, WHITESPACES);
314 if (parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
317 filter = parse_filter(&filters, graph, index, log_ctx);
322 if (filter->input_count == 1 && !curr_inputs && !index) {
323 /* First input can be omitted if it is "[in]" */
324 const char *tmp = "[in]";
325 if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
329 if (link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
332 if (parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
336 filters += strspn(filters, WHITESPACES);
339 if (chr == ';' && curr_inputs) {
340 av_log(log_ctx, AV_LOG_ERROR,
341 "Could not find a output to link when parsing \"%s\"\n",
346 } while (chr == ',' || chr == ';');
349 av_log(log_ctx, AV_LOG_ERROR,
350 "Unable to parse graph description substring: \"%s\"\n",
355 if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
356 /* Last output can be omitted if it is "[out]" */
357 const char *tmp = "[out]";
358 if (parse_outputs(&tmp, &curr_inputs, &open_inputs,
359 &open_outputs, log_ctx) < 0)
366 avfilter_graph_destroy(graph);
367 free_inout(open_inputs);
368 free_inout(open_outputs);
369 free_inout(curr_inputs);