1 /*
2 **
3 ** Copyright 2007, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #define LOG_TAG "AudioMixer"
19 //#define LOG_NDEBUG 0
20
21 #include "Configuration.h"
22 #include <stdint.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26
27 #include <utils/Errors.h>
28 #include <utils/Log.h>
29
30 #include <cutils/bitops.h>
31 #include <cutils/compiler.h>
32 #include <utils/Debug.h>
33
34 #include <system/audio.h>
35
36 #include <audio_utils/primitives.h>
37 #include <common_time/local_clock.h>
38 #include <common_time/cc_helper.h>
39
40 #include <media/EffectsFactoryApi.h>
41
42 #include "AudioMixer.h"
43
44 namespace android {
45
46 // ----------------------------------------------------------------------------
DownmixerBufferProvider()47 AudioMixer::DownmixerBufferProvider::DownmixerBufferProvider() : AudioBufferProvider(),
48 mTrackBufferProvider(NULL), mDownmixHandle(NULL)
49 {
50 }
51
~DownmixerBufferProvider()52 AudioMixer::DownmixerBufferProvider::~DownmixerBufferProvider()
53 {
54 ALOGV("AudioMixer deleting DownmixerBufferProvider (%p)", this);
55 EffectRelease(mDownmixHandle);
56 }
57
getNextBuffer(AudioBufferProvider::Buffer * pBuffer,int64_t pts)58 status_t AudioMixer::DownmixerBufferProvider::getNextBuffer(AudioBufferProvider::Buffer *pBuffer,
59 int64_t pts) {
60 //ALOGV("DownmixerBufferProvider::getNextBuffer()");
61 if (this->mTrackBufferProvider != NULL) {
62 status_t res = mTrackBufferProvider->getNextBuffer(pBuffer, pts);
63 if (res == OK) {
64 mDownmixConfig.inputCfg.buffer.frameCount = pBuffer->frameCount;
65 mDownmixConfig.inputCfg.buffer.raw = pBuffer->raw;
66 mDownmixConfig.outputCfg.buffer.frameCount = pBuffer->frameCount;
67 mDownmixConfig.outputCfg.buffer.raw = mDownmixConfig.inputCfg.buffer.raw;
68 // in-place so overwrite the buffer contents, has been set in prepareTrackForDownmix()
69 //mDownmixConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
70
71 res = (*mDownmixHandle)->process(mDownmixHandle,
72 &mDownmixConfig.inputCfg.buffer, &mDownmixConfig.outputCfg.buffer);
73 //ALOGV("getNextBuffer is downmixing");
74 }
75 return res;
76 } else {
77 ALOGE("DownmixerBufferProvider::getNextBuffer() error: NULL track buffer provider");
78 return NO_INIT;
79 }
80 }
81
releaseBuffer(AudioBufferProvider::Buffer * pBuffer)82 void AudioMixer::DownmixerBufferProvider::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) {
83 //ALOGV("DownmixerBufferProvider::releaseBuffer()");
84 if (this->mTrackBufferProvider != NULL) {
85 mTrackBufferProvider->releaseBuffer(pBuffer);
86 } else {
87 ALOGE("DownmixerBufferProvider::releaseBuffer() error: NULL track buffer provider");
88 }
89 }
90
91
92 // ----------------------------------------------------------------------------
93 bool AudioMixer::isMultichannelCapable = false;
94
95 effect_descriptor_t AudioMixer::dwnmFxDesc;
96
97 // Ensure mConfiguredNames bitmask is initialized properly on all architectures.
98 // The value of 1 << x is undefined in C when x >= 32.
99
AudioMixer(size_t frameCount,uint32_t sampleRate,uint32_t maxNumTracks)100 AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks)
101 : mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1),
102 mSampleRate(sampleRate)
103 {
104 // AudioMixer is not yet capable of multi-channel beyond stereo
105 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(2 == MAX_NUM_CHANNELS);
106
107 ALOG_ASSERT(maxNumTracks <= MAX_NUM_TRACKS, "maxNumTracks %u > MAX_NUM_TRACKS %u",
108 maxNumTracks, MAX_NUM_TRACKS);
109
110 // AudioMixer is not yet capable of more than 32 active track inputs
111 ALOG_ASSERT(32 >= MAX_NUM_TRACKS, "bad MAX_NUM_TRACKS %d", MAX_NUM_TRACKS);
112
113 // AudioMixer is not yet capable of multi-channel output beyond stereo
114 ALOG_ASSERT(2 == MAX_NUM_CHANNELS, "bad MAX_NUM_CHANNELS %d", MAX_NUM_CHANNELS);
115
116 LocalClock lc;
117
118 pthread_once(&sOnceControl, &sInitRoutine);
119
120 mState.enabledTracks= 0;
121 mState.needsChanged = 0;
122 mState.frameCount = frameCount;
123 mState.hook = process__nop;
124 mState.outputTemp = NULL;
125 mState.resampleTemp = NULL;
126 mState.mLog = &mDummyLog;
127 // mState.reserved
128
129 // FIXME Most of the following initialization is probably redundant since
130 // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0
131 // and mTrackNames is initially 0. However, leave it here until that's verified.
132 track_t* t = mState.tracks;
133 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
134 t->resampler = NULL;
135 t->downmixerBufferProvider = NULL;
136 t++;
137 }
138
139 // find multichannel downmix effect if we have to play multichannel content
140 uint32_t numEffects = 0;
141 int ret = EffectQueryNumberEffects(&numEffects);
142 if (ret != 0) {
143 ALOGE("AudioMixer() error %d querying number of effects", ret);
144 return;
145 }
146 ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects);
147
148 for (uint32_t i = 0 ; i < numEffects ; i++) {
149 if (EffectQueryEffect(i, &dwnmFxDesc) == 0) {
150 ALOGV("effect %d is called %s", i, dwnmFxDesc.name);
151 if (memcmp(&dwnmFxDesc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) {
152 ALOGI("found effect \"%s\" from %s",
153 dwnmFxDesc.name, dwnmFxDesc.implementor);
154 isMultichannelCapable = true;
155 break;
156 }
157 }
158 }
159 ALOGE_IF(!isMultichannelCapable, "unable to find downmix effect");
160 }
161
~AudioMixer()162 AudioMixer::~AudioMixer()
163 {
164 track_t* t = mState.tracks;
165 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
166 delete t->resampler;
167 delete t->downmixerBufferProvider;
168 t++;
169 }
170 delete [] mState.outputTemp;
171 delete [] mState.resampleTemp;
172 }
173
setLog(NBLog::Writer * log)174 void AudioMixer::setLog(NBLog::Writer *log)
175 {
176 mState.mLog = log;
177 }
178
getTrackName(audio_channel_mask_t channelMask,int sessionId)179 int AudioMixer::getTrackName(audio_channel_mask_t channelMask, int sessionId)
180 {
181 uint32_t names = (~mTrackNames) & mConfiguredNames;
182 if (names != 0) {
183 int n = __builtin_ctz(names);
184 ALOGV("add track (%d)", n);
185 mTrackNames |= 1 << n;
186 // assume default parameters for the track, except where noted below
187 track_t* t = &mState.tracks[n];
188 t->needs = 0;
189 t->volume[0] = UNITY_GAIN;
190 t->volume[1] = UNITY_GAIN;
191 // no initialization needed
192 // t->prevVolume[0]
193 // t->prevVolume[1]
194 t->volumeInc[0] = 0;
195 t->volumeInc[1] = 0;
196 t->auxLevel = 0;
197 t->auxInc = 0;
198 // no initialization needed
199 // t->prevAuxLevel
200 // t->frameCount
201 t->channelCount = 2;
202 t->enabled = false;
203 t->format = 16;
204 t->channelMask = AUDIO_CHANNEL_OUT_STEREO;
205 t->sessionId = sessionId;
206 // setBufferProvider(name, AudioBufferProvider *) is required before enable(name)
207 t->bufferProvider = NULL;
208 t->buffer.raw = NULL;
209 // no initialization needed
210 // t->buffer.frameCount
211 t->hook = NULL;
212 t->in = NULL;
213 t->resampler = NULL;
214 t->sampleRate = mSampleRate;
215 // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name)
216 t->mainBuffer = NULL;
217 t->auxBuffer = NULL;
218 t->downmixerBufferProvider = NULL;
219
220 status_t status = initTrackDownmix(&mState.tracks[n], n, channelMask);
221 if (status == OK) {
222 return TRACK0 + n;
223 }
224 ALOGE("AudioMixer::getTrackName(0x%x) failed, error preparing track for downmix",
225 channelMask);
226 }
227 return -1;
228 }
229
invalidateState(uint32_t mask)230 void AudioMixer::invalidateState(uint32_t mask)
231 {
232 if (mask) {
233 mState.needsChanged |= mask;
234 mState.hook = process__validate;
235 }
236 }
237
initTrackDownmix(track_t * pTrack,int trackNum,audio_channel_mask_t mask)238 status_t AudioMixer::initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask)
239 {
240 uint32_t channelCount = popcount(mask);
241 ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) && channelCount);
242 status_t status = OK;
243 if (channelCount > MAX_NUM_CHANNELS) {
244 pTrack->channelMask = mask;
245 pTrack->channelCount = channelCount;
246 ALOGV("initTrackDownmix(track=%d, mask=0x%x) calls prepareTrackForDownmix()",
247 trackNum, mask);
248 status = prepareTrackForDownmix(pTrack, trackNum);
249 } else {
250 unprepareTrackForDownmix(pTrack, trackNum);
251 }
252 return status;
253 }
254
unprepareTrackForDownmix(track_t * pTrack,int trackName)255 void AudioMixer::unprepareTrackForDownmix(track_t* pTrack, int trackName) {
256 ALOGV("AudioMixer::unprepareTrackForDownmix(%d)", trackName);
257
258 if (pTrack->downmixerBufferProvider != NULL) {
259 // this track had previously been configured with a downmixer, delete it
260 ALOGV(" deleting old downmixer");
261 pTrack->bufferProvider = pTrack->downmixerBufferProvider->mTrackBufferProvider;
262 delete pTrack->downmixerBufferProvider;
263 pTrack->downmixerBufferProvider = NULL;
264 } else {
265 ALOGV(" nothing to do, no downmixer to delete");
266 }
267 }
268
prepareTrackForDownmix(track_t * pTrack,int trackName)269 status_t AudioMixer::prepareTrackForDownmix(track_t* pTrack, int trackName)
270 {
271 ALOGV("AudioMixer::prepareTrackForDownmix(%d) with mask 0x%x", trackName, pTrack->channelMask);
272
273 // discard the previous downmixer if there was one
274 unprepareTrackForDownmix(pTrack, trackName);
275
276 DownmixerBufferProvider* pDbp = new DownmixerBufferProvider();
277 int32_t status;
278
279 if (!isMultichannelCapable) {
280 ALOGE("prepareTrackForDownmix(%d) fails: mixer doesn't support multichannel content",
281 trackName);
282 goto noDownmixForActiveTrack;
283 }
284
285 if (EffectCreate(&dwnmFxDesc.uuid,
286 pTrack->sessionId /*sessionId*/, -2 /*ioId not relevant here, using random value*/,
287 &pDbp->mDownmixHandle/*pHandle*/) != 0) {
288 ALOGE("prepareTrackForDownmix(%d) fails: error creating downmixer effect", trackName);
289 goto noDownmixForActiveTrack;
290 }
291
292 // channel input configuration will be overridden per-track
293 pDbp->mDownmixConfig.inputCfg.channels = pTrack->channelMask;
294 pDbp->mDownmixConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
295 pDbp->mDownmixConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
296 pDbp->mDownmixConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
297 pDbp->mDownmixConfig.inputCfg.samplingRate = pTrack->sampleRate;
298 pDbp->mDownmixConfig.outputCfg.samplingRate = pTrack->sampleRate;
299 pDbp->mDownmixConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
300 pDbp->mDownmixConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
301 // input and output buffer provider, and frame count will not be used as the downmix effect
302 // process() function is called directly (see DownmixerBufferProvider::getNextBuffer())
303 pDbp->mDownmixConfig.inputCfg.mask = EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS |
304 EFFECT_CONFIG_FORMAT | EFFECT_CONFIG_ACC_MODE;
305 pDbp->mDownmixConfig.outputCfg.mask = pDbp->mDownmixConfig.inputCfg.mask;
306
307 {// scope for local variables that are not used in goto label "noDownmixForActiveTrack"
308 int cmdStatus;
309 uint32_t replySize = sizeof(int);
310
311 // Configure and enable downmixer
312 status = (*pDbp->mDownmixHandle)->command(pDbp->mDownmixHandle,
313 EFFECT_CMD_SET_CONFIG /*cmdCode*/, sizeof(effect_config_t) /*cmdSize*/,
314 &pDbp->mDownmixConfig /*pCmdData*/,
315 &replySize /*replySize*/, &cmdStatus /*pReplyData*/);
316 if ((status != 0) || (cmdStatus != 0)) {
317 ALOGE("error %d while configuring downmixer for track %d", status, trackName);
318 goto noDownmixForActiveTrack;
319 }
320 replySize = sizeof(int);
321 status = (*pDbp->mDownmixHandle)->command(pDbp->mDownmixHandle,
322 EFFECT_CMD_ENABLE /*cmdCode*/, 0 /*cmdSize*/, NULL /*pCmdData*/,
323 &replySize /*replySize*/, &cmdStatus /*pReplyData*/);
324 if ((status != 0) || (cmdStatus != 0)) {
325 ALOGE("error %d while enabling downmixer for track %d", status, trackName);
326 goto noDownmixForActiveTrack;
327 }
328
329 // Set downmix type
330 // parameter size rounded for padding on 32bit boundary
331 const int psizePadded = ((sizeof(downmix_params_t) - 1)/sizeof(int) + 1) * sizeof(int);
332 const int downmixParamSize =
333 sizeof(effect_param_t) + psizePadded + sizeof(downmix_type_t);
334 effect_param_t * const param = (effect_param_t *) malloc(downmixParamSize);
335 param->psize = sizeof(downmix_params_t);
336 const downmix_params_t downmixParam = DOWNMIX_PARAM_TYPE;
337 memcpy(param->data, &downmixParam, param->psize);
338 const downmix_type_t downmixType = DOWNMIX_TYPE_FOLD;
339 param->vsize = sizeof(downmix_type_t);
340 memcpy(param->data + psizePadded, &downmixType, param->vsize);
341
342 status = (*pDbp->mDownmixHandle)->command(pDbp->mDownmixHandle,
343 EFFECT_CMD_SET_PARAM /* cmdCode */, downmixParamSize/* cmdSize */,
344 param /*pCmndData*/, &replySize /*replySize*/, &cmdStatus /*pReplyData*/);
345
346 free(param);
347
348 if ((status != 0) || (cmdStatus != 0)) {
349 ALOGE("error %d while setting downmix type for track %d", status, trackName);
350 goto noDownmixForActiveTrack;
351 } else {
352 ALOGV("downmix type set to %d for track %d", (int) downmixType, trackName);
353 }
354 }// end of scope for local variables that are not used in goto label "noDownmixForActiveTrack"
355
356 // initialization successful:
357 // - keep track of the real buffer provider in case it was set before
358 pDbp->mTrackBufferProvider = pTrack->bufferProvider;
359 // - we'll use the downmix effect integrated inside this
360 // track's buffer provider, and we'll use it as the track's buffer provider
361 pTrack->downmixerBufferProvider = pDbp;
362 pTrack->bufferProvider = pDbp;
363
364 return NO_ERROR;
365
366 noDownmixForActiveTrack:
367 delete pDbp;
368 pTrack->downmixerBufferProvider = NULL;
369 return NO_INIT;
370 }
371
deleteTrackName(int name)372 void AudioMixer::deleteTrackName(int name)
373 {
374 ALOGV("AudioMixer::deleteTrackName(%d)", name);
375 name -= TRACK0;
376 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
377 ALOGV("deleteTrackName(%d)", name);
378 track_t& track(mState.tracks[ name ]);
379 if (track.enabled) {
380 track.enabled = false;
381 invalidateState(1<<name);
382 }
383 // delete the resampler
384 delete track.resampler;
385 track.resampler = NULL;
386 // delete the downmixer
387 unprepareTrackForDownmix(&mState.tracks[name], name);
388
389 mTrackNames &= ~(1<<name);
390 }
391
enable(int name)392 void AudioMixer::enable(int name)
393 {
394 name -= TRACK0;
395 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
396 track_t& track = mState.tracks[name];
397
398 if (!track.enabled) {
399 track.enabled = true;
400 ALOGV("enable(%d)", name);
401 invalidateState(1 << name);
402 }
403 }
404
disable(int name)405 void AudioMixer::disable(int name)
406 {
407 name -= TRACK0;
408 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
409 track_t& track = mState.tracks[name];
410
411 if (track.enabled) {
412 track.enabled = false;
413 ALOGV("disable(%d)", name);
414 invalidateState(1 << name);
415 }
416 }
417
setParameter(int name,int target,int param,void * value)418 void AudioMixer::setParameter(int name, int target, int param, void *value)
419 {
420 name -= TRACK0;
421 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
422 track_t& track = mState.tracks[name];
423
424 int valueInt = (int)value;
425 int32_t *valueBuf = (int32_t *)value;
426
427 switch (target) {
428
429 case TRACK:
430 switch (param) {
431 case CHANNEL_MASK: {
432 audio_channel_mask_t mask = (audio_channel_mask_t) value;
433 if (track.channelMask != mask) {
434 uint32_t channelCount = popcount(mask);
435 ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) && channelCount);
436 track.channelMask = mask;
437 track.channelCount = channelCount;
438 // the mask has changed, does this track need a downmixer?
439 initTrackDownmix(&mState.tracks[name], name, mask);
440 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", mask);
441 invalidateState(1 << name);
442 }
443 } break;
444 case MAIN_BUFFER:
445 if (track.mainBuffer != valueBuf) {
446 track.mainBuffer = valueBuf;
447 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
448 invalidateState(1 << name);
449 }
450 break;
451 case AUX_BUFFER:
452 if (track.auxBuffer != valueBuf) {
453 track.auxBuffer = valueBuf;
454 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
455 invalidateState(1 << name);
456 }
457 break;
458 case FORMAT:
459 ALOG_ASSERT(valueInt == AUDIO_FORMAT_PCM_16_BIT);
460 break;
461 // FIXME do we want to support setting the downmix type from AudioFlinger?
462 // for a specific track? or per mixer?
463 /* case DOWNMIX_TYPE:
464 break */
465 default:
466 LOG_FATAL("bad param");
467 }
468 break;
469
470 case RESAMPLE:
471 switch (param) {
472 case SAMPLE_RATE:
473 ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt);
474 if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
475 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
476 uint32_t(valueInt));
477 invalidateState(1 << name);
478 }
479 break;
480 case RESET:
481 track.resetResampler();
482 invalidateState(1 << name);
483 break;
484 case REMOVE:
485 delete track.resampler;
486 track.resampler = NULL;
487 track.sampleRate = mSampleRate;
488 invalidateState(1 << name);
489 break;
490 default:
491 LOG_FATAL("bad param");
492 }
493 break;
494
495 case RAMP_VOLUME:
496 case VOLUME:
497 switch (param) {
498 case VOLUME0:
499 case VOLUME1:
500 if (track.volume[param-VOLUME0] != valueInt) {
501 ALOGV("setParameter(VOLUME, VOLUME0/1: %04x)", valueInt);
502 track.prevVolume[param-VOLUME0] = track.volume[param-VOLUME0] << 16;
503 track.volume[param-VOLUME0] = valueInt;
504 if (target == VOLUME) {
505 track.prevVolume[param-VOLUME0] = valueInt << 16;
506 track.volumeInc[param-VOLUME0] = 0;
507 } else {
508 int32_t d = (valueInt<<16) - track.prevVolume[param-VOLUME0];
509 int32_t volInc = d / int32_t(mState.frameCount);
510 track.volumeInc[param-VOLUME0] = volInc;
511 if (volInc == 0) {
512 track.prevVolume[param-VOLUME0] = valueInt << 16;
513 }
514 }
515 invalidateState(1 << name);
516 }
517 break;
518 case AUXLEVEL:
519 //ALOG_ASSERT(0 <= valueInt && valueInt <= MAX_GAIN_INT, "bad aux level %d", valueInt);
520 if (track.auxLevel != valueInt) {
521 ALOGV("setParameter(VOLUME, AUXLEVEL: %04x)", valueInt);
522 track.prevAuxLevel = track.auxLevel << 16;
523 track.auxLevel = valueInt;
524 if (target == VOLUME) {
525 track.prevAuxLevel = valueInt << 16;
526 track.auxInc = 0;
527 } else {
528 int32_t d = (valueInt<<16) - track.prevAuxLevel;
529 int32_t volInc = d / int32_t(mState.frameCount);
530 track.auxInc = volInc;
531 if (volInc == 0) {
532 track.prevAuxLevel = valueInt << 16;
533 }
534 }
535 invalidateState(1 << name);
536 }
537 break;
538 default:
539 LOG_FATAL("bad param");
540 }
541 break;
542
543 default:
544 LOG_FATAL("bad target");
545 }
546 }
547
setResampler(uint32_t value,uint32_t devSampleRate)548 bool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate)
549 {
550 if (value != devSampleRate || resampler != NULL) {
551 if (sampleRate != value) {
552 sampleRate = value;
553 if (resampler == NULL) {
554 ALOGV("creating resampler from track %d Hz to device %d Hz", value, devSampleRate);
555 AudioResampler::src_quality quality;
556 // force lowest quality level resampler if use case isn't music or video
557 // FIXME this is flawed for dynamic sample rates, as we choose the resampler
558 // quality level based on the initial ratio, but that could change later.
559 // Should have a way to distinguish tracks with static ratios vs. dynamic ratios.
560 if (!((value == 44100 && devSampleRate == 48000) ||
561 (value == 48000 && devSampleRate == 44100))) {
562 quality = AudioResampler::LOW_QUALITY;
563 } else {
564 quality = AudioResampler::DEFAULT_QUALITY;
565 }
566 resampler = AudioResampler::create(
567 format,
568 // the resampler sees the number of channels after the downmixer, if any
569 downmixerBufferProvider != NULL ? MAX_NUM_CHANNELS : channelCount,
570 devSampleRate, quality);
571 resampler->setLocalTimeFreq(sLocalTimeFreq);
572 }
573 return true;
574 }
575 }
576 return false;
577 }
578
579 inline
adjustVolumeRamp(bool aux)580 void AudioMixer::track_t::adjustVolumeRamp(bool aux)
581 {
582 for (uint32_t i=0 ; i<MAX_NUM_CHANNELS ; i++) {
583 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
584 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
585 volumeInc[i] = 0;
586 prevVolume[i] = volume[i]<<16;
587 }
588 }
589 if (aux) {
590 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
591 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
592 auxInc = 0;
593 prevAuxLevel = auxLevel<<16;
594 }
595 }
596 }
597
getUnreleasedFrames(int name) const598 size_t AudioMixer::getUnreleasedFrames(int name) const
599 {
600 name -= TRACK0;
601 if (uint32_t(name) < MAX_NUM_TRACKS) {
602 return mState.tracks[name].getUnreleasedFrames();
603 }
604 return 0;
605 }
606
setBufferProvider(int name,AudioBufferProvider * bufferProvider)607 void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider)
608 {
609 name -= TRACK0;
610 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
611
612 if (mState.tracks[name].downmixerBufferProvider != NULL) {
613 // update required?
614 if (mState.tracks[name].downmixerBufferProvider->mTrackBufferProvider != bufferProvider) {
615 ALOGV("AudioMixer::setBufferProvider(%p) for downmix", bufferProvider);
616 // setting the buffer provider for a track that gets downmixed consists in:
617 // 1/ setting the buffer provider to the "downmix / buffer provider" wrapper
618 // so it's the one that gets called when the buffer provider is needed,
619 mState.tracks[name].bufferProvider = mState.tracks[name].downmixerBufferProvider;
620 // 2/ saving the buffer provider for the track so the wrapper can use it
621 // when it downmixes.
622 mState.tracks[name].downmixerBufferProvider->mTrackBufferProvider = bufferProvider;
623 }
624 } else {
625 mState.tracks[name].bufferProvider = bufferProvider;
626 }
627 }
628
629
process(int64_t pts)630 void AudioMixer::process(int64_t pts)
631 {
632 mState.hook(&mState, pts);
633 }
634
635
process__validate(state_t * state,int64_t pts)636 void AudioMixer::process__validate(state_t* state, int64_t pts)
637 {
638 ALOGW_IF(!state->needsChanged,
639 "in process__validate() but nothing's invalid");
640
641 uint32_t changed = state->needsChanged;
642 state->needsChanged = 0; // clear the validation flag
643
644 // recompute which tracks are enabled / disabled
645 uint32_t enabled = 0;
646 uint32_t disabled = 0;
647 while (changed) {
648 const int i = 31 - __builtin_clz(changed);
649 const uint32_t mask = 1<<i;
650 changed &= ~mask;
651 track_t& t = state->tracks[i];
652 (t.enabled ? enabled : disabled) |= mask;
653 }
654 state->enabledTracks &= ~disabled;
655 state->enabledTracks |= enabled;
656
657 // compute everything we need...
658 int countActiveTracks = 0;
659 bool all16BitsStereoNoResample = true;
660 bool resampling = false;
661 bool volumeRamp = false;
662 uint32_t en = state->enabledTracks;
663 while (en) {
664 const int i = 31 - __builtin_clz(en);
665 en &= ~(1<<i);
666
667 countActiveTracks++;
668 track_t& t = state->tracks[i];
669 uint32_t n = 0;
670 n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
671 n |= NEEDS_FORMAT_16;
672 n |= t.doesResample() ? NEEDS_RESAMPLE_ENABLED : NEEDS_RESAMPLE_DISABLED;
673 if (t.auxLevel != 0 && t.auxBuffer != NULL) {
674 n |= NEEDS_AUX_ENABLED;
675 }
676
677 if (t.volumeInc[0]|t.volumeInc[1]) {
678 volumeRamp = true;
679 } else if (!t.doesResample() && t.volumeRL == 0) {
680 n |= NEEDS_MUTE_ENABLED;
681 }
682 t.needs = n;
683
684 if ((n & NEEDS_MUTE__MASK) == NEEDS_MUTE_ENABLED) {
685 t.hook = track__nop;
686 } else {
687 if ((n & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED) {
688 all16BitsStereoNoResample = false;
689 }
690 if ((n & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
691 all16BitsStereoNoResample = false;
692 resampling = true;
693 t.hook = track__genericResample;
694 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
695 "Track %d needs downmix + resample", i);
696 } else {
697 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
698 t.hook = track__16BitsMono;
699 all16BitsStereoNoResample = false;
700 }
701 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){
702 t.hook = track__16BitsStereo;
703 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
704 "Track %d needs downmix", i);
705 }
706 }
707 }
708 }
709
710 // select the processing hooks
711 state->hook = process__nop;
712 if (countActiveTracks) {
713 if (resampling) {
714 if (!state->outputTemp) {
715 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
716 }
717 if (!state->resampleTemp) {
718 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
719 }
720 state->hook = process__genericResampling;
721 } else {
722 if (state->outputTemp) {
723 delete [] state->outputTemp;
724 state->outputTemp = NULL;
725 }
726 if (state->resampleTemp) {
727 delete [] state->resampleTemp;
728 state->resampleTemp = NULL;
729 }
730 state->hook = process__genericNoResampling;
731 if (all16BitsStereoNoResample && !volumeRamp) {
732 if (countActiveTracks == 1) {
733 state->hook = process__OneTrack16BitsStereoNoResampling;
734 }
735 }
736 }
737 }
738
739 ALOGV("mixer configuration change: %d activeTracks (%08x) "
740 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
741 countActiveTracks, state->enabledTracks,
742 all16BitsStereoNoResample, resampling, volumeRamp);
743
744 state->hook(state, pts);
745
746 // Now that the volume ramp has been done, set optimal state and
747 // track hooks for subsequent mixer process
748 if (countActiveTracks) {
749 bool allMuted = true;
750 uint32_t en = state->enabledTracks;
751 while (en) {
752 const int i = 31 - __builtin_clz(en);
753 en &= ~(1<<i);
754 track_t& t = state->tracks[i];
755 if (!t.doesResample() && t.volumeRL == 0)
756 {
757 t.needs |= NEEDS_MUTE_ENABLED;
758 t.hook = track__nop;
759 } else {
760 allMuted = false;
761 }
762 }
763 if (allMuted) {
764 state->hook = process__nop;
765 } else if (all16BitsStereoNoResample) {
766 if (countActiveTracks == 1) {
767 state->hook = process__OneTrack16BitsStereoNoResampling;
768 }
769 }
770 }
771 }
772
773
track__genericResample(track_t * t,int32_t * out,size_t outFrameCount,int32_t * temp,int32_t * aux)774 void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount,
775 int32_t* temp, int32_t* aux)
776 {
777 t->resampler->setSampleRate(t->sampleRate);
778
779 // ramp gain - resample to temp buffer and scale/mix in 2nd step
780 if (aux != NULL) {
781 // always resample with unity gain when sending to auxiliary buffer to be able
782 // to apply send level after resampling
783 // TODO: modify each resampler to support aux channel?
784 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
785 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
786 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
787 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
788 volumeRampStereo(t, out, outFrameCount, temp, aux);
789 } else {
790 volumeStereo(t, out, outFrameCount, temp, aux);
791 }
792 } else {
793 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
794 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
795 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
796 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
797 volumeRampStereo(t, out, outFrameCount, temp, aux);
798 }
799
800 // constant gain
801 else {
802 t->resampler->setVolume(t->volume[0], t->volume[1]);
803 t->resampler->resample(out, outFrameCount, t->bufferProvider);
804 }
805 }
806 }
807
track__nop(track_t * t,int32_t * out,size_t outFrameCount,int32_t * temp,int32_t * aux)808 void AudioMixer::track__nop(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp,
809 int32_t* aux)
810 {
811 }
812
volumeRampStereo(track_t * t,int32_t * out,size_t frameCount,int32_t * temp,int32_t * aux)813 void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
814 int32_t* aux)
815 {
816 int32_t vl = t->prevVolume[0];
817 int32_t vr = t->prevVolume[1];
818 const int32_t vlInc = t->volumeInc[0];
819 const int32_t vrInc = t->volumeInc[1];
820
821 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
822 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
823 // (vl + vlInc*frameCount)/65536.0f, frameCount);
824
825 // ramp volume
826 if (CC_UNLIKELY(aux != NULL)) {
827 int32_t va = t->prevAuxLevel;
828 const int32_t vaInc = t->auxInc;
829 int32_t l;
830 int32_t r;
831
832 do {
833 l = (*temp++ >> 12);
834 r = (*temp++ >> 12);
835 *out++ += (vl >> 16) * l;
836 *out++ += (vr >> 16) * r;
837 *aux++ += (va >> 17) * (l + r);
838 vl += vlInc;
839 vr += vrInc;
840 va += vaInc;
841 } while (--frameCount);
842 t->prevAuxLevel = va;
843 } else {
844 do {
845 *out++ += (vl >> 16) * (*temp++ >> 12);
846 *out++ += (vr >> 16) * (*temp++ >> 12);
847 vl += vlInc;
848 vr += vrInc;
849 } while (--frameCount);
850 }
851 t->prevVolume[0] = vl;
852 t->prevVolume[1] = vr;
853 t->adjustVolumeRamp(aux != NULL);
854 }
855
volumeStereo(track_t * t,int32_t * out,size_t frameCount,int32_t * temp,int32_t * aux)856 void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
857 int32_t* aux)
858 {
859 const int16_t vl = t->volume[0];
860 const int16_t vr = t->volume[1];
861
862 if (CC_UNLIKELY(aux != NULL)) {
863 const int16_t va = t->auxLevel;
864 do {
865 int16_t l = (int16_t)(*temp++ >> 12);
866 int16_t r = (int16_t)(*temp++ >> 12);
867 out[0] = mulAdd(l, vl, out[0]);
868 int16_t a = (int16_t)(((int32_t)l + r) >> 1);
869 out[1] = mulAdd(r, vr, out[1]);
870 out += 2;
871 aux[0] = mulAdd(a, va, aux[0]);
872 aux++;
873 } while (--frameCount);
874 } else {
875 do {
876 int16_t l = (int16_t)(*temp++ >> 12);
877 int16_t r = (int16_t)(*temp++ >> 12);
878 out[0] = mulAdd(l, vl, out[0]);
879 out[1] = mulAdd(r, vr, out[1]);
880 out += 2;
881 } while (--frameCount);
882 }
883 }
884
track__16BitsStereo(track_t * t,int32_t * out,size_t frameCount,int32_t * temp,int32_t * aux)885 void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
886 int32_t* aux)
887 {
888 const int16_t *in = static_cast<const int16_t *>(t->in);
889
890 if (CC_UNLIKELY(aux != NULL)) {
891 int32_t l;
892 int32_t r;
893 // ramp gain
894 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
895 int32_t vl = t->prevVolume[0];
896 int32_t vr = t->prevVolume[1];
897 int32_t va = t->prevAuxLevel;
898 const int32_t vlInc = t->volumeInc[0];
899 const int32_t vrInc = t->volumeInc[1];
900 const int32_t vaInc = t->auxInc;
901 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
902 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
903 // (vl + vlInc*frameCount)/65536.0f, frameCount);
904
905 do {
906 l = (int32_t)*in++;
907 r = (int32_t)*in++;
908 *out++ += (vl >> 16) * l;
909 *out++ += (vr >> 16) * r;
910 *aux++ += (va >> 17) * (l + r);
911 vl += vlInc;
912 vr += vrInc;
913 va += vaInc;
914 } while (--frameCount);
915
916 t->prevVolume[0] = vl;
917 t->prevVolume[1] = vr;
918 t->prevAuxLevel = va;
919 t->adjustVolumeRamp(true);
920 }
921
922 // constant gain
923 else {
924 const uint32_t vrl = t->volumeRL;
925 const int16_t va = (int16_t)t->auxLevel;
926 do {
927 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
928 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
929 in += 2;
930 out[0] = mulAddRL(1, rl, vrl, out[0]);
931 out[1] = mulAddRL(0, rl, vrl, out[1]);
932 out += 2;
933 aux[0] = mulAdd(a, va, aux[0]);
934 aux++;
935 } while (--frameCount);
936 }
937 } else {
938 // ramp gain
939 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
940 int32_t vl = t->prevVolume[0];
941 int32_t vr = t->prevVolume[1];
942 const int32_t vlInc = t->volumeInc[0];
943 const int32_t vrInc = t->volumeInc[1];
944
945 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
946 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
947 // (vl + vlInc*frameCount)/65536.0f, frameCount);
948
949 do {
950 *out++ += (vl >> 16) * (int32_t) *in++;
951 *out++ += (vr >> 16) * (int32_t) *in++;
952 vl += vlInc;
953 vr += vrInc;
954 } while (--frameCount);
955
956 t->prevVolume[0] = vl;
957 t->prevVolume[1] = vr;
958 t->adjustVolumeRamp(false);
959 }
960
961 // constant gain
962 else {
963 const uint32_t vrl = t->volumeRL;
964 do {
965 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
966 in += 2;
967 out[0] = mulAddRL(1, rl, vrl, out[0]);
968 out[1] = mulAddRL(0, rl, vrl, out[1]);
969 out += 2;
970 } while (--frameCount);
971 }
972 }
973 t->in = in;
974 }
975
track__16BitsMono(track_t * t,int32_t * out,size_t frameCount,int32_t * temp,int32_t * aux)976 void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
977 int32_t* aux)
978 {
979 const int16_t *in = static_cast<int16_t const *>(t->in);
980
981 if (CC_UNLIKELY(aux != NULL)) {
982 // ramp gain
983 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
984 int32_t vl = t->prevVolume[0];
985 int32_t vr = t->prevVolume[1];
986 int32_t va = t->prevAuxLevel;
987 const int32_t vlInc = t->volumeInc[0];
988 const int32_t vrInc = t->volumeInc[1];
989 const int32_t vaInc = t->auxInc;
990
991 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
992 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
993 // (vl + vlInc*frameCount)/65536.0f, frameCount);
994
995 do {
996 int32_t l = *in++;
997 *out++ += (vl >> 16) * l;
998 *out++ += (vr >> 16) * l;
999 *aux++ += (va >> 16) * l;
1000 vl += vlInc;
1001 vr += vrInc;
1002 va += vaInc;
1003 } while (--frameCount);
1004
1005 t->prevVolume[0] = vl;
1006 t->prevVolume[1] = vr;
1007 t->prevAuxLevel = va;
1008 t->adjustVolumeRamp(true);
1009 }
1010 // constant gain
1011 else {
1012 const int16_t vl = t->volume[0];
1013 const int16_t vr = t->volume[1];
1014 const int16_t va = (int16_t)t->auxLevel;
1015 do {
1016 int16_t l = *in++;
1017 out[0] = mulAdd(l, vl, out[0]);
1018 out[1] = mulAdd(l, vr, out[1]);
1019 out += 2;
1020 aux[0] = mulAdd(l, va, aux[0]);
1021 aux++;
1022 } while (--frameCount);
1023 }
1024 } else {
1025 // ramp gain
1026 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
1027 int32_t vl = t->prevVolume[0];
1028 int32_t vr = t->prevVolume[1];
1029 const int32_t vlInc = t->volumeInc[0];
1030 const int32_t vrInc = t->volumeInc[1];
1031
1032 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
1033 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1034 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1035
1036 do {
1037 int32_t l = *in++;
1038 *out++ += (vl >> 16) * l;
1039 *out++ += (vr >> 16) * l;
1040 vl += vlInc;
1041 vr += vrInc;
1042 } while (--frameCount);
1043
1044 t->prevVolume[0] = vl;
1045 t->prevVolume[1] = vr;
1046 t->adjustVolumeRamp(false);
1047 }
1048 // constant gain
1049 else {
1050 const int16_t vl = t->volume[0];
1051 const int16_t vr = t->volume[1];
1052 do {
1053 int16_t l = *in++;
1054 out[0] = mulAdd(l, vl, out[0]);
1055 out[1] = mulAdd(l, vr, out[1]);
1056 out += 2;
1057 } while (--frameCount);
1058 }
1059 }
1060 t->in = in;
1061 }
1062
1063 // no-op case
process__nop(state_t * state,int64_t pts)1064 void AudioMixer::process__nop(state_t* state, int64_t pts)
1065 {
1066 uint32_t e0 = state->enabledTracks;
1067 size_t bufSize = state->frameCount * sizeof(int16_t) * MAX_NUM_CHANNELS;
1068 while (e0) {
1069 // process by group of tracks with same output buffer to
1070 // avoid multiple memset() on same buffer
1071 uint32_t e1 = e0, e2 = e0;
1072 int i = 31 - __builtin_clz(e1);
1073 {
1074 track_t& t1 = state->tracks[i];
1075 e2 &= ~(1<<i);
1076 while (e2) {
1077 i = 31 - __builtin_clz(e2);
1078 e2 &= ~(1<<i);
1079 track_t& t2 = state->tracks[i];
1080 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
1081 e1 &= ~(1<<i);
1082 }
1083 }
1084 e0 &= ~(e1);
1085
1086 memset(t1.mainBuffer, 0, bufSize);
1087 }
1088
1089 while (e1) {
1090 i = 31 - __builtin_clz(e1);
1091 e1 &= ~(1<<i);
1092 {
1093 track_t& t3 = state->tracks[i];
1094 size_t outFrames = state->frameCount;
1095 while (outFrames) {
1096 t3.buffer.frameCount = outFrames;
1097 int64_t outputPTS = calculateOutputPTS(
1098 t3, pts, state->frameCount - outFrames);
1099 t3.bufferProvider->getNextBuffer(&t3.buffer, outputPTS);
1100 if (t3.buffer.raw == NULL) break;
1101 outFrames -= t3.buffer.frameCount;
1102 t3.bufferProvider->releaseBuffer(&t3.buffer);
1103 }
1104 }
1105 }
1106 }
1107 }
1108
1109 // generic code without resampling
process__genericNoResampling(state_t * state,int64_t pts)1110 void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts)
1111 {
1112 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
1113
1114 // acquire each track's buffer
1115 uint32_t enabledTracks = state->enabledTracks;
1116 uint32_t e0 = enabledTracks;
1117 while (e0) {
1118 const int i = 31 - __builtin_clz(e0);
1119 e0 &= ~(1<<i);
1120 track_t& t = state->tracks[i];
1121 t.buffer.frameCount = state->frameCount;
1122 t.bufferProvider->getNextBuffer(&t.buffer, pts);
1123 t.frameCount = t.buffer.frameCount;
1124 t.in = t.buffer.raw;
1125 }
1126
1127 e0 = enabledTracks;
1128 while (e0) {
1129 // process by group of tracks with same output buffer to
1130 // optimize cache use
1131 uint32_t e1 = e0, e2 = e0;
1132 int j = 31 - __builtin_clz(e1);
1133 track_t& t1 = state->tracks[j];
1134 e2 &= ~(1<<j);
1135 while (e2) {
1136 j = 31 - __builtin_clz(e2);
1137 e2 &= ~(1<<j);
1138 track_t& t2 = state->tracks[j];
1139 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
1140 e1 &= ~(1<<j);
1141 }
1142 }
1143 e0 &= ~(e1);
1144 // this assumes output 16 bits stereo, no resampling
1145 int32_t *out = t1.mainBuffer;
1146 size_t numFrames = 0;
1147 do {
1148 memset(outTemp, 0, sizeof(outTemp));
1149 e2 = e1;
1150 while (e2) {
1151 const int i = 31 - __builtin_clz(e2);
1152 e2 &= ~(1<<i);
1153 track_t& t = state->tracks[i];
1154 size_t outFrames = BLOCKSIZE;
1155 int32_t *aux = NULL;
1156 if (CC_UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED)) {
1157 aux = t.auxBuffer + numFrames;
1158 }
1159 while (outFrames) {
1160 // t.in == NULL can happen if the track was flushed just after having
1161 // been enabled for mixing.
1162 if (t.in == NULL) {
1163 enabledTracks &= ~(1<<i);
1164 e1 &= ~(1<<i);
1165 break;
1166 }
1167 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
1168 if (inFrames) {
1169 t.hook(&t, outTemp + (BLOCKSIZE-outFrames)*MAX_NUM_CHANNELS, inFrames,
1170 state->resampleTemp, aux);
1171 t.frameCount -= inFrames;
1172 outFrames -= inFrames;
1173 if (CC_UNLIKELY(aux != NULL)) {
1174 aux += inFrames;
1175 }
1176 }
1177 if (t.frameCount == 0 && outFrames) {
1178 t.bufferProvider->releaseBuffer(&t.buffer);
1179 t.buffer.frameCount = (state->frameCount - numFrames) -
1180 (BLOCKSIZE - outFrames);
1181 int64_t outputPTS = calculateOutputPTS(
1182 t, pts, numFrames + (BLOCKSIZE - outFrames));
1183 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
1184 t.in = t.buffer.raw;
1185 if (t.in == NULL) {
1186 enabledTracks &= ~(1<<i);
1187 e1 &= ~(1<<i);
1188 break;
1189 }
1190 t.frameCount = t.buffer.frameCount;
1191 }
1192 }
1193 }
1194 ditherAndClamp(out, outTemp, BLOCKSIZE);
1195 out += BLOCKSIZE;
1196 numFrames += BLOCKSIZE;
1197 } while (numFrames < state->frameCount);
1198 }
1199
1200 // release each track's buffer
1201 e0 = enabledTracks;
1202 while (e0) {
1203 const int i = 31 - __builtin_clz(e0);
1204 e0 &= ~(1<<i);
1205 track_t& t = state->tracks[i];
1206 t.bufferProvider->releaseBuffer(&t.buffer);
1207 }
1208 }
1209
1210
1211 // generic code with resampling
process__genericResampling(state_t * state,int64_t pts)1212 void AudioMixer::process__genericResampling(state_t* state, int64_t pts)
1213 {
1214 // this const just means that local variable outTemp doesn't change
1215 int32_t* const outTemp = state->outputTemp;
1216 const size_t size = sizeof(int32_t) * MAX_NUM_CHANNELS * state->frameCount;
1217
1218 size_t numFrames = state->frameCount;
1219
1220 uint32_t e0 = state->enabledTracks;
1221 while (e0) {
1222 // process by group of tracks with same output buffer
1223 // to optimize cache use
1224 uint32_t e1 = e0, e2 = e0;
1225 int j = 31 - __builtin_clz(e1);
1226 track_t& t1 = state->tracks[j];
1227 e2 &= ~(1<<j);
1228 while (e2) {
1229 j = 31 - __builtin_clz(e2);
1230 e2 &= ~(1<<j);
1231 track_t& t2 = state->tracks[j];
1232 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
1233 e1 &= ~(1<<j);
1234 }
1235 }
1236 e0 &= ~(e1);
1237 int32_t *out = t1.mainBuffer;
1238 memset(outTemp, 0, size);
1239 while (e1) {
1240 const int i = 31 - __builtin_clz(e1);
1241 e1 &= ~(1<<i);
1242 track_t& t = state->tracks[i];
1243 int32_t *aux = NULL;
1244 if (CC_UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED)) {
1245 aux = t.auxBuffer;
1246 }
1247
1248 // this is a little goofy, on the resampling case we don't
1249 // acquire/release the buffers because it's done by
1250 // the resampler.
1251 if ((t.needs & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
1252 t.resampler->setPTS(pts);
1253 t.hook(&t, outTemp, numFrames, state->resampleTemp, aux);
1254 } else {
1255
1256 size_t outFrames = 0;
1257
1258 while (outFrames < numFrames) {
1259 t.buffer.frameCount = numFrames - outFrames;
1260 int64_t outputPTS = calculateOutputPTS(t, pts, outFrames);
1261 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
1262 t.in = t.buffer.raw;
1263 // t.in == NULL can happen if the track was flushed just after having
1264 // been enabled for mixing.
1265 if (t.in == NULL) break;
1266
1267 if (CC_UNLIKELY(aux != NULL)) {
1268 aux += outFrames;
1269 }
1270 t.hook(&t, outTemp + outFrames*MAX_NUM_CHANNELS, t.buffer.frameCount,
1271 state->resampleTemp, aux);
1272 outFrames += t.buffer.frameCount;
1273 t.bufferProvider->releaseBuffer(&t.buffer);
1274 }
1275 }
1276 }
1277 ditherAndClamp(out, outTemp, numFrames);
1278 }
1279 }
1280
1281 // one track, 16 bits stereo without resampling is the most common case
process__OneTrack16BitsStereoNoResampling(state_t * state,int64_t pts)1282 void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state,
1283 int64_t pts)
1284 {
1285 // This method is only called when state->enabledTracks has exactly
1286 // one bit set. The asserts below would verify this, but are commented out
1287 // since the whole point of this method is to optimize performance.
1288 //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled");
1289 const int i = 31 - __builtin_clz(state->enabledTracks);
1290 //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
1291 const track_t& t = state->tracks[i];
1292
1293 AudioBufferProvider::Buffer& b(t.buffer);
1294
1295 int32_t* out = t.mainBuffer;
1296 size_t numFrames = state->frameCount;
1297
1298 const int16_t vl = t.volume[0];
1299 const int16_t vr = t.volume[1];
1300 const uint32_t vrl = t.volumeRL;
1301 while (numFrames) {
1302 b.frameCount = numFrames;
1303 int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer);
1304 t.bufferProvider->getNextBuffer(&b, outputPTS);
1305 const int16_t *in = b.i16;
1306
1307 // in == NULL can happen if the track was flushed just after having
1308 // been enabled for mixing.
1309 if (in == NULL || ((unsigned long)in & 3)) {
1310 memset(out, 0, numFrames*MAX_NUM_CHANNELS*sizeof(int16_t));
1311 ALOGE_IF(((unsigned long)in & 3), "process stereo track: input buffer alignment pb: "
1312 "buffer %p track %d, channels %d, needs %08x",
1313 in, i, t.channelCount, t.needs);
1314 return;
1315 }
1316 size_t outFrames = b.frameCount;
1317
1318 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN || uint32_t(vr) > UNITY_GAIN)) {
1319 // volume is boosted, so we might need to clamp even though
1320 // we process only one track.
1321 do {
1322 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1323 in += 2;
1324 int32_t l = mulRL(1, rl, vrl) >> 12;
1325 int32_t r = mulRL(0, rl, vrl) >> 12;
1326 // clamping...
1327 l = clamp16(l);
1328 r = clamp16(r);
1329 *out++ = (r<<16) | (l & 0xFFFF);
1330 } while (--outFrames);
1331 } else {
1332 do {
1333 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1334 in += 2;
1335 int32_t l = mulRL(1, rl, vrl) >> 12;
1336 int32_t r = mulRL(0, rl, vrl) >> 12;
1337 *out++ = (r<<16) | (l & 0xFFFF);
1338 } while (--outFrames);
1339 }
1340 numFrames -= b.frameCount;
1341 t.bufferProvider->releaseBuffer(&b);
1342 }
1343 }
1344
1345 #if 0
1346 // 2 tracks is also a common case
1347 // NEVER used in current implementation of process__validate()
1348 // only use if the 2 tracks have the same output buffer
1349 void AudioMixer::process__TwoTracks16BitsStereoNoResampling(state_t* state,
1350 int64_t pts)
1351 {
1352 int i;
1353 uint32_t en = state->enabledTracks;
1354
1355 i = 31 - __builtin_clz(en);
1356 const track_t& t0 = state->tracks[i];
1357 AudioBufferProvider::Buffer& b0(t0.buffer);
1358
1359 en &= ~(1<<i);
1360 i = 31 - __builtin_clz(en);
1361 const track_t& t1 = state->tracks[i];
1362 AudioBufferProvider::Buffer& b1(t1.buffer);
1363
1364 const int16_t *in0;
1365 const int16_t vl0 = t0.volume[0];
1366 const int16_t vr0 = t0.volume[1];
1367 size_t frameCount0 = 0;
1368
1369 const int16_t *in1;
1370 const int16_t vl1 = t1.volume[0];
1371 const int16_t vr1 = t1.volume[1];
1372 size_t frameCount1 = 0;
1373
1374 //FIXME: only works if two tracks use same buffer
1375 int32_t* out = t0.mainBuffer;
1376 size_t numFrames = state->frameCount;
1377 const int16_t *buff = NULL;
1378
1379
1380 while (numFrames) {
1381
1382 if (frameCount0 == 0) {
1383 b0.frameCount = numFrames;
1384 int64_t outputPTS = calculateOutputPTS(t0, pts,
1385 out - t0.mainBuffer);
1386 t0.bufferProvider->getNextBuffer(&b0, outputPTS);
1387 if (b0.i16 == NULL) {
1388 if (buff == NULL) {
1389 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1390 }
1391 in0 = buff;
1392 b0.frameCount = numFrames;
1393 } else {
1394 in0 = b0.i16;
1395 }
1396 frameCount0 = b0.frameCount;
1397 }
1398 if (frameCount1 == 0) {
1399 b1.frameCount = numFrames;
1400 int64_t outputPTS = calculateOutputPTS(t1, pts,
1401 out - t0.mainBuffer);
1402 t1.bufferProvider->getNextBuffer(&b1, outputPTS);
1403 if (b1.i16 == NULL) {
1404 if (buff == NULL) {
1405 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1406 }
1407 in1 = buff;
1408 b1.frameCount = numFrames;
1409 } else {
1410 in1 = b1.i16;
1411 }
1412 frameCount1 = b1.frameCount;
1413 }
1414
1415 size_t outFrames = frameCount0 < frameCount1?frameCount0:frameCount1;
1416
1417 numFrames -= outFrames;
1418 frameCount0 -= outFrames;
1419 frameCount1 -= outFrames;
1420
1421 do {
1422 int32_t l0 = *in0++;
1423 int32_t r0 = *in0++;
1424 l0 = mul(l0, vl0);
1425 r0 = mul(r0, vr0);
1426 int32_t l = *in1++;
1427 int32_t r = *in1++;
1428 l = mulAdd(l, vl1, l0) >> 12;
1429 r = mulAdd(r, vr1, r0) >> 12;
1430 // clamping...
1431 l = clamp16(l);
1432 r = clamp16(r);
1433 *out++ = (r<<16) | (l & 0xFFFF);
1434 } while (--outFrames);
1435
1436 if (frameCount0 == 0) {
1437 t0.bufferProvider->releaseBuffer(&b0);
1438 }
1439 if (frameCount1 == 0) {
1440 t1.bufferProvider->releaseBuffer(&b1);
1441 }
1442 }
1443
1444 delete [] buff;
1445 }
1446 #endif
1447
calculateOutputPTS(const track_t & t,int64_t basePTS,int outputFrameIndex)1448 int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS,
1449 int outputFrameIndex)
1450 {
1451 if (AudioBufferProvider::kInvalidPTS == basePTS)
1452 return AudioBufferProvider::kInvalidPTS;
1453
1454 return basePTS + ((outputFrameIndex * sLocalTimeFreq) / t.sampleRate);
1455 }
1456
1457 /*static*/ uint64_t AudioMixer::sLocalTimeFreq;
1458 /*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT;
1459
sInitRoutine()1460 /*static*/ void AudioMixer::sInitRoutine()
1461 {
1462 LocalClock lc;
1463 sLocalTimeFreq = lc.getLocalFreq();
1464 }
1465
1466 // ----------------------------------------------------------------------------
1467 }; // namespace android
1468