+ if (link->type == AVMEDIA_TYPE_AUDIO) {
+ if (!link->in_samplerates->format_count) {
+ av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
+ " the link between filters %s and %s.\n", link->src->name,
+ link->dst->name);
+ return AVERROR(EINVAL);
+ }
+ link->in_samplerates->format_count = 1;
+ link->sample_rate = link->in_samplerates->formats[0];
+
+ if (!link->in_channel_layouts->nb_channel_layouts) {
+ av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
+ "the link between filters %s and %s.\n", link->src->name,
+ link->dst->name);
+ return AVERROR(EINVAL);
+ }
+ link->in_channel_layouts->nb_channel_layouts = 1;
+ link->channel_layout = link->in_channel_layouts->channel_layouts[0];
+ }
+
+ ff_formats_unref(&link->in_formats);
+ ff_formats_unref(&link->out_formats);
+ ff_formats_unref(&link->in_samplerates);
+ ff_formats_unref(&link->out_samplerates);
+ ff_channel_layouts_unref(&link->in_channel_layouts);
+ ff_channel_layouts_unref(&link->out_channel_layouts);
+
+ return 0;
+}
+
+#define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
+do { \
+ for (i = 0; i < filter->input_count; i++) { \
+ AVFilterLink *link = filter->inputs[i]; \
+ fmt_type fmt; \
+ \
+ if (!link->out_ ## list || link->out_ ## list->nb != 1) \
+ continue; \
+ fmt = link->out_ ## list->var[0]; \
+ \
+ for (j = 0; j < filter->output_count; j++) { \
+ AVFilterLink *out_link = filter->outputs[j]; \
+ list_type *fmts; \
+ \
+ if (link->type != out_link->type || \
+ out_link->in_ ## list->nb == 1) \
+ continue; \
+ fmts = out_link->in_ ## list; \
+ \
+ if (!out_link->in_ ## list->nb) { \
+ add_format(&out_link->in_ ##list, fmt); \
+ break; \
+ } \
+ \
+ for (k = 0; k < out_link->in_ ## list->nb; k++) \
+ if (fmts->var[k] == fmt) { \
+ fmts->var[0] = fmt; \
+ fmts->nb = 1; \
+ ret = 1; \
+ break; \
+ } \
+ } \
+ } \
+} while (0)
+
+static int reduce_formats_on_filter(AVFilterContext *filter)
+{
+ int i, j, k, ret = 0;
+
+ REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
+ format_count, ff_add_format);
+ REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
+ format_count, ff_add_format);
+ REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
+ channel_layouts, nb_channel_layouts, ff_add_channel_layout);
+
+ return ret;
+}
+
+static void reduce_formats(AVFilterGraph *graph)
+{
+ int i, reduced;
+
+ do {
+ reduced = 0;
+
+ for (i = 0; i < graph->filter_count; i++)
+ reduced |= reduce_formats_on_filter(graph->filters[i]);
+ } while (reduced);
+}
+
+static void swap_samplerates_on_filter(AVFilterContext *filter)
+{
+ AVFilterLink *link = NULL;
+ int sample_rate;
+ int i, j;
+
+ for (i = 0; i < filter->input_count; i++) {
+ link = filter->inputs[i];
+
+ if (link->type == AVMEDIA_TYPE_AUDIO &&
+ link->out_samplerates->format_count == 1)
+ break;
+ }
+ if (i == filter->input_count)
+ return;
+
+ sample_rate = link->out_samplerates->formats[0];
+
+ for (i = 0; i < filter->output_count; i++) {
+ AVFilterLink *outlink = filter->outputs[i];
+ int best_idx, best_diff = INT_MAX;
+
+ if (outlink->type != AVMEDIA_TYPE_AUDIO ||
+ outlink->in_samplerates->format_count < 2)
+ continue;
+
+ for (j = 0; j < outlink->in_samplerates->format_count; j++) {
+ int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
+
+ if (diff < best_diff) {
+ best_diff = diff;
+ best_idx = j;
+ }
+ }
+ FFSWAP(int, outlink->in_samplerates->formats[0],
+ outlink->in_samplerates->formats[best_idx]);
+ }
+}
+
+static void swap_samplerates(AVFilterGraph *graph)
+{
+ int i;
+
+ for (i = 0; i < graph->filter_count; i++)
+ swap_samplerates_on_filter(graph->filters[i]);