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