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