Simplify coupling band loop.
[FFMpeg-mirror/lagarith.git] / libavfilter / graphparser.c
blob592934c6ff636aa7b035a567039b5e331e4ef2b2
1 /*
2 * filter graph parser
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
23 #include <ctype.h>
24 #include <string.h>
26 #include "graphparser.h"
27 #include "avfilter.h"
28 #include "avfiltergraph.h"
29 #include "parseutils.h"
31 #define WHITESPACES " \n\t"
33 static int link_filter(AVFilterContext *src, int srcpad,
34 AVFilterContext *dst, int dstpad,
35 AVClass *log_ctx)
37 if(avfilter_link(src, srcpad, dst, dstpad)) {
38 av_log(log_ctx, AV_LOG_ERROR,
39 "cannot create the link %s:%d -> %s:%d\n",
40 src->filter->name, srcpad, dst->filter->name, dstpad);
41 return -1;
44 return 0;
47 /**
48 * Parse "[linkname]"
49 * @param name a pointer (that need to be free'd after use) to the name between
50 * parenthesis
52 static char *parse_link_name(const char **buf, AVClass *log_ctx)
54 const char *start = *buf;
55 char *name;
56 (*buf)++;
58 name = av_get_token(buf, "]");
60 if(!name[0]) {
61 av_log(log_ctx, AV_LOG_ERROR,
62 "Bad (empty?) label found in the following: \"%s\".\n", start);
63 goto fail;
66 if(*(*buf)++ != ']') {
67 av_log(log_ctx, AV_LOG_ERROR,
68 "Mismatched '[' found in the following: \"%s\".\n", start);
69 fail:
70 av_freep(&name);
73 return name;
76 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
77 const char *filt_name, const char *args,
78 AVClass *log_ctx)
80 AVFilterContext *filt_ctx;
82 AVFilter *filt;
83 char inst_name[30];
85 snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
87 filt = avfilter_get_by_name(filt_name);
89 if(!filt) {
90 av_log(log_ctx, AV_LOG_ERROR,
91 "no such filter: '%s'\n", filt_name);
92 return NULL;
95 filt_ctx = avfilter_open(filt, inst_name);
96 if(!filt_ctx) {
97 av_log(log_ctx, AV_LOG_ERROR,
98 "error creating filter '%s'\n", filt_name);
99 return NULL;
102 if(avfilter_graph_add_filter(ctx, filt_ctx) < 0) {
103 avfilter_destroy(filt_ctx);
104 return NULL;
107 if(avfilter_init_filter(filt_ctx, args, NULL)) {
108 av_log(log_ctx, AV_LOG_ERROR,
109 "error initializing filter '%s' with args '%s'\n", filt_name, args);
110 return NULL;
113 return filt_ctx;
117 * Parse "filter=params"
119 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
120 int index, AVClass *log_ctx)
122 char *opts = NULL;
123 char *name = av_get_token(buf, "=,[");
124 AVFilterContext *ret;
126 if(**buf == '=') {
127 (*buf)++;
128 opts = av_get_token(buf, "[],\n");
131 ret = create_filter(graph, index, name, opts, log_ctx);
132 av_free(name);
133 av_free(opts);
134 return ret;
137 static void free_inout(AVFilterInOut *head)
139 while(head) {
140 AVFilterInOut *next = head->next;
141 av_free(head->name);
142 av_free(head);
143 head = next;
147 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
149 AVFilterInOut *ret;
151 while(*links && strcmp((*links)->name, label))
152 links = &((*links)->next);
154 ret = *links;
156 if(ret)
157 *links = ret->next;
159 return ret;
162 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
164 element->next = *inouts;
165 *inouts = element;
168 static int link_filter_inouts(AVFilterContext *filter,
169 AVFilterInOut **curr_inputs,
170 AVFilterInOut **open_inputs, AVClass *log_ctx)
172 int pad = filter->input_count;
174 while(pad--) {
175 AVFilterInOut *p = *curr_inputs;
176 if(!p) {
177 av_log(log_ctx, AV_LOG_ERROR,
178 "Not enough inputs specified for the \"%s\" filter.\n",
179 filter->filter->name);
180 return -1;
183 *curr_inputs = (*curr_inputs)->next;
185 if(p->filter) {
186 if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
187 return -1;
188 av_free(p->name);
189 av_free(p);
190 } else {
191 p->filter = filter;
192 p->pad_idx = pad;
193 insert_inout(open_inputs, p);
197 if(*curr_inputs) {
198 av_log(log_ctx, AV_LOG_ERROR,
199 "Too many inputs specified for the \"%s\" filter.\n",
200 filter->filter->name);
201 return -1;
204 pad = filter->output_count;
205 while(pad--) {
206 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
207 currlinkn->filter = filter;
208 currlinkn->pad_idx = pad;
209 insert_inout(curr_inputs, currlinkn);
212 return 0;
215 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
216 AVFilterInOut **open_outputs, AVClass *log_ctx)
218 int pad = 0;
220 while(**buf == '[') {
221 char *name = parse_link_name(buf, log_ctx);
222 AVFilterInOut *match;
224 if(!name)
225 return -1;
227 /* First check if the label is not in the open_outputs list */
228 match = extract_inout(name, open_outputs);
230 if(match) {
231 av_free(name);
232 } else {
233 /* Not in the list, so add it as an input */
234 match = av_mallocz(sizeof(AVFilterInOut));
235 match->name = name;
236 match->pad_idx = pad;
239 insert_inout(curr_inputs, match);
241 *buf += strspn(*buf, WHITESPACES);
242 pad++;
245 return pad;
248 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
249 AVFilterInOut **open_inputs,
250 AVFilterInOut **open_outputs, AVClass *log_ctx)
252 int pad = 0;
254 while(**buf == '[') {
255 char *name = parse_link_name(buf, log_ctx);
256 AVFilterInOut *match;
258 AVFilterInOut *input = *curr_inputs;
259 *curr_inputs = (*curr_inputs)->next;
261 if(!name)
262 return -1;
264 /* First check if the label is not in the open_inputs list */
265 match = extract_inout(name, open_inputs);
267 if(match) {
268 if(link_filter(input->filter, input->pad_idx,
269 match->filter, match->pad_idx, log_ctx) < 0)
270 return -1;
271 av_free(match->name);
272 av_free(name);
273 av_free(match);
274 av_free(input);
275 } else {
276 /* Not in the list, so add the first input as a open_output */
277 input->name = name;
278 insert_inout(open_outputs, input);
280 *buf += strspn(*buf, WHITESPACES);
281 pad++;
284 return pad;
287 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
288 AVFilterInOut *open_inputs,
289 AVFilterInOut *open_outputs, AVClass *log_ctx)
291 int index = 0;
292 char chr = 0;
294 AVFilterInOut *curr_inputs = NULL;
296 do {
297 AVFilterContext *filter;
298 filters += strspn(filters, WHITESPACES);
300 if(parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
301 goto fail;
303 filter = parse_filter(&filters, graph, index, log_ctx);
305 if(!filter)
306 goto fail;
308 if(filter->input_count == 1 && !curr_inputs && !index) {
309 /* First input can be omitted if it is "[in]" */
310 const char *tmp = "[in]";
311 if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
312 goto fail;
315 if(link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
316 goto fail;
318 if(parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
319 log_ctx) < 0)
320 goto fail;
322 filters += strspn(filters, WHITESPACES);
323 chr = *filters++;
325 if(chr == ';' && curr_inputs) {
326 av_log(log_ctx, AV_LOG_ERROR,
327 "Could not find a output to link when parsing \"%s\"\n",
328 filters - 1);
329 goto fail;
331 index++;
332 } while(chr == ',' || chr == ';');
334 if (chr) {
335 av_log(log_ctx, AV_LOG_ERROR,
336 "Unable to parse graph description substring: \"%s\"\n",
337 filters - 1);
338 goto fail;
341 if(open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
342 /* Last output can be omitted if it is "[out]" */
343 const char *tmp = "[out]";
344 if(parse_outputs(&tmp, &curr_inputs, &open_inputs,
345 &open_outputs, log_ctx) < 0)
346 goto fail;
349 return 0;
351 fail:
352 avfilter_graph_destroy(graph);
353 free_inout(open_inputs);
354 free_inout(open_outputs);
355 free_inout(curr_inputs);
356 return -1;