Make x11grab cursor drawing suck less
[libav.git] / libavfilter / graphparser.c
CommitLineData
27afb09d
VS
1/*
2 * filter graph parser
3 * copyright (c) 2008 Vitor Sessak
4 * copyright (c) 2007 Bobby Bingham
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23#include <ctype.h>
24#include <string.h>
25
372e2884 26#include "libavutil/avstring.h"
6a0c770b 27#include "graphparser.h"
27afb09d
VS
28#include "avfilter.h"
29#include "avfiltergraph.h"
dd04911c 30#include "parseutils.h"
27afb09d 31
fd548e5b
SS
32#define WHITESPACES " \n\t"
33
d2874a9d
SS
34/**
35 * Link two filters together.
36 *
37 * @see avfilter_link()
38 */
9710beaf 39static int link_filter(AVFilterContext *src, int srcpad,
3a70bb2d
VS
40 AVFilterContext *dst, int dstpad,
41 AVClass *log_ctx)
27afb09d 42{
99ac59ca
SS
43 int ret;
44 if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
3a70bb2d 45 av_log(log_ctx, AV_LOG_ERROR,
bb90d855 46 "Cannot create the link %s:%d -> %s:%d\n",
9710beaf 47 src->filter->name, srcpad, dst->filter->name, dstpad);
99ac59ca 48 return ret;
27afb09d
VS
49 }
50
51 return 0;
52}
53
27afb09d 54/**
94b2120d
SS
55 * Parse the name of a link, which has the format "[linkname]".
56 *
57 * @return a pointer (that need to be freed after use) to the name
58 * between parenthesis
27afb09d 59 */
bd80b349 60static char *parse_link_name(const char **buf, AVClass *log_ctx)
27afb09d 61{
22260824 62 const char *start = *buf;
bd80b349 63 char *name;
27afb09d
VS
64 (*buf)++;
65
dd04911c 66 name = av_get_token(buf, "]");
27afb09d 67
f219eee5 68 if (!name[0]) {
3a70bb2d 69 av_log(log_ctx, AV_LOG_ERROR,
22260824 70 "Bad (empty?) label found in the following: \"%s\".\n", start);
27afb09d 71 goto fail;
22260824 72 }
27afb09d 73
f219eee5 74 if (*(*buf)++ != ']') {
3a70bb2d 75 av_log(log_ctx, AV_LOG_ERROR,
22260824 76 "Mismatched '[' found in the following: \"%s\".\n", start);
85cb8af7 77 fail:
bd80b349 78 av_freep(&name);
22260824 79 }
bd80b349
VS
80
81 return name;
27afb09d
VS
82}
83
98137a1a
SS
84/**
85 * Create an instance of a filter, initialize and insert it in the
86 * filtergraph in *ctx.
87 *
88 * @param ctx the filtergraph context
89 * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
90 * @param filt_name the name of the filter to create
91 * @param args the arguments provided to the filter during its initialization
92 * @param log_ctx the log context to use
93 * @return a filter context in case of successful creation and configuration, NULL otherwise.
94 */
8e74c889 95static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
e4a5f397 96 const char *filt_name, const char *args,
8e74c889
VS
97 AVClass *log_ctx)
98{
e4a5f397 99 AVFilterContext *filt_ctx;
8e74c889 100
e4a5f397 101 AVFilter *filt;
8e74c889 102 char inst_name[30];
b5049814 103 char tmp_args[256];
8e74c889 104
03c3bb5c 105 snprintf(inst_name, sizeof(inst_name), "Filter %d %s", index, filt_name);
8e74c889 106
e4a5f397 107 filt = avfilter_get_by_name(filt_name);
5e600185 108
f219eee5 109 if (!filt) {
8e74c889 110 av_log(log_ctx, AV_LOG_ERROR,
bb90d855 111 "No such filter: '%s'\n", filt_name);
8e74c889
VS
112 return NULL;
113 }
114
84c03869 115 avfilter_open(&filt_ctx, filt, inst_name);
f219eee5 116 if (!filt_ctx) {
8e74c889 117 av_log(log_ctx, AV_LOG_ERROR,
bb90d855 118 "Error creating filter '%s'\n", filt_name);
8e74c889
VS
119 return NULL;
120 }
121
f219eee5 122 if (avfilter_graph_add_filter(ctx, filt_ctx) < 0) {
e4a5f397 123 avfilter_destroy(filt_ctx);
8e74c889 124 return NULL;
64b164f4 125 }
8e74c889 126
b5049814
BC
127 if (!strcmp(filt_name, "scale") && !strstr(args, "flags")) {
128 snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
129 args, ctx->scale_sws_opts);
130 args = tmp_args;
131 }
132
f219eee5 133 if (avfilter_init_filter(filt_ctx, args, NULL)) {
8e74c889 134 av_log(log_ctx, AV_LOG_ERROR,
bb90d855 135 "Error initializing filter '%s' with args '%s'\n", filt_name, args);
8e74c889
VS
136 return NULL;
137 }
138
e4a5f397 139 return filt_ctx;
8e74c889
VS
140}
141
f5cbde2e
VS
142/**
143 * Parse "filter=params"
f5cbde2e
VS
144 */
145static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
146 int index, AVClass *log_ctx)
147{
ba3fed2f 148 char *opts = NULL;
15a316c0 149 char *name = av_get_token(buf, "=,;[\n");
64b164f4 150 AVFilterContext *ret;
f5cbde2e 151
f219eee5 152 if (**buf == '=') {
f5cbde2e 153 (*buf)++;
15a316c0 154 opts = av_get_token(buf, "[],;\n");
12849837 155 }
f5cbde2e 156
64b164f4
VS
157 ret = create_filter(graph, index, name, opts, log_ctx);
158 av_free(name);
159 av_free(opts);
160 return ret;
f5cbde2e
VS
161}
162
27afb09d
VS
163static void free_inout(AVFilterInOut *head)
164{
f219eee5 165 while (head) {
55672c83 166 AVFilterInOut *next = head->next;
64b164f4 167 av_free(head->name);
27afb09d
VS
168 av_free(head);
169 head = next;
170 }
171}
172
c9987633
VS
173static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
174{
175 AVFilterInOut *ret;
c9987633 176
f219eee5 177 while (*links && strcmp((*links)->name, label))
f6557d5e 178 links = &((*links)->next);
c9987633 179
f6557d5e 180 ret = *links;
c9987633 181
f219eee5 182 if (ret)
f6557d5e 183 *links = ret->next;
c9987633
VS
184
185 return ret;
186}
187
e97908ee
VS
188static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
189{
190 element->next = *inouts;
191 *inouts = element;
192}
c9987633
VS
193
194static int link_filter_inouts(AVFilterContext *filter,
7f9b3266
SS
195 AVFilterInOut **curr_inputs,
196 AVFilterInOut **open_inputs, AVClass *log_ctx)
c9987633 197{
90ed076c 198 int pad = filter->input_count;
c9987633 199
f219eee5 200 while (pad--) {
7f9b3266 201 AVFilterInOut *p = *curr_inputs;
f219eee5 202 if (!p) {
c9987633
VS
203 av_log(log_ctx, AV_LOG_ERROR,
204 "Not enough inputs specified for the \"%s\" filter.\n",
089d3714 205 filter->filter->name);
c9987633
VS
206 return -1;
207 }
208
7f9b3266 209 *curr_inputs = (*curr_inputs)->next;
4d11beb2 210
f219eee5
SS
211 if (p->filter) {
212 if (link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
c9987633 213 return -1;
64b164f4 214 av_free(p->name);
c9987633
VS
215 av_free(p);
216 } else {
c9987633
VS
217 p->filter = filter;
218 p->pad_idx = pad;
7f9b3266 219 insert_inout(open_inputs, p);
c9987633
VS
220 }
221 }
222
f219eee5 223 if (*curr_inputs) {
c9987633
VS
224 av_log(log_ctx, AV_LOG_ERROR,
225 "Too many inputs specified for the \"%s\" filter.\n",
089d3714 226 filter->filter->name);
c9987633
VS
227 return -1;
228 }
229
230 pad = filter->output_count;
f219eee5 231 while (pad--) {
c956dd43 232 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
c9987633
VS
233 currlinkn->filter = filter;
234 currlinkn->pad_idx = pad;
7f9b3266 235 insert_inout(curr_inputs, currlinkn);
c9987633
VS
236 }
237
238 return 0;
239}
240
7f9b3266
SS
241static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
242 AVFilterInOut **open_outputs, AVClass *log_ctx)
27afb09d 243{
c9987633 244 int pad = 0;
c9987633 245
f219eee5 246 while (**buf == '[') {
bd80b349 247 char *name = parse_link_name(buf, log_ctx);
b2ac16da 248 AVFilterInOut *match;
22260824 249
f219eee5 250 if (!name)
22260824 251 return -1;
22260824 252
7f9b3266
SS
253 /* First check if the label is not in the open_outputs list */
254 match = extract_inout(name, open_outputs);
cf4f7d38 255
f219eee5 256 if (match) {
64fbf5e2 257 av_free(name);
0de3407b
VS
258 } else {
259 /* Not in the list, so add it as an input */
c880791f
VS
260 match = av_mallocz(sizeof(AVFilterInOut));
261 match->name = name;
c880791f 262 match->pad_idx = pad;
cf4f7d38 263 }
e97908ee 264
7f9b3266 265 insert_inout(curr_inputs, match);
e97908ee 266
fd548e5b 267 *buf += strspn(*buf, WHITESPACES);
c9987633 268 pad++;
27afb09d 269 }
cf4f7d38 270
27afb09d
VS
271 return pad;
272}
273
7f9b3266
SS
274static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
275 AVFilterInOut **open_inputs,
276 AVFilterInOut **open_outputs, AVClass *log_ctx)
9710beaf 277{
c9987633
VS
278 int pad = 0;
279
f219eee5 280 while (**buf == '[') {
bd80b349 281 char *name = parse_link_name(buf, log_ctx);
c9987633
VS
282 AVFilterInOut *match;
283
7f9b3266
SS
284 AVFilterInOut *input = *curr_inputs;
285 *curr_inputs = (*curr_inputs)->next;
443c10ef 286
f219eee5 287 if (!name)
c9987633
VS
288 return -1;
289
7f9b3266
SS
290 /* First check if the label is not in the open_inputs list */
291 match = extract_inout(name, open_inputs);
c9987633 292
f219eee5
SS
293 if (match) {
294 if (link_filter(input->filter, input->pad_idx,
295 match->filter, match->pad_idx, log_ctx) < 0)
c9987633 296 return -1;
64b164f4
VS
297 av_free(match->name);
298 av_free(name);
c9987633 299 av_free(match);
7baa6210 300 av_free(input);
0de3407b 301 } else {
7f9b3266 302 /* Not in the list, so add the first input as a open_output */
7baa6210 303 input->name = name;
7f9b3266 304 insert_inout(open_outputs, input);
c9987633 305 }
fd548e5b 306 *buf += strspn(*buf, WHITESPACES);
c9987633 307 pad++;
8095a014 308 }
9710beaf 309
c9987633
VS
310 return pad;
311}
9710beaf 312
86a47378 313int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
7f9b3266
SS
314 AVFilterInOut *open_inputs,
315 AVFilterInOut *open_outputs, AVClass *log_ctx)
27afb09d 316{
27afb09d
VS
317 int index = 0;
318 char chr = 0;
27afb09d 319
7f9b3266 320 AVFilterInOut *curr_inputs = NULL;
27afb09d 321
27afb09d 322 do {
9710beaf 323 AVFilterContext *filter;
fd548e5b 324 filters += strspn(filters, WHITESPACES);
27afb09d 325
f219eee5 326 if (parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
27afb09d
VS
327 goto fail;
328
5e600185
VS
329 filter = parse_filter(&filters, graph, index, log_ctx);
330
f219eee5 331 if (!filter)
22260824
VS
332 goto fail;
333
f219eee5 334 if (filter->input_count == 1 && !curr_inputs && !index) {
e916c2ac 335 /* First input can be omitted if it is "[in]" */
c9987633 336 const char *tmp = "[in]";
7f9b3266 337 if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
27afb09d 338 goto fail;
27afb09d
VS
339 }
340
f219eee5 341 if (link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
c9987633 342 goto fail;
da790674 343
f219eee5 344 if (parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
e2b5fdaf 345 log_ctx) < 0)
e84f0b62
VS
346 goto fail;
347
fd548e5b 348 filters += strspn(filters, WHITESPACES);
27afb09d 349 chr = *filters++;
27afb09d 350
f219eee5 351 if (chr == ';' && curr_inputs) {
c9987633
VS
352 av_log(log_ctx, AV_LOG_ERROR,
353 "Could not find a output to link when parsing \"%s\"\n",
354 filters - 1);
355 goto fail;
27afb09d 356 }
c9987633 357 index++;
f219eee5 358 } while (chr == ',' || chr == ';');
27afb09d 359
fd51ff16 360 if (chr) {
78471234
SS
361 av_log(log_ctx, AV_LOG_ERROR,
362 "Unable to parse graph description substring: \"%s\"\n",
363 filters - 1);
364 goto fail;
365 }
366
f219eee5 367 if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
e916c2ac 368 /* Last output can be omitted if it is "[out]" */
c9987633 369 const char *tmp = "[out]";
f219eee5 370 if (parse_outputs(&tmp, &curr_inputs, &open_inputs,
7f9b3266 371 &open_outputs, log_ctx) < 0)
27afb09d 372 goto fail;
27afb09d
VS
373 }
374
375 return 0;
376
377 fail:
97dd1e4a 378 avfilter_graph_free(graph);
7f9b3266
SS
379 free_inout(open_inputs);
380 free_inout(open_outputs);
381 free_inout(curr_inputs);
27afb09d
VS
382 return -1;
383}