Statistics
| Branch: | Revision:

ffmpeg / libavfilter / avfiltergraph.c @ 54d7fcc1

History | View | Annotate | Download (19.5 KB)

1
/*
2
 * Filter graphs
3
 * copyright (c) 2007 Bobby Bingham
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include <string.h>
23
#include <stddef.h>
24

    
25
#include "avstring.h"
26
#include "avfilter.h"
27
#include "avfiltergraph.h"
28

    
29
#include "allfilters.h"
30

    
31
typedef struct AVFilterGraph {
32
    unsigned filter_count;
33
    AVFilterContext **filters;
34

    
35
    /** fake filter to handle links to internal filters */
36
    AVFilterContext *link_filter;
37
} GraphContext;
38

    
39
typedef struct {
40
    AVFilterContext *graph;
41
} GraphLinkContext;
42

    
43
static int link_init(AVFilterContext *ctx, const char *args, void *opaque)
44
{
45
    GraphLinkContext *linkctx = ctx->priv;
46
    linkctx->graph = opaque;
47
    return !opaque;
48
}
49

    
50
/**
51
 * Given the link between the dummy filter and an internal filter whose input
52
 * is being exported outside the graph, this returns the externally visible
53
 * link
54
 */
55
static inline AVFilterLink *get_extern_input_link(AVFilterLink *link)
56
{
57
    GraphLinkContext *lctx = link->src->priv;
58
    return lctx->graph->inputs[link->srcpad];
59
}
60

    
61
/** query the formats supported by a filter providing input to the graph */
62
static int *link_in_query_formats(AVFilterLink *link)
63
{
64
    AVFilterLink *link2 = get_extern_input_link(link);
65
    int *(*query_formats)(AVFilterLink *);
66

    
67
    if(!link2)
68
        return avfilter_make_format_list(0);
69

    
70
    if(!(query_formats = link2->src->output_pads[link2->srcpad].query_formats))
71
        query_formats = avfilter_default_query_output_formats;
72

    
73
    return query_formats(link2);
74
}
75

    
76
/** request a frame from a filter providing input to the graph */
77
static int link_in_request_frame(AVFilterLink *link)
78
{
79
    AVFilterLink *link2 = get_extern_input_link(link);
80

    
81
    if(!link2)
82
        return -1;
83
    return avfilter_request_frame(link2);
84
}
85

    
86
static int link_in_config_props(AVFilterLink *link)
87
{
88
    AVFilterLink *link2 = get_extern_input_link(link);
89
    int (*config_props)(AVFilterLink *);
90
    int ret;
91

    
92
    if(!link2)
93
        return -1;
94
    if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
95
        config_props = avfilter_default_config_output_link;
96
    ret = config_props(link2);
97

    
98
    link->w = link2->w;
99
    link->h = link2->h;
100

    
101
    return ret;
102
}
103

    
104
/**
105
 * Given the link between the dummy filter and an internal filter whose input
106
 * is being exported outside the graph, this returns the externally visible
107
 * link
108
 */
