blob: e93c064eff4362affea27fe73f7032596d8d0878 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2007 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
SathishKumar Mani76b11162012-01-17 10:49:47 -080017#define LOG_TAG "AudioResamplerSinc"
18//#define LOG_NDEBUG 0
19
Zhongwei Yao12b44bd2014-04-10 17:23:42 +010020#define __STDC_CONSTANT_MACROS
Mathias Agopian7aa7ed72012-11-05 01:51:37 -080021#include <malloc.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070022#include <string.h>
SathishKumar Mani76b11162012-01-17 10:49:47 -080023#include <stdlib.h>
Mathias Agopian46afbec2012-11-04 02:03:49 -080024#include <dlfcn.h>
25
Mathias Agopiana798c972012-11-03 23:37:53 -070026#include <cutils/compiler.h>
Mathias Agopian46afbec2012-11-04 02:03:49 -080027#include <cutils/properties.h>
28
29#include <utils/Log.h>
Andy Hung5e58b0a2014-06-23 19:07:29 -070030#include <audio_utils/primitives.h>
Mathias Agopian46afbec2012-11-04 02:03:49 -080031
32#include "AudioResamplerSinc.h"
33
Bernhard Rosenkraenzer4fbf2322014-09-19 01:50:16 +020034#if defined(__clang__) && !__has_builtin(__builtin_assume_aligned)
35#define __builtin_assume_aligned(p, a) \
36 (((uintptr_t(p) % (a)) == 0) ? (p) : (__builtin_unreachable(), (p)))
37#endif
Mathias Agopianad9af032012-11-04 15:16:13 -080038
39#if defined(__arm__) && !defined(__thumb__)
40#define USE_INLINE_ASSEMBLY (true)
41#else
42#define USE_INLINE_ASSEMBLY (false)
43#endif
44
Zhongwei Yao12b44bd2014-04-10 17:23:42 +010045#if defined(__aarch64__) || defined(__ARM_NEON__)
46#include <arm_neon.h>
47#define USE_NEON
Mathias Agopianad9af032012-11-04 15:16:13 -080048#else
Zhongwei Yao12b44bd2014-04-10 17:23:42 +010049#undef USE_NEON
Mathias Agopianad9af032012-11-04 15:16:13 -080050#endif
51
Zhongwei Yao12b44bd2014-04-10 17:23:42 +010052#define UNUSED(x) ((void)(x))
Mathias Agopianad9af032012-11-04 15:16:13 -080053
Mathias Agopian65ab4712010-07-14 17:59:35 -070054namespace android {
55// ----------------------------------------------------------------------------
56
57
58/*
59 * These coeficients are computed with the "fir" utility found in
60 * tools/resampler_tools
Mathias Agopiand88a0512012-10-30 12:49:07 -070061 * cmd-line: fir -l 7 -s 48000 -c 20478
Mathias Agopian65ab4712010-07-14 17:59:35 -070062 */
Glenn Kastenc4974312012-12-14 07:13:28 -080063const uint32_t AudioResamplerSinc::mFirCoefsUp[] __attribute__ ((aligned (32))) = {
Glenn Kasten675933b2015-02-17 14:23:04 -080064#include "AudioResamplerSincUp.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070065};
66
67/*
Mathias Agopian443e6962012-10-26 13:48:42 -070068 * These coefficients are optimized for 48KHz -> 44.1KHz
Mathias Agopian4ed475d2012-11-01 21:03:46 -070069 * cmd-line: fir -l 7 -s 48000 -c 17189
Mathias Agopian65ab4712010-07-14 17:59:35 -070070 */
Glenn Kastenc4974312012-12-14 07:13:28 -080071const uint32_t AudioResamplerSinc::mFirCoefsDown[] __attribute__ ((aligned (32))) = {
Glenn Kasten675933b2015-02-17 14:23:04 -080072#include "AudioResamplerSincDown.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070073};
74
Glenn Kastenac602052012-10-01 14:04:31 -070075// we use 15 bits to interpolate between these samples
76// this cannot change because the mul below rely on it.
77static const int pLerpBits = 15;
78
79static pthread_once_t once_control = PTHREAD_ONCE_INIT;
80static readCoefficientsFn readResampleCoefficients = NULL;
81
82/*static*/ AudioResamplerSinc::Constants AudioResamplerSinc::highQualityConstants;
83/*static*/ AudioResamplerSinc::Constants AudioResamplerSinc::veryHighQualityConstants;
84
85void AudioResamplerSinc::init_routine()
86{
87 // for high quality resampler, the parameters for coefficients are compile-time constants
88 Constants *c = &highQualityConstants;
89 c->coefsBits = RESAMPLE_FIR_LERP_INT_BITS;
90 c->cShift = kNumPhaseBits - c->coefsBits;
91 c->cMask = ((1<< c->coefsBits)-1) << c->cShift;
92 c->pShift = kNumPhaseBits - c->coefsBits - pLerpBits;
93 c->pMask = ((1<< pLerpBits)-1) << c->pShift;
94 c->halfNumCoefs = RESAMPLE_FIR_NUM_COEF;
95
96 // for very high quality resampler, the parameters are load-time constants
97 veryHighQualityConstants = highQualityConstants;
98
99 // Open the dll to get the coefficients for VERY_HIGH_QUALITY
100 void *resampleCoeffLib = dlopen("libaudio-resampler.so", RTLD_NOW);
101 ALOGV("Open libaudio-resampler library = %p", resampleCoeffLib);
102 if (resampleCoeffLib == NULL) {
103 ALOGE("Could not open audio-resampler library: %s", dlerror());
104 return;
105 }
106
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800107 readResampleFirNumCoeffFn readResampleFirNumCoeff;
108 readResampleFirLerpIntBitsFn readResampleFirLerpIntBits;
109
110 readResampleCoefficients = (readCoefficientsFn)
111 dlsym(resampleCoeffLib, "readResamplerCoefficients");
112 readResampleFirNumCoeff = (readResampleFirNumCoeffFn)
Glenn Kastenac602052012-10-01 14:04:31 -0700113 dlsym(resampleCoeffLib, "readResampleFirNumCoeff");
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800114 readResampleFirLerpIntBits = (readResampleFirLerpIntBitsFn)
Glenn Kastenac602052012-10-01 14:04:31 -0700115 dlsym(resampleCoeffLib, "readResampleFirLerpIntBits");
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800116
Glenn Kastenac602052012-10-01 14:04:31 -0700117 if (!readResampleCoefficients || !readResampleFirNumCoeff || !readResampleFirLerpIntBits) {
118 readResampleCoefficients = NULL;
119 dlclose(resampleCoeffLib);
120 resampleCoeffLib = NULL;
121 ALOGE("Could not find symbol: %s", dlerror());
122 return;
123 }
124
125 c = &veryHighQualityConstants;
Glenn Kastenac602052012-10-01 14:04:31 -0700126 c->coefsBits = readResampleFirLerpIntBits();
Glenn Kastenac602052012-10-01 14:04:31 -0700127 c->cShift = kNumPhaseBits - c->coefsBits;
128 c->cMask = ((1<<c->coefsBits)-1) << c->cShift;
129 c->pShift = kNumPhaseBits - c->coefsBits - pLerpBits;
130 c->pMask = ((1<<pLerpBits)-1) << c->pShift;
131 // number of zero-crossing on each side
132 c->halfNumCoefs = readResampleFirNumCoeff();
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800133 ALOGV("coefsBits = %d", c->coefsBits);
Glenn Kastenac602052012-10-01 14:04:31 -0700134 ALOGV("halfNumCoefs = %d", c->halfNumCoefs);
135 // note that we "leak" resampleCoeffLib until the process exits
136}
SathishKumar Mani76b11162012-01-17 10:49:47 -0800137
Mathias Agopian65ab4712010-07-14 17:59:35 -0700138// ----------------------------------------------------------------------------
139
140static inline
141int32_t mulRL(int left, int32_t in, uint32_t vRL)
142{
Mathias Agopianad9af032012-11-04 15:16:13 -0800143#if USE_INLINE_ASSEMBLY
Mathias Agopian65ab4712010-07-14 17:59:35 -0700144 int32_t out;
145 if (left) {
146 asm( "smultb %[out], %[in], %[vRL] \n"
147 : [out]"=r"(out)
148 : [in]"%r"(in), [vRL]"r"(vRL)
149 : );
150 } else {
151 asm( "smultt %[out], %[in], %[vRL] \n"
152 : [out]"=r"(out)
153 : [in]"%r"(in), [vRL]"r"(vRL)
154 : );
155 }
156 return out;
157#else
Mathias Agopian1f09b4a2012-10-30 13:51:44 -0700158 int16_t v = left ? int16_t(vRL) : int16_t(vRL>>16);
159 return int32_t((int64_t(in) * v) >> 16);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160#endif
161}
162
163static inline
164int32_t mulAdd(int16_t in, int32_t v, int32_t a)
165{
Mathias Agopianad9af032012-11-04 15:16:13 -0800166#if USE_INLINE_ASSEMBLY
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167 int32_t out;
168 asm( "smlawb %[out], %[v], %[in], %[a] \n"
169 : [out]"=r"(out)
170 : [in]"%r"(in), [v]"r"(v), [a]"r"(a)
171 : );
172 return out;
173#else
Mathias Agopian1f09b4a2012-10-30 13:51:44 -0700174 return a + int32_t((int64_t(v) * in) >> 16);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175#endif
176}
177
178static inline
179int32_t mulAddRL(int left, uint32_t inRL, int32_t v, int32_t a)
180{
Mathias Agopianad9af032012-11-04 15:16:13 -0800181#if USE_INLINE_ASSEMBLY
Mathias Agopian65ab4712010-07-14 17:59:35 -0700182 int32_t out;
183 if (left) {
184 asm( "smlawb %[out], %[v], %[inRL], %[a] \n"
185 : [out]"=r"(out)
186 : [inRL]"%r"(inRL), [v]"r"(v), [a]"r"(a)
187 : );
188 } else {
189 asm( "smlawt %[out], %[v], %[inRL], %[a] \n"
190 : [out]"=r"(out)
191 : [inRL]"%r"(inRL), [v]"r"(v), [a]"r"(a)
192 : );
193 }
194 return out;
195#else
Mathias Agopian1f09b4a2012-10-30 13:51:44 -0700196 int16_t s = left ? int16_t(inRL) : int16_t(inRL>>16);
197 return a + int32_t((int64_t(v) * s) >> 16);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700198#endif
199}
200
201// ----------------------------------------------------------------------------
202
Andy Hung3348e362014-07-07 10:21:44 -0700203AudioResamplerSinc::AudioResamplerSinc(
Glenn Kastenac602052012-10-01 14:04:31 -0700204 int inChannelCount, int32_t sampleRate, src_quality quality)
Andy Hung3348e362014-07-07 10:21:44 -0700205 : AudioResampler(inChannelCount, sampleRate, quality),
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800206 mState(0), mImpulse(0), mRingFull(0), mFirCoefs(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207{
208 /*
209 * Layout of the state buffer for 32 tap:
210 *
211 * "present" sample beginning of 2nd buffer
212 * v v
213 * 0 01 2 23 3
214 * 0 F0 0 F0 F
215 * [pppppppppppppppInnnnnnnnnnnnnnnnpppppppppppppppInnnnnnnnnnnnnnnn]
216 * ^ ^ head
217 *
218 * p = past samples, convoluted with the (p)ositive side of sinc()
219 * n = future samples, convoluted with the (n)egative side of sinc()
220 * r = extra space for implementing the ring buffer
221 *
222 */
223
Mathias Agopian0d585c82012-11-10 03:26:39 -0800224 mVolumeSIMD[0] = 0;
225 mVolumeSIMD[1] = 0;
226
Glenn Kastenac602052012-10-01 14:04:31 -0700227 // Load the constants for coefficients
228 int ok = pthread_once(&once_control, init_routine);
229 if (ok != 0) {
230 ALOGE("%s pthread_once failed: %d", __func__, ok);
SathishKumar Mani76b11162012-01-17 10:49:47 -0800231 }
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800232 mConstants = (quality == VERY_HIGH_QUALITY) ?
233 &veryHighQualityConstants : &highQualityConstants;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234}
235
SathishKumar Mani76b11162012-01-17 10:49:47 -0800236
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800237AudioResamplerSinc::~AudioResamplerSinc() {
238 free(mState);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239}
240
241void AudioResamplerSinc::init() {
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800242 const Constants& c(*mConstants);
243 const size_t numCoefs = 2 * c.halfNumCoefs;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800244 const size_t stateSize = numCoefs * mChannelCount * 2;
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800245 mState = (int16_t*)memalign(32, stateSize*sizeof(int16_t));
SathishKumar Mani76b11162012-01-17 10:49:47 -0800246 memset(mState, 0, sizeof(int16_t)*stateSize);
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800247 mImpulse = mState + (c.halfNumCoefs-1)*mChannelCount;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800248 mRingFull = mImpulse + (numCoefs+1)*mChannelCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249}
250
Andy Hung5e58b0a2014-06-23 19:07:29 -0700251void AudioResamplerSinc::setVolume(float left, float right) {
Mathias Agopian0d585c82012-11-10 03:26:39 -0800252 AudioResampler::setVolume(left, right);
Andy Hung5e58b0a2014-06-23 19:07:29 -0700253 // convert to U4_28 (rounding down).
254 // integer volume values are clamped to 0 to UNITY_GAIN.
255 mVolumeSIMD[0] = u4_28_from_float(clampFloatVol(left));
256 mVolumeSIMD[1] = u4_28_from_float(clampFloatVol(right));
Mathias Agopian0d585c82012-11-10 03:26:39 -0800257}
258
Andy Hung6b3b7e32015-03-29 00:49:22 -0700259size_t AudioResamplerSinc::resample(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700260 AudioBufferProvider* provider)
261{
Glenn Kastenac602052012-10-01 14:04:31 -0700262 // FIXME store current state (up or down sample) and only load the coefs when the state
263 // changes. Or load two pointers one for up and one for down in the init function.
264 // Not critical now since the read functions are fast, but would be important if read was slow.
Mathias Agopian61ea1172012-10-21 03:04:05 -0700265 if (mConstants == &veryHighQualityConstants && readResampleCoefficients) {
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800266 mFirCoefs = readResampleCoefficients( mInSampleRate <= mSampleRate );
Glenn Kastenac602052012-10-01 14:04:31 -0700267 } else {
Glenn Kasten2f5aa012015-02-17 15:04:28 -0800268 mFirCoefs = (const int32_t *)
269 ((mInSampleRate <= mSampleRate) ? mFirCoefsUp : mFirCoefsDown);
SathishKumar Mani76b11162012-01-17 10:49:47 -0800270 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271
272 // select the appropriate resampler
273 switch (mChannelCount) {
274 case 1:
Andy Hung6b3b7e32015-03-29 00:49:22 -0700275 return resample<1>(out, outFrameCount, provider);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276 case 2:
Andy Hung6b3b7e32015-03-29 00:49:22 -0700277 return resample<2>(out, outFrameCount, provider);
278 default:
279 LOG_ALWAYS_FATAL("invalid channel count: %d", mChannelCount);
280 return 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281 }
282}
283
284
285template<int CHANNELS>
Andy Hung6b3b7e32015-03-29 00:49:22 -0700286size_t AudioResamplerSinc::resample(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287 AudioBufferProvider* provider)
288{
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800289 const Constants& c(*mConstants);
290 const size_t headOffset = c.halfNumCoefs*CHANNELS;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291 int16_t* impulse = mImpulse;
292 uint32_t vRL = mVolumeRL;
293 size_t inputIndex = mInputIndex;
294 uint32_t phaseFraction = mPhaseFraction;
295 uint32_t phaseIncrement = mPhaseIncrement;
296 size_t outputIndex = 0;
297 size_t outputSampleCount = outFrameCount * 2;
Andy Hung24781ff2014-02-19 12:45:19 -0800298 size_t inFrameCount = getInFrameCountRequired(outFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300 while (outputIndex < outputSampleCount) {
301 // buffer is empty, fetch a new one
Glenn Kastend198b612012-02-02 14:09:43 -0800302 while (mBuffer.frameCount == 0) {
303 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -0800304 provider->getNextBuffer(&mBuffer);
Glenn Kastend198b612012-02-02 14:09:43 -0800305 if (mBuffer.raw == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306 goto resample_exit;
307 }
308 const uint32_t phaseIndex = phaseFraction >> kNumPhaseBits;
309 if (phaseIndex == 1) {
310 // read one frame
Glenn Kastend198b612012-02-02 14:09:43 -0800311 read<CHANNELS>(impulse, phaseFraction, mBuffer.i16, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312 } else if (phaseIndex == 2) {
313 // read 2 frames
Glenn Kastend198b612012-02-02 14:09:43 -0800314 read<CHANNELS>(impulse, phaseFraction, mBuffer.i16, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315 inputIndex++;
316 if (inputIndex >= mBuffer.frameCount) {
317 inputIndex -= mBuffer.frameCount;
Glenn Kastend198b612012-02-02 14:09:43 -0800318 provider->releaseBuffer(&mBuffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 } else {
Glenn Kastend198b612012-02-02 14:09:43 -0800320 read<CHANNELS>(impulse, phaseFraction, mBuffer.i16, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700321 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700322 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 }
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800324 int16_t const * const in = mBuffer.i16;
Glenn Kastend198b612012-02-02 14:09:43 -0800325 const size_t frameCount = mBuffer.frameCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326
327 // Always read-in the first samples from the input buffer
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800328 int16_t* head = impulse + headOffset;
Mathias Agopiana798c972012-11-03 23:37:53 -0700329 for (size_t i=0 ; i<CHANNELS ; i++) {
330 head[i] = in[inputIndex*CHANNELS + i];
331 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332
333 // handle boundary case
Mathias Agopiana798c972012-11-03 23:37:53 -0700334 while (CC_LIKELY(outputIndex < outputSampleCount)) {
Mathias Agopian0d585c82012-11-10 03:26:39 -0800335 filterCoefficient<CHANNELS>(&out[outputIndex], phaseFraction, impulse, vRL);
336 outputIndex += 2;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337
338 phaseFraction += phaseIncrement;
Mathias Agopiana798c972012-11-03 23:37:53 -0700339 const size_t phaseIndex = phaseFraction >> kNumPhaseBits;
340 for (size_t i=0 ; i<phaseIndex ; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341 inputIndex++;
Mathias Agopiana798c972012-11-03 23:37:53 -0700342 if (inputIndex >= frameCount) {
343 goto done; // need a new buffer
344 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 read<CHANNELS>(impulse, phaseFraction, in, inputIndex);
346 }
347 }
Mathias Agopiana798c972012-11-03 23:37:53 -0700348done:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349 // if done with buffer, save samples
350 if (inputIndex >= frameCount) {
351 inputIndex -= frameCount;
Glenn Kastend198b612012-02-02 14:09:43 -0800352 provider->releaseBuffer(&mBuffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 }
354 }
355
356resample_exit:
357 mImpulse = impulse;
358 mInputIndex = inputIndex;
359 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700360 return outputIndex / CHANNELS;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361}
362
363template<int CHANNELS>
364/***
365* read()
366*
367* This function reads only one frame from input buffer and writes it in
368* state buffer
369*
370**/
371void AudioResamplerSinc::read(
372 int16_t*& impulse, uint32_t& phaseFraction,
Glenn Kasten54c3b662012-01-06 07:46:30 -0800373 const int16_t* in, size_t inputIndex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375 impulse += CHANNELS;
376 phaseFraction -= 1LU<<kNumPhaseBits;
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800377
378 const Constants& c(*mConstants);
Mathias Agopiana798c972012-11-03 23:37:53 -0700379 if (CC_UNLIKELY(impulse >= mRingFull)) {
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800380 const size_t stateSize = (c.halfNumCoefs*2)*CHANNELS;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381 memcpy(mState, mState+stateSize, sizeof(int16_t)*stateSize);
382 impulse -= stateSize;
383 }
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800384
385 int16_t* head = impulse + c.halfNumCoefs*CHANNELS;
Mathias Agopiana798c972012-11-03 23:37:53 -0700386 for (size_t i=0 ; i<CHANNELS ; i++) {
387 head[i] = in[inputIndex*CHANNELS + i];
388 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389}
390
391template<int CHANNELS>
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100392void AudioResamplerSinc::filterCoefficient(int32_t* out, uint32_t phase,
393 const int16_t *samples, uint32_t vRL)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394{
Mathias Agopian7492a7f2012-11-10 04:44:30 -0800395 // NOTE: be very careful when modifying the code here. register
396 // pressure is very high and a small change might cause the compiler
397 // to generate far less efficient code.
398 // Always sanity check the result with objdump or test-resample.
399
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 // compute the index of the coefficient on the positive side and
401 // negative side
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800402 const Constants& c(*mConstants);
Mathias Agopian7492a7f2012-11-10 04:44:30 -0800403 const int32_t ONE = c.cMask | c.pMask;
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800404 uint32_t indexP = ( phase & c.cMask) >> c.cShift;
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800405 uint32_t lerpP = ( phase & c.pMask) >> c.pShift;
Mathias Agopian7492a7f2012-11-10 04:44:30 -0800406 uint32_t indexN = ((ONE-phase) & c.cMask) >> c.cShift;
407 uint32_t lerpN = ((ONE-phase) & c.pMask) >> c.pShift;
408
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800409 const size_t offset = c.halfNumCoefs;
Mathias Agopian46afbec2012-11-04 02:03:49 -0800410 indexP *= offset;
411 indexN *= offset;
412
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800413 int32_t const* coefsP = mFirCoefs + indexP;
414 int32_t const* coefsN = mFirCoefs + indexN;
Mathias Agopian46afbec2012-11-04 02:03:49 -0800415 int16_t const* sP = samples;
416 int16_t const* sN = samples + CHANNELS;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700417
Mathias Agopian46afbec2012-11-04 02:03:49 -0800418 size_t count = offset;
Mathias Agopianad9af032012-11-04 15:16:13 -0800419
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100420#ifndef USE_NEON
421 int32_t l = 0;
422 int32_t r = 0;
423 for (size_t i=0 ; i<count ; i++) {
424 interpolate<CHANNELS>(l, r, coefsP++, offset, lerpP, sP);
425 sP -= CHANNELS;
426 interpolate<CHANNELS>(l, r, coefsN++, offset, lerpN, sN);
427 sN += CHANNELS;
428 }
429 out[0] += 2 * mulRL(1, l, vRL);
430 out[1] += 2 * mulRL(0, r, vRL);
431#else
432 UNUSED(vRL);
433 if (CHANNELS == 1) {
Mathias Agopianad9af032012-11-04 15:16:13 -0800434 int32_t const* coefsP1 = coefsP + offset;
435 int32_t const* coefsN1 = coefsN + offset;
436 sP -= CHANNELS*3;
Mathias Agopianad9af032012-11-04 15:16:13 -0800437
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100438 int32x4_t sum;
439 int32x2_t lerpPN;
440 lerpPN = vdup_n_s32(0);
441 lerpPN = vld1_lane_s32((int32_t *)&lerpP, lerpPN, 0);
442 lerpPN = vld1_lane_s32((int32_t *)&lerpN, lerpPN, 1);
443 lerpPN = vshl_n_s32(lerpPN, 16);
444 sum = vdupq_n_s32(0);
Mathias Agopianad9af032012-11-04 15:16:13 -0800445
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100446 int16x4_t sampleP, sampleN;
447 int32x4_t samplePExt, sampleNExt;
448 int32x4_t coefsPV0, coefsPV1, coefsNV0, coefsNV1;
Mathias Agopianad9af032012-11-04 15:16:13 -0800449
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100450 coefsP = (const int32_t*)__builtin_assume_aligned(coefsP, 16);
451 coefsN = (const int32_t*)__builtin_assume_aligned(coefsN, 16);
452 coefsP1 = (const int32_t*)__builtin_assume_aligned(coefsP1, 16);
453 coefsN1 = (const int32_t*)__builtin_assume_aligned(coefsN1, 16);
454 for (; count > 0; count -= 4) {
455 sampleP = vld1_s16(sP);
456 sampleN = vld1_s16(sN);
457 coefsPV0 = vld1q_s32(coefsP);
458 coefsNV0 = vld1q_s32(coefsN);
459 coefsPV1 = vld1q_s32(coefsP1);
460 coefsNV1 = vld1q_s32(coefsN1);
461 sP -= 4;
462 sN += 4;
463 coefsP += 4;
464 coefsN += 4;
465 coefsP1 += 4;
466 coefsN1 += 4;
Mathias Agopianad9af032012-11-04 15:16:13 -0800467
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100468 sampleP = vrev64_s16(sampleP);
Mathias Agopianad9af032012-11-04 15:16:13 -0800469
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100470 // interpolate (step1)
471 coefsPV1 = vsubq_s32(coefsPV1, coefsPV0);
472 coefsNV1 = vsubq_s32(coefsNV1, coefsNV0);
473 samplePExt = vshll_n_s16(sampleP, 15);
474 // interpolate (step2)
475 coefsPV1 = vqrdmulhq_lane_s32(coefsPV1, lerpPN, 0);
476 coefsNV1 = vqrdmulhq_lane_s32(coefsNV1, lerpPN, 1);
477 sampleNExt = vshll_n_s16(sampleN, 15);
478 // interpolate (step3)
479 coefsPV0 = vaddq_s32(coefsPV0, coefsPV1);
480 coefsNV0 = vaddq_s32(coefsNV0, coefsNV1);
Mathias Agopianad9af032012-11-04 15:16:13 -0800481
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100482 samplePExt = vqrdmulhq_s32(samplePExt, coefsPV0);
483 sampleNExt = vqrdmulhq_s32(sampleNExt, coefsNV0);
484 sum = vaddq_s32(sum, samplePExt);
485 sum = vaddq_s32(sum, sampleNExt);
486 }
487 int32x2_t volumesV, outV;
488 volumesV = vld1_s32(mVolumeSIMD);
489 outV = vld1_s32(out);
Mathias Agopianad9af032012-11-04 15:16:13 -0800490
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100491 //add all 4 partial sums
492 int32x2_t sumLow, sumHigh;
493 sumLow = vget_low_s32(sum);
494 sumHigh = vget_high_s32(sum);
495 sumLow = vpadd_s32(sumLow, sumHigh);
496 sumLow = vpadd_s32(sumLow, sumLow);
Mathias Agopianad9af032012-11-04 15:16:13 -0800497
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100498 sumLow = vqrdmulh_s32(sumLow, volumesV);
499 outV = vadd_s32(outV, sumLow);
500 vst1_s32(out, outV);
Mathias Agopianad9af032012-11-04 15:16:13 -0800501 } else if (CHANNELS == 2) {
502 int32_t const* coefsP1 = coefsP + offset;
503 int32_t const* coefsN1 = coefsN + offset;
504 sP -= CHANNELS*3;
Mathias Agopianad9af032012-11-04 15:16:13 -0800505
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100506 int32x4_t sum0, sum1;
507 int32x2_t lerpPN;
Mathias Agopianad9af032012-11-04 15:16:13 -0800508
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100509 lerpPN = vdup_n_s32(0);
510 lerpPN = vld1_lane_s32((int32_t *)&lerpP, lerpPN, 0);
511 lerpPN = vld1_lane_s32((int32_t *)&lerpN, lerpPN, 1);
512 lerpPN = vshl_n_s32(lerpPN, 16);
513 sum0 = vdupq_n_s32(0);
514 sum1 = vdupq_n_s32(0);
Mathias Agopianad9af032012-11-04 15:16:13 -0800515
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100516 int16x4x2_t sampleP, sampleN;
517 int32x4x2_t samplePExt, sampleNExt;
518 int32x4_t coefsPV0, coefsPV1, coefsNV0, coefsNV1;
Mathias Agopianad9af032012-11-04 15:16:13 -0800519
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100520 coefsP = (const int32_t*)__builtin_assume_aligned(coefsP, 16);
521 coefsN = (const int32_t*)__builtin_assume_aligned(coefsN, 16);
522 coefsP1 = (const int32_t*)__builtin_assume_aligned(coefsP1, 16);
523 coefsN1 = (const int32_t*)__builtin_assume_aligned(coefsN1, 16);
524 for (; count > 0; count -= 4) {
525 sampleP = vld2_s16(sP);
526 sampleN = vld2_s16(sN);
527 coefsPV0 = vld1q_s32(coefsP);
528 coefsNV0 = vld1q_s32(coefsN);
529 coefsPV1 = vld1q_s32(coefsP1);
530 coefsNV1 = vld1q_s32(coefsN1);
531 sP -= 8;
532 sN += 8;
533 coefsP += 4;
534 coefsN += 4;
535 coefsP1 += 4;
536 coefsN1 += 4;
Mathias Agopianad9af032012-11-04 15:16:13 -0800537
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100538 sampleP.val[0] = vrev64_s16(sampleP.val[0]);
539 sampleP.val[1] = vrev64_s16(sampleP.val[1]);
Mathias Agopianad9af032012-11-04 15:16:13 -0800540
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100541 // interpolate (step1)
542 coefsPV1 = vsubq_s32(coefsPV1, coefsPV0);
543 coefsNV1 = vsubq_s32(coefsNV1, coefsNV0);
544 samplePExt.val[0] = vshll_n_s16(sampleP.val[0], 15);
545 samplePExt.val[1] = vshll_n_s16(sampleP.val[1], 15);
546 // interpolate (step2)
547 coefsPV1 = vqrdmulhq_lane_s32(coefsPV1, lerpPN, 0);
548 coefsNV1 = vqrdmulhq_lane_s32(coefsNV1, lerpPN, 1);
549 sampleNExt.val[0] = vshll_n_s16(sampleN.val[0], 15);
550 sampleNExt.val[1] = vshll_n_s16(sampleN.val[1], 15);
551 // interpolate (step3)
552 coefsPV0 = vaddq_s32(coefsPV0, coefsPV1);
553 coefsNV0 = vaddq_s32(coefsNV0, coefsNV1);
Mathias Agopianad9af032012-11-04 15:16:13 -0800554
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100555 samplePExt.val[0] = vqrdmulhq_s32(samplePExt.val[0], coefsPV0);
556 samplePExt.val[1] = vqrdmulhq_s32(samplePExt.val[1], coefsPV0);
557 sampleNExt.val[0] = vqrdmulhq_s32(sampleNExt.val[0], coefsNV0);
558 sampleNExt.val[1] = vqrdmulhq_s32(sampleNExt.val[1], coefsNV0);
559 sum0 = vaddq_s32(sum0, samplePExt.val[0]);
560 sum1 = vaddq_s32(sum1, samplePExt.val[1]);
561 sum0 = vaddq_s32(sum0, sampleNExt.val[0]);
562 sum1 = vaddq_s32(sum1, sampleNExt.val[1]);
563 }
564 int32x2_t volumesV, outV;
565 volumesV = vld1_s32(mVolumeSIMD);
566 outV = vld1_s32(out);
Mathias Agopianad9af032012-11-04 15:16:13 -0800567
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100568 //add all 4 partial sums
569 int32x2_t sumLow0, sumHigh0, sumLow1, sumHigh1;
570 sumLow0 = vget_low_s32(sum0);
571 sumHigh0 = vget_high_s32(sum0);
572 sumLow1 = vget_low_s32(sum1);
573 sumHigh1 = vget_high_s32(sum1);
574 sumLow0 = vpadd_s32(sumLow0, sumHigh0);
575 sumLow0 = vpadd_s32(sumLow0, sumLow0);
576 sumLow1 = vpadd_s32(sumLow1, sumHigh1);
577 sumLow1 = vpadd_s32(sumLow1, sumLow1);
Mathias Agopianad9af032012-11-04 15:16:13 -0800578
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100579 sumLow0 = vtrn_s32(sumLow0, sumLow1).val[0];
580 sumLow0 = vqrdmulh_s32(sumLow0, volumesV);
581 outV = vadd_s32(outV, sumLow0);
582 vst1_s32(out, outV);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583 }
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100584#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585}
586
587template<int CHANNELS>
588void AudioResamplerSinc::interpolate(
589 int32_t& l, int32_t& r,
Mathias Agopian46afbec2012-11-04 02:03:49 -0800590 const int32_t* coefs, size_t offset,
591 int32_t lerp, const int16_t* samples)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592{
593 int32_t c0 = coefs[0];
Mathias Agopian46afbec2012-11-04 02:03:49 -0800594 int32_t c1 = coefs[offset];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 int32_t sinc = mulAdd(lerp, (c1-c0)<<1, c0);
596 if (CHANNELS == 2) {
Glenn Kasten54c3b662012-01-06 07:46:30 -0800597 uint32_t rl = *reinterpret_cast<const uint32_t*>(samples);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700598 l = mulAddRL(1, rl, sinc, l);
599 r = mulAddRL(0, rl, sinc, r);
600 } else {
601 r = l = mulAdd(samples[0], sinc, l);
602 }
603}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604// ----------------------------------------------------------------------------
Glenn Kasten63238ef2015-03-02 15:50:29 -0800605} // namespace android