blob: 2292b19f6113795e68656223af7303f6da22f7ac [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 Hung936845a2021-06-08 00:09:06 -0700548 LOG_ALWAYS_FATAL_IF(mChannelCount < 1 || mChannelCount > FCC_LIMIT,
549 "Resampler channels(%d) must be between 1 to %d", mChannelCount, FCC_LIMIT);
Andy Hung075abae2014-04-09 19:36:43 -0700550 // stride 16 (falls back to stride 2 for machines that do not support NEON)
Andy Hung1b998522021-06-07 16:43:58 -0700551
552
553// For now use a #define as a compiler generated function table requires renaming.
554#pragma push_macro("AUDIORESAMPLERDYN_CASE")
555#undef AUDIORESAMPLERDYN_CASE
556#define AUDIORESAMPLERDYN_CASE(CHANNEL, LOCKED) \
557 case CHANNEL: if constexpr (CHANNEL <= FCC_LIMIT) {\
558 mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<CHANNEL, LOCKED, 16>; \
559 } break
560
Andy Hung075abae2014-04-09 19:36:43 -0700561 if (locked) {
562 switch (mChannelCount) {
Andy Hung1b998522021-06-07 16:43:58 -0700563 AUDIORESAMPLERDYN_CASE(1, true);
564 AUDIORESAMPLERDYN_CASE(2, true);
565 AUDIORESAMPLERDYN_CASE(3, true);
566 AUDIORESAMPLERDYN_CASE(4, true);
567 AUDIORESAMPLERDYN_CASE(5, true);
568 AUDIORESAMPLERDYN_CASE(6, true);
569 AUDIORESAMPLERDYN_CASE(7, true);
570 AUDIORESAMPLERDYN_CASE(8, true);
571 AUDIORESAMPLERDYN_CASE(9, true);
572 AUDIORESAMPLERDYN_CASE(10, true);
573 AUDIORESAMPLERDYN_CASE(11, true);
574 AUDIORESAMPLERDYN_CASE(12, true);
575 AUDIORESAMPLERDYN_CASE(13, true);
576 AUDIORESAMPLERDYN_CASE(14, true);
577 AUDIORESAMPLERDYN_CASE(15, true);
578 AUDIORESAMPLERDYN_CASE(16, true);
579 AUDIORESAMPLERDYN_CASE(17, true);
580 AUDIORESAMPLERDYN_CASE(18, true);
581 AUDIORESAMPLERDYN_CASE(19, true);
582 AUDIORESAMPLERDYN_CASE(20, true);
583 AUDIORESAMPLERDYN_CASE(21, true);
584 AUDIORESAMPLERDYN_CASE(22, true);
585 AUDIORESAMPLERDYN_CASE(23, true);
586 AUDIORESAMPLERDYN_CASE(24, true);
Andy Hung075abae2014-04-09 19:36:43 -0700587 }
588 } else {
589 switch (mChannelCount) {
Andy Hung1b998522021-06-07 16:43:58 -0700590 AUDIORESAMPLERDYN_CASE(1, false);
591 AUDIORESAMPLERDYN_CASE(2, false);
592 AUDIORESAMPLERDYN_CASE(3, false);
593 AUDIORESAMPLERDYN_CASE(4, false);
594 AUDIORESAMPLERDYN_CASE(5, false);
595 AUDIORESAMPLERDYN_CASE(6, false);
596 AUDIORESAMPLERDYN_CASE(7, false);
597 AUDIORESAMPLERDYN_CASE(8, false);
598 AUDIORESAMPLERDYN_CASE(9, false);
599 AUDIORESAMPLERDYN_CASE(10, false);
600 AUDIORESAMPLERDYN_CASE(11, false);
601 AUDIORESAMPLERDYN_CASE(12, false);
602 AUDIORESAMPLERDYN_CASE(13, false);
603 AUDIORESAMPLERDYN_CASE(14, false);
604 AUDIORESAMPLERDYN_CASE(15, false);
605 AUDIORESAMPLERDYN_CASE(16, false);
606 AUDIORESAMPLERDYN_CASE(17, false);
607 AUDIORESAMPLERDYN_CASE(18, false);
608 AUDIORESAMPLERDYN_CASE(19, false);
609 AUDIORESAMPLERDYN_CASE(20, false);
610 AUDIORESAMPLERDYN_CASE(21, false);
611 AUDIORESAMPLERDYN_CASE(22, false);
612 AUDIORESAMPLERDYN_CASE(23, false);
613 AUDIORESAMPLERDYN_CASE(24, false);
Andy Hung075abae2014-04-09 19:36:43 -0700614 }
615 }
Andy Hung1b998522021-06-07 16:43:58 -0700616#pragma pop_macro("AUDIORESAMPLERDYN_CASE")
617
Andy Hung86eae0e2013-12-09 12:12:46 -0800618#ifdef DEBUG_RESAMPLER
619 printf("channels:%d %s stride:%d %s coef:%d shift:%d\n",
620 mChannelCount, locked ? "locked" : "interpolated",
621 stride, useS32 ? "S32" : "S16", 2*c.mHalfNumCoefs, c.mShift);
622#endif
623}
624
Andy Hung771386e2014-04-08 18:44:38 -0700625template<typename TC, typename TI, typename TO>
Andy Hung6b3b7e32015-03-29 00:49:22 -0700626size_t AudioResamplerDyn<TC, TI, TO>::resample(int32_t* out, size_t outFrameCount,
Andy Hung86eae0e2013-12-09 12:12:46 -0800627 AudioBufferProvider* provider)
628{
Andy Hung6b3b7e32015-03-29 00:49:22 -0700629 return (this->*mResampleFunc)(reinterpret_cast<TO*>(out), outFrameCount, provider);
Andy Hung771386e2014-04-08 18:44:38 -0700630}
Andy Hung86eae0e2013-12-09 12:12:46 -0800631
Andy Hung771386e2014-04-08 18:44:38 -0700632template<typename TC, typename TI, typename TO>
Andy Hung771386e2014-04-08 18:44:38 -0700633template<int CHANNELS, bool LOCKED, int STRIDE>
Andy Hung6b3b7e32015-03-29 00:49:22 -0700634size_t AudioResamplerDyn<TC, TI, TO>::resample(TO* out, size_t outFrameCount,
Andy Hung771386e2014-04-08 18:44:38 -0700635 AudioBufferProvider* provider)
Andy Hung86eae0e2013-12-09 12:12:46 -0800636{
Andy Hung075abae2014-04-09 19:36:43 -0700637 // TODO Mono -> Mono is not supported. OUTPUT_CHANNELS reflects minimum of stereo out.
638 const int OUTPUT_CHANNELS = (CHANNELS < 2) ? 2 : CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800639 const Constants& c(mConstants);
Andy Hung771386e2014-04-08 18:44:38 -0700640 const TC* const coefs = mConstants.mFirCoefs;
641 TI* impulse = mInBuffer.getImpulse();
Andy Hung411cb8e2014-05-27 12:32:17 -0700642 size_t inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800643 uint32_t phaseFraction = mPhaseFraction;
644 const uint32_t phaseIncrement = mPhaseIncrement;
645 size_t outputIndex = 0;
Andy Hung075abae2014-04-09 19:36:43 -0700646 size_t outputSampleCount = outFrameCount * OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800647 const uint32_t phaseWrapLimit = c.mL << c.mShift;
Andy Hung71700742014-06-02 18:54:08 -0700648 size_t inFrameCount = (phaseIncrement * (uint64_t)outFrameCount + phaseFraction)
649 / phaseWrapLimit;
Jiabin Huang054ee162020-07-28 22:37:17 +0000650 // validate that inFrameCount is in signed 32 bit integer range.
Andy Hung71700742014-06-02 18:54:08 -0700651 ALOG_ASSERT(0 <= inFrameCount && inFrameCount < (1U << 31));
652
653 //ALOGV("inFrameCount:%d outFrameCount:%d"
654 // " phaseIncrement:%u phaseFraction:%u phaseWrapLimit:%u",
655 // inFrameCount, outFrameCount, phaseIncrement, phaseFraction, phaseWrapLimit);
Andy Hung86eae0e2013-12-09 12:12:46 -0800656
657 // NOTE: be very careful when modifying the code here. register
658 // pressure is very high and a small change might cause the compiler
659 // to generate far less efficient code.
Jiabin Huang054ee162020-07-28 22:37:17 +0000660 // Always validate the result with objdump or test-resample.
Andy Hung86eae0e2013-12-09 12:12:46 -0800661
662 // the following logic is a bit convoluted to keep the main processing loop
663 // as tight as possible with register allocation.
664 while (outputIndex < outputSampleCount) {
Andy Hung71700742014-06-02 18:54:08 -0700665 //ALOGV("LOOP: inFrameCount:%d outputIndex:%d outFrameCount:%d"
666 // " phaseFraction:%u phaseWrapLimit:%u",
667 // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
668
669 // check inputIndex overflow
Tobias Melin43489212016-09-16 10:04:26 +0200670 ALOG_ASSERT(inputIndex <= mBuffer.frameCount, "inputIndex%zu > frameCount%zu",
Andy Hung71700742014-06-02 18:54:08 -0700671 inputIndex, mBuffer.frameCount);
672 // Buffer is empty, fetch a new one if necessary (inFrameCount > 0).
673 // We may not fetch a new buffer if the existing data is sufficient.
674 while (mBuffer.frameCount == 0 && inFrameCount > 0) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800675 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -0800676 provider->getNextBuffer(&mBuffer);
Andy Hung86eae0e2013-12-09 12:12:46 -0800677 if (mBuffer.raw == NULL) {
Hochi Huangbd179d12016-03-28 13:30:46 -0700678 // We are either at the end of playback or in an underrun situation.
679 // Reset buffer to prevent pop noise at the next buffer.
680 mInBuffer.reset();
Andy Hung86eae0e2013-12-09 12:12:46 -0800681 goto resample_exit;
682 }
Andy Hung411cb8e2014-05-27 12:32:17 -0700683 inFrameCount -= mBuffer.frameCount;
Andy Hung86eae0e2013-12-09 12:12:46 -0800684 if (phaseFraction >= phaseWrapLimit) { // read in data
Andy Hung771386e2014-04-08 18:44:38 -0700685 mInBuffer.template readAdvance<CHANNELS>(
686 impulse, c.mHalfNumCoefs,
687 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700688 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800689 phaseFraction -= phaseWrapLimit;
690 while (phaseFraction >= phaseWrapLimit) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800691 if (inputIndex >= mBuffer.frameCount) {
Andy Hung411cb8e2014-05-27 12:32:17 -0700692 inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800693 provider->releaseBuffer(&mBuffer);
694 break;
695 }
Andy Hung771386e2014-04-08 18:44:38 -0700696 mInBuffer.template readAdvance<CHANNELS>(
697 impulse, c.mHalfNumCoefs,
698 reinterpret_cast<TI*>(mBuffer.raw), inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700699 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800700 phaseFraction -= phaseWrapLimit;
701 }
702 }
703 }
Andy Hung771386e2014-04-08 18:44:38 -0700704 const TI* const in = reinterpret_cast<const TI*>(mBuffer.raw);
Andy Hung86eae0e2013-12-09 12:12:46 -0800705 const size_t frameCount = mBuffer.frameCount;
706 const int coefShift = c.mShift;
707 const int halfNumCoefs = c.mHalfNumCoefs;
Andy Hung771386e2014-04-08 18:44:38 -0700708 const TO* const volumeSimd = mVolumeSimd;
Andy Hung86eae0e2013-12-09 12:12:46 -0800709
Andy Hung86eae0e2013-12-09 12:12:46 -0800710 // main processing loop
711 while (CC_LIKELY(outputIndex < outputSampleCount)) {
712 // caution: fir() is inlined and may be large.
713 // output will be loaded with the appropriate values
714 //
715 // from the input samples in impulse[-halfNumCoefs+1]... impulse[halfNumCoefs]
716 // from the polyphase filter of (phaseFraction / phaseWrapLimit) in coefs.
717 //
Andy Hung71700742014-06-02 18:54:08 -0700718 //ALOGV("LOOP2: inFrameCount:%d outputIndex:%d outFrameCount:%d"
719 // " phaseFraction:%u phaseWrapLimit:%u",
720 // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit);
721 ALOG_ASSERT(phaseFraction < phaseWrapLimit);
Andy Hung86eae0e2013-12-09 12:12:46 -0800722 fir<CHANNELS, LOCKED, STRIDE>(
723 &out[outputIndex],
724 phaseFraction, phaseWrapLimit,
725 coefShift, halfNumCoefs, coefs,
726 impulse, volumeSimd);
Andy Hung075abae2014-04-09 19:36:43 -0700727
728 outputIndex += OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800729
730 phaseFraction += phaseIncrement;
731 while (phaseFraction >= phaseWrapLimit) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800732 if (inputIndex >= frameCount) {
733 goto done; // need a new buffer
734 }
Andy Hung771386e2014-04-08 18:44:38 -0700735 mInBuffer.template readAdvance<CHANNELS>(impulse, halfNumCoefs, in, inputIndex);
Andy Hung71700742014-06-02 18:54:08 -0700736 inputIndex++;
Andy Hung86eae0e2013-12-09 12:12:46 -0800737 phaseFraction -= phaseWrapLimit;
738 }
739 }
740done:
Andy Hung71700742014-06-02 18:54:08 -0700741 // We arrive here when we're finished or when the input buffer runs out.
742 // Regardless we need to release the input buffer if we've acquired it.
743 if (inputIndex > 0) { // we've acquired a buffer (alternatively could check frameCount)
Tobias Melin43489212016-09-16 10:04:26 +0200744 ALOG_ASSERT(inputIndex == frameCount, "inputIndex(%zu) != frameCount(%zu)",
Andy Hung71700742014-06-02 18:54:08 -0700745 inputIndex, frameCount); // must have been fully read.
Andy Hung411cb8e2014-05-27 12:32:17 -0700746 inputIndex = 0;
Andy Hung86eae0e2013-12-09 12:12:46 -0800747 provider->releaseBuffer(&mBuffer);
Andy Hung411cb8e2014-05-27 12:32:17 -0700748 ALOG_ASSERT(mBuffer.frameCount == 0);
Andy Hung86eae0e2013-12-09 12:12:46 -0800749 }
750 }
751
752resample_exit:
Andy Hung71700742014-06-02 18:54:08 -0700753 // inputIndex must be zero in all three cases:
754 // (1) the buffer never was been acquired; (2) the buffer was
755 // released at "done:"; or (3) getNextBuffer() failed.
Tobias Melin43489212016-09-16 10:04:26 +0200756 ALOG_ASSERT(inputIndex == 0, "Releasing: inputindex:%zu frameCount:%zu phaseFraction:%u",
Andy Hung71700742014-06-02 18:54:08 -0700757 inputIndex, mBuffer.frameCount, phaseFraction);
758 ALOG_ASSERT(mBuffer.frameCount == 0); // there must be no frames in the buffer
Andy Hung86eae0e2013-12-09 12:12:46 -0800759 mInBuffer.setImpulse(impulse);
Andy Hung86eae0e2013-12-09 12:12:46 -0800760 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700761 return outputIndex / OUTPUT_CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800762}
763
Andy Hung771386e2014-04-08 18:44:38 -0700764/* instantiate templates used by AudioResampler::create */
765template class AudioResamplerDyn<float, float, float>;
766template class AudioResamplerDyn<int16_t, int16_t, int32_t>;
767template class AudioResamplerDyn<int32_t, int16_t, int32_t>;
768
Andy Hung86eae0e2013-12-09 12:12:46 -0800769// ----------------------------------------------------------------------------
Glenn Kasten63238ef2015-03-02 15:50:29 -0800770} // namespace android