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