graphparser.c 8.79 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
 * 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>

#include "avfilter.h"
#include "avfiltergraph.h"

/**
 * For use in av_log
 */
static const char *log_name(void *p)
{
    return "Filter parser";
}

static const AVClass filter_parser_class = {
    "Filter parser",
    log_name
};

static const AVClass *log_ctx = &filter_parser_class;

44
static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
Vitor Sessak's avatar
Vitor Sessak committed
45
                                      const char *name, const char *args)
46 47 48 49
{
    AVFilterContext *filt;

    AVFilter *filterdef;
50
    char inst_name[30];
51

52
    snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
Vitor Sessak's avatar
Vitor Sessak committed
53 54 55 56

    if(!(filterdef = avfilter_get_by_name(name))) {
        av_log(&log_ctx, AV_LOG_ERROR,
               "no such filter: '%s'\n", name);
57
        return NULL;
Vitor Sessak's avatar
Vitor Sessak committed
58 59
    }

60
    if(!(filt = avfilter_open(filterdef, inst_name))) {
61 62
        av_log(&log_ctx, AV_LOG_ERROR,
               "error creating filter '%s'\n", name);
63
        return NULL;
64
    }
Vitor Sessak's avatar
Vitor Sessak committed
65

Vitor Sessak's avatar
Vitor Sessak committed
66
    if(avfilter_graph_add_filter(ctx, filt) < 0)
67
        return NULL;
Vitor Sessak's avatar
Vitor Sessak committed
68

69 70
    if(avfilter_init_filter(filt, args, NULL)) {
        av_log(&log_ctx, AV_LOG_ERROR,
Vitor Sessak's avatar
Vitor Sessak committed
71
               "error initializing filter '%s' with args '%s'\n", name, args);
72
        return NULL;
73 74
    }

75
    return filt;
76 77
}

78 79
static int link_filter(AVFilterContext *src, int srcpad,
                       AVFilterContext *dst, int dstpad)
80
{
81 82 83 84
    if(avfilter_link(src, srcpad, dst, dstpad)) {
        av_log(&log_ctx, AV_LOG_ERROR,
               "cannot create the link %s:%d -> %s:%d\n",
               src->filter->name, srcpad, dst->filter->name, dstpad);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        return -1;
    }

    return 0;
}

static void consume_whitespace(const char **buf)
{
    *buf += strspn(*buf, " \n\t");
}

/**
 * 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)
{
102
    char *out = av_malloc(strlen(*buf) + 1);
103
    char *ret = out;
104 105 106

    consume_whitespace(buf);

107
    do{
Vitor Sessak's avatar
Vitor Sessak committed
108
        char c = *(*buf)++;
109 110
        switch (c) {
        case '\\':
Vitor Sessak's avatar
Vitor Sessak committed
111
            *out++= *(*buf)++;
112
            break;
113
        case '\'':
Vitor Sessak's avatar
Vitor Sessak committed
114 115 116
            while(**buf && **buf != '\'')
                *out++= *(*buf)++;
            if(**buf) (*buf)++;
117 118
            break;
        case 0:
119 120
        case ']':
        case '[':
121 122
        case '=':
        case ',':
123 124
        case ' ':
        case '\n':
125 126 127 128 129 130
            *out++= 0;
            break;
        default:
            *out++= c;
        }
    } while(out[-1]);
131

Vitor Sessak's avatar
Vitor Sessak committed
132
    (*buf)--;
133 134
    consume_whitespace(buf);

135 136 137 138
    return ret;
}

/**
Vitor Sessak's avatar
Vitor Sessak committed
139
 * Parse "[linkname]"
140 141 142 143 144
 * @arg name a pointer (that need to be free'd after use) to the name between
 *           parenthesis
 */
static void parse_link_name(const char **buf, char **name)
{
Vitor Sessak's avatar
Vitor Sessak committed
145
    const char *start = *buf;
146 147 148 149
    (*buf)++;

    *name = consume_string(buf);

Vitor Sessak's avatar
Vitor Sessak committed
150 151 152
    if(!*name[0]) {
        av_log(&log_ctx, AV_LOG_ERROR,
               "Bad (empty?) label found in the following: \"%s\".\n", start);
153
        goto fail;
Vitor Sessak's avatar
Vitor Sessak committed
154
    }
155

Vitor Sessak's avatar
Vitor Sessak committed
156 157 158
    if(*(*buf)++ != ']') {
        av_log(&log_ctx, AV_LOG_ERROR,
               "Mismatched '[' found in the following: \"%s\".\n", start);
159
        goto fail;
Vitor Sessak's avatar
Vitor Sessak committed
160
    }
161 162

    return;
Vitor Sessak's avatar
Vitor Sessak committed
163

164 165 166 167 168 169 170 171 172 173 174
 fail:
    av_freep(name);
}

/**
 * Parse "filter=params"
 * @arg name a pointer (that need to be free'd after use) to the name of the
 *           filter
 * @arg ars  a pointer (that need to be free'd after use) to the args of the
 *           filter
 */
