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