AudioMixer.h revision c56f3426099a3cf2d07ccff8886050c7fbce140f
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 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 = 0x1000; 62 63 enum { // names 64 65 // track names (MAX_NUM_TRACKS units) 66 TRACK0 = 0x1000, 67 68 // 0x2000 is unused 69 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 78 CHANNEL_MASK = 0x4000, 79 FORMAT = 0x4001, 80 MAIN_BUFFER = 0x4002, 81 AUX_BUFFER = 0x4003, 82 DOWNMIX_TYPE = 0X4004, 83 MIXER_FORMAT = 0x4005, // AUDIO_FORMAT_PCM_(FLOAT|16_BIT) 84 // for target RESAMPLE 85 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. 96 // for target RAMP_VOLUME and VOLUME (8 channels max) 97 // FIXME use float for these 3 to improve the dynamic range 98 VOLUME0 = 0x4200, 99 VOLUME1 = 0x4201, 100 AUXLEVEL = 0x4210, 101 }; 102 103 104 // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS 105 106 // Allocate a track name. Returns new track name if successful, -1 on failure. 107 int getTrackName(audio_channel_mask_t channelMask, int sessionId); 108 109 // Free an allocated track by name 110 void deleteTrackName(int name); 111 112 // Enable or disable an allocated track by name 113 void enable(int name); 114 void disable(int name); 115 116 void setParameter(int name, int target, int param, void *value); 117 118 void setBufferProvider(int name, AudioBufferProvider* bufferProvider); 119 void process(int64_t pts); 120 121 uint32_t trackNames() const { return mTrackNames; } 122 123 size_t getUnreleasedFrames(int name) const; 124 125private: 126 127 enum { 128 // FIXME this representation permits up to 8 channels 129 NEEDS_CHANNEL_COUNT__MASK = 0x00000007, 130 }; 131 132 enum { 133 NEEDS_CHANNEL_1 = 0x00000000, // mono 134 NEEDS_CHANNEL_2 = 0x00000001, // stereo 135 136 // sample format is not explicitly specified, and is assumed to be AUDIO_FORMAT_PCM_16_BIT 137 138 NEEDS_MUTE = 0x00000100, 139 NEEDS_RESAMPLE = 0x00001000, 140 NEEDS_AUX = 0x00010000, 141 }; 142 143 struct state_t; 144 struct track_t; 145 class DownmixerBufferProvider; 146 147 typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp, 148 int32_t* aux); 149 static const int BLOCKSIZE = 16; // 4 cache lines 150 151 struct track_t { 152 uint32_t needs; 153 154 union { 155 int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point 156 int32_t volumeRL; 157 }; 158 159 int32_t prevVolume[MAX_NUM_CHANNELS]; 160 161 // 16-byte boundary 162 163 int32_t volumeInc[MAX_NUM_CHANNELS]; 164 int32_t auxInc; 165 int32_t prevAuxLevel; 166 167 // 16-byte boundary 168 169 int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance 170 uint16_t frameCount; 171 172 uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK) 173 uint8_t format; // always 16 174 uint16_t enabled; // actually bool 175 audio_channel_mask_t channelMask; 176 177 // actual buffer provider used by the track hooks, see DownmixerBufferProvider below 178 // for how the Track buffer provider is wrapped by another one when dowmixing is required 179 AudioBufferProvider* bufferProvider; 180 181 // 16-byte boundary 182 183 mutable AudioBufferProvider::Buffer buffer; // 8 bytes 184 185 hook_t hook; 186 const void* in; // current location in buffer 187 188 // 16-byte boundary 189 190 AudioResampler* resampler; 191 uint32_t sampleRate; 192 int32_t* mainBuffer; 193 int32_t* auxBuffer; 194 195 // 16-byte boundary 196 197 DownmixerBufferProvider* downmixerBufferProvider; // 4 bytes 198 199 int32_t sessionId; 200 201 audio_format_t mMixerFormat; // at this time: AUDIO_FORMAT_PCM_(FLOAT|16_BIT) 202 203 int32_t padding[1]; 204 205 // 16-byte boundary 206 207 bool setResampler(uint32_t sampleRate, uint32_t devSampleRate); 208 bool doesResample() const { return resampler != NULL; } 209 void resetResampler() { if (resampler != NULL) resampler->reset(); } 210 void adjustVolumeRamp(bool aux); 211 size_t getUnreleasedFrames() const { return resampler != NULL ? 212 resampler->getUnreleasedFrames() : 0; }; 213 }; 214 215 // pad to 32-bytes to fill cache line 216 struct state_t { 217 uint32_t enabledTracks; 218 uint32_t needsChanged; 219 size_t frameCount; 220 void (*hook)(state_t* state, int64_t pts); // one of process__*, never NULL 221 int32_t *outputTemp; 222 int32_t *resampleTemp; 223 NBLog::Writer* mLog; 224 int32_t reserved[1]; 225 // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS 226 track_t tracks[MAX_NUM_TRACKS] __attribute__((aligned(32))); 227 }; 228 229 // AudioBufferProvider that wraps a track AudioBufferProvider by a call to a downmix effect 230 class DownmixerBufferProvider : public AudioBufferProvider { 231 public: 232 virtual status_t getNextBuffer(Buffer* buffer, int64_t pts); 233 virtual void releaseBuffer(Buffer* buffer); 234 DownmixerBufferProvider(); 235 virtual ~DownmixerBufferProvider(); 236 237 AudioBufferProvider* mTrackBufferProvider; 238 effect_handle_t mDownmixHandle; 239 effect_config_t mDownmixConfig; 240 }; 241 242 // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc. 243 uint32_t mTrackNames; 244 245 // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS, 246 // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS 247 const uint32_t mConfiguredNames; 248 249 const uint32_t mSampleRate; 250 251 NBLog::Writer mDummyLog; 252public: 253 void setLog(NBLog::Writer* log); 254private: 255 state_t mState __attribute__((aligned(32))); 256 257 // effect descriptor for the downmixer used by the mixer 258 static effect_descriptor_t sDwnmFxDesc; 259 // indicates whether a downmix effect has been found and is usable by this mixer 260 static bool sIsMultichannelCapable; 261 262 // Call after changing either the enabled status of a track, or parameters of an enabled track. 263 // OK to call more often than that, but unnecessary. 264 void invalidateState(uint32_t mask); 265 266 static status_t initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask); 267 static status_t prepareTrackForDownmix(track_t* pTrack, int trackNum); 268 static void unprepareTrackForDownmix(track_t* pTrack, int trackName); 269 270 static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, 271 int32_t* aux); 272 static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); 273 static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, 274 int32_t* aux); 275 static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, 276 int32_t* aux); 277 static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 278 int32_t* aux); 279 static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 280 int32_t* aux); 281 282 static void process__validate(state_t* state, int64_t pts); 283 static void process__nop(state_t* state, int64_t pts); 284 static void process__genericNoResampling(state_t* state, int64_t pts); 285 static void process__genericResampling(state_t* state, int64_t pts); 286 static void process__OneTrack16BitsStereoNoResampling(state_t* state, 287 int64_t pts); 288#if 0 289 static void process__TwoTracks16BitsStereoNoResampling(state_t* state, 290 int64_t pts); 291#endif 292 293 static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS, 294 int outputFrameIndex); 295 296 static uint64_t sLocalTimeFreq; 297 static pthread_once_t sOnceControl; 298 static void sInitRoutine(); 299}; 300 301// ---------------------------------------------------------------------------- 302}; // namespace android 303 304#endif // ANDROID_AUDIO_MIXER_H 305