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> |
| 28 | #include <utils/Log.h> |
| 29 | |
| 30 | #include "AudioResamplerFirOps.h" // USE_NEON and USE_INLINE_ASSEMBLY defined here |
| 31 | #include "AudioResamplerFirProcess.h" |
| 32 | #include "AudioResamplerFirProcessNeon.h" |
| 33 | #include "AudioResamplerFirGen.h" // requires math.h |
| 34 | #include "AudioResamplerDyn.h" |
| 35 | |
| 36 | //#define DEBUG_RESAMPLER |
| 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | // generate a unique resample type compile-time constant (constexpr) |
| 41 | #define RESAMPLETYPE(CHANNELS, LOCKED, STRIDE, COEFTYPE) \ |
| 42 | ((((CHANNELS)-1)&1) | !!(LOCKED)<<1 | (COEFTYPE)<<2 \ |
| 43 | | ((STRIDE)==8 ? 1 : (STRIDE)==16 ? 2 : 0)<<3) |
| 44 | |
| 45 | /* |
| 46 | * InBuffer is a type agnostic input buffer. |
| 47 | * |
| 48 | * Layout of the state buffer for halfNumCoefs=8. |
| 49 | * |
| 50 | * [rrrrrrppppppppnnnnnnnnrrrrrrrrrrrrrrrrrrr.... rrrrrrr] |
| 51 | * S I R |
| 52 | * |
| 53 | * S = mState |
| 54 | * I = mImpulse |
| 55 | * R = mRingFull |
| 56 | * p = past samples, convoluted with the (p)ositive side of sinc() |
| 57 | * n = future samples, convoluted with the (n)egative side of sinc() |
| 58 | * r = extra space for implementing the ring buffer |
| 59 | */ |
| 60 | |
| 61 | template<typename TI> |
| 62 | AudioResamplerDyn::InBuffer<TI>::InBuffer() |
| 63 | : mState(NULL), mImpulse(NULL), mRingFull(NULL), mStateSize(0) { |
| 64 | } |
| 65 | |
| 66 | template<typename TI> |
| 67 | AudioResamplerDyn::InBuffer<TI>::~InBuffer() { |
| 68 | init(); |
| 69 | } |
| 70 | |
| 71 | template<typename TI> |
| 72 | void AudioResamplerDyn::InBuffer<TI>::init() { |
| 73 | free(mState); |
| 74 | mState = NULL; |
| 75 | mImpulse = NULL; |
| 76 | mRingFull = NULL; |
| 77 | mStateSize = 0; |
| 78 | } |
| 79 | |
| 80 | // resizes the state buffer to accommodate the appropriate filter length |
| 81 | template<typename TI> |
| 82 | void AudioResamplerDyn::InBuffer<TI>::resize(int CHANNELS, int halfNumCoefs) { |
| 83 | // calculate desired state size |
| 84 | int stateSize = halfNumCoefs * CHANNELS * 2 |
| 85 | * kStateSizeMultipleOfFilterLength; |
| 86 | |
| 87 | // check if buffer needs resizing |
| 88 | if (mState |
| 89 | && stateSize == mStateSize |
| 90 | && mRingFull-mState == mStateSize-halfNumCoefs*CHANNELS) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | // create new buffer |
| 95 | TI* state = (int16_t*)memalign(32, stateSize*sizeof(*state)); |
| 96 | memset(state, 0, stateSize*sizeof(*state)); |
| 97 | |
| 98 | // attempt to preserve state |
| 99 | if (mState) { |
| 100 | TI* srcLo = mImpulse - halfNumCoefs*CHANNELS; |
| 101 | TI* srcHi = mImpulse + halfNumCoefs*CHANNELS; |
| 102 | TI* dst = state; |
| 103 | |
| 104 | if (srcLo < mState) { |
| 105 | dst += mState-srcLo; |
| 106 | srcLo = mState; |
| 107 | } |
| 108 | if (srcHi > mState + mStateSize) { |
| 109 | srcHi = mState + mStateSize; |
| 110 | } |
| 111 | memcpy(dst, srcLo, (srcHi - srcLo) * sizeof(*srcLo)); |
| 112 | free(mState); |
| 113 | } |
| 114 | |
| 115 | // set class member vars |
| 116 | mState = state; |
| 117 | mStateSize = stateSize; |
| 118 | mImpulse = mState + halfNumCoefs*CHANNELS; // actually one sample greater than needed |
| 119 | mRingFull = mState + mStateSize - halfNumCoefs*CHANNELS; |
| 120 | } |
| 121 | |
| 122 | // copy in the input data into the head (impulse+halfNumCoefs) of the buffer. |
| 123 | template<typename TI> |
| 124 | template<int CHANNELS> |
| 125 | void AudioResamplerDyn::InBuffer<TI>::readAgain(TI*& impulse, const int halfNumCoefs, |
| 126 | const TI* const in, const size_t inputIndex) { |
| 127 | int16_t* head = impulse + halfNumCoefs*CHANNELS; |
| 128 | for (size_t i=0 ; i<CHANNELS ; i++) { |
| 129 | head[i] = in[inputIndex*CHANNELS + i]; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // advance the impulse pointer, and load in data into the head (impulse+halfNumCoefs) |
| 134 | template<typename TI> |
| 135 | template<int CHANNELS> |
| 136 | void AudioResamplerDyn::InBuffer<TI>::readAdvance(TI*& impulse, const int halfNumCoefs, |
| 137 | const TI* const in, const size_t inputIndex) { |
| 138 | impulse += CHANNELS; |
| 139 | |
| 140 | if (CC_UNLIKELY(impulse >= mRingFull)) { |
| 141 | const size_t shiftDown = mRingFull - mState - halfNumCoefs*CHANNELS; |
| 142 | memcpy(mState, mState+shiftDown, halfNumCoefs*CHANNELS*2*sizeof(TI)); |
| 143 | impulse -= shiftDown; |
| 144 | } |
| 145 | readAgain<CHANNELS>(impulse, halfNumCoefs, in, inputIndex); |
| 146 | } |
| 147 | |
| 148 | void AudioResamplerDyn::Constants::set( |
| 149 | int L, int halfNumCoefs, int inSampleRate, int outSampleRate) |
| 150 | { |
| 151 | int bits = 0; |
| 152 | int lscale = inSampleRate/outSampleRate < 2 ? L - 1 : |
| 153 | static_cast<int>(static_cast<uint64_t>(L)*inSampleRate/outSampleRate); |
| 154 | for (int i=lscale; i; ++bits, i>>=1) |
| 155 | ; |
| 156 | mL = L; |
| 157 | mShift = kNumPhaseBits - bits; |
| 158 | mHalfNumCoefs = halfNumCoefs; |
| 159 | } |
| 160 | |
| 161 | AudioResamplerDyn::AudioResamplerDyn(int bitDepth, |
| 162 | int inChannelCount, int32_t sampleRate, src_quality quality) |
| 163 | : AudioResampler(bitDepth, inChannelCount, sampleRate, quality), |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 164 | mResampleType(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY), |
| 165 | mCoefBuffer(NULL) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 166 | { |
| 167 | mVolumeSimd[0] = mVolumeSimd[1] = 0; |
| 168 | mConstants.set(128, 8, mSampleRate, mSampleRate); // TODO: set better |
| 169 | } |
| 170 | |
| 171 | AudioResamplerDyn::~AudioResamplerDyn() { |
| 172 | free(mCoefBuffer); |
| 173 | } |
| 174 | |
| 175 | void AudioResamplerDyn::init() { |
| 176 | mFilterSampleRate = 0; // always trigger new filter generation |
| 177 | mInBuffer.init(); |
| 178 | } |
| 179 | |
| 180 | void AudioResamplerDyn::setVolume(int16_t left, int16_t right) { |
| 181 | AudioResampler::setVolume(left, right); |
| 182 | mVolumeSimd[0] = static_cast<int32_t>(left)<<16; |
| 183 | mVolumeSimd[1] = static_cast<int32_t>(right)<<16; |
| 184 | } |
| 185 | |
| 186 | template <typename T> T max(T a, T b) {return a > b ? a : b;} |
| 187 | |
| 188 | template <typename T> T absdiff(T a, T b) {return a > b ? a - b : b - a;} |
| 189 | |
| 190 | template<typename T> |
| 191 | void AudioResamplerDyn::createKaiserFir(Constants &c, double stopBandAtten, |
| 192 | int inSampleRate, int outSampleRate, double tbwCheat) { |
| 193 | T* buf = reinterpret_cast<T*>(memalign(32, (c.mL+1)*c.mHalfNumCoefs*sizeof(T))); |
| 194 | static const double atten = 0.9998; // to avoid ripple overflow |
| 195 | double fcr; |
| 196 | double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten); |
| 197 | |
| 198 | if (inSampleRate < outSampleRate) { // upsample |
| 199 | fcr = max(0.5*tbwCheat - tbw/2, tbw/2); |
| 200 | } else { // downsample |
| 201 | fcr = max(0.5*tbwCheat*outSampleRate/inSampleRate - tbw/2, tbw/2); |
| 202 | } |
| 203 | // create and set filter |
| 204 | firKaiserGen(buf, c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten); |
| 205 | c.setBuf(buf); |
| 206 | if (mCoefBuffer) { |
| 207 | free(mCoefBuffer); |
| 208 | } |
| 209 | mCoefBuffer = buf; |
| 210 | #ifdef DEBUG_RESAMPLER |
| 211 | // print basic filter stats |
| 212 | printf("L:%d hnc:%d stopBandAtten:%lf fcr:%lf atten:%lf tbw:%lf\n", |
| 213 | c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten, tbw); |
| 214 | // test the filter and report results |
| 215 | double fp = (fcr - tbw/2)/c.mL; |
| 216 | double fs = (fcr + tbw/2)/c.mL; |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 217 | double passMin, passMax, passRipple; |
| 218 | double stopMax, stopRipple; |
| 219 | testFir(buf, c.mL, c.mHalfNumCoefs, fp, fs, /*passSteps*/ 1000, /*stopSteps*/ 100000, |
| 220 | passMin, passMax, passRipple, stopMax, stopRipple); |
| 221 | printf("passband(%lf, %lf): %.8lf %.8lf %.8lf\n", 0., fp, passMin, passMax, passRipple); |
| 222 | printf("stopband(%lf, %lf): %.8lf %.3lf\n", fs, 0.5, stopMax, stopRipple); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 223 | #endif |
| 224 | } |
| 225 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 226 | // recursive gcd. Using objdump, it appears the tail recursion is converted to a while loop. |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 227 | static int gcd(int n, int m) { |
| 228 | if (m == 0) { |
| 229 | return n; |
| 230 | } |
| 231 | return gcd(m, n % m); |
| 232 | } |
| 233 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 234 | static bool isClose(int32_t newSampleRate, int32_t prevSampleRate, |
| 235 | int32_t filterSampleRate, int32_t outSampleRate) { |
| 236 | |
| 237 | // different upsampling ratios do not need a filter change. |
| 238 | if (filterSampleRate != 0 |
| 239 | && filterSampleRate < outSampleRate |
| 240 | && newSampleRate < outSampleRate) |
| 241 | return true; |
| 242 | |
| 243 | // check design criteria again if downsampling is detected. |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 244 | int pdiff = absdiff(newSampleRate, prevSampleRate); |
| 245 | int adiff = absdiff(newSampleRate, filterSampleRate); |
| 246 | |
| 247 | // allow up to 6% relative change increments. |
| 248 | // allow up to 12% absolute change increments (from filter design) |
| 249 | return pdiff < prevSampleRate>>4 && adiff < filterSampleRate>>3; |
| 250 | } |
| 251 | |
| 252 | void AudioResamplerDyn::setSampleRate(int32_t inSampleRate) { |
| 253 | if (mInSampleRate == inSampleRate) { |
| 254 | return; |
| 255 | } |
| 256 | int32_t oldSampleRate = mInSampleRate; |
| 257 | int32_t oldHalfNumCoefs = mConstants.mHalfNumCoefs; |
| 258 | uint32_t oldPhaseWrapLimit = mConstants.mL << mConstants.mShift; |
| 259 | bool useS32 = false; |
| 260 | |
| 261 | mInSampleRate = inSampleRate; |
| 262 | |
| 263 | // TODO: Add precalculated Equiripple filters |
| 264 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 265 | if (mFilterQuality != getQuality() || |
| 266 | !isClose(inSampleRate, oldSampleRate, mFilterSampleRate, mSampleRate)) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 267 | mFilterSampleRate = inSampleRate; |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 268 | mFilterQuality = getQuality(); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 269 | |
| 270 | // Begin Kaiser Filter computation |
| 271 | // |
| 272 | // The quantization floor for S16 is about 96db - 10*log_10(#length) + 3dB. |
| 273 | // Keep the stop band attenuation no greater than 84-85dB for 32 length S16 filters |
| 274 | // |
| 275 | // For s32 we keep the stop band attenuation at the same as 16b resolution, about |
| 276 | // 96-98dB |
| 277 | // |
| 278 | |
| 279 | double stopBandAtten; |
| 280 | double tbwCheat = 1.; // how much we "cheat" into aliasing |
| 281 | int halfLength; |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 282 | if (mFilterQuality == DYN_HIGH_QUALITY) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 283 | // 32b coefficients, 64 length |
| 284 | useS32 = true; |
| 285 | stopBandAtten = 98.; |
Andy Hung | a3bb9a3 | 2014-02-10 15:00:16 -0800 | [diff] [blame] | 286 | if (inSampleRate >= mSampleRate * 4) { |
| 287 | halfLength = 48; |
| 288 | } else if (inSampleRate >= mSampleRate * 2) { |
| 289 | halfLength = 40; |
| 290 | } else { |
| 291 | halfLength = 32; |
| 292 | } |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 293 | } else if (mFilterQuality == DYN_LOW_QUALITY) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 294 | // 16b coefficients, 16-32 length |
| 295 | useS32 = false; |
| 296 | stopBandAtten = 80.; |
Andy Hung | a3bb9a3 | 2014-02-10 15:00:16 -0800 | [diff] [blame] | 297 | if (inSampleRate >= mSampleRate * 4) { |
| 298 | halfLength = 24; |
| 299 | } else if (inSampleRate >= mSampleRate * 2) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 300 | halfLength = 16; |
| 301 | } else { |
| 302 | halfLength = 8; |
| 303 | } |
Andy Hung | a3bb9a3 | 2014-02-10 15:00:16 -0800 | [diff] [blame] | 304 | if (inSampleRate <= mSampleRate) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 305 | tbwCheat = 1.05; |
| 306 | } else { |
| 307 | tbwCheat = 1.03; |
| 308 | } |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 309 | } else { // DYN_MED_QUALITY |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 310 | // 16b coefficients, 32-64 length |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 311 | // note: > 64 length filters with 16b coefs can have quantization noise problems |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 312 | useS32 = false; |
| 313 | stopBandAtten = 84.; |
Andy Hung | a3bb9a3 | 2014-02-10 15:00:16 -0800 | [diff] [blame] | 314 | if (inSampleRate >= mSampleRate * 4) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 315 | halfLength = 32; |
Andy Hung | a3bb9a3 | 2014-02-10 15:00:16 -0800 | [diff] [blame] | 316 | } else if (inSampleRate >= mSampleRate * 2) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 317 | halfLength = 24; |
| 318 | } else { |
| 319 | halfLength = 16; |
| 320 | } |
Andy Hung | a3bb9a3 | 2014-02-10 15:00:16 -0800 | [diff] [blame] | 321 | if (inSampleRate <= mSampleRate) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 322 | tbwCheat = 1.03; |
| 323 | } else { |
| 324 | tbwCheat = 1.01; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | // determine the number of polyphases in the filterbank. |
| 329 | // for 16b, it is desirable to have 2^(16/2) = 256 phases. |
| 330 | // https://ccrma.stanford.edu/~jos/resample/Relation_Interpolation_Error_Quantization.html |
| 331 | // |
| 332 | // We are a bit more lax on this. |
| 333 | |
| 334 | int phases = mSampleRate / gcd(mSampleRate, inSampleRate); |
| 335 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 336 | // TODO: Once dynamic sample rate change is an option, the code below |
| 337 | // should be modified to execute only when dynamic sample rate change is enabled. |
| 338 | // |
| 339 | // as above, #phases less than 63 is too few phases for accurate linear interpolation. |
| 340 | // we increase the phases to compensate, but more phases means more memory per |
| 341 | // filter and more time to compute the filter. |
| 342 | // |
| 343 | // if we know that the filter will be used for dynamic sample rate changes, |
| 344 | // that would allow us skip this part for fixed sample rate resamplers. |
| 345 | // |
| 346 | while (phases<63) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 347 | phases *= 2; // this code only needed to support dynamic rate changes |
| 348 | } |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 349 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 350 | if (phases>=256) { // too many phases, always interpolate |
| 351 | phases = 127; |
| 352 | } |
| 353 | |
| 354 | // create the filter |
| 355 | mConstants.set(phases, halfLength, inSampleRate, mSampleRate); |
| 356 | if (useS32) { |
| 357 | createKaiserFir<int32_t>(mConstants, stopBandAtten, |
| 358 | inSampleRate, mSampleRate, tbwCheat); |
| 359 | } else { |
| 360 | createKaiserFir<int16_t>(mConstants, stopBandAtten, |
| 361 | inSampleRate, mSampleRate, tbwCheat); |
| 362 | } |
| 363 | } // End Kaiser filter |
| 364 | |
| 365 | // update phase and state based on the new filter. |
| 366 | const Constants& c(mConstants); |
| 367 | mInBuffer.resize(mChannelCount, c.mHalfNumCoefs); |
| 368 | const uint32_t phaseWrapLimit = c.mL << c.mShift; |
| 369 | // try to preserve as much of the phase fraction as possible for on-the-fly changes |
| 370 | mPhaseFraction = static_cast<unsigned long long>(mPhaseFraction) |
| 371 | * phaseWrapLimit / oldPhaseWrapLimit; |
| 372 | mPhaseFraction %= phaseWrapLimit; // should not do anything, but just in case. |
| 373 | mPhaseIncrement = static_cast<uint32_t>(static_cast<double>(phaseWrapLimit) |
| 374 | * inSampleRate / mSampleRate); |
| 375 | |
| 376 | // determine which resampler to use |
| 377 | // check if locked phase (works only if mPhaseIncrement has no "fractional phase bits") |
| 378 | int locked = (mPhaseIncrement << (sizeof(mPhaseIncrement)*8 - c.mShift)) == 0; |
| 379 | int stride = (c.mHalfNumCoefs&7)==0 ? 16 : (c.mHalfNumCoefs&3)==0 ? 8 : 2; |
| 380 | if (locked) { |
| 381 | mPhaseFraction = mPhaseFraction >> c.mShift << c.mShift; // remove fractional phase |
| 382 | } |
Andy Hung | 83be256 | 2014-02-03 14:11:09 -0800 | [diff] [blame] | 383 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 384 | mResampleType = RESAMPLETYPE(mChannelCount, locked, stride, !!useS32); |
| 385 | #ifdef DEBUG_RESAMPLER |
| 386 | printf("channels:%d %s stride:%d %s coef:%d shift:%d\n", |
| 387 | mChannelCount, locked ? "locked" : "interpolated", |
| 388 | stride, useS32 ? "S32" : "S16", 2*c.mHalfNumCoefs, c.mShift); |
| 389 | #endif |
| 390 | } |
| 391 | |
| 392 | void AudioResamplerDyn::resample(int32_t* out, size_t outFrameCount, |
| 393 | AudioBufferProvider* provider) |
| 394 | { |
| 395 | // TODO: |
| 396 | // 24 cases - this perhaps can be reduced later, as testing might take too long |
| 397 | switch (mResampleType) { |
| 398 | |
Andy Hung | 83be256 | 2014-02-03 14:11:09 -0800 | [diff] [blame] | 399 | // stride 16 (falls back to stride 2 for machines that do not support NEON) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 400 | case RESAMPLETYPE(1, true, 16, 0): |
| 401 | return resample<1, true, 16>(out, outFrameCount, mConstants.mFirCoefsS16, provider); |
| 402 | case RESAMPLETYPE(2, true, 16, 0): |
| 403 | return resample<2, true, 16>(out, outFrameCount, mConstants.mFirCoefsS16, provider); |
| 404 | case RESAMPLETYPE(1, false, 16, 0): |
| 405 | return resample<1, false, 16>(out, outFrameCount, mConstants.mFirCoefsS16, provider); |
| 406 | case RESAMPLETYPE(2, false, 16, 0): |
| 407 | return resample<2, false, 16>(out, outFrameCount, mConstants.mFirCoefsS16, provider); |
| 408 | case RESAMPLETYPE(1, true, 16, 1): |
| 409 | return resample<1, true, 16>(out, outFrameCount, mConstants.mFirCoefsS32, provider); |
| 410 | case RESAMPLETYPE(2, true, 16, 1): |
| 411 | return resample<2, true, 16>(out, outFrameCount, mConstants.mFirCoefsS32, provider); |
| 412 | case RESAMPLETYPE(1, false, 16, 1): |
| 413 | return resample<1, false, 16>(out, outFrameCount, mConstants.mFirCoefsS32, provider); |
| 414 | case RESAMPLETYPE(2, false, 16, 1): |
| 415 | return resample<2, false, 16>(out, outFrameCount, mConstants.mFirCoefsS32, provider); |
| 416 | #if 0 |
| 417 | // TODO: Remove these? |
| 418 | // stride 8 |
| 419 | case RESAMPLETYPE(1, true, 8, 0): |
| 420 | return resample<1, true, 8>(out, outFrameCount, mConstants.mFirCoefsS16, provider); |
| 421 | case RESAMPLETYPE(2, true, 8, 0): |
| 422 | return resample<2, true, 8>(out, outFrameCount, mConstants.mFirCoefsS16, provider); |
| 423 | case RESAMPLETYPE(1, false, 8, 0): |
| 424 | return resample<1, false, 8>(out, outFrameCount, mConstants.mFirCoefsS16, provider); |
| 425 | case RESAMPLETYPE(2, false, 8, 0): |
| 426 | return resample<2, false, 8>(out, outFrameCount, mConstants.mFirCoefsS16, provider); |
| 427 | case RESAMPLETYPE(1, true, 8, 1): |
| 428 | return resample<1, true, 8>(out, outFrameCount, mConstants.mFirCoefsS32, provider); |
| 429 | case RESAMPLETYPE(2, true, 8, 1): |
| 430 | return resample<2, true, 8>(out, outFrameCount, mConstants.mFirCoefsS32, provider); |
| 431 | case RESAMPLETYPE(1, false, 8, 1): |
| 432 | return resample<1, false, 8>(out, outFrameCount, mConstants.mFirCoefsS32, provider); |
| 433 | case RESAMPLETYPE(2, false, 8, 1): |
| 434 | return resample<2, false, 8>(out, outFrameCount, mConstants.mFirCoefsS32, provider); |
| 435 | // stride 2 (can handle any filter length) |
| 436 | case RESAMPLETYPE(1, true, 2, 0): |
| 437 | return resample<1, true, 2>(out, outFrameCount, mConstants.mFirCoefsS16, provider); |
| 438 | case RESAMPLETYPE(2, true, 2, 0): |
| 439 | return resample<2, true, 2>(out, outFrameCount, mConstants.mFirCoefsS16, provider); |
| 440 | case RESAMPLETYPE(1, false, 2, 0): |
| 441 | return resample<1, false, 2>(out, outFrameCount, mConstants.mFirCoefsS16, provider); |
| 442 | case RESAMPLETYPE(2, false, 2, 0): |
| 443 | return resample<2, false, 2>(out, outFrameCount, mConstants.mFirCoefsS16, provider); |
| 444 | case RESAMPLETYPE(1, true, 2, 1): |
| 445 | return resample<1, true, 2>(out, outFrameCount, mConstants.mFirCoefsS32, provider); |
| 446 | case RESAMPLETYPE(2, true, 2, 1): |
| 447 | return resample<2, true, 2>(out, outFrameCount, mConstants.mFirCoefsS32, provider); |
| 448 | case RESAMPLETYPE(1, false, 2, 1): |
| 449 | return resample<1, false, 2>(out, outFrameCount, mConstants.mFirCoefsS32, provider); |
| 450 | case RESAMPLETYPE(2, false, 2, 1): |
| 451 | return resample<2, false, 2>(out, outFrameCount, mConstants.mFirCoefsS32, provider); |
| 452 | #endif |
| 453 | default: |
| 454 | ; // error |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | template<int CHANNELS, bool LOCKED, int STRIDE, typename TC> |
| 459 | void AudioResamplerDyn::resample(int32_t* out, size_t outFrameCount, |
| 460 | const TC* const coefs, AudioBufferProvider* provider) |
| 461 | { |
| 462 | const Constants& c(mConstants); |
| 463 | int16_t* impulse = mInBuffer.getImpulse(); |
| 464 | size_t inputIndex = mInputIndex; |
| 465 | uint32_t phaseFraction = mPhaseFraction; |
| 466 | const uint32_t phaseIncrement = mPhaseIncrement; |
| 467 | size_t outputIndex = 0; |
| 468 | size_t outputSampleCount = outFrameCount * 2; // stereo output |
| 469 | size_t inFrameCount = (outFrameCount*mInSampleRate)/mSampleRate; |
| 470 | const uint32_t phaseWrapLimit = c.mL << c.mShift; |
| 471 | |
| 472 | // NOTE: be very careful when modifying the code here. register |
| 473 | // pressure is very high and a small change might cause the compiler |
| 474 | // to generate far less efficient code. |
| 475 | // Always sanity check the result with objdump or test-resample. |
| 476 | |
| 477 | // the following logic is a bit convoluted to keep the main processing loop |
| 478 | // as tight as possible with register allocation. |
| 479 | while (outputIndex < outputSampleCount) { |
| 480 | // buffer is empty, fetch a new one |
| 481 | while (mBuffer.frameCount == 0) { |
| 482 | mBuffer.frameCount = inFrameCount; |
| 483 | provider->getNextBuffer(&mBuffer, |
| 484 | calculateOutputPTS(outputIndex / 2)); |
| 485 | if (mBuffer.raw == NULL) { |
| 486 | goto resample_exit; |
| 487 | } |
| 488 | if (phaseFraction >= phaseWrapLimit) { // read in data |
| 489 | mInBuffer.readAdvance<CHANNELS>( |
| 490 | impulse, c.mHalfNumCoefs, mBuffer.i16, inputIndex); |
| 491 | phaseFraction -= phaseWrapLimit; |
| 492 | while (phaseFraction >= phaseWrapLimit) { |
| 493 | inputIndex++; |
| 494 | if (inputIndex >= mBuffer.frameCount) { |
| 495 | inputIndex -= mBuffer.frameCount; |
| 496 | provider->releaseBuffer(&mBuffer); |
| 497 | break; |
| 498 | } |
| 499 | mInBuffer.readAdvance<CHANNELS>( |
| 500 | impulse, c.mHalfNumCoefs, mBuffer.i16, inputIndex); |
| 501 | phaseFraction -= phaseWrapLimit; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | const int16_t* const in = mBuffer.i16; |
| 506 | const size_t frameCount = mBuffer.frameCount; |
| 507 | const int coefShift = c.mShift; |
| 508 | const int halfNumCoefs = c.mHalfNumCoefs; |
| 509 | const int32_t* const volumeSimd = mVolumeSimd; |
| 510 | |
| 511 | // reread the last input in. |
| 512 | mInBuffer.readAgain<CHANNELS>(impulse, halfNumCoefs, in, inputIndex); |
| 513 | |
| 514 | // main processing loop |
| 515 | while (CC_LIKELY(outputIndex < outputSampleCount)) { |
| 516 | // caution: fir() is inlined and may be large. |
| 517 | // output will be loaded with the appropriate values |
| 518 | // |
| 519 | // from the input samples in impulse[-halfNumCoefs+1]... impulse[halfNumCoefs] |
| 520 | // from the polyphase filter of (phaseFraction / phaseWrapLimit) in coefs. |
| 521 | // |
| 522 | fir<CHANNELS, LOCKED, STRIDE>( |
| 523 | &out[outputIndex], |
| 524 | phaseFraction, phaseWrapLimit, |
| 525 | coefShift, halfNumCoefs, coefs, |
| 526 | impulse, volumeSimd); |
| 527 | outputIndex += 2; |
| 528 | |
| 529 | phaseFraction += phaseIncrement; |
| 530 | while (phaseFraction >= phaseWrapLimit) { |
| 531 | inputIndex++; |
| 532 | if (inputIndex >= frameCount) { |
| 533 | goto done; // need a new buffer |
| 534 | } |
| 535 | mInBuffer.readAdvance<CHANNELS>(impulse, halfNumCoefs, in, inputIndex); |
| 536 | phaseFraction -= phaseWrapLimit; |
| 537 | } |
| 538 | } |
| 539 | done: |
| 540 | // often arrives here when input buffer runs out |
| 541 | if (inputIndex >= frameCount) { |
| 542 | inputIndex -= frameCount; |
| 543 | provider->releaseBuffer(&mBuffer); |
| 544 | // mBuffer.frameCount MUST be zero here. |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | resample_exit: |
| 549 | mInBuffer.setImpulse(impulse); |
| 550 | mInputIndex = inputIndex; |
| 551 | mPhaseFraction = phaseFraction; |
| 552 | } |
| 553 | |
| 554 | // ---------------------------------------------------------------------------- |
| 555 | }; // namespace android |