Home | History | Annotate | Download | only in audioflinger
      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 <hardware/audio_effect.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 
     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 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 
     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         MIXER_CHANNEL_MASK = 0x4006, // Channel mask for mixer output
     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 
    136 private:
    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 CopyBufferProvider;
    157 
    158     typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp,
    159                            int32_t* aux);
    160     static const int BLOCKSIZE = 16; // 4 cache lines
    161 
    162     struct track_t {
    163         uint32_t    needs;
    164 
    165         // TODO: Eventually remove legacy integer volume settings
    166         union {
    167         int16_t     volume[MAX_NUM_VOLUMES]; // U4.12 fixed point (top bit should be zero)
    168         int32_t     volumeRL;
    169         };
    170 
    171         int32_t     prevVolume[MAX_NUM_VOLUMES];
    172 
    173         // 16-byte boundary
    174 
    175         int32_t     volumeInc[MAX_NUM_VOLUMES];
    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;    // externally provided buffer provider.
    209         CopyBufferProvider*      mReformatBufferProvider; // provider wrapper for reformatting.
    210         CopyBufferProvider*      downmixerBufferProvider; // wrapper for channel conversion.
    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         float          mVolume[MAX_NUM_VOLUMES];     // floating point set volume
    221         float          mPrevVolume[MAX_NUM_VOLUMES]; // floating point previous volume
    222         float          mVolumeInc[MAX_NUM_VOLUMES];  // floating point volume increment
    223 
    224         float          mAuxLevel;                     // floating point set aux level
    225         float          mPrevAuxLevel;                 // floating point prev aux level
    226         float          mAuxInc;                       // floating point aux increment
    227 
    228         // 16-byte boundary
    229         audio_channel_mask_t mMixerChannelMask;
    230         uint32_t             mMixerChannelCount;
    231 
    232         bool        needsRamp() { return (volumeInc[0] | volumeInc[1] | auxInc) != 0; }
    233         bool        setResampler(uint32_t trackSampleRate, uint32_t devSampleRate);
    234         bool        doesResample() const { return resampler != NULL; }
    235         void        resetResampler() { if (resampler != NULL) resampler->reset(); }
    236         void        adjustVolumeRamp(bool aux, bool useFloat = false);
    237         size_t      getUnreleasedFrames() const { return resampler != NULL ?
    238                                                     resampler->getUnreleasedFrames() : 0; };
    239     };
    240 
    241     typedef void (*process_hook_t)(state_t* state, int64_t pts);
    242 
    243     // pad to 32-bytes to fill cache line
    244     struct state_t {
    245         uint32_t        enabledTracks;
    246         uint32_t        needsChanged;
    247         size_t          frameCount;
    248         process_hook_t  hook;   // one of process__*, never NULL
    249         int32_t         *outputTemp;
    250         int32_t         *resampleTemp;
    251         NBLog::Writer*  mLog;
    252         int32_t         reserved[1];
    253         // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS
    254         track_t         tracks[MAX_NUM_TRACKS] __attribute__((aligned(32)));
    255     };
    256 
    257     // Base AudioBufferProvider class used for DownMixerBufferProvider, RemixBufferProvider,
    258     // and ReformatBufferProvider.
    259     // It handles a private buffer for use in converting format or channel masks from the
    260     // input data to a form acceptable by the mixer.
    261     // TODO: Make a ResamplerBufferProvider when integers are entirely removed from the
    262     // processing pipeline.
    263     class CopyBufferProvider : public AudioBufferProvider {
    264     public:
    265         // Use a private buffer of bufferFrameCount frames (each frame is outputFrameSize bytes).
    266         // If bufferFrameCount is 0, no private buffer is created and in-place modification of
    267         // the upstream buffer provider's buffers is performed by copyFrames().
    268         CopyBufferProvider(size_t inputFrameSize, size_t outputFrameSize,
    269                 size_t bufferFrameCount);
    270         virtual ~CopyBufferProvider();
    271 
    272         // Overrides AudioBufferProvider methods
    273         virtual status_t getNextBuffer(Buffer* buffer, int64_t pts);
    274         virtual void releaseBuffer(Buffer* buffer);
    275 
    276         // Other public methods
    277 
    278         // call this to release the buffer to the upstream provider.
    279         // treat it as an audio discontinuity for future samples.
    280         virtual void reset();
    281 
    282         // this function should be supplied by the derived class.  It converts
    283         // #frames in the *src pointer to the *dst pointer.  It is public because
    284         // some providers will allow this to work on arbitrary buffers outside
    285         // of the internal buffers.
    286         virtual void copyFrames(void *dst, const void *src, size_t frames) = 0;
    287 
    288         // set the upstream buffer provider. Consider calling "reset" before this function.
    289         void setBufferProvider(AudioBufferProvider *p) {
    290             mTrackBufferProvider = p;
    291         }
    292 
    293     protected:
    294         AudioBufferProvider* mTrackBufferProvider;
    295         const size_t         mInputFrameSize;
    296         const size_t         mOutputFrameSize;
    297     private:
    298         AudioBufferProvider::Buffer mBuffer;
    299         const size_t         mLocalBufferFrameCount;
    300         void*                mLocalBufferData;
    301         size_t               mConsumed;
    302     };
    303 
    304     // DownmixerBufferProvider wraps a track AudioBufferProvider to provide
    305     // position dependent downmixing by an Audio Effect.
    306     class DownmixerBufferProvider : public CopyBufferProvider {
    307     public:
    308         DownmixerBufferProvider(audio_channel_mask_t inputChannelMask,
    309                 audio_channel_mask_t outputChannelMask, audio_format_t format,
    310                 uint32_t sampleRate, int32_t sessionId, size_t bufferFrameCount);
    311         virtual ~DownmixerBufferProvider();
    312         virtual void copyFrames(void *dst, const void *src, size_t frames);
    313         bool isValid() const { return mDownmixHandle != NULL; }
    314 
    315         static status_t init();
    316         static bool isMultichannelCapable() { return sIsMultichannelCapable; }
    317 
    318     protected:
    319         effect_handle_t    mDownmixHandle;
    320         effect_config_t    mDownmixConfig;
    321 
    322         // effect descriptor for the downmixer used by the mixer
    323         static effect_descriptor_t sDwnmFxDesc;
    324         // indicates whether a downmix effect has been found and is usable by this mixer
    325         static bool                sIsMultichannelCapable;
    326         // FIXME: should we allow effects outside of the framework?
    327         // We need to here. A special ioId that must be <= -2 so it does not map to a session.
    328         static const int32_t SESSION_ID_INVALID_AND_IGNORED = -2;
    329     };
    330 
    331     // RemixBufferProvider wraps a track AudioBufferProvider to perform an
    332     // upmix or downmix to the proper channel count and mask.
    333     class RemixBufferProvider : public CopyBufferProvider {
    334     public:
    335         RemixBufferProvider(audio_channel_mask_t inputChannelMask,
    336                 audio_channel_mask_t outputChannelMask, audio_format_t format,
    337                 size_t bufferFrameCount);
    338         virtual void copyFrames(void *dst, const void *src, size_t frames);
    339 
    340     protected:
    341         const audio_format_t mFormat;
    342         const size_t         mSampleSize;
    343         const size_t         mInputChannels;
    344         const size_t         mOutputChannels;
    345         int8_t               mIdxAry[sizeof(uint32_t)*8]; // 32 bits => channel indices
    346     };
    347 
    348     // ReformatBufferProvider wraps a track AudioBufferProvider to convert the input data
    349     // to an acceptable mixer input format type.
    350     class ReformatBufferProvider : public CopyBufferProvider {
    351     public:
    352         ReformatBufferProvider(int32_t channels,
    353                 audio_format_t inputFormat, audio_format_t outputFormat,
    354                 size_t bufferFrameCount);
    355         virtual void copyFrames(void *dst, const void *src, size_t frames);
    356 
    357     protected:
    358         const int32_t        mChannels;
    359         const audio_format_t mInputFormat;
    360         const audio_format_t mOutputFormat;
    361     };
    362 
    363     // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
    364     uint32_t        mTrackNames;
    365 
    366     // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS,
    367     // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS
    368     const uint32_t  mConfiguredNames;
    369 
    370     const uint32_t  mSampleRate;
    371 
    372     NBLog::Writer   mDummyLog;
    373 public:
    374     void            setLog(NBLog::Writer* log);
    375 private:
    376     state_t         mState __attribute__((aligned(32)));
    377 
    378     // Call after changing either the enabled status of a track, or parameters of an enabled track.
    379     // OK to call more often than that, but unnecessary.
    380     void invalidateState(uint32_t mask);
    381 
    382     bool setChannelMasks(int name,
    383             audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask);
    384 
    385     // TODO: remove unused trackName/trackNum from functions below.
    386     static status_t initTrackDownmix(track_t* pTrack, int trackName);
    387     static status_t prepareTrackForDownmix(track_t* pTrack, int trackNum);
    388     static void unprepareTrackForDownmix(track_t* pTrack, int trackName);
    389     static status_t prepareTrackForReformat(track_t* pTrack, int trackNum);
    390     static void unprepareTrackForReformat(track_t* pTrack, int trackName);
    391     static void reconfigureBufferProviders(track_t* pTrack);
    392 
    393     static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
    394             int32_t* aux);
    395     static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
    396     static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
    397             int32_t* aux);
    398     static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
    399             int32_t* aux);
    400     static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
    401             int32_t* aux);
    402     static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
    403             int32_t* aux);
    404 
    405     static void process__validate(state_t* state, int64_t pts);
    406     static void process__nop(state_t* state, int64_t pts);
    407     static void process__genericNoResampling(state_t* state, int64_t pts);
    408     static void process__genericResampling(state_t* state, int64_t pts);
    409     static void process__OneTrack16BitsStereoNoResampling(state_t* state,
    410                                                           int64_t pts);
    411 
    412     static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
    413                                       int outputFrameIndex);
    414 
    415     static uint64_t         sLocalTimeFreq;
    416     static pthread_once_t   sOnceControl;
    417     static void             sInitRoutine();
    418 
    419     /* multi-format volume mixing function (calls template functions
    420      * in AudioMixerOps.h).  The template parameters are as follows:
    421      *
    422      *   MIXTYPE     (see AudioMixerOps.h MIXTYPE_* enumeration)
    423      *   USEFLOATVOL (set to true if float volume is used)
    424      *   ADJUSTVOL   (set to true if volume ramp parameters needs adjustment afterwards)
    425      *   TO: int32_t (Q4.27) or float
    426      *   TI: int32_t (Q4.27) or int16_t (Q0.15) or float
    427      *   TA: int32_t (Q4.27)
    428      */
    429     template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
    430         typename TO, typename TI, typename TA>
    431     static void volumeMix(TO *out, size_t outFrames,
    432             const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t);
    433 
    434     // multi-format process hooks
    435     template <int MIXTYPE, typename TO, typename TI, typename TA>
    436     static void process_NoResampleOneTrack(state_t* state, int64_t pts);
    437 
    438     // multi-format track hooks
    439     template <int MIXTYPE, typename TO, typename TI, typename TA>
    440     static void track__Resample(track_t* t, TO* out, size_t frameCount,
    441             TO* temp __unused, TA* aux);
    442     template <int MIXTYPE, typename TO, typename TI, typename TA>
    443     static void track__NoResample(track_t* t, TO* out, size_t frameCount,
    444             TO* temp __unused, TA* aux);
    445 
    446     static void convertMixerFormat(void *out, audio_format_t mixerOutFormat,
    447             void *in, audio_format_t mixerInFormat, size_t sampleCount);
    448 
    449     // hook types
    450     enum {
    451         PROCESSTYPE_NORESAMPLEONETRACK,
    452     };
    453     enum {
    454         TRACKTYPE_NOP,
    455         TRACKTYPE_RESAMPLE,
    456         TRACKTYPE_NORESAMPLE,
    457         TRACKTYPE_NORESAMPLEMONO,
    458     };
    459 
    460     // functions for determining the proper process and track hooks.
    461     static process_hook_t getProcessHook(int processType, uint32_t channelCount,
    462             audio_format_t mixerInFormat, audio_format_t mixerOutFormat);
    463     static hook_t getTrackHook(int trackType, uint32_t channelCount,
    464             audio_format_t mixerInFormat, audio_format_t mixerOutFormat);
    465 };
    466 
    467 // ----------------------------------------------------------------------------
    468 }; // namespace android
    469 
    470 #endif // ANDROID_AUDIO_MIXER_H
    471