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