109
static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
110
{
111
    GraphLinkContext *lctx = link->dst->priv;
112
    return lctx->graph->outputs[link->dstpad];
113
}
114

    
115
/** query the formats supported by a filter taking output from the graph */
116
static int *link_out_query_formats(AVFilterLink *link)
117
{
118
    AVFilterLink *link2 = get_extern_output_link(link);
119

    
120
    if(!link2)
121
        return avfilter_make_format_list(0);
122

    
123
    return link2->dst->input_pads[link2->dstpad].query_formats(link2);
124
}
125

    
126
static int link_out_config_props(AVFilterLink *link)
127
{
128
    AVFilterLink *link2 = get_extern_output_link(link);
129
    int (*config_props)(AVFilterLink *);
130

    
131
    if(!link2)
132
        return 0;
133

    
134
    link2->w = link->w;
135
    link2->h = link->h;
136

    
137
    if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
138
        config_props = avfilter_default_config_input_link;
139
    return config_props(link2);
140
}
141

    
142
static void link_out_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
143
{
144
    AVFilterLink *link2 = get_extern_output_link(link);
145

    
146
    if(!link2)
147
        avfilter_unref_pic(picref);
148
    else
149
        avfilter_start_frame(link2, picref);
150
}
151

    
152
static void link_out_end_frame(AVFilterLink *link)
153
{
154
    AVFilterLink *link2 = get_extern_output_link(link);
155

    
156
    if(link2)
157
        avfilter_end_frame(link2);
158
}
159

    
160
static AVFilterPicRef *link_out_get_video_buffer(AVFilterLink *link, int perms)
161
{
162
    AVFilterLink *link2 = get_extern_output_link(link);
163

    
164
    if(!link2)
165
        return NULL;
166
    else
167
        return avfilter_get_video_buffer(link2, perms);
168
}
169

    
170
static void link_out_draw_slice(AVFilterLink *link, int y, int height)
171
{
172
    AVFilterLink *link2 = get_extern_output_link(link);
173

    
174
    if(link2)
175
        avfilter_draw_slice(link2, y, height);
176
}
177

    
178
/** dummy filter used to help export filters pads outside the graph */
179
static AVFilter vf_graph_dummy =
180
{
181
    .name      = "graph_dummy",
182
    .author    = "Bobby Bingham",
183

    
184
    .priv_size = sizeof(GraphLinkContext),
185

    
186
    .init      = link_init,
187

    
188
    .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
189
    .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
190
};
191

    
192
static AVFilterLink *get_intern_input_link(AVFilterLink *link)
193
{
194
    GraphContext *graph = link->dst->priv;
195
    return graph->link_filter->outputs[link->dstpad];
196
}
197

    
198
static void graph_in_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
199
{
200
    AVFilterLink *link2 = get_intern_input_link(link);
201
    if(link2)
202
        avfilter_start_frame(link2, picref);
203
}
204

    
205
static void graph_in_end_frame(AVFilterLink *link)
206
{
207
    AVFilterLink *link2 = get_intern_input_link(link);
208
    if(link2)
209
        avfilter_end_frame(link2);
210
}
211

    
212
static AVFilterPicRef *graph_in_get_video_buffer(AVFilterLink *link, int perms)
213
{
214
    AVFilterLink *link2 = get_intern_input_link(link);
215
    if(link2)
216
        return avfilter_get_video_buffer(link2, perms);
217
    return NULL;
218
}
219

    
220
static void graph_in_draw_slice(AVFilterLink *link, int y, int height)
221
{
222
    AVFilterLink *link2 = get_intern_input_link(link);
223
    if(link2)
224
        avfilter_draw_slice(link2, y, height);
225
}
226

    
227
static int *graph_in_query_formats(AVFilterLink *link)
228
{
229
    AVFilterLink *link2 = get_intern_input_link(link);
230

    
231
    if(!link2 || !link2->dst->input_pads[link2->dstpad].query_formats)
232
        return avfilter_make_format_list(0);
233
    return link2->dst->input_pads[link2->dstpad].query_formats(link2);
234
}
235

    
236
static int graph_in_config_props(AVFilterLink *link)
237
{
238
    AVFilterLink *link2 = get_intern_input_link(link);
239
    int (*config_props)(AVFilterLink *);
240

    
241
    if(!link2)
242
        return -1;
243

    
244
    /* copy link properties over to the dummy internal link */
245
    link2->w = link->w;
246
    link2->h = link->h;
247
    link2->format = link->format;
248

    
249
    if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
250
        return 0;   /* FIXME? */
251
        //config_props = avfilter_default_config_input_link;
252
    return config_props(link2);
253
}
254

    
255
static AVFilterLink *get_intern_output_link(AVFilterLink *link)
256
{
257
    GraphContext *graph = link->src->priv;
258
    return graph->link_filter->inputs[link->srcpad];
259
}
260

    
261
static int *graph_out_query_formats(AVFilterLink *link)
262
{
263
    AVFilterLink *link2 = get_intern_output_link(link);
264

    
265
    if(!link2)
266
        return avfilter_make_format_list(0);
267
    if(!link2->src->output_pads[link2->srcpad].query_formats)
268
        return avfilter_default_query_output_formats(link2);
269
    return link2->src->output_pads[link2->srcpad].query_formats(link2);
270
}
271

    
272
static int graph_out_request_frame(AVFilterLink *link)
273
{
274
    AVFilterLink *link2 = get_intern_output_link(link);
275

    
276
    if(link2)
277
        return avfilter_request_frame(link2);
278
    return -1;
279
}
280

    
281
static int graph_out_config_props(AVFilterLink *link)
282
{
283
    AVFilterLink *link2 = get_intern_output_link(link);
284
    int (*config_props)(AVFilterLink *);
285
    int ret;
286

    
287
    if(!link2)
288
        return 0;
289

    
290
    link2->w = link->w;
291
    link2->h = link->h;
292
    link2->format = link->format;
293

    
294
    if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
295
        config_props = avfilter_default_config_output_link;
296
    ret = config_props(link2);
297

    
298
    link->w = link2->w;
299
    link->h = link2->h;
300
    link->format = link2->format;
301

    
302
    return ret;
303
}
304

    
305
static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
306
                           char *name)
