graphparser.c 10.5 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 29
#include "avfilter.h"
#include "avfiltergraph.h"

30
static int link_filter(AVFilterContext *src, int srcpad,
31 32
                       AVFilterContext *dst, int dstpad,
                       AVClass *log_ctx)
33
{
34
    if(avfilter_link(src, srcpad, dst, dstpad)) {
35
        av_log(log_ctx, AV_LOG_ERROR,
36 37
               "cannot create the link %s:%d -> %s:%d\n",
               src->filter->name, srcpad, dst->filter->name, dstpad);
38 39 40 41 42 43
        return -1;
    }

    return 0;
}

44
static int consume_whitespace(const char *buf)
45
{
46
    return strspn(buf, " \n\t");
47 48 49 50 51 52 53 54
}

/**
 * Consumes a string from *buf.
 * @return a copy of the consumed string, which should be free'd after use
 */
static char *consume_string(const char **buf)
{
55
    char *out = av_malloc(strlen(*buf) + 1);
56
    char *ret = out;
57

58
    *buf += consume_whitespace(*buf);
59

60
    do{
Vitor Sessak's avatar
Vitor Sessak committed
61
        char c = *(*buf)++;
62 63
        switch (c) {
        case '\\':
64
            *out++ = *(*buf)++;
65
            break;
66
        case '\'':
Vitor Sessak's avatar
Vitor Sessak committed
67
            while(**buf && **buf != '\'')
68
                *out++ = *(*buf)++;
Vitor Sessak's avatar
Vitor Sessak committed
69
            if(**buf) (*buf)++;
70 71
            break;
        case 0:
72 73
        case ']':
        case '[':
74 75
        case '=':
        case ',':
76
        case ';':
77 78
        case ' ':
        case '\n':
79
            *out++ = 0;
80 81
            break;
        default:
82
            *out++ = c;
83 84
        }
    } while(out[-1]);
85

Vitor Sessak's avatar
Vitor Sessak committed
86
    (*buf)--;
87
    *buf += consume_whitespace(*buf);
88

89 90 91 92
    return ret;
}

/**
Vitor Sessak's avatar
Vitor Sessak committed
93
 * Parse "[linkname]"
Vitor Sessak's avatar
Vitor Sessak committed
94 95
 * @param name a pointer (that need to be free'd after use) to the name between
 *        parenthesis
96
 */
97
static char *parse_link_name(const char **buf, AVClass *log_ctx)
98
{
Vitor Sessak's avatar
Vitor Sessak committed
99
    const char *start = *buf;
100
    char *name;
101 102
    (*buf)++;

103
    name = consume_string(buf);
104

105
    if(!name[0]) {
106
        av_log(log_ctx, AV_LOG_ERROR,
Vitor Sessak's avatar
Vitor Sessak committed
107
               "Bad (empty?) label found in the following: \"%s\".\n", start);
108
        goto fail;
Vitor Sessak's avatar
Vitor Sessak committed
109
    }
110

Vitor Sessak's avatar
Vitor Sessak committed
111
    if(*(*buf)++ != ']') {
112
        av_log(log_ctx, AV_LOG_ERROR,
Vitor Sessak's avatar
Vitor Sessak committed
113
               "Mismatched '[' found in the following: \"%s\".\n", start);
Vitor Sessak's avatar
Vitor Sessak committed
114
    fail:
115
        av_freep(&name);
Vitor Sessak's avatar
Vitor Sessak committed
116
    }
117 118

    return name;
119 120
}

121 122 123 124 125 126 127 128 129 130 131
static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
                                      const char *name, const char *args,
                                      AVClass *log_ctx)
{
    AVFilterContext *filt;

    AVFilter *filterdef;
    char inst_name[30];

    snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);

132 133 134
    filterdef = avfilter_get_by_name(name);

    if(!filterdef) {
135 136 137 138 139
        av_log(log_ctx, AV_LOG_ERROR,
               "no such filter: '%s'\n", name);
        return NULL;
    }

140 141
    filt = avfilter_open(filterdef, inst_name);
    if(!filt) {
142 143 144 145 146
        av_log(log_ctx, AV_LOG_ERROR,
               "error creating filter '%s'\n", name);
        return NULL;
    }

Vitor Sessak's avatar
Vitor Sessak committed
147 148
    if(avfilter_graph_add_filter(ctx, filt) < 0) {
        avfilter_destroy(filt);
149
        return NULL;
Vitor Sessak's avatar
Vitor Sessak committed
150
    }
151 152 153 154 155 156 157 158 159 160

    if(avfilter_init_filter(filt, args, NULL)) {
        av_log(log_ctx, AV_LOG_ERROR,
               "error initializing filter '%s' with args '%s'\n", name, args);
        return NULL;
    }

    return filt;
}

