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