Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | namespace android { |
| 21 | |
| 22 | // depends on AudioResamplerFirOps.h |
| 23 | |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 24 | /* variant for input type TI = int16_t input samples */ |
| 25 | template<typename TC> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 26 | static inline |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 27 | void mac(int32_t& l, int32_t& r, TC coef, const int16_t* samples) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 28 | { |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 29 | uint32_t rl = *reinterpret_cast<const uint32_t*>(samples); |
| 30 | l = mulAddRL(1, rl, coef, l); |
| 31 | r = mulAddRL(0, rl, coef, r); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 34 | template<typename TC> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 35 | static inline |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 36 | void mac(int32_t& l, TC coef, const int16_t* samples) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 37 | { |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 38 | l = mulAdd(samples[0], coef, l); |
| 39 | } |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 40 | |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 41 | /* variant for input type TI = float input samples */ |
| 42 | template<typename TC> |
| 43 | static inline |
| 44 | void mac(float& l, float& r, TC coef, const float* samples) |
| 45 | { |
| 46 | l += *samples++ * coef; |
| 47 | r += *samples++ * coef; |
| 48 | } |
| 49 | |
| 50 | template<typename TC> |
| 51 | static inline |
| 52 | void 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 */ |
| 58 | static inline |
| 59 | int32_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 */ |
| 65 | static inline |
| 66 | float volumeAdjust(float value, float volume) |
| 67 | { |
| 68 | return value * volume; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 72 | * Calculates a single output frame (two samples). |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 73 | * |
| 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 Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 83 | template <int CHANNELS, int STRIDE, typename TC, typename TI, typename TO> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 84 | static inline |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 85 | void ProcessL(TO* const out, |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 86 | int count, |
| 87 | const TC* coefsP, |
| 88 | const TC* coefsN, |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 89 | const TI* sP, |
| 90 | const TI* sN, |
| 91 | const TO* const volumeLR) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 92 | { |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 93 | 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 Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 119 | * Calculates a single output frame (two samples) interpolating phase. |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 120 | * |
| 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 Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 130 | template<typename TC, typename T> |
| 131 | void adjustLerp(T& lerpP __unused) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | template<int32_t, typename T> |
| 136 | void adjustLerp(T& lerpP) |
| 137 | { |
| 138 | lerpP >>= 16; // lerpP is 32bit for NEON int32_t, but always 16 bit for non-NEON path |
| 139 | } |
| 140 | |
| 141 | template<typename TC, typename TINTERP> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 142 | static inline |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 143 | TC interpolate(TC coef_0, TC coef_1, TINTERP lerp) |
| 144 | { |
| 145 | return lerp * (coef_1 - coef_0) + coef_0; |
| 146 | } |
| 147 | |
| 148 | template<int16_t, uint32_t> |
| 149 | static inline |
| 150 | int16_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 | |
| 155 | template<int32_t, uint32_t> |
| 156 | static inline |
| 157 | int32_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 | |
| 162 | template <int CHANNELS, int STRIDE, typename TC, typename TI, typename TO, typename TINTERP> |
| 163 | static inline |
| 164 | void Process(TO* const out, |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 165 | int count, |
| 166 | const TC* coefsP, |
| 167 | const TC* coefsN, |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 168 | 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 Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 174 | { |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 175 | 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 Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 203 | } |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 207 | * Calculates a single output frame (two samples) from input sample pointer. |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 208 | * |
| 209 | * This sets up the params for the accelerated Process() and ProcessL() |
| 210 | * functions to do the appropriate dot products. |
| 211 | * |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 212 | * @param out should point to the output buffer with space for at least one output frame. |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 213 | * |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 214 | * @param phase is the fractional distance between input frames for interpolation: |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 215 | * 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 Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 263 | * 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 Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 266 | */ |
| 267 | |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 268 | template<int CHANNELS, bool LOCKED, int STRIDE, typename TC, typename TI, typename TO> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 269 | static inline |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 270 | void fir(TO* const out, |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 271 | const uint32_t phase, const uint32_t phaseWrapLimit, |
| 272 | const int coefShift, const int halfNumCoefs, const TC* const coefs, |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 273 | const TI* const samples, const TO* const volumeLR) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 274 | { |
| 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 Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 287 | const TI* sP = samples; |
| 288 | const TI* sN = samples + CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 289 | |
| 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 Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 302 | const TI* sP = samples; |
| 303 | const TI* sN = samples + CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 304 | |
| 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 Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 313 | |
| 314 | // on-the-fly interpolated dot product filter |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame^] | 315 | 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 Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | }; // namespace android |
| 332 | |
| 333 | #endif /*ANDROID_AUDIO_RESAMPLER_FIR_PROCESS_H*/ |