blob: 4eac0328ffe87f196cb082769a1cdc867a9a6cc5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Huang592a6d92011-04-06 14:19:29 +080029#ifdef __arm__
30#include <machine/cpu-features.h>
31#endif
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033namespace android {
34
Jim Huang592a6d92011-04-06 14:19:29 +080035#ifdef __ARM_HAVE_HALFWORD_MULTIPLY // optimized asm option
Glenn Kastencd498c32011-11-17 13:27:22 -080036 #define ASM_ARM_RESAMP1 // enable asm optimisation for ResamplerOrder1
Jim Huang592a6d92011-04-06 14:19:29 +080037#endif // __ARM_HAVE_HALFWORD_MULTIPLY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038// ----------------------------------------------------------------------------
39
40class AudioResamplerOrder1 : public AudioResampler {
41public:
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);
47private:
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,
62 uint32_t &phaseFraction, uint32_t phaseIncrement);
63 void AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
64 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
65 uint32_t &phaseFraction, uint32_t phaseIncrement);
66#endif // ASM_ARM_RESAMP1
67
68 static inline int32_t Interp(int32_t x0, int32_t x1, uint32_t f) {
69 return x0 + (((x1 - x0) * (int32_t)(f >> kPreInterpShift)) >> kNumInterpBits);
70 }
71 static inline void Advance(size_t* index, uint32_t* frac, uint32_t inc) {
72 *frac += inc;
73 *index += (size_t)(*frac >> kNumPhaseBits);
74 *frac &= kPhaseMask;
75 }
76 int mX0L;
77 int mX0R;
78};
79
80// ----------------------------------------------------------------------------
81AudioResampler* AudioResampler::create(int bitDepth, int inChannelCount,
82 int32_t sampleRate, int quality) {
83
84 // can only create low quality resample now
85 AudioResampler* resampler;
86
87 char value[PROPERTY_VALUE_MAX];
88 if (property_get("af.resampler.quality", value, 0)) {
89 quality = atoi(value);
Steve Block5baa3a62011-12-20 16:23:08 +000090 ALOGD("forcing AudioResampler quality to %d", quality);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 }
92
93 if (quality == DEFAULT)
94 quality = LOW_QUALITY;
95
96 switch (quality) {
97 default:
98 case LOW_QUALITY:
Steve Block71f2cf12011-10-20 11:56:00 +010099 ALOGV("Create linear Resampler");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 resampler = new AudioResamplerOrder1(bitDepth, inChannelCount, sampleRate);
101 break;
102 case MED_QUALITY:
Steve Block71f2cf12011-10-20 11:56:00 +0100103 ALOGV("Create cubic Resampler");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 resampler = new AudioResamplerCubic(bitDepth, inChannelCount, sampleRate);
105 break;
106 case HIGH_QUALITY:
Steve Block71f2cf12011-10-20 11:56:00 +0100107 ALOGV("Create sinc Resampler");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 resampler = new AudioResamplerSinc(bitDepth, inChannelCount, sampleRate);
109 break;
110 }
111
112 // initialize resampler
113 resampler->init();
114 return resampler;
115}
116
117AudioResampler::AudioResampler(int bitDepth, int inChannelCount,
118 int32_t sampleRate) :
119 mBitDepth(bitDepth), mChannelCount(inChannelCount),
120 mSampleRate(sampleRate), mInSampleRate(sampleRate), mInputIndex(0),
121 mPhaseFraction(0) {
122 // sanity check on format
123 if ((bitDepth != 16) ||(inChannelCount < 1) || (inChannelCount > 2)) {
Steve Block3762c312012-01-06 19:20:56 +0000124 ALOGE("Unsupported sample format, %d bits, %d channels", bitDepth,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 inChannelCount);
Steve Blockec193de2012-01-09 18:35:44 +0000126 // ALOG_ASSERT(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 }
128
129 // initialize common members
130 mVolume[0] = mVolume[1] = 0;
131 mBuffer.frameCount = 0;
132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133}
134
135AudioResampler::~AudioResampler() {
136}
137
138void AudioResampler::setSampleRate(int32_t inSampleRate) {
139 mInSampleRate = inSampleRate;
140 mPhaseIncrement = (uint32_t)((kPhaseMultiplier * inSampleRate) / mSampleRate);
141}
142
143void AudioResampler::setVolume(int16_t left, int16_t right) {
144 // TODO: Implement anti-zipper filter
145 mVolume[0] = left;
146 mVolume[1] = right;
147}
148
Eric Laurent4bb21c42011-02-28 16:52:51 -0800149void AudioResampler::reset() {
150 mInputIndex = 0;
151 mPhaseFraction = 0;
152 mBuffer.frameCount = 0;
153}
154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155// ----------------------------------------------------------------------------
156
157void AudioResamplerOrder1::resample(int32_t* out, size_t outFrameCount,
158 AudioBufferProvider* provider) {
159
160 // should never happen, but we overflow if it does
Steve Blockec193de2012-01-09 18:35:44 +0000161 // ALOG_ASSERT(outFrameCount < 32767);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
163 // select the appropriate resampler
164 switch (mChannelCount) {
165 case 1:
166 resampleMono16(out, outFrameCount, provider);
167 break;
168 case 2:
169 resampleStereo16(out, outFrameCount, provider);
170 break;
171 }
172}
173
174void AudioResamplerOrder1::resampleStereo16(int32_t* out, size_t outFrameCount,
175 AudioBufferProvider* provider) {
176
177 int32_t vl = mVolume[0];
178 int32_t vr = mVolume[1];
179
180 size_t inputIndex = mInputIndex;
181 uint32_t phaseFraction = mPhaseFraction;
182 uint32_t phaseIncrement = mPhaseIncrement;
183 size_t outputIndex = 0;
184 size_t outputSampleCount = outFrameCount * 2;
185 size_t inFrameCount = (outFrameCount*mInSampleRate)/mSampleRate;
186
Glenn Kasten0765c442012-01-27 15:24:38 -0800187 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
189
190 while (outputIndex < outputSampleCount) {
191
192 // buffer is empty, fetch a new one
193 while (mBuffer.frameCount == 0) {
194 mBuffer.frameCount = inFrameCount;
195 provider->getNextBuffer(&mBuffer);
196 if (mBuffer.raw == NULL) {
197 goto resampleStereo16_exit;
198 }
199
Glenn Kasten0765c442012-01-27 15:24:38 -0800200 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 if (mBuffer.frameCount > inputIndex) break;
202
203 inputIndex -= mBuffer.frameCount;
204 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
205 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
206 provider->releaseBuffer(&mBuffer);
207 // mBuffer.frameCount == 0 now so we reload a new buffer
208 }
209
210 int16_t *in = mBuffer.i16;
211
212 // handle boundary case
213 while (inputIndex == 0) {
Glenn Kasten0765c442012-01-27 15:24:38 -0800214 // ALOGE("boundary case");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 out[outputIndex++] += vl * Interp(mX0L, in[0], phaseFraction);
216 out[outputIndex++] += vr * Interp(mX0R, in[1], phaseFraction);
217 Advance(&inputIndex, &phaseFraction, phaseIncrement);
218 if (outputIndex == outputSampleCount)
219 break;
220 }
221
222 // process input samples
Glenn Kasten0765c442012-01-27 15:24:38 -0800223 // ALOGE("general case");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224
225#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
226 if (inputIndex + 2 < mBuffer.frameCount) {
227 int32_t* maxOutPt;
228 int32_t maxInIdx;
229
230 maxOutPt = out + (outputSampleCount - 2); // 2 because 2 frames per loop
231 maxInIdx = mBuffer.frameCount - 2;
232 AsmStereo16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
233 phaseFraction, phaseIncrement);
234 }
235#endif // ASM_ARM_RESAMP1
236
237 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
238 out[outputIndex++] += vl * Interp(in[inputIndex*2-2],
239 in[inputIndex*2], phaseFraction);
240 out[outputIndex++] += vr * Interp(in[inputIndex*2-1],
241 in[inputIndex*2+1], phaseFraction);
242 Advance(&inputIndex, &phaseFraction, phaseIncrement);
243 }
244
Glenn Kasten0765c442012-01-27 15:24:38 -0800245 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246
247 // if done with buffer, save samples
248 if (inputIndex >= mBuffer.frameCount) {
249 inputIndex -= mBuffer.frameCount;
250
Steve Block3762c312012-01-06 19:20:56 +0000251 // ALOGE("buffer done, new input index %d", inputIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
254 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
255 provider->releaseBuffer(&mBuffer);
256
257 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockec193de2012-01-09 18:35:44 +0000258 // ALOG_ASSERT(mBuffer.frameCount == 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 }
260 }
261
Glenn Kasten0765c442012-01-27 15:24:38 -0800262 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263
264resampleStereo16_exit:
265 // save state
266 mInputIndex = inputIndex;
267 mPhaseFraction = phaseFraction;
268}
269
270void AudioResamplerOrder1::resampleMono16(int32_t* out, size_t outFrameCount,
271 AudioBufferProvider* provider) {
272
273 int32_t vl = mVolume[0];
274 int32_t vr = mVolume[1];
275
276 size_t inputIndex = mInputIndex;
277 uint32_t phaseFraction = mPhaseFraction;
278 uint32_t phaseIncrement = mPhaseIncrement;
279 size_t outputIndex = 0;
280 size_t outputSampleCount = outFrameCount * 2;
281 size_t inFrameCount = (outFrameCount*mInSampleRate)/mSampleRate;
282
Glenn Kasten0765c442012-01-27 15:24:38 -0800283 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
285 while (outputIndex < outputSampleCount) {
286 // buffer is empty, fetch a new one
287 while (mBuffer.frameCount == 0) {
288 mBuffer.frameCount = inFrameCount;
289 provider->getNextBuffer(&mBuffer);
290 if (mBuffer.raw == NULL) {
291 mInputIndex = inputIndex;
292 mPhaseFraction = phaseFraction;
293 goto resampleMono16_exit;
294 }
Glenn Kasten0765c442012-01-27 15:24:38 -0800295 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 if (mBuffer.frameCount > inputIndex) break;
297
298 inputIndex -= mBuffer.frameCount;
299 mX0L = mBuffer.i16[mBuffer.frameCount-1];
300 provider->releaseBuffer(&mBuffer);
301 // mBuffer.frameCount == 0 now so we reload a new buffer
302 }
303 int16_t *in = mBuffer.i16;
304
305 // handle boundary case
306 while (inputIndex == 0) {
Glenn Kasten0765c442012-01-27 15:24:38 -0800307 // ALOGE("boundary case");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 int32_t sample = Interp(mX0L, in[0], phaseFraction);
309 out[outputIndex++] += vl * sample;
310 out[outputIndex++] += vr * sample;
311 Advance(&inputIndex, &phaseFraction, phaseIncrement);
312 if (outputIndex == outputSampleCount)
313 break;
314 }
315
316 // process input samples
Glenn Kasten0765c442012-01-27 15:24:38 -0800317 // ALOGE("general case");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
319#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
320 if (inputIndex + 2 < mBuffer.frameCount) {
321 int32_t* maxOutPt;
322 int32_t maxInIdx;
323
324 maxOutPt = out + (outputSampleCount - 2);
325 maxInIdx = (int32_t)mBuffer.frameCount - 2;
326 AsmMono16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
327 phaseFraction, phaseIncrement);
328 }
329#endif // ASM_ARM_RESAMP1
330
331 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
332 int32_t sample = Interp(in[inputIndex-1], in[inputIndex],
333 phaseFraction);
334 out[outputIndex++] += vl * sample;
335 out[outputIndex++] += vr * sample;
336 Advance(&inputIndex, &phaseFraction, phaseIncrement);
337 }
338
339
Glenn Kasten0765c442012-01-27 15:24:38 -0800340 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341
342 // if done with buffer, save samples
343 if (inputIndex >= mBuffer.frameCount) {
344 inputIndex -= mBuffer.frameCount;
345
Steve Block3762c312012-01-06 19:20:56 +0000346 // ALOGE("buffer done, new input index %d", inputIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347
348 mX0L = mBuffer.i16[mBuffer.frameCount-1];
349 provider->releaseBuffer(&mBuffer);
350
351 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockec193de2012-01-09 18:35:44 +0000352 // ALOG_ASSERT(mBuffer.frameCount == 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 }
354 }
355
Glenn Kasten0765c442012-01-27 15:24:38 -0800356 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357
358resampleMono16_exit:
359 // save state
360 mInputIndex = inputIndex;
361 mPhaseFraction = phaseFraction;
362}
363
364#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
365
366/*******************************************************************
367*
368* AsmMono16Loop
369* asm optimized monotonic loop version; one loop is 2 frames
370* Input:
371* in : pointer on input samples
372* maxOutPt : pointer on first not filled
373* maxInIdx : index on first not used
374* outputIndex : pointer on current output index
375* out : pointer on output buffer
376* inputIndex : pointer on current input index
377* vl, vr : left and right gain
378* phaseFraction : pointer on current phase fraction
379* phaseIncrement
380* Ouput:
381* outputIndex :
382* out : updated buffer
383* inputIndex : index of next to use
384* phaseFraction : phase fraction for next interpolation
385*
386*******************************************************************/
Glenn Kastencd498c32011-11-17 13:27:22 -0800387__attribute__((noinline))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388void AudioResamplerOrder1::AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
389 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
390 uint32_t &phaseFraction, uint32_t phaseIncrement)
391{
392#define MO_PARAM5 "36" // offset of parameter 5 (outputIndex)
393
394 asm(
395 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"
396 // get parameters
397 " ldr r6, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
398 " ldr r6, [r6]\n" // phaseFraction
399 " ldr r7, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
400 " ldr r7, [r7]\n" // inputIndex
401 " ldr r8, [sp, #" MO_PARAM5 " + 4]\n" // out
402 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
403 " ldr r0, [r0]\n" // outputIndex
404 " add r8, r0, asl #2\n" // curOut
405 " ldr r9, [sp, #" MO_PARAM5 " + 24]\n" // phaseIncrement
406 " ldr r10, [sp, #" MO_PARAM5 " + 12]\n" // vl
407 " ldr r11, [sp, #" MO_PARAM5 " + 16]\n" // vr
408
409 // r0 pin, x0, Samp
410
411 // r1 in
412 // r2 maxOutPt
413 // r3 maxInIdx
414
415 // r4 x1, i1, i3, Out1
416 // r5 out0
417
418 // r6 frac
419 // r7 inputIndex
420 // r8 curOut
421
422 // r9 inc
423 // r10 vl
424 // r11 vr
425
426 // r12
427 // r13 sp
428 // r14
429
430 // the following loop works on 2 frames
431
Nick Kralevich80754d22011-09-16 13:14:16 -0700432 "1:\n"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevich80754d22011-09-16 13:14:16 -0700434 " bcs 2f\n"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435
436#define MO_ONE_FRAME \
437 " add r0, r1, r7, asl #1\n" /* in + inputIndex */\
438 " ldrsh r4, [r0]\n" /* in[inputIndex] */\
439 " ldr r5, [r8]\n" /* out[outputIndex] */\
440 " ldrsh r0, [r0, #-2]\n" /* in[inputIndex-1] */\
441 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
442 " sub r4, r4, r0\n" /* in[inputIndex] - in[inputIndex-1] */\
443 " mov r4, r4, lsl #2\n" /* <<2 */\
444 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
445 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
446 " add r0, r0, r4\n" /* x0 - (..) */\
447 " mla r5, r0, r10, r5\n" /* vl*interp + out[] */\
448 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
449 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
450 " mla r4, r0, r11, r4\n" /* vr*interp + out[] */\
451 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */\
452 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */
453
454 MO_ONE_FRAME // frame 1
455 MO_ONE_FRAME // frame 2
456
457 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevich80754d22011-09-16 13:14:16 -0700458 " bcc 1b\n"
459 "2:\n"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460
461 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
462 // save modified values
463 " ldr r0, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
464 " str r6, [r0]\n" // phaseFraction
465 " ldr r0, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
466 " str r7, [r0]\n" // inputIndex
467 " ldr r0, [sp, #" MO_PARAM5 " + 4]\n" // out
468 " sub r8, r0\n" // curOut - out
469 " asr r8, #2\n" // new outputIndex
470 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
471 " str r8, [r0]\n" // save outputIndex
472
473 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, pc}\n"
474 );
475}
476
477/*******************************************************************
478*
479* AsmStereo16Loop
480* asm optimized stereo loop version; one loop is 2 frames
481* Input:
482* in : pointer on input samples
483* maxOutPt : pointer on first not filled
484* maxInIdx : index on first not used
485* outputIndex : pointer on current output index
486* out : pointer on output buffer
487* inputIndex : pointer on current input index
488* vl, vr : left and right gain
489* phaseFraction : pointer on current phase fraction
490* phaseIncrement
491* Ouput:
492* outputIndex :
493* out : updated buffer
494* inputIndex : index of next to use
495* phaseFraction : phase fraction for next interpolation
496*
497*******************************************************************/
Glenn Kastencd498c32011-11-17 13:27:22 -0800498__attribute__((noinline))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499void AudioResamplerOrder1::AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
500 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
501 uint32_t &phaseFraction, uint32_t phaseIncrement)
502{
503#define ST_PARAM5 "40" // offset of parameter 5 (outputIndex)
504 asm(
505 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}\n"
506 // get parameters
507 " ldr r6, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
508 " ldr r6, [r6]\n" // phaseFraction
509 " ldr r7, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
510 " ldr r7, [r7]\n" // inputIndex
511 " ldr r8, [sp, #" ST_PARAM5 " + 4]\n" // out
512 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
513 " ldr r0, [r0]\n" // outputIndex
514 " add r8, r0, asl #2\n" // curOut
515 " ldr r9, [sp, #" ST_PARAM5 " + 24]\n" // phaseIncrement
516 " ldr r10, [sp, #" ST_PARAM5 " + 12]\n" // vl
517 " ldr r11, [sp, #" ST_PARAM5 " + 16]\n" // vr
518
519 // r0 pin, x0, Samp
520
521 // r1 in
522 // r2 maxOutPt
523 // r3 maxInIdx
524
525 // r4 x1, i1, i3, out1
526 // r5 out0
527
528 // r6 frac
529 // r7 inputIndex
530 // r8 curOut
531
532 // r9 inc
533 // r10 vl
534 // r11 vr
535
536 // r12 temporary
537 // r13 sp
538 // r14
539
Nick Kralevich80754d22011-09-16 13:14:16 -0700540 "3:\n"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevich80754d22011-09-16 13:14:16 -0700542 " bcs 4f\n"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543
544#define ST_ONE_FRAME \
545 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
546\
547 " add r0, r1, r7, asl #2\n" /* in + 2*inputIndex */\
548\
549 " ldrsh r4, [r0]\n" /* in[2*inputIndex] */\
550 " ldr r5, [r8]\n" /* out[outputIndex] */\
551 " ldrsh r12, [r0, #-4]\n" /* in[2*inputIndex-2] */\
552 " sub r4, r4, r12\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
553 " mov r4, r4, lsl #2\n" /* <<2 */\
554 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
555 " add r12, r12, r4\n" /* x0 - (..) */\
556 " mla r5, r12, r10, r5\n" /* vl*interp + out[] */\
557 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
558 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
559\
560 " ldrsh r12, [r0, #+2]\n" /* in[2*inputIndex+1] */\
561 " ldrsh r0, [r0, #-2]\n" /* in[2*inputIndex-1] */\
562 " sub r12, r12, r0\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
563 " mov r12, r12, lsl #2\n" /* <<2 */\
564 " smulwt r12, r12, r6\n" /* (x1-x0)*.. */\
565 " add r12, r0, r12\n" /* x0 - (..) */\
566 " mla r4, r12, r11, r4\n" /* vr*interp + out[] */\
567 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */\
568\
569 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
570 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */
571
572 ST_ONE_FRAME // frame 1
573 ST_ONE_FRAME // frame 1
574
575 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevich80754d22011-09-16 13:14:16 -0700576 " bcc 3b\n"
577 "4:\n"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578
579 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
580 // save modified values
581 " ldr r0, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
582 " str r6, [r0]\n" // phaseFraction
583 " ldr r0, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
584 " str r7, [r0]\n" // inputIndex
585 " ldr r0, [sp, #" ST_PARAM5 " + 4]\n" // out
586 " sub r8, r0\n" // curOut - out
587 " asr r8, #2\n" // new outputIndex
588 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
589 " str r8, [r0]\n" // save outputIndex
590
591 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, pc}\n"
592 );
593}
594
595#endif // ASM_ARM_RESAMP1
596
597
598// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599
Glenn Kastencd498c32011-11-17 13:27:22 -0800600} // namespace android