From 3a2d5e68acb2ac0f8b09b896907a692b1c48c6b3 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 21 Sep 2017 11:07:02 +0200 Subject: audio: move libswresample wrapper out of audio filter code Move it from af_lavrresample.c to a new aconverter.c file, which is independent from the filter chain code. It also doesn't use mp_audio, and thus has no GPL dependencies. Preparation for later commits. Not particularly well tested, so have fun. --- audio/aconverter.c | 641 +++++++++++++++++++++++++++++++++++++++++ audio/aconverter.h | 39 +++ audio/aframe.c | 23 +- audio/aframe.h | 3 + audio/audio.c | 31 ++ audio/audio.h | 1 + audio/filter/af.c | 2 + audio/filter/af.h | 2 + audio/filter/af_lavrresample.c | 492 +++---------------------------- 9 files changed, 773 insertions(+), 461 deletions(-) create mode 100644 audio/aconverter.c create mode 100644 audio/aconverter.h (limited to 'audio') diff --git a/audio/aconverter.c b/audio/aconverter.c new file mode 100644 index 0000000000..f4efc61dc6 --- /dev/null +++ b/audio/aconverter.c @@ -0,0 +1,641 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "config.h" + +#include "common/common.h" +#include "common/av_common.h" +#include "common/msg.h" +#include "options/m_config.h" +#include "options/m_option.h" +#include "aconverter.h" +#include "aframe.h" +#include "fmt-conversion.h" +#include "format.h" + +#define HAVE_LIBSWRESAMPLE HAVE_IS_FFMPEG +#define HAVE_LIBAVRESAMPLE HAVE_IS_LIBAV + +#if HAVE_LIBAVRESAMPLE +#include +#elif HAVE_LIBSWRESAMPLE +#include +#define AVAudioResampleContext SwrContext +#define avresample_alloc_context swr_alloc +#define avresample_open swr_init +#define avresample_close(x) do { } while(0) +#define avresample_free swr_free +#define avresample_available(x) 0 +#define avresample_convert(ctx, out, out_planesize, out_samples, in, in_planesize, in_samples) \ + swr_convert(ctx, out, out_samples, (const uint8_t**)(in), in_samples) +#define avresample_set_channel_mapping swr_set_channel_mapping +#define avresample_set_compensation swr_set_compensation +#else +#error "config.h broken or no resampler found" +#endif + +struct mp_aconverter { + struct mp_log *log; + struct mpv_global *global; + double playback_speed; + bool is_resampling; + bool passthrough_mode; + struct AVAudioResampleContext *avrctx; + struct mp_aframe *avrctx_fmt; // output format of avrctx + struct mp_aframe *pool_fmt; // format used to allocate frames for avrctx output + struct mp_aframe *pre_out_fmt; // format before final conversion + struct AVAudioResampleContext *avrctx_out; // for output channel reordering + const struct mp_resample_opts *opts; // opts requested by the user + // At least libswresample keeps a pointer around for this: + int reorder_in[MP_NUM_CHANNELS]; + int reorder_out[MP_NUM_CHANNELS]; + struct mp_aframe_pool *reorder_buffer; + struct mp_aframe_pool *out_pool; + + int in_rate_user; // user input sample rate + int in_rate; // actual rate (used by lavr), adjusted for playback speed + int in_format; + struct mp_chmap in_channels; + int out_rate; + int out_format; + struct mp_chmap out_channels; + + struct mp_aframe *input; // queued input frame + bool input_eof; // queued input EOF + struct mp_aframe *output; // queued output frame + bool output_eof; // queued output EOF +}; + +#if HAVE_LIBAVRESAMPLE +static double get_delay(struct mp_aconverter *p) +{ + return avresample_get_delay(p->avrctx) / (double)p->in_rate + + avresample_available(p->avrctx) / (double)p->out_rate; +} +static int get_out_samples(struct mp_aconverter *p, int in_samples) +{ + return avresample_get_out_samples(p->avrctx, in_samples); +} +#else +static double get_delay(struct mp_aconverter *p) +{ + int64_t base = p->in_rate * (int64_t)p->out_rate; + return swr_get_delay(p->avrctx, base) / (double)base; +} +static int get_out_samples(struct mp_aconverter *p, int in_samples) +{ + return swr_get_out_samples(p->avrctx, in_samples); +} +#endif + +static void close_lavrr(struct mp_aconverter *p) +{ + if (p->avrctx) + avresample_close(p->avrctx); + avresample_free(&p->avrctx); + if (p->avrctx_out) + avresample_close(p->avrctx_out); + avresample_free(&p->avrctx_out); + + TA_FREEP(&p->pre_out_fmt); + TA_FREEP(&p->avrctx_fmt); + TA_FREEP(&p->pool_fmt); +} + +static int rate_from_speed(int rate, double speed) +{ + return lrint(rate * speed); +} + +static struct mp_chmap fudge_pairs[][2] = { + {MP_CHMAP2(BL, BR), MP_CHMAP2(SL, SR)}, + {MP_CHMAP2(SL, SR), MP_CHMAP2(BL, BR)}, + {MP_CHMAP2(SDL, SDR), MP_CHMAP2(SL, SR)}, + {MP_CHMAP2(SL, SR), MP_CHMAP2(SDL, SDR)}, +}; + +// Modify out_layout and return the new value. The intention is reducing the +// loss libswresample's rematrixing will cause by exchanging similar, but +// strictly speaking incompatible channel pairs. For example, 7.1 should be +// changed to 7.1(wide) without dropping the SL/SR channels. (We still leave +// it to libswresample to create the remix matrix.) +static uint64_t fudge_layout_conversion(struct mp_aconverter *p, + uint64_t in, uint64_t out) +{ + for (int n = 0; n < MP_ARRAY_SIZE(fudge_pairs); n++) { + uint64_t a = mp_chmap_to_lavc(&fudge_pairs[n][0]); + uint64_t b = mp_chmap_to_lavc(&fudge_pairs[n][1]); + if ((in & a) == a && (in & b) == 0 && + (out & a) == 0 && (out & b) == b) + { + out = (out & ~b) | a; + + MP_VERBOSE(p, "Fudge: %s -> %s\n", + mp_chmap_to_str(&fudge_pairs[n][0]), + mp_chmap_to_str(&fudge_pairs[n][1])); + } + } + return out; +} + +// mp_chmap_get_reorder() performs: +// to->speaker[n] = from->speaker[src[n]] +// but libavresample does: +// to->speaker[dst[n]] = from->speaker[n] +static void transpose_order(int *map, int num) +{ + int nmap[MP_NUM_CHANNELS] = {0}; + for (int n = 0; n < num; n++) { + for (int i = 0; i < num; i++) { + if (map[n] == i) + nmap[i] = n; + } + } + memcpy(map, nmap, sizeof(nmap)); +} + +static bool configure_lavrr(struct mp_aconverter *p, bool verbose) +{ + close_lavrr(p); + + p->in_rate = rate_from_speed(p->in_rate_user, p->playback_speed); + + p->passthrough_mode = p->opts->allow_passthrough && + p->in_rate == p->out_rate && + p->in_format == p->out_format && + mp_chmap_equals(&p->in_channels, &p->out_channels); + + if (p->passthrough_mode) + return true; + + p->avrctx = avresample_alloc_context(); + p->avrctx_out = avresample_alloc_context(); + if (!p->avrctx || !p->avrctx_out) + goto error; + + enum AVSampleFormat in_samplefmt = af_to_avformat(p->in_format); + enum AVSampleFormat out_samplefmt = af_to_avformat(p->out_format); + enum AVSampleFormat out_samplefmtp = av_get_planar_sample_fmt(out_samplefmt); + + if (in_samplefmt == AV_SAMPLE_FMT_NONE || + out_samplefmt == AV_SAMPLE_FMT_NONE || + out_samplefmtp == AV_SAMPLE_FMT_NONE) + goto error; + + av_opt_set_int(p->avrctx, "filter_size", p->opts->filter_size, 0); + av_opt_set_int(p->avrctx, "phase_shift", p->opts->phase_shift, 0); + av_opt_set_int(p->avrctx, "linear_interp", p->opts->linear, 0); + + double cutoff = p->opts->cutoff; + if (cutoff <= 0.0) + cutoff = MPMAX(1.0 - 6.5 / (p->opts->filter_size + 8), 0.80); + av_opt_set_double(p->avrctx, "cutoff", cutoff, 0); + + int global_normalize; + mp_read_option_raw(p->global, "audio-normalize-downmix", &m_option_type_flag, + &global_normalize); + int normalize = p->opts->normalize; + if (normalize < 0) + normalize = global_normalize; +#if HAVE_LIBSWRESAMPLE + av_opt_set_double(p->avrctx, "rematrix_maxval", normalize ? 1 : 1000, 0); +#else + av_opt_set_int(p->avrctx, "normalize_mix_level", !!normalize, 0); +#endif + + if (mp_set_avopts(p->log, p->avrctx, p->opts->avopts) < 0) + goto error; + + struct mp_chmap map_in = p->in_channels; + struct mp_chmap map_out = p->out_channels; + + // Try not to do any remixing if at least one is "unknown". Some corner + // cases also benefit from disabling all channel handling logic if the + // src/dst layouts are the same (like fl-fr-na -> fl-fr-na). + if (mp_chmap_is_unknown(&map_in) || mp_chmap_is_unknown(&map_out) || + mp_chmap_equals(&map_in, &map_out)) + { + mp_chmap_set_unknown(&map_in, map_in.num); + mp_chmap_set_unknown(&map_out, map_out.num); + } + + // unchecked: don't take any channel reordering into account + uint64_t in_ch_layout = mp_chmap_to_lavc_unchecked(&map_in); + uint64_t out_ch_layout = mp_chmap_to_lavc_unchecked(&map_out); + + struct mp_chmap in_lavc, out_lavc; + mp_chmap_from_lavc(&in_lavc, in_ch_layout); + mp_chmap_from_lavc(&out_lavc, out_ch_layout); + + if (verbose && !mp_chmap_equals(&in_lavc, &out_lavc)) { + MP_VERBOSE(p, "Remix: %s -> %s\n", mp_chmap_to_str(&in_lavc), + mp_chmap_to_str(&out_lavc)); + } + + if (in_lavc.num != map_in.num) { + // For handling NA channels, we would have to add a planarization step. + MP_FATAL(p, "Unsupported input channel layout %s.\n", + mp_chmap_to_str(&map_in)); + goto error; + } + + mp_chmap_get_reorder(p->reorder_in, &map_in, &in_lavc); + transpose_order(p->reorder_in, map_in.num); + + if (mp_chmap_equals(&out_lavc, &map_out)) { + // No intermediate step required - output new format directly. + out_samplefmtp = out_samplefmt; + } else { + // Verify that we really just reorder and/or insert NA channels. + struct mp_chmap withna = out_lavc; + mp_chmap_fill_na(&withna, map_out.num); + if (withna.num != map_out.num) + goto error; + } + mp_chmap_get_reorder(p->reorder_out, &out_lavc, &map_out); + + p->pre_out_fmt = mp_aframe_create(); + mp_aframe_set_rate(p->pre_out_fmt, p->out_rate); + mp_aframe_set_chmap(p->pre_out_fmt, &p->out_channels); + mp_aframe_set_format(p->pre_out_fmt, p->out_format); + + p->avrctx_fmt = mp_aframe_create(); + mp_aframe_config_copy(p->avrctx_fmt, p->pre_out_fmt); + mp_aframe_set_chmap(p->avrctx_fmt, &out_lavc); + mp_aframe_set_format(p->avrctx_fmt, af_from_avformat(out_samplefmtp)); + + // If there are NA channels, the final output will have more channels than + // the avrctx output. Also, avrctx will output planar (out_samplefmtp was + // not overwritten). Allocate the output frame with more channels, so the + // NA channels can be trivially added. + p->pool_fmt = mp_aframe_create(); + mp_aframe_config_copy(p->pool_fmt, p->avrctx_fmt); + if (map_out.num > out_lavc.num) + mp_aframe_set_chmap(p->pool_fmt, &map_out); + + out_ch_layout = fudge_layout_conversion(p, in_ch_layout, out_ch_layout); + + // Real conversion; output is input to avrctx_out. + av_opt_set_int(p->avrctx, "in_channel_layout", in_ch_layout, 0); + av_opt_set_int(p->avrctx, "out_channel_layout", out_ch_layout, 0); + av_opt_set_int(p->avrctx, "in_sample_rate", p->in_rate, 0); + av_opt_set_int(p->avrctx, "out_sample_rate", p->out_rate, 0); + av_opt_set_int(p->avrctx, "in_sample_fmt", in_samplefmt, 0); + av_opt_set_int(p->avrctx, "out_sample_fmt", out_samplefmtp, 0); + + // Just needs the correct number of channels for deplanarization. + struct mp_chmap fake_chmap; + mp_chmap_set_unknown(&fake_chmap, map_out.num); + uint64_t fake_out_ch_layout = mp_chmap_to_lavc_unchecked(&fake_chmap); + if (!fake_out_ch_layout) + goto error; + av_opt_set_int(p->avrctx_out, "in_channel_layout", fake_out_ch_layout, 0); + av_opt_set_int(p->avrctx_out, "out_channel_layout", fake_out_ch_layout, 0); + + av_opt_set_int(p->avrctx_out, "in_sample_fmt", out_samplefmtp, 0); + av_opt_set_int(p->avrctx_out, "out_sample_fmt", out_samplefmt, 0); + av_opt_set_int(p->avrctx_out, "in_sample_rate", p->out_rate, 0); + av_opt_set_int(p->avrctx_out, "out_sample_rate", p->out_rate, 0); + + // API has weird requirements, quoting avresample.h: + // * This function can only be called when the allocated context is not open. + // * Also, the input channel layout must have already been set. + avresample_set_channel_mapping(p->avrctx, p->reorder_in); + + p->is_resampling = false; + + if (avresample_open(p->avrctx) < 0 || avresample_open(p->avrctx_out) < 0) { + MP_ERR(p, "Cannot open Libavresample Context. \n"); + goto error; + } + return true; + +error: + close_lavrr(p); + return false; +} + +bool mp_aconverter_reconfig(struct mp_aconverter *p, + int in_rate, int in_format, struct mp_chmap in_channels, + int out_rate, int out_format, struct mp_chmap out_channels) +{ + close_lavrr(p); + + TA_FREEP(&p->input); + TA_FREEP(&p->output); + p->input_eof = p->output_eof = false; + + p->playback_speed = 1.0; + + p->in_rate_user = in_rate; + p->in_format = in_format; + p->in_channels = in_channels; + p->out_rate = out_rate; + p->out_format = out_format; + p->out_channels = out_channels; + + return configure_lavrr(p, true); +} + +void mp_aconverter_flush(struct mp_aconverter *p) +{ + if (!p->avrctx) + return; +#if HAVE_LIBSWRESAMPLE + swr_close(p->avrctx); + if (swr_init(p->avrctx) < 0) + close_lavrr(p); +#else + while (avresample_read(p->avrctx, NULL, 1000) > 0) {} +#endif +} + +void mp_aconverter_set_speed(struct mp_aconverter *p, double speed) +{ + p->playback_speed = speed; +} + +static void extra_output_conversion(struct mp_aframe *mpa) +{ + int format = af_fmt_from_planar(mp_aframe_get_format(mpa)); + int num_planes = mp_aframe_get_planes(mpa); + uint8_t **planes = mp_aframe_get_data_rw(mpa); + if (!planes) + return; + for (int p = 0; p < num_planes; p++) { + void *ptr = planes[p]; + int total = mp_aframe_get_total_plane_samples(mpa); + if (format == AF_FORMAT_FLOAT) { + for (int s = 0; s < total; s++) + ((float *)ptr)[s] = av_clipf(((float *)ptr)[s], -1.0f, 1.0f); + } else if (format == AF_FORMAT_DOUBLE) { + for (int s = 0; s < total; s++) + ((double *)ptr)[s] = MPCLAMP(((double *)ptr)[s], -1.0, 1.0); + } + } +} + +// This relies on the tricky way mpa was allocated. +static bool reorder_planes(struct mp_aframe *mpa, int *reorder, + struct mp_chmap *newmap) +{ + if (!mp_aframe_set_chmap(mpa, newmap)) + return false; + + int num_planes = newmap->num; + uint8_t **planes = mp_aframe_get_data_rw(mpa); + uint8_t *old_planes[MP_NUM_CHANNELS]; + assert(num_planes <= MP_NUM_CHANNELS); + for (int n = 0; n < num_planes; n++) + old_planes[n] = planes[n]; + + int next_na = 0; + for (int n = 0; n < num_planes; n++) + next_na += newmap->speaker[n] == MP_SPEAKER_ID_NA; + + for (int n = 0; n < num_planes; n++) { + int src = reorder[n]; + assert(src >= -1 && src < num_planes); + if (src >= 0) { + planes[n] = old_planes[src]; + } else { + assert(next_na < num_planes); + planes[n] = old_planes[next_na++]; + // The NA planes were never written by avrctx, so clear them. + af_fill_silence(planes[n], + mp_aframe_get_sstride(mpa) * mp_aframe_get_size(mpa), + mp_aframe_get_format(mpa)); + } + } + + return true; +} + +static int resample_frame(struct AVAudioResampleContext *r, + struct mp_aframe *out, struct mp_aframe *in) +{ + // Be aware that the channel layout and count can be different for in and + // out frames. In some situations the caller will fix up the frames before + // or after conversion. The sample rates can also be different. + AVFrame *av_i = in ? mp_aframe_get_raw_avframe(in) : NULL; + AVFrame *av_o = out ? mp_aframe_get_raw_avframe(out) : NULL; + return avresample_convert(r, + av_o ? av_o->extended_data : NULL, + av_o ? av_o->linesize[0] : 0, + av_o ? av_o->nb_samples : 0, + av_i ? av_i->extended_data : NULL, + av_i ? av_i->linesize[0] : 0, + av_i ? av_i->nb_samples : 0); +} + +static void filter_resample(struct mp_aconverter *p, struct mp_aframe *in) +{ + struct mp_aframe *out = NULL; + + if (!p->avrctx) + goto error; + + int samples = get_out_samples(p, in ? mp_aframe_get_size(in) : 0); + out = mp_aframe_create(); + mp_aframe_config_copy(out, p->pool_fmt); + if (mp_aframe_pool_allocate(p->out_pool, out, samples) < 0) + goto error; + + int out_samples = 0; + if (samples) { + out_samples = resample_frame(p->avrctx, out, in); + if (out_samples < 0 || out_samples > samples) + goto error; + mp_aframe_set_size(out, out_samples); + } + + struct mp_chmap out_chmap; + if (!mp_aframe_get_chmap(p->pool_fmt, &out_chmap)) + goto error; + if (!reorder_planes(out, p->reorder_out, &out_chmap)) + goto error; + + if (!mp_aframe_config_equals(out, p->pre_out_fmt)) { + struct mp_aframe *new = mp_aframe_create(); + mp_aframe_config_copy(new, p->pre_out_fmt); + if (mp_aframe_pool_allocate(p->reorder_buffer, new, out_samples) < 0) { + talloc_free(new); + goto error; + } + int got = 0; + if (out_samples) + got = resample_frame(p->avrctx_out, new, out); + talloc_free(out); + out = new; + if (got != out_samples) + goto error; + } + + extra_output_conversion(out); + + if (in) + mp_aframe_copy_attributes(out, in); + + if (out_samples) { + p->output = out; + } else { + talloc_free(out); + } + p->output_eof = !in; // we've read everything + + return; +error: + talloc_free(out); + MP_ERR(p, "Error on resampling.\n"); +} + +static void filter(struct mp_aconverter *p) +{ + if (p->output || p->output_eof || !(p->input || p->input_eof)) + return; + + int new_rate = rate_from_speed(p->in_rate_user, p->playback_speed); + + if (p->passthrough_mode && new_rate != p->in_rate) + configure_lavrr(p, false); + + if (p->passthrough_mode) { + p->output = p->input; + p->input = NULL; + p->output_eof = p->input_eof; + p->input_eof = false; + return; + } + + if (p->avrctx && !(!p->is_resampling && new_rate == p->in_rate)) { + AVRational r = av_d2q(p->playback_speed * p->in_rate_user / p->in_rate, + INT_MAX / 2); + // Essentially, swr/avresample_set_compensation() does 2 things: + // - adjust output sample rate by sample_delta/compensation_distance + // - reset the adjustment after compensation_distance output samples + // Increase the compensation_distance to avoid undesired reset + // semantics - we want to keep the ratio for the whole frame we're + // feeding it, until the next filter() call. + int mult = INT_MAX / 2 / MPMAX(MPMAX(abs(r.num), abs(r.den)), 1); + r = (AVRational){ r.num * mult, r.den * mult }; + if (avresample_set_compensation(p->avrctx, r.den - r.num, r.den) >= 0) { + new_rate = p->in_rate; + p->is_resampling = true; + } + } + + bool need_reinit = fabs(new_rate / (double)p->in_rate - 1) > 0.01; + if (need_reinit && new_rate != p->in_rate) { + // Before reconfiguring, drain the audio that is still buffered + // in the resampler. + filter_resample(p, NULL); + // Reinitialize resampler. + configure_lavrr(p, false); + p->output_eof = false; + if (p->output) + return; // need to read output before continuing filtering + } + + filter_resample(p, p->input); + TA_FREEP(&p->input); + p->input_eof = false; +} + +// Queue input. If true, ownership of in passes to mp_aconverted and the input +// was accepted. Otherwise, return false and reject in. +// in==NULL means trigger EOF. +bool mp_aconverter_write_input(struct mp_aconverter *p, struct mp_aframe *in) +{ + if (p->input || p->input_eof) + return false; + + p->input = in; + p->input_eof = !in; + return true; +} + +// Return output frame, or NULL if nothing available. +// *eof is set to true if NULL is returned, and it was due to EOF. +struct mp_aframe *mp_aconverter_read_output(struct mp_aconverter *p, bool *eof) +{ + *eof = false; + + filter(p); + + if (p->output) { + struct mp_aframe *out = p->output; + p->output = NULL; + return out; + } + + *eof = p->output_eof; + p->output_eof = false; + return NULL; +} + +double mp_aconverter_get_latency(struct mp_aconverter *p) +{ + double delay = get_delay(p); + + if (p->input) + delay += mp_aframe_duration(p->input); + + // In theory this is influenced by playback speed, but other parts of the + // player get it wrong anyway. + if (p->output) + delay += mp_aframe_duration(p->output); + + return delay; +} + +static void destroy_aconverter(void *ptr) +{ + struct mp_aconverter *p = ptr; + + close_lavrr(p); + + talloc_free(p->input); + talloc_free(p->output); +} + +// If opts is not NULL, the pointer must be valid for the lifetime of the +// mp_aconverter. +struct mp_aconverter *mp_aconverter_create(struct mpv_global *global, + struct mp_log *log, + const struct mp_resample_opts *opts) +{ + struct mp_aconverter *p = talloc_zero(NULL, struct mp_aconverter); + p->log = log; + p->global = global; + + static const struct mp_resample_opts defs = MP_RESAMPLE_OPTS_DEF; + + p->opts = opts ? opts : &defs; + + p->reorder_buffer = mp_aframe_pool_create(p); + p->out_pool = mp_aframe_pool_create(p); + + talloc_set_destructor(p, destroy_aconverter); + + return p; +} diff --git a/audio/aconverter.h b/audio/aconverter.h new file mode 100644 index 0000000000..57c5524c2f --- /dev/null +++ b/audio/aconverter.h @@ -0,0 +1,39 @@ +#pragma once + +#include + +#include "chmap.h" + +struct mp_aconverter; +struct mp_aframe; +struct mpv_global; +struct mp_log; + +struct mp_resample_opts { + int filter_size; + int phase_shift; + int linear; + double cutoff; + int normalize; + int allow_passthrough; + char **avopts; +}; + +#define MP_RESAMPLE_OPTS_DEF { \ + .filter_size = 16, \ + .cutoff = 0.0, \ + .phase_shift = 10, \ + .normalize = -1, \ + } + +struct mp_aconverter *mp_aconverter_create(struct mpv_global *global, + struct mp_log *log, + const struct mp_resample_opts *opts); +bool mp_aconverter_reconfig(struct mp_aconverter *p, + int in_rate, int in_format, struct mp_chmap in_channels, + int out_rate, int out_format, struct mp_chmap out_channels); +void mp_aconverter_flush(struct mp_aconverter *p); +void mp_aconverter_set_speed(struct mp_aconverter *p, double speed); +bool mp_aconverter_write_input(struct mp_aconverter *p, struct mp_aframe *in); +struct mp_aframe *mp_aconverter_read_output(struct mp_aconverter *p, bool *eof); +double mp_aconverter_get_latency(struct mp_aconverter *p); diff --git a/audio/aframe.c b/audio/aframe.c index cf785f6657..4168f92124 100644 --- a/audio/aframe.c +++ b/audio/aframe.c @@ -166,10 +166,9 @@ void mp_aframe_config_copy(struct mp_aframe *dst, struct mp_aframe *src) dst->chmap = src->chmap; dst->format = src->format; - dst->pts = src->pts; - if (av_frame_copy_props(dst->av_frame, src->av_frame) < 0) - abort(); + mp_aframe_copy_attributes(dst, src); + dst->av_frame->format = src->av_frame->format; dst->av_frame->channel_layout = src->av_frame->channel_layout; #if LIBAVUTIL_VERSION_MICRO >= 100 @@ -178,6 +177,16 @@ void mp_aframe_config_copy(struct mp_aframe *dst, struct mp_aframe *src) #endif } +// Copy "soft" attributes from src to dst, excluding things which affect +// frame allocation and organization. +void mp_aframe_copy_attributes(struct mp_aframe *dst, struct mp_aframe *src) +{ + dst->pts = src->pts; + + if (av_frame_copy_props(dst->av_frame, src->av_frame) < 0) + abort(); +} + // Return whether a and b use the same physical audio format. Extra metadata // such as PTS, per-frame signalling, and AVFrame side data is not compared. bool mp_aframe_config_equals(struct mp_aframe *a, struct mp_aframe *b) @@ -317,6 +326,14 @@ size_t mp_aframe_get_sstride(struct mp_aframe *frame) (af_fmt_is_planar(format) ? 1 : mp_aframe_get_channels(frame)); } +// Return total number of samples on each plane. +int mp_aframe_get_total_plane_samples(struct mp_aframe *frame) +{ + return frame->av_frame->nb_samples * + (af_fmt_is_planar(mp_aframe_get_format(frame)) + ? 1 : mp_aframe_get_channels(frame)); +} + // Set data to the audio after the given number of samples (i.e. slice it). void mp_aframe_skip_samples(struct mp_aframe *f, int samples) { diff --git a/audio/aframe.h b/audio/aframe.h index 5661178419..9ce19cdee1 100644 --- a/audio/aframe.h +++ b/audio/aframe.h @@ -23,6 +23,8 @@ void mp_aframe_config_copy(struct mp_aframe *dst, struct mp_aframe *src); bool mp_aframe_config_equals(struct mp_aframe *a, struct mp_aframe *b); bool mp_aframe_config_is_valid(struct mp_aframe *frame); +void mp_aframe_copy_attributes(struct mp_aframe *dst, struct mp_aframe *src); + uint8_t **mp_aframe_get_data_ro(struct mp_aframe *frame); uint8_t **mp_aframe_get_data_rw(struct mp_aframe *frame); @@ -40,6 +42,7 @@ bool mp_aframe_set_size(struct mp_aframe *frame, int samples); void mp_aframe_set_pts(struct mp_aframe *frame, double pts); int mp_aframe_get_planes(struct mp_aframe *frame); +int mp_aframe_get_total_plane_samples(struct mp_aframe *frame); size_t mp_aframe_get_sstride(struct mp_aframe *frame); void mp_aframe_skip_samples(struct mp_aframe *f, int samples); diff --git a/audio/audio.c b/audio/audio.c index 008aa1883b..b636c66620 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -406,6 +406,9 @@ fail: struct mp_audio *mp_audio_from_aframe(struct mp_aframe *aframe) { + if (!aframe) + return NULL; + struct AVFrame *av = mp_aframe_get_raw_avframe(aframe); struct mp_audio *res = mp_audio_from_avframe(av); if (!res) @@ -428,6 +431,34 @@ void mp_audio_config_from_aframe(struct mp_audio *dst, struct mp_aframe *src) dst->rate = mp_aframe_get_rate(src); } +struct mp_aframe *mp_audio_to_aframe(struct mp_audio *mpa) +{ + if (!mpa) + return NULL; + + struct mp_aframe *aframe = mp_aframe_create(); + struct AVFrame *av = mp_aframe_get_raw_avframe(aframe); + mp_aframe_set_format(aframe, mpa->format); + mp_aframe_set_chmap(aframe, &mpa->channels); + mp_aframe_set_rate(aframe, mpa->rate); + + // bullshit it into ffmpeg-compatible parameters + struct mp_audio mpb = *mpa; + struct mp_chmap chmap; + mp_chmap_set_unknown(&chmap, mpb.channels.num); + mp_audio_set_channels(&mpb, &chmap); + if (af_fmt_is_spdif(mpb.format)) + mp_audio_set_format(&mpb, AF_FORMAT_S16); + + // put the reference into av, which magically puts it into aframe + // aframe keeps its parameters, so the bullshit doesn't matter + if (mp_audio_to_avframe(&mpb, av) < 0) { + talloc_free(aframe); + return NULL; + } + return aframe; +} + int mp_audio_to_avframe(struct mp_audio *frame, struct AVFrame *avframe) { av_frame_unref(avframe); diff --git a/audio/audio.h b/audio/audio.h index a8370a0eb7..a4d9134a20 100644 --- a/audio/audio.h +++ b/audio/audio.h @@ -86,6 +86,7 @@ int mp_audio_to_avframe(struct mp_audio *frame, struct AVFrame *avframe); struct mp_aframe; struct mp_audio *mp_audio_from_aframe(struct mp_aframe *aframe); void mp_audio_config_from_aframe(struct mp_audio *dst, struct mp_aframe *src); +struct mp_aframe *mp_audio_to_aframe(struct mp_audio *mpa); struct mp_audio_pool; struct mp_audio_pool *mp_audio_pool_create(void *ta_parent); diff --git a/audio/filter/af.c b/audio/filter/af.c index 0df0f28bb6..a76945feea 100644 --- a/audio/filter/af.c +++ b/audio/filter/af.c @@ -175,6 +175,7 @@ static struct af_instance *af_create(struct af_stream *s, char *name, .data = talloc_zero(af, struct mp_audio), .log = mp_log_new(af, s->log, name), .opts = s->opts, + .global = s->global, .replaygain_data = s->replaygain_data, .out_pool = mp_audio_pool_create(af), }; @@ -546,6 +547,7 @@ struct af_stream *af_new(struct mpv_global *global) s->first->next = s->last; s->last->prev = s->first; s->opts = global->opts; + s->global = global; return s; } diff --git a/audio/filter/af.h b/audio/filter/af.h index 553fc03e32..bbf7b53bad 100644 --- a/audio/filter/af.h +++ b/audio/filter/af.h @@ -55,6 +55,7 @@ struct af_instance { char *full_name; struct mp_log *log; struct MPOpts *opts; + struct mpv_global *global; struct replaygain_data *replaygain_data; int (*control)(struct af_instance *af, int cmd, void *arg); void (*uninit)(struct af_instance *af); @@ -98,6 +99,7 @@ struct af_stream { struct mp_log *log; struct MPOpts *opts; + struct mpv_global *global; struct replaygain_data *replaygain_data; }; diff --git a/audio/filter/af_lavrresample.c b/audio/filter/af_lavrresample.c index c18f8cc28a..55eb6b0f20 100644 --- a/audio/filter/af_lavrresample.c +++ b/audio/filter/af_lavrresample.c @@ -27,326 +27,24 @@ #include #include -#include -#include -#include -#include -#include - #include "common/common.h" #include "config.h" -#define HAVE_LIBSWRESAMPLE HAVE_IS_FFMPEG -#define HAVE_LIBAVRESAMPLE HAVE_IS_LIBAV - -#if HAVE_LIBAVRESAMPLE -#include -#elif HAVE_LIBSWRESAMPLE -#include -#define AVAudioResampleContext SwrContext -#define avresample_alloc_context swr_alloc -#define avresample_open swr_init -#define avresample_close(x) do { } while(0) -#define avresample_free swr_free -#define avresample_available(x) 0 -#define avresample_convert(ctx, out, out_planesize, out_samples, in, in_planesize, in_samples) \ - swr_convert(ctx, out, out_samples, (const uint8_t**)(in), in_samples) -#define avresample_set_channel_mapping swr_set_channel_mapping -#define avresample_set_compensation swr_set_compensation -#else -#error "config.h broken or no resampler found" -#endif - #include "common/av_common.h" #include "common/msg.h" #include "options/m_option.h" #include "audio/filter/af.h" #include "audio/fmt-conversion.h" #include "osdep/endian.h" - -struct af_resample_opts { - int filter_size; - int phase_shift; - int linear; - double cutoff; - int normalize; -}; +#include "audio/aconverter.h" struct af_resample { int allow_detach; - char **avopts; double playback_speed; - bool is_resampling; - struct AVAudioResampleContext *avrctx; - struct mp_audio avrctx_fmt; // output format of avrctx - struct mp_audio pool_fmt; // format used to allocate frames for avrctx output - struct mp_audio pre_out_fmt; // format before final conversion (S24) - struct AVAudioResampleContext *avrctx_out; // for output channel reordering - struct af_resample_opts opts; // opts requested by the user - // At least libswresample keeps a pointer around for this: - int reorder_in[MP_NUM_CHANNELS]; - int reorder_out[MP_NUM_CHANNELS]; - struct mp_audio_pool *reorder_buffer; - - int in_rate_af; // filter input sample rate - int in_rate; // actual rate (used by lavr), adjusted for playback speed - int in_format; - struct mp_chmap in_channels; - int out_rate; - int out_format; - struct mp_chmap out_channels; -}; - -#if HAVE_LIBAVRESAMPLE -static double get_delay(struct af_resample *s) -{ - return avresample_get_delay(s->avrctx) / (double)s->in_rate + - avresample_available(s->avrctx) / (double)s->out_rate; -} -static int get_out_samples(struct af_resample *s, int in_samples) -{ - return avresample_get_out_samples(s->avrctx, in_samples); -} -#else -static double get_delay(struct af_resample *s) -{ - int64_t base = s->in_rate * (int64_t)s->out_rate; - return swr_get_delay(s->avrctx, base) / (double)base; -} -static int get_out_samples(struct af_resample *s, int in_samples) -{ - return swr_get_out_samples(s->avrctx, in_samples); -} -#endif - -static void close_lavrr(struct af_instance *af) -{ - struct af_resample *s = af->priv; - - if (s->avrctx) - avresample_close(s->avrctx); - avresample_free(&s->avrctx); - if (s->avrctx_out) - avresample_close(s->avrctx_out); - avresample_free(&s->avrctx_out); -} - -static int resample_frame(struct AVAudioResampleContext *r, - struct mp_audio *out, struct mp_audio *in) -{ - return avresample_convert(r, - out ? (uint8_t **)out->planes : NULL, - out ? mp_audio_get_allocated_size(out) : 0, - out ? out->samples : 0, - in ? (uint8_t **)in->planes : NULL, - in ? mp_audio_get_allocated_size(in) : 0, - in ? in->samples : 0); -} - -static double af_resample_default_cutoff(int filter_size) -{ - return FFMAX(1.0 - 6.5 / (filter_size + 8), 0.80); -} - -static int rate_from_speed(int rate, double speed) -{ - return lrint(rate * speed); -} - -static struct mp_chmap fudge_pairs[][2] = { - {MP_CHMAP2(BL, BR), MP_CHMAP2(SL, SR)}, - {MP_CHMAP2(SL, SR), MP_CHMAP2(BL, BR)}, - {MP_CHMAP2(SDL, SDR), MP_CHMAP2(SL, SR)}, - {MP_CHMAP2(SL, SR), MP_CHMAP2(SDL, SDR)}, + struct mp_resample_opts opts; + struct mp_aconverter *converter; }; -// Modify out_layout and return the new value. The intention is reducing the -// loss libswresample's rematrixing will cause by exchanging similar, but -// strictly speaking incompatible channel pairs. For example, 7.1 should be -// changed to 7.1(wide) without dropping the SL/SR channels. (We still leave -// it to libswresample to create the remix matrix.) -static uint64_t fudge_layout_conversion(struct af_instance *af, - uint64_t in, uint64_t out) -{ - for (int n = 0; n < MP_ARRAY_SIZE(fudge_pairs); n++) { - uint64_t a = mp_chmap_to_lavc(&fudge_pairs[n][0]); - uint64_t b = mp_chmap_to_lavc(&fudge_pairs[n][1]); - if ((in & a) == a && (in & b) == 0 && - (out & a) == 0 && (out & b) == b) - { - out = (out & ~b) | a; - - MP_VERBOSE(af, "Fudge: %s -> %s\n", - mp_chmap_to_str(&fudge_pairs[n][0]), - mp_chmap_to_str(&fudge_pairs[n][1])); - } - } - return out; -} - -// mp_chmap_get_reorder() performs: -// to->speaker[n] = from->speaker[src[n]] -// but libavresample does: -// to->speaker[dst[n]] = from->speaker[n] -static void transpose_order(int *map, int num) -{ - int nmap[MP_NUM_CHANNELS] = {0}; - for (int n = 0; n < num; n++) { - for (int i = 0; i < num; i++) { - if (map[n] == i) - nmap[i] = n; - } - } - memcpy(map, nmap, sizeof(nmap)); -} - -static int configure_lavrr(struct af_instance *af, struct mp_audio *in, - struct mp_audio *out, bool verbose) -{ - struct af_resample *s = af->priv; - - close_lavrr(af); - - s->avrctx = avresample_alloc_context(); - s->avrctx_out = avresample_alloc_context(); - if (!s->avrctx || !s->avrctx_out) - goto error; - - enum AVSampleFormat in_samplefmt = af_to_avformat(in->format); - enum AVSampleFormat out_samplefmt = af_to_avformat(out->format); - enum AVSampleFormat out_samplefmtp = av_get_planar_sample_fmt(out_samplefmt); - - if (in_samplefmt == AV_SAMPLE_FMT_NONE || - out_samplefmt == AV_SAMPLE_FMT_NONE || - out_samplefmtp == AV_SAMPLE_FMT_NONE) - goto error; - - s->out_rate = out->rate; - s->in_rate_af = in->rate; - s->in_rate = rate_from_speed(in->rate, s->playback_speed); - s->out_format = out->format; - s->in_format = in->format; - s->out_channels= out->channels; - s->in_channels = in->channels; - - av_opt_set_int(s->avrctx, "filter_size", s->opts.filter_size, 0); - av_opt_set_int(s->avrctx, "phase_shift", s->opts.phase_shift, 0); - av_opt_set_int(s->avrctx, "linear_interp", s->opts.linear, 0); - - av_opt_set_double(s->avrctx, "cutoff", s->opts.cutoff, 0); - - int normalize = s->opts.normalize; - if (normalize < 0) - normalize = af->opts->audio_normalize; -#if HAVE_LIBSWRESAMPLE - av_opt_set_double(s->avrctx, "rematrix_maxval", normalize ? 1 : 1000, 0); -#else - av_opt_set_int(s->avrctx, "normalize_mix_level", !!normalize, 0); -#endif - - if (mp_set_avopts(af->log, s->avrctx, s->avopts) < 0) - goto error; - - struct mp_chmap map_in = in->channels; - struct mp_chmap map_out = out->channels; - - // Try not to do any remixing if at least one is "unknown". - if (mp_chmap_is_unknown(&map_in) || mp_chmap_is_unknown(&map_out)) { - mp_chmap_set_unknown(&map_in, map_in.num); - mp_chmap_set_unknown(&map_out, map_out.num); - } - - // unchecked: don't take any channel reordering into account - uint64_t in_ch_layout = mp_chmap_to_lavc_unchecked(&map_in); - uint64_t out_ch_layout = mp_chmap_to_lavc_unchecked(&map_out); - - struct mp_chmap in_lavc, out_lavc; - mp_chmap_from_lavc(&in_lavc, in_ch_layout); - mp_chmap_from_lavc(&out_lavc, out_ch_layout); - - if (verbose && !mp_chmap_equals(&in_lavc, &out_lavc)) { - MP_VERBOSE(af, "Remix: %s -> %s\n", mp_chmap_to_str(&in_lavc), - mp_chmap_to_str(&out_lavc)); - } - - if (in_lavc.num != map_in.num) { - // For handling NA channels, we would have to add a planarization step. - MP_FATAL(af, "Unsupported channel remapping.\n"); - goto error; - } - - mp_chmap_get_reorder(s->reorder_in, &map_in, &in_lavc); - transpose_order(s->reorder_in, map_in.num); - - if (mp_chmap_equals(&out_lavc, &map_out)) { - // No intermediate step required - output new format directly. - out_samplefmtp = out_samplefmt; - } else { - // Verify that we really just reorder and/or insert NA channels. - struct mp_chmap withna = out_lavc; - mp_chmap_fill_na(&withna, map_out.num); - if (withna.num != map_out.num) - goto error; - } - mp_chmap_get_reorder(s->reorder_out, &out_lavc, &map_out); - - s->avrctx_fmt = *out; - mp_audio_set_channels(&s->avrctx_fmt, &out_lavc); - mp_audio_set_format(&s->avrctx_fmt, af_from_avformat(out_samplefmtp)); - - s->pre_out_fmt = *out; - - // If there are NA channels, the final output will have more channels than - // the avrctx output. Also, avrctx will output planar (out_samplefmtp was - // not overwritten). Allocate the output frame with more channels, so the - // NA channels can be trivially added. - s->pool_fmt = s->avrctx_fmt; - if (map_out.num > out_lavc.num) - mp_audio_set_channels(&s->pool_fmt, &map_out); - - out_ch_layout = fudge_layout_conversion(af, in_ch_layout, out_ch_layout); - - // Real conversion; output is input to avrctx_out. - av_opt_set_int(s->avrctx, "in_channel_layout", in_ch_layout, 0); - av_opt_set_int(s->avrctx, "out_channel_layout", out_ch_layout, 0); - av_opt_set_int(s->avrctx, "in_sample_rate", s->in_rate, 0); - av_opt_set_int(s->avrctx, "out_sample_rate", s->out_rate, 0); - av_opt_set_int(s->avrctx, "in_sample_fmt", in_samplefmt, 0); - av_opt_set_int(s->avrctx, "out_sample_fmt", out_samplefmtp, 0); - - // Just needs the correct number of channels for deplanarization. - struct mp_chmap fake_chmap; - mp_chmap_set_unknown(&fake_chmap, map_out.num); - uint64_t fake_out_ch_layout = mp_chmap_to_lavc_unchecked(&fake_chmap); - if (!fake_out_ch_layout) - goto error; - av_opt_set_int(s->avrctx_out, "in_channel_layout", fake_out_ch_layout, 0); - av_opt_set_int(s->avrctx_out, "out_channel_layout", fake_out_ch_layout, 0); - - av_opt_set_int(s->avrctx_out, "in_sample_fmt", out_samplefmtp, 0); - av_opt_set_int(s->avrctx_out, "out_sample_fmt", out_samplefmt, 0); - av_opt_set_int(s->avrctx_out, "in_sample_rate", s->out_rate, 0); - av_opt_set_int(s->avrctx_out, "out_sample_rate", s->out_rate, 0); - - // API has weird requirements, quoting avresample.h: - // * This function can only be called when the allocated context is not open. - // * Also, the input channel layout must have already been set. - avresample_set_channel_mapping(s->avrctx, s->reorder_in); - - s->is_resampling = false; - - if (avresample_open(s->avrctx) < 0 || avresample_open(s->avrctx_out) < 0) { - MP_ERR(af, "Cannot open Libavresample Context. \n"); - goto error; - } - return AF_OK; - -error: - close_lavrr(af); - return AF_ERROR; -} - - static int control(struct af_instance *af, int cmd, void *arg) { struct af_resample *s = af->priv; @@ -378,8 +76,12 @@ static int control(struct af_instance *af, int cmd, void *arg) mp_chmap_equals(&in->channels, &orig_in.channels)) ? AF_OK : AF_FALSE; - if (r == AF_OK) - r = configure_lavrr(af, in, out, true); + if (r == AF_OK) { + if (!mp_aconverter_reconfig(s->converter, + in->rate, in->format, in->channels, + out->rate, out->format, out->channels)) + r = AF_ERROR; + } return r; } case AF_CONTROL_SET_PLAYBACK_SPEED_RESAMPLE: { @@ -387,17 +89,7 @@ static int control(struct af_instance *af, int cmd, void *arg) return AF_OK; } case AF_CONTROL_RESET: - if (s->avrctx) { -#if HAVE_LIBSWRESAMPLE - swr_close(s->avrctx); - if (swr_init(s->avrctx) < 0) { - close_lavrr(af); - return AF_ERROR; - } -#else - while (avresample_read(s->avrctx, NULL, 1000) > 0) {} -#endif - } + mp_aconverter_flush(s->converter); return AF_OK; } return AF_UNKNOWN; @@ -405,149 +97,40 @@ static int control(struct af_instance *af, int cmd, void *arg) static void uninit(struct af_instance *af) { - close_lavrr(af); -} - -// The LSB is always ignored. -#if BYTE_ORDER == BIG_ENDIAN -#define SHIFT24(x) ((3-(x))*8) -#else -#define SHIFT24(x) (((x)+1)*8) -#endif - -static void extra_output_conversion(struct af_instance *af, struct mp_audio *mpa) -{ - for (int p = 0; p < mpa->num_planes; p++) { - void *ptr = mpa->planes[p]; - int total = mpa->samples * mpa->spf; - if (af_fmt_from_planar(mpa->format) == AF_FORMAT_FLOAT) { - for (int s = 0; s < total; s++) - ((float *)ptr)[s] = av_clipf(((float *)ptr)[s], -1.0f, 1.0f); - } else if (af_fmt_from_planar(mpa->format) == AF_FORMAT_DOUBLE) { - for (int s = 0; s < total; s++) - ((double *)ptr)[s] = MPCLAMP(((double *)ptr)[s], -1.0, 1.0); - } - } -} - -// This relies on the tricky way mpa was allocated. -static void reorder_planes(struct mp_audio *mpa, int *reorder, - struct mp_chmap *newmap) -{ - struct mp_audio prev = *mpa; - mp_audio_set_channels(mpa, newmap); - - // The trailing planes were never written by avrctx, they're the NA channels. - int next_na = prev.num_planes; + struct af_resample *s = af->priv; - for (int n = 0; n < mpa->num_planes; n++) { - int src = reorder[n]; - assert(src >= -1 && src < prev.num_planes); - if (src >= 0) { - mpa->planes[n] = prev.planes[src]; - } else { - assert(next_na < mpa->num_planes); - mpa->planes[n] = prev.planes[next_na++]; - af_fill_silence(mpa->planes[n], mpa->sstride * mpa->samples, - mpa->format); - } - } + talloc_free(s->converter); } -static int filter_resample(struct af_instance *af, struct mp_audio *in) +static int filter(struct af_instance *af, struct mp_audio *in) { struct af_resample *s = af->priv; - struct mp_audio *out = NULL; - - if (!s->avrctx) - goto error; - int samples = get_out_samples(s, in ? in->samples : 0); - - struct mp_audio out_format = s->pool_fmt; - out = mp_audio_pool_get(af->out_pool, &out_format, samples); - if (!out) - goto error; - if (in) - mp_audio_copy_attributes(out, in); - - if (out->samples) { - out->samples = resample_frame(s->avrctx, out, in); - if (out->samples < 0) - goto error; - } - - struct mp_audio real_out = *out; - mp_audio_copy_config(out, &s->avrctx_fmt); - - if (out->samples && !mp_audio_config_equals(out, &s->pre_out_fmt)) { - assert(af_fmt_is_planar(out->format) && out->format == real_out.format); - reorder_planes(out, s->reorder_out, &s->pool_fmt.channels); - if (!mp_audio_config_equals(out, &s->pre_out_fmt)) { - struct mp_audio *new = mp_audio_pool_get(s->reorder_buffer, - &s->pre_out_fmt, - out->samples); - if (!new) - goto error; - mp_audio_copy_attributes(new, out); - int out_samples = resample_frame(s->avrctx_out, new, out); - talloc_free(out); - out = new; - if (out_samples != new->samples) - goto error; - } - } + mp_aconverter_set_speed(s->converter, s->playback_speed); - extra_output_conversion(af, out); + af->filter_out(af); + struct mp_aframe *aframe = mp_audio_to_aframe(in); + if (!aframe && in) + return -1; talloc_free(in); - if (out->samples) { - af_add_output_frame(af, out); - } else { - talloc_free(out); - } - - af->delay = get_delay(s); + bool ok = mp_aconverter_write_input(s->converter, aframe); + if (!ok) + talloc_free(aframe); - return 0; -error: - talloc_free(in); - talloc_free(out); - return -1; + return ok ? 0 : -1; } -static int filter(struct af_instance *af, struct mp_audio *in) +static int filter_out(struct af_instance *af) { struct af_resample *s = af->priv; - - int new_rate = rate_from_speed(s->in_rate_af, s->playback_speed); - if (s->avrctx && !(!s->is_resampling && new_rate == s->in_rate)) { - AVRational r = av_d2q(s->playback_speed * s->in_rate_af / s->in_rate, - INT_MAX / 2); - // Essentially, swr/avresample_set_compensation() does 2 things: - // - adjust output sample rate by sample_delta/compensation_distance - // - reset the adjustment after compensation_distance output samples - // Increase the compensation_distance to avoid undesired reset - // semantics - we want to keep the ratio for the whole frame we're - // feeding it, until the next filter() call. - int mult = INT_MAX / 2 / MPMAX(MPMAX(abs(r.num), abs(r.den)), 1); - r = (AVRational){ r.num * mult, r.den * mult }; - if (avresample_set_compensation(s->avrctx, r.den - r.num, r.den) >= 0) { - new_rate = s->in_rate; - s->is_resampling = true; - } - } - - bool need_reinit = fabs(new_rate / (double)s->in_rate - 1) > 0.01; - if (need_reinit && new_rate != s->in_rate) { - // Before reconfiguring, drain the audio that is still buffered - // in the resampler. - filter_resample(af, NULL); - // Reinitialize resampler. - configure_lavrr(af, &af->fmt_in, &af->fmt_out, false); - } - - return filter_resample(af, in); + bool eof; + struct mp_aframe *out = mp_aconverter_read_output(s->converter, &eof); + if (out) + af_add_output_frame(af, mp_audio_from_aframe(out)); + talloc_free(out); + af->delay = mp_aconverter_get_latency(s->converter); + return 0; } static int af_open(struct af_instance *af) @@ -557,11 +140,9 @@ static int af_open(struct af_instance *af) af->control = control; af->uninit = uninit; af->filter_frame = filter; + af->filter_out = filter_out; - if (s->opts.cutoff <= 0.0) - s->opts.cutoff = af_resample_default_cutoff(s->opts.filter_size); - - s->reorder_buffer = mp_audio_pool_create(s); + s->converter = mp_aconverter_create(af->global, af->log, &s->opts); return AF_OK; } @@ -574,12 +155,7 @@ const struct af_info af_info_lavrresample = { .open = af_open, .priv_size = sizeof(struct af_resample), .priv_defaults = &(const struct af_resample) { - .opts = { - .filter_size = 16, - .cutoff = 0.0, - .phase_shift = 10, - .normalize = -1, - }, + .opts = MP_RESAMPLE_OPTS_DEF, .playback_speed = 1.0, .allow_detach = 1, }, @@ -591,7 +167,7 @@ const struct af_info af_info_lavrresample = { OPT_FLAG("detach", allow_detach, 0), OPT_CHOICE("normalize", opts.normalize, 0, ({"no", 0}, {"yes", 1}, {"auto", -1})), - OPT_KEYVALUELIST("o", avopts, 0), + OPT_KEYVALUELIST("o", opts.avopts, 0), {0} }, }; -- cgit v1.2.3