blob: 1aacfd1aed1e82eeb4225bcf45f45af6fca9d79a [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>
28#include <utils/Log.h>
Andy Hung5e58b0a2014-06-23 19:07:29 -070029#include <audio_utils/primitives.h>
Andy Hung86eae0e2013-12-09 12:12:46 -080030
Henrik Smiding841920d2016-02-15 16:20:45 +010031#include "AudioResamplerFirOps.h" // USE_NEON, USE_SSE and USE_INLINE_ASSEMBLY defined here
Andy Hung86eae0e2013-12-09 12:12:46 -080032#include "AudioResamplerFirProcess.h"
33#include "AudioResamplerFirProcessNeon.h"
Henrik Smiding841920d2016-02-15 16:20:45 +010034#include "AudioResamplerFirProcessSSE.h"
Andy Hung86eae0e2013-12-09 12:12:46 -080035#include "AudioResamplerFirGen.h" // requires math.h
36#include "AudioResamplerDyn.h"
37
38//#define DEBUG_RESAMPLER
39
Andy Hung6bd378f2017-10-24 19:23:52 -070040// use this for our buffer alignment. Should be at least 32 bytes.
41constexpr size_t CACHE_LINE_SIZE = 64;
42
Andy Hung86eae0e2013-12-09 12:12:46 -080043namespace android {
44
Andy Hung86eae0e2013-12-09 12:12:46 -080045/*
46 * InBuffer is a type agnostic input buffer.
47 *
48 * Layout of the state buffer for halfNumCoefs=8.
49 *
50 * [rrrrrrppppppppnnnnnnnnrrrrrrrrrrrrrrrrrrr.... rrrrrrr]
51 * S I R
52 *
53 * S = mState
54 * I = mImpulse
55 * R = mRingFull
56 * p = past samples, convoluted with the (p)ositive side of sinc()
57 * n = future samples, convoluted with the (n)egative side of sinc()
58 * r = extra space for implementing the ring buffer
59 */
60
Andy Hung771386e2014-04-08 18:44:38 -070061template<typename TC, typename TI, typename TO>
62AudioResamplerDyn<TC, TI, TO>::InBuffer::InBuffer()
63 : mState(NULL), mImpulse(NULL), mRingFull(NULL), mStateCount(0)
64{
Andy Hung86eae0e2013-12-09 12:12:46 -080065}
66
Andy Hung771386e2014-04-08 18:44:38 -070067template<typename TC, typename TI, typename TO>
68AudioResamplerDyn<TC, TI, TO>::InBuffer::~InBuffer()
69{
Andy Hung86eae0e2013-12-09 12:12:46 -080070 init();
71}
72
Andy Hung771386e2014-04-08 18:44:38 -070073template<typename TC, typename TI, typename TO>
74void AudioResamplerDyn<TC, TI, TO>::InBuffer::init()
75{
Andy Hung86eae0e2013-12-09 12:12:46 -080076 free(mState);
77 mState = NULL;
78 mImpulse = NULL;
79 mRingFull = NULL;
Andy Hung771386e2014-04-08 18:44:38 -070080 mStateCount = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -080081}
82
83// resizes the state buffer to accommodate the appropriate filter length
Andy Hung771386e2014-04-08 18:44:38 -070084template<typename TC, typename TI, typename TO>
85void AudioResamplerDyn<TC, TI, TO>::InBuffer::resize(int CHANNELS, int halfNumCoefs)
86{
Andy Hung86eae0e2013-12-09 12:12:46 -080087 // calculate desired state size
Glenn Kastena4daf0b2014-07-28 16:34:45 -070088 size_t stateCount = halfNumCoefs * CHANNELS * 2 * kStateSizeMultipleOfFilterLength;
Andy Hung86eae0e2013-12-09 12:12:46 -080089
90 // check if buffer needs resizing
91 if (mState
Andy Hung771386e2014-04-08 18:44:38 -070092 && stateCount == mStateCount
Glenn Kastena4daf0b2014-07-28 16:34:45 -070093 && mRingFull-mState == (ssize_t) (mStateCount-halfNumCoefs*CHANNELS)) {
Andy Hung86eae0e2013-12-09 12:12:46 -080094 return;
95 }
96
97 // create new buffer
Glenn Kastena4daf0b2014-07-28 16:34:45 -070098 TI* state = NULL;
Andy Hung6bd378f2017-10-24 19:23:52 -070099 (void)posix_memalign(
100 reinterpret_cast<void **>(&state),
101 CACHE_LINE_SIZE /* alignment */,
102 stateCount * sizeof(*state));
Andy Hung771386e2014-04-08 18:44:38 -0700103 memset(state, 0, stateCount*sizeof(*state));
Andy Hung86eae0e2013-12-09 12:12:46 -0800104
105 // attempt to preserve state
106 if (mState) {
107 TI* srcLo = mImpulse - halfNumCoefs*CHANNELS;
108 TI* srcHi = mImpulse + halfNumCoefs*CHANNELS;
109 TI* dst = state;
110
111 if (srcLo < mState) {
112 dst += mState-srcLo;
113 srcLo = mState;
114 }
Andy Hung771386e2014-04-08 18:44:38 -0700115 if (srcHi > mState + mStateCount) {
116 srcHi = mState + mStateCount;
Andy Hung86eae0e2013-12-09 12:12:46 -0800117 }
118 memcpy(dst, srcLo, (srcHi - srcLo) * sizeof(*srcLo));
119 free(mState);
120 }
121
122 // set class member vars
123 mState = state;
Andy Hung771386e2014-04-08 18:44:38 -0700124 mStateCount = stateCount;
125 mImpulse = state + halfNumCoefs*CHANNELS; // actually one sample greater than needed
126 mRingFull = state + mStateCount - halfNumCoefs*CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800127}
128
129// copy in the input data into the head (impulse+halfNumCoefs) of the buffer.
Andy Hung771386e2014-04-08 18:44:38 -0700130template<typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -0800131template<int CHANNELS>
Andy Hung771386e2014-04-08 18:44:38 -0700132void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAgain(TI*& impulse, const int halfNumCoefs,
133 const TI* const in, const size_t inputIndex)
134{
135 TI* head = impulse + halfNumCoefs*CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800136 for (size_t i=0 ; i<CHANNELS ; i++) {
137 head[i] = in[inputIndex*CHANNELS + i];
138 }
139}
140
141// advance the impulse pointer, and load in data into the head (impulse+halfNumCoefs)
Andy Hung771386e2014-04-08 18:44:38 -0700142template<typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -0800143template<int CHANNELS>
Andy Hung771386e2014-04-08 18:44:38 -0700144void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAdvance(TI*& impulse, const int halfNumCoefs,
145 const TI* const in, const size_t inputIndex)
146{
Andy Hung86eae0e2013-12-09 12:12:46 -0800147 impulse += CHANNELS;
148
149 if (CC_UNLIKELY(impulse >= mRingFull)) {
150 const size_t shiftDown = mRingFull - mState - halfNumCoefs*CHANNELS;
151 memcpy(mState, mState+shiftDown, halfNumCoefs*CHANNELS*2*sizeof(TI));
152 impulse -= shiftDown;
153 }
154 readAgain<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
155}
156
Andy Hung771386e2014-04-08 18:44:38 -0700157template<typename TC, typename TI, typename TO>
Hochi Huangbd179d12016-03-28 13:30:46 -0700158void AudioResamplerDyn<TC, TI, TO>::InBuffer::reset()
159{
160 // clear resampler state
161 if (mState != nullptr) {
162 memset(mState, 0, mStateCount * sizeof(TI));
163 }
164}
165
166template<typename TC, typename TI, typename TO>
Andy Hung771386e2014-04-08 18:44:38 -0700167void AudioResamplerDyn<TC, TI, TO>::Constants::set(
Andy Hung86eae0e2013-12-09 12:12:46 -0800168 int L, int halfNumCoefs, int inSampleRate, int outSampleRate)
169{
170 int bits = 0;
171 int lscale = inSampleRate/outSampleRate < 2 ? L - 1 :
172 static_cast<int>(static_cast<uint64_t>(L)*inSampleRate/outSampleRate);
173 for (int i=lscale; i; ++bits, i>>=1)
174 ;
175 mL = L;
176 mShift = kNumPhaseBits - bits;
177 mHalfNumCoefs = halfNumCoefs;
178}
179
Andy Hung771386e2014-04-08 18:44:38 -0700180template<typename TC, typename TI, typename TO>
Andy Hung3348e362014-07-07 10:21:44 -0700181AudioResamplerDyn<TC, TI, TO>::AudioResamplerDyn(
Andy Hung86eae0e2013-12-09 12:12:46 -0800182 int inChannelCount, int32_t sampleRate, src_quality quality)
Andy Hung3348e362014-07-07 10:21:44 -0700183 : AudioResampler(inChannelCount, sampleRate, quality),
Andy Hung771386e2014-04-08 18:44:38 -0700184 mResampleFunc(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY),
Andy Hung6582f2b2014-01-03 12:30:41 -0800185 mCoefBuffer(NULL)
Andy Hung86eae0e2013-12-09 12:12:46 -0800186{
187 mVolumeSimd[0] = mVolumeSimd[1] = 0;
Andy Hung1af34082014-02-19 17:42:25 -0800188 // The AudioResampler base class assumes we are always ready for 1:1 resampling.
189 // We reset mInSampleRate to 0, so setSampleRate() will calculate filters for
190 // setSampleRate() for 1:1. (May be removed if precalculated filters are used.)
191 mInSampleRate = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800192 mConstants.set(128, 8, mSampleRate, mSampleRate); // TODO: set better
Andy Hung6bd378f2017-10-24 19:23:52 -0700193
194 // fetch property based resampling parameters
195 mPropertyEnableAtSampleRate = property_get_int32(
196 "ro.audio.resampler.psd.enable_at_samplerate", mPropertyEnableAtSampleRate);
197 mPropertyHalfFilterLength = property_get_int32(
198 "ro.audio.resampler.psd.halflength", mPropertyHalfFilterLength);
199 mPropertyStopbandAttenuation = property_get_int32(
200 "ro.audio.resampler.psd.stopband", mPropertyStopbandAttenuation);
201 mPropertyCutoffPercent = property_get_int32(
202 "ro.audio.resampler.psd.cutoff_percent", mPropertyCutoffPercent);
Andy Hung86571a92019-04-02 15:40:54 -0700203 mPropertyTransitionBandwidthCheat = property_get_int32(
204 "ro.audio.resampler.psd.tbwcheat", mPropertyTransitionBandwidthCheat);
Andy Hung86eae0e2013-12-09 12:12:46 -0800205}
206
Andy Hung771386e2014-04-08 18:44:38 -0700207template<typename TC, typename TI, typename TO>
208AudioResamplerDyn<TC, TI, TO>::~AudioResamplerDyn()
209{
Andy Hung86eae0e2013-12-09 12:12:46 -0800210 free(mCoefBuffer);
211}
212
Andy Hung771386e2014-04-08 18:44:38 -0700213template<typename TC, typename TI, typename TO>
214void AudioResamplerDyn<TC, TI, TO>::init()
215{
Andy Hung86eae0e2013-12-09 12:12:46 -0800216 mFilterSampleRate = 0; // always trigger new filter generation
217 mInBuffer.init();
218}
219
Andy Hung771386e2014-04-08 18:44:38 -0700220template<typename TC, typename TI, typename TO>
Andy Hung5e58b0a2014-06-23 19:07:29 -0700221void AudioResamplerDyn<TC, TI, TO>::setVolume(float left, float right)
Andy Hung771386e2014-04-08 18:44:38 -0700222{
Andy Hung86eae0e2013-12-09 12:12:46 -0800223 AudioResampler::setVolume(left, right);
Andy Hung771386e2014-04-08 18:44:38 -0700224 if (is_same<TO, float>::value || is_same<TO, double>::value) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700225 mVolumeSimd[0] = static_cast<TO>(left);
226 mVolumeSimd[1] = static_cast<TO>(right);
227 } else { // integer requires scaling to U4_28 (rounding down)
228 // integer volumes are clamped to 0 to UNITY_GAIN so there
229 // are no issues with signed overflow.
230 mVolumeSimd[0] = u4_28_from_float(clampFloatVol(left));
231 mVolumeSimd[1] = u4_28_from_float(clampFloatVol(right));
Andy Hung771386e2014-04-08 18:44:38 -0700232 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800233}
234
Andy Hung6bd378f2017-10-24 19:23:52 -0700235// TODO: update to C++11
236
Andy Hung771386e2014-04-08 18:44:38 -0700237template<typename T> T max(T a, T b) {return a > b ? a : b;}
Andy Hung86eae0e2013-12-09 12:12:46 -0800238
Andy Hung771386e2014-04-08 18:44:38 -0700239template<typename T> T absdiff(T a, T b) {return a > b ? a - b : b - a;}
Andy Hung86eae0e2013-12-09 12:12:46 -0800240
Andy Hung771386e2014-04-08 18:44:38 -0700241template<typename TC, typename TI, typename TO>
242void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c,
243 double stopBandAtten, int inSampleRate, int outSampleRate, double tbwCheat)
244{
Andy Hung6bd378f2017-10-24 19:23:52 -0700245 // compute the normalized transition bandwidth
246 const double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten);
Andy Hung3f692412019-04-02 15:48:22 -0700247 const double halfbw = tbw * 0.5;
Andy Hung86eae0e2013-12-09 12:12:46 -0800248
Andy Hung6bd378f2017-10-24 19:23:52 -0700249 double fcr; // compute fcr, the 3 dB amplitude cut-off.
Andy Hung86eae0e2013-12-09 12:12:46 -0800250 if (inSampleRate < outSampleRate) { // upsample
Andy Hung6bd378f2017-10-24 19:23:52 -0700251 fcr = max(0.5 * tbwCheat - halfbw, halfbw);
Andy Hung86eae0e2013-12-09 12:12:46 -0800252 } else { // downsample
Andy Hung6bd378f2017-10-24 19:23:52 -0700253 fcr = max(0.5 * tbwCheat * outSampleRate / inSampleRate - halfbw, halfbw);
Andy Hung86eae0e2013-12-09 12:12:46 -0800254 }
Andy Hung6bd378f2017-10-24 19:23:52 -0700255 createKaiserFir(c, stopBandAtten, fcr);
256}
257
258template<typename TC, typename TI, typename TO>
259void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c,
260 double stopBandAtten, double fcr) {
261 // compute the normalized transition bandwidth
262 const double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten);
263 const int phases = c.mL;
264 const int halfLength = c.mHalfNumCoefs;
265
266 // create buffer
267 TC *coefs = nullptr;
268 int ret = posix_memalign(
269 reinterpret_cast<void **>(&coefs),
270 CACHE_LINE_SIZE /* alignment */,
271 (phases + 1) * halfLength * sizeof(TC));
272 LOG_ALWAYS_FATAL_IF(ret != 0, "Cannot allocate buffer memory, ret %d", ret);
273 c.mFirCoefs = coefs;
274 free(mCoefBuffer);
275 mCoefBuffer = coefs;
276
277 // square the computed minimum passband value (extra safety).
278 double attenuation =
279 computeWindowedSincMinimumPassbandValue(stopBandAtten);
280 attenuation *= attenuation;
281
282 // design filter
283 firKaiserGen(coefs, phases, halfLength, stopBandAtten, fcr, attenuation);
284
285 // update the design criteria
286 mNormalizedCutoffFrequency = fcr;
287 mNormalizedTransitionBandwidth = tbw;
288 mFilterAttenuation = attenuation;
289 mStopbandAttenuationDb = stopBandAtten;
290 mPassbandRippleDb = computeWindowedSincPassbandRippleDb(stopBandAtten);
291
292#if 0
293 // Keep this debug code in case an app causes resampler design issues.
Andy Hung3f692412019-04-02 15:48:22 -0700294 const double halfbw = tbw * 0.5;
Andy Hung86eae0e2013-12-09 12:12:46 -0800295 // print basic filter stats
Andy Hung6bd378f2017-10-24 19:23:52 -0700296 ALOGD("L:%d hnc:%d stopBandAtten:%lf fcr:%lf atten:%lf tbw:%lf\n",
297 c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, attenuation, tbw);
298
299 // test the filter and report results.
300 // Since this is a polyphase filter, normalized fp and fs must be scaled.
301 const double fp = (fcr - halfbw) / phases;
302 const double fs = (fcr + halfbw) / phases;
303
Andy Hung6582f2b2014-01-03 12:30:41 -0800304 double passMin, passMax, passRipple;
305 double stopMax, stopRipple;
Andy Hung6bd378f2017-10-24 19:23:52 -0700306
307 const int32_t passSteps = 1000;
308
Andy Hung3f692412019-04-02 15:48:22 -0700309 testFir(coefs, c.mL, c.mHalfNumCoefs, fp, fs, passSteps, passSteps * c.mL /*stopSteps*/,
Andy Hung6582f2b2014-01-03 12:30:41 -0800310 passMin, passMax, passRipple, stopMax, stopRipple);
Andy Hung6bd378f2017-10-24 19:23:52 -0700311 ALOGD("passband(%lf, %lf): %.8lf %.8lf %.8lf\n", 0., fp, passMin, passMax, passRipple);
312 ALOGD("stopband(%lf, %lf): %.8lf %.3lf\n", fs, 0.5, stopMax, stopRipple);
Andy Hung86eae0e2013-12-09 12:12:46 -0800313#endif
314}
315
Andy Hung6582f2b2014-01-03 12:30:41 -0800316// recursive gcd. Using objdump, it appears the tail recursion is converted to a while loop.
Andy Hung771386e2014-04-08 18:44:38 -0700317static int gcd(int n, int m)
318{
Andy Hung86eae0e2013-12-09 12:12:46 -0800319 if (m == 0) {
320 return n;
321 }
322 return gcd(m, n % m);
323}
324
Andy Hung6582f2b2014-01-03 12:30:41 -0800325static bool isClose(int32_t newSampleRate, int32_t prevSampleRate,
Andy Hung771386e2014-04-08 18:44:38 -0700326 int32_t filterSampleRate, int32_t outSampleRate)
327{
Andy Hung6582f2b2014-01-03 12:30:41 -0800328
329 // different upsampling ratios do not need a filter change.
330 if (filterSampleRate != 0
331 && filterSampleRate < outSampleRate
332 && newSampleRate < outSampleRate)
333 return true;
334
335 // check design criteria again if downsampling is detected.
Andy Hung86eae0e2013-12-09 12:12:46 -0800336 int pdiff = absdiff(newSampleRate, prevSampleRate);
337 int adiff = absdiff(newSampleRate, filterSampleRate);
338
339 // allow up to 6% relative change increments.
340 // allow up to 12% absolute change increments (from filter design)
341 return pdiff < prevSampleRate>>4 && adiff < filterSampleRate>>3;
342}
343
Andy Hung771386e2014-04-08 18:44:38 -0700344template<typename TC, typename TI, typename TO>
345void AudioResamplerDyn<TC, TI, TO>::setSampleRate(int32_t inSampleRate)
346{
Andy Hung86eae0e2013-12-09 12:12:46 -0800347 if (mInSampleRate == inSampleRate) {
348 return;
349 }
350 int32_t oldSampleRate = mInSampleRate;
Andy Hung86eae0e2013-12-09 12:12:46 -0800351 uint32_t oldPhaseWrapLimit = mConstants.mL << mConstants.mShift;
352 bool useS32 = false;
353
354 mInSampleRate = inSampleRate;
355
356 // TODO: Add precalculated Equiripple filters
357
Andy Hung6582f2b2014-01-03 12:30:41 -0800358 if (mFilterQuality != getQuality() ||
359 !isClose(inSampleRate, oldSampleRate, mFilterSampleRate, mSampleRate)) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800360 mFilterSampleRate = inSampleRate;
Andy Hung6582f2b2014-01-03 12:30:41 -0800361 mFilterQuality = getQuality();
Andy Hung86eae0e2013-12-09 12:12:46 -0800362
Andy Hung6bd378f2017-10-24 19:23:52 -0700363 double stopBandAtten;
364 double tbwCheat = 1.; // how much we "cheat" into aliasing
365 int halfLength;
366 double fcr = 0.;
367
Andy Hung86eae0e2013-12-09 12:12:46 -0800368 // Begin Kaiser Filter computation
369 //
370 // The quantization floor for S16 is about 96db - 10*log_10(#length) + 3dB.
371 // Keep the stop band attenuation no greater than 84-85dB for 32 length S16 filters
372 //
373 // For s32 we keep the stop band attenuation at the same as 16b resolution, about
374 // 96-98dB
375 //
376
Andy Hung6bd378f2017-10-24 19:23:52 -0700377 if (mPropertyEnableAtSampleRate >= 0 && mSampleRate >= mPropertyEnableAtSampleRate) {
378 // An alternative method which allows allows a greater fcr
379 // at the expense of potential aliasing.
380 halfLength = mPropertyHalfFilterLength;
381 stopBandAtten = mPropertyStopbandAttenuation;
Andy Hung86eae0e2013-12-09 12:12:46 -0800382 useS32 = true;
Andy Hung86571a92019-04-02 15:40:54 -0700383
384 // Use either the stopband location for design (tbwCheat)
385 // or use the 3dB cutoff location for design (fcr).
386 // This choice is exclusive and based on whether fcr > 0.
387 if (mPropertyTransitionBandwidthCheat != 0) {
388 tbwCheat = mPropertyTransitionBandwidthCheat / 100.;
389 } else {
390 fcr = mInSampleRate <= mSampleRate
391 ? 0.5 : 0.5 * mSampleRate / mInSampleRate;
392 fcr *= mPropertyCutoffPercent / 100.;
393 }
Andy Hung6bd378f2017-10-24 19:23:52 -0700394 } else {
Andy Hung06b40f92019-03-26 15:51:41 -0700395 // Voice quality devices have lower sampling rates
396 // (and may be a consequence of downstream AMR-WB / G.722 codecs).
397 // For these devices, we ensure a wider resampler passband
398 // at the expense of aliasing noise (stopband attenuation
399 // and stopband frequency).
400 //
401 constexpr uint32_t kVoiceDeviceSampleRate = 16000;
402
Andy Hung6bd378f2017-10-24 19:23:52 -0700403 if (mFilterQuality == DYN_HIGH_QUALITY) {
Andy Hung06b40f92019-03-26 15:51:41 -0700404 // float or 32b coefficients
Andy Hung6bd378f2017-10-24 19:23:52 -0700405 useS32 = true;
406 stopBandAtten = 98.;
407 if (inSampleRate >= mSampleRate * 4) {
408 halfLength = 48;
409 } else if (inSampleRate >= mSampleRate * 2) {
410 halfLength = 40;
411 } else {
412 halfLength = 32;
413 }
Andy Hung06b40f92019-03-26 15:51:41 -0700414
415 if (mSampleRate <= kVoiceDeviceSampleRate) {
416 if (inSampleRate >= mSampleRate * 2) {
417 halfLength += 16;
418 } else {
419 halfLength += 8;
420 }
421 stopBandAtten = 84.;
422 tbwCheat = 1.05;
423 }
Andy Hung6bd378f2017-10-24 19:23:52 -0700424 } else if (mFilterQuality == DYN_LOW_QUALITY) {
Andy Hung06b40f92019-03-26 15:51:41 -0700425 // float or 16b coefficients
Andy Hung6bd378f2017-10-24 19:23:52 -0700426 useS32 = false;
427 stopBandAtten = 80.;
428 if (inSampleRate >= mSampleRate * 4) {
429 halfLength = 24;
430 } else if (inSampleRate >= mSampleRate * 2) {
431 halfLength = 16;
432 } else {
433 halfLength = 8;
434 }
Andy Hung06b40f92019-03-26 15:51:41 -0700435 if (mSampleRate <= kVoiceDeviceSampleRate) {
436 if (inSampleRate >= mSampleRate * 2) {
437 halfLength += 8;
438 }
439 tbwCheat = 1.05;
440 } else if (inSampleRate <= mSampleRate) {
Andy Hung6bd378f2017-10-24 19:23:52 -0700441 tbwCheat = 1.05;
442 } else {
443 tbwCheat = 1.03;
444 }
445 } else { // DYN_MED_QUALITY
Andy Hung06b40f92019-03-26 15:51:41 -0700446 // float or 16b coefficients
Andy Hung6bd378f2017-10-24 19:23:52 -0700447 // note: > 64 length filters with 16b coefs can have quantization noise problems
448 useS32 = false;
449 stopBandAtten = 84.;
450 if (inSampleRate >= mSampleRate * 4) {
451 halfLength = 32;
452 } else if (inSampleRate >= mSampleRate * 2) {
453 halfLength = 24;
454 } else {
455 halfLength = 16;
456 }
Andy Hung06b40f92019-03-26 15:51:41 -0700457
458 if (mSampleRate <= kVoiceDeviceSampleRate) {
459 if (inSampleRate >= mSampleRate * 2) {
460 halfLength += 16;
461 } else {
462 halfLength += 8;
463 }
464 tbwCheat = 1.05;
465 } else if (inSampleRate <= mSampleRate) {
Andy Hung6bd378f2017-10-24 19:23:52 -0700466 tbwCheat = 1.03;
467 } else {
468 tbwCheat = 1.01;
469 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800470 }
471 }
472
Andy Hung06b40f92019-03-26 15:51:41 -0700473 if (fcr > 0.) {
474 ALOGV("%s: mFilterQuality:%d inSampleRate:%d mSampleRate:%d halfLength:%d "
475 "stopBandAtten:%lf fcr:%lf",
476 __func__, mFilterQuality, inSampleRate, mSampleRate, halfLength,
477 stopBandAtten, fcr);
478 } else {
479 ALOGV("%s: mFilterQuality:%d inSampleRate:%d mSampleRate:%d halfLength:%d "
480 "stopBandAtten:%lf tbwCheat:%lf",
481 __func__, mFilterQuality, inSampleRate, mSampleRate, halfLength,
482 stopBandAtten, tbwCheat);
483 }
484
485
Andy Hung86eae0e2013-12-09 12:12:46 -0800486 // determine the number of polyphases in the filterbank.
487 // for 16b, it is desirable to have 2^(16/2) = 256 phases.
488 // https://ccrma.stanford.edu/~jos/resample/Relation_Interpolation_Error_Quantization.html
489 //
490 // We are a bit more lax on this.
491
492 int phases = mSampleRate / gcd(mSampleRate, inSampleRate);
493
Andy Hung6582f2b2014-01-03 12:30:41 -0800494 // TODO: Once dynamic sample rate change is an option, the code below
495 // should be modified to execute only when dynamic sample rate change is enabled.
496 //
497 // as above, #phases less than 63 is too few phases for accurate linear interpolation.
498 // we increase the phases to compensate, but more phases means more memory per
499 // filter and more time to compute the filter.
500 //
501 // if we know that the filter will be used for dynamic sample rate changes,
502 // that would allow us skip this part for fixed sample rate resamplers.
503 //
504 while (phases<63) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800505 phases *= 2; // this code only needed to support dynamic rate changes
506 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800507
Andy Hung86eae0e2013-12-09 12:12:46 -0800508 if (phases>=256) { // too many phases, always interpolate
509 phases = 127;
510 }
511
512 // create the filter
513 mConstants.set(phases, halfLength, inSampleRate, mSampleRate);
Andy Hung6bd378f2017-10-24 19:23:52 -0700514 if (fcr > 0.) {
515 createKaiserFir(mConstants, stopBandAtten, fcr);
516 } else {
517 createKaiserFir(mConstants, stopBandAtten,
518 inSampleRate, mSampleRate, tbwCheat);
519 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800520 } // End Kaiser filter
521
522 // update phase and state based on the new filter.
523 const Constants& c(mConstants);
524 mInBuffer.resize(mChannelCount, c.mHalfNumCoefs);
525 const uint32_t phaseWrapLimit = c.mL << c.mShift;
526 // try to preserve as much of the phase fraction as possible for on-the-fly changes
527 mPhaseFraction = static_cast<unsigned long long>(mPhaseFraction)
528 * phaseWrapLimit / oldPhaseWrapLimit;
529 mPhaseFraction %= phaseWrapLimit; // should not do anything, but just in case.
Andy Hungcd044842014-08-07 11:04:34 -0700530 mPhaseIncrement = static_cast<uint32_t>(static_cast<uint64_t>(phaseWrapLimit)
Andy Hung86eae0e2013-12-09 12:12:46 -0800531 * inSampleRate / mSampleRate);
532
533 // determine which resampler to use
534 // check if locked phase (works only if mPhaseIncrement has no "fractional phase bits")
535 int locked = (mPhaseIncrement << (sizeof(mPhaseIncrement)*8 - c.mShift)) == 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800536 if (locked) {
537 mPhaseFraction = mPhaseFraction >> c.mShift << c.mShift; // remove fractional phase
538 }
Andy Hung83be2562014-02-03 14:11:09 -0800539
Andy Hung075abae2014-04-09 19:36:43 -0700540 // stride is the minimum number of filter coefficients processed per loop iteration.
541 // We currently only allow a stride of 16 to match with SIMD processing.
542 // This means that the filter length must be a multiple of 16,
543 // or half the filter length (mHalfNumCoefs) must be a multiple of 8.
544 //
545 // Note: A stride of 2 is achieved with non-SIMD processing.
546 int stride = ((c.mHalfNumCoefs & 7) == 0) ? 16 : 2;
547 LOG_ALWAYS_FATAL_IF(stride < 16, "Resampler stride must be 16 or more");
Andy Hung5e58b0a2014-06-23 19:07:29 -0700548 LOG_ALWAYS_FATAL_IF(mChannelCount < 1 || mChannelCount > 8,
Andy Hung075abae2014-04-09 19:36:43 -0700549 "Resampler channels(%d) must be between 1 to 8", mChannelCount);
550 // stride 16 (falls back to stride 2 for machines that do not support NEON)
551 if (locked) {
552 switch (mChannelCount) {
553 case 1:
554 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, true, 16>;
555 break;
556 case 2:
557 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, true, 16>;
558 break;
559 case 3:
560 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, true, 16>;
561 break;
562 case 4:
563 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, true, 16>;
564 break;
565 case 5:
566 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, true, 16>;
567 break;
568 case 6:
569 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, true, 16>;
570 break;
571 case 7:
572 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, true, 16>;
573 break;
574 case 8:
575 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, true, 16>;
576 break;
577 }
578 } else {
579 switch (mChannelCount) {
580 case 1:
581 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, false, 16>;
582 break;
583 case 2:
584 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, false, 16>;
585 break;
586 case 3:
587 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, false, 16>;
588 break;
589 case 4:
590 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, false, 16>;
591 break;
592 case 5:
593 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, false, 16>;
594 break;
595 case 6:
596 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, false, 16>;
597 break;
598 case 7:
599 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, false, 16>;
600 break;
601 case 8:
602 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, false, 16>;
603 break;
604 }
605 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800606#ifdef DEBUG_RESAMPLER
607 printf("channels:%d %s stride:%d %s coef:%d shift:%d\n",
608 mChannelCount, locked ? "locked" : "interpolated",
609 stride, useS32 ? "S32" : "S16", 2*c.mHalfNumCoefs, c.mShift);
610#endif
611}
612
Andy Hung771386e2014-04-08 18:44:38 -0700613template<typename TC, typename TI, typename TO>
Andy Hung6b3b7e32015-03-29 00:49:22 -0700614size_t AudioResamplerDyn<TC, TI, TO>::resample(int32_t* out, size_t outFrameCount,
Andy Hung86eae0e2013-12-09 12:12:46 -0800615 AudioBufferProvider* provider)
616{
Andy Hung6b3b7e32015-03-29 00:49:22 -0700617 return (this->*mResampleFunc)(reinterpret_cast<TO*>(out), outFrameCount, provider);
Andy Hung771386e2014-04-08 18:44:38 -0700618}
Andy Hung86eae0e2013-12-09 12:12:46 -0800619
Andy Hung771386e2014-04-08 18:44:38 -0700620template<typename TC, typename TI, typename TO>
Andy Hung771386e2014-04-08 18:44:38 -0700621template<int CHANNELS, bool LOCKED, int STRIDE>
Andy Hung6b3b7e32015-03-29 00:49:22 -0700622size_t AudioResamplerDyn<TC, TI, TO>::resample(TO* out, size_t outFrameCount,
Andy Hung771386e2014-04-08 18:44:38 -0700623 AudioBufferProvider* provider)
Andy Hung86eae0e2013-12-09 12:12:46 -0800624{
Andy Hung075abae2014-04-09 19:36:43 -0700625 // TODO Mono -> Mono is not supported. OUTPUT_CHANNELS reflects minimum of stereo out.
626 const int OUTPUT_CHANNELS = (CHANNELS < 2) ? 2 : CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800627 const Constants& c(mConstants);
Andy Hung771386e2014-04-08 18:44:38 -0700628 const TC* const coefs = mConstants.mFirCoefs;
629 TI* impulse = mInBuffer.getImpulse();
Andy Hung411cb8e2014-05-27 12:32:17 -0700630 size_t inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800631 uint32_t phaseFraction = mPhaseFraction;
632 const uint32_t phaseIncrement = mPhaseIncrement;
633 size_t outputIndex = 0;
Andy Hung075abae2014-04-09 19:36:43 -0700634 size_t outputSampleCount = outFrameCount * OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800635 const uint32_t phaseWrapLimit = c.mL << c.mShift;
Andy Hung71700742014-06-02 18:54:08 -0700636 size_t inFrameCount = (phaseIncrement * (uint64_t)outFrameCount + phaseFraction)
637 / phaseWrapLimit;
Jiabin Huang054ee162020-07-28 22:37:17 +0000638 // validate that inFrameCount is in signed 32 bit integer range.
Andy Hung71700742014-06-02 18:54:08 -0700639 ALOG_ASSERT(0 <= inFrameCount && inFrameCount < (1U << 31));
640
641 //ALOGV("inFrameCount:%d outFrameCount:%d"
642 // " phaseIncrement:%u phaseFraction:%u phaseWrapLimit:%u",
643 // inFrameCount, outFrameCount, phaseIncrement, phaseFraction, phaseWrapLimit);
Andy Hung86eae0e2013-12-09 12:12:46 -0800644
645 // NOTE: be very careful when modifying the code here. register
646 // pressure is very high and a small change might cause the compiler
647 // to generate far less efficient code.
Jiabin Huang054ee162020-07-28 22:37:17 +0000648 // Always validate the result with objdump or test-resample.
Andy Hung86eae0e2013-12-09 12:12:46 -0800649
650 // the following logic is a bit convoluted to keep the main processing loop
651 // as tight as possible with register allocation.
652 while (outputIndex < outputSampleCount) {
Andy Hung71700742014-06-02 18:54:08 -0700653 //ALOGV("LOOP: inFrameCount:%d outputIndex:%d outFrameCount:%d"
654 // " phaseFraction:%u phaseWrapLimit:%u",
655 // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
656
657 // check inputIndex overflow
Tobias Melin43489212016-09-16 10:04:26 +0200658 ALOG_ASSERT(inputIndex <= mBuffer.frameCount, "inputIndex%zu > frameCount%zu",
Andy Hung71700742014-06-02 18:54:08 -0700659 inputIndex, mBuffer.frameCount);
660 // Buffer is empty, fetch a new one if necessary (inFrameCount > 0).
661 // We may not fetch a new buffer if the existing data is sufficient.
662 while (mBuffer.frameCount == 0 && inFrameCount > 0) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800663 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -0800664 provider->getNextBuffer(&mBuffer);
Andy Hung86eae0e2013-12-09 12:12:46 -0800665 if (mBuffer.raw == NULL) {
Hochi Huangbd179d12016-03-28 13:30:46 -0700666 // We are either at the end of playback or in an underrun situation.
667 // Reset buffer to prevent pop noise at the next buffer.
668 mInBuffer.reset();
Andy Hung86eae0e2013-12-09 12:12:46 -0800669 goto resample_exit;
670 }
Andy Hung411cb8e2014-05-27 12:32:17 -0700671 inFrameCount -= mBuffer.frameCount;
Andy Hung86eae0e2013-12-09 12:12:46 -0800672 if (phaseFraction >= phaseWrapLimit) { // read in data
Andy Hung771386e2014-04-08 18:44:38 -0700673 mInBuffer.template readAdvance<CHANNELS>(
674 impulse, c.mHalfNumCoefs,
675 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700676 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800677 phaseFraction -= phaseWrapLimit;
678 while (phaseFraction >= phaseWrapLimit) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800679 if (inputIndex >= mBuffer.frameCount) {
Andy Hung411cb8e2014-05-27 12:32:17 -0700680 inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800681 provider->releaseBuffer(&mBuffer);
682 break;
683 }
Andy Hung771386e2014-04-08 18:44:38 -0700684 mInBuffer.template readAdvance<CHANNELS>(
685 impulse, c.mHalfNumCoefs,
686 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700687 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800688 phaseFraction -= phaseWrapLimit;
689 }
690 }
691 }
Andy Hung771386e2014-04-08 18:44:38 -0700692 const TI* const in = reinterpret_cast<const TI*>(mBuffer.raw);
Andy Hung86eae0e2013-12-09 12:12:46 -0800693 const size_t frameCount = mBuffer.frameCount;
694 const int coefShift = c.mShift;
695 const int halfNumCoefs = c.mHalfNumCoefs;
Andy Hung771386e2014-04-08 18:44:38 -0700696 const TO* const volumeSimd = mVolumeSimd;
Andy Hung86eae0e2013-12-09 12:12:46 -0800697
Andy Hung86eae0e2013-12-09 12:12:46 -0800698 // main processing loop
699 while (CC_LIKELY(outputIndex < outputSampleCount)) {
700 // caution: fir() is inlined and may be large.
701 // output will be loaded with the appropriate values
702 //
703 // from the input samples in impulse[-halfNumCoefs+1]... impulse[halfNumCoefs]
704 // from the polyphase filter of (phaseFraction / phaseWrapLimit) in coefs.
705 //
Andy Hung71700742014-06-02 18:54:08 -0700706 //ALOGV("LOOP2: inFrameCount:%d outputIndex:%d outFrameCount:%d"
707 // " phaseFraction:%u phaseWrapLimit:%u",
708 // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
709 ALOG_ASSERT(phaseFraction < phaseWrapLimit);
Andy Hung86eae0e2013-12-09 12:12:46 -0800710 fir<CHANNELS, LOCKED, STRIDE>(
711 &out[outputIndex],
712 phaseFraction, phaseWrapLimit,
713 coefShift, halfNumCoefs, coefs,
714 impulse, volumeSimd);
Andy Hung075abae2014-04-09 19:36:43 -0700715
716 outputIndex += OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800717
718 phaseFraction += phaseIncrement;
719 while (phaseFraction >= phaseWrapLimit) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800720 if (inputIndex >= frameCount) {
721 goto done; // need a new buffer
722 }
Andy Hung771386e2014-04-08 18:44:38 -0700723 mInBuffer.template readAdvance<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700724 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800725 phaseFraction -= phaseWrapLimit;
726 }
727 }
728done:
Andy Hung71700742014-06-02 18:54:08 -0700729 // We arrive here when we're finished or when the input buffer runs out.
730 // Regardless we need to release the input buffer if we've acquired it.
731 if (inputIndex > 0) { // we've acquired a buffer (alternatively could check frameCount)
Tobias Melin43489212016-09-16 10:04:26 +0200732 ALOG_ASSERT(inputIndex == frameCount, "inputIndex(%zu) != frameCount(%zu)",
Andy Hung71700742014-06-02 18:54:08 -0700733 inputIndex, frameCount); // must have been fully read.
Andy Hung411cb8e2014-05-27 12:32:17 -0700734 inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800735 provider->releaseBuffer(&mBuffer);
Andy Hung411cb8e2014-05-27 12:32:17 -0700736 ALOG_ASSERT(mBuffer.frameCount == 0);
Andy Hung86eae0e2013-12-09 12:12:46 -0800737 }
738 }
739
740resample_exit:
Andy Hung71700742014-06-02 18:54:08 -0700741 // inputIndex must be zero in all three cases:
742 // (1) the buffer never was been acquired; (2) the buffer was
743 // released at "done:"; or (3) getNextBuffer() failed.
Tobias Melin43489212016-09-16 10:04:26 +0200744 ALOG_ASSERT(inputIndex == 0, "Releasing: inputindex:%zu frameCount:%zu phaseFraction:%u",
Andy Hung71700742014-06-02 18:54:08 -0700745 inputIndex, mBuffer.frameCount, phaseFraction);
746 ALOG_ASSERT(mBuffer.frameCount == 0); // there must be no frames in the buffer
Andy Hung86eae0e2013-12-09 12:12:46 -0800747 mInBuffer.setImpulse(impulse);
Andy Hung86eae0e2013-12-09 12:12:46 -0800748 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700749 return outputIndex / OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800750}
751
Andy Hung771386e2014-04-08 18:44:38 -0700752/* instantiate templates used by AudioResampler::create */
753template class AudioResamplerDyn<float, float, float>;
754template class AudioResamplerDyn<int16_t, int16_t, int32_t>;
755template class AudioResamplerDyn<int32_t, int16_t, int32_t>;
756
Andy Hung86eae0e2013-12-09 12:12:46 -0800757// ----------------------------------------------------------------------------
Glenn Kasten63238ef2015-03-02 15:50:29 -0800758} // namespace android