graphparser.c 9.41 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
static int link_filter(AVFilterContext *src, int srcpad,
34 35
                       AVFilterContext *dst, int dstpad,
                       AVClass *log_ctx)
36
{
37
    if(avfilter_link(src, srcpad, dst, dstpad)) {
38
        av_log(log_ctx, AV_LOG_ERROR,
39 40
               "cannot create the link %s:%d -> %s:%d\n",
               src->filter->name, srcpad, dst->filter->name, dstpad);
41 42 43 44 45 46 47
        return -1;
    }

    return 0;
}

/**
Vitor Sessak's avatar
Vitor Sessak committed
48
 * Parse "[linkname]"
Vitor Sessak's avatar
Vitor Sessak committed
49 50
 * @param name a pointer (that need to be free'd after use) to the name between
 *        parenthesis
51
 */
52
static char *parse_link_name(const char **buf, AVClass *log_ctx)
53
{
Vitor Sessak's avatar
Vitor Sessak committed
54
    const char *start = *buf;
55
    char *name;
56 57
    (*buf)++;

58
    name = av_get_token(buf, "]");
59

60
    if(!name[0]) {
61
        av_log(log_ctx, AV_LOG_ERROR,
Vitor Sessak's avatar
Vitor Sessak committed
62
               "Bad (empty?) label found in the following: \"%s\".\n", start);
63
        goto fail;
Vitor Sessak's avatar
Vitor Sessak committed
64
    }
65

Vitor Sessak's avatar
Vitor Sessak committed
66
    if(*(*buf)++ != ']') {
67
        av_log(log_ctx, AV_LOG_ERROR,
Vitor Sessak's avatar
Vitor Sessak committed
68
               "Mismatched '[' found in the following: \"%s\".\n", start);
Vitor Sessak's avatar
Vitor Sessak committed
69
    fail:
70
        av_freep(&name);
Vitor Sessak's avatar
Vitor Sessak committed
71
    }
72 73

    return name;
74 75
}

76
static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
77
                                      const char *filt_name, const char *args,
78 79
                                      AVClass *log_ctx)
{
80
    AVFilterContext *filt_ctx;
81

82
    AVFilter *filt;
83 84 85 86
    char inst_name[30];

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

87
    filt = avfilter_get_by_name(filt_name);
88

89
    if(!filt) {
90
        av_log(log_ctx, AV_LOG_ERROR,
91
               "no such filter: '%s'\n", filt_name);
92 93 94
        return NULL;
    }

95 96
    filt_ctx = avfilter_open(filt, inst_name);
    if(!filt_ctx) {
97
        av_log(log_ctx, AV_LOG_ERROR,
98
               "error creating filter '%s'\n", filt_name);
99 100 101
        return NULL;
    }

102 103
    if(avfilter_graph_add_filter(ctx, filt_ctx) < 0) {
        avfilter_destroy(filt_ctx);
104
        return NULL;
Vitor Sessak's avatar
Vitor Sessak committed
105
    }
106

107
    if(avfilter_init_filter(filt_ctx, args, NULL)) {
108
        av_log(log_ctx, AV_LOG_ERROR,
109
               "error initializing filter '%s' with args '%s'\n", filt_name, args);
110 111 112
        return NULL;
    }

113
    return filt_ctx;
114 115
}

116 117 118 119 120 121
/**
 * Parse "filter=params"
 */
static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
                                     int index, AVClass *log_ctx)
{
Vitor Sessak's avatar
Vitor Sessak committed
122
    char *opts = NULL;
123
    char *name = av_get_token(buf, "=,;[\n");
Vitor Sessak's avatar
Vitor Sessak committed
124
    AVFilterContext *ret;
125

Vitor Sessak's avatar
Vitor Sessak committed
126
    if(**buf == '=') {
127
        (*buf)++;
128
        opts = av_get_token(buf, "[],;\n");
Vitor Sessak's avatar
Vitor Sessak committed
129
    }
130

Vitor Sessak's avatar
Vitor Sessak committed
131 132 133 134
    ret = create_filter(graph, index, name, opts, log_ctx);
    av_free(name);
    av_free(opts);
    return ret;
135 136
}

