blob: 76d2d660b7d01ffed567d17b9b23e5e9c7c42f05 [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;
47 r += *samples++ * coef;
48}
49
50template<typename TC>
51static inline
52void mac(float& l, TC coef, const float* samples)
53{
54 l += *samples++ * coef;
55}
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 Hungd5491392014-04-08 18:28:09 -070072 * Calculates a single output frame (two samples).
Andy Hung86eae0e2013-12-09 12:12:46 -080073 *
74 * This function computes both the positive half FIR dot product and
75 * the negative half FIR dot product, accumulates, and then applies the volume.
76 *
77 * This is a locked phase filter (it does not compute the interpolation).
78 *
79 * Use fir() to compute the proper coefficient pointers for a polyphase
80 * filter bank.
81 */
82
Andy Hungd5491392014-04-08 18:28:09 -070083template <int CHANNELS, int STRIDE, typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -080084static inline
Andy Hungd5491392014-04-08 18:28:09 -070085void ProcessL(TO* const out,
Andy Hung86eae0e2013-12-09 12:12:46 -080086 int count,
87 const TC* coefsP,
88 const TC* coefsN,
Andy Hungd5491392014-04-08 18:28:09 -070089 const TI* sP,
90 const TI* sN,
91 const TO* const volumeLR)
Andy Hung86eae0e2013-12-09 12:12:46 -080092{
Andy Hungd5491392014-04-08 18:28:09 -070093 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(CHANNELS >= 1 && CHANNELS <= 2)
94 if (CHANNELS == 2) {
95 TO l = 0;
96 TO r = 0;
97 do {
98 mac(l, r, *coefsP++, sP);
99 sP -= CHANNELS;
100 mac(l, r, *coefsN++, sN);
101 sN += CHANNELS;
102 } while (--count > 0);
103 out[0] += volumeAdjust(l, volumeLR[0]);
104 out[1] += volumeAdjust(r, volumeLR[1]);
105 } else { /* CHANNELS == 1 */
106 TO l = 0;
107 do {
108 mac(l, *coefsP++, sP);
109 sP -= CHANNELS;
110 mac(l, *coefsN++, sN);
111 sN += CHANNELS;
112 } while (--count > 0);
113 out[0] += volumeAdjust(l, volumeLR[0]);
114 out[1] += volumeAdjust(l, volumeLR[1]);
115 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800116}
117
118/*
Andy Hungd5491392014-04-08 18:28:09 -0700119 * Calculates a single output frame (two samples) interpolating phase.
Andy Hung86eae0e2013-12-09 12:12:46 -0800120 *
121 * This function computes both the positive half FIR dot product and
122 * the negative half FIR dot product, accumulates, and then applies the volume.
123 *
124 * This is an interpolated phase filter.
125 *
126 * Use fir() to compute the proper coefficient pointers for a polyphase
127 * filter bank.
128 */
129
Andy Hungd5491392014-04-08 18:28:09 -0700130template<typename TC, typename T>
131void adjustLerp(T& lerpP __unused)
132{
133}
134
135template<int32_t, typename T>
136void adjustLerp(T& lerpP)
137{
138 lerpP >>= 16; // lerpP is 32bit for NEON int32_t, but always 16 bit for non-NEON path
139}
140
141template<typename TC, typename TINTERP>
Andy Hung86eae0e2013-12-09 12:12:46 -0800142static inline
Andy Hungd5491392014-04-08 18:28:09 -0700143TC interpolate(TC coef_0, TC coef_1, TINTERP lerp)
144{
145 return lerp * (coef_1 - coef_0) + coef_0;
146}
147
148template<int16_t, uint32_t>
149static inline
150int16_t interpolate(int16_t coef_0, int16_t coef_1, uint32_t lerp)
151{
152 return (static_cast<int16_t>(lerp) * ((coef_1-coef_0)<<1)>>16) + coef_0;
153}
154
155template<int32_t, uint32_t>
156static inline
157int32_t interpolate(int32_t coef_0, int32_t coef_1, uint32_t lerp)
158{
159 return mulAdd(static_cast<int16_t>(lerp), (coef_1-coef_0)<<1, coef_0);
160}
161
162template <int CHANNELS, int STRIDE, typename TC, typename TI, typename TO, typename TINTERP>
163static inline
164void Process(TO* const out,
Andy Hung86eae0e2013-12-09 12:12:46 -0800165 int count,
166 const TC* coefsP,
167 const TC* coefsN,
Andy Hungd5491392014-04-08 18:28:09 -0700168 const TC* coefsP1 __unused,
169 const TC* coefsN1 __unused,
170 const TI* sP,
171 const TI* sN,
172 TINTERP lerpP,
173 const TO* const volumeLR)
Andy Hung86eae0e2013-12-09 12:12:46 -0800174{
Andy Hungd5491392014-04-08 18:28:09 -0700175 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(CHANNELS >= 1 && CHANNELS <= 2)
176 adjustLerp<TC, TINTERP>(lerpP); // coefficient type adjustment for interpolation
177
178 if (CHANNELS == 2) {
179 TO l = 0;
180 TO r = 0;
181 for (size_t i = 0; i < count; ++i) {
182 mac(l, r, interpolate(coefsP[0], coefsP[count], lerpP), sP);
183 coefsP++;
184 sP -= CHANNELS;
185 mac(l, r, interpolate(coefsN[count], coefsN[0], lerpP), sN);
186 coefsN++;
187 sN += CHANNELS;
188 }
189 out[0] += volumeAdjust(l, volumeLR[0]);
190 out[1] += volumeAdjust(r, volumeLR[1]);
191 } else { /* CHANNELS == 1 */
192 TO l = 0;
193 for (size_t i = 0; i < count; ++i) {
194 mac(l, interpolate(coefsP[0], coefsP[count], lerpP), sP);
195 coefsP++;
196 sP -= CHANNELS;
197 mac(l, interpolate(coefsN[count], coefsN[0], lerpP), sN);
198 coefsN++;
199 sN += CHANNELS;
200 }
201 out[0] += volumeAdjust(l, volumeLR[0]);
202 out[1] += volumeAdjust(l, volumeLR[1]);
Andy Hung86eae0e2013-12-09 12:12:46 -0800203 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800204}
205
206/*
Andy Hungd5491392014-04-08 18:28:09 -0700207 * Calculates a single output frame (two samples) from input sample pointer.
Andy Hung86eae0e2013-12-09 12:12:46 -0800208 *
209 * This sets up the params for the accelerated Process() and ProcessL()
210 * functions to do the appropriate dot products.
211 *
Andy Hungd5491392014-04-08 18:28:09 -0700212 * @param out should point to the output buffer with space for at least one output frame.
Andy Hung86eae0e2013-12-09 12:12:46 -0800213 *
Andy Hungd5491392014-04-08 18:28:09 -0700214 * @param phase is the fractional distance between input frames for interpolation:
Andy Hung86eae0e2013-12-09 12:12:46 -0800215 * phase >= 0 && phase < phaseWrapLimit. It can be thought of as a rational fraction
216 * of phase/phaseWrapLimit.
217 *
218 * @param phaseWrapLimit is #polyphases<<coefShift, where #polyphases is the number of polyphases
219 * in the polyphase filter. Likewise, #polyphases can be obtained as (phaseWrapLimit>>coefShift).
220 *
221 * @param coefShift gives the bit alignment of the polyphase index in the phase parameter.
222 *
223 * @param halfNumCoefs is the half the number of coefficients per polyphase filter. Since the
224 * overall filterbank is odd-length symmetric, only halfNumCoefs need be stored.
225 *
226 * @param coefs is the polyphase filter bank, starting at from polyphase index 0, and ranging to
227 * and including the #polyphases. Each polyphase of the filter has half-length halfNumCoefs
228 * (due to symmetry). The total size of the filter bank in coefficients is
229 * (#polyphases+1)*halfNumCoefs.
230 *
231 * The filter bank coefs should be aligned to a minimum of 16 bytes (preferrably to cache line).
232 *
233 * The coefs should be attenuated (to compensate for passband ripple)
234 * if storing back into the native format.
235 *
236 * @param samples are unaligned input samples. The position is in the "middle" of the
237 * sample array with respect to the FIR filter:
238 * the negative half of the filter is dot product from samples+1 to samples+halfNumCoefs;
239 * the positive half of the filter is dot product from samples to samples-halfNumCoefs+1.
240 *
241 * @param volumeLR is a pointer to an array of two 32 bit volume values, one per stereo channel,
242 * expressed as a S32 integer. A negative value inverts the channel 180 degrees.
243 * The pointer volumeLR should be aligned to a minimum of 8 bytes.
244 * A typical value for volume is 0x1000 to align to a unity gain output of 20.12.
245 *
246 * In between calls to filterCoefficient, the phase is incremented by phaseIncrement, where
247 * phaseIncrement is calculated as inputSampling * phaseWrapLimit / outputSampling.
248 *
249 * The filter polyphase index is given by indexP = phase >> coefShift. Due to
250 * odd length symmetric filter, the polyphase index of the negative half depends on
251 * whether interpolation is used.
252 *
253 * The fractional siting between the polyphase indices is given by the bits below coefShift:
254 *
255 * lerpP = phase << 32 - coefShift >> 1; // for 32 bit unsigned phase multiply
256 * lerpP = phase << 32 - coefShift >> 17; // for 16 bit unsigned phase multiply
257 *
258 * For integer types, this is expressed as:
259 *
260 * lerpP = phase << sizeof(phase)*8 - coefShift
261 * >> (sizeof(phase)-sizeof(*coefs))*8 + 1;
262 *
Andy Hungd5491392014-04-08 18:28:09 -0700263 * For floating point, lerpP is the fractional phase scaled to [0.0, 1.0):
264 *
265 * lerpP = (phase << 32 - coefShift) / (1 << 32); // floating point equivalent
Andy Hung86eae0e2013-12-09 12:12:46 -0800266 */
267
Andy Hungd5491392014-04-08 18:28:09 -0700268template<int CHANNELS, bool LOCKED, int STRIDE, typename TC, typename TI, typename TO>
Andy Hung86eae0e2013-12-09 12:12:46 -0800269static inline
Andy Hungd5491392014-04-08 18:28:09 -0700270void fir(TO* const out,
Andy Hung86eae0e2013-12-09 12:12:46 -0800271 const uint32_t phase, const uint32_t phaseWrapLimit,
272 const int coefShift, const int halfNumCoefs, const TC* const coefs,
Andy Hungd5491392014-04-08 18:28:09 -0700273 const TI* const samples, const TO* const volumeLR)
Andy Hung86eae0e2013-12-09 12:12:46 -0800274{
275 // NOTE: be very careful when modifying the code here. register
276 // pressure is very high and a small change might cause the compiler
277 // to generate far less efficient code.
278 // Always sanity check the result with objdump or test-resample.
279
280 if (LOCKED) {
281 // locked polyphase (no interpolation)
282 // Compute the polyphase filter index on the positive and negative side.
283 uint32_t indexP = phase >> coefShift;
284 uint32_t indexN = (phaseWrapLimit - phase) >> coefShift;
285 const TC* coefsP = coefs + indexP*halfNumCoefs;
286 const TC* coefsN = coefs + indexN*halfNumCoefs;
Andy Hungd5491392014-04-08 18:28:09 -0700287 const TI* sP = samples;
288 const TI* sN = samples + CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800289
290 // dot product filter.
291 ProcessL<CHANNELS, STRIDE>(out,
292 halfNumCoefs, coefsP, coefsN, sP, sN, volumeLR);
293 } else {
294 // interpolated polyphase
295 // Compute the polyphase filter index on the positive and negative side.
296 uint32_t indexP = phase >> coefShift;
297 uint32_t indexN = (phaseWrapLimit - phase - 1) >> coefShift; // one's complement.
298 const TC* coefsP = coefs + indexP*halfNumCoefs;
299 const TC* coefsN = coefs + indexN*halfNumCoefs;
300 const TC* coefsP1 = coefsP + halfNumCoefs;
301 const TC* coefsN1 = coefsN + halfNumCoefs;
Andy Hungd5491392014-04-08 18:28:09 -0700302 const TI* sP = samples;
303 const TI* sN = samples + CHANNELS;
Andy Hung86eae0e2013-12-09 12:12:46 -0800304
305 // Interpolation fraction lerpP derived by shifting all the way up and down
306 // to clear the appropriate bits and align to the appropriate level
307 // for the integer multiply. The constants should resolve in compile time.
308 //
309 // The interpolated filter coefficient is derived as follows for the pos/neg half:
310 //
311 // interpolated[P] = index[P]*lerpP + index[P+1]*(1-lerpP)
312 // interpolated[N] = index[N+1]*lerpP + index[N]*(1-lerpP)
Andy Hung86eae0e2013-12-09 12:12:46 -0800313
314 // on-the-fly interpolated dot product filter
Andy Hungd5491392014-04-08 18:28:09 -0700315 if (is_same<TC, float>::value || is_same<TC, double>::value) {
316 static const TC scale = 1. / (65536. * 65536.); // scale phase bits to [0.0, 1.0)
317 TC lerpP = TC(phase << (sizeof(phase)*8 - coefShift)) * scale;
318
319 Process<CHANNELS, STRIDE>(out,
320 halfNumCoefs, coefsP, coefsN, coefsP1, coefsN1, sP, sN, lerpP, volumeLR);
321 } else {
322 uint32_t lerpP = phase << (sizeof(phase)*8 - coefShift)
323 >> ((sizeof(phase)-sizeof(*coefs))*8 + 1);
324
325 Process<CHANNELS, STRIDE>(out,
326 halfNumCoefs, coefsP, coefsN, coefsP1, coefsN1, sP, sN, lerpP, volumeLR);
327 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800328 }
329}
330
331}; // namespace android
332
333#endif /*ANDROID_AUDIO_RESAMPLER_FIR_PROCESS_H*/