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