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