Commit | Line | Data |
---|---|---|
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 | 39 | static 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 | 60 | static 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 | |
c58572f8 | 89 | * @param put here a filter context in case of successful creation and configuration, NULL otherwise. |
98137a1a SS |
90 | * @param index an index which is supposed to be unique for each filter instance added to the filtergraph |
91 | * @param filt_name the name of the filter to create | |
92 | * @param args the arguments provided to the filter during its initialization | |
93 | * @param log_ctx the log context to use | |
c58572f8 | 94 | * @return 0 in case of success, a negative AVERROR code otherwise |
98137a1a | 95 | */ |
c58572f8 SS |
96 | static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index, |
97 | const char *filt_name, const char *args, AVClass *log_ctx) | |
8e74c889 | 98 | { |
e4a5f397 | 99 | AVFilter *filt; |
8e74c889 | 100 | char inst_name[30]; |
b5049814 | 101 | char tmp_args[256]; |
c58572f8 | 102 | int ret; |
8e74c889 | 103 | |
03c3bb5c | 104 | snprintf(inst_name, sizeof(inst_name), "Filter %d %s", index, filt_name); |
8e74c889 | 105 | |
e4a5f397 | 106 | filt = avfilter_get_by_name(filt_name); |
5e600185 | 107 | |
f219eee5 | 108 | if (!filt) { |
8e74c889 | 109 | av_log(log_ctx, AV_LOG_ERROR, |
bb90d855 | 110 | "No such filter: '%s'\n", filt_name); |
c58572f8 | 111 | return AVERROR(EINVAL); |
8e74c889 VS |
112 | } |
113 | ||
c58572f8 SS |
114 | ret = avfilter_open(filt_ctx, filt, inst_name); |
115 | if (!*filt_ctx) { | |
8e74c889 | 116 | av_log(log_ctx, AV_LOG_ERROR, |
bb90d855 | 117 | "Error creating filter '%s'\n", filt_name); |
c58572f8 | 118 | return ret; |
8e74c889 VS |
119 | } |
120 | ||
c58572f8 SS |
121 | if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) { |
122 | avfilter_destroy(*filt_ctx); | |
123 | return ret; | |
64b164f4 | 124 | } |
8e74c889 | 125 | |
b5049814 BC |
126 | if (!strcmp(filt_name, "scale") && !strstr(args, "flags")) { |
127 | snprintf(tmp_args, sizeof(tmp_args), "%s:%s", | |
128 | args, ctx->scale_sws_opts); | |
129 | args = tmp_args; | |
130 | } | |
131 | ||
c58572f8 | 132 | if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) { |
8e74c889 | 133 | av_log(log_ctx, AV_LOG_ERROR, |
bb90d855 | 134 | "Error initializing filter '%s' with args '%s'\n", filt_name, args); |
c58572f8 | 135 | return ret; |
8e74c889 VS |
136 | } |
137 | ||
c58572f8 | 138 | return 0; |
8e74c889 VS |
139 | } |
140 | ||
f5cbde2e VS |
141 | /** |
142 | * Parse "filter=params" | |
f5cbde2e | 143 | */ |
688b9dad SS |
144 | static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph, |
145 | int index, AVClass *log_ctx) | |
f5cbde2e | 146 | { |
ba3fed2f | 147 | char *opts = NULL; |
15a316c0 | 148 | char *name = av_get_token(buf, "=,;[\n"); |
c58572f8 | 149 | int ret; |
f5cbde2e | 150 | |
f219eee5 | 151 | if (**buf == '=') { |
f5cbde2e | 152 | (*buf)++; |
15a316c0 | 153 | opts = av_get_token(buf, "[],;\n"); |
12849837 | 154 | } |
f5cbde2e | 155 | |
688b9dad | 156 | ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx); |
64b164f4 VS |
157 | av_free(name); |
158 | av_free(opts); | |
688b9dad | 159 | return ret; |
f5cbde2e VS |
160 | } |
161 | ||
27afb09d VS |
162 | static void free_inout(AVFilterInOut *head) |
163 | { | |
f219eee5 | 164 | while (head) { |
55672c83 | 165 | AVFilterInOut *next = head->next; |
64b164f4 | 166 | av_free(head->name); |
27afb09d VS |
167 | av_free(head); |
168 | head = next; | |
169 | } | |
170 | } | |
171 | ||
c9987633 VS |
172 | static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links) |
173 | { | |
174 | AVFilterInOut *ret; | |
c9987633 | 175 | |
f219eee5 | 176 | while (*links && strcmp((*links)->name, label)) |
f6557d5e | 177 | links = &((*links)->next); |
c9987633 | 178 | |
f6557d5e | 179 | ret = *links; |
c9987633 | 180 | |
f219eee5 | 181 | if (ret) |
f6557d5e | 182 | *links = ret->next; |
c9987633 VS |
183 | |
184 | return ret; | |
185 | } | |
186 | ||
e97908ee VS |
187 | static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element) |
188 | { | |
189 | element->next = *inouts; | |
190 | *inouts = element; | |
191 | } | |
c9987633 | 192 | |
ed581e65 | 193 | static int link_filter_inouts(AVFilterContext *filt_ctx, |
7f9b3266 SS |
194 | AVFilterInOut **curr_inputs, |
195 | AVFilterInOut **open_inputs, AVClass *log_ctx) | |
c9987633 | 196 | { |
ed581e65 | 197 | int pad = filt_ctx->input_count, ret; |
c9987633 | 198 | |
f219eee5 | 199 | while (pad--) { |
7f9b3266 | 200 | AVFilterInOut *p = *curr_inputs; |
f219eee5 | 201 | if (!p) { |
c9987633 VS |
202 | av_log(log_ctx, AV_LOG_ERROR, |
203 | "Not enough inputs specified for the \"%s\" filter.\n", | |
ed581e65 | 204 | filt_ctx->filter->name); |
684ade49 | 205 | return AVERROR(EINVAL); |
c9987633 VS |
206 | } |
207 | ||
7f9b3266 | 208 | *curr_inputs = (*curr_inputs)->next; |
4d11beb2 | 209 | |
7313132b SS |
210 | if (p->filter_ctx) { |
211 | if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0) | |
684ade49 | 212 | return ret; |
64b164f4 | 213 | av_free(p->name); |
c9987633 VS |
214 | av_free(p); |
215 | } else { | |
7313132b | 216 | p->filter_ctx = filt_ctx; |
c9987633 | 217 | p->pad_idx = pad; |
7f9b3266 | 218 | insert_inout(open_inputs, p); |
c9987633 VS |
219 | } |
220 | } | |
221 | ||
f219eee5 | 222 | if (*curr_inputs) { |
c9987633 VS |
223 | av_log(log_ctx, AV_LOG_ERROR, |
224 | "Too many inputs specified for the \"%s\" filter.\n", | |
ed581e65 | 225 | filt_ctx->filter->name); |
684ade49 | 226 | return AVERROR(EINVAL); |
c9987633 VS |
227 | } |
228 | ||
ed581e65 | 229 | pad = filt_ctx->output_count; |
f219eee5 | 230 | while (pad--) { |
c956dd43 | 231 | AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut)); |
fbd97184 SS |
232 | if (!currlinkn) |
233 | return AVERROR(ENOMEM); | |
7313132b | 234 | currlinkn->filter_ctx = filt_ctx; |
c9987633 | 235 | currlinkn->pad_idx = pad; |
7f9b3266 | 236 | insert_inout(curr_inputs, currlinkn); |
c9987633 VS |
237 | } |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
7f9b3266 SS |
242 | static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs, |
243 | AVFilterInOut **open_outputs, AVClass *log_ctx) | |
27afb09d | 244 | { |
c9987633 | 245 | int pad = 0; |
c9987633 | 246 | |
f219eee5 | 247 | while (**buf == '[') { |
bd80b349 | 248 | char *name = parse_link_name(buf, log_ctx); |
b2ac16da | 249 | AVFilterInOut *match; |
22260824 | 250 | |
f219eee5 | 251 | if (!name) |
42e7f6d7 | 252 | return AVERROR(EINVAL); |
22260824 | 253 | |
7f9b3266 SS |
254 | /* First check if the label is not in the open_outputs list */ |
255 | match = extract_inout(name, open_outputs); | |
cf4f7d38 | 256 | |
f219eee5 | 257 | if (match) { |
64fbf5e2 | 258 | av_free(name); |
0de3407b VS |
259 | } else { |
260 | /* Not in the list, so add it as an input */ | |
00b3ca3c SS |
261 | if (!(match = av_mallocz(sizeof(AVFilterInOut)))) |
262 | return AVERROR(ENOMEM); | |
c880791f | 263 | match->name = name; |
c880791f | 264 | match->pad_idx = pad; |
cf4f7d38 | 265 | } |
e97908ee | 266 | |
7f9b3266 | 267 | insert_inout(curr_inputs, match); |
e97908ee | 268 | |
fd548e5b | 269 | *buf += strspn(*buf, WHITESPACES); |
c9987633 | 270 | pad++; |
27afb09d | 271 | } |
cf4f7d38 | 272 | |
27afb09d VS |
273 | return pad; |
274 | } | |
275 | ||
7f9b3266 SS |
276 | static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs, |
277 | AVFilterInOut **open_inputs, | |
278 | AVFilterInOut **open_outputs, AVClass *log_ctx) | |
9710beaf | 279 | { |
0cc8b659 | 280 | int ret, pad = 0; |
c9987633 | 281 | |
f219eee5 | 282 | while (**buf == '[') { |
bd80b349 | 283 | char *name = parse_link_name(buf, log_ctx); |
c9987633 VS |
284 | AVFilterInOut *match; |
285 | ||
7f9b3266 SS |
286 | AVFilterInOut *input = *curr_inputs; |
287 | *curr_inputs = (*curr_inputs)->next; | |
443c10ef | 288 | |
f219eee5 | 289 | if (!name) |
0cc8b659 | 290 | return AVERROR(EINVAL); |
c9987633 | 291 | |
7f9b3266 SS |
292 | /* First check if the label is not in the open_inputs list */ |
293 | match = extract_inout(name, open_inputs); | |
c9987633 | 294 | |
f219eee5 | 295 | if (match) { |
7313132b SS |
296 | if ((ret = link_filter(input->filter_ctx, input->pad_idx, |
297 | match->filter_ctx, match->pad_idx, log_ctx)) < 0) | |
0cc8b659 | 298 | return ret; |
64b164f4 VS |
299 | av_free(match->name); |
300 | av_free(name); | |
c9987633 | 301 | av_free(match); |
7baa6210 | 302 | av_free(input); |
0de3407b | 303 | } else { |
7f9b3266 | 304 | /* Not in the list, so add the first input as a open_output */ |
7baa6210 | 305 | input->name = name; |
7f9b3266 | 306 | insert_inout(open_outputs, input); |
c9987633 | 307 | } |
fd548e5b | 308 | *buf += strspn(*buf, WHITESPACES); |
c9987633 | 309 | pad++; |
8095a014 | 310 | } |
9710beaf | 311 | |
c9987633 VS |
312 | return pad; |
313 | } | |
9710beaf | 314 | |
86a47378 | 315 | int avfilter_graph_parse(AVFilterGraph *graph, const char *filters, |
7f9b3266 SS |
316 | AVFilterInOut *open_inputs, |
317 | AVFilterInOut *open_outputs, AVClass *log_ctx) | |
27afb09d | 318 | { |
c24f76b9 | 319 | int index = 0, ret; |
27afb09d | 320 | char chr = 0; |
27afb09d | 321 | |
7f9b3266 | 322 | AVFilterInOut *curr_inputs = NULL; |
27afb09d | 323 | |
27afb09d | 324 | do { |
9710beaf | 325 | AVFilterContext *filter; |
fd548e5b | 326 | filters += strspn(filters, WHITESPACES); |
27afb09d | 327 | |
c24f76b9 | 328 | if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0) |
27afb09d VS |
329 | goto fail; |
330 | ||
c24f76b9 | 331 | if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0) |
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]"; |
c24f76b9 | 337 | if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0) |
27afb09d | 338 | goto fail; |
27afb09d VS |
339 | } |
340 | ||
c24f76b9 | 341 | if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0) |
c9987633 | 342 | goto fail; |
da790674 | 343 | |
c24f76b9 SS |
344 | if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs, |
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); | |
c24f76b9 | 355 | ret = AVERROR(EINVAL); |
c9987633 | 356 | goto fail; |
27afb09d | 357 | } |
c9987633 | 358 | index++; |
f219eee5 | 359 | } while (chr == ',' || chr == ';'); |
27afb09d | 360 | |
fd51ff16 | 361 | if (chr) { |
78471234 SS |
362 | av_log(log_ctx, AV_LOG_ERROR, |
363 | "Unable to parse graph description substring: \"%s\"\n", | |
364 | filters - 1); | |
c24f76b9 | 365 | ret = AVERROR(EINVAL); |
78471234 SS |
366 | goto fail; |
367 | } | |
368 | ||
f219eee5 | 369 | if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) { |
e916c2ac | 370 | /* Last output can be omitted if it is "[out]" */ |
c9987633 | 371 | const char *tmp = "[out]"; |
c24f76b9 SS |
372 | if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs, |
373 | log_ctx)) < 0) | |
27afb09d | 374 | goto fail; |
27afb09d VS |
375 | } |
376 | ||
377 | return 0; | |
378 | ||
379 | fail: | |
97dd1e4a | 380 | avfilter_graph_free(graph); |
7f9b3266 SS |
381 | free_inout(open_inputs); |
382 | free_inout(open_outputs); | |
383 | free_inout(curr_inputs); | |
c24f76b9 | 384 | return ret; |
27afb09d | 385 | } |