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