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);
84 * Create an instance of a filter, initialize and insert it in the
85 * filtergraph in *ctx.
87 * @param ctx the filtergraph context
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 a filter context in case of successful creation and configuration, NULL otherwise.
94 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
95 const char *filt_name, const char *args,
98 AVFilterContext *filt_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);
114 filt_ctx = avfilter_open(filt, inst_name);
116 av_log(log_ctx, AV_LOG_ERROR,
117 "Error creating filter '%s'\n", filt_name);
121 if (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 (avfilter_init_filter(filt_ctx, args, NULL)) {
133 av_log(log_ctx, AV_LOG_ERROR,
134 "Error initializing filter '%s' with args '%s'\n", filt_name, args);
142 * Parse "filter=params"
144 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
145 int index, AVClass *log_ctx)
148 char *name = av_get_token(buf, "=,;[\n");
149 AVFilterContext *ret;
153 opts = av_get_token(buf, "[],;\n");
156 ret = create_filter(graph, index, name, opts, log_ctx);
162 static void free_inout(AVFilterInOut *head)
165 AVFilterInOut *next = head->next;
172 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
176 while (*links && strcmp((*links)->name, label))
177 links = &((*links)->next);
187 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
189 element->next = *inouts;
193 static int link_filter_inouts(AVFilterContext *filter,
194 AVFilterInOut **curr_inputs,
195 AVFilterInOut **open_inputs, AVClass *log_ctx)
197 int pad = filter->input_count;
200 AVFilterInOut *p = *curr_inputs;
202 av_log(log_ctx, AV_LOG_ERROR,
203 "Not enough inputs specified for the \"%s\" filter.\n",
204 filter->filter->name);
208 *curr_inputs = (*curr_inputs)->next;
211 if (link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
218 insert_inout(open_inputs, p);
223 av_log(log_ctx, AV_LOG_ERROR,
224 "Too many inputs specified for the \"%s\" filter.\n",
225 filter->filter->name);
229 pad = filter->output_count;
231 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
232 currlinkn->filter = filter;
233 currlinkn->pad_idx = pad;
234 insert_inout(curr_inputs, currlinkn);
240 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
241 AVFilterInOut **open_outputs, AVClass *log_ctx)
245 while (**buf == '[') {
246 char *name = parse_link_name(buf, log_ctx);
247 AVFilterInOut *match;
252 /* First check if the label is not in the open_outputs list */
253 match = extract_inout(name, open_outputs);
258 /* Not in the list, so add it as an input */
259 match = av_mallocz(sizeof(AVFilterInOut));
261 match->pad_idx = pad;
264 insert_inout(curr_inputs, match);
266 *buf += strspn(*buf, WHITESPACES);
273 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
274 AVFilterInOut **open_inputs,
275 AVFilterInOut **open_outputs, AVClass *log_ctx)
279 while (**buf == '[') {
280 char *name = parse_link_name(buf, log_ctx);
281 AVFilterInOut *match;
283 AVFilterInOut *input = *curr_inputs;
284 *curr_inputs = (*curr_inputs)->next;
289 /* First check if the label is not in the open_inputs list */
290 match = extract_inout(name, open_inputs);
293 if (link_filter(input->filter, input->pad_idx,
294 match->filter, match->pad_idx, log_ctx) < 0)
296 av_free(match->name);
301 /* Not in the list, so add the first input as a open_output */
303 insert_inout(open_outputs, input);
305 *buf += strspn(*buf, WHITESPACES);
312 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
313 AVFilterInOut *open_inputs,
314 AVFilterInOut *open_outputs, AVClass *log_ctx)
319 AVFilterInOut *curr_inputs = NULL;
322 AVFilterContext *filter;
323 filters += strspn(filters, WHITESPACES);
325 if (parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
328 filter = parse_filter(&filters, graph, index, log_ctx);
333 if (filter->input_count == 1 && !curr_inputs && !index) {
334 /* First input can be omitted if it is "[in]" */
335 const char *tmp = "[in]";
336 if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
340 if (link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
343 if (parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
347 filters += strspn(filters, WHITESPACES);
350 if (chr == ';' && curr_inputs) {
351 av_log(log_ctx, AV_LOG_ERROR,
352 "Could not find a output to link when parsing \"%s\"\n",
357 } while (chr == ',' || chr == ';');
360 av_log(log_ctx, AV_LOG_ERROR,
361 "Unable to parse graph description substring: \"%s\"\n",
366 if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
367 /* Last output can be omitted if it is "[out]" */
368 const char *tmp = "[out]";
369 if (parse_outputs(&tmp, &curr_inputs, &open_inputs,
370 &open_outputs, log_ctx) < 0)
377 avfilter_graph_destroy(graph);
378 free_inout(open_inputs);
379 free_inout(open_outputs);
380 free_inout(curr_inputs);