Statistics
| Branch: | Revision:

ffmpeg / libavfilter / graphparser.c @ master

History | View | Annotate | Download (11.6 KB)

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