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 * @arg name a pointer (that need to be free'd after use) to the name between
97 static void parse_link_name(const char **buf, char **name, AVClass *log_ctx)
99 const char *start = *buf;
102 *name = consume_string(buf);
105 av_log(log_ctx, AV_LOG_ERROR,
106 "Bad (empty?) label found in the following: \"%s\".\n", start);
110 if(*(*buf)++ != ']') {
111 av_log(log_ctx, AV_LOG_ERROR,
112 "Mismatched '[' found in the following: \"%s\".\n", start);
118 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
119 const char *name, const char *args,
122 AVFilterContext *filt;
127 snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
129 if(!(filterdef = avfilter_get_by_name(name))) {
130 av_log(log_ctx, AV_LOG_ERROR,
131 "no such filter: '%s'\n", name);
135 if(!(filt = avfilter_open(filterdef, inst_name))) {
136 av_log(log_ctx, AV_LOG_ERROR,
137 "error creating filter '%s'\n", name);
141 if(avfilter_graph_add_filter(ctx, filt) < 0)
144 if(avfilter_init_filter(filt, args, NULL)) {
145 av_log(log_ctx, AV_LOG_ERROR,
146 "error initializing filter '%s' with args '%s'\n", name, args);
154 * Parse "filter=params"
155 * @arg name a pointer (that need to be free'd after use) to the name of the
157 * @arg ars a pointer (that need to be free'd after use) to the args of the
160 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
161 int index, AVClass *log_ctx)
164 char *name = consume_string(buf);
168 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);
201 static int link_filter_inouts(AVFilterContext *filter,
202 AVFilterInOut **currInputs,
203 AVFilterInOut **openLinks, AVClass *log_ctx)
207 pad = filter->input_count;
209 AVFilterInOut *p = *currInputs;
210 *currInputs = (*currInputs)->next;
212 av_log(log_ctx, AV_LOG_ERROR,
213 "Not enough inputs specified for the \"%s\" filter.\n",
219 if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
225 p->next = *openLinks;
232 av_log(log_ctx, AV_LOG_ERROR,
233 "Too many inputs specified for the \"%s\" filter.\n",
238 pad = filter->output_count;
240 AVFilterInOut *currlinkn = av_malloc(sizeof(AVFilterInOut));
241 currlinkn->name = NULL;
242 currlinkn->type = LinkTypeOut;
243 currlinkn->filter = filter;
244 currlinkn->pad_idx = pad;
245 currlinkn->next = *currInputs;
246 *currInputs = currlinkn;
252 static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
253 AVFilterInOut **openLinks, AVClass *log_ctx)
257 while(**buf == '[') {
259 AVFilterInOut *link_to_add;
260 AVFilterInOut *match;
262 parse_link_name(buf, &name, log_ctx);
267 /* First check if the label is not in the openLinks list */
268 match = extract_inout(name, openLinks);
271 /* A label of a open link. Make it one of the inputs of the next
273 if(match->type != LinkTypeOut) {
274 av_log(log_ctx, AV_LOG_ERROR,
275 "Label \"%s\" appears twice as input!\n", match->name);
281 /* Not in the list, so add it as an input */
282 link_to_add = av_malloc(sizeof(AVFilterInOut));
284 link_to_add->name = name;
285 link_to_add->type = LinkTypeIn;
286 link_to_add->filter = NULL;
287 link_to_add->pad_idx = pad;
289 link_to_add->next = *currInputs;
290 *currInputs = link_to_add;
291 *buf += consume_whitespace(*buf);
298 static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
299 AVFilterInOut **openLinks, AVClass *log_ctx)
303 while(**buf == '[') {
305 AVFilterInOut *match;
307 AVFilterInOut *input = *currInputs;
308 *currInputs = (*currInputs)->next;
310 parse_link_name(buf, &name, log_ctx);
315 /* First check if the label is not in the openLinks list */
316 match = extract_inout(name, openLinks);
319 /* A label of a open link. Link it. */
320 if(match->type != LinkTypeIn) {
321 av_log(log_ctx, AV_LOG_ERROR,
322 "Label \"%s\" appears twice as output!\n", match->name);
326 if(link_filter(input->filter, input->pad_idx,
327 match->filter, match->pad_idx, log_ctx) < 0)
332 /* Not in the list, so add the first input as a openLink */
333 input->next = *openLinks;
334 input->type = LinkTypeOut;
338 *buf += consume_whitespace(*buf);
346 * Parse a string describing a filter graph.
348 int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
349 AVFilterInOut *openLinks, AVClass *log_ctx)
355 AVFilterInOut *currInputs = NULL;
358 AVFilterContext *filter;
359 filters += consume_whitespace(filters);
361 pad = parse_inputs(&filters, &currInputs, &openLinks, log_ctx);
366 if(!(filter = parse_filter(&filters, graph, index, log_ctx)))
369 if(filter->input_count == 1 && !currInputs && !index) {
370 // First input can be ommitted if it is "[in]"
371 const char *tmp = "[in]";
372 pad = parse_inputs(&tmp, &currInputs, &openLinks, log_ctx);
377 if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
380 pad = parse_outputs(&filters, &currInputs, &openLinks, log_ctx);
385 filters += consume_whitespace(filters);
388 if(chr == ';' && currInputs) {
389 av_log(log_ctx, AV_LOG_ERROR,
390 "Could not find a output to link when parsing \"%s\"\n",
395 } while(chr == ',' || chr == ';');
397 if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
398 // Last output can be ommitted if it is "[out]"
399 const char *tmp = "[out]";
400 if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
407 avfilter_destroy_graph(graph);
408 free_inout(openLinks);
409 free_inout(currInputs);