Statistics
| Branch: | Revision:

ffmpeg / libavfilter / graphparser.c @ 2839ff5e

History | View | Annotate | Download (8.37 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
                                      char *name, 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
    const char *in = *buf;
104
    char *ret = out;
105 27afb09d Vitor Sessak
106
    consume_whitespace(buf);
107
108 93b7a0f6 Vitor Sessak
    do{
109
        char c = *in++;
110
        switch (c) {
111
        case '\\':
112
            *out++= *in++;
113 27afb09d Vitor Sessak
            break;
114 93b7a0f6 Vitor Sessak
        case '\'':
115
            while(*in && *in != '\'')
116
                *out++= *in++;
117
            if(*in) in++;
118
            break;
119
        case 0:
120 d7ff2297 Vitor Sessak
        case ']':
121
        case '[':
122 93b7a0f6 Vitor Sessak
        case '=':
123
        case ',':
124
            *out++= 0;
125
            break;
126
        default:
127
            *out++= c;
128
        }
129
    } while(out[-1]);
130 27afb09d Vitor Sessak
131 93b7a0f6 Vitor Sessak
    *buf = in-1;
132 27afb09d Vitor Sessak
    return ret;
133
}
134
135
/**
136 ffac8784 Vitor Sessak
 * Parse "[linkname]"
137 27afb09d Vitor Sessak
 * @arg name a pointer (that need to be free'd after use) to the name between
138
 *           parenthesis
139
 */
140
static void parse_link_name(const char **buf, char **name)
141
{
142
    (*buf)++;
143
144
    *name = consume_string(buf);
145
146 2839ff5e Vitor Sessak
    if(!*name[0])
147 27afb09d Vitor Sessak
        goto fail;
148
149 2839ff5e Vitor Sessak
    if(*(*buf)++ != ']')
150 27afb09d Vitor Sessak
        goto fail;
151
152
    return;
153
 fail:
154
    av_freep(name);
155
    av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
156
}
157
158
/**
159
 * Parse "filter=params"
160
 * @arg name a pointer (that need to be free'd after use) to the name of the
161
 *           filter
162
 * @arg ars  a pointer (that need to be free'd after use) to the args of the
163
 *           filter
164
 */
165 9710beaf Vitor Sessak
static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
166 27afb09d Vitor Sessak
{
167
    char *name, *opts;
168
    name = consume_string(buf);
169
170 2839ff5e Vitor Sessak
    if(**buf == '=') {
171 27afb09d Vitor Sessak
        (*buf)++;
172
        opts = consume_string(buf);
173
    } else {
174
        opts = NULL;
175
    }
176
177
    return create_filter(graph, index, name, opts);
178
}
179
180
enum LinkType {
181
    LinkTypeIn,
182
    LinkTypeOut,
183
};
184
185
/**
186
 * A linked-list of the inputs/outputs of the filter chain.
187
 */
188
typedef struct AVFilterInOut {
189
    enum LinkType type;
190
    char *name;
191 37161d64 Vitor Sessak
    AVFilterContext *filter;
192 27afb09d Vitor Sessak
    int pad_idx;
193
194
    struct AVFilterInOut *next;
195
} AVFilterInOut;
196
197
static void free_inout(AVFilterInOut *head)
198
{
199
    while (head) {
200
        AVFilterInOut *next;
201
        next = head->next;
202
        av_free(head);
203
        head = next;
204
    }
205
}
206
207
/**
208 ffac8784 Vitor Sessak
 * Parse "[a1][link2] ... [etc]"
209 27afb09d Vitor Sessak
 */
210
static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
211 37161d64 Vitor Sessak
                        enum LinkType type, AVFilterContext *filter)
212 27afb09d Vitor Sessak
{
213
    int pad = firstpad;
214 d7ff2297 Vitor Sessak
    while (**buf == '[') {
215 27afb09d Vitor Sessak
        AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
216
        parse_link_name(buf, &inoutn->name);
217
        inoutn->type = type;
218 37161d64 Vitor Sessak
        inoutn->filter = filter;
219 27afb09d Vitor Sessak
        inoutn->pad_idx = pad++;
220
        inoutn->next = *inout;
221
        *inout = inoutn;
222
    }
223
    return pad;
224
}
225
226 9710beaf Vitor Sessak
static const char *skip_inouts(const char *buf)
227
{
228 d7ff2297 Vitor Sessak
    while (*buf == '[') {
229
        buf += strcspn(buf, "]");
230 9710beaf Vitor Sessak
        buf++;
231
    }
232
    return buf;
233
}
234
235
236 27afb09d Vitor Sessak
/**
237
 * Parse a string describing a filter graph.
238
 */
239 92973a04 Vitor Sessak
int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
240
                         AVFilterContext *in, int inpad,
