blob: 43fe66463bfc67cb6d0f7593b1e0f27f96450c54 [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 Kastenab7d72f2013-02-27 09:05:28 -080031#include <media/nbaio/NBLog.h>
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070032
Glenn Kastenc56f3422014-03-21 17:53:17 -070033// FIXME This is actually unity gain, which might not be max in future, expressed in U.12
Andy Hung97ae8242014-05-30 10:35:47 -070034#define MAX_GAIN_INT AudioMixer::UNITY_GAIN_INT
Glenn Kastenc56f3422014-03-21 17:53:17 -070035
Mathias Agopian65ab4712010-07-14 17:59:35 -070036namespace android {
37
38// ----------------------------------------------------------------------------
39
Mathias Agopian65ab4712010-07-14 17:59:35 -070040class AudioMixer
41{
42public:
Glenn Kasten5c94b6c2012-03-20 17:01:29 -070043 AudioMixer(size_t frameCount, uint32_t sampleRate,
44 uint32_t maxNumTracks = MAX_NUM_TRACKS);
Mathias Agopian65ab4712010-07-14 17:59:35 -070045
Glenn Kastenc19e2242012-01-30 14:54:39 -080046 /*virtual*/ ~AudioMixer(); // non-virtual saves a v-table, restore if sub-classed
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
Glenn Kasten599fabc2012-03-08 12:33:37 -080048
49 // This mixer has a hard-coded upper limit of 32 active track inputs.
50 // Adding support for > 32 tracks would require more than simply changing this value.
Mathias Agopian65ab4712010-07-14 17:59:35 -070051 static const uint32_t MAX_NUM_TRACKS = 32;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070052 // maximum number of channels supported by the mixer
Glenn Kasten599fabc2012-03-08 12:33:37 -080053
54 // This mixer has a hard-coded upper limit of 2 channels for output.
55 // There is support for > 2 channel tracks down-mixed to 2 channel output via a down-mix effect.
56 // Adding support for > 2 channel output would require more than simply changing this value.
Mathias Agopian65ab4712010-07-14 17:59:35 -070057 static const uint32_t MAX_NUM_CHANNELS = 2;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070058 // maximum number of channels supported for the content
59 static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = 8;
Mathias Agopian65ab4712010-07-14 17:59:35 -070060
Andy Hung97ae8242014-05-30 10:35:47 -070061 static const uint16_t UNITY_GAIN_INT = 0x1000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070062
63 enum { // names
64
Glenn Kasten9c56d4a2011-12-19 15:06:39 -080065 // track names (MAX_NUM_TRACKS units)
Mathias Agopian65ab4712010-07-14 17:59:35 -070066 TRACK0 = 0x1000,
67
Glenn Kasten1c48c3c2011-12-15 14:54:01 -080068 // 0x2000 is unused
Mathias Agopian65ab4712010-07-14 17:59:35 -070069
70 // setParameter targets
71 TRACK = 0x3000,
72 RESAMPLE = 0x3001,
73 RAMP_VOLUME = 0x3002, // ramp to new volume
74 VOLUME = 0x3003, // don't ramp
75
76 // set Parameter names
77 // for target TRACK
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070078 CHANNEL_MASK = 0x4000,
Mathias Agopian65ab4712010-07-14 17:59:35 -070079 FORMAT = 0x4001,
80 MAIN_BUFFER = 0x4002,
81 AUX_BUFFER = 0x4003,
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070082 DOWNMIX_TYPE = 0X4004,
Andy Hung78820702014-02-28 16:23:02 -080083 MIXER_FORMAT = 0x4005, // AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
Glenn Kasten362c4e62011-12-14 10:28:06 -080084 // for target RESAMPLE
Glenn Kasten4e2293f2012-04-12 09:39:07 -070085 SAMPLE_RATE = 0x4100, // Configure sample rate conversion on this track name;
86 // parameter 'value' is the new sample rate in Hz.
87 // Only creates a sample rate converter the first time that
88 // the track sample rate is different from the mix sample rate.
89 // If the new sample rate is the same as the mix sample rate,
90 // and a sample rate converter already exists,
91 // then the sample rate converter remains present but is a no-op.
92 RESET = 0x4101, // Reset sample rate converter without changing sample rate.
93 // This clears out the resampler's input buffer.
94 REMOVE = 0x4102, // Remove the sample rate converter on this track name;
95 // the track is restored to the mix sample rate.
Glenn Kasten362c4e62011-12-14 10:28:06 -080096 // for target RAMP_VOLUME and VOLUME (8 channels max)
Glenn Kastenc56f3422014-03-21 17:53:17 -070097 // FIXME use float for these 3 to improve the dynamic range
Mathias Agopian65ab4712010-07-14 17:59:35 -070098 VOLUME0 = 0x4200,
99 VOLUME1 = 0x4201,
100 AUXLEVEL = 0x4210,
101 };
102
103
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800104 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
Glenn Kasten17a736c2012-02-14 08:52:15 -0800105
106 // Allocate a track name. Returns new track name if successful, -1 on failure.
Andy Hunge8a1ced2014-05-09 15:02:21 -0700107 // The failure could be because of an invalid channelMask or format, or that
108 // the track capacity of the mixer is exceeded.
109 int getTrackName(audio_channel_mask_t channelMask,
110 audio_format_t format, int sessionId);
Glenn Kasten17a736c2012-02-14 08:52:15 -0800111
112 // Free an allocated track by name
Mathias Agopian65ab4712010-07-14 17:59:35 -0700113 void deleteTrackName(int name);
114
Glenn Kasten17a736c2012-02-14 08:52:15 -0800115 // Enable or disable an allocated track by name
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800116 void enable(int name);
117 void disable(int name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700118
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800119 void setParameter(int name, int target, int param, void *value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700120
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800121 void setBufferProvider(int name, AudioBufferProvider* bufferProvider);
John Grossman4ff14ba2012-02-08 16:37:41 -0800122 void process(int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700123
124 uint32_t trackNames() const { return mTrackNames; }
125
Glenn Kastenc59c0042012-02-02 14:06:11 -0800126 size_t getUnreleasedFrames(int name) const;
Eric Laurent071ccd52011-12-22 16:08:41 -0800127
Andy Hunge8a1ced2014-05-09 15:02:21 -0700128 static inline bool isValidPcmTrackFormat(audio_format_t format) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700129 return format == AUDIO_FORMAT_PCM_16_BIT ||
130 format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
131 format == AUDIO_FORMAT_PCM_32_BIT ||
132 format == AUDIO_FORMAT_PCM_FLOAT;
Andy Hunge8a1ced2014-05-09 15:02:21 -0700133 }
134
Mathias Agopian65ab4712010-07-14 17:59:35 -0700135private:
136
137 enum {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700138 // FIXME this representation permits up to 8 channels
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700139 NEEDS_CHANNEL_COUNT__MASK = 0x00000007,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700140 };
141
142 enum {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700143 NEEDS_CHANNEL_1 = 0x00000000, // mono
144 NEEDS_CHANNEL_2 = 0x00000001, // stereo
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145
Glenn Kastend6fadf02013-10-30 14:37:29 -0700146 // sample format is not explicitly specified, and is assumed to be AUDIO_FORMAT_PCM_16_BIT
Mathias Agopian65ab4712010-07-14 17:59:35 -0700147
Glenn Kastend6fadf02013-10-30 14:37:29 -0700148 NEEDS_MUTE = 0x00000100,
149 NEEDS_RESAMPLE = 0x00001000,
150 NEEDS_AUX = 0x00010000,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151 };
152
Mathias Agopian65ab4712010-07-14 17:59:35 -0700153 struct state_t;
154 struct track_t;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700155 class DownmixerBufferProvider;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700156 class ReformatBufferProvider;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700157
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700158 typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp,
159 int32_t* aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 static const int BLOCKSIZE = 16; // 4 cache lines
161
162 struct track_t {
163 uint32_t needs;
164
165 union {
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800166 int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167 int32_t volumeRL;
168 };
169
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800170 int32_t prevVolume[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700171
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800172 // 16-byte boundary
173
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800174 int32_t volumeInc[MAX_NUM_CHANNELS];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175 int32_t auxInc;
176 int32_t prevAuxLevel;
177
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800178 // 16-byte boundary
179
180 int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
Mathias Agopian65ab4712010-07-14 17:59:35 -0700181 uint16_t frameCount;
182
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800183 uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
Andy Hungef7c7fb2014-05-12 16:51:41 -0700184 uint8_t unused_padding; // formerly format, was always 16
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800185 uint16_t enabled; // actually bool
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700186 audio_channel_mask_t channelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700187
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700188 // actual buffer provider used by the track hooks, see DownmixerBufferProvider below
189 // for how the Track buffer provider is wrapped by another one when dowmixing is required
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190 AudioBufferProvider* bufferProvider;
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800191
192 // 16-byte boundary
193
194 mutable AudioBufferProvider::Buffer buffer; // 8 bytes
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195
196 hook_t hook;
Glenn Kasten54c3b662012-01-06 07:46:30 -0800197 const void* in; // current location in buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700198
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800199 // 16-byte boundary
200
Mathias Agopian65ab4712010-07-14 17:59:35 -0700201 AudioResampler* resampler;
202 uint32_t sampleRate;
203 int32_t* mainBuffer;
204 int32_t* auxBuffer;
205
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800206 // 16-byte boundary
Andy Hungef7c7fb2014-05-12 16:51:41 -0700207 AudioBufferProvider* mInputBufferProvider; // 4 bytes
208 ReformatBufferProvider* mReformatBufferProvider; // 4 bytes
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700209 DownmixerBufferProvider* downmixerBufferProvider; // 4 bytes
210
Jean-Michel Trivid06e1322012-09-12 15:47:07 -0700211 int32_t sessionId;
212
Andy Hungef7c7fb2014-05-12 16:51:41 -0700213 // 16-byte boundary
Andy Hunge8a1ced2014-05-09 15:02:21 -0700214 audio_format_t mMixerFormat; // output mix format: AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
215 audio_format_t mFormat; // input track format
Andy Hungef7c7fb2014-05-12 16:51:41 -0700216 audio_format_t mMixerInFormat; // mix internal format AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
217 // each track must be converted to this format.
218
219 int32_t mUnused[1]; // alignment padding
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800220
221 // 16-byte boundary
222
Mathias Agopian65ab4712010-07-14 17:59:35 -0700223 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800224 bool doesResample() const { return resampler != NULL; }
225 void resetResampler() { if (resampler != NULL) resampler->reset(); }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226 void adjustVolumeRamp(bool aux);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800227 size_t getUnreleasedFrames() const { return resampler != NULL ?
228 resampler->getUnreleasedFrames() : 0; };
Mathias Agopian65ab4712010-07-14 17:59:35 -0700229 };
230
231 // pad to 32-bytes to fill cache line
232 struct state_t {
233 uint32_t enabledTracks;
234 uint32_t needsChanged;
235 size_t frameCount;
Glenn Kastena1117922012-01-26 10:53:32 -0800236 void (*hook)(state_t* state, int64_t pts); // one of process__*, never NULL
Mathias Agopian65ab4712010-07-14 17:59:35 -0700237 int32_t *outputTemp;
238 int32_t *resampleTemp;
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800239 NBLog::Writer* mLog;
240 int32_t reserved[1];
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700241 // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800242 track_t tracks[MAX_NUM_TRACKS] __attribute__((aligned(32)));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243 };
244
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700245 // AudioBufferProvider that wraps a track AudioBufferProvider by a call to a downmix effect
246 class DownmixerBufferProvider : public AudioBufferProvider {
247 public:
248 virtual status_t getNextBuffer(Buffer* buffer, int64_t pts);
249 virtual void releaseBuffer(Buffer* buffer);
250 DownmixerBufferProvider();
251 virtual ~DownmixerBufferProvider();
252
253 AudioBufferProvider* mTrackBufferProvider;
254 effect_handle_t mDownmixHandle;
255 effect_config_t mDownmixConfig;
256 };
257
Andy Hungef7c7fb2014-05-12 16:51:41 -0700258 // AudioBufferProvider wrapper that reformats track to acceptable mixer input type
259 class ReformatBufferProvider : public AudioBufferProvider {
260 public:
261 ReformatBufferProvider(int32_t channels,
262 audio_format_t inputFormat, audio_format_t outputFormat);
263 virtual ~ReformatBufferProvider();
264
265 // overrides AudioBufferProvider methods
266 virtual status_t getNextBuffer(Buffer* buffer, int64_t pts);
267 virtual void releaseBuffer(Buffer* buffer);
268
269 void reset();
270 inline bool requiresInternalBuffers() {
271 return true; //mInputFrameSize < mOutputFrameSize;
272 }
273
274 AudioBufferProvider* mTrackBufferProvider;
275 int32_t mChannels;
276 audio_format_t mInputFormat;
277 audio_format_t mOutputFormat;
278 size_t mInputFrameSize;
279 size_t mOutputFrameSize;
280 // (only) required for reformatting to a larger size.
281 AudioBufferProvider::Buffer mBuffer;
282 void* mOutputData;
283 size_t mOutputCount;
284 size_t mConsumed;
285 };
286
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800287 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700288 uint32_t mTrackNames;
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700289
290 // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS,
291 // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS
292 const uint32_t mConfiguredNames;
293
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294 const uint32_t mSampleRate;
295
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800296 NBLog::Writer mDummyLog;
297public:
298 void setLog(NBLog::Writer* log);
299private:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300 state_t mState __attribute__((aligned(32)));
301
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700302 // effect descriptor for the downmixer used by the mixer
Glenn Kasten49c34ac2013-10-30 14:37:01 -0700303 static effect_descriptor_t sDwnmFxDesc;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700304 // indicates whether a downmix effect has been found and is usable by this mixer
Glenn Kasten49c34ac2013-10-30 14:37:01 -0700305 static bool sIsMultichannelCapable;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700306
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700307 // Call after changing either the enabled status of a track, or parameters of an enabled track.
308 // OK to call more often than that, but unnecessary.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309 void invalidateState(uint32_t mask);
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700310
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700311 static status_t initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700312 static status_t prepareTrackForDownmix(track_t* pTrack, int trackNum);
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700313 static void unprepareTrackForDownmix(track_t* pTrack, int trackName);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700314 static status_t prepareTrackForReformat(track_t* pTrack, int trackNum);
315 static void unprepareTrackForReformat(track_t* pTrack, int trackName);
316 static void reconfigureBufferProviders(track_t* pTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700318 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
319 int32_t* aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 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 -0700321 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
322 int32_t* aux);
323 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
324 int32_t* aux);
325 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
326 int32_t* aux);
327 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
328 int32_t* aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329
John Grossman4ff14ba2012-02-08 16:37:41 -0800330 static void process__validate(state_t* state, int64_t pts);
331 static void process__nop(state_t* state, int64_t pts);
332 static void process__genericNoResampling(state_t* state, int64_t pts);
333 static void process__genericResampling(state_t* state, int64_t pts);
334 static void process__OneTrack16BitsStereoNoResampling(state_t* state,
335 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800336#if 0
John Grossman4ff14ba2012-02-08 16:37:41 -0800337 static void process__TwoTracks16BitsStereoNoResampling(state_t* state,
338 int64_t pts);
Glenn Kasten81a028f2011-12-15 09:53:12 -0800339#endif
John Grossman4ff14ba2012-02-08 16:37:41 -0800340
341 static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
342 int outputFrameIndex);
Glenn Kasten52008f82012-03-18 09:34:41 -0700343
344 static uint64_t sLocalTimeFreq;
345 static pthread_once_t sOnceControl;
346 static void sInitRoutine();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347};
348
349// ----------------------------------------------------------------------------
350}; // namespace android
351
352#endif // ANDROID_AUDIO_MIXER_H