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