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