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 147 148 149 150 151 152 153 154 155 156 157 158
        av_log(log_ctx, AV_LOG_ERROR,
               "error creating filter '%s'\n", name);
        return NULL;
    }

    if(avfilter_graph_add_filter(ctx, filt) < 0)
        return NULL;

    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;
}

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

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

    return create_filter(graph, index, name, opts, log_ctx);
}

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

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

189
    while(*links && strcmp((*links)->name, label))
Vitor Sessak's avatar
Vitor Sessak committed
190
        links = &((*links)->next);
191

Vitor Sessak's avatar
Vitor Sessak committed
192
    ret = *links;
193

194
    if(ret)
Vitor Sessak's avatar
Vitor Sessak committed
195
        *links = ret->next;
196 197 198 199 200 201 202 203 204 205 206 207

    return ret;
}


static int link_filter_inouts(AVFilterContext *filter,
                              AVFilterInOut **currInputs,
                              AVFilterInOut **openLinks, AVClass *log_ctx)
{
    int pad = 0;

    pad = filter->input_count;
208
    while(pad--) {
209
        AVFilterInOut *p = *currInputs;
Vitor Sessak's avatar
Vitor Sessak committed
210
        *currInputs = (*currInputs)->next;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        if(!p) {
            av_log(log_ctx, AV_LOG_ERROR,
                   "Not enough inputs specified for the \"%s\" filter.\n",
                   filter->name);
            return -1;
        }

        if(p->filter) {
            if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
                return -1;
            av_free(p);
        } else {
            p->filter = filter;
            p->pad_idx = pad;
            p->next = *openLinks;
            *openLinks = p;
        }
    }


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

    pad = filter->output_count;
239
    while(pad--) {
240 241 242 243 244 245 246 247 248 249 250 251 252 253
        AVFilterInOut *currlinkn = av_malloc(sizeof(AVFilterInOut));
        currlinkn->name    = NULL;
        currlinkn->type    = LinkTypeOut;
        currlinkn->filter  = filter;
        currlinkn->pad_idx = pad;
        currlinkn->next    = *currInputs;
        *currInputs = currlinkn;
    }

    return 0;
}

static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
                        AVFilterInOut **openLinks, AVClass *log_ctx)
254
{
255 256
    int pad = 0;

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

Vitor Sessak's avatar
Vitor Sessak committed
262
        if(!name)
Vitor Sessak's avatar
Vitor Sessak committed
263 264
            return -1;

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

Vitor Sessak's avatar
Vitor Sessak committed
268
        if(match) {
269 270
            /* A label of a open link. Make it one of the inputs of the next
               filter */
Vitor Sessak's avatar
Vitor Sessak committed
271
            if(match->type != LinkTypeOut) {
272
                av_log(log_ctx, AV_LOG_ERROR,
Vitor Sessak's avatar
Vitor Sessak committed
273
                       "Label \"%s\" appears twice as input!\n", match->name);
274 275
                return -1;
            }
Vitor Sessak's avatar
Vitor Sessak committed
276 277

            link_to_add = match;
278 279
        } else {
            /* Not in the list, so add it as an input */
Vitor Sessak's avatar
Vitor Sessak committed
280 281 282 283 284 285
            link_to_add = av_malloc(sizeof(AVFilterInOut));

            link_to_add->name    = name;
            link_to_add->type    = LinkTypeIn;
            link_to_add->filter  = NULL;
            link_to_add->pad_idx = pad;
286
        }
Vitor Sessak's avatar
Vitor Sessak committed
287 288
        link_to_add->next = *currInputs;
        *currInputs = link_to_add;
289
        *buf += consume_whitespace(*buf);
290
        pad++;
291
    }
292

293 294 295
    return pad;
}

296 297
static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
                         AVFilterInOut **openLinks, AVClass *log_ctx)
298
{
299 300
    int pad = 0;

301
    while(**buf == '[') {
302
        char *name = parse_link_name(buf, log_ctx);
303 304
        AVFilterInOut *match;

Vitor Sessak's avatar
Vitor Sessak committed
305 306 307
        AVFilterInOut *input = *currInputs;
        *currInputs = (*currInputs)->next;

308 309 310 311 312 313
        if(!name)
            return -1;

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

314
        if(match) {
315
            /* A label of a open link. Link it. */
Vitor Sessak's avatar
Vitor Sessak committed
316
            if(match->type != LinkTypeIn) {
317 318 319 320 321
                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
322
            if(link_filter(input->filter, input->pad_idx,
323 324 325
                           match->filter, match->pad_idx, log_ctx) < 0)
                return -1;
            av_free(match);
Vitor Sessak's avatar
Vitor Sessak committed
326
            av_free(input);
327 328
        } else {
            /* Not in the list, so add the first input as a openLink */
Vitor Sessak's avatar
Vitor Sessak committed
329 330 331 332
            input->next = *openLinks;
            input->type = LinkTypeOut;
            input->name = name;
            *openLinks = input;
333
        }
334
        *buf += consume_whitespace(*buf);
335
        pad++;
336
    }
337

338 339
    return pad;
}
340

341 342 343
/**
 * Parse a string describing a filter graph.
 */
344
int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
Vitor Sessak's avatar
Vitor Sessak committed
345
                         AVFilterInOut *openLinks, AVClass *log_ctx)
346 347 348 349 350
{
    int index = 0;
    char chr = 0;
    int pad = 0;

351
    AVFilterInOut *currInputs = NULL;
352 353

    do {
354
        AVFilterContext *filter;
355
        filters += consume_whitespace(filters);
356

357
        pad = parse_inputs(&filters, &currInputs, &openLinks, log_ctx);
358

359
        if(pad < 0)
360 361
            goto fail;

362 363 364
        filter = parse_filter(&filters, graph, index, log_ctx);

        if(!filter)
Vitor Sessak's avatar
Vitor Sessak committed
365 366
            goto fail;

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

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

378
        pad = parse_outputs(&filters, &currInputs, &openLinks, log_ctx);
379

380
        if(pad < 0)
Vitor Sessak's avatar
Vitor Sessak committed
381 382
            goto fail;

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

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

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

    return 0;

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