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