Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "AudioResamplerDyn" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <malloc.h> |
| 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <dlfcn.h> |
| 24 | #include <math.h> |
| 25 | |
| 26 | #include <cutils/compiler.h> |
| 27 | #include <cutils/properties.h> |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame] | 28 | #include <utils/Debug.h> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 29 | #include <utils/Log.h> |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 30 | #include <audio_utils/primitives.h> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 31 | |
Henrik Smiding | 841920d | 2016-02-15 16:20:45 +0100 | [diff] [blame] | 32 | #include "AudioResamplerFirOps.h" // USE_NEON, USE_SSE and USE_INLINE_ASSEMBLY defined here |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 33 | #include "AudioResamplerFirProcess.h" |
| 34 | #include "AudioResamplerFirProcessNeon.h" |
Henrik Smiding | 841920d | 2016-02-15 16:20:45 +0100 | [diff] [blame] | 35 | #include "AudioResamplerFirProcessSSE.h" |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 36 | #include "AudioResamplerFirGen.h" // requires math.h |
| 37 | #include "AudioResamplerDyn.h" |
| 38 | |
| 39 | //#define DEBUG_RESAMPLER |
| 40 | |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 41 | // use this for our buffer alignment. Should be at least 32 bytes. |
| 42 | constexpr size_t CACHE_LINE_SIZE = 64; |
| 43 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 44 | namespace android { |
| 45 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 46 | /* |
| 47 | * InBuffer is a type agnostic input buffer. |
| 48 | * |
| 49 | * Layout of the state buffer for halfNumCoefs=8. |
| 50 | * |
| 51 | * [rrrrrrppppppppnnnnnnnnrrrrrrrrrrrrrrrrrrr.... rrrrrrr] |
| 52 | * S I R |
| 53 | * |
| 54 | * S = mState |
| 55 | * I = mImpulse |
| 56 | * R = mRingFull |
| 57 | * p = past samples, convoluted with the (p)ositive side of sinc() |
| 58 | * n = future samples, convoluted with the (n)egative side of sinc() |
| 59 | * r = extra space for implementing the ring buffer |
| 60 | */ |
| 61 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 62 | template<typename TC, typename TI, typename TO> |
| 63 | AudioResamplerDyn<TC, TI, TO>::InBuffer::InBuffer() |
| 64 | : mState(NULL), mImpulse(NULL), mRingFull(NULL), mStateCount(0) |
| 65 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 68 | template<typename TC, typename TI, typename TO> |
| 69 | AudioResamplerDyn<TC, TI, TO>::InBuffer::~InBuffer() |
| 70 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 71 | init(); |
| 72 | } |
| 73 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 74 | template<typename TC, typename TI, typename TO> |
| 75 | void AudioResamplerDyn<TC, TI, TO>::InBuffer::init() |
| 76 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 77 | free(mState); |
| 78 | mState = NULL; |
| 79 | mImpulse = NULL; |
| 80 | mRingFull = NULL; |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 81 | mStateCount = 0; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // resizes the state buffer to accommodate the appropriate filter length |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 85 | template<typename TC, typename TI, typename TO> |
| 86 | void AudioResamplerDyn<TC, TI, TO>::InBuffer::resize(int CHANNELS, int halfNumCoefs) |
| 87 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 88 | // calculate desired state size |
Glenn Kasten | a4daf0b | 2014-07-28 16:34:45 -0700 | [diff] [blame] | 89 | size_t stateCount = halfNumCoefs * CHANNELS * 2 * kStateSizeMultipleOfFilterLength; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 90 | |
| 91 | // check if buffer needs resizing |
| 92 | if (mState |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 93 | && stateCount == mStateCount |
Glenn Kasten | a4daf0b | 2014-07-28 16:34:45 -0700 | [diff] [blame] | 94 | && mRingFull-mState == (ssize_t) (mStateCount-halfNumCoefs*CHANNELS)) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
| 98 | // create new buffer |
Glenn Kasten | a4daf0b | 2014-07-28 16:34:45 -0700 | [diff] [blame] | 99 | TI* state = NULL; |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 100 | (void)posix_memalign( |
| 101 | reinterpret_cast<void **>(&state), |
| 102 | CACHE_LINE_SIZE /* alignment */, |
| 103 | stateCount * sizeof(*state)); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 104 | memset(state, 0, stateCount*sizeof(*state)); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 105 | |
| 106 | // attempt to preserve state |
| 107 | if (mState) { |
| 108 | TI* srcLo = mImpulse - halfNumCoefs*CHANNELS; |
| 109 | TI* srcHi = mImpulse + halfNumCoefs*CHANNELS; |
| 110 | TI* dst = state; |
| 111 | |
| 112 | if (srcLo < mState) { |
| 113 | dst += mState-srcLo; |
| 114 | srcLo = mState; |
| 115 | } |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 116 | if (srcHi > mState + mStateCount) { |
| 117 | srcHi = mState + mStateCount; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 118 | } |
| 119 | memcpy(dst, srcLo, (srcHi - srcLo) * sizeof(*srcLo)); |
| 120 | free(mState); |
| 121 | } |
| 122 | |
| 123 | // set class member vars |
| 124 | mState = state; |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 125 | mStateCount = stateCount; |
| 126 | mImpulse = state + halfNumCoefs*CHANNELS; // actually one sample greater than needed |
| 127 | mRingFull = state + mStateCount - halfNumCoefs*CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // copy in the input data into the head (impulse+halfNumCoefs) of the buffer. |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 131 | template<typename TC, typename TI, typename TO> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 132 | template<int CHANNELS> |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 133 | void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAgain(TI*& impulse, const int halfNumCoefs, |
| 134 | const TI* const in, const size_t inputIndex) |
| 135 | { |
| 136 | TI* head = impulse + halfNumCoefs*CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 137 | for (size_t i=0 ; i<CHANNELS ; i++) { |
| 138 | head[i] = in[inputIndex*CHANNELS + i]; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // advance the impulse pointer, and load in data into the head (impulse+halfNumCoefs) |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 143 | template<typename TC, typename TI, typename TO> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 144 | template<int CHANNELS> |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 145 | void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAdvance(TI*& impulse, const int halfNumCoefs, |
| 146 | const TI* const in, const size_t inputIndex) |
| 147 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 148 | impulse += CHANNELS; |
| 149 | |
| 150 | if (CC_UNLIKELY(impulse >= mRingFull)) { |
| 151 | const size_t shiftDown = mRingFull - mState - halfNumCoefs*CHANNELS; |
| 152 | memcpy(mState, mState+shiftDown, halfNumCoefs*CHANNELS*2*sizeof(TI)); |
| 153 | impulse -= shiftDown; |
| 154 | } |
| 155 | readAgain<CHANNELS>(impulse, halfNumCoefs, in, inputIndex); |
| 156 | } |
| 157 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 158 | template<typename TC, typename TI, typename TO> |
Hochi Huang | bd179d1 | 2016-03-28 13:30:46 -0700 | [diff] [blame] | 159 | void AudioResamplerDyn<TC, TI, TO>::InBuffer::reset() |
| 160 | { |
| 161 | // clear resampler state |
| 162 | if (mState != nullptr) { |
| 163 | memset(mState, 0, mStateCount * sizeof(TI)); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | template<typename TC, typename TI, typename TO> |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 168 | void AudioResamplerDyn<TC, TI, TO>::Constants::set( |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 169 | int L, int halfNumCoefs, int inSampleRate, int outSampleRate) |
| 170 | { |
| 171 | int bits = 0; |
| 172 | int lscale = inSampleRate/outSampleRate < 2 ? L - 1 : |
| 173 | static_cast<int>(static_cast<uint64_t>(L)*inSampleRate/outSampleRate); |
| 174 | for (int i=lscale; i; ++bits, i>>=1) |
| 175 | ; |
| 176 | mL = L; |
| 177 | mShift = kNumPhaseBits - bits; |
| 178 | mHalfNumCoefs = halfNumCoefs; |
| 179 | } |
| 180 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 181 | template<typename TC, typename TI, typename TO> |
Andy Hung | 3348e36 | 2014-07-07 10:21:44 -0700 | [diff] [blame] | 182 | AudioResamplerDyn<TC, TI, TO>::AudioResamplerDyn( |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 183 | int inChannelCount, int32_t sampleRate, src_quality quality) |
Andy Hung | 3348e36 | 2014-07-07 10:21:44 -0700 | [diff] [blame] | 184 | : AudioResampler(inChannelCount, sampleRate, quality), |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 185 | mResampleFunc(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY), |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 186 | mCoefBuffer(NULL) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 187 | { |
| 188 | mVolumeSimd[0] = mVolumeSimd[1] = 0; |
Andy Hung | 1af3408 | 2014-02-19 17:42:25 -0800 | [diff] [blame] | 189 | // The AudioResampler base class assumes we are always ready for 1:1 resampling. |
| 190 | // We reset mInSampleRate to 0, so setSampleRate() will calculate filters for |
| 191 | // setSampleRate() for 1:1. (May be removed if precalculated filters are used.) |
| 192 | mInSampleRate = 0; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 193 | mConstants.set(128, 8, mSampleRate, mSampleRate); // TODO: set better |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 194 | |
| 195 | // fetch property based resampling parameters |
| 196 | mPropertyEnableAtSampleRate = property_get_int32( |
| 197 | "ro.audio.resampler.psd.enable_at_samplerate", mPropertyEnableAtSampleRate); |
| 198 | mPropertyHalfFilterLength = property_get_int32( |
| 199 | "ro.audio.resampler.psd.halflength", mPropertyHalfFilterLength); |
| 200 | mPropertyStopbandAttenuation = property_get_int32( |
| 201 | "ro.audio.resampler.psd.stopband", mPropertyStopbandAttenuation); |
| 202 | mPropertyCutoffPercent = property_get_int32( |
| 203 | "ro.audio.resampler.psd.cutoff_percent", mPropertyCutoffPercent); |
Andy Hung | 076f690 | 2019-04-02 15:40:54 -0700 | [diff] [blame] | 204 | mPropertyTransitionBandwidthCheat = property_get_int32( |
| 205 | "ro.audio.resampler.psd.tbwcheat", mPropertyTransitionBandwidthCheat); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 208 | template<typename TC, typename TI, typename TO> |
| 209 | AudioResamplerDyn<TC, TI, TO>::~AudioResamplerDyn() |
| 210 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 211 | free(mCoefBuffer); |
| 212 | } |
| 213 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 214 | template<typename TC, typename TI, typename TO> |
| 215 | void AudioResamplerDyn<TC, TI, TO>::init() |
| 216 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 217 | mFilterSampleRate = 0; // always trigger new filter generation |
| 218 | mInBuffer.init(); |
| 219 | } |
| 220 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 221 | template<typename TC, typename TI, typename TO> |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 222 | void AudioResamplerDyn<TC, TI, TO>::setVolume(float left, float right) |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 223 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 224 | AudioResampler::setVolume(left, right); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 225 | if (is_same<TO, float>::value || is_same<TO, double>::value) { |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 226 | mVolumeSimd[0] = static_cast<TO>(left); |
| 227 | mVolumeSimd[1] = static_cast<TO>(right); |
| 228 | } else { // integer requires scaling to U4_28 (rounding down) |
| 229 | // integer volumes are clamped to 0 to UNITY_GAIN so there |
| 230 | // are no issues with signed overflow. |
| 231 | mVolumeSimd[0] = u4_28_from_float(clampFloatVol(left)); |
| 232 | mVolumeSimd[1] = u4_28_from_float(clampFloatVol(right)); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 233 | } |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 236 | // TODO: update to C++11 |
| 237 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 238 | template<typename T> T max(T a, T b) {return a > b ? a : b;} |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 239 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 240 | template<typename T> T absdiff(T a, T b) {return a > b ? a - b : b - a;} |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 241 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 242 | template<typename TC, typename TI, typename TO> |
| 243 | void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c, |
| 244 | double stopBandAtten, int inSampleRate, int outSampleRate, double tbwCheat) |
| 245 | { |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 246 | // compute the normalized transition bandwidth |
| 247 | const double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten); |
Andy Hung | 3f69241 | 2019-04-02 15:48:22 -0700 | [diff] [blame] | 248 | const double halfbw = tbw * 0.5; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 249 | |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 250 | double fcr; // compute fcr, the 3 dB amplitude cut-off. |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 251 | if (inSampleRate < outSampleRate) { // upsample |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 252 | fcr = max(0.5 * tbwCheat - halfbw, halfbw); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 253 | } else { // downsample |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 254 | fcr = max(0.5 * tbwCheat * outSampleRate / inSampleRate - halfbw, halfbw); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 255 | } |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 256 | createKaiserFir(c, stopBandAtten, fcr); |
| 257 | } |
| 258 | |
| 259 | template<typename TC, typename TI, typename TO> |
| 260 | void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c, |
| 261 | double stopBandAtten, double fcr) { |
| 262 | // compute the normalized transition bandwidth |
| 263 | const double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten); |
| 264 | const int phases = c.mL; |
| 265 | const int halfLength = c.mHalfNumCoefs; |
| 266 | |
| 267 | // create buffer |
| 268 | TC *coefs = nullptr; |
| 269 | int ret = posix_memalign( |
| 270 | reinterpret_cast<void **>(&coefs), |
| 271 | CACHE_LINE_SIZE /* alignment */, |
| 272 | (phases + 1) * halfLength * sizeof(TC)); |
| 273 | LOG_ALWAYS_FATAL_IF(ret != 0, "Cannot allocate buffer memory, ret %d", ret); |
| 274 | c.mFirCoefs = coefs; |
| 275 | free(mCoefBuffer); |
| 276 | mCoefBuffer = coefs; |
| 277 | |
| 278 | // square the computed minimum passband value (extra safety). |
| 279 | double attenuation = |
| 280 | computeWindowedSincMinimumPassbandValue(stopBandAtten); |
| 281 | attenuation *= attenuation; |
| 282 | |
| 283 | // design filter |
| 284 | firKaiserGen(coefs, phases, halfLength, stopBandAtten, fcr, attenuation); |
| 285 | |
| 286 | // update the design criteria |
| 287 | mNormalizedCutoffFrequency = fcr; |
| 288 | mNormalizedTransitionBandwidth = tbw; |
| 289 | mFilterAttenuation = attenuation; |
| 290 | mStopbandAttenuationDb = stopBandAtten; |
| 291 | mPassbandRippleDb = computeWindowedSincPassbandRippleDb(stopBandAtten); |
| 292 | |
| 293 | #if 0 |
| 294 | // Keep this debug code in case an app causes resampler design issues. |
Andy Hung | 3f69241 | 2019-04-02 15:48:22 -0700 | [diff] [blame] | 295 | const double halfbw = tbw * 0.5; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 296 | // print basic filter stats |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 297 | ALOGD("L:%d hnc:%d stopBandAtten:%lf fcr:%lf atten:%lf tbw:%lf\n", |
| 298 | c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, attenuation, tbw); |
| 299 | |
| 300 | // test the filter and report results. |
| 301 | // Since this is a polyphase filter, normalized fp and fs must be scaled. |
| 302 | const double fp = (fcr - halfbw) / phases; |
| 303 | const double fs = (fcr + halfbw) / phases; |
| 304 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 305 | double passMin, passMax, passRipple; |
| 306 | double stopMax, stopRipple; |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 307 | |
| 308 | const int32_t passSteps = 1000; |
| 309 | |
Andy Hung | 3f69241 | 2019-04-02 15:48:22 -0700 | [diff] [blame] | 310 | testFir(coefs, c.mL, c.mHalfNumCoefs, fp, fs, passSteps, passSteps * c.mL /*stopSteps*/, |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 311 | passMin, passMax, passRipple, stopMax, stopRipple); |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 312 | ALOGD("passband(%lf, %lf): %.8lf %.8lf %.8lf\n", 0., fp, passMin, passMax, passRipple); |
| 313 | ALOGD("stopband(%lf, %lf): %.8lf %.3lf\n", fs, 0.5, stopMax, stopRipple); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 314 | #endif |
| 315 | } |
| 316 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 317 | // recursive gcd. Using objdump, it appears the tail recursion is converted to a while loop. |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 318 | static int gcd(int n, int m) |
| 319 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 320 | if (m == 0) { |
| 321 | return n; |
| 322 | } |
| 323 | return gcd(m, n % m); |
| 324 | } |
| 325 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 326 | static bool isClose(int32_t newSampleRate, int32_t prevSampleRate, |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 327 | int32_t filterSampleRate, int32_t outSampleRate) |
| 328 | { |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 329 | |
| 330 | // different upsampling ratios do not need a filter change. |
| 331 | if (filterSampleRate != 0 |
| 332 | && filterSampleRate < outSampleRate |
| 333 | && newSampleRate < outSampleRate) |
| 334 | return true; |
| 335 | |
| 336 | // check design criteria again if downsampling is detected. |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 337 | int pdiff = absdiff(newSampleRate, prevSampleRate); |
| 338 | int adiff = absdiff(newSampleRate, filterSampleRate); |
| 339 | |
| 340 | // allow up to 6% relative change increments. |
| 341 | // allow up to 12% absolute change increments (from filter design) |
| 342 | return pdiff < prevSampleRate>>4 && adiff < filterSampleRate>>3; |
| 343 | } |
| 344 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 345 | template<typename TC, typename TI, typename TO> |
| 346 | void AudioResamplerDyn<TC, TI, TO>::setSampleRate(int32_t inSampleRate) |
| 347 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 348 | if (mInSampleRate == inSampleRate) { |
| 349 | return; |
| 350 | } |
| 351 | int32_t oldSampleRate = mInSampleRate; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 352 | uint32_t oldPhaseWrapLimit = mConstants.mL << mConstants.mShift; |
| 353 | bool useS32 = false; |
| 354 | |
| 355 | mInSampleRate = inSampleRate; |
| 356 | |
| 357 | // TODO: Add precalculated Equiripple filters |
| 358 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 359 | if (mFilterQuality != getQuality() || |
| 360 | !isClose(inSampleRate, oldSampleRate, mFilterSampleRate, mSampleRate)) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 361 | mFilterSampleRate = inSampleRate; |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 362 | mFilterQuality = getQuality(); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 363 | |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 364 | double stopBandAtten; |
| 365 | double tbwCheat = 1.; // how much we "cheat" into aliasing |
| 366 | int halfLength; |
| 367 | double fcr = 0.; |
| 368 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 369 | // Begin Kaiser Filter computation |
| 370 | // |
| 371 | // The quantization floor for S16 is about 96db - 10*log_10(#length) + 3dB. |
| 372 | // Keep the stop band attenuation no greater than 84-85dB for 32 length S16 filters |
| 373 | // |
| 374 | // For s32 we keep the stop band attenuation at the same as 16b resolution, about |
| 375 | // 96-98dB |
| 376 | // |
| 377 | |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 378 | if (mPropertyEnableAtSampleRate >= 0 && mSampleRate >= mPropertyEnableAtSampleRate) { |
| 379 | // An alternative method which allows allows a greater fcr |
| 380 | // at the expense of potential aliasing. |
| 381 | halfLength = mPropertyHalfFilterLength; |
| 382 | stopBandAtten = mPropertyStopbandAttenuation; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 383 | useS32 = true; |
Andy Hung | 076f690 | 2019-04-02 15:40:54 -0700 | [diff] [blame] | 384 | |
| 385 | // Use either the stopband location for design (tbwCheat) |
| 386 | // or use the 3dB cutoff location for design (fcr). |
| 387 | // This choice is exclusive and based on whether fcr > 0. |
| 388 | if (mPropertyTransitionBandwidthCheat != 0) { |
| 389 | tbwCheat = mPropertyTransitionBandwidthCheat / 100.; |
| 390 | } else { |
| 391 | fcr = mInSampleRate <= mSampleRate |
| 392 | ? 0.5 : 0.5 * mSampleRate / mInSampleRate; |
| 393 | fcr *= mPropertyCutoffPercent / 100.; |
| 394 | } |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 395 | } else { |
Andy Hung | 06b40f9 | 2019-03-26 15:51:41 -0700 | [diff] [blame] | 396 | // Voice quality devices have lower sampling rates |
| 397 | // (and may be a consequence of downstream AMR-WB / G.722 codecs). |
| 398 | // For these devices, we ensure a wider resampler passband |
| 399 | // at the expense of aliasing noise (stopband attenuation |
| 400 | // and stopband frequency). |
| 401 | // |
| 402 | constexpr uint32_t kVoiceDeviceSampleRate = 16000; |
| 403 | |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 404 | if (mFilterQuality == DYN_HIGH_QUALITY) { |
Andy Hung | 06b40f9 | 2019-03-26 15:51:41 -0700 | [diff] [blame] | 405 | // float or 32b coefficients |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 406 | useS32 = true; |
| 407 | stopBandAtten = 98.; |
| 408 | if (inSampleRate >= mSampleRate * 4) { |
| 409 | halfLength = 48; |
| 410 | } else if (inSampleRate >= mSampleRate * 2) { |
| 411 | halfLength = 40; |
| 412 | } else { |
| 413 | halfLength = 32; |
| 414 | } |
Andy Hung | 06b40f9 | 2019-03-26 15:51:41 -0700 | [diff] [blame] | 415 | |
| 416 | if (mSampleRate <= kVoiceDeviceSampleRate) { |
| 417 | if (inSampleRate >= mSampleRate * 2) { |
| 418 | halfLength += 16; |
| 419 | } else { |
| 420 | halfLength += 8; |
| 421 | } |
| 422 | stopBandAtten = 84.; |
| 423 | tbwCheat = 1.05; |
| 424 | } |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 425 | } else if (mFilterQuality == DYN_LOW_QUALITY) { |
Andy Hung | 06b40f9 | 2019-03-26 15:51:41 -0700 | [diff] [blame] | 426 | // float or 16b coefficients |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 427 | useS32 = false; |
| 428 | stopBandAtten = 80.; |
| 429 | if (inSampleRate >= mSampleRate * 4) { |
| 430 | halfLength = 24; |
| 431 | } else if (inSampleRate >= mSampleRate * 2) { |
| 432 | halfLength = 16; |
| 433 | } else { |
| 434 | halfLength = 8; |
| 435 | } |
Andy Hung | 06b40f9 | 2019-03-26 15:51:41 -0700 | [diff] [blame] | 436 | if (mSampleRate <= kVoiceDeviceSampleRate) { |
| 437 | if (inSampleRate >= mSampleRate * 2) { |
| 438 | halfLength += 8; |
| 439 | } |
| 440 | tbwCheat = 1.05; |
| 441 | } else if (inSampleRate <= mSampleRate) { |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 442 | tbwCheat = 1.05; |
| 443 | } else { |
| 444 | tbwCheat = 1.03; |
| 445 | } |
| 446 | } else { // DYN_MED_QUALITY |
Andy Hung | 06b40f9 | 2019-03-26 15:51:41 -0700 | [diff] [blame] | 447 | // float or 16b coefficients |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 448 | // note: > 64 length filters with 16b coefs can have quantization noise problems |
| 449 | useS32 = false; |
| 450 | stopBandAtten = 84.; |
| 451 | if (inSampleRate >= mSampleRate * 4) { |
| 452 | halfLength = 32; |
| 453 | } else if (inSampleRate >= mSampleRate * 2) { |
| 454 | halfLength = 24; |
| 455 | } else { |
| 456 | halfLength = 16; |
| 457 | } |
Andy Hung | 06b40f9 | 2019-03-26 15:51:41 -0700 | [diff] [blame] | 458 | |
| 459 | if (mSampleRate <= kVoiceDeviceSampleRate) { |
| 460 | if (inSampleRate >= mSampleRate * 2) { |
| 461 | halfLength += 16; |
| 462 | } else { |
| 463 | halfLength += 8; |
| 464 | } |
| 465 | tbwCheat = 1.05; |
| 466 | } else if (inSampleRate <= mSampleRate) { |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 467 | tbwCheat = 1.03; |
| 468 | } else { |
| 469 | tbwCheat = 1.01; |
| 470 | } |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Andy Hung | 06b40f9 | 2019-03-26 15:51:41 -0700 | [diff] [blame] | 474 | if (fcr > 0.) { |
| 475 | ALOGV("%s: mFilterQuality:%d inSampleRate:%d mSampleRate:%d halfLength:%d " |
| 476 | "stopBandAtten:%lf fcr:%lf", |
| 477 | __func__, mFilterQuality, inSampleRate, mSampleRate, halfLength, |
| 478 | stopBandAtten, fcr); |
| 479 | } else { |
| 480 | ALOGV("%s: mFilterQuality:%d inSampleRate:%d mSampleRate:%d halfLength:%d " |
| 481 | "stopBandAtten:%lf tbwCheat:%lf", |
| 482 | __func__, mFilterQuality, inSampleRate, mSampleRate, halfLength, |
| 483 | stopBandAtten, tbwCheat); |
| 484 | } |
| 485 | |
| 486 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 487 | // determine the number of polyphases in the filterbank. |
| 488 | // for 16b, it is desirable to have 2^(16/2) = 256 phases. |
| 489 | // https://ccrma.stanford.edu/~jos/resample/Relation_Interpolation_Error_Quantization.html |
| 490 | // |
| 491 | // We are a bit more lax on this. |
| 492 | |
| 493 | int phases = mSampleRate / gcd(mSampleRate, inSampleRate); |
| 494 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 495 | // TODO: Once dynamic sample rate change is an option, the code below |
| 496 | // should be modified to execute only when dynamic sample rate change is enabled. |
| 497 | // |
| 498 | // as above, #phases less than 63 is too few phases for accurate linear interpolation. |
| 499 | // we increase the phases to compensate, but more phases means more memory per |
| 500 | // filter and more time to compute the filter. |
| 501 | // |
| 502 | // if we know that the filter will be used for dynamic sample rate changes, |
| 503 | // that would allow us skip this part for fixed sample rate resamplers. |
| 504 | // |
| 505 | while (phases<63) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 506 | phases *= 2; // this code only needed to support dynamic rate changes |
| 507 | } |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 508 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 509 | if (phases>=256) { // too many phases, always interpolate |
| 510 | phases = 127; |
| 511 | } |
| 512 | |
| 513 | // create the filter |
| 514 | mConstants.set(phases, halfLength, inSampleRate, mSampleRate); |
Andy Hung | 6bd378f | 2017-10-24 19:23:52 -0700 | [diff] [blame] | 515 | if (fcr > 0.) { |
| 516 | createKaiserFir(mConstants, stopBandAtten, fcr); |
| 517 | } else { |
| 518 | createKaiserFir(mConstants, stopBandAtten, |
| 519 | inSampleRate, mSampleRate, tbwCheat); |
| 520 | } |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 521 | } // End Kaiser filter |
| 522 | |
| 523 | // update phase and state based on the new filter. |
| 524 | const Constants& c(mConstants); |
| 525 | mInBuffer.resize(mChannelCount, c.mHalfNumCoefs); |
| 526 | const uint32_t phaseWrapLimit = c.mL << c.mShift; |
| 527 | // try to preserve as much of the phase fraction as possible for on-the-fly changes |
| 528 | mPhaseFraction = static_cast<unsigned long long>(mPhaseFraction) |
| 529 | * phaseWrapLimit / oldPhaseWrapLimit; |
| 530 | mPhaseFraction %= phaseWrapLimit; // should not do anything, but just in case. |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 531 | mPhaseIncrement = static_cast<uint32_t>(static_cast<uint64_t>(phaseWrapLimit) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 532 | * inSampleRate / mSampleRate); |
| 533 | |
| 534 | // determine which resampler to use |
| 535 | // check if locked phase (works only if mPhaseIncrement has no "fractional phase bits") |
| 536 | int locked = (mPhaseIncrement << (sizeof(mPhaseIncrement)*8 - c.mShift)) == 0; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 537 | if (locked) { |
| 538 | mPhaseFraction = mPhaseFraction >> c.mShift << c.mShift; // remove fractional phase |
| 539 | } |
Andy Hung | 83be256 | 2014-02-03 14:11:09 -0800 | [diff] [blame] | 540 | |
Andy Hung | 075abae | 2014-04-09 19:36:43 -0700 | [diff] [blame] | 541 | // stride is the minimum number of filter coefficients processed per loop iteration. |
| 542 | // We currently only allow a stride of 16 to match with SIMD processing. |
| 543 | // This means that the filter length must be a multiple of 16, |
| 544 | // or half the filter length (mHalfNumCoefs) must be a multiple of 8. |
| 545 | // |
| 546 | // Note: A stride of 2 is achieved with non-SIMD processing. |
| 547 | int stride = ((c.mHalfNumCoefs & 7) == 0) ? 16 : 2; |
| 548 | LOG_ALWAYS_FATAL_IF(stride < 16, "Resampler stride must be 16 or more"); |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 549 | LOG_ALWAYS_FATAL_IF(mChannelCount < 1 || mChannelCount > 8, |
Andy Hung | 075abae | 2014-04-09 19:36:43 -0700 | [diff] [blame] | 550 | "Resampler channels(%d) must be between 1 to 8", mChannelCount); |
| 551 | // stride 16 (falls back to stride 2 for machines that do not support NEON) |
| 552 | if (locked) { |
| 553 | switch (mChannelCount) { |
| 554 | case 1: |
| 555 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, true, 16>; |
| 556 | break; |
| 557 | case 2: |
| 558 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, true, 16>; |
| 559 | break; |
| 560 | case 3: |
| 561 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, true, 16>; |
| 562 | break; |
| 563 | case 4: |
| 564 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, true, 16>; |
| 565 | break; |
| 566 | case 5: |
| 567 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, true, 16>; |
| 568 | break; |
| 569 | case 6: |
| 570 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, true, 16>; |
| 571 | break; |
| 572 | case 7: |
| 573 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, true, 16>; |
| 574 | break; |
| 575 | case 8: |
| 576 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, true, 16>; |
| 577 | break; |
| 578 | } |
| 579 | } else { |
| 580 | switch (mChannelCount) { |
| 581 | case 1: |
| 582 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, false, 16>; |
| 583 | break; |
| 584 | case 2: |
| 585 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, false, 16>; |
| 586 | break; |
| 587 | case 3: |
| 588 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, false, 16>; |
| 589 | break; |
| 590 | case 4: |
| 591 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, false, 16>; |
| 592 | break; |
| 593 | case 5: |
| 594 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, false, 16>; |
| 595 | break; |
| 596 | case 6: |
| 597 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, false, 16>; |
| 598 | break; |
| 599 | case 7: |
| 600 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, false, 16>; |
| 601 | break; |
| 602 | case 8: |
| 603 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, false, 16>; |
| 604 | break; |
| 605 | } |
| 606 | } |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 607 | #ifdef DEBUG_RESAMPLER |
| 608 | printf("channels:%d %s stride:%d %s coef:%d shift:%d\n", |
| 609 | mChannelCount, locked ? "locked" : "interpolated", |
| 610 | stride, useS32 ? "S32" : "S16", 2*c.mHalfNumCoefs, c.mShift); |
| 611 | #endif |
| 612 | } |
| 613 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 614 | template<typename TC, typename TI, typename TO> |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 615 | size_t AudioResamplerDyn<TC, TI, TO>::resample(int32_t* out, size_t outFrameCount, |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 616 | AudioBufferProvider* provider) |
| 617 | { |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 618 | return (this->*mResampleFunc)(reinterpret_cast<TO*>(out), outFrameCount, provider); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 619 | } |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 620 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 621 | template<typename TC, typename TI, typename TO> |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 622 | template<int CHANNELS, bool LOCKED, int STRIDE> |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 623 | size_t AudioResamplerDyn<TC, TI, TO>::resample(TO* out, size_t outFrameCount, |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 624 | AudioBufferProvider* provider) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 625 | { |
Andy Hung | 075abae | 2014-04-09 19:36:43 -0700 | [diff] [blame] | 626 | // TODO Mono -> Mono is not supported. OUTPUT_CHANNELS reflects minimum of stereo out. |
| 627 | const int OUTPUT_CHANNELS = (CHANNELS < 2) ? 2 : CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 628 | const Constants& c(mConstants); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 629 | const TC* const coefs = mConstants.mFirCoefs; |
| 630 | TI* impulse = mInBuffer.getImpulse(); |
Andy Hung | 411cb8e | 2014-05-27 12:32:17 -0700 | [diff] [blame] | 631 | size_t inputIndex = 0; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 632 | uint32_t phaseFraction = mPhaseFraction; |
| 633 | const uint32_t phaseIncrement = mPhaseIncrement; |
| 634 | size_t outputIndex = 0; |
Andy Hung | 075abae | 2014-04-09 19:36:43 -0700 | [diff] [blame] | 635 | size_t outputSampleCount = outFrameCount * OUTPUT_CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 636 | const uint32_t phaseWrapLimit = c.mL << c.mShift; |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 637 | size_t inFrameCount = (phaseIncrement * (uint64_t)outFrameCount + phaseFraction) |
| 638 | / phaseWrapLimit; |
| 639 | // sanity check that inFrameCount is in signed 32 bit integer range. |
| 640 | ALOG_ASSERT(0 <= inFrameCount && inFrameCount < (1U << 31)); |
| 641 | |
| 642 | //ALOGV("inFrameCount:%d outFrameCount:%d" |
| 643 | // " phaseIncrement:%u phaseFraction:%u phaseWrapLimit:%u", |
| 644 | // inFrameCount, outFrameCount, phaseIncrement, phaseFraction, phaseWrapLimit); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 645 | |
| 646 | // NOTE: be very careful when modifying the code here. register |
| 647 | // pressure is very high and a small change might cause the compiler |
| 648 | // to generate far less efficient code. |
| 649 | // Always sanity check the result with objdump or test-resample. |
| 650 | |
| 651 | // the following logic is a bit convoluted to keep the main processing loop |
| 652 | // as tight as possible with register allocation. |
| 653 | while (outputIndex < outputSampleCount) { |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 654 | //ALOGV("LOOP: inFrameCount:%d outputIndex:%d outFrameCount:%d" |
| 655 | // " phaseFraction:%u phaseWrapLimit:%u", |
| 656 | // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit); |
| 657 | |
| 658 | // check inputIndex overflow |
Tobias Melin | 4348921 | 2016-09-16 10:04:26 +0200 | [diff] [blame] | 659 | ALOG_ASSERT(inputIndex <= mBuffer.frameCount, "inputIndex%zu > frameCount%zu", |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 660 | inputIndex, mBuffer.frameCount); |
| 661 | // Buffer is empty, fetch a new one if necessary (inFrameCount > 0). |
| 662 | // We may not fetch a new buffer if the existing data is sufficient. |
| 663 | while (mBuffer.frameCount == 0 && inFrameCount > 0) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 664 | mBuffer.frameCount = inFrameCount; |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 665 | provider->getNextBuffer(&mBuffer); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 666 | if (mBuffer.raw == NULL) { |
Hochi Huang | bd179d1 | 2016-03-28 13:30:46 -0700 | [diff] [blame] | 667 | // We are either at the end of playback or in an underrun situation. |
| 668 | // Reset buffer to prevent pop noise at the next buffer. |
| 669 | mInBuffer.reset(); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 670 | goto resample_exit; |
| 671 | } |
Andy Hung | 411cb8e | 2014-05-27 12:32:17 -0700 | [diff] [blame] | 672 | inFrameCount -= mBuffer.frameCount; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 673 | if (phaseFraction >= phaseWrapLimit) { // read in data |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 674 | mInBuffer.template readAdvance<CHANNELS>( |
| 675 | impulse, c.mHalfNumCoefs, |
| 676 | reinterpret_cast<TI*>(mBuffer.raw), inputIndex); |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 677 | inputIndex++; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 678 | phaseFraction -= phaseWrapLimit; |
| 679 | while (phaseFraction >= phaseWrapLimit) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 680 | if (inputIndex >= mBuffer.frameCount) { |
Andy Hung | 411cb8e | 2014-05-27 12:32:17 -0700 | [diff] [blame] | 681 | inputIndex = 0; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 682 | provider->releaseBuffer(&mBuffer); |
| 683 | break; |
| 684 | } |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 685 | mInBuffer.template readAdvance<CHANNELS>( |
| 686 | impulse, c.mHalfNumCoefs, |
| 687 | reinterpret_cast<TI*>(mBuffer.raw), inputIndex); |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 688 | inputIndex++; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 689 | phaseFraction -= phaseWrapLimit; |
| 690 | } |
| 691 | } |
| 692 | } |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 693 | const TI* const in = reinterpret_cast<const TI*>(mBuffer.raw); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 694 | const size_t frameCount = mBuffer.frameCount; |
| 695 | const int coefShift = c.mShift; |
| 696 | const int halfNumCoefs = c.mHalfNumCoefs; |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 697 | const TO* const volumeSimd = mVolumeSimd; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 698 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 699 | // main processing loop |
| 700 | while (CC_LIKELY(outputIndex < outputSampleCount)) { |
| 701 | // caution: fir() is inlined and may be large. |
| 702 | // output will be loaded with the appropriate values |
| 703 | // |
| 704 | // from the input samples in impulse[-halfNumCoefs+1]... impulse[halfNumCoefs] |
| 705 | // from the polyphase filter of (phaseFraction / phaseWrapLimit) in coefs. |
| 706 | // |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 707 | //ALOGV("LOOP2: inFrameCount:%d outputIndex:%d outFrameCount:%d" |
| 708 | // " phaseFraction:%u phaseWrapLimit:%u", |
| 709 | // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit); |
| 710 | ALOG_ASSERT(phaseFraction < phaseWrapLimit); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 711 | fir<CHANNELS, LOCKED, STRIDE>( |
| 712 | &out[outputIndex], |
| 713 | phaseFraction, phaseWrapLimit, |
| 714 | coefShift, halfNumCoefs, coefs, |
| 715 | impulse, volumeSimd); |
Andy Hung | 075abae | 2014-04-09 19:36:43 -0700 | [diff] [blame] | 716 | |
| 717 | outputIndex += OUTPUT_CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 718 | |
| 719 | phaseFraction += phaseIncrement; |
| 720 | while (phaseFraction >= phaseWrapLimit) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 721 | if (inputIndex >= frameCount) { |
| 722 | goto done; // need a new buffer |
| 723 | } |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 724 | mInBuffer.template readAdvance<CHANNELS>(impulse, halfNumCoefs, in, inputIndex); |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 725 | inputIndex++; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 726 | phaseFraction -= phaseWrapLimit; |
| 727 | } |
| 728 | } |
| 729 | done: |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 730 | // We arrive here when we're finished or when the input buffer runs out. |
| 731 | // Regardless we need to release the input buffer if we've acquired it. |
| 732 | if (inputIndex > 0) { // we've acquired a buffer (alternatively could check frameCount) |
Tobias Melin | 4348921 | 2016-09-16 10:04:26 +0200 | [diff] [blame] | 733 | ALOG_ASSERT(inputIndex == frameCount, "inputIndex(%zu) != frameCount(%zu)", |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 734 | inputIndex, frameCount); // must have been fully read. |
Andy Hung | 411cb8e | 2014-05-27 12:32:17 -0700 | [diff] [blame] | 735 | inputIndex = 0; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 736 | provider->releaseBuffer(&mBuffer); |
Andy Hung | 411cb8e | 2014-05-27 12:32:17 -0700 | [diff] [blame] | 737 | ALOG_ASSERT(mBuffer.frameCount == 0); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
| 741 | resample_exit: |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 742 | // inputIndex must be zero in all three cases: |
| 743 | // (1) the buffer never was been acquired; (2) the buffer was |
| 744 | // released at "done:"; or (3) getNextBuffer() failed. |
Tobias Melin | 4348921 | 2016-09-16 10:04:26 +0200 | [diff] [blame] | 745 | ALOG_ASSERT(inputIndex == 0, "Releasing: inputindex:%zu frameCount:%zu phaseFraction:%u", |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 746 | inputIndex, mBuffer.frameCount, phaseFraction); |
| 747 | ALOG_ASSERT(mBuffer.frameCount == 0); // there must be no frames in the buffer |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 748 | mInBuffer.setImpulse(impulse); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 749 | mPhaseFraction = phaseFraction; |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 750 | return outputIndex / OUTPUT_CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 751 | } |
| 752 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 753 | /* instantiate templates used by AudioResampler::create */ |
| 754 | template class AudioResamplerDyn<float, float, float>; |
| 755 | template class AudioResamplerDyn<int16_t, int16_t, int32_t>; |
| 756 | template class AudioResamplerDyn<int32_t, int16_t, int32_t>; |
| 757 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 758 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 759 | } // namespace android |