graphparser.c 11.6 KB
Newer Older
1 2
/*
 * filter graph parser
3 4
 * Copyright (c) 2008 Vitor Sessak
 * Copyright (c) 2007 Bobby Bingham
5
 *
6
 * This file is part of Libav.
7
 *
8
 * Libav is free software; you can redistribute it and/or
9 10 11 12
 * 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.
 *
13
 * Libav is distributed in the hope that it will be useful,
14 15 16 17 18
 * 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
19
 * License along with Libav; if not, write to the Free Software
20 21 22 23 24 25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

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

26
#include "libavutil/avstring.h"
27 28 29
#include "avfilter.h"
#include "avfiltergraph.h"

30 31
#define WHITESPACES " \n\t"

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

    return 0;
}

/**
53 54 55 56
 * 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
57
 */
58
static char *parse_link_name(const char **buf, AVClass *log_ctx)
59
{
Vitor Sessak's avatar
Vitor Sessak committed
60
    const char *start = *buf;
61
    char *name;
62 63
    (*buf)++;

64
    name = av_get_token(buf, "]");
65

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

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

    return name;
80 81
}

Stefano Sabatini's avatar
Stefano Sabatini committed
82 83 84 85
/**
 * Create an instance of a filter, initialize and insert it in the
 * filtergraph in *ctx.
 *
86
 * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
Stefano Sabatini's avatar
Stefano Sabatini committed
87 88 89 90 91
 * @param ctx the filtergraph context
 * @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
92
 * @return 0 in case of success, a negative AVERROR code otherwise
Stefano Sabatini's avatar
Stefano Sabatini committed
93
 */
94 95
static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
                         const char *filt_name, const char *args, AVClass *log_ctx)
96
{
97
    AVFilter *filt;
98
    char inst_name[30];
99
    char tmp_args[256];
100
    int ret;
101

102
    snprintf(inst_name, sizeof(inst_name), "Parsed filter %d %s", index, filt_name);
103

104
    filt = avfilter_get_by_name(filt_name);
105

Stefano Sabatini's avatar
Stefano Sabatini committed
106
    if (!filt) {
107
        av_log(log_ctx, AV_LOG_ERROR,
108
               "No such filter: '%s'\n", filt_name);
109
        return AVERROR(EINVAL);
110 111
    }

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

119
    if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
120
        avfilter_free(*filt_ctx);
121
        return ret;
Vitor Sessak's avatar
Vitor Sessak committed
122
    }
123

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

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

136
    return 0;
137 138
}

139
/**
140 141 142 143 144 145 146 147 148 149 150 151
 * Parse a string of the form FILTER_NAME[=PARAMS], and create a
 * corresponding filter instance which is added to graph with
 * create_filter().
 *
 * @param filt_ctx put here a pointer to the created filter context on
 * success, NULL otherwise
 * @param buf pointer to the buffer to parse, *buf will be updated to
 * point to the char next after the parsed string
 * @param index an index which is assigned to the created filter
 * instance, and which is supposed to be unique for each filter
 * instance added to the filtergraph
 * @return 0 in case of success, a negative AVERROR code otherwise
152
 */
153 154
static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
                        int index, AVClass *log_ctx)
155
{
Vitor Sessak's avatar
Vitor Sessak committed
156
    char *opts = NULL;
157
    char *name = av_get_token(buf, "=,;[\n");
158
    int ret;
159

Stefano Sabatini's avatar
Stefano Sabatini committed
160
    if (**buf == '=') {
161
        (*buf)++;
162
        opts = av_get_token(buf, "[],;\n");
Vitor Sessak's avatar
Vitor Sessak committed
163
    }
164

165
    ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
Vitor Sessak's avatar
Vitor Sessak committed
166 167
    av_free(name);
    av_free(opts);
168
    return ret;
169 170
}

171 172
static void free_inout(AVFilterInOut *head)
{
Stefano Sabatini's avatar
Stefano Sabatini committed
173
    while (head) {
174
        AVFilterInOut *next = head->next;
Vitor Sessak's avatar
Vitor Sessak committed
175
        av_free(head->name);
176 177 178 179 180
        av_free(head);
        head = next;
    }
}

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

Stefano Sabatini's avatar
Stefano Sabatini committed
185
    while (*links && strcmp((*links)->name, label))
Vitor Sessak's avatar
Vitor Sessak committed
186
        links = &((*links)->next);
187

Vitor Sessak's avatar
Vitor Sessak committed
188
    ret = *links;
189

Stefano Sabatini's avatar
Stefano Sabatini committed
190
    if (ret)
Vitor Sessak's avatar
Vitor Sessak committed
191
        *links = ret->next;
192 193 194 195

    return ret;
}

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

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

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

217
        *curr_inputs = (*curr_inputs)->next;
Vitor Sessak's avatar
Vitor Sessak committed
218

219 220
        if (p->filter_ctx) {
            if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
221
                return ret;
Vitor Sessak's avatar
Vitor Sessak committed
222
            av_free(p->name);
223 224
            av_free(p);
        } else {
225
            p->filter_ctx = filt_ctx;
226
            p->pad_idx = pad;
227
            insert_inout(open_inputs, p);
228 229 230
        }
    }

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

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

    return 0;
}

251 252
static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
                        AVFilterInOut **open_outputs, AVClass *log_ctx)
