Statistics
| Branch: | Revision:

ffmpeg / libavfilter / graphparser.c @ 55b2a9ba

History | View | Annotate | Download (11.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 "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 a string of the form FILTER_NAME[=PARAMS], and create a
143
 * corresponding filter instance which is added to graph with
144
 * create_filter().
145
 *
146
 * @param filt_ctx put here a pointer to the created filter context on
147
 * success, NULL otherwise
148
 * @param buf pointer to the buffer to parse, *buf will be updated to
149
 * point to the char next after the parsed string
150
 * @param index an index which is assigned to the created filter
151
 * instance, and which is supposed to be unique for each filter
152
 * instance added to the filtergraph
153
 * @return 0 in case of success, a negative AVERROR code otherwise
154
 */
155
static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
156
                        int index, AVClass *log_ctx)
157
{
158
    char *opts = NULL;
159
    char *name = av_get_token(buf, "=,;[\n");
160
    int ret;
161

    
162
    if (**buf == '=') {
163
        (*buf)++;
164
        opts = av_get_token(buf, "[],;\n");
165
    }
166

    
167
    ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
168
    av_free(name);
169
    av_free(opts);
170
    return ret;
171
}
172

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

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

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

    
190
    ret = *links;
191

    
192
    if (ret)
193
        *links = ret->next;
194

    
195
    return ret;
196
}
197

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

    
204
static int link_filter_inouts(AVFilterContext *filt_ctx,
205
                              AVFilterInOut **curr_inputs,
206
                              AVFilterInOut **open_inputs, AVClass *log_ctx)
207
{
208
    int pad = filt_ctx->input_count, ret;
209

    
210
    while (pad--) {
211
        AVFilterInOut *p = *curr_inputs;
212
        if (!p) {
213
            av_log(log_ctx, AV_LOG_ERROR,
214
                   "Not enough inputs specified for the \"%s\" filter.\n",
215
                   filt_ctx->filter->name);
216
            return AVERROR(EINVAL);
217
        }
218

    
219
        *curr_inputs = (*curr_inputs)->next;
220

    
221
        if (p->filter_ctx) {
222
            if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
223
                return ret;
224
            av_free(p->name);
225
            av_free(p);
226
        } else {
227
            p->filter_ctx = filt_ctx;
228
            p->pad_idx = pad;
229
            insert_inout(open_inputs, p);
230
        }
231
    }
232

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

    
240
    pad = filt_ctx->output_count;
241
    while (pad--) {
242
        AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
243
        if (!currlinkn)
244
            return AVERROR(ENOMEM);
245
        currlinkn->filter_ctx  = filt_ctx;
246
        currlinkn->pad_idx = pad;
247
        insert_inout(curr_inputs, currlinkn);
248
    }
249

    
250
    return 0;
251
}
252

    
253
static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
254
                        AVFilterInOut **open_outputs, 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 AVERROR(EINVAL);
264

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

    
268
        if (match) {
269
            av_free(name);
270
        } else {
271
            /* Not in the list, so add it as an input */
272
            if (!(match = av_mallocz(sizeof(AVFilterInOut))))
273
                return AVERROR(ENOMEM);
274
            match->name    = name;
275
            match->pad_idx = pad;
276
        }
277

    
278
        insert_inout(curr_inputs, match);
279

    
280
        *buf += strspn(*buf, WHITESPACES);
281
        pad++;
282
    }
283

    
284
    return pad;
285
}
286

    
287
static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
288
                         AVFilterInOut **open_inputs,
289
                         AVFilterInOut **open_outputs, AVClass *log_ctx)
290
{
291
    int ret, pad = 0;
292

    
293
    while (**buf == '[') {
294
        char *name = parse_link_name(buf, log_ctx);
295
        AVFilterInOut *match;
296

    
297
        AVFilterInOut *input = *curr_inputs;
298
        *curr_inputs = (*curr_inputs)->next;
299

    
300
        if (!name)
301
            return AVERROR(EINVAL);
302

    
303
        /* First check if the label is not in the open_inputs list */
304
        match = extract_inout(name, open_inputs);
305

    
306
        if (match) {
307
            if ((ret = link_filter(input->filter_ctx, input->pad_idx,
308
                                   match->filter_ctx, match->pad_idx, log_ctx)) < 0)
309
                return ret;
310
            av_free(match->name);
311
            av_free(name);
312
            av_free(match);
313
            av_free(input);
314
        } else {
315
            /* Not in the list, so add the first input as a open_output */
316
            input->name = name;
317
            insert_inout(open_outputs, input);
318
        }
319
        *buf += strspn(*buf, WHITESPACES);
320
        pad++;
321
    }
322

    
323
    return pad;
324
}
325

    
326
int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
327
                         AVFilterInOut *open_inputs,
328
                         AVFilterInOut *open_outputs, AVClass *log_ctx)
329
{
330
    int index = 0, ret;
331
    char chr = 0;
332

    
333
    AVFilterInOut *curr_inputs = NULL;
334

    
335
    do {
336
        AVFilterContext *filter;
337
        filters += strspn(filters, WHITESPACES);
338

    
339
        if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
340
            goto fail;
341

    
342
        if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
343
            goto fail;
344

    
345
        if (filter->input_count == 1 && !curr_inputs && !index) {
346
            /* First input can be omitted if it is "[in]" */
347
            const char *tmp = "[in]";
348
            if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
349
                goto fail;
350
        }
351

    
352
        if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
353
            goto fail;
354

    
355
        if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
356
                                 log_ctx)) < 0)
357
            goto fail;
358

    
359
        filters += strspn(filters, WHITESPACES);
360
        chr = *filters++;
361

    
362
        if (chr == ';' && curr_inputs) {
363
            av_log(log_ctx, AV_LOG_ERROR,
364
                   "Could not find a output to link when parsing \"%s\"\n",
365
                   filters - 1);
366
            ret = AVERROR(EINVAL);
367
            goto fail;
368
        }
369
        index++;
370
    } while (chr == ',' || chr == ';');
371

    
372
    if (chr) {
373
        av_log(log_ctx, AV_LOG_ERROR,
374
               "Unable to parse graph description substring: \"%s\"\n",
375
               filters - 1);
376
        ret = AVERROR(EINVAL);
377
        goto fail;
378
    }
379

    
380
    if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
381
        /* Last output can be omitted if it is "[out]" */
382
        const char *tmp = "[out]";
383
        if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
384
                                 log_ctx)) < 0)
385
            goto fail;
386
    }
387

    
388
    return 0;
389

    
390
 fail:
391
    avfilter_graph_free(graph);
392
    free_inout(open_inputs);
393
    free_inout(open_outputs);
394
    free_inout(curr_inputs);
395
    return ret;
396
}