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