Implement avfilter_graph_check_validity().
[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 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
37 {
38     graph->filters = av_realloc(graph->filters,
39                                 sizeof(AVFilterContext*) * ++graph->filter_count);
40
41     if (!graph->filters)
42         return -1;
43
44     graph->filters[graph->filter_count - 1] = filter;
45
46     return 0;
47 }
48
49 int avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
50 {
51     AVFilterContext *filt;
52     int i, j;
53
54     for (i=0; i < graph->filter_count; i++) {
55         filt = graph->filters[i];
56
57         for (j = 0; j < filt->input_count; j++) {
58             if (!filt->inputs[j] || !filt->inputs[j]->src) {
59                 av_log(log_ctx, AV_LOG_ERROR,
60                        "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
61                        filt->input_pads[j].name, filt->name, filt->filter->name);
62                 return -1;
63             }
64         }
65
66         for (j = 0; j < filt->output_count; j++) {
67             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
68                 av_log(log_ctx, AV_LOG_ERROR,
69                        "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
70                        filt->output_pads[j].name, filt->name, filt->filter->name);
71                 return -1;
72             }
73         }
74     }
75
76     return 0;
77 }
78
79 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
80 {
81     int i;
82
83     for(i = 0; i < graph->filter_count; i ++)
84         if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
85             return graph->filters[i];
86
87     return NULL;
88 }
89
90 static int query_formats(AVFilterGraph *graph)
91 {
92     int i, j;
93     int scaler_count = 0;
94     char inst_name[30];
95
96     /* ask all the sub-filters for their supported colorspaces */
97     for(i = 0; i < graph->filter_count; i ++) {
98         if(graph->filters[i]->filter->query_formats)
99             graph->filters[i]->filter->query_formats(graph->filters[i]);
100         else
101             avfilter_default_query_formats(graph->filters[i]);
102     }
103
104     /* go through and merge as many format lists as possible */
105     for(i = 0; i < graph->filter_count; i ++) {
106         AVFilterContext *filter = graph->filters[i];
107
108         for(j = 0; j < filter->input_count; j ++) {
109             AVFilterLink *link = filter->inputs[j];
110             if(link && link->in_formats != link->out_formats) {
111                 if(!avfilter_merge_formats(link->in_formats,
112                                            link->out_formats)) {
113                     AVFilterContext *scale;
114                     /* couldn't merge format lists. auto-insert scale filter */
115                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
116                              scaler_count);
117                     scale =
118                         avfilter_open(avfilter_get_by_name("scale"),inst_name);
119
120                     if(!scale || scale->filter->init(scale, NULL, NULL) ||
121                                  avfilter_insert_filter(link, scale, 0, 0)) {
122                         avfilter_destroy(scale);
123                         return -1;
124                     }
125
126                     if (avfilter_graph_add_filter(graph, scale) < 0)
127                         return -1;
128
129                     scale->filter->query_formats(scale);
130                     if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
131                                                scale-> inputs[0]->out_formats)||
132                        !avfilter_merge_formats(scale->outputs[0]->in_formats,
133                                                scale->outputs[0]->out_formats))
134                         return -1;
135                 }
136             }
137         }
138     }
139
140     return 0;
141 }
142
143 static void pick_format(AVFilterLink *link)
144 {
145     if(!link || !link->in_formats)
146         return;
147
148     link->in_formats->format_count = 1;
149     link->format = link->in_formats->formats[0];
150
151     avfilter_formats_unref(&link->in_formats);
152     avfilter_formats_unref(&link->out_formats);
153 }
154
155 static void pick_formats(AVFilterGraph *graph)
156 {
157     int i, j;
158
159     for(i = 0; i < graph->filter_count; i ++) {
160         AVFilterContext *filter = graph->filters[i];
161
162         for(j = 0; j < filter->input_count; j ++)
163             pick_format(filter->inputs[j]);
164         for(j = 0; j < filter->output_count; j ++)
165             pick_format(filter->outputs[j]);
166     }
167 }
168
169 int avfilter_graph_config_formats(AVFilterGraph *graph)
170 {
171     /* find supported formats from sub-filters, and merge along links */
172     if(query_formats(graph))
173         return -1;
174
175     /* Once everything is merged, it's possible that we'll still have
176      * multiple valid colorspace choices. We pick the first one. */
177     pick_formats(graph);
178
179     return 0;
180 }
181