307
{
308
    GraphContext *graph = gctx->priv;
309

    
310
    AVFilterPad graph_inpad =
311
    {
312
        .name             = name,
313
        .type             = AV_PAD_VIDEO,
314
        .start_frame      = graph_in_start_frame,
315
        .end_frame        = graph_in_end_frame,
316
        .get_video_buffer = graph_in_get_video_buffer,
317
        .draw_slice       = graph_in_draw_slice,
318
        .query_formats    = graph_in_query_formats,
319
        .config_props     = graph_in_config_props,
320
        /* XXX */
321
    };
322
    AVFilterPad dummy_outpad =
323
    {
324
        .name          = NULL,          /* FIXME? */
325
        .type          = AV_PAD_VIDEO,
326
        .query_formats = link_in_query_formats,
327
        .request_frame = link_in_request_frame,
328
        .config_props  = link_in_config_props,
329
    };
330

    
331
    avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
332
    avfilter_insert_outpad(graph->link_filter, graph->link_filter->output_count,
333
                           &dummy_outpad);
334
    return avfilter_link(graph->link_filter,
335
                         graph->link_filter->output_count-1, filt, idx);
336
}
337

    
338
static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
339
                            char *name)
340
{
341
    GraphContext *graph = gctx->priv;
342

    
343
    AVFilterPad graph_outpad =
344
    {
345
        .name             = name,
346
        .type             = AV_PAD_VIDEO,
347
        .request_frame    = graph_out_request_frame,
348
        .query_formats    = graph_out_query_formats,
349
        .config_props     = graph_out_config_props,
350
    };
351
    AVFilterPad dummy_inpad =
352
    {
353
        .name             = NULL,          /* FIXME? */
354
        .type             = AV_PAD_VIDEO,
355
        .start_frame      = link_out_start_frame,
356
        .end_frame        = link_out_end_frame,
357
        .draw_slice       = link_out_draw_slice,
358
        .get_video_buffer = link_out_get_video_buffer,
359
        .query_formats    = link_out_query_formats,
360
        .config_props     = link_out_config_props,
361
    };
362

    
363
    avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
364
    avfilter_insert_inpad (graph->link_filter, graph->link_filter->input_count,
365
                           &dummy_inpad);
366
    return avfilter_link(filt, idx, graph->link_filter,
367
                         graph->link_filter->input_count-1);
368
}
369

    
370
static void uninit(AVFilterContext *ctx)
371
{
372
    GraphContext *graph = ctx->priv;
373

    
374
    if(graph->link_filter) {
375
        avfilter_destroy(graph->link_filter);
376
        graph->link_filter = NULL;
377
    }
378
    for(; graph->filter_count > 0; graph->filter_count --)
379
        avfilter_destroy(graph->filters[graph->filter_count - 1]);
380
    av_freep(&graph->filters);
381
}
382

    
383
/* TODO: insert in sorted order */
384
void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
385
{
386
    GraphContext *graph = graphctx->priv;
387

    
388
    graph->filters = av_realloc(graph->filters,
389
                                sizeof(AVFilterContext*) * ++graph->filter_count);
390
    graph->filters[graph->filter_count - 1] = filter;
391
}
392

    
393
/* search intelligently, once we insert in order */
394
AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
395
{
396
    GraphContext *graph = ctx->priv;
397
    int i;
398

    
399
    if(!name)
400
        return NULL;
401

    
402
    for(i = 0; i < graph->filter_count; i ++)
403
        if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
404
            return graph->filters[i];
405

    
406
    return NULL;
407
}
408

    
409
int avfilter_graph_config_links(AVFilterContext *graphctx)
410
{
411
    GraphContext *graph = graphctx->priv;
412
    int i, j;
413

    
414
    for(i = 0; i < graph->filter_count; i ++) {
415
        for(j = 0; j < graph->filters[i]->input_count; j ++) {
416
            /* ensure that graphs contained within graphs are configured */
417
            if((graph->filters[i]->filter == &avfilter_vf_graph     ||
418
                graph->filters[i]->filter == &avfilter_vf_graphfile ||
419
                graph->filters[i]->filter == &avfilter_vf_graphdesc) &&
420
                avfilter_graph_config_links(graph->filters[i]))
421
                return -1;
422
            if(avfilter_config_link(graph->filters[i]->inputs[j]))
423
                return -1;
424
        }
425
    }
426

    
427
    return 0;
428
}
429

    
430
static AVFilterContext *create_filter_with_args(const char *filt, void *opaque)
431
{
432
    AVFilterContext *ret;
433
    char *filter = av_strdup(filt); /* copy - don't mangle the input string */
434
    char *name, *args;
435

    
436
    name = filter;
437
    if((args = strchr(filter, '='))) {
438
        /* ensure we at least have a name */
439
        if(args == filter)
440
            goto fail;
441

    
442
        *args ++ = 0;
443
    }
444

    
445
    av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
446
           name, args ? args : "(none)");
