Statistics
| Branch: | Revision:

ffmpeg / libavfilter / graphparser.c @ cf4f7d38

History | View | Annotate | Download (9.12 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
static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
30
                                      const char *name, const char *args,
31
                                      AVClass *log_ctx)
32
{
33
    AVFilterContext *filt;
34

    
35
    AVFilter *filterdef;
36
    char inst_name[30];
37

    
38
    snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
39

    
40
    if(!(filterdef = avfilter_get_by_name(name))) {
41
        av_log(&log_ctx, AV_LOG_ERROR,
42
               "no such filter: '%s'\n", name);
43
        return NULL;
44
    }
45

    
46
    if(!(filt = avfilter_open(filterdef, inst_name))) {
47
        av_log(log_ctx, AV_LOG_ERROR,
48
               "error creating filter '%s'\n", name);
49
        return NULL;
50
    }
51

    
52
    if(avfilter_graph_add_filter(ctx, filt) < 0)
53
        return NULL;
54

    
55
    if(avfilter_init_filter(filt, args, NULL)) {
56
        av_log(log_ctx, AV_LOG_ERROR,
57
               "error initializing filter '%s' with args '%s'\n", name, args);
58
        return NULL;
59
    }
60

    
61
    return filt;
62
}
63

    
64
static int link_filter(AVFilterContext *src, int srcpad,
65
                       AVFilterContext *dst, int dstpad,
66
                       AVClass *log_ctx)
67
{
68
    if(avfilter_link(src, srcpad, dst, dstpad)) {
69
        av_log(log_ctx, AV_LOG_ERROR,
70
               "cannot create the link %s:%d -> %s:%d\n",
71
               src->filter->name, srcpad, dst->filter->name, dstpad);
72
        return -1;
73
    }
74

    
75
    return 0;
76
}
77

    
78
static void consume_whitespace(const char **buf)
79
{
80
    *buf += strspn(*buf, " \n\t");
81
}
82

    
83
/**
84
 * Consumes a string from *buf.
85
 * @return a copy of the consumed string, which should be free'd after use
86
 */
87
static char *consume_string(const char **buf)
88
{
89
    char *out = av_malloc(strlen(*buf) + 1);
90
    char *ret = out;
91

    
92
    consume_whitespace(buf);
93

    
94
    do{
95
        char c = *(*buf)++;
96
        switch (c) {
97
        case '\\':
98
            *out++= *(*buf)++;
99
            break;
100
        case '\'':
101
            while(**buf && **buf != '\'')
102
                *out++= *(*buf)++;
103
            if(**buf) (*buf)++;
104
            break;
105
        case 0:
106
        case ']':
107
        case '[':
108
        case '=':
109
        case ',':
110
        case ' ':
111
        case '\n':
112
            *out++= 0;
113
            break;
114
        default:
115
            *out++= c;
116
        }
117
    } while(out[-1]);
118

    
119
    (*buf)--;
120
    consume_whitespace(buf);
121

    
122
    return ret;
123
}
124

    
125
/**
126
 * Parse "[linkname]"
127
 * @arg name a pointer (that need to be free'd after use) to the name between
128
 *           parenthesis
129
 */
130
static void parse_link_name(const char **buf, char **name, AVClass *log_ctx)
131
{
132
    const char *start = *buf;
133
    (*buf)++;
134

    
135
    *name = consume_string(buf);
136

    
137
    if(!*name[0]) {
138
        av_log(log_ctx, AV_LOG_ERROR,
139
               "Bad (empty?) label found in the following: \"%s\".\n", start);
140
        goto fail;
141
    }
142

    
143
    if(*(*buf)++ != ']') {
144
        av_log(log_ctx, AV_LOG_ERROR,
145
               "Mismatched '[' found in the following: \"%s\".\n", start);
146
    fail:
147
        av_freep(name);
148
    }
149
}
150

    
151
/**
152
 * Parse "filter=params"
153
 * @arg name a pointer (that need to be free'd after use) to the name of the
154
 *           filter
155
 * @arg ars  a pointer (that need to be free'd after use) to the args of the
156
 *           filter
157
 */
158
static AVFilterContext *parse_filter(const char **buf,
159
                                     AVFilterGraph *graph, int index,
160
                                     AVClass *log_ctx)
161
{
162
    char *name, *opts;
163
    name = consume_string(buf);
164

    
165
    if(**buf == '=') {
166
        (*buf)++;
167
        opts = consume_string(buf);
168
    } else {
169
        opts = NULL;
170
    }
171

    
172
    return create_filter(graph, index, name, opts, log_ctx);
173
}
174

    
175
enum LinkType {
176
    LinkTypeIn,
177
    LinkTypeOut,
178
};
179

    
180
/**
181
 * A linked-list of the inputs/outputs of the filter chain.
182
 */
