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