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 /** request a frame from a filter providing input to the graph */
62 static int link_in_request_frame(AVFilterLink *link)
64 AVFilterLink *link2 = get_extern_input_link(link);
68 return avfilter_request_frame(link2);
71 static int link_in_config_props(AVFilterLink *link)
73 AVFilterLink *link2 = get_extern_input_link(link);
74 int (*config_props)(AVFilterLink *);
79 if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
80 config_props = avfilter_default_config_output_link;
81 ret = config_props(link2);
90 * Given the link between the dummy filter and an internal filter whose input
91 * is being exported outside the graph, this returns the externally visible
94 static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
96 GraphLinkContext *lctx = link->dst->priv;
97 return lctx->graph->outputs[link->dstpad];
100 static int link_out_config_props(AVFilterLink *link)
102 AVFilterLink *link2 = get_extern_output_link(link);
103 int (*config_props)(AVFilterLink *);
111 if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
112 config_props = avfilter_default_config_input_link;
113 return config_props(link2);
116 static void link_out_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
118 AVFilterLink *link2 = get_extern_output_link(link);
121 avfilter_unref_pic(picref);
123 avfilter_start_frame(link2, picref);
126 static void link_out_end_frame(AVFilterLink *link)
128 AVFilterLink *link2 = get_extern_output_link(link);
131 avfilter_end_frame(link2);
134 static AVFilterPicRef *link_out_get_video_buffer(AVFilterLink *link, int perms)
136 AVFilterLink *link2 = get_extern_output_link(link);
141 return avfilter_get_video_buffer(link2, perms);
144 static void link_out_draw_slice(AVFilterLink *link, int y, int height)
146 AVFilterLink *link2 = get_extern_output_link(link);
149 avfilter_draw_slice(link2, y, height);
152 /** dummy filter used to help export filters pads outside the graph */
153 static AVFilter vf_graph_dummy =
155 .name = "graph_dummy",
156 .author = "Bobby Bingham",
158 .priv_size = sizeof(GraphLinkContext),
162 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
163 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
166 static AVFilterLink *get_intern_input_link(AVFilterLink *link)
168 GraphContext *graph = link->dst->priv;
169 return graph->link_filter->outputs[link->dstpad];
172 static void graph_in_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
174 AVFilterLink *link2 = get_intern_input_link(link);
176 avfilter_start_frame(link2, picref);
179 static void graph_in_end_frame(AVFilterLink *link)
181 AVFilterLink *link2 = get_intern_input_link(link);
183 avfilter_end_frame(link2);
186 static AVFilterPicRef *graph_in_get_video_buffer(AVFilterLink *link, int perms)
188 AVFilterLink *link2 = get_intern_input_link(link);
190 return avfilter_get_video_buffer(link2, perms);
194 static void graph_in_draw_slice(AVFilterLink *link, int y, int height)
196 AVFilterLink *link2 = get_intern_input_link(link);
198 avfilter_draw_slice(link2, y, height);
201 static int graph_in_config_props(AVFilterLink *link)
203 AVFilterLink *link2 = get_intern_input_link(link);
204 int (*config_props)(AVFilterLink *);
209 /* copy link properties over to the dummy internal link */
212 link2->format = link->format;
214 if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
215 return 0; /* FIXME? */
216 //config_props = avfilter_default_config_input_link;
217 return config_props(link2);
220 static AVFilterLink *get_intern_output_link(AVFilterLink *link)
222 GraphContext *graph = link->src->priv;
223 return graph->link_filter->inputs[link->srcpad];
226 static int graph_out_request_frame(AVFilterLink *link)
228 AVFilterLink *link2 = get_intern_output_link(link);
231 return avfilter_request_frame(link2);
235 static int graph_out_config_props(AVFilterLink *link)
237 AVFilterLink *link2 = get_intern_output_link(link);
238 int (*config_props)(AVFilterLink *);
246 link2->format = link->format;
248 if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
249 config_props = avfilter_default_config_output_link;
250 ret = config_props(link2);
254 link->format = link2->format;
259 static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
262 GraphContext *graph = gctx->priv;
264 AVFilterPad graph_inpad =
267 .type = AV_PAD_VIDEO,
268 .start_frame = graph_in_start_frame,
269 .end_frame = graph_in_end_frame,
270 .get_video_buffer = graph_in_get_video_buffer,
271 .draw_slice = graph_in_draw_slice,
272 .config_props = graph_in_config_props,
275 AVFilterPad dummy_outpad =
277 .name = NULL, /* FIXME? */
278 .type = AV_PAD_VIDEO,
279 .request_frame = link_in_request_frame,
280 .config_props = link_in_config_props,
283 avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
284 avfilter_insert_outpad(graph->link_filter, graph->link_filter->output_count,
286 return avfilter_link(graph->link_filter,
287 graph->link_filter->output_count-1, filt, idx);
290 static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
293 GraphContext *graph = gctx->priv;
295 AVFilterPad graph_outpad =
298 .type = AV_PAD_VIDEO,
299 .request_frame = graph_out_request_frame,
300 .config_props = graph_out_config_props,
302 AVFilterPad dummy_inpad =
304 .name = NULL, /* FIXME? */
305 .type = AV_PAD_VIDEO,
306 .start_frame = link_out_start_frame,
307 .end_frame = link_out_end_frame,
308 .draw_slice = link_out_draw_slice,
309 .get_video_buffer = link_out_get_video_buffer,
310 .config_props = link_out_config_props,
313 avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
314 avfilter_insert_inpad (graph->link_filter, graph->link_filter->input_count,
316 return avfilter_link(filt, idx, graph->link_filter,
317 graph->link_filter->input_count-1);
320 static void uninit(AVFilterContext *ctx)
322 GraphContext *graph = ctx->priv;
324 if(graph->link_filter) {
325 avfilter_destroy(graph->link_filter);
326 graph->link_filter = NULL;
328 for(; graph->filter_count > 0; graph->filter_count --)
329 avfilter_destroy(graph->filters[graph->filter_count - 1]);
330 av_freep(&graph->filters);
333 /* TODO: insert in sorted order */
334 void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
336 GraphContext *graph = graphctx->priv;
338 graph->filters = av_realloc(graph->filters,
339 sizeof(AVFilterContext*) * ++graph->filter_count);
340 graph->filters[graph->filter_count - 1] = filter;
343 /* search intelligently, once we insert in order */
344 AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
346 GraphContext *graph = ctx->priv;
352 for(i = 0; i < graph->filter_count; i ++)
353 if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
354 return graph->filters[i];
359 static int query_formats(AVFilterContext *graphctx)
361 GraphContext *graph = graphctx->priv;
362 AVFilterContext *linkfilt = graph->link_filter;
365 /* ask all the sub-filters for their supported colorspaces */
366 for(i = 0; i < graph->filter_count; i ++) {
367 if(graph->filters[i]->filter->query_formats)
368 graph->filters[i]->filter->query_formats(graph->filters[i]);
370 avfilter_default_query_formats(graph->filters[i]);
373 /* use these formats on our exported links */
374 for(i = 0; i < linkfilt->input_count; i ++) {
375 avfilter_formats_ref( linkfilt->inputs[i]->in_formats,
376 &linkfilt->inputs[i]->out_formats);
378 if(graphctx->outputs[i])
379 avfilter_formats_ref( linkfilt-> inputs[i]->in_formats,
380 &graphctx->outputs[i]->in_formats);
382 for(i = 0; i < linkfilt->output_count; i ++) {
383 avfilter_formats_ref( linkfilt->outputs[i]->out_formats,
384 &linkfilt->outputs[i]->in_formats);
386 if(graphctx->inputs[i])
387 avfilter_formats_ref( linkfilt->outputs[i]->out_formats,
388 &graphctx-> inputs[i]->out_formats);
391 /* go through and merge as many format lists as possible */
392 for(i = 0; i < graph->filter_count; i ++) {
393 AVFilterContext *filter = graph->filters[i];
395 for(j = 0; j < filter->input_count; j ++) {
397 if(!(link = filter->inputs[j]))
399 if(link->in_formats != link->out_formats) {
400 if(!avfilter_merge_formats(link->in_formats,
401 link->out_formats)) {
402 /* couldn't merge format lists. auto-insert scale filter */
403 AVFilterContext *scale;
405 if(!(scale = avfilter_open(&avfilter_vf_scale, NULL)))
407 if(scale->filter->init(scale, NULL, NULL) ||
408 avfilter_insert_filter(link, scale, 0, 0)) {
409 avfilter_destroy(scale);
413 avfilter_graph_add_filter(graphctx, scale);
414 scale->filter->query_formats(scale);
415 if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
416 scale-> inputs[0]->out_formats) ||
417 !avfilter_merge_formats(scale->outputs[0]->in_formats,
418 scale->outputs[0]->out_formats))
428 static void pick_format(AVFilterLink *link)
430 if(!link || !link->in_formats)
433 link->in_formats->format_count = 1;
434 link->format = link->in_formats->formats[0];
436 avfilter_formats_unref(&link->in_formats);
437 avfilter_formats_unref(&link->out_formats);
440 static void pick_formats(GraphContext *graph)
444 for(i = 0; i < graph->filter_count; i ++) {
445 AVFilterContext *filter = graph->filters[i];
447 if(filter->filter == &avfilter_vf_graph ||
448 filter->filter == &avfilter_vf_graphfile ||
449 filter->filter == &avfilter_vf_graphdesc)
450 pick_formats(filter->priv);
452 for(j = 0; j < filter->input_count; j ++)
453 pick_format(filter->inputs[j]);
454 for(j = 0; j < filter->output_count; j ++)
455 pick_format(filter->outputs[j]);
459 int avfilter_graph_config_formats(AVFilterContext *graphctx)
461 GraphContext *graph = graphctx->priv;
463 /* Find supported formats from sub-filters, and merge along links */
464 if(query_formats(graphctx))
467 /* Once everything is merged, it's possible that we'll still have
468 * multiple valid choices of colorspace. We pick the first one. */
474 int avfilter_graph_config_links(AVFilterContext *graphctx)
476 GraphContext *graph = graphctx->priv;
479 for(i = 0; i < graph->filter_count; i ++) {
480 for(j = 0; j < graph->filters[i]->input_count; j ++) {
481 /* ensure that graphs contained within graphs are configured */
482 if((graph->filters[i]->filter == &avfilter_vf_graph ||
483 graph->filters[i]->filter == &avfilter_vf_graphfile ||
484 graph->filters[i]->filter == &avfilter_vf_graphdesc) &&
485 avfilter_graph_config_links(graph->filters[i]))
487 if(avfilter_config_link(graph->filters[i]->inputs[j]))
495 static AVFilterContext *create_filter_with_args(const char *filt, void *opaque)
497 AVFilterContext *ret;
498 char *filter = av_strdup(filt); /* copy - don't mangle the input string */
502 if((args = strchr(filter, '='))) {
503 /* ensure we at least have a name */
510 av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
511 name, args ? args : "(none)");
513 if((ret = avfilter_open(avfilter_get_by_name(name), NULL))) {
514 if(avfilter_init_filter(ret, args, opaque)) {
515 av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
516 avfilter_destroy(ret);
520 av_log(NULL, AV_LOG_ERROR,
521 "error creating filter \"%s\" with args \"%s\"\n",
522 name, args ? args : "(none)");
534 static int graph_load_chain(AVFilterContext *graphctx,
535 unsigned count, char **filter_list, void **opaque,
536 AVFilterContext **first, AVFilterContext **last)
539 AVFilterContext *filters[2] = {NULL,NULL};
541 for(i = 0; i < count; i ++) {
544 if(opaque) op = opaque[i];
547 if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
550 if(first) *first = filters[1];
552 if(avfilter_link(filters[0], 0, filters[1], 0)) {
553 av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
557 avfilter_graph_add_filter(graphctx, filters[1]);
558 if(i == 0 && filters[1]->input_count > 0)
559 add_graph_input(graphctx, filters[1], 0, "default");
560 filters[0] = filters[1];
563 if(filters[1]->output_count > 0)
564 add_graph_output(graphctx, filters[1], 0, "default");
566 if(last) *last = filters[1];
571 if(first) *first = NULL;
572 if(last) *last = NULL;
576 static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
577 AVFilterContext **first,
578 AVFilterContext **last)
584 strings = av_malloc(sizeof(char *));
585 strings[0] = av_strdup(str);
587 filt = strchr(strings[0], ',');
588 for(count = 1; filt; count ++) {
589 if(filt == strings[count-1]) {
594 strings = av_realloc(strings, sizeof(char *) * (count+1));
595 strings[count] = filt + 1;
597 filt = strchr(strings[count], ',');
600 ret = graph_load_chain(ctx, count, strings, NULL, first, last);
609 static int init(AVFilterContext *ctx, const char *args, void *opaque)
611 GraphContext *gctx = ctx->priv;
613 if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
615 if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
621 return graph_load_chain_from_string(ctx, args, NULL, NULL);
624 avfilter_destroy(gctx->link_filter);
628 AVFilter avfilter_vf_graph =
631 .author = "Bobby Bingham",
633 .priv_size = sizeof(GraphContext),
638 .query_formats = query_formats,
640 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
641 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
644 static int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc)
646 AVFilterGraphDescFilter *curfilt;
647 AVFilterGraphDescLink *curlink;
648 AVFilterGraphDescExport *curpad;
649 AVFilterContext *filt, *filtb;
653 /* create all filters */
654 for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
655 if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
656 !(filt = avfilter_open(filterdef, curfilt->name))) {
657 av_log(ctx, AV_LOG_ERROR, "error creating filter\n");
660 avfilter_graph_add_filter(ctx, filt);
661 if(avfilter_init_filter(filt, curfilt->args, NULL)) {
662 av_log(ctx, AV_LOG_ERROR, "error initializing filter\n");
667 /* create all links */
668 for(curlink = desc->links; curlink; curlink = curlink->next) {
669 if(!(filt = avfilter_graph_get_filter(ctx, curlink->src))) {
670 av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
673 if(!(filtb = avfilter_graph_get_filter(ctx, curlink->dst))) {
674 av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
677 if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
678 av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
683 /* export all input pads */
684 for(curpad = desc->inputs; curpad; curpad = curpad->next) {
685 if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
686 av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
689 add_graph_input(ctx, filt, curpad->pad, curpad->name);
692 /* export all output pads */
693 for(curpad = desc->outputs; curpad; curpad = curpad->next) {
694 if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
695 av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
698 add_graph_output(ctx, filt, curpad->pad, curpad->name);
708 static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
710 GraphContext *gctx = ctx->priv;
715 if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
717 if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
720 return graph_load_from_desc(ctx, opaque);
723 avfilter_destroy(gctx->link_filter);
727 AVFilter avfilter_vf_graphdesc =
729 .name = "graph_desc",
730 .author = "Bobby Bingham",
732 .priv_size = sizeof(GraphContext),
737 .query_formats = query_formats,
739 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
740 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
743 static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
745 AVFilterGraphDesc *desc;
750 if(!(desc = avfilter_graph_load_desc(args)))
753 ret = init_desc(ctx, NULL, desc);
754 avfilter_graph_free_desc(desc);
758 AVFilter avfilter_vf_graphfile =
760 .name = "graph_file",
761 .author = "Bobby Bingham",
763 .priv_size = sizeof(GraphContext),
768 .query_formats = query_formats,
770 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
771 .outputs = (AVFilterPad[]) {{ .name = NULL, }},