avfiltergraph: avoid harmful auto audio conversions.
[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 "libavutil/audioconvert.h"
27 #include "avfilter.h"
28 #include "avfiltergraph.h"
29 #include "internal.h"
30
31 AVFilterGraph *avfilter_graph_alloc(void)
32 {
33     return av_mallocz(sizeof(AVFilterGraph));
34 }
35
36 void avfilter_graph_free(AVFilterGraph **graph)
37 {
38     if (!*graph)
39         return;
40     for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
41         avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
42     av_freep(&(*graph)->scale_sws_opts);
43     av_freep(&(*graph)->filters);
44     av_freep(graph);
45 }
46
47 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
48 {
49     AVFilterContext **filters = av_realloc(graph->filters,
50                                            sizeof(AVFilterContext*) * (graph->filter_count+1));
51     if (!filters)
52         return AVERROR(ENOMEM);
53
54     graph->filters = filters;
55     graph->filters[graph->filter_count++] = filter;
56
57     return 0;
58 }
59
60 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
61                                  const char *name, const char *args, void *opaque,
62                                  AVFilterGraph *graph_ctx)
63 {
64     int ret;
65
66     if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
67         goto fail;
68     if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
69         goto fail;
70     if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
71         goto fail;
72     return 0;
73
74 fail:
75     if (*filt_ctx)
76         avfilter_free(*filt_ctx);
77     *filt_ctx = NULL;
78     return ret;
79 }
80
81 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
82 {
83     AVFilterContext *filt;
84     int i, j;
85
86     for (i = 0; i < graph->filter_count; i++) {
87         filt = graph->filters[i];
88
89         for (j = 0; j < filt->input_count; j++) {
90             if (!filt->inputs[j] || !filt->inputs[j]->src) {
91                 av_log(log_ctx, AV_LOG_ERROR,
92                        "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
93                        filt->input_pads[j].name, filt->name, filt->filter->name);
94                 return AVERROR(EINVAL);
95             }
96         }
97
98         for (j = 0; j < filt->output_count; j++) {
99             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
100                 av_log(log_ctx, AV_LOG_ERROR,
101                        "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
102                        filt->output_pads[j].name, filt->name, filt->filter->name);
103                 return AVERROR(EINVAL);
104             }
105         }
106     }
107
108     return 0;
109 }
110
111 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
112 {
113     AVFilterContext *filt;
114     int i, ret;
115
116     for (i=0; i < graph->filter_count; i++) {
117         filt = graph->filters[i];
118
119         if (!filt->output_count) {
120             if ((ret = avfilter_config_links(filt)))
121                 return ret;
122         }
123     }
124
125     return 0;
126 }
127
128 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
129 {
130     int i;
131
132     for (i = 0; i < graph->filter_count; i++)
133         if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
134             return graph->filters[i];
135
136     return NULL;
137 }
138
139 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
140                               const char *filt_name, const char *filt_args)
141 {
142     static int auto_count = 0, ret;
143     char inst_name[32];
144     AVFilterContext *filt_ctx;
145
146     snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
147             filt_name, auto_count++);
148
149     if ((ret = avfilter_graph_create_filter(&filt_ctx,
150                                             avfilter_get_by_name(filt_name),
151                                             inst_name, filt_args, NULL, graph)) < 0)
152         return ret;
153     if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
154         return ret;
155
156     filt_ctx->filter->query_formats(filt_ctx);
157
158     if ( ((link = filt_ctx-> inputs[0]) &&
159            !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
160          ((link = filt_ctx->outputs[0]) &&
161            !avfilter_merge_formats(link->in_formats, link->out_formats))
162        ) {
163         av_log(NULL, AV_LOG_ERROR,
164                "Impossible to convert between the formats supported by the filter "
165                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
166         return AVERROR(EINVAL);
167     }
168
169     if (link->type == AVMEDIA_TYPE_AUDIO &&
170          (((link = filt_ctx-> inputs[0]) &&
171            (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
172             !avfilter_merge_formats(link->in_packing,   link->out_packing))) ||
173          ((link = filt_ctx->outputs[0]) &&
174            (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
175             !avfilter_merge_formats(link->in_packing,   link->out_packing))))
176        ) {
177         av_log(NULL, AV_LOG_ERROR,
178                "Impossible to convert between the channel layouts/packing formats supported by the filter "
179                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
180         return AVERROR(EINVAL);
181     }
182
183     return 0;
184 }
185
186 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
187 {
188     int i, j, ret;
189     char filt_args[128];
190     AVFilterFormats *formats, *chlayouts, *packing;
191
192     /* ask all the sub-filters for their supported media formats */
193     for (i = 0; i < graph->filter_count; i++) {
194         if (graph->filters[i]->filter->query_formats)
195             graph->filters[i]->filter->query_formats(graph->filters[i]);
196         else
197             avfilter_default_query_formats(graph->filters[i]);
198     }
199
200     /* go through and merge as many format lists as possible */
201     for (i = 0; i < graph->filter_count; i++) {
202         AVFilterContext *filter = graph->filters[i];
203
204         for (j = 0; j < filter->input_count; j++) {
205             AVFilterLink *link = filter->inputs[j];
206             if (!link) continue;
207
208             if (!link->in_formats || !link->out_formats)
209                 return AVERROR(EINVAL);
210
211             if (link->type == AVMEDIA_TYPE_VIDEO &&
212                 !avfilter_merge_formats(link->in_formats, link->out_formats)) {
213
214                 /* couldn't merge format lists, auto-insert scale filter */
215                 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
216                          graph->scale_sws_opts);
217                 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
218                     return ret;
219             }
220             else if (link->type == AVMEDIA_TYPE_AUDIO) {
221                 if (!link->in_chlayouts || !link->out_chlayouts ||
222                     !link->in_packing   || !link->out_packing)
223                     return AVERROR(EINVAL);
224
225                 /* Merge all three list before checking: that way, in all
226                  * three categories, aconvert will use a common format
227                  * whenever possible. */
228                 formats   = avfilter_merge_formats(link->in_formats,   link->out_formats);
229                 chlayouts = avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts);
230                 packing   = avfilter_merge_formats(link->in_packing,   link->out_packing);
231                 if (!formats || !chlayouts || !packing)
232                     if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
233                        return ret;
234             }
235         }
236     }
237
238     return 0;
239 }
240
241 static void pick_format(AVFilterLink *link)
242 {
243     if (!link || !link->in_formats)
244         return;
245
246     link->in_formats->format_count = 1;
247     link->format = link->in_formats->formats[0];
248     avfilter_formats_unref(&link->in_formats);
249     avfilter_formats_unref(&link->out_formats);
250
251     if (link->type == AVMEDIA_TYPE_AUDIO) {
252         link->in_chlayouts->format_count = 1;
253         link->channel_layout = link->in_chlayouts->formats[0];
254         avfilter_formats_unref(&link->in_chlayouts);
255         avfilter_formats_unref(&link->out_chlayouts);
256
257         link->in_packing->format_count = 1;
258         link->planar = link->in_packing->formats[0] == AVFILTER_PLANAR;
259         avfilter_formats_unref(&link->in_packing);
260         avfilter_formats_unref(&link->out_packing);
261     }
262 }
263
264 static void pick_formats(AVFilterGraph *graph)
265 {
266     int i, j;
267
268     for (i = 0; i < graph->filter_count; i++) {
269         AVFilterContext *filter = graph->filters[i];
270
271         for (j = 0; j < filter->input_count; j++)
272             pick_format(filter->inputs[j]);
273         for (j = 0; j < filter->output_count; j++)
274             pick_format(filter->outputs[j]);
275     }
276 }
277
278 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
279 {
280     int ret;
281
282     /* find supported formats from sub-filters, and merge along links */
283     if ((ret = query_formats(graph, log_ctx)) < 0)
284         return ret;
285
286     /* Once everything is merged, it's possible that we'll still have
287      * multiple valid media format choices. We pick the first one. */
288     pick_formats(graph);
289
290     return 0;
291 }
292
293 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
294 {
295     int ret;
296
297     if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
298         return ret;
299     if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
300         return ret;
301     if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
302         return ret;
303
304     return 0;
305 }
306
307 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
308 {
309     int i, r = AVERROR(ENOSYS);
310
311     if(!graph)
312         return r;
313
314     if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
315         r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
316         if(r != AVERROR(ENOSYS))
317             return r;
318     }
319
320     if(res_len && res)
321         res[0]= 0;
322
323     for (i = 0; i < graph->filter_count; i++) {
324         AVFilterContext *filter = graph->filters[i];
325         if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
326             r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
327             if(r != AVERROR(ENOSYS)) {
328                 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
329                     return r;
330             }
331         }
332     }
333
334     return r;
335 }
336
337 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
338 {
339     int i;
340
341     if(!graph)
342         return 0;
343
344     for (i = 0; i < graph->filter_count; i++) {
345         AVFilterContext *filter = graph->filters[i];
346         if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
347             AVFilterCommand **que = &filter->command_queue, *next;
348             while(*que && (*que)->time <= ts)
349                 que = &(*que)->next;
350             next= *que;
351             *que= av_mallocz(sizeof(AVFilterCommand));
352             (*que)->command = av_strdup(command);
353             (*que)->arg     = av_strdup(arg);
354             (*que)->time    = ts;
355             (*que)->flags   = flags;
356             (*que)->next    = next;
357             if(flags & AVFILTER_CMD_FLAG_ONE)
358                 return 0;
359         }
360     }
361
362     return 0;
363 }