Remove unused check
[ffmpeg.git] / libavfilter / avfiltergraph.c
1 /*
2  * filter graphs
3  * copyright (c) 2008 Vitor Sessak
4  * copyright (c) 2007 Bobby Bingham
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <ctype.h>
24 #include <string.h>
25
26 #include "avfilter.h"
27 #include "avfiltergraph.h"
28
29 void avfilter_destroy_graph(AVFilterGraph *graph)
30 {
31     for(; graph->filter_count > 0; graph->filter_count --)
32         avfilter_destroy(graph->filters[graph->filter_count - 1]);
33     av_freep(&graph->filters);
34 }
35
36 void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
37 {
38     graph->filters = av_realloc(graph->filters,
39                                 sizeof(AVFilterContext*) * ++graph->filter_count);
40     graph->filters[graph->filter_count - 1] = filter;
41 }
42
43 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
44 {
45     int i;
46
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];
50
51     return NULL;
52 }
53
54 static int query_formats(AVFilterGraph *graph)
55 {
56     int i, j;
57
58     /* ask all the sub-filters for their supported colorspaces */
59     for(i = 0; i < graph->filter_count; i ++) {
60         if(graph->filters[i]->filter->query_formats)
61             graph->filters[i]->filter->query_formats(graph->filters[i]);
62         else
63             avfilter_default_query_formats(graph->filters[i]);
64     }
65
66     /* go through and merge as many format lists as possible */
67     for(i = 0; i < graph->filter_count; i ++) {
68         AVFilterContext *filter = graph->filters[i];
69
70         for(j = 0; j < filter->input_count; j ++) {
71             AVFilterLink *link;
72             if(!(link = filter->inputs[j]))
73                 continue;
74             if(link->in_formats != link->out_formats) {
75                 if(!avfilter_merge_formats(link->in_formats,
76                                            link->out_formats)) {
77                     /* couldn't merge format lists. auto-insert scale filter */
78                     AVFilterContext *scale;
79
80                     if(!(scale =
81                          avfilter_open(avfilter_get_by_name("scale"), NULL)))
82                         return -1;
83                     if(scale->filter->init(scale, NULL, NULL) ||
84                        avfilter_insert_filter(link, scale, 0, 0)) {
85                         avfilter_destroy(scale);
86                         return -1;
87                     }
88
89                     avfilter_graph_add_filter(graph, scale);
90                     scale->filter->query_formats(scale);
91                     if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
92                                                scale-> inputs[0]->out_formats)||
93                        !avfilter_merge_formats(scale->outputs[0]->in_formats,
94                                                scale->outputs[0]->out_formats))
95                         return -1;
96                 }
97             }
98         }
99     }
100
101     return 0;
102 }
103
104 static void pick_format(AVFilterLink *link)
105 {
106     if(!link || !link->in_formats)
107         return;
108
109     link->in_formats->format_count = 1;
110     link->format = link->in_formats->formats[0];
111
112     avfilter_formats_unref(&link->in_formats);
113     avfilter_formats_unref(&link->out_formats);
114 }
115
116 static void pick_formats(AVFilterGraph *graph)
117 {
118     int i, j;
119
120     for(i = 0; i < graph->filter_count; i ++) {
121         AVFilterContext *filter = graph->filters[i];
122
123         for(j = 0; j < filter->input_count; j ++)
124             pick_format(filter->inputs[j]);
125         for(j = 0; j < filter->output_count; j ++)
126             pick_format(filter->outputs[j]);
127     }
128 }
129
130 int avfilter_graph_config_formats(AVFilterGraph *graph)
131 {
132     /* find supported formats from sub-filters, and merge along links */
133     if(query_formats(graph))
134         return -1;
135
136     /* Once everything is merged, it's possible that we'll still have
137      * multiple valid colorspace choices. We pick the first one. */
138     pick_formats(graph);
139
140     return 0;
141 }
142