241
                         AVFilterContext *out, int outpad)
242 27afb09d Vitor Sessak
{
243
    AVFilterInOut *inout=NULL;
244
    AVFilterInOut  *head=NULL;
245
246
    int index = 0;
247
    char chr = 0;
248
    int pad = 0;
249
    int has_out = 0;
250
251 9710beaf Vitor Sessak
    AVFilterContext *last_filt = NULL;
252 27afb09d Vitor Sessak
253
    consume_whitespace(&filters);
254
255
    do {
256 9710beaf Vitor Sessak
        AVFilterContext *filter;
257 27afb09d Vitor Sessak
        int oldpad = pad;
258 9710beaf Vitor Sessak
        const char *inouts = filters;
259 27afb09d Vitor Sessak
260 9710beaf Vitor Sessak
        // We need to parse the inputs of the filter after we create it, so
261
        // skip it by now
262
        filters = skip_inouts(filters);
263 27afb09d Vitor Sessak
264 2839ff5e Vitor Sessak
        if(!(filter = parse_filter(&filters, graph, index)))
265 27afb09d Vitor Sessak
            goto fail;
266
267 9710beaf Vitor Sessak
        pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
268
269 27afb09d Vitor Sessak
        // If the first filter has an input and none was given, it is
270
        // implicitly the input of the whole graph.
271 2839ff5e Vitor Sessak
        if(pad == 0 && filter->input_count == 1) {
272 ee75692a Vitor Sessak
            if(link_filter(in, inpad, filter, 0))
273 27afb09d Vitor Sessak
                goto fail;
274
        }
275
276
        if(chr == ',') {
277 2839ff5e Vitor Sessak
            if(link_filter(last_filt, oldpad, filter, 0) < 0)
278 27afb09d Vitor Sessak
                goto fail;
279
        }
280 da790674 Vitor Sessak
281 9710beaf Vitor Sessak
        pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
282 27afb09d Vitor Sessak
        chr = *filters++;
283
        index++;
284 9710beaf Vitor Sessak
        last_filt = filter;
285 27afb09d Vitor Sessak
    } while (chr == ',' || chr == ';');
286
287
    head = inout;
288
    for (; inout != NULL; inout = inout->next) {
289 2839ff5e Vitor Sessak
        if(inout->filter == NULL)
290 27afb09d Vitor Sessak
            continue; // Already processed
291
292 2839ff5e Vitor Sessak
        if(!strcmp(inout->name, "in")) {
293 37161d64 Vitor Sessak
            if(link_filter(in, inpad, inout->filter, inout->pad_idx))
294 27afb09d Vitor Sessak
                goto fail;
295 ee75692a Vitor Sessak
296 2839ff5e Vitor Sessak
        } else if(!strcmp(inout->name, "out")) {
297 27afb09d Vitor Sessak
            has_out = 1;
298
299 37161d64 Vitor Sessak
            if(link_filter(inout->filter, inout->pad_idx, out, outpad))
300 27afb09d Vitor Sessak
                goto fail;
301
302
        } else {
303
            AVFilterInOut *p, *src, *dst;
304
            for (p = inout->next;
305
                 p && strcmp(p->name,inout->name); p = p->next);
306
307 2839ff5e Vitor Sessak
            if(!p) {
308 27afb09d Vitor Sessak
                av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
309
                       inout->name);
310
                goto fail;
311
            }
312
313 2839ff5e Vitor Sessak
            if(p->type == LinkTypeIn && inout->type == LinkTypeOut) {
314 27afb09d Vitor Sessak
                src = inout;
315
                dst = p;
316 2839ff5e Vitor Sessak
            } else if(p->type == LinkTypeOut && inout->type == LinkTypeIn) {
317 27afb09d Vitor Sessak
                src = p;
318
                dst = inout;
319
            } else {
320
                av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
321
                       inout->name);
322
                goto fail;
323
            }
324
325 2839ff5e Vitor Sessak
            if(link_filter(src->filter, src->pad_idx, dst->filter, dst->pad_idx) < 0)
326 27afb09d Vitor Sessak
                goto fail;
327
328 37161d64 Vitor Sessak
            src->filter = NULL;
329
            dst->filter = NULL;
330 27afb09d Vitor Sessak
        }
331
    }
332
333
    free_inout(head);
334
335 2839ff5e Vitor Sessak
    if(!has_out) {
336 ee75692a Vitor Sessak
        if(link_filter(last_filt, pad, out, outpad))
337 27afb09d Vitor Sessak
            goto fail;
338
    }
339
340
    return 0;
341
342
 fail:
343
    free_inout(head);
344
    avfilter_destroy_graph(graph);
345
    return -1;
346
}