175
static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
176 177 178 179
{
    char *name, *opts;
    name = consume_string(buf);

Vitor Sessak's avatar
Vitor Sessak committed
180
    if(**buf == '=') {
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        (*buf)++;
        opts = consume_string(buf);
    } else {
        opts = NULL;
    }

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

enum LinkType {
    LinkTypeIn,
    LinkTypeOut,
};

/**
 * A linked-list of the inputs/outputs of the filter chain.
 */
typedef struct AVFilterInOut {
    enum LinkType type;
    char *name;
201
    AVFilterContext *filter;
202 203 204 205 206 207 208 209
    int pad_idx;

    struct AVFilterInOut *next;
} AVFilterInOut;

static void free_inout(AVFilterInOut *head)
{
    while (head) {
210
        AVFilterInOut *next = head->next;
211 212 213 214 215 216
        av_free(head);
        head = next;
    }
}

/**
Vitor Sessak's avatar
Vitor Sessak committed
217
 * Parse "[a1][link2] ... [etc]"
218
 */
Vitor Sessak's avatar
Vitor Sessak committed
219
static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
220
                        enum LinkType type, AVFilterContext *filter)
221
{
222
    while (**buf == '[') {
223 224
        AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
        parse_link_name(buf, &inoutn->name);
Vitor Sessak's avatar
Vitor Sessak committed
225 226 227 228 229 230

        if (!inoutn->name) {
            av_free(inoutn);
            return -1;
        }

Vitor Sessak's avatar
Vitor Sessak committed
231 232
        inoutn->type    = type;
        inoutn->filter  = filter;
233
        inoutn->pad_idx = pad++;
Vitor Sessak's avatar
Vitor Sessak committed
234
        inoutn->next    = *inout;
235 236 237 238 239
        *inout = inoutn;
    }
    return pad;
}

240 241
static const char *skip_inouts(const char *buf)
{
Vitor Sessak's avatar
Vitor Sessak committed
242 243 244
    while (*buf == '[')
        buf += strcspn(buf, "]") + 1;

245 246 247 248
    return buf;
}


249 250 251
/**
 * Parse a string describing a filter graph.
 */
252 253 254
int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
                         AVFilterContext *in, int inpad,
                         AVFilterContext *out, int outpad)
255 256 257 258 259 260 261 262 263
{
    AVFilterInOut *inout=NULL;
    AVFilterInOut  *head=NULL;

    int index = 0;
    char chr = 0;
    int pad = 0;
    int has_out = 0;

264
    AVFilterContext *last_filt = NULL;
265 266

    do {
267
        AVFilterContext *filter;
268
        int oldpad = pad;
269 270 271 272
        const char *inouts;

        consume_whitespace(&filters);
        inouts = filters;
273

274 275 276
        // We need to parse the inputs of the filter after we create it, so
        // skip it by now
        filters = skip_inouts(filters);
277

Vitor Sessak's avatar
Vitor Sessak committed
278
        if(!(filter = parse_filter(&filters, graph, index)))
279 280
            goto fail;

281 282
        pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);

Vitor Sessak's avatar
Vitor Sessak committed
283 284 285
        if(pad < 0)
            goto fail;

286 287
        // If the first filter has an input and none was given, it is
        // implicitly the input of the whole graph.
Vitor Sessak's avatar
Vitor Sessak committed
288
        if(pad == 0 && filter->input_count == 1) {
289
            if(link_filter(in, inpad, filter, 0))
290 291 292 293
                goto fail;
        }

        if(chr == ',') {
Vitor Sessak's avatar
Vitor Sessak committed
294
            if(link_filter(last_filt, oldpad, filter, 0) < 0)
295 296
                goto fail;
        }
Vitor Sessak's avatar
Vitor Sessak committed
297

298
        pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
299 300 301

        consume_whitespace(&filters);

302 303
        chr = *filters++;
        index++;
304
        last_filt = filter;
305 306 307 308
    } while (chr == ',' || chr == ';');

    head = inout;
    for (; inout != NULL; inout = inout->next) {
Vitor Sessak's avatar
Vitor Sessak committed
309
        if(inout->filter == NULL)
310 311
            continue; // Already processed

Vitor Sessak's avatar
Vitor Sessak committed
312
        if(!strcmp(inout->name, "in")) {
313
            if(link_filter(in, inpad, inout->filter, inout->pad_idx))
314
                goto fail;
315

Vitor Sessak's avatar
Vitor Sessak committed
316
        } else if(!strcmp(inout->name, "out")) {
317 318
            has_out = 1;

319
            if(link_filter(inout->filter, inout->pad_idx, out, outpad))
320 321 322 323 324 325 326
                goto fail;

        } else {
            AVFilterInOut *p, *src, *dst;
            for (p = inout->next;
                 p && strcmp(p->name,inout->name); p = p->next);

Vitor Sessak's avatar
Vitor Sessak committed
327
            if(!p) {
328 329 330 331 332
                av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
                       inout->name);
                goto fail;
            }

Vitor Sessak's avatar
Vitor Sessak committed
333
            if(p->type == LinkTypeIn && inout->type == LinkTypeOut) {
334 335
                src = inout;
                dst = p;
Vitor Sessak's avatar
Vitor Sessak committed
336
            } else if(p->type == LinkTypeOut && inout->type == LinkTypeIn) {
337 338 339 340 341 342 343 344
                src = p;
                dst = inout;
            } else {
                av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
                       inout->name);
                goto fail;
            }

Vitor Sessak's avatar
Vitor Sessak committed
345
            if(link_filter(src->filter, src->pad_idx, dst->filter, dst->pad_idx) < 0)
346 347
                goto fail;

348 349
            src->filter = NULL;
            dst->filter = NULL;
350 351 352 353 354
        }
    }

    free_inout(head);

Vitor Sessak's avatar
Vitor Sessak committed
355
    if(!has_out) {
356
        if(link_filter(last_filt, pad, out, outpad))
357 358 359 360 361 362 363 364 365 366
            goto fail;
    }

    return 0;

 fail:
    free_inout(head);
    avfilter_destroy_graph(graph);
    return -1;
}