3 * copyright (c) 2008 Vitor Sessak
4 * copyright (c) 2007 Bobby Bingham
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "avfiltergraph.h"
29 void avfilter_destroy_graph(AVFilterGraph *graph)
31 for(; graph->filter_count > 0; graph->filter_count --)
32 avfilter_destroy(graph->filters[graph->filter_count - 1]);
33 av_freep(&graph->filters);
36 void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
38 graph->filters = av_realloc(graph->filters,
39 sizeof(AVFilterContext*) * ++graph->filter_count);
40 graph->filters[graph->filter_count - 1] = filter;
43 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
47 for(i = 0; i < graph->filter_count; i ++)
48 if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
49 return graph->filters[i];
54 static int query_formats(AVFilterGraph *graph)
60 /* ask all the sub-filters for their supported colorspaces */
61 for(i = 0; i < graph->filter_count; i ++) {
62 if(graph->filters[i]->filter->query_formats)
63 graph->filters[i]->filter->query_formats(graph->filters[i]);
65 avfilter_default_query_formats(graph->filters[i]);
68 /* go through and merge as many format lists as possible */
69 for(i = 0; i < graph->filter_count; i ++) {
70 AVFilterContext *filter = graph->filters[i];
72 for(j = 0; j < filter->input_count; j ++) {
73 AVFilterLink *link = filter->inputs[j];
74 if(link && link->in_formats != link->out_formats) {
75 if(!avfilter_merge_formats(link->in_formats,
77 AVFilterContext *scale;
78 /* couldn't merge format lists. auto-insert scale filter */
79 snprintf(inst_name, 30, "auto-inserted scaler %d",
82 avfilter_open(avfilter_get_by_name("scale"),inst_name);
84 if(!scale || scale->filter->init(scale, NULL, NULL) ||
85 avfilter_insert_filter(link, scale, 0, 0)) {
86 avfilter_destroy(scale);
90 avfilter_graph_add_filter(graph, scale);
91 scale->filter->query_formats(scale);
92 if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
93 scale-> inputs[0]->out_formats)||
94 !avfilter_merge_formats(scale->outputs[0]->in_formats,
95 scale->outputs[0]->out_formats))
105 static void pick_format(AVFilterLink *link)
107 if(!link || !link->in_formats)
110 link->in_formats->format_count = 1;
111 link->format = link->in_formats->formats[0];
113 avfilter_formats_unref(&link->in_formats);
114 avfilter_formats_unref(&link->out_formats);
117 static void pick_formats(AVFilterGraph *graph)
121 for(i = 0; i < graph->filter_count; i ++) {
122 AVFilterContext *filter = graph->filters[i];
124 for(j = 0; j < filter->input_count; j ++)
125 pick_format(filter->inputs[j]);
126 for(j = 0; j < filter->output_count; j ++)
127 pick_format(filter->outputs[j]);
131 int avfilter_graph_config_formats(AVFilterGraph *graph)
133 /* find supported formats from sub-filters, and merge along links */
134 if(query_formats(graph))
137 /* Once everything is merged, it's possible that we'll still have
138 * multiple valid colorspace choices. We pick the first one. */