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