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