]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
Prefix names of filter structs
[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 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 == &avfilter_vf_graph     ||
419                 graph->filters[i]->filter == &avfilter_vf_graphfile ||
420                 graph->filters[i]->filter == &avfilter_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     AVFilter *filterdef;
434     AVFilterContext *ret;
435     char *filter = av_strdup(filt); /* copy - don't mangle the input string */
436     char *name, *args;
437
438     name = filter;
439     if((args = strchr(filter, '='))) {
440         /* ensure we at least have a name */
441         if(args == filter)
442             goto fail;
443
444         *args ++ = 0;
445     }
446
447     av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
448            name, args ? args : "(none)");
449
450     if((filterdef = avfilter_get_by_name(name)) &&
451        (ret = avfilter_open(filterdef, NULL))) {
452         if(avfilter_init_filter(ret, args, opaque)) {
453             av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
454             avfilter_destroy(ret);
455             goto fail;
456         }
457     } else av_log(NULL, AV_LOG_ERROR, "error creating filter!\n");
458
459     av_free(filter);
460
461     return ret;
462
463 fail:
464     av_free(filter);
465     return NULL;
466 }
467
468 static int graph_load_chain(AVFilterContext *graphctx,
469                               unsigned count, char **filter_list, void **opaque,
470                               AVFilterContext **first, AVFilterContext **last)
471 {
472     unsigned i;
473     AVFilterContext *filters[2] = {NULL,NULL};
474
475     for(i = 0; i < count; i ++) {
476         void *op;
477
478         if(opaque) op = opaque[i];
479         else       op = NULL;
480
481         if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
482             goto fail;
483         if(i == 0) {
484             if(first) *first = filters[1];
485         } else {
486             if(avfilter_link(filters[0], 0, filters[1], 0)) {
487                 av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
488                 goto fail;
489             }
490         }
491         avfilter_graph_add_filter(graphctx, filters[1]);
492         if(i == 0 && filters[1]->input_count > 0)
493             add_graph_input(graphctx, filters[1], 0, "default");
494         filters[0] = filters[1];
495     }
496
497     if(filters[1]->output_count > 0)
498         add_graph_output(graphctx, filters[1], 0, "default");
499
500     if(last) *last = filters[1];
501     return 0;
502
503 fail:
504     uninit(graphctx);
505     if(first) *first = NULL;
506     if(last)  *last  = NULL;
507     return -1;
508 }
509
510 static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
511                                         AVFilterContext **first,
512                                         AVFilterContext **last)
513 {
514     int count, ret = 0;
515     char **strings;
516     char *filt;
517
518     strings    = av_malloc(sizeof(char *));
519     strings[0] = av_strdup(str);
520
521     filt = strchr(strings[0], ',');
522     for(count = 1; filt; count ++) {
523         if(filt == strings[count-1]) {
524             ret = -1;
525             goto done;
526         }
527
528         strings = av_realloc(strings, sizeof(char *) * (count+1));
529         strings[count] = filt + 1;
530         *filt = '\0';
531         filt = strchr(strings[count], ',');
532     }
533
534     ret = graph_load_chain(ctx, count, strings, NULL, first, last);
535
536 done:
537     av_free(strings[0]);
538     av_free(strings);
539
540     return ret;
541 }
542
543 static int init(AVFilterContext *ctx, const char *args, void *opaque)
544 {
545     GraphContext *gctx = ctx->priv;
546
547     if(!args)
548         return 0;
549
550     if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
551         return -1;
552     if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
553         goto fail;
554
555     return graph_load_chain_from_string(ctx, args, NULL, NULL);
556
557 fail:
558     avfilter_destroy(gctx->link_filter);
559     return -1;
560 }
561
562 AVFilter avfilter_vf_graph =
563 {
564     .name      = "graph",
565     .author    = "Bobby Bingham",
566
567     .priv_size = sizeof(GraphContext),
568
569     .init      = init,
570     .uninit    = uninit,
571
572     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
573     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
574 };
575
576 static int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc)
577 {
578     AVFilterGraphDescFilter *curfilt;
579     AVFilterGraphDescLink   *curlink;
580     AVFilterGraphDescExport *curpad;
581     AVFilterContext *filt, *filtb;
582
583     AVFilter *filterdef;
584
585     /* create all filters */
586     for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
587         if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
588            !(filt = avfilter_open(filterdef, curfilt->name))) {
589             av_log(ctx, AV_LOG_ERROR, "error creating filter\n");
590             goto fail;
591         }
592         avfilter_graph_add_filter(ctx, filt);
593         if(avfilter_init_filter(filt, curfilt->args, NULL)) {
594             av_log(ctx, AV_LOG_ERROR, "error initializing filter\n");
595             goto fail;
596         }
597     }
598
599     /* create all links */
600     for(curlink = desc->links; curlink; curlink = curlink->next) {
601         if(!(filt = avfilter_graph_get_filter(ctx, curlink->src))) {
602             av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
603             goto fail;
604         }
605         if(!(filtb = avfilter_graph_get_filter(ctx, curlink->dst))) {
606             av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
607             goto fail;
608         }
609         if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
610             av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
611             goto fail;
612         }
613     }
614
615     /* export all input pads */
616     for(curpad = desc->inputs; curpad; curpad = curpad->next) {
617         if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
618             av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
619             goto fail;
620         }
621         add_graph_input(ctx, filt, curpad->pad, curpad->name);
622     }
623
624     /* export all output pads */
625     for(curpad = desc->outputs; curpad; curpad = curpad->next) {
626         if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
627             av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
628             goto fail;
629         }
630         add_graph_output(ctx, filt, curpad->pad, curpad->name);
631     }
632
633     return 0;
634
635 fail:
636     uninit(ctx);
637     return -1;
638 }
639
640 static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
641 {
642     GraphContext *gctx = ctx->priv;
643
644     if(!opaque)
645         return -1;
646
647     if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
648         return -1;
649     if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
650         goto fail;
651
652     return graph_load_from_desc(ctx, opaque);
653
654 fail:
655     avfilter_destroy(gctx->link_filter);
656     return -1;
657 }
658
659 AVFilter avfilter_vf_graphdesc =
660 {
661     .name      = "graph_desc",
662     .author    = "Bobby Bingham",
663
664     .priv_size = sizeof(GraphContext),
665
666     .init      = init_desc,
667     .uninit    = uninit,
668
669     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
670     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
671 };
672
673 static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
674 {
675     AVFilterGraphDesc *desc;
676     int ret;
677
678     if(!args)
679         return -1;
680     if(!(desc = avfilter_graph_load_desc(args)))
681         return -1;
682
683     ret = init_desc(ctx, NULL, desc);
684     avfilter_graph_free_desc(desc);
685     return ret;
686 }
687
688 AVFilter avfilter_vf_graphfile =
689 {
690     .name      = "graph_file",
691     .author    = "Bobby Bingham",
692
693     .priv_size = sizeof(GraphContext),
694
695     .init      = init_file,
696     .uninit    = uninit,
697
698     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
699     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
700 };
701