blob: bb0f1c995c5ad18e6629ee907e01b649d733b90b [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#ifndef ANDROID_AUDIO_RESAMPLER_FIR_PROCESS_H
18#define ANDROID_AUDIO_RESAMPLER_FIR_PROCESS_H
19
20namespace android {
21
22// depends on AudioResamplerFirOps.h
23
Andy Hungd5491392014-04-08 18:28:09 -070024/* variant for input type TI = int16_t input samples */
25template<typename TC>
Andy Hung86eae0e2013-12-09 12:12:46 -080026static inline
Andy Hungd5491392014-04-08 18:28:09 -070027void mac(int32_t& l, int32_t& r, TC coef, const int16_t* samples)
Andy Hung86eae0e2013-12-09 12:12:46 -080028{
Andy Hungd5491392014-04-08 18:28:09 -070029 uint32_t rl = *reinterpret_cast<const uint32_t*>(samples);
30 l = mulAddRL(1, rl, coef, l);
31 r = mulAddRL(0, rl, coef, r);
Andy Hung86eae0e2013-12-09 12:12:46 -080032}
33
Andy Hungd5491392014-04-08 18:28:09 -070034template<typename TC>
Andy Hung86eae0e2013-12-09 12:12:46 -080035static inline
Andy Hungd5491392014-04-08 18:28:09 -070036void mac(int32_t& l, TC coef, const int16_t* samples)
Andy Hung86eae0e2013-12-09 12:12:46 -080037{
Andy Hungd5491392014-04-08 18:28:09 -070038 l = mulAdd(samples[0], coef, l);
39}
Andy Hung86eae0e2013-12-09 12:12:46 -080040
Andy Hungd5491392014-04-08 18:28:09 -070041/* variant for input type TI = float input samples */
42template<typename TC>
43static inline
44void mac(float& l, float& r, TC coef, const float* samples)
45{
46 l += *samples++ * coef;
Andy Hung68ffa202014-04-09 19:19:06 -070047 r += *samples * coef;
Andy Hungd5491392014-04-08 18:28:09 -070048}
49
50template<typename TC>
51static inline
52void mac(float& l, TC coef, const float* samples)
53{
Andy Hung68ffa202014-04-09 19:19:06 -070054 l += *samples * coef;
Andy Hungd5491392014-04-08 18:28:09 -070055}
56
57/* variant for output type TO = int32_t output samples */
58static inline
59int32_t volumeAdjust(int32_t value, int32_t volume)
60{
61 return 2 * mulRL(0, value, volume); // Note: only use top 16b
62}
63
64/* variant for output type TO = float output samples */
65static inline
66float volumeAdjust(float value, float volume)
67{
68 return value * volume;
Andy Hung86eae0e2013-12-09 12:12:46 -080069}
70
71/*
Andy Hung68ffa202014-04-09 19:19:06 -070072 * Helper template functions for loop unrolling accumulator operations.
Andy Hung86eae0e2013-12-09 12:12:46 -080073 *
Andy Hung68ffa202014-04-09 19:19:06 -070074 * Unrolling the loops achieves about 2x gain.
75 * Using a recursive template rather than an array of TO[] for the accumulator
76 * values is an additional 10-20% gain.
Andy Hung86eae0e2013-12-09 12:12:46 -080077 */
78
Andy Hung68ffa202014-04-09 19:19:06 -070079template<int CHANNELS, typename TO>
80class Accumulator : public Accumulator<CHANNELS-1, TO> // recursive
Andy Hung86eae0e2013-12-09 12:12:46 -080081{
Andy Hung68ffa202014-04-09 19:19:06 -070082public:
83 inline void clear() {
84 value = 0;
85 Accumulator<CHANNELS-1, TO>::clear();
Andy Hungd5491392014-04-08 18:28:09 -070086 }
Andy Hung68ffa202014-04-09 19:19:06 -070087 template<typename TC, typename TI>
88 inline void acc(TC coef, const TI*& data) {
89 mac(value, coef, data++);
90 Accumulator<CHANNELS-1, TO>::acc(coef, data);
91 }
92 inline void volume(TO*& out, TO gain) {
93 *out++ = volumeAdjust(value, gain);
94 Accumulator<CHANNELS-1, TO>::volume(out, gain);
95 }
96
97 TO value; // one per recursive inherited base class
98};
99
100template<typename TO>
101class Accumulator<0, TO> {
102public:
103 inline void clear() {
104 }
105 template<typename TC, typename TI>
106 inline void acc(TC coef __unused, const TI*& data __unused) {
107 }
108 inline void volume(TO*& out __unused, TO gain __unused) {
109 }
110};
Andy Hung86eae0e2013-12-09 12:12:46 -0800111
112/*
Andy Hung68ffa202014-04-09 19:19:06 -0700113 * Helper template functions for interpolating filter coefficients.
Andy Hung86eae0e2013-12-09 12:12:46 -0800114 */
115
Andy Hungd5491392014-04-08 18:28:09 -0700116template<typename TC, typename T>
117void adjustLerp(T& lerpP __unused)
118{
119}
120
121template<int32_t, typename T>
122void adjustLerp(T& lerpP)
123{
124 lerpP >>= 16; // lerpP is 32bit for NEON int32_t, but always 16 bit for non-NEON path
125}
126
127template<typename TC, typename TINTERP>
Andy Hung86eae0e2013-12-09 12:12:46 -0800128static inline
Andy Hungd5491392014-04-08 18:28:09 -0700129TC interpolate(TC coef_0, TC coef_1, TINTERP lerp)
130{
131 return lerp * (coef_1 - coef_0) + coef_0;
132}
133
134template<int16_t, uint32_t>
135static inline
136int16_t interpolate(int16_t coef_0, int16_t coef_1, uint32_t lerp)
137{
138 return (static_cast<int16_t>(lerp) * ((coef_1-coef_0)<<1)>>16) + coef_0;
139}
140
141template<int32_t, uint32_t>
142static inline
143int32_t interpolate(int32_t coef_0, int32_t coef_1, uint32_t lerp)
144{
145 return mulAdd(static_cast<int16_t>(lerp), (coef_1-coef_0)<<1, coef_0);
146}
147
Andy Hung68ffa202014-04-09 19:19:06 -0700148/* class scope for passing in functions into templates */
149struct InterpCompute {
150 template<typename TC, typename TINTERP>
151 static inline
152 TC interpolatep(TC coef_0, TC coef_1, TINTERP lerp) {
153 return interpolate(coef_0, coef_1, lerp);
154 }
155
156 template<typename TC, typename TINTERP>
157 static inline
158 TC interpolaten(TC coef_0, TC coef_1, TINTERP lerp) {
159 return interpolate(coef_0, coef_1, lerp);
160 }
161};
162
163struct InterpNull {
164 template<typename TC, typename TINTERP>
165 static inline
166 TC interpolatep(TC coef_0, TC coef_1 __unused, TINTERP lerp __unused) {
167 return coef_0;
168 }
169
170 template<typename TC, typename TINTERP>
171 static inline
172 TC interpolaten(TC coef_0 __unused, TC coef_1, TINTERP lerp __unused) {
173 return coef_1;
174 }
175};
176
177/*
178 * Calculates a single output frame (two samples).
179 *
180 * The Process*() functions compute both the positive half FIR dot product and
181 * the negative half FIR dot product, accumulates, and then applies the volume.
182 *
183 * Use fir() to compute the proper coefficient pointers for a polyphase
184 * filter bank.
185 *
186 * ProcessBase() is the fundamental processing template function.
187 *
188 * ProcessL() calls ProcessBase() with TFUNC = InterpNull, for fixed/locked phase.
189 * Process() calls ProcessBase() with TFUNC = InterpCompute, for interpolated phase.
190 */
191
192template <int CHANNELS, int STRIDE, typename TFUNC, typename TC, typename TI, typename TO, typename TINTERP>
193static inline
194void ProcessBase(TO* const out,
195 int count,
196 const TC* coefsP,
197 const TC* coefsN,
198 const TI* sP,
199 const TI* sN,
200 TINTERP lerpP,
201 const TO* const volumeLR)
202{
203 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(CHANNELS > 0)
204
205 if (CHANNELS > 2) {
206 // TO accum[CHANNELS];
207 Accumulator<CHANNELS, TO> accum;
208
209 // for (int j = 0; j < CHANNELS; ++j) accum[j] = 0;
210 accum.clear();
211 for (size_t i = 0; i < count; ++i) {
212 TC c = TFUNC::interpolatep(coefsP[0], coefsP[count], lerpP);
213
214 // for (int j = 0; j < CHANNELS; ++j) mac(accum[j], c, sP + j);
215 const TI *tmp_data = sP; // tmp_ptr seems to work better
216 accum.acc(c, tmp_data);
217
218 coefsP++;
219 sP -= CHANNELS;
220 c = TFUNC::interpolaten(coefsN[count], coefsN[0], lerpP);
221
222 // for (int j = 0; j < CHANNELS; ++j) mac(accum[j], c, sN + j);
223 tmp_data = sN; // tmp_ptr seems faster than directly using sN
224 accum.acc(c, tmp_data);
225
226 coefsN++;
227 sN += CHANNELS;
228 }
229 // for (int j = 0; j < CHANNELS; ++j) out[j] += volumeAdjust(accum[j], volumeLR[0]);
230 TO *tmp_out = out; // may remove if const out definition changes.
231 accum.volume(tmp_out, volumeLR[0]);
232 } else if (CHANNELS == 2) {
233 TO l = 0;
234 TO r = 0;
235 for (size_t i = 0; i < count; ++i) {
236 mac(l, r, TFUNC::interpolatep(coefsP[0], coefsP[count], lerpP), sP);
237 coefsP++;
238 sP -= CHANNELS;
239 mac(l, r, TFUNC::interpolaten(coefsN[count], coefsN[0], lerpP), sN);
240 coefsN++;
241 sN += CHANNELS;
242 }
243 out[0] += volumeAdjust(l, volumeLR[0]);
244 out[1] += volumeAdjust(r, volumeLR[1]);
245 } else { /* CHANNELS == 1 */
246 TO l = 0;
247 for (size_t i = 0; i < count; ++i) {
248 mac(l, TFUNC::interpolatep(coefsP[0], coefsP[count], lerpP), sP);
249 coefsP++;
250 sP -= CHANNELS;
251 mac(l, TFUNC::interpolaten(coefsN[count], coefsN[0], lerpP), sN);
252 coefsN++;
253 sN += CHANNELS;
254 }
255 out[0] += volumeAdjust(l, volumeLR[0]);
256 out[1] += volumeAdjust(l, volumeLR[1]);
257 }
258}
259
260template <int CHANNELS, int STRIDE, typename TC, typename TI, typename TO>
261static inline
262void ProcessL(TO* const out,
263 int count,
264 const TC* coefsP,
265 const TC* coefsN,
266 const TI* sP,
267 const TI* sN,
268 const TO* const volumeLR)
269{
270 ProcessBase<CHANNELS, STRIDE, InterpNull>(out, count, coefsP, coefsN, sP, sN, 0, volumeLR);
271}
272
Andy Hungd5491392014-04-08 18:28:09 -0700273template <int CHANNELS, int STRIDE, typename TC, typename TI, typename TO, typename TINTERP>
274static inline
275void Process(TO* const out,
Andy Hung86eae0e2013-12-09 12:12:46 -0800276 int count,
277 const TC* coefsP,
278 const TC* coefsN,
Andy Hungd5491392014-04-08 18:28:09 -0700279 const TC* coefsP1 __unused,
280 const TC* coefsN1 __unused,
281 const TI* sP,
282 const TI* sN,
283 TINTERP lerpP,
284 const TO* const volumeLR)
Andy Hung86eae0e2013-12-09 12:12:46 -0800285{
Andy Hung68ffa202014-04-09 19:19:06 -0700286 adjustLerp<TC, TINTERP>(lerpP); // coefficient type adjustment for interpolations
287 ProcessBase<CHANNELS, STRIDE, InterpCompute>(out, count, coefsP, coefsN, sP, sN, lerpP, volumeLR);
Andy Hung86eae0e2013-12-09 12:12:46 -0800288}
289
290/*
Andy Hungd5491392014-04-08 18:28:09 -0700291 * Calculates a single output frame (two samples) from input sample pointer.
Andy Hung86eae0e2013-12-09 12:12:46 -0800292 *
293 * This sets up the params for the accelerated Process() and ProcessL()
294 * functions to do the appropriate dot products.
295 *
Andy Hungd5491392014-04-08 18:28:09 -0700296 * @param out should point to the output buffer with space for at least one output frame.
Andy Hung86eae0e2013-12-09 12:12:46 -0800297 *
Andy Hungd5491392014-04-08 18:28:09 -0700298 * @param phase is the fractional distance between input frames for interpolation:
Andy Hung86eae0e2013-12-09 12:12:46 -0800299 * phase >= 0 && phase < phaseWrapLimit. It can be thought of as a rational fraction
300 * of phase/phaseWrapLimit.
301 *
302 * @param phaseWrapLimit is #polyphases<<coefShift, where #polyphases is the number of polyphases
303 * in the polyphase filter. Likewise, #polyphases can be obtained as (phaseWrapLimit>>coefShift).
304 *
305 * @param coefShift gives the bit alignment of the polyphase index in the phase parameter.
306 *
307 * @param halfNumCoefs is the half the number of coefficients per polyphase filter. Since the
308 * overall filterbank is odd-length symmetric, only halfNumCoefs need be stored.
309 *
310 * @param coefs is the polyphase filter bank, starting at from polyphase index 0, and ranging to
311 * and including the #polyphases. Each polyphase of the filter has half-length halfNumCoefs
312 * (due to symmetry). The total size of the filter bank in coefficients is
313 * (#polyphases+1)*halfNumCoefs.
314 *
315 * The filter bank coefs should be aligned to a minimum of 16 bytes (preferrably to cache line).
316 *
317 * The coefs should be attenuated (to compensate for passband ripple)
318 * if storing back into the native format.
319 *
320 * @param samples are unaligned input samples. The position is in the "middle" of the
321 * sample array with respect to the FIR filter:
322 * the negative half of the filter is dot product from samples+1 to samples+halfNumCoefs;
323 * the positive half of the filter is dot product from samples to samples-halfNumCoefs+1.
324 *
325 * @param volumeLR is a pointer to an array of two 32 bit volume values, one per stereo channel,
326 * expressed as a S32 integer. A negative value inverts the channel 180 degrees.
327 * The pointer volumeLR should be aligned to a minimum of 8 bytes.
328 * A typical value for volume is 0x1000 to align to a unity gain output of 20.12.
329 *
330 * In between calls to filterCoefficient, the phase is incremented by phaseIncrement, where
331 * phaseIncrement is calculated as inputSampling * phaseWrapLimit / outputSampling.
332 *
333 * The filter polyphase index is given by indexP = phase >> coefShift. Due to
334 * odd length symmetric filter, the polyphase index of the negative half depends on
335 * whether interpolation is used.
336 *
337 * The fractional siting between the polyphase indices is given by the bits below coefShift:
338 *
339 * lerpP = phase << 32 - coefShift >> 1; // for 32 bit unsigned phase multiply
340 * lerpP = phase << 32 - coefShift >> 17; // for 16 bit unsigned phase multiply
341 *
342 * For integer types, this is expressed as:
343 *
344 * lerpP = phase << sizeof(phase)*8 - coefShift
345 * >> (sizeof(phase)-sizeof(*coefs))*8 + 1;
346 *
Andy Hungd5491392014-04-08 18:28:09 -0700347 * For floating point, lerpP is the fractional phase scaled to [0.0, 1.0):
348 *
349 * lerpP = (phase << 32 - coefShift) / (1 << 32); // floating point equivalent
Andy Hung86eae0e2013-12-09 12:12:46 -0800350 */
351
Andy Hungd5491392014-04-08 18:28:09 -0700352template<int CHANNELS, bool LOCKED, int STRIDE, typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -0800353static inline
Andy Hungd5491392014-04-08 18:28:09 -0700354void fir(TO* const out,
Andy Hung86eae0e2013-12-09 12:12:46 -0800355 const uint32_t phase, const uint32_t phaseWrapLimit,
356 const int coefShift, const int halfNumCoefs, const TC* const coefs,
Andy Hungd5491392014-04-08 18:28:09 -0700357 const TI* const samples, const TO* const volumeLR)
Andy Hung86eae0e2013-12-09 12:12:46 -0800358{
359 // NOTE: be very careful when modifying the code here. register
360 // pressure is very high and a small change might cause the compiler
361 // to generate far less efficient code.
362 // Always sanity check the result with objdump or test-resample.
363
364 if (LOCKED) {
365 // locked polyphase (no interpolation)
366 // Compute the polyphase filter index on the positive and negative side.
367 uint32_t indexP = phase >> coefShift;
368 uint32_t indexN = (phaseWrapLimit - phase) >> coefShift;
369 const TC* coefsP = coefs + indexP*halfNumCoefs;
370 const TC* coefsN = coefs + indexN*halfNumCoefs;
Andy Hungd5491392014-04-08 18:28:09 -0700371 const TI* sP = samples;
372 const TI* sN = samples + CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800373
374 // dot product filter.
375 ProcessL<CHANNELS, STRIDE>(out,
376 halfNumCoefs, coefsP, coefsN, sP, sN, volumeLR);
377 } else {
378 // interpolated polyphase
379 // Compute the polyphase filter index on the positive and negative side.
380 uint32_t indexP = phase >> coefShift;
381 uint32_t indexN = (phaseWrapLimit - phase - 1) >> coefShift; // one's complement.
382 const TC* coefsP = coefs + indexP*halfNumCoefs;
383 const TC* coefsN = coefs + indexN*halfNumCoefs;
384 const TC* coefsP1 = coefsP + halfNumCoefs;
385 const TC* coefsN1 = coefsN + halfNumCoefs;
Andy Hungd5491392014-04-08 18:28:09 -0700386 const TI* sP = samples;
387 const TI* sN = samples + CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800388
389 // Interpolation fraction lerpP derived by shifting all the way up and down
390 // to clear the appropriate bits and align to the appropriate level
391 // for the integer multiply. The constants should resolve in compile time.
392 //
393 // The interpolated filter coefficient is derived as follows for the pos/neg half:
394 //
395 // interpolated[P] = index[P]*lerpP + index[P+1]*(1-lerpP)
396 // interpolated[N] = index[N+1]*lerpP + index[N]*(1-lerpP)
Andy Hung86eae0e2013-12-09 12:12:46 -0800397
398 // on-the-fly interpolated dot product filter
Andy Hungd5491392014-04-08 18:28:09 -0700399 if (is_same<TC, float>::value || is_same<TC, double>::value) {
400 static const TC scale = 1. / (65536. * 65536.); // scale phase bits to [0.0, 1.0)
401 TC lerpP = TC(phase << (sizeof(phase)*8 - coefShift)) * scale;
402
403 Process<CHANNELS, STRIDE>(out,
404 halfNumCoefs, coefsP, coefsN, coefsP1, coefsN1, sP, sN, lerpP, volumeLR);
405 } else {
406 uint32_t lerpP = phase << (sizeof(phase)*8 - coefShift)
407 >> ((sizeof(phase)-sizeof(*coefs))*8 + 1);
408
409 Process<CHANNELS, STRIDE>(out,
410 halfNumCoefs, coefsP, coefsN, coefsP1, coefsN1, sP, sN, lerpP, volumeLR);
411 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800412 }
413}
414
415}; // namespace android
416
417#endif /*ANDROID_AUDIO_RESAMPLER_FIR_PROCESS_H*/