253
{
254 255
    int pad = 0;

Stefano Sabatini's avatar
Stefano Sabatini committed
256
    while (**buf == '[') {
257
        char *name = parse_link_name(buf, log_ctx);
Vitor Sessak's avatar
Vitor Sessak committed
258
        AVFilterInOut *match;
Vitor Sessak's avatar
Vitor Sessak committed
259

Stefano Sabatini's avatar
Stefano Sabatini committed
260
        if (!name)
261
            return AVERROR(EINVAL);
Vitor Sessak's avatar
Vitor Sessak committed
262

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

Stefano Sabatini's avatar
Stefano Sabatini committed
266
        if (match) {
Vitor Sessak's avatar
Vitor Sessak committed
267
            av_free(name);
268 269
        } else {
            /* Not in the list, so add it as an input */
270 271
            if (!(match = av_mallocz(sizeof(AVFilterInOut))))
                return AVERROR(ENOMEM);
Vitor Sessak's avatar
Vitor Sessak committed
272 273
            match->name    = name;
            match->pad_idx = pad;
274
        }
275

276
        insert_inout(curr_inputs, match);
277

278
        *buf += strspn(*buf, WHITESPACES);
279
        pad++;
280
    }
281

282 283 284
    return pad;
}

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

Stefano Sabatini's avatar
Stefano Sabatini committed
291
    while (**buf == '[') {
292
        char *name = parse_link_name(buf, log_ctx);
293 294
        AVFilterInOut *match;

295
        AVFilterInOut *input = *curr_inputs;
296 297 298 299 300 301
        if (!input) {
            av_log(log_ctx, AV_LOG_ERROR,
                   "No output pad can be associated to link label '%s'.\n",
                   name);
            return AVERROR(EINVAL);
        }
302
        *curr_inputs = (*curr_inputs)->next;
Vitor Sessak's avatar
Vitor Sessak committed
303

Stefano Sabatini's avatar
Stefano Sabatini committed
304
        if (!name)
305
            return AVERROR(EINVAL);
306

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

Stefano Sabatini's avatar
Stefano Sabatini committed
310
        if (match) {
311 312
            if ((ret = link_filter(input->filter_ctx, input->pad_idx,
                                   match->filter_ctx, match->pad_idx, log_ctx)) < 0)
313
                return ret;
Vitor Sessak's avatar
Vitor Sessak committed
314 315
            av_free(match->name);
            av_free(name);
316
            av_free(match);
Vitor Sessak's avatar
Vitor Sessak committed
317
            av_free(input);
318
        } else {
319
            /* Not in the list, so add the first input as a open_output */
Vitor Sessak's avatar
Vitor Sessak committed
320
            input->name = name;
321
            insert_inout(open_outputs, input);
322
        }
323
        *buf += strspn(*buf, WHITESPACES);
324
        pad++;
325
    }
326

327 328
    return pad;
}
329

330
int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
331 332
                         AVFilterInOut *open_inputs,
                         AVFilterInOut *open_outputs, AVClass *log_ctx)
333
{
334
    int index = 0, ret;
335 336
    char chr = 0;

337
    AVFilterInOut *curr_inputs = NULL;
338 339

    do {
340
        AVFilterContext *filter;
341
        const char *filterchain = filters;
342
        filters += strspn(filters, WHITESPACES);
343

344
        if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
345 346
            goto fail;

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

Stefano Sabatini's avatar
Stefano Sabatini committed
350
        if (filter->input_count == 1 && !curr_inputs && !index) {
351
            /* First input can be omitted if it is "[in]" */
352
            const char *tmp = "[in]";
353
            if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
354 355 356
                goto fail;
        }

357
        if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
358
            goto fail;
Vitor Sessak's avatar
Vitor Sessak committed
359

360 361
        if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
                                 log_ctx)) < 0)
Vitor Sessak's avatar
Vitor Sessak committed
362 363
            goto fail;

364
        filters += strspn(filters, WHITESPACES);
365 366
        chr = *filters++;

Stefano Sabatini's avatar
Stefano Sabatini committed
367
        if (chr == ';' && curr_inputs) {
368
            av_log(log_ctx, AV_LOG_ERROR,
369 370
                   "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
                   filterchain);
371
            ret = AVERROR(EINVAL);
372
            goto fail;
373
        }
374
        index++;
Stefano Sabatini's avatar
Stefano Sabatini committed
375
    } while (chr == ',' || chr == ';');
376

377
    if (chr) {
378 379 380
        av_log(log_ctx, AV_LOG_ERROR,
               "Unable to parse graph description substring: \"%s\"\n",
               filters - 1);
381
        ret = AVERROR(EINVAL);
382 383 384
        goto fail;
    }

Stefano Sabatini's avatar
Stefano Sabatini committed
385
    if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
386
        /* Last output can be omitted if it is "[out]" */
387
        const char *tmp = "[out]";
388 389
        if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
                                 log_ctx)) < 0)
390 391 392 393 394 395
            goto fail;
    }

    return 0;

 fail:
396 397 398
    for (; graph->filter_count > 0; graph->filter_count--)
        avfilter_free(graph->filters[graph->filter_count - 1]);
    av_freep(&graph->filters);
399 400 401
    free_inout(open_inputs);
    free_inout(open_outputs);
    free_inout(curr_inputs);
402
    return ret;
403
}