]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
intrax8: Use local destination buffers
[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 "config.h"
24
25 #include <string.h>
26
27 #include "libavutil/avassert.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/common.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/log.h"
33 #include "libavutil/opt.h"
34
35 #include "avfilter.h"
36 #include "formats.h"
37 #include "internal.h"
38 #include "thread.h"
39
40 #define OFFSET(x) offsetof(AVFilterGraph, x)
41 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
42 static const AVOption filtergraph_options[] = {
43     { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
44         { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
45         { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" },
46     { "threads",     "Maximum number of threads", OFFSET(nb_threads),
47         AV_OPT_TYPE_INT,   { .i64 = 0 }, 0, INT_MAX, FLAGS },
48     { NULL },
49 };
50
51 static const AVClass filtergraph_class = {
52     .class_name = "AVFilterGraph",
53     .item_name  = av_default_item_name,
54     .version    = LIBAVUTIL_VERSION_INT,
55     .option     = filtergraph_options,
56 };
57
58 #if !HAVE_THREADS
59 void ff_graph_thread_free(AVFilterGraph *graph)
60 {
61 }
62
63 int ff_graph_thread_init(AVFilterGraph *graph)
64 {
65     graph->thread_type = 0;
66     graph->nb_threads  = 1;
67     return 0;
68 }
69 #endif
70
71 AVFilterGraph *avfilter_graph_alloc(void)
72 {
73     AVFilterGraph *ret = av_mallocz(sizeof(*ret));
74     if (!ret)
75         return NULL;
76
77     ret->internal = av_mallocz(sizeof(*ret->internal));
78     if (!ret->internal) {
79         av_freep(&ret);
80         return NULL;
81     }
82
83     ret->av_class = &filtergraph_class;
84     av_opt_set_defaults(ret);
85
86     return ret;
87 }
88
89 void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
90 {
91     int i;
92     for (i = 0; i < graph->nb_filters; i++) {
93         if (graph->filters[i] == filter) {
94             FFSWAP(AVFilterContext*, graph->filters[i],
95                    graph->filters[graph->nb_filters - 1]);
96             graph->nb_filters--;
97             return;
98         }
99     }
100 }
101
102 void avfilter_graph_free(AVFilterGraph **graph)
103 {
104     if (!*graph)
105         return;
106
107     while ((*graph)->nb_filters)
108         avfilter_free((*graph)->filters[0]);
109
110     ff_graph_thread_free(*graph);
111
112     av_freep(&(*graph)->scale_sws_opts);
113     av_freep(&(*graph)->resample_lavr_opts);
114     av_freep(&(*graph)->filters);
115     av_freep(&(*graph)->internal);
116     av_freep(graph);
117 }
118
119 #if FF_API_AVFILTER_OPEN
120 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
121 {
122     AVFilterContext **filters = av_realloc(graph->filters,
123                                            sizeof(*filters) * (graph->nb_filters + 1));
124     if (!filters)
125         return AVERROR(ENOMEM);
126
127     graph->filters = filters;
128     graph->filters[graph->nb_filters++] = filter;
129
130     filter->graph = graph;
131
132     return 0;
133 }
134 #endif
135
136 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt,
137                                  const char *name, const char *args, void *opaque,
138                                  AVFilterGraph *graph_ctx)
139 {
140     int ret;
141
142     *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
143     if (!*filt_ctx)
144         return AVERROR(ENOMEM);
145
146     ret = avfilter_init_str(*filt_ctx, args);
147     if (ret < 0)
148         goto fail;
149
150     return 0;
151
152 fail:
153     if (*filt_ctx)
154         avfilter_free(*filt_ctx);
155     *filt_ctx = NULL;
156     return ret;
157 }
158
159 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
160                                              const AVFilter *filter,
161                                              const char *name)
162 {
163     AVFilterContext **filters, *s;
164
165     if (graph->thread_type && !graph->internal->thread_execute) {
166         if (graph->execute) {
167             graph->internal->thread_execute = graph->execute;
168         } else {
169             int ret = ff_graph_thread_init(graph);
170             if (ret < 0) {
171                 av_log(graph, AV_LOG_ERROR, "Error initializing threading.\n");
172                 return NULL;
173             }
174         }
175     }
176
177     s = ff_filter_alloc(filter, name);
178     if (!s)
179         return NULL;
180
181     filters = av_realloc(graph->filters, sizeof(*filters) * (graph->nb_filters + 1));
182     if (!filters) {
183         avfilter_free(s);
184         return NULL;
185     }
186
187     graph->filters = filters;
188     graph->filters[graph->nb_filters++] = s;
189
190     s->graph = graph;
191
192     return s;
193 }
194
195 /**
196  * Check for the validity of graph.
197  *
198  * A graph is considered valid if all its input and output pads are
199  * connected.
200  *
201  * @return 0 in case of success, a negative value otherwise
202  */
203 static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
204 {
205     AVFilterContext *filt;
206     int i, j;
207
208     for (i = 0; i < graph->nb_filters; i++) {
209         filt = graph->filters[i];
210
211         for (j = 0; j < filt->nb_inputs; j++) {
212             if (!filt->inputs[j] || !filt->inputs[j]->src) {
213                 av_log(log_ctx, AV_LOG_ERROR,
214                        "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
215                        filt->input_pads[j].name, filt->name, filt->filter->name);
216                 return AVERROR(EINVAL);
217             }
218         }
219
220         for (j = 0; j < filt->nb_outputs; j++) {
221             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
222                 av_log(log_ctx, AV_LOG_ERROR,
223                        "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
224                        filt->output_pads[j].name, filt->name, filt->filter->name);
225                 return AVERROR(EINVAL);
226             }
227         }
228     }
229
230     return 0;
231 }
232
233 /**
234  * Configure all the links of graphctx.
235  *
236  * @return 0 in case of success, a negative value otherwise
237  */
238 static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
239 {
240     AVFilterContext *filt;
241     int i, ret;
242
243     for (i = 0; i < graph->nb_filters; i++) {
244         filt = graph->filters[i];
245
246         if (!filt->nb_outputs) {
247             if ((ret = avfilter_config_links(filt)))
248                 return ret;
249         }
250     }
251
252     return 0;
253 }
254
255 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
256 {
257     int i;
258
259     for (i = 0; i < graph->nb_filters; i++)
260         if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
261             return graph->filters[i];
262
263     return NULL;
264 }
265
266 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
267 {
268     int i, j, ret;
269     int scaler_count = 0, resampler_count = 0;
270
271     /* ask all the sub-filters for their supported media formats */
272     for (i = 0; i < graph->nb_filters; i++) {
273         if (graph->filters[i]->filter->query_formats)
274             graph->filters[i]->filter->query_formats(graph->filters[i]);
275         else
276             ff_default_query_formats(graph->filters[i]);
277     }
278
279     /* go through and merge as many format lists as possible */
280     for (i = 0; i < graph->nb_filters; i++) {
281         AVFilterContext *filter = graph->filters[i];
282
283         for (j = 0; j < filter->nb_inputs; j++) {
284             AVFilterLink *link = filter->inputs[j];
285             int convert_needed = 0;
286
287             if (!link)
288                 continue;
289
290             if (link->in_formats != link->out_formats &&
291                 !ff_merge_formats(link->in_formats,
292                                         link->out_formats))
293                 convert_needed = 1;
294             if (link->type == AVMEDIA_TYPE_AUDIO) {
295                 if (link->in_channel_layouts != link->out_channel_layouts &&
296                     !ff_merge_channel_layouts(link->in_channel_layouts,
297                                               link->out_channel_layouts))
298                     convert_needed = 1;
299                 if (link->in_samplerates != link->out_samplerates &&
300                     !ff_merge_samplerates(link->in_samplerates,
301                                           link->out_samplerates))
302                     convert_needed = 1;
303             }
304
305             if (convert_needed) {
306                 AVFilterContext *convert;
307                 AVFilter *filter;
308                 AVFilterLink *inlink, *outlink;
309                 char scale_args[256];
310                 char inst_name[30];
311
312                 /* couldn't merge format lists. auto-insert conversion filter */
313                 switch (link->type) {
314                 case AVMEDIA_TYPE_VIDEO:
315                     if (!(filter = avfilter_get_by_name("scale"))) {
316                         av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
317                                "not present, cannot convert pixel formats.\n");
318                         return AVERROR(EINVAL);
319                     }
320
321                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
322                              scaler_count++);
323
324                     if ((ret = avfilter_graph_create_filter(&convert, filter,
325                                                             inst_name, graph->scale_sws_opts, NULL,
326                                                             graph)) < 0)
327                         return ret;
328                     break;
329                 case AVMEDIA_TYPE_AUDIO:
330                     if (!(filter = avfilter_get_by_name("resample"))) {
331                         av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
332                                "not present, cannot convert audio formats.\n");
333                         return AVERROR(EINVAL);
334                     }
335
336                     snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
337                              resampler_count++);
338                     scale_args[0] = '\0';
339                     if (graph->resample_lavr_opts)
340                         snprintf(scale_args, sizeof(scale_args), "%s",
341                                  graph->resample_lavr_opts);
342                     if ((ret = avfilter_graph_create_filter(&convert, filter,
343                                                             inst_name, scale_args,
344                                                             NULL, graph)) < 0)
345                         return ret;
346                     break;
347                 default:
348                     return AVERROR(EINVAL);
349                 }
350
351                 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
352                     return ret;
353
354                 convert->filter->query_formats(convert);
355                 inlink  = convert->inputs[0];
356                 outlink = convert->outputs[0];
357                 if (!ff_merge_formats( inlink->in_formats,  inlink->out_formats) ||
358                     !ff_merge_formats(outlink->in_formats, outlink->out_formats))
359                     ret |= AVERROR(ENOSYS);
360                 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
361                     (!ff_merge_samplerates(inlink->in_samplerates,
362                                            inlink->out_samplerates) ||
363                      !ff_merge_channel_layouts(inlink->in_channel_layouts,
364                                                inlink->out_channel_layouts)))
365                     ret |= AVERROR(ENOSYS);
366                 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
367                     (!ff_merge_samplerates(outlink->in_samplerates,
368                                            outlink->out_samplerates) ||
369                      !ff_merge_channel_layouts(outlink->in_channel_layouts,
370                                                outlink->out_channel_layouts)))
371                     ret |= AVERROR(ENOSYS);
372
373                 if (ret < 0) {
374                     av_log(log_ctx, AV_LOG_ERROR,
375                            "Impossible to convert between the formats supported by the filter "
376                            "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
377                     return ret;
378                 }
379             }
380         }
381     }
382
383     return 0;
384 }
385
386 static int pick_format(AVFilterLink *link)
387 {
388     if (!link || !link->in_formats)
389         return 0;
390
391     link->in_formats->nb_formats = 1;
392     link->format = link->in_formats->formats[0];
393
394     if (link->type == AVMEDIA_TYPE_AUDIO) {
395         if (!link->in_samplerates->nb_formats) {
396             av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
397                    " the link between filters %s and %s.\n", link->src->name,
398                    link->dst->name);
399             return AVERROR(EINVAL);
400         }
401         link->in_samplerates->nb_formats = 1;
402         link->sample_rate = link->in_samplerates->formats[0];
403
404         if (!link->in_channel_layouts->nb_channel_layouts) {
405             av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
406                    "the link between filters %s and %s.\n", link->src->name,
407                    link->dst->name);
408             return AVERROR(EINVAL);
409         }
410         link->in_channel_layouts->nb_channel_layouts = 1;
411         link->channel_layout = link->in_channel_layouts->channel_layouts[0];
412     }
413
414     ff_formats_unref(&link->in_formats);
415     ff_formats_unref(&link->out_formats);
416     ff_formats_unref(&link->in_samplerates);
417     ff_formats_unref(&link->out_samplerates);
418     ff_channel_layouts_unref(&link->in_channel_layouts);
419     ff_channel_layouts_unref(&link->out_channel_layouts);
420
421     return 0;
422 }
423
424 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
425 do {                                                                   \
426     for (i = 0; i < filter->nb_inputs; i++) {                          \
427         AVFilterLink *link = filter->inputs[i];                        \
428         fmt_type fmt;                                                  \
429                                                                        \
430         if (!link->out_ ## list || link->out_ ## list->nb != 1)        \
431             continue;                                                  \
432         fmt = link->out_ ## list->var[0];                              \
433                                                                        \
434         for (j = 0; j < filter->nb_outputs; j++) {                     \
435             AVFilterLink *out_link = filter->outputs[j];               \
436             list_type *fmts;                                           \
437                                                                        \
438             if (link->type != out_link->type ||                        \
439                 out_link->in_ ## list->nb == 1)                        \
440                 continue;                                              \
441             fmts = out_link->in_ ## list;                              \
442                                                                        \
443             if (!out_link->in_ ## list->nb) {                          \
444                 add_format(&out_link->in_ ##list, fmt);                \
445                 break;                                                 \
446             }                                                          \
447                                                                        \
448             for (k = 0; k < out_link->in_ ## list->nb; k++)            \
449                 if (fmts->var[k] == fmt) {                             \
450                     fmts->var[0]  = fmt;                               \
451                     fmts->nb = 1;                                      \
452                     ret = 1;                                           \
453                     break;                                             \
454                 }                                                      \
455         }                                                              \
456     }                                                                  \
457 } while (0)
458
459 static int reduce_formats_on_filter(AVFilterContext *filter)
460 {
461     int i, j, k, ret = 0;
462
463     REDUCE_FORMATS(int,      AVFilterFormats,        formats,         formats,
464                    nb_formats, ff_add_format);
465     REDUCE_FORMATS(int,      AVFilterFormats,        samplerates,     formats,
466                    nb_formats, ff_add_format);
467     REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
468                    channel_layouts, nb_channel_layouts, ff_add_channel_layout);
469
470     return ret;
471 }
472
473 static void reduce_formats(AVFilterGraph *graph)
474 {
475     int i, reduced;
476
477     do {
478         reduced = 0;
479
480         for (i = 0; i < graph->nb_filters; i++)
481             reduced |= reduce_formats_on_filter(graph->filters[i]);
482     } while (reduced);
483 }
484
485 static void swap_samplerates_on_filter(AVFilterContext *filter)
486 {
487     AVFilterLink *link = NULL;
488     int sample_rate;
489     int i, j;
490
491     for (i = 0; i < filter->nb_inputs; i++) {
492         link = filter->inputs[i];
493
494         if (link->type == AVMEDIA_TYPE_AUDIO &&
495             link->out_samplerates->nb_formats== 1)
496             break;
497     }
498     if (i == filter->nb_inputs)
499         return;
500
501     sample_rate = link->out_samplerates->formats[0];
502
503     for (i = 0; i < filter->nb_outputs; i++) {
504         AVFilterLink *outlink = filter->outputs[i];
505         int best_idx, best_diff = INT_MAX;
506
507         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
508             outlink->in_samplerates->nb_formats < 2)
509             continue;
510
511         for (j = 0; j < outlink->in_samplerates->nb_formats; j++) {
512             int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
513
514             if (diff < best_diff) {
515                 best_diff = diff;
516                 best_idx  = j;
517             }
518         }
519         FFSWAP(int, outlink->in_samplerates->formats[0],
520                outlink->in_samplerates->formats[best_idx]);
521     }
522 }
523
524 static void swap_samplerates(AVFilterGraph *graph)
525 {
526     int i;
527
528     for (i = 0; i < graph->nb_filters; i++)
529         swap_samplerates_on_filter(graph->filters[i]);
530 }
531
532 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
533 #define CH_FRONT_PAIR  (AV_CH_FRONT_LEFT           | AV_CH_FRONT_RIGHT)
534 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT          | AV_CH_STEREO_RIGHT)
535 #define CH_WIDE_PAIR   (AV_CH_WIDE_LEFT            | AV_CH_WIDE_RIGHT)
536 #define CH_SIDE_PAIR   (AV_CH_SIDE_LEFT            | AV_CH_SIDE_RIGHT)
537 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
538 #define CH_BACK_PAIR   (AV_CH_BACK_LEFT            | AV_CH_BACK_RIGHT)
539
540 /* allowable substitutions for channel pairs when comparing layouts,
541  * ordered by priority for both values */
542 static const uint64_t ch_subst[][2] = {
543     { CH_FRONT_PAIR,      CH_CENTER_PAIR     },
544     { CH_FRONT_PAIR,      CH_WIDE_PAIR       },
545     { CH_FRONT_PAIR,      AV_CH_FRONT_CENTER },
546     { CH_CENTER_PAIR,     CH_FRONT_PAIR      },
547     { CH_CENTER_PAIR,     CH_WIDE_PAIR       },
548     { CH_CENTER_PAIR,     AV_CH_FRONT_CENTER },
549     { CH_WIDE_PAIR,       CH_FRONT_PAIR      },
550     { CH_WIDE_PAIR,       CH_CENTER_PAIR     },
551     { CH_WIDE_PAIR,       AV_CH_FRONT_CENTER },
552     { AV_CH_FRONT_CENTER, CH_FRONT_PAIR      },
553     { AV_CH_FRONT_CENTER, CH_CENTER_PAIR     },
554     { AV_CH_FRONT_CENTER, CH_WIDE_PAIR       },
555     { CH_SIDE_PAIR,       CH_DIRECT_PAIR     },
556     { CH_SIDE_PAIR,       CH_BACK_PAIR       },
557     { CH_SIDE_PAIR,       AV_CH_BACK_CENTER  },
558     { CH_BACK_PAIR,       CH_DIRECT_PAIR     },
559     { CH_BACK_PAIR,       CH_SIDE_PAIR       },
560     { CH_BACK_PAIR,       AV_CH_BACK_CENTER  },
561     { AV_CH_BACK_CENTER,  CH_BACK_PAIR       },
562     { AV_CH_BACK_CENTER,  CH_DIRECT_PAIR     },
563     { AV_CH_BACK_CENTER,  CH_SIDE_PAIR       },
564 };
565
566 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
567 {
568     AVFilterLink *link = NULL;
569     int i, j, k;
570
571     for (i = 0; i < filter->nb_inputs; i++) {
572         link = filter->inputs[i];
573
574         if (link->type == AVMEDIA_TYPE_AUDIO &&
575             link->out_channel_layouts->nb_channel_layouts == 1)
576             break;
577     }
578     if (i == filter->nb_inputs)
579         return;
580
581     for (i = 0; i < filter->nb_outputs; i++) {
582         AVFilterLink *outlink = filter->outputs[i];
583         int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
584
585         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
586             outlink->in_channel_layouts->nb_channel_layouts < 2)
587             continue;
588
589         for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
590             uint64_t  in_chlayout = link->out_channel_layouts->channel_layouts[0];
591             uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
592             int  in_channels      = av_get_channel_layout_nb_channels(in_chlayout);
593             int out_channels      = av_get_channel_layout_nb_channels(out_chlayout);
594             int count_diff        = out_channels - in_channels;
595             int matched_channels, extra_channels;
596             int score = 0;
597
598             /* channel substitution */
599             for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
600                 uint64_t cmp0 = ch_subst[k][0];
601                 uint64_t cmp1 = ch_subst[k][1];
602                 if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
603                     (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
604                     in_chlayout  &= ~cmp0;
605                     out_chlayout &= ~cmp1;
606                     /* add score for channel match, minus a deduction for
607                        having to do the substitution */
608                     score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
609                 }
610             }
611
612             /* no penalty for LFE channel mismatch */
613             if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
614                 (out_chlayout & AV_CH_LOW_FREQUENCY))
615                 score += 10;
616             in_chlayout  &= ~AV_CH_LOW_FREQUENCY;
617             out_chlayout &= ~AV_CH_LOW_FREQUENCY;
618
619             matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
620                                                                  out_chlayout);
621             extra_channels   = av_get_channel_layout_nb_channels(out_chlayout &
622                                                                  (~in_chlayout));
623             score += 10 * matched_channels - 5 * extra_channels;
624
625             if (score > best_score ||
626                 (count_diff < best_count_diff && score == best_score)) {
627                 best_score = score;
628                 best_idx   = j;
629                 best_count_diff = count_diff;
630             }
631         }
632         av_assert0(best_idx >= 0);
633         FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
634                outlink->in_channel_layouts->channel_layouts[best_idx]);
635     }
636
637 }
638
639 static void swap_channel_layouts(AVFilterGraph *graph)
640 {
641     int i;
642
643     for (i = 0; i < graph->nb_filters; i++)
644         swap_channel_layouts_on_filter(graph->filters[i]);
645 }
646
647 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
648 {
649     AVFilterLink *link = NULL;
650     int format, bps;
651     int i, j;
652
653     for (i = 0; i < filter->nb_inputs; i++) {
654         link = filter->inputs[i];
655
656         if (link->type == AVMEDIA_TYPE_AUDIO &&
657             link->out_formats->nb_formats == 1)
658             break;
659     }
660     if (i == filter->nb_inputs)
661         return;
662
663     format = link->out_formats->formats[0];
664     bps    = av_get_bytes_per_sample(format);
665
666     for (i = 0; i < filter->nb_outputs; i++) {
667         AVFilterLink *outlink = filter->outputs[i];
668         int best_idx = -1, best_score = INT_MIN;
669
670         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
671             outlink->in_formats->nb_formats < 2)
672             continue;
673
674         for (j = 0; j < outlink->in_formats->nb_formats; j++) {
675             int out_format = outlink->in_formats->formats[j];
676             int out_bps    = av_get_bytes_per_sample(out_format);
677             int score;
678
679             if (av_get_packed_sample_fmt(out_format) == format ||
680                 av_get_planar_sample_fmt(out_format) == format) {
681                 best_idx   = j;
682                 break;
683             }
684
685             /* for s32 and float prefer double to prevent loss of information */
686             if (bps == 4 && out_bps == 8) {
687                 best_idx = j;
688                 break;
689             }
690
691             /* prefer closest higher or equal bps */
692             score = -abs(out_bps - bps);
693             if (out_bps >= bps)
694                 score += INT_MAX/2;
695
696             if (score > best_score) {
697                 best_score = score;
698                 best_idx   = j;
699             }
700         }
701         av_assert0(best_idx >= 0);
702         FFSWAP(int, outlink->in_formats->formats[0],
703                outlink->in_formats->formats[best_idx]);
704     }
705 }
706
707 static void swap_sample_fmts(AVFilterGraph *graph)
708 {
709     int i;
710
711     for (i = 0; i < graph->nb_filters; i++)
712         swap_sample_fmts_on_filter(graph->filters[i]);
713
714 }
715
716 static int pick_formats(AVFilterGraph *graph)
717 {
718     int i, j, ret;
719
720     for (i = 0; i < graph->nb_filters; i++) {
721         AVFilterContext *filter = graph->filters[i];
722
723         for (j = 0; j < filter->nb_inputs; j++)
724             if ((ret = pick_format(filter->inputs[j])) < 0)
725                 return ret;
726         for (j = 0; j < filter->nb_outputs; j++)
727             if ((ret = pick_format(filter->outputs[j])) < 0)
728                 return ret;
729     }
730     return 0;
731 }
732
733 /**
734  * Configure the formats of all the links in the graph.
735  */
736 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
737 {
738     int ret;
739
740     /* find supported formats from sub-filters, and merge along links */
741     if ((ret = query_formats(graph, log_ctx)) < 0)
742         return ret;
743
744     /* Once everything is merged, it's possible that we'll still have
745      * multiple valid media format choices. We try to minimize the amount
746      * of format conversion inside filters */
747     reduce_formats(graph);
748
749     /* for audio filters, ensure the best format, sample rate and channel layout
750      * is selected */
751     swap_sample_fmts(graph);
752     swap_samplerates(graph);
753     swap_channel_layouts(graph);
754
755     if ((ret = pick_formats(graph)) < 0)
756         return ret;
757
758     return 0;
759 }
760
761 static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
762 {
763     AVFilterContext *f;
764     int i, j, ret;
765     int fifo_count = 0;
766
767     for (i = 0; i < graph->nb_filters; i++) {
768         f = graph->filters[i];
769
770         for (j = 0; j < f->nb_inputs; j++) {
771             AVFilterLink *link = f->inputs[j];
772             AVFilterContext *fifo_ctx;
773             AVFilter *fifo;
774             char name[32];
775
776             if (!link->dstpad->needs_fifo)
777                 continue;
778
779             fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
780                    avfilter_get_by_name("fifo") :
781                    avfilter_get_by_name("afifo");
782
783             snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
784
785             ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
786                                                NULL, graph);
787             if (ret < 0)
788                 return ret;
789
790             ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
791             if (ret < 0)
792                 return ret;
793         }
794     }
795
796     return 0;
797 }
798
799 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
800 {
801     int ret;
802
803     if ((ret = graph_check_validity(graphctx, log_ctx)))
804         return ret;
805     if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
806         return ret;
807     if ((ret = graph_config_formats(graphctx, log_ctx)))
808         return ret;
809     if ((ret = graph_config_links(graphctx, log_ctx)))
810         return ret;
811
812     return 0;
813 }