blob: fd21fda013031beb83c283974e8ff78dc10bb02b [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>
31
Mathias Agopian65ab4712010-07-14 17:59:35 -070032namespace android {
33
34// ----------------------------------------------------------------------------
35
Mathias Agopian65ab4712010-07-14 17:59:35 -070036class AudioMixer
37{
38public:
Glenn Kasten5c94b6c2012-03-20 17:01:29 -070039 AudioMixer(size_t frameCount, uint32_t sampleRate,
40 uint32_t maxNumTracks = MAX_NUM_TRACKS);
Mathias Agopian65ab4712010-07-14 17:59:35 -070041
Glenn Kastenc19e2242012-01-30 14:54:39 -080042 /*virtual*/ ~AudioMixer(); // non-virtual saves a v-table, restore if sub-classed
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
Glenn Kasten599fabc2012-03-08 12:33:37 -080044
45 // This mixer has a hard-coded upper limit of 32 active track inputs.
46 // Adding support for > 32 tracks would require more than simply changing this value.
Mathias Agopian65ab4712010-07-14 17:59:35 -070047 static const uint32_t MAX_NUM_TRACKS = 32;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070048 // maximum number of channels supported by the mixer
Glenn Kasten599fabc2012-03-08 12:33:37 -080049
50 // This mixer has a hard-coded upper limit of 2 channels for output.
51 // There is support for > 2 channel tracks down-mixed to 2 channel output via a down-mix effect.
52 // Adding support for > 2 channel output would require more than simply changing this value.
Mathias Agopian65ab4712010-07-14 17:59:35 -070053 static const uint32_t MAX_NUM_CHANNELS = 2;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070054 // maximum number of channels supported for the content
55 static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = 8;
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
57 static const uint16_t UNITY_GAIN = 0x1000;
58
59 enum { // names
60
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080061 // track names (MAX_NUM_TRACKS units)
Mathias Agopian65ab4712010-07-14 17:59:35 -070062 TRACK0 = 0x1000,
63
Glenn Kasten1c48c3c2011-12-15 14:54:01 -080064 // 0x2000 is unused
Mathias Agopian65ab4712010-07-14 17:59:35 -070065
66 // setParameter targets
67 TRACK = 0x3000,
68 RESAMPLE = 0x3001,
69 RAMP_VOLUME = 0x3002, // ramp to new volume
70 VOLUME = 0x3003, // don't ramp
71
72 // set Parameter names
73 // for target TRACK
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070074 CHANNEL_MASK = 0x4000,
Mathias Agopian65ab4712010-07-14 17:59:35 -070075 FORMAT = 0x4001,
76 MAIN_BUFFER = 0x4002,
77 AUX_BUFFER = 0x4003,
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070078 DOWNMIX_TYPE = 0X4004,
Glenn Kasten362c4e62011-12-14 10:28:06 -080079 // for target RESAMPLE
Glenn Kasten4e2293f2012-04-12 09:39:07 -070080 SAMPLE_RATE = 0x4100, // Configure sample rate conversion on this track name;
81 // parameter 'value' is the new sample rate in Hz.
82 // Only creates a sample rate converter the first time that
83 // the track sample rate is different from the mix sample rate.
84 // If the new sample rate is the same as the mix sample rate,
85 // and a sample rate converter already exists,
86 // then the sample rate converter remains present but is a no-op.
87 RESET = 0x4101, // Reset sample rate converter without changing sample rate.
88 // This clears out the resampler's input buffer.
89 REMOVE = 0x4102, // Remove the sample rate converter on this track name;
90 // the track is restored to the mix sample rate.
Glenn Kasten362c4e62011-12-14 10:28:06 -080091 // for target RAMP_VOLUME and VOLUME (8 channels max)
Mathias Agopian65ab4712010-07-14 17:59:35 -070092 VOLUME0 = 0x4200,
93 VOLUME1 = 0x4201,
94 AUXLEVEL = 0x4210,
95 };
96
97
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080098 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
Glenn Kasten17a736c2012-02-14 08:52:15 -080099
100 // Allocate a track name. Returns new track name if successful, -1 on failure.
Jean-Michel Trivid06e1322012-09-12 15:47:07 -0700101 int getTrackName(audio_channel_mask_t channelMask, int sessionId);
Glenn Kasten17a736c2012-02-14 08:52:15 -0800102
103 // Free an allocated track by name
Mathias Agopian65ab4712010-07-14 17:59:35 -0700104 void deleteTrackName(int name);
105
Glenn Kasten17a736c2012-02-14 08:52:15 -0800106 // Enable or disable an allocated track by name
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800107 void enable(int name);
108 void disable(int name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700109
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800110 void setParameter(int name, int target, int param, void *value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800112 void setBufferProvider(int name, AudioBufferProvider* bufferProvider);
John Grossman4ff14ba2012-02-08 16:37:41 -0800113 void process(int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700114
115 uint32_t trackNames() const { return mTrackNames; }
116
Glenn Kastenc59c0042012-02-02 14:06:11 -0800117 size_t getUnreleasedFrames(int name) const;
Eric Laurent071ccd52011-12-22 16:08:41 -0800118
Mathias Agopian65ab4712010-07-14 17:59:35 -0700119private:
120
121 enum {
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700122 NEEDS_CHANNEL_COUNT__MASK = 0x00000007,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700123 NEEDS_FORMAT__MASK = 0x000000F0,
124 NEEDS_MUTE__MASK = 0x00000100,
125 NEEDS_RESAMPLE__MASK = 0x00001000,
126 NEEDS_AUX__MASK = 0x00010000,
127 };
128
129 enum {
130 NEEDS_CHANNEL_1 = 0x00000000,
131 NEEDS_CHANNEL_2 = 0x00000001,
132
133 NEEDS_FORMAT_16 = 0x00000010,
134
135 NEEDS_MUTE_DISABLED = 0x00000000,
136 NEEDS_MUTE_ENABLED = 0x00000100,
137
138 NEEDS_RESAMPLE_DISABLED = 0x00000000,
139 NEEDS_RESAMPLE_ENABLED = 0x00001000,
140
141 NEEDS_AUX_DISABLED = 0x00000000,
142 NEEDS_AUX_ENABLED = 0x00010000,
143 };
144
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145 struct state_t;
146 struct track_t;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700147 class DownmixerBufferProvider;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700148
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700149 typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp,
150 int32_t* aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151 static const int BLOCKSIZE = 16; // 4 cache lines
152
153 struct track_t {
154 uint32_t needs;
155
156 union {
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800157 int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158 int32_t volumeRL;
159 };
160
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800161 int32_t prevVolume[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700162
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800163 // 16-byte boundary
164
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800165 int32_t volumeInc[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700166 int32_t auxInc;
167 int32_t prevAuxLevel;
168
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800169 // 16-byte boundary
170
171 int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
Mathias Agopian65ab4712010-07-14 17:59:35 -0700172 uint16_t frameCount;
173
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800174 uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
175 uint8_t format; // always 16
176 uint16_t enabled; // actually bool
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700177 audio_channel_mask_t channelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700178
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700179 // actual buffer provider used by the track hooks, see DownmixerBufferProvider below
180 // for how the Track buffer provider is wrapped by another one when dowmixing is required
Mathias Agopian65ab4712010-07-14 17:59:35 -0700181 AudioBufferProvider* bufferProvider;
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800182
183 // 16-byte boundary
184
185 mutable AudioBufferProvider::Buffer buffer; // 8 bytes
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186
187 hook_t hook;
Glenn Kasten54c3b662012-01-06 07:46:30 -0800188 const void* in; // current location in buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800190 // 16-byte boundary
191
Mathias Agopian65ab4712010-07-14 17:59:35 -0700192 AudioResampler* resampler;
193 uint32_t sampleRate;
194 int32_t* mainBuffer;
195 int32_t* auxBuffer;
196
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800197 // 16-byte boundary
198
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700199 DownmixerBufferProvider* downmixerBufferProvider; // 4 bytes
200
Jean-Michel Trivid06e1322012-09-12 15:47:07 -0700201 int32_t sessionId;
202
203 int32_t padding[2];
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800204
205 // 16-byte boundary
206
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800208 bool doesResample() const { return resampler != NULL; }
209 void resetResampler() { if (resampler != NULL) resampler->reset(); }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700210 void adjustVolumeRamp(bool aux);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800211 size_t getUnreleasedFrames() const { return resampler != NULL ?
212 resampler->getUnreleasedFrames() : 0; };
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213 };
214
215 // pad to 32-bytes to fill cache line
216 struct state_t {
217 uint32_t enabledTracks;
218 uint32_t needsChanged;
219 size_t frameCount;
Glenn Kastena1117922012-01-26 10:53:32 -0800220 void (*hook)(state_t* state, int64_t pts); // one of process__*, never NULL
Mathias Agopian65ab4712010-07-14 17:59:35 -0700221 int32_t *outputTemp;
222 int32_t *resampleTemp;
223 int32_t reserved[2];
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700224 // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800225 track_t tracks[MAX_NUM_TRACKS]; __attribute__((aligned(32)));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226 };
227
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700228 // AudioBufferProvider that wraps a track AudioBufferProvider by a call to a downmix effect
229 class DownmixerBufferProvider : public AudioBufferProvider {
230 public:
231 virtual status_t getNextBuffer(Buffer* buffer, int64_t pts);
232 virtual void releaseBuffer(Buffer* buffer);
233 DownmixerBufferProvider();
234 virtual ~DownmixerBufferProvider();
235
236 AudioBufferProvider* mTrackBufferProvider;
237 effect_handle_t mDownmixHandle;
238 effect_config_t mDownmixConfig;
239 };
240
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800241 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 uint32_t mTrackNames;
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700243
244 // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS,
245 // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS
246 const uint32_t mConfiguredNames;
247
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248 const uint32_t mSampleRate;
249
250 state_t mState __attribute__((aligned(32)));
251
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700252 // effect descriptor for the downmixer used by the mixer
253 static effect_descriptor_t dwnmFxDesc;
254 // indicates whether a downmix effect has been found and is usable by this mixer
255 static bool isMultichannelCapable;
256
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700257 // Call after changing either the enabled status of a track, or parameters of an enabled track.
258 // OK to call more often than that, but unnecessary.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259 void invalidateState(uint32_t mask);
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700260
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700261 static status_t initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700262 static status_t prepareTrackForDownmix(track_t* pTrack, int trackNum);
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700263 static void unprepareTrackForDownmix(track_t* pTrack, int trackName);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700265 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
266 int32_t* aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267 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 -0700268 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
269 int32_t* aux);
270 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
271 int32_t* aux);
272 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
273 int32_t* aux);
274 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
275 int32_t* aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276
John Grossman4ff14ba2012-02-08 16:37:41 -0800277 static void process__validate(state_t* state, int64_t pts);
278 static void process__nop(state_t* state, int64_t pts);
279 static void process__genericNoResampling(state_t* state, int64_t pts);
280 static void process__genericResampling(state_t* state, int64_t pts);
281 static void process__OneTrack16BitsStereoNoResampling(state_t* state,
282 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800283#if 0
John Grossman4ff14ba2012-02-08 16:37:41 -0800284 static void process__TwoTracks16BitsStereoNoResampling(state_t* state,
285 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800286#endif
John Grossman4ff14ba2012-02-08 16:37:41 -0800287
288 static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
289 int outputFrameIndex);
Glenn Kasten52008f82012-03-18 09:34:41 -0700290
291 static uint64_t sLocalTimeFreq;
292 static pthread_once_t sOnceControl;
293 static void sInitRoutine();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294};
295
296// ----------------------------------------------------------------------------
297}; // namespace android
298
299#endif // ANDROID_AUDIO_MIXER_H