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