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