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 int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc)
497 AVFilterGraphDescFilter *curfilt;
498 AVFilterGraphDescLink *curlink;
499 AVFilterGraphDescExport *curpad;
500 AVFilterContext *filt, *filtb;
504 /* create all filters */
505 for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
506 if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
507 !(filt = avfilter_open(filterdef, curfilt->name))) {
508 av_log(ctx, AV_LOG_ERROR, "error creating filter\n");
511 avfilter_graph_add_filter(ctx, filt);
512 if(avfilter_init_filter(filt, curfilt->args, NULL)) {
513 av_log(ctx, AV_LOG_ERROR, "error initializing filter\n");
518 /* create all links */
519 for(curlink = desc->links; curlink; curlink = curlink->next) {
520 if(!(filt = avfilter_graph_get_filter(ctx, curlink->src))) {
521 av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
524 if(!(filtb = avfilter_graph_get_filter(ctx, curlink->dst))) {
525 av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
528 if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
529 av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
534 /* export all input pads */
535 for(curpad = desc->inputs; curpad; curpad = curpad->next) {
536 if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
537 av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
540 add_graph_input(ctx, filt, curpad->pad, curpad->name);
543 /* export all output pads */
544 for(curpad = desc->outputs; curpad; curpad = curpad->next) {
545 if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
546 av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
549 add_graph_output(ctx, filt, curpad->pad, curpad->name);
559 static int init(AVFilterContext *ctx, const char *args, void *opaque)
561 GraphContext *gctx = ctx->priv;
562 AVFilterGraphDesc *desc;
565 if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
567 if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
573 if(!(desc = avfilter_graph_parse_chain(args)))
576 ret = graph_load_from_desc(ctx, desc);
577 avfilter_graph_free_desc(desc);
581 avfilter_destroy(gctx->link_filter);
585 AVFilter avfilter_vf_graph =
588 .author = "Bobby Bingham",
590 .priv_size = sizeof(GraphContext),
595 .query_formats = query_formats,
597 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
598 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
601 static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
603 GraphContext *gctx = ctx->priv;
608 if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
610 if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
613 return graph_load_from_desc(ctx, opaque);
616 avfilter_destroy(gctx->link_filter);
620 AVFilter avfilter_vf_graphdesc =
622 .name = "graph_desc",
623 .author = "Bobby Bingham",
625 .priv_size = sizeof(GraphContext),
630 .query_formats = query_formats,
632 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
633 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
636 static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
638 AVFilterGraphDesc *desc;
643 if(!(desc = avfilter_graph_load_desc(args)))
646 ret = init_desc(ctx, NULL, desc);
647 avfilter_graph_free_desc(desc);
651 AVFilter avfilter_vf_graphfile =
653 .name = "graph_file",
654 .author = "Bobby Bingham",
656 .priv_size = sizeof(GraphContext),
661 .query_formats = query_formats,
663 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
664 .outputs = (AVFilterPad[]) {{ .name = NULL, }},