Improve error handling
[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 44static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
03cea461 45 const char *name, const 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 103 char *ret = out;
27afb09d
VS
104
105 consume_whitespace(buf);
106
93b7a0f6 107 do{
a64821f4 108 char c = *(*buf)++;
93b7a0f6
VS
109 switch (c) {
110 case '\\':
a64821f4 111 *out++= *(*buf)++;
27afb09d 112 break;
93b7a0f6 113 case '\'':
a64821f4
VS
114 while(**buf && **buf != '\'')
115 *out++= *(*buf)++;
116 if(**buf) (*buf)++;
93b7a0f6
VS
117 break;
118 case 0:
d7ff2297
VS
119 case ']':
120 case '[':
93b7a0f6
VS
121 case '=':
122 case ',':
a78f2ccd
VS
123 case ' ':
124 case '\n':
93b7a0f6
VS
125 *out++= 0;
126 break;
127 default:
128 *out++= c;
129 }
130 } while(out[-1]);
27afb09d 131
a64821f4 132 (*buf)--;
a78f2ccd
VS
133 consume_whitespace(buf);
134
27afb09d
VS
135 return ret;
136}
137
138/**
ffac8784 139 * Parse "[linkname]"
27afb09d
VS
140 * @arg name a pointer (that need to be free'd after use) to the name between
141 * parenthesis
142 */
143static void parse_link_name(const char **buf, char **name)
144{
22260824 145 const char *start = *buf;
27afb09d
VS
146 (*buf)++;
147
148 *name = consume_string(buf);
149
22260824
VS
150 if(!*name[0]) {
151 av_log(&log_ctx, AV_LOG_ERROR,
152 "Bad (empty?) label found in the following: \"%s\".\n", start);
27afb09d 153 goto fail;
22260824 154 }
27afb09d 155
22260824
VS
156 if(*(*buf)++ != ']') {
157 av_log(&log_ctx, AV_LOG_ERROR,
158 "Mismatched '[' found in the following: \"%s\".\n", start);
85cb8af7
VS
159 fail:
160 av_freep(name);
22260824 161 }
27afb09d
VS
162}
163
164/**
165 * Parse "filter=params"
166 * @arg name a pointer (that need to be free'd after use) to the name of the
167 * filter
168 * @arg ars a pointer (that need to be free'd after use) to the args of the
169 * filter
170 */
9710beaf 171static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
27afb09d
VS
172{
173 char *name, *opts;
174 name = consume_string(buf);
175
2839ff5e 176 if(**buf == '=') {
27afb09d
VS
177 (*buf)++;
178 opts = consume_string(buf);
179 } else {
180 opts = NULL;
181 }
182
183 return create_filter(graph, index, name, opts);
184}
185
186enum LinkType {
187 LinkTypeIn,
188 LinkTypeOut,
189};
190
191/**
192 * A linked-list of the inputs/outputs of the filter chain.
193 */
194typedef struct AVFilterInOut {
195 enum LinkType type;
196 char *name;
37161d64 197 AVFilterContext *filter;
27afb09d
VS
198 int pad_idx;
199
200 struct AVFilterInOut *next;
201} AVFilterInOut;
202
203static void free_inout(AVFilterInOut *head)
204{
205 while (head) {
55672c83 206 AVFilterInOut *next = head->next;
27afb09d
VS
207 av_free(head);
208 head = next;
209 }
210}
211
212/**
2b7defc7
VS
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.
215 */
216static int handle_link(char *name, AVFilterInOut **inout, int pad,
217 enum LinkType type, AVFilterContext *filter)
218{
219 AVFilterInOut *p = *inout;
220
221 for (; p && strcmp(p->name, name); p = p->next);
222
223 if(!p) {
224 // First label apearence, add it to the linked list
225 AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
226
227 inoutn->name = name;
228 inoutn->type = type;
229 inoutn->filter = filter;
230 inoutn->pad_idx = pad;
231 inoutn->next = *inout;
232 *inout = inoutn;
233 return 0;
234 }
235
236 if(p->type == LinkTypeIn && type == LinkTypeOut) {
237 if(link_filter(filter, pad, p->filter, p->pad_idx) < 0)
238 goto fail;
239 } else if(p->type == LinkTypeOut && type == LinkTypeIn) {
240 if(link_filter(p->filter, p->pad_idx, filter, pad) < 0)
241 goto fail;
242 } else {
243 av_log(&log_ctx, AV_LOG_ERROR,
244 "Two links named '%s' are either both input or both output\n",
245 name);
246 goto fail;
247 }
248
249 p->filter = NULL;
250
251 return 0;
252 fail:
253 return -1;
254}
255
256
257/**
ffac8784 258 * Parse "[a1][link2] ... [etc]"
27afb09d 259 */
325cb1ef 260static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
37161d64 261 enum LinkType type, AVFilterContext *filter)
27afb09d 262{
d7ff2297 263 while (**buf == '[') {
2b7defc7 264 char *name;
22260824 265
2b7defc7
VS
266 parse_link_name(buf, &name);
267
a4501887 268 if(!name)
22260824 269 return -1;
22260824 270
7693be92
VS
271 if(handle_link(name, inout, pad++, type, filter) < 0)
272 return -1;
273
8095a014 274 consume_whitespace(buf);
27afb09d
VS
275 }
276 return pad;
277}
278
9710beaf
VS
279static const char *skip_inouts(const char *buf)
280{
8095a014 281 while (*buf == '[') {
a3acd1d9 282 buf += strcspn(buf, "]") + 1;
8095a014
VS
283 consume_whitespace(&buf);
284 }
9710beaf
VS
285 return buf;
286}
287
288
27afb09d
VS
289/**
290 * Parse a string describing a filter graph.
291 */
92973a04
VS
292int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
293 AVFilterContext *in, int inpad,
294 AVFilterContext *out, int outpad)
27afb09d
VS
295{
296 AVFilterInOut *inout=NULL;
297 AVFilterInOut *head=NULL;
298
299 int index = 0;
300 char chr = 0;
301 int pad = 0;
302 int has_out = 0;
303
9710beaf 304 AVFilterContext *last_filt = NULL;
27afb09d 305
27afb09d 306 do {
9710beaf 307 AVFilterContext *filter;
27afb09d 308 int oldpad = pad;
7a57c8d9
VS
309 const char *inouts;
310
311 consume_whitespace(&filters);
312 inouts = filters;
27afb09d 313
9710beaf
VS
314 // We need to parse the inputs of the filter after we create it, so
315 // skip it by now
316 filters = skip_inouts(filters);
27afb09d 317
2839ff5e 318 if(!(filter = parse_filter(&filters, graph, index)))
27afb09d
VS
319 goto fail;
320
9710beaf
VS
321 pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
322
22260824
VS
323 if(pad < 0)
324 goto fail;
325
27afb09d
VS
326 // If the first filter has an input and none was given, it is
327 // implicitly the input of the whole graph.
2839ff5e 328 if(pad == 0 && filter->input_count == 1) {
ee75692a 329 if(link_filter(in, inpad, filter, 0))
27afb09d 330 goto fail;
27afb09d
VS
331 }
332
333 if(chr == ',') {
2839ff5e 334 if(link_filter(last_filt, oldpad, filter, 0) < 0)
27afb09d 335 goto fail;
27afb09d 336 }
da790674 337
9710beaf 338 pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
7a57c8d9 339
e84f0b62
VS
340 if(pad < 0)
341 goto fail;
342
7a57c8d9
VS
343 consume_whitespace(&filters);
344
27afb09d
VS
345 chr = *filters++;
346 index++;
9710beaf 347 last_filt = filter;
27afb09d
VS
348 } while (chr == ',' || chr == ';');
349
350 head = inout;
2b7defc7 351 // Process remaining labels. Only inputs and outputs should be left.
a59a7734
VS
352 for (; inout; inout = inout->next) {
353 if(!inout->filter)
27afb09d
VS
354 continue; // Already processed
355
2839ff5e 356 if(!strcmp(inout->name, "in")) {
37161d64 357 if(link_filter(in, inpad, inout->filter, inout->pad_idx))
27afb09d 358 goto fail;
ee75692a 359
2839ff5e 360 } else if(!strcmp(inout->name, "out")) {
27afb09d 361 has_out = 1;
27afb09d 362
37161d64 363 if(link_filter(inout->filter, inout->pad_idx, out, outpad))
27afb09d 364 goto fail;
27afb09d
VS
365
366 } else {
2b7defc7
VS
367 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
368 inout->name);
27afb09d 369 goto fail;
27afb09d
VS
370 }
371 }
372
373 free_inout(head);
374
2839ff5e 375 if(!has_out) {
ee75692a 376 if(link_filter(last_filt, pad, out, outpad))
27afb09d 377 goto fail;
27afb09d
VS
378 }
379
380 return 0;
381
382 fail:
383 free_inout(head);
384 avfilter_destroy_graph(graph);
385 return -1;
386}