blob: 9698d0c8bcd98a0eea82617c016c808b4a55671b [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
Glenn Kasten4e2293f2012-04-12 09:39:07 -070073 SAMPLE_RATE = 0x4100, // Configure sample rate conversion on this track name;
74 // parameter 'value' is the new sample rate in Hz.
75 // Only creates a sample rate converter the first time that
76 // the track sample rate is different from the mix sample rate.
77 // If the new sample rate is the same as the mix sample rate,
78 // and a sample rate converter already exists,
79 // then the sample rate converter remains present but is a no-op.
80 RESET = 0x4101, // Reset sample rate converter without changing sample rate.
81 // This clears out the resampler's input buffer.
82 REMOVE = 0x4102, // Remove the sample rate converter on this track name;
83 // the track is restored to the mix sample rate.
Glenn Kasten362c4e62011-12-14 10:28:06 -080084 // for target RAMP_VOLUME and VOLUME (8 channels max)
Mathias Agopian65ab4712010-07-14 17:59:35 -070085 VOLUME0 = 0x4200,
86 VOLUME1 = 0x4201,
87 AUXLEVEL = 0x4210,
88 };
89
90
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080091 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
Glenn Kasten17a736c2012-02-14 08:52:15 -080092
93 // Allocate a track name. Returns new track name if successful, -1 on failure.
Mathias Agopian65ab4712010-07-14 17:59:35 -070094 int getTrackName();
Glenn Kasten17a736c2012-02-14 08:52:15 -080095
96 // Free an allocated track by name
Mathias Agopian65ab4712010-07-14 17:59:35 -070097 void deleteTrackName(int name);
98
Glenn Kasten17a736c2012-02-14 08:52:15 -080099 // Enable or disable an allocated track by name
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800100 void enable(int name);
101 void disable(int name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800103 void setParameter(int name, int target, int param, void *value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700104
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800105 void setBufferProvider(int name, AudioBufferProvider* bufferProvider);
John Grossman4ff14ba2012-02-08 16:37:41 -0800106 void process(int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700107
108 uint32_t trackNames() const { return mTrackNames; }
109
Glenn Kastenc59c0042012-02-02 14:06:11 -0800110 size_t getUnreleasedFrames(int name) const;
Eric Laurent071ccd52011-12-22 16:08:41 -0800111
Mathias Agopian65ab4712010-07-14 17:59:35 -0700112private:
113
114 enum {
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700115 NEEDS_CHANNEL_COUNT__MASK = 0x00000007,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700116 NEEDS_FORMAT__MASK = 0x000000F0,
117 NEEDS_MUTE__MASK = 0x00000100,
118 NEEDS_RESAMPLE__MASK = 0x00001000,
119 NEEDS_AUX__MASK = 0x00010000,
120 };
121
122 enum {
123 NEEDS_CHANNEL_1 = 0x00000000,
124 NEEDS_CHANNEL_2 = 0x00000001,
125
126 NEEDS_FORMAT_16 = 0x00000010,
127
128 NEEDS_MUTE_DISABLED = 0x00000000,
129 NEEDS_MUTE_ENABLED = 0x00000100,
130
131 NEEDS_RESAMPLE_DISABLED = 0x00000000,
132 NEEDS_RESAMPLE_ENABLED = 0x00001000,
133
134 NEEDS_AUX_DISABLED = 0x00000000,
135 NEEDS_AUX_ENABLED = 0x00010000,
136 };
137
Mathias Agopian65ab4712010-07-14 17:59:35 -0700138 struct state_t;
139 struct track_t;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700140 class DownmixerBufferProvider;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700141
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142 typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp, int32_t* aux);
143 static const int BLOCKSIZE = 16; // 4 cache lines
144
145 struct track_t {
146 uint32_t needs;
147
148 union {
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800149 int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point
Mathias Agopian65ab4712010-07-14 17:59:35 -0700150 int32_t volumeRL;
151 };
152
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800153 int32_t prevVolume[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700154
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800155 // 16-byte boundary
156
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800157 int32_t volumeInc[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158 int32_t auxInc;
159 int32_t prevAuxLevel;
160
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800161 // 16-byte boundary
162
163 int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
Mathias Agopian65ab4712010-07-14 17:59:35 -0700164 uint16_t frameCount;
165
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800166 uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
167 uint8_t format; // always 16
168 uint16_t enabled; // actually bool
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700169 audio_channel_mask_t channelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700171 // actual buffer provider used by the track hooks, see DownmixerBufferProvider below
172 // for how the Track buffer provider is wrapped by another one when dowmixing is required
Mathias Agopian65ab4712010-07-14 17:59:35 -0700173 AudioBufferProvider* bufferProvider;
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800174
175 // 16-byte boundary
176
177 mutable AudioBufferProvider::Buffer buffer; // 8 bytes
Mathias Agopian65ab4712010-07-14 17:59:35 -0700178
179 hook_t hook;
Glenn Kasten54c3b662012-01-06 07:46:30 -0800180 const void* in; // current location in buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700181
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800182 // 16-byte boundary
183
Mathias Agopian65ab4712010-07-14 17:59:35 -0700184 AudioResampler* resampler;
185 uint32_t sampleRate;
186 int32_t* mainBuffer;
187 int32_t* auxBuffer;
188
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800189 // 16-byte boundary
190
John Grossman4ff14ba2012-02-08 16:37:41 -0800191 uint64_t localTimeFreq;
192
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700193 DownmixerBufferProvider* downmixerBufferProvider; // 4 bytes
194
195 int32_t padding;
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800196
197 // 16-byte boundary
198
Mathias Agopian65ab4712010-07-14 17:59:35 -0700199 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800200 bool doesResample() const { return resampler != NULL; }
201 void resetResampler() { if (resampler != NULL) resampler->reset(); }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 void adjustVolumeRamp(bool aux);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800203 size_t getUnreleasedFrames() const { return resampler != NULL ?
204 resampler->getUnreleasedFrames() : 0; };
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 };
206
207 // pad to 32-bytes to fill cache line
208 struct state_t {
209 uint32_t enabledTracks;
210 uint32_t needsChanged;
211 size_t frameCount;
Glenn Kastena1117922012-01-26 10:53:32 -0800212 void (*hook)(state_t* state, int64_t pts); // one of process__*, never NULL
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213 int32_t *outputTemp;
214 int32_t *resampleTemp;
215 int32_t reserved[2];
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700216 // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800217 track_t tracks[MAX_NUM_TRACKS]; __attribute__((aligned(32)));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700218 };
219
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700220 // AudioBufferProvider that wraps a track AudioBufferProvider by a call to a downmix effect
221 class DownmixerBufferProvider : public AudioBufferProvider {
222 public:
223 virtual status_t getNextBuffer(Buffer* buffer, int64_t pts);
224 virtual void releaseBuffer(Buffer* buffer);
225 DownmixerBufferProvider();
226 virtual ~DownmixerBufferProvider();
227
228 AudioBufferProvider* mTrackBufferProvider;
229 effect_handle_t mDownmixHandle;
230 effect_config_t mDownmixConfig;
231 };
232
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800233 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234 uint32_t mTrackNames;
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700235
236 // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS,
237 // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS
238 const uint32_t mConfiguredNames;
239
Mathias Agopian65ab4712010-07-14 17:59:35 -0700240 const uint32_t mSampleRate;
241
242 state_t mState __attribute__((aligned(32)));
243
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700244 // effect descriptor for the downmixer used by the mixer
245 static effect_descriptor_t dwnmFxDesc;
246 // indicates whether a downmix effect has been found and is usable by this mixer
247 static bool isMultichannelCapable;
248
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700249 // Call after changing either the enabled status of a track, or parameters of an enabled track.
250 // OK to call more often than that, but unnecessary.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251 void invalidateState(uint32_t mask);
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700252
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700253 static status_t prepareTrackForDownmix(track_t* pTrack, int trackNum);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254
255 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
256 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
257 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
258 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
259 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
260 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
261
John Grossman4ff14ba2012-02-08 16:37:41 -0800262 static void process__validate(state_t* state, int64_t pts);
263 static void process__nop(state_t* state, int64_t pts);
264 static void process__genericNoResampling(state_t* state, int64_t pts);
265 static void process__genericResampling(state_t* state, int64_t pts);
266 static void process__OneTrack16BitsStereoNoResampling(state_t* state,
267 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800268#if 0
John Grossman4ff14ba2012-02-08 16:37:41 -0800269 static void process__TwoTracks16BitsStereoNoResampling(state_t* state,
270 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800271#endif
John Grossman4ff14ba2012-02-08 16:37:41 -0800272
273 static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
274 int outputFrameIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275};
276
277// ----------------------------------------------------------------------------
278}; // namespace android
279
280#endif // ANDROID_AUDIO_MIXER_H