Statistics
| Branch: | Revision:

ffmpeg / libavfilter / graphparser.c @ 8095a014

History | View | Annotate | Download (8.86 KB)

1
/*
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
static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
45
                                      const char *name, const char *args)
46
{
47
    AVFilterContext *filt;
48

    
49
    AVFilter *filterdef;
50
    char inst_name[30];
51

    
52
    snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
53

    
54
    if(!(filterdef = avfilter_get_by_name(name))) {
55
        av_log(&log_ctx, AV_LOG_ERROR,
56
               "no such filter: '%s'\n", name);
57
        return NULL;
58
    }
59

    
60
    if(!(filt = avfilter_open(filterdef, inst_name))) {
61
        av_log(&log_ctx, AV_LOG_ERROR,
62
               "error creating filter '%s'\n", name);
63
        return NULL;
64
    }
65

    
66
    if(avfilter_graph_add_filter(ctx, filt) < 0)
67
        return NULL;
68

    
69
    if(avfilter_init_filter(filt, args, NULL)) {
70
        av_log(&log_ctx, AV_LOG_ERROR,
71
               "error initializing filter '%s' with args '%s'\n", name, args);
72
        return NULL;
73
    }
74

    
75
    return filt;
76
}
77

    
78
static int link_filter(AVFilterContext *src, int srcpad,
79
                       AVFilterContext *dst, int dstpad)
80
{
81
    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
        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
    char *out = av_malloc(strlen(*buf) + 1);
103
    char *ret = out;
104

    
105
    consume_whitespace(buf);
106

    
107
    do{
108
        char c = *(*buf)++;
109
        switch (c) {
110
        case '\\':
111
            *out++= *(*buf)++;
112
            break;
113
        case '\'':
114
            while(**buf && **buf != '\'')
115
                *out++= *(*buf)++;
116
            if(**buf) (*buf)++;
117
            break;
118
        case 0:
119
        case ']':
120
        case '[':
121
        case '=':
122
        case ',':
123
        case ' ':
124
        case '\n':
125
            *out++= 0;
126
            break;
127
        default:
128
            *out++= c;
129
        }
130
    } while(out[-1]);
131

    
132
    (*buf)--;
133
    consume_whitespace(buf);
134

    
135
    return ret;
136
}
137

    
138
/**
139
 * Parse "[linkname]"
140
 * @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
    const char *start = *buf;
146
    (*buf)++;
147

    
148
    *name = consume_string(buf);
149

    
150
    if(!*name[0]) {
151
        av_log(&log_ctx, AV_LOG_ERROR,
152
               "Bad (empty?) label found in the following: \"%s\".\n", start);
153
        goto fail;
154
    }
155

    
156
    if(*(*buf)++ != ']') {
157
        av_log(&log_ctx, AV_LOG_ERROR,
158
               "Mismatched '[' found in the following: \"%s\".\n", start);
159
        goto fail;
160
    }
161

    
162
    return;
163

    
164
 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
static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
176
{
177
    char *name, *opts;
178
    name = consume_string(buf);
179

    
180
    if(**buf == '=') {
181
        (*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
    AVFilterContext *filter;
202
    int pad_idx;
203

    
204
    struct AVFilterInOut *next;
205
} AVFilterInOut;
206

    
207
static void free_inout(AVFilterInOut *head)
208
{
209
    while (head) {
210
        AVFilterInOut *next = head->next;
211
        av_free(head);
212
        head = next;
213
    }
214
}
215

    
216
/**
217
 * Parse "[a1][link2] ... [etc]"
218
 */
219
static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
220
                        enum LinkType type, AVFilterContext *filter)
221
{
222
    while (**buf == '[') {
223
        AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
224
        parse_link_name(buf, &inoutn->name);
225

    
226
        if (!inoutn->name) {
227
            av_free(inoutn);
228
            return -1;
229
        }
230

    
231
        inoutn->type    = type;
232
        inoutn->filter  = filter;
233
        inoutn->pad_idx = pad++;
234
        inoutn->next    = *inout;
235
        *inout = inoutn;
236
        consume_whitespace(buf);
237
    }
238
    return pad;
239
}
240

    
241
static const char *skip_inouts(const char *buf)
242
{
243
    while (*buf == '[') {
244
        buf += strcspn(buf, "]") + 1;
245
        consume_whitespace(&buf);
246
    }
247
    return buf;
248
}
249

    
250

    
251
/**
252
 * Parse a string describing a filter graph.
253
 */
254
int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
255
                         AVFilterContext *in, int inpad,
256
                         AVFilterContext *out, int outpad)
257
{
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
    AVFilterContext *last_filt = NULL;
267

    
268
    do {
269
        AVFilterContext *filter;
270
        int oldpad = pad;
271
        const char *inouts;
272

    
273
        consume_whitespace(&filters);
274
        inouts = filters;
275

    
276
        // 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

    
280
        if(!(filter = parse_filter(&filters, graph, index)))
281
            goto fail;
282

    
283
        pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
284

    
285
        if(pad < 0)
286
            goto fail;
287

    
288
        // If the first filter has an input and none was given, it is
289
        // implicitly the input of the whole graph.
290
        if(pad == 0 && filter->input_count == 1) {
291
            if(link_filter(in, inpad, filter, 0))
292
                goto fail;
293
        }
294

    
295
        if(chr == ',') {
296
            if(link_filter(last_filt, oldpad, filter, 0) < 0)
297
                goto fail;
298
        }
299

    
300
        pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
301

    
302
        consume_whitespace(&filters);
303

    
304
        chr = *filters++;
305
        index++;
306
        last_filt = filter;
307
    } while (chr == ',' || chr == ';');
308

    
309
    head = inout;
310
    for (; inout != NULL; inout = inout->next) {
311
        if(inout->filter == NULL)
312
            continue; // Already processed
313

    
314
        if(!strcmp(inout->name, "in")) {
315
            if(link_filter(in, inpad, inout->filter, inout->pad_idx))
316
                goto fail;
317

    
318
        } else if(!strcmp(inout->name, "out")) {
319
            has_out = 1;
320

    
321
            if(link_filter(inout->filter, inout->pad_idx, out, outpad))
322
                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
            if(!p) {
330
                av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
331
                       inout->name);
332
                goto fail;
333
            }
334

    
335
            if(p->type == LinkTypeIn && inout->type == LinkTypeOut) {
336
                src = inout;
337
                dst = p;
338
            } else if(p->type == LinkTypeOut && inout->type == LinkTypeIn) {
339
                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
            if(link_filter(src->filter, src->pad_idx, dst->filter, dst->pad_idx) < 0)
348
                goto fail;
349

    
350
            src->filter = NULL;
351
            dst->filter = NULL;
352
        }
353
    }
354

    
355
    free_inout(head);
356

    
357
    if(!has_out) {
358
        if(link_filter(last_filt, pad, out, outpad))
359
            goto fail;
360
    }
361

    
362
    return 0;
363

    
364
 fail:
365
    free_inout(head);
366
    avfilter_destroy_graph(graph);
367
    return -1;
368
}