Use a filter graph description for creating simple chain graphs so we
[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 filter to handle links to internal filters */
36     AVFilterContext *link_filter;
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 static int link_in_config_props(AVFilterLink *link)
72 {
73     AVFilterLink *link2 = get_extern_input_link(link);
74     int (*config_props)(AVFilterLink *);
75     int ret;
76
77     if(!link2)
78         return -1;
79     if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
80         config_props = avfilter_default_config_output_link;
81     ret = config_props(link2);
82
83     link->w = link2->w;
84     link->h = link2->h;
85
86     return ret;
87 }
88
89 /**
90  * Given the link between the dummy filter and an internal filter whose input
91  * is being exported outside the graph, this returns the externally visible
92  * link
93  */
94 static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
95 {
96     GraphLinkContext *lctx = link->dst->priv;
97     return lctx->graph->outputs[link->dstpad];
98 }
99
100 static int link_out_config_props(AVFilterLink *link)
101 {
102     AVFilterLink *link2 = get_extern_output_link(link);
103     int (*config_props)(AVFilterLink *);
104
105     if(!link2)
106         return 0;
107
108     link2->w = link->w;
109     link2->h = link->h;
110
111     if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
112         config_props = avfilter_default_config_input_link;
113     return config_props(link2);
114 }
115
116 static void link_out_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
117 {
118     AVFilterLink *link2 = get_extern_output_link(link);
119
120     if(!link2)
121         avfilter_unref_pic(picref);
122     else
123         avfilter_start_frame(link2, picref);
124 }
125
126 static void link_out_end_frame(AVFilterLink *link)
127 {
128     AVFilterLink *link2 = get_extern_output_link(link);
129
130     if(link2)
131         avfilter_end_frame(link2);
132 }
133
134 static AVFilterPicRef *link_out_get_video_buffer(AVFilterLink *link, int perms)
135 {
136     AVFilterLink *link2 = get_extern_output_link(link);
137
138     if(!link2)
139         return NULL;
140     else
141         return avfilter_get_video_buffer(link2, perms);
142 }
143
144 static void link_out_draw_slice(AVFilterLink *link, int y, int height)
145 {
146     AVFilterLink *link2 = get_extern_output_link(link);
147
148     if(link2)
149         avfilter_draw_slice(link2, y, height);
150 }
151
152 /** dummy filter used to help export filters pads outside the graph */
153 static AVFilter vf_graph_dummy =
154 {
155     .name      = "graph_dummy",
156     .author    = "Bobby Bingham",
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->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
206     if(!link2)
207         return -1;
208
209     /* copy link properties over to the dummy internal link */
210     link2->w = link->w;
211     link2->h = link->h;
212     link2->format = link->format;
213
214     if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
215         return 0;   /* FIXME? */
216         //config_props = avfilter_default_config_input_link;
217     return config_props(link2);
218 }
219
220 static AVFilterLink *get_intern_output_link(AVFilterLink *link)
221 {
222     GraphContext *graph = link->src->priv;
223     return graph->link_filter->inputs[link->srcpad];
224 }
225
226 static int graph_out_request_frame(AVFilterLink *link)
227 {
228     AVFilterLink *link2 = get_intern_output_link(link);
229
230     if(link2)
231         return avfilter_request_frame(link2);
232     return -1;
233 }
234
235 static int graph_out_config_props(AVFilterLink *link)
236 {
237     AVFilterLink *link2 = get_intern_output_link(link);
238     int (*config_props)(AVFilterLink *);
239     int ret;
240
241     if(!link2)
242         return 0;
243
244     link2->w = link->w;
245     link2->h = link->h;
246     link2->format = link->format;
247
248     if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
249         config_props = avfilter_default_config_output_link;
250     ret = config_props(link2);
251
252     link->w = link2->w;
253     link->h = link2->h;
254     link->format = link2->format;
255
256     return ret;
257 }
258
259 static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
260                            char *name)
261 {
262     GraphContext *graph = gctx->priv;
263
264     AVFilterPad graph_inpad =
265     {
266         .name             = name,
267         .type             = AV_PAD_VIDEO,
268         .start_frame      = graph_in_start_frame,
269         .end_frame        = graph_in_end_frame,
270         .get_video_buffer = graph_in_get_video_buffer,
271         .draw_slice       = graph_in_draw_slice,
272         .config_props     = graph_in_config_props,
273         /* XXX */
274     };
275     AVFilterPad dummy_outpad =
276     {
277         .name          = NULL,          /* FIXME? */
278         .type          = AV_PAD_VIDEO,
279         .request_frame = link_in_request_frame,
280         .config_props  = link_in_config_props,
281     };
282
283     avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
284     avfilter_insert_outpad(graph->link_filter, graph->link_filter->output_count,
285                            &dummy_outpad);
286     return avfilter_link(graph->link_filter,
287                          graph->link_filter->output_count-1, filt, idx);
288 }
289
290 static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
291                             char *name)
292 {
293     GraphContext *graph = gctx->priv;
294
295     AVFilterPad graph_outpad =
296     {
297         .name             = name,
298         .type             = AV_PAD_VIDEO,
299         .request_frame    = graph_out_request_frame,
300         .config_props     = graph_out_config_props,
301     };
302     AVFilterPad dummy_inpad =
303     {
304         .name             = NULL,          /* FIXME? */
305         .type             = AV_PAD_VIDEO,
306         .start_frame      = link_out_start_frame,
307         .end_frame        = link_out_end_frame,
308         .draw_slice       = link_out_draw_slice,
309         .get_video_buffer = link_out_get_video_buffer,
310         .config_props     = link_out_config_props,
311     };
312
313     avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
314     avfilter_insert_inpad (graph->link_filter, graph->link_filter->input_count,
315                            &dummy_inpad);
316     return avfilter_link(filt, idx, graph->link_filter,
317                          graph->link_filter->input_count-1);
318 }
319
320 static void uninit(AVFilterContext *ctx)
321 {
322     GraphContext *graph = ctx->priv;
323
324     if(graph->link_filter) {
325         avfilter_destroy(graph->link_filter);
326         graph->link_filter = NULL;
327     }
328     for(; graph->filter_count > 0; graph->filter_count --)
329         avfilter_destroy(graph->filters[graph->filter_count - 1]);
330     av_freep(&graph->filters);
331 }
332
333 /* TODO: insert in sorted order */
334 void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
335 {
336     GraphContext *graph = graphctx->priv;
337
338     graph->filters = av_realloc(graph->filters,
339                                 sizeof(AVFilterContext*) * ++graph->filter_count);
340     graph->filters[graph->filter_count - 1] = filter;
341 }
342
343 /* search intelligently, once we insert in order */
344 AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
345 {
346     GraphContext *graph = ctx->priv;
347     int i;
348
349     if(!name)
350         return NULL;
351
352     for(i = 0; i < graph->filter_count; i ++)
353         if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
354             return graph->filters[i];
355
356     return NULL;
357 }
358
359 static int query_formats(AVFilterContext *graphctx)
360 {
361     GraphContext *graph = graphctx->priv;
362     AVFilterContext *linkfilt = graph->link_filter;
363     int i, j;
364
365     /* ask all the sub-filters for their supported colorspaces */
366     for(i = 0; i < graph->filter_count; i ++) {
367         if(graph->filters[i]->filter->query_formats)
368             graph->filters[i]->filter->query_formats(graph->filters[i]);
369         else
370             avfilter_default_query_formats(graph->filters[i]);
371     }
372
373     /* use these formats on our exported links */
374     for(i = 0; i < linkfilt->input_count; i ++) {
375         avfilter_formats_ref( linkfilt->inputs[i]->in_formats,
376                              &linkfilt->inputs[i]->out_formats);
377
378         if(graphctx->outputs[i])
379             avfilter_formats_ref( linkfilt-> inputs[i]->in_formats,
380                                  &graphctx->outputs[i]->in_formats);
381     }
382     for(i = 0; i < linkfilt->output_count; i ++) {
383         avfilter_formats_ref( linkfilt->outputs[i]->out_formats,
384                              &linkfilt->outputs[i]->in_formats);
385
386         if(graphctx->inputs[i])
387             avfilter_formats_ref( linkfilt->outputs[i]->out_formats,
388                                  &graphctx-> inputs[i]->out_formats);
389     }
390
391     /* go through and merge as many format lists as possible */
392     for(i = 0; i < graph->filter_count; i ++) {
393         AVFilterContext *filter = graph->filters[i];
394
395         for(j = 0; j < filter->input_count; j ++) {
396             AVFilterLink *link;
397             if(!(link = filter->inputs[j]))
398                 continue;
399             if(link->in_formats != link->out_formats) {
400                 if(!avfilter_merge_formats(link->in_formats,
401                                            link->out_formats)) {
402                     /* couldn't merge format lists. auto-insert scale filter */
403                     AVFilterContext *scale;
404
405                     if(!(scale = avfilter_open(&avfilter_vf_scale, NULL)))
406                         return -1;
407                     if(scale->filter->init(scale, NULL, NULL) ||
408                        avfilter_insert_filter(link, scale, 0, 0)) {
409                         avfilter_destroy(scale);
410                         return -1;
411                     }
412
413                     avfilter_graph_add_filter(graphctx, scale);
414                     scale->filter->query_formats(scale);
415                     if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
416                                               scale-> inputs[0]->out_formats) ||
417                        !avfilter_merge_formats(scale->outputs[0]->in_formats,
418                                               scale->outputs[0]->out_formats))
419                         return -1;
420                 }
421             }
422         }
423     }
424
425     return 0;
426 }
427
428 static void pick_format(AVFilterLink *link)
429 {
430     if(!link || !link->in_formats)
431         return;
432
433     link->in_formats->format_count = 1;
434     link->format = link->in_formats->formats[0];
435
436     avfilter_formats_unref(&link->in_formats);
437     avfilter_formats_unref(&link->out_formats);
438 }
439
440 static void pick_formats(GraphContext *graph)
441 {
442     int i, j;
443
444     for(i = 0; i < graph->filter_count; i ++) {
445         AVFilterContext *filter = graph->filters[i];
446
447         if(filter->filter == &avfilter_vf_graph     ||
448            filter->filter == &avfilter_vf_graphfile ||
449            filter->filter == &avfilter_vf_graphdesc)
450             pick_formats(filter->priv);
451
452         for(j = 0; j < filter->input_count; j ++)
453             pick_format(filter->inputs[j]);
454         for(j = 0; j < filter->output_count; j ++)
455             pick_format(filter->outputs[j]);
456     }
457 }
458
459 int avfilter_graph_config_formats(AVFilterContext *graphctx)
460 {
461     GraphContext *graph = graphctx->priv;
462
463     /* Find supported formats from sub-filters, and merge along links */
464     if(query_formats(graphctx))
465         return -1;
466
467     /* Once everything is merged, it's possible that we'll still have
468      * multiple valid choices of colorspace. We pick the first one. */
469     pick_formats(graph);
470
471     return 0;
472 }
473
474 int avfilter_graph_config_links(AVFilterContext *graphctx)
475 {
476     GraphContext *graph = graphctx->priv;
477     int i, j;
478
479     for(i = 0; i < graph->filter_count; i ++) {
480         for(j = 0; j < graph->filters[i]->input_count; j ++) {
481             /* ensure that graphs contained within graphs are configured */
482             if((graph->filters[i]->filter == &avfilter_vf_graph     ||
483                 graph->filters[i]->filter == &avfilter_vf_graphfile ||
484                 graph->filters[i]->filter == &avfilter_vf_graphdesc) &&
485                 avfilter_graph_config_links(graph->filters[i]))
486                 return -1;
487             if(avfilter_config_link(graph->filters[i]->inputs[j]))
488                 return -1;
489         }
490     }
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, "error creating filter\n");
509             goto fail;
510         }
511         avfilter_graph_add_filter(ctx, filt);
512         if(avfilter_init_filter(filt, curfilt->args, NULL)) {
513             av_log(ctx, AV_LOG_ERROR, "error initializing filter\n");
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 = avfilter_open(&vf_graph_dummy, NULL)))
566         return -1;
567     if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
568         goto fail;
569
570     if(!args)
571         return 0;
572
573     if(!(desc = avfilter_graph_parse_chain(args)))
574         goto fail;
575
576     ret = graph_load_from_desc(ctx, desc);
577     avfilter_graph_free_desc(desc);
578     return ret;
579
580 fail:
581     avfilter_destroy(gctx->link_filter);
582     return -1;
583 }
584
585 AVFilter avfilter_vf_graph =
586 {
587     .name      = "graph",
588     .author    = "Bobby Bingham",
589
590     .priv_size = sizeof(GraphContext),
591
592     .init      = init,
593     .uninit    = uninit,
594
595     .query_formats = query_formats,
596
597     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
598     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
599 };
600
601 static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
602 {
603     GraphContext *gctx = ctx->priv;
604
605     if(!opaque)
606         return -1;
607
608     if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
609         return -1;
610     if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
611         goto fail;
612
613     return graph_load_from_desc(ctx, opaque);
614
615 fail:
616     avfilter_destroy(gctx->link_filter);
617     return -1;
618 }
619
620 AVFilter avfilter_vf_graphdesc =
621 {
622     .name      = "graph_desc",
623     .author    = "Bobby Bingham",
624
625     .priv_size = sizeof(GraphContext),
626
627     .init      = init_desc,
628     .uninit    = uninit,
629
630     .query_formats = query_formats,
631
632     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
633     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
634 };
635
636 static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
637 {
638     AVFilterGraphDesc *desc;
639     int ret;
640
641     if(!args)
642         return -1;
643     if(!(desc = avfilter_graph_load_desc(args)))
644         return -1;
645
646     ret = init_desc(ctx, NULL, desc);
647     avfilter_graph_free_desc(desc);
648     return ret;
649 }
650
651 AVFilter avfilter_vf_graphfile =
652 {
653     .name      = "graph_file",
654     .author    = "Bobby Bingham",
655
656     .priv_size = sizeof(GraphContext),
657
658     .init      = init_file,
659     .uninit    = uninit,
660
661     .query_formats = query_formats,
662
663     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
664     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
665 };
666