More simplifying
[libav.git] / libavfilter / graphparser.c
CommitLineData
27afb09d
VS
1/*
2 * filter graph parser
3 * copyright (c) 2008 Vitor Sessak
4 * copyright (c) 2007 Bobby Bingham
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23#include <ctype.h>
24#include <string.h>
25
26#include "avfilter.h"
27#include "avfiltergraph.h"
28
29/**
30 * For use in av_log
31 */
32static const char *log_name(void *p)
33{
34 return "Filter parser";
35}
36
37static const AVClass filter_parser_class = {
38 "Filter parser",
39 log_name
40};
41
42static const AVClass *log_ctx = &filter_parser_class;
43
9710beaf
VS
44static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
45 char *name, char *args)
27afb09d
VS
46{
47 AVFilterContext *filt;
48
49 AVFilter *filterdef;
58a2d7a5 50 char inst_name[30];
27afb09d 51
58a2d7a5 52 snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
27a669ab
VS
53
54 if(!(filterdef = avfilter_get_by_name(name))) {
55 av_log(&log_ctx, AV_LOG_ERROR,
56 "no such filter: '%s'\n", name);
9710beaf 57 return NULL;
27a669ab
VS
58 }
59
58a2d7a5 60 if(!(filt = avfilter_open(filterdef, inst_name))) {
27afb09d
VS
61 av_log(&log_ctx, AV_LOG_ERROR,
62 "error creating filter '%s'\n", name);
9710beaf 63 return NULL;
27afb09d 64 }
046f49ba 65
2839ff5e 66 if(avfilter_graph_add_filter(ctx, filt) < 0)
9710beaf 67 return NULL;
046f49ba 68
27afb09d
VS
69 if(avfilter_init_filter(filt, args, NULL)) {
70 av_log(&log_ctx, AV_LOG_ERROR,
27a669ab 71 "error initializing filter '%s' with args '%s'\n", name, args);
9710beaf 72 return NULL;
27afb09d
VS
73 }
74
9710beaf 75 return filt;
27afb09d
VS
76}
77
9710beaf
VS
78static int link_filter(AVFilterContext *src, int srcpad,
79 AVFilterContext *dst, int dstpad)
27afb09d 80{
9710beaf
VS
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);
27afb09d
VS
85 return -1;
86 }
87
88 return 0;
89}
90
91static void consume_whitespace(const char **buf)
92{
93 *buf += strspn(*buf, " \n\t");
94}
95
96/**
27afb09d
VS
97 * Consumes a string from *buf.
98 * @return a copy of the consumed string, which should be free'd after use
99 */
100static char *consume_string(const char **buf)
101{
2255026d 102 char *out = av_malloc(strlen(*buf) + 1);
93b7a0f6
VS
103 const char *in = *buf;
104 char *ret = out;
27afb09d
VS
105
106 consume_whitespace(buf);
107
93b7a0f6
VS
108 do{
109 char c = *in++;
110 switch (c) {
111 case '\\':
112 *out++= *in++;
27afb09d 113 break;
93b7a0f6
VS
114 case '\'':
115 while(*in && *in != '\'')
116 *out++= *in++;
117 if(*in) in++;
118 break;
119 case 0:
d7ff2297
VS
120 case ']':
121 case '[':
93b7a0f6
VS
122 case '=':
123 case ',':
124 *out++= 0;
125 break;
126 default:
127 *out++= c;
128 }
129 } while(out[-1]);
27afb09d 130
93b7a0f6 131 *buf = in-1;
27afb09d
VS
132 return ret;
133}
134
135/**
ffac8784 136 * Parse "[linkname]"
27afb09d
VS
137 * @arg name a pointer (that need to be free'd after use) to the name between
138 * parenthesis
139 */
140static void parse_link_name(const char **buf, char **name)
141{
142 (*buf)++;
143
144 *name = consume_string(buf);
145
2839ff5e 146 if(!*name[0])
27afb09d
VS
147 goto fail;
148
2839ff5e 149 if(*(*buf)++ != ']')
27afb09d
VS
150 goto fail;
151
152 return;
153 fail:
154 av_freep(name);
155 av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
156}
157
158/**
159 * Parse "filter=params"
160 * @arg name a pointer (that need to be free'd after use) to the name of the
161 * filter
162 * @arg ars a pointer (that need to be free'd after use) to the args of the
163 * filter
164 */
9710beaf 165static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
27afb09d
VS
166{
167 char *name, *opts;
168 name = consume_string(buf);
169
2839ff5e 170 if(**buf == '=') {
27afb09d
VS
171 (*buf)++;
172 opts = consume_string(buf);
173 } else {
174 opts = NULL;
175 }
176
177 return create_filter(graph, index, name, opts);
178}
179
180enum LinkType {
181 LinkTypeIn,
182 LinkTypeOut,
183};
184
185/**
186 * A linked-list of the inputs/outputs of the filter chain.
187 */
188typedef struct AVFilterInOut {
189 enum LinkType type;
190 char *name;
37161d64 191 AVFilterContext *filter;
27afb09d
VS
192 int pad_idx;
193
194 struct AVFilterInOut *next;
195} AVFilterInOut;
196
197static void free_inout(AVFilterInOut *head)
198{
199 while (head) {
55672c83 200 AVFilterInOut *next = head->next;
27afb09d
VS
201 av_free(head);
202 head = next;
203 }
204}
205
206/**
ffac8784 207 * Parse "[a1][link2] ... [etc]"
27afb09d 208 */
325cb1ef 209static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
37161d64 210 enum LinkType type, AVFilterContext *filter)
27afb09d 211{
d7ff2297 212 while (**buf == '[') {
27afb09d
VS
213 AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
214 parse_link_name(buf, &inoutn->name);
215 inoutn->type = type;
37161d64 216 inoutn->filter = filter;
27afb09d
VS
217 inoutn->pad_idx = pad++;
218 inoutn->next = *inout;
219 *inout = inoutn;
220 }
221 return pad;
222}
223
9710beaf
VS
224static const char *skip_inouts(const char *buf)
225{
a3acd1d9
VS
226 while (*buf == '[')
227 buf += strcspn(buf, "]") + 1;
228
9710beaf
VS
229 return buf;
230}
231
232
27afb09d
VS
233/**
234 * Parse a string describing a filter graph.
235 */
92973a04
VS
236int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
237 AVFilterContext *in, int inpad,
238 AVFilterContext *out, int outpad)
27afb09d
VS
239{
240 AVFilterInOut *inout=NULL;
241 AVFilterInOut *head=NULL;
242
243 int index = 0;
244 char chr = 0;
245 int pad = 0;
246 int has_out = 0;
247
9710beaf 248 AVFilterContext *last_filt = NULL;
27afb09d
VS
249
250 consume_whitespace(&filters);
251
252 do {
9710beaf 253 AVFilterContext *filter;
27afb09d 254 int oldpad = pad;
9710beaf 255 const char *inouts = filters;
27afb09d 256
9710beaf
VS
257 // We need to parse the inputs of the filter after we create it, so
258 // skip it by now
259 filters = skip_inouts(filters);
27afb09d 260
2839ff5e 261 if(!(filter = parse_filter(&filters, graph, index)))
27afb09d
VS
262 goto fail;
263
9710beaf
VS
264 pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
265
27afb09d
VS
266 // If the first filter has an input and none was given, it is
267 // implicitly the input of the whole graph.
2839ff5e 268 if(pad == 0 && filter->input_count == 1) {
ee75692a 269 if(link_filter(in, inpad, filter, 0))
27afb09d 270 goto fail;
27afb09d
VS
271 }
272
273 if(chr == ',') {
2839ff5e 274 if(link_filter(last_filt, oldpad, filter, 0) < 0)
27afb09d 275 goto fail;
27afb09d 276 }
da790674 277
9710beaf 278 pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
27afb09d
VS
279 chr = *filters++;
280 index++;
9710beaf 281 last_filt = filter;
27afb09d
VS
282 } while (chr == ',' || chr == ';');
283
284 head = inout;
285 for (; inout != NULL; inout = inout->next) {
2839ff5e 286 if(inout->filter == NULL)
27afb09d
VS
287 continue; // Already processed
288
2839ff5e 289 if(!strcmp(inout->name, "in")) {
37161d64 290 if(link_filter(in, inpad, inout->filter, inout->pad_idx))
27afb09d 291 goto fail;
ee75692a 292
2839ff5e 293 } else if(!strcmp(inout->name, "out")) {
27afb09d 294 has_out = 1;
27afb09d 295
37161d64 296 if(link_filter(inout->filter, inout->pad_idx, out, outpad))
27afb09d 297 goto fail;
27afb09d
VS
298
299 } else {
300 AVFilterInOut *p, *src, *dst;
301 for (p = inout->next;
302 p && strcmp(p->name,inout->name); p = p->next);
303
2839ff5e 304 if(!p) {
27afb09d
VS
305 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
306 inout->name);
307 goto fail;
308 }
309
2839ff5e 310 if(p->type == LinkTypeIn && inout->type == LinkTypeOut) {
27afb09d
VS
311 src = inout;
312 dst = p;
2839ff5e 313 } else if(p->type == LinkTypeOut && inout->type == LinkTypeIn) {
27afb09d
VS
314 src = p;
315 dst = inout;
316 } else {
317 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
318 inout->name);
319 goto fail;
320 }
321
2839ff5e 322 if(link_filter(src->filter, src->pad_idx, dst->filter, dst->pad_idx) < 0)
27afb09d
VS
323 goto fail;
324
37161d64
VS
325 src->filter = NULL;
326 dst->filter = NULL;
27afb09d
VS
327 }
328 }
329
330 free_inout(head);
331
2839ff5e 332 if(!has_out) {
ee75692a 333 if(link_filter(last_filt, pad, out, outpad))
27afb09d 334 goto fail;
27afb09d
VS
335 }
336
337 return 0;
338
339 fail:
340 free_inout(head);
341 avfilter_destroy_graph(graph);
342 return -1;
343}