From 771386e6e6e79697e2d839ef0f25a242946ba1e5 Mon Sep 17 00:00:00 2001 From: Andy Hung Date: Tue, 8 Apr 2014 18:44:38 -0700 Subject: Add and enable floating point option for audio resampler Can be tested with test-resample. Change-Id: I8339846d7c647444b6025d33cfa145d5d3658121 Signed-off-by: Andy Hung --- services/audioflinger/AudioResamplerDyn.cpp | 250 ++++++++++++++-------------- 1 file changed, 123 insertions(+), 127 deletions(-) (limited to 'services/audioflinger/AudioResamplerDyn.cpp') diff --git a/services/audioflinger/AudioResamplerDyn.cpp b/services/audioflinger/AudioResamplerDyn.cpp index 173a243..3abe8fd 100644 --- a/services/audioflinger/AudioResamplerDyn.cpp +++ b/services/audioflinger/AudioResamplerDyn.cpp @@ -39,9 +39,9 @@ namespace android { // generate a unique resample type compile-time constant (constexpr) -#define RESAMPLETYPE(CHANNELS, LOCKED, STRIDE, COEFTYPE) \ - ((((CHANNELS)-1)&1) | !!(LOCKED)<<1 | (COEFTYPE)<<2 \ - | ((STRIDE)==8 ? 1 : (STRIDE)==16 ? 2 : 0)<<3) +#define RESAMPLETYPE(CHANNELS, LOCKED, STRIDE) \ + ((((CHANNELS)-1)&1) | !!(LOCKED)<<1 \ + | ((STRIDE)==8 ? 1 : (STRIDE)==16 ? 2 : 0)<<2) /* * InBuffer is a type agnostic input buffer. @@ -59,42 +59,46 @@ namespace android { * r = extra space for implementing the ring buffer */ -template -AudioResamplerDyn::InBuffer::InBuffer() - : mState(NULL), mImpulse(NULL), mRingFull(NULL), mStateSize(0) { +template +AudioResamplerDyn::InBuffer::InBuffer() + : mState(NULL), mImpulse(NULL), mRingFull(NULL), mStateCount(0) +{ } -template -AudioResamplerDyn::InBuffer::~InBuffer() { +template +AudioResamplerDyn::InBuffer::~InBuffer() +{ init(); } -template -void AudioResamplerDyn::InBuffer::init() { +template +void AudioResamplerDyn::InBuffer::init() +{ free(mState); mState = NULL; mImpulse = NULL; mRingFull = NULL; - mStateSize = 0; + mStateCount = 0; } // resizes the state buffer to accommodate the appropriate filter length -template -void AudioResamplerDyn::InBuffer::resize(int CHANNELS, int halfNumCoefs) { +template +void AudioResamplerDyn::InBuffer::resize(int CHANNELS, int halfNumCoefs) +{ // calculate desired state size - int stateSize = halfNumCoefs * CHANNELS * 2 - * kStateSizeMultipleOfFilterLength; + int stateCount = halfNumCoefs * CHANNELS * 2 * kStateSizeMultipleOfFilterLength; // check if buffer needs resizing if (mState - && stateSize == mStateSize - && mRingFull-mState == mStateSize-halfNumCoefs*CHANNELS) { + && stateCount == mStateCount + && mRingFull-mState == mStateCount-halfNumCoefs*CHANNELS) { return; } // create new buffer - TI* state = (int16_t*)memalign(32, stateSize*sizeof(*state)); - memset(state, 0, stateSize*sizeof(*state)); + TI* state; + (void)posix_memalign(reinterpret_cast(&state), 32, stateCount*sizeof(*state)); + memset(state, 0, stateCount*sizeof(*state)); // attempt to preserve state if (mState) { @@ -106,8 +110,8 @@ void AudioResamplerDyn::InBuffer::resize(int CHANNELS, int halfNumCoefs) { dst += mState-srcLo; srcLo = mState; } - if (srcHi > mState + mStateSize) { - srcHi = mState + mStateSize; + if (srcHi > mState + mStateCount) { + srcHi = mState + mStateCount; } memcpy(dst, srcLo, (srcHi - srcLo) * sizeof(*srcLo)); free(mState); @@ -115,27 +119,29 @@ void AudioResamplerDyn::InBuffer::resize(int CHANNELS, int halfNumCoefs) { // set class member vars mState = state; - mStateSize = stateSize; - mImpulse = mState + halfNumCoefs*CHANNELS; // actually one sample greater than needed - mRingFull = mState + mStateSize - halfNumCoefs*CHANNELS; + mStateCount = stateCount; + mImpulse = state + halfNumCoefs*CHANNELS; // actually one sample greater than needed + mRingFull = state + mStateCount - halfNumCoefs*CHANNELS; } // copy in the input data into the head (impulse+halfNumCoefs) of the buffer. -template +template template -void AudioResamplerDyn::InBuffer::readAgain(TI*& impulse, const int halfNumCoefs, - const TI* const in, const size_t inputIndex) { - int16_t* head = impulse + halfNumCoefs*CHANNELS; +void AudioResamplerDyn::InBuffer::readAgain(TI*& impulse, const int halfNumCoefs, + const TI* const in, const size_t inputIndex) +{ + TI* head = impulse + halfNumCoefs*CHANNELS; for (size_t i=0 ; i +template template -void AudioResamplerDyn::InBuffer::readAdvance(TI*& impulse, const int halfNumCoefs, - const TI* const in, const size_t inputIndex) { +void AudioResamplerDyn::InBuffer::readAdvance(TI*& impulse, const int halfNumCoefs, + const TI* const in, const size_t inputIndex) +{ impulse += CHANNELS; if (CC_UNLIKELY(impulse >= mRingFull)) { @@ -146,7 +152,8 @@ void AudioResamplerDyn::InBuffer::readAdvance(TI*& impulse, const int halfNu readAgain(impulse, halfNumCoefs, in, inputIndex); } -void AudioResamplerDyn::Constants::set( +template +void AudioResamplerDyn::Constants::set( int L, int halfNumCoefs, int inSampleRate, int outSampleRate) { int bits = 0; @@ -159,10 +166,11 @@ void AudioResamplerDyn::Constants::set( mHalfNumCoefs = halfNumCoefs; } -AudioResamplerDyn::AudioResamplerDyn(int bitDepth, +template +AudioResamplerDyn::AudioResamplerDyn(int bitDepth, int inChannelCount, int32_t sampleRate, src_quality quality) : AudioResampler(bitDepth, inChannelCount, sampleRate, quality), - mResampleType(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY), + mResampleFunc(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY), mCoefBuffer(NULL) { mVolumeSimd[0] = mVolumeSimd[1] = 0; @@ -173,33 +181,48 @@ AudioResamplerDyn::AudioResamplerDyn(int bitDepth, mConstants.set(128, 8, mSampleRate, mSampleRate); // TODO: set better } -AudioResamplerDyn::~AudioResamplerDyn() { +template +AudioResamplerDyn::~AudioResamplerDyn() +{ free(mCoefBuffer); } -void AudioResamplerDyn::init() { +template +void AudioResamplerDyn::init() +{ mFilterSampleRate = 0; // always trigger new filter generation mInBuffer.init(); } -void AudioResamplerDyn::setVolume(int16_t left, int16_t right) { +template +void AudioResamplerDyn::setVolume(int16_t left, int16_t right) +{ AudioResampler::setVolume(left, right); - mVolumeSimd[0] = static_cast(left)<<16; - mVolumeSimd[1] = static_cast(right)<<16; + // volume is applied on the output type. + if (is_same::value || is_same::value) { + const TO scale = 1. / (1UL << 12); + mVolumeSimd[0] = static_cast(left) * scale; + mVolumeSimd[1] = static_cast(right) * scale; + } else { + mVolumeSimd[0] = static_cast(left) << 16; + mVolumeSimd[1] = static_cast(right) << 16; + } } -template T max(T a, T b) {return a > b ? a : b;} +template T max(T a, T b) {return a > b ? a : b;} -template T absdiff(T a, T b) {return a > b ? a - b : b - a;} +template T absdiff(T a, T b) {return a > b ? a - b : b - a;} -template -void AudioResamplerDyn::createKaiserFir(Constants &c, double stopBandAtten, - int inSampleRate, int outSampleRate, double tbwCheat) { - T* buf = reinterpret_cast(memalign(32, (c.mL+1)*c.mHalfNumCoefs*sizeof(T))); +template +void AudioResamplerDyn::createKaiserFir(Constants &c, + double stopBandAtten, int inSampleRate, int outSampleRate, double tbwCheat) +{ + TC* buf; static const double atten = 0.9998; // to avoid ripple overflow double fcr; double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten); + (void)posix_memalign(reinterpret_cast(&buf), 32, (c.mL+1)*c.mHalfNumCoefs*sizeof(TC)); if (inSampleRate < outSampleRate) { // upsample fcr = max(0.5*tbwCheat - tbw/2, tbw/2); } else { // downsample @@ -207,7 +230,7 @@ void AudioResamplerDyn::createKaiserFir(Constants &c, double stopBandAtten, } // create and set filter firKaiserGen(buf, c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten); - c.setBuf(buf); + c.mFirCoefs = buf; if (mCoefBuffer) { free(mCoefBuffer); } @@ -229,7 +252,8 @@ void AudioResamplerDyn::createKaiserFir(Constants &c, double stopBandAtten, } // recursive gcd. Using objdump, it appears the tail recursion is converted to a while loop. -static int gcd(int n, int m) { +static int gcd(int n, int m) +{ if (m == 0) { return n; } @@ -237,7 +261,8 @@ static int gcd(int n, int m) { } static bool isClose(int32_t newSampleRate, int32_t prevSampleRate, - int32_t filterSampleRate, int32_t outSampleRate) { + int32_t filterSampleRate, int32_t outSampleRate) +{ // different upsampling ratios do not need a filter change. if (filterSampleRate != 0 @@ -254,7 +279,9 @@ static bool isClose(int32_t newSampleRate, int32_t prevSampleRate, return pdiff < prevSampleRate>>4 && adiff < filterSampleRate>>3; } -void AudioResamplerDyn::setSampleRate(int32_t inSampleRate) { +template +void AudioResamplerDyn::setSampleRate(int32_t inSampleRate) +{ if (mInSampleRate == inSampleRate) { return; } @@ -358,13 +385,8 @@ void AudioResamplerDyn::setSampleRate(int32_t inSampleRate) { // create the filter mConstants.set(phases, halfLength, inSampleRate, mSampleRate); - if (useS32) { - createKaiserFir(mConstants, stopBandAtten, - inSampleRate, mSampleRate, tbwCheat); - } else { - createKaiserFir(mConstants, stopBandAtten, - inSampleRate, mSampleRate, tbwCheat); - } + createKaiserFir(mConstants, stopBandAtten, + inSampleRate, mSampleRate, tbwCheat); } // End Kaiser filter // update phase and state based on the new filter. @@ -386,7 +408,7 @@ void AudioResamplerDyn::setSampleRate(int32_t inSampleRate) { mPhaseFraction = mPhaseFraction >> c.mShift << c.mShift; // remove fractional phase } - mResampleType = RESAMPLETYPE(mChannelCount, locked, stride, !!useS32); + setResampler(RESAMPLETYPE(mChannelCount, locked, stride)); #ifdef DEBUG_RESAMPLER printf("channels:%d %s stride:%d %s coef:%d shift:%d\n", mChannelCount, locked ? "locked" : "interpolated", @@ -394,78 +416,45 @@ void AudioResamplerDyn::setSampleRate(int32_t inSampleRate) { #endif } -void AudioResamplerDyn::resample(int32_t* out, size_t outFrameCount, +template +void AudioResamplerDyn::resample(int32_t* out, size_t outFrameCount, AudioBufferProvider* provider) { - // TODO: - // 24 cases - this perhaps can be reduced later, as testing might take too long - switch (mResampleType) { + (this->*mResampleFunc)(reinterpret_cast(out), outFrameCount, provider); +} +template +void AudioResamplerDyn::setResampler(unsigned resampleType) +{ // stride 16 (falls back to stride 2 for machines that do not support NEON) - case RESAMPLETYPE(1, true, 16, 0): - return resample<1, true, 16>(out, outFrameCount, mConstants.mFirCoefsS16, provider); - case RESAMPLETYPE(2, true, 16, 0): - return resample<2, true, 16>(out, outFrameCount, mConstants.mFirCoefsS16, provider); - case RESAMPLETYPE(1, false, 16, 0): - return resample<1, false, 16>(out, outFrameCount, mConstants.mFirCoefsS16, provider); - case RESAMPLETYPE(2, false, 16, 0): - return resample<2, false, 16>(out, outFrameCount, mConstants.mFirCoefsS16, provider); - case RESAMPLETYPE(1, true, 16, 1): - return resample<1, true, 16>(out, outFrameCount, mConstants.mFirCoefsS32, provider); - case RESAMPLETYPE(2, true, 16, 1): - return resample<2, true, 16>(out, outFrameCount, mConstants.mFirCoefsS32, provider); - case RESAMPLETYPE(1, false, 16, 1): - return resample<1, false, 16>(out, outFrameCount, mConstants.mFirCoefsS32, provider); - case RESAMPLETYPE(2, false, 16, 1): - return resample<2, false, 16>(out, outFrameCount, mConstants.mFirCoefsS32, provider); -#if 0 - // TODO: Remove these? - // stride 8 - case RESAMPLETYPE(1, true, 8, 0): - return resample<1, true, 8>(out, outFrameCount, mConstants.mFirCoefsS16, provider); - case RESAMPLETYPE(2, true, 8, 0): - return resample<2, true, 8>(out, outFrameCount, mConstants.mFirCoefsS16, provider); - case RESAMPLETYPE(1, false, 8, 0): - return resample<1, false, 8>(out, outFrameCount, mConstants.mFirCoefsS16, provider); - case RESAMPLETYPE(2, false, 8, 0): - return resample<2, false, 8>(out, outFrameCount, mConstants.mFirCoefsS16, provider); - case RESAMPLETYPE(1, true, 8, 1): - return resample<1, true, 8>(out, outFrameCount, mConstants.mFirCoefsS32, provider); - case RESAMPLETYPE(2, true, 8, 1): - return resample<2, true, 8>(out, outFrameCount, mConstants.mFirCoefsS32, provider); - case RESAMPLETYPE(1, false, 8, 1): - return resample<1, false, 8>(out, outFrameCount, mConstants.mFirCoefsS32, provider); - case RESAMPLETYPE(2, false, 8, 1): - return resample<2, false, 8>(out, outFrameCount, mConstants.mFirCoefsS32, provider); - // stride 2 (can handle any filter length) - case RESAMPLETYPE(1, true, 2, 0): - return resample<1, true, 2>(out, outFrameCount, mConstants.mFirCoefsS16, provider); - case RESAMPLETYPE(2, true, 2, 0): - return resample<2, true, 2>(out, outFrameCount, mConstants.mFirCoefsS16, provider); - case RESAMPLETYPE(1, false, 2, 0): - return resample<1, false, 2>(out, outFrameCount, mConstants.mFirCoefsS16, provider); - case RESAMPLETYPE(2, false, 2, 0): - return resample<2, false, 2>(out, outFrameCount, mConstants.mFirCoefsS16, provider); - case RESAMPLETYPE(1, true, 2, 1): - return resample<1, true, 2>(out, outFrameCount, mConstants.mFirCoefsS32, provider); - case RESAMPLETYPE(2, true, 2, 1): - return resample<2, true, 2>(out, outFrameCount, mConstants.mFirCoefsS32, provider); - case RESAMPLETYPE(1, false, 2, 1): - return resample<1, false, 2>(out, outFrameCount, mConstants.mFirCoefsS32, provider); - case RESAMPLETYPE(2, false, 2, 1): - return resample<2, false, 2>(out, outFrameCount, mConstants.mFirCoefsS32, provider); -#endif + switch (resampleType) { + case RESAMPLETYPE(1, true, 16): + mResampleFunc = &AudioResamplerDyn::resample<1, true, 16>; + return; + case RESAMPLETYPE(2, true, 16): + mResampleFunc = &AudioResamplerDyn::resample<2, true, 16>; + return; + case RESAMPLETYPE(1, false, 16): + mResampleFunc = &AudioResamplerDyn::resample<1, false, 16>; + return; + case RESAMPLETYPE(2, false, 16): + mResampleFunc = &AudioResamplerDyn::resample<2, false, 16>; + return; default: - ; // error + LOG_ALWAYS_FATAL("Invalid resampler type: %u", resampleType); + mResampleFunc = NULL; + return; } } -template -void AudioResamplerDyn::resample(int32_t* out, size_t outFrameCount, - const TC* const coefs, AudioBufferProvider* provider) +template +template +void AudioResamplerDyn::resample(TO* out, size_t outFrameCount, + AudioBufferProvider* provider) { const Constants& c(mConstants); - int16_t* impulse = mInBuffer.getImpulse(); + const TC* const coefs = mConstants.mFirCoefs; + TI* impulse = mInBuffer.getImpulse(); size_t inputIndex = mInputIndex; uint32_t phaseFraction = mPhaseFraction; const uint32_t phaseIncrement = mPhaseIncrement; @@ -491,8 +480,9 @@ void AudioResamplerDyn::resample(int32_t* out, size_t outFrameCount, goto resample_exit; } if (phaseFraction >= phaseWrapLimit) { // read in data - mInBuffer.readAdvance( - impulse, c.mHalfNumCoefs, mBuffer.i16, inputIndex); + mInBuffer.template readAdvance( + impulse, c.mHalfNumCoefs, + reinterpret_cast(mBuffer.raw), inputIndex); phaseFraction -= phaseWrapLimit; while (phaseFraction >= phaseWrapLimit) { inputIndex++; @@ -501,20 +491,21 @@ void AudioResamplerDyn::resample(int32_t* out, size_t outFrameCount, provider->releaseBuffer(&mBuffer); break; } - mInBuffer.readAdvance( - impulse, c.mHalfNumCoefs, mBuffer.i16, inputIndex); + mInBuffer.template readAdvance( + impulse, c.mHalfNumCoefs, + reinterpret_cast(mBuffer.raw), inputIndex); phaseFraction -= phaseWrapLimit; } } } - const int16_t* const in = mBuffer.i16; + const TI* const in = reinterpret_cast(mBuffer.raw); const size_t frameCount = mBuffer.frameCount; const int coefShift = c.mShift; const int halfNumCoefs = c.mHalfNumCoefs; - const int32_t* const volumeSimd = mVolumeSimd; + const TO* const volumeSimd = mVolumeSimd; // reread the last input in. - mInBuffer.readAgain(impulse, halfNumCoefs, in, inputIndex); + mInBuffer.template readAgain(impulse, halfNumCoefs, in, inputIndex); // main processing loop while (CC_LIKELY(outputIndex < outputSampleCount)) { @@ -537,7 +528,7 @@ void AudioResamplerDyn::resample(int32_t* out, size_t outFrameCount, if (inputIndex >= frameCount) { goto done; // need a new buffer } - mInBuffer.readAdvance(impulse, halfNumCoefs, in, inputIndex); + mInBuffer.template readAdvance(impulse, halfNumCoefs, in, inputIndex); phaseFraction -= phaseWrapLimit; } } @@ -556,5 +547,10 @@ resample_exit: mPhaseFraction = phaseFraction; } +/* instantiate templates used by AudioResampler::create */ +template class AudioResamplerDyn; +template class AudioResamplerDyn; +template class AudioResamplerDyn; + // ---------------------------------------------------------------------------- }; // namespace android -- cgit v1.1