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 int create_filter(AVFilterGraph *ctx, int index, char *name,
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(AVFilterGraph *ctx, int src, int srcpad,
81 AVFilterContext *filt, *filtb;
85 snprintf(tmp, 20, "%d", src);
86 if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
87 av_log(&log_ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
90 snprintf(tmp, 20, "%d", dst);
91 if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
92 av_log(&log_ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
95 if(avfilter_link(filt, srcpad, filtb, dstpad)) {
96 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
103 static void consume_whitespace(const char **buf)
105 *buf += strspn(*buf, " \n\t");
109 * Copy the first size bytes of input string to a null-terminated string,
110 * removing any control character. Ex: "aaa'bb'c\'c\\" -> "aaabbc'c\"
112 static void copy_unquoted(char *out, const char *in, int size)
115 for (i=0; i < size; i++) {
118 else if (in[i] == '\\') {
131 * Consumes a string from *buf.
132 * @return a copy of the consumed string, which should be free'd after use
134 static char *consume_string(const char **buf)
140 consume_whitespace(buf);
143 return av_mallocz(1);
148 *buf += strcspn(*buf, " ()=,'\\");
156 const char *p = *buf;
160 } while (p && p[-1] == '\\');
164 *buf += strlen(*buf); // Move the pointer to the null end byte
167 size = *buf - start + 1;
168 ret = av_malloc(size);
169 copy_unquoted(ret, start, size-1);
176 * @arg name a pointer (that need to be free'd after use) to the name between
179 static void parse_link_name(const char **buf, char **name)
183 *name = consume_string(buf);
188 if (*(*buf)++ != ')')
194 av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
198 * Parse "filter=params"
199 * @arg name a pointer (that need to be free'd after use) to the name of the
201 * @arg ars a pointer (that need to be free'd after use) to the args of the
204 static int parse_filter(const char **buf, AVFilterGraph *graph, int index)
207 name = consume_string(buf);
211 opts = consume_string(buf);
216 return create_filter(graph, index, name, opts);
225 * A linked-list of the inputs/outputs of the filter chain.
227 typedef struct AVFilterInOut {
233 struct AVFilterInOut *next;
236 static void free_inout(AVFilterInOut *head)
247 * Parse "(a1)(link2) ... (etc)"
249 static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
250 enum LinkType type, int instance)
253 while (**buf == '(') {
254 AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
255 parse_link_name(buf, &inoutn->name);
257 inoutn->instance = instance;
258 inoutn->pad_idx = pad++;
259 inoutn->next = *inout;
266 * Parse a string describing a filter graph.
268 int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
270 AVFilterInOut *inout=NULL;
271 AVFilterInOut *head=NULL;
279 AVFilterContext *filt;
281 consume_whitespace(&filters);
286 pad = parse_inouts(&filters, &inout, chr == ',', LinkTypeIn, index);
288 if (parse_filter(&filters, graph, index) < 0)
291 // If the first filter has an input and none was given, it is
292 // implicitly the input of the whole graph.
293 if (pad == 0 && graph->filters[graph->filter_count-1]->input_count == 1) {
294 snprintf(tmp, 20, "%d", index);
295 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
296 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
299 if(avfilter_link(in, inpad, filt, 0)) {
300 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
306 if (link_filter(graph, index-1, oldpad, index, 0) < 0)
310 pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, index);
313 } while (chr == ',' || chr == ';');
316 for (; inout != NULL; inout = inout->next) {
317 if (inout->instance == -1)
318 continue; // Already processed
320 if (!strcmp(inout->name, "in")) {
321 snprintf(tmp, 20, "%d", inout->instance);
322 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
323 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
326 if(avfilter_link(in, inpad, filt, inout->pad_idx)) {
327 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
330 } else if (!strcmp(inout->name, "out")) {
332 snprintf(tmp, 20, "%d", inout->instance);
333 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
334 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
338 if(avfilter_link(filt, inout->pad_idx, out, outpad)) {
339 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
344 AVFilterInOut *p, *src, *dst;
345 for (p = inout->next;
346 p && strcmp(p->name,inout->name); p = p->next);
349 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
354 if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
357 } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
361 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
366 if (link_filter(graph, src->instance, src->pad_idx, dst->instance, dst->pad_idx) < 0)
377 snprintf(tmp, 20, "%d", index-1);
378 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
379 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
383 if(avfilter_link(filt, pad, out, outpad)) {
384 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
394 avfilter_destroy_graph(graph);