Statistics
| Branch: | Revision:

ffmpeg / libavfilter / graphparser.c @ 8095a014

History | View | Annotate | Download (8.86 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
#include "avfilter.h"
27
#include "avfiltergraph.h"
28
29
/**
30
 * For use in av_log
31
 */
32
static const char *log_name(void *p)
33
{
34
    return "Filter parser";
35
}
36
37
static const AVClass filter_parser_class = {
38
    "Filter parser",
39
    log_name
40
};
41
42
static const AVClass *log_ctx = &filter_parser_class;
43
44 9710beaf Vitor Sessak
static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
45 03cea461 Vitor Sessak
                                      const char *name, const char *args)
46 27afb09d Vitor Sessak
{
47
    AVFilterContext *filt;
48
49
    AVFilter *filterdef;
50 58a2d7a5 Vitor Sessak
    char inst_name[30];
51 27afb09d Vitor Sessak
52 58a2d7a5 Vitor Sessak
    snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
53 27a669ab Vitor Sessak
54
    if(!(filterdef = avfilter_get_by_name(name))) {
55
        av_log(&log_ctx, AV_LOG_ERROR,
56
               "no such filter: '%s'\n", name);
57 9710beaf Vitor Sessak
        return NULL;
58 27a669ab Vitor Sessak
    }
59
60 58a2d7a5 Vitor Sessak
    if(!(filt = avfilter_open(filterdef, inst_name))) {
61 27afb09d Vitor Sessak
        av_log(&log_ctx, AV_LOG_ERROR,
62
               "error creating filter '%s'\n", name);
63 9710beaf Vitor Sessak
        return NULL;
64 27afb09d Vitor Sessak
    }
65 046f49ba Vitor Sessak
66 2839ff5e Vitor Sessak
    if(avfilter_graph_add_filter(ctx, filt) < 0)
67 9710beaf Vitor Sessak
        return NULL;
68 046f49ba Vitor Sessak
69 27afb09d Vitor Sessak
    if(avfilter_init_filter(filt, args, NULL)) {
70
        av_log(&log_ctx, AV_LOG_ERROR,
71 27a669ab Vitor Sessak
               "error initializing filter '%s' with args '%s'\n", name, args);
72 9710beaf Vitor Sessak
        return NULL;
73 27afb09d Vitor Sessak
    }
74
75 9710beaf Vitor Sessak
    return filt;
76 27afb09d Vitor Sessak
}
77
78 9710beaf Vitor Sessak
static int link_filter(AVFilterContext *src, int srcpad,
79
                       AVFilterContext *dst, int dstpad)
80 27afb09d Vitor Sessak
{
81 9710beaf Vitor Sessak
    if(avfilter_link(src, srcpad, dst, dstpad)) {
82
        av_log(&log_ctx, AV_LOG_ERROR,
83
               "cannot create the link %s:%d -> %s:%d\n",
84
               src->filter->name, srcpad, dst->filter->name, dstpad);
85 27afb09d Vitor Sessak
        return -1;
86
    }
87
88
    return 0;
89
}
90
91
static void consume_whitespace(const char **buf)
92
{
93
    *buf += strspn(*buf, " \n\t");
94
}
95
96
/**
97
 * Consumes a string from *buf.
98
 * @return a copy of the consumed string, which should be free'd after use
99
 */
100
static char *consume_string(const char **buf)
101
{
102 2255026d Vitor Sessak
    char *out = av_malloc(strlen(*buf) + 1);
103 93b7a0f6 Vitor Sessak
    char *ret = out;
104 27afb09d Vitor Sessak
105
    consume_whitespace(buf);
106
107 93b7a0f6 Vitor Sessak
    do{
108 a64821f4 Vitor Sessak
        char c = *(*buf)++;
109 93b7a0f6 Vitor Sessak
        switch (c) {
110
        case '\\':
111 a64821f4 Vitor Sessak
            *out++= *(*buf)++;
112 27afb09d Vitor Sessak
            break;
113 93b7a0f6 Vitor Sessak
        case '\'':
114 a64821f4 Vitor Sessak
            while(**buf && **buf != '\'')
115
                *out++= *(*buf)++;
116
            if(**buf) (*buf)++;
117 93b7a0f6 Vitor Sessak
            break;
118
        case 0:
119 d7ff2297 Vitor Sessak
        case ']':
120
        case '[':
121 93b7a0f6 Vitor Sessak
        case '=':
122
        case ',':
123 a78f2ccd Vitor Sessak
        case ' ':
124
        case '\n':
125 93b7a0f6 Vitor Sessak
            *out++= 0;
126
            break;
127
        default:
128
            *out++= c;
129
        }
130
    } while(out[-1]);
131 27afb09d Vitor Sessak
132 a64821f4 Vitor Sessak
    (*buf)--;
133 a78f2ccd Vitor Sessak
    consume_whitespace(buf);
134
135 27afb09d Vitor Sessak
    return ret;
136
}
137
138
/**
139 ffac8784 Vitor Sessak
 * Parse "[linkname]"
140 27afb09d Vitor Sessak
 * @arg name a pointer (that need to be free'd after use) to the name between
141
 *           parenthesis
142
 */
