graphparser.c 10.1 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
static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
122
                                      const char *filt_name, const char *args,
123 124
                                      AVClass *log_ctx)
{
125
    AVFilterContext *filt_ctx;
126

127
    AVFilter *filt;
128 129 130 131
    char inst_name[30];

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

132
    filt = avfilter_get_by_name(filt_name);
133

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

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

147 148
    if(avfilter_graph_add_filter(ctx, filt_ctx) < 0) {
        avfilter_destroy(filt_ctx);
149
        return NULL;
Vitor Sessak's avatar
Vitor Sessak committed
150
    }
151

152
    if(avfilter_init_filter(filt_ctx, args, NULL)) {
153
        av_log(log_ctx, AV_LOG_ERROR,
154
               "error initializing filter '%s' with args '%s'\n", filt_name, args);
155 156 157
        return NULL;
    }

158
    return filt_ctx;
159 160
}

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

static int link_filter_inouts(AVFilterContext *filter,
                              AVFilterInOut **currInputs,
215
                              AVFilterInOut **openInputs, AVClass *log_ctx)
216
{
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
        currlinkn->filter  = filter;
        currlinkn->pad_idx = pad;
254
        insert_inout(currInputs, currlinkn);
255 256 257 258 259 260
    }

    return 0;
}

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

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

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

272 273
        /* First check if the label is not in the openOutputs list */
        match = extract_inout(name, openOutputs);
274

Vitor Sessak's avatar
Vitor Sessak committed
275
        if(match) {
Vitor Sessak's avatar
Vitor Sessak committed
276
            av_free(name);
277 278
        } else {
            /* Not in the list, so add it as an input */
Vitor Sessak's avatar
Vitor Sessak committed
279 280 281
            match = av_mallocz(sizeof(AVFilterInOut));
            match->name    = name;
            match->pad_idx = pad;
282
        }
283

Vitor Sessak's avatar
Vitor Sessak committed
284
        insert_inout(currInputs, match);
285

286
        *buf += consume_whitespace(*buf);
287
        pad++;
288
    }
289

290 291 292
    return pad;
}

293
static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
294 295
                         AVFilterInOut **openInputs,
                         AVFilterInOut **openOutputs, AVClass *log_ctx)
296
{
297 298
    int pad = 0;

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

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

306 307 308
        if(!name)
            return -1;

309 310
        /* First check if the label is not in the openInputs list */
        match = extract_inout(name, openInputs);
311

312
        if(match) {
Vitor Sessak's avatar
Vitor Sessak committed
313
            if(link_filter(input->filter, input->pad_idx,
314 315
                           match->filter, match->pad_idx, log_ctx) < 0)
                return -1;
Vitor Sessak's avatar
Vitor Sessak committed
316 317
            av_free(match->name);
            av_free(name);
318
            av_free(match);
Vitor Sessak's avatar
Vitor Sessak committed
319
            av_free(input);
320
        } else {
321
            /* Not in the list, so add the first input as a openOutput */
Vitor Sessak's avatar
Vitor Sessak committed
322
            input->name = name;
323
            insert_inout(openOutputs, input);
324
        }
325
        *buf += consume_whitespace(*buf);
326
        pad++;
327
    }
328

329 330
    return pad;
}
331

332
int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
333 334
                         AVFilterInOut *openInputs,
                         AVFilterInOut *openOutputs, AVClass *log_ctx)
335 336 337 338
{
    int index = 0;
    char chr = 0;

339
    AVFilterInOut *currInputs = NULL;
340 341

    do {
342
        AVFilterContext *filter;
343
        filters += consume_whitespace(filters);
344

345
        if(parse_inputs(&filters, &currInputs, &openOutputs, log_ctx) < 0)
346 347
            goto fail;

348 349 350
        filter = parse_filter(&filters, graph, index, log_ctx);

        if(!filter)
Vitor Sessak's avatar
Vitor Sessak committed
351 352
            goto fail;

353
        if(filter->input_count == 1 && !currInputs && !index) {
Vitor Sessak's avatar
Vitor Sessak committed
354
            /* First input can be ommitted if it is "[in]" */
355
            const char *tmp = "[in]";
356
            if(parse_inputs(&tmp, &currInputs, &openOutputs, log_ctx) < 0)
357 358 359
                goto fail;
        }

360
        if(link_filter_inouts(filter, &currInputs, &openInputs, log_ctx) < 0)
361
            goto fail;
Vitor Sessak's avatar
Vitor Sessak committed
362

363 364
        if(parse_outputs(&filters, &currInputs, &openInputs, &openOutputs,
                         log_ctx) < 0)
Vitor Sessak's avatar
Vitor Sessak committed
365 366
            goto fail;

367
        filters += consume_whitespace(filters);
368 369
        chr = *filters++;

Vitor Sessak's avatar
Vitor Sessak committed
370
        if(chr == ';' && currInputs) {
371 372 373 374
            av_log(log_ctx, AV_LOG_ERROR,
                   "Could not find a output to link when parsing \"%s\"\n",
                   filters - 1);
            goto fail;
375
        }
376
        index++;
377
    } while(chr == ',' || chr == ';');
378

379
    if(openInputs && !strcmp(openInputs->name, "out") && currInputs) {
Vitor Sessak's avatar
Vitor Sessak committed
380
        /* Last output can be ommitted if it is "[out]" */
381
        const char *tmp = "[out]";
382 383
        if(parse_outputs(&tmp, &currInputs, &openInputs,
                         &openOutputs, log_ctx) < 0)
384 385 386 387 388 389 390
            goto fail;
    }

    return 0;

 fail:
    avfilter_destroy_graph(graph);
391 392
    free_inout(openInputs);
    free_inout(openOutputs);
393
    free_inout(currInputs);
394 395
    return -1;
}