AudioMixer.cpp revision 5798d4ebf236357a4b13246f40e52b90a34d09a4
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 <sys/types.h>
25
26#include <utils/Errors.h>
27#include <utils/Log.h>
28
29#include <cutils/bitops.h>
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 <common_time/local_clock.h>
37#include <common_time/cc_helper.h>
38
39#include "AudioMixer.h"
40
41namespace android {
42
43// ----------------------------------------------------------------------------
44
45AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate)
46    :   mTrackNames(0), mSampleRate(sampleRate)
47{
48    // AudioMixer is not yet capable of multi-channel beyond stereo
49    COMPILE_TIME_ASSERT_FUNCTION_SCOPE(2 == MAX_NUM_CHANNELS);
50
51    LocalClock lc;
52
53    mState.enabledTracks= 0;
54    mState.needsChanged = 0;
55    mState.frameCount   = frameCount;
56    mState.hook         = process__nop;
57    mState.outputTemp   = NULL;
58    mState.resampleTemp = NULL;
59    // mState.reserved
60    track_t* t = mState.tracks;
61    for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
62        t->needs = 0;
63        t->volume[0] = UNITY_GAIN;
64        t->volume[1] = UNITY_GAIN;
65        // no initialization needed
66        // t->prevVolume[0]
67        // t->prevVolume[1]
68        t->volumeInc[0] = 0;
69        t->volumeInc[1] = 0;
70        t->auxLevel = 0;
71        t->auxInc = 0;
72        // no initialization needed
73        // t->prevAuxLevel
74        // t->frameCount
75        t->channelCount = 2;
76        t->enabled = false;
77        t->format = 16;
78        t->channelMask = AUDIO_CHANNEL_OUT_STEREO;
79        t->bufferProvider = NULL;
80        t->buffer.raw = NULL;
81        // t->buffer.frameCount
82        t->hook = NULL;
83        t->in = NULL;
84        t->resampler = NULL;
85        t->sampleRate = mSampleRate;
86        t->mainBuffer = NULL;
87        t->auxBuffer = NULL;
88        t->localTimeFreq = lc.getLocalFreq();
89        t++;
90    }
91}
92
93AudioMixer::~AudioMixer()
94{
95    track_t* t = mState.tracks;
96    for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
97        delete t->resampler;
98        t++;
99    }
100    delete [] mState.outputTemp;
101    delete [] mState.resampleTemp;
102}
103
104int AudioMixer::getTrackName()
105{
106    uint32_t names = ~mTrackNames;
107    if (names != 0) {
108        int n = __builtin_ctz(names);
109        ALOGV("add track (%d)", n);
110        mTrackNames |= 1 << n;
111        return TRACK0 + n;
112    }
113    return -1;
114}
115
116void AudioMixer::invalidateState(uint32_t mask)
117{
118    if (mask) {
119        mState.needsChanged |= mask;
120        mState.hook = process__validate;
121    }
122 }
123
124void AudioMixer::deleteTrackName(int name)
125{
126    name -= TRACK0;
127    ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
128    ALOGV("deleteTrackName(%d)", name);
129    track_t& track(mState.tracks[ name ]);
130    if (track.enabled) {
131        track.enabled = false;
132        invalidateState(1<<name);
133    }
134    if (track.resampler != NULL) {
135        // delete  the resampler
136        delete track.resampler;
137        track.resampler = NULL;
138        track.sampleRate = mSampleRate;
139        invalidateState(1<<name);
140    }
141    track.volumeInc[0] = 0;
142    track.volumeInc[1] = 0;
143    mTrackNames &= ~(1<<name);
144}
145
146void AudioMixer::enable(int name)
147{
148    name -= TRACK0;
149    ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
150    track_t& track = mState.tracks[name];
151
152    if (!track.enabled) {
153        track.enabled = true;
154        ALOGV("enable(%d)", name);
155        invalidateState(1 << name);
156    }
157}
158
159void AudioMixer::disable(int name)
160{
161    name -= TRACK0;
162    ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
163    track_t& track = mState.tracks[name];
164
165    if (track.enabled) {
166        track.enabled = false;
167        ALOGV("disable(%d)", name);
168        invalidateState(1 << name);
169    }
170}
171
172void AudioMixer::setParameter(int name, int target, int param, void *value)
173{
174    name -= TRACK0;
175    ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
176    track_t& track = mState.tracks[name];
177
178    int valueInt = (int)value;
179    int32_t *valueBuf = (int32_t *)value;
180
181    switch (target) {
182
183    case TRACK:
184        switch (param) {
185        case CHANNEL_MASK: {
186            uint32_t mask = (uint32_t)value;
187            if (track.channelMask != mask) {
188                uint32_t channelCount = popcount(mask);
189                ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS) && (channelCount),
190                        "bad channel count %u", channelCount);
191                track.channelMask = mask;
192                track.channelCount = channelCount;
193                ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", mask);
194                invalidateState(1 << name);
195            }
196            } break;
197        case MAIN_BUFFER:
198            if (track.mainBuffer != valueBuf) {
199                track.mainBuffer = valueBuf;
200                ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
201                invalidateState(1 << name);
202            }
203            break;
204        case AUX_BUFFER:
205            if (track.auxBuffer != valueBuf) {
206                track.auxBuffer = valueBuf;
207                ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
208                invalidateState(1 << name);
209            }
210            break;
211        default:
212            LOG_FATAL("bad param");
213        }
214        break;
215
216    case RESAMPLE:
217        switch (param) {
218        case SAMPLE_RATE:
219            ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt);
220            if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
221                ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
222                        uint32_t(valueInt));
223                invalidateState(1 << name);
224            }
225            break;
226        case RESET:
227            track.resetResampler();
228            invalidateState(1 << name);
229            break;
230        default:
231            LOG_FATAL("bad param");
232        }
233        break;
234
235    case RAMP_VOLUME:
236    case VOLUME:
237        switch (param) {
238        case VOLUME0:
239        case VOLUME1:
240            if (track.volume[param-VOLUME0] != valueInt) {
241                ALOGV("setParameter(VOLUME, VOLUME0/1: %04x)", valueInt);
242                track.prevVolume[param-VOLUME0] = track.volume[param-VOLUME0] << 16;
243                track.volume[param-VOLUME0] = valueInt;
244                if (target == VOLUME) {
245                    track.prevVolume[param-VOLUME0] = valueInt << 16;
246                    track.volumeInc[param-VOLUME0] = 0;
247                } else {
248                    int32_t d = (valueInt<<16) - track.prevVolume[param-VOLUME0];
249                    int32_t volInc = d / int32_t(mState.frameCount);
250                    track.volumeInc[param-VOLUME0] = volInc;
251                    if (volInc == 0) {
252                        track.prevVolume[param-VOLUME0] = valueInt << 16;
253                    }
254                }
255                invalidateState(1 << name);
256            }
257            break;
258        case AUXLEVEL:
259            //ALOG_ASSERT(0 <= valueInt && valueInt <= MAX_GAIN_INT, "bad aux level %d", valueInt);
260            if (track.auxLevel != valueInt) {
261                ALOGV("setParameter(VOLUME, AUXLEVEL: %04x)", valueInt);
262                track.prevAuxLevel = track.auxLevel << 16;
263                track.auxLevel = valueInt;
264                if (target == VOLUME) {
265                    track.prevAuxLevel = valueInt << 16;
266                    track.auxInc = 0;
267                } else {
268                    int32_t d = (valueInt<<16) - track.prevAuxLevel;
269                    int32_t volInc = d / int32_t(mState.frameCount);
270                    track.auxInc = volInc;
271                    if (volInc == 0) {
272                        track.prevAuxLevel = valueInt << 16;
273                    }
274                }
275                invalidateState(1 << name);
276            }
277            break;
278        default:
279            LOG_FATAL("bad param");
280        }
281        break;
282
283    default:
284        LOG_FATAL("bad target");
285    }
286}
287
288bool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate)
289{
290    if (value!=devSampleRate || resampler) {
291        if (sampleRate != value) {
292            sampleRate = value;
293            if (resampler == NULL) {
294                resampler = AudioResampler::create(
295                        format, channelCount, devSampleRate);
296                resampler->setLocalTimeFreq(localTimeFreq);
297            }
298            return true;
299        }
300    }
301    return false;
302}
303
304inline
305void AudioMixer::track_t::adjustVolumeRamp(bool aux)
306{
307    for (uint32_t i=0 ; i<MAX_NUM_CHANNELS ; i++) {
308        if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
309            ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
310            volumeInc[i] = 0;
311            prevVolume[i] = volume[i]<<16;
312        }
313    }
314    if (aux) {
315        if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
316            ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
317            auxInc = 0;
318            prevAuxLevel = auxLevel<<16;
319        }
320    }
321}
322
323size_t AudioMixer::getUnreleasedFrames(int name) const
324{
325    name -= TRACK0;
326    if (uint32_t(name) < MAX_NUM_TRACKS) {
327        return mState.tracks[name].getUnreleasedFrames();
328    }
329    return 0;
330}
331
332void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider)
333{
334    name -= TRACK0;
335    ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
336    mState.tracks[name].bufferProvider = bufferProvider;
337}
338
339
340
341void AudioMixer::process(int64_t pts)
342{
343    mState.hook(&mState, pts);
344}
345
346
347void AudioMixer::process__validate(state_t* state, int64_t pts)
348{
349    ALOGW_IF(!state->needsChanged,
350        "in process__validate() but nothing's invalid");
351
352    uint32_t changed = state->needsChanged;
353    state->needsChanged = 0; // clear the validation flag
354
355    // recompute which tracks are enabled / disabled
356    uint32_t enabled = 0;
357    uint32_t disabled = 0;
358    while (changed) {
359        const int i = 31 - __builtin_clz(changed);
360        const uint32_t mask = 1<<i;
361        changed &= ~mask;
362        track_t& t = state->tracks[i];
363        (t.enabled ? enabled : disabled) |= mask;
364    }
365    state->enabledTracks &= ~disabled;
366    state->enabledTracks |=  enabled;
367
368    // compute everything we need...
369    int countActiveTracks = 0;
370    bool all16BitsStereoNoResample = true;
371    bool resampling = false;
372    bool volumeRamp = false;
373    uint32_t en = state->enabledTracks;
374    while (en) {
375        const int i = 31 - __builtin_clz(en);
376        en &= ~(1<<i);
377
378        countActiveTracks++;
379        track_t& t = state->tracks[i];
380        uint32_t n = 0;
381        n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
382        n |= NEEDS_FORMAT_16;
383        n |= t.doesResample() ? NEEDS_RESAMPLE_ENABLED : NEEDS_RESAMPLE_DISABLED;
384        if (t.auxLevel != 0 && t.auxBuffer != NULL) {
385            n |= NEEDS_AUX_ENABLED;
386        }
387
388        if (t.volumeInc[0]|t.volumeInc[1]) {
389            volumeRamp = true;
390        } else if (!t.doesResample() && t.volumeRL == 0) {
391            n |= NEEDS_MUTE_ENABLED;
392        }
393        t.needs = n;
394
395        if ((n & NEEDS_MUTE__MASK) == NEEDS_MUTE_ENABLED) {
396            t.hook = track__nop;
397        } else {
398            if ((n & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED) {
399                all16BitsStereoNoResample = false;
400            }
401            if ((n & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
402                all16BitsStereoNoResample = false;
403                resampling = true;
404                t.hook = track__genericResample;
405            } else {
406                if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
407                    t.hook = track__16BitsMono;
408                    all16BitsStereoNoResample = false;
409                }
410                if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_2){
411                    t.hook = track__16BitsStereo;
412                }
413            }
414        }
415    }
416
417    // select the processing hooks
418    state->hook = process__nop;
419    if (countActiveTracks) {
420        if (resampling) {
421            if (!state->outputTemp) {
422                state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
423            }
424            if (!state->resampleTemp) {
425                state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
426            }
427            state->hook = process__genericResampling;
428        } else {
429            if (state->outputTemp) {
430                delete [] state->outputTemp;
431                state->outputTemp = NULL;
432            }
433            if (state->resampleTemp) {
434                delete [] state->resampleTemp;
435                state->resampleTemp = NULL;
436            }
437            state->hook = process__genericNoResampling;
438            if (all16BitsStereoNoResample && !volumeRamp) {
439                if (countActiveTracks == 1) {
440                    state->hook = process__OneTrack16BitsStereoNoResampling;
441                }
442            }
443        }
444    }
445
446    ALOGV("mixer configuration change: %d activeTracks (%08x) "
447        "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
448        countActiveTracks, state->enabledTracks,
449        all16BitsStereoNoResample, resampling, volumeRamp);
450
451   state->hook(state, pts);
452
453    // Now that the volume ramp has been done, set optimal state and
454    // track hooks for subsequent mixer process
455    if (countActiveTracks) {
456        bool allMuted = true;
457        uint32_t en = state->enabledTracks;
458        while (en) {
459            const int i = 31 - __builtin_clz(en);
460            en &= ~(1<<i);
461            track_t& t = state->tracks[i];
462            if (!t.doesResample() && t.volumeRL == 0)
463            {
464                t.needs |= NEEDS_MUTE_ENABLED;
465                t.hook = track__nop;
466            } else {
467                allMuted = false;
468            }
469        }
470        if (allMuted) {
471            state->hook = process__nop;
472        } else if (all16BitsStereoNoResample) {
473            if (countActiveTracks == 1) {
474                state->hook = process__OneTrack16BitsStereoNoResampling;
475            }
476        }
477    }
478}
479
480
481void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux)
482{
483    t->resampler->setSampleRate(t->sampleRate);
484
485    // ramp gain - resample to temp buffer and scale/mix in 2nd step
486    if (aux != NULL) {
487        // always resample with unity gain when sending to auxiliary buffer to be able
488        // to apply send level after resampling
489        // TODO: modify each resampler to support aux channel?
490        t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
491        memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
492        t->resampler->resample(temp, outFrameCount, t->bufferProvider);
493        if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
494            volumeRampStereo(t, out, outFrameCount, temp, aux);
495        } else {
496            volumeStereo(t, out, outFrameCount, temp, aux);
497        }
498    } else {
499        if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
500            t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
501            memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
502            t->resampler->resample(temp, outFrameCount, t->bufferProvider);
503            volumeRampStereo(t, out, outFrameCount, temp, aux);
504        }
505
506        // constant gain
507        else {
508            t->resampler->setVolume(t->volume[0], t->volume[1]);
509            t->resampler->resample(out, outFrameCount, t->bufferProvider);
510        }
511    }
512}
513
514void AudioMixer::track__nop(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux)
515{
516}
517
518void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
519{
520    int32_t vl = t->prevVolume[0];
521    int32_t vr = t->prevVolume[1];
522    const int32_t vlInc = t->volumeInc[0];
523    const int32_t vrInc = t->volumeInc[1];
524
525    //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
526    //        t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
527    //       (vl + vlInc*frameCount)/65536.0f, frameCount);
528
529    // ramp volume
530    if (CC_UNLIKELY(aux != NULL)) {
531        int32_t va = t->prevAuxLevel;
532        const int32_t vaInc = t->auxInc;
533        int32_t l;
534        int32_t r;
535
536        do {
537            l = (*temp++ >> 12);
538            r = (*temp++ >> 12);
539            *out++ += (vl >> 16) * l;
540            *out++ += (vr >> 16) * r;
541            *aux++ += (va >> 17) * (l + r);
542            vl += vlInc;
543            vr += vrInc;
544            va += vaInc;
545        } while (--frameCount);
546        t->prevAuxLevel = va;
547    } else {
548        do {
549            *out++ += (vl >> 16) * (*temp++ >> 12);
550            *out++ += (vr >> 16) * (*temp++ >> 12);
551            vl += vlInc;
552            vr += vrInc;
553        } while (--frameCount);
554    }
555    t->prevVolume[0] = vl;
556    t->prevVolume[1] = vr;
557    t->adjustVolumeRamp(aux != NULL);
558}
559
560void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
561{
562    const int16_t vl = t->volume[0];
563    const int16_t vr = t->volume[1];
564
565    if (CC_UNLIKELY(aux != NULL)) {
566        const int16_t va = t->auxLevel;
567        do {
568            int16_t l = (int16_t)(*temp++ >> 12);
569            int16_t r = (int16_t)(*temp++ >> 12);
570            out[0] = mulAdd(l, vl, out[0]);
571            int16_t a = (int16_t)(((int32_t)l + r) >> 1);
572            out[1] = mulAdd(r, vr, out[1]);
573            out += 2;
574            aux[0] = mulAdd(a, va, aux[0]);
575            aux++;
576        } while (--frameCount);
577    } else {
578        do {
579            int16_t l = (int16_t)(*temp++ >> 12);
580            int16_t r = (int16_t)(*temp++ >> 12);
581            out[0] = mulAdd(l, vl, out[0]);
582            out[1] = mulAdd(r, vr, out[1]);
583            out += 2;
584        } while (--frameCount);
585    }
586}
587
588void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
589{
590    const int16_t *in = static_cast<const int16_t *>(t->in);
591
592    if (CC_UNLIKELY(aux != NULL)) {
593        int32_t l;
594        int32_t r;
595        // ramp gain
596        if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
597            int32_t vl = t->prevVolume[0];
598            int32_t vr = t->prevVolume[1];
599            int32_t va = t->prevAuxLevel;
600            const int32_t vlInc = t->volumeInc[0];
601            const int32_t vrInc = t->volumeInc[1];
602            const int32_t vaInc = t->auxInc;
603            // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
604            //        t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
605            //        (vl + vlInc*frameCount)/65536.0f, frameCount);
606
607            do {
608                l = (int32_t)*in++;
609                r = (int32_t)*in++;
610                *out++ += (vl >> 16) * l;
611                *out++ += (vr >> 16) * r;
612                *aux++ += (va >> 17) * (l + r);
613                vl += vlInc;
614                vr += vrInc;
615                va += vaInc;
616            } while (--frameCount);
617
618            t->prevVolume[0] = vl;
619            t->prevVolume[1] = vr;
620            t->prevAuxLevel = va;
621            t->adjustVolumeRamp(true);
622        }
623
624        // constant gain
625        else {
626            const uint32_t vrl = t->volumeRL;
627            const int16_t va = (int16_t)t->auxLevel;
628            do {
629                uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
630                int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
631                in += 2;
632                out[0] = mulAddRL(1, rl, vrl, out[0]);
633                out[1] = mulAddRL(0, rl, vrl, out[1]);
634                out += 2;
635                aux[0] = mulAdd(a, va, aux[0]);
636                aux++;
637            } while (--frameCount);
638        }
639    } else {
640        // ramp gain
641        if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
642            int32_t vl = t->prevVolume[0];
643            int32_t vr = t->prevVolume[1];
644            const int32_t vlInc = t->volumeInc[0];
645            const int32_t vrInc = t->volumeInc[1];
646
647            // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
648            //        t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
649            //        (vl + vlInc*frameCount)/65536.0f, frameCount);
650
651            do {
652                *out++ += (vl >> 16) * (int32_t) *in++;
653                *out++ += (vr >> 16) * (int32_t) *in++;
654                vl += vlInc;
655                vr += vrInc;
656            } while (--frameCount);
657
658            t->prevVolume[0] = vl;
659            t->prevVolume[1] = vr;
660            t->adjustVolumeRamp(false);
661        }
662
663        // constant gain
664        else {
665            const uint32_t vrl = t->volumeRL;
666            do {
667                uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
668                in += 2;
669                out[0] = mulAddRL(1, rl, vrl, out[0]);
670                out[1] = mulAddRL(0, rl, vrl, out[1]);
671                out += 2;
672            } while (--frameCount);
673        }
674    }
675    t->in = in;
676}
677
678void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
679{
680    const int16_t *in = static_cast<int16_t const *>(t->in);
681
682    if (CC_UNLIKELY(aux != NULL)) {
683        // ramp gain
684        if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
685            int32_t vl = t->prevVolume[0];
686            int32_t vr = t->prevVolume[1];
687            int32_t va = t->prevAuxLevel;
688            const int32_t vlInc = t->volumeInc[0];
689            const int32_t vrInc = t->volumeInc[1];
690            const int32_t vaInc = t->auxInc;
691
692            // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
693            //         t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
694            //         (vl + vlInc*frameCount)/65536.0f, frameCount);
695
696            do {
697                int32_t l = *in++;
698                *out++ += (vl >> 16) * l;
699                *out++ += (vr >> 16) * l;
700                *aux++ += (va >> 16) * l;
701                vl += vlInc;
702                vr += vrInc;
703                va += vaInc;
704            } while (--frameCount);
705
706            t->prevVolume[0] = vl;
707            t->prevVolume[1] = vr;
708            t->prevAuxLevel = va;
709            t->adjustVolumeRamp(true);
710        }
711        // constant gain
712        else {
713            const int16_t vl = t->volume[0];
714            const int16_t vr = t->volume[1];
715            const int16_t va = (int16_t)t->auxLevel;
716            do {
717                int16_t l = *in++;
718                out[0] = mulAdd(l, vl, out[0]);
719                out[1] = mulAdd(l, vr, out[1]);
720                out += 2;
721                aux[0] = mulAdd(l, va, aux[0]);
722                aux++;
723            } while (--frameCount);
724        }
725    } else {
726        // ramp gain
727        if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
728            int32_t vl = t->prevVolume[0];
729            int32_t vr = t->prevVolume[1];
730            const int32_t vlInc = t->volumeInc[0];
731            const int32_t vrInc = t->volumeInc[1];
732
733            // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
734            //         t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
735            //         (vl + vlInc*frameCount)/65536.0f, frameCount);
736
737            do {
738                int32_t l = *in++;
739                *out++ += (vl >> 16) * l;
740                *out++ += (vr >> 16) * l;
741                vl += vlInc;
742                vr += vrInc;
743            } while (--frameCount);
744
745            t->prevVolume[0] = vl;
746            t->prevVolume[1] = vr;
747            t->adjustVolumeRamp(false);
748        }
749        // constant gain
750        else {
751            const int16_t vl = t->volume[0];
752            const int16_t vr = t->volume[1];
753            do {
754                int16_t l = *in++;
755                out[0] = mulAdd(l, vl, out[0]);
756                out[1] = mulAdd(l, vr, out[1]);
757                out += 2;
758            } while (--frameCount);
759        }
760    }
761    t->in = in;
762}
763
764// no-op case
765void AudioMixer::process__nop(state_t* state, int64_t pts)
766{
767    uint32_t e0 = state->enabledTracks;
768    size_t bufSize = state->frameCount * sizeof(int16_t) * MAX_NUM_CHANNELS;
769    while (e0) {
770        // process by group of tracks with same output buffer to
771        // avoid multiple memset() on same buffer
772        uint32_t e1 = e0, e2 = e0;
773        int i = 31 - __builtin_clz(e1);
774        track_t& t1 = state->tracks[i];
775        e2 &= ~(1<<i);
776        while (e2) {
777            i = 31 - __builtin_clz(e2);
778            e2 &= ~(1<<i);
779            track_t& t2 = state->tracks[i];
780            if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
781                e1 &= ~(1<<i);
782            }
783        }
784        e0 &= ~(e1);
785
786        memset(t1.mainBuffer, 0, bufSize);
787
788        while (e1) {
789            i = 31 - __builtin_clz(e1);
790            e1 &= ~(1<<i);
791            t1 = state->tracks[i];
792            size_t outFrames = state->frameCount;
793            while (outFrames) {
794                t1.buffer.frameCount = outFrames;
795                int64_t outputPTS = calculateOutputPTS(
796                    t1, pts, state->frameCount - outFrames);
797                t1.bufferProvider->getNextBuffer(&t1.buffer, outputPTS);
798                if (t1.buffer.raw == NULL) break;
799                outFrames -= t1.buffer.frameCount;
800                t1.bufferProvider->releaseBuffer(&t1.buffer);
801            }
802        }
803    }
804}
805
806// generic code without resampling
807void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts)
808{
809    int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
810
811    // acquire each track's buffer
812    uint32_t enabledTracks = state->enabledTracks;
813    uint32_t e0 = enabledTracks;
814    while (e0) {
815        const int i = 31 - __builtin_clz(e0);
816        e0 &= ~(1<<i);
817        track_t& t = state->tracks[i];
818        t.buffer.frameCount = state->frameCount;
819        t.bufferProvider->getNextBuffer(&t.buffer, pts);
820        t.frameCount = t.buffer.frameCount;
821        t.in = t.buffer.raw;
822        // t.in == NULL can happen if the track was flushed just after having
823        // been enabled for mixing.
824        if (t.in == NULL)
825            enabledTracks &= ~(1<<i);
826    }
827
828    e0 = enabledTracks;
829    while (e0) {
830        // process by group of tracks with same output buffer to
831        // optimize cache use
832        uint32_t e1 = e0, e2 = e0;
833        int j = 31 - __builtin_clz(e1);
834        track_t& t1 = state->tracks[j];
835        e2 &= ~(1<<j);
836        while (e2) {
837            j = 31 - __builtin_clz(e2);
838            e2 &= ~(1<<j);
839            track_t& t2 = state->tracks[j];
840            if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
841                e1 &= ~(1<<j);
842            }
843        }
844        e0 &= ~(e1);
845        // this assumes output 16 bits stereo, no resampling
846        int32_t *out = t1.mainBuffer;
847        size_t numFrames = 0;
848        do {
849            memset(outTemp, 0, sizeof(outTemp));
850            e2 = e1;
851            while (e2) {
852                const int i = 31 - __builtin_clz(e2);
853                e2 &= ~(1<<i);
854                track_t& t = state->tracks[i];
855                size_t outFrames = BLOCKSIZE;
856                int32_t *aux = NULL;
857                if (CC_UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED)) {
858                    aux = t.auxBuffer + numFrames;
859                }
860                while (outFrames) {
861                    size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
862                    if (inFrames) {
863                        t.hook(&t, outTemp + (BLOCKSIZE-outFrames)*MAX_NUM_CHANNELS, inFrames, state->resampleTemp, aux);
864                        t.frameCount -= inFrames;
865                        outFrames -= inFrames;
866                        if (CC_UNLIKELY(aux != NULL)) {
867                            aux += inFrames;
868                        }
869                    }
870                    if (t.frameCount == 0 && outFrames) {
871                        t.bufferProvider->releaseBuffer(&t.buffer);
872                        t.buffer.frameCount = (state->frameCount - numFrames) - (BLOCKSIZE - outFrames);
873                        int64_t outputPTS = calculateOutputPTS(
874                            t, pts, numFrames + (BLOCKSIZE - outFrames));
875                        t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
876                        t.in = t.buffer.raw;
877                        if (t.in == NULL) {
878                            enabledTracks &= ~(1<<i);
879                            e1 &= ~(1<<i);
880                            break;
881                        }
882                        t.frameCount = t.buffer.frameCount;
883                    }
884                }
885            }
886            ditherAndClamp(out, outTemp, BLOCKSIZE);
887            out += BLOCKSIZE;
888            numFrames += BLOCKSIZE;
889        } while (numFrames < state->frameCount);
890    }
891
892    // release each track's buffer
893    e0 = enabledTracks;
894    while (e0) {
895        const int i = 31 - __builtin_clz(e0);
896        e0 &= ~(1<<i);
897        track_t& t = state->tracks[i];
898        t.bufferProvider->releaseBuffer(&t.buffer);
899    }
900}
901
902
903// generic code with resampling
904void AudioMixer::process__genericResampling(state_t* state, int64_t pts)
905{
906    // this const just means that local variable outTemp doesn't change
907    int32_t* const outTemp = state->outputTemp;
908    const size_t size = sizeof(int32_t) * MAX_NUM_CHANNELS * state->frameCount;
909
910    size_t numFrames = state->frameCount;
911
912    uint32_t e0 = state->enabledTracks;
913    while (e0) {
914        // process by group of tracks with same output buffer
915        // to optimize cache use
916        uint32_t e1 = e0, e2 = e0;
917        int j = 31 - __builtin_clz(e1);
918        track_t& t1 = state->tracks[j];
919        e2 &= ~(1<<j);
920        while (e2) {
921            j = 31 - __builtin_clz(e2);
922            e2 &= ~(1<<j);
923            track_t& t2 = state->tracks[j];
924            if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
925                e1 &= ~(1<<j);
926            }
927        }
928        e0 &= ~(e1);
929        int32_t *out = t1.mainBuffer;
930        memset(outTemp, 0, size);
931        while (e1) {
932            const int i = 31 - __builtin_clz(e1);
933            e1 &= ~(1<<i);
934            track_t& t = state->tracks[i];
935            int32_t *aux = NULL;
936            if (CC_UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED)) {
937                aux = t.auxBuffer;
938            }
939
940            // this is a little goofy, on the resampling case we don't
941            // acquire/release the buffers because it's done by
942            // the resampler.
943            if ((t.needs & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
944                t.resampler->setPTS(pts);
945                t.hook(&t, outTemp, numFrames, state->resampleTemp, aux);
946            } else {
947
948                size_t outFrames = 0;
949
950                while (outFrames < numFrames) {
951                    t.buffer.frameCount = numFrames - outFrames;
952                    int64_t outputPTS = calculateOutputPTS(t, pts, outFrames);
953                    t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
954                    t.in = t.buffer.raw;
955                    // t.in == NULL can happen if the track was flushed just after having
956                    // been enabled for mixing.
957                    if (t.in == NULL) break;
958
959                    if (CC_UNLIKELY(aux != NULL)) {
960                        aux += outFrames;
961                    }
962                    t.hook(&t, outTemp + outFrames*MAX_NUM_CHANNELS, t.buffer.frameCount, state->resampleTemp, aux);
963                    outFrames += t.buffer.frameCount;
964                    t.bufferProvider->releaseBuffer(&t.buffer);
965                }
966            }
967        }
968        ditherAndClamp(out, outTemp, numFrames);
969    }
970}
971
972// one track, 16 bits stereo without resampling is the most common case
973void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state,
974                                                           int64_t pts)
975{
976    // This method is only called when state->enabledTracks has exactly
977    // one bit set.  The asserts below would verify this, but are commented out
978    // since the whole point of this method is to optimize performance.
979    //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled");
980    const int i = 31 - __builtin_clz(state->enabledTracks);
981    //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
982    const track_t& t = state->tracks[i];
983
984    AudioBufferProvider::Buffer& b(t.buffer);
985
986    int32_t* out = t.mainBuffer;
987    size_t numFrames = state->frameCount;
988
989    const int16_t vl = t.volume[0];
990    const int16_t vr = t.volume[1];
991    const uint32_t vrl = t.volumeRL;
992    while (numFrames) {
993        b.frameCount = numFrames;
994        int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer);
995        t.bufferProvider->getNextBuffer(&b, outputPTS);
996        const int16_t *in = b.i16;
997
998        // in == NULL can happen if the track was flushed just after having
999        // been enabled for mixing.
1000        if (in == NULL || ((unsigned long)in & 3)) {
1001            memset(out, 0, numFrames*MAX_NUM_CHANNELS*sizeof(int16_t));
1002            ALOGE_IF(((unsigned long)in & 3), "process stereo track: input buffer alignment pb: buffer %p track %d, channels %d, needs %08x",
1003                    in, i, t.channelCount, t.needs);
1004            return;
1005        }
1006        size_t outFrames = b.frameCount;
1007
1008        if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN || uint32_t(vr) > UNITY_GAIN)) {
1009            // volume is boosted, so we might need to clamp even though
1010            // we process only one track.
1011            do {
1012                uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1013                in += 2;
1014                int32_t l = mulRL(1, rl, vrl) >> 12;
1015                int32_t r = mulRL(0, rl, vrl) >> 12;
1016                // clamping...
1017                l = clamp16(l);
1018                r = clamp16(r);
1019                *out++ = (r<<16) | (l & 0xFFFF);
1020            } while (--outFrames);
1021        } else {
1022            do {
1023                uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1024                in += 2;
1025                int32_t l = mulRL(1, rl, vrl) >> 12;
1026                int32_t r = mulRL(0, rl, vrl) >> 12;
1027                *out++ = (r<<16) | (l & 0xFFFF);
1028            } while (--outFrames);
1029        }
1030        numFrames -= b.frameCount;
1031        t.bufferProvider->releaseBuffer(&b);
1032    }
1033}
1034
1035#if 0
1036// 2 tracks is also a common case
1037// NEVER used in current implementation of process__validate()
1038// only use if the 2 tracks have the same output buffer
1039void AudioMixer::process__TwoTracks16BitsStereoNoResampling(state_t* state,
1040                                                            int64_t pts)
1041{
1042    int i;
1043    uint32_t en = state->enabledTracks;
1044
1045    i = 31 - __builtin_clz(en);
1046    const track_t& t0 = state->tracks[i];
1047    AudioBufferProvider::Buffer& b0(t0.buffer);
1048
1049    en &= ~(1<<i);
1050    i = 31 - __builtin_clz(en);
1051    const track_t& t1 = state->tracks[i];
1052    AudioBufferProvider::Buffer& b1(t1.buffer);
1053
1054    const int16_t *in0;
1055    const int16_t vl0 = t0.volume[0];
1056    const int16_t vr0 = t0.volume[1];
1057    size_t frameCount0 = 0;
1058
1059    const int16_t *in1;
1060    const int16_t vl1 = t1.volume[0];
1061    const int16_t vr1 = t1.volume[1];
1062    size_t frameCount1 = 0;
1063
1064    //FIXME: only works if two tracks use same buffer
1065    int32_t* out = t0.mainBuffer;
1066    size_t numFrames = state->frameCount;
1067    const int16_t *buff = NULL;
1068
1069
1070    while (numFrames) {
1071
1072        if (frameCount0 == 0) {
1073            b0.frameCount = numFrames;
1074            int64_t outputPTS = calculateOutputPTS(t0, pts,
1075                                                   out - t0.mainBuffer);
1076            t0.bufferProvider->getNextBuffer(&b0, outputPTS);
1077            if (b0.i16 == NULL) {
1078                if (buff == NULL) {
1079                    buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1080                }
1081                in0 = buff;
1082                b0.frameCount = numFrames;
1083            } else {
1084                in0 = b0.i16;
1085            }
1086            frameCount0 = b0.frameCount;
1087        }
1088        if (frameCount1 == 0) {
1089            b1.frameCount = numFrames;
1090            int64_t outputPTS = calculateOutputPTS(t1, pts,
1091                                                   out - t0.mainBuffer);
1092            t1.bufferProvider->getNextBuffer(&b1, outputPTS);
1093            if (b1.i16 == NULL) {
1094                if (buff == NULL) {
1095                    buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1096                }
1097                in1 = buff;
1098                b1.frameCount = numFrames;
1099            } else {
1100                in1 = b1.i16;
1101            }
1102            frameCount1 = b1.frameCount;
1103        }
1104
1105        size_t outFrames = frameCount0 < frameCount1?frameCount0:frameCount1;
1106
1107        numFrames -= outFrames;
1108        frameCount0 -= outFrames;
1109        frameCount1 -= outFrames;
1110
1111        do {
1112            int32_t l0 = *in0++;
1113            int32_t r0 = *in0++;
1114            l0 = mul(l0, vl0);
1115            r0 = mul(r0, vr0);
1116            int32_t l = *in1++;
1117            int32_t r = *in1++;
1118            l = mulAdd(l, vl1, l0) >> 12;
1119            r = mulAdd(r, vr1, r0) >> 12;
1120            // clamping...
1121            l = clamp16(l);
1122            r = clamp16(r);
1123            *out++ = (r<<16) | (l & 0xFFFF);
1124        } while (--outFrames);
1125
1126        if (frameCount0 == 0) {
1127            t0.bufferProvider->releaseBuffer(&b0);
1128        }
1129        if (frameCount1 == 0) {
1130            t1.bufferProvider->releaseBuffer(&b1);
1131        }
1132    }
1133
1134    delete [] buff;
1135}
1136#endif
1137
1138int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS,
1139                                       int outputFrameIndex)
1140{
1141    if (AudioBufferProvider::kInvalidPTS == basePTS)
1142        return AudioBufferProvider::kInvalidPTS;
1143
1144    return basePTS + ((outputFrameIndex * t.localTimeFreq) / t.sampleRate);
1145}
1146
1147// ----------------------------------------------------------------------------
1148}; // namespace android
1149