Home | History | Annotate | Download | only in media
      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 <media/AudioBufferProvider.h>
     25 #include <media/AudioResampler.h>
     26 #include <media/AudioResamplerPublic.h>
     27 #include <media/BufferProviders.h>
     28 #include <media/nbaio/NBLog.h>
     29 #include <system/audio.h>
     30 #include <utils/Compat.h>
     31 #include <utils/threads.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 
     36 namespace android {
     37 
     38 // ----------------------------------------------------------------------------
     39 
     40 class AudioMixer
     41 {
     42 public:
     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 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
     57     // maximum number of channels supported for the content
     58     static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = AUDIO_CHANNEL_COUNT_MAX;
     59 
     60     static const uint16_t UNITY_GAIN_INT = 0x1000;
     61     static const CONSTEXPR float UNITY_GAIN_FLOAT = 1.0f;
     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         TIMESTRETCH     = 0x3004,
     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         MIXER_CHANNEL_MASK = 0x4006, // Channel mask for mixer output
     86         // for target RESAMPLE
     87         SAMPLE_RATE     = 0x4100, // Configure sample rate conversion on this track name;
     88                                   // parameter 'value' is the new sample rate in Hz.
     89                                   // Only creates a sample rate converter the first time that
     90                                   // the track sample rate is different from the mix sample rate.
     91                                   // If the new sample rate is the same as the mix sample rate,
     92                                   // and a sample rate converter already exists,
     93                                   // then the sample rate converter remains present but is a no-op.
     94         RESET           = 0x4101, // Reset sample rate converter without changing sample rate.
     95                                   // This clears out the resampler's input buffer.
     96         REMOVE          = 0x4102, // Remove the sample rate converter on this track name;
     97                                   // the track is restored to the mix sample rate.
     98         // for target RAMP_VOLUME and VOLUME (8 channels max)
     99         // FIXME use float for these 3 to improve the dynamic range
    100         VOLUME0         = 0x4200,
    101         VOLUME1         = 0x4201,
    102         AUXLEVEL        = 0x4210,
    103         // for target TIMESTRETCH
    104         PLAYBACK_RATE   = 0x4300, // Configure timestretch on this track name;
    105                                   // parameter 'value' is a pointer to the new playback rate.
    106     };
    107 
    108 
    109     // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
    110 
    111     // Allocate a track name.  Returns new track name if successful, -1 on failure.
    112     // The failure could be because of an invalid channelMask or format, or that
    113     // the track capacity of the mixer is exceeded.
    114     int         getTrackName(audio_channel_mask_t channelMask,
    115                              audio_format_t format, int sessionId);
    116 
    117     // Free an allocated track by name
    118     void        deleteTrackName(int name);
    119 
    120     // Enable or disable an allocated track by name
    121     void        enable(int name);
    122     void        disable(int name);
    123 
    124     void        setParameter(int name, int target, int param, void *value);
    125 
    126     void        setBufferProvider(int name, AudioBufferProvider* bufferProvider);
    127     void        process();
    128 
    129     uint32_t    trackNames() const { return mTrackNames; }
    130 
    131     size_t      getUnreleasedFrames(int name) const;
    132 
    133     static inline bool isValidPcmTrackFormat(audio_format_t format) {
    134         switch (format) {
    135         case AUDIO_FORMAT_PCM_8_BIT:
    136         case AUDIO_FORMAT_PCM_16_BIT:
    137         case AUDIO_FORMAT_PCM_24_BIT_PACKED:
    138         case AUDIO_FORMAT_PCM_32_BIT:
    139         case AUDIO_FORMAT_PCM_FLOAT:
    140             return true;
    141         default:
    142             return false;
    143         }
    144     }
    145 
    146 private:
    147 
    148     enum {
    149         // FIXME this representation permits up to 8 channels
    150         NEEDS_CHANNEL_COUNT__MASK   = 0x00000007,
    151     };
    152 
    153     enum {
    154         NEEDS_CHANNEL_1             = 0x00000000,   // mono
    155         NEEDS_CHANNEL_2             = 0x00000001,   // stereo
    156 
    157         // sample format is not explicitly specified, and is assumed to be AUDIO_FORMAT_PCM_16_BIT
    158 
    159         NEEDS_MUTE                  = 0x00000100,
    160         NEEDS_RESAMPLE              = 0x00001000,
    161         NEEDS_AUX                   = 0x00010000,
    162     };
    163 
    164     struct state_t;
    165     struct track_t;
    166 
    167     typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp,
    168                            int32_t* aux);
    169     static const int BLOCKSIZE = 16; // 4 cache lines
    170 
    171     struct track_t {
    172         uint32_t    needs;
    173 
    174         // TODO: Eventually remove legacy integer volume settings
    175         union {
    176         int16_t     volume[MAX_NUM_VOLUMES]; // U4.12 fixed point (top bit should be zero)
    177         int32_t     volumeRL;
    178         };
    179 
    180         int32_t     prevVolume[MAX_NUM_VOLUMES];
    181 
    182         // 16-byte boundary
    183 
    184         int32_t     volumeInc[MAX_NUM_VOLUMES];
    185         int32_t     auxInc;
    186         int32_t     prevAuxLevel;
    187 
    188         // 16-byte boundary
    189 
    190         int16_t     auxLevel;       // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
    191         uint16_t    frameCount;
    192 
    193         uint8_t     channelCount;   // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
    194         uint8_t     unused_padding; // formerly format, was always 16
    195         uint16_t    enabled;        // actually bool
    196         audio_channel_mask_t channelMask;
    197 
    198         // actual buffer provider used by the track hooks, see DownmixerBufferProvider below
    199         //  for how the Track buffer provider is wrapped by another one when dowmixing is required
    200         AudioBufferProvider*                bufferProvider;
    201 
    202         // 16-byte boundary
    203 
    204         mutable AudioBufferProvider::Buffer buffer; // 8 bytes
    205 
    206         hook_t      hook;
    207         const void* in;             // current location in buffer
    208 
    209         // 16-byte boundary
    210 
    211         AudioResampler*     resampler;
    212         uint32_t            sampleRate;
    213         int32_t*           mainBuffer;
    214         int32_t*           auxBuffer;
    215 
    216         // 16-byte boundary
    217 
    218         /* Buffer providers are constructed to translate the track input data as needed.
    219          *
    220          * TODO: perhaps make a single PlaybackConverterProvider class to move
    221          * all pre-mixer track buffer conversions outside the AudioMixer class.
    222          *
    223          * 1) mInputBufferProvider: The AudioTrack buffer provider.
    224          * 2) mReformatBufferProvider: If not NULL, performs the audio reformat to
    225          *    match either mMixerInFormat or mDownmixRequiresFormat, if the downmixer
    226          *    requires reformat. For example, it may convert floating point input to
    227          *    PCM_16_bit if that's required by the downmixer.
    228          * 3) downmixerBufferProvider: If not NULL, performs the channel remixing to match
    229          *    the number of channels required by the mixer sink.
    230          * 4) mPostDownmixReformatBufferProvider: If not NULL, performs reformatting from
    231          *    the downmixer requirements to the mixer engine input requirements.
    232          * 5) mTimestretchBufferProvider: Adds timestretching for playback rate
    233          */
    234         AudioBufferProvider*     mInputBufferProvider;    // externally provided buffer provider.
    235         PassthruBufferProvider*  mReformatBufferProvider; // provider wrapper for reformatting.
    236         PassthruBufferProvider*  downmixerBufferProvider; // wrapper for channel conversion.
    237         PassthruBufferProvider*  mPostDownmixReformatBufferProvider;
    238         PassthruBufferProvider*  mTimestretchBufferProvider;
    239 
    240         int32_t     sessionId;
    241 
    242         audio_format_t mMixerFormat;     // output mix format: AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
    243         audio_format_t mFormat;          // input track format
    244         audio_format_t mMixerInFormat;   // mix internal format AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
    245                                          // each track must be converted to this format.
    246         audio_format_t mDownmixRequiresFormat;  // required downmixer format
    247                                                 // AUDIO_FORMAT_PCM_16_BIT if 16 bit necessary
    248                                                 // AUDIO_FORMAT_INVALID if no required format
    249 
    250         float          mVolume[MAX_NUM_VOLUMES];     // floating point set volume
    251         float          mPrevVolume[MAX_NUM_VOLUMES]; // floating point previous volume
    252         float          mVolumeInc[MAX_NUM_VOLUMES];  // floating point volume increment
    253 
    254         float          mAuxLevel;                     // floating point set aux level
    255         float          mPrevAuxLevel;                 // floating point prev aux level
    256         float          mAuxInc;                       // floating point aux increment
    257 
    258         audio_channel_mask_t mMixerChannelMask;
    259         uint32_t             mMixerChannelCount;
    260 
    261         AudioPlaybackRate    mPlaybackRate;
    262 
    263         bool        needsRamp() { return (volumeInc[0] | volumeInc[1] | auxInc) != 0; }
    264         bool        setResampler(uint32_t trackSampleRate, uint32_t devSampleRate);
    265         bool        doesResample() const { return resampler != NULL; }
    266         void        resetResampler() { if (resampler != NULL) resampler->reset(); }
    267         void        adjustVolumeRamp(bool aux, bool useFloat = false);
    268         size_t      getUnreleasedFrames() const { return resampler != NULL ?
    269                                                     resampler->getUnreleasedFrames() : 0; };
    270 
    271         status_t    prepareForDownmix();
    272         void        unprepareForDownmix();
    273         status_t    prepareForReformat();
    274         void        unprepareForReformat();
    275         bool        setPlaybackRate(const AudioPlaybackRate &playbackRate);
    276         void        reconfigureBufferProviders();
    277     };
    278 
    279     typedef void (*process_hook_t)(state_t* state);
    280 
    281     // pad to 32-bytes to fill cache line
    282     struct state_t {
    283         uint32_t        enabledTracks;
    284         uint32_t        needsChanged;
    285         size_t          frameCount;
    286         process_hook_t  hook;   // one of process__*, never NULL
    287         int32_t         *outputTemp;
    288         int32_t         *resampleTemp;
    289         NBLog::Writer*  mNBLogWriter;   // associated NBLog::Writer or &mDummyLog
    290         int32_t         reserved[1];
    291         // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS
    292         track_t         tracks[MAX_NUM_TRACKS] __attribute__((aligned(32)));
    293     };
    294 
    295     // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
    296     uint32_t        mTrackNames;
    297 
    298     // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS,
    299     // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS
    300     const uint32_t  mConfiguredNames;
    301 
    302     const uint32_t  mSampleRate;
    303 
    304     NBLog::Writer   mDummyLogWriter;
    305 public:
    306     // Called by FastMixer to inform AudioMixer of it's associated NBLog::Writer.
    307     // FIXME It would be safer to use TLS for this, so we don't accidentally use wrong one.
    308     void            setNBLogWriter(NBLog::Writer* log);
    309 private:
    310     state_t         mState __attribute__((aligned(32)));
    311 
    312     // Call after changing either the enabled status of a track, or parameters of an enabled track.
    313     // OK to call more often than that, but unnecessary.
    314     void invalidateState(uint32_t mask);
    315 
    316     bool setChannelMasks(int name,
    317             audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask);
    318 
    319     static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
    320             int32_t* aux);
    321     static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
    322     static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
    323             int32_t* aux);
    324     static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
    325             int32_t* aux);
    326     static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
    327             int32_t* aux);
    328     static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
    329             int32_t* aux);
    330 
    331     static void process__validate(state_t* state);
    332     static void process__nop(state_t* state);
    333     static void process__genericNoResampling(state_t* state);
    334     static void process__genericResampling(state_t* state);
    335     static void process__OneTrack16BitsStereoNoResampling(state_t* state);
    336 
    337     static pthread_once_t   sOnceControl;
    338     static void             sInitRoutine();
    339 
    340     /* multi-format volume mixing function (calls template functions
    341      * in AudioMixerOps.h).  The template parameters are as follows:
    342      *
    343      *   MIXTYPE     (see AudioMixerOps.h MIXTYPE_* enumeration)
    344      *   USEFLOATVOL (set to true if float volume is used)
    345      *   ADJUSTVOL   (set to true if volume ramp parameters needs adjustment afterwards)
    346      *   TO: int32_t (Q4.27) or float
    347      *   TI: int32_t (Q4.27) or int16_t (Q0.15) or float
    348      *   TA: int32_t (Q4.27)
    349      */
    350     template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
    351         typename TO, typename TI, typename TA>
    352     static void volumeMix(TO *out, size_t outFrames,
    353             const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t);
    354 
    355     // multi-format process hooks
    356     template <int MIXTYPE, typename TO, typename TI, typename TA>
    357     static void process_NoResampleOneTrack(state_t* state);
    358 
    359     // multi-format track hooks
    360     template <int MIXTYPE, typename TO, typename TI, typename TA>
    361     static void track__Resample(track_t* t, TO* out, size_t frameCount,
    362             TO* temp __unused, TA* aux);
    363     template <int MIXTYPE, typename TO, typename TI, typename TA>
    364     static void track__NoResample(track_t* t, TO* out, size_t frameCount,
    365             TO* temp __unused, TA* aux);
    366 
    367     static void convertMixerFormat(void *out, audio_format_t mixerOutFormat,
    368             void *in, audio_format_t mixerInFormat, size_t sampleCount);
    369 
    370     // hook types
    371     enum {
    372         PROCESSTYPE_NORESAMPLEONETRACK,
    373     };
    374     enum {
    375         TRACKTYPE_NOP,
    376         TRACKTYPE_RESAMPLE,
    377         TRACKTYPE_NORESAMPLE,
    378         TRACKTYPE_NORESAMPLEMONO,
    379     };
    380 
    381     // functions for determining the proper process and track hooks.
    382     static process_hook_t getProcessHook(int processType, uint32_t channelCount,
    383             audio_format_t mixerInFormat, audio_format_t mixerOutFormat);
    384     static hook_t getTrackHook(int trackType, uint32_t channelCount,
    385             audio_format_t mixerInFormat, audio_format_t mixerOutFormat);
    386 };
    387 
    388 // ----------------------------------------------------------------------------
    389 } // namespace android
    390 
    391 #endif // ANDROID_AUDIO_MIXER_H
    392