161 162 163 164 165 166
/**
 * Parse "filter=params"
 */
static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
                                     int index, AVClass *log_ctx)
{
Vitor Sessak's avatar
Vitor Sessak committed
167
    char *opts = NULL;
168
    char *name = consume_string(buf);
Vitor Sessak's avatar
Vitor Sessak committed
169
    AVFilterContext *ret;
170

Vitor Sessak's avatar
Vitor Sessak committed
171
    if(**buf == '=') {
172 173
        (*buf)++;
        opts = consume_string(buf);
Vitor Sessak's avatar
Vitor Sessak committed
174
    }
175

Vitor Sessak's avatar
Vitor Sessak committed
176 177 178 179
    ret = create_filter(graph, index, name, opts, log_ctx);
    av_free(name);
    av_free(opts);
    return ret;
180 181
}

182 183
static void free_inout(AVFilterInOut *head)
{
184
    while(head) {
185
        AVFilterInOut *next = head->next;
Vitor Sessak's avatar
Vitor Sessak committed
186
        av_free(head->name);
187 188 189 190 191
        av_free(head);
        head = next;
    }
}

192 193 194 195
static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
{
    AVFilterInOut *ret;

196
    while(*links && strcmp((*links)->name, label))
Vitor Sessak's avatar
Vitor Sessak committed
197
        links = &((*links)->next);
198

Vitor Sessak's avatar
Vitor Sessak committed
199
    ret = *links;
200

201
    if(ret)
Vitor Sessak's avatar
Vitor Sessak committed
202
        *links = ret->next;
203 204 205 206

    return ret;
}

207 208 209 210 211
static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
{
    element->next = *inouts;
    *inouts = element;
}
212 213 214 215 216

static int link_filter_inouts(AVFilterContext *filter,
                              AVFilterInOut **currInputs,
                              AVFilterInOut **openLinks, AVClass *log_ctx)
{
217
    int pad = filter->input_count;
218

219
    while(pad--) {
220
        AVFilterInOut *p = *currInputs;
221 222 223
        if(!p) {
            av_log(log_ctx, AV_LOG_ERROR,
                   "Not enough inputs specified for the \"%s\" filter.\n",
224
                   filter->filter->name);
225 226 227
            return -1;
        }

Vitor Sessak's avatar
Vitor Sessak committed
228 229
        *currInputs = (*currInputs)->next;

230 231 232
        if(p->filter) {
            if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
                return -1;
Vitor Sessak's avatar
Vitor Sessak committed
233
            av_free(p->name);
234 235 236 237
            av_free(p);
        } else {
            p->filter = filter;
            p->pad_idx = pad;
238
            insert_inout(openInputs, p);
239 240 241 242 243 244
        }
    }

    if(*currInputs) {
        av_log(log_ctx, AV_LOG_ERROR,
               "Too many inputs specified for the \"%s\" filter.\n",
245
               filter->filter->name);
246 247 248 249
        return -1;
    }

    pad = filter->output_count;
250
    while(pad--) {
251
        AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
252 253 254
        currlinkn->type    = LinkTypeOut;
        currlinkn->filter  = filter;
        currlinkn->pad_idx = pad;
255
        insert_inout(currInputs, currlinkn);
256 257 258 259 260 261 262
    }

    return 0;
}

static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
                        AVFilterInOut **openLinks, AVClass *log_ctx)
263
{
264 265
    int pad = 0;

266
    while(**buf == '[') {
267
        char *name = parse_link_name(buf, log_ctx);
Vitor Sessak's avatar
Vitor Sessak committed
268
        AVFilterInOut *match;
Vitor Sessak's avatar
Vitor Sessak committed
269

Vitor Sessak's avatar
Vitor Sessak committed
270
        if(!name)
Vitor Sessak's avatar
Vitor Sessak committed
271 272
            return -1;

273
        /* First check if the label is not in the openLinks list */
Vitor Sessak's avatar
Vitor Sessak committed
274
        match = extract_inout(name, openLinks);
275

Vitor Sessak's avatar
Vitor Sessak committed
276
        if(match) {
277 278
            /* A label of a open link. Make it one of the inputs of the next
               filter */
Vitor Sessak's avatar
Vitor Sessak committed
279
            if(match->type != LinkTypeOut) {
280
                av_log(log_ctx, AV_LOG_ERROR,
Vitor Sessak's avatar
Vitor Sessak committed
281
                       "Label \"%s\" appears twice as input!\n", match->name);
282 283
                return -1;
            }
Vitor Sessak's avatar
Vitor Sessak committed
284
            av_free(name);
285 286
        } else {
            /* Not in the list, so add it as an input */
Vitor Sessak's avatar
Vitor Sessak committed
287 288 289 290
            match = av_mallocz(sizeof(AVFilterInOut));
            match->name    = name;
            match->type    = LinkTypeIn;
            match->pad_idx = pad;
291
        }
292

Vitor Sessak's avatar
Vitor Sessak committed
293
        insert_inout(currInputs, match);
294

295
        *buf += consume_whitespace(*buf);
296
        pad++;
297
    }
298

299 300 301
    return pad;
}

