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/Debug.h>
29 #include <utils/Log.h>
30 #include <audio_utils/primitives.h>
31 
32 #include "AudioResamplerFirOps.h" // USE_NEON, USE_SSE and USE_INLINE_ASSEMBLY defined here
33 #include "AudioResamplerFirProcess.h"
34 #include "AudioResamplerFirProcessNeon.h"
35 #include "AudioResamplerFirProcessSSE.h"
36 #include "AudioResamplerFirGen.h" // requires math.h
37 #include "AudioResamplerDyn.h"
38 
39 //#define DEBUG_RESAMPLER
40 
41 // use this for our buffer alignment.  Should be at least 32 bytes.
42 constexpr size_t CACHE_LINE_SIZE = 64;
43 
44 namespace android {
45 
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 
62 template<typename TC, typename TI, typename TO>
InBuffer()63 AudioResamplerDyn<TC, TI, TO>::InBuffer::InBuffer()
64     : mState(NULL), mImpulse(NULL), mRingFull(NULL), mStateCount(0)
65 {
66 }
67 
68 template<typename TC, typename TI, typename TO>
~InBuffer()69 AudioResamplerDyn<TC, TI, TO>::InBuffer::~InBuffer()
70 {
71     init();
72 }
73 
74 template<typename TC, typename TI, typename TO>
init()75 void AudioResamplerDyn<TC, TI, TO>::InBuffer::init()
76 {
77     free(mState);
78     mState = NULL;
79     mImpulse = NULL;
80     mRingFull = NULL;
81     mStateCount = 0;
82 }
83 
84 // resizes the state buffer to accommodate the appropriate filter length
85 template<typename TC, typename TI, typename TO>
resize(int CHANNELS,int halfNumCoefs)86 void AudioResamplerDyn<TC, TI, TO>::InBuffer::resize(int CHANNELS, int halfNumCoefs)
87 {
88     // calculate desired state size
89     size_t stateCount = halfNumCoefs * CHANNELS * 2 * kStateSizeMultipleOfFilterLength;
90 
91     // check if buffer needs resizing
92     if (mState
93             && stateCount == mStateCount
94             && mRingFull-mState == (ssize_t) (mStateCount-halfNumCoefs*CHANNELS)) {
95         return;
96     }
97 
98     // create new buffer
99     TI* state = NULL;
100     (void)posix_memalign(
101             reinterpret_cast<void **>(&state),
102             CACHE_LINE_SIZE /* alignment */,
103             stateCount * sizeof(*state));
104     memset(state, 0, stateCount*sizeof(*state));
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         }
116         if (srcHi > mState + mStateCount) {
117             srcHi = mState + mStateCount;
118         }
119         memcpy(dst, srcLo, (srcHi - srcLo) * sizeof(*srcLo));
120         free(mState);
121     }
122 
123     // set class member vars
124     mState = state;
125     mStateCount = stateCount;
126     mImpulse = state + halfNumCoefs*CHANNELS; // actually one sample greater than needed
127     mRingFull = state + mStateCount - halfNumCoefs*CHANNELS;
128 }
129 
130 // copy in the input data into the head (impulse+halfNumCoefs) of the buffer.
131 template<typename TC, typename TI, typename TO>
132 template<int CHANNELS>
readAgain(TI * & impulse,const int halfNumCoefs,const TI * const in,const size_t inputIndex)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;
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)
143 template<typename TC, typename TI, typename TO>
144 template<int CHANNELS>
readAdvance(TI * & impulse,const int halfNumCoefs,const TI * const in,const size_t inputIndex)145 void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAdvance(TI*& impulse, const int halfNumCoefs,
146         const TI* const in, const size_t inputIndex)
147 {
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 
158 template<typename TC, typename TI, typename TO>
reset()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>
set(int L,int halfNumCoefs,int inSampleRate,int outSampleRate)168 void AudioResamplerDyn<TC, TI, TO>::Constants::set(
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 
181 template<typename TC, typename TI, typename TO>
AudioResamplerDyn(int inChannelCount,int32_t sampleRate,src_quality quality)182 AudioResamplerDyn<TC, TI, TO>::AudioResamplerDyn(
183         int inChannelCount, int32_t sampleRate, src_quality quality)
184     : AudioResampler(inChannelCount, sampleRate, quality),
185       mResampleFunc(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY),
186     mCoefBuffer(NULL)
187 {
188     mVolumeSimd[0] = mVolumeSimd[1] = 0;
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;
193     mConstants.set(128, 8, mSampleRate, mSampleRate); // TODO: set better
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);
204 }
205 
206 template<typename TC, typename TI, typename TO>
~AudioResamplerDyn()207 AudioResamplerDyn<TC, TI, TO>::~AudioResamplerDyn()
208 {
209     free(mCoefBuffer);
210 }
211 
212 template<typename TC, typename TI, typename TO>
init()213 void AudioResamplerDyn<TC, TI, TO>::init()
214 {
215     mFilterSampleRate = 0; // always trigger new filter generation
216     mInBuffer.init();
217 }
218 
219 template<typename TC, typename TI, typename TO>
setVolume(float left,float right)220 void AudioResamplerDyn<TC, TI, TO>::setVolume(float left, float right)
221 {
222     AudioResampler::setVolume(left, right);
223     if (is_same<TO, float>::value || is_same<TO, double>::value) {
224         mVolumeSimd[0] = static_cast<TO>(left);
225         mVolumeSimd[1] = static_cast<TO>(right);
226     } else {  // integer requires scaling to U4_28 (rounding down)
227         // integer volumes are clamped to 0 to UNITY_GAIN so there
228         // are no issues with signed overflow.
229         mVolumeSimd[0] = u4_28_from_float(clampFloatVol(left));
230         mVolumeSimd[1] = u4_28_from_float(clampFloatVol(right));
231     }
232 }
233 
234 // TODO: update to C++11
235 
max(T a,T b)236 template<typename T> T max(T a, T b) {return a > b ? a : b;}
237 
absdiff(T a,T b)238 template<typename T> T absdiff(T a, T b) {return a > b ? a - b : b - a;}
239 
240 template<typename TC, typename TI, typename TO>
createKaiserFir(Constants & c,double stopBandAtten,int inSampleRate,int outSampleRate,double tbwCheat)241 void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c,
242         double stopBandAtten, int inSampleRate, int outSampleRate, double tbwCheat)
243 {
244     // compute the normalized transition bandwidth
245     const double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten);
246     const double halfbw = tbw / 2.;
247 
248     double fcr; // compute fcr, the 3 dB amplitude cut-off.
249     if (inSampleRate < outSampleRate) { // upsample
250         fcr = max(0.5 * tbwCheat - halfbw, halfbw);
251     } else { // downsample
252         fcr = max(0.5 * tbwCheat * outSampleRate / inSampleRate - halfbw, halfbw);
253     }
254     createKaiserFir(c, stopBandAtten, fcr);
255 }
256 
257 template<typename TC, typename TI, typename TO>
createKaiserFir(Constants & c,double stopBandAtten,double fcr)258 void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c,
259         double stopBandAtten, double fcr) {
260     // compute the normalized transition bandwidth
261     const double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten);
262     const int phases = c.mL;
263     const int halfLength = c.mHalfNumCoefs;
264 
265     // create buffer
266     TC *coefs = nullptr;
267     int ret = posix_memalign(
268             reinterpret_cast<void **>(&coefs),
269             CACHE_LINE_SIZE /* alignment */,
270             (phases + 1) * halfLength * sizeof(TC));
271     LOG_ALWAYS_FATAL_IF(ret != 0, "Cannot allocate buffer memory, ret %d", ret);
272     c.mFirCoefs = coefs;
273     free(mCoefBuffer);
274     mCoefBuffer = coefs;
275 
276     // square the computed minimum passband value (extra safety).
277     double attenuation =
278             computeWindowedSincMinimumPassbandValue(stopBandAtten);
279     attenuation *= attenuation;
280 
281     // design filter
282     firKaiserGen(coefs, phases, halfLength, stopBandAtten, fcr, attenuation);
283 
284     // update the design criteria
285     mNormalizedCutoffFrequency = fcr;
286     mNormalizedTransitionBandwidth = tbw;
287     mFilterAttenuation = attenuation;
288     mStopbandAttenuationDb = stopBandAtten;
289     mPassbandRippleDb = computeWindowedSincPassbandRippleDb(stopBandAtten);
290 
291 #if 0
292     // Keep this debug code in case an app causes resampler design issues.
293     const double halfbw = tbw / 2.;
294     // print basic filter stats
295     ALOGD("L:%d  hnc:%d  stopBandAtten:%lf  fcr:%lf  atten:%lf  tbw:%lf\n",
296             c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, attenuation, tbw);
297 
298     // test the filter and report results.
299     // Since this is a polyphase filter, normalized fp and fs must be scaled.
300     const double fp = (fcr - halfbw) / phases;
301     const double fs = (fcr + halfbw) / phases;
302 
303     double passMin, passMax, passRipple;
304     double stopMax, stopRipple;
305 
306     const int32_t passSteps = 1000;
307 
308     testFir(coefs, c.mL, c.mHalfNumCoefs, fp, fs, passSteps, passSteps * c.ML /*stopSteps*/,
309             passMin, passMax, passRipple, stopMax, stopRipple);
310     ALOGD("passband(%lf, %lf): %.8lf %.8lf %.8lf\n", 0., fp, passMin, passMax, passRipple);
311     ALOGD("stopband(%lf, %lf): %.8lf %.3lf\n", fs, 0.5, stopMax, stopRipple);
312 #endif
313 }
314 
315 // recursive gcd. Using objdump, it appears the tail recursion is converted to a while loop.
gcd(int n,int m)316 static int gcd(int n, int m)
317 {
318     if (m == 0) {
319         return n;
320     }
321     return gcd(m, n % m);
322 }
323 
isClose(int32_t newSampleRate,int32_t prevSampleRate,int32_t filterSampleRate,int32_t outSampleRate)324 static bool isClose(int32_t newSampleRate, int32_t prevSampleRate,
325         int32_t filterSampleRate, int32_t outSampleRate)
326 {
327 
328     // different upsampling ratios do not need a filter change.
329     if (filterSampleRate != 0
330             && filterSampleRate < outSampleRate
331             && newSampleRate < outSampleRate)
332         return true;
333 
334     // check design criteria again if downsampling is detected.
335     int pdiff = absdiff(newSampleRate, prevSampleRate);
336     int adiff = absdiff(newSampleRate, filterSampleRate);
337 
338     // allow up to 6% relative change increments.
339     // allow up to 12% absolute change increments (from filter design)
340     return pdiff < prevSampleRate>>4 && adiff < filterSampleRate>>3;
341 }
342 
343 template<typename TC, typename TI, typename TO>
setSampleRate(int32_t inSampleRate)344 void AudioResamplerDyn<TC, TI, TO>::setSampleRate(int32_t inSampleRate)
345 {
346     if (mInSampleRate == inSampleRate) {
347         return;
348     }
349     int32_t oldSampleRate = mInSampleRate;
350     uint32_t oldPhaseWrapLimit = mConstants.mL << mConstants.mShift;
351     bool useS32 = false;
352 
353     mInSampleRate = inSampleRate;
354 
355     // TODO: Add precalculated Equiripple filters
356 
357     if (mFilterQuality != getQuality() ||
358             !isClose(inSampleRate, oldSampleRate, mFilterSampleRate, mSampleRate)) {
359         mFilterSampleRate = inSampleRate;
360         mFilterQuality = getQuality();
361 
362         double stopBandAtten;
363         double tbwCheat = 1.; // how much we "cheat" into aliasing
364         int halfLength;
365         double fcr = 0.;
366 
367         // Begin Kaiser Filter computation
368         //
369         // The quantization floor for S16 is about 96db - 10*log_10(#length) + 3dB.
370         // Keep the stop band attenuation no greater than 84-85dB for 32 length S16 filters
371         //
372         // For s32 we keep the stop band attenuation at the same as 16b resolution, about
373         // 96-98dB
374         //
375 
376         if (mPropertyEnableAtSampleRate >= 0 && mSampleRate >= mPropertyEnableAtSampleRate) {
377             // An alternative method which allows allows a greater fcr
378             // at the expense of potential aliasing.
379             halfLength = mPropertyHalfFilterLength;
380             stopBandAtten = mPropertyStopbandAttenuation;
381             useS32 = true;
382             fcr = mInSampleRate <= mSampleRate
383                     ? 0.5 : 0.5 * mSampleRate / mInSampleRate;
384             fcr *= mPropertyCutoffPercent / 100.;
385         } else {
386             if (mFilterQuality == DYN_HIGH_QUALITY) {
387                 // 32b coefficients, 64 length
388                 useS32 = true;
389                 stopBandAtten = 98.;
390                 if (inSampleRate >= mSampleRate * 4) {
391                     halfLength = 48;
392                 } else if (inSampleRate >= mSampleRate * 2) {
393                     halfLength = 40;
394                 } else {
395                     halfLength = 32;
396                 }
397             } else if (mFilterQuality == DYN_LOW_QUALITY) {
398                 // 16b coefficients, 16-32 length
399                 useS32 = false;
400                 stopBandAtten = 80.;
401                 if (inSampleRate >= mSampleRate * 4) {
402                     halfLength = 24;
403                 } else if (inSampleRate >= mSampleRate * 2) {
404                     halfLength = 16;
405                 } else {
406                     halfLength = 8;
407                 }
408                 if (inSampleRate <= mSampleRate) {
409                     tbwCheat = 1.05;
410                 } else {
411                     tbwCheat = 1.03;
412                 }
413             } else { // DYN_MED_QUALITY
414                 // 16b coefficients, 32-64 length
415                 // note: > 64 length filters with 16b coefs can have quantization noise problems
416                 useS32 = false;
417                 stopBandAtten = 84.;
418                 if (inSampleRate >= mSampleRate * 4) {
419                     halfLength = 32;
420                 } else if (inSampleRate >= mSampleRate * 2) {
421                     halfLength = 24;
422                 } else {
423                     halfLength = 16;
424                 }
425                 if (inSampleRate <= mSampleRate) {
426                     tbwCheat = 1.03;
427                 } else {
428                     tbwCheat = 1.01;
429                 }
430             }
431         }
432 
433         // determine the number of polyphases in the filterbank.
434         // for 16b, it is desirable to have 2^(16/2) = 256 phases.
435         // https://ccrma.stanford.edu/~jos/resample/Relation_Interpolation_Error_Quantization.html
436         //
437         // We are a bit more lax on this.
438 
439         int phases = mSampleRate / gcd(mSampleRate, inSampleRate);
440 
441         // TODO: Once dynamic sample rate change is an option, the code below
442         // should be modified to execute only when dynamic sample rate change is enabled.
443         //
444         // as above, #phases less than 63 is too few phases for accurate linear interpolation.
445         // we increase the phases to compensate, but more phases means more memory per
446         // filter and more time to compute the filter.
447         //
448         // if we know that the filter will be used for dynamic sample rate changes,
449         // that would allow us skip this part for fixed sample rate resamplers.
450         //
451         while (phases<63) {
452             phases *= 2; // this code only needed to support dynamic rate changes
453         }
454 
455         if (phases>=256) {  // too many phases, always interpolate
456             phases = 127;
457         }
458 
459         // create the filter
460         mConstants.set(phases, halfLength, inSampleRate, mSampleRate);
461         if (fcr > 0.) {
462             createKaiserFir(mConstants, stopBandAtten, fcr);
463         } else {
464             createKaiserFir(mConstants, stopBandAtten,
465                     inSampleRate, mSampleRate, tbwCheat);
466         }
467     } // End Kaiser filter
468 
469     // update phase and state based on the new filter.
470     const Constants& c(mConstants);
471     mInBuffer.resize(mChannelCount, c.mHalfNumCoefs);
472     const uint32_t phaseWrapLimit = c.mL << c.mShift;
473     // try to preserve as much of the phase fraction as possible for on-the-fly changes
474     mPhaseFraction = static_cast<unsigned long long>(mPhaseFraction)
475             * phaseWrapLimit / oldPhaseWrapLimit;
476     mPhaseFraction %= phaseWrapLimit; // should not do anything, but just in case.
477     mPhaseIncrement = static_cast<uint32_t>(static_cast<uint64_t>(phaseWrapLimit)
478             * inSampleRate / mSampleRate);
479 
480     // determine which resampler to use
481     // check if locked phase (works only if mPhaseIncrement has no "fractional phase bits")
482     int locked = (mPhaseIncrement << (sizeof(mPhaseIncrement)*8 - c.mShift)) == 0;
483     if (locked) {
484         mPhaseFraction = mPhaseFraction >> c.mShift << c.mShift; // remove fractional phase
485     }
486 
487     // stride is the minimum number of filter coefficients processed per loop iteration.
488     // We currently only allow a stride of 16 to match with SIMD processing.
489     // This means that the filter length must be a multiple of 16,
490     // or half the filter length (mHalfNumCoefs) must be a multiple of 8.
491     //
492     // Note: A stride of 2 is achieved with non-SIMD processing.
493     int stride = ((c.mHalfNumCoefs & 7) == 0) ? 16 : 2;
494     LOG_ALWAYS_FATAL_IF(stride < 16, "Resampler stride must be 16 or more");
495     LOG_ALWAYS_FATAL_IF(mChannelCount < 1 || mChannelCount > 8,
496             "Resampler channels(%d) must be between 1 to 8", mChannelCount);
497     // stride 16 (falls back to stride 2 for machines that do not support NEON)
498     if (locked) {
499         switch (mChannelCount) {
500         case 1:
501             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, true, 16>;
502             break;
503         case 2:
504             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, true, 16>;
505             break;
506         case 3:
507             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, true, 16>;
508             break;
509         case 4:
510             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, true, 16>;
511             break;
512         case 5:
513             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, true, 16>;
514             break;
515         case 6:
516             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, true, 16>;
517             break;
518         case 7:
519             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, true, 16>;
520             break;
521         case 8:
522             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, true, 16>;
523             break;
524         }
525     } else {
526         switch (mChannelCount) {
527         case 1:
528             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, false, 16>;
529             break;
530         case 2:
531             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, false, 16>;
532             break;
533         case 3:
534             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, false, 16>;
535             break;
536         case 4:
537             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, false, 16>;
538             break;
539         case 5:
540             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, false, 16>;
541             break;
542         case 6:
543             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, false, 16>;
544             break;
545         case 7:
546             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, false, 16>;
547             break;
548         case 8:
549             mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, false, 16>;
550             break;
551         }
552     }
553 #ifdef DEBUG_RESAMPLER
554     printf("channels:%d  %s  stride:%d  %s  coef:%d  shift:%d\n",
555             mChannelCount, locked ? "locked" : "interpolated",
556             stride, useS32 ? "S32" : "S16", 2*c.mHalfNumCoefs, c.mShift);
557 #endif
558 }
559 
560 template<typename TC, typename TI, typename TO>
resample(int32_t * out,size_t outFrameCount,AudioBufferProvider * provider)561 size_t AudioResamplerDyn<TC, TI, TO>::resample(int32_t* out, size_t outFrameCount,
562             AudioBufferProvider* provider)
563 {
564     return (this->*mResampleFunc)(reinterpret_cast<TO*>(out), outFrameCount, provider);
565 }
566 
567 template<typename TC, typename TI, typename TO>
568 template<int CHANNELS, bool LOCKED, int STRIDE>
resample(TO * out,size_t outFrameCount,AudioBufferProvider * provider)569 size_t AudioResamplerDyn<TC, TI, TO>::resample(TO* out, size_t outFrameCount,
570         AudioBufferProvider* provider)
571 {
572     // TODO Mono -> Mono is not supported. OUTPUT_CHANNELS reflects minimum of stereo out.
573     const int OUTPUT_CHANNELS = (CHANNELS < 2) ? 2 : CHANNELS;
574     const Constants& c(mConstants);
575     const TC* const coefs = mConstants.mFirCoefs;
576     TI* impulse = mInBuffer.getImpulse();
577     size_t inputIndex = 0;
578     uint32_t phaseFraction = mPhaseFraction;
579     const uint32_t phaseIncrement = mPhaseIncrement;
580     size_t outputIndex = 0;
581     size_t outputSampleCount = outFrameCount * OUTPUT_CHANNELS;
582     const uint32_t phaseWrapLimit = c.mL << c.mShift;
583     size_t inFrameCount = (phaseIncrement * (uint64_t)outFrameCount + phaseFraction)
584             / phaseWrapLimit;
585     // sanity check that inFrameCount is in signed 32 bit integer range.
586     ALOG_ASSERT(0 <= inFrameCount && inFrameCount < (1U << 31));
587 
588     //ALOGV("inFrameCount:%d  outFrameCount:%d"
589     //        "  phaseIncrement:%u  phaseFraction:%u  phaseWrapLimit:%u",
590     //        inFrameCount, outFrameCount, phaseIncrement, phaseFraction, phaseWrapLimit);
591 
592     // NOTE: be very careful when modifying the code here. register
593     // pressure is very high and a small change might cause the compiler
594     // to generate far less efficient code.
595     // Always sanity check the result with objdump or test-resample.
596 
597     // the following logic is a bit convoluted to keep the main processing loop
598     // as tight as possible with register allocation.
599     while (outputIndex < outputSampleCount) {
600         //ALOGV("LOOP: inFrameCount:%d  outputIndex:%d  outFrameCount:%d"
601         //        "  phaseFraction:%u  phaseWrapLimit:%u",
602         //        inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
603 
604         // check inputIndex overflow
605         ALOG_ASSERT(inputIndex <= mBuffer.frameCount, "inputIndex%zu > frameCount%zu",
606                 inputIndex, mBuffer.frameCount);
607         // Buffer is empty, fetch a new one if necessary (inFrameCount > 0).
608         // We may not fetch a new buffer if the existing data is sufficient.
609         while (mBuffer.frameCount == 0 && inFrameCount > 0) {
610             mBuffer.frameCount = inFrameCount;
611             provider->getNextBuffer(&mBuffer);
612             if (mBuffer.raw == NULL) {
613                 // We are either at the end of playback or in an underrun situation.
614                 // Reset buffer to prevent pop noise at the next buffer.
615                 mInBuffer.reset();
616                 goto resample_exit;
617             }
618             inFrameCount -= mBuffer.frameCount;
619             if (phaseFraction >= phaseWrapLimit) { // read in data
620                 mInBuffer.template readAdvance<CHANNELS>(
621                         impulse, c.mHalfNumCoefs,
622                         reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
623                 inputIndex++;
624                 phaseFraction -= phaseWrapLimit;
625                 while (phaseFraction >= phaseWrapLimit) {
626                     if (inputIndex >= mBuffer.frameCount) {
627                         inputIndex = 0;
628                         provider->releaseBuffer(&mBuffer);
629                         break;
630                     }
631                     mInBuffer.template readAdvance<CHANNELS>(
632                             impulse, c.mHalfNumCoefs,
633                             reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
634                     inputIndex++;
635                     phaseFraction -= phaseWrapLimit;
636                 }
637             }
638         }
639         const TI* const in = reinterpret_cast<const TI*>(mBuffer.raw);
640         const size_t frameCount = mBuffer.frameCount;
641         const int coefShift = c.mShift;
642         const int halfNumCoefs = c.mHalfNumCoefs;
643         const TO* const volumeSimd = mVolumeSimd;
644 
645         // main processing loop
646         while (CC_LIKELY(outputIndex < outputSampleCount)) {
647             // caution: fir() is inlined and may be large.
648             // output will be loaded with the appropriate values
649             //
650             // from the input samples in impulse[-halfNumCoefs+1]... impulse[halfNumCoefs]
651             // from the polyphase filter of (phaseFraction / phaseWrapLimit) in coefs.
652             //
653             //ALOGV("LOOP2: inFrameCount:%d  outputIndex:%d  outFrameCount:%d"
654             //        "  phaseFraction:%u  phaseWrapLimit:%u",
655             //        inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
656             ALOG_ASSERT(phaseFraction < phaseWrapLimit);
657             fir<CHANNELS, LOCKED, STRIDE>(
658                     &out[outputIndex],
659                     phaseFraction, phaseWrapLimit,
660                     coefShift, halfNumCoefs, coefs,
661                     impulse, volumeSimd);
662 
663             outputIndex += OUTPUT_CHANNELS;
664 
665             phaseFraction += phaseIncrement;
666             while (phaseFraction >= phaseWrapLimit) {
667                 if (inputIndex >= frameCount) {
668                     goto done;  // need a new buffer
669                 }
670                 mInBuffer.template readAdvance<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
671                 inputIndex++;
672                 phaseFraction -= phaseWrapLimit;
673             }
674         }
675 done:
676         // We arrive here when we're finished or when the input buffer runs out.
677         // Regardless we need to release the input buffer if we've acquired it.
678         if (inputIndex > 0) {  // we've acquired a buffer (alternatively could check frameCount)
679             ALOG_ASSERT(inputIndex == frameCount, "inputIndex(%zu) != frameCount(%zu)",
680                     inputIndex, frameCount);  // must have been fully read.
681             inputIndex = 0;
682             provider->releaseBuffer(&mBuffer);
683             ALOG_ASSERT(mBuffer.frameCount == 0);
684         }
685     }
686 
687 resample_exit:
688     // inputIndex must be zero in all three cases:
689     // (1) the buffer never was been acquired; (2) the buffer was
690     // released at "done:"; or (3) getNextBuffer() failed.
691     ALOG_ASSERT(inputIndex == 0, "Releasing: inputindex:%zu frameCount:%zu  phaseFraction:%u",
692             inputIndex, mBuffer.frameCount, phaseFraction);
693     ALOG_ASSERT(mBuffer.frameCount == 0); // there must be no frames in the buffer
694     mInBuffer.setImpulse(impulse);
695     mPhaseFraction = phaseFraction;
696     return outputIndex / OUTPUT_CHANNELS;
697 }
698 
699 /* instantiate templates used by AudioResampler::create */
700 template class AudioResamplerDyn<float, float, float>;
701 template class AudioResamplerDyn<int16_t, int16_t, int32_t>;
702 template class AudioResamplerDyn<int32_t, int16_t, int32_t>;
703 
704 // ----------------------------------------------------------------------------
705 } // namespace android
706