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);
60 if (avfilter_graph_add_filter(ctx, filt) < 0)
63 if(avfilter_init_filter(filt, args, NULL)) {
64 av_log(&log_ctx, AV_LOG_ERROR,
65 "error initializing filter '%s'\n", name);
72 static int link_filter(AVFilterGraph *ctx, int src, int srcpad,
75 AVFilterContext *filt, *filtb;
79 snprintf(tmp, 20, "%d", src);
80 if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
81 av_log(&log_ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
84 snprintf(tmp, 20, "%d", dst);
85 if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
86 av_log(&log_ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
89 if(avfilter_link(filt, srcpad, filtb, dstpad)) {
90 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
97 static void consume_whitespace(const char **buf)
99 *buf += strspn(*buf, " \n\t");
103 * Copy the first size bytes of input string to a null-terminated string,
104 * removing any control character. Ex: "aaa'bb'c\'c\\" -> "aaabbc'c\"
106 static void copy_unquoted(char *out, const char *in, int size)
109 for (i=0; i < size; i++) {
112 else if (in[i] == '\\') {
125 * Consumes a string from *buf.
126 * @return a copy of the consumed string, which should be free'd after use
128 static char *consume_string(const char **buf)
134 consume_whitespace(buf);
137 return av_mallocz(1);
142 *buf += strcspn(*buf, " ()=,'\\");
150 const char *p = *buf;
154 } while (p && p[-1] == '\\');
158 *buf += strlen(*buf); // Move the pointer to the null end byte
161 size = *buf - start + 1;
162 ret = av_malloc(size);
163 copy_unquoted(ret, start, size-1);
170 * @arg name a pointer (that need to be free'd after use) to the name between
173 static void parse_link_name(const char **buf, char **name)
177 *name = consume_string(buf);
182 if (*(*buf)++ != ')')
188 av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
192 * Parse "filter=params"
193 * @arg name a pointer (that need to be free'd after use) to the name of the
195 * @arg ars a pointer (that need to be free'd after use) to the args of the
198 static int parse_filter(const char **buf, AVFilterGraph *graph, int index)
201 name = consume_string(buf);
205 opts = consume_string(buf);
210 return create_filter(graph, index, name, opts);
219 * A linked-list of the inputs/outputs of the filter chain.
221 typedef struct AVFilterInOut {
227 struct AVFilterInOut *next;
230 static void free_inout(AVFilterInOut *head)
241 * Parse "(a1)(link2) ... (etc)"
243 static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
244 enum LinkType type, int instance)
247 while (**buf == '(') {
248 AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
249 parse_link_name(buf, &inoutn->name);
251 inoutn->instance = instance;
252 inoutn->pad_idx = pad++;
253 inoutn->next = *inout;
260 * Parse a string describing a filter graph.
262 int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
264 AVFilterInOut *inout=NULL;
265 AVFilterInOut *head=NULL;
273 AVFilterContext *filt;
275 consume_whitespace(&filters);
280 pad = parse_inouts(&filters, &inout, chr == ',', LinkTypeIn, index);
282 if (parse_filter(&filters, graph, index) < 0)
285 // If the first filter has an input and none was given, it is
286 // implicitly the input of the whole graph.
287 if (pad == 0 && graph->filters[graph->filter_count-1]->input_count == 1) {
288 snprintf(tmp, 20, "%d", index);
289 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
290 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
293 if(avfilter_link(in, inpad, filt, 0)) {
294 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
300 if (link_filter(graph, index-1, oldpad, index, 0) < 0)
304 pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, index);
307 } while (chr == ',' || chr == ';');
310 for (; inout != NULL; inout = inout->next) {
311 if (inout->instance == -1)
312 continue; // Already processed
314 if (!strcmp(inout->name, "in")) {
315 snprintf(tmp, 20, "%d", inout->instance);
316 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
317 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
320 if(avfilter_link(in, inpad, filt, inout->pad_idx)) {
321 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
324 } else if (!strcmp(inout->name, "out")) {
326 snprintf(tmp, 20, "%d", inout->instance);
327 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
328 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
332 if(avfilter_link(filt, inout->pad_idx, out, outpad)) {
333 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
338 AVFilterInOut *p, *src, *dst;
339 for (p = inout->next;
340 p && strcmp(p->name,inout->name); p = p->next);
343 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
348 if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
351 } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
355 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
360 if (link_filter(graph, src->instance, src->pad_idx, dst->instance, dst->pad_idx) < 0)
371 snprintf(tmp, 20, "%d", index-1);
372 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
373 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
377 if(avfilter_link(filt, pad, out, outpad)) {
378 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
388 avfilter_destroy_graph(graph);