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