blob: a04fe952312345059c121da29fcf050610431085 [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
Mathias Agopian65ab4712010-07-14 17:59:35 -070026#include "AudioBufferProvider.h"
27#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
44 static const uint32_t MAX_NUM_TRACKS = 32;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070045 // maximum number of channels supported by the mixer
Mathias Agopian65ab4712010-07-14 17:59:35 -070046 static const uint32_t MAX_NUM_CHANNELS = 2;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070047 // maximum number of channels supported for the content
48 static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = 8;
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
50 static const uint16_t UNITY_GAIN = 0x1000;
51
52 enum { // names
53
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080054 // track names (MAX_NUM_TRACKS units)
Mathias Agopian65ab4712010-07-14 17:59:35 -070055 TRACK0 = 0x1000,
56
Glenn Kasten1c48c3c2011-12-15 14:54:01 -080057 // 0x2000 is unused
Mathias Agopian65ab4712010-07-14 17:59:35 -070058
59 // setParameter targets
60 TRACK = 0x3000,
61 RESAMPLE = 0x3001,
62 RAMP_VOLUME = 0x3002, // ramp to new volume
63 VOLUME = 0x3003, // don't ramp
64
65 // set Parameter names
66 // for target TRACK
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070067 CHANNEL_MASK = 0x4000,
Mathias Agopian65ab4712010-07-14 17:59:35 -070068 FORMAT = 0x4001,
69 MAIN_BUFFER = 0x4002,
70 AUX_BUFFER = 0x4003,
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070071 DOWNMIX_TYPE = 0X4004,
Glenn Kasten362c4e62011-12-14 10:28:06 -080072 // for target RESAMPLE
Mathias Agopian65ab4712010-07-14 17:59:35 -070073 SAMPLE_RATE = 0x4100,
Eric Laurent243f5f92011-02-28 16:52:51 -080074 RESET = 0x4101,
Glenn Kasten362c4e62011-12-14 10:28:06 -080075 // for target RAMP_VOLUME and VOLUME (8 channels max)
Mathias Agopian65ab4712010-07-14 17:59:35 -070076 VOLUME0 = 0x4200,
77 VOLUME1 = 0x4201,
78 AUXLEVEL = 0x4210,
79 };
80
81
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080082 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
Glenn Kasten17a736c2012-02-14 08:52:15 -080083
84 // Allocate a track name. Returns new track name if successful, -1 on failure.
Mathias Agopian65ab4712010-07-14 17:59:35 -070085 int getTrackName();
Glenn Kasten17a736c2012-02-14 08:52:15 -080086
87 // Free an allocated track by name
Mathias Agopian65ab4712010-07-14 17:59:35 -070088 void deleteTrackName(int name);
89
Glenn Kasten17a736c2012-02-14 08:52:15 -080090 // Enable or disable an allocated track by name
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080091 void enable(int name);
92 void disable(int name);
Mathias Agopian65ab4712010-07-14 17:59:35 -070093
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080094 void setParameter(int name, int target, int param, void *value);
Mathias Agopian65ab4712010-07-14 17:59:35 -070095
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080096 void setBufferProvider(int name, AudioBufferProvider* bufferProvider);
John Grossman4ff14ba2012-02-08 16:37:41 -080097 void process(int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -070098
99 uint32_t trackNames() const { return mTrackNames; }
100
Glenn Kastenc59c0042012-02-02 14:06:11 -0800101 size_t getUnreleasedFrames(int name) const;
Eric Laurent071ccd52011-12-22 16:08:41 -0800102
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103private:
104
105 enum {
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700106 NEEDS_CHANNEL_COUNT__MASK = 0x00000007,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700107 NEEDS_FORMAT__MASK = 0x000000F0,
108 NEEDS_MUTE__MASK = 0x00000100,
109 NEEDS_RESAMPLE__MASK = 0x00001000,
110 NEEDS_AUX__MASK = 0x00010000,
111 };
112
113 enum {
114 NEEDS_CHANNEL_1 = 0x00000000,
115 NEEDS_CHANNEL_2 = 0x00000001,
116
117 NEEDS_FORMAT_16 = 0x00000010,
118
119 NEEDS_MUTE_DISABLED = 0x00000000,
120 NEEDS_MUTE_ENABLED = 0x00000100,
121
122 NEEDS_RESAMPLE_DISABLED = 0x00000000,
123 NEEDS_RESAMPLE_ENABLED = 0x00001000,
124
125 NEEDS_AUX_DISABLED = 0x00000000,
126 NEEDS_AUX_ENABLED = 0x00010000,
127 };
128
Mathias Agopian65ab4712010-07-14 17:59:35 -0700129 struct state_t;
130 struct track_t;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700131 class DownmixerBufferProvider;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132
Mathias Agopian65ab4712010-07-14 17:59:35 -0700133 typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp, int32_t* aux);
134 static const int BLOCKSIZE = 16; // 4 cache lines
135
136 struct track_t {
137 uint32_t needs;
138
139 union {
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800140 int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point
Mathias Agopian65ab4712010-07-14 17:59:35 -0700141 int32_t volumeRL;
142 };
143
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800144 int32_t prevVolume[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800146 // 16-byte boundary
147
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800148 int32_t volumeInc[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149 int32_t auxInc;
150 int32_t prevAuxLevel;
151
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800152 // 16-byte boundary
153
154 int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155 uint16_t frameCount;
156
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800157 uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
158 uint8_t format; // always 16
159 uint16_t enabled; // actually bool
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700160 audio_channel_mask_t channelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700161
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700162 // actual buffer provider used by the track hooks, see DownmixerBufferProvider below
163 // for how the Track buffer provider is wrapped by another one when dowmixing is required
Mathias Agopian65ab4712010-07-14 17:59:35 -0700164 AudioBufferProvider* bufferProvider;
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800165
166 // 16-byte boundary
167
168 mutable AudioBufferProvider::Buffer buffer; // 8 bytes
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169
170 hook_t hook;
Glenn Kasten54c3b662012-01-06 07:46:30 -0800171 const void* in; // current location in buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700172
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800173 // 16-byte boundary
174
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175 AudioResampler* resampler;
176 uint32_t sampleRate;
177 int32_t* mainBuffer;
178 int32_t* auxBuffer;
179
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800180 // 16-byte boundary
181
John Grossman4ff14ba2012-02-08 16:37:41 -0800182 uint64_t localTimeFreq;
183
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700184 DownmixerBufferProvider* downmixerBufferProvider; // 4 bytes
185
186 int32_t padding;
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800187
188 // 16-byte boundary
189
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800191 bool doesResample() const { return resampler != NULL; }
192 void resetResampler() { if (resampler != NULL) resampler->reset(); }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193 void adjustVolumeRamp(bool aux);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800194 size_t getUnreleasedFrames() const { return resampler != NULL ?
195 resampler->getUnreleasedFrames() : 0; };
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196 };
197
198 // pad to 32-bytes to fill cache line
199 struct state_t {
200 uint32_t enabledTracks;
201 uint32_t needsChanged;
202 size_t frameCount;
Glenn Kastena1117922012-01-26 10:53:32 -0800203 void (*hook)(state_t* state, int64_t pts); // one of process__*, never NULL
Mathias Agopian65ab4712010-07-14 17:59:35 -0700204 int32_t *outputTemp;
205 int32_t *resampleTemp;
206 int32_t reserved[2];
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700207 // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800208 track_t tracks[MAX_NUM_TRACKS]; __attribute__((aligned(32)));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700209 };
210
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700211 // AudioBufferProvider that wraps a track AudioBufferProvider by a call to a downmix effect
212 class DownmixerBufferProvider : public AudioBufferProvider {
213 public:
214 virtual status_t getNextBuffer(Buffer* buffer, int64_t pts);
215 virtual void releaseBuffer(Buffer* buffer);
216 DownmixerBufferProvider();
217 virtual ~DownmixerBufferProvider();
218
219 AudioBufferProvider* mTrackBufferProvider;
220 effect_handle_t mDownmixHandle;
221 effect_config_t mDownmixConfig;
222 };
223
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800224 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700225 uint32_t mTrackNames;
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700226
227 // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS,
228 // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS
229 const uint32_t mConfiguredNames;
230
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231 const uint32_t mSampleRate;
232
233 state_t mState __attribute__((aligned(32)));
234
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700235 // effect descriptor for the downmixer used by the mixer
236 static effect_descriptor_t dwnmFxDesc;
237 // indicates whether a downmix effect has been found and is usable by this mixer
238 static bool isMultichannelCapable;
239
Mathias Agopian65ab4712010-07-14 17:59:35 -0700240 void invalidateState(uint32_t mask);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700241 static status_t prepareTrackForDownmix(track_t* pTrack, int trackNum);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242
243 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
244 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
245 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
246 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
247 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
248 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
249
John Grossman4ff14ba2012-02-08 16:37:41 -0800250 static void process__validate(state_t* state, int64_t pts);
251 static void process__nop(state_t* state, int64_t pts);
252 static void process__genericNoResampling(state_t* state, int64_t pts);
253 static void process__genericResampling(state_t* state, int64_t pts);
254 static void process__OneTrack16BitsStereoNoResampling(state_t* state,
255 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800256#if 0
John Grossman4ff14ba2012-02-08 16:37:41 -0800257 static void process__TwoTracks16BitsStereoNoResampling(state_t* state,
258 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800259#endif
John Grossman4ff14ba2012-02-08 16:37:41 -0800260
261 static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
262 int outputFrameIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263};
264
265// ----------------------------------------------------------------------------
266}; // namespace android
267
268#endif // ANDROID_AUDIO_MIXER_H