Statistics
| Branch: | Revision:

ffmpeg / libavfilter / graphparser.c @ 42e7f6d7

History | View | Annotate | Download (10.5 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 "libavutil/avstring.h"
27
#include "graphparser.h"
28
#include "avfilter.h"
29
#include "avfiltergraph.h"
30
#include "parseutils.h"
31

    
32
#define WHITESPACES " \n\t"
33

    
34
/**
35
 * Link two filters together.
36
 *
37
 * @see avfilter_link()
38
 */
39
static int link_filter(AVFilterContext *src, int srcpad,
40
                       AVFilterContext *dst, int dstpad,
41
                       AVClass *log_ctx)
42
{
43
    int ret;
44
    if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
45
        av_log(log_ctx, AV_LOG_ERROR,
46
               "Cannot create the link %s:%d -> %s:%d\n",
47
               src->filter->name, srcpad, dst->filter->name, dstpad);
48
        return ret;
49
    }
50

    
51
    return 0;
52
}
53

    
54
/**
55
 * Parse the name of a link, which has the format "[linkname]".
56
 *
57
 * @return a pointer (that need to be freed after use) to the name
58
 * between parenthesis
59
 */
60
static char *parse_link_name(const char **buf, AVClass *log_ctx)
61
{
62
    const char *start = *buf;
63
    char *name;
64
    (*buf)++;
65

    
66
    name = av_get_token(buf, "]");
67

    
68
    if (!name[0]) {
69
        av_log(log_ctx, AV_LOG_ERROR,
70
               "Bad (empty?) label found in the following: \"%s\".\n", start);
71
        goto fail;
72
    }
73

    
74
    if (*(*buf)++ != ']') {
75
        av_log(log_ctx, AV_LOG_ERROR,
76
               "Mismatched '[' found in the following: \"%s\".\n", start);
77
    fail:
78
        av_freep(&name);
79
    }
80

    
81
    return name;
82
}
83

    
84
/**
85
 * Create an instance of a filter, initialize and insert it in the
86
 * filtergraph in *ctx.
87
 *
88
 * @param ctx the filtergraph context
89
 * @param put here a filter context in case of successful creation and configuration, NULL otherwise.
90
 * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
91
 * @param filt_name the name of the filter to create
92
 * @param args the arguments provided to the filter during its initialization
93
 * @param log_ctx the log context to use
94
 * @return 0 in case of success, a negative AVERROR code otherwise
95
 */
96
static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
97
                         const char *filt_name, const char *args, AVClass *log_ctx)
98
{
99
    AVFilter *filt;
100
    char inst_name[30];
101
    char tmp_args[256];
102
    int ret;
103

    
104
    snprintf(inst_name, sizeof(inst_name), "Filter %d %s", index, filt_name);
105

    
106
    filt = avfilter_get_by_name(filt_name);
107

    
108
    if (!filt) {
109
        av_log(log_ctx, AV_LOG_ERROR,
110
               "No such filter: '%s'\n", filt_name);
111
        return AVERROR(EINVAL);
112
    }
113

    
114
    ret = avfilter_open(filt_ctx, filt, inst_name);
115
    if (!*filt_ctx) {
116
        av_log(log_ctx, AV_LOG_ERROR,
117
               "Error creating filter '%s'\n", filt_name);
118
        return ret;
119
    }
120

    
121
    if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
122
        avfilter_destroy(*filt_ctx);
123
        return ret;
124
    }
125

    
126
    if (!strcmp(filt_name, "scale") && !strstr(args, "flags")) {
127
        snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
128
                 args, ctx->scale_sws_opts);
129
        args = tmp_args;
130
    }
131

    
132
    if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
133
        av_log(log_ctx, AV_LOG_ERROR,
134
               "Error initializing filter '%s' with args '%s'\n", filt_name, args);
135
        return ret;
136
    }
137

    
138
    return 0;
139
}
140

    
141
/**
142
 * Parse "filter=params"
143
 */
144
static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
145
                        int index, AVClass *log_ctx)
146
{
147
    char *opts = NULL;
148
    char *name = av_get_token(buf, "=,;[\n");
149
    int ret;
150

    
151
    if (**buf == '=') {
152
        (*buf)++;
153
        opts = av_get_token(buf, "[],;\n");
154
    }
155

    
156
    ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
157
    av_free(name);
158
    av_free(opts);
159
    return ret;
160
}
161

    
162
static void free_inout(AVFilterInOut *head)
163
{
164
    while (head) {
165
        AVFilterInOut *next = head->next;
166
        av_free(head->name);
167
        av_free(head);
168
        head = next;
169
    }
170
}
171

    
172
static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
173
{
174
    AVFilterInOut *ret;
175

    
176
    while (*links && strcmp((*links)->name, label))
177
        links = &((*links)->next);
178

    
179
    ret = *links;
180

    
181
    if (ret)
182
        *links = ret->next;
183

    
184
    return ret;
185
}
186

    
187
static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
188
{
189
    element->next = *inouts;
190
    *inouts = element;
191
}
192

    
193
static int link_filter_inouts(AVFilterContext *filt_ctx,
194
                              AVFilterInOut **curr_inputs,
195
                              AVFilterInOut **open_inputs, AVClass *log_ctx)
