Statistics
| Branch: | Revision:

ffmpeg / libavfilter / graphparser.c @ c185fc5d

History | View | Annotate | Download (10.3 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 "graphparser.h"
27
#include "avfilter.h"
28
#include "avfiltergraph.h"
29

    
30
static int link_filter(AVFilterContext *src, int srcpad,
31
                       AVFilterContext *dst, int dstpad,
32
                       AVClass *log_ctx)
33
{
34
    if(avfilter_link(src, srcpad, dst, dstpad)) {
35
        av_log(log_ctx, AV_LOG_ERROR,
36
               "cannot create the link %s:%d -> %s:%d\n",
37
               src->filter->name, srcpad, dst->filter->name, dstpad);
38
        return -1;
39
    }
40

    
41
    return 0;
42
}
43

    
44
static int consume_whitespace(const char *buf)
45
{
46
    return strspn(buf, " \n\t");
47
}
48

    
49
/**
50
 * Consumes a string from *buf.
51
 * @return a copy of the consumed string, which should be free'd after use
52
 */
53
static char *consume_string(const char **buf)
54
{
55
    char *out = av_malloc(strlen(*buf) + 1);
56
    char *ret = out;
57

    
58
    *buf += consume_whitespace(*buf);
59

    
60
    do{
61
        char c = *(*buf)++;
62
        switch (c) {
63
        case '\\':
64
            *out++ = *(*buf)++;
65
            break;
66
        case '\'':
67
            while(**buf && **buf != '\'')
68
                *out++ = *(*buf)++;
69
            if(**buf) (*buf)++;
70
            break;
71
        case 0:
72
        case ']':
73
        case '[':
74
        case '=':
75
        case ',':
76
        case ';':
77
        case ' ':
78
        case '\n':
79
            *out++ = 0;
80
            break;
81
        default:
82
            *out++ = c;
83
        }
84
    } while(out[-1]);
85

    
86
    (*buf)--;
87
    *buf += consume_whitespace(*buf);
88

    
89
    return ret;
90
}
91

    
92
/**
93
 * Parse "[linkname]"
94
 * @param name a pointer (that need to be free'd after use) to the name between
95
 *        parenthesis
96
 */
97
static char *parse_link_name(const char **buf, AVClass *log_ctx)
98
{
99
    const char *start = *buf;
100
    char *name;
101
    (*buf)++;
102

    
103
    name = consume_string(buf);
104

    
105
    if(!name[0]) {
106
        av_log(log_ctx, AV_LOG_ERROR,
107
               "Bad (empty?) label found in the following: \"%s\".\n", start);
108
        goto fail;
109
    }
110

    
111
    if(*(*buf)++ != ']') {
112
        av_log(log_ctx, AV_LOG_ERROR,
113
               "Mismatched '[' found in the following: \"%s\".\n", start);
114
    fail:
115
        av_freep(&name);
116
    }
117

    
118
    return name;
119
}
120

    
121
static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
122
                                      const char *name, const char *args,
123
                                      AVClass *log_ctx)
124
{
125
    AVFilterContext *filt;
126

    
127
    AVFilter *filterdef;
128
    char inst_name[30];
129

    
130
    snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
131

    
132
    filterdef = avfilter_get_by_name(name);
133

    
134
    if(!filterdef) {
135
        av_log(log_ctx, AV_LOG_ERROR,
136
               "no such filter: '%s'\n", name);
137
        return NULL;
138
    }
139

    
140
    filt = avfilter_open(filterdef, inst_name);
141
    if(!filt) {
142
        av_log(log_ctx, AV_LOG_ERROR,
143
               "error creating filter '%s'\n", name);
144
        return NULL;
145
    }
146

    
147
    if(avfilter_graph_add_filter(ctx, filt) < 0)
148
        return NULL;
149

    
150
    if(avfilter_init_filter(filt, args, NULL)) {
151
        av_log(log_ctx, AV_LOG_ERROR,
152
               "error initializing filter '%s' with args '%s'\n", name, args);
153
        return NULL;
154
    }
155

    
156
    return filt;
157
}
158

    
159
/**
160
 * Parse "filter=params"
161
 */
162
static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
163
                                     int index, AVClass *log_ctx)
164
{
165
    char *opts = NULL;
166
    char *name = consume_string(buf);
167

    
168
    if(**buf == '=') {
169
        (*buf)++;
170
        opts = consume_string(buf);
171
    }
172

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

    
176
static void free_inout(AVFilterInOut *head)
177
{
178
    while(head) {
179
        AVFilterInOut *next = head->next;
180
        av_free(head);
181
        head = next;
182
    }
183
}
184

    
185
static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
186
{
187
    AVFilterInOut *ret;
188

    
189
    while(*links && strcmp((*links)->name, label))
190
        links = &((*links)->next);
191

    
192
    ret = *links;
193

    
194
    if(ret)
195
        *links = ret->next;
196

    
197
    return ret;
198
}
199

    
200
static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
201
{
202
    element->next = *inouts;
203
    *inouts = element;
204
}
205

    
206
static int link_filter_inouts(AVFilterContext *filter,
207
                              AVFilterInOut **currInputs,
208
                              AVFilterInOut **openLinks, AVClass *log_ctx)
209
{
210
    int pad = 0;
211

    
212
    pad = filter->input_count;
213
    while(pad--) {
214
        AVFilterInOut *p = *currInputs;
215
        *currInputs = (*currInputs)->next;
216
        if(!p) {
217
            av_log(log_ctx, AV_LOG_ERROR,
218
                   "Not enough inputs specified for the \"%s\" filter.\n",
219
                   filter->filter->name);
220
            return -1;
221
        }
222

    
223
        if(p->filter) {
224
            if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
225
                return -1;
226
            av_free(p);
227
        } else {
228
            p->filter = filter;
229
            p->pad_idx = pad;
230
            insert_inout(openInputs, p);
231
        }
232
    }
233

    
234
    if(*currInputs) {
235
        av_log(log_ctx, AV_LOG_ERROR,
236
               "Too many inputs specified for the \"%s\" filter.\n",
237
               filter->filter->name);
238
        return -1;
239
    }
240

    
241
    pad = filter->output_count;
242
    while(pad--) {
243
        AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
244
        currlinkn->type    = LinkTypeOut;
245
        currlinkn->filter  = filter;
246
        currlinkn->pad_idx = pad;
247
        insert_inout(currInputs, currlinkn);
248
    }
249

    
250
    return 0;
251
}
252

    
253
static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
254
                        AVFilterInOut **openLinks, AVClass *log_ctx)
