blob: 069d946b543a810d25f6e6f042953d462e1b7f39 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
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>
Dan Albert36802bd2014-11-20 11:31:17 -080022
Mathias Agopiane762be92013-05-09 16:26:45 -070023#include <cutils/compiler.h>
Dan Albert36802bd2014-11-20 11:31:17 -080024#include <utils/Compat.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025
Glenn Kasten2dd4bdd2012-08-29 11:10:32 -070026#include <media/AudioBufferProvider.h>
Andy Hung3348e362014-07-07 10:21:44 -070027#include <system/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028
29namespace android {
30// ----------------------------------------------------------------------------
31
Mathias Agopiane762be92013-05-09 16:26:45 -070032class ANDROID_API AudioResampler {
Mathias Agopian65ab4712010-07-14 17:59:35 -070033public:
34 // Determines quality of SRC.
35 // LOW_QUALITY: linear interpolator (1st order)
36 // MED_QUALITY: cubic interpolator (3rd order)
37 // HIGH_QUALITY: fixed multi-tap FIR (e.g. 48KHz->44.1KHz)
38 // NOTE: high quality SRC will only be supported for
39 // certain fixed rate conversions. Sample rate cannot be
Glenn Kastene53b9ea2012-03-12 16:29:55 -070040 // changed dynamically.
Mathias Agopian65ab4712010-07-14 17:59:35 -070041 enum src_quality {
Glenn Kastenac602052012-10-01 14:04:31 -070042 DEFAULT_QUALITY=0,
Mathias Agopian65ab4712010-07-14 17:59:35 -070043 LOW_QUALITY=1,
44 MED_QUALITY=2,
SathishKumar Mani76b11162012-01-17 10:49:47 -080045 HIGH_QUALITY=3,
Glenn Kastenac602052012-10-01 14:04:31 -070046 VERY_HIGH_QUALITY=4,
Andy Hung86eae0e2013-12-09 12:12:46 -080047 DYN_LOW_QUALITY=5,
48 DYN_MED_QUALITY=6,
49 DYN_HIGH_QUALITY=7,
Mathias Agopian65ab4712010-07-14 17:59:35 -070050 };
51
Dan Albert36802bd2014-11-20 11:31:17 -080052 static const CONSTEXPR float UNITY_GAIN_FLOAT = 1.0f;
Andy Hung5e58b0a2014-06-23 19:07:29 -070053
Andy Hung3348e362014-07-07 10:21:44 -070054 static AudioResampler* create(audio_format_t format, int inChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -070055 int32_t sampleRate, src_quality quality=DEFAULT_QUALITY);
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
57 virtual ~AudioResampler();
58
59 virtual void init() = 0;
60 virtual void setSampleRate(int32_t inSampleRate);
Andy Hung5e58b0a2014-06-23 19:07:29 -070061 virtual void setVolume(float left, float right);
John Grossman4ff14ba2012-02-08 16:37:41 -080062 virtual void setLocalTimeFreq(uint64_t freq);
63
64 // set the PTS of the next buffer output by the resampler
65 virtual void setPTS(int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -070066
Glenn Kasten34af0262013-07-30 11:52:39 -070067 // Resample int16_t samples from provider and accumulate into 'out'.
68 // A mono provider delivers a sequence of samples.
69 // A stereo provider delivers a sequence of interleaved pairs of samples.
70 // Multi-channel providers are not supported.
Andy Hung84a0c6e2014-04-02 11:24:53 -070071 // In either case, 'out' holds interleaved pairs of fixed-point Q4.27.
Glenn Kasten34af0262013-07-30 11:52:39 -070072 // That is, for a mono provider, there is an implicit up-channeling.
73 // Since this method accumulates, the caller is responsible for clearing 'out' initially.
74 // FIXME assumes provider is always successful; it should return the actual frame count.
Mathias Agopian65ab4712010-07-14 17:59:35 -070075 virtual void resample(int32_t* out, size_t outFrameCount,
76 AudioBufferProvider* provider) = 0;
77
Eric Laurent243f5f92011-02-28 16:52:51 -080078 virtual void reset();
Glenn Kastenc59c0042012-02-02 14:06:11 -080079 virtual size_t getUnreleasedFrames() const { return mInputIndex; }
Eric Laurent243f5f92011-02-28 16:52:51 -080080
Glenn Kastenac602052012-10-01 14:04:31 -070081 // called from destructor, so must not be virtual
82 src_quality getQuality() const { return mQuality; }
83
Mathias Agopian65ab4712010-07-14 17:59:35 -070084protected:
85 // number of bits for phase fraction - 30 bits allows nearly 2x downsampling
86 static const int kNumPhaseBits = 30;
87
88 // phase mask for fraction
89 static const uint32_t kPhaseMask = (1LU<<kNumPhaseBits)-1;
90
91 // multiplier to calculate fixed point phase increment
Glenn Kasten01d3acb2014-02-06 08:24:07 -080092 static const double kPhaseMultiplier;
Mathias Agopian65ab4712010-07-14 17:59:35 -070093
Andy Hung3348e362014-07-07 10:21:44 -070094 AudioResampler(int inChannelCount, int32_t sampleRate, src_quality quality);
Mathias Agopian65ab4712010-07-14 17:59:35 -070095
96 // prevent copying
97 AudioResampler(const AudioResampler&);
98 AudioResampler& operator=(const AudioResampler&);
99
John Grossman4ff14ba2012-02-08 16:37:41 -0800100 int64_t calculateOutputPTS(int outputFrameIndex);
101
Glenn Kasten004f7192012-01-30 09:26:17 -0800102 const int32_t mChannelCount;
103 const int32_t mSampleRate;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700104 int32_t mInSampleRate;
105 AudioBufferProvider::Buffer mBuffer;
106 union {
107 int16_t mVolume[2];
108 uint32_t mVolumeRL;
109 };
110 int16_t mTargetVolume[2];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111 size_t mInputIndex;
112 int32_t mPhaseIncrement;
113 uint32_t mPhaseFraction;
John Grossman4ff14ba2012-02-08 16:37:41 -0800114 uint64_t mLocalTimeFreq;
115 int64_t mPTS;
Glenn Kastenac602052012-10-01 14:04:31 -0700116
Andy Hung24781ff2014-02-19 12:45:19 -0800117 // returns the inFrameCount required to generate outFrameCount frames.
118 //
119 // Placed here to be a consistent for all resamplers.
120 //
121 // Right now, we use the upper bound without regards to the current state of the
122 // input buffer using integer arithmetic, as follows:
123 //
124 // (static_cast<uint64_t>(outFrameCount)*mInSampleRate + (mSampleRate - 1))/mSampleRate;
125 //
126 // The double precision equivalent (float may not be precise enough):
127 // ceil(static_cast<double>(outFrameCount) * mInSampleRate / mSampleRate);
128 //
129 // this relies on the fact that the mPhaseIncrement is rounded down from
130 // #phases * mInSampleRate/mSampleRate and the fact that Sum(Floor(x)) <= Floor(Sum(x)).
131 // http://www.proofwiki.org/wiki/Sum_of_Floors_Not_Greater_Than_Floor_of_Sums
132 //
133 // (so long as double precision is computed accurately enough to be considered
134 // greater than or equal to the Floor(x) value in int32_t arithmetic; thus this
135 // will not necessarily hold for floats).
136 //
137 // TODO:
138 // Greater accuracy and a tight bound is obtained by:
139 // 1) subtract and adjust for the current state of the AudioBufferProvider buffer.
140 // 2) using the exact integer formula where (ignoring 64b casting)
141 // inFrameCount = (mPhaseIncrement * (outFrameCount - 1) + mPhaseFraction) / phaseWrapLimit;
142 // phaseWrapLimit is the wraparound (1 << kNumPhaseBits), if not specified explicitly.
143 //
144 inline size_t getInFrameCountRequired(size_t outFrameCount) {
145 return (static_cast<uint64_t>(outFrameCount)*mInSampleRate
146 + (mSampleRate - 1))/mSampleRate;
147 }
148
Andy Hung5e58b0a2014-06-23 19:07:29 -0700149 inline float clampFloatVol(float volume) {
150 if (volume > UNITY_GAIN_FLOAT) {
151 return UNITY_GAIN_FLOAT;
152 } else if (volume >= 0.) {
153 return volume;
154 }
155 return 0.; // NaN or negative volume maps to 0.
156 }
157
Glenn Kastenac602052012-10-01 14:04:31 -0700158private:
159 const src_quality mQuality;
160
161 // Return 'true' if the quality level is supported without explicit request
162 static bool qualityIsSupported(src_quality quality);
163
164 // For pthread_once()
165 static void init_routine();
166
167 // Return the estimated CPU load for specific resampler in MHz.
168 // The absolute number is irrelevant, it's the relative values that matter.
169 static uint32_t qualityMHz(src_quality quality);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170};
171
172// ----------------------------------------------------------------------------
173}
174; // namespace android
175
176#endif // ANDROID_AUDIO_RESAMPLER_H