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