AudioMixer.h revision 296b741e8eb38e749e3202182f703a2e30ee5f1f
1/* 2** 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 24#include <utils/threads.h> 25 26#include <media/AudioBufferProvider.h> 27#include "AudioResampler.h" 28 29#include <audio_effects/effect_downmix.h> 30#include <system/audio.h> 31#include <media/nbaio/NBLog.h> 32 33// FIXME This is actually unity gain, which might not be max in future, expressed in U.12 34#define MAX_GAIN_INT AudioMixer::UNITY_GAIN_INT 35 36namespace android { 37 38// ---------------------------------------------------------------------------- 39 40class AudioMixer 41{ 42public: 43 AudioMixer(size_t frameCount, uint32_t sampleRate, 44 uint32_t maxNumTracks = MAX_NUM_TRACKS); 45 46 /*virtual*/ ~AudioMixer(); // non-virtual saves a v-table, restore if sub-classed 47 48 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. 51 static const uint32_t MAX_NUM_TRACKS = 32; 52 // maximum number of channels supported by the mixer 53 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. 57 static const uint32_t MAX_NUM_CHANNELS = 2; 58 // maximum number of channels supported for the content 59 static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = 8; 60 61 static const uint16_t UNITY_GAIN_INT = 0x1000; 62 static const float UNITY_GAIN_FLOAT = 1.0f; 63 64 enum { // names 65 66 // track names (MAX_NUM_TRACKS units) 67 TRACK0 = 0x1000, 68 69 // 0x2000 is unused 70 71 // setParameter targets 72 TRACK = 0x3000, 73 RESAMPLE = 0x3001, 74 RAMP_VOLUME = 0x3002, // ramp to new volume 75 VOLUME = 0x3003, // don't ramp 76 77 // set Parameter names 78 // for target TRACK 79 CHANNEL_MASK = 0x4000, 80 FORMAT = 0x4001, 81 MAIN_BUFFER = 0x4002, 82 AUX_BUFFER = 0x4003, 83 DOWNMIX_TYPE = 0X4004, 84 MIXER_FORMAT = 0x4005, // AUDIO_FORMAT_PCM_(FLOAT|16_BIT) 85 // for target RESAMPLE 86 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. 97 // for target RAMP_VOLUME and VOLUME (8 channels max) 98 // FIXME use float for these 3 to improve the dynamic range 99 VOLUME0 = 0x4200, 100 VOLUME1 = 0x4201, 101 AUXLEVEL = 0x4210, 102 }; 103 104 105 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS 106 107 // Allocate a track name. Returns new track name if successful, -1 on failure. 108 // 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); 112 113 // Free an allocated track by name 114 void deleteTrackName(int name); 115 116 // Enable or disable an allocated track by name 117 void enable(int name); 118 void disable(int name); 119 120 void setParameter(int name, int target, int param, void *value); 121 122 void setBufferProvider(int name, AudioBufferProvider* bufferProvider); 123 void process(int64_t pts); 124 125 uint32_t trackNames() const { return mTrackNames; } 126 127 size_t getUnreleasedFrames(int name) const; 128 129 static inline bool isValidPcmTrackFormat(audio_format_t format) { 130 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; 134 } 135 136private: 137 138 enum { 139 // FIXME this representation permits up to 8 channels 140 NEEDS_CHANNEL_COUNT__MASK = 0x00000007, 141 }; 142 143 enum { 144 NEEDS_CHANNEL_1 = 0x00000000, // mono 145 NEEDS_CHANNEL_2 = 0x00000001, // stereo 146 147 // sample format is not explicitly specified, and is assumed to be AUDIO_FORMAT_PCM_16_BIT 148 149 NEEDS_MUTE = 0x00000100, 150 NEEDS_RESAMPLE = 0x00001000, 151 NEEDS_AUX = 0x00010000, 152 }; 153 154 struct state_t; 155 struct track_t; 156 class DownmixerBufferProvider; 157 class ReformatBufferProvider; 158 159 typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp, 160 int32_t* aux); 161 static const int BLOCKSIZE = 16; // 4 cache lines 162 163 struct track_t { 164 uint32_t needs; 165 166 union { 167 int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point 168 int32_t volumeRL; 169 }; 170 171 int32_t prevVolume[MAX_NUM_CHANNELS]; 172 173 // 16-byte boundary 174 175 int32_t volumeInc[MAX_NUM_CHANNELS]; 176 int32_t auxInc; 177 int32_t prevAuxLevel; 178 179 // 16-byte boundary 180 181 int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance 182 uint16_t frameCount; 183 184 uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK) 185 uint8_t unused_padding; // formerly format, was always 16 186 uint16_t enabled; // actually bool 187 audio_channel_mask_t channelMask; 188 189 // 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 191 AudioBufferProvider* bufferProvider; 192 193 // 16-byte boundary 194 195 mutable AudioBufferProvider::Buffer buffer; // 8 bytes 196 197 hook_t hook; 198 const void* in; // current location in buffer 199 200 // 16-byte boundary 201 202 AudioResampler* resampler; 203 uint32_t sampleRate; 204 int32_t* mainBuffer; 205 int32_t* auxBuffer; 206 207 // 16-byte boundary 208 AudioBufferProvider* mInputBufferProvider; // 4 bytes 209 ReformatBufferProvider* mReformatBufferProvider; // 4 bytes 210 DownmixerBufferProvider* downmixerBufferProvider; // 4 bytes 211 212 int32_t sessionId; 213 214 // 16-byte boundary 215 audio_format_t mMixerFormat; // output mix format: AUDIO_FORMAT_PCM_(FLOAT|16_BIT) 216 audio_format_t mFormat; // input track format 217 audio_format_t mMixerInFormat; // mix internal format AUDIO_FORMAT_PCM_(FLOAT|16_BIT) 218 // each track must be converted to this format. 219 220 int32_t mUnused[1]; // alignment padding 221 222 // 16-byte boundary 223 224 bool needsRamp() { return (volumeInc[0] | volumeInc[1] | auxInc) != 0; } 225 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate); 226 bool doesResample() const { return resampler != NULL; } 227 void resetResampler() { if (resampler != NULL) resampler->reset(); } 228 void adjustVolumeRamp(bool aux); 229 size_t getUnreleasedFrames() const { return resampler != NULL ? 230 resampler->getUnreleasedFrames() : 0; }; 231 }; 232 233 typedef void (*process_hook_t)(state_t* state, int64_t pts); 234 235 // pad to 32-bytes to fill cache line 236 struct state_t { 237 uint32_t enabledTracks; 238 uint32_t needsChanged; 239 size_t frameCount; 240 process_hook_t hook; // one of process__*, never NULL 241 int32_t *outputTemp; 242 int32_t *resampleTemp; 243 NBLog::Writer* mLog; 244 int32_t reserved[1]; 245 // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS 246 track_t tracks[MAX_NUM_TRACKS] __attribute__((aligned(32))); 247 }; 248 249 // AudioBufferProvider that wraps a track AudioBufferProvider by a call to a downmix effect 250 class DownmixerBufferProvider : public AudioBufferProvider { 251 public: 252 virtual status_t getNextBuffer(Buffer* buffer, int64_t pts); 253 virtual void releaseBuffer(Buffer* buffer); 254 DownmixerBufferProvider(); 255 virtual ~DownmixerBufferProvider(); 256 257 AudioBufferProvider* mTrackBufferProvider; 258 effect_handle_t mDownmixHandle; 259 effect_config_t mDownmixConfig; 260 }; 261 262 // AudioBufferProvider wrapper that reformats track to acceptable mixer input type 263 class ReformatBufferProvider : public AudioBufferProvider { 264 public: 265 ReformatBufferProvider(int32_t channels, 266 audio_format_t inputFormat, audio_format_t outputFormat); 267 virtual ~ReformatBufferProvider(); 268 269 // overrides AudioBufferProvider methods 270 virtual status_t getNextBuffer(Buffer* buffer, int64_t pts); 271 virtual void releaseBuffer(Buffer* buffer); 272 273 void reset(); 274 inline bool requiresInternalBuffers() { 275 return true; //mInputFrameSize < mOutputFrameSize; 276 } 277 278 AudioBufferProvider* mTrackBufferProvider; 279 int32_t mChannels; 280 audio_format_t mInputFormat; 281 audio_format_t mOutputFormat; 282 size_t mInputFrameSize; 283 size_t mOutputFrameSize; 284 // (only) required for reformatting to a larger size. 285 AudioBufferProvider::Buffer mBuffer; 286 void* mOutputData; 287 size_t mOutputCount; 288 size_t mConsumed; 289 }; 290 291 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc. 292 uint32_t mTrackNames; 293 294 // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS, 295 // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS 296 const uint32_t mConfiguredNames; 297 298 const uint32_t mSampleRate; 299 300 NBLog::Writer mDummyLog; 301public: 302 void setLog(NBLog::Writer* log); 303private: 304 state_t mState __attribute__((aligned(32))); 305 306 // effect descriptor for the downmixer used by the mixer 307 static effect_descriptor_t sDwnmFxDesc; 308 // indicates whether a downmix effect has been found and is usable by this mixer 309 static bool sIsMultichannelCapable; 310 311 // Call after changing either the enabled status of a track, or parameters of an enabled track. 312 // OK to call more often than that, but unnecessary. 313 void invalidateState(uint32_t mask); 314 315 static status_t initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask); 316 static status_t prepareTrackForDownmix(track_t* pTrack, int trackNum); 317 static void unprepareTrackForDownmix(track_t* pTrack, int trackName); 318 static status_t prepareTrackForReformat(track_t* pTrack, int trackNum); 319 static void unprepareTrackForReformat(track_t* pTrack, int trackName); 320 static void reconfigureBufferProviders(track_t* pTrack); 321 322 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, 323 int32_t* aux); 324 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); 325 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, 326 int32_t* aux); 327 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, 328 int32_t* aux); 329 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 330 int32_t* aux); 331 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 332 int32_t* aux); 333 334 static void process__validate(state_t* state, int64_t pts); 335 static void process__nop(state_t* state, int64_t pts); 336 static void process__genericNoResampling(state_t* state, int64_t pts); 337 static void process__genericResampling(state_t* state, int64_t pts); 338 static void process__OneTrack16BitsStereoNoResampling(state_t* state, 339 int64_t pts); 340#if 0 341 static void process__TwoTracks16BitsStereoNoResampling(state_t* state, 342 int64_t pts); 343#endif 344 345 static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS, 346 int outputFrameIndex); 347 348 static uint64_t sLocalTimeFreq; 349 static pthread_once_t sOnceControl; 350 static void sInitRoutine(); 351 352 // multi-format process hooks 353 template <int MIXTYPE, int NCHAN, typename TO, typename TI, typename TA> 354 static void process_NoResampleOneTrack(state_t* state, int64_t pts); 355 356 // multi-format track hooks 357 template <int MIXTYPE, int NCHAN, typename TO, typename TI, typename TA> 358 static void track__Resample(track_t* t, TO* out, size_t frameCount, 359 TO* temp __unused, TA* aux); 360 template <int MIXTYPE, int NCHAN, typename TO, typename TI, typename TA> 361 static void track__NoResample(track_t* t, TO* out, size_t frameCount, 362 TO* temp __unused, TA* aux); 363 364 static void convertMixerFormat(void *out, audio_format_t mixerOutFormat, 365 void *in, audio_format_t mixerInFormat, size_t sampleCount); 366 367 // hook types 368 enum { 369 PROCESSTYPE_NORESAMPLEONETRACK, 370 }; 371 enum { 372 TRACKTYPE_NOP, 373 TRACKTYPE_RESAMPLE, 374 TRACKTYPE_NORESAMPLE, 375 TRACKTYPE_NORESAMPLEMONO, 376 }; 377 378 // functions for determining the proper process and track hooks. 379 static process_hook_t getProcessHook(int processType, int channels, 380 audio_format_t mixerInFormat, audio_format_t mixerOutFormat); 381 static hook_t getTrackHook(int trackType, int channels, 382 audio_format_t mixerInFormat, audio_format_t mixerOutFormat); 383}; 384 385// ---------------------------------------------------------------------------- 386}; // namespace android 387 388#endif // ANDROID_AUDIO_MIXER_H 389