Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_AUDIO_RESAMPLER_H |
| 18 | #define ANDROID_AUDIO_RESAMPLER_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
Mathias Agopian | e762be9 | 2013-05-09 16:26:45 -0700 | [diff] [blame] | 22 | #include <cutils/compiler.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 23 | |
Glenn Kasten | 2dd4bdd | 2012-08-29 11:10:32 -0700 | [diff] [blame] | 24 | #include <media/AudioBufferProvider.h> |
Andy Hung | 3348e36 | 2014-07-07 10:21:44 -0700 | [diff] [blame] | 25 | #include <system/audio.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | // ---------------------------------------------------------------------------- |
| 29 | |
Mathias Agopian | e762be9 | 2013-05-09 16:26:45 -0700 | [diff] [blame] | 30 | class ANDROID_API AudioResampler { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 31 | public: |
| 32 | // Determines quality of SRC. |
| 33 | // LOW_QUALITY: linear interpolator (1st order) |
| 34 | // MED_QUALITY: cubic interpolator (3rd order) |
| 35 | // HIGH_QUALITY: fixed multi-tap FIR (e.g. 48KHz->44.1KHz) |
| 36 | // NOTE: high quality SRC will only be supported for |
| 37 | // certain fixed rate conversions. Sample rate cannot be |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 38 | // changed dynamically. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 39 | enum src_quality { |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 40 | DEFAULT_QUALITY=0, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 41 | LOW_QUALITY=1, |
| 42 | MED_QUALITY=2, |
SathishKumar Mani | 76b1116 | 2012-01-17 10:49:47 -0800 | [diff] [blame] | 43 | HIGH_QUALITY=3, |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 44 | VERY_HIGH_QUALITY=4, |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 45 | DYN_LOW_QUALITY=5, |
| 46 | DYN_MED_QUALITY=6, |
| 47 | DYN_HIGH_QUALITY=7, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame^] | 50 | static const float UNITY_GAIN_FLOAT = 1.0f; |
| 51 | |
Andy Hung | 3348e36 | 2014-07-07 10:21:44 -0700 | [diff] [blame] | 52 | static AudioResampler* create(audio_format_t format, int inChannelCount, |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 53 | int32_t sampleRate, src_quality quality=DEFAULT_QUALITY); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 54 | |
| 55 | virtual ~AudioResampler(); |
| 56 | |
| 57 | virtual void init() = 0; |
| 58 | virtual void setSampleRate(int32_t inSampleRate); |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame^] | 59 | virtual void setVolume(float left, float right); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 60 | virtual void setLocalTimeFreq(uint64_t freq); |
| 61 | |
| 62 | // set the PTS of the next buffer output by the resampler |
| 63 | virtual void setPTS(int64_t pts); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 64 | |
Glenn Kasten | 34af026 | 2013-07-30 11:52:39 -0700 | [diff] [blame] | 65 | // Resample int16_t samples from provider and accumulate into 'out'. |
| 66 | // A mono provider delivers a sequence of samples. |
| 67 | // A stereo provider delivers a sequence of interleaved pairs of samples. |
| 68 | // Multi-channel providers are not supported. |
Andy Hung | 84a0c6e | 2014-04-02 11:24:53 -0700 | [diff] [blame] | 69 | // In either case, 'out' holds interleaved pairs of fixed-point Q4.27. |
Glenn Kasten | 34af026 | 2013-07-30 11:52:39 -0700 | [diff] [blame] | 70 | // That is, for a mono provider, there is an implicit up-channeling. |
| 71 | // Since this method accumulates, the caller is responsible for clearing 'out' initially. |
| 72 | // FIXME assumes provider is always successful; it should return the actual frame count. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 73 | virtual void resample(int32_t* out, size_t outFrameCount, |
| 74 | AudioBufferProvider* provider) = 0; |
| 75 | |
Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 76 | virtual void reset(); |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 77 | virtual size_t getUnreleasedFrames() const { return mInputIndex; } |
Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 78 | |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 79 | // called from destructor, so must not be virtual |
| 80 | src_quality getQuality() const { return mQuality; } |
| 81 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 82 | protected: |
| 83 | // number of bits for phase fraction - 30 bits allows nearly 2x downsampling |
| 84 | static const int kNumPhaseBits = 30; |
| 85 | |
| 86 | // phase mask for fraction |
| 87 | static const uint32_t kPhaseMask = (1LU<<kNumPhaseBits)-1; |
| 88 | |
| 89 | // multiplier to calculate fixed point phase increment |
Glenn Kasten | 01d3acb | 2014-02-06 08:24:07 -0800 | [diff] [blame] | 90 | static const double kPhaseMultiplier; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 91 | |
Andy Hung | 3348e36 | 2014-07-07 10:21:44 -0700 | [diff] [blame] | 92 | AudioResampler(int inChannelCount, int32_t sampleRate, src_quality quality); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 93 | |
| 94 | // prevent copying |
| 95 | AudioResampler(const AudioResampler&); |
| 96 | AudioResampler& operator=(const AudioResampler&); |
| 97 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 98 | int64_t calculateOutputPTS(int outputFrameIndex); |
| 99 | |
Glenn Kasten | 004f719 | 2012-01-30 09:26:17 -0800 | [diff] [blame] | 100 | const int32_t mChannelCount; |
| 101 | const int32_t mSampleRate; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 102 | int32_t mInSampleRate; |
| 103 | AudioBufferProvider::Buffer mBuffer; |
| 104 | union { |
| 105 | int16_t mVolume[2]; |
| 106 | uint32_t mVolumeRL; |
| 107 | }; |
| 108 | int16_t mTargetVolume[2]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 109 | size_t mInputIndex; |
| 110 | int32_t mPhaseIncrement; |
| 111 | uint32_t mPhaseFraction; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 112 | uint64_t mLocalTimeFreq; |
| 113 | int64_t mPTS; |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 114 | |
Andy Hung | 24781ff | 2014-02-19 12:45:19 -0800 | [diff] [blame] | 115 | // returns the inFrameCount required to generate outFrameCount frames. |
| 116 | // |
| 117 | // Placed here to be a consistent for all resamplers. |
| 118 | // |
| 119 | // Right now, we use the upper bound without regards to the current state of the |
| 120 | // input buffer using integer arithmetic, as follows: |
| 121 | // |
| 122 | // (static_cast<uint64_t>(outFrameCount)*mInSampleRate + (mSampleRate - 1))/mSampleRate; |
| 123 | // |
| 124 | // The double precision equivalent (float may not be precise enough): |
| 125 | // ceil(static_cast<double>(outFrameCount) * mInSampleRate / mSampleRate); |
| 126 | // |
| 127 | // this relies on the fact that the mPhaseIncrement is rounded down from |
| 128 | // #phases * mInSampleRate/mSampleRate and the fact that Sum(Floor(x)) <= Floor(Sum(x)). |
| 129 | // http://www.proofwiki.org/wiki/Sum_of_Floors_Not_Greater_Than_Floor_of_Sums |
| 130 | // |
| 131 | // (so long as double precision is computed accurately enough to be considered |
| 132 | // greater than or equal to the Floor(x) value in int32_t arithmetic; thus this |
| 133 | // will not necessarily hold for floats). |
| 134 | // |
| 135 | // TODO: |
| 136 | // Greater accuracy and a tight bound is obtained by: |
| 137 | // 1) subtract and adjust for the current state of the AudioBufferProvider buffer. |
| 138 | // 2) using the exact integer formula where (ignoring 64b casting) |
| 139 | // inFrameCount = (mPhaseIncrement * (outFrameCount - 1) + mPhaseFraction) / phaseWrapLimit; |
| 140 | // phaseWrapLimit is the wraparound (1 << kNumPhaseBits), if not specified explicitly. |
| 141 | // |
| 142 | inline size_t getInFrameCountRequired(size_t outFrameCount) { |
| 143 | return (static_cast<uint64_t>(outFrameCount)*mInSampleRate |
| 144 | + (mSampleRate - 1))/mSampleRate; |
| 145 | } |
| 146 | |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame^] | 147 | inline float clampFloatVol(float volume) { |
| 148 | if (volume > UNITY_GAIN_FLOAT) { |
| 149 | return UNITY_GAIN_FLOAT; |
| 150 | } else if (volume >= 0.) { |
| 151 | return volume; |
| 152 | } |
| 153 | return 0.; // NaN or negative volume maps to 0. |
| 154 | } |
| 155 | |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 156 | private: |
| 157 | const src_quality mQuality; |
| 158 | |
| 159 | // Return 'true' if the quality level is supported without explicit request |
| 160 | static bool qualityIsSupported(src_quality quality); |
| 161 | |
| 162 | // For pthread_once() |
| 163 | static void init_routine(); |
| 164 | |
| 165 | // Return the estimated CPU load for specific resampler in MHz. |
| 166 | // The absolute number is irrelevant, it's the relative values that matter. |
| 167 | static uint32_t qualityMHz(src_quality quality); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | // ---------------------------------------------------------------------------- |
| 171 | } |
| 172 | ; // namespace android |
| 173 | |
| 174 | #endif // ANDROID_AUDIO_RESAMPLER_H |