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(inst_name, sizeof(inst_name), "Parsed filter %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, inst_name))) {
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 * Consumes a string from *buf.
98 * @return a copy of the consumed string, which should be free'd after use
100 static char *consume_string(const char **buf)
102 char *out = av_malloc(strlen(*buf) + 1);
103 const char *in = *buf;
106 consume_whitespace(buf);
115 while(*in && *in != '\'')
137 * @arg name a pointer (that need to be free'd after use) to the name between
140 static void parse_link_name(const char **buf, char **name)
144 *name = consume_string(buf);
155 av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
159 * Parse "filter=params"
160 * @arg name a pointer (that need to be free'd after use) to the name of the
162 * @arg ars a pointer (that need to be free'd after use) to the args of the
165 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
168 name = consume_string(buf);
172 opts = consume_string(buf);
177 return create_filter(graph, index, name, opts);
186 * A linked-list of the inputs/outputs of the filter chain.
188 typedef struct AVFilterInOut {
191 AVFilterContext *filter;
194 struct AVFilterInOut *next;
197 static void free_inout(AVFilterInOut *head)
208 * Parse "[a1][link2] ... [etc]"
210 static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
211 enum LinkType type, AVFilterContext *filter)
214 while (**buf == '[') {
215 AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
216 parse_link_name(buf, &inoutn->name);
218 inoutn->filter = filter;
219 inoutn->pad_idx = pad++;
220 inoutn->next = *inout;
226 static const char *skip_inouts(const char *buf)
228 while (*buf == '[') {
229 buf += strcspn(buf, "]");
237 * Parse a string describing a filter graph.
239 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
240 AVFilterContext *in, int inpad,
241 AVFilterContext *out, int outpad)
243 AVFilterInOut *inout=NULL;
244 AVFilterInOut *head=NULL;
251 AVFilterContext *last_filt = NULL;
253 consume_whitespace(&filters);
256 AVFilterContext *filter;
258 const char *inouts = filters;
260 // We need to parse the inputs of the filter after we create it, so
262 filters = skip_inouts(filters);
264 if(!(filter = parse_filter(&filters, graph, index)))
267 pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
269 // If the first filter has an input and none was given, it is
270 // implicitly the input of the whole graph.
271 if(pad == 0 && filter->input_count == 1) {
272 if(link_filter(in, inpad, filter, 0))
277 if(link_filter(last_filt, oldpad, filter, 0) < 0)
281 pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
285 } while (chr == ',' || chr == ';');
288 for (; inout != NULL; inout = inout->next) {
289 if(inout->filter == NULL)
290 continue; // Already processed
292 if(!strcmp(inout->name, "in")) {
293 if(link_filter(in, inpad, inout->filter, inout->pad_idx))
296 } else if(!strcmp(inout->name, "out")) {
299 if(link_filter(inout->filter, inout->pad_idx, out, outpad))
303 AVFilterInOut *p, *src, *dst;
304 for (p = inout->next;
305 p && strcmp(p->name,inout->name); p = p->next);
308 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
313 if(p->type == LinkTypeIn && inout->type == LinkTypeOut) {
316 } else if(p->type == LinkTypeOut && inout->type == LinkTypeIn) {
320 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
325 if(link_filter(src->filter, src->pad_idx, dst->filter, dst->pad_idx) < 0)
336 if(link_filter(last_filt, pad, out, outpad))
344 avfilter_destroy_graph(graph);