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 typedef struct AVFilterGraph {
30 unsigned filter_count;
31 AVFilterContext **filters;
33 /** fake filter to handle links to internal filters */
34 AVFilterContext *link_filter;
38 AVFilterContext *graph;
41 static int link_init(AVFilterContext *ctx, const char *args, void *opaque)
43 GraphLinkContext *linkctx = ctx->priv;
44 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
51 static inline AVFilterLink *get_extern_input_link(AVFilterLink *link)
53 GraphLinkContext *lctx = link->src->priv;
54 return lctx->graph->inputs[link->srcpad];
57 /** query the formats supported by a filter providing input to the graph */
58 static int *link_in_query_formats(AVFilterLink *link)
60 AVFilterLink *link2 = get_extern_input_link(link);
61 int *(*query_formats)(AVFilterLink *);
64 return avfilter_make_format_list(0);
66 if(!(query_formats = link2->src->output_pads[link2->srcpad].query_formats))
67 query_formats = avfilter_default_query_output_formats;
69 return query_formats(link2);
72 /** request a frame from a filter providing input to the graph */
73 static void link_in_request_frame(AVFilterLink *link)
75 AVFilterLink *link2 = get_extern_input_link(link);
79 avfilter_request_frame(link2);
82 static int link_in_config_props(AVFilterLink *link)
84 AVFilterLink *link2 = get_extern_input_link(link);
85 int (*config_props)(AVFilterLink *);
90 if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
91 config_props = avfilter_default_config_output_link;
92 ret = config_props(link2);
100 /* given the link between the dummy filter and an internal filter whose input
101 * is being exported outside the graph, this returns the externally visible
103 static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
105 GraphLinkContext *lctx = link->dst->priv;
106 return lctx->graph->outputs[link->dstpad];
109 /** query the formats supported by a filter taking output from the graph */
110 static int *link_out_query_formats(AVFilterLink *link)
112 AVFilterLink *link2 = get_extern_output_link(link);
115 return avfilter_make_format_list(0);
117 return link2->dst->input_pads[link2->dstpad].query_formats(link2);
120 static int link_out_config_props(AVFilterLink *link)
122 AVFilterLink *link2 = get_extern_output_link(link);
123 int (*config_props)(AVFilterLink *);
131 if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
132 config_props = avfilter_default_config_input_link;
133 return config_props(link2);
136 static void link_out_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
138 AVFilterLink *link2 = get_extern_output_link(link);
141 avfilter_unref_pic(picref);
143 avfilter_start_frame(link2, picref);
146 static void link_out_end_frame(AVFilterLink *link)
148 AVFilterLink *link2 = get_extern_output_link(link);
151 avfilter_end_frame(link2);
154 static AVFilterPicRef *link_out_get_video_buffer(AVFilterLink *link, int perms)
156 AVFilterLink *link2 = get_extern_output_link(link);
161 return avfilter_get_video_buffer(link2, perms);
164 static void link_out_draw_slice(AVFilterLink *link, uint8_t *data[4], int y,
167 AVFilterLink *link2 = get_extern_output_link(link);
170 avfilter_draw_slice(link2, data, y, height);
173 /** dummy filter used to help export filters pads outside the graph */
174 static AVFilter vf_graph_dummy =
176 .name = "graph_dummy",
177 .author = "Bobby Bingham",
179 .priv_size = sizeof(GraphLinkContext),
184 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
185 .outputs = (AVFilterPad[]) {{ .name = NULL, }},
188 static AVFilterLink *get_intern_input_link(AVFilterLink *link)
190 GraphContext *graph = link->dst->priv;
191 return graph->link_filter->outputs[link->dstpad];
194 static void graph_in_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
196 AVFilterLink *link2 = get_intern_input_link(link);
198 avfilter_start_frame(link2, picref);
201 static void graph_in_end_frame(AVFilterLink *link)
203 AVFilterLink *link2 = get_intern_input_link(link);
205 avfilter_end_frame(link2);
208 static AVFilterPicRef *graph_in_get_video_buffer(AVFilterLink *link, int perms)
210 AVFilterLink *link2 = get_intern_input_link(link);
212 return avfilter_get_video_buffer(link2, perms);
216 static void graph_in_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int height)
218 AVFilterLink *link2 = get_intern_input_link(link);
220 avfilter_draw_slice(link2, data, y, height);
223 static int *graph_in_query_formats(AVFilterLink *link)
225 AVFilterLink *link2 = get_intern_input_link(link);
227 if(!link2 || !link2->dst->input_pads[link2->dstpad].query_formats)
228 return avfilter_make_format_list(0);
229 return link2->dst->input_pads[link2->dstpad].query_formats(link2);
232 static int graph_in_config_props(AVFilterLink *link)
234 AVFilterLink *link2 = get_intern_input_link(link);
235 int (*config_props)(AVFilterLink *);
240 /* copy link properties over to the dummy internal link */
244 if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
245 return 0; /* FIXME? */
246 //config_props = avfilter_default_config_input_link;
247 return config_props(link2);
250 static AVFilterLink *get_intern_output_link(AVFilterLink *link)
252 GraphContext *graph = link->src->priv;
253 return graph->link_filter->inputs[link->srcpad];
256 static int *graph_out_query_formats(AVFilterLink *link)
258 AVFilterLink *link2 = get_intern_output_link(link);
261 return avfilter_make_format_list(0);
262 if(!link2->src->output_pads[link2->srcpad].query_formats)
263 return avfilter_default_query_output_formats(link2);
264 return link2->src->output_pads[link2->srcpad].query_formats(link2);
267 static void graph_out_request_frame(AVFilterLink *link)
269 AVFilterLink *link2 = get_intern_output_link(link);
272 avfilter_request_frame(link2);
275 static int graph_out_config_props(AVFilterLink *link)
277 AVFilterLink *link2 = get_intern_output_link(link);
278 int (*config_props)(AVFilterLink *);
286 link2->format = link->format;
288 if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
289 config_props = avfilter_default_config_output_link;
290 ret = config_props(link2);
294 link->format = link2->format;
299 static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
302 GraphContext *graph = gctx->priv;
304 AVFilterPad graph_inpad =
307 .type = AV_PAD_VIDEO,
308 .start_frame = graph_in_start_frame,
309 .end_frame = graph_in_end_frame,
310 .get_video_buffer = graph_in_get_video_buffer,
311 .draw_slice = graph_in_draw_slice,
312 .query_formats = graph_in_query_formats,
313 .config_props = graph_in_config_props,
316 AVFilterPad dummy_outpad =
318 .name = NULL, /* FIXME? */
319 .type = AV_PAD_VIDEO,
320 .query_formats = link_in_query_formats,
321 .request_frame = link_in_request_frame,
322 .config_props = link_in_config_props,
325 avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
326 avfilter_insert_outpad(graph->link_filter, graph->link_filter->output_count,
328 return avfilter_link(graph->link_filter,
329 graph->link_filter->output_count-1, filt, idx);
332 static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
335 GraphContext *graph = gctx->priv;
337 AVFilterPad graph_outpad =
340 .type = AV_PAD_VIDEO,
341 .request_frame = graph_out_request_frame,
342 .query_formats = graph_out_query_formats,
343 .config_props = graph_out_config_props,
345 AVFilterPad dummy_inpad =
347 .name = NULL, /* FIXME? */
348 .type = AV_PAD_VIDEO,
349 .start_frame = link_out_start_frame,
350 .end_frame = link_out_end_frame,
351 .draw_slice = link_out_draw_slice,
352 .get_video_buffer = link_out_get_video_buffer,
353 .query_formats = link_out_query_formats,
354 .config_props = link_out_config_props,
357 avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
358 avfilter_insert_inpad (graph->link_filter, graph->link_filter->input_count,
360 return avfilter_link(filt, idx, graph->link_filter,
361 graph->link_filter->input_count-1);
364 static void uninit(AVFilterContext *ctx)
366 GraphContext *graph = ctx->priv;
368 if(graph->link_filter) {
369 avfilter_destroy(graph->link_filter);
370 graph->link_filter = NULL;
372 for(; graph->filter_count > 0; graph->filter_count --)
373 avfilter_destroy(graph->filters[graph->filter_count - 1]);
374 av_freep(&graph->filters);
377 void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
379 GraphContext *graph = graphctx->priv;
381 graph->filters = av_realloc(graph->filters,
382 sizeof(AVFilterContext*) * ++graph->filter_count);
383 graph->filters[graph->filter_count - 1] = filter;
386 int avfilter_graph_config_links(AVFilterContext *graphctx)
388 GraphContext *graph = graphctx->priv;
391 for(i = 0; i < graph->filter_count; i ++) {
392 for(j = 0; j < graph->filters[i]->input_count; j ++)
393 if(avfilter_config_link(graph->filters[i]->inputs[j]))
400 static AVFilterContext *create_filter_with_args(const char *filt, void *opaque)
402 AVFilterContext *ret;
403 char *filter = av_strdup(filt); /* copy - don't mangle the input string */
407 if((args = strchr(filter, '='))) {
408 /* ensure we at least have a name */
415 av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
416 name, args ? args : "(none)");
418 if((ret = avfilter_create_by_name(name, NULL))) {
419 if(avfilter_init_filter(ret, args, opaque)) {
420 av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
421 avfilter_destroy(ret);
424 } else av_log(NULL, AV_LOG_ERROR, "error creating filter!\n");
435 static int graph_load_chain(AVFilterContext *graphctx,
436 unsigned count, char **filter_list, void **opaque,
437 AVFilterContext **first, AVFilterContext **last)
440 AVFilterContext *filters[2] = {NULL,NULL};
442 for(i = 0; i < count; i ++) {
445 if(opaque) op = opaque[i];
448 if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
451 if(first) *first = filters[1];
453 if(avfilter_link(filters[0], 0, filters[1], 0)) {
454 av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
458 avfilter_graph_add_filter(graphctx, filters[1]);
459 if(i == 0 && filters[1]->input_count > 0)
460 add_graph_input(graphctx, filters[1], 0, "default");
461 filters[0] = filters[1];
464 if(filters[1]->output_count > 0)
465 add_graph_output(graphctx, filters[1], 0, "default");
467 if(last) *last = filters[1];
472 if(first) *first = NULL;
473 if(last) *last = NULL;
477 static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
478 AVFilterContext **first,
479 AVFilterContext **last)
485 strings = av_malloc(sizeof(char *));
486 strings[0] = av_strdup(str);
488 filt = strchr(strings[0], ',');
489 for(count = 1; filt; count ++) {
490 if(filt == strings[count-1]) {
495 strings = av_realloc(strings, sizeof(char *) * (count+1));
496 strings[count] = filt + 1;
498 filt = strchr(strings[count], ',');
501 ret = graph_load_chain(ctx, count, strings, NULL, first, last);
510 static int init(AVFilterContext *ctx, const char *args, void *opaque)
512 GraphContext *gctx = ctx->priv;
517 if(!(gctx->link_filter = avfilter_create(&vf_graph_dummy, NULL)))
519 if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
522 return graph_load_chain_from_string(ctx, args, NULL, NULL);
525 avfilter_destroy(gctx->link_filter);
532 .author = "Bobby Bingham",
534 .priv_size = sizeof(GraphContext),
539 .inputs = (AVFilterPad[]) {{ .name = NULL, }},
540 .outputs = (AVFilterPad[]) {{ .name = NULL, }},