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