AudioMixer.h revision ef7c7fbd0e3fb36af14cd7d39f64c949031516a5
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 33namespace android { 34 35// ---------------------------------------------------------------------------- 36 37class AudioMixer 38{ 39public: 40 AudioMixer(size_t frameCount, uint32_t sampleRate, 41 uint32_t maxNumTracks = MAX_NUM_TRACKS); 42 43 /*virtual*/ ~AudioMixer(); // non-virtual saves a v-table, restore if sub-classed 44 45 46 // This mixer has a hard-coded upper limit of 32 active track inputs. 47 // Adding support for > 32 tracks would require more than simply changing this value. 48 static const uint32_t MAX_NUM_TRACKS = 32; 49 // maximum number of channels supported by the mixer 50 51 // This mixer has a hard-coded upper limit of 2 channels for output. 52 // There is support for > 2 channel tracks down-mixed to 2 channel output via a down-mix effect. 53 // Adding support for > 2 channel output would require more than simply changing this value. 54 static const uint32_t MAX_NUM_CHANNELS = 2; 55 // maximum number of channels supported for the content 56 static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = 8; 57 58 static const uint16_t UNITY_GAIN = 0x1000; 59 60 enum { // names 61 62 // track names (MAX_NUM_TRACKS units) 63 TRACK0 = 0x1000, 64 65 // 0x2000 is unused 66 67 // setParameter targets 68 TRACK = 0x3000, 69 RESAMPLE = 0x3001, 70 RAMP_VOLUME = 0x3002, // ramp to new volume 71 VOLUME = 0x3003, // don't ramp 72 73 // set Parameter names 74 // for target TRACK 75 CHANNEL_MASK = 0x4000, 76 FORMAT = 0x4001, 77 MAIN_BUFFER = 0x4002, 78 AUX_BUFFER = 0x4003, 79 DOWNMIX_TYPE = 0X4004, 80 MIXER_FORMAT = 0x4005, // AUDIO_FORMAT_PCM_(FLOAT|16_BIT) 81 // for target RESAMPLE 82 SAMPLE_RATE = 0x4100, // Configure sample rate conversion on this track name; 83 // parameter 'value' is the new sample rate in Hz. 84 // Only creates a sample rate converter the first time that 85 // the track sample rate is different from the mix sample rate. 86 // If the new sample rate is the same as the mix sample rate, 87 // and a sample rate converter already exists, 88 // then the sample rate converter remains present but is a no-op. 89 RESET = 0x4101, // Reset sample rate converter without changing sample rate. 90 // This clears out the resampler's input buffer. 91 REMOVE = 0x4102, // Remove the sample rate converter on this track name; 92 // the track is restored to the mix sample rate. 93 // for target RAMP_VOLUME and VOLUME (8 channels max) 94 VOLUME0 = 0x4200, 95 VOLUME1 = 0x4201, 96 AUXLEVEL = 0x4210, 97 }; 98 99 100 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS 101 102 // Allocate a track name. Returns new track name if successful, -1 on failure. 103 // The failure could be because of an invalid channelMask or format, or that 104 // the track capacity of the mixer is exceeded. 105 int getTrackName(audio_channel_mask_t channelMask, 106 audio_format_t format, int sessionId); 107 108 // Free an allocated track by name 109 void deleteTrackName(int name); 110 111 // Enable or disable an allocated track by name 112 void enable(int name); 113 void disable(int name); 114 115 void setParameter(int name, int target, int param, void *value); 116 117 void setBufferProvider(int name, AudioBufferProvider* bufferProvider); 118 void process(int64_t pts); 119 120 uint32_t trackNames() const { return mTrackNames; } 121 122 size_t getUnreleasedFrames(int name) const; 123 124 static inline bool isValidPcmTrackFormat(audio_format_t format) { 125 return format == AUDIO_FORMAT_PCM_16_BIT || 126 format == AUDIO_FORMAT_PCM_24_BIT_PACKED || 127 format == AUDIO_FORMAT_PCM_32_BIT || 128 format == AUDIO_FORMAT_PCM_FLOAT; 129 } 130 131private: 132 133 enum { 134 // FIXME this representation permits up to 8 channels 135 NEEDS_CHANNEL_COUNT__MASK = 0x00000007, 136 }; 137 138 enum { 139 NEEDS_CHANNEL_1 = 0x00000000, // mono 140 NEEDS_CHANNEL_2 = 0x00000001, // stereo 141 142 // sample format is not explicitly specified, and is assumed to be AUDIO_FORMAT_PCM_16_BIT 143 144 NEEDS_MUTE = 0x00000100, 145 NEEDS_RESAMPLE = 0x00001000, 146 NEEDS_AUX = 0x00010000, 147 }; 148 149 struct state_t; 150 struct track_t; 151 class DownmixerBufferProvider; 152 class ReformatBufferProvider; 153 154 typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp, 155 int32_t* aux); 156 static const int BLOCKSIZE = 16; // 4 cache lines 157 158 struct track_t { 159 uint32_t needs; 160 161 union { 162 int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point 163 int32_t volumeRL; 164 }; 165 166 int32_t prevVolume[MAX_NUM_CHANNELS]; 167 168 // 16-byte boundary 169 170 int32_t volumeInc[MAX_NUM_CHANNELS]; 171 int32_t auxInc; 172 int32_t prevAuxLevel; 173 174 // 16-byte boundary 175 176 int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance 177 uint16_t frameCount; 178 179 uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK) 180 uint8_t unused_padding; // formerly format, was always 16 181 uint16_t enabled; // actually bool 182 audio_channel_mask_t channelMask; 183 184 // actual buffer provider used by the track hooks, see DownmixerBufferProvider below 185 // for how the Track buffer provider is wrapped by another one when dowmixing is required 186 AudioBufferProvider* bufferProvider; 187 188 // 16-byte boundary 189 190 mutable AudioBufferProvider::Buffer buffer; // 8 bytes 191 192 hook_t hook; 193 const void* in; // current location in buffer 194 195 // 16-byte boundary 196 197 AudioResampler* resampler; 198 uint32_t sampleRate; 199 int32_t* mainBuffer; 200 int32_t* auxBuffer; 201 202 // 16-byte boundary 203 AudioBufferProvider* mInputBufferProvider; // 4 bytes 204 ReformatBufferProvider* mReformatBufferProvider; // 4 bytes 205 DownmixerBufferProvider* downmixerBufferProvider; // 4 bytes 206 207 int32_t sessionId; 208 209 // 16-byte boundary 210 audio_format_t mMixerFormat; // output mix format: AUDIO_FORMAT_PCM_(FLOAT|16_BIT) 211 audio_format_t mFormat; // input track format 212 audio_format_t mMixerInFormat; // mix internal format AUDIO_FORMAT_PCM_(FLOAT|16_BIT) 213 // each track must be converted to this format. 214 215 int32_t mUnused[1]; // alignment padding 216 217 // 16-byte boundary 218 219 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate); 220 bool doesResample() const { return resampler != NULL; } 221 void resetResampler() { if (resampler != NULL) resampler->reset(); } 222 void adjustVolumeRamp(bool aux); 223 size_t getUnreleasedFrames() const { return resampler != NULL ? 224 resampler->getUnreleasedFrames() : 0; }; 225 }; 226 227 // pad to 32-bytes to fill cache line 228 struct state_t { 229 uint32_t enabledTracks; 230 uint32_t needsChanged; 231 size_t frameCount; 232 void (*hook)(state_t* state, int64_t pts); // one of process__*, never NULL 233 int32_t *outputTemp; 234 int32_t *resampleTemp; 235 NBLog::Writer* mLog; 236 int32_t reserved[1]; 237 // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS 238 track_t tracks[MAX_NUM_TRACKS] __attribute__((aligned(32))); 239 }; 240 241 // AudioBufferProvider that wraps a track AudioBufferProvider by a call to a downmix effect 242 class DownmixerBufferProvider : public AudioBufferProvider { 243 public: 244 virtual status_t getNextBuffer(Buffer* buffer, int64_t pts); 245 virtual void releaseBuffer(Buffer* buffer); 246 DownmixerBufferProvider(); 247 virtual ~DownmixerBufferProvider(); 248 249 AudioBufferProvider* mTrackBufferProvider; 250 effect_handle_t mDownmixHandle; 251 effect_config_t mDownmixConfig; 252 }; 253 254 // AudioBufferProvider wrapper that reformats track to acceptable mixer input type 255 class ReformatBufferProvider : public AudioBufferProvider { 256 public: 257 ReformatBufferProvider(int32_t channels, 258 audio_format_t inputFormat, audio_format_t outputFormat); 259 virtual ~ReformatBufferProvider(); 260 261 // overrides AudioBufferProvider methods 262 virtual status_t getNextBuffer(Buffer* buffer, int64_t pts); 263 virtual void releaseBuffer(Buffer* buffer); 264 265 void reset(); 266 inline bool requiresInternalBuffers() { 267 return true; //mInputFrameSize < mOutputFrameSize; 268 } 269 270 AudioBufferProvider* mTrackBufferProvider; 271 int32_t mChannels; 272 audio_format_t mInputFormat; 273 audio_format_t mOutputFormat; 274 size_t mInputFrameSize; 275 size_t mOutputFrameSize; 276 // (only) required for reformatting to a larger size. 277 AudioBufferProvider::Buffer mBuffer; 278 void* mOutputData; 279 size_t mOutputCount; 280 size_t mConsumed; 281 }; 282 283 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc. 284 uint32_t mTrackNames; 285 286 // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS, 287 // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS 288 const uint32_t mConfiguredNames; 289 290 const uint32_t mSampleRate; 291 292 NBLog::Writer mDummyLog; 293public: 294 void setLog(NBLog::Writer* log); 295private: 296 state_t mState __attribute__((aligned(32))); 297 298 // effect descriptor for the downmixer used by the mixer 299 static effect_descriptor_t sDwnmFxDesc; 300 // indicates whether a downmix effect has been found and is usable by this mixer 301 static bool sIsMultichannelCapable; 302 303 // Call after changing either the enabled status of a track, or parameters of an enabled track. 304 // OK to call more often than that, but unnecessary. 305 void invalidateState(uint32_t mask); 306 307 static status_t initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask); 308 static status_t prepareTrackForDownmix(track_t* pTrack, int trackNum); 309 static void unprepareTrackForDownmix(track_t* pTrack, int trackName); 310 static status_t prepareTrackForReformat(track_t* pTrack, int trackNum); 311 static void unprepareTrackForReformat(track_t* pTrack, int trackName); 312 static void reconfigureBufferProviders(track_t* pTrack); 313 314 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, 315 int32_t* aux); 316 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); 317 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, 318 int32_t* aux); 319 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, 320 int32_t* aux); 321 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 322 int32_t* aux); 323 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 324 int32_t* aux); 325 326 static void process__validate(state_t* state, int64_t pts); 327 static void process__nop(state_t* state, int64_t pts); 328 static void process__genericNoResampling(state_t* state, int64_t pts); 329 static void process__genericResampling(state_t* state, int64_t pts); 330 static void process__OneTrack16BitsStereoNoResampling(state_t* state, 331 int64_t pts); 332#if 0 333 static void process__TwoTracks16BitsStereoNoResampling(state_t* state, 334 int64_t pts); 335#endif 336 337 static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS, 338 int outputFrameIndex); 339 340 static uint64_t sLocalTimeFreq; 341 static pthread_once_t sOnceControl; 342 static void sInitRoutine(); 343}; 344 345// ---------------------------------------------------------------------------- 346}; // namespace android 347 348#endif // ANDROID_AUDIO_MIXER_H 349