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