| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -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 | #define LOG_TAG "AudioResampler" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <cutils/log.h> |
| 24 | #include <cutils/properties.h> |
| 25 | #include "AudioResampler.h" |
| 26 | #include "AudioResamplerSinc.h" |
| 27 | #include "AudioResamplerCubic.h" |
| 28 | |
| Jim Huang | 0c0a1c0 | 2011-04-06 14:19:29 +0800 | [diff] [blame] | 29 | #ifdef __arm__ |
| 30 | #include <machine/cpu-features.h> |
| 31 | #endif |
| 32 | |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | |
| Jim Huang | 0c0a1c0 | 2011-04-06 14:19:29 +0800 | [diff] [blame] | 35 | #ifdef __ARM_HAVE_HALFWORD_MULTIPLY // optimized asm option |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 36 | #define ASM_ARM_RESAMP1 // enable asm optimisation for ResamplerOrder1 |
| Jim Huang | 0c0a1c0 | 2011-04-06 14:19:29 +0800 | [diff] [blame] | 37 | #endif // __ARM_HAVE_HALFWORD_MULTIPLY |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 38 | // ---------------------------------------------------------------------------- |
| 39 | |
| 40 | class AudioResamplerOrder1 : public AudioResampler { |
| 41 | public: |
| 42 | AudioResamplerOrder1(int bitDepth, int inChannelCount, int32_t sampleRate) : |
| 43 | AudioResampler(bitDepth, inChannelCount, sampleRate), mX0L(0), mX0R(0) { |
| 44 | } |
| 45 | virtual void resample(int32_t* out, size_t outFrameCount, |
| 46 | AudioBufferProvider* provider); |
| 47 | private: |
| 48 | // number of bits used in interpolation multiply - 15 bits avoids overflow |
| 49 | static const int kNumInterpBits = 15; |
| 50 | |
| 51 | // bits to shift the phase fraction down to avoid overflow |
| 52 | static const int kPreInterpShift = kNumPhaseBits - kNumInterpBits; |
| 53 | |
| 54 | void init() {} |
| 55 | void resampleMono16(int32_t* out, size_t outFrameCount, |
| 56 | AudioBufferProvider* provider); |
| 57 | void resampleStereo16(int32_t* out, size_t outFrameCount, |
| 58 | AudioBufferProvider* provider); |
| 59 | #ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1 |
| 60 | void AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx, |
| 61 | size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr, |
| Jason Simmons | 6fe5bd8 | 2011-10-27 16:38:29 -0700 | [diff] [blame] | 62 | uint32_t &phaseFraction, uint32_t phaseIncrement) |
| 63 | __attribute__((noinline)); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 64 | void AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx, |
| 65 | size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr, |
| Jason Simmons | 6fe5bd8 | 2011-10-27 16:38:29 -0700 | [diff] [blame] | 66 | uint32_t &phaseFraction, uint32_t phaseIncrement) |
| 67 | __attribute__((noinline)); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 68 | #endif // ASM_ARM_RESAMP1 |
| 69 | |
| 70 | static inline int32_t Interp(int32_t x0, int32_t x1, uint32_t f) { |
| 71 | return x0 + (((x1 - x0) * (int32_t)(f >> kPreInterpShift)) >> kNumInterpBits); |
| 72 | } |
| 73 | static inline void Advance(size_t* index, uint32_t* frac, uint32_t inc) { |
| 74 | *frac += inc; |
| 75 | *index += (size_t)(*frac >> kNumPhaseBits); |
| 76 | *frac &= kPhaseMask; |
| 77 | } |
| 78 | int mX0L; |
| 79 | int mX0R; |
| 80 | }; |
| 81 | |
| 82 | // ---------------------------------------------------------------------------- |
| 83 | AudioResampler* AudioResampler::create(int bitDepth, int inChannelCount, |
| 84 | int32_t sampleRate, int quality) { |
| 85 | |
| 86 | // can only create low quality resample now |
| 87 | AudioResampler* resampler; |
| 88 | |
| 89 | char value[PROPERTY_VALUE_MAX]; |
| 90 | if (property_get("af.resampler.quality", value, 0)) { |
| 91 | quality = atoi(value); |
| 92 | LOGD("forcing AudioResampler quality to %d", quality); |
| 93 | } |
| 94 | |
| 95 | if (quality == DEFAULT) |
| 96 | quality = LOW_QUALITY; |
| 97 | |
| 98 | switch (quality) { |
| 99 | default: |
| 100 | case LOW_QUALITY: |
| 101 | LOGV("Create linear Resampler"); |
| 102 | resampler = new AudioResamplerOrder1(bitDepth, inChannelCount, sampleRate); |
| 103 | break; |
| 104 | case MED_QUALITY: |
| 105 | LOGV("Create cubic Resampler"); |
| 106 | resampler = new AudioResamplerCubic(bitDepth, inChannelCount, sampleRate); |
| 107 | break; |
| 108 | case HIGH_QUALITY: |
| 109 | LOGV("Create sinc Resampler"); |
| 110 | resampler = new AudioResamplerSinc(bitDepth, inChannelCount, sampleRate); |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | // initialize resampler |
| 115 | resampler->init(); |
| 116 | return resampler; |
| 117 | } |
| 118 | |
| 119 | AudioResampler::AudioResampler(int bitDepth, int inChannelCount, |
| 120 | int32_t sampleRate) : |
| 121 | mBitDepth(bitDepth), mChannelCount(inChannelCount), |
| 122 | mSampleRate(sampleRate), mInSampleRate(sampleRate), mInputIndex(0), |
| Mike J. Chen | 500e510 | 2011-08-15 13:28:26 -0700 | [diff] [blame] | 123 | mPhaseFraction(0), mLocalTimeFreq(0), |
| 124 | mPTS(AudioBufferProvider::kInvalidPTS) { |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 125 | // sanity check on format |
| 126 | if ((bitDepth != 16) ||(inChannelCount < 1) || (inChannelCount > 2)) { |
| 127 | LOGE("Unsupported sample format, %d bits, %d channels", bitDepth, |
| 128 | inChannelCount); |
| 129 | // LOG_ASSERT(0); |
| 130 | } |
| 131 | |
| 132 | // initialize common members |
| 133 | mVolume[0] = mVolume[1] = 0; |
| 134 | mBuffer.frameCount = 0; |
| 135 | |
| 136 | // save format for quick lookup |
| 137 | if (inChannelCount == 1) { |
| 138 | mFormat = MONO_16_BIT; |
| 139 | } else { |
| 140 | mFormat = STEREO_16_BIT; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | AudioResampler::~AudioResampler() { |
| 145 | } |
| 146 | |
| 147 | void AudioResampler::setSampleRate(int32_t inSampleRate) { |
| 148 | mInSampleRate = inSampleRate; |
| 149 | mPhaseIncrement = (uint32_t)((kPhaseMultiplier * inSampleRate) / mSampleRate); |
| 150 | } |
| 151 | |
| 152 | void AudioResampler::setVolume(int16_t left, int16_t right) { |
| 153 | // TODO: Implement anti-zipper filter |
| 154 | mVolume[0] = left; |
| 155 | mVolume[1] = right; |
| 156 | } |
| 157 | |
| Mike J. Chen | 500e510 | 2011-08-15 13:28:26 -0700 | [diff] [blame] | 158 | void AudioResampler::setLocalTimeFreq(uint64_t freq) { |
| 159 | mLocalTimeFreq = freq; |
| 160 | } |
| 161 | |
| 162 | void AudioResampler::setPTS(int64_t pts) { |
| 163 | mPTS = pts; |
| 164 | } |
| 165 | |
| 166 | int64_t AudioResampler::calculateOutputPTS(int outputFrameIndex) { |
| 167 | |
| 168 | if (mPTS == AudioBufferProvider::kInvalidPTS) { |
| 169 | return AudioBufferProvider::kInvalidPTS; |
| 170 | } else { |
| 171 | return mPTS + ((outputFrameIndex * mLocalTimeFreq) / mSampleRate); |
| 172 | } |
| 173 | } |
| 174 | |
| Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 175 | void AudioResampler::reset() { |
| 176 | mInputIndex = 0; |
| 177 | mPhaseFraction = 0; |
| 178 | mBuffer.frameCount = 0; |
| 179 | } |
| 180 | |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 181 | // ---------------------------------------------------------------------------- |
| 182 | |
| 183 | void AudioResamplerOrder1::resample(int32_t* out, size_t outFrameCount, |
| 184 | AudioBufferProvider* provider) { |
| 185 | |
| 186 | // should never happen, but we overflow if it does |
| 187 | // LOG_ASSERT(outFrameCount < 32767); |
| 188 | |
| 189 | // select the appropriate resampler |
| 190 | switch (mChannelCount) { |
| 191 | case 1: |
| 192 | resampleMono16(out, outFrameCount, provider); |
| 193 | break; |
| 194 | case 2: |
| 195 | resampleStereo16(out, outFrameCount, provider); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void AudioResamplerOrder1::resampleStereo16(int32_t* out, size_t outFrameCount, |
| 201 | AudioBufferProvider* provider) { |
| 202 | |
| 203 | int32_t vl = mVolume[0]; |
| 204 | int32_t vr = mVolume[1]; |
| 205 | |
| 206 | size_t inputIndex = mInputIndex; |
| 207 | uint32_t phaseFraction = mPhaseFraction; |
| 208 | uint32_t phaseIncrement = mPhaseIncrement; |
| 209 | size_t outputIndex = 0; |
| 210 | size_t outputSampleCount = outFrameCount * 2; |
| 211 | size_t inFrameCount = (outFrameCount*mInSampleRate)/mSampleRate; |
| 212 | |
| 213 | // LOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d\n", |
| 214 | // outFrameCount, inputIndex, phaseFraction, phaseIncrement); |
| 215 | |
| 216 | while (outputIndex < outputSampleCount) { |
| 217 | |
| 218 | // buffer is empty, fetch a new one |
| 219 | while (mBuffer.frameCount == 0) { |
| 220 | mBuffer.frameCount = inFrameCount; |
| Mike J. Chen | 500e510 | 2011-08-15 13:28:26 -0700 | [diff] [blame] | 221 | provider->getNextBuffer(&mBuffer, |
| 222 | calculateOutputPTS(outputIndex / 2)); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 223 | if (mBuffer.raw == NULL) { |
| 224 | goto resampleStereo16_exit; |
| 225 | } |
| 226 | |
| 227 | // LOGE("New buffer fetched: %d frames\n", mBuffer.frameCount); |
| 228 | if (mBuffer.frameCount > inputIndex) break; |
| 229 | |
| 230 | inputIndex -= mBuffer.frameCount; |
| 231 | mX0L = mBuffer.i16[mBuffer.frameCount*2-2]; |
| 232 | mX0R = mBuffer.i16[mBuffer.frameCount*2-1]; |
| 233 | provider->releaseBuffer(&mBuffer); |
| 234 | // mBuffer.frameCount == 0 now so we reload a new buffer |
| 235 | } |
| 236 | |
| 237 | int16_t *in = mBuffer.i16; |
| 238 | |
| 239 | // handle boundary case |
| 240 | while (inputIndex == 0) { |
| 241 | // LOGE("boundary case\n"); |
| 242 | out[outputIndex++] += vl * Interp(mX0L, in[0], phaseFraction); |
| 243 | out[outputIndex++] += vr * Interp(mX0R, in[1], phaseFraction); |
| 244 | Advance(&inputIndex, &phaseFraction, phaseIncrement); |
| 245 | if (outputIndex == outputSampleCount) |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | // process input samples |
| 250 | // LOGE("general case\n"); |
| 251 | |
| 252 | #ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1 |
| 253 | if (inputIndex + 2 < mBuffer.frameCount) { |
| 254 | int32_t* maxOutPt; |
| 255 | int32_t maxInIdx; |
| 256 | |
| 257 | maxOutPt = out + (outputSampleCount - 2); // 2 because 2 frames per loop |
| 258 | maxInIdx = mBuffer.frameCount - 2; |
| 259 | AsmStereo16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr, |
| 260 | phaseFraction, phaseIncrement); |
| 261 | } |
| 262 | #endif // ASM_ARM_RESAMP1 |
| 263 | |
| 264 | while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) { |
| 265 | out[outputIndex++] += vl * Interp(in[inputIndex*2-2], |
| 266 | in[inputIndex*2], phaseFraction); |
| 267 | out[outputIndex++] += vr * Interp(in[inputIndex*2-1], |
| 268 | in[inputIndex*2+1], phaseFraction); |
| 269 | Advance(&inputIndex, &phaseFraction, phaseIncrement); |
| 270 | } |
| 271 | |
| 272 | // LOGE("loop done - outputIndex=%d, inputIndex=%d\n", outputIndex, inputIndex); |
| 273 | |
| 274 | // if done with buffer, save samples |
| 275 | if (inputIndex >= mBuffer.frameCount) { |
| 276 | inputIndex -= mBuffer.frameCount; |
| 277 | |
| 278 | // LOGE("buffer done, new input index %d", inputIndex); |
| 279 | |
| 280 | mX0L = mBuffer.i16[mBuffer.frameCount*2-2]; |
| 281 | mX0R = mBuffer.i16[mBuffer.frameCount*2-1]; |
| 282 | provider->releaseBuffer(&mBuffer); |
| 283 | |
| 284 | // verify that the releaseBuffer resets the buffer frameCount |
| 285 | // LOG_ASSERT(mBuffer.frameCount == 0); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // LOGE("output buffer full - outputIndex=%d, inputIndex=%d\n", outputIndex, inputIndex); |
| 290 | |
| 291 | resampleStereo16_exit: |
| 292 | // save state |
| 293 | mInputIndex = inputIndex; |
| 294 | mPhaseFraction = phaseFraction; |
| 295 | } |
| 296 | |
| 297 | void AudioResamplerOrder1::resampleMono16(int32_t* out, size_t outFrameCount, |
| 298 | AudioBufferProvider* provider) { |
| 299 | |
| 300 | int32_t vl = mVolume[0]; |
| 301 | int32_t vr = mVolume[1]; |
| 302 | |
| 303 | size_t inputIndex = mInputIndex; |
| 304 | uint32_t phaseFraction = mPhaseFraction; |
| 305 | uint32_t phaseIncrement = mPhaseIncrement; |
| 306 | size_t outputIndex = 0; |
| 307 | size_t outputSampleCount = outFrameCount * 2; |
| 308 | size_t inFrameCount = (outFrameCount*mInSampleRate)/mSampleRate; |
| 309 | |
| 310 | // LOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d\n", |
| 311 | // outFrameCount, inputIndex, phaseFraction, phaseIncrement); |
| 312 | while (outputIndex < outputSampleCount) { |
| 313 | // buffer is empty, fetch a new one |
| 314 | while (mBuffer.frameCount == 0) { |
| 315 | mBuffer.frameCount = inFrameCount; |
| Mike J. Chen | 500e510 | 2011-08-15 13:28:26 -0700 | [diff] [blame] | 316 | provider->getNextBuffer(&mBuffer, |
| 317 | calculateOutputPTS(outputIndex / 2)); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 318 | if (mBuffer.raw == NULL) { |
| 319 | mInputIndex = inputIndex; |
| 320 | mPhaseFraction = phaseFraction; |
| 321 | goto resampleMono16_exit; |
| 322 | } |
| 323 | // LOGE("New buffer fetched: %d frames\n", mBuffer.frameCount); |
| 324 | if (mBuffer.frameCount > inputIndex) break; |
| 325 | |
| 326 | inputIndex -= mBuffer.frameCount; |
| 327 | mX0L = mBuffer.i16[mBuffer.frameCount-1]; |
| 328 | provider->releaseBuffer(&mBuffer); |
| 329 | // mBuffer.frameCount == 0 now so we reload a new buffer |
| 330 | } |
| 331 | int16_t *in = mBuffer.i16; |
| 332 | |
| 333 | // handle boundary case |
| 334 | while (inputIndex == 0) { |
| 335 | // LOGE("boundary case\n"); |
| 336 | int32_t sample = Interp(mX0L, in[0], phaseFraction); |
| 337 | out[outputIndex++] += vl * sample; |
| 338 | out[outputIndex++] += vr * sample; |
| 339 | Advance(&inputIndex, &phaseFraction, phaseIncrement); |
| 340 | if (outputIndex == outputSampleCount) |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | // process input samples |
| 345 | // LOGE("general case\n"); |
| 346 | |
| 347 | #ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1 |
| 348 | if (inputIndex + 2 < mBuffer.frameCount) { |
| 349 | int32_t* maxOutPt; |
| 350 | int32_t maxInIdx; |
| 351 | |
| 352 | maxOutPt = out + (outputSampleCount - 2); |
| 353 | maxInIdx = (int32_t)mBuffer.frameCount - 2; |
| 354 | AsmMono16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr, |
| 355 | phaseFraction, phaseIncrement); |
| 356 | } |
| 357 | #endif // ASM_ARM_RESAMP1 |
| 358 | |
| 359 | while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) { |
| 360 | int32_t sample = Interp(in[inputIndex-1], in[inputIndex], |
| 361 | phaseFraction); |
| 362 | out[outputIndex++] += vl * sample; |
| 363 | out[outputIndex++] += vr * sample; |
| 364 | Advance(&inputIndex, &phaseFraction, phaseIncrement); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | // LOGE("loop done - outputIndex=%d, inputIndex=%d\n", outputIndex, inputIndex); |
| 369 | |
| 370 | // if done with buffer, save samples |
| 371 | if (inputIndex >= mBuffer.frameCount) { |
| 372 | inputIndex -= mBuffer.frameCount; |
| 373 | |
| 374 | // LOGE("buffer done, new input index %d", inputIndex); |
| 375 | |
| 376 | mX0L = mBuffer.i16[mBuffer.frameCount-1]; |
| 377 | provider->releaseBuffer(&mBuffer); |
| 378 | |
| 379 | // verify that the releaseBuffer resets the buffer frameCount |
| 380 | // LOG_ASSERT(mBuffer.frameCount == 0); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // LOGE("output buffer full - outputIndex=%d, inputIndex=%d\n", outputIndex, inputIndex); |
| 385 | |
| 386 | resampleMono16_exit: |
| 387 | // save state |
| 388 | mInputIndex = inputIndex; |
| 389 | mPhaseFraction = phaseFraction; |
| 390 | } |
| 391 | |
| 392 | #ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1 |
| 393 | |
| 394 | /******************************************************************* |
| 395 | * |
| 396 | * AsmMono16Loop |
| 397 | * asm optimized monotonic loop version; one loop is 2 frames |
| 398 | * Input: |
| 399 | * in : pointer on input samples |
| 400 | * maxOutPt : pointer on first not filled |
| 401 | * maxInIdx : index on first not used |
| 402 | * outputIndex : pointer on current output index |
| 403 | * out : pointer on output buffer |
| 404 | * inputIndex : pointer on current input index |
| 405 | * vl, vr : left and right gain |
| 406 | * phaseFraction : pointer on current phase fraction |
| 407 | * phaseIncrement |
| 408 | * Ouput: |
| 409 | * outputIndex : |
| 410 | * out : updated buffer |
| 411 | * inputIndex : index of next to use |
| 412 | * phaseFraction : phase fraction for next interpolation |
| 413 | * |
| 414 | *******************************************************************/ |
| 415 | void AudioResamplerOrder1::AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx, |
| 416 | size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr, |
| 417 | uint32_t &phaseFraction, uint32_t phaseIncrement) |
| 418 | { |
| 419 | #define MO_PARAM5 "36" // offset of parameter 5 (outputIndex) |
| 420 | |
| 421 | asm( |
| 422 | "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n" |
| 423 | // get parameters |
| 424 | " ldr r6, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction |
| 425 | " ldr r6, [r6]\n" // phaseFraction |
| 426 | " ldr r7, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex |
| 427 | " ldr r7, [r7]\n" // inputIndex |
| 428 | " ldr r8, [sp, #" MO_PARAM5 " + 4]\n" // out |
| 429 | " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex |
| 430 | " ldr r0, [r0]\n" // outputIndex |
| 431 | " add r8, r0, asl #2\n" // curOut |
| 432 | " ldr r9, [sp, #" MO_PARAM5 " + 24]\n" // phaseIncrement |
| 433 | " ldr r10, [sp, #" MO_PARAM5 " + 12]\n" // vl |
| 434 | " ldr r11, [sp, #" MO_PARAM5 " + 16]\n" // vr |
| 435 | |
| 436 | // r0 pin, x0, Samp |
| 437 | |
| 438 | // r1 in |
| 439 | // r2 maxOutPt |
| 440 | // r3 maxInIdx |
| 441 | |
| 442 | // r4 x1, i1, i3, Out1 |
| 443 | // r5 out0 |
| 444 | |
| 445 | // r6 frac |
| 446 | // r7 inputIndex |
| 447 | // r8 curOut |
| 448 | |
| 449 | // r9 inc |
| 450 | // r10 vl |
| 451 | // r11 vr |
| 452 | |
| 453 | // r12 |
| 454 | // r13 sp |
| 455 | // r14 |
| 456 | |
| 457 | // the following loop works on 2 frames |
| 458 | |
| Nick Kralevich | eb8b914 | 2011-09-16 13:14:16 -0700 | [diff] [blame] | 459 | "1:\n" |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 460 | " cmp r8, r2\n" // curOut - maxCurOut |
| Nick Kralevich | eb8b914 | 2011-09-16 13:14:16 -0700 | [diff] [blame] | 461 | " bcs 2f\n" |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 462 | |
| 463 | #define MO_ONE_FRAME \ |
| 464 | " add r0, r1, r7, asl #1\n" /* in + inputIndex */\ |
| 465 | " ldrsh r4, [r0]\n" /* in[inputIndex] */\ |
| 466 | " ldr r5, [r8]\n" /* out[outputIndex] */\ |
| 467 | " ldrsh r0, [r0, #-2]\n" /* in[inputIndex-1] */\ |
| 468 | " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\ |
| 469 | " sub r4, r4, r0\n" /* in[inputIndex] - in[inputIndex-1] */\ |
| 470 | " mov r4, r4, lsl #2\n" /* <<2 */\ |
| 471 | " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\ |
| 472 | " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\ |
| 473 | " add r0, r0, r4\n" /* x0 - (..) */\ |
| 474 | " mla r5, r0, r10, r5\n" /* vl*interp + out[] */\ |
| 475 | " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\ |
| 476 | " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\ |
| 477 | " mla r4, r0, r11, r4\n" /* vr*interp + out[] */\ |
| 478 | " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */\ |
| 479 | " str r4, [r8], #4\n" /* out[outputIndex++] = ... */ |
| 480 | |
| 481 | MO_ONE_FRAME // frame 1 |
| 482 | MO_ONE_FRAME // frame 2 |
| 483 | |
| 484 | " cmp r7, r3\n" // inputIndex - maxInIdx |
| Nick Kralevich | eb8b914 | 2011-09-16 13:14:16 -0700 | [diff] [blame] | 485 | " bcc 1b\n" |
| 486 | "2:\n" |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 487 | |
| 488 | " bic r6, r6, #0xC0000000\n" // phaseFraction & ... |
| 489 | // save modified values |
| 490 | " ldr r0, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction |
| 491 | " str r6, [r0]\n" // phaseFraction |
| 492 | " ldr r0, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex |
| 493 | " str r7, [r0]\n" // inputIndex |
| 494 | " ldr r0, [sp, #" MO_PARAM5 " + 4]\n" // out |
| 495 | " sub r8, r0\n" // curOut - out |
| 496 | " asr r8, #2\n" // new outputIndex |
| 497 | " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex |
| 498 | " str r8, [r0]\n" // save outputIndex |
| 499 | |
| 500 | " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, pc}\n" |
| 501 | ); |
| 502 | } |
| 503 | |
| 504 | /******************************************************************* |
| 505 | * |
| 506 | * AsmStereo16Loop |
| 507 | * asm optimized stereo loop version; one loop is 2 frames |
| 508 | * Input: |
| 509 | * in : pointer on input samples |
| 510 | * maxOutPt : pointer on first not filled |
| 511 | * maxInIdx : index on first not used |
| 512 | * outputIndex : pointer on current output index |
| 513 | * out : pointer on output buffer |
| 514 | * inputIndex : pointer on current input index |
| 515 | * vl, vr : left and right gain |
| 516 | * phaseFraction : pointer on current phase fraction |
| 517 | * phaseIncrement |
| 518 | * Ouput: |
| 519 | * outputIndex : |
| 520 | * out : updated buffer |
| 521 | * inputIndex : index of next to use |
| 522 | * phaseFraction : phase fraction for next interpolation |
| 523 | * |
| 524 | *******************************************************************/ |
| 525 | void AudioResamplerOrder1::AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx, |
| 526 | size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr, |
| 527 | uint32_t &phaseFraction, uint32_t phaseIncrement) |
| 528 | { |
| 529 | #define ST_PARAM5 "40" // offset of parameter 5 (outputIndex) |
| 530 | asm( |
| 531 | "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}\n" |
| 532 | // get parameters |
| 533 | " ldr r6, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction |
| 534 | " ldr r6, [r6]\n" // phaseFraction |
| 535 | " ldr r7, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex |
| 536 | " ldr r7, [r7]\n" // inputIndex |
| 537 | " ldr r8, [sp, #" ST_PARAM5 " + 4]\n" // out |
| 538 | " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex |
| 539 | " ldr r0, [r0]\n" // outputIndex |
| 540 | " add r8, r0, asl #2\n" // curOut |
| 541 | " ldr r9, [sp, #" ST_PARAM5 " + 24]\n" // phaseIncrement |
| 542 | " ldr r10, [sp, #" ST_PARAM5 " + 12]\n" // vl |
| 543 | " ldr r11, [sp, #" ST_PARAM5 " + 16]\n" // vr |
| 544 | |
| 545 | // r0 pin, x0, Samp |
| 546 | |
| 547 | // r1 in |
| 548 | // r2 maxOutPt |
| 549 | // r3 maxInIdx |
| 550 | |
| 551 | // r4 x1, i1, i3, out1 |
| 552 | // r5 out0 |
| 553 | |
| 554 | // r6 frac |
| 555 | // r7 inputIndex |
| 556 | // r8 curOut |
| 557 | |
| 558 | // r9 inc |
| 559 | // r10 vl |
| 560 | // r11 vr |
| 561 | |
| 562 | // r12 temporary |
| 563 | // r13 sp |
| 564 | // r14 |
| 565 | |
| Nick Kralevich | eb8b914 | 2011-09-16 13:14:16 -0700 | [diff] [blame] | 566 | "3:\n" |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 567 | " cmp r8, r2\n" // curOut - maxCurOut |
| Nick Kralevich | eb8b914 | 2011-09-16 13:14:16 -0700 | [diff] [blame] | 568 | " bcs 4f\n" |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 569 | |
| 570 | #define ST_ONE_FRAME \ |
| 571 | " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\ |
| 572 | \ |
| 573 | " add r0, r1, r7, asl #2\n" /* in + 2*inputIndex */\ |
| 574 | \ |
| 575 | " ldrsh r4, [r0]\n" /* in[2*inputIndex] */\ |
| 576 | " ldr r5, [r8]\n" /* out[outputIndex] */\ |
| 577 | " ldrsh r12, [r0, #-4]\n" /* in[2*inputIndex-2] */\ |
| 578 | " sub r4, r4, r12\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\ |
| 579 | " mov r4, r4, lsl #2\n" /* <<2 */\ |
| 580 | " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\ |
| 581 | " add r12, r12, r4\n" /* x0 - (..) */\ |
| 582 | " mla r5, r12, r10, r5\n" /* vl*interp + out[] */\ |
| 583 | " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\ |
| 584 | " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\ |
| 585 | \ |
| 586 | " ldrsh r12, [r0, #+2]\n" /* in[2*inputIndex+1] */\ |
| 587 | " ldrsh r0, [r0, #-2]\n" /* in[2*inputIndex-1] */\ |
| 588 | " sub r12, r12, r0\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\ |
| 589 | " mov r12, r12, lsl #2\n" /* <<2 */\ |
| 590 | " smulwt r12, r12, r6\n" /* (x1-x0)*.. */\ |
| 591 | " add r12, r0, r12\n" /* x0 - (..) */\ |
| 592 | " mla r4, r12, r11, r4\n" /* vr*interp + out[] */\ |
| 593 | " str r4, [r8], #4\n" /* out[outputIndex++] = ... */\ |
| 594 | \ |
| 595 | " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\ |
| 596 | " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */ |
| 597 | |
| 598 | ST_ONE_FRAME // frame 1 |
| 599 | ST_ONE_FRAME // frame 1 |
| 600 | |
| 601 | " cmp r7, r3\n" // inputIndex - maxInIdx |
| Nick Kralevich | eb8b914 | 2011-09-16 13:14:16 -0700 | [diff] [blame] | 602 | " bcc 3b\n" |
| 603 | "4:\n" |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 604 | |
| 605 | " bic r6, r6, #0xC0000000\n" // phaseFraction & ... |
| 606 | // save modified values |
| 607 | " ldr r0, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction |
| 608 | " str r6, [r0]\n" // phaseFraction |
| 609 | " ldr r0, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex |
| 610 | " str r7, [r0]\n" // inputIndex |
| 611 | " ldr r0, [sp, #" ST_PARAM5 " + 4]\n" // out |
| 612 | " sub r8, r0\n" // curOut - out |
| 613 | " asr r8, #2\n" // new outputIndex |
| 614 | " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex |
| 615 | " str r8, [r0]\n" // save outputIndex |
| 616 | |
| 617 | " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, pc}\n" |
| 618 | ); |
| 619 | } |
| 620 | |
| 621 | #endif // ASM_ARM_RESAMP1 |
| 622 | |
| 623 | |
| 624 | // ---------------------------------------------------------------------------- |
| 625 | } |
| 626 | ; // namespace android |