blob: f0ccd8e25232c593ad3eaabf22315d76de43581a [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
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
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070024#include <utils/threads.h>
25
Glenn Kasten2dd4bdd2012-08-29 11:10:32 -070026#include <media/AudioBufferProvider.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070027#include "AudioResampler.h"
28
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070029#include <audio_effects/effect_downmix.h>
30#include <system/audio.h>
Glenn Kasten639482c2013-02-13 14:46:45 -080031#include <media/nbaio/NBLog.h>
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070032
Mathias Agopian65ab4712010-07-14 17:59:35 -070033namespace android {
34
35// ----------------------------------------------------------------------------
36
Mathias Agopian65ab4712010-07-14 17:59:35 -070037class AudioMixer
38{
39public:
Glenn Kasten5c94b6c2012-03-20 17:01:29 -070040 AudioMixer(size_t frameCount, uint32_t sampleRate,
41 uint32_t maxNumTracks = MAX_NUM_TRACKS);
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
Glenn Kastenc19e2242012-01-30 14:54:39 -080043 /*virtual*/ ~AudioMixer(); // non-virtual saves a v-table, restore if sub-classed
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
Glenn Kasten599fabc2012-03-08 12:33:37 -080045
46 // This mixer has a hard-coded upper limit of 32 active track inputs.
47 // Adding support for > 32 tracks would require more than simply changing this value.
Mathias Agopian65ab4712010-07-14 17:59:35 -070048 static const uint32_t MAX_NUM_TRACKS = 32;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070049 // maximum number of channels supported by the mixer
Glenn Kasten599fabc2012-03-08 12:33:37 -080050
51 // This mixer has a hard-coded upper limit of 2 channels for output.
52 // There is support for > 2 channel tracks down-mixed to 2 channel output via a down-mix effect.
53 // Adding support for > 2 channel output would require more than simply changing this value.
Mathias Agopian65ab4712010-07-14 17:59:35 -070054 static const uint32_t MAX_NUM_CHANNELS = 2;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070055 // maximum number of channels supported for the content
56 static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = 8;
Mathias Agopian65ab4712010-07-14 17:59:35 -070057
58 static const uint16_t UNITY_GAIN = 0x1000;
59
60 enum { // names
61
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080062 // track names (MAX_NUM_TRACKS units)
Mathias Agopian65ab4712010-07-14 17:59:35 -070063 TRACK0 = 0x1000,
64
Glenn Kasten1c48c3c2011-12-15 14:54:01 -080065 // 0x2000 is unused
Mathias Agopian65ab4712010-07-14 17:59:35 -070066
67 // setParameter targets
68 TRACK = 0x3000,
69 RESAMPLE = 0x3001,
70 RAMP_VOLUME = 0x3002, // ramp to new volume
71 VOLUME = 0x3003, // don't ramp
72
73 // set Parameter names
74 // for target TRACK
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070075 CHANNEL_MASK = 0x4000,
Mathias Agopian65ab4712010-07-14 17:59:35 -070076 FORMAT = 0x4001,
77 MAIN_BUFFER = 0x4002,
78 AUX_BUFFER = 0x4003,
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070079 DOWNMIX_TYPE = 0X4004,
Glenn Kasten639482c2013-02-13 14:46:45 -080080 FAST_INDEX = 0x4005, // for debugging only
Glenn Kasten362c4e62011-12-14 10:28:06 -080081 // for target RESAMPLE
Glenn Kasten4e2293f2012-04-12 09:39:07 -070082 SAMPLE_RATE = 0x4100, // Configure sample rate conversion on this track name;
83 // parameter 'value' is the new sample rate in Hz.
84 // Only creates a sample rate converter the first time that
85 // the track sample rate is different from the mix sample rate.
86 // If the new sample rate is the same as the mix sample rate,
87 // and a sample rate converter already exists,
88 // then the sample rate converter remains present but is a no-op.
89 RESET = 0x4101, // Reset sample rate converter without changing sample rate.
90 // This clears out the resampler's input buffer.
91 REMOVE = 0x4102, // Remove the sample rate converter on this track name;
92 // the track is restored to the mix sample rate.
Glenn Kasten362c4e62011-12-14 10:28:06 -080093 // for target RAMP_VOLUME and VOLUME (8 channels max)
Mathias Agopian65ab4712010-07-14 17:59:35 -070094 VOLUME0 = 0x4200,
95 VOLUME1 = 0x4201,
96 AUXLEVEL = 0x4210,
97 };
98
99
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800100 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
Glenn Kasten17a736c2012-02-14 08:52:15 -0800101
102 // Allocate a track name. Returns new track name if successful, -1 on failure.
Jean-Michel Trivid06e1322012-09-12 15:47:07 -0700103 int getTrackName(audio_channel_mask_t channelMask, int sessionId);
Glenn Kasten17a736c2012-02-14 08:52:15 -0800104
105 // Free an allocated track by name
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106 void deleteTrackName(int name);
107
Glenn Kasten17a736c2012-02-14 08:52:15 -0800108 // Enable or disable an allocated track by name
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800109 void enable(int name);
110 void disable(int name);
Glenn Kasten639482c2013-02-13 14:46:45 -0800111 bool enabled(int name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700112
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800113 void setParameter(int name, int target, int param, void *value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700114
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800115 void setBufferProvider(int name, AudioBufferProvider* bufferProvider);
John Grossman4ff14ba2012-02-08 16:37:41 -0800116 void process(int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700117
118 uint32_t trackNames() const { return mTrackNames; }
119
Glenn Kastenc59c0042012-02-02 14:06:11 -0800120 size_t getUnreleasedFrames(int name) const;
Eric Laurent071ccd52011-12-22 16:08:41 -0800121
Mathias Agopian65ab4712010-07-14 17:59:35 -0700122private:
123
124 enum {
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700125 NEEDS_CHANNEL_COUNT__MASK = 0x00000007,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700126 NEEDS_FORMAT__MASK = 0x000000F0,
127 NEEDS_MUTE__MASK = 0x00000100,
128 NEEDS_RESAMPLE__MASK = 0x00001000,
129 NEEDS_AUX__MASK = 0x00010000,
130 };
131
132 enum {
133 NEEDS_CHANNEL_1 = 0x00000000,
134 NEEDS_CHANNEL_2 = 0x00000001,
135
136 NEEDS_FORMAT_16 = 0x00000010,
137
138 NEEDS_MUTE_DISABLED = 0x00000000,
139 NEEDS_MUTE_ENABLED = 0x00000100,
140
141 NEEDS_RESAMPLE_DISABLED = 0x00000000,
142 NEEDS_RESAMPLE_ENABLED = 0x00001000,
143
144 NEEDS_AUX_DISABLED = 0x00000000,
145 NEEDS_AUX_ENABLED = 0x00010000,
146 };
147
Mathias Agopian65ab4712010-07-14 17:59:35 -0700148 struct state_t;
149 struct track_t;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700150 class DownmixerBufferProvider;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700152 typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp,
153 int32_t* aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700154 static const int BLOCKSIZE = 16; // 4 cache lines
155
156 struct track_t {
157 uint32_t needs;
158
159 union {
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800160 int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point
Mathias Agopian65ab4712010-07-14 17:59:35 -0700161 int32_t volumeRL;
162 };
163
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800164 int32_t prevVolume[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800166 // 16-byte boundary
167
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800168 int32_t volumeInc[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169 int32_t auxInc;
170 int32_t prevAuxLevel;
171
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800172 // 16-byte boundary
173
174 int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175 uint16_t frameCount;
176
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800177 uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
178 uint8_t format; // always 16
179 uint16_t enabled; // actually bool
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700180 audio_channel_mask_t channelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700181
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700182 // actual buffer provider used by the track hooks, see DownmixerBufferProvider below
183 // for how the Track buffer provider is wrapped by another one when dowmixing is required
Mathias Agopian65ab4712010-07-14 17:59:35 -0700184 AudioBufferProvider* bufferProvider;
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800185
186 // 16-byte boundary
187
188 mutable AudioBufferProvider::Buffer buffer; // 8 bytes
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189
190 hook_t hook;
Glenn Kasten54c3b662012-01-06 07:46:30 -0800191 const void* in; // current location in buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700192
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800193 // 16-byte boundary
194
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195 AudioResampler* resampler;
196 uint32_t sampleRate;
197 int32_t* mainBuffer;
198 int32_t* auxBuffer;
199
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800200 // 16-byte boundary
201
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700202 DownmixerBufferProvider* downmixerBufferProvider; // 4 bytes
203
Jean-Michel Trivid06e1322012-09-12 15:47:07 -0700204 int32_t sessionId;
205
Glenn Kasten639482c2013-02-13 14:46:45 -0800206 int32_t fastIndex;
207 int32_t magic;
208 static const int kMagic = 0x54637281;
209 //int32_t padding[1];
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800210
211 // 16-byte boundary
212
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800214 bool doesResample() const { return resampler != NULL; }
215 void resetResampler() { if (resampler != NULL) resampler->reset(); }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216 void adjustVolumeRamp(bool aux);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800217 size_t getUnreleasedFrames() const { return resampler != NULL ?
218 resampler->getUnreleasedFrames() : 0; };
Glenn Kasten639482c2013-02-13 14:46:45 -0800219 void checkMagic() {
220 if (magic != kMagic) {
221 ALOGE("magic=%#x fastIndex=%d", magic, fastIndex);
222 }
223 }
224
Mathias Agopian65ab4712010-07-14 17:59:35 -0700225 };
226
227 // pad to 32-bytes to fill cache line
228 struct state_t {
229 uint32_t enabledTracks;
230 uint32_t needsChanged;
231 size_t frameCount;
Glenn Kastena1117922012-01-26 10:53:32 -0800232 void (*hook)(state_t* state, int64_t pts); // one of process__*, never NULL
Mathias Agopian65ab4712010-07-14 17:59:35 -0700233 int32_t *outputTemp;
234 int32_t *resampleTemp;
Glenn Kasten639482c2013-02-13 14:46:45 -0800235 NBLog::Writer* mLog;
236 int32_t reserved[1];
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700237 // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800238 track_t tracks[MAX_NUM_TRACKS]; __attribute__((aligned(32)));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239 };
240
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700241 // AudioBufferProvider that wraps a track AudioBufferProvider by a call to a downmix effect
242 class DownmixerBufferProvider : public AudioBufferProvider {
243 public:
244 virtual status_t getNextBuffer(Buffer* buffer, int64_t pts);
245 virtual void releaseBuffer(Buffer* buffer);
246 DownmixerBufferProvider();
247 virtual ~DownmixerBufferProvider();
248
249 AudioBufferProvider* mTrackBufferProvider;
250 effect_handle_t mDownmixHandle;
251 effect_config_t mDownmixConfig;
252 };
253
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800254 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255 uint32_t mTrackNames;
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700256
257 // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS,
258 // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS
259 const uint32_t mConfiguredNames;
260
Mathias Agopian65ab4712010-07-14 17:59:35 -0700261 const uint32_t mSampleRate;
262
Glenn Kasten639482c2013-02-13 14:46:45 -0800263 NBLog::Writer* mLog;
264 NBLog::Writer mDummyLog;
265public:
266 void setLog(NBLog::Writer* log);
267private:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268 state_t mState __attribute__((aligned(32)));
269
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700270 // effect descriptor for the downmixer used by the mixer
271 static effect_descriptor_t dwnmFxDesc;
272 // indicates whether a downmix effect has been found and is usable by this mixer
273 static bool isMultichannelCapable;
274
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700275 // Call after changing either the enabled status of a track, or parameters of an enabled track.
276 // OK to call more often than that, but unnecessary.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277 void invalidateState(uint32_t mask);
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700278
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700279 static status_t initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700280 static status_t prepareTrackForDownmix(track_t* pTrack, int trackNum);
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700281 static void unprepareTrackForDownmix(track_t* pTrack, int trackName);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700283 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
284 int32_t* aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700286 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
287 int32_t* aux);
288 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
289 int32_t* aux);
290 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
291 int32_t* aux);
292 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
293 int32_t* aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294
John Grossman4ff14ba2012-02-08 16:37:41 -0800295 static void process__validate(state_t* state, int64_t pts);
296 static void process__nop(state_t* state, int64_t pts);
297 static void process__genericNoResampling(state_t* state, int64_t pts);
298 static void process__genericResampling(state_t* state, int64_t pts);
299 static void process__OneTrack16BitsStereoNoResampling(state_t* state,
300 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800301#if 0
John Grossman4ff14ba2012-02-08 16:37:41 -0800302 static void process__TwoTracks16BitsStereoNoResampling(state_t* state,
303 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800304#endif
John Grossman4ff14ba2012-02-08 16:37:41 -0800305
306 static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
307 int outputFrameIndex);
Glenn Kasten52008f82012-03-18 09:34:41 -0700308
309 static uint64_t sLocalTimeFreq;
310 static pthread_once_t sOnceControl;
311 static void sInitRoutine();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312};
313
314// ----------------------------------------------------------------------------
315}; // namespace android
316
317#endif // ANDROID_AUDIO_MIXER_H