Nits: more punctuation and capitalization
[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
73 static int link_in_poll_frame(AVFilterLink *link)
74 {
75     AVFilterLink *link2 = get_extern_input_link(link);
76     if(!link2)
77         return -1;
78     return avfilter_poll_frame(link2);
79 }
80
81 static int link_in_config_props(AVFilterLink *link)
82 {
83     AVFilterLink *link2 = get_extern_input_link(link);
84     int (*config_props)(AVFilterLink *);
85     int ret;
86
87     if(!link2)
88         return -1;
89     if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
90         config_props = avfilter_default_config_output_link;
91     ret = config_props(link2);
92
93     link->w = link2->w;
94     link->h = link2->h;
95
96     return ret;
97 }
98
99 /**
100  * Given the link between the dummy filter and an internal filter whose input
101  * is being exported outside the graph, this returns the externally visible
102  * link.
103  */
104 static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
105 {
106     GraphLinkContext *lctx = link->dst->priv;
107     return lctx->graph->outputs[link->dstpad];
108 }
109
110 static int link_out_config_props(AVFilterLink *link)
111 {
112     AVFilterLink *link2 = get_extern_output_link(link);
113
114     if(!link2)
115         return 0;
116
117     link2->w = link->w;
118     link2->h = link->h;
119
120     return 0;
121 }
122
123 static void link_out_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
124 {
125     AVFilterLink *link2 = get_extern_output_link(link);
126
127     if(!link2)
128         avfilter_unref_pic(picref);
129     else
130         avfilter_start_frame(link2, picref);
131 }
132
133 static void link_out_end_frame(AVFilterLink *link)
134 {
135     AVFilterLink *link2 = get_extern_output_link(link);
136
137     if(link2)
138         avfilter_end_frame(link2);
139 }
140
141 static AVFilterPicRef *link_out_get_video_buffer(AVFilterLink *link, int perms)
142 {
143     AVFilterLink *link2 = get_extern_output_link(link);
144
145     if(!link2)
146         return NULL;
147     else
148         return avfilter_get_video_buffer(link2, perms);
149 }
150
151 static void link_out_draw_slice(AVFilterLink *link, int y, int height)
152 {
153     AVFilterLink *link2 = get_extern_output_link(link);
154
155     if(link2)
156         avfilter_draw_slice(link2, y, height);
157 }
158
159 /** dummy filter used to help export filters pads outside the graph */
160 static AVFilter vf_graph_dummy =
161 {
162     .name      = "graph_dummy",
163     .author    = "Bobby Bingham",
164
165     .priv_size = sizeof(GraphLinkContext),
166
167     .init      = link_init,
168
169     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
170     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
171 };
172
173 static AVFilterLink *get_intern_input_link(AVFilterLink *link)
174 {
175     GraphContext *graph = link->dst->priv;
176     return graph->link_filter_in->outputs[link->dstpad];
177 }
178
179 static void graph_in_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
180 {
181     AVFilterLink *link2 = get_intern_input_link(link);
182     if(link2)
183         avfilter_start_frame(link2, picref);
184 }
185
186 static void graph_in_end_frame(AVFilterLink *link)
187 {
188     AVFilterLink *link2 = get_intern_input_link(link);
189     if(link2)
190         avfilter_end_frame(link2);
191 }
192
193 static AVFilterPicRef *graph_in_get_video_buffer(AVFilterLink *link, int perms)
194 {
195     AVFilterLink *link2 = get_intern_input_link(link);
196     if(link2)
197         return avfilter_get_video_buffer(link2, perms);
198     return NULL;
199 }
200
201 static void graph_in_draw_slice(AVFilterLink *link, int y, int height)
202 {
203     AVFilterLink *link2 = get_intern_input_link(link);
204     if(link2)
205         avfilter_draw_slice(link2, y, height);
206 }
207
208 static int graph_in_config_props(AVFilterLink *link)
209 {
210     AVFilterLink *link2 = get_intern_input_link(link);
211     int (*config_props)(AVFilterLink *);
212     int ret;
213
214     if(!link2)
215         return -1;
216
217     /* copy link properties over to the dummy internal link */
218     link2->w = link->w;
219     link2->h = link->h;
220     link2->format = link->format;
221
222     if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
223         return 0;   /* FIXME? */
224         //config_props = avfilter_default_config_input_link;
225     if(!(ret = config_props(link2)))
226         link2->init_state = AVLINK_INIT;
227     else
228         link2->init_state = AVLINK_STARTINIT;
229
230     return ret;
231 }
232
233 static AVFilterLink *get_intern_output_link(AVFilterLink *link)
234 {
235     GraphContext *graph = link->src->priv;
236     return graph->link_filter_out->inputs[link->srcpad];
237 }
238
239 static int graph_out_request_frame(AVFilterLink *link)
240 {
241     AVFilterLink *link2 = get_intern_output_link(link);
242
243     if(link2)
244         return avfilter_request_frame(link2);
245     return -1;
246 }
247
248 static int graph_out_poll_frame(AVFilterLink *link)
249 {
250     AVFilterLink *link2 = get_intern_output_link(link);
251
252     if(!link2)
253         return -1;
254
255     return avfilter_poll_frame(link2);
256 }
257
258 static int graph_out_config_props(AVFilterLink *link)
259 {
260     GraphContext *graph = link->src->priv;
261     AVFilterLink *link2 = graph->link_filter_out->inputs[link->srcpad];
262     int ret;
263
264     if((ret = avfilter_config_links(graph->link_filter_out)))
265         return ret;
266
267     if(!link2)
268         return 0;
269
270     link->w = link2->w;
271     link->h = link2->h;
272     link->format = link2->format;
273
274     return 0;
275 }
276
277 static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
278                            char *name)
279 {
280     GraphContext *graph = gctx->priv;
281
282     AVFilterPad graph_inpad =
283     {
284         .name             = name,
285         .type             = AV_PAD_VIDEO,
286         .start_frame      = graph_in_start_frame,
287         .end_frame        = graph_in_end_frame,
288         .get_video_buffer = graph_in_get_video_buffer,
289         .draw_slice       = graph_in_draw_slice,
290         .config_props     = graph_in_config_props,
291         /* XXX */
292     };
293     AVFilterPad dummy_outpad =
294     {
295         .name          = NULL,          /* FIXME? */
296         .type          = AV_PAD_VIDEO,
297         .request_frame = link_in_request_frame,
298         .poll_frame    = link_in_poll_frame,
299         .config_props  = link_in_config_props,
300     };
301
302     avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
303     avfilter_insert_outpad(graph->link_filter_in, graph->link_filter_in->output_count,
304                            &dummy_outpad);
305     return avfilter_link(graph->link_filter_in,
306                          graph->link_filter_in->output_count-1, filt, idx);
307 }
308
309 static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
310                             char *name)
311 {
312     GraphContext *graph = gctx->priv;
313
314     AVFilterPad graph_outpad =
315     {
316         .name             = name,
317         .type             = AV_PAD_VIDEO,
318         .request_frame    = graph_out_request_frame,
319         .poll_frame       = graph_out_poll_frame,
320         .config_props     = graph_out_config_props,
321     };
322     AVFilterPad dummy_inpad =
323     {
324         .name             = NULL,          /* FIXME? */
325         .type             = AV_PAD_VIDEO,
326         .start_frame      = link_out_start_frame,
327         .end_frame        = link_out_end_frame,
328         .draw_slice       = link_out_draw_slice,
329         .get_video_buffer = link_out_get_video_buffer,
330         .config_props     = link_out_config_props,
331     };
332
333     avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
334     avfilter_insert_inpad (graph->link_filter_out, graph->link_filter_out->input_count,
335                            &dummy_inpad);
336     return avfilter_link(filt, idx, graph->link_filter_out,
337                          graph->link_filter_out->input_count-1);
338 }
339
340 static void uninit(AVFilterContext *ctx)
341 {
342     GraphContext *graph = ctx->priv;
343
344     if(graph->link_filter_in) {
345         avfilter_destroy(graph->link_filter_in);
346         graph->link_filter_in = NULL;
347     }
348     if(graph->link_filter_out) {
349         avfilter_destroy(graph->link_filter_out);
350         graph->link_filter_out = NULL;
351     }
352     for(; graph->filter_count > 0; graph->filter_count --)
353         avfilter_destroy(graph->filters[graph->filter_count - 1]);
354     av_freep(&graph->filters);
355 }
356
357 /* TODO: insert in sorted order */
358 void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
359 {
360     GraphContext *graph = graphctx->priv;
361
362     graph->filters = av_realloc(graph->filters,
363                                 sizeof(AVFilterContext*) * ++graph->filter_count);
364     graph->filters[graph->filter_count - 1] = filter;
365 }
366
367 /* search intelligently, once we insert in order */
368 AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
369 {
370     GraphContext *graph = ctx->priv;
371     int i;
372
373     if(!name)
374         return NULL;
375
376     for(i = 0; i < graph->filter_count; i ++)
377         if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
378             return graph->filters[i];
379
380     return NULL;
381 }
382
383 static int query_formats(AVFilterContext *graphctx)
384 {
385     GraphContext *graph = graphctx->priv;
386     AVFilterContext *linkfiltin  = graph->link_filter_in;
387     AVFilterContext *linkfiltout = graph->link_filter_out;
388     int i, j;
389
390     /* ask all the sub-filters for their supported colorspaces */
391     for(i = 0; i < graph->filter_count; i ++) {
392         if(graph->filters[i]->filter->query_formats)
393             graph->filters[i]->filter->query_formats(graph->filters[i]);
394         else
395             avfilter_default_query_formats(graph->filters[i]);
396     }
397
398     /* use these formats on our exported links */
399     for(i = 0; i < linkfiltout->input_count; i ++) {
400         avfilter_formats_ref( linkfiltout->inputs[i]->in_formats,
401                              &linkfiltout->inputs[i]->out_formats);
402
403         if(graphctx->outputs[i])
404             avfilter_formats_ref(linkfiltout->inputs[i]->in_formats,
405                                  &graphctx->outputs[i]->in_formats);
406     }
407     for(i = 0; i < linkfiltin->output_count; i ++) {
408         avfilter_formats_ref( linkfiltin->outputs[i]->out_formats,
409                              &linkfiltin->outputs[i]->in_formats);
410
411         if(graphctx->inputs[i])
412             avfilter_formats_ref(linkfiltin->outputs[i]->out_formats,
413                                  &graphctx-> inputs[i]->out_formats);
414     }
415
416     /* go through and merge as many format lists as possible */
417     for(i = 0; i < graph->filter_count; i ++) {
418         AVFilterContext *filter = graph->filters[i];
419
420         for(j = 0; j < filter->input_count; j ++) {
421             AVFilterLink *link;
422             if(!(link = filter->inputs[j]))
423                 continue;
424             if(link->in_formats != link->out_formats) {
425                 if(!avfilter_merge_formats(link->in_formats,
426                                            link->out_formats)) {
427                     /* couldn't merge format lists. auto-insert scale filter */
428                     AVFilterContext *scale;
429
430                     if(!(scale = avfilter_open(&avfilter_vf_scale, NULL)))
431                         return -1;
432                     if(scale->filter->init(scale, NULL, NULL) ||
433                        avfilter_insert_filter(link, scale, 0, 0)) {
434                         avfilter_destroy(scale);
435                         return -1;
436                     }
437
438                     avfilter_graph_add_filter(graphctx, scale);
439                     scale->filter->query_formats(scale);
440                     if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
441                                               scale-> inputs[0]->out_formats) ||
442                        !avfilter_merge_formats(scale->outputs[0]->in_formats,
443                                               scale->outputs[0]->out_formats))
444                         return -1;
445                 }
446             }
447         }
448     }
449
450     return 0;
451 }
452
453 static void pick_format(AVFilterLink *link)
454 {
455     if(!link || !link->in_formats)
456         return;
457
458     link->in_formats->format_count = 1;
459     link->format = link->in_formats->formats[0];
460
461     avfilter_formats_unref(&link->in_formats);
462     avfilter_formats_unref(&link->out_formats);
463 }
464
465 static void pick_formats(GraphContext *graph)
466 {
467     int i, j;
468
469     for(i = 0; i < graph->filter_count; i ++) {
470         AVFilterContext *filter = graph->filters[i];
471
472         if(filter->filter == &avfilter_vf_graph     ||
473            filter->filter == &avfilter_vf_graphfile ||
474            filter->filter == &avfilter_vf_graphdesc)
475             pick_formats(filter->priv);
476
477         for(j = 0; j < filter->input_count; j ++)
478             pick_format(filter->inputs[j]);
479         for(j = 0; j < filter->output_count; j ++)
480             pick_format(filter->outputs[j]);
481     }
482 }
483
484 int avfilter_graph_config_formats(AVFilterContext *graphctx)
485 {
486     GraphContext *graph = graphctx->priv;
487
488     /* find supported formats from sub-filters, and merge along links */
489     if(query_formats(graphctx))
490         return -1;
491
492     /* Once everything is merged, it's possible that we'll still have
493      * multiple valid choices of colorspace. We pick the first one. */
494     pick_formats(graph);
495
496     return 0;
497 }
498
499 static int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc)
500 {
501     AVFilterGraphDescFilter *curfilt;
502     AVFilterGraphDescLink   *curlink;
503     AVFilterGraphDescExport *curpad;
504     AVFilterContext *filt, *filtb;
505
506     AVFilter *filterdef;
507
508     /* create all filters */
509     for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
510         if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
511            !(filt = avfilter_open(filterdef, curfilt->name))) {
512             av_log(ctx, AV_LOG_ERROR,
513                "error creating filter '%s'\n", curfilt->name);
514             goto fail;
515         }
516         avfilter_graph_add_filter(ctx, filt);
517         if(avfilter_init_filter(filt, curfilt->args, NULL)) {
518             av_log(ctx, AV_LOG_ERROR,
519                 "error initializing filter '%s'\n", curfilt->name);
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