137 138
static void free_inout(AVFilterInOut *head)
{
139
    while(head) {
140
        AVFilterInOut *next = head->next;
Vitor Sessak's avatar
Vitor Sessak committed
141
        av_free(head->name);
142 143 144 145 146
        av_free(head);
        head = next;
    }
}

147 148 149 150
static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
{
    AVFilterInOut *ret;

151
    while(*links && strcmp((*links)->name, label))
Vitor Sessak's avatar
Vitor Sessak committed
152
        links = &((*links)->next);
153

Vitor Sessak's avatar
Vitor Sessak committed
154
    ret = *links;
155

156
    if(ret)
Vitor Sessak's avatar
Vitor Sessak committed
157
        *links = ret->next;
158 159 160 161

    return ret;
}

162 163 164 165 166
static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
{
    element->next = *inouts;
    *inouts = element;
}
167 168

static int link_filter_inouts(AVFilterContext *filter,
169 170
                              AVFilterInOut **curr_inputs,
                              AVFilterInOut **open_inputs, AVClass *log_ctx)
171
{
172
    int pad = filter->input_count;
173

174
    while(pad--) {
175
        AVFilterInOut *p = *curr_inputs;
176 177 178
        if(!p) {
            av_log(log_ctx, AV_LOG_ERROR,
                   "Not enough inputs specified for the \"%s\" filter.\n",
179
                   filter->filter->name);
180 181 182
            return -1;
        }

183
        *curr_inputs = (*curr_inputs)->next;
Vitor Sessak's avatar
Vitor Sessak committed
184

185 186 187
        if(p->filter) {
            if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
                return -1;
Vitor Sessak's avatar
Vitor Sessak committed
188
            av_free(p->name);
189 190 191 192
            av_free(p);
        } else {
            p->filter = filter;
            p->pad_idx = pad;
193
            insert_inout(open_inputs, p);
194 195 196
        }
    }

197
    if(*curr_inputs) {
198 199
        av_log(log_ctx, AV_LOG_ERROR,
               "Too many inputs specified for the \"%s\" filter.\n",
200
               filter->filter->name);
201 202 203 204
        return -1;
    }

    pad = filter->output_count;
205
    while(pad--) {
206
        AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
207 208
        currlinkn->filter  = filter;
        currlinkn->pad_idx = pad;
209
        insert_inout(curr_inputs, currlinkn);
210 211 212 213 214
    }

    return 0;
}

215 216
static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
                        AVFilterInOut **open_outputs, AVClass *log_ctx)
217
{
218 219
    int pad = 0;

220
    while(**buf == '[') {
221
        char *name = parse_link_name(buf, log_ctx);
Vitor Sessak's avatar
Vitor Sessak committed
222
        AVFilterInOut *match;
Vitor Sessak's avatar
Vitor Sessak committed
223

Vitor Sessak's avatar
Vitor Sessak committed
224
        if(!name)
Vitor Sessak's avatar
Vitor Sessak committed
225 226
            return -1;

227 228
        /* First check if the label is not in the open_outputs list */
        match = extract_inout(name, open_outputs);
229

Vitor Sessak's avatar
Vitor Sessak committed
230
        if(match) {
Vitor Sessak's avatar
Vitor Sessak committed
231
            av_free(name);
232 233
        } else {
            /* Not in the list, so add it as an input */
Vitor Sessak's avatar
Vitor Sessak committed
234 235 236
            match = av_mallocz(sizeof(AVFilterInOut));
            match->name    = name;
            match->pad_idx = pad;
237
        }
238

239
        insert_inout(curr_inputs, match);
240

241
        *buf += strspn(*buf, WHITESPACES);
242
        pad++;
243
    }
244

245 246 247
    return pad;
}

