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"
30 static int link_filter(AVFilterContext *src, int srcpad,
31 AVFilterContext *dst, int dstpad,
34 if(avfilter_link(src, srcpad, dst, dstpad)) {
35 av_log(log_ctx, AV_LOG_ERROR,
36 "cannot create the link %s:%d -> %s:%d\n",
37 src->filter->name, srcpad, dst->filter->name, dstpad);
44 static int consume_whitespace(const char *buf)
46 return strspn(buf, " \n\t");
50 * Consumes a string from *buf.
51 * @return a copy of the consumed string, which should be free'd after use
53 static char *consume_string(const char **buf)
55 char *out = av_malloc(strlen(*buf) + 1);
58 *buf += consume_whitespace(*buf);
67 while(**buf && **buf != '\'')
87 *buf += consume_whitespace(*buf);
94 * @param name a pointer (that need to be free'd after use) to the name between
97 static char *parse_link_name(const char **buf, AVClass *log_ctx)
99 const char *start = *buf;
103 name = consume_string(buf);
106 av_log(log_ctx, AV_LOG_ERROR,
107 "Bad (empty?) label found in the following: \"%s\".\n", start);
111 if(*(*buf)++ != ']') {
112 av_log(log_ctx, AV_LOG_ERROR,
113 "Mismatched '[' found in the following: \"%s\".\n", start);
121 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
122 const char *name, const char *args,
125 AVFilterContext *filt;
130 snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
132 filterdef = avfilter_get_by_name(name);
135 av_log(log_ctx, AV_LOG_ERROR,
136 "no such filter: '%s'\n", name);
140 filt = avfilter_open(filterdef, inst_name);
142 av_log(log_ctx, AV_LOG_ERROR,
143 "error creating filter '%s'\n", name);
147 if(avfilter_graph_add_filter(ctx, filt) < 0)
150 if(avfilter_init_filter(filt, args, NULL)) {
151 av_log(log_ctx, AV_LOG_ERROR,
152 "error initializing filter '%s' with args '%s'\n", name, args);
160 * Parse "filter=params"
161 * @arg name a pointer (that need to be free'd after use) to the name of the
163 * @arg ars a pointer (that need to be free'd after use) to the args of the
166 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
167 int index, AVClass *log_ctx)
170 char *name = consume_string(buf);
174 opts = consume_string(buf);
179 return create_filter(graph, index, name, opts, log_ctx);
182 static void free_inout(AVFilterInOut *head)
185 AVFilterInOut *next = head->next;
191 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
195 while(*links && strcmp((*links)->name, label))
196 links = &((*links)->next);
207 static int link_filter_inouts(AVFilterContext *filter,
208 AVFilterInOut **currInputs,
209 AVFilterInOut **openLinks, AVClass *log_ctx)
213 pad = filter->input_count;
215 AVFilterInOut *p = *currInputs;
216 *currInputs = (*currInputs)->next;
218 av_log(log_ctx, AV_LOG_ERROR,
219 "Not enough inputs specified for the \"%s\" filter.\n",
225 if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
231 p->next = *openLinks;
238 av_log(log_ctx, AV_LOG_ERROR,
239 "Too many inputs specified for the \"%s\" filter.\n",
244 pad = filter->output_count;
246 AVFilterInOut *currlinkn = av_malloc(sizeof(AVFilterInOut));
247 currlinkn->name = NULL;
248 currlinkn->type = LinkTypeOut;
249 currlinkn->filter = filter;
250 currlinkn->pad_idx = pad;
251 currlinkn->next = *currInputs;
252 *currInputs = currlinkn;
258 static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
259 AVFilterInOut **openLinks, AVClass *log_ctx)
263 while(**buf == '[') {
264 char *name = parse_link_name(buf, log_ctx);
265 AVFilterInOut *link_to_add;
266 AVFilterInOut *match;
271 /* First check if the label is not in the openLinks list */
272 match = extract_inout(name, openLinks);
275 /* A label of a open link. Make it one of the inputs of the next
277 if(match->type != LinkTypeOut) {
278 av_log(log_ctx, AV_LOG_ERROR,
279 "Label \"%s\" appears twice as input!\n", match->name);
285 /* Not in the list, so add it as an input */
286 link_to_add = av_malloc(sizeof(AVFilterInOut));
288 link_to_add->name = name;
289 link_to_add->type = LinkTypeIn;
290 link_to_add->filter = NULL;
291 link_to_add->pad_idx = pad;
293 link_to_add->next = *currInputs;
294 *currInputs = link_to_add;
295 *buf += consume_whitespace(*buf);
302 static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
303 AVFilterInOut **openLinks, AVClass *log_ctx)
307 while(**buf == '[') {
308 char *name = parse_link_name(buf, log_ctx);
309 AVFilterInOut *match;
311 AVFilterInOut *input = *currInputs;
312 *currInputs = (*currInputs)->next;
317 /* First check if the label is not in the openLinks list */
318 match = extract_inout(name, openLinks);
321 /* A label of a open link. Link it. */
322 if(match->type != LinkTypeIn) {
323 av_log(log_ctx, AV_LOG_ERROR,
324 "Label \"%s\" appears twice as output!\n", match->name);
328 if(link_filter(input->filter, input->pad_idx,
329 match->filter, match->pad_idx, log_ctx) < 0)
334 /* Not in the list, so add the first input as a openLink */
335 input->next = *openLinks;
336 input->type = LinkTypeOut;
340 *buf += consume_whitespace(*buf);
348 * Parse a string describing a filter graph.
350 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
351 AVFilterInOut *openLinks, AVClass *log_ctx)
357 AVFilterInOut *currInputs = NULL;
360 AVFilterContext *filter;
361 filters += consume_whitespace(filters);
363 pad = parse_inputs(&filters, &currInputs, &openLinks, log_ctx);
368 filter = parse_filter(&filters, graph, index, log_ctx);
373 if(filter->input_count == 1 && !currInputs && !index) {
374 // First input can be ommitted if it is "[in]"
375 const char *tmp = "[in]";
376 pad = parse_inputs(&tmp, &currInputs, &openLinks, log_ctx);
381 if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
384 pad = parse_outputs(&filters, &currInputs, &openLinks, log_ctx);
389 filters += consume_whitespace(filters);
392 if(chr == ';' && currInputs) {
393 av_log(log_ctx, AV_LOG_ERROR,
394 "Could not find a output to link when parsing \"%s\"\n",
399 } while(chr == ',' || chr == ';');
401 if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
402 // Last output can be ommitted if it is "[out]"
403 const char *tmp = "[out]";
404 if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
411 avfilter_destroy_graph(graph);
412 free_inout(openLinks);
413 free_inout(currInputs);