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 #ifndef ANDROID_AUDIO_MIXER_H
19 #define ANDROID_AUDIO_MIXER_H
20 
21 #include <stdint.h>
22 #include <sys/types.h>
23 
24 #include <hardware/audio_effect.h>
25 #include <media/AudioBufferProvider.h>
26 #include <media/AudioResamplerPublic.h>
27 #include <media/nbaio/NBLog.h>
28 #include <system/audio.h>
29 #include <utils/Compat.h>
30 #include <utils/threads.h>
31 
32 #include "AudioResampler.h"
33 #include "BufferProviders.h"
34 
35 // FIXME This is actually unity gain, which might not be max in future, expressed in U.12
36 #define MAX_GAIN_INT AudioMixer::UNITY_GAIN_INT
37 
38 namespace android {
39 
40 // ----------------------------------------------------------------------------
41 
42 class AudioMixer
43 {
44 public:
45                             AudioMixer(size_t frameCount, uint32_t sampleRate,
46                                        uint32_t maxNumTracks = MAX_NUM_TRACKS);
47 
48     /*virtual*/             ~AudioMixer();  // non-virtual saves a v-table, restore if sub-classed
49 
50 
51     // This mixer has a hard-coded upper limit of 32 active track inputs.
52     // Adding support for > 32 tracks would require more than simply changing this value.
53     static const uint32_t MAX_NUM_TRACKS = 32;
54     // maximum number of channels supported by the mixer
55 
56     // This mixer has a hard-coded upper limit of 8 channels for output.
57     static const uint32_t MAX_NUM_CHANNELS = 8;
58     static const uint32_t MAX_NUM_VOLUMES = 2; // stereo volume only
59     // maximum number of channels supported for the content
60     static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = AUDIO_CHANNEL_COUNT_MAX;
61 
62     static const uint16_t UNITY_GAIN_INT = 0x1000;
63     static const CONSTEXPR float UNITY_GAIN_FLOAT = 1.0f;
64 
65     enum { // names
66 
67         // track names (MAX_NUM_TRACKS units)
68         TRACK0          = 0x1000,
69 
70         // 0x2000 is unused
71 
72         // setParameter targets
73         TRACK           = 0x3000,
74         RESAMPLE        = 0x3001,
75         RAMP_VOLUME     = 0x3002, // ramp to new volume
76         VOLUME          = 0x3003, // don't ramp
77         TIMESTRETCH     = 0x3004,
78 
79         // set Parameter names
80         // for target TRACK
81         CHANNEL_MASK    = 0x4000,
82         FORMAT          = 0x4001,
83         MAIN_BUFFER     = 0x4002,
84         AUX_BUFFER      = 0x4003,
85         DOWNMIX_TYPE    = 0X4004,
86         MIXER_FORMAT    = 0x4005, // AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
87         MIXER_CHANNEL_MASK = 0x4006, // Channel mask for mixer output
88         // for target RESAMPLE
89         SAMPLE_RATE     = 0x4100, // Configure sample rate conversion on this track name;
90                                   // parameter 'value' is the new sample rate in Hz.
91                                   // Only creates a sample rate converter the first time that
92                                   // the track sample rate is different from the mix sample rate.
93                                   // If the new sample rate is the same as the mix sample rate,
94                                   // and a sample rate converter already exists,
95                                   // then the sample rate converter remains present but is a no-op.
96         RESET           = 0x4101, // Reset sample rate converter without changing sample rate.
97                                   // This clears out the resampler's input buffer.
98         REMOVE          = 0x4102, // Remove the sample rate converter on this track name;
99                                   // the track is restored to the mix sample rate.
100         // for target RAMP_VOLUME and VOLUME (8 channels max)
101         // FIXME use float for these 3 to improve the dynamic range
102         VOLUME0         = 0x4200,
103         VOLUME1         = 0x4201,
104         AUXLEVEL        = 0x4210,
105         // for target TIMESTRETCH
106         PLAYBACK_RATE   = 0x4300, // Configure timestretch on this track name;
107                                   // parameter 'value' is a pointer to the new playback rate.
108     };
109 
110 
111     // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
112 
113     // Allocate a track name.  Returns new track name if successful, -1 on failure.
114     // The failure could be because of an invalid channelMask or format, or that
115     // the track capacity of the mixer is exceeded.
116     int         getTrackName(audio_channel_mask_t channelMask,
117                              audio_format_t format, int sessionId);
118 
119     // Free an allocated track by name
120     void        deleteTrackName(int name);
121 
122     // Enable or disable an allocated track by name
123     void        enable(int name);
124     void        disable(int name);
125 
126     void        setParameter(int name, int target, int param, void *value);
127 
128     void        setBufferProvider(int name, AudioBufferProvider* bufferProvider);
129     void        process(int64_t pts);
130 
trackNames()131     uint32_t    trackNames() const { return mTrackNames; }
132 
133     size_t      getUnreleasedFrames(int name) const;
134 
isValidPcmTrackFormat(audio_format_t format)135     static inline bool isValidPcmTrackFormat(audio_format_t format) {
136         switch (format) {
137         case AUDIO_FORMAT_PCM_8_BIT:
138         case AUDIO_FORMAT_PCM_16_BIT:
139         case AUDIO_FORMAT_PCM_24_BIT_PACKED:
140         case AUDIO_FORMAT_PCM_32_BIT:
141         case AUDIO_FORMAT_PCM_FLOAT:
142             return true;
143         default:
144             return false;
145         }
146     }
147 
148 private:
149 
150     enum {
151         // FIXME this representation permits up to 8 channels
152         NEEDS_CHANNEL_COUNT__MASK   = 0x00000007,
153     };
154 
155     enum {
156         NEEDS_CHANNEL_1             = 0x00000000,   // mono
157         NEEDS_CHANNEL_2             = 0x00000001,   // stereo
158 
159         // sample format is not explicitly specified, and is assumed to be AUDIO_FORMAT_PCM_16_BIT
160 
161         NEEDS_MUTE                  = 0x00000100,
162         NEEDS_RESAMPLE              = 0x00001000,
163         NEEDS_AUX                   = 0x00010000,
164     };
165 
166     struct state_t;
167     struct track_t;
168 
169     typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp,
170                            int32_t* aux);
171     static const int BLOCKSIZE = 16; // 4 cache lines
172 
173     struct track_t {
174         uint32_t    needs;
175 
176         // TODO: Eventually remove legacy integer volume settings
177         union {
178         int16_t     volume[MAX_NUM_VOLUMES]; // U4.12 fixed point (top bit should be zero)
179         int32_t     volumeRL;
180         };
181 
182         int32_t     prevVolume[MAX_NUM_VOLUMES];
183 
184         // 16-byte boundary
185 
186         int32_t     volumeInc[MAX_NUM_VOLUMES];
187         int32_t     auxInc;
188         int32_t     prevAuxLevel;
189 
190         // 16-byte boundary
191 
192         int16_t     auxLevel;       // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
193         uint16_t    frameCount;
194 
195         uint8_t     channelCount;   // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
196         uint8_t     unused_padding; // formerly format, was always 16
197         uint16_t    enabled;        // actually bool
198         audio_channel_mask_t channelMask;
199 
200         // actual buffer provider used by the track hooks, see DownmixerBufferProvider below
201         //  for how the Track buffer provider is wrapped by another one when dowmixing is required
202         AudioBufferProvider*                bufferProvider;
203 
204         // 16-byte boundary
205 
206         mutable AudioBufferProvider::Buffer buffer; // 8 bytes
207 
208         hook_t      hook;
209         const void* in;             // current location in buffer
210 
211         // 16-byte boundary
212 
213         AudioResampler*     resampler;
214         uint32_t            sampleRate;
215         int32_t*           mainBuffer;
216         int32_t*           auxBuffer;
217 
218         // 16-byte boundary
219 
220         /* Buffer providers are constructed to translate the track input data as needed.
221          *
222          * TODO: perhaps make a single PlaybackConverterProvider class to move
223          * all pre-mixer track buffer conversions outside the AudioMixer class.
224          *
225          * 1) mInputBufferProvider: The AudioTrack buffer provider.
226          * 2) mReformatBufferProvider: If not NULL, performs the audio reformat to
227          *    match either mMixerInFormat or mDownmixRequiresFormat, if the downmixer
228          *    requires reformat. For example, it may convert floating point input to
229          *    PCM_16_bit if that's required by the downmixer.
230          * 3) downmixerBufferProvider: If not NULL, performs the channel remixing to match
231          *    the number of channels required by the mixer sink.
232          * 4) mPostDownmixReformatBufferProvider: If not NULL, performs reformatting from
233          *    the downmixer requirements to the mixer engine input requirements.
234          * 5) mTimestretchBufferProvider: Adds timestretching for playback rate
235          */
236         AudioBufferProvider*     mInputBufferProvider;    // externally provided buffer provider.
237         PassthruBufferProvider*  mReformatBufferProvider; // provider wrapper for reformatting.
238         PassthruBufferProvider*  downmixerBufferProvider; // wrapper for channel conversion.
239         PassthruBufferProvider*  mPostDownmixReformatBufferProvider;
240         PassthruBufferProvider*  mTimestretchBufferProvider;
241 
242         int32_t     sessionId;
243 
244         audio_format_t mMixerFormat;     // output mix format: AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
245         audio_format_t mFormat;          // input track format
246         audio_format_t mMixerInFormat;   // mix internal format AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
247                                          // each track must be converted to this format.
248         audio_format_t mDownmixRequiresFormat;  // required downmixer format
249                                                 // AUDIO_FORMAT_PCM_16_BIT if 16 bit necessary
250                                                 // AUDIO_FORMAT_INVALID if no required format
251 
252         float          mVolume[MAX_NUM_VOLUMES];     // floating point set volume
253         float          mPrevVolume[MAX_NUM_VOLUMES]; // floating point previous volume
254         float          mVolumeInc[MAX_NUM_VOLUMES];  // floating point volume increment
255 
256         float          mAuxLevel;                     // floating point set aux level
257         float          mPrevAuxLevel;                 // floating point prev aux level
258         float          mAuxInc;                       // floating point aux increment
259 
260         audio_channel_mask_t mMixerChannelMask;
261         uint32_t             mMixerChannelCount;
262 
263         AudioPlaybackRate    mPlaybackRate;
264 
needsRamptrack_t265         bool        needsRamp() { return (volumeInc[0] | volumeInc[1] | auxInc) != 0; }
266         bool        setResampler(uint32_t trackSampleRate, uint32_t devSampleRate);
doesResampletrack_t267         bool        doesResample() const { return resampler != NULL; }
resetResamplertrack_t268         void        resetResampler() { if (resampler != NULL) resampler->reset(); }
269         void        adjustVolumeRamp(bool aux, bool useFloat = false);
getUnreleasedFramestrack_t270         size_t      getUnreleasedFrames() const { return resampler != NULL ?
271                                                     resampler->getUnreleasedFrames() : 0; };
272 
273         status_t    prepareForDownmix();
274         void        unprepareForDownmix();
275         status_t    prepareForReformat();
276         void        unprepareForReformat();
277         bool        setPlaybackRate(const AudioPlaybackRate &playbackRate);
278         void        reconfigureBufferProviders();
279     };
280 
281     typedef void (*process_hook_t)(state_t* state, int64_t pts);
282 
283     // pad to 32-bytes to fill cache line
284     struct state_t {
285         uint32_t        enabledTracks;
286         uint32_t        needsChanged;
287         size_t          frameCount;
288         process_hook_t  hook;   // one of process__*, never NULL
289         int32_t         *outputTemp;
290         int32_t         *resampleTemp;
291         NBLog::Writer*  mLog;
292         int32_t         reserved[1];
293         // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS
294         track_t         tracks[MAX_NUM_TRACKS] __attribute__((aligned(32)));
295     };
296 
297     // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
298     uint32_t        mTrackNames;
299 
300     // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS,
301     // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS
302     const uint32_t  mConfiguredNames;
303 
304     const uint32_t  mSampleRate;
305 
306     NBLog::Writer   mDummyLog;
307 public:
308     void            setLog(NBLog::Writer* log);
309 private:
310     state_t         mState __attribute__((aligned(32)));
311 
312     // Call after changing either the enabled status of a track, or parameters of an enabled track.
313     // OK to call more often than that, but unnecessary.
314     void invalidateState(uint32_t mask);
315 
316     bool setChannelMasks(int name,
317             audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask);
318 
319     static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
320             int32_t* aux);
321     static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
322     static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
323             int32_t* aux);
324     static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
325             int32_t* aux);
326     static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
327             int32_t* aux);
328     static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
329             int32_t* aux);
330 
331     static void process__validate(state_t* state, int64_t pts);
332     static void process__nop(state_t* state, int64_t pts);
333     static void process__genericNoResampling(state_t* state, int64_t pts);
334     static void process__genericResampling(state_t* state, int64_t pts);
335     static void process__OneTrack16BitsStereoNoResampling(state_t* state,
336                                                           int64_t pts);
337 
338     static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
339                                       int outputFrameIndex);
340 
341     static uint64_t         sLocalTimeFreq;
342     static pthread_once_t   sOnceControl;
343     static void             sInitRoutine();
344 
345     /* multi-format volume mixing function (calls template functions
346      * in AudioMixerOps.h).  The template parameters are as follows:
347      *
348      *   MIXTYPE     (see AudioMixerOps.h MIXTYPE_* enumeration)
349      *   USEFLOATVOL (set to true if float volume is used)
350      *   ADJUSTVOL   (set to true if volume ramp parameters needs adjustment afterwards)
351      *   TO: int32_t (Q4.27) or float
352      *   TI: int32_t (Q4.27) or int16_t (Q0.15) or float
353      *   TA: int32_t (Q4.27)
354      */
355     template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
356         typename TO, typename TI, typename TA>
357     static void volumeMix(TO *out, size_t outFrames,
358             const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t);
359 
360     // multi-format process hooks
361     template <int MIXTYPE, typename TO, typename TI, typename TA>
362     static void process_NoResampleOneTrack(state_t* state, int64_t pts);
363 
364     // multi-format track hooks
365     template <int MIXTYPE, typename TO, typename TI, typename TA>
366     static void track__Resample(track_t* t, TO* out, size_t frameCount,
367             TO* temp __unused, TA* aux);
368     template <int MIXTYPE, typename TO, typename TI, typename TA>
369     static void track__NoResample(track_t* t, TO* out, size_t frameCount,
370             TO* temp __unused, TA* aux);
371 
372     static void convertMixerFormat(void *out, audio_format_t mixerOutFormat,
373             void *in, audio_format_t mixerInFormat, size_t sampleCount);
374 
375     // hook types
376     enum {
377         PROCESSTYPE_NORESAMPLEONETRACK,
378     };
379     enum {
380         TRACKTYPE_NOP,
381         TRACKTYPE_RESAMPLE,
382         TRACKTYPE_NORESAMPLE,
383         TRACKTYPE_NORESAMPLEMONO,
384     };
385 
386     // functions for determining the proper process and track hooks.
387     static process_hook_t getProcessHook(int processType, uint32_t channelCount,
388             audio_format_t mixerInFormat, audio_format_t mixerOutFormat);
389     static hook_t getTrackHook(int trackType, uint32_t channelCount,
390             audio_format_t mixerInFormat, audio_format_t mixerOutFormat);
391 };
392 
393 // ----------------------------------------------------------------------------
394 } // namespace android
395 
396 #endif // ANDROID_AUDIO_MIXER_H
397