196
{
197
    int pad = filt_ctx->input_count, ret;
198

    
199
    while (pad--) {
200
        AVFilterInOut *p = *curr_inputs;
201
        if (!p) {
202
            av_log(log_ctx, AV_LOG_ERROR,
203
                   "Not enough inputs specified for the \"%s\" filter.\n",
204
                   filt_ctx->filter->name);
205
            return AVERROR(EINVAL);
206
        }
207

    
208
        *curr_inputs = (*curr_inputs)->next;
209

    
210
        if (p->filter) {
211
            if ((ret = link_filter(p->filter, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
212
                return ret;
213
            av_free(p->name);
214
            av_free(p);
215
        } else {
216
            p->filter = filt_ctx;
217
            p->pad_idx = pad;
218
            insert_inout(open_inputs, p);
219
        }
220
    }
221

    
222
    if (*curr_inputs) {
223
        av_log(log_ctx, AV_LOG_ERROR,
224
               "Too many inputs specified for the \"%s\" filter.\n",
225
               filt_ctx->filter->name);
226
        return AVERROR(EINVAL);
227
    }
228

    
229
    pad = filt_ctx->output_count;
230
    while (pad--) {
231
        AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
232
        if (!currlinkn)
233
            return AVERROR(ENOMEM);
234
        currlinkn->filter  = filt_ctx;
235
        currlinkn->pad_idx = pad;
236
        insert_inout(curr_inputs, currlinkn);
237
    }
238

    
239
    return 0;
240
}
241

    
242
static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
243
                        AVFilterInOut **open_outputs, AVClass *log_ctx)
244
{
245
    int pad = 0;
246

    
247
    while (**buf == '[') {
248
        char *name = parse_link_name(buf, log_ctx);
249
        AVFilterInOut *match;
250

    
251
        if (!name)
252
            return AVERROR(EINVAL);
253

    
254
        /* First check if the label is not in the open_outputs list */
255
        match = extract_inout(name, open_outputs);
256

    
257
        if (match) {
258
            av_free(name);
259
        } else {
260
            /* Not in the list, so add it as an input */
261
            match = av_mallocz(sizeof(AVFilterInOut));
262
            match->name    = name;
263
            match->pad_idx = pad;
264
        }
265

    
266
        insert_inout(curr_inputs, match);
267

    
268
        *buf += strspn(*buf, WHITESPACES);
269
        pad++;
270
    }
271

    
272
    return pad;
273
}
274

    
275
static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
276
                         AVFilterInOut **open_inputs,
277
                         AVFilterInOut **open_outputs, AVClass *log_ctx)
278
{
279
    int pad = 0;
280

    
281
    while (**buf == '[') {
282
        char *name = parse_link_name(buf, log_ctx);
283
        AVFilterInOut *match;
284

    
285
        AVFilterInOut *input = *curr_inputs;
286
        *curr_inputs = (*curr_inputs)->next;
287

    
288
        if (!name)
289
            return -1;
290

    
291
        /* First check if the label is not in the open_inputs list */
292
        match = extract_inout(name, open_inputs);
293

    
294
        if (match) {
295
            if (link_filter(input->filter, input->pad_idx,
296
                            match->filter, match->pad_idx, log_ctx) < 0)
297
                return -1;
298
            av_free(match->name);
299
            av_free(name);
300
            av_free(match);
301
            av_free(input);
302
        } else {
303
            /* Not in the list, so add the first input as a open_output */
304
            input->name = name;
305
            insert_inout(open_outputs, input);
306
        }
307
        *buf += strspn(*buf, WHITESPACES);
308
        pad++;
309
    }
310

    
311
    return pad;
312
}
313

    
314
int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
315
                         AVFilterInOut *open_inputs,
316
                         AVFilterInOut *open_outputs, AVClass *log_ctx)
317
{
318
    int index = 0;
319
    char chr = 0;
320

    
321
    AVFilterInOut *curr_inputs = NULL;
322

    
323
    do {
324
        AVFilterContext *filter;
325
        filters += strspn(filters, WHITESPACES);
326

    
327
        if (parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
328
            goto fail;
329

    
330
        if (parse_filter(&filter, &filters, graph, index, log_ctx) < 0)
331
            goto fail;
332

    
333
        if (filter->input_count == 1 && !curr_inputs && !index) {
334
            /* First input can be omitted if it is "[in]" */
335
            const char *tmp = "[in]";
336
            if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
337
                goto fail;
338
        }
339

    
340
        if (link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
341
            goto fail;
342

    
343
        if (parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
344
                         log_ctx) < 0)
345
            goto fail;
346

    
347
        filters += strspn(filters, WHITESPACES);
348
        chr = *filters++;
349

    
350
        if (chr == ';' && curr_inputs) {
351
            av_log(log_ctx, AV_LOG_ERROR,
352
                   "Could not find a output to link when parsing \"%s\"\n",
353
                   filters - 1);
354
            goto fail;
355
        }
356
        index++;
357
    } while (chr == ',' || chr == ';');
358

    
359
    if (chr) {
360
        av_log(log_ctx, AV_LOG_ERROR,
361
               "Unable to parse graph description substring: \"%s\"\n",
362
               filters - 1);
363
        goto fail;
364
    }
365

    
366
    if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
367
        /* Last output can be omitted if it is "[out]" */
368
        const char *tmp = "[out]";
369
        if (parse_outputs(&tmp, &curr_inputs, &open_inputs,
370
                         &open_outputs, log_ctx) < 0)
371
            goto fail;
372
    }
373

    
374
    return 0;
375

    
376
 fail:
377
    avfilter_graph_free(graph);
378
    free_inout(open_inputs);
379
    free_inout(open_outputs);
380
    free_inout(curr_inputs);
381
    return -1;
382
}