AudioResampler.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  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. #define LOG_TAG "AudioResampler"
  17. //#define LOG_NDEBUG 0
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <sys/types.h>
  21. #include <pthread.h>
  22. #include <new>
  23. #include "audio/android/cutils/log.h"
  24. #include "audio/android/utils/Utils.h"
  25. //#include <cutils/properties.h>
  26. #include "audio/android/audio_utils/include/audio_utils/primitives.h"
  27. #include "audio/android/AudioResampler.h"
  28. //#include "audio/android/AudioResamplerSinc.h"
  29. #include "audio/android/AudioResamplerCubic.h"
  30. //#include "AudioResamplerDyn.h"
  31. //cjh #ifdef __arm__
  32. // #define ASM_ARM_RESAMP1 // enable asm optimisation for ResamplerOrder1
  33. //#endif
  34. namespace cocos2d { namespace experimental {
  35. // ----------------------------------------------------------------------------
  36. class AudioResamplerOrder1 : public AudioResampler {
  37. public:
  38. AudioResamplerOrder1(int inChannelCount, int32_t sampleRate) :
  39. AudioResampler(inChannelCount, sampleRate, LOW_QUALITY), mX0L(0), mX0R(0) {
  40. }
  41. virtual size_t resample(int32_t* out, size_t outFrameCount,
  42. AudioBufferProvider* provider);
  43. private:
  44. // number of bits used in interpolation multiply - 15 bits avoids overflow
  45. static const int kNumInterpBits = 15;
  46. // bits to shift the phase fraction down to avoid overflow
  47. static const int kPreInterpShift = kNumPhaseBits - kNumInterpBits;
  48. void init() {}
  49. size_t resampleMono16(int32_t* out, size_t outFrameCount,
  50. AudioBufferProvider* provider);
  51. size_t resampleStereo16(int32_t* out, size_t outFrameCount,
  52. AudioBufferProvider* provider);
  53. #ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
  54. void AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
  55. size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
  56. uint32_t &phaseFraction, uint32_t phaseIncrement);
  57. void AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
  58. size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
  59. uint32_t &phaseFraction, uint32_t phaseIncrement);
  60. #endif // ASM_ARM_RESAMP1
  61. static inline int32_t Interp(int32_t x0, int32_t x1, uint32_t f) {
  62. return x0 + (((x1 - x0) * (int32_t)(f >> kPreInterpShift)) >> kNumInterpBits);
  63. }
  64. static inline void Advance(size_t* index, uint32_t* frac, uint32_t inc) {
  65. *frac += inc;
  66. *index += (size_t)(*frac >> kNumPhaseBits);
  67. *frac &= kPhaseMask;
  68. }
  69. int mX0L;
  70. int mX0R;
  71. };
  72. /*static*/
  73. const double AudioResampler::kPhaseMultiplier = 1L << AudioResampler::kNumPhaseBits;
  74. bool AudioResampler::qualityIsSupported(src_quality quality)
  75. {
  76. switch (quality) {
  77. case DEFAULT_QUALITY:
  78. case LOW_QUALITY:
  79. case MED_QUALITY:
  80. case HIGH_QUALITY:
  81. case VERY_HIGH_QUALITY:
  82. return true;
  83. default:
  84. return false;
  85. }
  86. }
  87. // ----------------------------------------------------------------------------
  88. static pthread_once_t once_control = PTHREAD_ONCE_INIT;
  89. static AudioResampler::src_quality defaultQuality = AudioResampler::DEFAULT_QUALITY;
  90. void AudioResampler::init_routine()
  91. {
  92. // int resamplerQuality = getSystemProperty("af.resampler.quality");
  93. // if (resamplerQuality > 0) {
  94. // defaultQuality = (src_quality) resamplerQuality;
  95. // ALOGD("forcing AudioResampler quality to %d", defaultQuality);
  96. // if (defaultQuality < DEFAULT_QUALITY || defaultQuality > VERY_HIGH_QUALITY) {
  97. // defaultQuality = DEFAULT_QUALITY;
  98. // }
  99. // }
  100. }
  101. uint32_t AudioResampler::qualityMHz(src_quality quality)
  102. {
  103. switch (quality) {
  104. default:
  105. case DEFAULT_QUALITY:
  106. case LOW_QUALITY:
  107. return 3;
  108. case MED_QUALITY:
  109. return 6;
  110. case HIGH_QUALITY:
  111. return 20;
  112. case VERY_HIGH_QUALITY:
  113. return 34;
  114. // case DYN_LOW_QUALITY:
  115. // return 4;
  116. // case DYN_MED_QUALITY:
  117. // return 6;
  118. // case DYN_HIGH_QUALITY:
  119. // return 12;
  120. }
  121. }
  122. static const uint32_t maxMHz = 130; // an arbitrary number that permits 3 VHQ, should be tunable
  123. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  124. static uint32_t currentMHz = 0;
  125. AudioResampler* AudioResampler::create(audio_format_t format, int inChannelCount,
  126. int32_t sampleRate, src_quality quality) {
  127. bool atFinalQuality;
  128. if (quality == DEFAULT_QUALITY) {
  129. // read the resampler default quality property the first time it is needed
  130. int ok = pthread_once(&once_control, init_routine);
  131. if (ok != 0) {
  132. ALOGE("%s pthread_once failed: %d", __func__, ok);
  133. }
  134. quality = defaultQuality;
  135. atFinalQuality = false;
  136. } else {
  137. atFinalQuality = true;
  138. }
  139. /* if the caller requests DEFAULT_QUALITY and af.resampler.property
  140. * has not been set, the target resampler quality is set to DYN_MED_QUALITY,
  141. * and allowed to "throttle" down to DYN_LOW_QUALITY if necessary
  142. * due to estimated CPU load of having too many active resamplers
  143. * (the code below the if).
  144. */
  145. if (quality == DEFAULT_QUALITY) {
  146. //cjh quality = DYN_MED_QUALITY;
  147. }
  148. // naive implementation of CPU load throttling doesn't account for whether resampler is active
  149. pthread_mutex_lock(&mutex);
  150. for (;;) {
  151. uint32_t deltaMHz = qualityMHz(quality);
  152. uint32_t newMHz = currentMHz + deltaMHz;
  153. if ((qualityIsSupported(quality) && newMHz <= maxMHz) || atFinalQuality) {
  154. ALOGV("resampler load %u -> %u MHz due to delta +%u MHz from quality %d",
  155. currentMHz, newMHz, deltaMHz, quality);
  156. currentMHz = newMHz;
  157. break;
  158. }
  159. // not enough CPU available for proposed quality level, so try next lowest level
  160. switch (quality) {
  161. default:
  162. case LOW_QUALITY:
  163. atFinalQuality = true;
  164. break;
  165. case MED_QUALITY:
  166. quality = LOW_QUALITY;
  167. break;
  168. case HIGH_QUALITY:
  169. quality = MED_QUALITY;
  170. break;
  171. case VERY_HIGH_QUALITY:
  172. quality = HIGH_QUALITY;
  173. break;
  174. // case DYN_LOW_QUALITY:
  175. // atFinalQuality = true;
  176. // break;
  177. // case DYN_MED_QUALITY:
  178. // quality = DYN_LOW_QUALITY;
  179. // break;
  180. // case DYN_HIGH_QUALITY:
  181. // quality = DYN_MED_QUALITY;
  182. // break;
  183. }
  184. }
  185. pthread_mutex_unlock(&mutex);
  186. AudioResampler* resampler;
  187. switch (quality) {
  188. default:
  189. case LOW_QUALITY:
  190. ALOGV("Create linear Resampler");
  191. LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT, "invalid pcm format");
  192. resampler = new (std::nothrow) AudioResamplerOrder1(inChannelCount, sampleRate);
  193. break;
  194. case MED_QUALITY:
  195. ALOGV("Create cubic Resampler");
  196. LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT, "invalid pcm format");
  197. resampler = new (std::nothrow) AudioResamplerCubic(inChannelCount, sampleRate);
  198. break;
  199. case HIGH_QUALITY:
  200. ALOGV("Create HIGH_QUALITY sinc Resampler");
  201. LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT, "invalid pcm format");
  202. ALOG_ASSERT(false, "HIGH_QUALITY isn't supported");
  203. // Cocos2d-x only uses MED_QUALITY, so we could remove Sinc relative files
  204. // resampler = new (std::nothrow) AudioResamplerSinc(inChannelCount, sampleRate);
  205. break;
  206. case VERY_HIGH_QUALITY:
  207. ALOGV("Create VERY_HIGH_QUALITY sinc Resampler = %d", quality);
  208. LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT, "invalid pcm format");
  209. // Cocos2d-x only uses MED_QUALITY, so we could remove Sinc relative files
  210. // resampler = new (std::nothrow) AudioResamplerSinc(inChannelCount, sampleRate, quality);
  211. ALOG_ASSERT(false, "VERY_HIGH_QUALITY isn't supported");
  212. break;
  213. }
  214. // initialize resampler
  215. resampler->init();
  216. return resampler;
  217. }
  218. AudioResampler::AudioResampler(int inChannelCount,
  219. int32_t sampleRate, src_quality quality) :
  220. mChannelCount(inChannelCount),
  221. mSampleRate(sampleRate), mInSampleRate(sampleRate), mInputIndex(0),
  222. mPhaseFraction(0), mLocalTimeFreq(0),
  223. mPTS(AudioBufferProvider::kInvalidPTS), mQuality(quality) {
  224. const int maxChannels = 2;//cjh quality < DYN_LOW_QUALITY ? 2 : 8;
  225. if (inChannelCount < 1
  226. || inChannelCount > maxChannels) {
  227. LOG_ALWAYS_FATAL("Unsupported sample format %d quality %d channels",
  228. quality, inChannelCount);
  229. }
  230. if (sampleRate <= 0) {
  231. LOG_ALWAYS_FATAL("Unsupported sample rate %d Hz", sampleRate);
  232. }
  233. // initialize common members
  234. mVolume[0] = mVolume[1] = 0;
  235. mBuffer.frameCount = 0;
  236. }
  237. AudioResampler::~AudioResampler() {
  238. pthread_mutex_lock(&mutex);
  239. src_quality quality = getQuality();
  240. uint32_t deltaMHz = qualityMHz(quality);
  241. int32_t newMHz = currentMHz - deltaMHz;
  242. ALOGV("resampler load %u -> %d MHz due to delta -%u MHz from quality %d",
  243. currentMHz, newMHz, deltaMHz, quality);
  244. LOG_ALWAYS_FATAL_IF(newMHz < 0, "negative resampler load %d MHz", newMHz);
  245. currentMHz = newMHz;
  246. pthread_mutex_unlock(&mutex);
  247. }
  248. void AudioResampler::setSampleRate(int32_t inSampleRate) {
  249. mInSampleRate = inSampleRate;
  250. mPhaseIncrement = (uint32_t)((kPhaseMultiplier * inSampleRate) / mSampleRate);
  251. }
  252. void AudioResampler::setVolume(float left, float right) {
  253. // TODO: Implement anti-zipper filter
  254. // convert to U4.12 for internal integer use (round down)
  255. // integer volume values are clamped to 0 to UNITY_GAIN.
  256. mVolume[0] = u4_12_from_float(clampFloatVol(left));
  257. mVolume[1] = u4_12_from_float(clampFloatVol(right));
  258. }
  259. void AudioResampler::setLocalTimeFreq(uint64_t freq) {
  260. mLocalTimeFreq = freq;
  261. }
  262. void AudioResampler::setPTS(int64_t pts) {
  263. mPTS = pts;
  264. }
  265. int64_t AudioResampler::calculateOutputPTS(int outputFrameIndex) {
  266. if (mPTS == AudioBufferProvider::kInvalidPTS) {
  267. return AudioBufferProvider::kInvalidPTS;
  268. } else {
  269. return mPTS + ((outputFrameIndex * mLocalTimeFreq) / mSampleRate);
  270. }
  271. }
  272. void AudioResampler::reset() {
  273. mInputIndex = 0;
  274. mPhaseFraction = 0;
  275. mBuffer.frameCount = 0;
  276. }
  277. // ----------------------------------------------------------------------------
  278. size_t AudioResamplerOrder1::resample(int32_t* out, size_t outFrameCount,
  279. AudioBufferProvider* provider) {
  280. // should never happen, but we overflow if it does
  281. // ALOG_ASSERT(outFrameCount < 32767);
  282. // select the appropriate resampler
  283. switch (mChannelCount) {
  284. case 1:
  285. return resampleMono16(out, outFrameCount, provider);
  286. case 2:
  287. return resampleStereo16(out, outFrameCount, provider);
  288. default:
  289. LOG_ALWAYS_FATAL("invalid channel count: %d", mChannelCount);
  290. return 0;
  291. }
  292. }
  293. size_t AudioResamplerOrder1::resampleStereo16(int32_t* out, size_t outFrameCount,
  294. AudioBufferProvider* provider) {
  295. int32_t vl = mVolume[0];
  296. int32_t vr = mVolume[1];
  297. size_t inputIndex = mInputIndex;
  298. uint32_t phaseFraction = mPhaseFraction;
  299. uint32_t phaseIncrement = mPhaseIncrement;
  300. size_t outputIndex = 0;
  301. size_t outputSampleCount = outFrameCount * 2;
  302. size_t inFrameCount = getInFrameCountRequired(outFrameCount);
  303. // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
  304. // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
  305. while (outputIndex < outputSampleCount) {
  306. // buffer is empty, fetch a new one
  307. while (mBuffer.frameCount == 0) {
  308. mBuffer.frameCount = inFrameCount;
  309. provider->getNextBuffer(&mBuffer,
  310. calculateOutputPTS(outputIndex / 2));
  311. if (mBuffer.raw == NULL) {
  312. goto resampleStereo16_exit;
  313. }
  314. // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
  315. if (mBuffer.frameCount > inputIndex) break;
  316. inputIndex -= mBuffer.frameCount;
  317. mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
  318. mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
  319. provider->releaseBuffer(&mBuffer);
  320. // mBuffer.frameCount == 0 now so we reload a new buffer
  321. }
  322. int16_t *in = mBuffer.i16;
  323. // handle boundary case
  324. while (inputIndex == 0) {
  325. // ALOGE("boundary case");
  326. out[outputIndex++] += vl * Interp(mX0L, in[0], phaseFraction);
  327. out[outputIndex++] += vr * Interp(mX0R, in[1], phaseFraction);
  328. Advance(&inputIndex, &phaseFraction, phaseIncrement);
  329. if (outputIndex == outputSampleCount) {
  330. break;
  331. }
  332. }
  333. // process input samples
  334. // ALOGE("general case");
  335. #ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
  336. if (inputIndex + 2 < mBuffer.frameCount) {
  337. int32_t* maxOutPt;
  338. int32_t maxInIdx;
  339. maxOutPt = out + (outputSampleCount - 2); // 2 because 2 frames per loop
  340. maxInIdx = mBuffer.frameCount - 2;
  341. AsmStereo16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
  342. phaseFraction, phaseIncrement);
  343. }
  344. #endif // ASM_ARM_RESAMP1
  345. while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
  346. out[outputIndex++] += vl * Interp(in[inputIndex*2-2],
  347. in[inputIndex*2], phaseFraction);
  348. out[outputIndex++] += vr * Interp(in[inputIndex*2-1],
  349. in[inputIndex*2+1], phaseFraction);
  350. Advance(&inputIndex, &phaseFraction, phaseIncrement);
  351. }
  352. // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
  353. // if done with buffer, save samples
  354. if (inputIndex >= mBuffer.frameCount) {
  355. inputIndex -= mBuffer.frameCount;
  356. // ALOGE("buffer done, new input index %d", inputIndex);
  357. mX0L = mBuffer.i16[mBuffer.frameCount*2-2];
  358. mX0R = mBuffer.i16[mBuffer.frameCount*2-1];
  359. provider->releaseBuffer(&mBuffer);
  360. // verify that the releaseBuffer resets the buffer frameCount
  361. // ALOG_ASSERT(mBuffer.frameCount == 0);
  362. }
  363. }
  364. // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
  365. resampleStereo16_exit:
  366. // save state
  367. mInputIndex = inputIndex;
  368. mPhaseFraction = phaseFraction;
  369. return outputIndex / 2 /* channels for stereo */;
  370. }
  371. size_t AudioResamplerOrder1::resampleMono16(int32_t* out, size_t outFrameCount,
  372. AudioBufferProvider* provider) {
  373. int32_t vl = mVolume[0];
  374. int32_t vr = mVolume[1];
  375. size_t inputIndex = mInputIndex;
  376. uint32_t phaseFraction = mPhaseFraction;
  377. uint32_t phaseIncrement = mPhaseIncrement;
  378. size_t outputIndex = 0;
  379. size_t outputSampleCount = outFrameCount * 2;
  380. size_t inFrameCount = getInFrameCountRequired(outFrameCount);
  381. // ALOGE("starting resample %d frames, inputIndex=%d, phaseFraction=%d, phaseIncrement=%d",
  382. // outFrameCount, inputIndex, phaseFraction, phaseIncrement);
  383. while (outputIndex < outputSampleCount) {
  384. // buffer is empty, fetch a new one
  385. while (mBuffer.frameCount == 0) {
  386. mBuffer.frameCount = inFrameCount;
  387. provider->getNextBuffer(&mBuffer,
  388. calculateOutputPTS(outputIndex / 2));
  389. if (mBuffer.raw == NULL) {
  390. mInputIndex = inputIndex;
  391. mPhaseFraction = phaseFraction;
  392. goto resampleMono16_exit;
  393. }
  394. // ALOGE("New buffer fetched: %d frames", mBuffer.frameCount);
  395. if (mBuffer.frameCount > inputIndex) break;
  396. inputIndex -= mBuffer.frameCount;
  397. mX0L = mBuffer.i16[mBuffer.frameCount-1];
  398. provider->releaseBuffer(&mBuffer);
  399. // mBuffer.frameCount == 0 now so we reload a new buffer
  400. }
  401. int16_t *in = mBuffer.i16;
  402. // handle boundary case
  403. while (inputIndex == 0) {
  404. // ALOGE("boundary case");
  405. int32_t sample = Interp(mX0L, in[0], phaseFraction);
  406. out[outputIndex++] += vl * sample;
  407. out[outputIndex++] += vr * sample;
  408. Advance(&inputIndex, &phaseFraction, phaseIncrement);
  409. if (outputIndex == outputSampleCount) {
  410. break;
  411. }
  412. }
  413. // process input samples
  414. // ALOGE("general case");
  415. #ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
  416. if (inputIndex + 2 < mBuffer.frameCount) {
  417. int32_t* maxOutPt;
  418. int32_t maxInIdx;
  419. maxOutPt = out + (outputSampleCount - 2);
  420. maxInIdx = (int32_t)mBuffer.frameCount - 2;
  421. AsmMono16Loop(in, maxOutPt, maxInIdx, outputIndex, out, inputIndex, vl, vr,
  422. phaseFraction, phaseIncrement);
  423. }
  424. #endif // ASM_ARM_RESAMP1
  425. while (outputIndex < outputSampleCount && inputIndex < mBuffer.frameCount) {
  426. int32_t sample = Interp(in[inputIndex-1], in[inputIndex],
  427. phaseFraction);
  428. out[outputIndex++] += vl * sample;
  429. out[outputIndex++] += vr * sample;
  430. Advance(&inputIndex, &phaseFraction, phaseIncrement);
  431. }
  432. // ALOGE("loop done - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
  433. // if done with buffer, save samples
  434. if (inputIndex >= mBuffer.frameCount) {
  435. inputIndex -= mBuffer.frameCount;
  436. // ALOGE("buffer done, new input index %d", inputIndex);
  437. mX0L = mBuffer.i16[mBuffer.frameCount-1];
  438. provider->releaseBuffer(&mBuffer);
  439. // verify that the releaseBuffer resets the buffer frameCount
  440. // ALOG_ASSERT(mBuffer.frameCount == 0);
  441. }
  442. }
  443. // ALOGE("output buffer full - outputIndex=%d, inputIndex=%d", outputIndex, inputIndex);
  444. resampleMono16_exit:
  445. // save state
  446. mInputIndex = inputIndex;
  447. mPhaseFraction = phaseFraction;
  448. return outputIndex;
  449. }
  450. #ifdef ASM_ARM_RESAMP1 // asm optimisation for ResamplerOrder1
  451. /*******************************************************************
  452. *
  453. * AsmMono16Loop
  454. * asm optimized monotonic loop version; one loop is 2 frames
  455. * Input:
  456. * in : pointer on input samples
  457. * maxOutPt : pointer on first not filled
  458. * maxInIdx : index on first not used
  459. * outputIndex : pointer on current output index
  460. * out : pointer on output buffer
  461. * inputIndex : pointer on current input index
  462. * vl, vr : left and right gain
  463. * phaseFraction : pointer on current phase fraction
  464. * phaseIncrement
  465. * Output:
  466. * outputIndex :
  467. * out : updated buffer
  468. * inputIndex : index of next to use
  469. * phaseFraction : phase fraction for next interpolation
  470. *
  471. *******************************************************************/
  472. __attribute__((noinline))
  473. void AudioResamplerOrder1::AsmMono16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
  474. size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
  475. uint32_t &phaseFraction, uint32_t phaseIncrement)
  476. {
  477. (void)maxOutPt; // remove unused parameter warnings
  478. (void)maxInIdx;
  479. (void)outputIndex;
  480. (void)out;
  481. (void)inputIndex;
  482. (void)vl;
  483. (void)vr;
  484. (void)phaseFraction;
  485. (void)phaseIncrement;
  486. (void)in;
  487. #define MO_PARAM5 "36" // offset of parameter 5 (outputIndex)
  488. asm(
  489. "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"
  490. // get parameters
  491. " ldr r6, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
  492. " ldr r6, [r6]\n" // phaseFraction
  493. " ldr r7, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
  494. " ldr r7, [r7]\n" // inputIndex
  495. " ldr r8, [sp, #" MO_PARAM5 " + 4]\n" // out
  496. " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
  497. " ldr r0, [r0]\n" // outputIndex
  498. " add r8, r8, r0, asl #2\n" // curOut
  499. " ldr r9, [sp, #" MO_PARAM5 " + 24]\n" // phaseIncrement
  500. " ldr r10, [sp, #" MO_PARAM5 " + 12]\n" // vl
  501. " ldr r11, [sp, #" MO_PARAM5 " + 16]\n" // vr
  502. // r0 pin, x0, Samp
  503. // r1 in
  504. // r2 maxOutPt
  505. // r3 maxInIdx
  506. // r4 x1, i1, i3, Out1
  507. // r5 out0
  508. // r6 frac
  509. // r7 inputIndex
  510. // r8 curOut
  511. // r9 inc
  512. // r10 vl
  513. // r11 vr
  514. // r12
  515. // r13 sp
  516. // r14
  517. // the following loop works on 2 frames
  518. "1:\n"
  519. " cmp r8, r2\n" // curOut - maxCurOut
  520. " bcs 2f\n"
  521. #define MO_ONE_FRAME \
  522. " add r0, r1, r7, asl #1\n" /* in + inputIndex */\
  523. " ldrsh r4, [r0]\n" /* in[inputIndex] */\
  524. " ldr r5, [r8]\n" /* out[outputIndex] */\
  525. " ldrsh r0, [r0, #-2]\n" /* in[inputIndex-1] */\
  526. " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
  527. " sub r4, r4, r0\n" /* in[inputIndex] - in[inputIndex-1] */\
  528. " mov r4, r4, lsl #2\n" /* <<2 */\
  529. " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
  530. " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
  531. " add r0, r0, r4\n" /* x0 - (..) */\
  532. " mla r5, r0, r10, r5\n" /* vl*interp + out[] */\
  533. " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
  534. " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
  535. " mla r4, r0, r11, r4\n" /* vr*interp + out[] */\
  536. " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */\
  537. " str r4, [r8], #4\n" /* out[outputIndex++] = ... */
  538. MO_ONE_FRAME // frame 1
  539. MO_ONE_FRAME // frame 2
  540. " cmp r7, r3\n" // inputIndex - maxInIdx
  541. " bcc 1b\n"
  542. "2:\n"
  543. " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
  544. // save modified values
  545. " ldr r0, [sp, #" MO_PARAM5 " + 20]\n" // &phaseFraction
  546. " str r6, [r0]\n" // phaseFraction
  547. " ldr r0, [sp, #" MO_PARAM5 " + 8]\n" // &inputIndex
  548. " str r7, [r0]\n" // inputIndex
  549. " ldr r0, [sp, #" MO_PARAM5 " + 4]\n" // out
  550. " sub r8, r0\n" // curOut - out
  551. " asr r8, #2\n" // new outputIndex
  552. " ldr r0, [sp, #" MO_PARAM5 " + 0]\n" // &outputIndex
  553. " str r8, [r0]\n" // save outputIndex
  554. " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, pc}\n"
  555. );
  556. }
  557. /*******************************************************************
  558. *
  559. * AsmStereo16Loop
  560. * asm optimized stereo loop version; one loop is 2 frames
  561. * Input:
  562. * in : pointer on input samples
  563. * maxOutPt : pointer on first not filled
  564. * maxInIdx : index on first not used
  565. * outputIndex : pointer on current output index
  566. * out : pointer on output buffer
  567. * inputIndex : pointer on current input index
  568. * vl, vr : left and right gain
  569. * phaseFraction : pointer on current phase fraction
  570. * phaseIncrement
  571. * Output:
  572. * outputIndex :
  573. * out : updated buffer
  574. * inputIndex : index of next to use
  575. * phaseFraction : phase fraction for next interpolation
  576. *
  577. *******************************************************************/
  578. __attribute__((noinline))
  579. void AudioResamplerOrder1::AsmStereo16Loop(int16_t *in, int32_t* maxOutPt, int32_t maxInIdx,
  580. size_t &outputIndex, int32_t* out, size_t &inputIndex, int32_t vl, int32_t vr,
  581. uint32_t &phaseFraction, uint32_t phaseIncrement)
  582. {
  583. (void)maxOutPt; // remove unused parameter warnings
  584. (void)maxInIdx;
  585. (void)outputIndex;
  586. (void)out;
  587. (void)inputIndex;
  588. (void)vl;
  589. (void)vr;
  590. (void)phaseFraction;
  591. (void)phaseIncrement;
  592. (void)in;
  593. #define ST_PARAM5 "40" // offset of parameter 5 (outputIndex)
  594. asm(
  595. "stmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}\n"
  596. // get parameters
  597. " ldr r6, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
  598. " ldr r6, [r6]\n" // phaseFraction
  599. " ldr r7, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
  600. " ldr r7, [r7]\n" // inputIndex
  601. " ldr r8, [sp, #" ST_PARAM5 " + 4]\n" // out
  602. " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
  603. " ldr r0, [r0]\n" // outputIndex
  604. " add r8, r8, r0, asl #2\n" // curOut
  605. " ldr r9, [sp, #" ST_PARAM5 " + 24]\n" // phaseIncrement
  606. " ldr r10, [sp, #" ST_PARAM5 " + 12]\n" // vl
  607. " ldr r11, [sp, #" ST_PARAM5 " + 16]\n" // vr
  608. // r0 pin, x0, Samp
  609. // r1 in
  610. // r2 maxOutPt
  611. // r3 maxInIdx
  612. // r4 x1, i1, i3, out1
  613. // r5 out0
  614. // r6 frac
  615. // r7 inputIndex
  616. // r8 curOut
  617. // r9 inc
  618. // r10 vl
  619. // r11 vr
  620. // r12 temporary
  621. // r13 sp
  622. // r14
  623. "3:\n"
  624. " cmp r8, r2\n" // curOut - maxCurOut
  625. " bcs 4f\n"
  626. #define ST_ONE_FRAME \
  627. " bic r6, r6, #0xC0000000\n" /* phaseFraction & ... */\
  628. \
  629. " add r0, r1, r7, asl #2\n" /* in + 2*inputIndex */\
  630. \
  631. " ldrsh r4, [r0]\n" /* in[2*inputIndex] */\
  632. " ldr r5, [r8]\n" /* out[outputIndex] */\
  633. " ldrsh r12, [r0, #-4]\n" /* in[2*inputIndex-2] */\
  634. " sub r4, r4, r12\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
  635. " mov r4, r4, lsl #2\n" /* <<2 */\
  636. " smulwt r4, r4, r6\n" /* (x1-x0)*.. */\
  637. " add r12, r12, r4\n" /* x0 - (..) */\
  638. " mla r5, r12, r10, r5\n" /* vl*interp + out[] */\
  639. " ldr r4, [r8, #4]\n" /* out[outputIndex+1] */\
  640. " str r5, [r8], #4\n" /* out[outputIndex++] = ... */\
  641. \
  642. " ldrsh r12, [r0, #+2]\n" /* in[2*inputIndex+1] */\
  643. " ldrsh r0, [r0, #-2]\n" /* in[2*inputIndex-1] */\
  644. " sub r12, r12, r0\n" /* in[2*InputIndex] - in[2*InputIndex-2] */\
  645. " mov r12, r12, lsl #2\n" /* <<2 */\
  646. " smulwt r12, r12, r6\n" /* (x1-x0)*.. */\
  647. " add r12, r0, r12\n" /* x0 - (..) */\
  648. " mla r4, r12, r11, r4\n" /* vr*interp + out[] */\
  649. " str r4, [r8], #4\n" /* out[outputIndex++] = ... */\
  650. \
  651. " add r6, r6, r9\n" /* phaseFraction + phaseIncrement */\
  652. " add r7, r7, r6, lsr #30\n" /* inputIndex + phaseFraction>>30 */
  653. ST_ONE_FRAME // frame 1
  654. ST_ONE_FRAME // frame 1
  655. " cmp r7, r3\n" // inputIndex - maxInIdx
  656. " bcc 3b\n"
  657. "4:\n"
  658. " bic r6, r6, #0xC0000000\n" // phaseFraction & ...
  659. // save modified values
  660. " ldr r0, [sp, #" ST_PARAM5 " + 20]\n" // &phaseFraction
  661. " str r6, [r0]\n" // phaseFraction
  662. " ldr r0, [sp, #" ST_PARAM5 " + 8]\n" // &inputIndex
  663. " str r7, [r0]\n" // inputIndex
  664. " ldr r0, [sp, #" ST_PARAM5 " + 4]\n" // out
  665. " sub r8, r0\n" // curOut - out
  666. " asr r8, #2\n" // new outputIndex
  667. " ldr r0, [sp, #" ST_PARAM5 " + 0]\n" // &outputIndex
  668. " str r8, [r0]\n" // save outputIndex
  669. " ldmfd sp!, {r4, r5, r6, r7, r8, r9, r10, r11, r12, pc}\n"
  670. );
  671. }
  672. #endif // ASM_ARM_RESAMP1
  673. // ----------------------------------------------------------------------------
  674. }} // namespace cocos2d { namespace experimental {