1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MEDIA_BASE_SINC_RESAMPLER_H_ 6 #define MEDIA_BASE_SINC_RESAMPLER_H_ 7 8 #include "base/atomic_ref_count.h" 9 #include "base/callback.h" 10 #include "base/gtest_prod_util.h" 11 #include "base/memory/aligned_memory.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "build/build_config.h" 14 #include "media/base/media_export.h" 15 16 namespace media { 17 18 // SincResampler is a high-quality single-channel sample-rate converter. 19 class MEDIA_EXPORT SincResampler { 20 public: 21 enum { 22 // The kernel size can be adjusted for quality (higher is better) at the 23 // expense of performance. Must be a multiple of 32. 24 // TODO(dalecurtis): Test performance to see if we can jack this up to 64+. 25 kKernelSize = 32, 26 27 // Default request size. Affects how often and for how much SincResampler 28 // calls back for input. Must be greater than kKernelSize. 29 kDefaultRequestSize = 512, 30 31 // The kernel offset count is used for interpolation and is the number of 32 // sub-sample kernel shifts. Can be adjusted for quality (higher is better) 33 // at the expense of allocating more memory. 34 kKernelOffsetCount = 32, 35 kKernelStorageSize = kKernelSize * (kKernelOffsetCount + 1), 36 }; 37 38 // Selects runtime specific CPU features like SSE. Must be called before 39 // using SincResampler. 40 static void InitializeCPUSpecificFeatures(); 41 42 // Callback type for providing more data into the resampler. Expects |frames| 43 // of data to be rendered into |destination|; zero padded if not enough frames 44 // are available to satisfy the request. 45 typedef base::Callback<void(int frames, float* destination)> ReadCB; 46 47 // Constructs a SincResampler with the specified |read_cb|, which is used to 48 // acquire audio data for resampling. |io_sample_rate_ratio| is the ratio 49 // of input / output sample rates. |request_frames| controls the size in 50 // frames of the buffer requested by each |read_cb| call. The value must be 51 // greater than kKernelSize. Specify kDefaultRequestSize if there are no 52 // request size constraints. 53 SincResampler(double io_sample_rate_ratio, 54 int request_frames, 55 const ReadCB& read_cb); 56 virtual ~SincResampler(); 57 58 // Resample |frames| of data from |read_cb_| into |destination|. 59 void Resample(int frames, float* destination); 60 61 // The maximum size in frames that guarantees Resample() will only make a 62 // single call to |read_cb_| for more data. 63 int ChunkSize() const; 64 65 // Flush all buffered data and reset internal indices. Not thread safe, do 66 // not call while Resample() is in progress. 67 void Flush(); 68 69 // Update |io_sample_rate_ratio_|. SetRatio() will cause a reconstruction of 70 // the kernels used for resampling. Not thread safe, do not call while 71 // Resample() is in progress. 72 void SetRatio(double io_sample_rate_ratio); 73 74 float* get_kernel_for_testing() { return kernel_storage_.get(); } 75 76 private: 77 FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, Convolve); 78 FRIEND_TEST_ALL_PREFIXES(SincResamplerPerfTest, Convolve); 79 80 void InitializeKernel(); 81 void UpdateRegions(bool second_load); 82 83 // Compute convolution of |k1| and |k2| over |input_ptr|, resultant sums are 84 // linearly interpolated using |kernel_interpolation_factor|. On x86, the 85 // underlying implementation is chosen at run time based on SSE support. On 86 // ARM, NEON support is chosen at compile time based on compilation flags. 87 static float Convolve_C(const float* input_ptr, const float* k1, 88 const float* k2, double kernel_interpolation_factor); 89 #if defined(ARCH_CPU_X86_FAMILY) 90 static float Convolve_SSE(const float* input_ptr, const float* k1, 91 const float* k2, 92 double kernel_interpolation_factor); 93 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON) 94 static float Convolve_NEON(const float* input_ptr, const float* k1, 95 const float* k2, 96 double kernel_interpolation_factor); 97 #endif 98 99 // The ratio of input / output sample rates. 100 double io_sample_rate_ratio_; 101 102 // An index on the source input buffer with sub-sample precision. It must be 103 // double precision to avoid drift. 104 double virtual_source_idx_; 105 106 // The buffer is primed once at the very beginning of processing. 107 bool buffer_primed_; 108 109 // Source of data for resampling. 110 const ReadCB read_cb_; 111 112 // The size (in samples) to request from each |read_cb_| execution. 113 const int request_frames_; 114 115 // The number of source frames processed per pass. 116 int block_size_; 117 118 // The size (in samples) of the internal buffer used by the resampler. 119 const int input_buffer_size_; 120 121 // Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize. 122 // The kernel offsets are sub-sample shifts of a windowed sinc shifted from 123 // 0.0 to 1.0 sample. 124 scoped_ptr<float[], base::ScopedPtrAlignedFree> kernel_storage_; 125 scoped_ptr<float[], base::ScopedPtrAlignedFree> kernel_pre_sinc_storage_; 126 scoped_ptr<float[], base::ScopedPtrAlignedFree> kernel_window_storage_; 127 128 // Data from the source is copied into this buffer for each processing pass. 129 scoped_ptr<float[], base::ScopedPtrAlignedFree> input_buffer_; 130 131 // Pointers to the various regions inside |input_buffer_|. See the diagram at 132 // the top of the .cc file for more information. 133 float* r0_; 134 float* const r1_; 135 float* const r2_; 136 float* r3_; 137 float* r4_; 138 139 // Atomic ref count indicating when when we're in the middle of resampling. 140 // Will be CHECK'd to find crashes... 141 // TODO(dalecurtis): Remove debug helpers for http://crbug.com/295278 142 base::AtomicRefCount currently_resampling_; 143 144 DISALLOW_COPY_AND_ASSIGN(SincResampler); 145 }; 146 147 } // namespace media 148 149 #endif // MEDIA_BASE_SINC_RESAMPLER_H_ 150