]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
lavfi: autoinsert resample filter when necessary.
[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 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, resampler_count = 0;
156
157     /* ask all the sub-filters for their supported media formats */
158     for (i = 0; i < graph->filter_count; i++) {
159         if (graph->filters[i]->filter->query_formats)
160             graph->filters[i]->filter->query_formats(graph->filters[i]);
161         else
162             avfilter_default_query_formats(graph->filters[i]);
163     }
164
165     /* go through and merge as many format lists as possible */
166     for (i = 0; i < graph->filter_count; i++) {
167         AVFilterContext *filter = graph->filters[i];
168
169         for (j = 0; j < filter->input_count; j++) {
170             AVFilterLink *link = filter->inputs[j];
171             if (link && link->in_formats != link->out_formats) {
172                 if (!avfilter_merge_formats(link->in_formats,
173                                             link->out_formats)) {
174                     AVFilterContext *convert;
175                     AVFilter *filter;
176                     AVFilterLink *inlink, *outlink;
177                     char scale_args[256];
178                     char inst_name[30];
179
180                     /* couldn't merge format lists. auto-insert conversion filter */
181                     switch (link->type) {
182                     case AVMEDIA_TYPE_VIDEO:
183                         snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
184                                  scaler_count++);
185                         snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
186                         if ((ret = avfilter_graph_create_filter(&convert,
187                                                                 avfilter_get_by_name("scale"),
188                                                                 inst_name, scale_args, NULL,
189                                                                 graph)) < 0)
190                             return ret;
191                         break;
192                     case AVMEDIA_TYPE_AUDIO:
193                         if (!(filter = avfilter_get_by_name("resample"))) {
194                             av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
195                                    "not present, cannot convert audio formats.\n");
196                             return AVERROR(EINVAL);
197                         }
198
199                         snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
200                                  resampler_count++);
201                         if ((ret = avfilter_graph_create_filter(&convert,
202                                                                 avfilter_get_by_name("resample"),
203                                                                 inst_name, NULL, NULL, graph)) < 0)
204                             return ret;
205                         break;
206                     default:
207                         return AVERROR(EINVAL);
208                     }
209
210                     if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
211                         return ret;
212
213                     convert->filter->query_formats(convert);
214                     inlink  = convert->inputs[0];
215                     outlink = convert->outputs[0];
216                     if (!avfilter_merge_formats( inlink->in_formats,  inlink->out_formats) ||
217                         !avfilter_merge_formats(outlink->in_formats, outlink->out_formats)) {
218                         av_log(log_ctx, AV_LOG_ERROR,
219                                "Impossible to convert between the formats supported by the filter "
220                                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
221                         return AVERROR(EINVAL);
222                     }
223                 }
224             }
225         }
226     }
227
228     return 0;
229 }
230
231 static void pick_format(AVFilterLink *link)
232 {
233     if (!link || !link->in_formats)
234         return;
235
236     link->in_formats->format_count = 1;
237     link->format = link->in_formats->formats[0];
238
239     avfilter_formats_unref(&link->in_formats);
240     avfilter_formats_unref(&link->out_formats);
241 }
242
243 static int reduce_formats_on_filter(AVFilterContext *filter)
244 {
245     int i, j, k, ret = 0;
246
247     for (i = 0; i < filter->input_count; i++) {
248         AVFilterLink *link = filter->inputs[i];
249         int         format = link->out_formats->formats[0];
250
251         if (link->out_formats->format_count != 1)
252             continue;
253
254         for (j = 0; j < filter->output_count; j++) {
255             AVFilterLink *out_link = filter->outputs[j];
256             AVFilterFormats  *fmts = out_link->in_formats;
257
258             if (link->type != out_link->type ||
259                 out_link->in_formats->format_count == 1)
260                 continue;
261
262             for (k = 0; k < out_link->in_formats->format_count; k++)
263                 if (fmts->formats[k] == format) {
264                     fmts->formats[0]   = format;
265                     fmts->format_count = 1;
266                     ret = 1;
267                     break;
268                 }
269         }
270     }
271     return ret;
272 }
273
274 static void reduce_formats(AVFilterGraph *graph)
275 {
276     int i, reduced;
277
278     do {
279         reduced = 0;
280
281         for (i = 0; i < graph->filter_count; i++)
282             reduced |= reduce_formats_on_filter(graph->filters[i]);
283     } while (reduced);
284 }
285
286 static void pick_formats(AVFilterGraph *graph)
287 {
288     int i, j;
289
290     for (i = 0; i < graph->filter_count; i++) {
291         AVFilterContext *filter = graph->filters[i];
292
293         for (j = 0; j < filter->input_count; j++)
294             pick_format(filter->inputs[j]);
295         for (j = 0; j < filter->output_count; j++)
296             pick_format(filter->outputs[j]);
297     }
298 }
299
300 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
301 {
302     int ret;
303
304     /* find supported formats from sub-filters, and merge along links */
305     if ((ret = query_formats(graph, log_ctx)) < 0)
306         return ret;
307
308     /* Once everything is merged, it's possible that we'll still have
309      * multiple valid media format choices. We try to minimize the amount
310      * of format conversion inside filters */
311     reduce_formats(graph);
312
313     pick_formats(graph);
314
315     return 0;
316 }
317
318 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
319 {
320     int ret;
321
322     if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
323         return ret;
324     if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
325         return ret;
326     if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
327         return ret;
328
329     return 0;
330 }