blob: 79a3a5a3c0e10cc48bfd33c1a1187b0b592c95f1 [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
32#include "AudioResamplerFirOps.h" // USE_NEON and USE_INLINE_ASSEMBLY defined here
33#include "AudioResamplerFirProcess.h"
34#include "AudioResamplerFirProcessNeon.h"
35#include "AudioResamplerFirGen.h" // requires math.h
36#include "AudioResamplerDyn.h"
37
38//#define DEBUG_RESAMPLER
39
40namespace android {
41
Andy Hung86eae0e2013-12-09 12:12:46 -080042/*
43 * InBuffer is a type agnostic input buffer.
44 *
45 * Layout of the state buffer for halfNumCoefs=8.
46 *
47 * [rrrrrrppppppppnnnnnnnnrrrrrrrrrrrrrrrrrrr.... rrrrrrr]
48 * S I R
49 *
50 * S = mState
51 * I = mImpulse
52 * R = mRingFull
53 * p = past samples, convoluted with the (p)ositive side of sinc()
54 * n = future samples, convoluted with the (n)egative side of sinc()
55 * r = extra space for implementing the ring buffer
56 */
57
Andy Hung771386e2014-04-08 18:44:38 -070058template<typename TC, typename TI, typename TO>
59AudioResamplerDyn<TC, TI, TO>::InBuffer::InBuffer()
60 : mState(NULL), mImpulse(NULL), mRingFull(NULL), mStateCount(0)
61{
Andy Hung86eae0e2013-12-09 12:12:46 -080062}
63
Andy Hung771386e2014-04-08 18:44:38 -070064template<typename TC, typename TI, typename TO>
65AudioResamplerDyn<TC, TI, TO>::InBuffer::~InBuffer()
66{
Andy Hung86eae0e2013-12-09 12:12:46 -080067 init();
68}
69
Andy Hung771386e2014-04-08 18:44:38 -070070template<typename TC, typename TI, typename TO>
71void AudioResamplerDyn<TC, TI, TO>::InBuffer::init()
72{
Andy Hung86eae0e2013-12-09 12:12:46 -080073 free(mState);
74 mState = NULL;
75 mImpulse = NULL;
76 mRingFull = NULL;
Andy Hung771386e2014-04-08 18:44:38 -070077 mStateCount = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -080078}
79
80// resizes the state buffer to accommodate the appropriate filter length
Andy Hung771386e2014-04-08 18:44:38 -070081template<typename TC, typename TI, typename TO>
82void AudioResamplerDyn<TC, TI, TO>::InBuffer::resize(int CHANNELS, int halfNumCoefs)
83{
Andy Hung86eae0e2013-12-09 12:12:46 -080084 // calculate desired state size
Glenn Kastena4daf0b2014-07-28 16:34:45 -070085 size_t stateCount = halfNumCoefs * CHANNELS * 2 * kStateSizeMultipleOfFilterLength;
Andy Hung86eae0e2013-12-09 12:12:46 -080086
87 // check if buffer needs resizing
88 if (mState
Andy Hung771386e2014-04-08 18:44:38 -070089 && stateCount == mStateCount
Glenn Kastena4daf0b2014-07-28 16:34:45 -070090 && mRingFull-mState == (ssize_t) (mStateCount-halfNumCoefs*CHANNELS)) {
Andy Hung86eae0e2013-12-09 12:12:46 -080091 return;
92 }
93
94 // create new buffer
Glenn Kastena4daf0b2014-07-28 16:34:45 -070095 TI* state = NULL;
Andy Hung771386e2014-04-08 18:44:38 -070096 (void)posix_memalign(reinterpret_cast<void**>(&state), 32, stateCount*sizeof(*state));
97 memset(state, 0, stateCount*sizeof(*state));
Andy Hung86eae0e2013-12-09 12:12:46 -080098
99 // attempt to preserve state
100 if (mState) {
101 TI* srcLo = mImpulse - halfNumCoefs*CHANNELS;
102 TI* srcHi = mImpulse + halfNumCoefs*CHANNELS;
103 TI* dst = state;
104
105 if (srcLo < mState) {
106 dst += mState-srcLo;
107 srcLo = mState;
108 }
Andy Hung771386e2014-04-08 18:44:38 -0700109 if (srcHi > mState + mStateCount) {
110 srcHi = mState + mStateCount;
Andy Hung86eae0e2013-12-09 12:12:46 -0800111 }
112 memcpy(dst, srcLo, (srcHi - srcLo) * sizeof(*srcLo));
113 free(mState);
114 }
115
116 // set class member vars
117 mState = state;
Andy Hung771386e2014-04-08 18:44:38 -0700118 mStateCount = stateCount;
119 mImpulse = state + halfNumCoefs*CHANNELS; // actually one sample greater than needed
120 mRingFull = state + mStateCount - halfNumCoefs*CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800121}
122
123// copy in the input data into the head (impulse+halfNumCoefs) of the buffer.
Andy Hung771386e2014-04-08 18:44:38 -0700124template<typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -0800125template<int CHANNELS>
Andy Hung771386e2014-04-08 18:44:38 -0700126void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAgain(TI*& impulse, const int halfNumCoefs,
127 const TI* const in, const size_t inputIndex)
128{
129 TI* head = impulse + halfNumCoefs*CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800130 for (size_t i=0 ; i<CHANNELS ; i++) {
131 head[i] = in[inputIndex*CHANNELS + i];
132 }
133}
134
135// advance the impulse pointer, and load in data into the head (impulse+halfNumCoefs)
Andy Hung771386e2014-04-08 18:44:38 -0700136template<typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -0800137template<int CHANNELS>
Andy Hung771386e2014-04-08 18:44:38 -0700138void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAdvance(TI*& impulse, const int halfNumCoefs,
139 const TI* const in, const size_t inputIndex)
140{
Andy Hung86eae0e2013-12-09 12:12:46 -0800141 impulse += CHANNELS;
142
143 if (CC_UNLIKELY(impulse >= mRingFull)) {
144 const size_t shiftDown = mRingFull - mState - halfNumCoefs*CHANNELS;
145 memcpy(mState, mState+shiftDown, halfNumCoefs*CHANNELS*2*sizeof(TI));
146 impulse -= shiftDown;
147 }
148 readAgain<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
149}
150
Andy Hung771386e2014-04-08 18:44:38 -0700151template<typename TC, typename TI, typename TO>
Hochi Huangbd179d12016-03-28 13:30:46 -0700152void AudioResamplerDyn<TC, TI, TO>::InBuffer::reset()
153{
154 // clear resampler state
155 if (mState != nullptr) {
156 memset(mState, 0, mStateCount * sizeof(TI));
157 }
158}
159
160template<typename TC, typename TI, typename TO>
Andy Hung771386e2014-04-08 18:44:38 -0700161void AudioResamplerDyn<TC, TI, TO>::Constants::set(
Andy Hung86eae0e2013-12-09 12:12:46 -0800162 int L, int halfNumCoefs, int inSampleRate, int outSampleRate)
163{
164 int bits = 0;
165 int lscale = inSampleRate/outSampleRate < 2 ? L - 1 :
166 static_cast<int>(static_cast<uint64_t>(L)*inSampleRate/outSampleRate);
167 for (int i=lscale; i; ++bits, i>>=1)
168 ;
169 mL = L;
170 mShift = kNumPhaseBits - bits;
171 mHalfNumCoefs = halfNumCoefs;
172}
173
Andy Hung771386e2014-04-08 18:44:38 -0700174template<typename TC, typename TI, typename TO>
Andy Hung3348e362014-07-07 10:21:44 -0700175AudioResamplerDyn<TC, TI, TO>::AudioResamplerDyn(
Andy Hung86eae0e2013-12-09 12:12:46 -0800176 int inChannelCount, int32_t sampleRate, src_quality quality)
Andy Hung3348e362014-07-07 10:21:44 -0700177 : AudioResampler(inChannelCount, sampleRate, quality),
Andy Hung771386e2014-04-08 18:44:38 -0700178 mResampleFunc(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY),
Andy Hung6582f2b2014-01-03 12:30:41 -0800179 mCoefBuffer(NULL)
Andy Hung86eae0e2013-12-09 12:12:46 -0800180{
181 mVolumeSimd[0] = mVolumeSimd[1] = 0;
Andy Hung1af34082014-02-19 17:42:25 -0800182 // The AudioResampler base class assumes we are always ready for 1:1 resampling.
183 // We reset mInSampleRate to 0, so setSampleRate() will calculate filters for
184 // setSampleRate() for 1:1. (May be removed if precalculated filters are used.)
185 mInSampleRate = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800186 mConstants.set(128, 8, mSampleRate, mSampleRate); // TODO: set better
187}
188
Andy Hung771386e2014-04-08 18:44:38 -0700189template<typename TC, typename TI, typename TO>
190AudioResamplerDyn<TC, TI, TO>::~AudioResamplerDyn()
191{
Andy Hung86eae0e2013-12-09 12:12:46 -0800192 free(mCoefBuffer);
193}
194
Andy Hung771386e2014-04-08 18:44:38 -0700195template<typename TC, typename TI, typename TO>
196void AudioResamplerDyn<TC, TI, TO>::init()
197{
Andy Hung86eae0e2013-12-09 12:12:46 -0800198 mFilterSampleRate = 0; // always trigger new filter generation
199 mInBuffer.init();
200}
201
Andy Hung771386e2014-04-08 18:44:38 -0700202template<typename TC, typename TI, typename TO>
Andy Hung5e58b0a2014-06-23 19:07:29 -0700203void AudioResamplerDyn<TC, TI, TO>::setVolume(float left, float right)
Andy Hung771386e2014-04-08 18:44:38 -0700204{
Andy Hung86eae0e2013-12-09 12:12:46 -0800205 AudioResampler::setVolume(left, right);
Andy Hung771386e2014-04-08 18:44:38 -0700206 if (is_same<TO, float>::value || is_same<TO, double>::value) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700207 mVolumeSimd[0] = static_cast<TO>(left);
208 mVolumeSimd[1] = static_cast<TO>(right);
209 } else { // integer requires scaling to U4_28 (rounding down)
210 // integer volumes are clamped to 0 to UNITY_GAIN so there
211 // are no issues with signed overflow.
212 mVolumeSimd[0] = u4_28_from_float(clampFloatVol(left));
213 mVolumeSimd[1] = u4_28_from_float(clampFloatVol(right));
Andy Hung771386e2014-04-08 18:44:38 -0700214 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800215}
216
Andy Hung771386e2014-04-08 18:44:38 -0700217template<typename T> T max(T a, T b) {return a > b ? a : b;}
Andy Hung86eae0e2013-12-09 12:12:46 -0800218
Andy Hung771386e2014-04-08 18:44:38 -0700219template<typename T> T absdiff(T a, T b) {return a > b ? a - b : b - a;}
Andy Hung86eae0e2013-12-09 12:12:46 -0800220
Andy Hung771386e2014-04-08 18:44:38 -0700221template<typename TC, typename TI, typename TO>
222void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c,
223 double stopBandAtten, int inSampleRate, int outSampleRate, double tbwCheat)
224{
Glenn Kastena4daf0b2014-07-28 16:34:45 -0700225 TC* buf = NULL;
Andy Hung86eae0e2013-12-09 12:12:46 -0800226 static const double atten = 0.9998; // to avoid ripple overflow
227 double fcr;
228 double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten);
229
Andy Hung771386e2014-04-08 18:44:38 -0700230 (void)posix_memalign(reinterpret_cast<void**>(&buf), 32, (c.mL+1)*c.mHalfNumCoefs*sizeof(TC));
Andy Hung86eae0e2013-12-09 12:12:46 -0800231 if (inSampleRate < outSampleRate) { // upsample
232 fcr = max(0.5*tbwCheat - tbw/2, tbw/2);
233 } else { // downsample
234 fcr = max(0.5*tbwCheat*outSampleRate/inSampleRate - tbw/2, tbw/2);
235 }
236 // create and set filter
237 firKaiserGen(buf, c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten);
Andy Hung771386e2014-04-08 18:44:38 -0700238 c.mFirCoefs = buf;
Andy Hung86eae0e2013-12-09 12:12:46 -0800239 if (mCoefBuffer) {
240 free(mCoefBuffer);
241 }
242 mCoefBuffer = buf;
243#ifdef DEBUG_RESAMPLER
244 // print basic filter stats
245 printf("L:%d hnc:%d stopBandAtten:%lf fcr:%lf atten:%lf tbw:%lf\n",
246 c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten, tbw);
247 // test the filter and report results
248 double fp = (fcr - tbw/2)/c.mL;
249 double fs = (fcr + tbw/2)/c.mL;
Andy Hung6582f2b2014-01-03 12:30:41 -0800250 double passMin, passMax, passRipple;
251 double stopMax, stopRipple;
252 testFir(buf, c.mL, c.mHalfNumCoefs, fp, fs, /*passSteps*/ 1000, /*stopSteps*/ 100000,
253 passMin, passMax, passRipple, stopMax, stopRipple);
254 printf("passband(%lf, %lf): %.8lf %.8lf %.8lf\n", 0., fp, passMin, passMax, passRipple);
255 printf("stopband(%lf, %lf): %.8lf %.3lf\n", fs, 0.5, stopMax, stopRipple);
Andy Hung86eae0e2013-12-09 12:12:46 -0800256#endif
257}
258
Andy Hung6582f2b2014-01-03 12:30:41 -0800259// recursive gcd. Using objdump, it appears the tail recursion is converted to a while loop.
Andy Hung771386e2014-04-08 18:44:38 -0700260static int gcd(int n, int m)
261{
Andy Hung86eae0e2013-12-09 12:12:46 -0800262 if (m == 0) {
263 return n;
264 }
265 return gcd(m, n % m);
266}
267
Andy Hung6582f2b2014-01-03 12:30:41 -0800268static bool isClose(int32_t newSampleRate, int32_t prevSampleRate,
Andy Hung771386e2014-04-08 18:44:38 -0700269 int32_t filterSampleRate, int32_t outSampleRate)
270{
Andy Hung6582f2b2014-01-03 12:30:41 -0800271
272 // different upsampling ratios do not need a filter change.
273 if (filterSampleRate != 0
274 && filterSampleRate < outSampleRate
275 && newSampleRate < outSampleRate)
276 return true;
277
278 // check design criteria again if downsampling is detected.
Andy Hung86eae0e2013-12-09 12:12:46 -0800279 int pdiff = absdiff(newSampleRate, prevSampleRate);
280 int adiff = absdiff(newSampleRate, filterSampleRate);
281
282 // allow up to 6% relative change increments.
283 // allow up to 12% absolute change increments (from filter design)
284 return pdiff < prevSampleRate>>4 && adiff < filterSampleRate>>3;
285}
286
Andy Hung771386e2014-04-08 18:44:38 -0700287template<typename TC, typename TI, typename TO>
288void AudioResamplerDyn<TC, TI, TO>::setSampleRate(int32_t inSampleRate)
289{
Andy Hung86eae0e2013-12-09 12:12:46 -0800290 if (mInSampleRate == inSampleRate) {
291 return;
292 }
293 int32_t oldSampleRate = mInSampleRate;
Andy Hung86eae0e2013-12-09 12:12:46 -0800294 uint32_t oldPhaseWrapLimit = mConstants.mL << mConstants.mShift;
295 bool useS32 = false;
296
297 mInSampleRate = inSampleRate;
298
299 // TODO: Add precalculated Equiripple filters
300
Andy Hung6582f2b2014-01-03 12:30:41 -0800301 if (mFilterQuality != getQuality() ||
302 !isClose(inSampleRate, oldSampleRate, mFilterSampleRate, mSampleRate)) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800303 mFilterSampleRate = inSampleRate;
Andy Hung6582f2b2014-01-03 12:30:41 -0800304 mFilterQuality = getQuality();
Andy Hung86eae0e2013-12-09 12:12:46 -0800305
306 // Begin Kaiser Filter computation
307 //
308 // The quantization floor for S16 is about 96db - 10*log_10(#length) + 3dB.
309 // Keep the stop band attenuation no greater than 84-85dB for 32 length S16 filters
310 //
311 // For s32 we keep the stop band attenuation at the same as 16b resolution, about
312 // 96-98dB
313 //
314
315 double stopBandAtten;
316 double tbwCheat = 1.; // how much we "cheat" into aliasing
317 int halfLength;
Andy Hung6582f2b2014-01-03 12:30:41 -0800318 if (mFilterQuality == DYN_HIGH_QUALITY) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800319 // 32b coefficients, 64 length
320 useS32 = true;
321 stopBandAtten = 98.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800322 if (inSampleRate >= mSampleRate * 4) {
323 halfLength = 48;
324 } else if (inSampleRate >= mSampleRate * 2) {
325 halfLength = 40;
326 } else {
327 halfLength = 32;
328 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800329 } else if (mFilterQuality == DYN_LOW_QUALITY) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800330 // 16b coefficients, 16-32 length
331 useS32 = false;
332 stopBandAtten = 80.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800333 if (inSampleRate >= mSampleRate * 4) {
334 halfLength = 24;
335 } else if (inSampleRate >= mSampleRate * 2) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800336 halfLength = 16;
337 } else {
338 halfLength = 8;
339 }
Andy Hunga3bb9a32014-02-10 15:00:16 -0800340 if (inSampleRate <= mSampleRate) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800341 tbwCheat = 1.05;
342 } else {
343 tbwCheat = 1.03;
344 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800345 } else { // DYN_MED_QUALITY
Andy Hung86eae0e2013-12-09 12:12:46 -0800346 // 16b coefficients, 32-64 length
Andy Hung6582f2b2014-01-03 12:30:41 -0800347 // note: > 64 length filters with 16b coefs can have quantization noise problems
Andy Hung86eae0e2013-12-09 12:12:46 -0800348 useS32 = false;
349 stopBandAtten = 84.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800350 if (inSampleRate >= mSampleRate * 4) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800351 halfLength = 32;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800352 } else if (inSampleRate >= mSampleRate * 2) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800353 halfLength = 24;
354 } else {
355 halfLength = 16;
356 }
Andy Hunga3bb9a32014-02-10 15:00:16 -0800357 if (inSampleRate <= mSampleRate) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800358 tbwCheat = 1.03;
359 } else {
360 tbwCheat = 1.01;
361 }
362 }
363
364 // determine the number of polyphases in the filterbank.
365 // for 16b, it is desirable to have 2^(16/2) = 256 phases.
366 // https://ccrma.stanford.edu/~jos/resample/Relation_Interpolation_Error_Quantization.html
367 //
368 // We are a bit more lax on this.
369
370 int phases = mSampleRate / gcd(mSampleRate, inSampleRate);
371
Andy Hung6582f2b2014-01-03 12:30:41 -0800372 // TODO: Once dynamic sample rate change is an option, the code below
373 // should be modified to execute only when dynamic sample rate change is enabled.
374 //
375 // as above, #phases less than 63 is too few phases for accurate linear interpolation.
376 // we increase the phases to compensate, but more phases means more memory per
377 // filter and more time to compute the filter.
378 //
379 // if we know that the filter will be used for dynamic sample rate changes,
380 // that would allow us skip this part for fixed sample rate resamplers.
381 //
382 while (phases<63) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800383 phases *= 2; // this code only needed to support dynamic rate changes
384 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800385
Andy Hung86eae0e2013-12-09 12:12:46 -0800386 if (phases>=256) { // too many phases, always interpolate
387 phases = 127;
388 }
389
390 // create the filter
391 mConstants.set(phases, halfLength, inSampleRate, mSampleRate);
Andy Hung771386e2014-04-08 18:44:38 -0700392 createKaiserFir(mConstants, stopBandAtten,
393 inSampleRate, mSampleRate, tbwCheat);
Andy Hung86eae0e2013-12-09 12:12:46 -0800394 } // End Kaiser filter
395
396 // update phase and state based on the new filter.
397 const Constants& c(mConstants);
398 mInBuffer.resize(mChannelCount, c.mHalfNumCoefs);
399 const uint32_t phaseWrapLimit = c.mL << c.mShift;
400 // try to preserve as much of the phase fraction as possible for on-the-fly changes
401 mPhaseFraction = static_cast<unsigned long long>(mPhaseFraction)
402 * phaseWrapLimit / oldPhaseWrapLimit;
403 mPhaseFraction %= phaseWrapLimit; // should not do anything, but just in case.
Andy Hungcd044842014-08-07 11:04:34 -0700404 mPhaseIncrement = static_cast<uint32_t>(static_cast<uint64_t>(phaseWrapLimit)
Andy Hung86eae0e2013-12-09 12:12:46 -0800405 * inSampleRate / mSampleRate);
406
407 // determine which resampler to use
408 // check if locked phase (works only if mPhaseIncrement has no "fractional phase bits")
409 int locked = (mPhaseIncrement << (sizeof(mPhaseIncrement)*8 - c.mShift)) == 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800410 if (locked) {
411 mPhaseFraction = mPhaseFraction >> c.mShift << c.mShift; // remove fractional phase
412 }
Andy Hung83be2562014-02-03 14:11:09 -0800413
Andy Hung075abae2014-04-09 19:36:43 -0700414 // stride is the minimum number of filter coefficients processed per loop iteration.
415 // We currently only allow a stride of 16 to match with SIMD processing.
416 // This means that the filter length must be a multiple of 16,
417 // or half the filter length (mHalfNumCoefs) must be a multiple of 8.
418 //
419 // Note: A stride of 2 is achieved with non-SIMD processing.
420 int stride = ((c.mHalfNumCoefs & 7) == 0) ? 16 : 2;
421 LOG_ALWAYS_FATAL_IF(stride < 16, "Resampler stride must be 16 or more");
Andy Hung5e58b0a2014-06-23 19:07:29 -0700422 LOG_ALWAYS_FATAL_IF(mChannelCount < 1 || mChannelCount > 8,
Andy Hung075abae2014-04-09 19:36:43 -0700423 "Resampler channels(%d) must be between 1 to 8", mChannelCount);
424 // stride 16 (falls back to stride 2 for machines that do not support NEON)
425 if (locked) {
426 switch (mChannelCount) {
427 case 1:
428 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, true, 16>;
429 break;
430 case 2:
431 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, true, 16>;
432 break;
433 case 3:
434 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, true, 16>;
435 break;
436 case 4:
437 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, true, 16>;
438 break;
439 case 5:
440 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, true, 16>;
441 break;
442 case 6:
443 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, true, 16>;
444 break;
445 case 7:
446 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, true, 16>;
447 break;
448 case 8:
449 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, true, 16>;
450 break;
451 }
452 } else {
453 switch (mChannelCount) {
454 case 1:
455 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, false, 16>;
456 break;
457 case 2:
458 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, false, 16>;
459 break;
460 case 3:
461 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, false, 16>;
462 break;
463 case 4:
464 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, false, 16>;
465 break;
466 case 5:
467 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, false, 16>;
468 break;
469 case 6:
470 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, false, 16>;
471 break;
472 case 7:
473 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, false, 16>;
474 break;
475 case 8:
476 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, false, 16>;
477 break;
478 }
479 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800480#ifdef DEBUG_RESAMPLER
481 printf("channels:%d %s stride:%d %s coef:%d shift:%d\n",
482 mChannelCount, locked ? "locked" : "interpolated",
483 stride, useS32 ? "S32" : "S16", 2*c.mHalfNumCoefs, c.mShift);
484#endif
485}
486
Andy Hung771386e2014-04-08 18:44:38 -0700487template<typename TC, typename TI, typename TO>
Andy Hung6b3b7e32015-03-29 00:49:22 -0700488size_t AudioResamplerDyn<TC, TI, TO>::resample(int32_t* out, size_t outFrameCount,
Andy Hung86eae0e2013-12-09 12:12:46 -0800489 AudioBufferProvider* provider)
490{
Andy Hung6b3b7e32015-03-29 00:49:22 -0700491 return (this->*mResampleFunc)(reinterpret_cast<TO*>(out), outFrameCount, provider);
Andy Hung771386e2014-04-08 18:44:38 -0700492}
Andy Hung86eae0e2013-12-09 12:12:46 -0800493
Andy Hung771386e2014-04-08 18:44:38 -0700494template<typename TC, typename TI, typename TO>
Andy Hung771386e2014-04-08 18:44:38 -0700495template<int CHANNELS, bool LOCKED, int STRIDE>
Andy Hung6b3b7e32015-03-29 00:49:22 -0700496size_t AudioResamplerDyn<TC, TI, TO>::resample(TO* out, size_t outFrameCount,
Andy Hung771386e2014-04-08 18:44:38 -0700497 AudioBufferProvider* provider)
Andy Hung86eae0e2013-12-09 12:12:46 -0800498{
Andy Hung075abae2014-04-09 19:36:43 -0700499 // TODO Mono -> Mono is not supported. OUTPUT_CHANNELS reflects minimum of stereo out.
500 const int OUTPUT_CHANNELS = (CHANNELS < 2) ? 2 : CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800501 const Constants& c(mConstants);
Andy Hung771386e2014-04-08 18:44:38 -0700502 const TC* const coefs = mConstants.mFirCoefs;
503 TI* impulse = mInBuffer.getImpulse();
Andy Hung411cb8e2014-05-27 12:32:17 -0700504 size_t inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800505 uint32_t phaseFraction = mPhaseFraction;
506 const uint32_t phaseIncrement = mPhaseIncrement;
507 size_t outputIndex = 0;
Andy Hung075abae2014-04-09 19:36:43 -0700508 size_t outputSampleCount = outFrameCount * OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800509 const uint32_t phaseWrapLimit = c.mL << c.mShift;
Andy Hung71700742014-06-02 18:54:08 -0700510 size_t inFrameCount = (phaseIncrement * (uint64_t)outFrameCount + phaseFraction)
511 / phaseWrapLimit;
512 // sanity check that inFrameCount is in signed 32 bit integer range.
513 ALOG_ASSERT(0 <= inFrameCount && inFrameCount < (1U << 31));
514
515 //ALOGV("inFrameCount:%d outFrameCount:%d"
516 // " phaseIncrement:%u phaseFraction:%u phaseWrapLimit:%u",
517 // inFrameCount, outFrameCount, phaseIncrement, phaseFraction, phaseWrapLimit);
Andy Hung86eae0e2013-12-09 12:12:46 -0800518
519 // NOTE: be very careful when modifying the code here. register
520 // pressure is very high and a small change might cause the compiler
521 // to generate far less efficient code.
522 // Always sanity check the result with objdump or test-resample.
523
524 // the following logic is a bit convoluted to keep the main processing loop
525 // as tight as possible with register allocation.
526 while (outputIndex < outputSampleCount) {
Andy Hung71700742014-06-02 18:54:08 -0700527 //ALOGV("LOOP: inFrameCount:%d outputIndex:%d outFrameCount:%d"
528 // " phaseFraction:%u phaseWrapLimit:%u",
529 // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
530
531 // check inputIndex overflow
532 ALOG_ASSERT(inputIndex <= mBuffer.frameCount, "inputIndex%d > frameCount%d",
533 inputIndex, mBuffer.frameCount);
534 // Buffer is empty, fetch a new one if necessary (inFrameCount > 0).
535 // We may not fetch a new buffer if the existing data is sufficient.
536 while (mBuffer.frameCount == 0 && inFrameCount > 0) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800537 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -0800538 provider->getNextBuffer(&mBuffer);
Andy Hung86eae0e2013-12-09 12:12:46 -0800539 if (mBuffer.raw == NULL) {
Hochi Huangbd179d12016-03-28 13:30:46 -0700540 // We are either at the end of playback or in an underrun situation.
541 // Reset buffer to prevent pop noise at the next buffer.
542 mInBuffer.reset();
Andy Hung86eae0e2013-12-09 12:12:46 -0800543 goto resample_exit;
544 }
Andy Hung411cb8e2014-05-27 12:32:17 -0700545 inFrameCount -= mBuffer.frameCount;
Andy Hung86eae0e2013-12-09 12:12:46 -0800546 if (phaseFraction >= phaseWrapLimit) { // read in data
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 while (phaseFraction >= phaseWrapLimit) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800553 if (inputIndex >= mBuffer.frameCount) {
Andy Hung411cb8e2014-05-27 12:32:17 -0700554 inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800555 provider->releaseBuffer(&mBuffer);
556 break;
557 }
Andy Hung771386e2014-04-08 18:44:38 -0700558 mInBuffer.template readAdvance<CHANNELS>(
559 impulse, c.mHalfNumCoefs,
560 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700561 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800562 phaseFraction -= phaseWrapLimit;
563 }
564 }
565 }
Andy Hung771386e2014-04-08 18:44:38 -0700566 const TI* const in = reinterpret_cast<const TI*>(mBuffer.raw);
Andy Hung86eae0e2013-12-09 12:12:46 -0800567 const size_t frameCount = mBuffer.frameCount;
568 const int coefShift = c.mShift;
569 const int halfNumCoefs = c.mHalfNumCoefs;
Andy Hung771386e2014-04-08 18:44:38 -0700570 const TO* const volumeSimd = mVolumeSimd;
Andy Hung86eae0e2013-12-09 12:12:46 -0800571
Andy Hung86eae0e2013-12-09 12:12:46 -0800572 // main processing loop
573 while (CC_LIKELY(outputIndex < outputSampleCount)) {
574 // caution: fir() is inlined and may be large.
575 // output will be loaded with the appropriate values
576 //
577 // from the input samples in impulse[-halfNumCoefs+1]... impulse[halfNumCoefs]
578 // from the polyphase filter of (phaseFraction / phaseWrapLimit) in coefs.
579 //
Andy Hung71700742014-06-02 18:54:08 -0700580 //ALOGV("LOOP2: inFrameCount:%d outputIndex:%d outFrameCount:%d"
581 // " phaseFraction:%u phaseWrapLimit:%u",
582 // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
583 ALOG_ASSERT(phaseFraction < phaseWrapLimit);
Andy Hung86eae0e2013-12-09 12:12:46 -0800584 fir<CHANNELS, LOCKED, STRIDE>(
585 &out[outputIndex],
586 phaseFraction, phaseWrapLimit,
587 coefShift, halfNumCoefs, coefs,
588 impulse, volumeSimd);
Andy Hung075abae2014-04-09 19:36:43 -0700589
590 outputIndex += OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800591
592 phaseFraction += phaseIncrement;
593 while (phaseFraction >= phaseWrapLimit) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800594 if (inputIndex >= frameCount) {
595 goto done; // need a new buffer
596 }
Andy Hung771386e2014-04-08 18:44:38 -0700597 mInBuffer.template readAdvance<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700598 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800599 phaseFraction -= phaseWrapLimit;
600 }
601 }
602done:
Andy Hung71700742014-06-02 18:54:08 -0700603 // We arrive here when we're finished or when the input buffer runs out.
604 // Regardless we need to release the input buffer if we've acquired it.
605 if (inputIndex > 0) { // we've acquired a buffer (alternatively could check frameCount)
606 ALOG_ASSERT(inputIndex == frameCount, "inputIndex(%d) != frameCount(%d)",
607 inputIndex, frameCount); // must have been fully read.
Andy Hung411cb8e2014-05-27 12:32:17 -0700608 inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800609 provider->releaseBuffer(&mBuffer);
Andy Hung411cb8e2014-05-27 12:32:17 -0700610 ALOG_ASSERT(mBuffer.frameCount == 0);
Andy Hung86eae0e2013-12-09 12:12:46 -0800611 }
612 }
613
614resample_exit:
Andy Hung71700742014-06-02 18:54:08 -0700615 // inputIndex must be zero in all three cases:
616 // (1) the buffer never was been acquired; (2) the buffer was
617 // released at "done:"; or (3) getNextBuffer() failed.
618 ALOG_ASSERT(inputIndex == 0, "Releasing: inputindex:%d frameCount:%d phaseFraction:%u",
619 inputIndex, mBuffer.frameCount, phaseFraction);
620 ALOG_ASSERT(mBuffer.frameCount == 0); // there must be no frames in the buffer
Andy Hung86eae0e2013-12-09 12:12:46 -0800621 mInBuffer.setImpulse(impulse);
Andy Hung86eae0e2013-12-09 12:12:46 -0800622 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700623 return outputIndex / OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800624}
625
Andy Hung771386e2014-04-08 18:44:38 -0700626/* instantiate templates used by AudioResampler::create */
627template class AudioResamplerDyn<float, float, float>;
628template class AudioResamplerDyn<int16_t, int16_t, int32_t>;
629template class AudioResamplerDyn<int32_t, int16_t, int32_t>;
630
Andy Hung86eae0e2013-12-09 12:12:46 -0800631// ----------------------------------------------------------------------------
Glenn Kasten63238ef2015-03-02 15:50:29 -0800632} // namespace android