183
typedef struct AVFilterInOut {
184
    enum LinkType type;
185
    char *name;
186
    AVFilterContext *filter;
187
    int pad_idx;
188

    
189
    struct AVFilterInOut *next;
190
} AVFilterInOut;
191

    
192
static void free_inout(AVFilterInOut *head)
193
{
194
    while (head) {
195
        AVFilterInOut *next = head->next;
196
        av_free(head);
197
        head = next;
198
    }
199
}
200

    
201
/**
202
 * Parse "[a1][link2] ... [etc]"
203
 */
204
static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
205
                        enum LinkType type, AVFilterContext *filter,
206
                        AVClass *log_ctx)
207
{
208
    while (**buf == '[') {
209
        char *name;
210
        AVFilterInOut *p = *inout;
211

    
212
        parse_link_name(buf, &name, log_ctx);
213

    
214
        if(!name)
215
            return -1;
216

    
217
        for (; p && strcmp(p->name, name); p = p->next);
218

    
219
        if(!p) {
220
            // First label apearence, add it to the linked list
221
            AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
222

    
223
            inoutn->name    = name;
224
            inoutn->type    = type;
225
            inoutn->filter  = filter;
226
            inoutn->pad_idx = pad;
227
            inoutn->next    = *inout;
228
            *inout = inoutn;
229
        } else {
230

    
231
            if(p->type == LinkTypeIn && type == LinkTypeOut) {
232
                if(link_filter(filter, pad, p->filter, p->pad_idx, log_ctx) < 0)
233
                    return -1;
234
            } else if(p->type == LinkTypeOut && type == LinkTypeIn) {
235
                if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx) < 0)
236
                    return -1;
237
            } else {
238
                av_log(log_ctx, AV_LOG_ERROR,
239
                       "Two links named '%s' are either both input or both output\n",
240
                       name);
241
                return -1;
242
            }
243

    
244
            p->filter = NULL;
245
        }
246

    
247
        pad++;
248
        consume_whitespace(buf);
249
    }
250

    
251
    return pad;
252
}
253

    
254
static const char *skip_inouts(const char *buf)
255
{
256
    while (*buf == '[') {
257
        buf += strcspn(buf, "]") + 1;
258
        consume_whitespace(&buf);
259
    }
260
    return buf;
261
}
262

    
263

    
264
/**
265
 * Parse a string describing a filter graph.
266
 */
267
int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
268
                         AVFilterContext *in, int inpad,
269
                         AVFilterContext *out, int outpad,
270
                         AVClass *log_ctx)
271
{
272
    AVFilterInOut *inout=NULL;
273
    AVFilterInOut  *head=NULL;
274

    
275
    int index = 0;
276
    char chr = 0;
277
    int pad = 0;
278
    int has_out = 0;
279

    
280
    AVFilterContext *last_filt = NULL;
281

    
282
    do {
283
        AVFilterContext *filter;
284
        int oldpad = pad;
285
        const char *inouts;
286

    
287
        consume_whitespace(&filters);
288
        inouts = filters;
289

    
290
        // We need to parse the inputs of the filter after we create it, so
291
        // skip it by now
292
        filters = skip_inouts(filters);
293

    
294
        if(!(filter = parse_filter(&filters, graph, index, log_ctx)))
295
            goto fail;
296

    
297
        pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter,
298
                           log_ctx);
299

    
300
        if(pad < 0)
301
            goto fail;
302

    
303
        // If the first filter has an input and none was given, it is
304
        // implicitly the input of the whole graph.
305
        if(pad == 0 && filter->input_count == 1) {
306
            if(link_filter(in, inpad, filter, 0, log_ctx))
307
                goto fail;
308
        }
309

    
310
        if(chr == ',') {
311
            if(link_filter(last_filt, oldpad, filter, 0, log_ctx) < 0)
312
                goto fail;
313
        }
314

    
315
        pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter, log_ctx);
316

    
317
        if (pad < 0)
318
            goto fail;
319

    
320
        consume_whitespace(&filters);
321

    
322
        chr = *filters++;
323
        index++;
324
        last_filt = filter;
325
    } while (chr == ',' || chr == ';');
326

    
327
    head = inout;
328
    // Process remaining labels. Only inputs and outputs should be left.
329
    for (; inout; inout = inout->next) {
330
        if(!inout->filter)
331
            continue; // Already processed
332

    
333
        if(!strcmp(inout->name, "in")) {
334
            if(link_filter(in, inpad, inout->filter, inout->pad_idx, log_ctx))
335
                goto fail;
336

    
337
        } else if(!strcmp(inout->name, "out")) {
338
            has_out = 1;
339

    
340
            if(link_filter(inout->filter, inout->pad_idx, out, outpad, log_ctx))
341
                goto fail;
342

    
343
        } else {
344
            av_log(log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
345
                   inout->name);
346
                goto fail;
347
        }
348
    }
349

    
350
    free_inout(head);
351

    
352
    if(!has_out) {
353
        if(link_filter(last_filt, pad, out, outpad, log_ctx))
354
            goto fail;
355
    }
356

    
357
    return 0;
358

    
359
 fail:
360
    free_inout(head);
361
    avfilter_destroy_graph(graph);
362
    return -1;
363
}