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/callback.h" 9 #include "base/gtest_prod_util.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 15 namespace media { 16 17 // SincResampler is a high-quality single-channel sample-rate converter. 18 class MEDIA_EXPORT SincResampler { 19 public: 20 enum { 21 // The kernel size can be adjusted for quality (higher is better) at the 22 // expense of performance. Must be a multiple of 32. 23 // TODO(dalecurtis): Test performance to see if we can jack this up to 64+. 24 kKernelSize = 32, 25 26 // Default request size. Affects how often and for how much SincResampler 27 // calls back for input. Must be greater than kKernelSize. 28 kDefaultRequestSize = 512, 29 30 // 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) 32 // at the expense of allocating more memory. 33 kKernelOffsetCount = 32, 34 kKernelStorageSize = kKernelSize * (kKernelOffsetCount + 1), 35 }; 36 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 47 // acquire audio data for resampling. |io_sample_rate_ratio| is the ratio 48 // 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 50 // greater than kKernelSize. Specify kDefaultRequestSize if there are no 51 // request size constraints. 52 SincResampler(double io_sample_rate_ratio, 53 int request_frames, 54 const ReadCB& read_cb); 55 virtual ~SincResampler(); 56 57 // Resample |frames| of data from |read_cb_| into |destination|. 58 void Resample(int frames, float* destination); 59 60 // The maximum size in frames that guarantees Resample() will only make a 61 // single call to |read_cb_| for more data. 62 int ChunkSize() const; 63 64 // Flush all buffered data and reset internal indices. Not thread safe, do 65 // not call while Resample() is in progress. 66 void Flush(); 67 68 // Update |io_sample_rate_ratio_|. SetRatio() will cause a reconstruction of 69 // the kernels used for resampling. Not thread safe, do not call while 70 // Resample() is in progress. 71 void SetRatio(double io_sample_rate_ratio); 72 73 float* get_kernel_for_testing() { return kernel_storage_.get(); } 74 75 private: 76 FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, Convolve); 77 FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, ConvolveBenchmark); 78 79 void InitializeKernel(); 80 void UpdateRegions(bool second_load); 81 82 // Compute convolution of |k1| and |k2| over |input_ptr|, resultant sums are 83 // linearly interpolated using |kernel_interpolation_factor|. On x86, the 84 // 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. 86 static float Convolve_C(const float* input_ptr, const float* k1, 87 const float* k2, double kernel_interpolation_factor); 88 #if defined(ARCH_CPU_X86_FAMILY) 89 static float Convolve_SSE(const float* input_ptr, const float* k1, 90 const float* k2, 91 double kernel_interpolation_factor); 92 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON) 93 static float Convolve_NEON(const float* input_ptr, const float* k1, 94 const float* k2, 95 double kernel_interpolation_factor); 96 #endif 97 98 // The ratio of input / output sample rates. 99 double io_sample_rate_ratio_; 100 101 // An index on the source input buffer with sub-sample precision. It must be 102 // double precision to avoid drift. 103 double virtual_source_idx_; 104 105 // The buffer is primed once at the very beginning of processing. 106 bool buffer_primed_; 107 108 // Source of data for resampling. 109 const ReadCB read_cb_; 110 111 // The size (in samples) to request from each |read_cb_| execution. 112 const int request_frames_; 113 114 // The number of source frames processed per pass. 115 int block_size_; 116 117 // The size (in samples) of the internal buffer used by the resampler. 118 const int input_buffer_size_; 119 120 // Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize. 121 // The kernel offsets are sub-sample shifts of a windowed sinc shifted from 122 // 0.0 to 1.0 sample. 123 scoped_ptr<float[], base::ScopedPtrAlignedFree> kernel_storage_; 124 scoped_ptr<float[], base::ScopedPtrAlignedFree> kernel_pre_sinc_storage_; 125 scoped_ptr<float[], base::ScopedPtrAlignedFree> kernel_window_storage_; 126 127 // Data from the source is copied into this buffer for each processing pass. 128 scoped_ptr<float[], base::ScopedPtrAlignedFree> input_buffer_; 129 130 // Pointers to the various regions inside |input_buffer_|. See the diagram at 131 // the top of the .cc file for more information. 132 float* r0_; 133 float* const r1_; 134 float* const r2_; 135 float* r3_; 136 float* r4_; 137 138 DISALLOW_COPY_AND_ASSIGN(SincResampler); 139 }; 140 141 } // namespace media 142 143 #endif // MEDIA_BASE_SINC_RESAMPLER_H_ 144