Statistics
| Branch: | Revision:

ffmpeg / libavfilter / graphparser.c @ 34017fd9

History | View | Annotate | Download (10.3 KB)

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