blob: c34132546f70ff23672f05e180b11ceb42e034ff [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>
Mathias Agopiane762be92013-05-09 16:26:45 -070022#include <cutils/compiler.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070023
Glenn Kasten2dd4bdd2012-08-29 11:10:32 -070024#include <media/AudioBufferProvider.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025
26namespace android {
27// ----------------------------------------------------------------------------
28
Mathias Agopiane762be92013-05-09 16:26:45 -070029class ANDROID_API AudioResampler {
Mathias Agopian65ab4712010-07-14 17:59:35 -070030public:
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 Kastene53b9ea2012-03-12 16:29:55 -070037 // changed dynamically.
Mathias Agopian65ab4712010-07-14 17:59:35 -070038 enum src_quality {
Glenn Kastenac602052012-10-01 14:04:31 -070039 DEFAULT_QUALITY=0,
Mathias Agopian65ab4712010-07-14 17:59:35 -070040 LOW_QUALITY=1,
41 MED_QUALITY=2,
SathishKumar Mani76b11162012-01-17 10:49:47 -080042 HIGH_QUALITY=3,
Glenn Kastenac602052012-10-01 14:04:31 -070043 VERY_HIGH_QUALITY=4,
Andy Hung86eae0e2013-12-09 12:12:46 -080044 DYN_LOW_QUALITY=5,
45 DYN_MED_QUALITY=6,
46 DYN_HIGH_QUALITY=7,
Mathias Agopian65ab4712010-07-14 17:59:35 -070047 };
48
49 static AudioResampler* create(int bitDepth, int inChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -070050 int32_t sampleRate, src_quality quality=DEFAULT_QUALITY);
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
52 virtual ~AudioResampler();
53
54 virtual void init() = 0;
55 virtual void setSampleRate(int32_t inSampleRate);
56 virtual void setVolume(int16_t left, int16_t right);
John Grossman4ff14ba2012-02-08 16:37:41 -080057 virtual void setLocalTimeFreq(uint64_t freq);
58
59 // set the PTS of the next buffer output by the resampler
60 virtual void setPTS(int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -070061
Glenn Kasten34af0262013-07-30 11:52:39 -070062 // Resample int16_t samples from provider and accumulate into 'out'.
63 // A mono provider delivers a sequence of samples.
64 // A stereo provider delivers a sequence of interleaved pairs of samples.
65 // Multi-channel providers are not supported.
66 // In either case, 'out' holds interleaved pairs of fixed-point signed Q19.12.
67 // That is, for a mono provider, there is an implicit up-channeling.
68 // Since this method accumulates, the caller is responsible for clearing 'out' initially.
69 // FIXME assumes provider is always successful; it should return the actual frame count.
Mathias Agopian65ab4712010-07-14 17:59:35 -070070 virtual void resample(int32_t* out, size_t outFrameCount,
71 AudioBufferProvider* provider) = 0;
72
Eric Laurent243f5f92011-02-28 16:52:51 -080073 virtual void reset();
Glenn Kastenc59c0042012-02-02 14:06:11 -080074 virtual size_t getUnreleasedFrames() const { return mInputIndex; }
Eric Laurent243f5f92011-02-28 16:52:51 -080075
Glenn Kastenac602052012-10-01 14:04:31 -070076 // called from destructor, so must not be virtual
77 src_quality getQuality() const { return mQuality; }
78
Mathias Agopian65ab4712010-07-14 17:59:35 -070079protected:
80 // number of bits for phase fraction - 30 bits allows nearly 2x downsampling
81 static const int kNumPhaseBits = 30;
82
83 // phase mask for fraction
84 static const uint32_t kPhaseMask = (1LU<<kNumPhaseBits)-1;
85
86 // multiplier to calculate fixed point phase increment
87 static const double kPhaseMultiplier = 1L << kNumPhaseBits;
88
Glenn Kastenac602052012-10-01 14:04:31 -070089 AudioResampler(int bitDepth, int inChannelCount, int32_t sampleRate, src_quality quality);
Mathias Agopian65ab4712010-07-14 17:59:35 -070090
91 // prevent copying
92 AudioResampler(const AudioResampler&);
93 AudioResampler& operator=(const AudioResampler&);
94
John Grossman4ff14ba2012-02-08 16:37:41 -080095 int64_t calculateOutputPTS(int outputFrameIndex);
96
Glenn Kasten004f7192012-01-30 09:26:17 -080097 const int32_t mBitDepth;
98 const int32_t mChannelCount;
99 const int32_t mSampleRate;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700100 int32_t mInSampleRate;
101 AudioBufferProvider::Buffer mBuffer;
102 union {
103 int16_t mVolume[2];
104 uint32_t mVolumeRL;
105 };
106 int16_t mTargetVolume[2];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700107 size_t mInputIndex;
108 int32_t mPhaseIncrement;
109 uint32_t mPhaseFraction;
John Grossman4ff14ba2012-02-08 16:37:41 -0800110 uint64_t mLocalTimeFreq;
111 int64_t mPTS;
Glenn Kastenac602052012-10-01 14:04:31 -0700112
113private:
114 const src_quality mQuality;
115
116 // Return 'true' if the quality level is supported without explicit request
117 static bool qualityIsSupported(src_quality quality);
118
119 // For pthread_once()
120 static void init_routine();
121
122 // Return the estimated CPU load for specific resampler in MHz.
123 // The absolute number is irrelevant, it's the relative values that matter.
124 static uint32_t qualityMHz(src_quality quality);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700125};
126
127// ----------------------------------------------------------------------------
128}
129; // namespace android
130
131#endif // ANDROID_AUDIO_RESAMPLER_H