]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
avfiltergraph: smarter sample format selection.
[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 "formats.h"
29 #include "internal.h"
30
31 #include "libavutil/audioconvert.h"
32 #include "libavutil/log.h"
33
34 static const AVClass filtergraph_class = {
35     .class_name = "AVFilterGraph",
36     .item_name  = av_default_item_name,
37     .version    = LIBAVUTIL_VERSION_INT,
38 };
39
40 AVFilterGraph *avfilter_graph_alloc(void)
41 {
42     AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
43     if (!ret)
44         return NULL;
45 #if FF_API_GRAPH_AVCLASS
46     ret->av_class = &filtergraph_class;
47 #endif
48     return ret;
49 }
50
51 void avfilter_graph_free(AVFilterGraph **graph)
52 {
53     if (!*graph)
54         return;
55     for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
56         avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
57     av_freep(&(*graph)->scale_sws_opts);
58     av_freep(&(*graph)->filters);
59     av_freep(graph);
60 }
61
62 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
63 {
64     AVFilterContext **filters = av_realloc(graph->filters,
65                                            sizeof(AVFilterContext*) * (graph->filter_count+1));
66     if (!filters)
67         return AVERROR(ENOMEM);
68
69     graph->filters = filters;
70     graph->filters[graph->filter_count++] = filter;
71
72     return 0;
73 }
74
75 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
76                                  const char *name, const char *args, void *opaque,
77                                  AVFilterGraph *graph_ctx)
78 {
79     int ret;
80
81     if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
82         goto fail;
83     if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
84         goto fail;
85     if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
86         goto fail;
87     return 0;
88
89 fail:
90     if (*filt_ctx)
91         avfilter_free(*filt_ctx);
92     *filt_ctx = NULL;
93     return ret;
94 }
95
96 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
97 {
98     AVFilterContext *filt;
99     int i, j;
100
101     for (i = 0; i < graph->filter_count; i++) {
102         filt = graph->filters[i];
103
104         for (j = 0; j < filt->input_count; j++) {
105             if (!filt->inputs[j] || !filt->inputs[j]->src) {
106                 av_log(log_ctx, AV_LOG_ERROR,
107                        "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
108                        filt->input_pads[j].name, filt->name, filt->filter->name);
109                 return AVERROR(EINVAL);
110             }
111         }
112
113         for (j = 0; j < filt->output_count; j++) {
114             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
115                 av_log(log_ctx, AV_LOG_ERROR,
116                        "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
117                        filt->output_pads[j].name, filt->name, filt->filter->name);
118                 return AVERROR(EINVAL);
119             }
120         }
121     }
122
123     return 0;
124 }
125
126 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
127 {
128     AVFilterContext *filt;
129     int i, ret;
130
131     for (i=0; i < graph->filter_count; i++) {
132         filt = graph->filters[i];
133
134         if (!filt->output_count) {
135             if ((ret = avfilter_config_links(filt)))
136                 return ret;
137         }
138     }
139
140     return 0;
141 }
142
143 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
144 {
145     int i;
146
147     for (i = 0; i < graph->filter_count; i++)
148         if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
149             return graph->filters[i];
150
151     return NULL;
152 }
153
154 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
155 {
156     int i, j, ret;
157     int scaler_count = 0, resampler_count = 0;
158
159     /* ask all the sub-filters for their supported media formats */
160     for (i = 0; i < graph->filter_count; i++) {
161         if (graph->filters[i]->filter->query_formats)
162             graph->filters[i]->filter->query_formats(graph->filters[i]);
163         else
164             avfilter_default_query_formats(graph->filters[i]);
165     }
166
167     /* go through and merge as many format lists as possible */
168     for (i = 0; i < graph->filter_count; i++) {
169         AVFilterContext *filter = graph->filters[i];
170
171         for (j = 0; j < filter->input_count; j++) {
172             AVFilterLink *link = filter->inputs[j];
173             int convert_needed = 0;
174
175             if (!link)
176                 continue;
177
178             if (link->in_formats != link->out_formats &&
179                 !avfilter_merge_formats(link->in_formats,
180                                         link->out_formats))
181                 convert_needed = 1;
182             if (link->type == AVMEDIA_TYPE_AUDIO) {
183                 if (link->in_channel_layouts != link->out_channel_layouts &&
184                     !ff_merge_channel_layouts(link->in_channel_layouts,
185                                               link->out_channel_layouts))
186                     convert_needed = 1;
187                 if (link->in_samplerates != link->out_samplerates &&
188                     !ff_merge_samplerates(link->in_samplerates,
189                                           link->out_samplerates))
190                     convert_needed = 1;
191             }
192
193             if (convert_needed) {
194                 AVFilterContext *convert;
195                 AVFilter *filter;
196                 AVFilterLink *inlink, *outlink;
197                 char scale_args[256];
198                 char inst_name[30];
199
200                 /* couldn't merge format lists. auto-insert conversion filter */
201                 switch (link->type) {
202                 case AVMEDIA_TYPE_VIDEO:
203                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
204                              scaler_count++);
205                     snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
206                     if ((ret = avfilter_graph_create_filter(&convert,
207                                                             avfilter_get_by_name("scale"),
208                                                             inst_name, scale_args, NULL,
209                                                             graph)) < 0)
210                         return ret;
211                     break;
212                 case AVMEDIA_TYPE_AUDIO:
213                     if (!(filter = avfilter_get_by_name("resample"))) {
214                         av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
215                                "not present, cannot convert audio formats.\n");
216                         return AVERROR(EINVAL);
217                     }
218
219                     snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
220                              resampler_count++);
221                     if ((ret = avfilter_graph_create_filter(&convert,
222                                                             avfilter_get_by_name("resample"),
223                                                             inst_name, NULL, NULL, graph)) < 0)
224                         return ret;
225                     break;
226                 default:
227                     return AVERROR(EINVAL);
228                 }
229
230                 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
231                     return ret;
232
233                 convert->filter->query_formats(convert);
234                 inlink  = convert->inputs[0];
235                 outlink = convert->outputs[0];
236                 if (!avfilter_merge_formats( inlink->in_formats,  inlink->out_formats) ||
237                     !avfilter_merge_formats(outlink->in_formats, outlink->out_formats))
238                     ret |= AVERROR(ENOSYS);
239                 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
240                     (!ff_merge_samplerates(inlink->in_samplerates,
241                                            inlink->out_samplerates) ||
242                      !ff_merge_channel_layouts(inlink->in_channel_layouts,
243                                                inlink->out_channel_layouts)))
244                     ret |= AVERROR(ENOSYS);
245                 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
246                     (!ff_merge_samplerates(outlink->in_samplerates,
247                                            outlink->out_samplerates) ||
248                      !ff_merge_channel_layouts(outlink->in_channel_layouts,
249                                                outlink->out_channel_layouts)))
250                     ret |= AVERROR(ENOSYS);
251
252                 if (ret < 0) {
253                     av_log(log_ctx, AV_LOG_ERROR,
254                            "Impossible to convert between the formats supported by the filter "
255                            "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
256                     return ret;
257                 }
258             }
259         }
260     }
261
262     return 0;
263 }
264
265 static int pick_format(AVFilterLink *link)
266 {
267     if (!link || !link->in_formats)
268         return 0;
269
270     link->in_formats->format_count = 1;
271     link->format = link->in_formats->formats[0];
272
273     if (link->type == AVMEDIA_TYPE_AUDIO) {
274         if (!link->in_samplerates->format_count) {
275             av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
276                    " the link between filters %s and %s.\n", link->src->name,
277                    link->dst->name);
278             return AVERROR(EINVAL);
279         }
280         link->in_samplerates->format_count = 1;
281         link->sample_rate = link->in_samplerates->formats[0];
282
283         if (!link->in_channel_layouts->nb_channel_layouts) {
284             av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
285                    "the link between filters %s and %s.\n", link->src->name,
286                    link->dst->name);
287             return AVERROR(EINVAL);
288         }
289         link->in_channel_layouts->nb_channel_layouts = 1;
290         link->channel_layout = link->in_channel_layouts->channel_layouts[0];
291     }
292
293     avfilter_formats_unref(&link->in_formats);
294     avfilter_formats_unref(&link->out_formats);
295     avfilter_formats_unref(&link->in_samplerates);
296     avfilter_formats_unref(&link->out_samplerates);
297     ff_channel_layouts_unref(&link->in_channel_layouts);
298     ff_channel_layouts_unref(&link->out_channel_layouts);
299
300     return 0;
301 }
302
303 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
304 do {                                                                   \
305     for (i = 0; i < filter->input_count; i++) {                        \
306         AVFilterLink *link = filter->inputs[i];                        \
307         fmt_type fmt;                                                  \
308                                                                        \
309         if (!link->out_ ## list || link->out_ ## list->nb != 1)        \
310             continue;                                                  \
311         fmt = link->out_ ## list->var[0];                              \
312                                                                        \
313         for (j = 0; j < filter->output_count; j++) {                   \
314             AVFilterLink *out_link = filter->outputs[j];               \
315             list_type *fmts;                                           \
316                                                                        \
317             if (link->type != out_link->type ||                        \
318                 out_link->in_ ## list->nb == 1)                        \
319                 continue;                                              \
320             fmts = out_link->in_ ## list;                              \
321                                                                        \
322             if (!out_link->in_ ## list->nb) {                          \
323                 add_format(&out_link->in_ ##list, fmt);                \
324                 break;                                                 \
325             }                                                          \
326                                                                        \
327             for (k = 0; k < out_link->in_ ## list->nb; k++)            \
328                 if (fmts->var[k] == fmt) {                             \
329                     fmts->var[0]  = fmt;                               \
330                     fmts->nb = 1;                                      \
331                     ret = 1;                                           \
332                     break;                                             \
333                 }                                                      \
334         }                                                              \
335     }                                                                  \
336 } while (0)
337
338 static int reduce_formats_on_filter(AVFilterContext *filter)
339 {
340     int i, j, k, ret = 0;
341
342     REDUCE_FORMATS(int,      AVFilterFormats,        formats,         formats,
343                    format_count, avfilter_add_format);
344     REDUCE_FORMATS(int,      AVFilterFormats,        samplerates,     formats,
345                    format_count, avfilter_add_format);
346     REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
347                    channel_layouts, nb_channel_layouts, ff_add_channel_layout);
348
349     return ret;
350 }
351
352 static void reduce_formats(AVFilterGraph *graph)
353 {
354     int i, reduced;
355
356     do {
357         reduced = 0;
358
359         for (i = 0; i < graph->filter_count; i++)
360             reduced |= reduce_formats_on_filter(graph->filters[i]);
361     } while (reduced);
362 }
363
364 static void swap_samplerates_on_filter(AVFilterContext *filter)
365 {
366     AVFilterLink *link = NULL;
367     int sample_rate;
368     int i, j;
369
370     for (i = 0; i < filter->input_count; i++) {
371         link = filter->inputs[i];
372
373         if (link->type == AVMEDIA_TYPE_AUDIO &&
374             link->out_samplerates->format_count == 1)
375             break;
376     }
377     if (i == filter->input_count)
378         return;
379
380     sample_rate = link->out_samplerates->formats[0];
381
382     for (i = 0; i < filter->output_count; i++) {
383         AVFilterLink *outlink = filter->outputs[i];
384         int best_idx, best_diff = INT_MAX;
385
386         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
387             outlink->in_samplerates->format_count < 2)
388             continue;
389
390         for (j = 0; j < outlink->in_samplerates->format_count; j++) {
391             int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
392
393             if (diff < best_diff) {
394                 best_diff = diff;
395                 best_idx  = j;
396             }
397         }
398         FFSWAP(int, outlink->in_samplerates->formats[0],
399                outlink->in_samplerates->formats[best_idx]);
400     }
401 }
402
403 static void swap_samplerates(AVFilterGraph *graph)
404 {
405     int i;
406
407     for (i = 0; i < graph->filter_count; i++)
408         swap_samplerates_on_filter(graph->filters[i]);
409 }
410
411 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
412 {
413     AVFilterLink *link = NULL;
414     uint64_t chlayout;
415     int i, j;
416
417     for (i = 0; i < filter->input_count; i++) {
418         link = filter->inputs[i];
419
420         if (link->type == AVMEDIA_TYPE_AUDIO &&
421             link->out_channel_layouts->nb_channel_layouts == 1)
422             break;
423     }
424     if (i == filter->input_count)
425         return;
426
427     chlayout = link->out_channel_layouts->channel_layouts[0];
428
429     for (i = 0; i < filter->output_count; i++) {
430         AVFilterLink *outlink = filter->outputs[i];
431         int best_idx, best_score = INT_MIN;
432
433         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
434             outlink->in_channel_layouts->nb_channel_layouts < 2)
435             continue;
436
437         for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
438             uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
439             int matched_channels  = av_get_channel_layout_nb_channels(chlayout &
440                                                                       out_chlayout);
441             int extra_channels     = av_get_channel_layout_nb_channels(out_chlayout &
442                                                                        (~chlayout));
443             int score = matched_channels - extra_channels;
444
445             if (score > best_score) {
446                 best_score = score;
447                 best_idx   = j;
448             }
449         }
450         FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
451                outlink->in_channel_layouts->channel_layouts[best_idx]);
452     }
453
454 }
455
456 static void swap_channel_layouts(AVFilterGraph *graph)
457 {
458     int i;
459
460     for (i = 0; i < graph->filter_count; i++)
461         swap_channel_layouts_on_filter(graph->filters[i]);
462 }
463
464 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
465 {
466     AVFilterLink *link = NULL;
467     int format, bps;
468     int i, j;
469
470     for (i = 0; i < filter->input_count; i++) {
471         link = filter->inputs[i];
472
473         if (link->type == AVMEDIA_TYPE_AUDIO &&
474             link->out_formats->format_count == 1)
475             break;
476     }
477     if (i == filter->input_count)
478         return;
479
480     format = link->out_formats->formats[0];
481     bps    = av_get_bytes_per_sample(format);
482
483     for (i = 0; i < filter->output_count; i++) {
484         AVFilterLink *outlink = filter->outputs[i];
485         int best_idx, best_score = INT_MIN;
486
487         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
488             outlink->in_formats->format_count < 2)
489             continue;
490
491         for (j = 0; j < outlink->in_formats->format_count; j++) {
492             int out_format = outlink->in_formats->formats[j];
493             int out_bps    = av_get_bytes_per_sample(out_format);
494             int score;
495
496             if (av_get_packed_sample_fmt(out_format) == format ||
497                 av_get_planar_sample_fmt(out_format) == format) {
498                 best_idx   = j;
499                 break;
500             }
501
502             /* for s32 and float prefer double to prevent loss of information */
503             if (bps == 4 && out_bps == 8) {
504                 best_idx = j;
505                 break;
506             }
507
508             /* prefer closest higher or equal bps */
509             score = -abs(out_bps - bps);
510             if (out_bps >= bps)
511                 score += INT_MAX/2;
512
513             if (score > best_score) {
514                 best_score = score;
515                 best_idx   = j;
516             }
517         }
518         FFSWAP(int, outlink->in_formats->formats[0],
519                outlink->in_formats->formats[best_idx]);
520     }
521 }
522
523 static void swap_sample_fmts(AVFilterGraph *graph)
524 {
525     int i;
526
527     for (i = 0; i < graph->filter_count; i++)
528         swap_sample_fmts_on_filter(graph->filters[i]);
529
530 }
531
532 static int pick_formats(AVFilterGraph *graph)
533 {
534     int i, j, ret;
535
536     for (i = 0; i < graph->filter_count; i++) {
537         AVFilterContext *filter = graph->filters[i];
538
539         for (j = 0; j < filter->input_count; j++)
540             if ((ret = pick_format(filter->inputs[j])) < 0)
541                 return ret;
542         for (j = 0; j < filter->output_count; j++)
543             if ((ret = pick_format(filter->outputs[j])) < 0)
544                 return ret;
545     }
546     return 0;
547 }
548
549 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
550 {
551     int ret;
552
553     /* find supported formats from sub-filters, and merge along links */
554     if ((ret = query_formats(graph, log_ctx)) < 0)
555         return ret;
556
557     /* Once everything is merged, it's possible that we'll still have
558      * multiple valid media format choices. We try to minimize the amount
559      * of format conversion inside filters */
560     reduce_formats(graph);
561
562     /* for audio filters, ensure the best format, sample rate and channel layout
563      * is selected */
564     swap_sample_fmts(graph);
565     swap_samplerates(graph);
566     swap_channel_layouts(graph);
567
568     if ((ret = pick_formats(graph)) < 0)
569         return ret;
570
571     return 0;
572 }
573
574 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
575 {
576     int ret;
577
578     if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
579         return ret;
580     if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
581         return ret;
582     if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
583         return ret;
584
585     return 0;
586 }