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 #include <audio_utils/primitives.h>
30
31 #include "AudioResamplerFirOps.h" // USE_NEON, USE_SSE and USE_INLINE_ASSEMBLY defined here
32 #include "AudioResamplerFirProcess.h"
33 #include "AudioResamplerFirProcessNeon.h"
34 #include "AudioResamplerFirProcessSSE.h"
35 #include "AudioResamplerFirGen.h" // requires math.h
36 #include "AudioResamplerDyn.h"
37
38 //#define DEBUG_RESAMPLER
39
40 // use this for our buffer alignment. Should be at least 32 bytes.
41 constexpr size_t CACHE_LINE_SIZE = 64;
42
43 namespace android {
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 TC, typename TI, typename TO>
InBuffer()62 AudioResamplerDyn<TC, TI, TO>::InBuffer::InBuffer()
63 : mState(NULL), mImpulse(NULL), mRingFull(NULL), mStateCount(0)
64 {
65 }
66
67 template<typename TC, typename TI, typename TO>
~InBuffer()68 AudioResamplerDyn<TC, TI, TO>::InBuffer::~InBuffer()
69 {
70 init();
71 }
72
73 template<typename TC, typename TI, typename TO>
init()74 void AudioResamplerDyn<TC, TI, TO>::InBuffer::init()
75 {
76 free(mState);
77 mState = NULL;
78 mImpulse = NULL;
79 mRingFull = NULL;
80 mStateCount = 0;
81 }
82
83 // resizes the state buffer to accommodate the appropriate filter length
84 template<typename TC, typename TI, typename TO>
resize(int CHANNELS,int halfNumCoefs)85 void AudioResamplerDyn<TC, TI, TO>::InBuffer::resize(int CHANNELS, int halfNumCoefs)
86 {
87 // calculate desired state size
88 size_t stateCount = halfNumCoefs * CHANNELS * 2 * kStateSizeMultipleOfFilterLength;
89
90 // check if buffer needs resizing
91 if (mState
92 && stateCount == mStateCount
93 && mRingFull-mState == (ssize_t) (mStateCount-halfNumCoefs*CHANNELS)) {
94 return;
95 }
96
97 // create new buffer
98 TI* state = NULL;
99 (void)posix_memalign(
100 reinterpret_cast<void **>(&state),
101 CACHE_LINE_SIZE /* alignment */,
102 stateCount * sizeof(*state));
103 memset(state, 0, stateCount*sizeof(*state));
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 }
115 if (srcHi > mState + mStateCount) {
116 srcHi = mState + mStateCount;
117 }
118 memcpy(dst, srcLo, (srcHi - srcLo) * sizeof(*srcLo));
119 free(mState);
120 }
121
122 // set class member vars
123 mState = state;
124 mStateCount = stateCount;
125 mImpulse = state + halfNumCoefs*CHANNELS; // actually one sample greater than needed
126 mRingFull = state + mStateCount - halfNumCoefs*CHANNELS;
127 }
128
129 // copy in the input data into the head (impulse+halfNumCoefs) of the buffer.
130 template<typename TC, typename TI, typename TO>
131 template<int CHANNELS>
readAgain(TI * & impulse,const int halfNumCoefs,const TI * const in,const size_t inputIndex)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;
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)
142 template<typename TC, typename TI, typename TO>
143 template<int CHANNELS>
readAdvance(TI * & impulse,const int halfNumCoefs,const TI * const in,const size_t inputIndex)144 void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAdvance(TI*& impulse, const int halfNumCoefs,
145 const TI* const in, const size_t inputIndex)
146 {
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
157 template<typename TC, typename TI, typename TO>
reset()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>
set(int L,int halfNumCoefs,int inSampleRate,int outSampleRate)167 void AudioResamplerDyn<TC, TI, TO>::Constants::set(
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
180 template<typename TC, typename TI, typename TO>
AudioResamplerDyn(int inChannelCount,int32_t sampleRate,src_quality quality)181 AudioResamplerDyn<TC, TI, TO>::AudioResamplerDyn(
182 int inChannelCount, int32_t sampleRate, src_quality quality)
183 : AudioResampler(inChannelCount, sampleRate, quality),
184 mResampleFunc(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY),
185 mCoefBuffer(NULL)
186 {
187 mVolumeSimd[0] = mVolumeSimd[1] = 0;
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;
192 mConstants.set(128, 8, mSampleRate, mSampleRate); // TODO: set better
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);
203 mPropertyTransitionBandwidthCheat = property_get_int32(
204 "ro.audio.resampler.psd.tbwcheat", mPropertyTransitionBandwidthCheat);
205 }
206
207 template<typename TC, typename TI, typename TO>
~AudioResamplerDyn()208 AudioResamplerDyn<TC, TI, TO>::~AudioResamplerDyn()
209 {
210 free(mCoefBuffer);
211 }
212
213 template<typename TC, typename TI, typename TO>
init()214 void AudioResamplerDyn<TC, TI, TO>::init()
215 {
216 mFilterSampleRate = 0; // always trigger new filter generation
217 mInBuffer.init();
218 }
219
220 template<typename TC, typename TI, typename TO>
setVolume(float left,float right)221 void AudioResamplerDyn<TC, TI, TO>::setVolume(float left, float right)
222 {
223 AudioResampler::setVolume(left, right);
224 if (is_same<TO, float>::value || is_same<TO, double>::value) {
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));
232 }
233 }
234
235 // TODO: update to C++11
236
max(T a,T b)237 template<typename T> T max(T a, T b) {return a > b ? a : b;}
238
absdiff(T a,T b)239 template<typename T> T absdiff(T a, T b) {return a > b ? a - b : b - a;}
240
241 template<typename TC, typename TI, typename TO>
createKaiserFir(Constants & c,double stopBandAtten,int inSampleRate,int outSampleRate,double tbwCheat)242 void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c,
243 double stopBandAtten, int inSampleRate, int outSampleRate, double tbwCheat)
244 {
245 // compute the normalized transition bandwidth
246 const double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten);
247 const double halfbw = tbw * 0.5;
248
249 double fcr; // compute fcr, the 3 dB amplitude cut-off.
250 if (inSampleRate < outSampleRate) { // upsample
251 fcr = max(0.5 * tbwCheat - halfbw, halfbw);
252 } else { // downsample
253 fcr = max(0.5 * tbwCheat * outSampleRate / inSampleRate - halfbw, halfbw);
254 }
255 createKaiserFir(c, stopBandAtten, fcr);
256 }
257
258 template<typename TC, typename TI, typename TO>
createKaiserFir(Constants & c,double stopBandAtten,double fcr)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.
294 const double halfbw = tbw * 0.5;
295 // print basic filter stats
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
304 double passMin, passMax, passRipple;
305 double stopMax, stopRipple;
306
307 const int32_t passSteps = 1000;
308
309 testFir(coefs, c.mL, c.mHalfNumCoefs, fp, fs, passSteps, passSteps * c.mL /*stopSteps*/,
310 passMin, passMax, passRipple, stopMax, stopRipple);
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);
313 #endif
314 }
315
316 // recursive gcd. Using objdump, it appears the tail recursion is converted to a while loop.
gcd(int n,int m)317 static int gcd(int n, int m)
318 {
319 if (m == 0) {
320 return n;
321 }
322 return gcd(m, n % m);
323 }
324
isClose(int32_t newSampleRate,int32_t prevSampleRate,int32_t filterSampleRate,int32_t outSampleRate)325 static bool isClose(int32_t newSampleRate, int32_t prevSampleRate,
326 int32_t filterSampleRate, int32_t outSampleRate)
327 {
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.
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
344 template<typename TC, typename TI, typename TO>
setSampleRate(int32_t inSampleRate)345 void AudioResamplerDyn<TC, TI, TO>::setSampleRate(int32_t inSampleRate)
346 {
347 if (mInSampleRate == inSampleRate) {
348 return;
349 }
350 int32_t oldSampleRate = mInSampleRate;
351 uint32_t oldPhaseWrapLimit = mConstants.mL << mConstants.mShift;
352 bool useS32 = false;
353
354 mInSampleRate = inSampleRate;
355
356 // TODO: Add precalculated Equiripple filters
357
358 if (mFilterQuality != getQuality() ||
359 !isClose(inSampleRate, oldSampleRate, mFilterSampleRate, mSampleRate)) {
360 mFilterSampleRate = inSampleRate;
361 mFilterQuality = getQuality();
362
363 double stopBandAtten;
364 double tbwCheat = 1.; // how much we "cheat" into aliasing
365 int halfLength;
366 double fcr = 0.;
367
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
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;
382 useS32 = true;
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 }
394 } else {
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
403 if (mFilterQuality == DYN_HIGH_QUALITY) {
404 // float or 32b coefficients
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 }
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 }
424 } else if (mFilterQuality == DYN_LOW_QUALITY) {
425 // float or 16b coefficients
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 }
435 if (mSampleRate <= kVoiceDeviceSampleRate) {
436 if (inSampleRate >= mSampleRate * 2) {
437 halfLength += 8;
438 }
439 tbwCheat = 1.05;
440 } else if (inSampleRate <= mSampleRate) {
441 tbwCheat = 1.05;
442 } else {
443 tbwCheat = 1.03;
444 }
445 } else { // DYN_MED_QUALITY
446 // float or 16b coefficients
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 }
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) {
466 tbwCheat = 1.03;
467 } else {
468 tbwCheat = 1.01;
469 }
470 }
471 }
472
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
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
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) {
505 phases *= 2; // this code only needed to support dynamic rate changes
506 }
507
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);
514 if (fcr > 0.) {
515 createKaiserFir(mConstants, stopBandAtten, fcr);
516 } else {
517 createKaiserFir(mConstants, stopBandAtten,
518 inSampleRate, mSampleRate, tbwCheat);
519 }
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.
530 mPhaseIncrement = static_cast<uint32_t>(static_cast<uint64_t>(phaseWrapLimit)
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;
536 if (locked) {
537 mPhaseFraction = mPhaseFraction >> c.mShift << c.mShift; // remove fractional phase
538 }
539
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");
548 LOG_ALWAYS_FATAL_IF(mChannelCount < 1 || mChannelCount > FCC_LIMIT,
549 "Resampler channels(%d) must be between 1 to %d", mChannelCount, FCC_LIMIT);
550 // stride 16 (falls back to stride 2 for machines that do not support NEON)
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
561 if (locked) {
562 switch (mChannelCount) {
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);
587 }
588 } else {
589 switch (mChannelCount) {
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);
614 }
615 }
616 #pragma pop_macro("AUDIORESAMPLERDYN_CASE")
617
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
625 template<typename TC, typename TI, typename TO>
resample(int32_t * out,size_t outFrameCount,AudioBufferProvider * provider)626 size_t AudioResamplerDyn<TC, TI, TO>::resample(int32_t* out, size_t outFrameCount,
627 AudioBufferProvider* provider)
628 {
629 return (this->*mResampleFunc)(reinterpret_cast<TO*>(out), outFrameCount, provider);
630 }
631
632 template<typename TC, typename TI, typename TO>
633 template<int CHANNELS, bool LOCKED, int STRIDE>
resample(TO * out,size_t outFrameCount,AudioBufferProvider * provider)634 size_t AudioResamplerDyn<TC, TI, TO>::resample(TO* out, size_t outFrameCount,
635 AudioBufferProvider* provider)
636 {
637 // TODO Mono -> Mono is not supported. OUTPUT_CHANNELS reflects minimum of stereo out.
638 const int OUTPUT_CHANNELS = (CHANNELS < 2) ? 2 : CHANNELS;
639 const Constants& c(mConstants);
640 const TC* const coefs = mConstants.mFirCoefs;
641 TI* impulse = mInBuffer.getImpulse();
642 size_t inputIndex = 0;
643 uint32_t phaseFraction = mPhaseFraction;
644 const uint32_t phaseIncrement = mPhaseIncrement;
645 size_t outputIndex = 0;
646 size_t outputSampleCount = outFrameCount * OUTPUT_CHANNELS;
647 const uint32_t phaseWrapLimit = c.mL << c.mShift;
648 size_t inFrameCount = (phaseIncrement * (uint64_t)outFrameCount + phaseFraction)
649 / phaseWrapLimit;
650 // validate that inFrameCount is in signed 32 bit integer range.
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);
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.
660 // Always validate the result with objdump or test-resample.
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) {
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
670 ALOG_ASSERT(inputIndex <= mBuffer.frameCount, "inputIndex%zu > frameCount%zu",
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) {
675 mBuffer.frameCount = inFrameCount;
676 provider->getNextBuffer(&mBuffer);
677 if (mBuffer.raw == NULL) {
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();
681 goto resample_exit;
682 }
683 inFrameCount -= mBuffer.frameCount;
684 if (phaseFraction >= phaseWrapLimit) { // read in data
685 mInBuffer.template readAdvance<CHANNELS>(
686 impulse, c.mHalfNumCoefs,
687 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
688 inputIndex++;
689 phaseFraction -= phaseWrapLimit;
690 while (phaseFraction >= phaseWrapLimit) {
691 if (inputIndex >= mBuffer.frameCount) {
692 inputIndex = 0;
693 provider->releaseBuffer(&mBuffer);
694 break;
695 }
696 mInBuffer.template readAdvance<CHANNELS>(
697 impulse, c.mHalfNumCoefs,
698 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
699 inputIndex++;
700 phaseFraction -= phaseWrapLimit;
701 }
702 }
703 }
704 const TI* const in = reinterpret_cast<const TI*>(mBuffer.raw);
705 const size_t frameCount = mBuffer.frameCount;
706 const int coefShift = c.mShift;
707 const int halfNumCoefs = c.mHalfNumCoefs;
708 const TO* const volumeSimd = mVolumeSimd;
709
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 //
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);
722 fir<CHANNELS, LOCKED, STRIDE>(
723 &out[outputIndex],
724 phaseFraction, phaseWrapLimit,
725 coefShift, halfNumCoefs, coefs,
726 impulse, volumeSimd);
727
728 outputIndex += OUTPUT_CHANNELS;
729
730 phaseFraction += phaseIncrement;
731 while (phaseFraction >= phaseWrapLimit) {
732 if (inputIndex >= frameCount) {
733 goto done; // need a new buffer
734 }
735 mInBuffer.template readAdvance<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
736 inputIndex++;
737 phaseFraction -= phaseWrapLimit;
738 }
739 }
740 done:
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)
744 ALOG_ASSERT(inputIndex == frameCount, "inputIndex(%zu) != frameCount(%zu)",
745 inputIndex, frameCount); // must have been fully read.
746 inputIndex = 0;
747 provider->releaseBuffer(&mBuffer);
748 ALOG_ASSERT(mBuffer.frameCount == 0);
749 }
750 }
751
752 resample_exit:
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.
756 ALOG_ASSERT(inputIndex == 0, "Releasing: inputindex:%zu frameCount:%zu phaseFraction:%u",
757 inputIndex, mBuffer.frameCount, phaseFraction);
758 ALOG_ASSERT(mBuffer.frameCount == 0); // there must be no frames in the buffer
759 mInBuffer.setImpulse(impulse);
760 mPhaseFraction = phaseFraction;
761 return outputIndex / OUTPUT_CHANNELS;
762 }
763
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
769 // ----------------------------------------------------------------------------
770 } // namespace android
771