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
23 #include "avfiltergraph.h"
25 #include "allfilters.h"
27 typedef struct AVFilterGraph {
28 unsigned filter_count;
29 AVFilterContext **filters;
31 /** fake filters to handle links to internal filters */
32 AVFilterContext *link_filter_in;
33 AVFilterContext *link_filter_out;
37 AVFilterContext *graph;
40 static int link_init(AVFilterContext *ctx, const char *args, void *opaque)
42 GraphLinkContext *linkctx = ctx->priv;
43 linkctx->graph = opaque;
48 * Given the link between the dummy filter and an internal filter whose input
49 * is being exported outside the graph, this returns the externally visible
52 static inline AVFilterLink *get_extern_input_link(AVFilterLink *link)
54 GraphLinkContext *lctx = link->src->priv;
55 return lctx->graph->inputs[link->srcpad];
58 /** request a frame from a filter providing input to the graph */
59 static int link_in_request_frame(AVFilterLink *link)
61 AVFilterLink *link2 = get_extern_input_link(link);
65 return avfilter_request_frame(link2);
69 static int link_in_poll_frame(AVFilterLink *link)
71 AVFilterLink *link2 = get_extern_input_link(link);
74 return avfilter_poll_frame(link2);
77 static int link_in_config_props(AVFilterLink *link)
79 AVFilterLink *link2 = get_extern_input_link(link);
80 int (*config_props)(AVFilterLink *);
85 if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
86 config_props = avfilter_default_config_output_link;
87 ret = config_props(link2);
96 * Given the link between the dummy filter and an internal filter whose input
97 * is being exported outside the graph, this returns the externally visible
100 static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
102 GraphLinkContext *lctx = link->dst->priv;
103 return lctx->graph->outputs[link->dstpad];
106 static int link_out_config_props(AVFilterLink *link)
108 AVFilterLink *link2 = get_extern_output_link(link);
119 static void link_out_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
121 AVFilterLink *link2 = get_extern_output_link(link);
124 avfilter_unref_pic(picref);
126 avfilter_start_frame(link2, picref);
129 static void link_out_end_frame(AVFilterLink *link)
131 AVFilterLink *link2 = get_extern_output_link(link);
134 avfilter_end_frame(link2);
137 static AVFilterPicRef *link_out_get_video_buffer(AVFilterLink *link, int perms)
139 AVFilterLink *link2 = get_extern_output_link(link);
144 return avfilter_get_video_buffer(link2, perms);
147 static void link_out_draw_slice(AVFilterLink *link, int y, int height)
149 AVFilterLink *link2 = get_extern_output_link(link);
152 avfilter_draw_slice(link2, y, height);
155 /** dummy filter used to help export filters pads outside the graph */
156 static AVFilter vf_graph_dummy =
158 .name = "graph_dummy",
159 .author = "Bobby Bingham",
161 .priv_size = sizeof(GraphLinkContext),
165 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
166 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
169 static AVFilterLink *get_intern_input_link(AVFilterLink *link)
171 GraphContext *graph = link->dst->priv;
172 return graph->link_filter_in->outputs[link->dstpad];
175 static void graph_in_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
177 AVFilterLink *link2 = get_intern_input_link(link);
179 avfilter_start_frame(link2, picref);
182 static void graph_in_end_frame(AVFilterLink *link)
184 AVFilterLink *link2 = get_intern_input_link(link);
186 avfilter_end_frame(link2);
189 static AVFilterPicRef *graph_in_get_video_buffer(AVFilterLink *link, int perms)
191 AVFilterLink *link2 = get_intern_input_link(link);
193 return avfilter_get_video_buffer(link2, perms);
197 static void graph_in_draw_slice(AVFilterLink *link, int y, int height)
199 AVFilterLink *link2 = get_intern_input_link(link);
201 avfilter_draw_slice(link2, y, height);
204 static int graph_in_config_props(AVFilterLink *link)
206 AVFilterLink *link2 = get_intern_input_link(link);
207 int (*config_props)(AVFilterLink *);
213 /* copy link properties over to the dummy internal link */
216 link2->format = link->format;
218 if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
219 return 0; /* FIXME? */
220 //config_props = avfilter_default_config_input_link;
221 if(!(ret = config_props(link2)))
222 link2->init_state = AVLINK_INIT;
224 link2->init_state = AVLINK_STARTINIT;
229 static AVFilterLink *get_intern_output_link(AVFilterLink *link)
231 GraphContext *graph = link->src->priv;
232 return graph->link_filter_out->inputs[link->srcpad];
235 static int graph_out_request_frame(AVFilterLink *link)
237 AVFilterLink *link2 = get_intern_output_link(link);
240 return avfilter_request_frame(link2);
244 static int graph_out_poll_frame(AVFilterLink *link)
246 AVFilterLink *link2 = get_intern_output_link(link);
251 return avfilter_poll_frame(link2);
254 static int graph_out_config_props(AVFilterLink *link)
256 GraphContext *graph = link->src->priv;
257 AVFilterLink *link2 = graph->link_filter_out->inputs[link->srcpad];
260 if((ret = avfilter_config_links(graph->link_filter_out)))
268 link->format = link2->format;
273 static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
276 GraphContext *graph = gctx->priv;
278 AVFilterPad graph_inpad =
281 .type = AV_PAD_VIDEO,
282 .start_frame = graph_in_start_frame,
283 .end_frame = graph_in_end_frame,
284 .get_video_buffer = graph_in_get_video_buffer,
285 .draw_slice = graph_in_draw_slice,
286 .config_props = graph_in_config_props,
289 AVFilterPad dummy_outpad =
291 .name = NULL, /* FIXME? */
292 .type = AV_PAD_VIDEO,
293 .request_frame = link_in_request_frame,
294 .poll_frame = link_in_poll_frame,
295 .config_props = link_in_config_props,
298 avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
299 avfilter_insert_outpad(graph->link_filter_in, graph->link_filter_in->output_count,
301 return avfilter_link(graph->link_filter_in,
302 graph->link_filter_in->output_count-1, filt, idx);
305 static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
308 GraphContext *graph = gctx->priv;
310 AVFilterPad graph_outpad =
313 .type = AV_PAD_VIDEO,
314 .request_frame = graph_out_request_frame,
315 .poll_frame = graph_out_poll_frame,
316 .config_props = graph_out_config_props,
318 AVFilterPad dummy_inpad =
320 .name = NULL, /* FIXME? */
321 .type = AV_PAD_VIDEO,
322 .start_frame = link_out_start_frame,
323 .end_frame = link_out_end_frame,
324 .draw_slice = link_out_draw_slice,
325 .get_video_buffer = link_out_get_video_buffer,
326 .config_props = link_out_config_props,
329 avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
330 avfilter_insert_inpad (graph->link_filter_out, graph->link_filter_out->input_count,
332 return avfilter_link(filt, idx, graph->link_filter_out,
333 graph->link_filter_out->input_count-1);
336 static void uninit(AVFilterContext *ctx)
338 GraphContext *graph = ctx->priv;
340 if(graph->link_filter_in) {
341 avfilter_destroy(graph->link_filter_in);
342 graph->link_filter_in = NULL;
344 if(graph->link_filter_out) {
345 avfilter_destroy(graph->link_filter_out);
346 graph->link_filter_out = NULL;
348 for(; graph->filter_count > 0; graph->filter_count --)
349 avfilter_destroy(graph->filters[graph->filter_count - 1]);
350 av_freep(&graph->filters);
353 /* TODO: insert in sorted order */
354 void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
356 GraphContext *graph = graphctx->priv;
358 graph->filters = av_realloc(graph->filters,
359 sizeof(AVFilterContext*) * ++graph->filter_count);
360 graph->filters[graph->filter_count - 1] = filter;
363 /* search intelligently, once we insert in order */
364 AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
366 GraphContext *graph = ctx->priv;
372 for(i = 0; i < graph->filter_count; i ++)
373 if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
374 return graph->filters[i];
379 static int query_formats(AVFilterContext *graphctx)
381 GraphContext *graph = graphctx->priv;
382 AVFilterContext *linkfiltin = graph->link_filter_in;
383 AVFilterContext *linkfiltout = graph->link_filter_out;
386 /* ask all the sub-filters for their supported colorspaces */
387 for(i = 0; i < graph->filter_count; i ++) {
388 if(graph->filters[i]->filter->query_formats)
389 graph->filters[i]->filter->query_formats(graph->filters[i]);
391 avfilter_default_query_formats(graph->filters[i]);
394 /* use these formats on our exported links */
395 for(i = 0; i < linkfiltout->input_count; i ++) {
396 avfilter_formats_ref( linkfiltout->inputs[i]->in_formats,
397 &linkfiltout->inputs[i]->out_formats);
399 if(graphctx->outputs[i])
400 avfilter_formats_ref(linkfiltout->inputs[i]->in_formats,
401 &graphctx->outputs[i]->in_formats);
403 for(i = 0; i < linkfiltin->output_count; i ++) {
404 avfilter_formats_ref( linkfiltin->outputs[i]->out_formats,
405 &linkfiltin->outputs[i]->in_formats);
407 if(graphctx->inputs[i])
408 avfilter_formats_ref(linkfiltin->outputs[i]->out_formats,
409 &graphctx-> inputs[i]->out_formats);
412 /* go through and merge as many format lists as possible */
413 for(i = 0; i < graph->filter_count; i ++) {
414 AVFilterContext *filter = graph->filters[i];
416 for(j = 0; j < filter->input_count; j ++) {
418 if(!(link = filter->inputs[j]))
420 if(link->in_formats != link->out_formats) {
421 if(!avfilter_merge_formats(link->in_formats,
422 link->out_formats)) {
423 /* couldn't merge format lists. auto-insert scale filter */
424 AVFilterContext *scale;
426 if(!(scale = avfilter_open(&avfilter_vf_scale, NULL)))
428 if(scale->filter->init(scale, NULL, NULL) ||
429 avfilter_insert_filter(link, scale, 0, 0)) {
430 avfilter_destroy(scale);
434 avfilter_graph_add_filter(graphctx, scale);
435 scale->filter->query_formats(scale);
436 if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
437 scale-> inputs[0]->out_formats) ||
438 !avfilter_merge_formats(scale->outputs[0]->in_formats,
439 scale->outputs[0]->out_formats))
449 static void pick_format(AVFilterLink *link)
451 if(!link || !link->in_formats)
454 link->in_formats->format_count = 1;
455 link->format = link->in_formats->formats[0];
457 avfilter_formats_unref(&link->in_formats);
458 avfilter_formats_unref(&link->out_formats);
461 static void pick_formats(GraphContext *graph)
465 for(i = 0; i < graph->filter_count; i ++) {
466 AVFilterContext *filter = graph->filters[i];
468 if(filter->filter == &avfilter_vf_graph ||
469 filter->filter == &avfilter_vf_graphfile ||
470 filter->filter == &avfilter_vf_graphdesc)
471 pick_formats(filter->priv);
473 for(j = 0; j < filter->input_count; j ++)
474 pick_format(filter->inputs[j]);
475 for(j = 0; j < filter->output_count; j ++)
476 pick_format(filter->outputs[j]);
480 int avfilter_graph_config_formats(AVFilterContext *graphctx)
482 GraphContext *graph = graphctx->priv;
484 /* find supported formats from sub-filters, and merge along links */
485 if(query_formats(graphctx))
488 /* Once everything is merged, it's possible that we'll still have
489 * multiple valid choices of colorspace. We pick the first one. */
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,
509 "error creating filter '%s'\n", curfilt->name);
512 avfilter_graph_add_filter(ctx, filt);
513 if(avfilter_init_filter(filt, curfilt->args, NULL)) {
514 av_log(ctx, AV_LOG_ERROR,
515 "error initializing filter '%s'\n", curfilt->name);
520 /* create all links */
521 for(curlink = desc->links; curlink; curlink = curlink->next) {
522 if(!(filt = avfilter_graph_get_filter(ctx, curlink->src))) {
523 av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
526 if(!(filtb = avfilter_graph_get_filter(ctx, curlink->dst))) {
527 av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
530 if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
531 av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
536 /* export all input pads */
537 for(curpad = desc->inputs; curpad; curpad = curpad->next) {
538 if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
539 av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
542 add_graph_input(ctx, filt, curpad->pad, curpad->name);
545 /* export all output pads */
546 for(curpad = desc->outputs; curpad; curpad = curpad->next) {
547 if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
548 av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
551 add_graph_output(ctx, filt, curpad->pad, curpad->name);
561 static int init(AVFilterContext *ctx, const char *args, void *opaque)
563 GraphContext *gctx = ctx->priv;
564 AVFilterGraphDesc *desc;
567 if(!(gctx->link_filter_in = avfilter_open(&vf_graph_dummy, NULL)))
569 if(avfilter_init_filter(gctx->link_filter_in, NULL, ctx))
571 if(!(gctx->link_filter_out = avfilter_open(&vf_graph_dummy, NULL)))
573 if(avfilter_init_filter(gctx->link_filter_out, NULL, ctx))
579 if(!(desc = avfilter_graph_parse_chain(args)))
582 ret = graph_load_from_desc(ctx, desc);
583 avfilter_graph_free_desc(desc);
587 avfilter_destroy(gctx->link_filter_in);
588 if(gctx->link_filter_out)
589 avfilter_destroy(gctx->link_filter_out);
593 AVFilter avfilter_vf_graph =
596 .author = "Bobby Bingham",
598 .priv_size = sizeof(GraphContext),
603 .query_formats = query_formats,
605 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
606 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
609 static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
611 GraphContext *gctx = ctx->priv;
616 if(!(gctx->link_filter_in = avfilter_open(&vf_graph_dummy, NULL)))
618 if(avfilter_init_filter(gctx->link_filter_in, NULL, ctx))
620 if(!(gctx->link_filter_out = avfilter_open(&vf_graph_dummy, NULL)))
622 if(avfilter_init_filter(gctx->link_filter_out, NULL, ctx))
625 return graph_load_from_desc(ctx, opaque);
628 avfilter_destroy(gctx->link_filter_in);
629 if(gctx->link_filter_out)
630 avfilter_destroy(gctx->link_filter_out);
634 AVFilter avfilter_vf_graphdesc =
636 .name = "graph_desc",
637 .author = "Bobby Bingham",
639 .priv_size = sizeof(GraphContext),
644 .query_formats = query_formats,
646 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
647 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
650 static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
652 AVFilterGraphDesc *desc;
657 if(!(desc = avfilter_graph_load_desc(args)))
660 ret = init_desc(ctx, NULL, desc);
661 avfilter_graph_free_desc(desc);
665 AVFilter avfilter_vf_graphfile =
667 .name = "graph_file",
668 .author = "Bobby Bingham",
670 .priv_size = sizeof(GraphContext),
675 .query_formats = query_formats,
677 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
678 .outputs = (AVFilterPad[]) {{ .name = NULL, }},