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;
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
55 static inline AVFilterLink *get_extern_input_link(AVFilterLink *link)
57 GraphLinkContext *lctx = link->src->priv;
58 return lctx->graph->inputs[link->srcpad];
61 /** query the formats supported by a filter providing input to the graph */
62 static int *link_in_query_formats(AVFilterLink *link)
64 AVFilterLink *link2 = get_extern_input_link(link);
65 int *(*query_formats)(AVFilterLink *);
68 return avfilter_make_format_list(0);
70 if(!(query_formats = link2->src->output_pads[link2->srcpad].query_formats))
71 query_formats = avfilter_default_query_output_formats;
73 return query_formats(link2);
76 /** request a frame from a filter providing input to the graph */
77 static int link_in_request_frame(AVFilterLink *link)
79 AVFilterLink *link2 = get_extern_input_link(link);
83 return avfilter_request_frame(link2);
86 static int link_in_config_props(AVFilterLink *link)
88 AVFilterLink *link2 = get_extern_input_link(link);
89 int (*config_props)(AVFilterLink *);
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);
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
109 static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
111 GraphLinkContext *lctx = link->dst->priv;
112 return lctx->graph->outputs[link->dstpad];
115 /** query the formats supported by a filter taking output from the graph */
116 static int *link_out_query_formats(AVFilterLink *link)
118 AVFilterLink *link2 = get_extern_output_link(link);
121 return avfilter_make_format_list(0);
123 return link2->dst->input_pads[link2->dstpad].query_formats(link2);
126 static int link_out_config_props(AVFilterLink *link)
128 AVFilterLink *link2 = get_extern_output_link(link);
129 int (*config_props)(AVFilterLink *);
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);
142 static void link_out_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
144 AVFilterLink *link2 = get_extern_output_link(link);
147 avfilter_unref_pic(picref);
149 avfilter_start_frame(link2, picref);
152 static void link_out_end_frame(AVFilterLink *link)
154 AVFilterLink *link2 = get_extern_output_link(link);
157 avfilter_end_frame(link2);
160 static AVFilterPicRef *link_out_get_video_buffer(AVFilterLink *link, int perms)
162 AVFilterLink *link2 = get_extern_output_link(link);
167 return avfilter_get_video_buffer(link2, perms);
170 static void link_out_draw_slice(AVFilterLink *link, int y, int height)
172 AVFilterLink *link2 = get_extern_output_link(link);
175 avfilter_draw_slice(link2, y, height);
178 /** dummy filter used to help export filters pads outside the graph */
179 static AVFilter vf_graph_dummy =
181 .name = "graph_dummy",
182 .author = "Bobby Bingham",
184 .priv_size = sizeof(GraphLinkContext),
188 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
189 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
192 static AVFilterLink *get_intern_input_link(AVFilterLink *link)
194 GraphContext *graph = link->dst->priv;
195 return graph->link_filter->outputs[link->dstpad];
198 static void graph_in_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
200 AVFilterLink *link2 = get_intern_input_link(link);
202 avfilter_start_frame(link2, picref);
205 static void graph_in_end_frame(AVFilterLink *link)
207 AVFilterLink *link2 = get_intern_input_link(link);
209 avfilter_end_frame(link2);
212 static AVFilterPicRef *graph_in_get_video_buffer(AVFilterLink *link, int perms)
214 AVFilterLink *link2 = get_intern_input_link(link);
216 return avfilter_get_video_buffer(link2, perms);
220 static void graph_in_draw_slice(AVFilterLink *link, int y, int height)
222 AVFilterLink *link2 = get_intern_input_link(link);
224 avfilter_draw_slice(link2, y, height);
227 static int *graph_in_query_formats(AVFilterLink *link)
229 AVFilterLink *link2 = get_intern_input_link(link);
231 if(!link2 || !link2->dst->input_pads[link2->dstpad].query_formats)
232 return avfilter_make_format_list(0);
233 return link2->dst->input_pads[link2->dstpad].query_formats(link2);
236 static int graph_in_config_props(AVFilterLink *link)
238 AVFilterLink *link2 = get_intern_input_link(link);
239 int (*config_props)(AVFilterLink *);
244 /* copy link properties over to the dummy internal link */
247 link2->format = link->format;
249 if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
250 return 0; /* FIXME? */
251 //config_props = avfilter_default_config_input_link;
252 return config_props(link2);
255 static AVFilterLink *get_intern_output_link(AVFilterLink *link)
257 GraphContext *graph = link->src->priv;
258 return graph->link_filter->inputs[link->srcpad];
261 static int *graph_out_query_formats(AVFilterLink *link)
263 AVFilterLink *link2 = get_intern_output_link(link);
266 return avfilter_make_format_list(0);
267 if(!link2->src->output_pads[link2->srcpad].query_formats)
268 return avfilter_default_query_output_formats(link2);
269 return link2->src->output_pads[link2->srcpad].query_formats(link2);
272 static int graph_out_request_frame(AVFilterLink *link)
274 AVFilterLink *link2 = get_intern_output_link(link);
277 return avfilter_request_frame(link2);
281 static int graph_out_config_props(AVFilterLink *link)
283 AVFilterLink *link2 = get_intern_output_link(link);
284 int (*config_props)(AVFilterLink *);
292 link2->format = link->format;
294 if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
295 config_props = avfilter_default_config_output_link;
296 ret = config_props(link2);
300 link->format = link2->format;
305 static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
308 GraphContext *graph = gctx->priv;
310 AVFilterPad graph_inpad =
313 .type = AV_PAD_VIDEO,
314 .start_frame = graph_in_start_frame,
315 .end_frame = graph_in_end_frame,
316 .get_video_buffer = graph_in_get_video_buffer,
317 .draw_slice = graph_in_draw_slice,
318 .query_formats = graph_in_query_formats,
319 .config_props = graph_in_config_props,
322 AVFilterPad dummy_outpad =
324 .name = NULL, /* FIXME? */
325 .type = AV_PAD_VIDEO,
326 .query_formats = link_in_query_formats,
327 .request_frame = link_in_request_frame,
328 .config_props = link_in_config_props,
331 avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
332 avfilter_insert_outpad(graph->link_filter, graph->link_filter->output_count,
334 return avfilter_link(graph->link_filter,
335 graph->link_filter->output_count-1, filt, idx);
338 static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
341 GraphContext *graph = gctx->priv;
343 AVFilterPad graph_outpad =
346 .type = AV_PAD_VIDEO,
347 .request_frame = graph_out_request_frame,
348 .query_formats = graph_out_query_formats,
349 .config_props = graph_out_config_props,
351 AVFilterPad dummy_inpad =
353 .name = NULL, /* FIXME? */
354 .type = AV_PAD_VIDEO,
355 .start_frame = link_out_start_frame,
356 .end_frame = link_out_end_frame,
357 .draw_slice = link_out_draw_slice,
358 .get_video_buffer = link_out_get_video_buffer,
359 .query_formats = link_out_query_formats,
360 .config_props = link_out_config_props,
363 avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
364 avfilter_insert_inpad (graph->link_filter, graph->link_filter->input_count,
366 return avfilter_link(filt, idx, graph->link_filter,
367 graph->link_filter->input_count-1);
370 static void uninit(AVFilterContext *ctx)
372 GraphContext *graph = ctx->priv;
374 if(graph->link_filter) {
375 avfilter_destroy(graph->link_filter);
376 graph->link_filter = NULL;
378 for(; graph->filter_count > 0; graph->filter_count --)
379 avfilter_destroy(graph->filters[graph->filter_count - 1]);
380 av_freep(&graph->filters);
383 /* TODO: insert in sorted order */
384 void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
386 GraphContext *graph = graphctx->priv;
388 graph->filters = av_realloc(graph->filters,
389 sizeof(AVFilterContext*) * ++graph->filter_count);
390 graph->filters[graph->filter_count - 1] = filter;
393 /* search intelligently, once we insert in order */
394 AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
396 GraphContext *graph = ctx->priv;
402 for(i = 0; i < graph->filter_count; i ++)
403 if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
404 return graph->filters[i];
409 int avfilter_graph_config_links(AVFilterContext *graphctx)
411 GraphContext *graph = graphctx->priv;
414 for(i = 0; i < graph->filter_count; i ++) {
415 for(j = 0; j < graph->filters[i]->input_count; j ++) {
416 /* ensure that graphs contained within graphs are configured */
417 if((graph->filters[i]->filter == &avfilter_vf_graph ||
418 graph->filters[i]->filter == &avfilter_vf_graphfile ||
419 graph->filters[i]->filter == &avfilter_vf_graphdesc) &&
420 avfilter_graph_config_links(graph->filters[i]))
422 if(avfilter_config_link(graph->filters[i]->inputs[j]))
430 static AVFilterContext *create_filter_with_args(const char *filt, void *opaque)
432 AVFilterContext *ret;
433 char *filter = av_strdup(filt); /* copy - don't mangle the input string */
437 if((args = strchr(filter, '='))) {
438 /* ensure we at least have a name */
445 av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
446 name, args ? args : "(none)");
448 if((ret = avfilter_open(avfilter_get_by_name(name), NULL))) {
449 if(avfilter_init_filter(ret, args, opaque)) {
450 av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
451 avfilter_destroy(ret);
455 av_log(NULL, AV_LOG_ERROR,
456 "error creating filter \"%s\" with args \"%s\"\n",
457 name, args ? args : "(none)");
470 static int graph_load_chain(AVFilterContext *graphctx,
471 unsigned count, char **filter_list, void **opaque,
472 AVFilterContext **first, AVFilterContext **last)
475 AVFilterContext *filters[2] = {NULL,NULL};
477 for(i = 0; i < count; i ++) {
480 if(opaque) op = opaque[i];
483 if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
486 if(first) *first = filters[1];
488 if(avfilter_link(filters[0], 0, filters[1], 0)) {
489 av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
493 avfilter_graph_add_filter(graphctx, filters[1]);
494 if(i == 0 && filters[1]->input_count > 0)
495 add_graph_input(graphctx, filters[1], 0, "default");
496 filters[0] = filters[1];
499 if(filters[1]->output_count > 0)
500 add_graph_output(graphctx, filters[1], 0, "default");
502 if(last) *last = filters[1];
507 if(first) *first = NULL;
508 if(last) *last = NULL;
512 static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
513 AVFilterContext **first,
514 AVFilterContext **last)
520 strings = av_malloc(sizeof(char *));
521 strings[0] = av_strdup(str);
523 filt = strchr(strings[0], ',');
524 for(count = 1; filt; count ++) {
525 if(filt == strings[count-1]) {
530 strings = av_realloc(strings, sizeof(char *) * (count+1));
531 strings[count] = filt + 1;
533 filt = strchr(strings[count], ',');
536 ret = graph_load_chain(ctx, count, strings, NULL, first, last);
545 static int init(AVFilterContext *ctx, const char *args, void *opaque)
547 GraphContext *gctx = ctx->priv;
549 if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
551 if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
557 return graph_load_chain_from_string(ctx, args, NULL, NULL);
560 avfilter_destroy(gctx->link_filter);
564 AVFilter avfilter_vf_graph =
567 .author = "Bobby Bingham",
569 .priv_size = sizeof(GraphContext),
574 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
575 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
578 static int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc)
580 AVFilterGraphDescFilter *curfilt;
581 AVFilterGraphDescLink *curlink;
582 AVFilterGraphDescExport *curpad;
583 AVFilterContext *filt, *filtb;
587 /* create all filters */
588 for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
589 if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
590 !(filt = avfilter_open(filterdef, curfilt->name))) {
591 av_log(ctx, AV_LOG_ERROR, "error creating filter\n");
594 avfilter_graph_add_filter(ctx, filt);
595 if(avfilter_init_filter(filt, curfilt->args, NULL)) {
596 av_log(ctx, AV_LOG_ERROR, "error initializing filter\n");
601 /* create all links */
602 for(curlink = desc->links; curlink; curlink = curlink->next) {
603 if(!(filt = avfilter_graph_get_filter(ctx, curlink->src))) {
604 av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
607 if(!(filtb = avfilter_graph_get_filter(ctx, curlink->dst))) {
608 av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
611 if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
612 av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
617 /* export all input pads */
618 for(curpad = desc->inputs; curpad; curpad = curpad->next) {
619 if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
620 av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
623 add_graph_input(ctx, filt, curpad->pad, curpad->name);
626 /* export all output pads */
627 for(curpad = desc->outputs; curpad; curpad = curpad->next) {
628 if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
629 av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
632 add_graph_output(ctx, filt, curpad->pad, curpad->name);
642 static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
644 GraphContext *gctx = ctx->priv;
649 if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
651 if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
654 return graph_load_from_desc(ctx, opaque);
657 avfilter_destroy(gctx->link_filter);
661 AVFilter avfilter_vf_graphdesc =
663 .name = "graph_desc",
664 .author = "Bobby Bingham",
666 .priv_size = sizeof(GraphContext),
671 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
672 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
675 static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
677 AVFilterGraphDesc *desc;
682 if(!(desc = avfilter_graph_load_desc(args)))
685 ret = init_desc(ctx, NULL, desc);
686 avfilter_graph_free_desc(desc);
690 AVFilter avfilter_vf_graphfile =
692 .name = "graph_file",
693 .author = "Bobby Bingham",
695 .priv_size = sizeof(GraphContext),
700 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
701 .outputs = (AVFilterPad[]) {{ .name = NULL, }},