blob: f137ed9c5665716e97b3737889adbea5db0e920f [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>
Andy Hung5e58b0a2014-06-23 19:07:29 -070030#include <audio_utils/primitives.h>
Andy Hung86eae0e2013-12-09 12:12:46 -080031
Henrik Smiding841920d2016-02-15 16:20:45 +010032#include "AudioResamplerFirOps.h" // USE_NEON, USE_SSE and USE_INLINE_ASSEMBLY defined here
Andy Hung86eae0e2013-12-09 12:12:46 -080033#include "AudioResamplerFirProcess.h"
34#include "AudioResamplerFirProcessNeon.h"
Henrik Smiding841920d2016-02-15 16:20:45 +010035#include "AudioResamplerFirProcessSSE.h"
Andy Hung86eae0e2013-12-09 12:12:46 -080036#include "AudioResamplerFirGen.h" // requires math.h
37#include "AudioResamplerDyn.h"
38
39//#define DEBUG_RESAMPLER
40
41namespace android {
42
Andy Hung86eae0e2013-12-09 12:12:46 -080043/*
44 * InBuffer is a type agnostic input buffer.
45 *
46 * Layout of the state buffer for halfNumCoefs=8.
47 *
48 * [rrrrrrppppppppnnnnnnnnrrrrrrrrrrrrrrrrrrr.... rrrrrrr]
49 * S I R
50 *
51 * S = mState
52 * I = mImpulse
53 * R = mRingFull
54 * p = past samples, convoluted with the (p)ositive side of sinc()
55 * n = future samples, convoluted with the (n)egative side of sinc()
56 * r = extra space for implementing the ring buffer
57 */
58
Andy Hung771386e2014-04-08 18:44:38 -070059template<typename TC, typename TI, typename TO>
60AudioResamplerDyn<TC, TI, TO>::InBuffer::InBuffer()
61 : mState(NULL), mImpulse(NULL), mRingFull(NULL), mStateCount(0)
62{
Andy Hung86eae0e2013-12-09 12:12:46 -080063}
64
Andy Hung771386e2014-04-08 18:44:38 -070065template<typename TC, typename TI, typename TO>
66AudioResamplerDyn<TC, TI, TO>::InBuffer::~InBuffer()
67{
Andy Hung86eae0e2013-12-09 12:12:46 -080068 init();
69}
70
Andy Hung771386e2014-04-08 18:44:38 -070071template<typename TC, typename TI, typename TO>
72void AudioResamplerDyn<TC, TI, TO>::InBuffer::init()
73{
Andy Hung86eae0e2013-12-09 12:12:46 -080074 free(mState);
75 mState = NULL;
76 mImpulse = NULL;
77 mRingFull = NULL;
Andy Hung771386e2014-04-08 18:44:38 -070078 mStateCount = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -080079}
80
81// resizes the state buffer to accommodate the appropriate filter length
Andy Hung771386e2014-04-08 18:44:38 -070082template<typename TC, typename TI, typename TO>
83void AudioResamplerDyn<TC, TI, TO>::InBuffer::resize(int CHANNELS, int halfNumCoefs)
84{
Andy Hung86eae0e2013-12-09 12:12:46 -080085 // calculate desired state size
Glenn Kastena4daf0b2014-07-28 16:34:45 -070086 size_t stateCount = halfNumCoefs * CHANNELS * 2 * kStateSizeMultipleOfFilterLength;
Andy Hung86eae0e2013-12-09 12:12:46 -080087
88 // check if buffer needs resizing
89 if (mState
Andy Hung771386e2014-04-08 18:44:38 -070090 && stateCount == mStateCount
Glenn Kastena4daf0b2014-07-28 16:34:45 -070091 && mRingFull-mState == (ssize_t) (mStateCount-halfNumCoefs*CHANNELS)) {
Andy Hung86eae0e2013-12-09 12:12:46 -080092 return;
93 }
94
95 // create new buffer
Glenn Kastena4daf0b2014-07-28 16:34:45 -070096 TI* state = NULL;
Andy Hung771386e2014-04-08 18:44:38 -070097 (void)posix_memalign(reinterpret_cast<void**>(&state), 32, stateCount*sizeof(*state));
98 memset(state, 0, stateCount*sizeof(*state));
Andy Hung86eae0e2013-12-09 12:12:46 -080099
100 // attempt to preserve state
101 if (mState) {
102 TI* srcLo = mImpulse - halfNumCoefs*CHANNELS;
103 TI* srcHi = mImpulse + halfNumCoefs*CHANNELS;
104 TI* dst = state;
105
106 if (srcLo < mState) {
107 dst += mState-srcLo;
108 srcLo = mState;
109 }
Andy Hung771386e2014-04-08 18:44:38 -0700110 if (srcHi > mState + mStateCount) {
111 srcHi = mState + mStateCount;
Andy Hung86eae0e2013-12-09 12:12:46 -0800112 }
113 memcpy(dst, srcLo, (srcHi - srcLo) * sizeof(*srcLo));
114 free(mState);
115 }
116
117 // set class member vars
118 mState = state;
Andy Hung771386e2014-04-08 18:44:38 -0700119 mStateCount = stateCount;
120 mImpulse = state + halfNumCoefs*CHANNELS; // actually one sample greater than needed
121 mRingFull = state + mStateCount - halfNumCoefs*CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800122}
123
124// copy in the input data into the head (impulse+halfNumCoefs) of the buffer.
Andy Hung771386e2014-04-08 18:44:38 -0700125template<typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -0800126template<int CHANNELS>
Andy Hung771386e2014-04-08 18:44:38 -0700127void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAgain(TI*& impulse, const int halfNumCoefs,
128 const TI* const in, const size_t inputIndex)
129{
130 TI* head = impulse + halfNumCoefs*CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800131 for (size_t i=0 ; i<CHANNELS ; i++) {
132 head[i] = in[inputIndex*CHANNELS + i];
133 }
134}
135
136// advance the impulse pointer, and load in data into the head (impulse+halfNumCoefs)
Andy Hung771386e2014-04-08 18:44:38 -0700137template<typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -0800138template<int CHANNELS>
Andy Hung771386e2014-04-08 18:44:38 -0700139void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAdvance(TI*& impulse, const int halfNumCoefs,
140 const TI* const in, const size_t inputIndex)
141{
Andy Hung86eae0e2013-12-09 12:12:46 -0800142 impulse += CHANNELS;
143
144 if (CC_UNLIKELY(impulse >= mRingFull)) {
145 const size_t shiftDown = mRingFull - mState - halfNumCoefs*CHANNELS;
146 memcpy(mState, mState+shiftDown, halfNumCoefs*CHANNELS*2*sizeof(TI));
147 impulse -= shiftDown;
148 }
149 readAgain<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
150}
151
Andy Hung771386e2014-04-08 18:44:38 -0700152template<typename TC, typename TI, typename TO>
153void AudioResamplerDyn<TC, TI, TO>::Constants::set(
Andy Hung86eae0e2013-12-09 12:12:46 -0800154 int L, int halfNumCoefs, int inSampleRate, int outSampleRate)
155{
156 int bits = 0;
157 int lscale = inSampleRate/outSampleRate < 2 ? L - 1 :
158 static_cast<int>(static_cast<uint64_t>(L)*inSampleRate/outSampleRate);
159 for (int i=lscale; i; ++bits, i>>=1)
160 ;
161 mL = L;
162 mShift = kNumPhaseBits - bits;
163 mHalfNumCoefs = halfNumCoefs;
164}
165
Andy Hung771386e2014-04-08 18:44:38 -0700166template<typename TC, typename TI, typename TO>
Andy Hung3348e362014-07-07 10:21:44 -0700167AudioResamplerDyn<TC, TI, TO>::AudioResamplerDyn(
Andy Hung86eae0e2013-12-09 12:12:46 -0800168 int inChannelCount, int32_t sampleRate, src_quality quality)
Andy Hung3348e362014-07-07 10:21:44 -0700169 : AudioResampler(inChannelCount, sampleRate, quality),
Andy Hung771386e2014-04-08 18:44:38 -0700170 mResampleFunc(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY),
Andy Hung6582f2b2014-01-03 12:30:41 -0800171 mCoefBuffer(NULL)
Andy Hung86eae0e2013-12-09 12:12:46 -0800172{
173 mVolumeSimd[0] = mVolumeSimd[1] = 0;
Andy Hung1af34082014-02-19 17:42:25 -0800174 // The AudioResampler base class assumes we are always ready for 1:1 resampling.
175 // We reset mInSampleRate to 0, so setSampleRate() will calculate filters for
176 // setSampleRate() for 1:1. (May be removed if precalculated filters are used.)
177 mInSampleRate = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800178 mConstants.set(128, 8, mSampleRate, mSampleRate); // TODO: set better
179}
180
Andy Hung771386e2014-04-08 18:44:38 -0700181template<typename TC, typename TI, typename TO>
182AudioResamplerDyn<TC, TI, TO>::~AudioResamplerDyn()
183{
Andy Hung86eae0e2013-12-09 12:12:46 -0800184 free(mCoefBuffer);
185}
186
Andy Hung771386e2014-04-08 18:44:38 -0700187template<typename TC, typename TI, typename TO>
188void AudioResamplerDyn<TC, TI, TO>::init()
189{
Andy Hung86eae0e2013-12-09 12:12:46 -0800190 mFilterSampleRate = 0; // always trigger new filter generation
191 mInBuffer.init();
192}
193
Andy Hung771386e2014-04-08 18:44:38 -0700194template<typename TC, typename TI, typename TO>
Andy Hung5e58b0a2014-06-23 19:07:29 -0700195void AudioResamplerDyn<TC, TI, TO>::setVolume(float left, float right)
Andy Hung771386e2014-04-08 18:44:38 -0700196{
Andy Hung86eae0e2013-12-09 12:12:46 -0800197 AudioResampler::setVolume(left, right);
Andy Hung771386e2014-04-08 18:44:38 -0700198 if (is_same<TO, float>::value || is_same<TO, double>::value) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700199 mVolumeSimd[0] = static_cast<TO>(left);
200 mVolumeSimd[1] = static_cast<TO>(right);
201 } else { // integer requires scaling to U4_28 (rounding down)
202 // integer volumes are clamped to 0 to UNITY_GAIN so there
203 // are no issues with signed overflow.
204 mVolumeSimd[0] = u4_28_from_float(clampFloatVol(left));
205 mVolumeSimd[1] = u4_28_from_float(clampFloatVol(right));
Andy Hung771386e2014-04-08 18:44:38 -0700206 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800207}
208
Andy Hung771386e2014-04-08 18:44:38 -0700209template<typename T> T max(T a, T b) {return a > b ? a : b;}
Andy Hung86eae0e2013-12-09 12:12:46 -0800210
Andy Hung771386e2014-04-08 18:44:38 -0700211template<typename T> T absdiff(T a, T b) {return a > b ? a - b : b - a;}
Andy Hung86eae0e2013-12-09 12:12:46 -0800212
Andy Hung771386e2014-04-08 18:44:38 -0700213template<typename TC, typename TI, typename TO>
214void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c,
215 double stopBandAtten, int inSampleRate, int outSampleRate, double tbwCheat)
216{
Glenn Kastena4daf0b2014-07-28 16:34:45 -0700217 TC* buf = NULL;
Andy Hung86eae0e2013-12-09 12:12:46 -0800218 static const double atten = 0.9998; // to avoid ripple overflow
219 double fcr;
220 double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten);
221
Andy Hung771386e2014-04-08 18:44:38 -0700222 (void)posix_memalign(reinterpret_cast<void**>(&buf), 32, (c.mL+1)*c.mHalfNumCoefs*sizeof(TC));
Andy Hung86eae0e2013-12-09 12:12:46 -0800223 if (inSampleRate < outSampleRate) { // upsample
224 fcr = max(0.5*tbwCheat - tbw/2, tbw/2);
225 } else { // downsample
226 fcr = max(0.5*tbwCheat*outSampleRate/inSampleRate - tbw/2, tbw/2);
227 }
228 // create and set filter
229 firKaiserGen(buf, c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten);
Andy Hung771386e2014-04-08 18:44:38 -0700230 c.mFirCoefs = buf;
Andy Hung86eae0e2013-12-09 12:12:46 -0800231 if (mCoefBuffer) {
232 free(mCoefBuffer);
233 }
234 mCoefBuffer = buf;
235#ifdef DEBUG_RESAMPLER
236 // print basic filter stats
237 printf("L:%d hnc:%d stopBandAtten:%lf fcr:%lf atten:%lf tbw:%lf\n",
238 c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten, tbw);
239 // test the filter and report results
240 double fp = (fcr - tbw/2)/c.mL;
241 double fs = (fcr + tbw/2)/c.mL;
Andy Hung6582f2b2014-01-03 12:30:41 -0800242 double passMin, passMax, passRipple;
243 double stopMax, stopRipple;
244 testFir(buf, c.mL, c.mHalfNumCoefs, fp, fs, /*passSteps*/ 1000, /*stopSteps*/ 100000,
245 passMin, passMax, passRipple, stopMax, stopRipple);
246 printf("passband(%lf, %lf): %.8lf %.8lf %.8lf\n", 0., fp, passMin, passMax, passRipple);
247 printf("stopband(%lf, %lf): %.8lf %.3lf\n", fs, 0.5, stopMax, stopRipple);
Andy Hung86eae0e2013-12-09 12:12:46 -0800248#endif
249}
250
Andy Hung6582f2b2014-01-03 12:30:41 -0800251// recursive gcd. Using objdump, it appears the tail recursion is converted to a while loop.
Andy Hung771386e2014-04-08 18:44:38 -0700252static int gcd(int n, int m)
253{
Andy Hung86eae0e2013-12-09 12:12:46 -0800254 if (m == 0) {
255 return n;
256 }
257 return gcd(m, n % m);
258}
259
Andy Hung6582f2b2014-01-03 12:30:41 -0800260static bool isClose(int32_t newSampleRate, int32_t prevSampleRate,
Andy Hung771386e2014-04-08 18:44:38 -0700261 int32_t filterSampleRate, int32_t outSampleRate)
262{
Andy Hung6582f2b2014-01-03 12:30:41 -0800263
264 // different upsampling ratios do not need a filter change.
265 if (filterSampleRate != 0
266 && filterSampleRate < outSampleRate
267 && newSampleRate < outSampleRate)
268 return true;
269
270 // check design criteria again if downsampling is detected.
Andy Hung86eae0e2013-12-09 12:12:46 -0800271 int pdiff = absdiff(newSampleRate, prevSampleRate);
272 int adiff = absdiff(newSampleRate, filterSampleRate);
273
274 // allow up to 6% relative change increments.
275 // allow up to 12% absolute change increments (from filter design)
276 return pdiff < prevSampleRate>>4 && adiff < filterSampleRate>>3;
277}
278
Andy Hung771386e2014-04-08 18:44:38 -0700279template<typename TC, typename TI, typename TO>
280void AudioResamplerDyn<TC, TI, TO>::setSampleRate(int32_t inSampleRate)
281{
Andy Hung86eae0e2013-12-09 12:12:46 -0800282 if (mInSampleRate == inSampleRate) {
283 return;
284 }
285 int32_t oldSampleRate = mInSampleRate;
286 int32_t oldHalfNumCoefs = mConstants.mHalfNumCoefs;
287 uint32_t oldPhaseWrapLimit = mConstants.mL << mConstants.mShift;
288 bool useS32 = false;
289
290 mInSampleRate = inSampleRate;
291
292 // TODO: Add precalculated Equiripple filters
293
Andy Hung6582f2b2014-01-03 12:30:41 -0800294 if (mFilterQuality != getQuality() ||
295 !isClose(inSampleRate, oldSampleRate, mFilterSampleRate, mSampleRate)) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800296 mFilterSampleRate = inSampleRate;
Andy Hung6582f2b2014-01-03 12:30:41 -0800297 mFilterQuality = getQuality();
Andy Hung86eae0e2013-12-09 12:12:46 -0800298
299 // Begin Kaiser Filter computation
300 //
301 // The quantization floor for S16 is about 96db - 10*log_10(#length) + 3dB.
302 // Keep the stop band attenuation no greater than 84-85dB for 32 length S16 filters
303 //
304 // For s32 we keep the stop band attenuation at the same as 16b resolution, about
305 // 96-98dB
306 //
307
308 double stopBandAtten;
309 double tbwCheat = 1.; // how much we "cheat" into aliasing
310 int halfLength;
Andy Hung6582f2b2014-01-03 12:30:41 -0800311 if (mFilterQuality == DYN_HIGH_QUALITY) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800312 // 32b coefficients, 64 length
313 useS32 = true;
314 stopBandAtten = 98.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800315 if (inSampleRate >= mSampleRate * 4) {
316 halfLength = 48;
317 } else if (inSampleRate >= mSampleRate * 2) {
318 halfLength = 40;
319 } else {
320 halfLength = 32;
321 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800322 } else if (mFilterQuality == DYN_LOW_QUALITY) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800323 // 16b coefficients, 16-32 length
324 useS32 = false;
325 stopBandAtten = 80.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800326 if (inSampleRate >= mSampleRate * 4) {
327 halfLength = 24;
328 } else if (inSampleRate >= mSampleRate * 2) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800329 halfLength = 16;
330 } else {
331 halfLength = 8;
332 }
Andy Hunga3bb9a32014-02-10 15:00:16 -0800333 if (inSampleRate <= mSampleRate) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800334 tbwCheat = 1.05;
335 } else {
336 tbwCheat = 1.03;
337 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800338 } else { // DYN_MED_QUALITY
Andy Hung86eae0e2013-12-09 12:12:46 -0800339 // 16b coefficients, 32-64 length
Andy Hung6582f2b2014-01-03 12:30:41 -0800340 // note: > 64 length filters with 16b coefs can have quantization noise problems
Andy Hung86eae0e2013-12-09 12:12:46 -0800341 useS32 = false;
342 stopBandAtten = 84.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800343 if (inSampleRate >= mSampleRate * 4) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800344 halfLength = 32;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800345 } else if (inSampleRate >= mSampleRate * 2) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800346 halfLength = 24;
347 } else {
348 halfLength = 16;
349 }
Andy Hunga3bb9a32014-02-10 15:00:16 -0800350 if (inSampleRate <= mSampleRate) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800351 tbwCheat = 1.03;
352 } else {
353 tbwCheat = 1.01;
354 }
355 }
356
357 // determine the number of polyphases in the filterbank.
358 // for 16b, it is desirable to have 2^(16/2) = 256 phases.
359 // https://ccrma.stanford.edu/~jos/resample/Relation_Interpolation_Error_Quantization.html
360 //
361 // We are a bit more lax on this.
362
363 int phases = mSampleRate / gcd(mSampleRate, inSampleRate);
364
Andy Hung6582f2b2014-01-03 12:30:41 -0800365 // TODO: Once dynamic sample rate change is an option, the code below
366 // should be modified to execute only when dynamic sample rate change is enabled.
367 //
368 // as above, #phases less than 63 is too few phases for accurate linear interpolation.
369 // we increase the phases to compensate, but more phases means more memory per
370 // filter and more time to compute the filter.
371 //
372 // if we know that the filter will be used for dynamic sample rate changes,
373 // that would allow us skip this part for fixed sample rate resamplers.
374 //
375 while (phases<63) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800376 phases *= 2; // this code only needed to support dynamic rate changes
377 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800378
Andy Hung86eae0e2013-12-09 12:12:46 -0800379 if (phases>=256) { // too many phases, always interpolate
380 phases = 127;
381 }
382
383 // create the filter
384 mConstants.set(phases, halfLength, inSampleRate, mSampleRate);
Andy Hung771386e2014-04-08 18:44:38 -0700385 createKaiserFir(mConstants, stopBandAtten,
386 inSampleRate, mSampleRate, tbwCheat);
Andy Hung86eae0e2013-12-09 12:12:46 -0800387 } // End Kaiser filter
388
389 // update phase and state based on the new filter.
390 const Constants& c(mConstants);
391 mInBuffer.resize(mChannelCount, c.mHalfNumCoefs);
392 const uint32_t phaseWrapLimit = c.mL << c.mShift;
393 // try to preserve as much of the phase fraction as possible for on-the-fly changes
394 mPhaseFraction = static_cast<unsigned long long>(mPhaseFraction)
395 * phaseWrapLimit / oldPhaseWrapLimit;
396 mPhaseFraction %= phaseWrapLimit; // should not do anything, but just in case.
Andy Hungcd044842014-08-07 11:04:34 -0700397 mPhaseIncrement = static_cast<uint32_t>(static_cast<uint64_t>(phaseWrapLimit)
Andy Hung86eae0e2013-12-09 12:12:46 -0800398 * inSampleRate / mSampleRate);
399
400 // determine which resampler to use
401 // check if locked phase (works only if mPhaseIncrement has no "fractional phase bits")
402 int locked = (mPhaseIncrement << (sizeof(mPhaseIncrement)*8 - c.mShift)) == 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800403 if (locked) {
404 mPhaseFraction = mPhaseFraction >> c.mShift << c.mShift; // remove fractional phase
405 }
Andy Hung83be2562014-02-03 14:11:09 -0800406
Andy Hung075abae2014-04-09 19:36:43 -0700407 // stride is the minimum number of filter coefficients processed per loop iteration.
408 // We currently only allow a stride of 16 to match with SIMD processing.
409 // This means that the filter length must be a multiple of 16,
410 // or half the filter length (mHalfNumCoefs) must be a multiple of 8.
411 //
412 // Note: A stride of 2 is achieved with non-SIMD processing.
413 int stride = ((c.mHalfNumCoefs & 7) == 0) ? 16 : 2;
414 LOG_ALWAYS_FATAL_IF(stride < 16, "Resampler stride must be 16 or more");
Andy Hung5e58b0a2014-06-23 19:07:29 -0700415 LOG_ALWAYS_FATAL_IF(mChannelCount < 1 || mChannelCount > 8,
Andy Hung075abae2014-04-09 19:36:43 -0700416 "Resampler channels(%d) must be between 1 to 8", mChannelCount);
417 // stride 16 (falls back to stride 2 for machines that do not support NEON)
418 if (locked) {
419 switch (mChannelCount) {
420 case 1:
421 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, true, 16>;
422 break;
423 case 2:
424 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, true, 16>;
425 break;
426 case 3:
427 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, true, 16>;
428 break;
429 case 4:
430 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, true, 16>;
431 break;
432 case 5:
433 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, true, 16>;
434 break;
435 case 6:
436 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, true, 16>;
437 break;
438 case 7:
439 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, true, 16>;
440 break;
441 case 8:
442 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, true, 16>;
443 break;
444 }
445 } else {
446 switch (mChannelCount) {
447 case 1:
448 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, false, 16>;
449 break;
450 case 2:
451 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, false, 16>;
452 break;
453 case 3:
454 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, false, 16>;
455 break;
456 case 4:
457 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, false, 16>;
458 break;
459 case 5:
460 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, false, 16>;
461 break;
462 case 6:
463 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, false, 16>;
464 break;
465 case 7:
466 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, false, 16>;
467 break;
468 case 8:
469 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, false, 16>;
470 break;
471 }
472 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800473#ifdef DEBUG_RESAMPLER
474 printf("channels:%d %s stride:%d %s coef:%d shift:%d\n",
475 mChannelCount, locked ? "locked" : "interpolated",
476 stride, useS32 ? "S32" : "S16", 2*c.mHalfNumCoefs, c.mShift);
477#endif
478}
479
Andy Hung771386e2014-04-08 18:44:38 -0700480template<typename TC, typename TI, typename TO>
Andy Hung6b3b7e32015-03-29 00:49:22 -0700481size_t AudioResamplerDyn<TC, TI, TO>::resample(int32_t* out, size_t outFrameCount,
Andy Hung86eae0e2013-12-09 12:12:46 -0800482 AudioBufferProvider* provider)
483{
Andy Hung6b3b7e32015-03-29 00:49:22 -0700484 return (this->*mResampleFunc)(reinterpret_cast<TO*>(out), outFrameCount, provider);
Andy Hung771386e2014-04-08 18:44:38 -0700485}
Andy Hung86eae0e2013-12-09 12:12:46 -0800486
Andy Hung771386e2014-04-08 18:44:38 -0700487template<typename TC, typename TI, typename TO>
Andy Hung771386e2014-04-08 18:44:38 -0700488template<int CHANNELS, bool LOCKED, int STRIDE>
Andy Hung6b3b7e32015-03-29 00:49:22 -0700489size_t AudioResamplerDyn<TC, TI, TO>::resample(TO* out, size_t outFrameCount,
Andy Hung771386e2014-04-08 18:44:38 -0700490 AudioBufferProvider* provider)
Andy Hung86eae0e2013-12-09 12:12:46 -0800491{
Andy Hung075abae2014-04-09 19:36:43 -0700492 // TODO Mono -> Mono is not supported. OUTPUT_CHANNELS reflects minimum of stereo out.
493 const int OUTPUT_CHANNELS = (CHANNELS < 2) ? 2 : CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800494 const Constants& c(mConstants);
Andy Hung771386e2014-04-08 18:44:38 -0700495 const TC* const coefs = mConstants.mFirCoefs;
496 TI* impulse = mInBuffer.getImpulse();
Andy Hung411cb8e2014-05-27 12:32:17 -0700497 size_t inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800498 uint32_t phaseFraction = mPhaseFraction;
499 const uint32_t phaseIncrement = mPhaseIncrement;
500 size_t outputIndex = 0;
Andy Hung075abae2014-04-09 19:36:43 -0700501 size_t outputSampleCount = outFrameCount * OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800502 const uint32_t phaseWrapLimit = c.mL << c.mShift;
Andy Hung71700742014-06-02 18:54:08 -0700503 size_t inFrameCount = (phaseIncrement * (uint64_t)outFrameCount + phaseFraction)
504 / phaseWrapLimit;
505 // sanity check that inFrameCount is in signed 32 bit integer range.
506 ALOG_ASSERT(0 <= inFrameCount && inFrameCount < (1U << 31));
507
508 //ALOGV("inFrameCount:%d outFrameCount:%d"
509 // " phaseIncrement:%u phaseFraction:%u phaseWrapLimit:%u",
510 // inFrameCount, outFrameCount, phaseIncrement, phaseFraction, phaseWrapLimit);
Andy Hung86eae0e2013-12-09 12:12:46 -0800511
512 // NOTE: be very careful when modifying the code here. register
513 // pressure is very high and a small change might cause the compiler
514 // to generate far less efficient code.
515 // Always sanity check the result with objdump or test-resample.
516
517 // the following logic is a bit convoluted to keep the main processing loop
518 // as tight as possible with register allocation.
519 while (outputIndex < outputSampleCount) {
Andy Hung71700742014-06-02 18:54:08 -0700520 //ALOGV("LOOP: inFrameCount:%d outputIndex:%d outFrameCount:%d"
521 // " phaseFraction:%u phaseWrapLimit:%u",
522 // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
523
524 // check inputIndex overflow
525 ALOG_ASSERT(inputIndex <= mBuffer.frameCount, "inputIndex%d > frameCount%d",
526 inputIndex, mBuffer.frameCount);
527 // Buffer is empty, fetch a new one if necessary (inFrameCount > 0).
528 // We may not fetch a new buffer if the existing data is sufficient.
529 while (mBuffer.frameCount == 0 && inFrameCount > 0) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800530 mBuffer.frameCount = inFrameCount;
531 provider->getNextBuffer(&mBuffer,
Andy Hung075abae2014-04-09 19:36:43 -0700532 calculateOutputPTS(outputIndex / OUTPUT_CHANNELS));
Andy Hung86eae0e2013-12-09 12:12:46 -0800533 if (mBuffer.raw == NULL) {
534 goto resample_exit;
535 }
Andy Hung411cb8e2014-05-27 12:32:17 -0700536 inFrameCount -= mBuffer.frameCount;
Andy Hung86eae0e2013-12-09 12:12:46 -0800537 if (phaseFraction >= phaseWrapLimit) { // read in data
Andy Hung771386e2014-04-08 18:44:38 -0700538 mInBuffer.template readAdvance<CHANNELS>(
539 impulse, c.mHalfNumCoefs,
540 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700541 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800542 phaseFraction -= phaseWrapLimit;
543 while (phaseFraction >= phaseWrapLimit) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800544 if (inputIndex >= mBuffer.frameCount) {
Andy Hung411cb8e2014-05-27 12:32:17 -0700545 inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800546 provider->releaseBuffer(&mBuffer);
547 break;
548 }
Andy Hung771386e2014-04-08 18:44:38 -0700549 mInBuffer.template readAdvance<CHANNELS>(
550 impulse, c.mHalfNumCoefs,
551 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700552 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800553 phaseFraction -= phaseWrapLimit;
554 }
555 }
556 }
Andy Hung771386e2014-04-08 18:44:38 -0700557 const TI* const in = reinterpret_cast<const TI*>(mBuffer.raw);
Andy Hung86eae0e2013-12-09 12:12:46 -0800558 const size_t frameCount = mBuffer.frameCount;
559 const int coefShift = c.mShift;
560 const int halfNumCoefs = c.mHalfNumCoefs;
Andy Hung771386e2014-04-08 18:44:38 -0700561 const TO* const volumeSimd = mVolumeSimd;
Andy Hung86eae0e2013-12-09 12:12:46 -0800562
Andy Hung86eae0e2013-12-09 12:12:46 -0800563 // main processing loop
564 while (CC_LIKELY(outputIndex < outputSampleCount)) {
565 // caution: fir() is inlined and may be large.
566 // output will be loaded with the appropriate values
567 //
568 // from the input samples in impulse[-halfNumCoefs+1]... impulse[halfNumCoefs]
569 // from the polyphase filter of (phaseFraction / phaseWrapLimit) in coefs.
570 //
Andy Hung71700742014-06-02 18:54:08 -0700571 //ALOGV("LOOP2: inFrameCount:%d outputIndex:%d outFrameCount:%d"
572 // " phaseFraction:%u phaseWrapLimit:%u",
573 // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
574 ALOG_ASSERT(phaseFraction < phaseWrapLimit);
Andy Hung86eae0e2013-12-09 12:12:46 -0800575 fir<CHANNELS, LOCKED, STRIDE>(
576 &out[outputIndex],
577 phaseFraction, phaseWrapLimit,
578 coefShift, halfNumCoefs, coefs,
579 impulse, volumeSimd);
Andy Hung075abae2014-04-09 19:36:43 -0700580
581 outputIndex += OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800582
583 phaseFraction += phaseIncrement;
584 while (phaseFraction >= phaseWrapLimit) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800585 if (inputIndex >= frameCount) {
586 goto done; // need a new buffer
587 }
Andy Hung771386e2014-04-08 18:44:38 -0700588 mInBuffer.template readAdvance<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700589 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800590 phaseFraction -= phaseWrapLimit;
591 }
592 }
593done:
Andy Hung71700742014-06-02 18:54:08 -0700594 // We arrive here when we're finished or when the input buffer runs out.
595 // Regardless we need to release the input buffer if we've acquired it.
596 if (inputIndex > 0) { // we've acquired a buffer (alternatively could check frameCount)
597 ALOG_ASSERT(inputIndex == frameCount, "inputIndex(%d) != frameCount(%d)",
598 inputIndex, frameCount); // must have been fully read.
Andy Hung411cb8e2014-05-27 12:32:17 -0700599 inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800600 provider->releaseBuffer(&mBuffer);
Andy Hung411cb8e2014-05-27 12:32:17 -0700601 ALOG_ASSERT(mBuffer.frameCount == 0);
Andy Hung86eae0e2013-12-09 12:12:46 -0800602 }
603 }
604
605resample_exit:
Andy Hung71700742014-06-02 18:54:08 -0700606 // inputIndex must be zero in all three cases:
607 // (1) the buffer never was been acquired; (2) the buffer was
608 // released at "done:"; or (3) getNextBuffer() failed.
609 ALOG_ASSERT(inputIndex == 0, "Releasing: inputindex:%d frameCount:%d phaseFraction:%u",
610 inputIndex, mBuffer.frameCount, phaseFraction);
611 ALOG_ASSERT(mBuffer.frameCount == 0); // there must be no frames in the buffer
Andy Hung86eae0e2013-12-09 12:12:46 -0800612 mInBuffer.setImpulse(impulse);
Andy Hung86eae0e2013-12-09 12:12:46 -0800613 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700614 return outputIndex / OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800615}
616
Andy Hung771386e2014-04-08 18:44:38 -0700617/* instantiate templates used by AudioResampler::create */
618template class AudioResamplerDyn<float, float, float>;
619template class AudioResamplerDyn<int16_t, int16_t, int32_t>;
620template class AudioResamplerDyn<int32_t, int16_t, int32_t>;
621
Andy Hung86eae0e2013-12-09 12:12:46 -0800622// ----------------------------------------------------------------------------
Glenn Kasten63238ef2015-03-02 15:50:29 -0800623} // namespace android