302 303
static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
                         AVFilterInOut **openLinks, AVClass *log_ctx)
304
{
305 306
    int pad = 0;

307
    while(**buf == '[') {
308
        char *name = parse_link_name(buf, log_ctx);
309 310
        AVFilterInOut *match;

Vitor Sessak's avatar
Vitor Sessak committed
311 312 313
        AVFilterInOut *input = *currInputs;
        *currInputs = (*currInputs)->next;

314 315 316 317 318 319
        if(!name)
            return -1;

        /* First check if the label is not in the openLinks list */
        match = extract_inout(name, openLinks);

320
        if(match) {
321
            /* A label of a open link. Link it. */
Vitor Sessak's avatar
Vitor Sessak committed
322
            if(match->type != LinkTypeIn) {
323 324 325 326 327
                av_log(log_ctx, AV_LOG_ERROR,
                       "Label \"%s\" appears twice as output!\n", match->name);
                return -1;
            }

Vitor Sessak's avatar
Vitor Sessak committed
328
            if(link_filter(input->filter, input->pad_idx,
329 330
                           match->filter, match->pad_idx, log_ctx) < 0)
                return -1;
Vitor Sessak's avatar
Vitor Sessak committed
331 332
            av_free(match->name);
            av_free(name);
333
            av_free(match);
Vitor Sessak's avatar
Vitor Sessak committed
334
            av_free(input);
335 336
        } else {
            /* Not in the list, so add the first input as a openLink */
Vitor Sessak's avatar
Vitor Sessak committed
337 338 339
            input->next = *openLinks;
            input->type = LinkTypeOut;
            input->name = name;
340
            insert_inout(openOutputs, input);
341
        }
342
        *buf += consume_whitespace(*buf);
343
        pad++;
344
    }
345

346 347
    return pad;
}
348

349
int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
Vitor Sessak's avatar
Vitor Sessak committed
350
                         AVFilterInOut *openLinks, AVClass *log_ctx)
351 352 353 354
{
    int index = 0;
    char chr = 0;

355
    AVFilterInOut *currInputs = NULL;
356 357

    do {
358
        AVFilterContext *filter;
359
        filters += consume_whitespace(filters);
360

Vitor Sessak's avatar
Vitor Sessak committed
361
        if(parse_inputs(&filters, &currInputs, &openLinks, log_ctx) < 0)
362 363
            goto fail;

364 365 366
        filter = parse_filter(&filters, graph, index, log_ctx);

        if(!filter)
Vitor Sessak's avatar
Vitor Sessak committed
367 368
            goto fail;

369
        if(filter->input_count == 1 && !currInputs && !index) {
Vitor Sessak's avatar
Vitor Sessak committed
370
            /* First input can be ommitted if it is "[in]" */
371
            const char *tmp = "[in]";
Vitor Sessak's avatar
Vitor Sessak committed
372
            if(parse_inputs(&tmp, &currInputs, &openLinks, log_ctx))
373 374 375
                goto fail;
        }

376 377
        if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
            goto fail;
Vitor Sessak's avatar
Vitor Sessak committed
378

Vitor Sessak's avatar
Vitor Sessak committed
379
        if(parse_outputs(&filters, &currInputs, &openLinks, log_ctx))
Vitor Sessak's avatar
Vitor Sessak committed
380 381
            goto fail;

382
        filters += consume_whitespace(filters);
383 384
        chr = *filters++;

Vitor Sessak's avatar
Vitor Sessak committed
385
        if(chr == ';' && currInputs) {
386 387 388 389
            av_log(log_ctx, AV_LOG_ERROR,
                   "Could not find a output to link when parsing \"%s\"\n",
                   filters - 1);
            goto fail;
390
        }
391
        index++;
392
    } while(chr == ',' || chr == ';');
393

394
    if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
Vitor Sessak's avatar
Vitor Sessak committed
395
        /* Last output can be ommitted if it is "[out]" */
396 397
        const char *tmp = "[out]";
        if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
398 399 400 401 402 403 404
            goto fail;
    }

    return 0;

 fail:
    avfilter_destroy_graph(graph);
405 406
    free_inout(openLinks);
    free_inout(currInputs);
407 408
    return -1;
}