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);
37 * @todo insert in sorted order
39 void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
41 graph->filters = av_realloc(graph->filters,
42 sizeof(AVFilterContext*) * ++graph->filter_count);
43 graph->filters[graph->filter_count - 1] = filter;
47 * @todo search intelligently, once we insert in order
49 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
56 for(i = 0; i < graph->filter_count; i ++)
57 if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
58 return graph->filters[i];
63 static int query_formats(AVFilterGraph *graph)
67 /* ask all the sub-filters for their supported colorspaces */
68 for(i = 0; i < graph->filter_count; i ++) {
69 if(graph->filters[i]->filter->query_formats)
70 graph->filters[i]->filter->query_formats(graph->filters[i]);
72 avfilter_default_query_formats(graph->filters[i]);
75 /* go through and merge as many format lists as possible */
76 for(i = 0; i < graph->filter_count; i ++) {
77 AVFilterContext *filter = graph->filters[i];
79 for(j = 0; j < filter->input_count; j ++) {
81 if(!(link = filter->inputs[j]))
83 if(link->in_formats != link->out_formats) {
84 if(!avfilter_merge_formats(link->in_formats,
86 /* couldn't merge format lists. auto-insert scale filter */
87 AVFilterContext *scale;
90 avfilter_open(avfilter_get_by_name("scale"), NULL)))
92 if(scale->filter->init(scale, NULL, NULL) ||
93 avfilter_insert_filter(link, scale, 0, 0)) {
94 avfilter_destroy(scale);
98 avfilter_graph_add_filter(graph, scale);
99 scale->filter->query_formats(scale);
100 if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
101 scale-> inputs[0]->out_formats) ||
102 !avfilter_merge_formats(scale->outputs[0]->in_formats,
103 scale->outputs[0]->out_formats))
113 static void pick_format(AVFilterLink *link)
115 if(!link || !link->in_formats)
118 link->in_formats->format_count = 1;
119 link->format = link->in_formats->formats[0];
121 avfilter_formats_unref(&link->in_formats);
122 avfilter_formats_unref(&link->out_formats);
125 static void pick_formats(AVFilterGraph *graph)
129 for(i = 0; i < graph->filter_count; i ++) {
130 AVFilterContext *filter = graph->filters[i];
132 for(j = 0; j < filter->input_count; j ++)
133 pick_format(filter->inputs[j]);
134 for(j = 0; j < filter->output_count; j ++)
135 pick_format(filter->outputs[j]);
139 int avfilter_graph_config_formats(AVFilterGraph *graph)
141 /* find supported formats from sub-filters, and merge along links */
142 if(query_formats(graph))
145 /* Once everything is merged, it's possible that we'll still have
146 * multiple valid colorspace choices. We pick the first one. */