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