447

    
448
    if((ret = avfilter_open(avfilter_get_by_name(name), NULL))) {
449
        if(avfilter_init_filter(ret, args, opaque)) {
450
            av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
451
            avfilter_destroy(ret);
452
            goto fail;
453
        }
454
    } else {
455
        av_log(NULL, AV_LOG_ERROR,
456
               "error creating filter \"%s\" with args \"%s\"\n",
457
               name, args ? args : "(none)");
458
        return NULL;
459
    }
460

    
461
    av_free(filter);
462

    
463
    return ret;
464

    
465
fail:
466
    av_free(filter);
467
    return NULL;
468
}
469

    
470
static int graph_load_chain(AVFilterContext *graphctx,
471
                              unsigned count, char **filter_list, void **opaque,
472
                              AVFilterContext **first, AVFilterContext **last)
473
{
474
    unsigned i;
475
    AVFilterContext *filters[2] = {NULL,NULL};
476

    
477
    for(i = 0; i < count; i ++) {
478
        void *op;
479

    
480
        if(opaque) op = opaque[i];
481
        else       op = NULL;
482

    
483
        if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
484
            goto fail;
485
        if(i == 0) {
486
            if(first) *first = filters[1];
487
        } else {
488
            if(avfilter_link(filters[0], 0, filters[1], 0)) {
489
                av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
490
                goto fail;
491
            }
492
        }
493
        avfilter_graph_add_filter(graphctx, filters[1]);
494
        if(i == 0 && filters[1]->input_count > 0)
495
            add_graph_input(graphctx, filters[1], 0, "default");
496
        filters[0] = filters[1];
497
    }
498

    
499
    if(filters[1]->output_count > 0)
500
        add_graph_output(graphctx, filters[1], 0, "default");
501

    
502
    if(last) *last = filters[1];
503
    return 0;
504

    
505
fail:
506
    uninit(graphctx);
507
    if(first) *first = NULL;
508
    if(last)  *last  = NULL;
509
    return -1;
510
}
511

    
512
static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
513
                                        AVFilterContext **first,
514
                                        AVFilterContext **last)
