blob: f600d6cfe3ed56188f3d2d772104caaeb84313c8 [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__)
Glenn Kasten4699a6a2016-02-16 10:49:09 -080046#ifndef USE_NEON
47#define USE_NEON (true)
48#endif
Mathias Agopianad9af032012-11-04 15:16:13 -080049#else
Glenn Kasten4699a6a2016-02-16 10:49:09 -080050#define USE_NEON (false)
51#endif
52#if USE_NEON
53#include <arm_neon.h>
Mathias Agopianad9af032012-11-04 15:16:13 -080054#endif
55
Zhongwei Yao12b44bd2014-04-10 17:23:42 +010056#define UNUSED(x) ((void)(x))
Mathias Agopianad9af032012-11-04 15:16:13 -080057
Mathias Agopian65ab4712010-07-14 17:59:35 -070058namespace android {
59// ----------------------------------------------------------------------------
60
61
62/*
63 * These coeficients are computed with the "fir" utility found in
64 * tools/resampler_tools
Mathias Agopiand88a0512012-10-30 12:49:07 -070065 * cmd-line: fir -l 7 -s 48000 -c 20478
Mathias Agopian65ab4712010-07-14 17:59:35 -070066 */
Glenn Kastenc4974312012-12-14 07:13:28 -080067const uint32_t AudioResamplerSinc::mFirCoefsUp[] __attribute__ ((aligned (32))) = {
Glenn Kasten675933b2015-02-17 14:23:04 -080068#include "AudioResamplerSincUp.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070069};
70
71/*
Mathias Agopian443e6962012-10-26 13:48:42 -070072 * These coefficients are optimized for 48KHz -> 44.1KHz
Mathias Agopian4ed475d2012-11-01 21:03:46 -070073 * cmd-line: fir -l 7 -s 48000 -c 17189
Mathias Agopian65ab4712010-07-14 17:59:35 -070074 */
Glenn Kastenc4974312012-12-14 07:13:28 -080075const uint32_t AudioResamplerSinc::mFirCoefsDown[] __attribute__ ((aligned (32))) = {
Glenn Kasten675933b2015-02-17 14:23:04 -080076#include "AudioResamplerSincDown.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070077};
78
Glenn Kastenac602052012-10-01 14:04:31 -070079// we use 15 bits to interpolate between these samples
80// this cannot change because the mul below rely on it.
81static const int pLerpBits = 15;
82
83static pthread_once_t once_control = PTHREAD_ONCE_INIT;
84static readCoefficientsFn readResampleCoefficients = NULL;
85
86/*static*/ AudioResamplerSinc::Constants AudioResamplerSinc::highQualityConstants;
87/*static*/ AudioResamplerSinc::Constants AudioResamplerSinc::veryHighQualityConstants;
88
89void AudioResamplerSinc::init_routine()
90{
91 // for high quality resampler, the parameters for coefficients are compile-time constants
92 Constants *c = &highQualityConstants;
93 c->coefsBits = RESAMPLE_FIR_LERP_INT_BITS;
94 c->cShift = kNumPhaseBits - c->coefsBits;
95 c->cMask = ((1<< c->coefsBits)-1) << c->cShift;
96 c->pShift = kNumPhaseBits - c->coefsBits - pLerpBits;
97 c->pMask = ((1<< pLerpBits)-1) << c->pShift;
98 c->halfNumCoefs = RESAMPLE_FIR_NUM_COEF;
99
100 // for very high quality resampler, the parameters are load-time constants
101 veryHighQualityConstants = highQualityConstants;
102
103 // Open the dll to get the coefficients for VERY_HIGH_QUALITY
104 void *resampleCoeffLib = dlopen("libaudio-resampler.so", RTLD_NOW);
105 ALOGV("Open libaudio-resampler library = %p", resampleCoeffLib);
106 if (resampleCoeffLib == NULL) {
107 ALOGE("Could not open audio-resampler library: %s", dlerror());
108 return;
109 }
110
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800111 readResampleFirNumCoeffFn readResampleFirNumCoeff;
112 readResampleFirLerpIntBitsFn readResampleFirLerpIntBits;
113
114 readResampleCoefficients = (readCoefficientsFn)
115 dlsym(resampleCoeffLib, "readResamplerCoefficients");
116 readResampleFirNumCoeff = (readResampleFirNumCoeffFn)
Glenn Kastenac602052012-10-01 14:04:31 -0700117 dlsym(resampleCoeffLib, "readResampleFirNumCoeff");
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800118 readResampleFirLerpIntBits = (readResampleFirLerpIntBitsFn)
Glenn Kastenac602052012-10-01 14:04:31 -0700119 dlsym(resampleCoeffLib, "readResampleFirLerpIntBits");
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800120
Glenn Kastenac602052012-10-01 14:04:31 -0700121 if (!readResampleCoefficients || !readResampleFirNumCoeff || !readResampleFirLerpIntBits) {
122 readResampleCoefficients = NULL;
123 dlclose(resampleCoeffLib);
124 resampleCoeffLib = NULL;
125 ALOGE("Could not find symbol: %s", dlerror());
126 return;
127 }
128
129 c = &veryHighQualityConstants;
Glenn Kastenac602052012-10-01 14:04:31 -0700130 c->coefsBits = readResampleFirLerpIntBits();
Glenn Kastenac602052012-10-01 14:04:31 -0700131 c->cShift = kNumPhaseBits - c->coefsBits;
132 c->cMask = ((1<<c->coefsBits)-1) << c->cShift;
133 c->pShift = kNumPhaseBits - c->coefsBits - pLerpBits;
134 c->pMask = ((1<<pLerpBits)-1) << c->pShift;
135 // number of zero-crossing on each side
136 c->halfNumCoefs = readResampleFirNumCoeff();
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800137 ALOGV("coefsBits = %d", c->coefsBits);
Glenn Kastenac602052012-10-01 14:04:31 -0700138 ALOGV("halfNumCoefs = %d", c->halfNumCoefs);
139 // note that we "leak" resampleCoeffLib until the process exits
140}
SathishKumar Mani76b11162012-01-17 10:49:47 -0800141
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142// ----------------------------------------------------------------------------
143
144static inline
145int32_t mulRL(int left, int32_t in, uint32_t vRL)
146{
Mathias Agopianad9af032012-11-04 15:16:13 -0800147#if USE_INLINE_ASSEMBLY
Mathias Agopian65ab4712010-07-14 17:59:35 -0700148 int32_t out;
149 if (left) {
150 asm( "smultb %[out], %[in], %[vRL] \n"
151 : [out]"=r"(out)
152 : [in]"%r"(in), [vRL]"r"(vRL)
153 : );
154 } else {
155 asm( "smultt %[out], %[in], %[vRL] \n"
156 : [out]"=r"(out)
157 : [in]"%r"(in), [vRL]"r"(vRL)
158 : );
159 }
160 return out;
161#else
Mathias Agopian1f09b4a2012-10-30 13:51:44 -0700162 int16_t v = left ? int16_t(vRL) : int16_t(vRL>>16);
163 return int32_t((int64_t(in) * v) >> 16);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700164#endif
165}
166
167static inline
168int32_t mulAdd(int16_t in, int32_t v, int32_t a)
169{
Mathias Agopianad9af032012-11-04 15:16:13 -0800170#if USE_INLINE_ASSEMBLY
Mathias Agopian65ab4712010-07-14 17:59:35 -0700171 int32_t out;
172 asm( "smlawb %[out], %[v], %[in], %[a] \n"
173 : [out]"=r"(out)
174 : [in]"%r"(in), [v]"r"(v), [a]"r"(a)
175 : );
176 return out;
177#else
Mathias Agopian1f09b4a2012-10-30 13:51:44 -0700178 return a + int32_t((int64_t(v) * in) >> 16);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700179#endif
180}
181
182static inline
183int32_t mulAddRL(int left, uint32_t inRL, int32_t v, int32_t a)
184{
Mathias Agopianad9af032012-11-04 15:16:13 -0800185#if USE_INLINE_ASSEMBLY
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186 int32_t out;
187 if (left) {
188 asm( "smlawb %[out], %[v], %[inRL], %[a] \n"
189 : [out]"=r"(out)
190 : [inRL]"%r"(inRL), [v]"r"(v), [a]"r"(a)
191 : );
192 } else {
193 asm( "smlawt %[out], %[v], %[inRL], %[a] \n"
194 : [out]"=r"(out)
195 : [inRL]"%r"(inRL), [v]"r"(v), [a]"r"(a)
196 : );
197 }
198 return out;
199#else
Mathias Agopian1f09b4a2012-10-30 13:51:44 -0700200 int16_t s = left ? int16_t(inRL) : int16_t(inRL>>16);
201 return a + int32_t((int64_t(v) * s) >> 16);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202#endif
203}
204
205// ----------------------------------------------------------------------------
206
Andy Hung3348e362014-07-07 10:21:44 -0700207AudioResamplerSinc::AudioResamplerSinc(
Glenn Kastenac602052012-10-01 14:04:31 -0700208 int inChannelCount, int32_t sampleRate, src_quality quality)
Andy Hung3348e362014-07-07 10:21:44 -0700209 : AudioResampler(inChannelCount, sampleRate, quality),
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800210 mState(0), mImpulse(0), mRingFull(0), mFirCoefs(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211{
212 /*
213 * Layout of the state buffer for 32 tap:
214 *
215 * "present" sample beginning of 2nd buffer
216 * v v
217 * 0 01 2 23 3
218 * 0 F0 0 F0 F
219 * [pppppppppppppppInnnnnnnnnnnnnnnnpppppppppppppppInnnnnnnnnnnnnnnn]
220 * ^ ^ head
221 *
222 * p = past samples, convoluted with the (p)ositive side of sinc()
223 * n = future samples, convoluted with the (n)egative side of sinc()
224 * r = extra space for implementing the ring buffer
225 *
226 */
227
Mathias Agopian0d585c82012-11-10 03:26:39 -0800228 mVolumeSIMD[0] = 0;
229 mVolumeSIMD[1] = 0;
230
Glenn Kastenac602052012-10-01 14:04:31 -0700231 // Load the constants for coefficients
232 int ok = pthread_once(&once_control, init_routine);
233 if (ok != 0) {
234 ALOGE("%s pthread_once failed: %d", __func__, ok);
SathishKumar Mani76b11162012-01-17 10:49:47 -0800235 }
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800236 mConstants = (quality == VERY_HIGH_QUALITY) ?
237 &veryHighQualityConstants : &highQualityConstants;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238}
239
SathishKumar Mani76b11162012-01-17 10:49:47 -0800240
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800241AudioResamplerSinc::~AudioResamplerSinc() {
242 free(mState);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243}
244
245void AudioResamplerSinc::init() {
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800246 const Constants& c(*mConstants);
247 const size_t numCoefs = 2 * c.halfNumCoefs;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800248 const size_t stateSize = numCoefs * mChannelCount * 2;
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800249 mState = (int16_t*)memalign(32, stateSize*sizeof(int16_t));
SathishKumar Mani76b11162012-01-17 10:49:47 -0800250 memset(mState, 0, sizeof(int16_t)*stateSize);
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800251 mImpulse = mState + (c.halfNumCoefs-1)*mChannelCount;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800252 mRingFull = mImpulse + (numCoefs+1)*mChannelCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253}
254
Andy Hung5e58b0a2014-06-23 19:07:29 -0700255void AudioResamplerSinc::setVolume(float left, float right) {
Mathias Agopian0d585c82012-11-10 03:26:39 -0800256 AudioResampler::setVolume(left, right);
Andy Hung5e58b0a2014-06-23 19:07:29 -0700257 // convert to U4_28 (rounding down).
258 // integer volume values are clamped to 0 to UNITY_GAIN.
259 mVolumeSIMD[0] = u4_28_from_float(clampFloatVol(left));
260 mVolumeSIMD[1] = u4_28_from_float(clampFloatVol(right));
Mathias Agopian0d585c82012-11-10 03:26:39 -0800261}
262
Andy Hung6b3b7e32015-03-29 00:49:22 -0700263size_t AudioResamplerSinc::resample(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264 AudioBufferProvider* provider)
265{
Glenn Kastenac602052012-10-01 14:04:31 -0700266 // FIXME store current state (up or down sample) and only load the coefs when the state
267 // changes. Or load two pointers one for up and one for down in the init function.
268 // Not critical now since the read functions are fast, but would be important if read was slow.
Mathias Agopian61ea1172012-10-21 03:04:05 -0700269 if (mConstants == &veryHighQualityConstants && readResampleCoefficients) {
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800270 mFirCoefs = readResampleCoefficients( mInSampleRate <= mSampleRate );
Glenn Kastenac602052012-10-01 14:04:31 -0700271 } else {
Glenn Kasten2f5aa012015-02-17 15:04:28 -0800272 mFirCoefs = (const int32_t *)
273 ((mInSampleRate <= mSampleRate) ? mFirCoefsUp : mFirCoefsDown);
SathishKumar Mani76b11162012-01-17 10:49:47 -0800274 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275
276 // select the appropriate resampler
277 switch (mChannelCount) {
278 case 1:
Andy Hung6b3b7e32015-03-29 00:49:22 -0700279 return resample<1>(out, outFrameCount, provider);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280 case 2:
Andy Hung6b3b7e32015-03-29 00:49:22 -0700281 return resample<2>(out, outFrameCount, provider);
282 default:
283 LOG_ALWAYS_FATAL("invalid channel count: %d", mChannelCount);
284 return 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285 }
286}
287
288
289template<int CHANNELS>
Andy Hung6b3b7e32015-03-29 00:49:22 -0700290size_t AudioResamplerSinc::resample(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291 AudioBufferProvider* provider)
292{
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800293 const Constants& c(*mConstants);
294 const size_t headOffset = c.halfNumCoefs*CHANNELS;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295 int16_t* impulse = mImpulse;
296 uint32_t vRL = mVolumeRL;
297 size_t inputIndex = mInputIndex;
298 uint32_t phaseFraction = mPhaseFraction;
299 uint32_t phaseIncrement = mPhaseIncrement;
300 size_t outputIndex = 0;
301 size_t outputSampleCount = outFrameCount * 2;
Andy Hung24781ff2014-02-19 12:45:19 -0800302 size_t inFrameCount = getInFrameCountRequired(outFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304 while (outputIndex < outputSampleCount) {
305 // buffer is empty, fetch a new one
Glenn Kastend198b612012-02-02 14:09:43 -0800306 while (mBuffer.frameCount == 0) {
307 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -0800308 provider->getNextBuffer(&mBuffer);
Glenn Kastend198b612012-02-02 14:09:43 -0800309 if (mBuffer.raw == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310 goto resample_exit;
311 }
312 const uint32_t phaseIndex = phaseFraction >> kNumPhaseBits;
313 if (phaseIndex == 1) {
314 // read one frame
Glenn Kastend198b612012-02-02 14:09:43 -0800315 read<CHANNELS>(impulse, phaseFraction, mBuffer.i16, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 } else if (phaseIndex == 2) {
317 // read 2 frames
Glenn Kastend198b612012-02-02 14:09:43 -0800318 read<CHANNELS>(impulse, phaseFraction, mBuffer.i16, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 inputIndex++;
320 if (inputIndex >= mBuffer.frameCount) {
321 inputIndex -= mBuffer.frameCount;
Glenn Kastend198b612012-02-02 14:09:43 -0800322 provider->releaseBuffer(&mBuffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 } else {
Glenn Kastend198b612012-02-02 14:09:43 -0800324 read<CHANNELS>(impulse, phaseFraction, mBuffer.i16, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700326 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327 }
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800328 int16_t const * const in = mBuffer.i16;
Glenn Kastend198b612012-02-02 14:09:43 -0800329 const size_t frameCount = mBuffer.frameCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330
331 // Always read-in the first samples from the input buffer
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800332 int16_t* head = impulse + headOffset;
Mathias Agopiana798c972012-11-03 23:37:53 -0700333 for (size_t i=0 ; i<CHANNELS ; i++) {
334 head[i] = in[inputIndex*CHANNELS + i];
335 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336
337 // handle boundary case
Mathias Agopiana798c972012-11-03 23:37:53 -0700338 while (CC_LIKELY(outputIndex < outputSampleCount)) {
Mathias Agopian0d585c82012-11-10 03:26:39 -0800339 filterCoefficient<CHANNELS>(&out[outputIndex], phaseFraction, impulse, vRL);
340 outputIndex += 2;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341
342 phaseFraction += phaseIncrement;
Mathias Agopiana798c972012-11-03 23:37:53 -0700343 const size_t phaseIndex = phaseFraction >> kNumPhaseBits;
344 for (size_t i=0 ; i<phaseIndex ; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 inputIndex++;
Mathias Agopiana798c972012-11-03 23:37:53 -0700346 if (inputIndex >= frameCount) {
347 goto done; // need a new buffer
348 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349 read<CHANNELS>(impulse, phaseFraction, in, inputIndex);
350 }
351 }
Mathias Agopiana798c972012-11-03 23:37:53 -0700352done:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 // if done with buffer, save samples
354 if (inputIndex >= frameCount) {
355 inputIndex -= frameCount;
Glenn Kastend198b612012-02-02 14:09:43 -0800356 provider->releaseBuffer(&mBuffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357 }
358 }
359
360resample_exit:
361 mImpulse = impulse;
362 mInputIndex = inputIndex;
363 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700364 return outputIndex / CHANNELS;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365}
366
367template<int CHANNELS>
368/***
369* read()
370*
371* This function reads only one frame from input buffer and writes it in
372* state buffer
373*
374**/
375void AudioResamplerSinc::read(
376 int16_t*& impulse, uint32_t& phaseFraction,
Glenn Kasten54c3b662012-01-06 07:46:30 -0800377 const int16_t* in, size_t inputIndex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379 impulse += CHANNELS;
380 phaseFraction -= 1LU<<kNumPhaseBits;
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800381
382 const Constants& c(*mConstants);
Mathias Agopiana798c972012-11-03 23:37:53 -0700383 if (CC_UNLIKELY(impulse >= mRingFull)) {
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800384 const size_t stateSize = (c.halfNumCoefs*2)*CHANNELS;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385 memcpy(mState, mState+stateSize, sizeof(int16_t)*stateSize);
386 impulse -= stateSize;
387 }
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800388
389 int16_t* head = impulse + c.halfNumCoefs*CHANNELS;
Mathias Agopiana798c972012-11-03 23:37:53 -0700390 for (size_t i=0 ; i<CHANNELS ; i++) {
391 head[i] = in[inputIndex*CHANNELS + i];
392 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393}
394
395template<int CHANNELS>
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100396void AudioResamplerSinc::filterCoefficient(int32_t* out, uint32_t phase,
397 const int16_t *samples, uint32_t vRL)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700398{
Mathias Agopian7492a7f2012-11-10 04:44:30 -0800399 // NOTE: be very careful when modifying the code here. register
400 // pressure is very high and a small change might cause the compiler
401 // to generate far less efficient code.
402 // Always sanity check the result with objdump or test-resample.
403
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404 // compute the index of the coefficient on the positive side and
405 // negative side
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800406 const Constants& c(*mConstants);
Mathias Agopian7492a7f2012-11-10 04:44:30 -0800407 const int32_t ONE = c.cMask | c.pMask;
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800408 uint32_t indexP = ( phase & c.cMask) >> c.cShift;
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800409 uint32_t lerpP = ( phase & c.pMask) >> c.pShift;
Mathias Agopian7492a7f2012-11-10 04:44:30 -0800410 uint32_t indexN = ((ONE-phase) & c.cMask) >> c.cShift;
411 uint32_t lerpN = ((ONE-phase) & c.pMask) >> c.pShift;
412
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800413 const size_t offset = c.halfNumCoefs;
Mathias Agopian46afbec2012-11-04 02:03:49 -0800414 indexP *= offset;
415 indexN *= offset;
416
Mathias Agopian7aa7ed72012-11-05 01:51:37 -0800417 int32_t const* coefsP = mFirCoefs + indexP;
418 int32_t const* coefsN = mFirCoefs + indexN;
Mathias Agopian46afbec2012-11-04 02:03:49 -0800419 int16_t const* sP = samples;
420 int16_t const* sN = samples + CHANNELS;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421
Mathias Agopian46afbec2012-11-04 02:03:49 -0800422 size_t count = offset;
Mathias Agopianad9af032012-11-04 15:16:13 -0800423
Glenn Kasten4699a6a2016-02-16 10:49:09 -0800424#if !USE_NEON
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100425 int32_t l = 0;
426 int32_t r = 0;
427 for (size_t i=0 ; i<count ; i++) {
428 interpolate<CHANNELS>(l, r, coefsP++, offset, lerpP, sP);
429 sP -= CHANNELS;
430 interpolate<CHANNELS>(l, r, coefsN++, offset, lerpN, sN);
431 sN += CHANNELS;
432 }
433 out[0] += 2 * mulRL(1, l, vRL);
434 out[1] += 2 * mulRL(0, r, vRL);
435#else
436 UNUSED(vRL);
437 if (CHANNELS == 1) {
Mathias Agopianad9af032012-11-04 15:16:13 -0800438 int32_t const* coefsP1 = coefsP + offset;
439 int32_t const* coefsN1 = coefsN + offset;
440 sP -= CHANNELS*3;
Mathias Agopianad9af032012-11-04 15:16:13 -0800441
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100442 int32x4_t sum;
443 int32x2_t lerpPN;
444 lerpPN = vdup_n_s32(0);
445 lerpPN = vld1_lane_s32((int32_t *)&lerpP, lerpPN, 0);
446 lerpPN = vld1_lane_s32((int32_t *)&lerpN, lerpPN, 1);
447 lerpPN = vshl_n_s32(lerpPN, 16);
448 sum = vdupq_n_s32(0);
Mathias Agopianad9af032012-11-04 15:16:13 -0800449
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100450 int16x4_t sampleP, sampleN;
451 int32x4_t samplePExt, sampleNExt;
452 int32x4_t coefsPV0, coefsPV1, coefsNV0, coefsNV1;
Mathias Agopianad9af032012-11-04 15:16:13 -0800453
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100454 coefsP = (const int32_t*)__builtin_assume_aligned(coefsP, 16);
455 coefsN = (const int32_t*)__builtin_assume_aligned(coefsN, 16);
456 coefsP1 = (const int32_t*)__builtin_assume_aligned(coefsP1, 16);
457 coefsN1 = (const int32_t*)__builtin_assume_aligned(coefsN1, 16);
458 for (; count > 0; count -= 4) {
459 sampleP = vld1_s16(sP);
460 sampleN = vld1_s16(sN);
461 coefsPV0 = vld1q_s32(coefsP);
462 coefsNV0 = vld1q_s32(coefsN);
463 coefsPV1 = vld1q_s32(coefsP1);
464 coefsNV1 = vld1q_s32(coefsN1);
465 sP -= 4;
466 sN += 4;
467 coefsP += 4;
468 coefsN += 4;
469 coefsP1 += 4;
470 coefsN1 += 4;
Mathias Agopianad9af032012-11-04 15:16:13 -0800471
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100472 sampleP = vrev64_s16(sampleP);
Mathias Agopianad9af032012-11-04 15:16:13 -0800473
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100474 // interpolate (step1)
475 coefsPV1 = vsubq_s32(coefsPV1, coefsPV0);
476 coefsNV1 = vsubq_s32(coefsNV1, coefsNV0);
477 samplePExt = vshll_n_s16(sampleP, 15);
478 // interpolate (step2)
479 coefsPV1 = vqrdmulhq_lane_s32(coefsPV1, lerpPN, 0);
480 coefsNV1 = vqrdmulhq_lane_s32(coefsNV1, lerpPN, 1);
481 sampleNExt = vshll_n_s16(sampleN, 15);
482 // interpolate (step3)
483 coefsPV0 = vaddq_s32(coefsPV0, coefsPV1);
484 coefsNV0 = vaddq_s32(coefsNV0, coefsNV1);
Mathias Agopianad9af032012-11-04 15:16:13 -0800485
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100486 samplePExt = vqrdmulhq_s32(samplePExt, coefsPV0);
487 sampleNExt = vqrdmulhq_s32(sampleNExt, coefsNV0);
488 sum = vaddq_s32(sum, samplePExt);
489 sum = vaddq_s32(sum, sampleNExt);
490 }
491 int32x2_t volumesV, outV;
492 volumesV = vld1_s32(mVolumeSIMD);
493 outV = vld1_s32(out);
Mathias Agopianad9af032012-11-04 15:16:13 -0800494
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100495 //add all 4 partial sums
496 int32x2_t sumLow, sumHigh;
497 sumLow = vget_low_s32(sum);
498 sumHigh = vget_high_s32(sum);
499 sumLow = vpadd_s32(sumLow, sumHigh);
500 sumLow = vpadd_s32(sumLow, sumLow);
Mathias Agopianad9af032012-11-04 15:16:13 -0800501
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100502 sumLow = vqrdmulh_s32(sumLow, volumesV);
503 outV = vadd_s32(outV, sumLow);
504 vst1_s32(out, outV);
Mathias Agopianad9af032012-11-04 15:16:13 -0800505 } else if (CHANNELS == 2) {
506 int32_t const* coefsP1 = coefsP + offset;
507 int32_t const* coefsN1 = coefsN + offset;
508 sP -= CHANNELS*3;
Mathias Agopianad9af032012-11-04 15:16:13 -0800509
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100510 int32x4_t sum0, sum1;
511 int32x2_t lerpPN;
Mathias Agopianad9af032012-11-04 15:16:13 -0800512
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100513 lerpPN = vdup_n_s32(0);
514 lerpPN = vld1_lane_s32((int32_t *)&lerpP, lerpPN, 0);
515 lerpPN = vld1_lane_s32((int32_t *)&lerpN, lerpPN, 1);
516 lerpPN = vshl_n_s32(lerpPN, 16);
517 sum0 = vdupq_n_s32(0);
518 sum1 = vdupq_n_s32(0);
Mathias Agopianad9af032012-11-04 15:16:13 -0800519
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100520 int16x4x2_t sampleP, sampleN;
521 int32x4x2_t samplePExt, sampleNExt;
522 int32x4_t coefsPV0, coefsPV1, coefsNV0, coefsNV1;
Mathias Agopianad9af032012-11-04 15:16:13 -0800523
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100524 coefsP = (const int32_t*)__builtin_assume_aligned(coefsP, 16);
525 coefsN = (const int32_t*)__builtin_assume_aligned(coefsN, 16);
526 coefsP1 = (const int32_t*)__builtin_assume_aligned(coefsP1, 16);
527 coefsN1 = (const int32_t*)__builtin_assume_aligned(coefsN1, 16);
528 for (; count > 0; count -= 4) {
529 sampleP = vld2_s16(sP);
530 sampleN = vld2_s16(sN);
531 coefsPV0 = vld1q_s32(coefsP);
532 coefsNV0 = vld1q_s32(coefsN);
533 coefsPV1 = vld1q_s32(coefsP1);
534 coefsNV1 = vld1q_s32(coefsN1);
535 sP -= 8;
536 sN += 8;
537 coefsP += 4;
538 coefsN += 4;
539 coefsP1 += 4;
540 coefsN1 += 4;
Mathias Agopianad9af032012-11-04 15:16:13 -0800541
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100542 sampleP.val[0] = vrev64_s16(sampleP.val[0]);
543 sampleP.val[1] = vrev64_s16(sampleP.val[1]);
Mathias Agopianad9af032012-11-04 15:16:13 -0800544
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100545 // interpolate (step1)
546 coefsPV1 = vsubq_s32(coefsPV1, coefsPV0);
547 coefsNV1 = vsubq_s32(coefsNV1, coefsNV0);
548 samplePExt.val[0] = vshll_n_s16(sampleP.val[0], 15);
549 samplePExt.val[1] = vshll_n_s16(sampleP.val[1], 15);
550 // interpolate (step2)
551 coefsPV1 = vqrdmulhq_lane_s32(coefsPV1, lerpPN, 0);
552 coefsNV1 = vqrdmulhq_lane_s32(coefsNV1, lerpPN, 1);
553 sampleNExt.val[0] = vshll_n_s16(sampleN.val[0], 15);
554 sampleNExt.val[1] = vshll_n_s16(sampleN.val[1], 15);
555 // interpolate (step3)
556 coefsPV0 = vaddq_s32(coefsPV0, coefsPV1);
557 coefsNV0 = vaddq_s32(coefsNV0, coefsNV1);
Mathias Agopianad9af032012-11-04 15:16:13 -0800558
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100559 samplePExt.val[0] = vqrdmulhq_s32(samplePExt.val[0], coefsPV0);
560 samplePExt.val[1] = vqrdmulhq_s32(samplePExt.val[1], coefsPV0);
561 sampleNExt.val[0] = vqrdmulhq_s32(sampleNExt.val[0], coefsNV0);
562 sampleNExt.val[1] = vqrdmulhq_s32(sampleNExt.val[1], coefsNV0);
563 sum0 = vaddq_s32(sum0, samplePExt.val[0]);
564 sum1 = vaddq_s32(sum1, samplePExt.val[1]);
565 sum0 = vaddq_s32(sum0, sampleNExt.val[0]);
566 sum1 = vaddq_s32(sum1, sampleNExt.val[1]);
567 }
568 int32x2_t volumesV, outV;
569 volumesV = vld1_s32(mVolumeSIMD);
570 outV = vld1_s32(out);
Mathias Agopianad9af032012-11-04 15:16:13 -0800571
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100572 //add all 4 partial sums
573 int32x2_t sumLow0, sumHigh0, sumLow1, sumHigh1;
574 sumLow0 = vget_low_s32(sum0);
575 sumHigh0 = vget_high_s32(sum0);
576 sumLow1 = vget_low_s32(sum1);
577 sumHigh1 = vget_high_s32(sum1);
578 sumLow0 = vpadd_s32(sumLow0, sumHigh0);
579 sumLow0 = vpadd_s32(sumLow0, sumLow0);
580 sumLow1 = vpadd_s32(sumLow1, sumHigh1);
581 sumLow1 = vpadd_s32(sumLow1, sumLow1);
Mathias Agopianad9af032012-11-04 15:16:13 -0800582
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100583 sumLow0 = vtrn_s32(sumLow0, sumLow1).val[0];
584 sumLow0 = vqrdmulh_s32(sumLow0, volumesV);
585 outV = vadd_s32(outV, sumLow0);
586 vst1_s32(out, outV);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 }
Zhongwei Yao12b44bd2014-04-10 17:23:42 +0100588#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589}
590
591template<int CHANNELS>
592void AudioResamplerSinc::interpolate(
593 int32_t& l, int32_t& r,
Mathias Agopian46afbec2012-11-04 02:03:49 -0800594 const int32_t* coefs, size_t offset,
595 int32_t lerp, const int16_t* samples)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596{
597 int32_t c0 = coefs[0];
Mathias Agopian46afbec2012-11-04 02:03:49 -0800598 int32_t c1 = coefs[offset];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599 int32_t sinc = mulAdd(lerp, (c1-c0)<<1, c0);
600 if (CHANNELS == 2) {
Glenn Kasten54c3b662012-01-06 07:46:30 -0800601 uint32_t rl = *reinterpret_cast<const uint32_t*>(samples);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 l = mulAddRL(1, rl, sinc, l);
603 r = mulAddRL(0, rl, sinc, r);
604 } else {
605 r = l = mulAdd(samples[0], sinc, l);
606 }
607}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700608// ----------------------------------------------------------------------------
Glenn Kasten63238ef2015-03-02 15:50:29 -0800609} // namespace android