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> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | // ---------------------------------------------------------------------------- |
| 28 | |
Mathias Agopian | e762be9 | 2013-05-09 16:26:45 -0700 | [diff] [blame^] | 29 | class ANDROID_API AudioResampler { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 30 | public: |
| 31 | // Determines quality of SRC. |
| 32 | // LOW_QUALITY: linear interpolator (1st order) |
| 33 | // MED_QUALITY: cubic interpolator (3rd order) |
| 34 | // HIGH_QUALITY: fixed multi-tap FIR (e.g. 48KHz->44.1KHz) |
| 35 | // NOTE: high quality SRC will only be supported for |
| 36 | // certain fixed rate conversions. Sample rate cannot be |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 37 | // changed dynamically. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 38 | enum src_quality { |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 39 | DEFAULT_QUALITY=0, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | LOW_QUALITY=1, |
| 41 | MED_QUALITY=2, |
SathishKumar Mani | 76b1116 | 2012-01-17 10:49:47 -0800 | [diff] [blame] | 42 | HIGH_QUALITY=3, |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 43 | VERY_HIGH_QUALITY=4, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static AudioResampler* create(int bitDepth, int inChannelCount, |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 47 | int32_t sampleRate, src_quality quality=DEFAULT_QUALITY); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 48 | |
| 49 | virtual ~AudioResampler(); |
| 50 | |
| 51 | virtual void init() = 0; |
| 52 | virtual void setSampleRate(int32_t inSampleRate); |
| 53 | virtual void setVolume(int16_t left, int16_t right); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 54 | virtual void setLocalTimeFreq(uint64_t freq); |
| 55 | |
| 56 | // set the PTS of the next buffer output by the resampler |
| 57 | virtual void setPTS(int64_t pts); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 | |
| 59 | virtual void resample(int32_t* out, size_t outFrameCount, |
| 60 | AudioBufferProvider* provider) = 0; |
| 61 | |
Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 62 | virtual void reset(); |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 63 | virtual size_t getUnreleasedFrames() const { return mInputIndex; } |
Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 64 | |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 65 | // called from destructor, so must not be virtual |
| 66 | src_quality getQuality() const { return mQuality; } |
| 67 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 68 | protected: |
| 69 | // number of bits for phase fraction - 30 bits allows nearly 2x downsampling |
| 70 | static const int kNumPhaseBits = 30; |
| 71 | |
| 72 | // phase mask for fraction |
| 73 | static const uint32_t kPhaseMask = (1LU<<kNumPhaseBits)-1; |
| 74 | |
| 75 | // multiplier to calculate fixed point phase increment |
| 76 | static const double kPhaseMultiplier = 1L << kNumPhaseBits; |
| 77 | |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 78 | AudioResampler(int bitDepth, int inChannelCount, int32_t sampleRate, src_quality quality); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 79 | |
| 80 | // prevent copying |
| 81 | AudioResampler(const AudioResampler&); |
| 82 | AudioResampler& operator=(const AudioResampler&); |
| 83 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 84 | int64_t calculateOutputPTS(int outputFrameIndex); |
| 85 | |
Glenn Kasten | 004f719 | 2012-01-30 09:26:17 -0800 | [diff] [blame] | 86 | const int32_t mBitDepth; |
| 87 | const int32_t mChannelCount; |
| 88 | const int32_t mSampleRate; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 89 | int32_t mInSampleRate; |
| 90 | AudioBufferProvider::Buffer mBuffer; |
| 91 | union { |
| 92 | int16_t mVolume[2]; |
| 93 | uint32_t mVolumeRL; |
| 94 | }; |
| 95 | int16_t mTargetVolume[2]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 96 | size_t mInputIndex; |
| 97 | int32_t mPhaseIncrement; |
| 98 | uint32_t mPhaseFraction; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 99 | uint64_t mLocalTimeFreq; |
| 100 | int64_t mPTS; |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 101 | |
| 102 | private: |
| 103 | const src_quality mQuality; |
| 104 | |
| 105 | // Return 'true' if the quality level is supported without explicit request |
| 106 | static bool qualityIsSupported(src_quality quality); |
| 107 | |
| 108 | // For pthread_once() |
| 109 | static void init_routine(); |
| 110 | |
| 111 | // Return the estimated CPU load for specific resampler in MHz. |
| 112 | // The absolute number is irrelevant, it's the relative values that matter. |
| 113 | static uint32_t qualityMHz(src_quality quality); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | // ---------------------------------------------------------------------------- |
| 117 | } |
| 118 | ; // namespace android |
| 119 | |
| 120 | #endif // ANDROID_AUDIO_RESAMPLER_H |