143
static void parse_link_name(const char **buf, char **name)
144
{
145 22260824 Vitor Sessak
    const char *start = *buf;
146 27afb09d Vitor Sessak
    (*buf)++;
147
148
    *name = consume_string(buf);
149
150 22260824 Vitor Sessak
    if(!*name[0]) {
151
        av_log(&log_ctx, AV_LOG_ERROR,
152
               "Bad (empty?) label found in the following: \"%s\".\n", start);
153 27afb09d Vitor Sessak
        goto fail;
154 22260824 Vitor Sessak
    }
155 27afb09d Vitor Sessak
156 22260824 Vitor Sessak
    if(*(*buf)++ != ']') {
157
        av_log(&log_ctx, AV_LOG_ERROR,
158
               "Mismatched '[' found in the following: \"%s\".\n", start);
159 27afb09d Vitor Sessak
        goto fail;
160 22260824 Vitor Sessak
    }
161 27afb09d Vitor Sessak
162
    return;
163 22260824 Vitor Sessak
164 27afb09d Vitor Sessak
 fail:
165
    av_freep(name);
166
}
167
168
/**
169
 * Parse "filter=params"
170
 * @arg name a pointer (that need to be free'd after use) to the name of the
171
 *           filter
172
 * @arg ars  a pointer (that need to be free'd after use) to the args of the
173
 *           filter
174
 */
175 9710beaf Vitor Sessak
static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
176 27afb09d Vitor Sessak
{
177
    char *name, *opts;
178
    name = consume_string(buf);
179
180 2839ff5e Vitor Sessak
    if(**buf == '=') {
181 27afb09d Vitor Sessak
        (*buf)++;
182
        opts = consume_string(buf);
183
    } else {
184
        opts = NULL;
185
    }
186
187
    return create_filter(graph, index, name, opts);
188
}
189
190
enum LinkType {
191
    LinkTypeIn,
192
    LinkTypeOut,
193
};
194
195
/**
196
 * A linked-list of the inputs/outputs of the filter chain.
197
 */
198
typedef struct AVFilterInOut {
199
    enum LinkType type;
200
    char *name;
201 37161d64 Vitor Sessak
    AVFilterContext *filter;
202 27afb09d Vitor Sessak
    int pad_idx;
203
204
    struct AVFilterInOut *next;
205
} AVFilterInOut;
206
207
static void free_inout(AVFilterInOut *head)
208
{
209
    while (head) {
210 55672c83 Vitor Sessak
        AVFilterInOut *next = head->next;
211 27afb09d Vitor Sessak
        av_free(head);
212
        head = next;
213
    }
214
}
215
216
/**
217 ffac8784 Vitor Sessak
 * Parse "[a1][link2] ... [etc]"
218 27afb09d Vitor Sessak
 */
219 325cb1ef Vitor Sessak
static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
220 37161d64 Vitor Sessak
                        enum LinkType type, AVFilterContext *filter)
221 27afb09d Vitor Sessak
{
222 d7ff2297 Vitor Sessak
    while (**buf == '[') {
223 27afb09d Vitor Sessak
        AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
224
        parse_link_name(buf, &inoutn->name);
225 22260824 Vitor Sessak
226
        if (!inoutn->name) {
227
            av_free(inoutn);
228
            return -1;
229
        }
230
231 89475efd Vitor Sessak
        inoutn->type    = type;
232
        inoutn->filter  = filter;
233 27afb09d Vitor Sessak
        inoutn->pad_idx = pad++;
234 89475efd Vitor Sessak
        inoutn->next    = *inout;
235 27afb09d Vitor Sessak
        *inout = inoutn;
236 8095a014 Vitor Sessak
        consume_whitespace(buf);
237 27afb09d Vitor Sessak
    }
238
    return pad;
239
}
240
241 9710beaf Vitor Sessak
static const char *skip_inouts(const char *buf)
242
{
243 8095a014 Vitor Sessak
    while (*buf == '[') {
244 a3acd1d9 Vitor Sessak
        buf += strcspn(buf, "]") + 1;
245 8095a014 Vitor Sessak
        consume_whitespace(&buf);
246
    }
247 9710beaf Vitor Sessak
    return buf;
248
}
249
250
251 27afb09d Vitor Sessak
/**
252
 * Parse a string describing a filter graph.
253
 */
254 92973a04 Vitor Sessak
int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
255
                         AVFilterContext *in, int inpad,
256
                         AVFilterContext *out, int outpad)
