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 filters to handle links to internal filters */
36 AVFilterContext *link_filter_in;
37 AVFilterContext *link_filter_out;
41 AVFilterContext *graph;
44 static int link_init(AVFilterContext *ctx, const char *args, void *opaque)
46 GraphLinkContext *linkctx = ctx->priv;
47 linkctx->graph = opaque;
52 * Given the link between the dummy filter and an internal filter whose input
53 * is being exported outside the graph, this returns the externally visible
56 static inline AVFilterLink *get_extern_input_link(AVFilterLink *link)
58 GraphLinkContext *lctx = link->src->priv;
59 return lctx->graph->inputs[link->srcpad];
62 /** request a frame from a filter providing input to the graph */
63 static int link_in_request_frame(AVFilterLink *link)
65 AVFilterLink *link2 = get_extern_input_link(link);
69 return avfilter_request_frame(link2);
72 static int link_in_config_props(AVFilterLink *link)
74 AVFilterLink *link2 = get_extern_input_link(link);
75 int (*config_props)(AVFilterLink *);
80 if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
81 config_props = avfilter_default_config_output_link;
82 ret = config_props(link2);
91 * Given the link between the dummy filter and an internal filter whose input
92 * is being exported outside the graph, this returns the externally visible
95 static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
97 GraphLinkContext *lctx = link->dst->priv;
98 return lctx->graph->outputs[link->dstpad];
101 static int link_out_config_props(AVFilterLink *link)
103 AVFilterLink *link2 = get_extern_output_link(link);
114 static void link_out_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
116 AVFilterLink *link2 = get_extern_output_link(link);
119 avfilter_unref_pic(picref);
121 avfilter_start_frame(link2, picref);
124 static void link_out_end_frame(AVFilterLink *link)
126 AVFilterLink *link2 = get_extern_output_link(link);
129 avfilter_end_frame(link2);
132 static AVFilterPicRef *link_out_get_video_buffer(AVFilterLink *link, int perms)
134 AVFilterLink *link2 = get_extern_output_link(link);
139 return avfilter_get_video_buffer(link2, perms);
142 static void link_out_draw_slice(AVFilterLink *link, int y, int height)
144 AVFilterLink *link2 = get_extern_output_link(link);
147 avfilter_draw_slice(link2, y, height);
150 /** dummy filter used to help export filters pads outside the graph */
151 static AVFilter vf_graph_dummy =
153 .name = "graph_dummy",
154 .author = "Bobby Bingham",
156 .priv_size = sizeof(GraphLinkContext),
160 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
161 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
164 static AVFilterLink *get_intern_input_link(AVFilterLink *link)
166 GraphContext *graph = link->dst->priv;
167 return graph->link_filter_in->outputs[link->dstpad];
170 static void graph_in_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
172 AVFilterLink *link2 = get_intern_input_link(link);
174 avfilter_start_frame(link2, picref);
177 static void graph_in_end_frame(AVFilterLink *link)
179 AVFilterLink *link2 = get_intern_input_link(link);
181 avfilter_end_frame(link2);
184 static AVFilterPicRef *graph_in_get_video_buffer(AVFilterLink *link, int perms)
186 AVFilterLink *link2 = get_intern_input_link(link);
188 return avfilter_get_video_buffer(link2, perms);
192 static void graph_in_draw_slice(AVFilterLink *link, int y, int height)
194 AVFilterLink *link2 = get_intern_input_link(link);
196 avfilter_draw_slice(link2, y, height);
199 static int graph_in_config_props(AVFilterLink *link)
201 AVFilterLink *link2 = get_intern_input_link(link);
202 int (*config_props)(AVFilterLink *);
208 /* copy link properties over to the dummy internal link */
211 link2->format = link->format;
213 if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
214 return 0; /* FIXME? */
215 //config_props = avfilter_default_config_input_link;
216 if(!(ret = config_props(link2)))
217 link2->init_state = AVLINK_INIT;
219 link2->init_state = AVLINK_STARTINIT;
224 static AVFilterLink *get_intern_output_link(AVFilterLink *link)
226 GraphContext *graph = link->src->priv;
227 return graph->link_filter_out->inputs[link->srcpad];
230 static int graph_out_request_frame(AVFilterLink *link)
232 AVFilterLink *link2 = get_intern_output_link(link);
235 return avfilter_request_frame(link2);
239 static int graph_out_config_props(AVFilterLink *link)
241 GraphContext *graph = link->src->priv;
242 AVFilterLink *link2 = graph->link_filter_out->inputs[link->srcpad];
245 if((ret = avfilter_config_links(graph->link_filter_out)))
253 link->format = link2->format;
258 static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
261 GraphContext *graph = gctx->priv;
263 AVFilterPad graph_inpad =
266 .type = AV_PAD_VIDEO,
267 .start_frame = graph_in_start_frame,
268 .end_frame = graph_in_end_frame,
269 .get_video_buffer = graph_in_get_video_buffer,
270 .draw_slice = graph_in_draw_slice,
271 .config_props = graph_in_config_props,
274 AVFilterPad dummy_outpad =
276 .name = NULL, /* FIXME? */
277 .type = AV_PAD_VIDEO,
278 .request_frame = link_in_request_frame,
279 .config_props = link_in_config_props,
282 avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
283 avfilter_insert_outpad(graph->link_filter_in, graph->link_filter_in->output_count,
285 return avfilter_link(graph->link_filter_in,
286 graph->link_filter_in->output_count-1, filt, idx);
289 static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
292 GraphContext *graph = gctx->priv;
294 AVFilterPad graph_outpad =
297 .type = AV_PAD_VIDEO,
298 .request_frame = graph_out_request_frame,
299 .config_props = graph_out_config_props,
301 AVFilterPad dummy_inpad =
303 .name = NULL, /* FIXME? */
304 .type = AV_PAD_VIDEO,
305 .start_frame = link_out_start_frame,
306 .end_frame = link_out_end_frame,
307 .draw_slice = link_out_draw_slice,
308 .get_video_buffer = link_out_get_video_buffer,
309 .config_props = link_out_config_props,
312 avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
313 avfilter_insert_inpad (graph->link_filter_out, graph->link_filter_out->input_count,
315 return avfilter_link(filt, idx, graph->link_filter_out,
316 graph->link_filter_out->input_count-1);
319 static void uninit(AVFilterContext *ctx)
321 GraphContext *graph = ctx->priv;
323 if(graph->link_filter_in) {
324 avfilter_destroy(graph->link_filter_in);
325 graph->link_filter_in = NULL;
327 if(graph->link_filter_out) {
328 avfilter_destroy(graph->link_filter_out);
329 graph->link_filter_out = NULL;
331 for(; graph->filter_count > 0; graph->filter_count --)
332 avfilter_destroy(graph->filters[graph->filter_count - 1]);
333 av_freep(&graph->filters);
336 /* TODO: insert in sorted order */
337 void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
339 GraphContext *graph = graphctx->priv;
341 graph->filters = av_realloc(graph->filters,
342 sizeof(AVFilterContext*) * ++graph->filter_count);
343 graph->filters[graph->filter_count - 1] = filter;
346 /* search intelligently, once we insert in order */
347 AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
349 GraphContext *graph = ctx->priv;
355 for(i = 0; i < graph->filter_count; i ++)
356 if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
357 return graph->filters[i];
362 static int query_formats(AVFilterContext *graphctx)
364 GraphContext *graph = graphctx->priv;
365 AVFilterContext *linkfiltin = graph->link_filter_in;
366 AVFilterContext *linkfiltout = graph->link_filter_out;
369 /* ask all the sub-filters for their supported colorspaces */
370 for(i = 0; i < graph->filter_count; i ++) {
371 if(graph->filters[i]->filter->query_formats)
372 graph->filters[i]->filter->query_formats(graph->filters[i]);
374 avfilter_default_query_formats(graph->filters[i]);
377 /* use these formats on our exported links */
378 for(i = 0; i < linkfiltout->input_count; i ++) {
379 avfilter_formats_ref( linkfiltout->inputs[i]->in_formats,
380 &linkfiltout->inputs[i]->out_formats);
382 if(graphctx->outputs[i])
383 avfilter_formats_ref(linkfiltout->inputs[i]->in_formats,
384 &graphctx->outputs[i]->in_formats);
386 for(i = 0; i < linkfiltin->output_count; i ++) {
387 avfilter_formats_ref( linkfiltin->outputs[i]->out_formats,
388 &linkfiltin->outputs[i]->in_formats);
390 if(graphctx->inputs[i])
391 avfilter_formats_ref(linkfiltin->outputs[i]->out_formats,
392 &graphctx-> inputs[i]->out_formats);
395 /* go through and merge as many format lists as possible */
396 for(i = 0; i < graph->filter_count; i ++) {
397 AVFilterContext *filter = graph->filters[i];
399 for(j = 0; j < filter->input_count; j ++) {
401 if(!(link = filter->inputs[j]))
403 if(link->in_formats != link->out_formats) {
404 if(!avfilter_merge_formats(link->in_formats,
405 link->out_formats)) {
406 /* couldn't merge format lists. auto-insert scale filter */
407 AVFilterContext *scale;
409 if(!(scale = avfilter_open(&avfilter_vf_scale, NULL)))
411 if(scale->filter->init(scale, NULL, NULL) ||
412 avfilter_insert_filter(link, scale, 0, 0)) {
413 avfilter_destroy(scale);
417 avfilter_graph_add_filter(graphctx, scale);
418 scale->filter->query_formats(scale);
419 if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
420 scale-> inputs[0]->out_formats) ||
421 !avfilter_merge_formats(scale->outputs[0]->in_formats,
422 scale->outputs[0]->out_formats))
432 static void pick_format(AVFilterLink *link)
434 if(!link || !link->in_formats)
437 link->in_formats->format_count = 1;
438 link->format = link->in_formats->formats[0];
440 avfilter_formats_unref(&link->in_formats);
441 avfilter_formats_unref(&link->out_formats);
444 static void pick_formats(GraphContext *graph)
448 for(i = 0; i < graph->filter_count; i ++) {
449 AVFilterContext *filter = graph->filters[i];
451 if(filter->filter == &avfilter_vf_graph ||
452 filter->filter == &avfilter_vf_graphfile ||
453 filter->filter == &avfilter_vf_graphdesc)
454 pick_formats(filter->priv);
456 for(j = 0; j < filter->input_count; j ++)
457 pick_format(filter->inputs[j]);
458 for(j = 0; j < filter->output_count; j ++)
459 pick_format(filter->outputs[j]);
463 int avfilter_graph_config_formats(AVFilterContext *graphctx)
465 GraphContext *graph = graphctx->priv;
467 /* Find supported formats from sub-filters, and merge along links */
468 if(query_formats(graphctx))
471 /* Once everything is merged, it's possible that we'll still have
472 * multiple valid choices of colorspace. We pick the first one. */
478 static int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc)
480 AVFilterGraphDescFilter *curfilt;
481 AVFilterGraphDescLink *curlink;
482 AVFilterGraphDescExport *curpad;
483 AVFilterContext *filt, *filtb;
487 /* create all filters */
488 for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
489 if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
490 !(filt = avfilter_open(filterdef, curfilt->name))) {
491 av_log(ctx, AV_LOG_ERROR, "error creating filter\n");
494 avfilter_graph_add_filter(ctx, filt);
495 if(avfilter_init_filter(filt, curfilt->args, NULL)) {
496 av_log(ctx, AV_LOG_ERROR, "error initializing filter\n");
501 /* create all links */
502 for(curlink = desc->links; curlink; curlink = curlink->next) {
503 if(!(filt = avfilter_graph_get_filter(ctx, curlink->src))) {
504 av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
507 if(!(filtb = avfilter_graph_get_filter(ctx, curlink->dst))) {
508 av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
511 if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
512 av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
517 /* export all input pads */
518 for(curpad = desc->inputs; curpad; curpad = curpad->next) {
519 if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
520 av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
523 add_graph_input(ctx, filt, curpad->pad, curpad->name);
526 /* export all output pads */
527 for(curpad = desc->outputs; curpad; curpad = curpad->next) {
528 if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
529 av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
532 add_graph_output(ctx, filt, curpad->pad, curpad->name);
542 static int init(AVFilterContext *ctx, const char *args, void *opaque)
544 GraphContext *gctx = ctx->priv;
545 AVFilterGraphDesc *desc;
548 if(!(gctx->link_filter_in = avfilter_open(&vf_graph_dummy, NULL)))
550 if(avfilter_init_filter(gctx->link_filter_in, NULL, ctx))
552 if(!(gctx->link_filter_out = avfilter_open(&vf_graph_dummy, NULL)))
554 if(avfilter_init_filter(gctx->link_filter_out, NULL, ctx))
560 if(!(desc = avfilter_graph_parse_chain(args)))
563 ret = graph_load_from_desc(ctx, desc);
564 avfilter_graph_free_desc(desc);
568 avfilter_destroy(gctx->link_filter_in);
569 if(gctx->link_filter_out)
570 avfilter_destroy(gctx->link_filter_out);
574 AVFilter avfilter_vf_graph =
577 .author = "Bobby Bingham",
579 .priv_size = sizeof(GraphContext),
584 .query_formats = query_formats,
586 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
587 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
590 static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
592 GraphContext *gctx = ctx->priv;
597 if(!(gctx->link_filter_in = avfilter_open(&vf_graph_dummy, NULL)))
599 if(avfilter_init_filter(gctx->link_filter_in, NULL, ctx))
601 if(!(gctx->link_filter_out = avfilter_open(&vf_graph_dummy, NULL)))
603 if(avfilter_init_filter(gctx->link_filter_out, NULL, ctx))
606 return graph_load_from_desc(ctx, opaque);
609 avfilter_destroy(gctx->link_filter_in);
610 if(gctx->link_filter_out)
611 avfilter_destroy(gctx->link_filter_out);
615 AVFilter avfilter_vf_graphdesc =
617 .name = "graph_desc",
618 .author = "Bobby Bingham",
620 .priv_size = sizeof(GraphContext),
625 .query_formats = query_formats,
627 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
628 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
631 static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
633 AVFilterGraphDesc *desc;
638 if(!(desc = avfilter_graph_load_desc(args)))
641 ret = init_desc(ctx, NULL, desc);
642 avfilter_graph_free_desc(desc);
646 AVFilter avfilter_vf_graphfile =
648 .name = "graph_file",
649 .author = "Bobby Bingham",
651 .priv_size = sizeof(GraphContext),
656 .query_formats = query_formats,
658 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
659 .outputs = (AVFilterPad[]) {{ .name = NULL, }},