avfiltergraph: try to reduce format conversions in filters.
[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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 #include "internal.h"
29
30 #include "libavutil/log.h"
31
32 static const AVClass filtergraph_class = {
33     .class_name = "AVFilterGraph",
34     .item_name  = av_default_item_name,
35     .version    = LIBAVUTIL_VERSION_INT,
36 };
37
38 AVFilterGraph *avfilter_graph_alloc(void)
39 {
40     AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
41     if (!ret)
42         return NULL;
43 #if FF_API_GRAPH_AVCLASS
44     ret->av_class = &filtergraph_class;
45 #endif
46     return ret;
47 }
48
49 void avfilter_graph_free(AVFilterGraph **graph)
50 {
51     if (!*graph)
52         return;
53     for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
54         avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
55     av_freep(&(*graph)->scale_sws_opts);
56     av_freep(&(*graph)->filters);
57     av_freep(graph);
58 }
59
60 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
61 {
62     AVFilterContext **filters = av_realloc(graph->filters,
63                                            sizeof(AVFilterContext*) * (graph->filter_count+1));
64     if (!filters)
65         return AVERROR(ENOMEM);
66
67     graph->filters = filters;
68     graph->filters[graph->filter_count++] = filter;
69
70     return 0;
71 }
72
73 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
74                                  const char *name, const char *args, void *opaque,
75                                  AVFilterGraph *graph_ctx)
76 {
77     int ret;
78
79     if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
80         goto fail;
81     if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
82         goto fail;
83     if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
84         goto fail;
85     return 0;
86
87 fail:
88     if (*filt_ctx)
89         avfilter_free(*filt_ctx);
90     *filt_ctx = NULL;
91     return ret;
92 }
93
94 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
95 {
96     AVFilterContext *filt;
97     int i, j;
98
99     for (i = 0; i < graph->filter_count; i++) {
100         filt = graph->filters[i];
101
102         for (j = 0; j < filt->input_count; j++) {
103             if (!filt->inputs[j] || !filt->inputs[j]->src) {
104                 av_log(log_ctx, AV_LOG_ERROR,
105                        "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
106                        filt->input_pads[j].name, filt->name, filt->filter->name);
107                 return AVERROR(EINVAL);
108             }
109         }
110
111         for (j = 0; j < filt->output_count; j++) {
112             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
113                 av_log(log_ctx, AV_LOG_ERROR,
114                        "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
115                        filt->output_pads[j].name, filt->name, filt->filter->name);
116                 return AVERROR(EINVAL);
117             }
118         }
119     }
120
121     return 0;
122 }
123
124 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
125 {
126     AVFilterContext *filt;
127     int i, ret;
128
129     for (i=0; i < graph->filter_count; i++) {
130         filt = graph->filters[i];
131
132         if (!filt->output_count) {
133             if ((ret = avfilter_config_links(filt)))
134                 return ret;
135         }
136     }
137
138     return 0;
139 }
140
141 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
142 {
143     int i;
144
145     for (i = 0; i < graph->filter_count; i++)
146         if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
147             return graph->filters[i];
148
149     return NULL;
150 }
151
152 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
153 {
154     int i, j, ret;
155     int scaler_count = 0;
156     char inst_name[30];
157
158     /* ask all the sub-filters for their supported media formats */
159     for (i = 0; i < graph->filter_count; i++) {
160         if (graph->filters[i]->filter->query_formats)
161             graph->filters[i]->filter->query_formats(graph->filters[i]);
162         else
163             avfilter_default_query_formats(graph->filters[i]);
164     }
165
166     /* go through and merge as many format lists as possible */
167     for (i = 0; i < graph->filter_count; i++) {
168         AVFilterContext *filter = graph->filters[i];
169
170         for (j = 0; j < filter->input_count; j++) {
171             AVFilterLink *link = filter->inputs[j];
172             if (link && link->in_formats != link->out_formats) {
173                 if (!avfilter_merge_formats(link->in_formats,
174                                             link->out_formats)) {
175                     AVFilterContext *scale;
176                     char scale_args[256];
177                     /* couldn't merge format lists. auto-insert scale filter */
178                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
179                              scaler_count++);
180                     snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
181                     if ((ret = avfilter_graph_create_filter(&scale, avfilter_get_by_name("scale"),
182                                                             inst_name, scale_args, NULL, graph)) < 0)
183                         return ret;
184                     if ((ret = avfilter_insert_filter(link, scale, 0, 0)) < 0)
185                         return ret;
186
187                     scale->filter->query_formats(scale);
188                     if (((link = scale-> inputs[0]) &&
189                          !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
190                         ((link = scale->outputs[0]) &&
191                          !avfilter_merge_formats(link->in_formats, link->out_formats))) {
192                         av_log(log_ctx, AV_LOG_ERROR,
193                                "Impossible to convert between the formats supported by the filter "
194                                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
195                         return AVERROR(EINVAL);
196                     }
197                 }
198             }
199         }
200     }
201
202     return 0;
203 }
204
205 static void pick_format(AVFilterLink *link)
206 {
207     if (!link || !link->in_formats)
208         return;
209
210     link->in_formats->format_count = 1;
211     link->format = link->in_formats->formats[0];
212
213     avfilter_formats_unref(&link->in_formats);
214     avfilter_formats_unref(&link->out_formats);
215 }
216
217 static int reduce_formats_on_filter(AVFilterContext *filter)
218 {
219     int i, j, k, ret = 0;
220
221     for (i = 0; i < filter->input_count; i++) {
222         AVFilterLink *link = filter->inputs[i];
223         int         format = link->out_formats->formats[0];
224
225         if (link->out_formats->format_count != 1)
226             continue;
227
228         for (j = 0; j < filter->output_count; j++) {
229             AVFilterLink *out_link = filter->outputs[j];
230             AVFilterFormats  *fmts = out_link->in_formats;
231
232             if (link->type != out_link->type ||
233                 out_link->in_formats->format_count == 1)
234                 continue;
235
236             for (k = 0; k < out_link->in_formats->format_count; k++)
237                 if (fmts->formats[k] == format) {
238                     fmts->formats[0]   = format;
239                     fmts->format_count = 1;
240                     ret = 1;
241                     break;
242                 }
243         }
244     }
245     return ret;
246 }
247
248 static void reduce_formats(AVFilterGraph *graph)
249 {
250     int i, reduced;
251
252     do {
253         reduced = 0;
254
255         for (i = 0; i < graph->filter_count; i++)
256             reduced |= reduce_formats_on_filter(graph->filters[i]);
257     } while (reduced);
258 }
259
260 static void pick_formats(AVFilterGraph *graph)
261 {
262     int i, j;
263
264     for (i = 0; i < graph->filter_count; i++) {
265         AVFilterContext *filter = graph->filters[i];
266
267         for (j = 0; j < filter->input_count; j++)
268             pick_format(filter->inputs[j]);
269         for (j = 0; j < filter->output_count; j++)
270             pick_format(filter->outputs[j]);
271     }
272 }
273
274 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
275 {
276     int ret;
277
278     /* find supported formats from sub-filters, and merge along links */
279     if ((ret = query_formats(graph, log_ctx)) < 0)
280         return ret;
281
282     /* Once everything is merged, it's possible that we'll still have
283      * multiple valid media format choices. We try to minimize the amount
284      * of format conversion inside filters */
285     reduce_formats(graph);
286
287     pick_formats(graph);
288
289     return 0;
290 }
291
292 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
293 {
294     int ret;
295
296     if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
297         return ret;
298     if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
299         return ret;
300     if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
301         return ret;
302
303     return 0;
304 }