blob: 24ff55d8f7a49aff39f9712b6bb4781c307e260a [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"
Andy Hung86eae0e2013-12-09 12:12:46 -080028#include "AudioResamplerDyn.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070029
Jim Huang0c0a1c02011-04-06 14:19:29 +080030#ifdef __arm__
31#include <machine/cpu-features.h>
32#endif
33
Mathias Agopian65ab4712010-07-14 17:59:35 -070034namespace android {
35
Jim Huang0c0a1c02011-04-06 14:19:29 +080036#ifdef __ARM_HAVE_HALFWORD_MULTIPLY // optimized asm option
Glenn Kastenc23e2f22011-11-17 13:27:22 -080037 #define ASM_ARM_RESAMP1 // enable asm optimisation for ResamplerOrder1
Jim Huang0c0a1c02011-04-06 14:19:29 +080038#endif // __ARM_HAVE_HALFWORD_MULTIPLY
Mathias Agopian65ab4712010-07-14 17:59:35 -070039// ----------------------------------------------------------------------------
40
41class AudioResamplerOrder1 : public AudioResampler {
42public:
43 AudioResamplerOrder1(int bitDepth, int inChannelCount, int32_t sampleRate) :
Glenn Kastenac602052012-10-01 14:04:31 -070044 AudioResampler(bitDepth, inChannelCount, sampleRate, LOW_QUALITY), mX0L(0), mX0R(0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -070045 }
46 virtual void resample(int32_t* out, size_t outFrameCount,
47 AudioBufferProvider* provider);
48private:
49 // number of bits used in interpolation multiply - 15 bits avoids overflow
50 static const int kNumInterpBits = 15;
51
52 // bits to shift the phase fraction down to avoid overflow
53 static const int kPreInterpShift = kNumPhaseBits - kNumInterpBits;
54
55 void init() {}
56 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:
Andy Hung86eae0e2013-12-09 12:12:46 -080089 case DYN_LOW_QUALITY:
90 case DYN_MED_QUALITY:
91 case DYN_HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070092 return true;
93 default:
94 return false;
95 }
96}
97
Mathias Agopian65ab4712010-07-14 17:59:35 -070098// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070099
Glenn Kastenac602052012-10-01 14:04:31 -0700100static pthread_once_t once_control = PTHREAD_ONCE_INIT;
101static AudioResampler::src_quality defaultQuality = AudioResampler::DEFAULT_QUALITY;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102
Glenn Kastenac602052012-10-01 14:04:31 -0700103void AudioResampler::init_routine()
104{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700105 char value[PROPERTY_VALUE_MAX];
Glenn Kastenac602052012-10-01 14:04:31 -0700106 if (property_get("af.resampler.quality", value, NULL) > 0) {
107 char *endptr;
108 unsigned long l = strtoul(value, &endptr, 0);
109 if (*endptr == '\0') {
110 defaultQuality = (src_quality) l;
111 ALOGD("forcing AudioResampler quality to %d", defaultQuality);
Andy Hung86eae0e2013-12-09 12:12:46 -0800112 if (defaultQuality < DEFAULT_QUALITY || defaultQuality > DYN_HIGH_QUALITY) {
Glenn Kastenac602052012-10-01 14:04:31 -0700113 defaultQuality = DEFAULT_QUALITY;
114 }
115 }
116 }
117}
118
119uint32_t AudioResampler::qualityMHz(src_quality quality)
120{
121 switch (quality) {
122 default:
123 case DEFAULT_QUALITY:
124 case LOW_QUALITY:
125 return 3;
126 case MED_QUALITY:
127 return 6;
128 case HIGH_QUALITY:
129 return 20;
130 case VERY_HIGH_QUALITY:
131 return 34;
Andy Hung86eae0e2013-12-09 12:12:46 -0800132 case DYN_LOW_QUALITY:
133 return 4;
134 case DYN_MED_QUALITY:
135 return 6;
136 case DYN_HIGH_QUALITY:
137 return 12;
Glenn Kastenac602052012-10-01 14:04:31 -0700138 }
139}
140
Glenn Kastenc4640c92012-10-22 17:09:27 -0700141static const uint32_t maxMHz = 130; // an arbitrary number that permits 3 VHQ, should be tunable
Glenn Kastenac602052012-10-01 14:04:31 -0700142static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
143static uint32_t currentMHz = 0;
144
145AudioResampler* AudioResampler::create(int bitDepth, int inChannelCount,
146 int32_t sampleRate, src_quality quality) {
147
148 bool atFinalQuality;
149 if (quality == DEFAULT_QUALITY) {
150 // read the resampler default quality property the first time it is needed
151 int ok = pthread_once(&once_control, init_routine);
152 if (ok != 0) {
153 ALOGE("%s pthread_once failed: %d", __func__, ok);
154 }
155 quality = defaultQuality;
156 atFinalQuality = false;
157 } else {
158 atFinalQuality = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700159 }
160
Glenn Kastenac602052012-10-01 14:04:31 -0700161 // naive implementation of CPU load throttling doesn't account for whether resampler is active
162 pthread_mutex_lock(&mutex);
163 for (;;) {
164 uint32_t deltaMHz = qualityMHz(quality);
165 uint32_t newMHz = currentMHz + deltaMHz;
166 if ((qualityIsSupported(quality) && newMHz <= maxMHz) || atFinalQuality) {
167 ALOGV("resampler load %u -> %u MHz due to delta +%u MHz from quality %d",
168 currentMHz, newMHz, deltaMHz, quality);
169 currentMHz = newMHz;
170 break;
171 }
172 // not enough CPU available for proposed quality level, so try next lowest level
173 switch (quality) {
174 default:
175 case DEFAULT_QUALITY:
176 case LOW_QUALITY:
177 atFinalQuality = true;
178 break;
179 case MED_QUALITY:
180 quality = LOW_QUALITY;
181 break;
182 case HIGH_QUALITY:
183 quality = MED_QUALITY;
184 break;
185 case VERY_HIGH_QUALITY:
186 quality = HIGH_QUALITY;
187 break;
Andy Hung86eae0e2013-12-09 12:12:46 -0800188 case DYN_LOW_QUALITY:
189 atFinalQuality = true;
190 break;
191 case DYN_MED_QUALITY:
192 quality = DYN_LOW_QUALITY;
193 break;
194 case DYN_HIGH_QUALITY:
195 quality = DYN_MED_QUALITY;
196 break;
Glenn Kastenac602052012-10-01 14:04:31 -0700197 }
198 }
199 pthread_mutex_unlock(&mutex);
200
201 AudioResampler* resampler;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202
203 switch (quality) {
204 default:
Glenn Kastenac602052012-10-01 14:04:31 -0700205 case DEFAULT_QUALITY:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700206 case LOW_QUALITY:
Steve Block3856b092011-10-20 11:56:00 +0100207 ALOGV("Create linear Resampler");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208 resampler = new AudioResamplerOrder1(bitDepth, inChannelCount, sampleRate);
209 break;
210 case MED_QUALITY:
Steve Block3856b092011-10-20 11:56:00 +0100211 ALOGV("Create cubic Resampler");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212 resampler = new AudioResamplerCubic(bitDepth, inChannelCount, sampleRate);
213 break;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800214 case HIGH_QUALITY:
215 ALOGV("Create HIGH_QUALITY sinc Resampler");
216 resampler = new AudioResamplerSinc(bitDepth, inChannelCount, sampleRate);
Glenn Kastenac602052012-10-01 14:04:31 -0700217 break;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800218 case VERY_HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -0700219 ALOGV("Create VERY_HIGH_QUALITY sinc Resampler = %d", quality);
SathishKumar Mani76b11162012-01-17 10:49:47 -0800220 resampler = new AudioResamplerSinc(bitDepth, inChannelCount, sampleRate, quality);
221 break;
Andy Hung86eae0e2013-12-09 12:12:46 -0800222 case DYN_LOW_QUALITY:
223 case DYN_MED_QUALITY:
224 case DYN_HIGH_QUALITY:
225 ALOGV("Create dynamic Resampler = %d", quality);
226 resampler = new AudioResamplerDyn(bitDepth, inChannelCount, sampleRate, quality);
227 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228 }
229
230 // initialize resampler
231 resampler->init();
232 return resampler;
233}
234
235AudioResampler::AudioResampler(int bitDepth, int inChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -0700236 int32_t sampleRate, src_quality quality) :
Mathias Agopian65ab4712010-07-14 17:59:35 -0700237 mBitDepth(bitDepth), mChannelCount(inChannelCount),
238 mSampleRate(sampleRate), mInSampleRate(sampleRate), mInputIndex(0),
John Grossman4ff14ba2012-02-08 16:37:41 -0800239 mPhaseFraction(0), mLocalTimeFreq(0),
Glenn Kastenac602052012-10-01 14:04:31 -0700240 mPTS(AudioBufferProvider::kInvalidPTS), mQuality(quality) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700241 // sanity check on format
242 if ((bitDepth != 16) ||(inChannelCount < 1) || (inChannelCount > 2)) {
Steve Block29357bc2012-01-06 19:20:56 +0000243 ALOGE("Unsupported sample format, %d bits, %d channels", bitDepth,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244 inChannelCount);
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000245 // ALOG_ASSERT(0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246 }
Glenn Kastenac602052012-10-01 14:04:31 -0700247 if (sampleRate <= 0) {
248 ALOGE("Unsupported sample rate %d Hz", sampleRate);
249 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700250
251 // initialize common members
252 mVolume[0] = mVolume[1] = 0;
253 mBuffer.frameCount = 0;
254
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255}
256
257AudioResampler::~AudioResampler() {
Glenn Kastenac602052012-10-01 14:04:31 -0700258 pthread_mutex_lock(&mutex);
259 src_quality quality = getQuality();
260 uint32_t deltaMHz = qualityMHz(quality);
261 int32_t newMHz = currentMHz - deltaMHz;
262 ALOGV("resampler load %u -> %d MHz due to delta -%u MHz from quality %d",
263 currentMHz, newMHz, deltaMHz, quality);
264 LOG_ALWAYS_FATAL_IF(newMHz < 0, "negative resampler load %d MHz", newMHz);
265 currentMHz = newMHz;
266 pthread_mutex_unlock(&mutex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267}
268
269void AudioResampler::setSampleRate(int32_t inSampleRate) {
270 mInSampleRate = inSampleRate;
271 mPhaseIncrement = (uint32_t)((kPhaseMultiplier * inSampleRate) / mSampleRate);
272}
273
274void AudioResampler::setVolume(int16_t left, int16_t right) {
275 // TODO: Implement anti-zipper filter
276 mVolume[0] = left;
277 mVolume[1] = right;
278}
279
John Grossman4ff14ba2012-02-08 16:37:41 -0800280void AudioResampler::setLocalTimeFreq(uint64_t freq) {
281 mLocalTimeFreq = freq;
282}
283
284void AudioResampler::setPTS(int64_t pts) {
285 mPTS = pts;
286}
287
288int64_t AudioResampler::calculateOutputPTS(int outputFrameIndex) {
289
290 if (mPTS == AudioBufferProvider::kInvalidPTS) {
291 return AudioBufferProvider::kInvalidPTS;
292 } else {
293 return mPTS + ((outputFrameIndex * mLocalTimeFreq) / mSampleRate);
294 }
295}
296
Eric Laurent243f5f92011-02-28 16:52:51 -0800297void AudioResampler::reset() {
298 mInputIndex = 0;
299 mPhaseFraction = 0;
300 mBuffer.frameCount = 0;
301}
302
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303// ----------------------------------------------------------------------------
304
305void AudioResamplerOrder1::resample(int32_t* out, size_t outFrameCount,
306 AudioBufferProvider* provider) {
307
308 // should never happen, but we overflow if it does
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000309 // ALOG_ASSERT(outFrameCount < 32767);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310
311 // select the appropriate resampler
312 switch (mChannelCount) {
313 case 1:
314 resampleMono16(out, outFrameCount, provider);
315 break;
316 case 2:
317 resampleStereo16(out, outFrameCount, provider);
318 break;
319 }
320}
321
322void AudioResamplerOrder1::resampleStereo16(int32_t* out, size_t outFrameCount,
323 AudioBufferProvider* provider) {
324
325 int32_t vl = mVolume[0];
326 int32_t vr = mVolume[1];
327
328 size_t inputIndex = mInputIndex;
329 uint32_t phaseFraction = mPhaseFraction;
330 uint32_t phaseIncrement = mPhaseIncrement;
331 size_t outputIndex = 0;
332 size_t outputSampleCount = outFrameCount * 2;
333 size_t inFrameCount = (outFrameCount*mInSampleRate)/mSampleRate;
334
Glenn Kasten90bebef2012-01-27 15:24:38 -0800335 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
337
338 while (outputIndex < outputSampleCount) {
339
340 // buffer is empty, fetch a new one
341 while (mBuffer.frameCount == 0) {
342 mBuffer.frameCount = inFrameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -0800343 provider->getNextBuffer(&mBuffer,
344 calculateOutputPTS(outputIndex / 2));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 if (mBuffer.raw == NULL) {
346 goto resampleStereo16_exit;
347 }
348
Glenn Kasten90bebef2012-01-27 15:24:38 -0800349 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350 if (mBuffer.frameCount > inputIndex) break;
351
352 inputIndex -= mBuffer.frameCount;
353 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
354 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
355 provider->releaseBuffer(&mBuffer);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700356 // mBuffer.frameCount == 0 now so we reload a new buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357 }
358
359 int16_t *in = mBuffer.i16;
360
361 // handle boundary case
362 while (inputIndex == 0) {
Glenn Kasten90bebef2012-01-27 15:24:38 -0800363 // ALOGE("boundary case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 out[outputIndex++] += vl * Interp(mX0L, in[0], phaseFraction);
365 out[outputIndex++] += vr * Interp(mX0R, in[1], phaseFraction);
366 Advance(&inputIndex, &phaseFraction, phaseIncrement);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700367 if (outputIndex == outputSampleCount) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368 break;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700369 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 }
371
372 // process input samples
Glenn Kasten90bebef2012-01-27 15:24:38 -0800373 // ALOGE("general case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374
375#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
376 if (inputIndex + 2 < mBuffer.frameCount) {
377 int32_t* maxOutPt;
378 int32_t maxInIdx;
379
380 maxOutPt = out + (outputSampleCount - 2); // 2 because 2 frames per loop
381 maxInIdx = mBuffer.frameCount - 2;
382 AsmStereo16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
383 phaseFraction, phaseIncrement);
384 }
385#endif // ASM_ARM_RESAMP1
386
387 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
388 out[outputIndex++] += vl * Interp(in[inputIndex*2-2],
389 in[inputIndex*2], phaseFraction);
390 out[outputIndex++] += vr * Interp(in[inputIndex*2-1],
391 in[inputIndex*2+1], phaseFraction);
392 Advance(&inputIndex, &phaseFraction, phaseIncrement);
393 }
394
Glenn Kasten90bebef2012-01-27 15:24:38 -0800395 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396
397 // if done with buffer, save samples
398 if (inputIndex >= mBuffer.frameCount) {
399 inputIndex -= mBuffer.frameCount;
400
Steve Block29357bc2012-01-06 19:20:56 +0000401 // ALOGE("buffer done, new input index %d", inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402
403 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
404 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
405 provider->releaseBuffer(&mBuffer);
406
407 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000408 // ALOG_ASSERT(mBuffer.frameCount == 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 }
410 }
411
Glenn Kasten90bebef2012-01-27 15:24:38 -0800412 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413
414resampleStereo16_exit:
415 // save state
416 mInputIndex = inputIndex;
417 mPhaseFraction = phaseFraction;
418}
419
420void AudioResamplerOrder1::resampleMono16(int32_t* out, size_t outFrameCount,
421 AudioBufferProvider* provider) {
422
423 int32_t vl = mVolume[0];
424 int32_t vr = mVolume[1];
425
426 size_t inputIndex = mInputIndex;
427 uint32_t phaseFraction = mPhaseFraction;
428 uint32_t phaseIncrement = mPhaseIncrement;
429 size_t outputIndex = 0;
430 size_t outputSampleCount = outFrameCount * 2;
431 size_t inFrameCount = (outFrameCount*mInSampleRate)/mSampleRate;
432
Glenn Kasten90bebef2012-01-27 15:24:38 -0800433 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700434 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
435 while (outputIndex < outputSampleCount) {
436 // buffer is empty, fetch a new one
437 while (mBuffer.frameCount == 0) {
438 mBuffer.frameCount = inFrameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -0800439 provider->getNextBuffer(&mBuffer,
440 calculateOutputPTS(outputIndex / 2));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441 if (mBuffer.raw == NULL) {
442 mInputIndex = inputIndex;
443 mPhaseFraction = phaseFraction;
444 goto resampleMono16_exit;
445 }
Glenn Kasten90bebef2012-01-27 15:24:38 -0800446 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700447 if (mBuffer.frameCount > inputIndex) break;
448
449 inputIndex -= mBuffer.frameCount;
450 mX0L = mBuffer.i16[mBuffer.frameCount-1];
451 provider->releaseBuffer(&mBuffer);
452 // mBuffer.frameCount == 0 now so we reload a new buffer
453 }
454 int16_t *in = mBuffer.i16;
455
456 // handle boundary case
457 while (inputIndex == 0) {
Glenn Kasten90bebef2012-01-27 15:24:38 -0800458 // ALOGE("boundary case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700459 int32_t sample = Interp(mX0L, in[0], phaseFraction);
460 out[outputIndex++] += vl * sample;
461 out[outputIndex++] += vr * sample;
462 Advance(&inputIndex, &phaseFraction, phaseIncrement);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700463 if (outputIndex == outputSampleCount) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700464 break;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700465 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466 }
467
468 // process input samples
Glenn Kasten90bebef2012-01-27 15:24:38 -0800469 // ALOGE("general case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470
471#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
472 if (inputIndex + 2 < mBuffer.frameCount) {
473 int32_t* maxOutPt;
474 int32_t maxInIdx;
475
476 maxOutPt = out + (outputSampleCount - 2);
477 maxInIdx = (int32_t)mBuffer.frameCount - 2;
478 AsmMono16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
479 phaseFraction, phaseIncrement);
480 }
481#endif // ASM_ARM_RESAMP1
482
483 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
484 int32_t sample = Interp(in[inputIndex-1], in[inputIndex],
485 phaseFraction);
486 out[outputIndex++] += vl * sample;
487 out[outputIndex++] += vr * sample;
488 Advance(&inputIndex, &phaseFraction, phaseIncrement);
489 }
490
491
Glenn Kasten90bebef2012-01-27 15:24:38 -0800492 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700493
494 // if done with buffer, save samples
495 if (inputIndex >= mBuffer.frameCount) {
496 inputIndex -= mBuffer.frameCount;
497
Steve Block29357bc2012-01-06 19:20:56 +0000498 // ALOGE("buffer done, new input index %d", inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700499
500 mX0L = mBuffer.i16[mBuffer.frameCount-1];
501 provider->releaseBuffer(&mBuffer);
502
503 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000504 // ALOG_ASSERT(mBuffer.frameCount == 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505 }
506 }
507
Glenn Kasten90bebef2012-01-27 15:24:38 -0800508 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700509
510resampleMono16_exit:
511 // save state
512 mInputIndex = inputIndex;
513 mPhaseFraction = phaseFraction;
514}
515
516#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
517
518/*******************************************************************
519*
520* AsmMono16Loop
521* asm optimized monotonic loop version; one loop is 2 frames
522* Input:
523* in : pointer on input samples
524* maxOutPt : pointer on first not filled
525* maxInIdx : index on first not used
526* outputIndex : pointer on current output index
527* out : pointer on output buffer
528* inputIndex : pointer on current input index
529* vl, vr : left and right gain
530* phaseFraction : pointer on current phase fraction
531* phaseIncrement
532* Ouput:
533* outputIndex :
534* out : updated buffer
535* inputIndex : index of next to use
536* phaseFraction : phase fraction for next interpolation
537*
538*******************************************************************/
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800539__attribute__((noinline))
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540void AudioResamplerOrder1::AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
541 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
542 uint32_t &phaseFraction, uint32_t phaseIncrement)
543{
Andy Hungee931ff2014-01-28 13:44:14 -0800544 (void)maxOutPt; // remove unused parameter warnings
545 (void)maxInIdx;
546 (void)outputIndex;
547 (void)out;
548 (void)inputIndex;
549 (void)vl;
550 (void)vr;
551 (void)phaseFraction;
552 (void)phaseIncrement;
553 (void)in;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700554#define MO_PARAM5 "36" // offset of parameter 5 (outputIndex)
555
556 asm(
557 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"
558 // get parameters
559 " ldr r6, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
560 " ldr r6, [r6]\n" // phaseFraction
561 " ldr r7, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
562 " ldr r7, [r7]\n" // inputIndex
563 " ldr r8, [sp, #" MO_PARAM5 " + 4]\n" // out
564 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
565 " ldr r0, [r0]\n" // outputIndex
566 " add r8, r0, asl #2\n" // curOut
567 " ldr r9, [sp, #" MO_PARAM5 " + 24]\n" // phaseIncrement
568 " ldr r10, [sp, #" MO_PARAM5 " + 12]\n" // vl
569 " ldr r11, [sp, #" MO_PARAM5 " + 16]\n" // vr
570
571 // r0 pin, x0, Samp
572
573 // r1 in
574 // r2 maxOutPt
575 // r3 maxInIdx
576
577 // r4 x1, i1, i3, Out1
578 // r5 out0
579
580 // r6 frac
581 // r7 inputIndex
582 // r8 curOut
583
584 // r9 inc
585 // r10 vl
586 // r11 vr
587
588 // r12
589 // r13 sp
590 // r14
591
592 // the following loop works on 2 frames
593
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700594 "1:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700596 " bcs 2f\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597
598#define MO_ONE_FRAME \
599 " add r0, r1, r7, asl #1\n" /* in + inputIndex */\
600 " ldrsh r4, [r0]\n" /* in[inputIndex] */\
601 " ldr r5, [r8]\n" /* out[outputIndex] */\
602 " ldrsh r0, [r0, #-2]\n" /* in[inputIndex-1] */\
603 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
604 " sub r4, r4, r0\n" /* in[inputIndex] - in[inputIndex-1] */\
605 " mov r4, r4, lsl #2\n" /* <<2 */\
606 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
607 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
608 " add r0, r0, r4\n" /* x0 - (..) */\
609 " mla r5, r0, r10, r5\n" /* vl*interp + out[] */\
610 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
611 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
612 " mla r4, r0, r11, r4\n" /* vr*interp + out[] */\
613 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */\
614 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */
615
616 MO_ONE_FRAME // frame 1
617 MO_ONE_FRAME // frame 2
618
619 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700620 " bcc 1b\n"
621 "2:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622
623 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
624 // save modified values
625 " ldr r0, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
626 " str r6, [r0]\n" // phaseFraction
627 " ldr r0, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
628 " str r7, [r0]\n" // inputIndex
629 " ldr r0, [sp, #" MO_PARAM5 " + 4]\n" // out
630 " sub r8, r0\n" // curOut - out
631 " asr r8, #2\n" // new outputIndex
632 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
633 " str r8, [r0]\n" // save outputIndex
634
635 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, pc}\n"
636 );
637}
638
639/*******************************************************************
640*
641* AsmStereo16Loop
642* asm optimized stereo loop version; one loop is 2 frames
643* Input:
644* in : pointer on input samples
645* maxOutPt : pointer on first not filled
646* maxInIdx : index on first not used
647* outputIndex : pointer on current output index
648* out : pointer on output buffer
649* inputIndex : pointer on current input index
650* vl, vr : left and right gain
651* phaseFraction : pointer on current phase fraction
652* phaseIncrement
653* Ouput:
654* outputIndex :
655* out : updated buffer
656* inputIndex : index of next to use
657* phaseFraction : phase fraction for next interpolation
658*
659*******************************************************************/
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800660__attribute__((noinline))
Mathias Agopian65ab4712010-07-14 17:59:35 -0700661void AudioResamplerOrder1::AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
662 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
663 uint32_t &phaseFraction, uint32_t phaseIncrement)
664{
Andy Hungee931ff2014-01-28 13:44:14 -0800665 (void)maxOutPt; // remove unused parameter warnings
666 (void)maxInIdx;
667 (void)outputIndex;
668 (void)out;
669 (void)inputIndex;
670 (void)vl;
671 (void)vr;
672 (void)phaseFraction;
673 (void)phaseIncrement;
674 (void)in;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675#define ST_PARAM5 "40" // offset of parameter 5 (outputIndex)
676 asm(
677 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}\n"
678 // get parameters
679 " ldr r6, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
680 " ldr r6, [r6]\n" // phaseFraction
681 " ldr r7, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
682 " ldr r7, [r7]\n" // inputIndex
683 " ldr r8, [sp, #" ST_PARAM5 " + 4]\n" // out
684 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
685 " ldr r0, [r0]\n" // outputIndex
686 " add r8, r0, asl #2\n" // curOut
687 " ldr r9, [sp, #" ST_PARAM5 " + 24]\n" // phaseIncrement
688 " ldr r10, [sp, #" ST_PARAM5 " + 12]\n" // vl
689 " ldr r11, [sp, #" ST_PARAM5 " + 16]\n" // vr
690
691 // r0 pin, x0, Samp
692
693 // r1 in
694 // r2 maxOutPt
695 // r3 maxInIdx
696
697 // r4 x1, i1, i3, out1
698 // r5 out0
699
700 // r6 frac
701 // r7 inputIndex
702 // r8 curOut
703
704 // r9 inc
705 // r10 vl
706 // r11 vr
707
708 // r12 temporary
709 // r13 sp
710 // r14
711
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700712 "3:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700714 " bcs 4f\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715
716#define ST_ONE_FRAME \
717 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
718\
719 " add r0, r1, r7, asl #2\n" /* in + 2*inputIndex */\
720\
721 " ldrsh r4, [r0]\n" /* in[2*inputIndex] */\
722 " ldr r5, [r8]\n" /* out[outputIndex] */\
723 " ldrsh r12, [r0, #-4]\n" /* in[2*inputIndex-2] */\
724 " sub r4, r4, r12\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
725 " mov r4, r4, lsl #2\n" /* <<2 */\
726 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
727 " add r12, r12, r4\n" /* x0 - (..) */\
728 " mla r5, r12, r10, r5\n" /* vl*interp + out[] */\
729 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
730 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
731\
732 " ldrsh r12, [r0, #+2]\n" /* in[2*inputIndex+1] */\
733 " ldrsh r0, [r0, #-2]\n" /* in[2*inputIndex-1] */\
734 " sub r12, r12, r0\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
735 " mov r12, r12, lsl #2\n" /* <<2 */\
736 " smulwt r12, r12, r6\n" /* (x1-x0)*.. */\
737 " add r12, r0, r12\n" /* x0 - (..) */\
738 " mla r4, r12, r11, r4\n" /* vr*interp + out[] */\
739 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */\
740\
741 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
742 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */
743
744 ST_ONE_FRAME // frame 1
745 ST_ONE_FRAME // frame 1
746
747 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700748 " bcc 3b\n"
749 "4:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750
751 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
752 // save modified values
753 " ldr r0, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
754 " str r6, [r0]\n" // phaseFraction
755 " ldr r0, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
756 " str r7, [r0]\n" // inputIndex
757 " ldr r0, [sp, #" ST_PARAM5 " + 4]\n" // out
758 " sub r8, r0\n" // curOut - out
759 " asr r8, #2\n" // new outputIndex
760 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
761 " str r8, [r0]\n" // save outputIndex
762
763 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, pc}\n"
764 );
765}
766
767#endif // ASM_ARM_RESAMP1
768
769
770// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800772} // namespace android