• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 // Modified from the Chromium original here:
12 // src/media/base/sinc_resampler.h
13 
14 #ifndef WEBRTC_COMMON_AUDIO_RESAMPLER_SINC_RESAMPLER_H_
15 #define WEBRTC_COMMON_AUDIO_RESAMPLER_SINC_RESAMPLER_H_
16 
17 #include "webrtc/base/constructormagic.h"
18 #include "webrtc/base/scoped_ptr.h"
19 #include "webrtc/system_wrappers/include/aligned_malloc.h"
20 #include "webrtc/test/testsupport/gtest_prod_util.h"
21 #include "webrtc/typedefs.h"
22 
23 namespace webrtc {
24 
25 // Callback class for providing more data into the resampler.  Expects |frames|
26 // of data to be rendered into |destination|; zero padded if not enough frames
27 // are available to satisfy the request.
28 class SincResamplerCallback {
29  public:
~SincResamplerCallback()30   virtual ~SincResamplerCallback() {}
31   virtual void Run(size_t frames, float* destination) = 0;
32 };
33 
34 // SincResampler is a high-quality single-channel sample-rate converter.
35 class SincResampler {
36  public:
37   // The kernel size can be adjusted for quality (higher is better) at the
38   // expense of performance.  Must be a multiple of 32.
39   // TODO(dalecurtis): Test performance to see if we can jack this up to 64+.
40   static const size_t kKernelSize = 32;
41 
42   // Default request size.  Affects how often and for how much SincResampler
43   // calls back for input.  Must be greater than kKernelSize.
44   static const size_t kDefaultRequestSize = 512;
45 
46   // The kernel offset count is used for interpolation and is the number of
47   // sub-sample kernel shifts.  Can be adjusted for quality (higher is better)
48   // at the expense of allocating more memory.
49   static const size_t kKernelOffsetCount = 32;
50   static const size_t kKernelStorageSize =
51       kKernelSize * (kKernelOffsetCount + 1);
52 
53   // Constructs a SincResampler with the specified |read_cb|, which is used to
54   // acquire audio data for resampling.  |io_sample_rate_ratio| is the ratio
55   // of input / output sample rates.  |request_frames| controls the size in
56   // frames of the buffer requested by each |read_cb| call.  The value must be
57   // greater than kKernelSize.  Specify kDefaultRequestSize if there are no
58   // request size constraints.
59   SincResampler(double io_sample_rate_ratio,
60                 size_t request_frames,
61                 SincResamplerCallback* read_cb);
62   virtual ~SincResampler();
63 
64   // Resample |frames| of data from |read_cb_| into |destination|.
65   void Resample(size_t frames, float* destination);
66 
67   // The maximum size in frames that guarantees Resample() will only make a
68   // single call to |read_cb_| for more data.
69   size_t ChunkSize() const;
70 
request_frames()71   size_t request_frames() const { return request_frames_; }
72 
73   // Flush all buffered data and reset internal indices.  Not thread safe, do
74   // not call while Resample() is in progress.
75   void Flush();
76 
77   // Update |io_sample_rate_ratio_|.  SetRatio() will cause a reconstruction of
78   // the kernels used for resampling.  Not thread safe, do not call while
79   // Resample() is in progress.
80   //
81   // TODO(ajm): Use this in PushSincResampler rather than reconstructing
82   // SincResampler.  We would also need a way to update |request_frames_|.
83   void SetRatio(double io_sample_rate_ratio);
84 
get_kernel_for_testing()85   float* get_kernel_for_testing() { return kernel_storage_.get(); }
86 
87  private:
88   FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, Convolve);
89   FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, ConvolveBenchmark);
90 
91   void InitializeKernel();
92   void UpdateRegions(bool second_load);
93 
94   // Selects runtime specific CPU features like SSE.  Must be called before
95   // using SincResampler.
96   // TODO(ajm): Currently managed by the class internally. See the note with
97   // |convolve_proc_| below.
98   void InitializeCPUSpecificFeatures();
99 
100   // Compute convolution of |k1| and |k2| over |input_ptr|, resultant sums are
101   // linearly interpolated using |kernel_interpolation_factor|.  On x86 and ARM
102   // the underlying implementation is chosen at run time.
103   static float Convolve_C(const float* input_ptr, const float* k1,
104                           const float* k2, double kernel_interpolation_factor);
105 #if defined(WEBRTC_ARCH_X86_FAMILY)
106   static float Convolve_SSE(const float* input_ptr, const float* k1,
107                             const float* k2,
108                             double kernel_interpolation_factor);
109 #elif defined(WEBRTC_DETECT_NEON) || defined(WEBRTC_HAS_NEON)
110   static float Convolve_NEON(const float* input_ptr, const float* k1,
111                              const float* k2,
112                              double kernel_interpolation_factor);
113 #endif
114 
115   // The ratio of input / output sample rates.
116   double io_sample_rate_ratio_;
117 
118   // An index on the source input buffer with sub-sample precision.  It must be
119   // double precision to avoid drift.
120   double virtual_source_idx_;
121 
122   // The buffer is primed once at the very beginning of processing.
123   bool buffer_primed_;
124 
125   // Source of data for resampling.
126   SincResamplerCallback* read_cb_;
127 
128   // The size (in samples) to request from each |read_cb_| execution.
129   const size_t request_frames_;
130 
131   // The number of source frames processed per pass.
132   size_t block_size_;
133 
134   // The size (in samples) of the internal buffer used by the resampler.
135   const size_t input_buffer_size_;
136 
137   // Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize.
138   // The kernel offsets are sub-sample shifts of a windowed sinc shifted from
139   // 0.0 to 1.0 sample.
140   rtc::scoped_ptr<float[], AlignedFreeDeleter> kernel_storage_;
141   rtc::scoped_ptr<float[], AlignedFreeDeleter> kernel_pre_sinc_storage_;
142   rtc::scoped_ptr<float[], AlignedFreeDeleter> kernel_window_storage_;
143 
144   // Data from the source is copied into this buffer for each processing pass.
145   rtc::scoped_ptr<float[], AlignedFreeDeleter> input_buffer_;
146 
147   // Stores the runtime selection of which Convolve function to use.
148   // TODO(ajm): Move to using a global static which must only be initialized
149   // once by the user. We're not doing this initially, because we don't have
150   // e.g. a LazyInstance helper in webrtc.
151 #if defined(WEBRTC_CPU_DETECTION)
152   typedef float (*ConvolveProc)(const float*, const float*, const float*,
153                                 double);
154   ConvolveProc convolve_proc_;
155 #endif
156 
157   // Pointers to the various regions inside |input_buffer_|.  See the diagram at
158   // the top of the .cc file for more information.
159   float* r0_;
160   float* const r1_;
161   float* const r2_;
162   float* r3_;
163   float* r4_;
164 
165   RTC_DISALLOW_COPY_AND_ASSIGN(SincResampler);
166 };
167 
168 }  // namespace webrtc
169 
170 #endif  // WEBRTC_COMMON_AUDIO_RESAMPLER_SINC_RESAMPLER_H_
171