blob: 323f1a4a1f4e374515ddd1d207c42c0348aaaf8d [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
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 Huang0c0a1c02011-04-06 14:19:29 +080029#ifdef __arm__
30#include <machine/cpu-features.h>
31#endif
32
Mathias Agopian65ab4712010-07-14 17:59:35 -070033namespace android {
34
Jim Huang0c0a1c02011-04-06 14:19:29 +080035#ifdef __ARM_HAVE_HALFWORD_MULTIPLY // optimized asm option
Glenn Kastenc23e2f22011-11-17 13:27:22 -080036 #define ASM_ARM_RESAMP1 // enable asm optimisation for ResamplerOrder1
Jim Huang0c0a1c02011-04-06 14:19:29 +080037#endif // __ARM_HAVE_HALFWORD_MULTIPLY
Mathias Agopian65ab4712010-07-14 17:59:35 -070038// ----------------------------------------------------------------------------
39
40class AudioResamplerOrder1 : public AudioResampler {
41public:
42 AudioResamplerOrder1(int bitDepth, int inChannelCount, int32_t sampleRate) :
Glenn Kastenac602052012-10-01 14:04:31 -070043 AudioResampler(bitDepth, inChannelCount, sampleRate, LOW_QUALITY), mX0L(0), mX0R(0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -070044 }
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
Glenn Kastenac602052012-10-01 14:04:31 -070080bool AudioResampler::qualityIsSupported(src_quality quality)
81{
82 switch (quality) {
83 case DEFAULT_QUALITY:
84 case LOW_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070085 case MED_QUALITY:
86 case HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070087 case VERY_HIGH_QUALITY:
88 return true;
89 default:
90 return false;
91 }
92}
93
Mathias Agopian65ab4712010-07-14 17:59:35 -070094// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070095
Glenn Kastenac602052012-10-01 14:04:31 -070096static pthread_once_t once_control = PTHREAD_ONCE_INIT;
97static AudioResampler::src_quality defaultQuality = AudioResampler::DEFAULT_QUALITY;
Mathias Agopian65ab4712010-07-14 17:59:35 -070098
Glenn Kastenac602052012-10-01 14:04:31 -070099void AudioResampler::init_routine()
100{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700101 char value[PROPERTY_VALUE_MAX];
Glenn Kastenac602052012-10-01 14:04:31 -0700102 if (property_get("af.resampler.quality", value, NULL) > 0) {
103 char *endptr;
104 unsigned long l = strtoul(value, &endptr, 0);
105 if (*endptr == '\0') {
106 defaultQuality = (src_quality) l;
107 ALOGD("forcing AudioResampler quality to %d", defaultQuality);
108 if (defaultQuality < DEFAULT_QUALITY || defaultQuality > VERY_HIGH_QUALITY) {
109 defaultQuality = DEFAULT_QUALITY;
110 }
111 }
112 }
113}
114
115uint32_t AudioResampler::qualityMHz(src_quality quality)
116{
117 switch (quality) {
118 default:
119 case DEFAULT_QUALITY:
120 case LOW_QUALITY:
121 return 3;
122 case MED_QUALITY:
123 return 6;
124 case HIGH_QUALITY:
125 return 20;
126 case VERY_HIGH_QUALITY:
127 return 34;
128 }
129}
130
Glenn Kastenc4640c92012-10-22 17:09:27 -0700131static const uint32_t maxMHz = 130; // an arbitrary number that permits 3 VHQ, should be tunable
Glenn Kastenac602052012-10-01 14:04:31 -0700132static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
133static uint32_t currentMHz = 0;
134
135AudioResampler* AudioResampler::create(int bitDepth, int inChannelCount,
136 int32_t sampleRate, src_quality quality) {
137
138 bool atFinalQuality;
139 if (quality == DEFAULT_QUALITY) {
140 // read the resampler default quality property the first time it is needed
141 int ok = pthread_once(&once_control, init_routine);
142 if (ok != 0) {
143 ALOGE("%s pthread_once failed: %d", __func__, ok);
144 }
145 quality = defaultQuality;
146 atFinalQuality = false;
147 } else {
148 atFinalQuality = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149 }
150
Glenn Kastenac602052012-10-01 14:04:31 -0700151 // naive implementation of CPU load throttling doesn't account for whether resampler is active
152 pthread_mutex_lock(&mutex);
153 for (;;) {
154 uint32_t deltaMHz = qualityMHz(quality);
155 uint32_t newMHz = currentMHz + deltaMHz;
156 if ((qualityIsSupported(quality) && newMHz <= maxMHz) || atFinalQuality) {
157 ALOGV("resampler load %u -> %u MHz due to delta +%u MHz from quality %d",
158 currentMHz, newMHz, deltaMHz, quality);
159 currentMHz = newMHz;
160 break;
161 }
162 // not enough CPU available for proposed quality level, so try next lowest level
163 switch (quality) {
164 default:
165 case DEFAULT_QUALITY:
166 case LOW_QUALITY:
167 atFinalQuality = true;
168 break;
169 case MED_QUALITY:
170 quality = LOW_QUALITY;
171 break;
172 case HIGH_QUALITY:
173 quality = MED_QUALITY;
174 break;
175 case VERY_HIGH_QUALITY:
176 quality = HIGH_QUALITY;
177 break;
178 }
179 }
180 pthread_mutex_unlock(&mutex);
181
182 AudioResampler* resampler;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700183
184 switch (quality) {
185 default:
Glenn Kastenac602052012-10-01 14:04:31 -0700186 case DEFAULT_QUALITY:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700187 case LOW_QUALITY:
Steve Block3856b092011-10-20 11:56:00 +0100188 ALOGV("Create linear Resampler");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189 resampler = new AudioResamplerOrder1(bitDepth, inChannelCount, sampleRate);
190 break;
191 case MED_QUALITY:
Steve Block3856b092011-10-20 11:56:00 +0100192 ALOGV("Create cubic Resampler");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193 resampler = new AudioResamplerCubic(bitDepth, inChannelCount, sampleRate);
194 break;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800195 case HIGH_QUALITY:
196 ALOGV("Create HIGH_QUALITY sinc Resampler");
197 resampler = new AudioResamplerSinc(bitDepth, inChannelCount, sampleRate);
Glenn Kastenac602052012-10-01 14:04:31 -0700198 break;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800199 case VERY_HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -0700200 ALOGV("Create VERY_HIGH_QUALITY sinc Resampler = %d", quality);
SathishKumar Mani76b11162012-01-17 10:49:47 -0800201 resampler = new AudioResamplerSinc(bitDepth, inChannelCount, sampleRate, quality);
202 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700203 }
204
205 // initialize resampler
206 resampler->init();
207 return resampler;
208}
209
210AudioResampler::AudioResampler(int bitDepth, int inChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -0700211 int32_t sampleRate, src_quality quality) :
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212 mBitDepth(bitDepth), mChannelCount(inChannelCount),
213 mSampleRate(sampleRate), mInSampleRate(sampleRate), mInputIndex(0),
John Grossman4ff14ba2012-02-08 16:37:41 -0800214 mPhaseFraction(0), mLocalTimeFreq(0),
Glenn Kastenac602052012-10-01 14:04:31 -0700215 mPTS(AudioBufferProvider::kInvalidPTS), mQuality(quality) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216 // sanity check on format
217 if ((bitDepth != 16) ||(inChannelCount < 1) || (inChannelCount > 2)) {
Steve Block29357bc2012-01-06 19:20:56 +0000218 ALOGE("Unsupported sample format, %d bits, %d channels", bitDepth,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700219 inChannelCount);
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000220 // ALOG_ASSERT(0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700221 }
Glenn Kastenac602052012-10-01 14:04:31 -0700222 if (sampleRate <= 0) {
223 ALOGE("Unsupported sample rate %d Hz", sampleRate);
224 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700225
226 // initialize common members
227 mVolume[0] = mVolume[1] = 0;
228 mBuffer.frameCount = 0;
229
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230}
231
232AudioResampler::~AudioResampler() {
Glenn Kastenac602052012-10-01 14:04:31 -0700233 pthread_mutex_lock(&mutex);
234 src_quality quality = getQuality();
235 uint32_t deltaMHz = qualityMHz(quality);
236 int32_t newMHz = currentMHz - deltaMHz;
237 ALOGV("resampler load %u -> %d MHz due to delta -%u MHz from quality %d",
238 currentMHz, newMHz, deltaMHz, quality);
239 LOG_ALWAYS_FATAL_IF(newMHz < 0, "negative resampler load %d MHz", newMHz);
240 currentMHz = newMHz;
241 pthread_mutex_unlock(&mutex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242}
243
244void AudioResampler::setSampleRate(int32_t inSampleRate) {
245 mInSampleRate = inSampleRate;
246 mPhaseIncrement = (uint32_t)((kPhaseMultiplier * inSampleRate) / mSampleRate);
247}
248
249void AudioResampler::setVolume(int16_t left, int16_t right) {
250 // TODO: Implement anti-zipper filter
251 mVolume[0] = left;
252 mVolume[1] = right;
253}
254
John Grossman4ff14ba2012-02-08 16:37:41 -0800255void AudioResampler::setLocalTimeFreq(uint64_t freq) {
256 mLocalTimeFreq = freq;
257}
258
259void AudioResampler::setPTS(int64_t pts) {
260 mPTS = pts;
261}
262
263int64_t AudioResampler::calculateOutputPTS(int outputFrameIndex) {
264
265 if (mPTS == AudioBufferProvider::kInvalidPTS) {
266 return AudioBufferProvider::kInvalidPTS;
267 } else {
268 return mPTS + ((outputFrameIndex * mLocalTimeFreq) / mSampleRate);
269 }
270}
271
Eric Laurent243f5f92011-02-28 16:52:51 -0800272void AudioResampler::reset() {
273 mInputIndex = 0;
274 mPhaseFraction = 0;
275 mBuffer.frameCount = 0;
276}
277
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278// ----------------------------------------------------------------------------
279
280void AudioResamplerOrder1::resample(int32_t* out, size_t outFrameCount,
281 AudioBufferProvider* provider) {
282
283 // should never happen, but we overflow if it does
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000284 // ALOG_ASSERT(outFrameCount < 32767);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285
286 // select the appropriate resampler
287 switch (mChannelCount) {
288 case 1:
289 resampleMono16(out, outFrameCount, provider);
290 break;
291 case 2:
292 resampleStereo16(out, outFrameCount, provider);
293 break;
294 }
295}
296
297void AudioResamplerOrder1::resampleStereo16(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
Glenn Kasten90bebef2012-01-27 15:24:38 -0800310 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700311 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
312
313 while (outputIndex < outputSampleCount) {
314
315 // buffer is empty, fetch a new one
316 while (mBuffer.frameCount == 0) {
317 mBuffer.frameCount = inFrameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -0800318 provider->getNextBuffer(&mBuffer,
319 calculateOutputPTS(outputIndex / 2));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 if (mBuffer.raw == NULL) {
321 goto resampleStereo16_exit;
322 }
323
Glenn Kasten90bebef2012-01-27 15:24:38 -0800324 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325 if (mBuffer.frameCount > inputIndex) break;
326
327 inputIndex -= mBuffer.frameCount;
328 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
329 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
330 provider->releaseBuffer(&mBuffer);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700331 // mBuffer.frameCount == 0 now so we reload a new buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332 }
333
334 int16_t *in = mBuffer.i16;
335
336 // handle boundary case
337 while (inputIndex == 0) {
Glenn Kasten90bebef2012-01-27 15:24:38 -0800338 // ALOGE("boundary case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700339 out[outputIndex++] += vl * Interp(mX0L, in[0], phaseFraction);
340 out[outputIndex++] += vr * Interp(mX0R, in[1], phaseFraction);
341 Advance(&inputIndex, &phaseFraction, phaseIncrement);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700342 if (outputIndex == outputSampleCount) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343 break;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700344 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 }
346
347 // process input samples
Glenn Kasten90bebef2012-01-27 15:24:38 -0800348 // ALOGE("general case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349
350#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
351 if (inputIndex + 2 < mBuffer.frameCount) {
352 int32_t* maxOutPt;
353 int32_t maxInIdx;
354
355 maxOutPt = out + (outputSampleCount - 2); // 2 because 2 frames per loop
356 maxInIdx = mBuffer.frameCount - 2;
357 AsmStereo16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
358 phaseFraction, phaseIncrement);
359 }
360#endif // ASM_ARM_RESAMP1
361
362 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
363 out[outputIndex++] += vl * Interp(in[inputIndex*2-2],
364 in[inputIndex*2], phaseFraction);
365 out[outputIndex++] += vr * Interp(in[inputIndex*2-1],
366 in[inputIndex*2+1], phaseFraction);
367 Advance(&inputIndex, &phaseFraction, phaseIncrement);
368 }
369
Glenn Kasten90bebef2012-01-27 15:24:38 -0800370 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371
372 // if done with buffer, save samples
373 if (inputIndex >= mBuffer.frameCount) {
374 inputIndex -= mBuffer.frameCount;
375
Steve Block29357bc2012-01-06 19:20:56 +0000376 // ALOGE("buffer done, new input index %d", inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377
378 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
379 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
380 provider->releaseBuffer(&mBuffer);
381
382 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000383 // ALOG_ASSERT(mBuffer.frameCount == 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 }
385 }
386
Glenn Kasten90bebef2012-01-27 15:24:38 -0800387 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388
389resampleStereo16_exit:
390 // save state
391 mInputIndex = inputIndex;
392 mPhaseFraction = phaseFraction;
393}
394
395void AudioResamplerOrder1::resampleMono16(int32_t* out, size_t outFrameCount,
396 AudioBufferProvider* provider) {
397
398 int32_t vl = mVolume[0];
399 int32_t vr = mVolume[1];
400
401 size_t inputIndex = mInputIndex;
402 uint32_t phaseFraction = mPhaseFraction;
403 uint32_t phaseIncrement = mPhaseIncrement;
404 size_t outputIndex = 0;
405 size_t outputSampleCount = outFrameCount * 2;
406 size_t inFrameCount = (outFrameCount*mInSampleRate)/mSampleRate;
407
Glenn Kasten90bebef2012-01-27 15:24:38 -0800408 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
410 while (outputIndex < outputSampleCount) {
411 // buffer is empty, fetch a new one
412 while (mBuffer.frameCount == 0) {
413 mBuffer.frameCount = inFrameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -0800414 provider->getNextBuffer(&mBuffer,
415 calculateOutputPTS(outputIndex / 2));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416 if (mBuffer.raw == NULL) {
417 mInputIndex = inputIndex;
418 mPhaseFraction = phaseFraction;
419 goto resampleMono16_exit;
420 }
Glenn Kasten90bebef2012-01-27 15:24:38 -0800421 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422 if (mBuffer.frameCount > inputIndex) break;
423
424 inputIndex -= mBuffer.frameCount;
425 mX0L = mBuffer.i16[mBuffer.frameCount-1];
426 provider->releaseBuffer(&mBuffer);
427 // mBuffer.frameCount == 0 now so we reload a new buffer
428 }
429 int16_t *in = mBuffer.i16;
430
431 // handle boundary case
432 while (inputIndex == 0) {
Glenn Kasten90bebef2012-01-27 15:24:38 -0800433 // ALOGE("boundary case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700434 int32_t sample = Interp(mX0L, in[0], phaseFraction);
435 out[outputIndex++] += vl * sample;
436 out[outputIndex++] += vr * sample;
437 Advance(&inputIndex, &phaseFraction, phaseIncrement);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700438 if (outputIndex == outputSampleCount) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439 break;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700440 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441 }
442
443 // process input samples
Glenn Kasten90bebef2012-01-27 15:24:38 -0800444 // ALOGE("general case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445
446#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
447 if (inputIndex + 2 < mBuffer.frameCount) {
448 int32_t* maxOutPt;
449 int32_t maxInIdx;
450
451 maxOutPt = out + (outputSampleCount - 2);
452 maxInIdx = (int32_t)mBuffer.frameCount - 2;
453 AsmMono16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
454 phaseFraction, phaseIncrement);
455 }
456#endif // ASM_ARM_RESAMP1
457
458 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
459 int32_t sample = Interp(in[inputIndex-1], in[inputIndex],
460 phaseFraction);
461 out[outputIndex++] += vl * sample;
462 out[outputIndex++] += vr * sample;
463 Advance(&inputIndex, &phaseFraction, phaseIncrement);
464 }
465
466
Glenn Kasten90bebef2012-01-27 15:24:38 -0800467 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468
469 // if done with buffer, save samples
470 if (inputIndex >= mBuffer.frameCount) {
471 inputIndex -= mBuffer.frameCount;
472
Steve Block29357bc2012-01-06 19:20:56 +0000473 // ALOGE("buffer done, new input index %d", inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700474
475 mX0L = mBuffer.i16[mBuffer.frameCount-1];
476 provider->releaseBuffer(&mBuffer);
477
478 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000479 // ALOG_ASSERT(mBuffer.frameCount == 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700480 }
481 }
482
Glenn Kasten90bebef2012-01-27 15:24:38 -0800483 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700484
485resampleMono16_exit:
486 // save state
487 mInputIndex = inputIndex;
488 mPhaseFraction = phaseFraction;
489}
490
491#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
492
493/*******************************************************************
494*
495* AsmMono16Loop
496* asm optimized monotonic loop version; one loop is 2 frames
497* Input:
498* in : pointer on input samples
499* maxOutPt : pointer on first not filled
500* maxInIdx : index on first not used
501* outputIndex : pointer on current output index
502* out : pointer on output buffer
503* inputIndex : pointer on current input index
504* vl, vr : left and right gain
505* phaseFraction : pointer on current phase fraction
506* phaseIncrement
507* Ouput:
508* outputIndex :
509* out : updated buffer
510* inputIndex : index of next to use
511* phaseFraction : phase fraction for next interpolation
512*
513*******************************************************************/
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800514__attribute__((noinline))
Mathias Agopian65ab4712010-07-14 17:59:35 -0700515void AudioResamplerOrder1::AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
516 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
517 uint32_t &phaseFraction, uint32_t phaseIncrement)
518{
519#define MO_PARAM5 "36" // offset of parameter 5 (outputIndex)
520
521 asm(
522 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"
523 // get parameters
524 " ldr r6, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
525 " ldr r6, [r6]\n" // phaseFraction
526 " ldr r7, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
527 " ldr r7, [r7]\n" // inputIndex
528 " ldr r8, [sp, #" MO_PARAM5 " + 4]\n" // out
529 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
530 " ldr r0, [r0]\n" // outputIndex
531 " add r8, r0, asl #2\n" // curOut
532 " ldr r9, [sp, #" MO_PARAM5 " + 24]\n" // phaseIncrement
533 " ldr r10, [sp, #" MO_PARAM5 " + 12]\n" // vl
534 " ldr r11, [sp, #" MO_PARAM5 " + 16]\n" // vr
535
536 // r0 pin, x0, Samp
537
538 // r1 in
539 // r2 maxOutPt
540 // r3 maxInIdx
541
542 // r4 x1, i1, i3, Out1
543 // r5 out0
544
545 // r6 frac
546 // r7 inputIndex
547 // r8 curOut
548
549 // r9 inc
550 // r10 vl
551 // r11 vr
552
553 // r12
554 // r13 sp
555 // r14
556
557 // the following loop works on 2 frames
558
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700559 "1:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700561 " bcs 2f\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562
563#define MO_ONE_FRAME \
564 " add r0, r1, r7, asl #1\n" /* in + inputIndex */\
565 " ldrsh r4, [r0]\n" /* in[inputIndex] */\
566 " ldr r5, [r8]\n" /* out[outputIndex] */\
567 " ldrsh r0, [r0, #-2]\n" /* in[inputIndex-1] */\
568 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
569 " sub r4, r4, r0\n" /* in[inputIndex] - in[inputIndex-1] */\
570 " mov r4, r4, lsl #2\n" /* <<2 */\
571 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
572 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
573 " add r0, r0, r4\n" /* x0 - (..) */\
574 " mla r5, r0, r10, r5\n" /* vl*interp + out[] */\
575 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
576 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
577 " mla r4, r0, r11, r4\n" /* vr*interp + out[] */\
578 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */\
579 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */
580
581 MO_ONE_FRAME // frame 1
582 MO_ONE_FRAME // frame 2
583
584 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700585 " bcc 1b\n"
586 "2:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587
588 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
589 // save modified values
590 " ldr r0, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
591 " str r6, [r0]\n" // phaseFraction
592 " ldr r0, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
593 " str r7, [r0]\n" // inputIndex
594 " ldr r0, [sp, #" MO_PARAM5 " + 4]\n" // out
595 " sub r8, r0\n" // curOut - out
596 " asr r8, #2\n" // new outputIndex
597 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
598 " str r8, [r0]\n" // save outputIndex
599
600 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, pc}\n"
601 );
602}
603
604/*******************************************************************
605*
606* AsmStereo16Loop
607* asm optimized stereo loop version; one loop is 2 frames
608* Input:
609* in : pointer on input samples
610* maxOutPt : pointer on first not filled
611* maxInIdx : index on first not used
612* outputIndex : pointer on current output index
613* out : pointer on output buffer
614* inputIndex : pointer on current input index
615* vl, vr : left and right gain
616* phaseFraction : pointer on current phase fraction
617* phaseIncrement
618* Ouput:
619* outputIndex :
620* out : updated buffer
621* inputIndex : index of next to use
622* phaseFraction : phase fraction for next interpolation
623*
624*******************************************************************/
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800625__attribute__((noinline))
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626void AudioResamplerOrder1::AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
627 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
628 uint32_t &phaseFraction, uint32_t phaseIncrement)
629{
630#define ST_PARAM5 "40" // offset of parameter 5 (outputIndex)
631 asm(
632 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}\n"
633 // get parameters
634 " ldr r6, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
635 " ldr r6, [r6]\n" // phaseFraction
636 " ldr r7, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
637 " ldr r7, [r7]\n" // inputIndex
638 " ldr r8, [sp, #" ST_PARAM5 " + 4]\n" // out
639 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
640 " ldr r0, [r0]\n" // outputIndex
641 " add r8, r0, asl #2\n" // curOut
642 " ldr r9, [sp, #" ST_PARAM5 " + 24]\n" // phaseIncrement
643 " ldr r10, [sp, #" ST_PARAM5 " + 12]\n" // vl
644 " ldr r11, [sp, #" ST_PARAM5 " + 16]\n" // vr
645
646 // r0 pin, x0, Samp
647
648 // r1 in
649 // r2 maxOutPt
650 // r3 maxInIdx
651
652 // r4 x1, i1, i3, out1
653 // r5 out0
654
655 // r6 frac
656 // r7 inputIndex
657 // r8 curOut
658
659 // r9 inc
660 // r10 vl
661 // r11 vr
662
663 // r12 temporary
664 // r13 sp
665 // r14
666
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700667 "3:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700669 " bcs 4f\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670
671#define ST_ONE_FRAME \
672 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
673\
674 " add r0, r1, r7, asl #2\n" /* in + 2*inputIndex */\
675\
676 " ldrsh r4, [r0]\n" /* in[2*inputIndex] */\
677 " ldr r5, [r8]\n" /* out[outputIndex] */\
678 " ldrsh r12, [r0, #-4]\n" /* in[2*inputIndex-2] */\
679 " sub r4, r4, r12\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
680 " mov r4, r4, lsl #2\n" /* <<2 */\
681 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
682 " add r12, r12, r4\n" /* x0 - (..) */\
683 " mla r5, r12, r10, r5\n" /* vl*interp + out[] */\
684 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
685 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
686\
687 " ldrsh r12, [r0, #+2]\n" /* in[2*inputIndex+1] */\
688 " ldrsh r0, [r0, #-2]\n" /* in[2*inputIndex-1] */\
689 " sub r12, r12, r0\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
690 " mov r12, r12, lsl #2\n" /* <<2 */\
691 " smulwt r12, r12, r6\n" /* (x1-x0)*.. */\
692 " add r12, r0, r12\n" /* x0 - (..) */\
693 " mla r4, r12, r11, r4\n" /* vr*interp + out[] */\
694 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */\
695\
696 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
697 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */
698
699 ST_ONE_FRAME // frame 1
700 ST_ONE_FRAME // frame 1
701
702 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700703 " bcc 3b\n"
704 "4:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705
706 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
707 // save modified values
708 " ldr r0, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
709 " str r6, [r0]\n" // phaseFraction
710 " ldr r0, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
711 " str r7, [r0]\n" // inputIndex
712 " ldr r0, [sp, #" ST_PARAM5 " + 4]\n" // out
713 " sub r8, r0\n" // curOut - out
714 " asr r8, #2\n" // new outputIndex
715 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
716 " str r8, [r0]\n" // save outputIndex
717
718 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, pc}\n"
719 );
720}
721
722#endif // ASM_ARM_RESAMP1
723
724
725// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800727} // namespace android