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
26 #include "graphparser.h"
28 #include "avfiltergraph.h"
30 #define WHITESPACES " \n\t"
32 static int link_filter(AVFilterContext *src, int srcpad,
33 AVFilterContext *dst, int dstpad,
36 if(avfilter_link(src, srcpad, dst, dstpad)) {
37 av_log(log_ctx, AV_LOG_ERROR,
38 "cannot create the link %s:%d -> %s:%d\n",
39 src->filter->name, srcpad, dst->filter->name, dstpad);
47 * Consumes a string from *buf.
48 * @return a copy of the consumed string, which should be free'd after use
50 static char *consume_string(const char **buf)
52 char *out = av_malloc(strlen(*buf) + 1);
55 *buf += strspn(*buf, WHITESPACES);
64 while(**buf && **buf != '\'')
84 *buf += strspn(*buf, WHITESPACES);
91 * @param name a pointer (that need to be free'd after use) to the name between
94 static char *parse_link_name(const char **buf, AVClass *log_ctx)
96 const char *start = *buf;
100 name = consume_string(buf);
103 av_log(log_ctx, AV_LOG_ERROR,
104 "Bad (empty?) label found in the following: \"%s\".\n", start);
108 if(*(*buf)++ != ']') {
109 av_log(log_ctx, AV_LOG_ERROR,
110 "Mismatched '[' found in the following: \"%s\".\n", start);
118 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
119 const char *filt_name, const char *args,
122 AVFilterContext *filt_ctx;
127 snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
129 filt = avfilter_get_by_name(filt_name);
132 av_log(log_ctx, AV_LOG_ERROR,
133 "no such filter: '%s'\n", filt_name);
137 filt_ctx = avfilter_open(filt, inst_name);
139 av_log(log_ctx, AV_LOG_ERROR,
140 "error creating filter '%s'\n", filt_name);
144 if(avfilter_graph_add_filter(ctx, filt_ctx) < 0) {
145 avfilter_destroy(filt_ctx);
149 if(avfilter_init_filter(filt_ctx, args, NULL)) {
150 av_log(log_ctx, AV_LOG_ERROR,
151 "error initializing filter '%s' with args '%s'\n", filt_name, args);
159 * Parse "filter=params"
161 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
162 int index, AVClass *log_ctx)
165 char *name = consume_string(buf);
166 AVFilterContext *ret;
170 opts = consume_string(buf);
173 ret = create_filter(graph, index, name, opts, log_ctx);
179 static void free_inout(AVFilterInOut *head)
182 AVFilterInOut *next = head->next;
189 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
193 while(*links && strcmp((*links)->name, label))
194 links = &((*links)->next);
204 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
206 element->next = *inouts;
210 static int link_filter_inouts(AVFilterContext *filter,
211 AVFilterInOut **curr_inputs,
212 AVFilterInOut **open_inputs, AVClass *log_ctx)
214 int pad = filter->input_count;
217 AVFilterInOut *p = *curr_inputs;
219 av_log(log_ctx, AV_LOG_ERROR,
220 "Not enough inputs specified for the \"%s\" filter.\n",
221 filter->filter->name);
225 *curr_inputs = (*curr_inputs)->next;
228 if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
235 insert_inout(open_inputs, p);
240 av_log(log_ctx, AV_LOG_ERROR,
241 "Too many inputs specified for the \"%s\" filter.\n",
242 filter->filter->name);
246 pad = filter->output_count;
248 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
249 currlinkn->filter = filter;
250 currlinkn->pad_idx = pad;
251 insert_inout(curr_inputs, currlinkn);
257 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
258 AVFilterInOut **open_outputs, AVClass *log_ctx)
262 while(**buf == '[') {
263 char *name = parse_link_name(buf, log_ctx);
264 AVFilterInOut *match;
269 /* First check if the label is not in the open_outputs list */
270 match = extract_inout(name, open_outputs);
275 /* Not in the list, so add it as an input */
276 match = av_mallocz(sizeof(AVFilterInOut));
278 match->pad_idx = pad;
281 insert_inout(curr_inputs, match);
283 *buf += strspn(*buf, WHITESPACES);
290 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
291 AVFilterInOut **open_inputs,
292 AVFilterInOut **open_outputs, AVClass *log_ctx)
296 while(**buf == '[') {
297 char *name = parse_link_name(buf, log_ctx);
298 AVFilterInOut *match;
300 AVFilterInOut *input = *curr_inputs;
301 *curr_inputs = (*curr_inputs)->next;
306 /* First check if the label is not in the open_inputs list */
307 match = extract_inout(name, open_inputs);
310 if(link_filter(input->filter, input->pad_idx,
311 match->filter, match->pad_idx, log_ctx) < 0)
313 av_free(match->name);
318 /* Not in the list, so add the first input as a open_output */
320 insert_inout(open_outputs, input);
322 *buf += strspn(*buf, WHITESPACES);
329 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
330 AVFilterInOut *open_inputs,
331 AVFilterInOut *open_outputs, AVClass *log_ctx)
336 AVFilterInOut *curr_inputs = NULL;
339 AVFilterContext *filter;
340 filters += strspn(filters, WHITESPACES);
342 if(parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
345 filter = parse_filter(&filters, graph, index, log_ctx);
350 if(filter->input_count == 1 && !curr_inputs && !index) {
351 /* First input can be omitted if it is "[in]" */
352 const char *tmp = "[in]";
353 if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
357 if(link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
360 if(parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
364 filters += strspn(filters, WHITESPACES);
367 if(chr == ';' && curr_inputs) {
368 av_log(log_ctx, AV_LOG_ERROR,
369 "Could not find a output to link when parsing \"%s\"\n",
374 } while(chr == ',' || chr == ';');
377 av_log(log_ctx, AV_LOG_ERROR,
378 "Unable to parse graph description substring: \"%s\"\n",
383 if(open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
384 /* Last output can be omitted if it is "[out]" */
385 const char *tmp = "[out]";
386 if(parse_outputs(&tmp, &curr_inputs, &open_inputs,
387 &open_outputs, log_ctx) < 0)
394 avfilter_graph_destroy(graph);
395 free_inout(open_inputs);
396 free_inout(open_outputs);
397 free_inout(curr_inputs);