blob: 8f7b9820b78c50d0d30790d2453ce08c1ca202d4 [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>
Hochi Huangbd179d12016-03-28 13:30:46 -0700153void AudioResamplerDyn<TC, TI, TO>::InBuffer::reset()
154{
155 // clear resampler state
156 if (mState != nullptr) {
157 memset(mState, 0, mStateCount * sizeof(TI));
158 }
159}
160
161template<typename TC, typename TI, typename TO>
Andy Hung771386e2014-04-08 18:44:38 -0700162void AudioResamplerDyn<TC, TI, TO>::Constants::set(
Andy Hung86eae0e2013-12-09 12:12:46 -0800163 int L, int halfNumCoefs, int inSampleRate, int outSampleRate)
164{
165 int bits = 0;
166 int lscale = inSampleRate/outSampleRate < 2 ? L - 1 :
167 static_cast<int>(static_cast<uint64_t>(L)*inSampleRate/outSampleRate);
168 for (int i=lscale; i; ++bits, i>>=1)
169 ;
170 mL = L;
171 mShift = kNumPhaseBits - bits;
172 mHalfNumCoefs = halfNumCoefs;
173}
174
Andy Hung771386e2014-04-08 18:44:38 -0700175template<typename TC, typename TI, typename TO>
Andy Hung3348e362014-07-07 10:21:44 -0700176AudioResamplerDyn<TC, TI, TO>::AudioResamplerDyn(
Andy Hung86eae0e2013-12-09 12:12:46 -0800177 int inChannelCount, int32_t sampleRate, src_quality quality)
Andy Hung3348e362014-07-07 10:21:44 -0700178 : AudioResampler(inChannelCount, sampleRate, quality),
Andy Hung771386e2014-04-08 18:44:38 -0700179 mResampleFunc(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY),
Andy Hung6582f2b2014-01-03 12:30:41 -0800180 mCoefBuffer(NULL)
Andy Hung86eae0e2013-12-09 12:12:46 -0800181{
182 mVolumeSimd[0] = mVolumeSimd[1] = 0;
Andy Hung1af34082014-02-19 17:42:25 -0800183 // The AudioResampler base class assumes we are always ready for 1:1 resampling.
184 // We reset mInSampleRate to 0, so setSampleRate() will calculate filters for
185 // setSampleRate() for 1:1. (May be removed if precalculated filters are used.)
186 mInSampleRate = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800187 mConstants.set(128, 8, mSampleRate, mSampleRate); // TODO: set better
188}
189
Andy Hung771386e2014-04-08 18:44:38 -0700190template<typename TC, typename TI, typename TO>
191AudioResamplerDyn<TC, TI, TO>::~AudioResamplerDyn()
192{
Andy Hung86eae0e2013-12-09 12:12:46 -0800193 free(mCoefBuffer);
194}
195
Andy Hung771386e2014-04-08 18:44:38 -0700196template<typename TC, typename TI, typename TO>
197void AudioResamplerDyn<TC, TI, TO>::init()
198{
Andy Hung86eae0e2013-12-09 12:12:46 -0800199 mFilterSampleRate = 0; // always trigger new filter generation
200 mInBuffer.init();
201}
202
Andy Hung771386e2014-04-08 18:44:38 -0700203template<typename TC, typename TI, typename TO>
Andy Hung5e58b0a2014-06-23 19:07:29 -0700204void AudioResamplerDyn<TC, TI, TO>::setVolume(float left, float right)
Andy Hung771386e2014-04-08 18:44:38 -0700205{
Andy Hung86eae0e2013-12-09 12:12:46 -0800206 AudioResampler::setVolume(left, right);
Andy Hung771386e2014-04-08 18:44:38 -0700207 if (is_same<TO, float>::value || is_same<TO, double>::value) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700208 mVolumeSimd[0] = static_cast<TO>(left);
209 mVolumeSimd[1] = static_cast<TO>(right);
210 } else { // integer requires scaling to U4_28 (rounding down)
211 // integer volumes are clamped to 0 to UNITY_GAIN so there
212 // are no issues with signed overflow.
213 mVolumeSimd[0] = u4_28_from_float(clampFloatVol(left));
214 mVolumeSimd[1] = u4_28_from_float(clampFloatVol(right));
Andy Hung771386e2014-04-08 18:44:38 -0700215 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800216}
217
Andy Hung771386e2014-04-08 18:44:38 -0700218template<typename T> T max(T a, T b) {return a > b ? a : b;}
Andy Hung86eae0e2013-12-09 12:12:46 -0800219
Andy Hung771386e2014-04-08 18:44:38 -0700220template<typename T> T absdiff(T a, T b) {return a > b ? a - b : b - a;}
Andy Hung86eae0e2013-12-09 12:12:46 -0800221
Andy Hung771386e2014-04-08 18:44:38 -0700222template<typename TC, typename TI, typename TO>
223void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c,
224 double stopBandAtten, int inSampleRate, int outSampleRate, double tbwCheat)
225{
Glenn Kastena4daf0b2014-07-28 16:34:45 -0700226 TC* buf = NULL;
Andy Hung86eae0e2013-12-09 12:12:46 -0800227 static const double atten = 0.9998; // to avoid ripple overflow
228 double fcr;
229 double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten);
230
Andy Hung771386e2014-04-08 18:44:38 -0700231 (void)posix_memalign(reinterpret_cast<void**>(&buf), 32, (c.mL+1)*c.mHalfNumCoefs*sizeof(TC));
Andy Hung86eae0e2013-12-09 12:12:46 -0800232 if (inSampleRate < outSampleRate) { // upsample
233 fcr = max(0.5*tbwCheat - tbw/2, tbw/2);
234 } else { // downsample
235 fcr = max(0.5*tbwCheat*outSampleRate/inSampleRate - tbw/2, tbw/2);
236 }
237 // create and set filter
238 firKaiserGen(buf, c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten);
Andy Hung771386e2014-04-08 18:44:38 -0700239 c.mFirCoefs = buf;
Andy Hung86eae0e2013-12-09 12:12:46 -0800240 if (mCoefBuffer) {
241 free(mCoefBuffer);
242 }
243 mCoefBuffer = buf;
244#ifdef DEBUG_RESAMPLER
245 // print basic filter stats
246 printf("L:%d hnc:%d stopBandAtten:%lf fcr:%lf atten:%lf tbw:%lf\n",
247 c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten, tbw);
248 // test the filter and report results
249 double fp = (fcr - tbw/2)/c.mL;
250 double fs = (fcr + tbw/2)/c.mL;
Andy Hung6582f2b2014-01-03 12:30:41 -0800251 double passMin, passMax, passRipple;
252 double stopMax, stopRipple;
253 testFir(buf, c.mL, c.mHalfNumCoefs, fp, fs, /*passSteps*/ 1000, /*stopSteps*/ 100000,
254 passMin, passMax, passRipple, stopMax, stopRipple);
255 printf("passband(%lf, %lf): %.8lf %.8lf %.8lf\n", 0., fp, passMin, passMax, passRipple);
256 printf("stopband(%lf, %lf): %.8lf %.3lf\n", fs, 0.5, stopMax, stopRipple);
Andy Hung86eae0e2013-12-09 12:12:46 -0800257#endif
258}
259
Andy Hung6582f2b2014-01-03 12:30:41 -0800260// recursive gcd. Using objdump, it appears the tail recursion is converted to a while loop.
Andy Hung771386e2014-04-08 18:44:38 -0700261static int gcd(int n, int m)
262{
Andy Hung86eae0e2013-12-09 12:12:46 -0800263 if (m == 0) {
264 return n;
265 }
266 return gcd(m, n % m);
267}
268
Andy Hung6582f2b2014-01-03 12:30:41 -0800269static bool isClose(int32_t newSampleRate, int32_t prevSampleRate,
Andy Hung771386e2014-04-08 18:44:38 -0700270 int32_t filterSampleRate, int32_t outSampleRate)
271{
Andy Hung6582f2b2014-01-03 12:30:41 -0800272
273 // different upsampling ratios do not need a filter change.
274 if (filterSampleRate != 0
275 && filterSampleRate < outSampleRate
276 && newSampleRate < outSampleRate)
277 return true;
278
279 // check design criteria again if downsampling is detected.
Andy Hung86eae0e2013-12-09 12:12:46 -0800280 int pdiff = absdiff(newSampleRate, prevSampleRate);
281 int adiff = absdiff(newSampleRate, filterSampleRate);
282
283 // allow up to 6% relative change increments.
284 // allow up to 12% absolute change increments (from filter design)
285 return pdiff < prevSampleRate>>4 && adiff < filterSampleRate>>3;
286}
287
Andy Hung771386e2014-04-08 18:44:38 -0700288template<typename TC, typename TI, typename TO>
289void AudioResamplerDyn<TC, TI, TO>::setSampleRate(int32_t inSampleRate)
290{
Andy Hung86eae0e2013-12-09 12:12:46 -0800291 if (mInSampleRate == inSampleRate) {
292 return;
293 }
294 int32_t oldSampleRate = mInSampleRate;
Andy Hung86eae0e2013-12-09 12:12:46 -0800295 uint32_t oldPhaseWrapLimit = mConstants.mL << mConstants.mShift;
296 bool useS32 = false;
297
298 mInSampleRate = inSampleRate;
299
300 // TODO: Add precalculated Equiripple filters
301
Andy Hung6582f2b2014-01-03 12:30:41 -0800302 if (mFilterQuality != getQuality() ||
303 !isClose(inSampleRate, oldSampleRate, mFilterSampleRate, mSampleRate)) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800304 mFilterSampleRate = inSampleRate;
Andy Hung6582f2b2014-01-03 12:30:41 -0800305 mFilterQuality = getQuality();
Andy Hung86eae0e2013-12-09 12:12:46 -0800306
307 // Begin Kaiser Filter computation
308 //
309 // The quantization floor for S16 is about 96db - 10*log_10(#length) + 3dB.
310 // Keep the stop band attenuation no greater than 84-85dB for 32 length S16 filters
311 //
312 // For s32 we keep the stop band attenuation at the same as 16b resolution, about
313 // 96-98dB
314 //
315
316 double stopBandAtten;
317 double tbwCheat = 1.; // how much we "cheat" into aliasing
318 int halfLength;
Andy Hung6582f2b2014-01-03 12:30:41 -0800319 if (mFilterQuality == DYN_HIGH_QUALITY) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800320 // 32b coefficients, 64 length
321 useS32 = true;
322 stopBandAtten = 98.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800323 if (inSampleRate >= mSampleRate * 4) {
324 halfLength = 48;
325 } else if (inSampleRate >= mSampleRate * 2) {
326 halfLength = 40;
327 } else {
328 halfLength = 32;
329 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800330 } else if (mFilterQuality == DYN_LOW_QUALITY) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800331 // 16b coefficients, 16-32 length
332 useS32 = false;
333 stopBandAtten = 80.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800334 if (inSampleRate >= mSampleRate * 4) {
335 halfLength = 24;
336 } else if (inSampleRate >= mSampleRate * 2) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800337 halfLength = 16;
338 } else {
339 halfLength = 8;
340 }
Andy Hunga3bb9a32014-02-10 15:00:16 -0800341 if (inSampleRate <= mSampleRate) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800342 tbwCheat = 1.05;
343 } else {
344 tbwCheat = 1.03;
345 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800346 } else { // DYN_MED_QUALITY
Andy Hung86eae0e2013-12-09 12:12:46 -0800347 // 16b coefficients, 32-64 length
Andy Hung6582f2b2014-01-03 12:30:41 -0800348 // note: > 64 length filters with 16b coefs can have quantization noise problems
Andy Hung86eae0e2013-12-09 12:12:46 -0800349 useS32 = false;
350 stopBandAtten = 84.;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800351 if (inSampleRate >= mSampleRate * 4) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800352 halfLength = 32;
Andy Hunga3bb9a32014-02-10 15:00:16 -0800353 } else if (inSampleRate >= mSampleRate * 2) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800354 halfLength = 24;
355 } else {
356 halfLength = 16;
357 }
Andy Hunga3bb9a32014-02-10 15:00:16 -0800358 if (inSampleRate <= mSampleRate) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800359 tbwCheat = 1.03;
360 } else {
361 tbwCheat = 1.01;
362 }
363 }
364
365 // determine the number of polyphases in the filterbank.
366 // for 16b, it is desirable to have 2^(16/2) = 256 phases.
367 // https://ccrma.stanford.edu/~jos/resample/Relation_Interpolation_Error_Quantization.html
368 //
369 // We are a bit more lax on this.
370
371 int phases = mSampleRate / gcd(mSampleRate, inSampleRate);
372
Andy Hung6582f2b2014-01-03 12:30:41 -0800373 // TODO: Once dynamic sample rate change is an option, the code below
374 // should be modified to execute only when dynamic sample rate change is enabled.
375 //
376 // as above, #phases less than 63 is too few phases for accurate linear interpolation.
377 // we increase the phases to compensate, but more phases means more memory per
378 // filter and more time to compute the filter.
379 //
380 // if we know that the filter will be used for dynamic sample rate changes,
381 // that would allow us skip this part for fixed sample rate resamplers.
382 //
383 while (phases<63) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800384 phases *= 2; // this code only needed to support dynamic rate changes
385 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800386
Andy Hung86eae0e2013-12-09 12:12:46 -0800387 if (phases>=256) { // too many phases, always interpolate
388 phases = 127;
389 }
390
391 // create the filter
392 mConstants.set(phases, halfLength, inSampleRate, mSampleRate);
Andy Hung771386e2014-04-08 18:44:38 -0700393 createKaiserFir(mConstants, stopBandAtten,
394 inSampleRate, mSampleRate, tbwCheat);
Andy Hung86eae0e2013-12-09 12:12:46 -0800395 } // End Kaiser filter
396
397 // update phase and state based on the new filter.
398 const Constants& c(mConstants);
399 mInBuffer.resize(mChannelCount, c.mHalfNumCoefs);
400 const uint32_t phaseWrapLimit = c.mL << c.mShift;
401 // try to preserve as much of the phase fraction as possible for on-the-fly changes
402 mPhaseFraction = static_cast<unsigned long long>(mPhaseFraction)
403 * phaseWrapLimit / oldPhaseWrapLimit;
404 mPhaseFraction %= phaseWrapLimit; // should not do anything, but just in case.
Andy Hungcd044842014-08-07 11:04:34 -0700405 mPhaseIncrement = static_cast<uint32_t>(static_cast<uint64_t>(phaseWrapLimit)
Andy Hung86eae0e2013-12-09 12:12:46 -0800406 * inSampleRate / mSampleRate);
407
408 // determine which resampler to use
409 // check if locked phase (works only if mPhaseIncrement has no "fractional phase bits")
410 int locked = (mPhaseIncrement << (sizeof(mPhaseIncrement)*8 - c.mShift)) == 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800411 if (locked) {
412 mPhaseFraction = mPhaseFraction >> c.mShift << c.mShift; // remove fractional phase
413 }
Andy Hung83be2562014-02-03 14:11:09 -0800414
Andy Hung075abae2014-04-09 19:36:43 -0700415 // stride is the minimum number of filter coefficients processed per loop iteration.
416 // We currently only allow a stride of 16 to match with SIMD processing.
417 // This means that the filter length must be a multiple of 16,
418 // or half the filter length (mHalfNumCoefs) must be a multiple of 8.
419 //
420 // Note: A stride of 2 is achieved with non-SIMD processing.
421 int stride = ((c.mHalfNumCoefs & 7) == 0) ? 16 : 2;
422 LOG_ALWAYS_FATAL_IF(stride < 16, "Resampler stride must be 16 or more");
Andy Hung5e58b0a2014-06-23 19:07:29 -0700423 LOG_ALWAYS_FATAL_IF(mChannelCount < 1 || mChannelCount > 8,
Andy Hung075abae2014-04-09 19:36:43 -0700424 "Resampler channels(%d) must be between 1 to 8", mChannelCount);
425 // stride 16 (falls back to stride 2 for machines that do not support NEON)
426 if (locked) {
427 switch (mChannelCount) {
428 case 1:
429 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, true, 16>;
430 break;
431 case 2:
432 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, true, 16>;
433 break;
434 case 3:
435 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, true, 16>;
436 break;
437 case 4:
438 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, true, 16>;
439 break;
440 case 5:
441 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, true, 16>;
442 break;
443 case 6:
444 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, true, 16>;
445 break;
446 case 7:
447 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, true, 16>;
448 break;
449 case 8:
450 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, true, 16>;
451 break;
452 }
453 } else {
454 switch (mChannelCount) {
455 case 1:
456 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, false, 16>;
457 break;
458 case 2:
459 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, false, 16>;
460 break;
461 case 3:
462 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, false, 16>;
463 break;
464 case 4:
465 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, false, 16>;
466 break;
467 case 5:
468 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, false, 16>;
469 break;
470 case 6:
471 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, false, 16>;
472 break;
473 case 7:
474 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, false, 16>;
475 break;
476 case 8:
477 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, false, 16>;
478 break;
479 }
480 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800481#ifdef DEBUG_RESAMPLER
482 printf("channels:%d %s stride:%d %s coef:%d shift:%d\n",
483 mChannelCount, locked ? "locked" : "interpolated",
484 stride, useS32 ? "S32" : "S16", 2*c.mHalfNumCoefs, c.mShift);
485#endif
486}
487
Andy Hung771386e2014-04-08 18:44:38 -0700488template<typename TC, typename TI, typename TO>
Andy Hung6b3b7e32015-03-29 00:49:22 -0700489size_t AudioResamplerDyn<TC, TI, TO>::resample(int32_t* out, size_t outFrameCount,
Andy Hung86eae0e2013-12-09 12:12:46 -0800490 AudioBufferProvider* provider)
491{
Andy Hung6b3b7e32015-03-29 00:49:22 -0700492 return (this->*mResampleFunc)(reinterpret_cast<TO*>(out), outFrameCount, provider);
Andy Hung771386e2014-04-08 18:44:38 -0700493}
Andy Hung86eae0e2013-12-09 12:12:46 -0800494
Andy Hung771386e2014-04-08 18:44:38 -0700495template<typename TC, typename TI, typename TO>
Andy Hung771386e2014-04-08 18:44:38 -0700496template<int CHANNELS, bool LOCKED, int STRIDE>
Andy Hung6b3b7e32015-03-29 00:49:22 -0700497size_t AudioResamplerDyn<TC, TI, TO>::resample(TO* out, size_t outFrameCount,
Andy Hung771386e2014-04-08 18:44:38 -0700498 AudioBufferProvider* provider)
Andy Hung86eae0e2013-12-09 12:12:46 -0800499{
Andy Hung075abae2014-04-09 19:36:43 -0700500 // TODO Mono -> Mono is not supported. OUTPUT_CHANNELS reflects minimum of stereo out.
501 const int OUTPUT_CHANNELS = (CHANNELS < 2) ? 2 : CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800502 const Constants& c(mConstants);
Andy Hung771386e2014-04-08 18:44:38 -0700503 const TC* const coefs = mConstants.mFirCoefs;
504 TI* impulse = mInBuffer.getImpulse();
Andy Hung411cb8e2014-05-27 12:32:17 -0700505 size_t inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800506 uint32_t phaseFraction = mPhaseFraction;
507 const uint32_t phaseIncrement = mPhaseIncrement;
508 size_t outputIndex = 0;
Andy Hung075abae2014-04-09 19:36:43 -0700509 size_t outputSampleCount = outFrameCount * OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800510 const uint32_t phaseWrapLimit = c.mL << c.mShift;
Andy Hung71700742014-06-02 18:54:08 -0700511 size_t inFrameCount = (phaseIncrement * (uint64_t)outFrameCount + phaseFraction)
512 / phaseWrapLimit;
513 // sanity check that inFrameCount is in signed 32 bit integer range.
514 ALOG_ASSERT(0 <= inFrameCount && inFrameCount < (1U << 31));
515
516 //ALOGV("inFrameCount:%d outFrameCount:%d"
517 // " phaseIncrement:%u phaseFraction:%u phaseWrapLimit:%u",
518 // inFrameCount, outFrameCount, phaseIncrement, phaseFraction, phaseWrapLimit);
Andy Hung86eae0e2013-12-09 12:12:46 -0800519
520 // NOTE: be very careful when modifying the code here. register
521 // pressure is very high and a small change might cause the compiler
522 // to generate far less efficient code.
523 // Always sanity check the result with objdump or test-resample.
524
525 // the following logic is a bit convoluted to keep the main processing loop
526 // as tight as possible with register allocation.
527 while (outputIndex < outputSampleCount) {
Andy Hung71700742014-06-02 18:54:08 -0700528 //ALOGV("LOOP: inFrameCount:%d outputIndex:%d outFrameCount:%d"
529 // " phaseFraction:%u phaseWrapLimit:%u",
530 // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
531
532 // check inputIndex overflow
Tobias Melin43489212016-09-16 10:04:26 +0200533 ALOG_ASSERT(inputIndex <= mBuffer.frameCount, "inputIndex%zu > frameCount%zu",
Andy Hung71700742014-06-02 18:54:08 -0700534 inputIndex, mBuffer.frameCount);
535 // Buffer is empty, fetch a new one if necessary (inFrameCount > 0).
536 // We may not fetch a new buffer if the existing data is sufficient.
537 while (mBuffer.frameCount == 0 && inFrameCount > 0) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800538 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -0800539 provider->getNextBuffer(&mBuffer);
Andy Hung86eae0e2013-12-09 12:12:46 -0800540 if (mBuffer.raw == NULL) {
Hochi Huangbd179d12016-03-28 13:30:46 -0700541 // We are either at the end of playback or in an underrun situation.
542 // Reset buffer to prevent pop noise at the next buffer.
543 mInBuffer.reset();
Andy Hung86eae0e2013-12-09 12:12:46 -0800544 goto resample_exit;
545 }
Andy Hung411cb8e2014-05-27 12:32:17 -0700546 inFrameCount -= mBuffer.frameCount;
Andy Hung86eae0e2013-12-09 12:12:46 -0800547 if (phaseFraction >= phaseWrapLimit) { // read in data
Andy Hung771386e2014-04-08 18:44:38 -0700548 mInBuffer.template readAdvance<CHANNELS>(
549 impulse, c.mHalfNumCoefs,
550 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700551 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800552 phaseFraction -= phaseWrapLimit;
553 while (phaseFraction >= phaseWrapLimit) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800554 if (inputIndex >= mBuffer.frameCount) {
Andy Hung411cb8e2014-05-27 12:32:17 -0700555 inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800556 provider->releaseBuffer(&mBuffer);
557 break;
558 }
Andy Hung771386e2014-04-08 18:44:38 -0700559 mInBuffer.template readAdvance<CHANNELS>(
560 impulse, c.mHalfNumCoefs,
561 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700562 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800563 phaseFraction -= phaseWrapLimit;
564 }
565 }
566 }
Andy Hung771386e2014-04-08 18:44:38 -0700567 const TI* const in = reinterpret_cast<const TI*>(mBuffer.raw);
Andy Hung86eae0e2013-12-09 12:12:46 -0800568 const size_t frameCount = mBuffer.frameCount;
569 const int coefShift = c.mShift;
570 const int halfNumCoefs = c.mHalfNumCoefs;
Andy Hung771386e2014-04-08 18:44:38 -0700571 const TO* const volumeSimd = mVolumeSimd;
Andy Hung86eae0e2013-12-09 12:12:46 -0800572
Andy Hung86eae0e2013-12-09 12:12:46 -0800573 // main processing loop
574 while (CC_LIKELY(outputIndex < outputSampleCount)) {
575 // caution: fir() is inlined and may be large.
576 // output will be loaded with the appropriate values
577 //
578 // from the input samples in impulse[-halfNumCoefs+1]... impulse[halfNumCoefs]
579 // from the polyphase filter of (phaseFraction / phaseWrapLimit) in coefs.
580 //
Andy Hung71700742014-06-02 18:54:08 -0700581 //ALOGV("LOOP2: inFrameCount:%d outputIndex:%d outFrameCount:%d"
582 // " phaseFraction:%u phaseWrapLimit:%u",
583 // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
584 ALOG_ASSERT(phaseFraction < phaseWrapLimit);
Andy Hung86eae0e2013-12-09 12:12:46 -0800585 fir<CHANNELS, LOCKED, STRIDE>(
586 &out[outputIndex],
587 phaseFraction, phaseWrapLimit,
588 coefShift, halfNumCoefs, coefs,
589 impulse, volumeSimd);
Andy Hung075abae2014-04-09 19:36:43 -0700590
591 outputIndex += OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800592
593 phaseFraction += phaseIncrement;
594 while (phaseFraction >= phaseWrapLimit) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800595 if (inputIndex >= frameCount) {
596 goto done; // need a new buffer
597 }
Andy Hung771386e2014-04-08 18:44:38 -0700598 mInBuffer.template readAdvance<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700599 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800600 phaseFraction -= phaseWrapLimit;
601 }
602 }
603done:
Andy Hung71700742014-06-02 18:54:08 -0700604 // We arrive here when we're finished or when the input buffer runs out.
605 // Regardless we need to release the input buffer if we've acquired it.
606 if (inputIndex > 0) { // we've acquired a buffer (alternatively could check frameCount)
Tobias Melin43489212016-09-16 10:04:26 +0200607 ALOG_ASSERT(inputIndex == frameCount, "inputIndex(%zu) != frameCount(%zu)",
Andy Hung71700742014-06-02 18:54:08 -0700608 inputIndex, frameCount); // must have been fully read.
Andy Hung411cb8e2014-05-27 12:32:17 -0700609 inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800610 provider->releaseBuffer(&mBuffer);
Andy Hung411cb8e2014-05-27 12:32:17 -0700611 ALOG_ASSERT(mBuffer.frameCount == 0);
Andy Hung86eae0e2013-12-09 12:12:46 -0800612 }
613 }
614
615resample_exit:
Andy Hung71700742014-06-02 18:54:08 -0700616 // inputIndex must be zero in all three cases:
617 // (1) the buffer never was been acquired; (2) the buffer was
618 // released at "done:"; or (3) getNextBuffer() failed.
Tobias Melin43489212016-09-16 10:04:26 +0200619 ALOG_ASSERT(inputIndex == 0, "Releasing: inputindex:%zu frameCount:%zu phaseFraction:%u",
Andy Hung71700742014-06-02 18:54:08 -0700620 inputIndex, mBuffer.frameCount, phaseFraction);
621 ALOG_ASSERT(mBuffer.frameCount == 0); // there must be no frames in the buffer
Andy Hung86eae0e2013-12-09 12:12:46 -0800622 mInBuffer.setImpulse(impulse);
Andy Hung86eae0e2013-12-09 12:12:46 -0800623 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700624 return outputIndex / OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800625}
626
Andy Hung771386e2014-04-08 18:44:38 -0700627/* instantiate templates used by AudioResampler::create */
628template class AudioResamplerDyn<float, float, float>;
629template class AudioResamplerDyn<int16_t, int16_t, int32_t>;
630template class AudioResamplerDyn<int32_t, int16_t, int32_t>;
631
Andy Hung86eae0e2013-12-09 12:12:46 -0800632// ----------------------------------------------------------------------------
Glenn Kasten63238ef2015-03-02 15:50:29 -0800633} // namespace android