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