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);
153 * Parse "filter=params"
154 * @arg name a pointer (that need to be free'd after use) to the name of the
156 * @arg ars a pointer (that need to be free'd after use) to the args of the
159 static AVFilterContext *parse_filter(const char **buf,
160 AVFilterGraph *graph, int index,
164 name = consume_string(buf);
168 opts = consume_string(buf);
173 return create_filter(graph, index, name, opts, log_ctx);
182 * A linked-list of the inputs/outputs of the filter chain.
184 typedef struct AVFilterInOut {
187 AVFilterContext *filter;
190 struct AVFilterInOut *next;
193 static void free_inout(AVFilterInOut *head)
196 AVFilterInOut *next = head->next;
203 * Parse "[a1][link2] ... [etc]"
205 static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
206 enum LinkType type, AVFilterContext *filter,
209 while (**buf == '[') {
211 AVFilterInOut *p = *inout;
213 parse_link_name(buf, &name, log_ctx);
218 for (; p && strcmp(p->name, name); p = p->next);
221 // First label apearence, add it to the linked list
222 AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
226 inoutn->filter = filter;
227 inoutn->pad_idx = pad;
228 inoutn->next = *inout;
232 if(p->type == LinkTypeIn && type == LinkTypeOut) {
233 if(link_filter(filter, pad, p->filter, p->pad_idx, log_ctx) < 0)
235 } else if(p->type == LinkTypeOut && type == LinkTypeIn) {
236 if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx) < 0)
239 av_log(log_ctx, AV_LOG_ERROR,
240 "Two links named '%s' are either both input or both output\n",
249 consume_whitespace(buf);
255 static const char *skip_inouts(const char *buf)
257 while (*buf == '[') {
258 buf += strcspn(buf, "]") + 1;
259 consume_whitespace(&buf);
266 * Parse a string describing a filter graph.
268 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
269 AVFilterContext *in, int inpad,
270 AVFilterContext *out, int outpad,
273 AVFilterInOut *inout=NULL;
274 AVFilterInOut *head=NULL;
281 AVFilterContext *last_filt = NULL;
284 AVFilterContext *filter;
288 consume_whitespace(&filters);
291 // We need to parse the inputs of the filter after we create it, so
293 filters = skip_inouts(filters);
295 if(!(filter = parse_filter(&filters, graph, index, log_ctx)))
298 pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter,
304 // If the first filter has an input and none was given, it is
305 // implicitly the input of the whole graph.
306 if(pad == 0 && filter->input_count == 1) {
307 if(link_filter(in, inpad, filter, 0, log_ctx))
312 if(link_filter(last_filt, oldpad, filter, 0, log_ctx) < 0)
316 pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter, log_ctx);
321 consume_whitespace(&filters);
326 } while (chr == ',' || chr == ';');
329 // Process remaining labels. Only inputs and outputs should be left.
330 for (; inout; inout = inout->next) {
332 continue; // Already processed
334 if(!strcmp(inout->name, "in")) {
335 if(link_filter(in, inpad, inout->filter, inout->pad_idx, log_ctx))
338 } else if(!strcmp(inout->name, "out")) {
341 if(link_filter(inout->filter, inout->pad_idx, out, outpad, log_ctx))
345 av_log(log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
354 if(link_filter(last_filt, pad, out, outpad, log_ctx))
362 avfilter_destroy_graph(graph);