blob: 7c20478a12de6323e0be915f0a44367b1f92f6a2 [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__
Elliott Hughes8a9737c2014-12-02 16:59:38 -080032 #define ASM_ARM_RESAMP1 // enable asm optimisation for ResamplerOrder1
Jim Huang0c0a1c02011-04-06 14:19:29 +080033#endif
34
Mathias Agopian65ab4712010-07-14 17:59:35 -070035namespace android {
36
Mathias Agopian65ab4712010-07-14 17:59:35 -070037// ----------------------------------------------------------------------------
38
39class AudioResamplerOrder1 : public AudioResampler {
40public:
Andy Hung3348e362014-07-07 10:21:44 -070041 AudioResamplerOrder1(int inChannelCount, int32_t sampleRate) :
42 AudioResampler(inChannelCount, sampleRate, LOW_QUALITY), mX0L(0), mX0R(0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -070043 }
Andy Hung6b3b7e32015-03-29 00:49:22 -070044 virtual size_t resample(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -070045 AudioBufferProvider* provider);
46private:
47 // number of bits used in interpolation multiply - 15 bits avoids overflow
48 static const int kNumInterpBits = 15;
49
50 // bits to shift the phase fraction down to avoid overflow
51 static const int kPreInterpShift = kNumPhaseBits - kNumInterpBits;
52
53 void init() {}
Andy Hung6b3b7e32015-03-29 00:49:22 -070054 size_t resampleMono16(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -070055 AudioBufferProvider* provider);
Andy Hung6b3b7e32015-03-29 00:49:22 -070056 size_t resampleStereo16(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -070057 AudioBufferProvider* provider);
58#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
59 void AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
60 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
61 uint32_t &phaseFraction, uint32_t phaseIncrement);
62 void AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
63 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
64 uint32_t &phaseFraction, uint32_t phaseIncrement);
65#endif // ASM_ARM_RESAMP1
66
67 static inline int32_t Interp(int32_t x0, int32_t x1, uint32_t f) {
68 return x0 + (((x1 - x0) * (int32_t)(f >> kPreInterpShift)) >> kNumInterpBits);
69 }
70 static inline void Advance(size_t* index, uint32_t* frac, uint32_t inc) {
71 *frac += inc;
72 *index += (size_t)(*frac >> kNumPhaseBits);
73 *frac &= kPhaseMask;
74 }
75 int mX0L;
76 int mX0R;
77};
78
Glenn Kasten01d3acb2014-02-06 08:24:07 -080079/*static*/
80const double AudioResampler::kPhaseMultiplier = 1L << AudioResampler::kNumPhaseBits;
81
Glenn Kastenac602052012-10-01 14:04:31 -070082bool AudioResampler::qualityIsSupported(src_quality quality)
83{
84 switch (quality) {
85 case DEFAULT_QUALITY:
86 case LOW_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070087 case MED_QUALITY:
88 case HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070089 case VERY_HIGH_QUALITY:
Andy Hung86eae0e2013-12-09 12:12:46 -080090 case DYN_LOW_QUALITY:
91 case DYN_MED_QUALITY:
92 case DYN_HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -070093 return true;
94 default:
95 return false;
96 }
97}
98
Mathias Agopian65ab4712010-07-14 17:59:35 -070099// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700100
Glenn Kastenac602052012-10-01 14:04:31 -0700101static pthread_once_t once_control = PTHREAD_ONCE_INIT;
102static AudioResampler::src_quality defaultQuality = AudioResampler::DEFAULT_QUALITY;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103
Glenn Kastenac602052012-10-01 14:04:31 -0700104void AudioResampler::init_routine()
105{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106 char value[PROPERTY_VALUE_MAX];
Glenn Kastenac602052012-10-01 14:04:31 -0700107 if (property_get("af.resampler.quality", value, NULL) > 0) {
108 char *endptr;
109 unsigned long l = strtoul(value, &endptr, 0);
110 if (*endptr == '\0') {
111 defaultQuality = (src_quality) l;
112 ALOGD("forcing AudioResampler quality to %d", defaultQuality);
Andy Hung86eae0e2013-12-09 12:12:46 -0800113 if (defaultQuality < DEFAULT_QUALITY || defaultQuality > DYN_HIGH_QUALITY) {
Glenn Kastenac602052012-10-01 14:04:31 -0700114 defaultQuality = DEFAULT_QUALITY;
115 }
116 }
117 }
118}
119
120uint32_t AudioResampler::qualityMHz(src_quality quality)
121{
122 switch (quality) {
123 default:
124 case DEFAULT_QUALITY:
125 case LOW_QUALITY:
126 return 3;
127 case MED_QUALITY:
128 return 6;
129 case HIGH_QUALITY:
130 return 20;
131 case VERY_HIGH_QUALITY:
132 return 34;
Andy Hung86eae0e2013-12-09 12:12:46 -0800133 case DYN_LOW_QUALITY:
134 return 4;
135 case DYN_MED_QUALITY:
136 return 6;
137 case DYN_HIGH_QUALITY:
138 return 12;
Glenn Kastenac602052012-10-01 14:04:31 -0700139 }
140}
141
Glenn Kastenc4640c92012-10-22 17:09:27 -0700142static const uint32_t maxMHz = 130; // an arbitrary number that permits 3 VHQ, should be tunable
Glenn Kastenac602052012-10-01 14:04:31 -0700143static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
144static uint32_t currentMHz = 0;
145
Andy Hung3348e362014-07-07 10:21:44 -0700146AudioResampler* AudioResampler::create(audio_format_t format, int inChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -0700147 int32_t sampleRate, src_quality quality) {
148
149 bool atFinalQuality;
150 if (quality == DEFAULT_QUALITY) {
151 // read the resampler default quality property the first time it is needed
152 int ok = pthread_once(&once_control, init_routine);
153 if (ok != 0) {
154 ALOGE("%s pthread_once failed: %d", __func__, ok);
155 }
156 quality = defaultQuality;
157 atFinalQuality = false;
158 } else {
159 atFinalQuality = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 }
161
Andy Hung9e0308c2014-01-30 14:32:31 -0800162 /* if the caller requests DEFAULT_QUALITY and af.resampler.property
163 * has not been set, the target resampler quality is set to DYN_MED_QUALITY,
164 * and allowed to "throttle" down to DYN_LOW_QUALITY if necessary
165 * due to estimated CPU load of having too many active resamplers
166 * (the code below the if).
167 */
168 if (quality == DEFAULT_QUALITY) {
169 quality = DYN_MED_QUALITY;
170 }
171
Glenn Kastenac602052012-10-01 14:04:31 -0700172 // naive implementation of CPU load throttling doesn't account for whether resampler is active
173 pthread_mutex_lock(&mutex);
174 for (;;) {
175 uint32_t deltaMHz = qualityMHz(quality);
176 uint32_t newMHz = currentMHz + deltaMHz;
177 if ((qualityIsSupported(quality) && newMHz <= maxMHz) || atFinalQuality) {
178 ALOGV("resampler load %u -> %u MHz due to delta +%u MHz from quality %d",
179 currentMHz, newMHz, deltaMHz, quality);
180 currentMHz = newMHz;
181 break;
182 }
183 // not enough CPU available for proposed quality level, so try next lowest level
184 switch (quality) {
185 default:
Glenn Kastenac602052012-10-01 14:04:31 -0700186 case LOW_QUALITY:
187 atFinalQuality = true;
188 break;
189 case MED_QUALITY:
190 quality = LOW_QUALITY;
191 break;
192 case HIGH_QUALITY:
193 quality = MED_QUALITY;
194 break;
195 case VERY_HIGH_QUALITY:
196 quality = HIGH_QUALITY;
197 break;
Andy Hung86eae0e2013-12-09 12:12:46 -0800198 case DYN_LOW_QUALITY:
199 atFinalQuality = true;
200 break;
201 case DYN_MED_QUALITY:
202 quality = DYN_LOW_QUALITY;
203 break;
204 case DYN_HIGH_QUALITY:
205 quality = DYN_MED_QUALITY;
206 break;
Glenn Kastenac602052012-10-01 14:04:31 -0700207 }
208 }
209 pthread_mutex_unlock(&mutex);
210
211 AudioResampler* resampler;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212
213 switch (quality) {
214 default:
215 case LOW_QUALITY:
Steve Block3856b092011-10-20 11:56:00 +0100216 ALOGV("Create linear Resampler");
Andy Hung3348e362014-07-07 10:21:44 -0700217 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
218 resampler = new AudioResamplerOrder1(inChannelCount, sampleRate);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700219 break;
220 case MED_QUALITY:
Steve Block3856b092011-10-20 11:56:00 +0100221 ALOGV("Create cubic Resampler");
Andy Hung3348e362014-07-07 10:21:44 -0700222 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
223 resampler = new AudioResamplerCubic(inChannelCount, sampleRate);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700224 break;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800225 case HIGH_QUALITY:
226 ALOGV("Create HIGH_QUALITY sinc Resampler");
Andy Hung3348e362014-07-07 10:21:44 -0700227 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
228 resampler = new AudioResamplerSinc(inChannelCount, sampleRate);
Glenn Kastenac602052012-10-01 14:04:31 -0700229 break;
SathishKumar Mani76b11162012-01-17 10:49:47 -0800230 case VERY_HIGH_QUALITY:
Glenn Kastenac602052012-10-01 14:04:31 -0700231 ALOGV("Create VERY_HIGH_QUALITY sinc Resampler = %d", quality);
Andy Hung3348e362014-07-07 10:21:44 -0700232 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
233 resampler = new AudioResamplerSinc(inChannelCount, sampleRate, quality);
SathishKumar Mani76b11162012-01-17 10:49:47 -0800234 break;
Andy Hung86eae0e2013-12-09 12:12:46 -0800235 case DYN_LOW_QUALITY:
236 case DYN_MED_QUALITY:
237 case DYN_HIGH_QUALITY:
238 ALOGV("Create dynamic Resampler = %d", quality);
Andy Hung3348e362014-07-07 10:21:44 -0700239 if (format == AUDIO_FORMAT_PCM_FLOAT) {
240 resampler = new AudioResamplerDyn<float, float, float>(inChannelCount,
Andy Hung771386e2014-04-08 18:44:38 -0700241 sampleRate, quality);
242 } else {
Andy Hung3348e362014-07-07 10:21:44 -0700243 LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
244 if (quality == DYN_HIGH_QUALITY) {
245 resampler = new AudioResamplerDyn<int32_t, int16_t, int32_t>(inChannelCount,
246 sampleRate, quality);
247 } else {
248 resampler = new AudioResamplerDyn<int16_t, int16_t, int32_t>(inChannelCount,
249 sampleRate, quality);
250 }
Andy Hung771386e2014-04-08 18:44:38 -0700251 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800252 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253 }
254
255 // initialize resampler
256 resampler->init();
257 return resampler;
258}
259
Andy Hung3348e362014-07-07 10:21:44 -0700260AudioResampler::AudioResampler(int inChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -0700261 int32_t sampleRate, src_quality quality) :
Andy Hung3348e362014-07-07 10:21:44 -0700262 mChannelCount(inChannelCount),
263 mSampleRate(sampleRate), mInSampleRate(sampleRate), mInputIndex(0),
Glenn Kastend79072e2016-01-06 08:41:20 -0800264 mPhaseFraction(0),
265 mQuality(quality) {
Andy Hung3348e362014-07-07 10:21:44 -0700266
Andy Hung5e58b0a2014-06-23 19:07:29 -0700267 const int maxChannels = quality < DYN_LOW_QUALITY ? 2 : 8;
Andy Hung3348e362014-07-07 10:21:44 -0700268 if (inChannelCount < 1
Andy Hung5e58b0a2014-06-23 19:07:29 -0700269 || inChannelCount > maxChannels) {
Andy Hung3348e362014-07-07 10:21:44 -0700270 LOG_ALWAYS_FATAL("Unsupported sample format %d quality %d channels",
271 quality, inChannelCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700272 }
Glenn Kastenac602052012-10-01 14:04:31 -0700273 if (sampleRate <= 0) {
Andy Hung075abae2014-04-09 19:36:43 -0700274 LOG_ALWAYS_FATAL("Unsupported sample rate %d Hz", sampleRate);
Glenn Kastenac602052012-10-01 14:04:31 -0700275 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276
277 // initialize common members
278 mVolume[0] = mVolume[1] = 0;
279 mBuffer.frameCount = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280}
281
282AudioResampler::~AudioResampler() {
Glenn Kastenac602052012-10-01 14:04:31 -0700283 pthread_mutex_lock(&mutex);
284 src_quality quality = getQuality();
285 uint32_t deltaMHz = qualityMHz(quality);
286 int32_t newMHz = currentMHz - deltaMHz;
287 ALOGV("resampler load %u -> %d MHz due to delta -%u MHz from quality %d",
288 currentMHz, newMHz, deltaMHz, quality);
289 LOG_ALWAYS_FATAL_IF(newMHz < 0, "negative resampler load %d MHz", newMHz);
290 currentMHz = newMHz;
291 pthread_mutex_unlock(&mutex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292}
293
294void AudioResampler::setSampleRate(int32_t inSampleRate) {
295 mInSampleRate = inSampleRate;
296 mPhaseIncrement = (uint32_t)((kPhaseMultiplier * inSampleRate) / mSampleRate);
297}
298
Andy Hung5e58b0a2014-06-23 19:07:29 -0700299void AudioResampler::setVolume(float left, float right) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300 // TODO: Implement anti-zipper filter
Andy Hung5e58b0a2014-06-23 19:07:29 -0700301 // convert to U4.12 for internal integer use (round down)
302 // integer volume values are clamped to 0 to UNITY_GAIN.
303 mVolume[0] = u4_12_from_float(clampFloatVol(left));
304 mVolume[1] = u4_12_from_float(clampFloatVol(right));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305}
306
Eric Laurent243f5f92011-02-28 16:52:51 -0800307void AudioResampler::reset() {
308 mInputIndex = 0;
309 mPhaseFraction = 0;
310 mBuffer.frameCount = 0;
311}
312
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313// ----------------------------------------------------------------------------
314
Andy Hung6b3b7e32015-03-29 00:49:22 -0700315size_t AudioResamplerOrder1::resample(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 AudioBufferProvider* provider) {
317
318 // should never happen, but we overflow if it does
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000319 // ALOG_ASSERT(outFrameCount < 32767);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320
321 // select the appropriate resampler
322 switch (mChannelCount) {
323 case 1:
Andy Hung6b3b7e32015-03-29 00:49:22 -0700324 return resampleMono16(out, outFrameCount, provider);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325 case 2:
Andy Hung6b3b7e32015-03-29 00:49:22 -0700326 return resampleStereo16(out, outFrameCount, provider);
327 default:
328 LOG_ALWAYS_FATAL("invalid channel count: %d", mChannelCount);
329 return 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330 }
331}
332
Andy Hung6b3b7e32015-03-29 00:49:22 -0700333size_t AudioResamplerOrder1::resampleStereo16(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334 AudioBufferProvider* provider) {
335
336 int32_t vl = mVolume[0];
337 int32_t vr = mVolume[1];
338
339 size_t inputIndex = mInputIndex;
340 uint32_t phaseFraction = mPhaseFraction;
341 uint32_t phaseIncrement = mPhaseIncrement;
342 size_t outputIndex = 0;
343 size_t outputSampleCount = outFrameCount * 2;
Andy Hung24781ff2014-02-19 12:45:19 -0800344 size_t inFrameCount = getInFrameCountRequired(outFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345
Glenn Kasten90bebef2012-01-27 15:24:38 -0800346 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
348
349 while (outputIndex < outputSampleCount) {
350
351 // buffer is empty, fetch a new one
352 while (mBuffer.frameCount == 0) {
353 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -0800354 provider->getNextBuffer(&mBuffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355 if (mBuffer.raw == NULL) {
356 goto resampleStereo16_exit;
357 }
358
Glenn Kasten90bebef2012-01-27 15:24:38 -0800359 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360 if (mBuffer.frameCount > inputIndex) break;
361
362 inputIndex -= mBuffer.frameCount;
363 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
364 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
365 provider->releaseBuffer(&mBuffer);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700366 // mBuffer.frameCount == 0 now so we reload a new buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700367 }
368
369 int16_t *in = mBuffer.i16;
370
371 // handle boundary case
372 while (inputIndex == 0) {
Glenn Kasten90bebef2012-01-27 15:24:38 -0800373 // ALOGE("boundary case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374 out[outputIndex++] += vl * Interp(mX0L, in[0], phaseFraction);
375 out[outputIndex++] += vr * Interp(mX0R, in[1], phaseFraction);
376 Advance(&inputIndex, &phaseFraction, phaseIncrement);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700377 if (outputIndex == outputSampleCount) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378 break;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700379 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 }
381
382 // process input samples
Glenn Kasten90bebef2012-01-27 15:24:38 -0800383 // ALOGE("general case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384
385#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
386 if (inputIndex + 2 < mBuffer.frameCount) {
387 int32_t* maxOutPt;
388 int32_t maxInIdx;
389
390 maxOutPt = out + (outputSampleCount - 2); // 2 because 2 frames per loop
391 maxInIdx = mBuffer.frameCount - 2;
392 AsmStereo16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
393 phaseFraction, phaseIncrement);
394 }
395#endif // ASM_ARM_RESAMP1
396
397 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
398 out[outputIndex++] += vl * Interp(in[inputIndex*2-2],
399 in[inputIndex*2], phaseFraction);
400 out[outputIndex++] += vr * Interp(in[inputIndex*2-1],
401 in[inputIndex*2+1], phaseFraction);
402 Advance(&inputIndex, &phaseFraction, phaseIncrement);
403 }
404
Glenn Kasten90bebef2012-01-27 15:24:38 -0800405 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700406
407 // if done with buffer, save samples
408 if (inputIndex >= mBuffer.frameCount) {
409 inputIndex -= mBuffer.frameCount;
410
Steve Block29357bc2012-01-06 19:20:56 +0000411 // ALOGE("buffer done, new input index %d", inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412
413 mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
414 mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
415 provider->releaseBuffer(&mBuffer);
416
417 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000418 // ALOG_ASSERT(mBuffer.frameCount == 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700419 }
420 }
421
Glenn Kasten90bebef2012-01-27 15:24:38 -0800422 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700423
424resampleStereo16_exit:
425 // save state
426 mInputIndex = inputIndex;
427 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700428 return outputIndex / 2 /* channels for stereo */;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700429}
430
Andy Hung6b3b7e32015-03-29 00:49:22 -0700431size_t AudioResamplerOrder1::resampleMono16(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432 AudioBufferProvider* provider) {
433
434 int32_t vl = mVolume[0];
435 int32_t vr = mVolume[1];
436
437 size_t inputIndex = mInputIndex;
438 uint32_t phaseFraction = mPhaseFraction;
439 uint32_t phaseIncrement = mPhaseIncrement;
440 size_t outputIndex = 0;
441 size_t outputSampleCount = outFrameCount * 2;
Andy Hung24781ff2014-02-19 12:45:19 -0800442 size_t inFrameCount = getInFrameCountRequired(outFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443
Glenn Kasten90bebef2012-01-27 15:24:38 -0800444 // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445 // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
446 while (outputIndex < outputSampleCount) {
447 // buffer is empty, fetch a new one
448 while (mBuffer.frameCount == 0) {
449 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -0800450 provider->getNextBuffer(&mBuffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700451 if (mBuffer.raw == NULL) {
452 mInputIndex = inputIndex;
453 mPhaseFraction = phaseFraction;
454 goto resampleMono16_exit;
455 }
Glenn Kasten90bebef2012-01-27 15:24:38 -0800456 // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700457 if (mBuffer.frameCount > inputIndex) break;
458
459 inputIndex -= mBuffer.frameCount;
460 mX0L = mBuffer.i16[mBuffer.frameCount-1];
461 provider->releaseBuffer(&mBuffer);
462 // mBuffer.frameCount == 0 now so we reload a new buffer
463 }
464 int16_t *in = mBuffer.i16;
465
466 // handle boundary case
467 while (inputIndex == 0) {
Glenn Kasten90bebef2012-01-27 15:24:38 -0800468 // ALOGE("boundary case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700469 int32_t sample = Interp(mX0L, in[0], phaseFraction);
470 out[outputIndex++] += vl * sample;
471 out[outputIndex++] += vr * sample;
472 Advance(&inputIndex, &phaseFraction, phaseIncrement);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700473 if (outputIndex == outputSampleCount) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700474 break;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700475 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476 }
477
478 // process input samples
Glenn Kasten90bebef2012-01-27 15:24:38 -0800479 // ALOGE("general case");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700480
481#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
482 if (inputIndex + 2 < mBuffer.frameCount) {
483 int32_t* maxOutPt;
484 int32_t maxInIdx;
485
486 maxOutPt = out + (outputSampleCount - 2);
487 maxInIdx = (int32_t)mBuffer.frameCount - 2;
488 AsmMono16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
489 phaseFraction, phaseIncrement);
490 }
491#endif // ASM_ARM_RESAMP1
492
493 while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
494 int32_t sample = Interp(in[inputIndex-1], in[inputIndex],
495 phaseFraction);
496 out[outputIndex++] += vl * sample;
497 out[outputIndex++] += vr * sample;
498 Advance(&inputIndex, &phaseFraction, phaseIncrement);
499 }
500
501
Glenn Kasten90bebef2012-01-27 15:24:38 -0800502 // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503
504 // if done with buffer, save samples
505 if (inputIndex >= mBuffer.frameCount) {
506 inputIndex -= mBuffer.frameCount;
507
Steve Block29357bc2012-01-06 19:20:56 +0000508 // ALOGE("buffer done, new input index %d", inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700509
510 mX0L = mBuffer.i16[mBuffer.frameCount-1];
511 provider->releaseBuffer(&mBuffer);
512
513 // verify that the releaseBuffer resets the buffer frameCount
Steve Blockc1dc1cb2012-01-09 18:35:44 +0000514 // ALOG_ASSERT(mBuffer.frameCount == 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700515 }
516 }
517
Glenn Kasten90bebef2012-01-27 15:24:38 -0800518 // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519
520resampleMono16_exit:
521 // save state
522 mInputIndex = inputIndex;
523 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700524 return outputIndex;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700525}
526
527#ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
528
529/*******************************************************************
530*
531* AsmMono16Loop
532* asm optimized monotonic loop version; one loop is 2 frames
533* Input:
534* in : pointer on input samples
535* maxOutPt : pointer on first not filled
536* maxInIdx : index on first not used
537* outputIndex : pointer on current output index
538* out : pointer on output buffer
539* inputIndex : pointer on current input index
540* vl, vr : left and right gain
541* phaseFraction : pointer on current phase fraction
542* phaseIncrement
543* Ouput:
544* outputIndex :
545* out : updated buffer
546* inputIndex : index of next to use
547* phaseFraction : phase fraction for next interpolation
548*
549*******************************************************************/
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800550__attribute__((noinline))
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551void AudioResamplerOrder1::AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
552 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
553 uint32_t &phaseFraction, uint32_t phaseIncrement)
554{
Andy Hungee931ff2014-01-28 13:44:14 -0800555 (void)maxOutPt; // remove unused parameter warnings
556 (void)maxInIdx;
557 (void)outputIndex;
558 (void)out;
559 (void)inputIndex;
560 (void)vl;
561 (void)vr;
562 (void)phaseFraction;
563 (void)phaseIncrement;
564 (void)in;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700565#define MO_PARAM5 "36" // offset of parameter 5 (outputIndex)
566
567 asm(
568 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"
569 // get parameters
570 " ldr r6, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
571 " ldr r6, [r6]\n" // phaseFraction
572 " ldr r7, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
573 " ldr r7, [r7]\n" // inputIndex
574 " ldr r8, [sp, #" MO_PARAM5 " + 4]\n" // out
575 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
576 " ldr r0, [r0]\n" // outputIndex
synergy dev5f51ade2014-02-04 06:38:33 -0500577 " add r8, r8, r0, asl #2\n" // curOut
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578 " ldr r9, [sp, #" MO_PARAM5 " + 24]\n" // phaseIncrement
579 " ldr r10, [sp, #" MO_PARAM5 " + 12]\n" // vl
580 " ldr r11, [sp, #" MO_PARAM5 " + 16]\n" // vr
581
582 // r0 pin, x0, Samp
583
584 // r1 in
585 // r2 maxOutPt
586 // r3 maxInIdx
587
588 // r4 x1, i1, i3, Out1
589 // r5 out0
590
591 // r6 frac
592 // r7 inputIndex
593 // r8 curOut
594
595 // r9 inc
596 // r10 vl
597 // r11 vr
598
599 // r12
600 // r13 sp
601 // r14
602
603 // the following loop works on 2 frames
604
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700605 "1:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700607 " bcs 2f\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700608
609#define MO_ONE_FRAME \
610 " add r0, r1, r7, asl #1\n" /* in + inputIndex */\
611 " ldrsh r4, [r0]\n" /* in[inputIndex] */\
612 " ldr r5, [r8]\n" /* out[outputIndex] */\
613 " ldrsh r0, [r0, #-2]\n" /* in[inputIndex-1] */\
614 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
615 " sub r4, r4, r0\n" /* in[inputIndex] - in[inputIndex-1] */\
616 " mov r4, r4, lsl #2\n" /* <<2 */\
617 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
618 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
619 " add r0, r0, r4\n" /* x0 - (..) */\
620 " mla r5, r0, r10, r5\n" /* vl*interp + out[] */\
621 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
622 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
623 " mla r4, r0, r11, r4\n" /* vr*interp + out[] */\
624 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */\
625 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */
626
627 MO_ONE_FRAME // frame 1
628 MO_ONE_FRAME // frame 2
629
630 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700631 " bcc 1b\n"
632 "2:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633
634 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
635 // save modified values
636 " ldr r0, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
637 " str r6, [r0]\n" // phaseFraction
638 " ldr r0, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
639 " str r7, [r0]\n" // inputIndex
640 " ldr r0, [sp, #" MO_PARAM5 " + 4]\n" // out
641 " sub r8, r0\n" // curOut - out
642 " asr r8, #2\n" // new outputIndex
643 " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
644 " str r8, [r0]\n" // save outputIndex
645
646 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, pc}\n"
647 );
648}
649
650/*******************************************************************
651*
652* AsmStereo16Loop
653* asm optimized stereo loop version; one loop is 2 frames
654* Input:
655* in : pointer on input samples
656* maxOutPt : pointer on first not filled
657* maxInIdx : index on first not used
658* outputIndex : pointer on current output index
659* out : pointer on output buffer
660* inputIndex : pointer on current input index
661* vl, vr : left and right gain
662* phaseFraction : pointer on current phase fraction
663* phaseIncrement
664* Ouput:
665* outputIndex :
666* out : updated buffer
667* inputIndex : index of next to use
668* phaseFraction : phase fraction for next interpolation
669*
670*******************************************************************/
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800671__attribute__((noinline))
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672void AudioResamplerOrder1::AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
673 size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
674 uint32_t &phaseFraction, uint32_t phaseIncrement)
675{
Andy Hungee931ff2014-01-28 13:44:14 -0800676 (void)maxOutPt; // remove unused parameter warnings
677 (void)maxInIdx;
678 (void)outputIndex;
679 (void)out;
680 (void)inputIndex;
681 (void)vl;
682 (void)vr;
683 (void)phaseFraction;
684 (void)phaseIncrement;
685 (void)in;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686#define ST_PARAM5 "40" // offset of parameter 5 (outputIndex)
687 asm(
688 "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}\n"
689 // get parameters
690 " ldr r6, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
691 " ldr r6, [r6]\n" // phaseFraction
692 " ldr r7, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
693 " ldr r7, [r7]\n" // inputIndex
694 " ldr r8, [sp, #" ST_PARAM5 " + 4]\n" // out
695 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
696 " ldr r0, [r0]\n" // outputIndex
synergy dev5f51ade2014-02-04 06:38:33 -0500697 " add r8, r8, r0, asl #2\n" // curOut
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698 " ldr r9, [sp, #" ST_PARAM5 " + 24]\n" // phaseIncrement
699 " ldr r10, [sp, #" ST_PARAM5 " + 12]\n" // vl
700 " ldr r11, [sp, #" ST_PARAM5 " + 16]\n" // vr
701
702 // r0 pin, x0, Samp
703
704 // r1 in
705 // r2 maxOutPt
706 // r3 maxInIdx
707
708 // r4 x1, i1, i3, out1
709 // r5 out0
710
711 // r6 frac
712 // r7 inputIndex
713 // r8 curOut
714
715 // r9 inc
716 // r10 vl
717 // r11 vr
718
719 // r12 temporary
720 // r13 sp
721 // r14
722
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700723 "3:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724 " cmp r8, r2\n" // curOut - maxCurOut
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700725 " bcs 4f\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726
727#define ST_ONE_FRAME \
728 " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
729\
730 " add r0, r1, r7, asl #2\n" /* in + 2*inputIndex */\
731\
732 " ldrsh r4, [r0]\n" /* in[2*inputIndex] */\
733 " ldr r5, [r8]\n" /* out[outputIndex] */\
734 " ldrsh r12, [r0, #-4]\n" /* in[2*inputIndex-2] */\
735 " sub r4, r4, r12\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
736 " mov r4, r4, lsl #2\n" /* <<2 */\
737 " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
738 " add r12, r12, r4\n" /* x0 - (..) */\
739 " mla r5, r12, r10, r5\n" /* vl*interp + out[] */\
740 " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
741 " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
742\
743 " ldrsh r12, [r0, #+2]\n" /* in[2*inputIndex+1] */\
744 " ldrsh r0, [r0, #-2]\n" /* in[2*inputIndex-1] */\
745 " sub r12, r12, r0\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
746 " mov r12, r12, lsl #2\n" /* <<2 */\
747 " smulwt r12, r12, r6\n" /* (x1-x0)*.. */\
748 " add r12, r0, r12\n" /* x0 - (..) */\
749 " mla r4, r12, r11, r4\n" /* vr*interp + out[] */\
750 " str r4, [r8], #4\n" /* out[outputIndex++] = ... */\
751\
752 " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
753 " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */
754
755 ST_ONE_FRAME // frame 1
756 ST_ONE_FRAME // frame 1
757
758 " cmp r7, r3\n" // inputIndex - maxInIdx
Nick Kralevicheb8b9142011-09-16 13:14:16 -0700759 " bcc 3b\n"
760 "4:\n"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761
762 " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
763 // save modified values
764 " ldr r0, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
765 " str r6, [r0]\n" // phaseFraction
766 " ldr r0, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
767 " str r7, [r0]\n" // inputIndex
768 " ldr r0, [sp, #" ST_PARAM5 " + 4]\n" // out
769 " sub r8, r0\n" // curOut - out
770 " asr r8, #2\n" // new outputIndex
771 " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
772 " str r8, [r0]\n" // save outputIndex
773
774 " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, pc}\n"
775 );
776}
777
778#endif // ASM_ARM_RESAMP1
779
780
781// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782
Glenn Kastenc23e2f22011-11-17 13:27:22 -0800783} // namespace android