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