Use tables symetry to reduce their size by half.
[ffmpeg-lucabe.git] / libavfilter / avfiltergraph.c
blobccb275c6f9514aab64385c9797d7297e79e82f7a
1 /*
2 * filter graphs
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
23 #include <ctype.h>
24 #include <string.h>
26 #include "avfilter.h"
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 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
38 graph->filters = av_realloc(graph->filters,
39 sizeof(AVFilterContext*) * ++graph->filter_count);
41 if (!graph->filters)
42 return -1;
44 graph->filters[graph->filter_count - 1] = filter;
46 return 0;
49 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
51 int i;
53 for(i = 0; i < graph->filter_count; i ++)
54 if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
55 return graph->filters[i];
57 return NULL;
60 static int query_formats(AVFilterGraph *graph)
62 int i, j;
63 int scaler_count = 0;
64 char inst_name[30];
66 /* ask all the sub-filters for their supported colorspaces */
67 for(i = 0; i < graph->filter_count; i ++) {
68 if(graph->filters[i]->filter->query_formats)
69 graph->filters[i]->filter->query_formats(graph->filters[i]);
70 else
71 avfilter_default_query_formats(graph->filters[i]);
74 /* go through and merge as many format lists as possible */
75 for(i = 0; i < graph->filter_count; i ++) {
76 AVFilterContext *filter = graph->filters[i];
78 for(j = 0; j < filter->input_count; j ++) {
79 AVFilterLink *link = filter->inputs[j];
80 if(link && link->in_formats != link->out_formats) {
81 if(!avfilter_merge_formats(link->in_formats,
82 link->out_formats)) {
83 AVFilterContext *scale;
84 /* couldn't merge format lists. auto-insert scale filter */
85 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
86 scaler_count);
87 scale =
88 avfilter_open(avfilter_get_by_name("scale"),inst_name);
90 if(!scale || scale->filter->init(scale, NULL, NULL) ||
91 avfilter_insert_filter(link, scale, 0, 0)) {
92 avfilter_destroy(scale);
93 return -1;
96 if (avfilter_graph_add_filter(graph, scale) < 0)
97 return -1;
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))
104 return -1;
110 return 0;
113 static void pick_format(AVFilterLink *link)
115 if(!link || !link->in_formats)
116 return;
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)
127 int i, j;
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))
143 return -1;
145 /* Once everything is merged, it's possible that we'll still have
146 * multiple valid colorspace choices. We pick the first one. */
147 pick_formats(graph);
149 return 0;