blob: 4f8b41382adbf0159fb7ba5bc03402d0de3c0327 [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>
Andy Hung5e58b0a2014-06-23 19:07:29 -070025#include <audio_utils/primitives.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070026#include "AudioResampler.h"
27#include "AudioResamplerSinc.h"
28#include "AudioResamplerCubic.h"
Andy Hung86eae0e2013-12-09 12:12:46 -080029#include "AudioResamplerDyn.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070030
Jim Huang0c0a1c02011-04-06 14:19:29 +080031#ifdef __arm__
Glenn Kasten505fd302016-02-16 15:52:22 -080032 // bug 13102576
33 //#define ASM_ARM_RESAMP1 // enable asm optimisation for ResamplerOrder1
Jim Huang0c0a1c02011-04-06 14:19:29 +080034#endif
35
Mathias Agopian65ab4712010-07-14 17:59:35 -070036namespace android {
37
Mathias Agopian65ab4712010-07-14 17:59:35 -070038// ----------------------------------------------------------------------------
39
40class AudioResamplerOrder1 : public AudioResampler {
41public:
Andy Hung3348e362014-07-07 10:21:44 -070042 AudioResamplerOrder1(int inChannelCount, int32_t sampleRate) :
43 AudioResampler(inChannelCount, sampleRate, LOW_QUALITY), mX0L(0), mX0R(0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -070044 }
Andy Hung6b3b7e32015-03-29 00:49:22 -070045 virtual size_t resample(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -070046 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() {}
Andy Hung6b3b7e32015-03-29 00:49:22 -070055 size_t resampleMono16(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -070056 AudioBufferProvider* provider);
Andy Hung6b3b7e32015-03-29 00:49:22 -070057 size_t resampleStereo16(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -070058 AudioBufferProvider* provider);
59#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
60 void AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
61 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
62 uint32_t &phaseFraction, uint32_t phaseIncrement);
63 void AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
64 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
65 uint32_t &phaseFraction, uint32_t phaseIncrement);
66#endif // ASM_ARM_RESAMP1
67
68 static inline int32_t Interp(int32_t x0, int32_t x1, uint32_t f) {
69 return x0 + (((x1 - x0) * (int32_t)(f >> kPreInterpShift)) >> kNumInterpBits);
70 }
71 static inline void Advance(size_t* index, uint32_t* frac, uint32_t inc) {
72 *frac += inc;
73 *index += (size_t)(*frac >> kNumPhaseBits);
74 *frac &= kPhaseMask;
75 }
76 int mX0L;
77 int mX0R;
78};
79
Glenn Kasten01d3acb2014-02-06 08:24:07 -080080/*static*/
81const double AudioResampler::kPhaseMultiplier = 1L << AudioResampler::kNumPhaseBits;
82
Glenn Kastenac602052012-10-01 14:04:31 -070083bool AudioResampler::qualityIsSupported(src_quality quality)
84{
85 switch (quality) {
86 case DEFAULT_QUALITY:
87 case LOW_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070088 case MED_QUALITY:
89 case HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070090 case VERY_HIGH_QUALITY:
Andy Hung86eae0e2013-12-09 12:12:46 -080091 case DYN_LOW_QUALITY:
92 case DYN_MED_QUALITY:
93 case DYN_HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070094 return true;
95 default:
96 return false;
97 }
98}
99
Mathias Agopian65ab4712010-07-14 17:59:35 -0700100// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700101
Glenn Kastenac602052012-10-01 14:04:31 -0700102static pthread_once_t once_control = PTHREAD_ONCE_INIT;
103static AudioResampler::src_quality defaultQuality = AudioResampler::DEFAULT_QUALITY;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700104
Glenn Kastenac602052012-10-01 14:04:31 -0700105void AudioResampler::init_routine()
106{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700107 char value[PROPERTY_VALUE_MAX];
Glenn Kastenac602052012-10-01 14:04:31 -0700108 if (property_get("af.resampler.quality", value, NULL) > 0) {
109 char *endptr;
110 unsigned long l = strtoul(value, &endptr, 0);
111 if (*endptr == '\0') {
112 defaultQuality = (src_quality) l;
113 ALOGD("forcing AudioResampler quality to %d", defaultQuality);
Andy Hung86eae0e2013-12-09 12:12:46 -0800114 if (defaultQuality < DEFAULT_QUALITY || defaultQuality > DYN_HIGH_QUALITY) {
Glenn Kastenac602052012-10-01 14:04:31 -0700115 defaultQuality = DEFAULT_QUALITY;
116 }
117 }
118 }
119}
120
121uint32_t AudioResampler::qualityMHz(src_quality quality)
122{
123 switch (quality) {
124 default:
125 case DEFAULT_QUALITY:
126 case LOW_QUALITY:
127 return 3;
128 case MED_QUALITY:
129 return 6;
130 case HIGH_QUALITY:
131 return 20;
132 case VERY_HIGH_QUALITY:
133 return 34;
Andy Hung86eae0e2013-12-09 12:12:46 -0800134 case DYN_LOW_QUALITY:
135 return 4;
136 case DYN_MED_QUALITY:
137 return 6;
138 case DYN_HIGH_QUALITY:
139 return 12;
Glenn Kastenac602052012-10-01 14:04:31 -0700140 }
141}
142
Glenn Kastenc4640c92012-10-22 17:09:27 -0700143static const uint32_t maxMHz = 130; // an arbitrary number that permits 3 VHQ, should be tunable
Glenn Kastenac602052012-10-01 14:04:31 -0700144static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
145static uint32_t currentMHz = 0;
146
Andy Hung3348e362014-07-07 10:21:44 -0700147AudioResampler* AudioResampler::create(audio_format_t format, int inChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -0700148 int32_t sampleRate, src_quality quality) {
149
150 bool atFinalQuality;
151 if (quality == DEFAULT_QUALITY) {
152 // read the resampler default quality property the first time it is needed
153 int ok = pthread_once(&once_control, init_routine);
154 if (ok != 0) {
155 ALOGE("%s pthread_once failed: %d", __func__, ok);
156 }
157 quality = defaultQuality;
158 atFinalQuality = false;
159 } else {
160 atFinalQuality = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700161 }
162
Andy Hung9e0308c2014-01-30 14:32:31 -0800163 /* if the caller requests DEFAULT_QUALITY and af.resampler.property
164 * has not been set, the target resampler quality is set to DYN_MED_QUALITY,
165 * and allowed to "throttle" down to DYN_LOW_QUALITY if necessary
166 * due to estimated CPU load of having too many active resamplers
167 * (the code below the if).
168 */
169 if (quality == DEFAULT_QUALITY) {
170 quality = DYN_MED_QUALITY;
171 }
172
Glenn Kastenac602052012-10-01 14:04:31 -0700173 // naive implementation of CPU load throttling doesn't account for whether resampler is active
174 pthread_mutex_lock(&mutex);
175 for (;;) {
176 uint32_t deltaMHz = qualityMHz(quality);
177 uint32_t newMHz = currentMHz + deltaMHz;
178 if ((qualityIsSupported(quality) && newMHz <= maxMHz) || atFinalQuality) {
179 ALOGV("resampler load %u -> %u MHz due to delta +%u MHz from quality %d",
180 currentMHz, newMHz, deltaMHz, quality);
181 currentMHz = newMHz;
182 break;
183 }
184 // not enough CPU available for proposed quality level, so try next lowest level
185 switch (quality) {
186 default:
Glenn Kastenac602052012-10-01 14:04:31 -0700187 case LOW_QUALITY:
188 atFinalQuality = true;
189 break;
190 case MED_QUALITY:
191 quality = LOW_QUALITY;
192 break;
193 case HIGH_QUALITY:
194 quality = MED_QUALITY;
195 break;
196 case VERY_HIGH_QUALITY:
197 quality = HIGH_QUALITY;
198 break;
Andy Hung86eae0e2013-12-09 12:12:46 -0800199 case DYN_LOW_QUALITY:
200 atFinalQuality = true;
201 break;
202 case DYN_MED_QUALITY:
203 quality = DYN_LOW_QUALITY;
204 break;
205 case DYN_HIGH_QUALITY:
206 quality = DYN_MED_QUALITY;
207 break;
Glenn Kastenac602052012-10-01 14:04:31 -0700208 }
209 }
210 pthread_mutex_unlock(&mutex);
211
212 AudioResampler* resampler;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213
214 switch (quality) {
215 default:
216 case LOW_QUALITY:
Steve Block3856b092011-10-20 11:56:00 +0100217 ALOGV("Create linear Resampler");
Andy Hung3348e362014-07-07 10:21:44 -0700218 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
219 resampler = new AudioResamplerOrder1(inChannelCount, sampleRate);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220 break;
221 case MED_QUALITY:
Steve Block3856b092011-10-20 11:56:00 +0100222 ALOGV("Create cubic Resampler");
Andy Hung3348e362014-07-07 10:21:44 -0700223 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
224 resampler = new AudioResamplerCubic(inChannelCount, sampleRate);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700225 break;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800226 case HIGH_QUALITY:
227 ALOGV("Create HIGH_QUALITY sinc Resampler");
Andy Hung3348e362014-07-07 10:21:44 -0700228 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
229 resampler = new AudioResamplerSinc(inChannelCount, sampleRate);
Glenn Kastenac602052012-10-01 14:04:31 -0700230 break;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800231 case VERY_HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -0700232 ALOGV("Create VERY_HIGH_QUALITY sinc Resampler = %d", quality);
Andy Hung3348e362014-07-07 10:21:44 -0700233 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
234 resampler = new AudioResamplerSinc(inChannelCount, sampleRate, quality);
SathishKumar Mani76b11162012-01-17 10:49:47 -0800235 break;
Andy Hung86eae0e2013-12-09 12:12:46 -0800236 case DYN_LOW_QUALITY:
237 case DYN_MED_QUALITY:
238 case DYN_HIGH_QUALITY:
239 ALOGV("Create dynamic Resampler = %d", quality);
Andy Hung3348e362014-07-07 10:21:44 -0700240 if (format == AUDIO_FORMAT_PCM_FLOAT) {
241 resampler = new AudioResamplerDyn<float, float, float>(inChannelCount,
Andy Hung771386e2014-04-08 18:44:38 -0700242 sampleRate, quality);
243 } else {
Andy Hung3348e362014-07-07 10:21:44 -0700244 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
245 if (quality == DYN_HIGH_QUALITY) {
246 resampler = new AudioResamplerDyn<int32_t, int16_t, int32_t>(inChannelCount,
247 sampleRate, quality);
248 } else {
249 resampler = new AudioResamplerDyn<int16_t, int16_t, int32_t>(inChannelCount,
250 sampleRate, quality);
251 }
Andy Hung771386e2014-04-08 18:44:38 -0700252 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800253 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254 }
255
256 // initialize resampler
257 resampler->init();
258 return resampler;
259}
260
Andy Hung3348e362014-07-07 10:21:44 -0700261AudioResampler::AudioResampler(int inChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -0700262 int32_t sampleRate, src_quality quality) :
Andy Hung3348e362014-07-07 10:21:44 -0700263 mChannelCount(inChannelCount),
264 mSampleRate(sampleRate), mInSampleRate(sampleRate), mInputIndex(0),
Glenn Kastend79072e2016-01-06 08:41:20 -0800265 mPhaseFraction(0),
266 mQuality(quality) {
Andy Hung3348e362014-07-07 10:21:44 -0700267
Andy Hung5e58b0a2014-06-23 19:07:29 -0700268 const int maxChannels = quality < DYN_LOW_QUALITY ? 2 : 8;
Andy Hung3348e362014-07-07 10:21:44 -0700269 if (inChannelCount < 1
Andy Hung5e58b0a2014-06-23 19:07:29 -0700270 || inChannelCount > maxChannels) {
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
Andy Hung5e58b0a2014-06-23 19:07:29 -0700300void AudioResampler::setVolume(float left, float right) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700301 // TODO: Implement anti-zipper filter
Andy Hung5e58b0a2014-06-23 19:07:29 -0700302 // convert to U4.12 for internal integer use (round down)
303 // integer volume values are clamped to 0 to UNITY_GAIN.
304 mVolume[0] = u4_12_from_float(clampFloatVol(left));
305 mVolume[1] = u4_12_from_float(clampFloatVol(right));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306}
307
Eric Laurent243f5f92011-02-28 16:52:51 -0800308void AudioResampler::reset() {
309 mInputIndex = 0;
310 mPhaseFraction = 0;
311 mBuffer.frameCount = 0;
312}
313
Mathias Agopian65ab4712010-07-14 17:59:35 -0700314// ----------------------------------------------------------------------------
315
Andy Hung6b3b7e32015-03-29 00:49:22 -0700316size_t AudioResamplerOrder1::resample(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317 AudioBufferProvider* provider) {
318
319 // should never happen, but we overflow if it does
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000320 // ALOG_ASSERT(outFrameCount < 32767);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700321
322 // select the appropriate resampler
323 switch (mChannelCount) {
324 case 1:
Andy Hung6b3b7e32015-03-29 00:49:22 -0700325 return resampleMono16(out, outFrameCount, provider);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326 case 2:
Andy Hung6b3b7e32015-03-29 00:49:22 -0700327 return resampleStereo16(out, outFrameCount, provider);
328 default:
329 LOG_ALWAYS_FATAL("invalid channel count: %d", mChannelCount);
330 return 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331 }
332}
333
Andy Hung6b3b7e32015-03-29 00:49:22 -0700334size_t AudioResamplerOrder1::resampleStereo16(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 AudioBufferProvider* provider) {
336
337 int32_t vl = mVolume[0];
338 int32_t vr = mVolume[1];
339
340 size_t inputIndex = mInputIndex;
341 uint32_t phaseFraction = mPhaseFraction;
342 uint32_t phaseIncrement = mPhaseIncrement;
343 size_t outputIndex = 0;
344 size_t outputSampleCount = outFrameCount * 2;
Andy Hung24781ff2014-02-19 12:45:19 -0800345 size_t inFrameCount = getInFrameCountRequired(outFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346
Glenn Kasten90bebef2012-01-27 15:24:38 -0800347 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
349
350 while (outputIndex < outputSampleCount) {
351
352 // buffer is empty, fetch a new one
353 while (mBuffer.frameCount == 0) {
354 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -0800355 provider->getNextBuffer(&mBuffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356 if (mBuffer.raw == NULL) {
357 goto resampleStereo16_exit;
358 }
359
Glenn Kasten90bebef2012-01-27 15:24:38 -0800360 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361 if (mBuffer.frameCount > inputIndex) break;
362
363 inputIndex -= mBuffer.frameCount;
364 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
365 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
366 provider->releaseBuffer(&mBuffer);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700367 // mBuffer.frameCount == 0 now so we reload a new buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368 }
369
370 int16_t *in = mBuffer.i16;
371
372 // handle boundary case
373 while (inputIndex == 0) {
Glenn Kasten90bebef2012-01-27 15:24:38 -0800374 // ALOGE("boundary case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375 out[outputIndex++] += vl * Interp(mX0L, in[0], phaseFraction);
376 out[outputIndex++] += vr * Interp(mX0R, in[1], phaseFraction);
377 Advance(&inputIndex, &phaseFraction, phaseIncrement);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700378 if (outputIndex == outputSampleCount) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379 break;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700380 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381 }
382
383 // process input samples
Glenn Kasten90bebef2012-01-27 15:24:38 -0800384 // ALOGE("general case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385
386#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
387 if (inputIndex + 2 < mBuffer.frameCount) {
388 int32_t* maxOutPt;
389 int32_t maxInIdx;
390
391 maxOutPt = out + (outputSampleCount - 2); // 2 because 2 frames per loop
392 maxInIdx = mBuffer.frameCount - 2;
393 AsmStereo16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
394 phaseFraction, phaseIncrement);
395 }
396#endif // ASM_ARM_RESAMP1
397
398 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
399 out[outputIndex++] += vl * Interp(in[inputIndex*2-2],
400 in[inputIndex*2], phaseFraction);
401 out[outputIndex++] += vr * Interp(in[inputIndex*2-1],
402 in[inputIndex*2+1], phaseFraction);
403 Advance(&inputIndex, &phaseFraction, phaseIncrement);
404 }
405
Glenn Kasten90bebef2012-01-27 15:24:38 -0800406 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407
408 // if done with buffer, save samples
409 if (inputIndex >= mBuffer.frameCount) {
410 inputIndex -= mBuffer.frameCount;
411
Steve Block29357bc2012-01-06 19:20:56 +0000412 // ALOGE("buffer done, new input index %d", inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413
414 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
415 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
416 provider->releaseBuffer(&mBuffer);
417
418 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000419 // ALOG_ASSERT(mBuffer.frameCount == 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420 }
421 }
422
Glenn Kasten90bebef2012-01-27 15:24:38 -0800423 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424
425resampleStereo16_exit:
426 // save state
427 mInputIndex = inputIndex;
428 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700429 return outputIndex / 2 /* channels for stereo */;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430}
431
Andy Hung6b3b7e32015-03-29 00:49:22 -0700432size_t AudioResamplerOrder1::resampleMono16(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700433 AudioBufferProvider* provider) {
434
435 int32_t vl = mVolume[0];
436 int32_t vr = mVolume[1];
437
438 size_t inputIndex = mInputIndex;
439 uint32_t phaseFraction = mPhaseFraction;
440 uint32_t phaseIncrement = mPhaseIncrement;
441 size_t outputIndex = 0;
442 size_t outputSampleCount = outFrameCount * 2;
Andy Hung24781ff2014-02-19 12:45:19 -0800443 size_t inFrameCount = getInFrameCountRequired(outFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700444
Glenn Kasten90bebef2012-01-27 15:24:38 -0800445 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700446 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
447 while (outputIndex < outputSampleCount) {
448 // buffer is empty, fetch a new one
449 while (mBuffer.frameCount == 0) {
450 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -0800451 provider->getNextBuffer(&mBuffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 if (mBuffer.raw == NULL) {
453 mInputIndex = inputIndex;
454 mPhaseFraction = phaseFraction;
455 goto resampleMono16_exit;
456 }
Glenn Kasten90bebef2012-01-27 15:24:38 -0800457 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700458 if (mBuffer.frameCount > inputIndex) break;
459
460 inputIndex -= mBuffer.frameCount;
461 mX0L = mBuffer.i16[mBuffer.frameCount-1];
462 provider->releaseBuffer(&mBuffer);
463 // mBuffer.frameCount == 0 now so we reload a new buffer
464 }
465 int16_t *in = mBuffer.i16;
466
467 // handle boundary case
468 while (inputIndex == 0) {
Glenn Kasten90bebef2012-01-27 15:24:38 -0800469 // ALOGE("boundary case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470 int32_t sample = Interp(mX0L, in[0], phaseFraction);
471 out[outputIndex++] += vl * sample;
472 out[outputIndex++] += vr * sample;
473 Advance(&inputIndex, &phaseFraction, phaseIncrement);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700474 if (outputIndex == outputSampleCount) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475 break;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700476 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700477 }
478
479 // process input samples
Glenn Kasten90bebef2012-01-27 15:24:38 -0800480 // ALOGE("general case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700481
482#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
483 if (inputIndex + 2 < mBuffer.frameCount) {
484 int32_t* maxOutPt;
485 int32_t maxInIdx;
486
487 maxOutPt = out + (outputSampleCount - 2);
488 maxInIdx = (int32_t)mBuffer.frameCount - 2;
489 AsmMono16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
490 phaseFraction, phaseIncrement);
491 }
492#endif // ASM_ARM_RESAMP1
493
494 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
495 int32_t sample = Interp(in[inputIndex-1], in[inputIndex],
496 phaseFraction);
497 out[outputIndex++] += vl * sample;
498 out[outputIndex++] += vr * sample;
499 Advance(&inputIndex, &phaseFraction, phaseIncrement);
500 }
501
502
Glenn Kasten90bebef2012-01-27 15:24:38 -0800503 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700504
505 // if done with buffer, save samples
506 if (inputIndex >= mBuffer.frameCount) {
507 inputIndex -= mBuffer.frameCount;
508
Steve Block29357bc2012-01-06 19:20:56 +0000509 // ALOGE("buffer done, new input index %d", inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700510
511 mX0L = mBuffer.i16[mBuffer.frameCount-1];
512 provider->releaseBuffer(&mBuffer);
513
514 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000515 // ALOG_ASSERT(mBuffer.frameCount == 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700516 }
517 }
518
Glenn Kasten90bebef2012-01-27 15:24:38 -0800519 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700520
521resampleMono16_exit:
522 // save state
523 mInputIndex = inputIndex;
524 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700525 return outputIndex;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526}
527
528#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
529
530/*******************************************************************
531*
532* AsmMono16Loop
533* asm optimized monotonic loop version; one loop is 2 frames
534* Input:
535* in : pointer on input samples
536* maxOutPt : pointer on first not filled
537* maxInIdx : index on first not used
538* outputIndex : pointer on current output index
539* out : pointer on output buffer
540* inputIndex : pointer on current input index
541* vl, vr : left and right gain
542* phaseFraction : pointer on current phase fraction
543* phaseIncrement
544* Ouput:
545* outputIndex :
546* out : updated buffer
547* inputIndex : index of next to use
548* phaseFraction : phase fraction for next interpolation
549*
550*******************************************************************/
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800551__attribute__((noinline))
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552void AudioResamplerOrder1::AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
553 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
554 uint32_t &phaseFraction, uint32_t phaseIncrement)
555{
Andy Hungee931ff2014-01-28 13:44:14 -0800556 (void)maxOutPt; // remove unused parameter warnings
557 (void)maxInIdx;
558 (void)outputIndex;
559 (void)out;
560 (void)inputIndex;
561 (void)vl;
562 (void)vr;
563 (void)phaseFraction;
564 (void)phaseIncrement;
565 (void)in;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700566#define MO_PARAM5 "36" // offset of parameter 5 (outputIndex)
567
568 asm(
569 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"
570 // get parameters
571 " ldr r6, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
572 " ldr r6, [r6]\n" // phaseFraction
573 " ldr r7, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
574 " ldr r7, [r7]\n" // inputIndex
575 " ldr r8, [sp, #" MO_PARAM5 " + 4]\n" // out
576 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
577 " ldr r0, [r0]\n" // outputIndex
synergy dev5f51ade2014-02-04 06:38:33 -0500578 " add r8, r8, r0, asl #2\n" // curOut
Mathias Agopian65ab4712010-07-14 17:59:35 -0700579 " ldr r9, [sp, #" MO_PARAM5 " + 24]\n" // phaseIncrement
580 " ldr r10, [sp, #" MO_PARAM5 " + 12]\n" // vl
581 " ldr r11, [sp, #" MO_PARAM5 " + 16]\n" // vr
582
583 // r0 pin, x0, Samp
584
585 // r1 in
586 // r2 maxOutPt
587 // r3 maxInIdx
588
589 // r4 x1, i1, i3, Out1
590 // r5 out0
591
592 // r6 frac
593 // r7 inputIndex
594 // r8 curOut
595
596 // r9 inc
597 // r10 vl
598 // r11 vr
599
600 // r12
601 // r13 sp
602 // r14
603
604 // the following loop works on 2 frames
605
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700606 "1:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700608 " bcs 2f\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609
610#define MO_ONE_FRAME \
611 " add r0, r1, r7, asl #1\n" /* in + inputIndex */\
612 " ldrsh r4, [r0]\n" /* in[inputIndex] */\
613 " ldr r5, [r8]\n" /* out[outputIndex] */\
614 " ldrsh r0, [r0, #-2]\n" /* in[inputIndex-1] */\
615 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
616 " sub r4, r4, r0\n" /* in[inputIndex] - in[inputIndex-1] */\
617 " mov r4, r4, lsl #2\n" /* <<2 */\
618 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
619 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
620 " add r0, r0, r4\n" /* x0 - (..) */\
621 " mla r5, r0, r10, r5\n" /* vl*interp + out[] */\
622 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
623 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
624 " mla r4, r0, r11, r4\n" /* vr*interp + out[] */\
625 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */\
626 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */
627
628 MO_ONE_FRAME // frame 1
629 MO_ONE_FRAME // frame 2
630
631 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700632 " bcc 1b\n"
633 "2:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634
635 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
636 // save modified values
637 " ldr r0, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
638 " str r6, [r0]\n" // phaseFraction
639 " ldr r0, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
640 " str r7, [r0]\n" // inputIndex
641 " ldr r0, [sp, #" MO_PARAM5 " + 4]\n" // out
642 " sub r8, r0\n" // curOut - out
643 " asr r8, #2\n" // new outputIndex
644 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
645 " str r8, [r0]\n" // save outputIndex
646
647 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, pc}\n"
648 );
649}
650
651/*******************************************************************
652*
653* AsmStereo16Loop
654* asm optimized stereo loop version; one loop is 2 frames
655* Input:
656* in : pointer on input samples
657* maxOutPt : pointer on first not filled
658* maxInIdx : index on first not used
659* outputIndex : pointer on current output index
660* out : pointer on output buffer
661* inputIndex : pointer on current input index
662* vl, vr : left and right gain
663* phaseFraction : pointer on current phase fraction
664* phaseIncrement
665* Ouput:
666* outputIndex :
667* out : updated buffer
668* inputIndex : index of next to use
669* phaseFraction : phase fraction for next interpolation
670*
671*******************************************************************/
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800672__attribute__((noinline))
Mathias Agopian65ab4712010-07-14 17:59:35 -0700673void AudioResamplerOrder1::AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
674 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
675 uint32_t &phaseFraction, uint32_t phaseIncrement)
676{
Andy Hungee931ff2014-01-28 13:44:14 -0800677 (void)maxOutPt; // remove unused parameter warnings
678 (void)maxInIdx;
679 (void)outputIndex;
680 (void)out;
681 (void)inputIndex;
682 (void)vl;
683 (void)vr;
684 (void)phaseFraction;
685 (void)phaseIncrement;
686 (void)in;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687#define ST_PARAM5 "40" // offset of parameter 5 (outputIndex)
688 asm(
689 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}\n"
690 // get parameters
691 " ldr r6, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
692 " ldr r6, [r6]\n" // phaseFraction
693 " ldr r7, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
694 " ldr r7, [r7]\n" // inputIndex
695 " ldr r8, [sp, #" ST_PARAM5 " + 4]\n" // out
696 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
697 " ldr r0, [r0]\n" // outputIndex
synergy dev5f51ade2014-02-04 06:38:33 -0500698 " add r8, r8, r0, asl #2\n" // curOut
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 " ldr r9, [sp, #" ST_PARAM5 " + 24]\n" // phaseIncrement
700 " ldr r10, [sp, #" ST_PARAM5 " + 12]\n" // vl
701 " ldr r11, [sp, #" ST_PARAM5 " + 16]\n" // vr
702
703 // r0 pin, x0, Samp
704
705 // r1 in
706 // r2 maxOutPt
707 // r3 maxInIdx
708
709 // r4 x1, i1, i3, out1
710 // r5 out0
711
712 // r6 frac
713 // r7 inputIndex
714 // r8 curOut
715
716 // r9 inc
717 // r10 vl
718 // r11 vr
719
720 // r12 temporary
721 // r13 sp
722 // r14
723
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700724 "3:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700726 " bcs 4f\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727
728#define ST_ONE_FRAME \
729 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
730\
731 " add r0, r1, r7, asl #2\n" /* in + 2*inputIndex */\
732\
733 " ldrsh r4, [r0]\n" /* in[2*inputIndex] */\
734 " ldr r5, [r8]\n" /* out[outputIndex] */\
735 " ldrsh r12, [r0, #-4]\n" /* in[2*inputIndex-2] */\
736 " sub r4, r4, r12\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
737 " mov r4, r4, lsl #2\n" /* <<2 */\
738 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
739 " add r12, r12, r4\n" /* x0 - (..) */\
740 " mla r5, r12, r10, r5\n" /* vl*interp + out[] */\
741 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
742 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
743\
744 " ldrsh r12, [r0, #+2]\n" /* in[2*inputIndex+1] */\
745 " ldrsh r0, [r0, #-2]\n" /* in[2*inputIndex-1] */\
746 " sub r12, r12, r0\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
747 " mov r12, r12, lsl #2\n" /* <<2 */\
748 " smulwt r12, r12, r6\n" /* (x1-x0)*.. */\
749 " add r12, r0, r12\n" /* x0 - (..) */\
750 " mla r4, r12, r11, r4\n" /* vr*interp + out[] */\
751 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */\
752\
753 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
754 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */
755
756 ST_ONE_FRAME // frame 1
757 ST_ONE_FRAME // frame 1
758
759 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700760 " bcc 3b\n"
761 "4:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700762
763 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
764 // save modified values
765 " ldr r0, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
766 " str r6, [r0]\n" // phaseFraction
767 " ldr r0, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
768 " str r7, [r0]\n" // inputIndex
769 " ldr r0, [sp, #" ST_PARAM5 " + 4]\n" // out
770 " sub r8, r0\n" // curOut - out
771 " asr r8, #2\n" // new outputIndex
772 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
773 " str r8, [r0]\n" // save outputIndex
774
775 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, pc}\n"
776 );
777}
778
779#endif // ASM_ARM_RESAMP1
780
781
782// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800784} // namespace android