graphparser.c 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * filter graph parser
 * copyright (c) 2008 Vitor Sessak
 * copyright (c) 2007 Bobby Bingham
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <ctype.h>
#include <string.h>

26
#include "libavutil/avstring.h"
27
#include "graphparser.h"
28 29
#include "avfilter.h"
#include "avfiltergraph.h"
30
#include "parseutils.h"
31

32 33
#define WHITESPACES " \n\t"

34 35 36 37 38
/**
 * Link two filters together.
 *
 * @see avfilter_link()
 */
39
static int link_filter(AVFilterContext *src, int srcpad,
40 41
                       AVFilterContext *dst, int dstpad,
                       AVClass *log_ctx)
42
{
43 44
    int ret;
    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 52 53 54
    }

    return 0;
}

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

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

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

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

    return name;
82 83
}

Stefano Sabatini's avatar
Stefano Sabatini committed
84 85 86 87 88
/**
 * Create an instance of a filter, initialize and insert it in the
 * filtergraph in *ctx.
 *
 * @param ctx the filtergraph context
89
 * @param put here a filter context in case of successful creation and configuration, NULL otherwise.
Stefano Sabatini's avatar
Stefano Sabatini committed
90 91 92 93
 * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
 * @param filt_name the name of the filter to create
 * @param args the arguments provided to the filter during its initialization
 * @param log_ctx the log context to use
94
 * @return 0 in case of success, a negative AVERROR code otherwise
Stefano Sabatini's avatar
Stefano Sabatini committed
95
 */
96 97
static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
                         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

Stefano Sabatini's avatar
Stefano Sabatini committed
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 115
    ret = avfilter_open(filt_ctx, filt, inst_name);
    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 122 123
    if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
        avfilter_destroy(*filt_ctx);
        return ret;
Vitor Sessak's avatar
Vitor Sessak committed
124
    }
125

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

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 143
/**
 * Parse "filter=params"
 */
144 145
static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
                        int index, AVClass *log_ctx)
146
{
Vitor Sessak's avatar
Vitor Sessak committed
147
    char *opts = NULL;
148
    char *name = av_get_token(buf, "=,;[\n");
149
    int ret;
150

Stefano Sabatini's avatar
Stefano Sabatini committed
151
    if (**buf == '=') {
152
        (*buf)++;
153
        opts = av_get_token(buf, "[],;\n");
Vitor Sessak's avatar
Vitor Sessak committed
154
    }
155

156
    ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
Vitor Sessak's avatar
Vitor Sessak committed
157 158
    av_free(name);
    av_free(opts);
159
    return ret;
160 161
}

162 163
static void free_inout(AVFilterInOut *head)
{
Stefano Sabatini's avatar
Stefano Sabatini committed
164
    while (head) {
165
        AVFilterInOut *next = head->next;
Vitor Sessak's avatar
Vitor Sessak committed
166
        av_free(head->name);
167 168 169 170 171
        av_free(head);
        head = next;
    }
}

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

Stefano Sabatini's avatar
Stefano Sabatini committed
176
    while (*links && strcmp((*links)->name, label))
Vitor Sessak's avatar
Vitor Sessak committed
177
        links = &((*links)->next);
178

Vitor Sessak's avatar
Vitor Sessak committed
179
    ret = *links;
180

Stefano Sabatini's avatar
Stefano Sabatini committed
181
    if (ret)
Vitor Sessak's avatar
Vitor Sessak committed
182
        *links = ret->next;
183 184 185 186

    return ret;
}

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

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

Stefano Sabatini's avatar
Stefano Sabatini committed
199
    while (pad--) {
200
        AVFilterInOut *p = *curr_inputs;
Stefano Sabatini's avatar
Stefano Sabatini committed
201
        if (!p) {
202 203
            av_log(log_ctx, AV_LOG_ERROR,
                   "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;
Vitor Sessak's avatar
Vitor Sessak committed
209

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

Stefano Sabatini's avatar
Stefano Sabatini committed
222
    if (*curr_inputs) {
223 224
        av_log(log_ctx, AV_LOG_ERROR,
               "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;
Stefano Sabatini's avatar
Stefano Sabatini committed
230
    while (pad--) {
231
        AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
232 233
        if (!currlinkn)
            return AVERROR(ENOMEM);
234
        currlinkn->filter_ctx  = filt_ctx;
235
        currlinkn->pad_idx = pad;
236
        insert_inout(curr_inputs, currlinkn);
237 238 239 240 241
    }

    return 0;
}

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

Stefano Sabatini's avatar
Stefano Sabatini committed
247
    while (**buf == '[') {
248
        char *name = parse_link_name(buf, log_ctx);
Vitor Sessak's avatar
Vitor Sessak committed
249
        AVFilterInOut *match;
Vitor Sessak's avatar
Vitor Sessak committed
250

Stefano Sabatini's avatar
Stefano Sabatini committed
251
        if (!name)
252
            return AVERROR(EINVAL);
Vitor Sessak's avatar
Vitor Sessak committed
253

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

Stefano Sabatini's avatar
Stefano Sabatini committed
257
        if (match) {
Vitor Sessak's avatar
Vitor Sessak committed
258
            av_free(name);
259 260
        } else {
            /* Not in the list, so add it as an input */
261 262
            if (!(match = av_mallocz(sizeof(AVFilterInOut))))
                return AVERROR(ENOMEM);
Vitor Sessak's avatar
Vitor Sessak committed
263 264
            match->name    = name;
            match->pad_idx = pad;
265
        }
266

267
        insert_inout(curr_inputs, match);
268

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

273 274 275
    return pad;
}

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

Stefano Sabatini's avatar
Stefano Sabatini committed
282
    while (**buf == '[') {
283
        char *name = parse_link_name(buf, log_ctx);
284 285
        AVFilterInOut *match;

286 287
        AVFilterInOut *input = *curr_inputs;
        *curr_inputs = (*curr_inputs)->next;
Vitor Sessak's avatar
Vitor Sessak committed
288

Stefano Sabatini's avatar
Stefano Sabatini committed
289
        if (!name)
290
            return AVERROR(EINVAL);
291

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

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

312 313
    return pad;
}
314

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

322
    AVFilterInOut *curr_inputs = NULL;
323 324

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

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

331
        if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
Vitor Sessak's avatar
Vitor Sessak committed
332 333
            goto fail;

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

341
        if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
342
            goto fail;
Vitor Sessak's avatar
Vitor Sessak committed
343

344 345
        if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
                                 log_ctx)) < 0)
Vitor Sessak's avatar
Vitor Sessak committed
346 347
            goto fail;

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

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

361
    if (chr) {
362 363 364
        av_log(log_ctx, AV_LOG_ERROR,
               "Unable to parse graph description substring: \"%s\"\n",
               filters - 1);
365
        ret = AVERROR(EINVAL);
366 367 368
        goto fail;
    }

Stefano Sabatini's avatar
Stefano Sabatini committed
369
    if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
370
        /* Last output can be omitted if it is "[out]" */
371
        const char *tmp = "[out]";
372 373
        if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
                                 log_ctx)) < 0)
374 375 376 377 378 379
            goto fail;
    }

    return 0;

 fail:
380
    avfilter_graph_free(graph);
381 382 383
    free_inout(open_inputs);
    free_inout(open_outputs);
    free_inout(curr_inputs);
384
    return ret;
385
}