257 27afb09d Vitor Sessak
{
258
    AVFilterInOut *inout=NULL;
259
    AVFilterInOut  *head=NULL;
260
261
    int index = 0;
262
    char chr = 0;
263
    int pad = 0;
264
    int has_out = 0;
265
266 9710beaf Vitor Sessak
    AVFilterContext *last_filt = NULL;
267 27afb09d Vitor Sessak
268
    do {
269 9710beaf Vitor Sessak
        AVFilterContext *filter;
270 27afb09d Vitor Sessak
        int oldpad = pad;
271 7a57c8d9 Vitor Sessak
        const char *inouts;
272
273
        consume_whitespace(&filters);
274
        inouts = filters;
275 27afb09d Vitor Sessak
276 9710beaf Vitor Sessak
        // We need to parse the inputs of the filter after we create it, so
277
        // skip it by now
278
        filters = skip_inouts(filters);
279 27afb09d Vitor Sessak
280 2839ff5e Vitor Sessak
        if(!(filter = parse_filter(&filters, graph, index)))
281 27afb09d Vitor Sessak
            goto fail;
282
283 9710beaf Vitor Sessak
        pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
284
285 22260824 Vitor Sessak
        if(pad < 0)
286
            goto fail;
287
288 27afb09d Vitor Sessak
        // If the first filter has an input and none was given, it is
289
        // implicitly the input of the whole graph.
290 2839ff5e Vitor Sessak
        if(pad == 0 && filter->input_count == 1) {
291 ee75692a Vitor Sessak
            if(link_filter(in, inpad, filter, 0))
292 27afb09d Vitor Sessak
                goto fail;
293
        }
294
295
        if(chr == ',') {
296 2839ff5e Vitor Sessak
            if(link_filter(last_filt, oldpad, filter, 0) < 0)
297 27afb09d Vitor Sessak
                goto fail;
298
        }
299 da790674 Vitor Sessak
300 9710beaf Vitor Sessak
        pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
301 7a57c8d9 Vitor Sessak
302
        consume_whitespace(&filters);
303
304 27afb09d Vitor Sessak
        chr = *filters++;
305
        index++;
306 9710beaf Vitor Sessak
        last_filt = filter;
307 27afb09d Vitor Sessak
    } while (chr == ',' || chr == ';');
308
309
    head = inout;
310
    for (; inout != NULL; inout = inout->next) {
311 2839ff5e Vitor Sessak
        if(inout->filter == NULL)
312 27afb09d Vitor Sessak
            continue; // Already processed
313
314 2839ff5e Vitor Sessak
        if(!strcmp(inout->name, "in")) {
315 37161d64 Vitor Sessak
            if(link_filter(in, inpad, inout->filter, inout->pad_idx))
316 27afb09d Vitor Sessak
                goto fail;
317 ee75692a Vitor Sessak
318 2839ff5e Vitor Sessak
        } else if(!strcmp(inout->name, "out")) {
319 27afb09d Vitor Sessak
            has_out = 1;
320
321 37161d64 Vitor Sessak
            if(link_filter(inout->filter, inout->pad_idx, out, outpad))
322 27afb09d Vitor Sessak
                goto fail;
323
324
        } else {
325
            AVFilterInOut *p, *src, *dst;
326
            for (p = inout->next;
327
                 p && strcmp(p->name,inout->name); p = p->next);
328
329 2839ff5e Vitor Sessak
            if(!p) {
330 27afb09d Vitor Sessak
                av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
331
                       inout->name);
332
                goto fail;
333
            }
334
335 2839ff5e Vitor Sessak
            if(p->type == LinkTypeIn && inout->type == LinkTypeOut) {
336 27afb09d Vitor Sessak
                src = inout;
337
                dst = p;
338 2839ff5e Vitor Sessak
            } else if(p->type == LinkTypeOut && inout->type == LinkTypeIn) {
339 27afb09d Vitor Sessak
                src = p;
340
                dst = inout;
341
            } else {
342
                av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
343
                       inout->name);
344
                goto fail;
345
            }
346
347 2839ff5e Vitor Sessak
            if(link_filter(src->filter, src->pad_idx, dst->filter, dst->pad_idx) < 0)
348 27afb09d Vitor Sessak
                goto fail;
349
350 37161d64 Vitor Sessak
            src->filter = NULL;
351
            dst->filter = NULL;
352 27afb09d Vitor Sessak
        }
353
    }
354
355
    free_inout(head);
356
357 2839ff5e Vitor Sessak
    if(!has_out) {
358 ee75692a Vitor Sessak
        if(link_filter(last_filt, pad, out, outpad))
359 27afb09d Vitor Sessak
            goto fail;
360
    }
361
362
    return 0;
363
364
 fail:
365
    free_inout(head);
366
    avfilter_destroy_graph(graph);
367
    return -1;
368
}