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 if(!(filterdef = avfilter_get_by_name(name))) {
133 av_log(log_ctx, AV_LOG_ERROR,
134 "no such filter: '%s'\n", name);
138 if(!(filt = avfilter_open(filterdef, inst_name))) {
139 av_log(log_ctx, AV_LOG_ERROR,
140 "error creating filter '%s'\n", name);
144 if(avfilter_graph_add_filter(ctx, filt) < 0)
147 if(avfilter_init_filter(filt, args, NULL)) {
148 av_log(log_ctx, AV_LOG_ERROR,
149 "error initializing filter '%s' with args '%s'\n", name, args);
157 * Parse "filter=params"
158 * @arg name a pointer (that need to be free'd after use) to the name of the
160 * @arg ars a pointer (that need to be free'd after use) to the args of the
163 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
164 int index, AVClass *log_ctx)
167 char *name = consume_string(buf);
171 opts = consume_string(buf);
176 return create_filter(graph, index, name, opts, log_ctx);
179 static void free_inout(AVFilterInOut *head)
182 AVFilterInOut *next = head->next;
188 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
192 while(*links && strcmp((*links)->name, label))
193 links = &((*links)->next);
204 static int link_filter_inouts(AVFilterContext *filter,
205 AVFilterInOut **currInputs,
206 AVFilterInOut **openLinks, AVClass *log_ctx)
210 pad = filter->input_count;
212 AVFilterInOut *p = *currInputs;
213 *currInputs = (*currInputs)->next;
215 av_log(log_ctx, AV_LOG_ERROR,
216 "Not enough inputs specified for the \"%s\" filter.\n",
222 if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
228 p->next = *openLinks;
235 av_log(log_ctx, AV_LOG_ERROR,
236 "Too many inputs specified for the \"%s\" filter.\n",
241 pad = filter->output_count;
243 AVFilterInOut *currlinkn = av_malloc(sizeof(AVFilterInOut));
244 currlinkn->name = NULL;
245 currlinkn->type = LinkTypeOut;
246 currlinkn->filter = filter;
247 currlinkn->pad_idx = pad;
248 currlinkn->next = *currInputs;
249 *currInputs = currlinkn;
255 static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
256 AVFilterInOut **openLinks, AVClass *log_ctx)
260 while(**buf == '[') {
261 char *name = parse_link_name(buf, log_ctx);
262 AVFilterInOut *link_to_add;
263 AVFilterInOut *match;
268 /* First check if the label is not in the openLinks list */
269 match = extract_inout(name, openLinks);
272 /* A label of a open link. Make it one of the inputs of the next
274 if(match->type != LinkTypeOut) {
275 av_log(log_ctx, AV_LOG_ERROR,
276 "Label \"%s\" appears twice as input!\n", match->name);
282 /* Not in the list, so add it as an input */
283 link_to_add = av_malloc(sizeof(AVFilterInOut));
285 link_to_add->name = name;
286 link_to_add->type = LinkTypeIn;
287 link_to_add->filter = NULL;
288 link_to_add->pad_idx = pad;
290 link_to_add->next = *currInputs;
291 *currInputs = link_to_add;
292 *buf += consume_whitespace(*buf);
299 static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
300 AVFilterInOut **openLinks, AVClass *log_ctx)
304 while(**buf == '[') {
305 char *name = parse_link_name(buf, log_ctx);
306 AVFilterInOut *match;
308 AVFilterInOut *input = *currInputs;
309 *currInputs = (*currInputs)->next;
314 /* First check if the label is not in the openLinks list */
315 match = extract_inout(name, openLinks);
318 /* A label of a open link. Link it. */
319 if(match->type != LinkTypeIn) {
320 av_log(log_ctx, AV_LOG_ERROR,
321 "Label \"%s\" appears twice as output!\n", match->name);
325 if(link_filter(input->filter, input->pad_idx,
326 match->filter, match->pad_idx, log_ctx) < 0)
331 /* Not in the list, so add the first input as a openLink */
332 input->next = *openLinks;
333 input->type = LinkTypeOut;
337 *buf += consume_whitespace(*buf);
345 * Parse a string describing a filter graph.
347 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
348 AVFilterInOut *openLinks, AVClass *log_ctx)
354 AVFilterInOut *currInputs = NULL;
357 AVFilterContext *filter;
358 filters += consume_whitespace(filters);
360 pad = parse_inputs(&filters, &currInputs, &openLinks, log_ctx);
365 if(!(filter = parse_filter(&filters, graph, index, log_ctx)))
368 if(filter->input_count == 1 && !currInputs && !index) {
369 // First input can be ommitted if it is "[in]"
370 const char *tmp = "[in]";
371 pad = parse_inputs(&tmp, &currInputs, &openLinks, log_ctx);
376 if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
379 pad = parse_outputs(&filters, &currInputs, &openLinks, log_ctx);
384 filters += consume_whitespace(filters);
387 if(chr == ';' && currInputs) {
388 av_log(log_ctx, AV_LOG_ERROR,
389 "Could not find a output to link when parsing \"%s\"\n",
394 } while(chr == ',' || chr == ';');
396 if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
397 // Last output can be ommitted if it is "[out]"
398 const char *tmp = "[out]";
399 if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
406 avfilter_destroy_graph(graph);
407 free_inout(openLinks);
408 free_inout(currInputs);