]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
abd41aa44f77702210290e8319e88ec0a8d22682
[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 typedef struct AVFilterGraph {
30     unsigned filter_count;
31     AVFilterContext **filters;
32
33     /** fake filter to handle links to internal filters */
34     AVFilterContext *link_filter;
35 } GraphContext;
36
37 typedef struct {
38     AVFilterContext *graph;
39 } GraphLinkContext;
40
41 static int link_init(AVFilterContext *ctx, const char *args, void *opaque)
42 {
43     GraphLinkContext *linkctx = ctx->priv;
44     linkctx->graph = opaque;
45     return !opaque;
46 }
47
48 /* given the link between the dummy filter and an internal filter whose input
49  * is being exported outside the graph, this returns the externally visible
50  * link */
51 static inline AVFilterLink *get_extern_input_link(AVFilterLink *link)
52 {
53     GraphLinkContext *lctx = link->src->priv;
54     return lctx->graph->inputs[link->srcpad];
55 }
56
57 /** query the formats supported by a filter providing input to the graph */
58 static int *link_in_query_formats(AVFilterLink *link)
59 {
60     AVFilterLink *link2 = get_extern_input_link(link);
61     int *(*query_formats)(AVFilterLink *);
62
63     if(!link2)
64         return avfilter_make_format_list(0);
65
66     if(!(query_formats = link2->src->output_pads[link2->srcpad].query_formats))
67         query_formats = avfilter_default_query_output_formats;
68
69     return query_formats(link2);
70 }
71
72 /** request a frame from a filter providing input to the graph */
73 static void link_in_request_frame(AVFilterLink *link)
74 {
75     AVFilterLink *link2 = get_extern_input_link(link);
76
77     if(!link2)
78         return;
79     avfilter_request_frame(link2);
80 }
81
82 static int link_in_config_props(AVFilterLink *link)
83 {
84     AVFilterLink *link2 = get_extern_input_link(link);
85     int (*config_props)(AVFilterLink *);
86     int ret;
87
88     if(!link2)
89         return -1;
90     if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
91         config_props = avfilter_default_config_output_link;
92     ret = config_props(link2);
93
94     link->w = link2->w;
95     link->h = link2->h;
96
97     return ret;
98 }
99
100 /* given the link between the dummy filter and an internal filter whose input
101  * is being exported outside the graph, this returns the externally visible
102  * link */
103 static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
104 {
105     GraphLinkContext *lctx = link->dst->priv;
106     return lctx->graph->outputs[link->dstpad];
107 }
108
109 /** query the formats supported by a filter taking output from the graph */
110 static int *link_out_query_formats(AVFilterLink *link)
111 {
112     AVFilterLink *link2 = get_extern_output_link(link);
113
114     if(!link2)
115         return avfilter_make_format_list(0);
116
117     return link2->dst->input_pads[link2->dstpad].query_formats(link2);
118 }
119
120 static int link_out_config_props(AVFilterLink *link)
121 {
122     AVFilterLink *link2 = get_extern_output_link(link);
123     int (*config_props)(AVFilterLink *);
124
125     if(!link2)
126         return 0;
127
128     link2->w = link->w;
129     link2->h = link->h;
130
131     if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
132         config_props = avfilter_default_config_input_link;
133     return config_props(link2);
134 }
135
136 static void link_out_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
137 {
138     AVFilterLink *link2 = get_extern_output_link(link);
139
140     if(!link2)
141         avfilter_unref_pic(picref);
142     else
143         avfilter_start_frame(link2, picref);
144 }
145
146 static void link_out_end_frame(AVFilterLink *link)
147 {
148     AVFilterLink *link2 = get_extern_output_link(link);
149
150     if(link2)
151         avfilter_end_frame(link2);
152 }
153
154 static AVFilterPicRef *link_out_get_video_buffer(AVFilterLink *link, int perms)
155 {
156     AVFilterLink *link2 = get_extern_output_link(link);
157
158     if(!link2)
159         return NULL;
160     else
161         return avfilter_get_video_buffer(link2, perms);
162 }
163
164 static void link_out_draw_slice(AVFilterLink *link, uint8_t *data[4], int y,
165                                 int height)
166 {
167     AVFilterLink *link2 = get_extern_output_link(link);
168
169     if(link2)
170         avfilter_draw_slice(link2, data, y, height);
171 }
172
173 /** dummy filter used to help export filters pads outside the graph */
174 static AVFilter vf_graph_dummy =
175 {
176     .name      = "graph_dummy",
177     .author    = "Bobby Bingham",
178
179     .priv_size = sizeof(GraphLinkContext),
180
181     .init      = link_init,
182     //.uninit    = uninit,
183
184     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
185     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
186 };
187
188 static AVFilterLink *get_intern_input_link(AVFilterLink *link)
189 {
190     GraphContext *graph = link->dst->priv;
191     return graph->link_filter->outputs[link->dstpad];
192 }
193
194 static void graph_in_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
195 {
196     AVFilterLink *link2 = get_intern_input_link(link);
197     if(link2)
198         avfilter_start_frame(link2, picref);
199 }
200
201 static void graph_in_end_frame(AVFilterLink *link)
202 {
203     AVFilterLink *link2 = get_intern_input_link(link);
204     if(link2)
205         avfilter_end_frame(link2);
206 }
207
208 static AVFilterPicRef *graph_in_get_video_buffer(AVFilterLink *link, int perms)
209 {
210     AVFilterLink *link2 = get_intern_input_link(link);
211     if(link2)
212         return avfilter_get_video_buffer(link2, perms);
213     return NULL;
214 }
215
216 static void graph_in_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int height)
217 {
218     AVFilterLink *link2 = get_intern_input_link(link);
219     if(link2)
220         avfilter_draw_slice(link2, data, y, height);
221 }
222
223 static int *graph_in_query_formats(AVFilterLink *link)
224 {
225     AVFilterLink *link2 = get_intern_input_link(link);
226
227     if(!link2 || !link2->dst->input_pads[link2->dstpad].query_formats)
228         return avfilter_make_format_list(0);
229     return link2->dst->input_pads[link2->dstpad].query_formats(link2);
230 }
231
232 static int graph_in_config_props(AVFilterLink *link)
233 {
234     AVFilterLink *link2 = get_intern_input_link(link);
235     int (*config_props)(AVFilterLink *);
236
237     if(!link2)
238         return -1;
239
240     /* copy link properties over to the dummy internal link */
241     link2->w = link->w;
242     link2->h = link->h;
243     link2->format = link->format;
244
245     if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
246         return 0;   /* FIXME? */
247         //config_props = avfilter_default_config_input_link;
248     return config_props(link2);
249 }
250
251 static AVFilterLink *get_intern_output_link(AVFilterLink *link)
252 {
253     GraphContext *graph = link->src->priv;
254     return graph->link_filter->inputs[link->srcpad];
255 }
256
257 static int *graph_out_query_formats(AVFilterLink *link)
258 {
259     AVFilterLink *link2 = get_intern_output_link(link);
260
261     if(!link2)
262         return avfilter_make_format_list(0);
263     if(!link2->src->output_pads[link2->srcpad].query_formats)
264         return avfilter_default_query_output_formats(link2);
265     return link2->src->output_pads[link2->srcpad].query_formats(link2);
266 }
267
268 static void graph_out_request_frame(AVFilterLink *link)
269 {
270     AVFilterLink *link2 = get_intern_output_link(link);
271
272     if(link2)
273         avfilter_request_frame(link2);
274 }
275
276 static int graph_out_config_props(AVFilterLink *link)
277 {
278     AVFilterLink *link2 = get_intern_output_link(link);
279     int (*config_props)(AVFilterLink *);
280     int ret;
281
282     if(!link2)
283         return 0;
284
285     link2->w = link->w;
286     link2->h = link->h;
287     link2->format = link->format;
288
289     if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
290         config_props = avfilter_default_config_output_link;
291     ret = config_props(link2);
292
293     link->w = link2->w;
294     link->h = link2->h;
295     link->format = link2->format;
296
297     return ret;
298 }
299
300 static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
301                            char *name)
302 {
303     GraphContext *graph = gctx->priv;
304
305     AVFilterPad graph_inpad =
306     {
307         .name             = name,
308         .type             = AV_PAD_VIDEO,
309         .start_frame      = graph_in_start_frame,
310         .end_frame        = graph_in_end_frame,
311         .get_video_buffer = graph_in_get_video_buffer,
312         .draw_slice       = graph_in_draw_slice,
313         .query_formats    = graph_in_query_formats,
314         .config_props     = graph_in_config_props,
315         /* XXX */
316     };
317     AVFilterPad dummy_outpad =
318     {
319         .name          = NULL,          /* FIXME? */
320         .type          = AV_PAD_VIDEO,
321         .query_formats = link_in_query_formats,
322         .request_frame = link_in_request_frame,
323         .config_props  = link_in_config_props,
324     };
325
326     avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
327     avfilter_insert_outpad(graph->link_filter, graph->link_filter->output_count,
328                            &dummy_outpad);
329     return avfilter_link(graph->link_filter,
330                          graph->link_filter->output_count-1, filt, idx);
331 }
332
333 static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
334                             char *name)
335 {
336     GraphContext *graph = gctx->priv;
337
338     AVFilterPad graph_outpad =
339     {
340         .name             = name,
341         .type             = AV_PAD_VIDEO,
342         .request_frame    = graph_out_request_frame,
343         .query_formats    = graph_out_query_formats,
344         .config_props     = graph_out_config_props,
345     };
346     AVFilterPad dummy_inpad =
347     {
348         .name             = NULL,          /* FIXME? */
349         .type             = AV_PAD_VIDEO,
350         .start_frame      = link_out_start_frame,
351         .end_frame        = link_out_end_frame,
352         .draw_slice       = link_out_draw_slice,
353         .get_video_buffer = link_out_get_video_buffer,
354         .query_formats    = link_out_query_formats,
355         .config_props     = link_out_config_props,
356     };
357
358     avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
359     avfilter_insert_inpad (graph->link_filter, graph->link_filter->input_count,
360                            &dummy_inpad);
361     return avfilter_link(filt, idx, graph->link_filter,
362                          graph->link_filter->input_count-1);
363 }
364
365 static void uninit(AVFilterContext *ctx)
366 {
367     GraphContext *graph = ctx->priv;
368
369     if(graph->link_filter) {
370         avfilter_destroy(graph->link_filter);
371         graph->link_filter = NULL;
372     }
373     for(; graph->filter_count > 0; graph->filter_count --)
374         avfilter_destroy(graph->filters[graph->filter_count - 1]);
375     av_freep(&graph->filters);
376 }
377
378 void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
379 {
380     GraphContext *graph = graphctx->priv;
381
382     graph->filters = av_realloc(graph->filters,
383                                 sizeof(AVFilterContext*) * ++graph->filter_count);
384     graph->filters[graph->filter_count - 1] = filter;
385 }
386
387 int avfilter_graph_config_links(AVFilterContext *graphctx)
388 {
389     GraphContext *graph = graphctx->priv;
390     int i, j;
391
392     for(i = 0; i < graph->filter_count; i ++) {
393         for(j = 0; j < graph->filters[i]->input_count; j ++)
394             if(avfilter_config_link(graph->filters[i]->inputs[j]))
395                 return -1;
396     }
397
398     return 0;
399 }
400
401 static AVFilterContext *create_filter_with_args(const char *filt, void *opaque)
402 {
403     AVFilterContext *ret;
404     char *filter = av_strdup(filt); /* copy - don't mangle the input string */
405     char *name, *args;
406
407     name = filter;
408     if((args = strchr(filter, '='))) {
409         /* ensure we at least have a name */
410         if(args == filter)
411             goto fail;
412
413         *args ++ = 0;
414     }
415
416     av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
417            name, args ? args : "(none)");
418
419     if((ret = avfilter_create_by_name(name, NULL))) {
420         if(avfilter_init_filter(ret, args, opaque)) {
421             av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
422             avfilter_destroy(ret);
423             goto fail;
424         }
425     } else av_log(NULL, AV_LOG_ERROR, "error creating filter!\n");
426
427     av_free(filter);
428
429     return ret;
430
431 fail:
432     av_free(filter);
433     return NULL;
434 }
435
436 static int graph_load_chain(AVFilterContext *graphctx,
437                               unsigned count, char **filter_list, void **opaque,
438                               AVFilterContext **first, AVFilterContext **last)
439 {
440     unsigned i;
441     AVFilterContext *filters[2] = {NULL,NULL};
442
443     for(i = 0; i < count; i ++) {
444         void *op;
445
446         if(opaque) op = opaque[i];
447         else       op = NULL;
448
449         if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
450             goto fail;
451         if(i == 0) {
452             if(first) *first = filters[1];
453         } else {
454             if(avfilter_link(filters[0], 0, filters[1], 0)) {
455                 av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
456                 goto fail;
457             }
458         }
459         avfilter_graph_add_filter(graphctx, filters[1]);
460         if(i == 0 && filters[1]->input_count > 0)
461             add_graph_input(graphctx, filters[1], 0, "default");
462         filters[0] = filters[1];
463     }
464
465     if(filters[1]->output_count > 0)
466         add_graph_output(graphctx, filters[1], 0, "default");
467
468     if(last) *last = filters[1];
469     return 0;
470
471 fail:
472     uninit(graphctx);
473     if(first) *first = NULL;
474     if(last)  *last  = NULL;
475     return -1;
476 }
477
478 static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
479                                         AVFilterContext **first,
480                                         AVFilterContext **last)
481 {
482     int count, ret = 0;
483     char **strings;
484     char *filt;
485
486     strings    = av_malloc(sizeof(char *));
487     strings[0] = av_strdup(str);
488
489     filt = strchr(strings[0], ',');
490     for(count = 1; filt; count ++) {
491         if(filt == strings[count-1]) {
492             ret = -1;
493             goto done;
494         }
495
496         strings = av_realloc(strings, sizeof(char *) * (count+1));
497         strings[count] = filt + 1;
498         *filt = '\0';
499         filt = strchr(strings[count], ',');
500     }
501
502     ret = graph_load_chain(ctx, count, strings, NULL, first, last);
503
504 done:
505     av_free(strings[0]);
506     av_free(strings);
507
508     return ret;
509 }
510
511 static int init(AVFilterContext *ctx, const char *args, void *opaque)
512 {
513     GraphContext *gctx = ctx->priv;
514
515     if(!args)
516         return 0;
517
518     if(!(gctx->link_filter = avfilter_create(&vf_graph_dummy, NULL)))
519         return -1;
520     if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
521         goto fail;
522
523     return graph_load_chain_from_string(ctx, args, NULL, NULL);
524
525 fail:
526     avfilter_destroy(gctx->link_filter);
527     return -1;
528 }
529
530 AVFilter vf_graph =
531 {
532     .name      = "graph",
533     .author    = "Bobby Bingham",
534
535     .priv_size = sizeof(GraphContext),
536
537     .init      = init,
538     .uninit    = uninit,
539
540     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
541     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
542 };
543