The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | #include <string.h> |
| 18 | #include "AudioResamplerSinc.h" |
| 19 | |
| 20 | namespace android { |
| 21 | // ---------------------------------------------------------------------------- |
| 22 | |
| 23 | |
| 24 | /* |
| 25 | * These coeficients are computed with the "fir" utility found in |
| 26 | * tools/resampler_tools |
| 27 | * TODO: A good optimization would be to transpose this matrix, to take |
| 28 | * better advantage of the data-cache. |
| 29 | */ |
| 30 | const int32_t AudioResamplerSinc::mFirCoefsUp[] = { |
| 31 | 0x7fffffff, 0x7f15d078, 0x7c5e0da6, 0x77ecd867, 0x71e2e251, 0x6a6c304a, 0x61be7269, 0x58170412, 0x4db8ab05, 0x42e92ea6, 0x37eee214, 0x2d0e3bb1, 0x22879366, 0x18951e95, 0x0f693d0d, 0x072d2621, |
| 32 | 0x00000000, 0xf9f66655, 0xf51a5fd7, 0xf16bbd84, 0xeee0d9ac, 0xed67a922, 0xece70de6, 0xed405897, 0xee50e505, 0xeff3be30, 0xf203370f, 0xf45a6741, 0xf6d67d53, 0xf957db66, 0xfbc2f647, 0xfe00f2b9, |
| 33 | 0x00000000, 0x01b37218, 0x0313a0c6, 0x041d930d, 0x04d28057, 0x053731b0, 0x05534dff, 0x05309bfd, 0x04da440d, 0x045c1aee, 0x03c1fcdd, 0x03173ef5, 0x02663ae8, 0x01b7f736, 0x0113ec79, 0x007fe6a9, |
| 34 | 0x00000000, 0xff96b229, 0xff44f99f, 0xff0a86be, 0xfee5f803, 0xfed518fd, 0xfed521fd, 0xfee2f4fd, 0xfefb54f8, 0xff1b159b, 0xff3f4203, 0xff6539e0, 0xff8ac502, 0xffae1ddd, 0xffcdf3f9, 0xffe96798, |
| 35 | 0x00000000, 0x00119de6, 0x001e6b7e, 0x0026cb7a, 0x002b4830, 0x002c83d6, 0x002b2a82, 0x0027e67a, 0x002356f9, 0x001e098e, 0x001875e4, 0x0012fbbe, 0x000de2d1, 0x00095c10, 0x00058414, 0x00026636, |
| 36 | 0x00000000, 0xfffe44a9, 0xfffd206d, 0xfffc7b7f, 0xfffc3c8f, 0xfffc4ac2, 0xfffc8f2b, 0xfffcf5c4, 0xfffd6df3, 0xfffdeab2, 0xfffe6275, 0xfffececf, 0xffff2c07, 0xffff788c, 0xffffb471, 0xffffe0f2, |
| 37 | 0x00000000, 0x000013e6, 0x00001f03, 0x00002396, 0x00002399, 0x000020b6, 0x00001c3c, 0x00001722, 0x00001216, 0x00000d81, 0x0000099c, 0x0000067c, 0x00000419, 0x0000025f, 0x00000131, 0x00000070, |
| 38 | 0x00000000, 0xffffffc7, 0xffffffb3, 0xffffffb3, 0xffffffbe, 0xffffffcd, 0xffffffdb, 0xffffffe7, 0xfffffff0, 0xfffffff7, 0xfffffffb, 0xfffffffe, 0xffffffff, 0x00000000, 0x00000000, 0x00000000, |
| 39 | 0x00000000 // this one is needed for lerping the last coefficient |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * These coefficients are optimized for 48KHz -> 44.1KHz (stop-band at 22.050KHz) |
| 44 | * It's possible to use the above coefficient for any down-sampling |
| 45 | * at the expense of a slower processing loop (we can interpolate |
| 46 | * these coefficient from the above by "Stretching" them in time). |
| 47 | */ |
| 48 | const int32_t AudioResamplerSinc::mFirCoefsDown[] = { |
| 49 | 0x7fffffff, 0x7f55e46d, 0x7d5b4c60, 0x7a1b4b98, 0x75a7fb14, 0x7019f0bd, 0x698f875a, 0x622bfd59, 0x5a167256, 0x5178cc54, 0x487e8e6c, 0x3f53aae8, 0x36235ad4, 0x2d17047b, 0x245539ab, 0x1c00d540, |
| 50 | 0x14383e57, 0x0d14d5ca, 0x06aa910b, 0x0107c38b, 0xfc351654, 0xf835abae, 0xf5076b45, 0xf2a37202, 0xf0fe9faa, 0xf00a3bbd, 0xefb4aa81, 0xefea2b05, 0xf0959716, 0xf1a11e83, 0xf2f6f7a0, 0xf481fff4, |
| 51 | 0xf62e48ce, 0xf7e98ca5, 0xf9a38b4c, 0xfb4e4bfa, 0xfcde456f, 0xfe4a6d30, 0xff8c2fdf, 0x009f5555, 0x0181d393, 0x0233940f, 0x02b62f06, 0x030ca07d, 0x033afa62, 0x03461725, 0x03334f83, 0x030835fa, |
| 52 | 0x02ca59cc, 0x027f12d1, 0x022b570d, 0x01d39a49, 0x017bb78f, 0x0126e414, 0x00d7aaaf, 0x008feec7, 0x0050f584, 0x001b73e3, 0xffefa063, 0xffcd46ed, 0xffb3ddcd, 0xffa29aaa, 0xff988691, 0xff949066, |
| 53 | 0xff959d24, 0xff9a959e, 0xffa27195, 0xffac4011, 0xffb72d2b, 0xffc28569, 0xffcdb706, 0xffd85171, 0xffe20364, 0xffea97e9, 0xfff1f2b2, 0xfff80c06, 0xfffcec92, 0x0000a955, 0x00035fd8, 0x000532cf, |
| 54 | 0x00064735, 0x0006c1f9, 0x0006c62d, 0x000673ba, 0x0005e68f, 0x00053630, 0x000475a3, 0x0003b397, 0x0002fac1, 0x00025257, 0x0001be9e, 0x0001417a, 0x0000dafd, 0x000089eb, 0x00004c28, 0x00001f1d, |
| 55 | 0x00000000, 0xffffec10, 0xffffe0be, 0xffffdbc5, 0xffffdb39, 0xffffdd8b, 0xffffe182, 0xffffe638, 0xffffeb0a, 0xffffef8f, 0xfffff38b, 0xfffff6e3, 0xfffff993, 0xfffffba6, 0xfffffd30, 0xfffffe4a, |
| 56 | 0xffffff09, 0xffffff85, 0xffffffd1, 0xfffffffb, 0x0000000f, 0x00000016, 0x00000015, 0x00000012, 0x0000000d, 0x00000009, 0x00000006, 0x00000003, 0x00000002, 0x00000001, 0x00000000, 0x00000000, |
| 57 | 0x00000000 // this one is needed for lerping the last coefficient |
| 58 | }; |
| 59 | |
| 60 | // ---------------------------------------------------------------------------- |
| 61 | |
| 62 | static inline |
| 63 | int32_t mulRL(int left, int32_t in, uint32_t vRL) |
| 64 | { |
| 65 | #if defined(__arm__) && !defined(__thumb__) |
| 66 | int32_t out; |
| 67 | if (left) { |
| 68 | asm( "smultb %[out], %[in], %[vRL] \n" |
| 69 | : [out]"=r"(out) |
| 70 | : [in]"%r"(in), [vRL]"r"(vRL) |
| 71 | : ); |
| 72 | } else { |
| 73 | asm( "smultt %[out], %[in], %[vRL] \n" |
| 74 | : [out]"=r"(out) |
| 75 | : [in]"%r"(in), [vRL]"r"(vRL) |
| 76 | : ); |
| 77 | } |
| 78 | return out; |
| 79 | #else |
| 80 | if (left) { |
| 81 | return int16_t(in>>16) * int16_t(vRL&0xFFFF); |
| 82 | } else { |
| 83 | return int16_t(in>>16) * int16_t(vRL>>16); |
| 84 | } |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | static inline |
| 89 | int32_t mulAdd(int16_t in, int32_t v, int32_t a) |
| 90 | { |
| 91 | #if defined(__arm__) && !defined(__thumb__) |
| 92 | int32_t out; |
| 93 | asm( "smlawb %[out], %[v], %[in], %[a] \n" |
| 94 | : [out]"=r"(out) |
| 95 | : [in]"%r"(in), [v]"r"(v), [a]"r"(a) |
| 96 | : ); |
| 97 | return out; |
| 98 | #else |
| 99 | return a + ((in * int32_t(v))>>16); |
| 100 | #endif |
| 101 | } |
| 102 | |
| 103 | static inline |
| 104 | int32_t mulAddRL(int left, uint32_t inRL, int32_t v, int32_t a) |
| 105 | { |
| 106 | #if defined(__arm__) && !defined(__thumb__) |
| 107 | int32_t out; |
| 108 | if (left) { |
| 109 | asm( "smlawb %[out], %[v], %[inRL], %[a] \n" |
| 110 | : [out]"=r"(out) |
| 111 | : [inRL]"%r"(inRL), [v]"r"(v), [a]"r"(a) |
| 112 | : ); |
| 113 | } else { |
| 114 | asm( "smlawt %[out], %[v], %[inRL], %[a] \n" |
| 115 | : [out]"=r"(out) |
| 116 | : [inRL]"%r"(inRL), [v]"r"(v), [a]"r"(a) |
| 117 | : ); |
| 118 | } |
| 119 | return out; |
| 120 | #else |
| 121 | if (left) { |
| 122 | return a + ((int16_t(inRL&0xFFFF) * int32_t(v))>>16); |
| 123 | } else { |
| 124 | return a + ((int16_t(inRL>>16) * int32_t(v))>>16); |
| 125 | } |
| 126 | #endif |
| 127 | } |
| 128 | |
| 129 | // ---------------------------------------------------------------------------- |
| 130 | |
| 131 | AudioResamplerSinc::AudioResamplerSinc(int bitDepth, |
| 132 | int inChannelCount, int32_t sampleRate) |
| 133 | : AudioResampler(bitDepth, inChannelCount, sampleRate), |
| 134 | mState(0) |
| 135 | { |
| 136 | /* |
| 137 | * Layout of the state buffer for 32 tap: |
| 138 | * |
| 139 | * "present" sample beginning of 2nd buffer |
| 140 | * v v |
| 141 | * 0 01 2 23 3 |
| 142 | * 0 F0 0 F0 F |
| 143 | * [pppppppppppppppInnnnnnnnnnnnnnnnpppppppppppppppInnnnnnnnnnnnnnnn] |
| 144 | * ^ ^ head |
| 145 | * |
| 146 | * p = past samples, convoluted with the (p)ositive side of sinc() |
| 147 | * n = future samples, convoluted with the (n)egative side of sinc() |
| 148 | * r = extra space for implementing the ring buffer |
| 149 | * |
| 150 | */ |
| 151 | |
| 152 | const size_t numCoefs = 2*halfNumCoefs; |
| 153 | const size_t stateSize = numCoefs * inChannelCount * 2; |
| 154 | mState = new int16_t[stateSize]; |
| 155 | memset(mState, 0, sizeof(int16_t)*stateSize); |
| 156 | mImpulse = mState + (halfNumCoefs-1)*inChannelCount; |
| 157 | mRingFull = mImpulse + (numCoefs+1)*inChannelCount; |
| 158 | } |
| 159 | |
| 160 | AudioResamplerSinc::~AudioResamplerSinc() |
| 161 | { |
| 162 | delete [] mState; |
| 163 | } |
| 164 | |
| 165 | void AudioResamplerSinc::init() { |
| 166 | } |
| 167 | |
| 168 | void AudioResamplerSinc::resample(int32_t* out, size_t outFrameCount, |
| 169 | AudioBufferProvider* provider) |
| 170 | { |
| 171 | mFirCoefs = (mInSampleRate <= mSampleRate) ? mFirCoefsUp : mFirCoefsDown; |
| 172 | |
| 173 | // select the appropriate resampler |
| 174 | switch (mChannelCount) { |
| 175 | case 1: |
| 176 | resample<1>(out, outFrameCount, provider); |
| 177 | break; |
| 178 | case 2: |
| 179 | resample<2>(out, outFrameCount, provider); |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | |
| 185 | template<int CHANNELS> |
| 186 | void AudioResamplerSinc::resample(int32_t* out, size_t outFrameCount, |
| 187 | AudioBufferProvider* provider) |
| 188 | { |
| 189 | int16_t* impulse = mImpulse; |
| 190 | uint32_t vRL = mVolumeRL; |
| 191 | size_t inputIndex = mInputIndex; |
| 192 | uint32_t phaseFraction = mPhaseFraction; |
| 193 | uint32_t phaseIncrement = mPhaseIncrement; |
| 194 | size_t outputIndex = 0; |
| 195 | size_t outputSampleCount = outFrameCount * 2; |
| 196 | |
| 197 | AudioBufferProvider::Buffer& buffer(mBuffer); |
| 198 | while (outputIndex < outputSampleCount) { |
| 199 | // buffer is empty, fetch a new one |
| 200 | if (buffer.raw == NULL) { |
| 201 | provider->getNextBuffer(&buffer); |
| 202 | if (buffer.raw == NULL) |
| 203 | break; |
| 204 | const uint32_t phaseIndex = phaseFraction >> kNumPhaseBits; |
| 205 | if (phaseIndex) { |
| 206 | read<CHANNELS>(impulse, phaseFraction, buffer.i16, inputIndex); |
| 207 | } |
| 208 | } |
| 209 | int16_t *in = buffer.i16; |
| 210 | const size_t frameCount = buffer.frameCount; |
| 211 | |
| 212 | // Always read-in the first samples from the input buffer |
| 213 | int16_t* head = impulse + halfNumCoefs*CHANNELS; |
| 214 | head[0] = in[inputIndex*CHANNELS + 0]; |
| 215 | if (CHANNELS == 2) |
| 216 | head[1] = in[inputIndex*CHANNELS + 1]; |
| 217 | |
| 218 | // handle boundary case |
| 219 | int32_t l, r; |
| 220 | while (outputIndex < outputSampleCount) { |
| 221 | filterCoefficient<CHANNELS>(l, r, phaseFraction, impulse); |
| 222 | out[outputIndex++] = mulRL(1, l, vRL); |
| 223 | out[outputIndex++] = mulRL(0, r, vRL); |
| 224 | |
| 225 | phaseFraction += phaseIncrement; |
| 226 | const uint32_t phaseIndex = phaseFraction >> kNumPhaseBits; |
| 227 | if (phaseIndex) { |
| 228 | inputIndex += phaseIndex; |
| 229 | if (inputIndex >= frameCount) |
| 230 | break; |
| 231 | read<CHANNELS>(impulse, phaseFraction, in, inputIndex); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // if done with buffer, save samples |
| 236 | if (inputIndex >= frameCount) { |
| 237 | inputIndex -= frameCount; |
| 238 | provider->releaseBuffer(&buffer); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | mImpulse = impulse; |
| 243 | mInputIndex = inputIndex; |
| 244 | mPhaseFraction = phaseFraction; |
| 245 | } |
| 246 | |
| 247 | template<int CHANNELS> |
| 248 | void AudioResamplerSinc::read( |
| 249 | int16_t*& impulse, uint32_t& phaseFraction, |
| 250 | int16_t const* in, size_t inputIndex) |
| 251 | { |
| 252 | // read new samples into the ring buffer |
| 253 | while (phaseFraction >> kNumPhaseBits) { |
| 254 | const uint32_t phaseIndex = phaseFraction >> kNumPhaseBits; |
| 255 | impulse += CHANNELS; |
| 256 | phaseFraction -= 1LU<<kNumPhaseBits; |
| 257 | if (impulse >= mRingFull) { |
| 258 | const size_t stateSize = (halfNumCoefs*2)*CHANNELS; |
| 259 | memcpy(mState, mState+stateSize, sizeof(int16_t)*stateSize); |
| 260 | impulse -= stateSize; |
| 261 | } |
| 262 | int16_t* head = impulse + halfNumCoefs*CHANNELS; |
| 263 | head[0] = in[inputIndex*CHANNELS + 0]; |
| 264 | if (CHANNELS == 2) |
| 265 | head[1] = in[inputIndex*CHANNELS + 1]; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | template<int CHANNELS> |
| 270 | void AudioResamplerSinc::filterCoefficient( |
| 271 | int32_t& l, int32_t& r, uint32_t phase, int16_t const *samples) |
| 272 | { |
| 273 | // compute the index of the coefficient on the positive side and |
| 274 | // negative side |
| 275 | uint32_t indexP = (phase & cMask) >> cShift; |
| 276 | uint16_t lerpP = (phase & pMask) >> pShift; |
| 277 | uint32_t indexN = (-phase & cMask) >> cShift; |
| 278 | uint16_t lerpN = (-phase & pMask) >> pShift; |
| 279 | |
| 280 | l = 0; |
| 281 | r = 0; |
| 282 | int32_t const* coefs = mFirCoefs; |
| 283 | int16_t const *sP = samples; |
| 284 | int16_t const *sN = samples+CHANNELS; |
| 285 | for (unsigned int i=0 ; i<halfNumCoefs/4 ; i++) { |
| 286 | interpolate<CHANNELS>(l, r, coefs+indexP, lerpP, sP); |
| 287 | interpolate<CHANNELS>(l, r, coefs+indexN, lerpN, sN); |
| 288 | sP -= CHANNELS; sN += CHANNELS; coefs += 1<<coefsBits; |
| 289 | interpolate<CHANNELS>(l, r, coefs+indexP, lerpP, sP); |
| 290 | interpolate<CHANNELS>(l, r, coefs+indexN, lerpN, sN); |
| 291 | sP -= CHANNELS; sN += CHANNELS; coefs += 1<<coefsBits; |
| 292 | interpolate<CHANNELS>(l, r, coefs+indexP, lerpP, sP); |
| 293 | interpolate<CHANNELS>(l, r, coefs+indexN, lerpN, sN); |
| 294 | sP -= CHANNELS; sN += CHANNELS; coefs += 1<<coefsBits; |
| 295 | interpolate<CHANNELS>(l, r, coefs+indexP, lerpP, sP); |
| 296 | interpolate<CHANNELS>(l, r, coefs+indexN, lerpN, sN); |
| 297 | sP -= CHANNELS; sN += CHANNELS; coefs += 1<<coefsBits; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | template<int CHANNELS> |
| 302 | void AudioResamplerSinc::interpolate( |
| 303 | int32_t& l, int32_t& r, |
| 304 | int32_t const* coefs, int16_t lerp, int16_t const* samples) |
| 305 | { |
| 306 | int32_t c0 = coefs[0]; |
| 307 | int32_t c1 = coefs[1]; |
| 308 | int32_t sinc = mulAdd(lerp, (c1-c0)<<1, c0); |
| 309 | if (CHANNELS == 2) { |
| 310 | uint32_t rl = *reinterpret_cast<uint32_t const*>(samples); |
| 311 | l = mulAddRL(1, rl, sinc, l); |
| 312 | r = mulAddRL(0, rl, sinc, r); |
| 313 | } else { |
| 314 | r = l = mulAdd(samples[0], sinc, l); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // ---------------------------------------------------------------------------- |
| 319 | }; // namespace android |
| 320 | |