graphparser.c 10.3 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 "graphparser.h"
27 28
#include "avfilter.h"
#include "avfiltergraph.h"
29
#include "parseutils.h"
30

31 32
#define WHITESPACES " \n\t"

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

    return 0;
}

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

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

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

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

    return name;
81 82
}

Stefano Sabatini's avatar
Stefano Sabatini committed
83 84 85 86 87 88 89 90 91 92 93
/**
 * Create an instance of a filter, initialize and insert it in the
 * filtergraph in *ctx.
 *
 * @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
 * @return a filter context in case of successful creation and configuration, NULL otherwise.
 */
94
static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
95
                                      const char *filt_name, const char *args,
96 97
                                      AVClass *log_ctx)
{
98
    AVFilterContext *filt_ctx;
99

100
    AVFilter *filt;
101
    char inst_name[30];
102
    char tmp_args[256];
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 112 113
        return NULL;
    }

114
    avfilter_open(&filt_ctx, filt, inst_name);
Stefano Sabatini's avatar
Stefano Sabatini committed
115
    if (!filt_ctx) {
116
        av_log(log_ctx, AV_LOG_ERROR,
117
               "Error creating filter '%s'\n", filt_name);
118 119 120
        return NULL;
    }

Stefano Sabatini's avatar
Stefano Sabatini committed
121
    if (avfilter_graph_add_filter(ctx, filt_ctx) < 0) {
122
        avfilter_destroy(filt_ctx);
123
        return NULL;
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;
    }

Stefano Sabatini's avatar
Stefano Sabatini committed
132
    if (avfilter_init_filter(filt_ctx, args, NULL)) {
133
        av_log(log_ctx, AV_LOG_ERROR,
134
               "Error initializing filter '%s' with args '%s'\n", filt_name, args);
135 136 137
        return NULL;
    }

138
    return filt_ctx;
139 140
}

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

Vitor Sessak's avatar
Vitor Sessak committed
156 157 158 159
    ret = create_filter(graph, index, name, opts, log_ctx);
    av_free(name);
    av_free(opts);
    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 *filter,
194 195
                              AVFilterInOut **curr_inputs,
                              AVFilterInOut **open_inputs, AVClass *log_ctx)
196
{
197
    int pad = filter->input_count;
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
                   filter->filter->name);
205 206 207
            return -1;
        }

208
        *curr_inputs = (*curr_inputs)->next;
Vitor Sessak's avatar
Vitor Sessak committed
209

Stefano Sabatini's avatar
Stefano Sabatini committed
210 211
        if (p->filter) {
            if (link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
212
                return -1;
Vitor Sessak's avatar
Vitor Sessak committed
213
            av_free(p->name);
214 215 216 217
            av_free(p);
        } else {
            p->filter = filter;
            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
               filter->filter->name);
226 227 228 229
        return -1;
    }

    pad = filter->output_count;
Stefano Sabatini's avatar
Stefano Sabatini committed
230
    while (pad--) {
231
        AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
232 233
        currlinkn->filter  = filter;
        currlinkn->pad_idx = pad;
234
        insert_inout(curr_inputs, currlinkn);
235 236 237 238 239
    }

    return 0;
}

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

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

Stefano Sabatini's avatar
Stefano Sabatini committed
249
        if (!name)
Vitor Sessak's avatar
Vitor Sessak committed
250 251
            return -1;

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

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

264
        insert_inout(curr_inputs, match);
265

266
        *buf += strspn(*buf, WHITESPACES);
267
        pad++;
268
    }
269

270 271 272
    return pad;
}

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

Stefano Sabatini's avatar
Stefano Sabatini committed
279
    while (**buf == '[') {
280
        char *name = parse_link_name(buf, log_ctx);
281 282
        AVFilterInOut *match;

283 284
        AVFilterInOut *input = *curr_inputs;
        *curr_inputs = (*curr_inputs)->next;
Vitor Sessak's avatar
Vitor Sessak committed
285

Stefano Sabatini's avatar
Stefano Sabatini committed
286
        if (!name)
287 288
            return -1;

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

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

309 310
    return pad;
}
311

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

319
    AVFilterInOut *curr_inputs = NULL;
320 321

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

Stefano Sabatini's avatar
Stefano Sabatini committed
325
        if (parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
326 327
            goto fail;

328 329
        filter = parse_filter(&filters, graph, index, log_ctx);

Stefano Sabatini's avatar
Stefano Sabatini committed
330
        if (!filter)
Vitor Sessak's avatar
Vitor Sessak committed
331 332
            goto fail;

Stefano Sabatini's avatar
Stefano Sabatini committed
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 338 339
                goto fail;
        }

Stefano Sabatini's avatar
Stefano Sabatini committed
340
        if (link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
341
            goto fail;
Vitor Sessak's avatar
Vitor Sessak committed
342

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

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

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

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

Stefano Sabatini's avatar
Stefano Sabatini committed
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]";
Stefano Sabatini's avatar
Stefano Sabatini committed
369
        if (parse_outputs(&tmp, &curr_inputs, &open_inputs,
370
                         &open_outputs, log_ctx) < 0)
371 372 373 374 375 376
            goto fail;
    }

    return 0;

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