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"
32 static const char *log_name(void *p)
34 return "Filter parser";
37 static const AVClass filter_parser_class = {
42 static const AVClass *log_ctx = &filter_parser_class;
44 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
45 char *name, char *args)
47 AVFilterContext *filt;
52 snprintf(tmp, 20, "%d", index);
54 if(!(filterdef = avfilter_get_by_name(name))) {
55 av_log(&log_ctx, AV_LOG_ERROR,
56 "no such filter: '%s'\n", name);
60 if(!(filt = avfilter_open(filterdef, tmp))) {
61 av_log(&log_ctx, AV_LOG_ERROR,
62 "error creating filter '%s'\n", name);
66 if (avfilter_graph_add_filter(ctx, filt) < 0)
69 if(avfilter_init_filter(filt, args, NULL)) {
70 av_log(&log_ctx, AV_LOG_ERROR,
71 "error initializing filter '%s' with args '%s'\n", name, args);
78 static int link_filter(AVFilterContext *src, int srcpad,
79 AVFilterContext *dst, int dstpad)
81 if(avfilter_link(src, srcpad, dst, dstpad)) {
82 av_log(&log_ctx, AV_LOG_ERROR,
83 "cannot create the link %s:%d -> %s:%d\n",
84 src->filter->name, srcpad, dst->filter->name, dstpad);
91 static void consume_whitespace(const char **buf)
93 *buf += strspn(*buf, " \n\t");
97 * Copy the first size bytes of input string to a null-terminated string,
98 * removing any control character. Ex: "aaa'bb'c\'c\\" -> "aaabbc'c\"
100 static void copy_unquoted(char *out, const char *in, int size)
103 for (i=0; i < size; i++) {
106 else if (in[i] == '\\') {
119 * Consumes a string from *buf.
120 * @return a copy of the consumed string, which should be free'd after use
122 static char *consume_string(const char **buf)
128 consume_whitespace(buf);
131 return av_mallocz(1);
136 *buf += strcspn(*buf, " ()=,'\\");
144 const char *p = *buf;
148 } while (p && p[-1] == '\\');
152 *buf += strlen(*buf); // Move the pointer to the null end byte
155 size = *buf - start + 1;
156 ret = av_malloc(size);
157 copy_unquoted(ret, start, size-1);
164 * @arg name a pointer (that need to be free'd after use) to the name between
167 static void parse_link_name(const char **buf, char **name)
171 *name = consume_string(buf);
176 if (*(*buf)++ != ')')
182 av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
186 * Parse "filter=params"
187 * @arg name a pointer (that need to be free'd after use) to the name of the
189 * @arg ars a pointer (that need to be free'd after use) to the args of the
192 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
195 name = consume_string(buf);
199 opts = consume_string(buf);
204 return create_filter(graph, index, name, opts);
213 * A linked-list of the inputs/outputs of the filter chain.
215 typedef struct AVFilterInOut {
218 AVFilterContext *instance;
221 struct AVFilterInOut *next;
224 static void free_inout(AVFilterInOut *head)
235 * Parse "(a1)(link2) ... (etc)"
237 static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
238 enum LinkType type, AVFilterContext *instance)
241 while (**buf == '(') {
242 AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
243 parse_link_name(buf, &inoutn->name);
245 inoutn->instance = instance;
246 inoutn->pad_idx = pad++;
247 inoutn->next = *inout;
253 static const char *skip_inouts(const char *buf)
255 while (*buf == '(') {
256 buf += strcspn(buf, ")");
264 * Parse a string describing a filter graph.
266 int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
268 AVFilterInOut *inout=NULL;
269 AVFilterInOut *head=NULL;
276 AVFilterContext *last_filt = NULL;
278 consume_whitespace(&filters);
281 AVFilterContext *filter;
283 const char *inouts = filters;
285 // We need to parse the inputs of the filter after we create it, so
287 filters = skip_inouts(filters);
289 if (!(filter = parse_filter(&filters, graph, index)))
292 pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
294 // If the first filter has an input and none was given, it is
295 // implicitly the input of the whole graph.
296 if (pad == 0 && graph->filters[graph->filter_count-1]->input_count == 1) {
297 if(link_filter(in, inpad, filter, 0))
302 if (link_filter(last_filt, oldpad, filter, 0) < 0)
306 pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
310 } while (chr == ',' || chr == ';');
313 for (; inout != NULL; inout = inout->next) {
314 if (inout->instance == NULL)
315 continue; // Already processed
317 if (!strcmp(inout->name, "in")) {
318 if(link_filter(in, inpad, inout->instance, inout->pad_idx))
321 } else if (!strcmp(inout->name, "out")) {
324 if(link_filter(inout->instance, inout->pad_idx, out, outpad))
328 AVFilterInOut *p, *src, *dst;
329 for (p = inout->next;
330 p && strcmp(p->name,inout->name); p = p->next);
333 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
338 if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
341 } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
345 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
350 if (link_filter(src->instance, src->pad_idx, dst->instance, dst->pad_idx) < 0)
353 src->instance = NULL;
354 dst->instance = NULL;
361 if(link_filter(last_filt, pad, out, outpad))
369 avfilter_destroy_graph(graph);