3 * copyright (c) 2007 Bobby Bingham
5 * This file is part of FFmpeg.
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.
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.
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
27 #include "avfiltergraph.h"
29 #include "allfilters.h"
31 typedef struct AVFilterGraph {
32 unsigned filter_count;
33 AVFilterContext **filters;
35 /** fake filter to handle links to internal filters */
36 AVFilterContext *link_filter;
40 AVFilterContext *graph;
43 static int link_init(AVFilterContext *ctx, const char *args, void *opaque)
45 GraphLinkContext *linkctx = ctx->priv;
46 linkctx->graph = opaque;
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
53 static inline AVFilterLink *get_extern_input_link(AVFilterLink *link)
55 GraphLinkContext *lctx = link->src->priv;
56 return lctx->graph->inputs[link->srcpad];
59 /** query the formats supported by a filter providing input to the graph */
60 static int *link_in_query_formats(AVFilterLink *link)
62 AVFilterLink *link2 = get_extern_input_link(link);
63 int *(*query_formats)(AVFilterLink *);
66 return avfilter_make_format_list(0);
68 if(!(query_formats = link2->src->output_pads[link2->srcpad].query_formats))
69 query_formats = avfilter_default_query_output_formats;
71 return query_formats(link2);
74 /** request a frame from a filter providing input to the graph */
75 static int link_in_request_frame(AVFilterLink *link)
77 AVFilterLink *link2 = get_extern_input_link(link);
81 return avfilter_request_frame(link2);
84 static int link_in_config_props(AVFilterLink *link)
86 AVFilterLink *link2 = get_extern_input_link(link);
87 int (*config_props)(AVFilterLink *);
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);
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
105 static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
107 GraphLinkContext *lctx = link->dst->priv;
108 return lctx->graph->outputs[link->dstpad];
111 /** query the formats supported by a filter taking output from the graph */
112 static int *link_out_query_formats(AVFilterLink *link)
114 AVFilterLink *link2 = get_extern_output_link(link);
117 return avfilter_make_format_list(0);
119 return link2->dst->input_pads[link2->dstpad].query_formats(link2);
122 static int link_out_config_props(AVFilterLink *link)
124 AVFilterLink *link2 = get_extern_output_link(link);
125 int (*config_props)(AVFilterLink *);
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);
138 static void link_out_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
140 AVFilterLink *link2 = get_extern_output_link(link);
143 avfilter_unref_pic(picref);
145 avfilter_start_frame(link2, picref);
148 static void link_out_end_frame(AVFilterLink *link)
150 AVFilterLink *link2 = get_extern_output_link(link);
153 avfilter_end_frame(link2);
156 static AVFilterPicRef *link_out_get_video_buffer(AVFilterLink *link, int perms)
158 AVFilterLink *link2 = get_extern_output_link(link);
163 return avfilter_get_video_buffer(link2, perms);
166 static void link_out_draw_slice(AVFilterLink *link, uint8_t *data[4], int y,
169 AVFilterLink *link2 = get_extern_output_link(link);
172 avfilter_draw_slice(link2, data, y, height);
175 /** dummy filter used to help export filters pads outside the graph */
176 static AVFilter vf_graph_dummy =
178 .name = "graph_dummy",
179 .author = "Bobby Bingham",
181 .priv_size = sizeof(GraphLinkContext),
186 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
187 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
190 static AVFilterLink *get_intern_input_link(AVFilterLink *link)
192 GraphContext *graph = link->dst->priv;
193 return graph->link_filter->outputs[link->dstpad];
196 static void graph_in_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
198 AVFilterLink *link2 = get_intern_input_link(link);
200 avfilter_start_frame(link2, picref);
203 static void graph_in_end_frame(AVFilterLink *link)
205 AVFilterLink *link2 = get_intern_input_link(link);
207 avfilter_end_frame(link2);
210 static AVFilterPicRef *graph_in_get_video_buffer(AVFilterLink *link, int perms)
212 AVFilterLink *link2 = get_intern_input_link(link);
214 return avfilter_get_video_buffer(link2, perms);
218 static void graph_in_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int height)
220 AVFilterLink *link2 = get_intern_input_link(link);
222 avfilter_draw_slice(link2, data, y, height);
225 static int *graph_in_query_formats(AVFilterLink *link)
227 AVFilterLink *link2 = get_intern_input_link(link);
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);
234 static int graph_in_config_props(AVFilterLink *link)
236 AVFilterLink *link2 = get_intern_input_link(link);
237 int (*config_props)(AVFilterLink *);
242 /* copy link properties over to the dummy internal link */
245 link2->format = link->format;
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);
253 static AVFilterLink *get_intern_output_link(AVFilterLink *link)
255 GraphContext *graph = link->src->priv;
256 return graph->link_filter->inputs[link->srcpad];
259 static int *graph_out_query_formats(AVFilterLink *link)
261 AVFilterLink *link2 = get_intern_output_link(link);
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);
270 static int graph_out_request_frame(AVFilterLink *link)
272 AVFilterLink *link2 = get_intern_output_link(link);
275 return avfilter_request_frame(link2);
279 static int graph_out_config_props(AVFilterLink *link)
281 AVFilterLink *link2 = get_intern_output_link(link);
282 int (*config_props)(AVFilterLink *);
290 link2->format = link->format;
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);
298 link->format = link2->format;
303 static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
306 GraphContext *graph = gctx->priv;
308 AVFilterPad graph_inpad =
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,
320 AVFilterPad dummy_outpad =
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,
329 avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
330 avfilter_insert_outpad(graph->link_filter, graph->link_filter->output_count,
332 return avfilter_link(graph->link_filter,
333 graph->link_filter->output_count-1, filt, idx);
336 static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
339 GraphContext *graph = gctx->priv;
341 AVFilterPad graph_outpad =
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,
349 AVFilterPad dummy_inpad =
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,
361 avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
362 avfilter_insert_inpad (graph->link_filter, graph->link_filter->input_count,
364 return avfilter_link(filt, idx, graph->link_filter,
365 graph->link_filter->input_count-1);
368 static void uninit(AVFilterContext *ctx)
370 GraphContext *graph = ctx->priv;
372 if(graph->link_filter) {
373 avfilter_destroy(graph->link_filter);
374 graph->link_filter = NULL;
376 for(; graph->filter_count > 0; graph->filter_count --)
377 avfilter_destroy(graph->filters[graph->filter_count - 1]);
378 av_freep(&graph->filters);
381 /* TODO: insert in sorted order */
382 void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
384 GraphContext *graph = graphctx->priv;
386 graph->filters = av_realloc(graph->filters,
387 sizeof(AVFilterContext*) * ++graph->filter_count);
388 graph->filters[graph->filter_count - 1] = filter;
391 /* search intelligently, once we insert in order */
392 AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
394 GraphContext *graph = ctx->priv;
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];
407 int avfilter_graph_config_links(AVFilterContext *graphctx)
409 GraphContext *graph = graphctx->priv;
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]))
420 if(avfilter_config_link(graph->filters[i]->inputs[j]))
428 static AVFilterContext *create_filter_with_args(const char *filt, void *opaque)
430 AVFilterContext *ret;
431 char *filter = av_strdup(filt); /* copy - don't mangle the input string */
435 if((args = strchr(filter, '='))) {
436 /* ensure we at least have a name */
443 av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
444 name, args ? args : "(none)");
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);
452 } else av_log(NULL, AV_LOG_ERROR, "error creating filter!\n");
463 static int graph_load_chain(AVFilterContext *graphctx,
464 unsigned count, char **filter_list, void **opaque,
465 AVFilterContext **first, AVFilterContext **last)
468 AVFilterContext *filters[2] = {NULL,NULL};
470 for(i = 0; i < count; i ++) {
473 if(opaque) op = opaque[i];
476 if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
479 if(first) *first = filters[1];
481 if(avfilter_link(filters[0], 0, filters[1], 0)) {
482 av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
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];
492 if(filters[1]->output_count > 0)
493 add_graph_output(graphctx, filters[1], 0, "default");
495 if(last) *last = filters[1];
500 if(first) *first = NULL;
501 if(last) *last = NULL;
505 static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
506 AVFilterContext **first,
507 AVFilterContext **last)
513 strings = av_malloc(sizeof(char *));
514 strings[0] = av_strdup(str);
516 filt = strchr(strings[0], ',');
517 for(count = 1; filt; count ++) {
518 if(filt == strings[count-1]) {
523 strings = av_realloc(strings, sizeof(char *) * (count+1));
524 strings[count] = filt + 1;
526 filt = strchr(strings[count], ',');
529 ret = graph_load_chain(ctx, count, strings, NULL, first, last);
538 static int init(AVFilterContext *ctx, const char *args, void *opaque)
540 GraphContext *gctx = ctx->priv;
545 if(!(gctx->link_filter = avfilter_create(&vf_graph_dummy, NULL)))
547 if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
550 return graph_load_chain_from_string(ctx, args, NULL, NULL);
553 avfilter_destroy(gctx->link_filter);
560 .author = "Bobby Bingham",
562 .priv_size = sizeof(GraphContext),
567 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
568 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
571 static int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc)
573 AVFilterGraphDescFilter *curfilt;
574 AVFilterGraphDescLink *curlink;
575 AVFilterGraphDescExport *curpad;
576 AVFilterContext *filt, *filtb;
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");
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");
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");
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");
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");
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");
613 add_graph_input(ctx, filt, curpad->pad, curpad->name);
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");
622 add_graph_output(ctx, filt, curpad->pad, curpad->name);
632 static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
634 GraphContext *gctx = ctx->priv;
639 if(!(gctx->link_filter = avfilter_create(&vf_graph_dummy, NULL)))
641 if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
644 return graph_load_from_desc(ctx, opaque);
647 avfilter_destroy(gctx->link_filter);
651 AVFilter vf_graphdesc =
653 .name = "graph_desc",
654 .author = "Bobby Bingham",
656 .priv_size = sizeof(GraphContext),
661 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
662 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
665 static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
667 AVFilterGraphDesc *desc;
672 if(!(desc = avfilter_graph_load_desc(args)))
675 ret = init_desc(ctx, NULL, desc);
676 avfilter_graph_free_desc(desc);
680 AVFilter vf_graphfile =
682 .name = "graph_file",
683 .author = "Bobby Bingham",
685 .priv_size = sizeof(GraphContext),
690 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
691 .outputs = (AVFilterPad[]) {{ .name = NULL, }},