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);
53 if(!(filterdef = avfilter_get_by_name(name)) ||
54 !(filt = avfilter_open(filterdef, tmp))) {
55 av_log(&log_ctx, AV_LOG_ERROR,
56 "error creating filter '%s'\n", name);
59 avfilter_graph_add_filter(ctx, filt);
60 if(avfilter_init_filter(filt, args, NULL)) {
61 av_log(&log_ctx, AV_LOG_ERROR,
62 "error initializing filter '%s'\n", name);
69 static int link_filter(AVFilterGraph *ctx, int src, int srcpad,
72 AVFilterContext *filt, *filtb;
76 snprintf(tmp, 20, "%d", src);
77 if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
78 av_log(&log_ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
81 snprintf(tmp, 20, "%d", dst);
82 if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
83 av_log(&log_ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
86 if(avfilter_link(filt, srcpad, filtb, dstpad)) {
87 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
94 static void consume_whitespace(const char **buf)
96 *buf += strspn(*buf, " \n\t");
100 * Copy the first size bytes of input string to a null-terminated string,
101 * removing any control character. Ex: "aaa'bb'c\'c\\" -> "aaabbc'c\"
103 static void copy_unquoted(char *out, const char *in, int size)
106 for (i=0; i < size; i++) {
109 else if (in[i] == '\\') {
122 * Consumes a string from *buf.
123 * @return a copy of the consumed string, which should be free'd after use
125 static char *consume_string(const char **buf)
131 consume_whitespace(buf);
134 return av_mallocz(1);
139 *buf += strcspn(*buf, " ()=,'\\");
147 const char *p = *buf;
151 } while (p && p[-1] == '\\');
155 *buf += strlen(*buf); // Move the pointer to the null end byte
158 size = *buf - start + 1;
159 ret = av_malloc(size);
160 copy_unquoted(ret, start, size-1);
167 * @arg name a pointer (that need to be free'd after use) to the name between
170 static void parse_link_name(const char **buf, char **name)
174 *name = consume_string(buf);
179 if (*(*buf)++ != ')')
185 av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
189 * Parse "filter=params"
190 * @arg name a pointer (that need to be free'd after use) to the name of the
192 * @arg ars a pointer (that need to be free'd after use) to the args of the
195 static int parse_filter(const char **buf, AVFilterGraph *graph, int index)
198 name = consume_string(buf);
202 opts = consume_string(buf);
207 return create_filter(graph, index, name, opts);
216 * A linked-list of the inputs/outputs of the filter chain.
218 typedef struct AVFilterInOut {
224 struct AVFilterInOut *next;
227 static void free_inout(AVFilterInOut *head)
238 * Parse "(a1)(link2) ... (etc)"
240 static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
241 enum LinkType type, int instance)
244 while (**buf == '(') {
245 AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
246 parse_link_name(buf, &inoutn->name);
248 inoutn->instance = instance;
249 inoutn->pad_idx = pad++;
250 inoutn->next = *inout;
257 * Parse a string describing a filter graph.
259 int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
261 AVFilterInOut *inout=NULL;
262 AVFilterInOut *head=NULL;
270 AVFilterContext *filt;
272 consume_whitespace(&filters);
277 pad = parse_inouts(&filters, &inout, chr == ',', LinkTypeIn, index);
279 if (parse_filter(&filters, graph, index) < 0)
282 // If the first filter has an input and none was given, it is
283 // implicitly the input of the whole graph.
284 if (pad == 0 && graph->filters[graph->filter_count-1]->input_count == 1) {
285 snprintf(tmp, 20, "%d", index);
286 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
287 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
290 if(avfilter_link(in, inpad, filt, 0)) {
291 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
297 if (link_filter(graph, index-1, oldpad, index, 0) < 0)
301 pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, index);
304 } while (chr == ',' || chr == ';');
307 for (; inout != NULL; inout = inout->next) {
308 if (inout->instance == -1)
309 continue; // Already processed
311 if (!strcmp(inout->name, "in")) {
312 snprintf(tmp, 20, "%d", inout->instance);
313 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
314 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
317 if(avfilter_link(in, inpad, filt, inout->pad_idx)) {
318 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
321 } else if (!strcmp(inout->name, "out")) {
323 snprintf(tmp, 20, "%d", inout->instance);
324 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
325 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
329 if(avfilter_link(filt, inout->pad_idx, out, outpad)) {
330 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
335 AVFilterInOut *p, *src, *dst;
336 for (p = inout->next;
337 p && strcmp(p->name,inout->name); p = p->next);
340 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
345 if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
348 } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
352 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
357 if (link_filter(graph, src->instance, src->pad_idx, dst->instance, dst->pad_idx) < 0)
368 snprintf(tmp, 20, "%d", index-1);
369 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
370 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
374 if(avfilter_link(filt, pad, out, outpad)) {
375 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
385 avfilter_destroy_graph(graph);