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 static int link_filter(AVFilterContext *src, int srcpad,
31 AVFilterContext *dst, int dstpad,
34 if(avfilter_link(src, srcpad, dst, dstpad)) {
35 av_log(log_ctx, AV_LOG_ERROR,
36 "cannot create the link %s:%d -> %s:%d\n",
37 src->filter->name, srcpad, dst->filter->name, dstpad);
44 static int consume_whitespace(const char *buf)
46 return strspn(buf, " \n\t");
50 * Consumes a string from *buf.
51 * @return a copy of the consumed string, which should be free'd after use
53 static char *consume_string(const char **buf)
55 char *out = av_malloc(strlen(*buf) + 1);
58 *buf += consume_whitespace(*buf);
67 while(**buf && **buf != '\'')
87 *buf += consume_whitespace(*buf);
94 * @param name a pointer (that need to be free'd after use) to the name between
97 static char *parse_link_name(const char **buf, AVClass *log_ctx)
99 const char *start = *buf;
103 name = consume_string(buf);
106 av_log(log_ctx, AV_LOG_ERROR,
107 "Bad (empty?) label found in the following: \"%s\".\n", start);
111 if(*(*buf)++ != ']') {
112 av_log(log_ctx, AV_LOG_ERROR,
113 "Mismatched '[' found in the following: \"%s\".\n", start);
121 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
122 const char *name, const char *args,
125 AVFilterContext *filt;
130 snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
132 filterdef = avfilter_get_by_name(name);
135 av_log(log_ctx, AV_LOG_ERROR,
136 "no such filter: '%s'\n", name);
140 filt = avfilter_open(filterdef, inst_name);
142 av_log(log_ctx, AV_LOG_ERROR,
143 "error creating filter '%s'\n", name);
147 if(avfilter_graph_add_filter(ctx, filt) < 0)
150 if(avfilter_init_filter(filt, args, NULL)) {
151 av_log(log_ctx, AV_LOG_ERROR,
152 "error initializing filter '%s' with args '%s'\n", name, args);
160 * Parse "filter=params"
162 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
163 int index, AVClass *log_ctx)
166 char *name = consume_string(buf);
170 opts = consume_string(buf);
173 return create_filter(graph, index, name, opts, log_ctx);
176 static void free_inout(AVFilterInOut *head)
179 AVFilterInOut *next = head->next;
185 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
189 while(*links && strcmp((*links)->name, label))
190 links = &((*links)->next);
200 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
202 element->next = *inouts;
206 static int link_filter_inouts(AVFilterContext *filter,
207 AVFilterInOut **currInputs,
208 AVFilterInOut **openLinks, AVClass *log_ctx)
210 int pad = filter->input_count;
213 AVFilterInOut *p = *currInputs;
214 *currInputs = (*currInputs)->next;
216 av_log(log_ctx, AV_LOG_ERROR,
217 "Not enough inputs specified for the \"%s\" filter.\n",
218 filter->filter->name);
223 if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
229 insert_inout(openInputs, p);
234 av_log(log_ctx, AV_LOG_ERROR,
235 "Too many inputs specified for the \"%s\" filter.\n",
236 filter->filter->name);
240 pad = filter->output_count;
242 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
243 currlinkn->type = LinkTypeOut;
244 currlinkn->filter = filter;
245 currlinkn->pad_idx = pad;
246 insert_inout(currInputs, currlinkn);
252 static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
253 AVFilterInOut **openLinks, AVClass *log_ctx)
257 while(**buf == '[') {
258 char *name = parse_link_name(buf, log_ctx);
259 AVFilterInOut *match;
264 /* First check if the label is not in the openLinks list */
265 match = extract_inout(name, openLinks);
268 /* A label of a open link. Make it one of the inputs of the next
270 if(match->type != LinkTypeOut) {
271 av_log(log_ctx, AV_LOG_ERROR,
272 "Label \"%s\" appears twice as input!\n", match->name);
276 /* Not in the list, so add it as an input */
277 match = av_mallocz(sizeof(AVFilterInOut));
279 match->type = LinkTypeIn;
280 match->pad_idx = pad;
283 insert_inout(currInputs, match);
285 *buf += consume_whitespace(*buf);
292 static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
293 AVFilterInOut **openLinks, AVClass *log_ctx)
297 while(**buf == '[') {
298 char *name = parse_link_name(buf, log_ctx);
299 AVFilterInOut *match;
301 AVFilterInOut *input = *currInputs;
302 *currInputs = (*currInputs)->next;
307 /* First check if the label is not in the openLinks list */
308 match = extract_inout(name, openLinks);
311 /* A label of a open link. Link it. */
312 if(match->type != LinkTypeIn) {
313 av_log(log_ctx, AV_LOG_ERROR,
314 "Label \"%s\" appears twice as output!\n", match->name);
318 if(link_filter(input->filter, input->pad_idx,
319 match->filter, match->pad_idx, log_ctx) < 0)
324 /* Not in the list, so add the first input as a openLink */
325 input->next = *openLinks;
326 input->type = LinkTypeOut;
328 insert_inout(openOutputs, input);
330 *buf += consume_whitespace(*buf);
337 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
338 AVFilterInOut *openLinks, AVClass *log_ctx)
343 AVFilterInOut *currInputs = NULL;
346 AVFilterContext *filter;
347 filters += consume_whitespace(filters);
349 if(parse_inputs(&filters, &currInputs, &openLinks, log_ctx) < 0)
352 filter = parse_filter(&filters, graph, index, log_ctx);
357 if(filter->input_count == 1 && !currInputs && !index) {
358 /* First input can be ommitted if it is "[in]" */
359 const char *tmp = "[in]";
360 if(parse_inputs(&tmp, &currInputs, &openLinks, log_ctx))
364 if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
367 if(parse_outputs(&filters, &currInputs, &openLinks, log_ctx))
370 filters += consume_whitespace(filters);
373 if(chr == ';' && currInputs) {
374 av_log(log_ctx, AV_LOG_ERROR,
375 "Could not find a output to link when parsing \"%s\"\n",
380 } while(chr == ',' || chr == ';');
382 if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
383 /* Last output can be ommitted if it is "[out]" */
384 const char *tmp = "[out]";
385 if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
392 avfilter_destroy_graph(graph);
393 free_inout(openLinks);
394 free_inout(currInputs);