255
{
256
    int pad = 0;
257

    
258
    while(**buf == '[') {
259
        char *name = parse_link_name(buf, log_ctx);
260
        AVFilterInOut *match;
261

    
262
        if(!name)
263
            return -1;
264

    
265
        /* First check if the label is not in the openLinks list */
266
        match = extract_inout(name, openLinks);
267

    
268
        if(match) {
269
            /* A label of a open link. Make it one of the inputs of the next
270
               filter */
271
            if(match->type != LinkTypeOut) {
272
                av_log(log_ctx, AV_LOG_ERROR,
273
                       "Label \"%s\" appears twice as input!\n", match->name);
274
                return -1;
275
            }
276
        } else {
277
            /* Not in the list, so add it as an input */
278
            match = av_mallocz(sizeof(AVFilterInOut));
279
            match->name    = name;
280
            match->type    = LinkTypeIn;
281
            match->pad_idx = pad;
282
        }
283

    
284
        insert_inout(currInputs, match);
285

    
286
        *buf += consume_whitespace(*buf);
287
        pad++;
288
    }
289

    
290
    return pad;
291
}
292

    
293
static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
294
                         AVFilterInOut **openLinks, AVClass *log_ctx)
295
{
296
    int pad = 0;
297

    
298
    while(**buf == '[') {
299
        char *name = parse_link_name(buf, log_ctx);
300
        AVFilterInOut *match;
301

    
302
        AVFilterInOut *input = *currInputs;
303
        *currInputs = (*currInputs)->next;
304

    
305
        if(!name)
306
            return -1;
307

    
308
        /* First check if the label is not in the openLinks list */
309
        match = extract_inout(name, openLinks);
310

    
311
        if(match) {
312
            /* A label of a open link. Link it. */
313
            if(match->type != LinkTypeIn) {
314
                av_log(log_ctx, AV_LOG_ERROR,
315
                       "Label \"%s\" appears twice as output!\n", match->name);
316
                return -1;
317
            }
318

    
319
            if(link_filter(input->filter, input->pad_idx,
320
                           match->filter, match->pad_idx, log_ctx) < 0)
321
                return -1;
322
            av_free(match);
323
            av_free(input);
324
        } else {
325
            /* Not in the list, so add the first input as a openLink */
326
            input->next = *openLinks;
327
            input->type = LinkTypeOut;
328
            input->name = name;
329
            insert_inout(openOutputs, input);
330
        }
331
        *buf += consume_whitespace(*buf);
332
        pad++;
333
    }
334

    
335
    return pad;
336
}
337

    
338
int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
339
                         AVFilterInOut *openLinks, AVClass *log_ctx)
340
{
341
    int index = 0;
342
    char chr = 0;
343

    
344
    AVFilterInOut *currInputs = NULL;
345

    
346
    do {
347
        AVFilterContext *filter;
348
        filters += consume_whitespace(filters);
349

    
350
        if(parse_inputs(&filters, &currInputs, &openLinks, log_ctx) < 0)
351
            goto fail;
352

    
353
        filter = parse_filter(&filters, graph, index, log_ctx);
354

    
355
        if(!filter)
356
            goto fail;
357

    
358
        if(filter->input_count == 1 && !currInputs && !index) {
359
            /* First input can be ommitted if it is "[in]" */
360
            const char *tmp = "[in]";
361
            if(parse_inputs(&tmp, &currInputs, &openLinks, log_ctx))
362
                goto fail;
363
        }
364

    
365
        if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
366
            goto fail;
367

    
368
        if(parse_outputs(&filters, &currInputs, &openLinks, log_ctx))
369
            goto fail;
370

    
371
        filters += consume_whitespace(filters);
372
        chr = *filters++;
373

    
374
        if(chr == ';' && currInputs) {
375
            av_log(log_ctx, AV_LOG_ERROR,
376
                   "Could not find a output to link when parsing \"%s\"\n",
377
                   filters - 1);
378
            goto fail;
379
        }
380
        index++;
381
    } while(chr == ',' || chr == ';');
382

    
383
    if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
384
        /* Last output can be ommitted if it is "[out]" */
385
        const char *tmp = "[out]";
386
        if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
387
            goto fail;
388
    }
389

    
390
    return 0;
391

    
392
 fail:
393
    avfilter_destroy_graph(graph);
394
    free_inout(openLinks);
395
    free_inout(currInputs);
396
    return -1;
397
}