blob: 88e94c5e1bec72f2a426c3cbe71db89384f0b4c5 [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
Andy Hung9a592762014-07-21 21:56:01 -070029#include <hardware/audio_effect.h>
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070030#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
Andy Hunge93b6b72014-07-17 21:30:53 -070054 // This mixer has a hard-coded upper limit of 8 channels for output.
55 static const uint32_t MAX_NUM_CHANNELS = 8;
56 static const uint32_t MAX_NUM_VOLUMES = 2; // stereo volume only
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070057 // maximum number of channels supported for the content
Andy Hunge93b6b72014-07-17 21:30:53 -070058 static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = AUDIO_CHANNEL_COUNT_MAX;
Mathias Agopian65ab4712010-07-14 17:59:35 -070059
Andy Hung97ae8242014-05-30 10:35:47 -070060 static const uint16_t UNITY_GAIN_INT = 0x1000;
Andy Hung6be49402014-05-30 10:42:03 -070061 static const float UNITY_GAIN_FLOAT = 1.0f;
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)
Andy Hunge93b6b72014-07-17 21:30:53 -070084 MIXER_CHANNEL_MASK = 0x4006, // Channel mask for mixer output
Glenn Kasten362c4e62011-12-14 10:28:06 -080085 // for target RESAMPLE
Glenn Kasten4e2293f2012-04-12 09:39:07 -070086 SAMPLE_RATE = 0x4100, // Configure sample rate conversion on this track name;
87 // parameter 'value' is the new sample rate in Hz.
88 // Only creates a sample rate converter the first time that
89 // the track sample rate is different from the mix sample rate.
90 // If the new sample rate is the same as the mix sample rate,
91 // and a sample rate converter already exists,
92 // then the sample rate converter remains present but is a no-op.
93 RESET = 0x4101, // Reset sample rate converter without changing sample rate.
94 // This clears out the resampler's input buffer.
95 REMOVE = 0x4102, // Remove the sample rate converter on this track name;
96 // the track is restored to the mix sample rate.
Glenn Kasten362c4e62011-12-14 10:28:06 -080097 // for target RAMP_VOLUME and VOLUME (8 channels max)
Glenn Kastenc56f3422014-03-21 17:53:17 -070098 // FIXME use float for these 3 to improve the dynamic range
Mathias Agopian65ab4712010-07-14 17:59:35 -070099 VOLUME0 = 0x4200,
100 VOLUME1 = 0x4201,
101 AUXLEVEL = 0x4210,
102 };
103
104
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800105 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
Glenn Kasten17a736c2012-02-14 08:52:15 -0800106
107 // Allocate a track name. Returns new track name if successful, -1 on failure.
Andy Hunge8a1ced2014-05-09 15:02:21 -0700108 // The failure could be because of an invalid channelMask or format, or that
109 // the track capacity of the mixer is exceeded.
110 int getTrackName(audio_channel_mask_t channelMask,
111 audio_format_t format, int sessionId);
Glenn Kasten17a736c2012-02-14 08:52:15 -0800112
113 // Free an allocated track by name
Mathias Agopian65ab4712010-07-14 17:59:35 -0700114 void deleteTrackName(int name);
115
Glenn Kasten17a736c2012-02-14 08:52:15 -0800116 // Enable or disable an allocated track by name
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800117 void enable(int name);
118 void disable(int name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700119
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800120 void setParameter(int name, int target, int param, void *value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700121
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800122 void setBufferProvider(int name, AudioBufferProvider* bufferProvider);
John Grossman4ff14ba2012-02-08 16:37:41 -0800123 void process(int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700124
125 uint32_t trackNames() const { return mTrackNames; }
126
Glenn Kastenc59c0042012-02-02 14:06:11 -0800127 size_t getUnreleasedFrames(int name) const;
Eric Laurent071ccd52011-12-22 16:08:41 -0800128
Andy Hunge8a1ced2014-05-09 15:02:21 -0700129 static inline bool isValidPcmTrackFormat(audio_format_t format) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700130 return format == AUDIO_FORMAT_PCM_16_BIT ||
131 format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
132 format == AUDIO_FORMAT_PCM_32_BIT ||
133 format == AUDIO_FORMAT_PCM_FLOAT;
Andy Hunge8a1ced2014-05-09 15:02:21 -0700134 }
135
Mathias Agopian65ab4712010-07-14 17:59:35 -0700136private:
137
138 enum {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700139 // FIXME this representation permits up to 8 channels
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700140 NEEDS_CHANNEL_COUNT__MASK = 0x00000007,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700141 };
142
143 enum {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700144 NEEDS_CHANNEL_1 = 0x00000000, // mono
145 NEEDS_CHANNEL_2 = 0x00000001, // stereo
Mathias Agopian65ab4712010-07-14 17:59:35 -0700146
Glenn Kastend6fadf02013-10-30 14:37:29 -0700147 // sample format is not explicitly specified, and is assumed to be AUDIO_FORMAT_PCM_16_BIT
Mathias Agopian65ab4712010-07-14 17:59:35 -0700148
Glenn Kastend6fadf02013-10-30 14:37:29 -0700149 NEEDS_MUTE = 0x00000100,
150 NEEDS_RESAMPLE = 0x00001000,
151 NEEDS_AUX = 0x00010000,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152 };
153
Mathias Agopian65ab4712010-07-14 17:59:35 -0700154 struct state_t;
155 struct track_t;
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700156 class CopyBufferProvider;
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
Andy Hung5e58b0a2014-06-23 19:07:29 -0700165 // TODO: Eventually remove legacy integer volume settings
Mathias Agopian65ab4712010-07-14 17:59:35 -0700166 union {
Andy Hunge93b6b72014-07-17 21:30:53 -0700167 int16_t volume[MAX_NUM_VOLUMES]; // U4.12 fixed point (top bit should be zero)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168 int32_t volumeRL;
169 };
170
Andy Hunge93b6b72014-07-17 21:30:53 -0700171 int32_t prevVolume[MAX_NUM_VOLUMES];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700172
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800173 // 16-byte boundary
174
Andy Hunge93b6b72014-07-17 21:30:53 -0700175 int32_t volumeInc[MAX_NUM_VOLUMES];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700176 int32_t auxInc;
177 int32_t prevAuxLevel;
178
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800179 // 16-byte boundary
180
181 int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
Mathias Agopian65ab4712010-07-14 17:59:35 -0700182 uint16_t frameCount;
183
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800184 uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
Andy Hungef7c7fb2014-05-12 16:51:41 -0700185 uint8_t unused_padding; // formerly format, was always 16
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800186 uint16_t enabled; // actually bool
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700187 audio_channel_mask_t channelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700188
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700189 // actual buffer provider used by the track hooks, see DownmixerBufferProvider below
190 // for how the Track buffer provider is wrapped by another one when dowmixing is required
Mathias Agopian65ab4712010-07-14 17:59:35 -0700191 AudioBufferProvider* bufferProvider;
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800192
193 // 16-byte boundary
194
195 mutable AudioBufferProvider::Buffer buffer; // 8 bytes
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196
197 hook_t hook;
Glenn Kasten54c3b662012-01-06 07:46:30 -0800198 const void* in; // current location in buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -0700199
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800200 // 16-byte boundary
201
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 AudioResampler* resampler;
203 uint32_t sampleRate;
204 int32_t* mainBuffer;
205 int32_t* auxBuffer;
206
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800207 // 16-byte boundary
Andy Hung7f475492014-08-25 16:36:37 -0700208
209 /* Buffer providers are constructed to translate the track input data as needed.
210 *
211 * 1) mInputBufferProvider: The AudioTrack buffer provider.
212 * 2) mReformatBufferProvider: If not NULL, performs the audio reformat to
213 * match either mMixerInFormat or mDownmixRequiresFormat, if the downmixer
214 * requires reformat. For example, it may convert floating point input to
215 * PCM_16_bit if that's required by the downmixer.
216 * 3) downmixerBufferProvider: If not NULL, performs the channel remixing to match
217 * the number of channels required by the mixer sink.
218 * 4) mPostDownmixReformatBufferProvider: If not NULL, performs reformatting from
219 * the downmixer requirements to the mixer engine input requirements.
220 */
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700221 AudioBufferProvider* mInputBufferProvider; // externally provided buffer provider.
222 CopyBufferProvider* mReformatBufferProvider; // provider wrapper for reformatting.
Andy Hung34803d52014-07-16 21:41:35 -0700223 CopyBufferProvider* downmixerBufferProvider; // wrapper for channel conversion.
Andy Hung7f475492014-08-25 16:36:37 -0700224 CopyBufferProvider* mPostDownmixReformatBufferProvider;
Jean-Michel Trivid06e1322012-09-12 15:47:07 -0700225
Andy Hungef7c7fb2014-05-12 16:51:41 -0700226 // 16-byte boundary
Andy Hung7f475492014-08-25 16:36:37 -0700227 int32_t sessionId;
228
Andy Hunge8a1ced2014-05-09 15:02:21 -0700229 audio_format_t mMixerFormat; // output mix format: AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
230 audio_format_t mFormat; // input track format
Andy Hungef7c7fb2014-05-12 16:51:41 -0700231 audio_format_t mMixerInFormat; // mix internal format AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
232 // each track must be converted to this format.
Andy Hung7f475492014-08-25 16:36:37 -0700233 audio_format_t mDownmixRequiresFormat; // required downmixer format
234 // AUDIO_FORMAT_PCM_16_BIT if 16 bit necessary
235 // AUDIO_FORMAT_INVALID if no required format
Andy Hungef7c7fb2014-05-12 16:51:41 -0700236
Andy Hunge93b6b72014-07-17 21:30:53 -0700237 float mVolume[MAX_NUM_VOLUMES]; // floating point set volume
238 float mPrevVolume[MAX_NUM_VOLUMES]; // floating point previous volume
239 float mVolumeInc[MAX_NUM_VOLUMES]; // floating point volume increment
Andy Hung5e58b0a2014-06-23 19:07:29 -0700240
241 float mAuxLevel; // floating point set aux level
242 float mPrevAuxLevel; // floating point prev aux level
243 float mAuxInc; // floating point aux increment
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800244
Andy Hunge93b6b72014-07-17 21:30:53 -0700245 audio_channel_mask_t mMixerChannelMask;
246 uint32_t mMixerChannelCount;
Glenn Kasten3b81aca2012-01-27 15:26:23 -0800247
Andy Hung296b7412014-06-17 15:25:47 -0700248 bool needsRamp() { return (volumeInc[0] | volumeInc[1] | auxInc) != 0; }
Andy Hunge93b6b72014-07-17 21:30:53 -0700249 bool setResampler(uint32_t trackSampleRate, uint32_t devSampleRate);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800250 bool doesResample() const { return resampler != NULL; }
251 void resetResampler() { if (resampler != NULL) resampler->reset(); }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700252 void adjustVolumeRamp(bool aux, bool useFloat = false);
Glenn Kastenc59c0042012-02-02 14:06:11 -0800253 size_t getUnreleasedFrames() const { return resampler != NULL ?
254 resampler->getUnreleasedFrames() : 0; };
Andy Hung0f451e92014-08-04 21:28:47 -0700255
256 status_t prepareForDownmix();
257 void unprepareForDownmix();
258 status_t prepareForReformat();
259 void unprepareForReformat();
260 void reconfigureBufferProviders();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700261 };
262
Andy Hung296b7412014-06-17 15:25:47 -0700263 typedef void (*process_hook_t)(state_t* state, int64_t pts);
264
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265 // pad to 32-bytes to fill cache line
266 struct state_t {
267 uint32_t enabledTracks;
268 uint32_t needsChanged;
269 size_t frameCount;
Andy Hung296b7412014-06-17 15:25:47 -0700270 process_hook_t hook; // one of process__*, never NULL
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271 int32_t *outputTemp;
272 int32_t *resampleTemp;
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800273 NBLog::Writer* mLog;
274 int32_t reserved[1];
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700275 // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800276 track_t tracks[MAX_NUM_TRACKS] __attribute__((aligned(32)));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277 };
278
Andy Hunga08810b2014-07-16 21:53:43 -0700279 // Base AudioBufferProvider class used for DownMixerBufferProvider, RemixBufferProvider,
280 // and ReformatBufferProvider.
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700281 // It handles a private buffer for use in converting format or channel masks from the
282 // input data to a form acceptable by the mixer.
283 // TODO: Make a ResamplerBufferProvider when integers are entirely removed from the
284 // processing pipeline.
285 class CopyBufferProvider : public AudioBufferProvider {
286 public:
287 // Use a private buffer of bufferFrameCount frames (each frame is outputFrameSize bytes).
288 // If bufferFrameCount is 0, no private buffer is created and in-place modification of
289 // the upstream buffer provider's buffers is performed by copyFrames().
290 CopyBufferProvider(size_t inputFrameSize, size_t outputFrameSize,
291 size_t bufferFrameCount);
292 virtual ~CopyBufferProvider();
293
294 // Overrides AudioBufferProvider methods
295 virtual status_t getNextBuffer(Buffer* buffer, int64_t pts);
296 virtual void releaseBuffer(Buffer* buffer);
297
298 // Other public methods
299
300 // call this to release the buffer to the upstream provider.
301 // treat it as an audio discontinuity for future samples.
302 virtual void reset();
303
304 // this function should be supplied by the derived class. It converts
305 // #frames in the *src pointer to the *dst pointer. It is public because
306 // some providers will allow this to work on arbitrary buffers outside
307 // of the internal buffers.
308 virtual void copyFrames(void *dst, const void *src, size_t frames) = 0;
309
310 // set the upstream buffer provider. Consider calling "reset" before this function.
311 void setBufferProvider(AudioBufferProvider *p) {
312 mTrackBufferProvider = p;
313 }
314
315 protected:
316 AudioBufferProvider* mTrackBufferProvider;
317 const size_t mInputFrameSize;
318 const size_t mOutputFrameSize;
319 private:
320 AudioBufferProvider::Buffer mBuffer;
321 const size_t mLocalBufferFrameCount;
322 void* mLocalBufferData;
323 size_t mConsumed;
324 };
325
Andy Hung34803d52014-07-16 21:41:35 -0700326 // DownmixerBufferProvider wraps a track AudioBufferProvider to provide
327 // position dependent downmixing by an Audio Effect.
328 class DownmixerBufferProvider : public CopyBufferProvider {
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700329 public:
Andy Hung34803d52014-07-16 21:41:35 -0700330 DownmixerBufferProvider(audio_channel_mask_t inputChannelMask,
331 audio_channel_mask_t outputChannelMask, audio_format_t format,
332 uint32_t sampleRate, int32_t sessionId, size_t bufferFrameCount);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700333 virtual ~DownmixerBufferProvider();
Andy Hung34803d52014-07-16 21:41:35 -0700334 virtual void copyFrames(void *dst, const void *src, size_t frames);
335 bool isValid() const { return mDownmixHandle != NULL; }
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700336
Andy Hung34803d52014-07-16 21:41:35 -0700337 static status_t init();
338 static bool isMultichannelCapable() { return sIsMultichannelCapable; }
339
340 protected:
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700341 effect_handle_t mDownmixHandle;
342 effect_config_t mDownmixConfig;
Andy Hung34803d52014-07-16 21:41:35 -0700343
344 // effect descriptor for the downmixer used by the mixer
345 static effect_descriptor_t sDwnmFxDesc;
346 // indicates whether a downmix effect has been found and is usable by this mixer
347 static bool sIsMultichannelCapable;
348 // FIXME: should we allow effects outside of the framework?
349 // We need to here. A special ioId that must be <= -2 so it does not map to a session.
350 static const int32_t SESSION_ID_INVALID_AND_IGNORED = -2;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700351 };
352
Andy Hunga08810b2014-07-16 21:53:43 -0700353 // RemixBufferProvider wraps a track AudioBufferProvider to perform an
354 // upmix or downmix to the proper channel count and mask.
355 class RemixBufferProvider : public CopyBufferProvider {
356 public:
357 RemixBufferProvider(audio_channel_mask_t inputChannelMask,
358 audio_channel_mask_t outputChannelMask, audio_format_t format,
359 size_t bufferFrameCount);
360 virtual void copyFrames(void *dst, const void *src, size_t frames);
361
362 protected:
363 const audio_format_t mFormat;
364 const size_t mSampleSize;
365 const size_t mInputChannels;
366 const size_t mOutputChannels;
367 int8_t mIdxAry[sizeof(uint32_t)*8]; // 32 bits => channel indices
368 };
369
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700370 // ReformatBufferProvider wraps a track AudioBufferProvider to convert the input data
371 // to an acceptable mixer input format type.
372 class ReformatBufferProvider : public CopyBufferProvider {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700373 public:
374 ReformatBufferProvider(int32_t channels,
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700375 audio_format_t inputFormat, audio_format_t outputFormat,
376 size_t bufferFrameCount);
377 virtual void copyFrames(void *dst, const void *src, size_t frames);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700378
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700379 protected:
380 const int32_t mChannels;
381 const audio_format_t mInputFormat;
382 const audio_format_t mOutputFormat;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700383 };
384
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800385 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700386 uint32_t mTrackNames;
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700387
388 // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS,
389 // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS
390 const uint32_t mConfiguredNames;
391
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392 const uint32_t mSampleRate;
393
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800394 NBLog::Writer mDummyLog;
395public:
396 void setLog(NBLog::Writer* log);
397private:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700398 state_t mState __attribute__((aligned(32)));
399
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700400 // Call after changing either the enabled status of a track, or parameters of an enabled track.
401 // OK to call more often than that, but unnecessary.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402 void invalidateState(uint32_t mask);
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700403
Andy Hunge93b6b72014-07-17 21:30:53 -0700404 bool setChannelMasks(int name,
405 audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask);
406
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700407 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
408 int32_t* aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 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 -0700410 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
411 int32_t* aux);
412 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
413 int32_t* aux);
414 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
415 int32_t* aux);
416 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
417 int32_t* aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418
John Grossman4ff14ba2012-02-08 16:37:41 -0800419 static void process__validate(state_t* state, int64_t pts);
420 static void process__nop(state_t* state, int64_t pts);
421 static void process__genericNoResampling(state_t* state, int64_t pts);
422 static void process__genericResampling(state_t* state, int64_t pts);
423 static void process__OneTrack16BitsStereoNoResampling(state_t* state,
424 int64_t pts);
John Grossman4ff14ba2012-02-08 16:37:41 -0800425
426 static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
427 int outputFrameIndex);
Glenn Kasten52008f82012-03-18 09:34:41 -0700428
429 static uint64_t sLocalTimeFreq;
430 static pthread_once_t sOnceControl;
431 static void sInitRoutine();
Andy Hung296b7412014-06-17 15:25:47 -0700432
Andy Hung5e58b0a2014-06-23 19:07:29 -0700433 /* multi-format volume mixing function (calls template functions
434 * in AudioMixerOps.h). The template parameters are as follows:
435 *
436 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
Andy Hung5e58b0a2014-06-23 19:07:29 -0700437 * USEFLOATVOL (set to true if float volume is used)
438 * ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards)
439 * TO: int32_t (Q4.27) or float
440 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
441 * TA: int32_t (Q4.27)
442 */
Andy Hunge93b6b72014-07-17 21:30:53 -0700443 template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
Andy Hung5e58b0a2014-06-23 19:07:29 -0700444 typename TO, typename TI, typename TA>
445 static void volumeMix(TO *out, size_t outFrames,
446 const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t);
447
Andy Hung296b7412014-06-17 15:25:47 -0700448 // multi-format process hooks
Andy Hunge93b6b72014-07-17 21:30:53 -0700449 template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung296b7412014-06-17 15:25:47 -0700450 static void process_NoResampleOneTrack(state_t* state, int64_t pts);
451
452 // multi-format track hooks
Andy Hunge93b6b72014-07-17 21:30:53 -0700453 template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung296b7412014-06-17 15:25:47 -0700454 static void track__Resample(track_t* t, TO* out, size_t frameCount,
455 TO* temp __unused, TA* aux);
Andy Hunge93b6b72014-07-17 21:30:53 -0700456 template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung296b7412014-06-17 15:25:47 -0700457 static void track__NoResample(track_t* t, TO* out, size_t frameCount,
458 TO* temp __unused, TA* aux);
459
460 static void convertMixerFormat(void *out, audio_format_t mixerOutFormat,
461 void *in, audio_format_t mixerInFormat, size_t sampleCount);
462
463 // hook types
464 enum {
465 PROCESSTYPE_NORESAMPLEONETRACK,
466 };
467 enum {
468 TRACKTYPE_NOP,
469 TRACKTYPE_RESAMPLE,
470 TRACKTYPE_NORESAMPLE,
471 TRACKTYPE_NORESAMPLEMONO,
472 };
473
474 // functions for determining the proper process and track hooks.
Andy Hunge93b6b72014-07-17 21:30:53 -0700475 static process_hook_t getProcessHook(int processType, uint32_t channelCount,
Andy Hung296b7412014-06-17 15:25:47 -0700476 audio_format_t mixerInFormat, audio_format_t mixerOutFormat);
Andy Hunge93b6b72014-07-17 21:30:53 -0700477 static hook_t getTrackHook(int trackType, uint32_t channelCount,
Andy Hung296b7412014-06-17 15:25:47 -0700478 audio_format_t mixerInFormat, audio_format_t mixerOutFormat);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700479};
480
481// ----------------------------------------------------------------------------
482}; // namespace android
483
484#endif // ANDROID_AUDIO_MIXER_H