blob: 043c8035e324f6448d7a1a6ddbcb25ee7838f52a [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
Andy Hung86eae0e2013-12-09 12:12:46 -080041/*
42 * InBuffer is a type agnostic input buffer.
43 *
44 * Layout of the state buffer for halfNumCoefs=8.
45 *
46 * [rrrrrrppppppppnnnnnnnnrrrrrrrrrrrrrrrrrrr.... rrrrrrr]
47 * S I R
48 *
49 * S = mState
50 * I = mImpulse
51 * R = mRingFull
52 * p = past samples, convoluted with the (p)ositive side of sinc()
53 * n = future samples, convoluted with the (n)egative side of sinc()
54 * r = extra space for implementing the ring buffer
55 */
56
Andy Hung771386e2014-04-08 18:44:38 -070057template<typename TC, typename TI, typename TO>
58AudioResamplerDyn<TC, TI, TO>::InBuffer::InBuffer()
59 : mState(NULL), mImpulse(NULL), mRingFull(NULL), mStateCount(0)
60{
Andy Hung86eae0e2013-12-09 12:12:46 -080061}
62
Andy Hung771386e2014-04-08 18:44:38 -070063template<typename TC, typename TI, typename TO>
64AudioResamplerDyn<TC, TI, TO>::InBuffer::~InBuffer()
65{
Andy Hung86eae0e2013-12-09 12:12:46 -080066 init();
67}
68
Andy Hung771386e2014-04-08 18:44:38 -070069template<typename TC, typename TI, typename TO>
70void AudioResamplerDyn<TC, TI, TO>::InBuffer::init()
71{
Andy Hung86eae0e2013-12-09 12:12:46 -080072 free(mState);
73 mState = NULL;
74 mImpulse = NULL;
75 mRingFull = NULL;
Andy Hung771386e2014-04-08 18:44:38 -070076 mStateCount = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -080077}
78
79// resizes the state buffer to accommodate the appropriate filter length
Andy Hung771386e2014-04-08 18:44:38 -070080template<typename TC, typename TI, typename TO>
81void AudioResamplerDyn<TC, TI, TO>::InBuffer::resize(int CHANNELS, int halfNumCoefs)
82{
Andy Hung86eae0e2013-12-09 12:12:46 -080083 // calculate desired state size
Andy Hung771386e2014-04-08 18:44:38 -070084 int stateCount = halfNumCoefs * CHANNELS * 2 * kStateSizeMultipleOfFilterLength;
Andy Hung86eae0e2013-12-09 12:12:46 -080085
86 // check if buffer needs resizing
87 if (mState
Andy Hung771386e2014-04-08 18:44:38 -070088 && stateCount == mStateCount
89 && mRingFull-mState == mStateCount-halfNumCoefs*CHANNELS) {
Andy Hung86eae0e2013-12-09 12:12:46 -080090 return;
91 }
92
93 // create new buffer
Andy Hung771386e2014-04-08 18:44:38 -070094 TI* state;
95 (void)posix_memalign(reinterpret_cast<void**>(&state), 32, stateCount*sizeof(*state));
96 memset(state, 0, stateCount*sizeof(*state));
Andy Hung86eae0e2013-12-09 12:12:46 -080097
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 }
Andy Hung771386e2014-04-08 18:44:38 -0700108 if (srcHi > mState + mStateCount) {
109 srcHi = mState + mStateCount;
Andy Hung86eae0e2013-12-09 12:12:46 -0800110 }
111 memcpy(dst, srcLo, (srcHi - srcLo) * sizeof(*srcLo));
112 free(mState);
113 }
114
115 // set class member vars
116 mState = state;
Andy Hung771386e2014-04-08 18:44:38 -0700117 mStateCount = stateCount;
118 mImpulse = state + halfNumCoefs*CHANNELS; // actually one sample greater than needed
119 mRingFull = state + mStateCount - halfNumCoefs*CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800120}
121
122// copy in the input data into the head (impulse+halfNumCoefs) of the buffer.
Andy Hung771386e2014-04-08 18:44:38 -0700123template<typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -0800124template<int CHANNELS>
Andy Hung771386e2014-04-08 18:44:38 -0700125void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAgain(TI*& impulse, const int halfNumCoefs,
126 const TI* const in, const size_t inputIndex)
127{
128 TI* head = impulse + halfNumCoefs*CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800129 for (size_t i=0 ; i<CHANNELS ; i++) {
130 head[i] = in[inputIndex*CHANNELS + i];
131 }
132}
133
134// advance the impulse pointer, and load in data into the head (impulse+halfNumCoefs)
Andy Hung771386e2014-04-08 18:44:38 -0700135template<typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -0800136template<int CHANNELS>
Andy Hung771386e2014-04-08 18:44:38 -0700137void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAdvance(TI*& impulse, const int halfNumCoefs,
138 const TI* const in, const size_t inputIndex)
139{
Andy Hung86eae0e2013-12-09 12:12:46 -0800140 impulse += CHANNELS;
141
142 if (CC_UNLIKELY(impulse >= mRingFull)) {
143 const size_t shiftDown = mRingFull - mState - halfNumCoefs*CHANNELS;
144 memcpy(mState, mState+shiftDown, halfNumCoefs*CHANNELS*2*sizeof(TI));
145 impulse -= shiftDown;
146 }
147 readAgain<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
148}
149
Andy Hung771386e2014-04-08 18:44:38 -0700150template<typename TC, typename TI, typename TO>
151void AudioResamplerDyn<TC, TI, TO>::Constants::set(
Andy Hung86eae0e2013-12-09 12:12:46 -0800152 int L, int halfNumCoefs, int inSampleRate, int outSampleRate)
153{
154 int bits = 0;
155 int lscale = inSampleRate/outSampleRate < 2 ? L - 1 :
156 static_cast<int>(static_cast<uint64_t>(L)*inSampleRate/outSampleRate);
157 for (int i=lscale; i; ++bits, i>>=1)
158 ;
159 mL = L;
160 mShift = kNumPhaseBits - bits;
161 mHalfNumCoefs = halfNumCoefs;
162}
163
Andy Hung771386e2014-04-08 18:44:38 -0700164template<typename TC, typename TI, typename TO>
Andy Hung3348e362014-07-07 10:21:44 -0700165AudioResamplerDyn<TC, TI, TO>::AudioResamplerDyn(
Andy Hung86eae0e2013-12-09 12:12:46 -0800166 int inChannelCount, int32_t sampleRate, src_quality quality)
Andy Hung3348e362014-07-07 10:21:44 -0700167 : AudioResampler(inChannelCount, sampleRate, quality),
Andy Hung771386e2014-04-08 18:44:38 -0700168 mResampleFunc(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY),
Andy Hung6582f2b2014-01-03 12:30:41 -0800169 mCoefBuffer(NULL)
Andy Hung86eae0e2013-12-09 12:12:46 -0800170{
171 mVolumeSimd[0] = mVolumeSimd[1] = 0;
Andy Hung1af34082014-02-19 17:42:25 -0800172 // The AudioResampler base class assumes we are always ready for 1:1 resampling.
173 // We reset mInSampleRate to 0, so setSampleRate() will calculate filters for
174 // setSampleRate() for 1:1. (May be removed if precalculated filters are used.)
175 mInSampleRate = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800176 mConstants.set(128, 8, mSampleRate, mSampleRate); // TODO: set better
177}
178
Andy Hung771386e2014-04-08 18:44:38 -0700179template<typename TC, typename TI, typename TO>
180AudioResamplerDyn<TC, TI, TO>::~AudioResamplerDyn()
181{
Andy Hung86eae0e2013-12-09 12:12:46 -0800182 free(mCoefBuffer);
183}
184
Andy Hung771386e2014-04-08 18:44:38 -0700185template<typename TC, typename TI, typename TO>
186void AudioResamplerDyn<TC, TI, TO>::init()
187{
Andy Hung86eae0e2013-12-09 12:12:46 -0800188 mFilterSampleRate = 0; // always trigger new filter generation
189 mInBuffer.init();
190}
191
Andy Hung771386e2014-04-08 18:44:38 -0700192template<typename TC, typename TI, typename TO>
193void AudioResamplerDyn<TC, TI, TO>::setVolume(int16_t left, int16_t right)
194{
Andy Hung86eae0e2013-12-09 12:12:46 -0800195 AudioResampler::setVolume(left, right);
Andy Hung771386e2014-04-08 18:44:38 -0700196 // volume is applied on the output type.
197 if (is_same<TO, float>::value || is_same<TO, double>::value) {
198 const TO scale = 1. / (1UL << 12);
199 mVolumeSimd[0] = static_cast<TO>(left) * scale;
200 mVolumeSimd[1] = static_cast<TO>(right) * scale;
201 } else {
202 mVolumeSimd[0] = static_cast<int32_t>(left) << 16;
203 mVolumeSimd[1] = static_cast<int32_t>(right) << 16;
204 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800205}
206
Andy Hung771386e2014-04-08 18:44:38 -0700207template<typename T> T max(T a, T b) {return a > b ? a : b;}
Andy Hung86eae0e2013-12-09 12:12:46 -0800208
Andy Hung771386e2014-04-08 18:44:38 -0700209template<typename T> T absdiff(T a, T b) {return a > b ? a - b : b - a;}
Andy Hung86eae0e2013-12-09 12:12:46 -0800210
Andy Hung771386e2014-04-08 18:44:38 -0700211template<typename TC, typename TI, typename TO>
212void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c,
213 double stopBandAtten, int inSampleRate, int outSampleRate, double tbwCheat)
214{
215 TC* buf;
Andy Hung86eae0e2013-12-09 12:12:46 -0800216 static const double atten = 0.9998; // to avoid ripple overflow
217 double fcr;
218 double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten);
219
Andy Hung771386e2014-04-08 18:44:38 -0700220 (void)posix_memalign(reinterpret_cast<void**>(&buf), 32, (c.mL+1)*c.mHalfNumCoefs*sizeof(TC));
Andy Hung86eae0e2013-12-09 12:12:46 -0800221 if (inSampleRate < outSampleRate) { // upsample
222 fcr = max(0.5*tbwCheat - tbw/2, tbw/2);
223 } else { // downsample
224 fcr = max(0.5*tbwCheat*outSampleRate/inSampleRate - tbw/2, tbw/2);
225 }
226 // create and set filter
227 firKaiserGen(buf, c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten);
Andy Hung771386e2014-04-08 18:44:38 -0700228 c.mFirCoefs = buf;
Andy Hung86eae0e2013-12-09 12:12:46 -0800229 if (mCoefBuffer) {
230 free(mCoefBuffer);
231 }
232 mCoefBuffer = buf;
233#ifdef DEBUG_RESAMPLER
234 // print basic filter stats
235 printf("L:%d hnc:%d stopBandAtten:%lf fcr:%lf atten:%lf tbw:%lf\n",
236 c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten, tbw);
237 // test the filter and report results
238 double fp = (fcr - tbw/2)/c.mL;
239 double fs = (fcr + tbw/2)/c.mL;
Andy Hung6582f2b2014-01-03 12:30:41 -0800240 double passMin, passMax, passRipple;
241 double stopMax, stopRipple;
242 testFir(buf, c.mL, c.mHalfNumCoefs, fp, fs, /*passSteps*/ 1000, /*stopSteps*/ 100000,
243 passMin, passMax, passRipple, stopMax, stopRipple);
244 printf("passband(%lf, %lf): %.8lf %.8lf %.8lf\n", 0., fp, passMin, passMax, passRipple);
245 printf("stopband(%lf, %lf): %.8lf %.3lf\n", fs, 0.5, stopMax, stopRipple);
Andy Hung86eae0e2013-12-09 12:12:46 -0800246#endif
247}
248
Andy Hung6582f2b2014-01-03 12:30:41 -0800249// recursive gcd. Using objdump, it appears the tail recursion is converted to a while loop.
Andy Hung771386e2014-04-08 18:44:38 -0700250static int gcd(int n, int m)
251{
Andy Hung86eae0e2013-12-09 12:12:46 -0800252 if (m == 0) {
253 return n;
254 }
255 return gcd(m, n % m);
256}
257
Andy Hung6582f2b2014-01-03 12:30:41 -0800258static bool isClose(int32_t newSampleRate, int32_t prevSampleRate,
Andy Hung771386e2014-04-08 18:44:38 -0700259 int32_t filterSampleRate, int32_t outSampleRate)
260{
Andy Hung6582f2b2014-01-03 12:30:41 -0800261
262 // different upsampling ratios do not need a filter change.
263 if (filterSampleRate != 0
264 && filterSampleRate < outSampleRate
265 && newSampleRate < outSampleRate)
266 return true;
267
268 // check design criteria again if downsampling is detected.
Andy Hung86eae0e2013-12-09 12:12:46 -0800269 int pdiff = absdiff(newSampleRate, prevSampleRate);
270 int adiff = absdiff(newSampleRate, filterSampleRate);
271
272 // allow up to 6% relative change increments.
273 // allow up to 12% absolute change increments (from filter design)
274 return pdiff < prevSampleRate>>4 && adiff < filterSampleRate>>3;
275}
276
Andy Hung771386e2014-04-08 18:44:38 -0700277template<typename TC, typename TI, typename TO>
278void AudioResamplerDyn<TC, TI, TO>::setSampleRate(int32_t inSampleRate)
279{
Andy Hung86eae0e2013-12-09 12:12:46 -0800280 if (mInSampleRate == inSampleRate) {
281 return;
282 }
283 int32_t oldSampleRate = mInSampleRate;
284 int32_t oldHalfNumCoefs = mConstants.mHalfNumCoefs;
285 uint32_t oldPhaseWrapLimit = mConstants.mL << mConstants.mShift;
286 bool useS32 = false;
287
288 mInSampleRate = inSampleRate;
289
290 // TODO: Add precalculated Equiripple filters
291
Andy Hung6582f2b2014-01-03 12:30:41 -0800292 if (mFilterQuality != getQuality() ||
293 !isClose(inSampleRate, oldSampleRate, mFilterSampleRate, mSampleRate)) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800294 mFilterSampleRate = inSampleRate;
Andy Hung6582f2b2014-01-03 12:30:41 -0800295 mFilterQuality = getQuality();
Andy Hung86eae0e2013-12-09 12:12:46 -0800296
297 // Begin Kaiser Filter computation
298 //
299 // The quantization floor for S16 is about 96db - 10*log_10(#length) + 3dB.
300 // Keep the stop band attenuation no greater than 84-85dB for 32 length S16 filters
301 //
302 // For s32 we keep the stop band attenuation at the same as 16b resolution, about
303 // 96-98dB
304 //
305
306 double stopBandAtten;
307 double tbwCheat = 1.; // how much we "cheat" into aliasing
308 int halfLength;
Andy Hung6582f2b2014-01-03 12:30:41 -0800309 if (mFilterQuality == DYN_HIGH_QUALITY) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800310 // 32b coefficients, 64 length
311 useS32 = true;
312 stopBandAtten = 98.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800313 if (inSampleRate >= mSampleRate * 4) {
314 halfLength = 48;
315 } else if (inSampleRate >= mSampleRate * 2) {
316 halfLength = 40;
317 } else {
318 halfLength = 32;
319 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800320 } else if (mFilterQuality == DYN_LOW_QUALITY) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800321 // 16b coefficients, 16-32 length
322 useS32 = false;
323 stopBandAtten = 80.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800324 if (inSampleRate >= mSampleRate * 4) {
325 halfLength = 24;
326 } else if (inSampleRate >= mSampleRate * 2) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800327 halfLength = 16;
328 } else {
329 halfLength = 8;
330 }
Andy Hunga3bb9a32014-02-10 15:00:16 -0800331 if (inSampleRate <= mSampleRate) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800332 tbwCheat = 1.05;
333 } else {
334 tbwCheat = 1.03;
335 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800336 } else { // DYN_MED_QUALITY
Andy Hung86eae0e2013-12-09 12:12:46 -0800337 // 16b coefficients, 32-64 length
Andy Hung6582f2b2014-01-03 12:30:41 -0800338 // note: > 64 length filters with 16b coefs can have quantization noise problems
Andy Hung86eae0e2013-12-09 12:12:46 -0800339 useS32 = false;
340 stopBandAtten = 84.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800341 if (inSampleRate >= mSampleRate * 4) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800342 halfLength = 32;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800343 } else if (inSampleRate >= mSampleRate * 2) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800344 halfLength = 24;
345 } else {
346 halfLength = 16;
347 }
Andy Hunga3bb9a32014-02-10 15:00:16 -0800348 if (inSampleRate <= mSampleRate) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800349 tbwCheat = 1.03;
350 } else {
351 tbwCheat = 1.01;
352 }
353 }
354
355 // determine the number of polyphases in the filterbank.
356 // for 16b, it is desirable to have 2^(16/2) = 256 phases.
357 // https://ccrma.stanford.edu/~jos/resample/Relation_Interpolation_Error_Quantization.html
358 //
359 // We are a bit more lax on this.
360
361 int phases = mSampleRate / gcd(mSampleRate, inSampleRate);
362
Andy Hung6582f2b2014-01-03 12:30:41 -0800363 // TODO: Once dynamic sample rate change is an option, the code below
364 // should be modified to execute only when dynamic sample rate change is enabled.
365 //
366 // as above, #phases less than 63 is too few phases for accurate linear interpolation.
367 // we increase the phases to compensate, but more phases means more memory per
368 // filter and more time to compute the filter.
369 //
370 // if we know that the filter will be used for dynamic sample rate changes,
371 // that would allow us skip this part for fixed sample rate resamplers.
372 //
373 while (phases<63) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800374 phases *= 2; // this code only needed to support dynamic rate changes
375 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800376
Andy Hung86eae0e2013-12-09 12:12:46 -0800377 if (phases>=256) { // too many phases, always interpolate
378 phases = 127;
379 }
380
381 // create the filter
382 mConstants.set(phases, halfLength, inSampleRate, mSampleRate);
Andy Hung771386e2014-04-08 18:44:38 -0700383 createKaiserFir(mConstants, stopBandAtten,
384 inSampleRate, mSampleRate, tbwCheat);
Andy Hung86eae0e2013-12-09 12:12:46 -0800385 } // End Kaiser filter
386
387 // update phase and state based on the new filter.
388 const Constants& c(mConstants);
389 mInBuffer.resize(mChannelCount, c.mHalfNumCoefs);
390 const uint32_t phaseWrapLimit = c.mL << c.mShift;
391 // try to preserve as much of the phase fraction as possible for on-the-fly changes
392 mPhaseFraction = static_cast<unsigned long long>(mPhaseFraction)
393 * phaseWrapLimit / oldPhaseWrapLimit;
394 mPhaseFraction %= phaseWrapLimit; // should not do anything, but just in case.
395 mPhaseIncrement = static_cast<uint32_t>(static_cast<double>(phaseWrapLimit)
396 * inSampleRate / mSampleRate);
397
398 // determine which resampler to use
399 // check if locked phase (works only if mPhaseIncrement has no "fractional phase bits")
400 int locked = (mPhaseIncrement << (sizeof(mPhaseIncrement)*8 - c.mShift)) == 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800401 if (locked) {
402 mPhaseFraction = mPhaseFraction >> c.mShift << c.mShift; // remove fractional phase
403 }
Andy Hung83be2562014-02-03 14:11:09 -0800404
Andy Hung075abae2014-04-09 19:36:43 -0700405 // stride is the minimum number of filter coefficients processed per loop iteration.
406 // We currently only allow a stride of 16 to match with SIMD processing.
407 // This means that the filter length must be a multiple of 16,
408 // or half the filter length (mHalfNumCoefs) must be a multiple of 8.
409 //
410 // Note: A stride of 2 is achieved with non-SIMD processing.
411 int stride = ((c.mHalfNumCoefs & 7) == 0) ? 16 : 2;
412 LOG_ALWAYS_FATAL_IF(stride < 16, "Resampler stride must be 16 or more");
413 LOG_ALWAYS_FATAL_IF(mChannelCount > 8 || mChannelCount < 1,
414 "Resampler channels(%d) must be between 1 to 8", mChannelCount);
415 // stride 16 (falls back to stride 2 for machines that do not support NEON)
416 if (locked) {
417 switch (mChannelCount) {
418 case 1:
419 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, true, 16>;
420 break;
421 case 2:
422 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, true, 16>;
423 break;
424 case 3:
425 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, true, 16>;
426 break;
427 case 4:
428 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, true, 16>;
429 break;
430 case 5:
431 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, true, 16>;
432 break;
433 case 6:
434 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, true, 16>;
435 break;
436 case 7:
437 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, true, 16>;
438 break;
439 case 8:
440 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, true, 16>;
441 break;
442 }
443 } else {
444 switch (mChannelCount) {
445 case 1:
446 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, false, 16>;
447 break;
448 case 2:
449 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, false, 16>;
450 break;
451 case 3:
452 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, false, 16>;
453 break;
454 case 4:
455 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, false, 16>;
456 break;
457 case 5:
458 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, false, 16>;
459 break;
460 case 6:
461 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, false, 16>;
462 break;
463 case 7:
464 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, false, 16>;
465 break;
466 case 8:
467 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, false, 16>;
468 break;
469 }
470 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800471#ifdef DEBUG_RESAMPLER
472 printf("channels:%d %s stride:%d %s coef:%d shift:%d\n",
473 mChannelCount, locked ? "locked" : "interpolated",
474 stride, useS32 ? "S32" : "S16", 2*c.mHalfNumCoefs, c.mShift);
475#endif
476}
477
Andy Hung771386e2014-04-08 18:44:38 -0700478template<typename TC, typename TI, typename TO>
479void AudioResamplerDyn<TC, TI, TO>::resample(int32_t* out, size_t outFrameCount,
Andy Hung86eae0e2013-12-09 12:12:46 -0800480 AudioBufferProvider* provider)
481{
Andy Hung771386e2014-04-08 18:44:38 -0700482 (this->*mResampleFunc)(reinterpret_cast<TO*>(out), outFrameCount, provider);
483}
Andy Hung86eae0e2013-12-09 12:12:46 -0800484
Andy Hung771386e2014-04-08 18:44:38 -0700485template<typename TC, typename TI, typename TO>
Andy Hung771386e2014-04-08 18:44:38 -0700486template<int CHANNELS, bool LOCKED, int STRIDE>
487void AudioResamplerDyn<TC, TI, TO>::resample(TO* out, size_t outFrameCount,
488 AudioBufferProvider* provider)
Andy Hung86eae0e2013-12-09 12:12:46 -0800489{
Andy Hung075abae2014-04-09 19:36:43 -0700490 // TODO Mono -> Mono is not supported. OUTPUT_CHANNELS reflects minimum of stereo out.
491 const int OUTPUT_CHANNELS = (CHANNELS < 2) ? 2 : CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800492 const Constants& c(mConstants);
Andy Hung771386e2014-04-08 18:44:38 -0700493 const TC* const coefs = mConstants.mFirCoefs;
494 TI* impulse = mInBuffer.getImpulse();
Andy Hung411cb8e2014-05-27 12:32:17 -0700495 size_t inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800496 uint32_t phaseFraction = mPhaseFraction;
497 const uint32_t phaseIncrement = mPhaseIncrement;
498 size_t outputIndex = 0;
Andy Hung075abae2014-04-09 19:36:43 -0700499 size_t outputSampleCount = outFrameCount * OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800500 const uint32_t phaseWrapLimit = c.mL << c.mShift;
Andy Hung71700742014-06-02 18:54:08 -0700501 size_t inFrameCount = (phaseIncrement * (uint64_t)outFrameCount + phaseFraction)
502 / phaseWrapLimit;
503 // sanity check that inFrameCount is in signed 32 bit integer range.
504 ALOG_ASSERT(0 <= inFrameCount && inFrameCount < (1U << 31));
505
506 //ALOGV("inFrameCount:%d outFrameCount:%d"
507 // " phaseIncrement:%u phaseFraction:%u phaseWrapLimit:%u",
508 // inFrameCount, outFrameCount, phaseIncrement, phaseFraction, phaseWrapLimit);
Andy Hung86eae0e2013-12-09 12:12:46 -0800509
510 // NOTE: be very careful when modifying the code here. register
511 // pressure is very high and a small change might cause the compiler
512 // to generate far less efficient code.
513 // Always sanity check the result with objdump or test-resample.
514
515 // the following logic is a bit convoluted to keep the main processing loop
516 // as tight as possible with register allocation.
517 while (outputIndex < outputSampleCount) {
Andy Hung71700742014-06-02 18:54:08 -0700518 //ALOGV("LOOP: inFrameCount:%d outputIndex:%d outFrameCount:%d"
519 // " phaseFraction:%u phaseWrapLimit:%u",
520 // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
521
522 // check inputIndex overflow
523 ALOG_ASSERT(inputIndex <= mBuffer.frameCount, "inputIndex%d > frameCount%d",
524 inputIndex, mBuffer.frameCount);
525 // Buffer is empty, fetch a new one if necessary (inFrameCount > 0).
526 // We may not fetch a new buffer if the existing data is sufficient.
527 while (mBuffer.frameCount == 0 && inFrameCount > 0) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800528 mBuffer.frameCount = inFrameCount;
529 provider->getNextBuffer(&mBuffer,
Andy Hung075abae2014-04-09 19:36:43 -0700530 calculateOutputPTS(outputIndex / OUTPUT_CHANNELS));
Andy Hung86eae0e2013-12-09 12:12:46 -0800531 if (mBuffer.raw == NULL) {
532 goto resample_exit;
533 }
Andy Hung411cb8e2014-05-27 12:32:17 -0700534 inFrameCount -= mBuffer.frameCount;
Andy Hung86eae0e2013-12-09 12:12:46 -0800535 if (phaseFraction >= phaseWrapLimit) { // read in data
Andy Hung771386e2014-04-08 18:44:38 -0700536 mInBuffer.template readAdvance<CHANNELS>(
537 impulse, c.mHalfNumCoefs,
538 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700539 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800540 phaseFraction -= phaseWrapLimit;
541 while (phaseFraction >= phaseWrapLimit) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800542 if (inputIndex >= mBuffer.frameCount) {
Andy Hung411cb8e2014-05-27 12:32:17 -0700543 inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800544 provider->releaseBuffer(&mBuffer);
545 break;
546 }
Andy Hung771386e2014-04-08 18:44:38 -0700547 mInBuffer.template readAdvance<CHANNELS>(
548 impulse, c.mHalfNumCoefs,
549 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700550 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800551 phaseFraction -= phaseWrapLimit;
552 }
553 }
554 }
Andy Hung771386e2014-04-08 18:44:38 -0700555 const TI* const in = reinterpret_cast<const TI*>(mBuffer.raw);
Andy Hung86eae0e2013-12-09 12:12:46 -0800556 const size_t frameCount = mBuffer.frameCount;
557 const int coefShift = c.mShift;
558 const int halfNumCoefs = c.mHalfNumCoefs;
Andy Hung771386e2014-04-08 18:44:38 -0700559 const TO* const volumeSimd = mVolumeSimd;
Andy Hung86eae0e2013-12-09 12:12:46 -0800560
Andy Hung86eae0e2013-12-09 12:12:46 -0800561 // main processing loop
562 while (CC_LIKELY(outputIndex < outputSampleCount)) {
563 // caution: fir() is inlined and may be large.
564 // output will be loaded with the appropriate values
565 //
566 // from the input samples in impulse[-halfNumCoefs+1]... impulse[halfNumCoefs]
567 // from the polyphase filter of (phaseFraction / phaseWrapLimit) in coefs.
568 //
Andy Hung71700742014-06-02 18:54:08 -0700569 //ALOGV("LOOP2: inFrameCount:%d outputIndex:%d outFrameCount:%d"
570 // " phaseFraction:%u phaseWrapLimit:%u",
571 // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
572 ALOG_ASSERT(phaseFraction < phaseWrapLimit);
Andy Hung86eae0e2013-12-09 12:12:46 -0800573 fir<CHANNELS, LOCKED, STRIDE>(
574 &out[outputIndex],
575 phaseFraction, phaseWrapLimit,
576 coefShift, halfNumCoefs, coefs,
577 impulse, volumeSimd);
Andy Hung075abae2014-04-09 19:36:43 -0700578
579 outputIndex += OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800580
581 phaseFraction += phaseIncrement;
582 while (phaseFraction >= phaseWrapLimit) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800583 if (inputIndex >= frameCount) {
584 goto done; // need a new buffer
585 }
Andy Hung771386e2014-04-08 18:44:38 -0700586 mInBuffer.template readAdvance<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700587 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800588 phaseFraction -= phaseWrapLimit;
589 }
590 }
591done:
Andy Hung71700742014-06-02 18:54:08 -0700592 // We arrive here when we're finished or when the input buffer runs out.
593 // Regardless we need to release the input buffer if we've acquired it.
594 if (inputIndex > 0) { // we've acquired a buffer (alternatively could check frameCount)
595 ALOG_ASSERT(inputIndex == frameCount, "inputIndex(%d) != frameCount(%d)",
596 inputIndex, frameCount); // must have been fully read.
Andy Hung411cb8e2014-05-27 12:32:17 -0700597 inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800598 provider->releaseBuffer(&mBuffer);
Andy Hung411cb8e2014-05-27 12:32:17 -0700599 ALOG_ASSERT(mBuffer.frameCount == 0);
Andy Hung86eae0e2013-12-09 12:12:46 -0800600 }
601 }
602
603resample_exit:
Andy Hung71700742014-06-02 18:54:08 -0700604 // inputIndex must be zero in all three cases:
605 // (1) the buffer never was been acquired; (2) the buffer was
606 // released at "done:"; or (3) getNextBuffer() failed.
607 ALOG_ASSERT(inputIndex == 0, "Releasing: inputindex:%d frameCount:%d phaseFraction:%u",
608 inputIndex, mBuffer.frameCount, phaseFraction);
609 ALOG_ASSERT(mBuffer.frameCount == 0); // there must be no frames in the buffer
Andy Hung86eae0e2013-12-09 12:12:46 -0800610 mInBuffer.setImpulse(impulse);
Andy Hung86eae0e2013-12-09 12:12:46 -0800611 mPhaseFraction = phaseFraction;
612}
613
Andy Hung771386e2014-04-08 18:44:38 -0700614/* instantiate templates used by AudioResampler::create */
615template class AudioResamplerDyn<float, float, float>;
616template class AudioResamplerDyn<int16_t, int16_t, int32_t>;
617template class AudioResamplerDyn<int32_t, int16_t, int32_t>;
618
Andy Hung86eae0e2013-12-09 12:12:46 -0800619// ----------------------------------------------------------------------------
620}; // namespace android