blob: 48c189c0bf80a06172d0e612077eecf2cb15b0c3 [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() {}
Vivek Singh395b6a12013-07-05 11:59:38 +053055 void reset();
Mathias Agopian65ab4712010-07-14 17:59:35 -070056 void resampleMono16(int32_t* out, size_t outFrameCount,
57 AudioBufferProvider* provider);
58 void resampleStereo16(int32_t* out, size_t outFrameCount,
59 AudioBufferProvider* provider);
60#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
61 void AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
62 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
63 uint32_t &phaseFraction, uint32_t phaseIncrement);
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,
66 uint32_t &phaseFraction, uint32_t phaseIncrement);
67#endif // ASM_ARM_RESAMP1
68
69 static inline int32_t Interp(int32_t x0, int32_t x1, uint32_t f) {
70 return x0 + (((x1 - x0) * (int32_t)(f >> kPreInterpShift)) >> kNumInterpBits);
71 }
72 static inline void Advance(size_t* index, uint32_t* frac, uint32_t inc) {
73 *frac += inc;
74 *index += (size_t)(*frac >> kNumPhaseBits);
75 *frac &= kPhaseMask;
76 }
77 int mX0L;
78 int mX0R;
79};
80
Glenn Kastenac602052012-10-01 14:04:31 -070081bool AudioResampler::qualityIsSupported(src_quality quality)
82{
83 switch (quality) {
84 case DEFAULT_QUALITY:
85 case LOW_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070086 case MED_QUALITY:
87 case HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070088 case VERY_HIGH_QUALITY:
89 return true;
90 default:
91 return false;
92 }
93}
94
Mathias Agopian65ab4712010-07-14 17:59:35 -070095// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070096
Glenn Kastenac602052012-10-01 14:04:31 -070097static pthread_once_t once_control = PTHREAD_ONCE_INIT;
98static AudioResampler::src_quality defaultQuality = AudioResampler::DEFAULT_QUALITY;
Mathias Agopian65ab4712010-07-14 17:59:35 -070099
Glenn Kastenac602052012-10-01 14:04:31 -0700100void AudioResampler::init_routine()
101{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102 char value[PROPERTY_VALUE_MAX];
Glenn Kastenac602052012-10-01 14:04:31 -0700103 if (property_get("af.resampler.quality", value, NULL) > 0) {
104 char *endptr;
105 unsigned long l = strtoul(value, &endptr, 0);
106 if (*endptr == '\0') {
107 defaultQuality = (src_quality) l;
108 ALOGD("forcing AudioResampler quality to %d", defaultQuality);
109 if (defaultQuality < DEFAULT_QUALITY || defaultQuality > VERY_HIGH_QUALITY) {
110 defaultQuality = DEFAULT_QUALITY;
111 }
112 }
113 }
114}
115
116uint32_t AudioResampler::qualityMHz(src_quality quality)
117{
118 switch (quality) {
119 default:
120 case DEFAULT_QUALITY:
121 case LOW_QUALITY:
122 return 3;
123 case MED_QUALITY:
124 return 6;
125 case HIGH_QUALITY:
126 return 20;
127 case VERY_HIGH_QUALITY:
128 return 34;
129 }
130}
131
Glenn Kastenc4640c92012-10-22 17:09:27 -0700132static const uint32_t maxMHz = 130; // an arbitrary number that permits 3 VHQ, should be tunable
Glenn Kastenac602052012-10-01 14:04:31 -0700133static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
134static uint32_t currentMHz = 0;
135
136AudioResampler* AudioResampler::create(int bitDepth, int inChannelCount,
137 int32_t sampleRate, src_quality quality) {
138
139 bool atFinalQuality;
140 if (quality == DEFAULT_QUALITY) {
141 // read the resampler default quality property the first time it is needed
142 int ok = pthread_once(&once_control, init_routine);
143 if (ok != 0) {
144 ALOGE("%s pthread_once failed: %d", __func__, ok);
145 }
146 quality = defaultQuality;
147 atFinalQuality = false;
148 } else {
149 atFinalQuality = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700150 }
151
Glenn Kastenac602052012-10-01 14:04:31 -0700152 // naive implementation of CPU load throttling doesn't account for whether resampler is active
153 pthread_mutex_lock(&mutex);
154 for (;;) {
155 uint32_t deltaMHz = qualityMHz(quality);
156 uint32_t newMHz = currentMHz + deltaMHz;
157 if ((qualityIsSupported(quality) && newMHz <= maxMHz) || atFinalQuality) {
158 ALOGV("resampler load %u -> %u MHz due to delta +%u MHz from quality %d",
159 currentMHz, newMHz, deltaMHz, quality);
160 currentMHz = newMHz;
161 break;
162 }
163 // not enough CPU available for proposed quality level, so try next lowest level
164 switch (quality) {
165 default:
166 case DEFAULT_QUALITY:
167 case LOW_QUALITY:
168 atFinalQuality = true;
169 break;
170 case MED_QUALITY:
171 quality = LOW_QUALITY;
172 break;
173 case HIGH_QUALITY:
174 quality = MED_QUALITY;
175 break;
176 case VERY_HIGH_QUALITY:
177 quality = HIGH_QUALITY;
178 break;
179 }
180 }
181 pthread_mutex_unlock(&mutex);
182
183 AudioResampler* resampler;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700184
185 switch (quality) {
186 default:
Glenn Kastenac602052012-10-01 14:04:31 -0700187 case DEFAULT_QUALITY:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700188 case LOW_QUALITY:
Steve Block3856b092011-10-20 11:56:00 +0100189 ALOGV("Create linear Resampler");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190 resampler = new AudioResamplerOrder1(bitDepth, inChannelCount, sampleRate);
191 break;
192 case MED_QUALITY:
Steve Block3856b092011-10-20 11:56:00 +0100193 ALOGV("Create cubic Resampler");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194 resampler = new AudioResamplerCubic(bitDepth, inChannelCount, sampleRate);
195 break;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800196 case HIGH_QUALITY:
197 ALOGV("Create HIGH_QUALITY sinc Resampler");
198 resampler = new AudioResamplerSinc(bitDepth, inChannelCount, sampleRate);
Glenn Kastenac602052012-10-01 14:04:31 -0700199 break;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800200 case VERY_HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -0700201 ALOGV("Create VERY_HIGH_QUALITY sinc Resampler = %d", quality);
SathishKumar Mani76b11162012-01-17 10:49:47 -0800202 resampler = new AudioResamplerSinc(bitDepth, inChannelCount, sampleRate, quality);
203 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700204 }
205
206 // initialize resampler
207 resampler->init();
208 return resampler;
209}
210
211AudioResampler::AudioResampler(int bitDepth, int inChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -0700212 int32_t sampleRate, src_quality quality) :
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213 mBitDepth(bitDepth), mChannelCount(inChannelCount),
214 mSampleRate(sampleRate), mInSampleRate(sampleRate), mInputIndex(0),
John Grossman4ff14ba2012-02-08 16:37:41 -0800215 mPhaseFraction(0), mLocalTimeFreq(0),
Glenn Kastenac602052012-10-01 14:04:31 -0700216 mPTS(AudioBufferProvider::kInvalidPTS), mQuality(quality) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 // sanity check on format
218 if ((bitDepth != 16) ||(inChannelCount < 1) || (inChannelCount > 2)) {
Steve Block29357bc2012-01-06 19:20:56 +0000219 ALOGE("Unsupported sample format, %d bits, %d channels", bitDepth,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220 inChannelCount);
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000221 // ALOG_ASSERT(0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700222 }
Glenn Kastenac602052012-10-01 14:04:31 -0700223 if (sampleRate <= 0) {
224 ALOGE("Unsupported sample rate %d Hz", sampleRate);
225 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226
227 // initialize common members
228 mVolume[0] = mVolume[1] = 0;
229 mBuffer.frameCount = 0;
230
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231}
232
233AudioResampler::~AudioResampler() {
Glenn Kastenac602052012-10-01 14:04:31 -0700234 pthread_mutex_lock(&mutex);
235 src_quality quality = getQuality();
236 uint32_t deltaMHz = qualityMHz(quality);
237 int32_t newMHz = currentMHz - deltaMHz;
238 ALOGV("resampler load %u -> %d MHz due to delta -%u MHz from quality %d",
239 currentMHz, newMHz, deltaMHz, quality);
240 LOG_ALWAYS_FATAL_IF(newMHz < 0, "negative resampler load %d MHz", newMHz);
241 currentMHz = newMHz;
242 pthread_mutex_unlock(&mutex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243}
244
245void AudioResampler::setSampleRate(int32_t inSampleRate) {
246 mInSampleRate = inSampleRate;
247 mPhaseIncrement = (uint32_t)((kPhaseMultiplier * inSampleRate) / mSampleRate);
248}
249
250void AudioResampler::setVolume(int16_t left, int16_t right) {
251 // TODO: Implement anti-zipper filter
252 mVolume[0] = left;
253 mVolume[1] = right;
254}
255
John Grossman4ff14ba2012-02-08 16:37:41 -0800256void AudioResampler::setLocalTimeFreq(uint64_t freq) {
257 mLocalTimeFreq = freq;
258}
259
260void AudioResampler::setPTS(int64_t pts) {
261 mPTS = pts;
262}
263
264int64_t AudioResampler::calculateOutputPTS(int outputFrameIndex) {
265
266 if (mPTS == AudioBufferProvider::kInvalidPTS) {
267 return AudioBufferProvider::kInvalidPTS;
268 } else {
269 return mPTS + ((outputFrameIndex * mLocalTimeFreq) / mSampleRate);
270 }
271}
272
Eric Laurent243f5f92011-02-28 16:52:51 -0800273void AudioResampler::reset() {
274 mInputIndex = 0;
275 mPhaseFraction = 0;
276 mBuffer.frameCount = 0;
277}
278
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279// ----------------------------------------------------------------------------
280
281void AudioResamplerOrder1::resample(int32_t* out, size_t outFrameCount,
282 AudioBufferProvider* provider) {
283
284 // should never happen, but we overflow if it does
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000285 // ALOG_ASSERT(outFrameCount < 32767);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286
287 // select the appropriate resampler
288 switch (mChannelCount) {
289 case 1:
290 resampleMono16(out, outFrameCount, provider);
291 break;
292 case 2:
293 resampleStereo16(out, outFrameCount, provider);
294 break;
295 }
296}
297
Vivek Singh395b6a12013-07-05 11:59:38 +0530298void AudioResamplerOrder1::reset() {
299 mInputIndex = 0;
300 mPhaseFraction = 0;
301 mBuffer.frameCount = 0;
302 mX0L = 0;
303 mX0R = 0;
304}
305
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306void AudioResamplerOrder1::resampleStereo16(int32_t* out, size_t outFrameCount,
307 AudioBufferProvider* provider) {
308
309 int32_t vl = mVolume[0];
310 int32_t vr = mVolume[1];
311
312 size_t inputIndex = mInputIndex;
313 uint32_t phaseFraction = mPhaseFraction;
314 uint32_t phaseIncrement = mPhaseIncrement;
315 size_t outputIndex = 0;
316 size_t outputSampleCount = outFrameCount * 2;
317 size_t inFrameCount = (outFrameCount*mInSampleRate)/mSampleRate;
318
Glenn Kasten90bebef2012-01-27 15:24:38 -0800319 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
321
322 while (outputIndex < outputSampleCount) {
323
324 // buffer is empty, fetch a new one
325 while (mBuffer.frameCount == 0) {
326 mBuffer.frameCount = inFrameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -0800327 provider->getNextBuffer(&mBuffer,
328 calculateOutputPTS(outputIndex / 2));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329 if (mBuffer.raw == NULL) {
330 goto resampleStereo16_exit;
331 }
332
Glenn Kasten90bebef2012-01-27 15:24:38 -0800333 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334 if (mBuffer.frameCount > inputIndex) break;
335
336 inputIndex -= mBuffer.frameCount;
337 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
338 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
339 provider->releaseBuffer(&mBuffer);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700340 // mBuffer.frameCount == 0 now so we reload a new buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341 }
342
343 int16_t *in = mBuffer.i16;
344
345 // handle boundary case
346 while (inputIndex == 0) {
Glenn Kasten90bebef2012-01-27 15:24:38 -0800347 // ALOGE("boundary case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 out[outputIndex++] += vl * Interp(mX0L, in[0], phaseFraction);
349 out[outputIndex++] += vr * Interp(mX0R, in[1], phaseFraction);
350 Advance(&inputIndex, &phaseFraction, phaseIncrement);
351 if (outputIndex == outputSampleCount)
352 break;
353 }
354
355 // process input samples
Glenn Kasten90bebef2012-01-27 15:24:38 -0800356 // ALOGE("general case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357
358#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
359 if (inputIndex + 2 < mBuffer.frameCount) {
360 int32_t* maxOutPt;
361 int32_t maxInIdx;
362
363 maxOutPt = out + (outputSampleCount - 2); // 2 because 2 frames per loop
364 maxInIdx = mBuffer.frameCount - 2;
365 AsmStereo16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
366 phaseFraction, phaseIncrement);
367 }
368#endif // ASM_ARM_RESAMP1
369
370 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
371 out[outputIndex++] += vl * Interp(in[inputIndex*2-2],
372 in[inputIndex*2], phaseFraction);
373 out[outputIndex++] += vr * Interp(in[inputIndex*2-1],
374 in[inputIndex*2+1], phaseFraction);
375 Advance(&inputIndex, &phaseFraction, phaseIncrement);
376 }
377
Glenn Kasten90bebef2012-01-27 15:24:38 -0800378 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379
380 // if done with buffer, save samples
381 if (inputIndex >= mBuffer.frameCount) {
382 inputIndex -= mBuffer.frameCount;
383
Steve Block29357bc2012-01-06 19:20:56 +0000384 // ALOGE("buffer done, new input index %d", inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385
386 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
387 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
388 provider->releaseBuffer(&mBuffer);
389
390 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000391 // ALOG_ASSERT(mBuffer.frameCount == 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392 }
393 }
394
Glenn Kasten90bebef2012-01-27 15:24:38 -0800395 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396
397resampleStereo16_exit:
398 // save state
399 mInputIndex = inputIndex;
400 mPhaseFraction = phaseFraction;
401}
402
403void AudioResamplerOrder1::resampleMono16(int32_t* out, size_t outFrameCount,
404 AudioBufferProvider* provider) {
405
406 int32_t vl = mVolume[0];
407 int32_t vr = mVolume[1];
408
409 size_t inputIndex = mInputIndex;
410 uint32_t phaseFraction = mPhaseFraction;
411 uint32_t phaseIncrement = mPhaseIncrement;
412 size_t outputIndex = 0;
413 size_t outputSampleCount = outFrameCount * 2;
414 size_t inFrameCount = (outFrameCount*mInSampleRate)/mSampleRate;
415
Glenn Kasten90bebef2012-01-27 15:24:38 -0800416 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700417 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
418 while (outputIndex < outputSampleCount) {
419 // buffer is empty, fetch a new one
420 while (mBuffer.frameCount == 0) {
421 mBuffer.frameCount = inFrameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -0800422 provider->getNextBuffer(&mBuffer,
423 calculateOutputPTS(outputIndex / 2));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424 if (mBuffer.raw == NULL) {
425 mInputIndex = inputIndex;
426 mPhaseFraction = phaseFraction;
427 goto resampleMono16_exit;
428 }
Glenn Kasten90bebef2012-01-27 15:24:38 -0800429 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430 if (mBuffer.frameCount > inputIndex) break;
431
432 inputIndex -= mBuffer.frameCount;
433 mX0L = mBuffer.i16[mBuffer.frameCount-1];
434 provider->releaseBuffer(&mBuffer);
435 // mBuffer.frameCount == 0 now so we reload a new buffer
436 }
437 int16_t *in = mBuffer.i16;
438
439 // handle boundary case
440 while (inputIndex == 0) {
Glenn Kasten90bebef2012-01-27 15:24:38 -0800441 // ALOGE("boundary case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442 int32_t sample = Interp(mX0L, in[0], phaseFraction);
443 out[outputIndex++] += vl * sample;
444 out[outputIndex++] += vr * sample;
445 Advance(&inputIndex, &phaseFraction, phaseIncrement);
446 if (outputIndex == outputSampleCount)
447 break;
448 }
449
450 // process input samples
Glenn Kasten90bebef2012-01-27 15:24:38 -0800451 // ALOGE("general case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452
453#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
454 if (inputIndex + 2 < mBuffer.frameCount) {
455 int32_t* maxOutPt;
456 int32_t maxInIdx;
457
458 maxOutPt = out + (outputSampleCount - 2);
459 maxInIdx = (int32_t)mBuffer.frameCount - 2;
460 AsmMono16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
461 phaseFraction, phaseIncrement);
462 }
463#endif // ASM_ARM_RESAMP1
464
465 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
466 int32_t sample = Interp(in[inputIndex-1], in[inputIndex],
467 phaseFraction);
468 out[outputIndex++] += vl * sample;
469 out[outputIndex++] += vr * sample;
470 Advance(&inputIndex, &phaseFraction, phaseIncrement);
471 }
472
473
Glenn Kasten90bebef2012-01-27 15:24:38 -0800474 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475
476 // if done with buffer, save samples
477 if (inputIndex >= mBuffer.frameCount) {
478 inputIndex -= mBuffer.frameCount;
479
Steve Block29357bc2012-01-06 19:20:56 +0000480 // ALOGE("buffer done, new input index %d", inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700481
482 mX0L = mBuffer.i16[mBuffer.frameCount-1];
483 provider->releaseBuffer(&mBuffer);
484
485 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000486 // ALOG_ASSERT(mBuffer.frameCount == 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700487 }
488 }
489
Glenn Kasten90bebef2012-01-27 15:24:38 -0800490 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491
492resampleMono16_exit:
493 // save state
494 mInputIndex = inputIndex;
495 mPhaseFraction = phaseFraction;
496}
497
498#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
499
500/*******************************************************************
501*
502* AsmMono16Loop
503* asm optimized monotonic loop version; one loop is 2 frames
504* Input:
505* in : pointer on input samples
506* maxOutPt : pointer on first not filled
507* maxInIdx : index on first not used
508* outputIndex : pointer on current output index
509* out : pointer on output buffer
510* inputIndex : pointer on current input index
511* vl, vr : left and right gain
512* phaseFraction : pointer on current phase fraction
513* phaseIncrement
514* Ouput:
515* outputIndex :
516* out : updated buffer
517* inputIndex : index of next to use
518* phaseFraction : phase fraction for next interpolation
519*
520*******************************************************************/
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800521__attribute__((noinline))
Mathias Agopian65ab4712010-07-14 17:59:35 -0700522void AudioResamplerOrder1::AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
523 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
524 uint32_t &phaseFraction, uint32_t phaseIncrement)
525{
526#define MO_PARAM5 "36" // offset of parameter 5 (outputIndex)
527
528 asm(
529 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"
530 // get parameters
531 " ldr r6, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
532 " ldr r6, [r6]\n" // phaseFraction
533 " ldr r7, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
534 " ldr r7, [r7]\n" // inputIndex
535 " ldr r8, [sp, #" MO_PARAM5 " + 4]\n" // out
536 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
537 " ldr r0, [r0]\n" // outputIndex
538 " add r8, r0, asl #2\n" // curOut
539 " ldr r9, [sp, #" MO_PARAM5 " + 24]\n" // phaseIncrement
540 " ldr r10, [sp, #" MO_PARAM5 " + 12]\n" // vl
541 " ldr r11, [sp, #" MO_PARAM5 " + 16]\n" // vr
542
543 // r0 pin, x0, Samp
544
545 // r1 in
546 // r2 maxOutPt
547 // r3 maxInIdx
548
549 // r4 x1, i1, i3, Out1
550 // r5 out0
551
552 // r6 frac
553 // r7 inputIndex
554 // r8 curOut
555
556 // r9 inc
557 // r10 vl
558 // r11 vr
559
560 // r12
561 // r13 sp
562 // r14
563
564 // the following loop works on 2 frames
565
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700566 "1:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700568 " bcs 2f\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569
570#define MO_ONE_FRAME \
571 " add r0, r1, r7, asl #1\n" /* in + inputIndex */\
572 " ldrsh r4, [r0]\n" /* in[inputIndex] */\
573 " ldr r5, [r8]\n" /* out[outputIndex] */\
574 " ldrsh r0, [r0, #-2]\n" /* in[inputIndex-1] */\
575 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
576 " sub r4, r4, r0\n" /* in[inputIndex] - in[inputIndex-1] */\
577 " mov r4, r4, lsl #2\n" /* <<2 */\
578 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
579 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
580 " add r0, r0, r4\n" /* x0 - (..) */\
581 " mla r5, r0, r10, r5\n" /* vl*interp + out[] */\
582 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
583 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
584 " mla r4, r0, r11, r4\n" /* vr*interp + out[] */\
585 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */\
586 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */
587
588 MO_ONE_FRAME // frame 1
589 MO_ONE_FRAME // frame 2
590
591 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700592 " bcc 1b\n"
593 "2:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594
595 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
596 // save modified values
597 " ldr r0, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
598 " str r6, [r0]\n" // phaseFraction
599 " ldr r0, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
600 " str r7, [r0]\n" // inputIndex
601 " ldr r0, [sp, #" MO_PARAM5 " + 4]\n" // out
602 " sub r8, r0\n" // curOut - out
603 " asr r8, #2\n" // new outputIndex
604 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
605 " str r8, [r0]\n" // save outputIndex
606
607 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, pc}\n"
608 );
609}
610
611/*******************************************************************
612*
613* AsmStereo16Loop
614* asm optimized stereo loop version; one loop is 2 frames
615* Input:
616* in : pointer on input samples
617* maxOutPt : pointer on first not filled
618* maxInIdx : index on first not used
619* outputIndex : pointer on current output index
620* out : pointer on output buffer
621* inputIndex : pointer on current input index
622* vl, vr : left and right gain
623* phaseFraction : pointer on current phase fraction
624* phaseIncrement
625* Ouput:
626* outputIndex :
627* out : updated buffer
628* inputIndex : index of next to use
629* phaseFraction : phase fraction for next interpolation
630*
631*******************************************************************/
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800632__attribute__((noinline))
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633void AudioResamplerOrder1::AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
634 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
635 uint32_t &phaseFraction, uint32_t phaseIncrement)
636{
637#define ST_PARAM5 "40" // offset of parameter 5 (outputIndex)
638 asm(
639 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}\n"
640 // get parameters
641 " ldr r6, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
642 " ldr r6, [r6]\n" // phaseFraction
643 " ldr r7, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
644 " ldr r7, [r7]\n" // inputIndex
645 " ldr r8, [sp, #" ST_PARAM5 " + 4]\n" // out
646 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
647 " ldr r0, [r0]\n" // outputIndex
648 " add r8, r0, asl #2\n" // curOut
649 " ldr r9, [sp, #" ST_PARAM5 " + 24]\n" // phaseIncrement
650 " ldr r10, [sp, #" ST_PARAM5 " + 12]\n" // vl
651 " ldr r11, [sp, #" ST_PARAM5 " + 16]\n" // vr
652
653 // r0 pin, x0, Samp
654
655 // r1 in
656 // r2 maxOutPt
657 // r3 maxInIdx
658
659 // r4 x1, i1, i3, out1
660 // r5 out0
661
662 // r6 frac
663 // r7 inputIndex
664 // r8 curOut
665
666 // r9 inc
667 // r10 vl
668 // r11 vr
669
670 // r12 temporary
671 // r13 sp
672 // r14
673
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700674 "3:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700676 " bcs 4f\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677
678#define ST_ONE_FRAME \
679 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
680\
681 " add r0, r1, r7, asl #2\n" /* in + 2*inputIndex */\
682\
683 " ldrsh r4, [r0]\n" /* in[2*inputIndex] */\
684 " ldr r5, [r8]\n" /* out[outputIndex] */\
685 " ldrsh r12, [r0, #-4]\n" /* in[2*inputIndex-2] */\
686 " sub r4, r4, r12\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
687 " mov r4, r4, lsl #2\n" /* <<2 */\
688 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
689 " add r12, r12, r4\n" /* x0 - (..) */\
690 " mla r5, r12, r10, r5\n" /* vl*interp + out[] */\
691 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
692 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
693\
694 " ldrsh r12, [r0, #+2]\n" /* in[2*inputIndex+1] */\
695 " ldrsh r0, [r0, #-2]\n" /* in[2*inputIndex-1] */\
696 " sub r12, r12, r0\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
697 " mov r12, r12, lsl #2\n" /* <<2 */\
698 " smulwt r12, r12, r6\n" /* (x1-x0)*.. */\
699 " add r12, r0, r12\n" /* x0 - (..) */\
700 " mla r4, r12, r11, r4\n" /* vr*interp + out[] */\
701 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */\
702\
703 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
704 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */
705
706 ST_ONE_FRAME // frame 1
707 ST_ONE_FRAME // frame 1
708
709 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700710 " bcc 3b\n"
711 "4:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712
713 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
714 // save modified values
715 " ldr r0, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
716 " str r6, [r0]\n" // phaseFraction
717 " ldr r0, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
718 " str r7, [r0]\n" // inputIndex
719 " ldr r0, [sp, #" ST_PARAM5 " + 4]\n" // out
720 " sub r8, r0\n" // curOut - out
721 " asr r8, #2\n" // new outputIndex
722 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
723 " str r8, [r0]\n" // save outputIndex
724
725 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, pc}\n"
726 );
727}
728
729#endif // ASM_ARM_RESAMP1
730
731
732// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700733
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800734} // namespace android