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