blob: 38c90612d4e4f03a40e10aa90cf9a9ca0d16ea89 [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:
Andy Hung3348e362014-07-07 10:21:44 -070043 AudioResamplerOrder1(int inChannelCount, int32_t sampleRate) :
44 AudioResampler(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 Kasten01d3acb2014-02-06 08:24:07 -080081/*static*/
82const double AudioResampler::kPhaseMultiplier = 1L << AudioResampler::kNumPhaseBits;
83
Glenn Kastenac602052012-10-01 14:04:31 -070084bool AudioResampler::qualityIsSupported(src_quality quality)
85{
86 switch (quality) {
87 case DEFAULT_QUALITY:
88 case LOW_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070089 case MED_QUALITY:
90 case HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070091 case VERY_HIGH_QUALITY:
Andy Hung86eae0e2013-12-09 12:12:46 -080092 case DYN_LOW_QUALITY:
93 case DYN_MED_QUALITY:
94 case DYN_HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070095 return true;
96 default:
97 return false;
98 }
99}
100
Mathias Agopian65ab4712010-07-14 17:59:35 -0700101// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102
Glenn Kastenac602052012-10-01 14:04:31 -0700103static pthread_once_t once_control = PTHREAD_ONCE_INIT;
104static AudioResampler::src_quality defaultQuality = AudioResampler::DEFAULT_QUALITY;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700105
Glenn Kastenac602052012-10-01 14:04:31 -0700106void AudioResampler::init_routine()
107{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700108 char value[PROPERTY_VALUE_MAX];
Glenn Kastenac602052012-10-01 14:04:31 -0700109 if (property_get("af.resampler.quality", value, NULL) > 0) {
110 char *endptr;
111 unsigned long l = strtoul(value, &endptr, 0);
112 if (*endptr == '\0') {
113 defaultQuality = (src_quality) l;
114 ALOGD("forcing AudioResampler quality to %d", defaultQuality);
Andy Hung86eae0e2013-12-09 12:12:46 -0800115 if (defaultQuality < DEFAULT_QUALITY || defaultQuality > DYN_HIGH_QUALITY) {
Glenn Kastenac602052012-10-01 14:04:31 -0700116 defaultQuality = DEFAULT_QUALITY;
117 }
118 }
119 }
120}
121
122uint32_t AudioResampler::qualityMHz(src_quality quality)
123{
124 switch (quality) {
125 default:
126 case DEFAULT_QUALITY:
127 case LOW_QUALITY:
128 return 3;
129 case MED_QUALITY:
130 return 6;
131 case HIGH_QUALITY:
132 return 20;
133 case VERY_HIGH_QUALITY:
134 return 34;
Andy Hung86eae0e2013-12-09 12:12:46 -0800135 case DYN_LOW_QUALITY:
136 return 4;
137 case DYN_MED_QUALITY:
138 return 6;
139 case DYN_HIGH_QUALITY:
140 return 12;
Glenn Kastenac602052012-10-01 14:04:31 -0700141 }
142}
143
Glenn Kastenc4640c92012-10-22 17:09:27 -0700144static const uint32_t maxMHz = 130; // an arbitrary number that permits 3 VHQ, should be tunable
Glenn Kastenac602052012-10-01 14:04:31 -0700145static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
146static uint32_t currentMHz = 0;
147
Andy Hung3348e362014-07-07 10:21:44 -0700148AudioResampler* AudioResampler::create(audio_format_t format, int inChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -0700149 int32_t sampleRate, src_quality quality) {
150
151 bool atFinalQuality;
152 if (quality == DEFAULT_QUALITY) {
153 // read the resampler default quality property the first time it is needed
154 int ok = pthread_once(&once_control, init_routine);
155 if (ok != 0) {
156 ALOGE("%s pthread_once failed: %d", __func__, ok);
157 }
158 quality = defaultQuality;
159 atFinalQuality = false;
160 } else {
161 atFinalQuality = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700162 }
163
Andy Hung9e0308c2014-01-30 14:32:31 -0800164 /* if the caller requests DEFAULT_QUALITY and af.resampler.property
165 * has not been set, the target resampler quality is set to DYN_MED_QUALITY,
166 * and allowed to "throttle" down to DYN_LOW_QUALITY if necessary
167 * due to estimated CPU load of having too many active resamplers
168 * (the code below the if).
169 */
170 if (quality == DEFAULT_QUALITY) {
171 quality = DYN_MED_QUALITY;
172 }
173
Glenn Kastenac602052012-10-01 14:04:31 -0700174 // naive implementation of CPU load throttling doesn't account for whether resampler is active
175 pthread_mutex_lock(&mutex);
176 for (;;) {
177 uint32_t deltaMHz = qualityMHz(quality);
178 uint32_t newMHz = currentMHz + deltaMHz;
179 if ((qualityIsSupported(quality) && newMHz <= maxMHz) || atFinalQuality) {
180 ALOGV("resampler load %u -> %u MHz due to delta +%u MHz from quality %d",
181 currentMHz, newMHz, deltaMHz, quality);
182 currentMHz = newMHz;
183 break;
184 }
185 // not enough CPU available for proposed quality level, so try next lowest level
186 switch (quality) {
187 default:
Glenn Kastenac602052012-10-01 14:04:31 -0700188 case LOW_QUALITY:
189 atFinalQuality = true;
190 break;
191 case MED_QUALITY:
192 quality = LOW_QUALITY;
193 break;
194 case HIGH_QUALITY:
195 quality = MED_QUALITY;
196 break;
197 case VERY_HIGH_QUALITY:
198 quality = HIGH_QUALITY;
199 break;
Andy Hung86eae0e2013-12-09 12:12:46 -0800200 case DYN_LOW_QUALITY:
201 atFinalQuality = true;
202 break;
203 case DYN_MED_QUALITY:
204 quality = DYN_LOW_QUALITY;
205 break;
206 case DYN_HIGH_QUALITY:
207 quality = DYN_MED_QUALITY;
208 break;
Glenn Kastenac602052012-10-01 14:04:31 -0700209 }
210 }
211 pthread_mutex_unlock(&mutex);
212
213 AudioResampler* resampler;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214
215 switch (quality) {
216 default:
217 case LOW_QUALITY:
Steve Block3856b092011-10-20 11:56:00 +0100218 ALOGV("Create linear Resampler");
Andy Hung3348e362014-07-07 10:21:44 -0700219 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
220 resampler = new AudioResamplerOrder1(inChannelCount, sampleRate);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700221 break;
222 case MED_QUALITY:
Steve Block3856b092011-10-20 11:56:00 +0100223 ALOGV("Create cubic Resampler");
Andy Hung3348e362014-07-07 10:21:44 -0700224 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
225 resampler = new AudioResamplerCubic(inChannelCount, sampleRate);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226 break;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800227 case HIGH_QUALITY:
228 ALOGV("Create HIGH_QUALITY sinc Resampler");
Andy Hung3348e362014-07-07 10:21:44 -0700229 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
230 resampler = new AudioResamplerSinc(inChannelCount, sampleRate);
Glenn Kastenac602052012-10-01 14:04:31 -0700231 break;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800232 case VERY_HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -0700233 ALOGV("Create VERY_HIGH_QUALITY sinc Resampler = %d", quality);
Andy Hung3348e362014-07-07 10:21:44 -0700234 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
235 resampler = new AudioResamplerSinc(inChannelCount, sampleRate, quality);
SathishKumar Mani76b11162012-01-17 10:49:47 -0800236 break;
Andy Hung86eae0e2013-12-09 12:12:46 -0800237 case DYN_LOW_QUALITY:
238 case DYN_MED_QUALITY:
239 case DYN_HIGH_QUALITY:
240 ALOGV("Create dynamic Resampler = %d", quality);
Andy Hung3348e362014-07-07 10:21:44 -0700241 if (format == AUDIO_FORMAT_PCM_FLOAT) {
242 resampler = new AudioResamplerDyn<float, float, float>(inChannelCount,
Andy Hung771386e2014-04-08 18:44:38 -0700243 sampleRate, quality);
244 } else {
Andy Hung3348e362014-07-07 10:21:44 -0700245 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
246 if (quality == DYN_HIGH_QUALITY) {
247 resampler = new AudioResamplerDyn<int32_t, int16_t, int32_t>(inChannelCount,
248 sampleRate, quality);
249 } else {
250 resampler = new AudioResamplerDyn<int16_t, int16_t, int32_t>(inChannelCount,
251 sampleRate, quality);
252 }
Andy Hung771386e2014-04-08 18:44:38 -0700253 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800254 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255 }
256
257 // initialize resampler
258 resampler->init();
259 return resampler;
260}
261
Andy Hung3348e362014-07-07 10:21:44 -0700262AudioResampler::AudioResampler(int inChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -0700263 int32_t sampleRate, src_quality quality) :
Andy Hung3348e362014-07-07 10:21:44 -0700264 mChannelCount(inChannelCount),
265 mSampleRate(sampleRate), mInSampleRate(sampleRate), mInputIndex(0),
266 mPhaseFraction(0), mLocalTimeFreq(0),
267 mPTS(AudioBufferProvider::kInvalidPTS), mQuality(quality) {
268
269 if (inChannelCount < 1
Andy Hung075abae2014-04-09 19:36:43 -0700270 || inChannelCount > (quality < DYN_LOW_QUALITY ? 2 : 8)) {
Andy Hung3348e362014-07-07 10:21:44 -0700271 LOG_ALWAYS_FATAL("Unsupported sample format %d quality %d channels",
272 quality, inChannelCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273 }
Glenn Kastenac602052012-10-01 14:04:31 -0700274 if (sampleRate <= 0) {
Andy Hung075abae2014-04-09 19:36:43 -0700275 LOG_ALWAYS_FATAL("Unsupported sample rate %d Hz", sampleRate);
Glenn Kastenac602052012-10-01 14:04:31 -0700276 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277
278 // initialize common members
279 mVolume[0] = mVolume[1] = 0;
280 mBuffer.frameCount = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281}
282
283AudioResampler::~AudioResampler() {
Glenn Kastenac602052012-10-01 14:04:31 -0700284 pthread_mutex_lock(&mutex);
285 src_quality quality = getQuality();
286 uint32_t deltaMHz = qualityMHz(quality);
287 int32_t newMHz = currentMHz - deltaMHz;
288 ALOGV("resampler load %u -> %d MHz due to delta -%u MHz from quality %d",
289 currentMHz, newMHz, deltaMHz, quality);
290 LOG_ALWAYS_FATAL_IF(newMHz < 0, "negative resampler load %d MHz", newMHz);
291 currentMHz = newMHz;
292 pthread_mutex_unlock(&mutex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700293}
294
295void AudioResampler::setSampleRate(int32_t inSampleRate) {
296 mInSampleRate = inSampleRate;
297 mPhaseIncrement = (uint32_t)((kPhaseMultiplier * inSampleRate) / mSampleRate);
298}
299
300void AudioResampler::setVolume(int16_t left, int16_t right) {
301 // TODO: Implement anti-zipper filter
302 mVolume[0] = left;
303 mVolume[1] = right;
304}
305
John Grossman4ff14ba2012-02-08 16:37:41 -0800306void AudioResampler::setLocalTimeFreq(uint64_t freq) {
307 mLocalTimeFreq = freq;
308}
309
310void AudioResampler::setPTS(int64_t pts) {
311 mPTS = pts;
312}
313
314int64_t AudioResampler::calculateOutputPTS(int outputFrameIndex) {
315
316 if (mPTS == AudioBufferProvider::kInvalidPTS) {
317 return AudioBufferProvider::kInvalidPTS;
318 } else {
319 return mPTS + ((outputFrameIndex * mLocalTimeFreq) / mSampleRate);
320 }
321}
322
Eric Laurent243f5f92011-02-28 16:52:51 -0800323void AudioResampler::reset() {
324 mInputIndex = 0;
325 mPhaseFraction = 0;
326 mBuffer.frameCount = 0;
327}
328
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329// ----------------------------------------------------------------------------
330
331void AudioResamplerOrder1::resample(int32_t* out, size_t outFrameCount,
332 AudioBufferProvider* provider) {
333
334 // should never happen, but we overflow if it does
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000335 // ALOG_ASSERT(outFrameCount < 32767);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336
337 // select the appropriate resampler
338 switch (mChannelCount) {
339 case 1:
340 resampleMono16(out, outFrameCount, provider);
341 break;
342 case 2:
343 resampleStereo16(out, outFrameCount, provider);
344 break;
345 }
346}
347
348void AudioResamplerOrder1::resampleStereo16(int32_t* out, size_t outFrameCount,
349 AudioBufferProvider* provider) {
350
351 int32_t vl = mVolume[0];
352 int32_t vr = mVolume[1];
353
354 size_t inputIndex = mInputIndex;
355 uint32_t phaseFraction = mPhaseFraction;
356 uint32_t phaseIncrement = mPhaseIncrement;
357 size_t outputIndex = 0;
358 size_t outputSampleCount = outFrameCount * 2;
Andy Hung24781ff2014-02-19 12:45:19 -0800359 size_t inFrameCount = getInFrameCountRequired(outFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360
Glenn Kasten90bebef2012-01-27 15:24:38 -0800361 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
363
364 while (outputIndex < outputSampleCount) {
365
366 // buffer is empty, fetch a new one
367 while (mBuffer.frameCount == 0) {
368 mBuffer.frameCount = inFrameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -0800369 provider->getNextBuffer(&mBuffer,
370 calculateOutputPTS(outputIndex / 2));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371 if (mBuffer.raw == NULL) {
372 goto resampleStereo16_exit;
373 }
374
Glenn Kasten90bebef2012-01-27 15:24:38 -0800375 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376 if (mBuffer.frameCount > inputIndex) break;
377
378 inputIndex -= mBuffer.frameCount;
379 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
380 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
381 provider->releaseBuffer(&mBuffer);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700382 // mBuffer.frameCount == 0 now so we reload a new buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 }
384
385 int16_t *in = mBuffer.i16;
386
387 // handle boundary case
388 while (inputIndex == 0) {
Glenn Kasten90bebef2012-01-27 15:24:38 -0800389 // ALOGE("boundary case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390 out[outputIndex++] += vl * Interp(mX0L, in[0], phaseFraction);
391 out[outputIndex++] += vr * Interp(mX0R, in[1], phaseFraction);
392 Advance(&inputIndex, &phaseFraction, phaseIncrement);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700393 if (outputIndex == outputSampleCount) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394 break;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700395 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396 }
397
398 // process input samples
Glenn Kasten90bebef2012-01-27 15:24:38 -0800399 // ALOGE("general case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400
401#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
402 if (inputIndex + 2 < mBuffer.frameCount) {
403 int32_t* maxOutPt;
404 int32_t maxInIdx;
405
406 maxOutPt = out + (outputSampleCount - 2); // 2 because 2 frames per loop
407 maxInIdx = mBuffer.frameCount - 2;
408 AsmStereo16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
409 phaseFraction, phaseIncrement);
410 }
411#endif // ASM_ARM_RESAMP1
412
413 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
414 out[outputIndex++] += vl * Interp(in[inputIndex*2-2],
415 in[inputIndex*2], phaseFraction);
416 out[outputIndex++] += vr * Interp(in[inputIndex*2-1],
417 in[inputIndex*2+1], phaseFraction);
418 Advance(&inputIndex, &phaseFraction, phaseIncrement);
419 }
420
Glenn Kasten90bebef2012-01-27 15:24:38 -0800421 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422
423 // if done with buffer, save samples
424 if (inputIndex >= mBuffer.frameCount) {
425 inputIndex -= mBuffer.frameCount;
426
Steve Block29357bc2012-01-06 19:20:56 +0000427 // ALOGE("buffer done, new input index %d", inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428
429 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
430 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
431 provider->releaseBuffer(&mBuffer);
432
433 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000434 // ALOG_ASSERT(mBuffer.frameCount == 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 }
436 }
437
Glenn Kasten90bebef2012-01-27 15:24:38 -0800438 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439
440resampleStereo16_exit:
441 // save state
442 mInputIndex = inputIndex;
443 mPhaseFraction = phaseFraction;
444}
445
446void AudioResamplerOrder1::resampleMono16(int32_t* out, size_t outFrameCount,
447 AudioBufferProvider* provider) {
448
449 int32_t vl = mVolume[0];
450 int32_t vr = mVolume[1];
451
452 size_t inputIndex = mInputIndex;
453 uint32_t phaseFraction = mPhaseFraction;
454 uint32_t phaseIncrement = mPhaseIncrement;
455 size_t outputIndex = 0;
456 size_t outputSampleCount = outFrameCount * 2;
Andy Hung24781ff2014-02-19 12:45:19 -0800457 size_t inFrameCount = getInFrameCountRequired(outFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700458
Glenn Kasten90bebef2012-01-27 15:24:38 -0800459 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700460 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
461 while (outputIndex < outputSampleCount) {
462 // buffer is empty, fetch a new one
463 while (mBuffer.frameCount == 0) {
464 mBuffer.frameCount = inFrameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -0800465 provider->getNextBuffer(&mBuffer,
466 calculateOutputPTS(outputIndex / 2));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700467 if (mBuffer.raw == NULL) {
468 mInputIndex = inputIndex;
469 mPhaseFraction = phaseFraction;
470 goto resampleMono16_exit;
471 }
Glenn Kasten90bebef2012-01-27 15:24:38 -0800472 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700473 if (mBuffer.frameCount > inputIndex) break;
474
475 inputIndex -= mBuffer.frameCount;
476 mX0L = mBuffer.i16[mBuffer.frameCount-1];
477 provider->releaseBuffer(&mBuffer);
478 // mBuffer.frameCount == 0 now so we reload a new buffer
479 }
480 int16_t *in = mBuffer.i16;
481
482 // handle boundary case
483 while (inputIndex == 0) {
Glenn Kasten90bebef2012-01-27 15:24:38 -0800484 // ALOGE("boundary case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700485 int32_t sample = Interp(mX0L, in[0], phaseFraction);
486 out[outputIndex++] += vl * sample;
487 out[outputIndex++] += vr * sample;
488 Advance(&inputIndex, &phaseFraction, phaseIncrement);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700489 if (outputIndex == outputSampleCount) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700490 break;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700491 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700492 }
493
494 // process input samples
Glenn Kasten90bebef2012-01-27 15:24:38 -0800495 // ALOGE("general case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700496
497#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
498 if (inputIndex + 2 < mBuffer.frameCount) {
499 int32_t* maxOutPt;
500 int32_t maxInIdx;
501
502 maxOutPt = out + (outputSampleCount - 2);
503 maxInIdx = (int32_t)mBuffer.frameCount - 2;
504 AsmMono16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
505 phaseFraction, phaseIncrement);
506 }
507#endif // ASM_ARM_RESAMP1
508
509 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
510 int32_t sample = Interp(in[inputIndex-1], in[inputIndex],
511 phaseFraction);
512 out[outputIndex++] += vl * sample;
513 out[outputIndex++] += vr * sample;
514 Advance(&inputIndex, &phaseFraction, phaseIncrement);
515 }
516
517
Glenn Kasten90bebef2012-01-27 15:24:38 -0800518 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519
520 // if done with buffer, save samples
521 if (inputIndex >= mBuffer.frameCount) {
522 inputIndex -= mBuffer.frameCount;
523
Steve Block29357bc2012-01-06 19:20:56 +0000524 // ALOGE("buffer done, new input index %d", inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700525
526 mX0L = mBuffer.i16[mBuffer.frameCount-1];
527 provider->releaseBuffer(&mBuffer);
528
529 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000530 // ALOG_ASSERT(mBuffer.frameCount == 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 }
532 }
533
Glenn Kasten90bebef2012-01-27 15:24:38 -0800534 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700535
536resampleMono16_exit:
537 // save state
538 mInputIndex = inputIndex;
539 mPhaseFraction = phaseFraction;
540}
541
542#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
543
544/*******************************************************************
545*
546* AsmMono16Loop
547* asm optimized monotonic loop version; one loop is 2 frames
548* Input:
549* in : pointer on input samples
550* maxOutPt : pointer on first not filled
551* maxInIdx : index on first not used
552* outputIndex : pointer on current output index
553* out : pointer on output buffer
554* inputIndex : pointer on current input index
555* vl, vr : left and right gain
556* phaseFraction : pointer on current phase fraction
557* phaseIncrement
558* Ouput:
559* outputIndex :
560* out : updated buffer
561* inputIndex : index of next to use
562* phaseFraction : phase fraction for next interpolation
563*
564*******************************************************************/
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800565__attribute__((noinline))
Mathias Agopian65ab4712010-07-14 17:59:35 -0700566void AudioResamplerOrder1::AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
567 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
568 uint32_t &phaseFraction, uint32_t phaseIncrement)
569{
Andy Hungee931ff2014-01-28 13:44:14 -0800570 (void)maxOutPt; // remove unused parameter warnings
571 (void)maxInIdx;
572 (void)outputIndex;
573 (void)out;
574 (void)inputIndex;
575 (void)vl;
576 (void)vr;
577 (void)phaseFraction;
578 (void)phaseIncrement;
579 (void)in;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580#define MO_PARAM5 "36" // offset of parameter 5 (outputIndex)
581
582 asm(
583 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"
584 // get parameters
585 " ldr r6, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
586 " ldr r6, [r6]\n" // phaseFraction
587 " ldr r7, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
588 " ldr r7, [r7]\n" // inputIndex
589 " ldr r8, [sp, #" MO_PARAM5 " + 4]\n" // out
590 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
591 " ldr r0, [r0]\n" // outputIndex
synergy dev5f51ade2014-02-04 06:38:33 -0500592 " add r8, r8, r0, asl #2\n" // curOut
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593 " ldr r9, [sp, #" MO_PARAM5 " + 24]\n" // phaseIncrement
594 " ldr r10, [sp, #" MO_PARAM5 " + 12]\n" // vl
595 " ldr r11, [sp, #" MO_PARAM5 " + 16]\n" // vr
596
597 // r0 pin, x0, Samp
598
599 // r1 in
600 // r2 maxOutPt
601 // r3 maxInIdx
602
603 // r4 x1, i1, i3, Out1
604 // r5 out0
605
606 // r6 frac
607 // r7 inputIndex
608 // r8 curOut
609
610 // r9 inc
611 // r10 vl
612 // r11 vr
613
614 // r12
615 // r13 sp
616 // r14
617
618 // the following loop works on 2 frames
619
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700620 "1:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700622 " bcs 2f\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623
624#define MO_ONE_FRAME \
625 " add r0, r1, r7, asl #1\n" /* in + inputIndex */\
626 " ldrsh r4, [r0]\n" /* in[inputIndex] */\
627 " ldr r5, [r8]\n" /* out[outputIndex] */\
628 " ldrsh r0, [r0, #-2]\n" /* in[inputIndex-1] */\
629 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
630 " sub r4, r4, r0\n" /* in[inputIndex] - in[inputIndex-1] */\
631 " mov r4, r4, lsl #2\n" /* <<2 */\
632 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
633 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
634 " add r0, r0, r4\n" /* x0 - (..) */\
635 " mla r5, r0, r10, r5\n" /* vl*interp + out[] */\
636 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
637 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
638 " mla r4, r0, r11, r4\n" /* vr*interp + out[] */\
639 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */\
640 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */
641
642 MO_ONE_FRAME // frame 1
643 MO_ONE_FRAME // frame 2
644
645 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700646 " bcc 1b\n"
647 "2:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648
649 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
650 // save modified values
651 " ldr r0, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
652 " str r6, [r0]\n" // phaseFraction
653 " ldr r0, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
654 " str r7, [r0]\n" // inputIndex
655 " ldr r0, [sp, #" MO_PARAM5 " + 4]\n" // out
656 " sub r8, r0\n" // curOut - out
657 " asr r8, #2\n" // new outputIndex
658 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
659 " str r8, [r0]\n" // save outputIndex
660
661 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, pc}\n"
662 );
663}
664
665/*******************************************************************
666*
667* AsmStereo16Loop
668* asm optimized stereo loop version; one loop is 2 frames
669* Input:
670* in : pointer on input samples
671* maxOutPt : pointer on first not filled
672* maxInIdx : index on first not used
673* outputIndex : pointer on current output index
674* out : pointer on output buffer
675* inputIndex : pointer on current input index
676* vl, vr : left and right gain
677* phaseFraction : pointer on current phase fraction
678* phaseIncrement
679* Ouput:
680* outputIndex :
681* out : updated buffer
682* inputIndex : index of next to use
683* phaseFraction : phase fraction for next interpolation
684*
685*******************************************************************/
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800686__attribute__((noinline))
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687void AudioResamplerOrder1::AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
688 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
689 uint32_t &phaseFraction, uint32_t phaseIncrement)
690{
Andy Hungee931ff2014-01-28 13:44:14 -0800691 (void)maxOutPt; // remove unused parameter warnings
692 (void)maxInIdx;
693 (void)outputIndex;
694 (void)out;
695 (void)inputIndex;
696 (void)vl;
697 (void)vr;
698 (void)phaseFraction;
699 (void)phaseIncrement;
700 (void)in;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701#define ST_PARAM5 "40" // offset of parameter 5 (outputIndex)
702 asm(
703 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}\n"
704 // get parameters
705 " ldr r6, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
706 " ldr r6, [r6]\n" // phaseFraction
707 " ldr r7, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
708 " ldr r7, [r7]\n" // inputIndex
709 " ldr r8, [sp, #" ST_PARAM5 " + 4]\n" // out
710 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
711 " ldr r0, [r0]\n" // outputIndex
synergy dev5f51ade2014-02-04 06:38:33 -0500712 " add r8, r8, r0, asl #2\n" // curOut
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713 " ldr r9, [sp, #" ST_PARAM5 " + 24]\n" // phaseIncrement
714 " ldr r10, [sp, #" ST_PARAM5 " + 12]\n" // vl
715 " ldr r11, [sp, #" ST_PARAM5 " + 16]\n" // vr
716
717 // r0 pin, x0, Samp
718
719 // r1 in
720 // r2 maxOutPt
721 // r3 maxInIdx
722
723 // r4 x1, i1, i3, out1
724 // r5 out0
725
726 // r6 frac
727 // r7 inputIndex
728 // r8 curOut
729
730 // r9 inc
731 // r10 vl
732 // r11 vr
733
734 // r12 temporary
735 // r13 sp
736 // r14
737
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700738 "3:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700739 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700740 " bcs 4f\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700741
742#define ST_ONE_FRAME \
743 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
744\
745 " add r0, r1, r7, asl #2\n" /* in + 2*inputIndex */\
746\
747 " ldrsh r4, [r0]\n" /* in[2*inputIndex] */\
748 " ldr r5, [r8]\n" /* out[outputIndex] */\
749 " ldrsh r12, [r0, #-4]\n" /* in[2*inputIndex-2] */\
750 " sub r4, r4, r12\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
751 " mov r4, r4, lsl #2\n" /* <<2 */\
752 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
753 " add r12, r12, r4\n" /* x0 - (..) */\
754 " mla r5, r12, r10, r5\n" /* vl*interp + out[] */\
755 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
756 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
757\
758 " ldrsh r12, [r0, #+2]\n" /* in[2*inputIndex+1] */\
759 " ldrsh r0, [r0, #-2]\n" /* in[2*inputIndex-1] */\
760 " sub r12, r12, r0\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
761 " mov r12, r12, lsl #2\n" /* <<2 */\
762 " smulwt r12, r12, r6\n" /* (x1-x0)*.. */\
763 " add r12, r0, r12\n" /* x0 - (..) */\
764 " mla r4, r12, r11, r4\n" /* vr*interp + out[] */\
765 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */\
766\
767 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
768 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */
769
770 ST_ONE_FRAME // frame 1
771 ST_ONE_FRAME // frame 1
772
773 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700774 " bcc 3b\n"
775 "4:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776
777 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
778 // save modified values
779 " ldr r0, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
780 " str r6, [r0]\n" // phaseFraction
781 " ldr r0, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
782 " str r7, [r0]\n" // inputIndex
783 " ldr r0, [sp, #" ST_PARAM5 " + 4]\n" // out
784 " sub r8, r0\n" // curOut - out
785 " asr r8, #2\n" // new outputIndex
786 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
787 " str r8, [r0]\n" // save outputIndex
788
789 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, pc}\n"
790 );
791}
792
793#endif // ASM_ARM_RESAMP1
794
795
796// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800798} // namespace android