515
{
516
    int count, ret = 0;
517
    char **strings;
518
    char *filt;
519

    
520
    strings    = av_malloc(sizeof(char *));
521
    strings[0] = av_strdup(str);
522

    
523
    filt = strchr(strings[0], ',');
524
    for(count = 1; filt; count ++) {
525
        if(filt == strings[count-1]) {
526
            ret = -1;
527
            goto done;
528
        }
529

    
530
        strings = av_realloc(strings, sizeof(char *) * (count+1));
531
        strings[count] = filt + 1;
532
        *filt = '\0';
533
        filt = strchr(strings[count], ',');
534
    }
535

    
536
    ret = graph_load_chain(ctx, count, strings, NULL, first, last);
537

    
538
done:
539
    av_free(strings[0]);
540
    av_free(strings);
541

    
542
    return ret;
543
}
544

    
545
static int init(AVFilterContext *ctx, const char *args, void *opaque)
546
{
547
    GraphContext *gctx = ctx->priv;
548

    
549
    if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
550
        return -1;
551
    if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
552
        goto fail;
553

    
554
    if(!args)
555
        return 0;
556

    
557
    return graph_load_chain_from_string(ctx, args, NULL, NULL);
558

    
559
fail:
560
    avfilter_destroy(gctx->link_filter);
561
    return -1;
562
}
563

    
564
AVFilter avfilter_vf_graph =
565
{
566
    .name      = "graph",
567
    .author    = "Bobby Bingham",
568

    
569
    .priv_size = sizeof(GraphContext),
570

    
571
    .init      = init,
572
    .uninit    = uninit,
573

    
574
    .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
575
    .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
576
};
577

    
578
static int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc)
579
{
580
    AVFilterGraphDescFilter *curfilt;
581
    AVFilterGraphDescLink   *curlink;
582
    AVFilterGraphDescExport *curpad;
583
    AVFilterContext *filt, *filtb;
584

    
585
    AVFilter *filterdef;
586

    
587
    /* create all filters */
588
    for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
589
        if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
590
           !(filt = avfilter_open(filterdef, curfilt->name))) {
591
            av_log(ctx, AV_LOG_ERROR, "error creating filter\n");
592
            goto fail;
593
        }
594
        avfilter_graph_add_filter(ctx, filt);
595
        if(avfilter_init_filter(filt, curfilt->args, NULL)) {
596
            av_log(ctx, AV_LOG_ERROR, "error initializing filter\n");
597
            goto fail;
598
        }
599
    }
600

    
601
    /* create all links */
602
    for(curlink = desc->links; curlink; curlink = curlink->next) {
603
        if(!(filt = avfilter_graph_get_filter(ctx, curlink->src))) {
604
            av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
605
            goto fail;
606
        }
607
        if(!(filtb = avfilter_graph_get_filter(ctx, curlink->dst))) {
608
            av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
609
            goto fail;
610
        }
611
        if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
612
            av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
613
            goto fail;
614
        }
615
    }
616

    
617
    /* export all input pads */
618
    for(curpad = desc->inputs; curpad; curpad = curpad->next) {
619
        if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
620
            av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
621
            goto fail;
622
        }
623
        add_graph_input(ctx, filt, curpad->pad, curpad->name);
624
    }
625

    
626
    /* export all output pads */
627
    for(curpad = desc->outputs; curpad; curpad = curpad->next) {
628
        if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
629
            av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
630
            goto fail;
631
        }
632
        add_graph_output(ctx, filt, curpad->pad, curpad->name);
633
    }
634

    
635
    return 0;
636

    
637
fail:
638
    uninit(ctx);
639
    return -1;
640
}
641

    
642
static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
643
{
644
    GraphContext *gctx = ctx->priv;
645

    
646
    if(!opaque)
647
        return -1;
648

    
649
    if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
650
        return -1;
651
    if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
652
        goto fail;
653

    
654
    return graph_load_from_desc(ctx, opaque);
655

    
656
fail:
657
    avfilter_destroy(gctx->link_filter);
658
    return -1;
659
}
660

    
661
AVFilter avfilter_vf_graphdesc =
662
{
663
    .name      = "graph_desc",
664
    .author    = "Bobby Bingham",
665

    
666
    .priv_size = sizeof(GraphContext),
667

    
668
    .init      = init_desc,
669
    .uninit    = uninit,
670

    
671
    .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
672
    .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
673
};
674

    
675
static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
676
{
677
    AVFilterGraphDesc *desc;
678
    int ret;
679

    
680
    if(!args)
681
        return -1;
682
    if(!(desc = avfilter_graph_load_desc(args)))
683
        return -1;
684

    
685
    ret = init_desc(ctx, NULL, desc);
686
    avfilter_graph_free_desc(desc);
687
    return ret;
688
}
689

    
690
AVFilter avfilter_vf_graphfile =
691
{
692
    .name      = "graph_file",
693
    .author    = "Bobby Bingham",
694

    
695
    .priv_size = sizeof(GraphContext),
696

    
697
    .init      = init_file,
698
    .uninit    = uninit,
699

    
700
    .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
701
    .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
702
};
703