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
27 #include "avfiltergraph.h"
29 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
30 const char *name, const char *args,
33 AVFilterContext *filt;
38 snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
40 if(!(filterdef = avfilter_get_by_name(name))) {
41 av_log(log_ctx, AV_LOG_ERROR,
42 "no such filter: '%s'\n", name);
46 if(!(filt = avfilter_open(filterdef, inst_name))) {
47 av_log(log_ctx, AV_LOG_ERROR,
48 "error creating filter '%s'\n", name);
52 if(avfilter_graph_add_filter(ctx, filt) < 0)
55 if(avfilter_init_filter(filt, args, NULL)) {
56 av_log(log_ctx, AV_LOG_ERROR,
57 "error initializing filter '%s' with args '%s'\n", name, args);
64 static int link_filter(AVFilterContext *src, int srcpad,
65 AVFilterContext *dst, int dstpad,
68 if(avfilter_link(src, srcpad, dst, dstpad)) {
69 av_log(log_ctx, AV_LOG_ERROR,
70 "cannot create the link %s:%d -> %s:%d\n",
71 src->filter->name, srcpad, dst->filter->name, dstpad);
78 static void consume_whitespace(const char **buf)
80 *buf += strspn(*buf, " \n\t");
84 * Consumes a string from *buf.
85 * @return a copy of the consumed string, which should be free'd after use
87 static char *consume_string(const char **buf)
89 char *out = av_malloc(strlen(*buf) + 1);
92 consume_whitespace(buf);
101 while(**buf && **buf != '\'')
121 consume_whitespace(buf);
128 * @arg name a pointer (that need to be free'd after use) to the name between
131 static void parse_link_name(const char **buf, char **name, AVClass *log_ctx)
133 const char *start = *buf;
136 *name = consume_string(buf);
139 av_log(log_ctx, AV_LOG_ERROR,
140 "Bad (empty?) label found in the following: \"%s\".\n", start);
144 if(*(*buf)++ != ']') {
145 av_log(log_ctx, AV_LOG_ERROR,
146 "Mismatched '[' found in the following: \"%s\".\n", start);
159 * A linked-list of the inputs/outputs of the filter chain.
161 typedef struct AVFilterInOut {
164 AVFilterContext *filter;
167 struct AVFilterInOut *next;
170 static void free_inout(AVFilterInOut *head)
173 AVFilterInOut *next = head->next;
179 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
184 while(*links && strcmp((*links)->name, label))
185 links= &((*links)->next);
196 static int link_filter_inouts(AVFilterContext *filter,
197 AVFilterInOut **currInputs,
198 AVFilterInOut **openLinks, AVClass *log_ctx)
203 pad = filter->input_count;
207 av_log(log_ctx, AV_LOG_ERROR,
208 "Not enough inputs specified for the \"%s\" filter.\n",
214 if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
216 *currInputs = (*currInputs)->next;
220 *currInputs = (*currInputs)->next;
223 p->next = *openLinks;
230 av_log(log_ctx, AV_LOG_ERROR,
231 "Too many inputs specified for the \"%s\" filter.\n",
236 pad = filter->output_count;
238 AVFilterInOut *currlinkn = av_malloc(sizeof(AVFilterInOut));
239 currlinkn->name = NULL;
240 currlinkn->type = LinkTypeOut;
241 currlinkn->filter = filter;
242 currlinkn->pad_idx = pad;
243 currlinkn->next = *currInputs;
244 *currInputs = currlinkn;
251 * Parse "filter=params"
252 * @arg name a pointer (that need to be free'd after use) to the name of the
254 * @arg ars a pointer (that need to be free'd after use) to the args of the
257 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
258 int index, AVClass *log_ctx)
261 char *name = consume_string(buf);
265 opts = consume_string(buf);
270 return create_filter(graph, index, name, opts, log_ctx);
273 static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
274 AVFilterInOut **openLinks, AVClass *log_ctx)
279 while (**buf == '[') {
282 parse_link_name(buf, &name, log_ctx);
287 /* First check if the label is not in the openLinks list */
288 p = extract_inout(name, openLinks);
290 /* Not in the list, so add it as an input */
292 AVFilterInOut *currlinkn = av_malloc(sizeof(AVFilterInOut));
294 currlinkn->name = name;
295 currlinkn->type = LinkTypeIn;
296 currlinkn->filter = NULL;
297 currlinkn->pad_idx = pad;
298 currlinkn->next = *currInputs;
299 *currInputs = currlinkn;
301 /* A label of a open link. Make it one of the inputs of the next
303 AVFilterInOut *currlinkn = p;
304 if (p->type != LinkTypeOut) {
305 av_log(log_ctx, AV_LOG_ERROR,
306 "Label \"%s\" appears twice as input!\n", p->name);
309 currlinkn->next = *currInputs;
310 *currInputs = currlinkn;
312 consume_whitespace(buf);
319 static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
320 AVFilterInOut **openLinks, AVClass *log_ctx)
324 while (**buf == '[') {
326 AVFilterInOut *match;
328 parse_link_name(buf, &name, log_ctx);
333 /* First check if the label is not in the openLinks list */
334 match = extract_inout(name, openLinks);
336 /* Not in the list, so add the first input as a openLink */
338 AVFilterInOut *p = *currInputs;
339 *currInputs = (*currInputs)->next;
340 p->next = *openLinks;
341 p->type = LinkTypeOut;
345 /* A label of a open link. Link it. */
346 AVFilterInOut *p = *currInputs;
347 if (match->type != LinkTypeIn) {
348 av_log(log_ctx, AV_LOG_ERROR,
349 "Label \"%s\" appears twice as output!\n", match->name);
353 *currInputs = (*currInputs)->next;
354 if(link_filter(p->filter, p->pad_idx,
355 match->filter, match->pad_idx, log_ctx) < 0)
360 consume_whitespace(buf);
368 * Parse a string describing a filter graph.
370 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
371 AVFilterContext *in, int inpad,
372 AVFilterContext *out, int outpad,
379 AVFilterInOut *currInputs=NULL;
380 AVFilterInOut *openLinks = av_malloc(sizeof(AVFilterInOut));
382 openLinks->name = "in";
383 openLinks->filter = in;
384 openLinks->type = LinkTypeOut;
385 openLinks->pad_idx = inpad;
386 openLinks->next = av_malloc(sizeof(AVFilterInOut));
388 openLinks->next->name = "out";
389 openLinks->next->filter = out;
390 openLinks->next->type = LinkTypeIn;
391 openLinks->next->pad_idx = outpad;
392 openLinks->next->next = NULL;
395 AVFilterContext *filter;
396 consume_whitespace(&filters);
398 pad = parse_inputs(&filters, &currInputs, &openLinks, log_ctx);
403 if(!(filter = parse_filter(&filters, graph, index, log_ctx)))
406 if(filter->input_count == 1 && !currInputs && !index) {
407 // First input can be ommitted if it is "[in]"
408 const char *tmp = "[in]";
409 pad = parse_inputs(&tmp, &currInputs, &openLinks, log_ctx);
414 if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
417 pad = parse_outputs(&filters, &currInputs, &openLinks, log_ctx);
422 consume_whitespace(&filters);
425 if (chr == ';' && currInputs) {
426 av_log(log_ctx, AV_LOG_ERROR,
427 "Could not find a output to link when parsing \"%s\"\n",
432 } while (chr == ',' || chr == ';');
434 if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
435 // Last output can be ommitted if it is "[out]"
436 const char *tmp = "[out]";
437 if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
444 avfilter_destroy_graph(graph);
445 free_inout(openLinks);
446 free_inout(currInputs);