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 const char *name, const 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);
105 consume_whitespace(buf);
114 while(**buf && **buf != '\'')
133 consume_whitespace(buf);
140 * @arg name a pointer (that need to be free'd after use) to the name between
143 static void parse_link_name(const char **buf, char **name)
145 const char *start = *buf;
148 *name = consume_string(buf);
151 av_log(&log_ctx, AV_LOG_ERROR,
152 "Bad (empty?) label found in the following: \"%s\".\n", start);
156 if(*(*buf)++ != ']') {
157 av_log(&log_ctx, AV_LOG_ERROR,
158 "Mismatched '[' found in the following: \"%s\".\n", start);
165 * Parse "filter=params"
166 * @arg name a pointer (that need to be free'd after use) to the name of the
168 * @arg ars a pointer (that need to be free'd after use) to the args of the
171 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
174 name = consume_string(buf);
178 opts = consume_string(buf);
183 return create_filter(graph, index, name, opts);
192 * A linked-list of the inputs/outputs of the filter chain.
194 typedef struct AVFilterInOut {
197 AVFilterContext *filter;
200 struct AVFilterInOut *next;
203 static void free_inout(AVFilterInOut *head)
206 AVFilterInOut *next = head->next;
213 * Process a link. This funcion looks for a matching label in the *inout
214 * linked list. If none is found, it adds this link to the list.
216 static int handle_link(char *name, AVFilterInOut **inout, int pad,
217 enum LinkType type, AVFilterContext *filter)
219 AVFilterInOut *p = *inout;
221 for (; p && strcmp(p->name, name); p = p->next);
224 // First label apearence, add it to the linked list
225 AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
229 inoutn->filter = filter;
230 inoutn->pad_idx = pad;
231 inoutn->next = *inout;
236 if(p->type == LinkTypeIn && type == LinkTypeOut) {
237 if(link_filter(filter, pad, p->filter, p->pad_idx) < 0)
239 } else if(p->type == LinkTypeOut && type == LinkTypeIn) {
240 if(link_filter(p->filter, p->pad_idx, filter, pad) < 0)
243 av_log(&log_ctx, AV_LOG_ERROR,
244 "Two links named '%s' are either both input or both output\n",
258 * Parse "[a1][link2] ... [etc]"
260 static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
261 enum LinkType type, AVFilterContext *filter)
263 while (**buf == '[') {
266 parse_link_name(buf, &name);
271 handle_link(name, inout, pad++, type, filter);
272 consume_whitespace(buf);
277 static const char *skip_inouts(const char *buf)
279 while (*buf == '[') {
280 buf += strcspn(buf, "]") + 1;
281 consume_whitespace(&buf);
288 * Parse a string describing a filter graph.
290 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
291 AVFilterContext *in, int inpad,
292 AVFilterContext *out, int outpad)
294 AVFilterInOut *inout=NULL;
295 AVFilterInOut *head=NULL;
302 AVFilterContext *last_filt = NULL;
305 AVFilterContext *filter;
309 consume_whitespace(&filters);
312 // We need to parse the inputs of the filter after we create it, so
314 filters = skip_inouts(filters);
316 if(!(filter = parse_filter(&filters, graph, index)))
319 pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
324 // If the first filter has an input and none was given, it is
325 // implicitly the input of the whole graph.
326 if(pad == 0 && filter->input_count == 1) {
327 if(link_filter(in, inpad, filter, 0))
332 if(link_filter(last_filt, oldpad, filter, 0) < 0)
336 pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
341 consume_whitespace(&filters);
346 } while (chr == ',' || chr == ';');
349 // Process remaining labels. Only inputs and outputs should be left.
350 for (; inout; inout = inout->next) {
352 continue; // Already processed
354 if(!strcmp(inout->name, "in")) {
355 if(link_filter(in, inpad, inout->filter, inout->pad_idx))
358 } else if(!strcmp(inout->name, "out")) {
361 if(link_filter(inout->filter, inout->pad_idx, out, outpad))
365 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
374 if(link_filter(last_filt, pad, out, outpad))
382 avfilter_destroy_graph(graph);