248 249 250
static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
                         AVFilterInOut **open_inputs,
                         AVFilterInOut **open_outputs, AVClass *log_ctx)
251
{
252 253
    int pad = 0;

254
    while(**buf == '[') {
255
        char *name = parse_link_name(buf, log_ctx);
256 257
        AVFilterInOut *match;

258 259
        AVFilterInOut *input = *curr_inputs;
        *curr_inputs = (*curr_inputs)->next;
Vitor Sessak's avatar
Vitor Sessak committed
260

261 262 263
        if(!name)
            return -1;

264 265
        /* First check if the label is not in the open_inputs list */
        match = extract_inout(name, open_inputs);
266

267
        if(match) {
Vitor Sessak's avatar
Vitor Sessak committed
268
            if(link_filter(input->filter, input->pad_idx,
269 270
                           match->filter, match->pad_idx, log_ctx) < 0)
                return -1;
Vitor Sessak's avatar
Vitor Sessak committed
271 272
            av_free(match->name);
            av_free(name);
273
            av_free(match);
Vitor Sessak's avatar
Vitor Sessak committed
274
            av_free(input);
275
        } else {
276
            /* Not in the list, so add the first input as a open_output */
Vitor Sessak's avatar
Vitor Sessak committed
277
            input->name = name;
278
            insert_inout(open_outputs, input);
279
        }
280
        *buf += strspn(*buf, WHITESPACES);
281
        pad++;
282
    }
283

284 285
    return pad;
}
286

287
int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
288 289
                         AVFilterInOut *open_inputs,
                         AVFilterInOut *open_outputs, AVClass *log_ctx)
290 291 292 293
{
    int index = 0;
    char chr = 0;

294
    AVFilterInOut *curr_inputs = NULL;
295 296

    do {
297
        AVFilterContext *filter;
298
        filters += strspn(filters, WHITESPACES);
299

300
        if(parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
301 302
            goto fail;

303 304 305
        filter = parse_filter(&filters, graph, index, log_ctx);

        if(!filter)
Vitor Sessak's avatar
Vitor Sessak committed
306 307
            goto fail;

308
        if(filter->input_count == 1 && !curr_inputs && !index) {
309
            /* First input can be omitted if it is "[in]" */
310
            const char *tmp = "[in]";
311
            if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
312 313 314
                goto fail;
        }

315
        if(link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
316
            goto fail;
Vitor Sessak's avatar
Vitor Sessak committed
317

318
        if(parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
319
                         log_ctx) < 0)
Vitor Sessak's avatar
Vitor Sessak committed
320 321
            goto fail;

322
        filters += strspn(filters, WHITESPACES);
323 324
        chr = *filters++;

325
        if(chr == ';' && curr_inputs) {
326 327 328 329
            av_log(log_ctx, AV_LOG_ERROR,
                   "Could not find a output to link when parsing \"%s\"\n",
                   filters - 1);
            goto fail;
330
        }
331
        index++;
332
    } while(chr == ',' || chr == ';');
333

334
    if (chr) {
335 336 337 338 339 340
        av_log(log_ctx, AV_LOG_ERROR,
               "Unable to parse graph description substring: \"%s\"\n",
               filters - 1);
        goto fail;
    }

341
    if(open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
342
        /* Last output can be omitted if it is "[out]" */
343
        const char *tmp = "[out]";
344 345
        if(parse_outputs(&tmp, &curr_inputs, &open_inputs,
                         &open_outputs, log_ctx) < 0)
346 347 348 349 350 351
            goto fail;
    }

    return 0;

 fail:
352
    avfilter_graph_destroy(graph);
353 354 355
    free_inout(open_inputs);
    free_inout(open_outputs);
    free_inout(curr_inputs);
356 357
    return -1;
}