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