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