blob: 3abe8fdbe43b2244f3418cbcafe48a706529174d [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>
Andy Hungd5491392014-04-08 18:28:09 -070028#include <utils/Debug.h>
Andy Hung86eae0e2013-12-09 12:12:46 -080029#include <utils/Log.h>
30
31#include "AudioResamplerFirOps.h" // USE_NEON and USE_INLINE_ASSEMBLY defined here
32#include "AudioResamplerFirProcess.h"
33#include "AudioResamplerFirProcessNeon.h"
34#include "AudioResamplerFirGen.h" // requires math.h
35#include "AudioResamplerDyn.h"
36
37//#define DEBUG_RESAMPLER
38
39namespace android {
40
41// generate a unique resample type compile-time constant (constexpr)
Andy Hung771386e2014-04-08 18:44:38 -070042#define RESAMPLETYPE(CHANNELS, LOCKED, STRIDE) \
43 ((((CHANNELS)-1)&1) | !!(LOCKED)<<1 \
44 | ((STRIDE)==8 ? 1 : (STRIDE)==16 ? 2 : 0)<<2)
Andy Hung86eae0e2013-12-09 12:12:46 -080045
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
Andy Hung771386e2014-04-08 18:44:38 -070062template<typename TC, typename TI, typename TO>
63AudioResamplerDyn<TC, TI, TO>::InBuffer::InBuffer()
64 : mState(NULL), mImpulse(NULL), mRingFull(NULL), mStateCount(0)
65{
Andy Hung86eae0e2013-12-09 12:12:46 -080066}
67
Andy Hung771386e2014-04-08 18:44:38 -070068template<typename TC, typename TI, typename TO>
69AudioResamplerDyn<TC, TI, TO>::InBuffer::~InBuffer()
70{
Andy Hung86eae0e2013-12-09 12:12:46 -080071 init();
72}
73
Andy Hung771386e2014-04-08 18:44:38 -070074template<typename TC, typename TI, typename TO>
75void AudioResamplerDyn<TC, TI, TO>::InBuffer::init()
76{
Andy Hung86eae0e2013-12-09 12:12:46 -080077 free(mState);
78 mState = NULL;
79 mImpulse = NULL;
80 mRingFull = NULL;
Andy Hung771386e2014-04-08 18:44:38 -070081 mStateCount = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -080082}
83
84// resizes the state buffer to accommodate the appropriate filter length
Andy Hung771386e2014-04-08 18:44:38 -070085template<typename TC, typename TI, typename TO>
86void AudioResamplerDyn<TC, TI, TO>::InBuffer::resize(int CHANNELS, int halfNumCoefs)
87{
Andy Hung86eae0e2013-12-09 12:12:46 -080088 // calculate desired state size
Andy Hung771386e2014-04-08 18:44:38 -070089 int stateCount = halfNumCoefs * CHANNELS * 2 * kStateSizeMultipleOfFilterLength;
Andy Hung86eae0e2013-12-09 12:12:46 -080090
91 // check if buffer needs resizing
92 if (mState
Andy Hung771386e2014-04-08 18:44:38 -070093 && stateCount == mStateCount
94 && mRingFull-mState == mStateCount-halfNumCoefs*CHANNELS) {
Andy Hung86eae0e2013-12-09 12:12:46 -080095 return;
96 }
97
98 // create new buffer
Andy Hung771386e2014-04-08 18:44:38 -070099 TI* state;
100 (void)posix_memalign(reinterpret_cast<void**>(&state), 32, stateCount*sizeof(*state));
101 memset(state, 0, stateCount*sizeof(*state));
Andy Hung86eae0e2013-12-09 12:12:46 -0800102
103 // attempt to preserve state
104 if (mState) {
105 TI* srcLo = mImpulse - halfNumCoefs*CHANNELS;
106 TI* srcHi = mImpulse + halfNumCoefs*CHANNELS;
107 TI* dst = state;
108
109 if (srcLo < mState) {
110 dst += mState-srcLo;
111 srcLo = mState;
112 }
Andy Hung771386e2014-04-08 18:44:38 -0700113 if (srcHi > mState + mStateCount) {
114 srcHi = mState + mStateCount;
Andy Hung86eae0e2013-12-09 12:12:46 -0800115 }
116 memcpy(dst, srcLo, (srcHi - srcLo) * sizeof(*srcLo));
117 free(mState);
118 }
119
120 // set class member vars
121 mState = state;
Andy Hung771386e2014-04-08 18:44:38 -0700122 mStateCount = stateCount;
123 mImpulse = state + halfNumCoefs*CHANNELS; // actually one sample greater than needed
124 mRingFull = state + mStateCount - halfNumCoefs*CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800125}
126
127// copy in the input data into the head (impulse+halfNumCoefs) of the buffer.
Andy Hung771386e2014-04-08 18:44:38 -0700128template<typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -0800129template<int CHANNELS>
Andy Hung771386e2014-04-08 18:44:38 -0700130void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAgain(TI*& impulse, const int halfNumCoefs,
131 const TI* const in, const size_t inputIndex)
132{
133 TI* head = impulse + halfNumCoefs*CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800134 for (size_t i=0 ; i<CHANNELS ; i++) {
135 head[i] = in[inputIndex*CHANNELS + i];
136 }
137}
138
139// advance the impulse pointer, and load in data into the head (impulse+halfNumCoefs)
Andy Hung771386e2014-04-08 18:44:38 -0700140template<typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -0800141template<int CHANNELS>
Andy Hung771386e2014-04-08 18:44:38 -0700142void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAdvance(TI*& impulse, const int halfNumCoefs,
143 const TI* const in, const size_t inputIndex)
144{
Andy Hung86eae0e2013-12-09 12:12:46 -0800145 impulse += CHANNELS;
146
147 if (CC_UNLIKELY(impulse >= mRingFull)) {
148 const size_t shiftDown = mRingFull - mState - halfNumCoefs*CHANNELS;
149 memcpy(mState, mState+shiftDown, halfNumCoefs*CHANNELS*2*sizeof(TI));
150 impulse -= shiftDown;
151 }
152 readAgain<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
153}
154
Andy Hung771386e2014-04-08 18:44:38 -0700155template<typename TC, typename TI, typename TO>
156void AudioResamplerDyn<TC, TI, TO>::Constants::set(
Andy Hung86eae0e2013-12-09 12:12:46 -0800157 int L, int halfNumCoefs, int inSampleRate, int outSampleRate)
158{
159 int bits = 0;
160 int lscale = inSampleRate/outSampleRate < 2 ? L - 1 :
161 static_cast<int>(static_cast<uint64_t>(L)*inSampleRate/outSampleRate);
162 for (int i=lscale; i; ++bits, i>>=1)
163 ;
164 mL = L;
165 mShift = kNumPhaseBits - bits;
166 mHalfNumCoefs = halfNumCoefs;
167}
168
Andy Hung771386e2014-04-08 18:44:38 -0700169template<typename TC, typename TI, typename TO>
170AudioResamplerDyn<TC, TI, TO>::AudioResamplerDyn(int bitDepth,
Andy Hung86eae0e2013-12-09 12:12:46 -0800171 int inChannelCount, int32_t sampleRate, src_quality quality)
172 : AudioResampler(bitDepth, inChannelCount, sampleRate, quality),
Andy Hung771386e2014-04-08 18:44:38 -0700173 mResampleFunc(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY),
Andy Hung6582f2b2014-01-03 12:30:41 -0800174 mCoefBuffer(NULL)
Andy Hung86eae0e2013-12-09 12:12:46 -0800175{
176 mVolumeSimd[0] = mVolumeSimd[1] = 0;
Andy Hung1af34082014-02-19 17:42:25 -0800177 // The AudioResampler base class assumes we are always ready for 1:1 resampling.
178 // We reset mInSampleRate to 0, so setSampleRate() will calculate filters for
179 // setSampleRate() for 1:1. (May be removed if precalculated filters are used.)
180 mInSampleRate = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800181 mConstants.set(128, 8, mSampleRate, mSampleRate); // TODO: set better
182}
183
Andy Hung771386e2014-04-08 18:44:38 -0700184template<typename TC, typename TI, typename TO>
185AudioResamplerDyn<TC, TI, TO>::~AudioResamplerDyn()
186{
Andy Hung86eae0e2013-12-09 12:12:46 -0800187 free(mCoefBuffer);
188}
189
Andy Hung771386e2014-04-08 18:44:38 -0700190template<typename TC, typename TI, typename TO>
191void AudioResamplerDyn<TC, TI, TO>::init()
192{
Andy Hung86eae0e2013-12-09 12:12:46 -0800193 mFilterSampleRate = 0; // always trigger new filter generation
194 mInBuffer.init();
195}
196
Andy Hung771386e2014-04-08 18:44:38 -0700197template<typename TC, typename TI, typename TO>
198void AudioResamplerDyn<TC, TI, TO>::setVolume(int16_t left, int16_t right)
199{
Andy Hung86eae0e2013-12-09 12:12:46 -0800200 AudioResampler::setVolume(left, right);
Andy Hung771386e2014-04-08 18:44:38 -0700201 // volume is applied on the output type.
202 if (is_same<TO, float>::value || is_same<TO, double>::value) {
203 const TO scale = 1. / (1UL << 12);
204 mVolumeSimd[0] = static_cast<TO>(left) * scale;
205 mVolumeSimd[1] = static_cast<TO>(right) * scale;
206 } else {
207 mVolumeSimd[0] = static_cast<int32_t>(left) << 16;
208 mVolumeSimd[1] = static_cast<int32_t>(right) << 16;
209 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800210}
211
Andy Hung771386e2014-04-08 18:44:38 -0700212template<typename T> T max(T a, T b) {return a > b ? a : b;}
Andy Hung86eae0e2013-12-09 12:12:46 -0800213
Andy Hung771386e2014-04-08 18:44:38 -0700214template<typename T> T absdiff(T a, T b) {return a > b ? a - b : b - a;}
Andy Hung86eae0e2013-12-09 12:12:46 -0800215
Andy Hung771386e2014-04-08 18:44:38 -0700216template<typename TC, typename TI, typename TO>
217void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c,
218 double stopBandAtten, int inSampleRate, int outSampleRate, double tbwCheat)
219{
220 TC* buf;
Andy Hung86eae0e2013-12-09 12:12:46 -0800221 static const double atten = 0.9998; // to avoid ripple overflow
222 double fcr;
223 double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten);
224
Andy Hung771386e2014-04-08 18:44:38 -0700225 (void)posix_memalign(reinterpret_cast<void**>(&buf), 32, (c.mL+1)*c.mHalfNumCoefs*sizeof(TC));
Andy Hung86eae0e2013-12-09 12:12:46 -0800226 if (inSampleRate < outSampleRate) { // upsample
227 fcr = max(0.5*tbwCheat - tbw/2, tbw/2);
228 } else { // downsample
229 fcr = max(0.5*tbwCheat*outSampleRate/inSampleRate - tbw/2, tbw/2);
230 }
231 // create and set filter
232 firKaiserGen(buf, c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten);
Andy Hung771386e2014-04-08 18:44:38 -0700233 c.mFirCoefs = buf;
Andy Hung86eae0e2013-12-09 12:12:46 -0800234 if (mCoefBuffer) {
235 free(mCoefBuffer);
236 }
237 mCoefBuffer = buf;
238#ifdef DEBUG_RESAMPLER
239 // print basic filter stats
240 printf("L:%d hnc:%d stopBandAtten:%lf fcr:%lf atten:%lf tbw:%lf\n",
241 c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten, tbw);
242 // test the filter and report results
243 double fp = (fcr - tbw/2)/c.mL;
244 double fs = (fcr + tbw/2)/c.mL;
Andy Hung6582f2b2014-01-03 12:30:41 -0800245 double passMin, passMax, passRipple;
246 double stopMax, stopRipple;
247 testFir(buf, c.mL, c.mHalfNumCoefs, fp, fs, /*passSteps*/ 1000, /*stopSteps*/ 100000,
248 passMin, passMax, passRipple, stopMax, stopRipple);
249 printf("passband(%lf, %lf): %.8lf %.8lf %.8lf\n", 0., fp, passMin, passMax, passRipple);
250 printf("stopband(%lf, %lf): %.8lf %.3lf\n", fs, 0.5, stopMax, stopRipple);
Andy Hung86eae0e2013-12-09 12:12:46 -0800251#endif
252}
253
Andy Hung6582f2b2014-01-03 12:30:41 -0800254// recursive gcd. Using objdump, it appears the tail recursion is converted to a while loop.
Andy Hung771386e2014-04-08 18:44:38 -0700255static int gcd(int n, int m)
256{
Andy Hung86eae0e2013-12-09 12:12:46 -0800257 if (m == 0) {
258 return n;
259 }
260 return gcd(m, n % m);
261}
262
Andy Hung6582f2b2014-01-03 12:30:41 -0800263static bool isClose(int32_t newSampleRate, int32_t prevSampleRate,
Andy Hung771386e2014-04-08 18:44:38 -0700264 int32_t filterSampleRate, int32_t outSampleRate)
265{
Andy Hung6582f2b2014-01-03 12:30:41 -0800266
267 // different upsampling ratios do not need a filter change.
268 if (filterSampleRate != 0
269 && filterSampleRate < outSampleRate
270 && newSampleRate < outSampleRate)
271 return true;
272
273 // check design criteria again if downsampling is detected.
Andy Hung86eae0e2013-12-09 12:12:46 -0800274 int pdiff = absdiff(newSampleRate, prevSampleRate);
275 int adiff = absdiff(newSampleRate, filterSampleRate);
276
277 // allow up to 6% relative change increments.
278 // allow up to 12% absolute change increments (from filter design)
279 return pdiff < prevSampleRate>>4 && adiff < filterSampleRate>>3;
280}
281
Andy Hung771386e2014-04-08 18:44:38 -0700282template<typename TC, typename TI, typename TO>
283void AudioResamplerDyn<TC, TI, TO>::setSampleRate(int32_t inSampleRate)
284{
Andy Hung86eae0e2013-12-09 12:12:46 -0800285 if (mInSampleRate == inSampleRate) {
286 return;
287 }
288 int32_t oldSampleRate = mInSampleRate;
289 int32_t oldHalfNumCoefs = mConstants.mHalfNumCoefs;
290 uint32_t oldPhaseWrapLimit = mConstants.mL << mConstants.mShift;
291 bool useS32 = false;
292
293 mInSampleRate = inSampleRate;
294
295 // TODO: Add precalculated Equiripple filters
296
Andy Hung6582f2b2014-01-03 12:30:41 -0800297 if (mFilterQuality != getQuality() ||
298 !isClose(inSampleRate, oldSampleRate, mFilterSampleRate, mSampleRate)) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800299 mFilterSampleRate = inSampleRate;
Andy Hung6582f2b2014-01-03 12:30:41 -0800300 mFilterQuality = getQuality();
Andy Hung86eae0e2013-12-09 12:12:46 -0800301
302 // Begin Kaiser Filter computation
303 //
304 // The quantization floor for S16 is about 96db - 10*log_10(#length) + 3dB.
305 // Keep the stop band attenuation no greater than 84-85dB for 32 length S16 filters
306 //
307 // For s32 we keep the stop band attenuation at the same as 16b resolution, about
308 // 96-98dB
309 //
310
311 double stopBandAtten;
312 double tbwCheat = 1.; // how much we "cheat" into aliasing
313 int halfLength;
Andy Hung6582f2b2014-01-03 12:30:41 -0800314 if (mFilterQuality == DYN_HIGH_QUALITY) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800315 // 32b coefficients, 64 length
316 useS32 = true;
317 stopBandAtten = 98.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800318 if (inSampleRate >= mSampleRate * 4) {
319 halfLength = 48;
320 } else if (inSampleRate >= mSampleRate * 2) {
321 halfLength = 40;
322 } else {
323 halfLength = 32;
324 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800325 } else if (mFilterQuality == DYN_LOW_QUALITY) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800326 // 16b coefficients, 16-32 length
327 useS32 = false;
328 stopBandAtten = 80.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800329 if (inSampleRate >= mSampleRate * 4) {
330 halfLength = 24;
331 } else if (inSampleRate >= mSampleRate * 2) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800332 halfLength = 16;
333 } else {
334 halfLength = 8;
335 }
Andy Hunga3bb9a32014-02-10 15:00:16 -0800336 if (inSampleRate <= mSampleRate) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800337 tbwCheat = 1.05;
338 } else {
339 tbwCheat = 1.03;
340 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800341 } else { // DYN_MED_QUALITY
Andy Hung86eae0e2013-12-09 12:12:46 -0800342 // 16b coefficients, 32-64 length
Andy Hung6582f2b2014-01-03 12:30:41 -0800343 // note: > 64 length filters with 16b coefs can have quantization noise problems
Andy Hung86eae0e2013-12-09 12:12:46 -0800344 useS32 = false;
345 stopBandAtten = 84.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800346 if (inSampleRate >= mSampleRate * 4) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800347 halfLength = 32;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800348 } else if (inSampleRate >= mSampleRate * 2) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800349 halfLength = 24;
350 } else {
351 halfLength = 16;
352 }
Andy Hunga3bb9a32014-02-10 15:00:16 -0800353 if (inSampleRate <= mSampleRate) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800354 tbwCheat = 1.03;
355 } else {
356 tbwCheat = 1.01;
357 }
358 }
359
360 // determine the number of polyphases in the filterbank.
361 // for 16b, it is desirable to have 2^(16/2) = 256 phases.
362 // https://ccrma.stanford.edu/~jos/resample/Relation_Interpolation_Error_Quantization.html
363 //
364 // We are a bit more lax on this.
365
366 int phases = mSampleRate / gcd(mSampleRate, inSampleRate);
367
Andy Hung6582f2b2014-01-03 12:30:41 -0800368 // TODO: Once dynamic sample rate change is an option, the code below
369 // should be modified to execute only when dynamic sample rate change is enabled.
370 //
371 // as above, #phases less than 63 is too few phases for accurate linear interpolation.
372 // we increase the phases to compensate, but more phases means more memory per
373 // filter and more time to compute the filter.
374 //
375 // if we know that the filter will be used for dynamic sample rate changes,
376 // that would allow us skip this part for fixed sample rate resamplers.
377 //
378 while (phases<63) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800379 phases *= 2; // this code only needed to support dynamic rate changes
380 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800381
Andy Hung86eae0e2013-12-09 12:12:46 -0800382 if (phases>=256) { // too many phases, always interpolate
383 phases = 127;
384 }
385
386 // create the filter
387 mConstants.set(phases, halfLength, inSampleRate, mSampleRate);
Andy Hung771386e2014-04-08 18:44:38 -0700388 createKaiserFir(mConstants, stopBandAtten,
389 inSampleRate, mSampleRate, tbwCheat);
Andy Hung86eae0e2013-12-09 12:12:46 -0800390 } // End Kaiser filter
391
392 // update phase and state based on the new filter.
393 const Constants& c(mConstants);
394 mInBuffer.resize(mChannelCount, c.mHalfNumCoefs);
395 const uint32_t phaseWrapLimit = c.mL << c.mShift;
396 // try to preserve as much of the phase fraction as possible for on-the-fly changes
397 mPhaseFraction = static_cast<unsigned long long>(mPhaseFraction)
398 * phaseWrapLimit / oldPhaseWrapLimit;
399 mPhaseFraction %= phaseWrapLimit; // should not do anything, but just in case.
400 mPhaseIncrement = static_cast<uint32_t>(static_cast<double>(phaseWrapLimit)
401 * inSampleRate / mSampleRate);
402
403 // determine which resampler to use
404 // check if locked phase (works only if mPhaseIncrement has no "fractional phase bits")
405 int locked = (mPhaseIncrement << (sizeof(mPhaseIncrement)*8 - c.mShift)) == 0;
406 int stride = (c.mHalfNumCoefs&7)==0 ? 16 : (c.mHalfNumCoefs&3)==0 ? 8 : 2;
407 if (locked) {
408 mPhaseFraction = mPhaseFraction >> c.mShift << c.mShift; // remove fractional phase
409 }
Andy Hung83be2562014-02-03 14:11:09 -0800410
Andy Hung771386e2014-04-08 18:44:38 -0700411 setResampler(RESAMPLETYPE(mChannelCount, locked, stride));
Andy Hung86eae0e2013-12-09 12:12:46 -0800412#ifdef DEBUG_RESAMPLER
413 printf("channels:%d %s stride:%d %s coef:%d shift:%d\n",
414 mChannelCount, locked ? "locked" : "interpolated",
415 stride, useS32 ? "S32" : "S16", 2*c.mHalfNumCoefs, c.mShift);
416#endif
417}
418
Andy Hung771386e2014-04-08 18:44:38 -0700419template<typename TC, typename TI, typename TO>
420void AudioResamplerDyn<TC, TI, TO>::resample(int32_t* out, size_t outFrameCount,
Andy Hung86eae0e2013-12-09 12:12:46 -0800421 AudioBufferProvider* provider)
422{
Andy Hung771386e2014-04-08 18:44:38 -0700423 (this->*mResampleFunc)(reinterpret_cast<TO*>(out), outFrameCount, provider);
424}
Andy Hung86eae0e2013-12-09 12:12:46 -0800425
Andy Hung771386e2014-04-08 18:44:38 -0700426template<typename TC, typename TI, typename TO>
427void AudioResamplerDyn<TC, TI, TO>::setResampler(unsigned resampleType)
428{
Andy Hung83be2562014-02-03 14:11:09 -0800429 // stride 16 (falls back to stride 2 for machines that do not support NEON)
Andy Hung771386e2014-04-08 18:44:38 -0700430 switch (resampleType) {
431 case RESAMPLETYPE(1, true, 16):
432 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, true, 16>;
433 return;
434 case RESAMPLETYPE(2, true, 16):
435 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, true, 16>;
436 return;
437 case RESAMPLETYPE(1, false, 16):
438 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, false, 16>;
439 return;
440 case RESAMPLETYPE(2, false, 16):
441 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, false, 16>;
442 return;
Andy Hung86eae0e2013-12-09 12:12:46 -0800443 default:
Andy Hung771386e2014-04-08 18:44:38 -0700444 LOG_ALWAYS_FATAL("Invalid resampler type: %u", resampleType);
445 mResampleFunc = NULL;
446 return;
Andy Hung86eae0e2013-12-09 12:12:46 -0800447 }
448}
449
Andy Hung771386e2014-04-08 18:44:38 -0700450template<typename TC, typename TI, typename TO>
451template<int CHANNELS, bool LOCKED, int STRIDE>
452void AudioResamplerDyn<TC, TI, TO>::resample(TO* out, size_t outFrameCount,
453 AudioBufferProvider* provider)
Andy Hung86eae0e2013-12-09 12:12:46 -0800454{
455 const Constants& c(mConstants);
Andy Hung771386e2014-04-08 18:44:38 -0700456 const TC* const coefs = mConstants.mFirCoefs;
457 TI* impulse = mInBuffer.getImpulse();
Andy Hung86eae0e2013-12-09 12:12:46 -0800458 size_t inputIndex = mInputIndex;
459 uint32_t phaseFraction = mPhaseFraction;
460 const uint32_t phaseIncrement = mPhaseIncrement;
461 size_t outputIndex = 0;
462 size_t outputSampleCount = outFrameCount * 2; // stereo output
Andy Hung24781ff2014-02-19 12:45:19 -0800463 size_t inFrameCount = getInFrameCountRequired(outFrameCount);
Andy Hung86eae0e2013-12-09 12:12:46 -0800464 const uint32_t phaseWrapLimit = c.mL << c.mShift;
465
466 // NOTE: be very careful when modifying the code here. register
467 // pressure is very high and a small change might cause the compiler
468 // to generate far less efficient code.
469 // Always sanity check the result with objdump or test-resample.
470
471 // the following logic is a bit convoluted to keep the main processing loop
472 // as tight as possible with register allocation.
473 while (outputIndex < outputSampleCount) {
474 // buffer is empty, fetch a new one
475 while (mBuffer.frameCount == 0) {
476 mBuffer.frameCount = inFrameCount;
477 provider->getNextBuffer(&mBuffer,
478 calculateOutputPTS(outputIndex / 2));
479 if (mBuffer.raw == NULL) {
480 goto resample_exit;
481 }
482 if (phaseFraction >= phaseWrapLimit) { // read in data
Andy Hung771386e2014-04-08 18:44:38 -0700483 mInBuffer.template readAdvance<CHANNELS>(
484 impulse, c.mHalfNumCoefs,
485 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung86eae0e2013-12-09 12:12:46 -0800486 phaseFraction -= phaseWrapLimit;
487 while (phaseFraction >= phaseWrapLimit) {
488 inputIndex++;
489 if (inputIndex >= mBuffer.frameCount) {
490 inputIndex -= mBuffer.frameCount;
491 provider->releaseBuffer(&mBuffer);
492 break;
493 }
Andy Hung771386e2014-04-08 18:44:38 -0700494 mInBuffer.template readAdvance<CHANNELS>(
495 impulse, c.mHalfNumCoefs,
496 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung86eae0e2013-12-09 12:12:46 -0800497 phaseFraction -= phaseWrapLimit;
498 }
499 }
500 }
Andy Hung771386e2014-04-08 18:44:38 -0700501 const TI* const in = reinterpret_cast<const TI*>(mBuffer.raw);
Andy Hung86eae0e2013-12-09 12:12:46 -0800502 const size_t frameCount = mBuffer.frameCount;
503 const int coefShift = c.mShift;
504 const int halfNumCoefs = c.mHalfNumCoefs;
Andy Hung771386e2014-04-08 18:44:38 -0700505 const TO* const volumeSimd = mVolumeSimd;
Andy Hung86eae0e2013-12-09 12:12:46 -0800506
507 // reread the last input in.
Andy Hung771386e2014-04-08 18:44:38 -0700508 mInBuffer.template readAgain<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
Andy Hung86eae0e2013-12-09 12:12:46 -0800509
510 // main processing loop
511 while (CC_LIKELY(outputIndex < outputSampleCount)) {
512 // caution: fir() is inlined and may be large.
513 // output will be loaded with the appropriate values
514 //
515 // from the input samples in impulse[-halfNumCoefs+1]... impulse[halfNumCoefs]
516 // from the polyphase filter of (phaseFraction / phaseWrapLimit) in coefs.
517 //
518 fir<CHANNELS, LOCKED, STRIDE>(
519 &out[outputIndex],
520 phaseFraction, phaseWrapLimit,
521 coefShift, halfNumCoefs, coefs,
522 impulse, volumeSimd);
523 outputIndex += 2;
524
525 phaseFraction += phaseIncrement;
526 while (phaseFraction >= phaseWrapLimit) {
527 inputIndex++;
528 if (inputIndex >= frameCount) {
529 goto done; // need a new buffer
530 }
Andy Hung771386e2014-04-08 18:44:38 -0700531 mInBuffer.template readAdvance<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
Andy Hung86eae0e2013-12-09 12:12:46 -0800532 phaseFraction -= phaseWrapLimit;
533 }
534 }
535done:
536 // often arrives here when input buffer runs out
537 if (inputIndex >= frameCount) {
538 inputIndex -= frameCount;
539 provider->releaseBuffer(&mBuffer);
540 // mBuffer.frameCount MUST be zero here.
541 }
542 }
543
544resample_exit:
545 mInBuffer.setImpulse(impulse);
546 mInputIndex = inputIndex;
547 mPhaseFraction = phaseFraction;
548}
549
Andy Hung771386e2014-04-08 18:44:38 -0700550/* instantiate templates used by AudioResampler::create */
551template class AudioResamplerDyn<float, float, float>;
552template class AudioResamplerDyn<int16_t, int16_t, int32_t>;
553template class AudioResamplerDyn<int32_t, int16_t, int32_t>;
554
Andy Hung86eae0e2013-12-09 12:12:46 -0800555// ----------------------------------------------------------------------------
556}; // namespace android