]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavfilter/avfiltergraph.c
Use separate fake filters for exporting inputs and outputs from filter graphs.
[frescor/ffmpeg.git] / libavfilter / avfiltergraph.c
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 filters to handle links to internal filters */
36     AVFilterContext *link_filter_in;
37     AVFilterContext *link_filter_out;
38 } GraphContext;
39
40 typedef struct {
41     AVFilterContext *graph;
42 } GraphLinkContext;
43
44 static int link_init(AVFilterContext *ctx, const char *args, void *opaque)
45 {
46     GraphLinkContext *linkctx = ctx->priv;
47     linkctx->graph = opaque;
48     return !opaque;
49 }
50
51 /**
52  * Given the link between the dummy filter and an internal filter whose input
53  * is being exported outside the graph, this returns the externally visible
54  * link
55  */
56 static inline AVFilterLink *get_extern_input_link(AVFilterLink *link)
57 {
58     GraphLinkContext *lctx = link->src->priv;
59     return lctx->graph->inputs[link->srcpad];
60 }
61
62 /** request a frame from a filter providing input to the graph */
63 static int link_in_request_frame(AVFilterLink *link)
64 {
65     AVFilterLink *link2 = get_extern_input_link(link);
66
67     if(!link2)
68         return -1;
69     return avfilter_request_frame(link2);
70 }
71
72 static int link_in_config_props(AVFilterLink *link)
73 {
74     AVFilterLink *link2 = get_extern_input_link(link);
75     int (*config_props)(AVFilterLink *);
76     int ret;
77
78     if(!link2)
79         return -1;
80     if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
81         config_props = avfilter_default_config_output_link;
82     ret = config_props(link2);
83
84     link->w = link2->w;
85     link->h = link2->h;
86
87     return ret;
88 }
89
90 /**
91  * Given the link between the dummy filter and an internal filter whose input
92  * is being exported outside the graph, this returns the externally visible
93  * link
94  */
95 static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
96 {
97     GraphLinkContext *lctx = link->dst->priv;
98     return lctx->graph->outputs[link->dstpad];
99 }
100
101 static int link_out_config_props(AVFilterLink *link)
102 {
103     AVFilterLink *link2 = get_extern_output_link(link);
104     int (*config_props)(AVFilterLink *);
105
106     if(!link2)
107         return 0;
108
109     link2->w = link->w;
110     link2->h = link->h;
111
112     if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
113         config_props = avfilter_default_config_input_link;
114     return config_props(link2);
115 }
116
117 static void link_out_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
118 {
119     AVFilterLink *link2 = get_extern_output_link(link);
120
121     if(!link2)
122         avfilter_unref_pic(picref);
123     else
124         avfilter_start_frame(link2, picref);
125 }
126
127 static void link_out_end_frame(AVFilterLink *link)
128 {
129     AVFilterLink *link2 = get_extern_output_link(link);
130
131     if(link2)
132         avfilter_end_frame(link2);
133 }
134
135 static AVFilterPicRef *link_out_get_video_buffer(AVFilterLink *link, int perms)
136 {
137     AVFilterLink *link2 = get_extern_output_link(link);
138
139     if(!link2)
140         return NULL;
141     else
142         return avfilter_get_video_buffer(link2, perms);
143 }
144
145 static void link_out_draw_slice(AVFilterLink *link, int y, int height)
146 {
147     AVFilterLink *link2 = get_extern_output_link(link);
148
149     if(link2)
150         avfilter_draw_slice(link2, y, height);
151 }
152
153 /** dummy filter used to help export filters pads outside the graph */
154 static AVFilter vf_graph_dummy =
155 {
156     .name      = "graph_dummy",
157     .author    = "Bobby Bingham",
158
159     .priv_size = sizeof(GraphLinkContext),
160
161     .init      = link_init,
162
163     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
164     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
165 };
166
167 static AVFilterLink *get_intern_input_link(AVFilterLink *link)
168 {
169     GraphContext *graph = link->dst->priv;
170     return graph->link_filter_in->outputs[link->dstpad];
171 }
172
173 static void graph_in_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
174 {
175     AVFilterLink *link2 = get_intern_input_link(link);
176     if(link2)
177         avfilter_start_frame(link2, picref);
178 }
179
180 static void graph_in_end_frame(AVFilterLink *link)
181 {
182     AVFilterLink *link2 = get_intern_input_link(link);
183     if(link2)
184         avfilter_end_frame(link2);
185 }
186
187 static AVFilterPicRef *graph_in_get_video_buffer(AVFilterLink *link, int perms)
188 {
189     AVFilterLink *link2 = get_intern_input_link(link);
190     if(link2)
191         return avfilter_get_video_buffer(link2, perms);
192     return NULL;
193 }
194
195 static void graph_in_draw_slice(AVFilterLink *link, int y, int height)
196 {
197     AVFilterLink *link2 = get_intern_input_link(link);
198     if(link2)
199         avfilter_draw_slice(link2, y, height);
200 }
201
202 static int graph_in_config_props(AVFilterLink *link)
203 {
204     AVFilterLink *link2 = get_intern_input_link(link);
205     int (*config_props)(AVFilterLink *);
206
207     if(!link2)
208         return -1;
209
210     /* copy link properties over to the dummy internal link */
211     link2->w = link->w;
212     link2->h = link->h;
213     link2->format = link->format;
214
215     if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
216         return 0;   /* FIXME? */
217         //config_props = avfilter_default_config_input_link;
218     return config_props(link2);
219 }
220
221 static AVFilterLink *get_intern_output_link(AVFilterLink *link)
222 {
223     GraphContext *graph = link->src->priv;
224     return graph->link_filter_out->inputs[link->srcpad];
225 }
226
227 static int graph_out_request_frame(AVFilterLink *link)
228 {
229     AVFilterLink *link2 = get_intern_output_link(link);
230
231     if(link2)
232         return avfilter_request_frame(link2);
233     return -1;
234 }
235
236 static int graph_out_config_props(AVFilterLink *link)
237 {
238     AVFilterLink *link2 = get_intern_output_link(link);
239     int (*config_props)(AVFilterLink *);
240     int ret;
241
242     if(!link2)
243         return 0;
244
245     link2->w = link->w;
246     link2->h = link->h;
247     link2->format = link->format;
248
249     if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
250         config_props = avfilter_default_config_output_link;
251     ret = config_props(link2);
252
253     link->w = link2->w;
254     link->h = link2->h;
255     link->format = link2->format;
256
257     return ret;
258 }
259
260 static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
261                            char *name)
262 {
263     GraphContext *graph = gctx->priv;
264
265     AVFilterPad graph_inpad =
266     {
267         .name             = name,
268         .type             = AV_PAD_VIDEO,
269         .start_frame      = graph_in_start_frame,
270         .end_frame        = graph_in_end_frame,
271         .get_video_buffer = graph_in_get_video_buffer,
272         .draw_slice       = graph_in_draw_slice,
273         .config_props     = graph_in_config_props,
274         /* XXX */
275     };
276     AVFilterPad dummy_outpad =
277     {
278         .name          = NULL,          /* FIXME? */
279         .type          = AV_PAD_VIDEO,
280         .request_frame = link_in_request_frame,
281         .config_props  = link_in_config_props,
282     };
283
284     avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
285     avfilter_insert_outpad(graph->link_filter_in, graph->link_filter_in->output_count,
286                            &dummy_outpad);
287     return avfilter_link(graph->link_filter_in,
288                          graph->link_filter_in->output_count-1, filt, idx);
289 }
290
291 static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
292                             char *name)
293 {
294     GraphContext *graph = gctx->priv;
295
296     AVFilterPad graph_outpad =
297     {
298         .name             = name,
299         .type             = AV_PAD_VIDEO,
300         .request_frame    = graph_out_request_frame,
301         .config_props     = graph_out_config_props,
302     };
303     AVFilterPad dummy_inpad =
304     {
305         .name             = NULL,          /* FIXME? */
306         .type             = AV_PAD_VIDEO,
307         .start_frame      = link_out_start_frame,
308         .end_frame        = link_out_end_frame,
309         .draw_slice       = link_out_draw_slice,
310         .get_video_buffer = link_out_get_video_buffer,
311         .config_props     = link_out_config_props,
312     };
313
314     avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
315     avfilter_insert_inpad (graph->link_filter_out, graph->link_filter_out->input_count,
316                            &dummy_inpad);
317     return avfilter_link(filt, idx, graph->link_filter_out,
318                          graph->link_filter_out->input_count-1);
319 }
320
321 static void uninit(AVFilterContext *ctx)
322 {
323     GraphContext *graph = ctx->priv;
324
325     if(graph->link_filter_in) {
326         avfilter_destroy(graph->link_filter_in);
327         graph->link_filter_in = NULL;
328     }
329     if(graph->link_filter_out) {
330         avfilter_destroy(graph->link_filter_out);
331         graph->link_filter_out = NULL;
332     }
333     for(; graph->filter_count > 0; graph->filter_count --)
334         avfilter_destroy(graph->filters[graph->filter_count - 1]);
335     av_freep(&graph->filters);
336 }
337
338 /* TODO: insert in sorted order */
339 void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
340 {
341     GraphContext *graph = graphctx->priv;
342
343     graph->filters = av_realloc(graph->filters,
344                                 sizeof(AVFilterContext*) * ++graph->filter_count);
345     graph->filters[graph->filter_count - 1] = filter;
346 }
347
348 /* search intelligently, once we insert in order */
349 AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
350 {
351     GraphContext *graph = ctx->priv;
352     int i;
353
354     if(!name)
355         return NULL;
356
357     for(i = 0; i < graph->filter_count; i ++)
358         if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
359             return graph->filters[i];
360
361     return NULL;
362 }
363
364 static int query_formats(AVFilterContext *graphctx)
365 {
366     GraphContext *graph = graphctx->priv;
367     AVFilterContext *linkfiltin  = graph->link_filter_in;
368     AVFilterContext *linkfiltout = graph->link_filter_out;
369     int i, j;
370
371     /* ask all the sub-filters for their supported colorspaces */
372     for(i = 0; i < graph->filter_count; i ++) {
373         if(graph->filters[i]->filter->query_formats)
374             graph->filters[i]->filter->query_formats(graph->filters[i]);
375         else
376             avfilter_default_query_formats(graph->filters[i]);
377     }
378
379     /* use these formats on our exported links */
380     for(i = 0; i < linkfiltout->input_count; i ++) {
381         avfilter_formats_ref( linkfiltout->inputs[i]->in_formats,
382                              &linkfiltout->inputs[i]->out_formats);
383
384         if(graphctx->outputs[i])
385             avfilter_formats_ref(linkfiltout->inputs[i]->in_formats,
386                                  &graphctx->outputs[i]->in_formats);
387     }
388     for(i = 0; i < linkfiltin->output_count; i ++) {
389         avfilter_formats_ref( linkfiltin->outputs[i]->out_formats,
390                              &linkfiltin->outputs[i]->in_formats);
391
392         if(graphctx->inputs[i])
393             avfilter_formats_ref(linkfiltin->outputs[i]->out_formats,
394                                  &graphctx-> inputs[i]->out_formats);
395     }
396
397     /* go through and merge as many format lists as possible */
398     for(i = 0; i < graph->filter_count; i ++) {
399         AVFilterContext *filter = graph->filters[i];
400
401         for(j = 0; j < filter->input_count; j ++) {
402             AVFilterLink *link;
403             if(!(link = filter->inputs[j]))
404                 continue;
405             if(link->in_formats != link->out_formats) {
406                 if(!avfilter_merge_formats(link->in_formats,
407                                            link->out_formats)) {
408                     /* couldn't merge format lists. auto-insert scale filter */
409                     AVFilterContext *scale;
410
411                     if(!(scale = avfilter_open(&avfilter_vf_scale, NULL)))
412                         return -1;
413                     if(scale->filter->init(scale, NULL, NULL) ||
414                        avfilter_insert_filter(link, scale, 0, 0)) {
415                         avfilter_destroy(scale);
416                         return -1;
417                     }
418
419                     avfilter_graph_add_filter(graphctx, scale);
420                     scale->filter->query_formats(scale);
421                     if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
422                                               scale-> inputs[0]->out_formats) ||
423                        !avfilter_merge_formats(scale->outputs[0]->in_formats,
424                                               scale->outputs[0]->out_formats))
425                         return -1;
426                 }
427             }
428         }
429     }
430
431     return 0;
432 }
433
434 static void pick_format(AVFilterLink *link)
435 {
436     if(!link || !link->in_formats)
437         return;
438
439     link->in_formats->format_count = 1;
440     link->format = link->in_formats->formats[0];
441
442     avfilter_formats_unref(&link->in_formats);
443     avfilter_formats_unref(&link->out_formats);
444 }
445
446 static void pick_formats(GraphContext *graph)
447 {
448     int i, j;
449
450     for(i = 0; i < graph->filter_count; i ++) {
451         AVFilterContext *filter = graph->filters[i];
452
453         if(filter->filter == &avfilter_vf_graph     ||
454            filter->filter == &avfilter_vf_graphfile ||
455            filter->filter == &avfilter_vf_graphdesc)
456             pick_formats(filter->priv);
457
458         for(j = 0; j < filter->input_count; j ++)
459             pick_format(filter->inputs[j]);
460         for(j = 0; j < filter->output_count; j ++)
461             pick_format(filter->outputs[j]);
462     }
463 }
464
465 int avfilter_graph_config_formats(AVFilterContext *graphctx)
466 {
467     GraphContext *graph = graphctx->priv;
468
469     /* Find supported formats from sub-filters, and merge along links */
470     if(query_formats(graphctx))
471         return -1;
472
473     /* Once everything is merged, it's possible that we'll still have
474      * multiple valid choices of colorspace. We pick the first one. */
475     pick_formats(graph);
476
477     return 0;
478 }
479
480 int avfilter_graph_config_links(AVFilterContext *graphctx)
481 {
482     GraphContext *graph = graphctx->priv;
483     int i, j;
484
485     for(i = 0; i < graph->filter_count; i ++) {
486         for(j = 0; j < graph->filters[i]->input_count; j ++) {
487             /* ensure that graphs contained within graphs are configured */
488             if((graph->filters[i]->filter == &avfilter_vf_graph     ||
489                 graph->filters[i]->filter == &avfilter_vf_graphfile ||
490                 graph->filters[i]->filter == &avfilter_vf_graphdesc) &&
491                 avfilter_graph_config_links(graph->filters[i]))
492                 return -1;
493             if(avfilter_config_link(graph->filters[i]->inputs[j]))
494                 return -1;
495         }
496     }
497
498     return 0;
499 }
500
501 static int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc)
502 {
503     AVFilterGraphDescFilter *curfilt;
504     AVFilterGraphDescLink   *curlink;
505     AVFilterGraphDescExport *curpad;
506     AVFilterContext *filt, *filtb;
507
508     AVFilter *filterdef;
509
510     /* create all filters */
511     for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
512         if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
513            !(filt = avfilter_open(filterdef, curfilt->name))) {
514             av_log(ctx, AV_LOG_ERROR, "error creating filter\n");
515             goto fail;
516         }
517         avfilter_graph_add_filter(ctx, filt);
518         if(avfilter_init_filter(filt, curfilt->args, NULL)) {
519             av_log(ctx, AV_LOG_ERROR, "error initializing filter\n");
520             goto fail;
521         }
522     }
523
524     /* create all links */
525     for(curlink = desc->links; curlink; curlink = curlink->next) {
526         if(!(filt = avfilter_graph_get_filter(ctx, curlink->src))) {
527             av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
528             goto fail;
529         }
530         if(!(filtb = avfilter_graph_get_filter(ctx, curlink->dst))) {
531             av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
532             goto fail;
533         }
534         if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
535             av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
536             goto fail;
537         }
538     }
539
540     /* export all input pads */
541     for(curpad = desc->inputs; curpad; curpad = curpad->next) {
542         if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
543             av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
544             goto fail;
545         }
546         add_graph_input(ctx, filt, curpad->pad, curpad->name);
547     }
548
549     /* export all output pads */
550     for(curpad = desc->outputs; curpad; curpad = curpad->next) {
551         if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
552             av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
553             goto fail;
554         }
555         add_graph_output(ctx, filt, curpad->pad, curpad->name);
556     }
557
558     return 0;
559
560 fail:
561     uninit(ctx);
562     return -1;
563 }
564
565 static int init(AVFilterContext *ctx, const char *args, void *opaque)
566 {
567     GraphContext *gctx = ctx->priv;
568     AVFilterGraphDesc *desc;
569     int ret;
570
571     if(!(gctx->link_filter_in = avfilter_open(&vf_graph_dummy, NULL)))
572         return -1;
573     if(avfilter_init_filter(gctx->link_filter_in, NULL, ctx))
574         goto fail;
575     if(!(gctx->link_filter_out = avfilter_open(&vf_graph_dummy, NULL)))
576         goto fail;
577     if(avfilter_init_filter(gctx->link_filter_out, NULL, ctx))
578         goto fail;
579
580     if(!args)
581         return 0;
582
583     if(!(desc = avfilter_graph_parse_chain(args)))
584         goto fail;
585
586     ret = graph_load_from_desc(ctx, desc);
587     avfilter_graph_free_desc(desc);
588     return ret;
589
590 fail:
591     avfilter_destroy(gctx->link_filter_in);
592     if(gctx->link_filter_out)
593         avfilter_destroy(gctx->link_filter_out);
594     return -1;
595 }
596
597 AVFilter avfilter_vf_graph =
598 {
599     .name      = "graph",
600     .author    = "Bobby Bingham",
601
602     .priv_size = sizeof(GraphContext),
603
604     .init      = init,
605     .uninit    = uninit,
606
607     .query_formats = query_formats,
608
609     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
610     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
611 };
612
613 static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
614 {
615     GraphContext *gctx = ctx->priv;
616
617     if(!opaque)
618         return -1;
619
620     if(!(gctx->link_filter_in = avfilter_open(&vf_graph_dummy, NULL)))
621         return -1;
622     if(avfilter_init_filter(gctx->link_filter_in, NULL, ctx))
623         goto fail;
624     if(!(gctx->link_filter_out = avfilter_open(&vf_graph_dummy, NULL)))
625         goto fail;
626     if(avfilter_init_filter(gctx->link_filter_out, NULL, ctx))
627         goto fail;
628
629     return graph_load_from_desc(ctx, opaque);
630
631 fail:
632     avfilter_destroy(gctx->link_filter_in);
633     if(gctx->link_filter_out)
634         avfilter_destroy(gctx->link_filter_out);
635     return -1;
636 }
637
638 AVFilter avfilter_vf_graphdesc =
639 {
640     .name      = "graph_desc",
641     .author    = "Bobby Bingham",
642
643     .priv_size = sizeof(GraphContext),
644
645     .init      = init_desc,
646     .uninit    = uninit,
647
648     .query_formats = query_formats,
649
650     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
651     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
652 };
653
654 static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
655 {
656     AVFilterGraphDesc *desc;
657     int ret;
658
659     if(!args)
660         return -1;
661     if(!(desc = avfilter_graph_load_desc(args)))
662         return -1;
663
664     ret = init_desc(ctx, NULL, desc);
665     avfilter_graph_free_desc(desc);
666     return ret;
667 }
668
669 AVFilter avfilter_vf_graphfile =
670 {
671     .name      = "graph_file",
672     .author    = "Bobby Bingham",
673
674     .priv_size = sizeof(GraphContext),
675
676     .init      = init_file,
677     .uninit    = uninit,
678
679     .query_formats = query_formats,
680
681     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
682     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
683 };
684