Statistics
| Branch: | Revision:

ffmpeg / libavfilter / graphparser.c @ d370e3e9

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