AudioMixer.cpp revision a08810b2feafeec88870c7c1f01efc39ee8e0d78
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 <math.h> 26#include <sys/types.h> 27 28#include <utils/Errors.h> 29#include <utils/Log.h> 30 31#include <cutils/bitops.h> 32#include <cutils/compiler.h> 33#include <utils/Debug.h> 34 35#include <system/audio.h> 36 37#include <audio_utils/primitives.h> 38#include <audio_utils/format.h> 39#include <common_time/local_clock.h> 40#include <common_time/cc_helper.h> 41 42#include <media/EffectsFactoryApi.h> 43 44#include "AudioMixerOps.h" 45#include "AudioMixer.h" 46 47// Use the FCC_2 macro for code assuming Fixed Channel Count of 2 and 48// whose stereo assumption may need to be revisited later. 49#ifndef FCC_2 50#define FCC_2 2 51#endif 52 53/* VERY_VERY_VERBOSE_LOGGING will show exactly which process hook and track hook is 54 * being used. This is a considerable amount of log spam, so don't enable unless you 55 * are verifying the hook based code. 56 */ 57//#define VERY_VERY_VERBOSE_LOGGING 58#ifdef VERY_VERY_VERBOSE_LOGGING 59#define ALOGVV ALOGV 60//define ALOGVV printf // for test-mixer.cpp 61#else 62#define ALOGVV(a...) do { } while (0) 63#endif 64 65#ifndef ARRAY_SIZE 66#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) 67#endif 68 69// Set kUseNewMixer to true to use the new mixer engine. Otherwise the 70// original code will be used. This is false for now. 71static const bool kUseNewMixer = false; 72 73// Set kUseFloat to true to allow floating input into the mixer engine. 74// If kUseNewMixer is false, this is ignored or may be overridden internally 75// because of downmix/upmix support. 76static const bool kUseFloat = true; 77 78// Set to default copy buffer size in frames for input processing. 79static const size_t kCopyBufferFrameCount = 256; 80 81namespace android { 82 83// ---------------------------------------------------------------------------- 84 85template <typename T> 86T min(const T& a, const T& b) 87{ 88 return a < b ? a : b; 89} 90 91AudioMixer::CopyBufferProvider::CopyBufferProvider(size_t inputFrameSize, 92 size_t outputFrameSize, size_t bufferFrameCount) : 93 mInputFrameSize(inputFrameSize), 94 mOutputFrameSize(outputFrameSize), 95 mLocalBufferFrameCount(bufferFrameCount), 96 mLocalBufferData(NULL), 97 mConsumed(0) 98{ 99 ALOGV("CopyBufferProvider(%p)(%zu, %zu, %zu)", this, 100 inputFrameSize, outputFrameSize, bufferFrameCount); 101 LOG_ALWAYS_FATAL_IF(inputFrameSize < outputFrameSize && bufferFrameCount == 0, 102 "Requires local buffer if inputFrameSize(%d) < outputFrameSize(%d)", 103 inputFrameSize, outputFrameSize); 104 if (mLocalBufferFrameCount) { 105 (void)posix_memalign(&mLocalBufferData, 32, mLocalBufferFrameCount * mOutputFrameSize); 106 } 107 mBuffer.frameCount = 0; 108} 109 110AudioMixer::CopyBufferProvider::~CopyBufferProvider() 111{ 112 ALOGV("~CopyBufferProvider(%p)", this); 113 if (mBuffer.frameCount != 0) { 114 mTrackBufferProvider->releaseBuffer(&mBuffer); 115 } 116 free(mLocalBufferData); 117} 118 119status_t AudioMixer::CopyBufferProvider::getNextBuffer(AudioBufferProvider::Buffer *pBuffer, 120 int64_t pts) 121{ 122 //ALOGV("CopyBufferProvider(%p)::getNextBuffer(%p (%zu), %lld)", 123 // this, pBuffer, pBuffer->frameCount, pts); 124 if (mLocalBufferFrameCount == 0) { 125 status_t res = mTrackBufferProvider->getNextBuffer(pBuffer, pts); 126 if (res == OK) { 127 copyFrames(pBuffer->raw, pBuffer->raw, pBuffer->frameCount); 128 } 129 return res; 130 } 131 if (mBuffer.frameCount == 0) { 132 mBuffer.frameCount = pBuffer->frameCount; 133 status_t res = mTrackBufferProvider->getNextBuffer(&mBuffer, pts); 134 // At one time an upstream buffer provider had 135 // res == OK and mBuffer.frameCount == 0, doesn't seem to happen now 7/18/2014. 136 // 137 // By API spec, if res != OK, then mBuffer.frameCount == 0. 138 // but there may be improper implementations. 139 ALOG_ASSERT(res == OK || mBuffer.frameCount == 0); 140 if (res != OK || mBuffer.frameCount == 0) { // not needed by API spec, but to be safe. 141 pBuffer->raw = NULL; 142 pBuffer->frameCount = 0; 143 return res; 144 } 145 mConsumed = 0; 146 } 147 ALOG_ASSERT(mConsumed < mBuffer.frameCount); 148 size_t count = min(mLocalBufferFrameCount, mBuffer.frameCount - mConsumed); 149 count = min(count, pBuffer->frameCount); 150 pBuffer->raw = mLocalBufferData; 151 pBuffer->frameCount = count; 152 copyFrames(pBuffer->raw, (uint8_t*)mBuffer.raw + mConsumed * mInputFrameSize, 153 pBuffer->frameCount); 154 return OK; 155} 156 157void AudioMixer::CopyBufferProvider::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) 158{ 159 //ALOGV("CopyBufferProvider(%p)::releaseBuffer(%p(%zu))", 160 // this, pBuffer, pBuffer->frameCount); 161 if (mLocalBufferFrameCount == 0) { 162 mTrackBufferProvider->releaseBuffer(pBuffer); 163 return; 164 } 165 // LOG_ALWAYS_FATAL_IF(pBuffer->frameCount == 0, "Invalid framecount"); 166 mConsumed += pBuffer->frameCount; // TODO: update for efficiency to reuse existing content 167 if (mConsumed != 0 && mConsumed >= mBuffer.frameCount) { 168 mTrackBufferProvider->releaseBuffer(&mBuffer); 169 ALOG_ASSERT(mBuffer.frameCount == 0); 170 } 171 pBuffer->raw = NULL; 172 pBuffer->frameCount = 0; 173} 174 175void AudioMixer::CopyBufferProvider::reset() 176{ 177 if (mBuffer.frameCount != 0) { 178 mTrackBufferProvider->releaseBuffer(&mBuffer); 179 } 180 mConsumed = 0; 181} 182 183AudioMixer::DownmixerBufferProvider::DownmixerBufferProvider( 184 audio_channel_mask_t inputChannelMask, 185 audio_channel_mask_t outputChannelMask, audio_format_t format, 186 uint32_t sampleRate, int32_t sessionId, size_t bufferFrameCount) : 187 CopyBufferProvider( 188 audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(inputChannelMask), 189 audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(outputChannelMask), 190 bufferFrameCount) // set bufferFrameCount to 0 to do in-place 191{ 192 ALOGV("DownmixerBufferProvider(%p)(%#x, %#x, %#x %u %d)", 193 this, inputChannelMask, outputChannelMask, format, 194 sampleRate, sessionId); 195 if (!sIsMultichannelCapable 196 || EffectCreate(&sDwnmFxDesc.uuid, 197 sessionId, 198 SESSION_ID_INVALID_AND_IGNORED, 199 &mDownmixHandle) != 0) { 200 ALOGE("DownmixerBufferProvider() error creating downmixer effect"); 201 mDownmixHandle = NULL; 202 return; 203 } 204 // channel input configuration will be overridden per-track 205 mDownmixConfig.inputCfg.channels = inputChannelMask; // FIXME: Should be bits 206 mDownmixConfig.outputCfg.channels = outputChannelMask; // FIXME: should be bits 207 mDownmixConfig.inputCfg.format = format; 208 mDownmixConfig.outputCfg.format = format; 209 mDownmixConfig.inputCfg.samplingRate = sampleRate; 210 mDownmixConfig.outputCfg.samplingRate = sampleRate; 211 mDownmixConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; 212 mDownmixConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; 213 // input and output buffer provider, and frame count will not be used as the downmix effect 214 // process() function is called directly (see DownmixerBufferProvider::getNextBuffer()) 215 mDownmixConfig.inputCfg.mask = EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | 216 EFFECT_CONFIG_FORMAT | EFFECT_CONFIG_ACC_MODE; 217 mDownmixConfig.outputCfg.mask = mDownmixConfig.inputCfg.mask; 218 219 int cmdStatus; 220 uint32_t replySize = sizeof(int); 221 222 // Configure downmixer 223 status_t status = (*mDownmixHandle)->command(mDownmixHandle, 224 EFFECT_CMD_SET_CONFIG /*cmdCode*/, sizeof(effect_config_t) /*cmdSize*/, 225 &mDownmixConfig /*pCmdData*/, 226 &replySize, &cmdStatus /*pReplyData*/); 227 if (status != 0 || cmdStatus != 0) { 228 ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while configuring downmixer", 229 status, cmdStatus); 230 EffectRelease(mDownmixHandle); 231 mDownmixHandle = NULL; 232 return; 233 } 234 235 // Enable downmixer 236 replySize = sizeof(int); 237 status = (*mDownmixHandle)->command(mDownmixHandle, 238 EFFECT_CMD_ENABLE /*cmdCode*/, 0 /*cmdSize*/, NULL /*pCmdData*/, 239 &replySize, &cmdStatus /*pReplyData*/); 240 if (status != 0 || cmdStatus != 0) { 241 ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while enabling downmixer", 242 status, cmdStatus); 243 EffectRelease(mDownmixHandle); 244 mDownmixHandle = NULL; 245 return; 246 } 247 248 // Set downmix type 249 // parameter size rounded for padding on 32bit boundary 250 const int psizePadded = ((sizeof(downmix_params_t) - 1)/sizeof(int) + 1) * sizeof(int); 251 const int downmixParamSize = 252 sizeof(effect_param_t) + psizePadded + sizeof(downmix_type_t); 253 effect_param_t * const param = (effect_param_t *) malloc(downmixParamSize); 254 param->psize = sizeof(downmix_params_t); 255 const downmix_params_t downmixParam = DOWNMIX_PARAM_TYPE; 256 memcpy(param->data, &downmixParam, param->psize); 257 const downmix_type_t downmixType = DOWNMIX_TYPE_FOLD; 258 param->vsize = sizeof(downmix_type_t); 259 memcpy(param->data + psizePadded, &downmixType, param->vsize); 260 replySize = sizeof(int); 261 status = (*mDownmixHandle)->command(mDownmixHandle, 262 EFFECT_CMD_SET_PARAM /* cmdCode */, downmixParamSize /* cmdSize */, 263 param /*pCmdData*/, &replySize, &cmdStatus /*pReplyData*/); 264 free(param); 265 if (status != 0 || cmdStatus != 0) { 266 ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while setting downmix type", 267 status, cmdStatus); 268 EffectRelease(mDownmixHandle); 269 mDownmixHandle = NULL; 270 return; 271 } 272 ALOGV("DownmixerBufferProvider() downmix type set to %d", (int) downmixType); 273} 274 275AudioMixer::DownmixerBufferProvider::~DownmixerBufferProvider() 276{ 277 ALOGV("~DownmixerBufferProvider (%p)", this); 278 EffectRelease(mDownmixHandle); 279 mDownmixHandle = NULL; 280} 281 282void AudioMixer::DownmixerBufferProvider::copyFrames(void *dst, const void *src, size_t frames) 283{ 284 mDownmixConfig.inputCfg.buffer.frameCount = frames; 285 mDownmixConfig.inputCfg.buffer.raw = const_cast<void *>(src); 286 mDownmixConfig.outputCfg.buffer.frameCount = frames; 287 mDownmixConfig.outputCfg.buffer.raw = dst; 288 // may be in-place if src == dst. 289 status_t res = (*mDownmixHandle)->process(mDownmixHandle, 290 &mDownmixConfig.inputCfg.buffer, &mDownmixConfig.outputCfg.buffer); 291 ALOGE_IF(res != OK, "DownmixBufferProvider error %d", res); 292} 293 294/* call once in a pthread_once handler. */ 295/*static*/ status_t AudioMixer::DownmixerBufferProvider::init() 296{ 297 // find multichannel downmix effect if we have to play multichannel content 298 uint32_t numEffects = 0; 299 int ret = EffectQueryNumberEffects(&numEffects); 300 if (ret != 0) { 301 ALOGE("AudioMixer() error %d querying number of effects", ret); 302 return NO_INIT; 303 } 304 ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects); 305 306 for (uint32_t i = 0 ; i < numEffects ; i++) { 307 if (EffectQueryEffect(i, &sDwnmFxDesc) == 0) { 308 ALOGV("effect %d is called %s", i, sDwnmFxDesc.name); 309 if (memcmp(&sDwnmFxDesc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) { 310 ALOGI("found effect \"%s\" from %s", 311 sDwnmFxDesc.name, sDwnmFxDesc.implementor); 312 sIsMultichannelCapable = true; 313 break; 314 } 315 } 316 } 317 ALOGW_IF(!sIsMultichannelCapable, "unable to find downmix effect"); 318 return NO_INIT; 319} 320 321/*static*/ bool AudioMixer::DownmixerBufferProvider::sIsMultichannelCapable = false; 322/*static*/ effect_descriptor_t AudioMixer::DownmixerBufferProvider::sDwnmFxDesc; 323 324AudioMixer::RemixBufferProvider::RemixBufferProvider(audio_channel_mask_t inputChannelMask, 325 audio_channel_mask_t outputChannelMask, audio_format_t format, 326 size_t bufferFrameCount) : 327 CopyBufferProvider( 328 audio_bytes_per_sample(format) 329 * audio_channel_count_from_out_mask(inputChannelMask), 330 audio_bytes_per_sample(format) 331 * audio_channel_count_from_out_mask(outputChannelMask), 332 bufferFrameCount), 333 mFormat(format), 334 mSampleSize(audio_bytes_per_sample(format)), 335 mInputChannels(audio_channel_count_from_out_mask(inputChannelMask)), 336 mOutputChannels(audio_channel_count_from_out_mask(outputChannelMask)) 337{ 338 ALOGV("RemixBufferProvider(%p)(%#x, %#x, %#x) %d %d", 339 this, format, inputChannelMask, outputChannelMask, 340 mInputChannels, mOutputChannels); 341 // TODO: consider channel representation in index array formulation 342 // We ignore channel representation, and just use the bits. 343 memcpy_by_index_array_initialization(mIdxAry, ARRAY_SIZE(mIdxAry), 344 audio_channel_mask_get_bits(outputChannelMask), 345 audio_channel_mask_get_bits(inputChannelMask)); 346} 347 348void AudioMixer::RemixBufferProvider::copyFrames(void *dst, const void *src, size_t frames) 349{ 350 memcpy_by_index_array(dst, mOutputChannels, 351 src, mInputChannels, mIdxAry, mSampleSize, frames); 352} 353 354AudioMixer::ReformatBufferProvider::ReformatBufferProvider(int32_t channels, 355 audio_format_t inputFormat, audio_format_t outputFormat, 356 size_t bufferFrameCount) : 357 CopyBufferProvider( 358 channels * audio_bytes_per_sample(inputFormat), 359 channels * audio_bytes_per_sample(outputFormat), 360 bufferFrameCount), 361 mChannels(channels), 362 mInputFormat(inputFormat), 363 mOutputFormat(outputFormat) 364{ 365 ALOGV("ReformatBufferProvider(%p)(%d, %#x, %#x)", this, channels, inputFormat, outputFormat); 366} 367 368void AudioMixer::ReformatBufferProvider::copyFrames(void *dst, const void *src, size_t frames) 369{ 370 memcpy_by_audio_format(dst, mOutputFormat, src, mInputFormat, frames * mChannels); 371} 372 373// ---------------------------------------------------------------------------- 374 375// Ensure mConfiguredNames bitmask is initialized properly on all architectures. 376// The value of 1 << x is undefined in C when x >= 32. 377 378AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks) 379 : mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1), 380 mSampleRate(sampleRate) 381{ 382 // AudioMixer is not yet capable of multi-channel beyond stereo 383 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(2 == MAX_NUM_CHANNELS); 384 385 ALOG_ASSERT(maxNumTracks <= MAX_NUM_TRACKS, "maxNumTracks %u > MAX_NUM_TRACKS %u", 386 maxNumTracks, MAX_NUM_TRACKS); 387 388 // AudioMixer is not yet capable of more than 32 active track inputs 389 ALOG_ASSERT(32 >= MAX_NUM_TRACKS, "bad MAX_NUM_TRACKS %d", MAX_NUM_TRACKS); 390 391 // AudioMixer is not yet capable of multi-channel output beyond stereo 392 ALOG_ASSERT(2 == MAX_NUM_CHANNELS, "bad MAX_NUM_CHANNELS %d", MAX_NUM_CHANNELS); 393 394 pthread_once(&sOnceControl, &sInitRoutine); 395 396 mState.enabledTracks= 0; 397 mState.needsChanged = 0; 398 mState.frameCount = frameCount; 399 mState.hook = process__nop; 400 mState.outputTemp = NULL; 401 mState.resampleTemp = NULL; 402 mState.mLog = &mDummyLog; 403 // mState.reserved 404 405 // FIXME Most of the following initialization is probably redundant since 406 // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0 407 // and mTrackNames is initially 0. However, leave it here until that's verified. 408 track_t* t = mState.tracks; 409 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) { 410 t->resampler = NULL; 411 t->downmixerBufferProvider = NULL; 412 t->mReformatBufferProvider = NULL; 413 t++; 414 } 415 416} 417 418AudioMixer::~AudioMixer() 419{ 420 track_t* t = mState.tracks; 421 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) { 422 delete t->resampler; 423 delete t->downmixerBufferProvider; 424 delete t->mReformatBufferProvider; 425 t++; 426 } 427 delete [] mState.outputTemp; 428 delete [] mState.resampleTemp; 429} 430 431void AudioMixer::setLog(NBLog::Writer *log) 432{ 433 mState.mLog = log; 434} 435 436int AudioMixer::getTrackName(audio_channel_mask_t channelMask, 437 audio_format_t format, int sessionId) 438{ 439 if (!isValidPcmTrackFormat(format)) { 440 ALOGE("AudioMixer::getTrackName invalid format (%#x)", format); 441 return -1; 442 } 443 uint32_t names = (~mTrackNames) & mConfiguredNames; 444 if (names != 0) { 445 int n = __builtin_ctz(names); 446 ALOGV("add track (%d)", n); 447 // assume default parameters for the track, except where noted below 448 track_t* t = &mState.tracks[n]; 449 t->needs = 0; 450 451 // Integer volume. 452 // Currently integer volume is kept for the legacy integer mixer. 453 // Will be removed when the legacy mixer path is removed. 454 t->volume[0] = UNITY_GAIN_INT; 455 t->volume[1] = UNITY_GAIN_INT; 456 t->prevVolume[0] = UNITY_GAIN_INT << 16; 457 t->prevVolume[1] = UNITY_GAIN_INT << 16; 458 t->volumeInc[0] = 0; 459 t->volumeInc[1] = 0; 460 t->auxLevel = 0; 461 t->auxInc = 0; 462 t->prevAuxLevel = 0; 463 464 // Floating point volume. 465 t->mVolume[0] = UNITY_GAIN_FLOAT; 466 t->mVolume[1] = UNITY_GAIN_FLOAT; 467 t->mPrevVolume[0] = UNITY_GAIN_FLOAT; 468 t->mPrevVolume[1] = UNITY_GAIN_FLOAT; 469 t->mVolumeInc[0] = 0.; 470 t->mVolumeInc[1] = 0.; 471 t->mAuxLevel = 0.; 472 t->mAuxInc = 0.; 473 t->mPrevAuxLevel = 0.; 474 475 // no initialization needed 476 // t->frameCount 477 t->channelCount = audio_channel_count_from_out_mask(channelMask); 478 t->enabled = false; 479 ALOGV_IF(channelMask != AUDIO_CHANNEL_OUT_STEREO, 480 "Non-stereo channel mask: %d\n", channelMask); 481 t->channelMask = channelMask; 482 t->sessionId = sessionId; 483 // setBufferProvider(name, AudioBufferProvider *) is required before enable(name) 484 t->bufferProvider = NULL; 485 t->buffer.raw = NULL; 486 // no initialization needed 487 // t->buffer.frameCount 488 t->hook = NULL; 489 t->in = NULL; 490 t->resampler = NULL; 491 t->sampleRate = mSampleRate; 492 // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name) 493 t->mainBuffer = NULL; 494 t->auxBuffer = NULL; 495 t->mInputBufferProvider = NULL; 496 t->mReformatBufferProvider = NULL; 497 t->downmixerBufferProvider = NULL; 498 t->mMixerFormat = AUDIO_FORMAT_PCM_16_BIT; 499 t->mFormat = format; 500 t->mMixerInFormat = kUseFloat && kUseNewMixer 501 ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT; 502 // Check the downmixing (or upmixing) requirements. 503 status_t status = initTrackDownmix(t, n, channelMask); 504 if (status != OK) { 505 ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask); 506 return -1; 507 } 508 // initTrackDownmix() may change the input format requirement. 509 // If you desire floating point input to the mixer, it may change 510 // to integer because the downmixer requires integer to process. 511 ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat); 512 prepareTrackForReformat(t, n); 513 mTrackNames |= 1 << n; 514 return TRACK0 + n; 515 } 516 ALOGE("AudioMixer::getTrackName out of available tracks"); 517 return -1; 518} 519 520void AudioMixer::invalidateState(uint32_t mask) 521{ 522 if (mask != 0) { 523 mState.needsChanged |= mask; 524 mState.hook = process__validate; 525 } 526 } 527 528status_t AudioMixer::initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask) 529{ 530 uint32_t channelCount = audio_channel_count_from_out_mask(mask); 531 ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) && channelCount); 532 status_t status = OK; 533 if (channelCount > MAX_NUM_CHANNELS) { 534 pTrack->channelMask = mask; 535 pTrack->channelCount = channelCount; 536 ALOGV("initTrackDownmix(track=%d, mask=0x%x) calls prepareTrackForDownmix()", 537 trackNum, mask); 538 status = prepareTrackForDownmix(pTrack, trackNum); 539 } else { 540 unprepareTrackForDownmix(pTrack, trackNum); 541 } 542 return status; 543} 544 545void AudioMixer::unprepareTrackForDownmix(track_t* pTrack, int trackName __unused) { 546 ALOGV("AudioMixer::unprepareTrackForDownmix(%d)", trackName); 547 548 if (pTrack->downmixerBufferProvider != NULL) { 549 // this track had previously been configured with a downmixer, delete it 550 ALOGV(" deleting old downmixer"); 551 delete pTrack->downmixerBufferProvider; 552 pTrack->downmixerBufferProvider = NULL; 553 reconfigureBufferProviders(pTrack); 554 } else { 555 ALOGV(" nothing to do, no downmixer to delete"); 556 } 557} 558 559status_t AudioMixer::prepareTrackForDownmix(track_t* pTrack, int trackName) 560{ 561 ALOGV("AudioMixer::prepareTrackForDownmix(%d) with mask 0x%x", trackName, pTrack->channelMask); 562 563 // discard the previous downmixer if there was one 564 unprepareTrackForDownmix(pTrack, trackName); 565 if (DownmixerBufferProvider::isMultichannelCapable()) { 566 DownmixerBufferProvider* pDbp = new DownmixerBufferProvider(pTrack->channelMask, 567 /* pTrack->mMixerChannelMask */ audio_channel_out_mask_from_count(2), 568 /* pTrack->mMixerInFormat */ AUDIO_FORMAT_PCM_16_BIT, 569 pTrack->sampleRate, pTrack->sessionId, kCopyBufferFrameCount); 570 571 if (pDbp->isValid()) { // if constructor completed properly 572 pTrack->mMixerInFormat = AUDIO_FORMAT_PCM_16_BIT; // PCM 16 bit required for downmix 573 pTrack->downmixerBufferProvider = pDbp; 574 reconfigureBufferProviders(pTrack); 575 return NO_ERROR; 576 } 577 delete pDbp; 578 } 579 pTrack->downmixerBufferProvider = NULL; 580 reconfigureBufferProviders(pTrack); 581 return NO_INIT; 582} 583 584void AudioMixer::unprepareTrackForReformat(track_t* pTrack, int trackName __unused) { 585 ALOGV("AudioMixer::unprepareTrackForReformat(%d)", trackName); 586 if (pTrack->mReformatBufferProvider != NULL) { 587 delete pTrack->mReformatBufferProvider; 588 pTrack->mReformatBufferProvider = NULL; 589 reconfigureBufferProviders(pTrack); 590 } 591} 592 593status_t AudioMixer::prepareTrackForReformat(track_t* pTrack, int trackName) 594{ 595 ALOGV("AudioMixer::prepareTrackForReformat(%d) with format %#x", trackName, pTrack->mFormat); 596 // discard the previous reformatter if there was one 597 unprepareTrackForReformat(pTrack, trackName); 598 // only configure reformatter if needed 599 if (pTrack->mFormat != pTrack->mMixerInFormat) { 600 pTrack->mReformatBufferProvider = new ReformatBufferProvider( 601 audio_channel_count_from_out_mask(pTrack->channelMask), 602 pTrack->mFormat, pTrack->mMixerInFormat, 603 kCopyBufferFrameCount); 604 reconfigureBufferProviders(pTrack); 605 } 606 return NO_ERROR; 607} 608 609void AudioMixer::reconfigureBufferProviders(track_t* pTrack) 610{ 611 pTrack->bufferProvider = pTrack->mInputBufferProvider; 612 if (pTrack->mReformatBufferProvider) { 613 pTrack->mReformatBufferProvider->setBufferProvider(pTrack->bufferProvider); 614 pTrack->bufferProvider = pTrack->mReformatBufferProvider; 615 } 616 if (pTrack->downmixerBufferProvider) { 617 pTrack->downmixerBufferProvider->setBufferProvider(pTrack->bufferProvider); 618 pTrack->bufferProvider = pTrack->downmixerBufferProvider; 619 } 620} 621 622void AudioMixer::deleteTrackName(int name) 623{ 624 ALOGV("AudioMixer::deleteTrackName(%d)", name); 625 name -= TRACK0; 626 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 627 ALOGV("deleteTrackName(%d)", name); 628 track_t& track(mState.tracks[ name ]); 629 if (track.enabled) { 630 track.enabled = false; 631 invalidateState(1<<name); 632 } 633 // delete the resampler 634 delete track.resampler; 635 track.resampler = NULL; 636 // delete the downmixer 637 unprepareTrackForDownmix(&mState.tracks[name], name); 638 // delete the reformatter 639 unprepareTrackForReformat(&mState.tracks[name], name); 640 641 mTrackNames &= ~(1<<name); 642} 643 644void AudioMixer::enable(int name) 645{ 646 name -= TRACK0; 647 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 648 track_t& track = mState.tracks[name]; 649 650 if (!track.enabled) { 651 track.enabled = true; 652 ALOGV("enable(%d)", name); 653 invalidateState(1 << name); 654 } 655} 656 657void AudioMixer::disable(int name) 658{ 659 name -= TRACK0; 660 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 661 track_t& track = mState.tracks[name]; 662 663 if (track.enabled) { 664 track.enabled = false; 665 ALOGV("disable(%d)", name); 666 invalidateState(1 << name); 667 } 668} 669 670/* Sets the volume ramp variables for the AudioMixer. 671 * 672 * The volume ramp variables are used to transition from the previous 673 * volume to the set volume. ramp controls the duration of the transition. 674 * Its value is typically one state framecount period, but may also be 0, 675 * meaning "immediate." 676 * 677 * FIXME: 1) Volume ramp is enabled only if there is a nonzero integer increment 678 * even if there is a nonzero floating point increment (in that case, the volume 679 * change is immediate). This restriction should be changed when the legacy mixer 680 * is removed (see #2). 681 * FIXME: 2) Integer volume variables are used for Legacy mixing and should be removed 682 * when no longer needed. 683 * 684 * @param newVolume set volume target in floating point [0.0, 1.0]. 685 * @param ramp number of frames to increment over. if ramp is 0, the volume 686 * should be set immediately. Currently ramp should not exceed 65535 (frames). 687 * @param pIntSetVolume pointer to the U4.12 integer target volume, set on return. 688 * @param pIntPrevVolume pointer to the U4.28 integer previous volume, set on return. 689 * @param pIntVolumeInc pointer to the U4.28 increment per output audio frame, set on return. 690 * @param pSetVolume pointer to the float target volume, set on return. 691 * @param pPrevVolume pointer to the float previous volume, set on return. 692 * @param pVolumeInc pointer to the float increment per output audio frame, set on return. 693 * @return true if the volume has changed, false if volume is same. 694 */ 695static inline bool setVolumeRampVariables(float newVolume, int32_t ramp, 696 int16_t *pIntSetVolume, int32_t *pIntPrevVolume, int32_t *pIntVolumeInc, 697 float *pSetVolume, float *pPrevVolume, float *pVolumeInc) { 698 if (newVolume == *pSetVolume) { 699 return false; 700 } 701 /* set the floating point volume variables */ 702 if (ramp != 0) { 703 *pVolumeInc = (newVolume - *pSetVolume) / ramp; 704 *pPrevVolume = *pSetVolume; 705 } else { 706 *pVolumeInc = 0; 707 *pPrevVolume = newVolume; 708 } 709 *pSetVolume = newVolume; 710 711 /* set the legacy integer volume variables */ 712 int32_t intVolume = newVolume * AudioMixer::UNITY_GAIN_INT; 713 if (intVolume > AudioMixer::UNITY_GAIN_INT) { 714 intVolume = AudioMixer::UNITY_GAIN_INT; 715 } else if (intVolume < 0) { 716 ALOGE("negative volume %.7g", newVolume); 717 intVolume = 0; // should never happen, but for safety check. 718 } 719 if (intVolume == *pIntSetVolume) { 720 *pIntVolumeInc = 0; 721 /* TODO: integer/float workaround: ignore floating volume ramp */ 722 *pVolumeInc = 0; 723 *pPrevVolume = newVolume; 724 return true; 725 } 726 if (ramp != 0) { 727 *pIntVolumeInc = ((intVolume - *pIntSetVolume) << 16) / ramp; 728 *pIntPrevVolume = (*pIntVolumeInc == 0 ? intVolume : *pIntSetVolume) << 16; 729 } else { 730 *pIntVolumeInc = 0; 731 *pIntPrevVolume = intVolume << 16; 732 } 733 *pIntSetVolume = intVolume; 734 return true; 735} 736 737void AudioMixer::setParameter(int name, int target, int param, void *value) 738{ 739 name -= TRACK0; 740 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 741 track_t& track = mState.tracks[name]; 742 743 int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value)); 744 int32_t *valueBuf = reinterpret_cast<int32_t*>(value); 745 746 switch (target) { 747 748 case TRACK: 749 switch (param) { 750 case CHANNEL_MASK: { 751 audio_channel_mask_t mask = 752 static_cast<audio_channel_mask_t>(reinterpret_cast<uintptr_t>(value)); 753 if (track.channelMask != mask) { 754 uint32_t channelCount = audio_channel_count_from_out_mask(mask); 755 ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) && channelCount); 756 track.channelMask = mask; 757 track.channelCount = channelCount; 758 // the mask has changed, does this track need a downmixer? 759 // update to try using our desired format (if we aren't already using it) 760 track.mMixerInFormat = kUseFloat && kUseNewMixer 761 ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT; 762 status_t status = initTrackDownmix(&mState.tracks[name], name, mask); 763 ALOGE_IF(status != OK, 764 "Invalid channel mask %#x, initTrackDownmix returned %d", 765 mask, status); 766 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", mask); 767 prepareTrackForReformat(&track, name); // format may have changed 768 invalidateState(1 << name); 769 } 770 } break; 771 case MAIN_BUFFER: 772 if (track.mainBuffer != valueBuf) { 773 track.mainBuffer = valueBuf; 774 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf); 775 invalidateState(1 << name); 776 } 777 break; 778 case AUX_BUFFER: 779 if (track.auxBuffer != valueBuf) { 780 track.auxBuffer = valueBuf; 781 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf); 782 invalidateState(1 << name); 783 } 784 break; 785 case FORMAT: { 786 audio_format_t format = static_cast<audio_format_t>(valueInt); 787 if (track.mFormat != format) { 788 ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format); 789 track.mFormat = format; 790 ALOGV("setParameter(TRACK, FORMAT, %#x)", format); 791 prepareTrackForReformat(&track, name); 792 invalidateState(1 << name); 793 } 794 } break; 795 // FIXME do we want to support setting the downmix type from AudioFlinger? 796 // for a specific track? or per mixer? 797 /* case DOWNMIX_TYPE: 798 break */ 799 case MIXER_FORMAT: { 800 audio_format_t format = static_cast<audio_format_t>(valueInt); 801 if (track.mMixerFormat != format) { 802 track.mMixerFormat = format; 803 ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format); 804 } 805 } break; 806 default: 807 LOG_ALWAYS_FATAL("setParameter track: bad param %d", param); 808 } 809 break; 810 811 case RESAMPLE: 812 switch (param) { 813 case SAMPLE_RATE: 814 ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt); 815 if (track.setResampler(uint32_t(valueInt), mSampleRate)) { 816 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)", 817 uint32_t(valueInt)); 818 invalidateState(1 << name); 819 } 820 break; 821 case RESET: 822 track.resetResampler(); 823 invalidateState(1 << name); 824 break; 825 case REMOVE: 826 delete track.resampler; 827 track.resampler = NULL; 828 track.sampleRate = mSampleRate; 829 invalidateState(1 << name); 830 break; 831 default: 832 LOG_ALWAYS_FATAL("setParameter resample: bad param %d", param); 833 } 834 break; 835 836 case RAMP_VOLUME: 837 case VOLUME: 838 switch (param) { 839 case VOLUME0: 840 case VOLUME1: 841 if (setVolumeRampVariables(*reinterpret_cast<float*>(value), 842 target == RAMP_VOLUME ? mState.frameCount : 0, 843 &track.volume[param - VOLUME0], &track.prevVolume[param - VOLUME0], 844 &track.volumeInc[param - VOLUME0], 845 &track.mVolume[param - VOLUME0], &track.mPrevVolume[param - VOLUME0], 846 &track.mVolumeInc[param - VOLUME0])) { 847 ALOGV("setParameter(%s, VOLUME%d: %04x)", 848 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", param - VOLUME0, 849 track.volume[param - VOLUME0]); 850 invalidateState(1 << name); 851 } 852 break; 853 case AUXLEVEL: 854 if (setVolumeRampVariables(*reinterpret_cast<float*>(value), 855 target == RAMP_VOLUME ? mState.frameCount : 0, 856 &track.auxLevel, &track.prevAuxLevel, &track.auxInc, 857 &track.mAuxLevel, &track.mPrevAuxLevel, &track.mAuxInc)) { 858 ALOGV("setParameter(%s, AUXLEVEL: %04x)", 859 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", track.auxLevel); 860 invalidateState(1 << name); 861 } 862 break; 863 default: 864 LOG_ALWAYS_FATAL("setParameter volume: bad param %d", param); 865 } 866 break; 867 868 default: 869 LOG_ALWAYS_FATAL("setParameter: bad target %d", target); 870 } 871} 872 873bool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate) 874{ 875 if (value != devSampleRate || resampler != NULL) { 876 if (sampleRate != value) { 877 sampleRate = value; 878 if (resampler == NULL) { 879 ALOGV("creating resampler from track %d Hz to device %d Hz", value, devSampleRate); 880 AudioResampler::src_quality quality; 881 // force lowest quality level resampler if use case isn't music or video 882 // FIXME this is flawed for dynamic sample rates, as we choose the resampler 883 // quality level based on the initial ratio, but that could change later. 884 // Should have a way to distinguish tracks with static ratios vs. dynamic ratios. 885 if (!((value == 44100 && devSampleRate == 48000) || 886 (value == 48000 && devSampleRate == 44100))) { 887 quality = AudioResampler::DYN_LOW_QUALITY; 888 } else { 889 quality = AudioResampler::DEFAULT_QUALITY; 890 } 891 892 ALOGVV("Creating resampler with %d bits\n", bits); 893 resampler = AudioResampler::create( 894 mMixerInFormat, 895 // the resampler sees the number of channels after the downmixer, if any 896 (int) (downmixerBufferProvider != NULL ? MAX_NUM_CHANNELS : channelCount), 897 devSampleRate, quality); 898 resampler->setLocalTimeFreq(sLocalTimeFreq); 899 } 900 return true; 901 } 902 } 903 return false; 904} 905 906/* Checks to see if the volume ramp has completed and clears the increment 907 * variables appropriately. 908 * 909 * FIXME: There is code to handle int/float ramp variable switchover should it not 910 * complete within a mixer buffer processing call, but it is preferred to avoid switchover 911 * due to precision issues. The switchover code is included for legacy code purposes 912 * and can be removed once the integer volume is removed. 913 * 914 * It is not sufficient to clear only the volumeInc integer variable because 915 * if one channel requires ramping, all channels are ramped. 916 * 917 * There is a bit of duplicated code here, but it keeps backward compatibility. 918 */ 919inline void AudioMixer::track_t::adjustVolumeRamp(bool aux, bool useFloat) 920{ 921 if (useFloat) { 922 for (uint32_t i=0 ; i<MAX_NUM_CHANNELS ; i++) { 923 if (mVolumeInc[i] != 0 && fabs(mVolume[i] - mPrevVolume[i]) <= fabs(mVolumeInc[i])) { 924 volumeInc[i] = 0; 925 prevVolume[i] = volume[i] << 16; 926 mVolumeInc[i] = 0.; 927 mPrevVolume[i] = mVolume[i]; 928 929 } else { 930 //ALOGV("ramp: %f %f %f", mVolume[i], mPrevVolume[i], mVolumeInc[i]); 931 prevVolume[i] = u4_28_from_float(mPrevVolume[i]); 932 } 933 } 934 } else { 935 for (uint32_t i=0 ; i<MAX_NUM_CHANNELS ; i++) { 936 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) || 937 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) { 938 volumeInc[i] = 0; 939 prevVolume[i] = volume[i] << 16; 940 mVolumeInc[i] = 0.; 941 mPrevVolume[i] = mVolume[i]; 942 } else { 943 //ALOGV("ramp: %d %d %d", volume[i] << 16, prevVolume[i], volumeInc[i]); 944 mPrevVolume[i] = float_from_u4_28(prevVolume[i]); 945 } 946 } 947 } 948 /* TODO: aux is always integer regardless of output buffer type */ 949 if (aux) { 950 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) || 951 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) { 952 auxInc = 0; 953 prevAuxLevel = auxLevel << 16; 954 mAuxInc = 0.; 955 mPrevAuxLevel = mAuxLevel; 956 } else { 957 //ALOGV("aux ramp: %d %d %d", auxLevel << 16, prevAuxLevel, auxInc); 958 } 959 } 960} 961 962size_t AudioMixer::getUnreleasedFrames(int name) const 963{ 964 name -= TRACK0; 965 if (uint32_t(name) < MAX_NUM_TRACKS) { 966 return mState.tracks[name].getUnreleasedFrames(); 967 } 968 return 0; 969} 970 971void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider) 972{ 973 name -= TRACK0; 974 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 975 976 if (mState.tracks[name].mInputBufferProvider == bufferProvider) { 977 return; // don't reset any buffer providers if identical. 978 } 979 if (mState.tracks[name].mReformatBufferProvider != NULL) { 980 mState.tracks[name].mReformatBufferProvider->reset(); 981 } else if (mState.tracks[name].downmixerBufferProvider != NULL) { 982 } 983 984 mState.tracks[name].mInputBufferProvider = bufferProvider; 985 reconfigureBufferProviders(&mState.tracks[name]); 986} 987 988 989void AudioMixer::process(int64_t pts) 990{ 991 mState.hook(&mState, pts); 992} 993 994 995void AudioMixer::process__validate(state_t* state, int64_t pts) 996{ 997 ALOGW_IF(!state->needsChanged, 998 "in process__validate() but nothing's invalid"); 999 1000 uint32_t changed = state->needsChanged; 1001 state->needsChanged = 0; // clear the validation flag 1002 1003 // recompute which tracks are enabled / disabled 1004 uint32_t enabled = 0; 1005 uint32_t disabled = 0; 1006 while (changed) { 1007 const int i = 31 - __builtin_clz(changed); 1008 const uint32_t mask = 1<<i; 1009 changed &= ~mask; 1010 track_t& t = state->tracks[i]; 1011 (t.enabled ? enabled : disabled) |= mask; 1012 } 1013 state->enabledTracks &= ~disabled; 1014 state->enabledTracks |= enabled; 1015 1016 // compute everything we need... 1017 int countActiveTracks = 0; 1018 bool all16BitsStereoNoResample = true; 1019 bool resampling = false; 1020 bool volumeRamp = false; 1021 uint32_t en = state->enabledTracks; 1022 while (en) { 1023 const int i = 31 - __builtin_clz(en); 1024 en &= ~(1<<i); 1025 1026 countActiveTracks++; 1027 track_t& t = state->tracks[i]; 1028 uint32_t n = 0; 1029 // FIXME can overflow (mask is only 3 bits) 1030 n |= NEEDS_CHANNEL_1 + t.channelCount - 1; 1031 if (t.doesResample()) { 1032 n |= NEEDS_RESAMPLE; 1033 } 1034 if (t.auxLevel != 0 && t.auxBuffer != NULL) { 1035 n |= NEEDS_AUX; 1036 } 1037 1038 if (t.volumeInc[0]|t.volumeInc[1]) { 1039 volumeRamp = true; 1040 } else if (!t.doesResample() && t.volumeRL == 0) { 1041 n |= NEEDS_MUTE; 1042 } 1043 t.needs = n; 1044 1045 if (n & NEEDS_MUTE) { 1046 t.hook = track__nop; 1047 } else { 1048 if (n & NEEDS_AUX) { 1049 all16BitsStereoNoResample = false; 1050 } 1051 if (n & NEEDS_RESAMPLE) { 1052 all16BitsStereoNoResample = false; 1053 resampling = true; 1054 t.hook = getTrackHook(TRACKTYPE_RESAMPLE, FCC_2, 1055 t.mMixerInFormat, t.mMixerFormat); 1056 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2, 1057 "Track %d needs downmix + resample", i); 1058 } else { 1059 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){ 1060 t.hook = getTrackHook(TRACKTYPE_NORESAMPLEMONO, FCC_2, 1061 t.mMixerInFormat, t.mMixerFormat); 1062 all16BitsStereoNoResample = false; 1063 } 1064 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){ 1065 t.hook = getTrackHook(TRACKTYPE_NORESAMPLE, FCC_2, 1066 t.mMixerInFormat, t.mMixerFormat); 1067 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2, 1068 "Track %d needs downmix", i); 1069 } 1070 } 1071 } 1072 } 1073 1074 // select the processing hooks 1075 state->hook = process__nop; 1076 if (countActiveTracks > 0) { 1077 if (resampling) { 1078 if (!state->outputTemp) { 1079 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount]; 1080 } 1081 if (!state->resampleTemp) { 1082 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount]; 1083 } 1084 state->hook = process__genericResampling; 1085 } else { 1086 if (state->outputTemp) { 1087 delete [] state->outputTemp; 1088 state->outputTemp = NULL; 1089 } 1090 if (state->resampleTemp) { 1091 delete [] state->resampleTemp; 1092 state->resampleTemp = NULL; 1093 } 1094 state->hook = process__genericNoResampling; 1095 if (all16BitsStereoNoResample && !volumeRamp) { 1096 if (countActiveTracks == 1) { 1097 const int i = 31 - __builtin_clz(state->enabledTracks); 1098 track_t& t = state->tracks[i]; 1099 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK, FCC_2, 1100 t.mMixerInFormat, t.mMixerFormat); 1101 } 1102 } 1103 } 1104 } 1105 1106 ALOGV("mixer configuration change: %d activeTracks (%08x) " 1107 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d", 1108 countActiveTracks, state->enabledTracks, 1109 all16BitsStereoNoResample, resampling, volumeRamp); 1110 1111 state->hook(state, pts); 1112 1113 // Now that the volume ramp has been done, set optimal state and 1114 // track hooks for subsequent mixer process 1115 if (countActiveTracks > 0) { 1116 bool allMuted = true; 1117 uint32_t en = state->enabledTracks; 1118 while (en) { 1119 const int i = 31 - __builtin_clz(en); 1120 en &= ~(1<<i); 1121 track_t& t = state->tracks[i]; 1122 if (!t.doesResample() && t.volumeRL == 0) { 1123 t.needs |= NEEDS_MUTE; 1124 t.hook = track__nop; 1125 } else { 1126 allMuted = false; 1127 } 1128 } 1129 if (allMuted) { 1130 state->hook = process__nop; 1131 } else if (all16BitsStereoNoResample) { 1132 if (countActiveTracks == 1) { 1133 state->hook = process__OneTrack16BitsStereoNoResampling; 1134 } 1135 } 1136 } 1137} 1138 1139 1140void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount, 1141 int32_t* temp, int32_t* aux) 1142{ 1143 ALOGVV("track__genericResample\n"); 1144 t->resampler->setSampleRate(t->sampleRate); 1145 1146 // ramp gain - resample to temp buffer and scale/mix in 2nd step 1147 if (aux != NULL) { 1148 // always resample with unity gain when sending to auxiliary buffer to be able 1149 // to apply send level after resampling 1150 // TODO: modify each resampler to support aux channel? 1151 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT); 1152 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t)); 1153 t->resampler->resample(temp, outFrameCount, t->bufferProvider); 1154 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 1155 volumeRampStereo(t, out, outFrameCount, temp, aux); 1156 } else { 1157 volumeStereo(t, out, outFrameCount, temp, aux); 1158 } 1159 } else { 1160 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 1161 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT); 1162 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t)); 1163 t->resampler->resample(temp, outFrameCount, t->bufferProvider); 1164 volumeRampStereo(t, out, outFrameCount, temp, aux); 1165 } 1166 1167 // constant gain 1168 else { 1169 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]); 1170 t->resampler->resample(out, outFrameCount, t->bufferProvider); 1171 } 1172 } 1173} 1174 1175void AudioMixer::track__nop(track_t* t __unused, int32_t* out __unused, 1176 size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused) 1177{ 1178} 1179 1180void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 1181 int32_t* aux) 1182{ 1183 int32_t vl = t->prevVolume[0]; 1184 int32_t vr = t->prevVolume[1]; 1185 const int32_t vlInc = t->volumeInc[0]; 1186 const int32_t vrInc = t->volumeInc[1]; 1187 1188 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1189 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1190 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1191 1192 // ramp volume 1193 if (CC_UNLIKELY(aux != NULL)) { 1194 int32_t va = t->prevAuxLevel; 1195 const int32_t vaInc = t->auxInc; 1196 int32_t l; 1197 int32_t r; 1198 1199 do { 1200 l = (*temp++ >> 12); 1201 r = (*temp++ >> 12); 1202 *out++ += (vl >> 16) * l; 1203 *out++ += (vr >> 16) * r; 1204 *aux++ += (va >> 17) * (l + r); 1205 vl += vlInc; 1206 vr += vrInc; 1207 va += vaInc; 1208 } while (--frameCount); 1209 t->prevAuxLevel = va; 1210 } else { 1211 do { 1212 *out++ += (vl >> 16) * (*temp++ >> 12); 1213 *out++ += (vr >> 16) * (*temp++ >> 12); 1214 vl += vlInc; 1215 vr += vrInc; 1216 } while (--frameCount); 1217 } 1218 t->prevVolume[0] = vl; 1219 t->prevVolume[1] = vr; 1220 t->adjustVolumeRamp(aux != NULL); 1221} 1222 1223void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 1224 int32_t* aux) 1225{ 1226 const int16_t vl = t->volume[0]; 1227 const int16_t vr = t->volume[1]; 1228 1229 if (CC_UNLIKELY(aux != NULL)) { 1230 const int16_t va = t->auxLevel; 1231 do { 1232 int16_t l = (int16_t)(*temp++ >> 12); 1233 int16_t r = (int16_t)(*temp++ >> 12); 1234 out[0] = mulAdd(l, vl, out[0]); 1235 int16_t a = (int16_t)(((int32_t)l + r) >> 1); 1236 out[1] = mulAdd(r, vr, out[1]); 1237 out += 2; 1238 aux[0] = mulAdd(a, va, aux[0]); 1239 aux++; 1240 } while (--frameCount); 1241 } else { 1242 do { 1243 int16_t l = (int16_t)(*temp++ >> 12); 1244 int16_t r = (int16_t)(*temp++ >> 12); 1245 out[0] = mulAdd(l, vl, out[0]); 1246 out[1] = mulAdd(r, vr, out[1]); 1247 out += 2; 1248 } while (--frameCount); 1249 } 1250} 1251 1252void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, 1253 int32_t* temp __unused, int32_t* aux) 1254{ 1255 ALOGVV("track__16BitsStereo\n"); 1256 const int16_t *in = static_cast<const int16_t *>(t->in); 1257 1258 if (CC_UNLIKELY(aux != NULL)) { 1259 int32_t l; 1260 int32_t r; 1261 // ramp gain 1262 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 1263 int32_t vl = t->prevVolume[0]; 1264 int32_t vr = t->prevVolume[1]; 1265 int32_t va = t->prevAuxLevel; 1266 const int32_t vlInc = t->volumeInc[0]; 1267 const int32_t vrInc = t->volumeInc[1]; 1268 const int32_t vaInc = t->auxInc; 1269 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1270 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1271 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1272 1273 do { 1274 l = (int32_t)*in++; 1275 r = (int32_t)*in++; 1276 *out++ += (vl >> 16) * l; 1277 *out++ += (vr >> 16) * r; 1278 *aux++ += (va >> 17) * (l + r); 1279 vl += vlInc; 1280 vr += vrInc; 1281 va += vaInc; 1282 } while (--frameCount); 1283 1284 t->prevVolume[0] = vl; 1285 t->prevVolume[1] = vr; 1286 t->prevAuxLevel = va; 1287 t->adjustVolumeRamp(true); 1288 } 1289 1290 // constant gain 1291 else { 1292 const uint32_t vrl = t->volumeRL; 1293 const int16_t va = (int16_t)t->auxLevel; 1294 do { 1295 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1296 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1); 1297 in += 2; 1298 out[0] = mulAddRL(1, rl, vrl, out[0]); 1299 out[1] = mulAddRL(0, rl, vrl, out[1]); 1300 out += 2; 1301 aux[0] = mulAdd(a, va, aux[0]); 1302 aux++; 1303 } while (--frameCount); 1304 } 1305 } else { 1306 // ramp gain 1307 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 1308 int32_t vl = t->prevVolume[0]; 1309 int32_t vr = t->prevVolume[1]; 1310 const int32_t vlInc = t->volumeInc[0]; 1311 const int32_t vrInc = t->volumeInc[1]; 1312 1313 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1314 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1315 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1316 1317 do { 1318 *out++ += (vl >> 16) * (int32_t) *in++; 1319 *out++ += (vr >> 16) * (int32_t) *in++; 1320 vl += vlInc; 1321 vr += vrInc; 1322 } while (--frameCount); 1323 1324 t->prevVolume[0] = vl; 1325 t->prevVolume[1] = vr; 1326 t->adjustVolumeRamp(false); 1327 } 1328 1329 // constant gain 1330 else { 1331 const uint32_t vrl = t->volumeRL; 1332 do { 1333 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1334 in += 2; 1335 out[0] = mulAddRL(1, rl, vrl, out[0]); 1336 out[1] = mulAddRL(0, rl, vrl, out[1]); 1337 out += 2; 1338 } while (--frameCount); 1339 } 1340 } 1341 t->in = in; 1342} 1343 1344void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount, 1345 int32_t* temp __unused, int32_t* aux) 1346{ 1347 ALOGVV("track__16BitsMono\n"); 1348 const int16_t *in = static_cast<int16_t const *>(t->in); 1349 1350 if (CC_UNLIKELY(aux != NULL)) { 1351 // ramp gain 1352 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 1353 int32_t vl = t->prevVolume[0]; 1354 int32_t vr = t->prevVolume[1]; 1355 int32_t va = t->prevAuxLevel; 1356 const int32_t vlInc = t->volumeInc[0]; 1357 const int32_t vrInc = t->volumeInc[1]; 1358 const int32_t vaInc = t->auxInc; 1359 1360 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1361 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1362 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1363 1364 do { 1365 int32_t l = *in++; 1366 *out++ += (vl >> 16) * l; 1367 *out++ += (vr >> 16) * l; 1368 *aux++ += (va >> 16) * l; 1369 vl += vlInc; 1370 vr += vrInc; 1371 va += vaInc; 1372 } while (--frameCount); 1373 1374 t->prevVolume[0] = vl; 1375 t->prevVolume[1] = vr; 1376 t->prevAuxLevel = va; 1377 t->adjustVolumeRamp(true); 1378 } 1379 // constant gain 1380 else { 1381 const int16_t vl = t->volume[0]; 1382 const int16_t vr = t->volume[1]; 1383 const int16_t va = (int16_t)t->auxLevel; 1384 do { 1385 int16_t l = *in++; 1386 out[0] = mulAdd(l, vl, out[0]); 1387 out[1] = mulAdd(l, vr, out[1]); 1388 out += 2; 1389 aux[0] = mulAdd(l, va, aux[0]); 1390 aux++; 1391 } while (--frameCount); 1392 } 1393 } else { 1394 // ramp gain 1395 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 1396 int32_t vl = t->prevVolume[0]; 1397 int32_t vr = t->prevVolume[1]; 1398 const int32_t vlInc = t->volumeInc[0]; 1399 const int32_t vrInc = t->volumeInc[1]; 1400 1401 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1402 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1403 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1404 1405 do { 1406 int32_t l = *in++; 1407 *out++ += (vl >> 16) * l; 1408 *out++ += (vr >> 16) * l; 1409 vl += vlInc; 1410 vr += vrInc; 1411 } while (--frameCount); 1412 1413 t->prevVolume[0] = vl; 1414 t->prevVolume[1] = vr; 1415 t->adjustVolumeRamp(false); 1416 } 1417 // constant gain 1418 else { 1419 const int16_t vl = t->volume[0]; 1420 const int16_t vr = t->volume[1]; 1421 do { 1422 int16_t l = *in++; 1423 out[0] = mulAdd(l, vl, out[0]); 1424 out[1] = mulAdd(l, vr, out[1]); 1425 out += 2; 1426 } while (--frameCount); 1427 } 1428 } 1429 t->in = in; 1430} 1431 1432// no-op case 1433void AudioMixer::process__nop(state_t* state, int64_t pts) 1434{ 1435 ALOGVV("process__nop\n"); 1436 uint32_t e0 = state->enabledTracks; 1437 size_t sampleCount = state->frameCount * MAX_NUM_CHANNELS; 1438 while (e0) { 1439 // process by group of tracks with same output buffer to 1440 // avoid multiple memset() on same buffer 1441 uint32_t e1 = e0, e2 = e0; 1442 int i = 31 - __builtin_clz(e1); 1443 { 1444 track_t& t1 = state->tracks[i]; 1445 e2 &= ~(1<<i); 1446 while (e2) { 1447 i = 31 - __builtin_clz(e2); 1448 e2 &= ~(1<<i); 1449 track_t& t2 = state->tracks[i]; 1450 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 1451 e1 &= ~(1<<i); 1452 } 1453 } 1454 e0 &= ~(e1); 1455 1456 memset(t1.mainBuffer, 0, sampleCount 1457 * audio_bytes_per_sample(t1.mMixerFormat)); 1458 } 1459 1460 while (e1) { 1461 i = 31 - __builtin_clz(e1); 1462 e1 &= ~(1<<i); 1463 { 1464 track_t& t3 = state->tracks[i]; 1465 size_t outFrames = state->frameCount; 1466 while (outFrames) { 1467 t3.buffer.frameCount = outFrames; 1468 int64_t outputPTS = calculateOutputPTS( 1469 t3, pts, state->frameCount - outFrames); 1470 t3.bufferProvider->getNextBuffer(&t3.buffer, outputPTS); 1471 if (t3.buffer.raw == NULL) break; 1472 outFrames -= t3.buffer.frameCount; 1473 t3.bufferProvider->releaseBuffer(&t3.buffer); 1474 } 1475 } 1476 } 1477 } 1478} 1479 1480// generic code without resampling 1481void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts) 1482{ 1483 ALOGVV("process__genericNoResampling\n"); 1484 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32))); 1485 1486 // acquire each track's buffer 1487 uint32_t enabledTracks = state->enabledTracks; 1488 uint32_t e0 = enabledTracks; 1489 while (e0) { 1490 const int i = 31 - __builtin_clz(e0); 1491 e0 &= ~(1<<i); 1492 track_t& t = state->tracks[i]; 1493 t.buffer.frameCount = state->frameCount; 1494 t.bufferProvider->getNextBuffer(&t.buffer, pts); 1495 t.frameCount = t.buffer.frameCount; 1496 t.in = t.buffer.raw; 1497 } 1498 1499 e0 = enabledTracks; 1500 while (e0) { 1501 // process by group of tracks with same output buffer to 1502 // optimize cache use 1503 uint32_t e1 = e0, e2 = e0; 1504 int j = 31 - __builtin_clz(e1); 1505 track_t& t1 = state->tracks[j]; 1506 e2 &= ~(1<<j); 1507 while (e2) { 1508 j = 31 - __builtin_clz(e2); 1509 e2 &= ~(1<<j); 1510 track_t& t2 = state->tracks[j]; 1511 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 1512 e1 &= ~(1<<j); 1513 } 1514 } 1515 e0 &= ~(e1); 1516 // this assumes output 16 bits stereo, no resampling 1517 int32_t *out = t1.mainBuffer; 1518 size_t numFrames = 0; 1519 do { 1520 memset(outTemp, 0, sizeof(outTemp)); 1521 e2 = e1; 1522 while (e2) { 1523 const int i = 31 - __builtin_clz(e2); 1524 e2 &= ~(1<<i); 1525 track_t& t = state->tracks[i]; 1526 size_t outFrames = BLOCKSIZE; 1527 int32_t *aux = NULL; 1528 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) { 1529 aux = t.auxBuffer + numFrames; 1530 } 1531 while (outFrames) { 1532 // t.in == NULL can happen if the track was flushed just after having 1533 // been enabled for mixing. 1534 if (t.in == NULL) { 1535 enabledTracks &= ~(1<<i); 1536 e1 &= ~(1<<i); 1537 break; 1538 } 1539 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount; 1540 if (inFrames > 0) { 1541 t.hook(&t, outTemp + (BLOCKSIZE-outFrames)*MAX_NUM_CHANNELS, inFrames, 1542 state->resampleTemp, aux); 1543 t.frameCount -= inFrames; 1544 outFrames -= inFrames; 1545 if (CC_UNLIKELY(aux != NULL)) { 1546 aux += inFrames; 1547 } 1548 } 1549 if (t.frameCount == 0 && outFrames) { 1550 t.bufferProvider->releaseBuffer(&t.buffer); 1551 t.buffer.frameCount = (state->frameCount - numFrames) - 1552 (BLOCKSIZE - outFrames); 1553 int64_t outputPTS = calculateOutputPTS( 1554 t, pts, numFrames + (BLOCKSIZE - outFrames)); 1555 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS); 1556 t.in = t.buffer.raw; 1557 if (t.in == NULL) { 1558 enabledTracks &= ~(1<<i); 1559 e1 &= ~(1<<i); 1560 break; 1561 } 1562 t.frameCount = t.buffer.frameCount; 1563 } 1564 } 1565 } 1566 1567 convertMixerFormat(out, t1.mMixerFormat, outTemp, t1.mMixerInFormat, 1568 BLOCKSIZE * FCC_2); 1569 // TODO: fix ugly casting due to choice of out pointer type 1570 out = reinterpret_cast<int32_t*>((uint8_t*)out 1571 + BLOCKSIZE * FCC_2 * audio_bytes_per_sample(t1.mMixerFormat)); 1572 numFrames += BLOCKSIZE; 1573 } while (numFrames < state->frameCount); 1574 } 1575 1576 // release each track's buffer 1577 e0 = enabledTracks; 1578 while (e0) { 1579 const int i = 31 - __builtin_clz(e0); 1580 e0 &= ~(1<<i); 1581 track_t& t = state->tracks[i]; 1582 t.bufferProvider->releaseBuffer(&t.buffer); 1583 } 1584} 1585 1586 1587// generic code with resampling 1588void AudioMixer::process__genericResampling(state_t* state, int64_t pts) 1589{ 1590 ALOGVV("process__genericResampling\n"); 1591 // this const just means that local variable outTemp doesn't change 1592 int32_t* const outTemp = state->outputTemp; 1593 const size_t size = sizeof(int32_t) * MAX_NUM_CHANNELS * state->frameCount; 1594 1595 size_t numFrames = state->frameCount; 1596 1597 uint32_t e0 = state->enabledTracks; 1598 while (e0) { 1599 // process by group of tracks with same output buffer 1600 // to optimize cache use 1601 uint32_t e1 = e0, e2 = e0; 1602 int j = 31 - __builtin_clz(e1); 1603 track_t& t1 = state->tracks[j]; 1604 e2 &= ~(1<<j); 1605 while (e2) { 1606 j = 31 - __builtin_clz(e2); 1607 e2 &= ~(1<<j); 1608 track_t& t2 = state->tracks[j]; 1609 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 1610 e1 &= ~(1<<j); 1611 } 1612 } 1613 e0 &= ~(e1); 1614 int32_t *out = t1.mainBuffer; 1615 memset(outTemp, 0, size); 1616 while (e1) { 1617 const int i = 31 - __builtin_clz(e1); 1618 e1 &= ~(1<<i); 1619 track_t& t = state->tracks[i]; 1620 int32_t *aux = NULL; 1621 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) { 1622 aux = t.auxBuffer; 1623 } 1624 1625 // this is a little goofy, on the resampling case we don't 1626 // acquire/release the buffers because it's done by 1627 // the resampler. 1628 if (t.needs & NEEDS_RESAMPLE) { 1629 t.resampler->setPTS(pts); 1630 t.hook(&t, outTemp, numFrames, state->resampleTemp, aux); 1631 } else { 1632 1633 size_t outFrames = 0; 1634 1635 while (outFrames < numFrames) { 1636 t.buffer.frameCount = numFrames - outFrames; 1637 int64_t outputPTS = calculateOutputPTS(t, pts, outFrames); 1638 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS); 1639 t.in = t.buffer.raw; 1640 // t.in == NULL can happen if the track was flushed just after having 1641 // been enabled for mixing. 1642 if (t.in == NULL) break; 1643 1644 if (CC_UNLIKELY(aux != NULL)) { 1645 aux += outFrames; 1646 } 1647 t.hook(&t, outTemp + outFrames*MAX_NUM_CHANNELS, t.buffer.frameCount, 1648 state->resampleTemp, aux); 1649 outFrames += t.buffer.frameCount; 1650 t.bufferProvider->releaseBuffer(&t.buffer); 1651 } 1652 } 1653 } 1654 convertMixerFormat(out, t1.mMixerFormat, outTemp, t1.mMixerInFormat, numFrames * FCC_2); 1655 } 1656} 1657 1658// one track, 16 bits stereo without resampling is the most common case 1659void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state, 1660 int64_t pts) 1661{ 1662 ALOGVV("process__OneTrack16BitsStereoNoResampling\n"); 1663 // This method is only called when state->enabledTracks has exactly 1664 // one bit set. The asserts below would verify this, but are commented out 1665 // since the whole point of this method is to optimize performance. 1666 //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled"); 1667 const int i = 31 - __builtin_clz(state->enabledTracks); 1668 //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled"); 1669 const track_t& t = state->tracks[i]; 1670 1671 AudioBufferProvider::Buffer& b(t.buffer); 1672 1673 int32_t* out = t.mainBuffer; 1674 float *fout = reinterpret_cast<float*>(out); 1675 size_t numFrames = state->frameCount; 1676 1677 const int16_t vl = t.volume[0]; 1678 const int16_t vr = t.volume[1]; 1679 const uint32_t vrl = t.volumeRL; 1680 while (numFrames) { 1681 b.frameCount = numFrames; 1682 int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer); 1683 t.bufferProvider->getNextBuffer(&b, outputPTS); 1684 const int16_t *in = b.i16; 1685 1686 // in == NULL can happen if the track was flushed just after having 1687 // been enabled for mixing. 1688 if (in == NULL || (((uintptr_t)in) & 3)) { 1689 memset(out, 0, numFrames 1690 * MAX_NUM_CHANNELS * audio_bytes_per_sample(t.mMixerFormat)); 1691 ALOGE_IF((((uintptr_t)in) & 3), "process stereo track: input buffer alignment pb: " 1692 "buffer %p track %d, channels %d, needs %08x", 1693 in, i, t.channelCount, t.needs); 1694 return; 1695 } 1696 size_t outFrames = b.frameCount; 1697 1698 switch (t.mMixerFormat) { 1699 case AUDIO_FORMAT_PCM_FLOAT: 1700 do { 1701 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1702 in += 2; 1703 int32_t l = mulRL(1, rl, vrl); 1704 int32_t r = mulRL(0, rl, vrl); 1705 *fout++ = float_from_q4_27(l); 1706 *fout++ = float_from_q4_27(r); 1707 // Note: In case of later int16_t sink output, 1708 // conversion and clamping is done by memcpy_to_i16_from_float(). 1709 } while (--outFrames); 1710 break; 1711 case AUDIO_FORMAT_PCM_16_BIT: 1712 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) { 1713 // volume is boosted, so we might need to clamp even though 1714 // we process only one track. 1715 do { 1716 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1717 in += 2; 1718 int32_t l = mulRL(1, rl, vrl) >> 12; 1719 int32_t r = mulRL(0, rl, vrl) >> 12; 1720 // clamping... 1721 l = clamp16(l); 1722 r = clamp16(r); 1723 *out++ = (r<<16) | (l & 0xFFFF); 1724 } while (--outFrames); 1725 } else { 1726 do { 1727 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1728 in += 2; 1729 int32_t l = mulRL(1, rl, vrl) >> 12; 1730 int32_t r = mulRL(0, rl, vrl) >> 12; 1731 *out++ = (r<<16) | (l & 0xFFFF); 1732 } while (--outFrames); 1733 } 1734 break; 1735 default: 1736 LOG_ALWAYS_FATAL("bad mixer format: %d", t.mMixerFormat); 1737 } 1738 numFrames -= b.frameCount; 1739 t.bufferProvider->releaseBuffer(&b); 1740 } 1741} 1742 1743#if 0 1744// 2 tracks is also a common case 1745// NEVER used in current implementation of process__validate() 1746// only use if the 2 tracks have the same output buffer 1747void AudioMixer::process__TwoTracks16BitsStereoNoResampling(state_t* state, 1748 int64_t pts) 1749{ 1750 int i; 1751 uint32_t en = state->enabledTracks; 1752 1753 i = 31 - __builtin_clz(en); 1754 const track_t& t0 = state->tracks[i]; 1755 AudioBufferProvider::Buffer& b0(t0.buffer); 1756 1757 en &= ~(1<<i); 1758 i = 31 - __builtin_clz(en); 1759 const track_t& t1 = state->tracks[i]; 1760 AudioBufferProvider::Buffer& b1(t1.buffer); 1761 1762 const int16_t *in0; 1763 const int16_t vl0 = t0.volume[0]; 1764 const int16_t vr0 = t0.volume[1]; 1765 size_t frameCount0 = 0; 1766 1767 const int16_t *in1; 1768 const int16_t vl1 = t1.volume[0]; 1769 const int16_t vr1 = t1.volume[1]; 1770 size_t frameCount1 = 0; 1771 1772 //FIXME: only works if two tracks use same buffer 1773 int32_t* out = t0.mainBuffer; 1774 size_t numFrames = state->frameCount; 1775 const int16_t *buff = NULL; 1776 1777 1778 while (numFrames) { 1779 1780 if (frameCount0 == 0) { 1781 b0.frameCount = numFrames; 1782 int64_t outputPTS = calculateOutputPTS(t0, pts, 1783 out - t0.mainBuffer); 1784 t0.bufferProvider->getNextBuffer(&b0, outputPTS); 1785 if (b0.i16 == NULL) { 1786 if (buff == NULL) { 1787 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount]; 1788 } 1789 in0 = buff; 1790 b0.frameCount = numFrames; 1791 } else { 1792 in0 = b0.i16; 1793 } 1794 frameCount0 = b0.frameCount; 1795 } 1796 if (frameCount1 == 0) { 1797 b1.frameCount = numFrames; 1798 int64_t outputPTS = calculateOutputPTS(t1, pts, 1799 out - t0.mainBuffer); 1800 t1.bufferProvider->getNextBuffer(&b1, outputPTS); 1801 if (b1.i16 == NULL) { 1802 if (buff == NULL) { 1803 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount]; 1804 } 1805 in1 = buff; 1806 b1.frameCount = numFrames; 1807 } else { 1808 in1 = b1.i16; 1809 } 1810 frameCount1 = b1.frameCount; 1811 } 1812 1813 size_t outFrames = frameCount0 < frameCount1?frameCount0:frameCount1; 1814 1815 numFrames -= outFrames; 1816 frameCount0 -= outFrames; 1817 frameCount1 -= outFrames; 1818 1819 do { 1820 int32_t l0 = *in0++; 1821 int32_t r0 = *in0++; 1822 l0 = mul(l0, vl0); 1823 r0 = mul(r0, vr0); 1824 int32_t l = *in1++; 1825 int32_t r = *in1++; 1826 l = mulAdd(l, vl1, l0) >> 12; 1827 r = mulAdd(r, vr1, r0) >> 12; 1828 // clamping... 1829 l = clamp16(l); 1830 r = clamp16(r); 1831 *out++ = (r<<16) | (l & 0xFFFF); 1832 } while (--outFrames); 1833 1834 if (frameCount0 == 0) { 1835 t0.bufferProvider->releaseBuffer(&b0); 1836 } 1837 if (frameCount1 == 0) { 1838 t1.bufferProvider->releaseBuffer(&b1); 1839 } 1840 } 1841 1842 delete [] buff; 1843} 1844#endif 1845 1846int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS, 1847 int outputFrameIndex) 1848{ 1849 if (AudioBufferProvider::kInvalidPTS == basePTS) { 1850 return AudioBufferProvider::kInvalidPTS; 1851 } 1852 1853 return basePTS + ((outputFrameIndex * sLocalTimeFreq) / t.sampleRate); 1854} 1855 1856/*static*/ uint64_t AudioMixer::sLocalTimeFreq; 1857/*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT; 1858 1859/*static*/ void AudioMixer::sInitRoutine() 1860{ 1861 LocalClock lc; 1862 sLocalTimeFreq = lc.getLocalFreq(); // for the resampler 1863 1864 DownmixerBufferProvider::init(); // for the downmixer 1865} 1866 1867template <int MIXTYPE, int NCHAN, bool USEFLOATVOL, bool ADJUSTVOL, 1868 typename TO, typename TI, typename TA> 1869void AudioMixer::volumeMix(TO *out, size_t outFrames, 1870 const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t) 1871{ 1872 if (USEFLOATVOL) { 1873 if (ramp) { 1874 volumeRampMulti<MIXTYPE, NCHAN>(out, outFrames, in, aux, 1875 t->mPrevVolume, t->mVolumeInc, &t->prevAuxLevel, t->auxInc); 1876 if (ADJUSTVOL) { 1877 t->adjustVolumeRamp(aux != NULL, true); 1878 } 1879 } else { 1880 volumeMulti<MIXTYPE, NCHAN>(out, outFrames, in, aux, 1881 t->mVolume, t->auxLevel); 1882 } 1883 } else { 1884 if (ramp) { 1885 volumeRampMulti<MIXTYPE, NCHAN>(out, outFrames, in, aux, 1886 t->prevVolume, t->volumeInc, &t->prevAuxLevel, t->auxInc); 1887 if (ADJUSTVOL) { 1888 t->adjustVolumeRamp(aux != NULL); 1889 } 1890 } else { 1891 volumeMulti<MIXTYPE, NCHAN>(out, outFrames, in, aux, 1892 t->volume, t->auxLevel); 1893 } 1894 } 1895} 1896 1897/* This process hook is called when there is a single track without 1898 * aux buffer, volume ramp, or resampling. 1899 * TODO: Update the hook selection: this can properly handle aux and ramp. 1900 */ 1901template <int MIXTYPE, int NCHAN, typename TO, typename TI, typename TA> 1902void AudioMixer::process_NoResampleOneTrack(state_t* state, int64_t pts) 1903{ 1904 ALOGVV("process_NoResampleOneTrack\n"); 1905 // CLZ is faster than CTZ on ARM, though really not sure if true after 31 - clz. 1906 const int i = 31 - __builtin_clz(state->enabledTracks); 1907 ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled"); 1908 track_t *t = &state->tracks[i]; 1909 TO* out = reinterpret_cast<TO*>(t->mainBuffer); 1910 TA* aux = reinterpret_cast<TA*>(t->auxBuffer); 1911 const bool ramp = t->needsRamp(); 1912 1913 for (size_t numFrames = state->frameCount; numFrames; ) { 1914 AudioBufferProvider::Buffer& b(t->buffer); 1915 // get input buffer 1916 b.frameCount = numFrames; 1917 const int64_t outputPTS = calculateOutputPTS(*t, pts, state->frameCount - numFrames); 1918 t->bufferProvider->getNextBuffer(&b, outputPTS); 1919 const TI *in = reinterpret_cast<TI*>(b.raw); 1920 1921 // in == NULL can happen if the track was flushed just after having 1922 // been enabled for mixing. 1923 if (in == NULL || (((uintptr_t)in) & 3)) { 1924 memset(out, 0, numFrames 1925 * NCHAN * audio_bytes_per_sample(t->mMixerFormat)); 1926 ALOGE_IF((((uintptr_t)in) & 3), "process_NoResampleOneTrack: bus error: " 1927 "buffer %p track %p, channels %d, needs %#x", 1928 in, t, t->channelCount, t->needs); 1929 return; 1930 } 1931 1932 const size_t outFrames = b.frameCount; 1933 volumeMix<MIXTYPE, NCHAN, is_same<TI, float>::value, false> (out, 1934 outFrames, in, aux, ramp, t); 1935 1936 out += outFrames * NCHAN; 1937 if (aux != NULL) { 1938 aux += NCHAN; 1939 } 1940 numFrames -= b.frameCount; 1941 1942 // release buffer 1943 t->bufferProvider->releaseBuffer(&b); 1944 } 1945 if (ramp) { 1946 t->adjustVolumeRamp(aux != NULL, is_same<TI, float>::value); 1947 } 1948} 1949 1950/* This track hook is called to do resampling then mixing, 1951 * pulling from the track's upstream AudioBufferProvider. 1952 */ 1953template <int MIXTYPE, int NCHAN, typename TO, typename TI, typename TA> 1954void AudioMixer::track__Resample(track_t* t, TO* out, size_t outFrameCount, TO* temp, TA* aux) 1955{ 1956 ALOGVV("track__Resample\n"); 1957 t->resampler->setSampleRate(t->sampleRate); 1958 1959 const bool ramp = t->needsRamp(); 1960 if (ramp || aux != NULL) { 1961 // if ramp: resample with unity gain to temp buffer and scale/mix in 2nd step. 1962 // if aux != NULL: resample with unity gain to temp buffer then apply send level. 1963 1964 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT); 1965 memset(temp, 0, outFrameCount * NCHAN * sizeof(TO)); 1966 t->resampler->resample((int32_t*)temp, outFrameCount, t->bufferProvider); 1967 1968 volumeMix<MIXTYPE, NCHAN, is_same<TI, float>::value, true>(out, outFrameCount, 1969 temp, aux, ramp, t); 1970 1971 } else { // constant volume gain 1972 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]); 1973 t->resampler->resample((int32_t*)out, outFrameCount, t->bufferProvider); 1974 } 1975} 1976 1977/* This track hook is called to mix a track, when no resampling is required. 1978 * The input buffer should be present in t->in. 1979 */ 1980template <int MIXTYPE, int NCHAN, typename TO, typename TI, typename TA> 1981void AudioMixer::track__NoResample(track_t* t, TO* out, size_t frameCount, 1982 TO* temp __unused, TA* aux) 1983{ 1984 ALOGVV("track__NoResample\n"); 1985 const TI *in = static_cast<const TI *>(t->in); 1986 1987 volumeMix<MIXTYPE, NCHAN, is_same<TI, float>::value, true>(out, frameCount, 1988 in, aux, t->needsRamp(), t); 1989 1990 // MIXTYPE_MONOEXPAND reads a single input channel and expands to NCHAN output channels. 1991 // MIXTYPE_MULTI reads NCHAN input channels and places to NCHAN output channels. 1992 in += (MIXTYPE == MIXTYPE_MONOEXPAND) ? frameCount : frameCount * NCHAN; 1993 t->in = in; 1994} 1995 1996/* The Mixer engine generates either int32_t (Q4_27) or float data. 1997 * We use this function to convert the engine buffers 1998 * to the desired mixer output format, either int16_t (Q.15) or float. 1999 */ 2000void AudioMixer::convertMixerFormat(void *out, audio_format_t mixerOutFormat, 2001 void *in, audio_format_t mixerInFormat, size_t sampleCount) 2002{ 2003 switch (mixerInFormat) { 2004 case AUDIO_FORMAT_PCM_FLOAT: 2005 switch (mixerOutFormat) { 2006 case AUDIO_FORMAT_PCM_FLOAT: 2007 memcpy(out, in, sampleCount * sizeof(float)); // MEMCPY. TODO optimize out 2008 break; 2009 case AUDIO_FORMAT_PCM_16_BIT: 2010 memcpy_to_i16_from_float((int16_t*)out, (float*)in, sampleCount); 2011 break; 2012 default: 2013 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); 2014 break; 2015 } 2016 break; 2017 case AUDIO_FORMAT_PCM_16_BIT: 2018 switch (mixerOutFormat) { 2019 case AUDIO_FORMAT_PCM_FLOAT: 2020 memcpy_to_float_from_q4_27((float*)out, (int32_t*)in, sampleCount); 2021 break; 2022 case AUDIO_FORMAT_PCM_16_BIT: 2023 // two int16_t are produced per iteration 2024 ditherAndClamp((int32_t*)out, (int32_t*)in, sampleCount >> 1); 2025 break; 2026 default: 2027 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); 2028 break; 2029 } 2030 break; 2031 default: 2032 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2033 break; 2034 } 2035} 2036 2037/* Returns the proper track hook to use for mixing the track into the output buffer. 2038 */ 2039AudioMixer::hook_t AudioMixer::getTrackHook(int trackType, int channels, 2040 audio_format_t mixerInFormat, audio_format_t mixerOutFormat __unused) 2041{ 2042 if (!kUseNewMixer && channels == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) { 2043 switch (trackType) { 2044 case TRACKTYPE_NOP: 2045 return track__nop; 2046 case TRACKTYPE_RESAMPLE: 2047 return track__genericResample; 2048 case TRACKTYPE_NORESAMPLEMONO: 2049 return track__16BitsMono; 2050 case TRACKTYPE_NORESAMPLE: 2051 return track__16BitsStereo; 2052 default: 2053 LOG_ALWAYS_FATAL("bad trackType: %d", trackType); 2054 break; 2055 } 2056 } 2057 LOG_ALWAYS_FATAL_IF(channels != FCC_2); // TODO: must be stereo right now 2058 switch (trackType) { 2059 case TRACKTYPE_NOP: 2060 return track__nop; 2061 case TRACKTYPE_RESAMPLE: 2062 switch (mixerInFormat) { 2063 case AUDIO_FORMAT_PCM_FLOAT: 2064 return (AudioMixer::hook_t) 2065 track__Resample<MIXTYPE_MULTI, 2, float, float, int32_t>; 2066 case AUDIO_FORMAT_PCM_16_BIT: 2067 return (AudioMixer::hook_t)\ 2068 track__Resample<MIXTYPE_MULTI, 2, int32_t, int16_t, int32_t>; 2069 default: 2070 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2071 break; 2072 } 2073 break; 2074 case TRACKTYPE_NORESAMPLEMONO: 2075 switch (mixerInFormat) { 2076 case AUDIO_FORMAT_PCM_FLOAT: 2077 return (AudioMixer::hook_t) 2078 track__NoResample<MIXTYPE_MONOEXPAND, 2, float, float, int32_t>; 2079 case AUDIO_FORMAT_PCM_16_BIT: 2080 return (AudioMixer::hook_t) 2081 track__NoResample<MIXTYPE_MONOEXPAND, 2, int32_t, int16_t, int32_t>; 2082 default: 2083 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2084 break; 2085 } 2086 break; 2087 case TRACKTYPE_NORESAMPLE: 2088 switch (mixerInFormat) { 2089 case AUDIO_FORMAT_PCM_FLOAT: 2090 return (AudioMixer::hook_t) 2091 track__NoResample<MIXTYPE_MULTI, 2, float, float, int32_t>; 2092 case AUDIO_FORMAT_PCM_16_BIT: 2093 return (AudioMixer::hook_t) 2094 track__NoResample<MIXTYPE_MULTI, 2, int32_t, int16_t, int32_t>; 2095 default: 2096 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2097 break; 2098 } 2099 break; 2100 default: 2101 LOG_ALWAYS_FATAL("bad trackType: %d", trackType); 2102 break; 2103 } 2104 return NULL; 2105} 2106 2107/* Returns the proper process hook for mixing tracks. Currently works only for 2108 * PROCESSTYPE_NORESAMPLEONETRACK, a mix involving one track, no resampling. 2109 */ 2110AudioMixer::process_hook_t AudioMixer::getProcessHook(int processType, int channels, 2111 audio_format_t mixerInFormat, audio_format_t mixerOutFormat) 2112{ 2113 if (processType != PROCESSTYPE_NORESAMPLEONETRACK) { // Only NORESAMPLEONETRACK 2114 LOG_ALWAYS_FATAL("bad processType: %d", processType); 2115 return NULL; 2116 } 2117 if (!kUseNewMixer && channels == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) { 2118 return process__OneTrack16BitsStereoNoResampling; 2119 } 2120 LOG_ALWAYS_FATAL_IF(channels != FCC_2); // TODO: must be stereo right now 2121 switch (mixerInFormat) { 2122 case AUDIO_FORMAT_PCM_FLOAT: 2123 switch (mixerOutFormat) { 2124 case AUDIO_FORMAT_PCM_FLOAT: 2125 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2, 2126 float, float, int32_t>; 2127 case AUDIO_FORMAT_PCM_16_BIT: 2128 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2, 2129 int16_t, float, int32_t>; 2130 default: 2131 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); 2132 break; 2133 } 2134 break; 2135 case AUDIO_FORMAT_PCM_16_BIT: 2136 switch (mixerOutFormat) { 2137 case AUDIO_FORMAT_PCM_FLOAT: 2138 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2, 2139 float, int16_t, int32_t>; 2140 case AUDIO_FORMAT_PCM_16_BIT: 2141 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2, 2142 int16_t, int16_t, int32_t>; 2143 default: 2144 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); 2145 break; 2146 } 2147 break; 2148 default: 2149 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2150 break; 2151 } 2152 return NULL; 2153} 2154 2155// ---------------------------------------------------------------------------- 2156}; // namespace android 2157