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 request 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 kDefaultRequestSize = 512, |
Chris Rogers
2013/05/07 22:57:53
Maybe we can reduce this down to at least 256 now?
DaleCurtis
2013/05/07 23:49:41
I'd like to do that in a different CL since I'm wo
| |
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(int frames, float* destination)> 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 |
52 // input / output sample rates. | 44 // of input / output sample rates. |request_frames| controls the size in |
53 SincResampler(double io_sample_rate_ratio, const ReadCB& read_cb); | 45 // frames of the buffer requested by each |read_cb| call. The value must be |
46 // greater than kKernelSize. Specify kDefaultRequestSize if there are no | |
47 // request size constraints. | |
48 SincResampler(double io_sample_rate_ratio, | |
49 size_t request_frames, | |
50 const ReadCB& read_cb); | |
54 virtual ~SincResampler(); | 51 virtual ~SincResampler(); |
55 | 52 |
56 // Resample |frames| of data from |read_cb_| into |destination|. | 53 // Resample |frames| of data from |read_cb_| into |destination|. |
57 void Resample(float* destination, int frames); | 54 void Resample(int frames, float* destination); |
58 | 55 |
59 // The maximum size in frames that guarantees Resample() will only make a | 56 // The maximum size in frames that guarantees Resample() will only make a |
60 // single call to |read_cb_| for more data. | 57 // single call to |read_cb_| for more data. |
61 int ChunkSize() const; | 58 int ChunkSize() const; |
62 | 59 |
63 // Flush all buffered data and reset internal indices. Not thread safe, do | 60 // Flush all buffered data and reset internal indices. Not thread safe, do |
64 // not call while Resample() is in progress. | 61 // not call while Resample() is in progress. |
65 void Flush(); | 62 void Flush(); |
66 | 63 |
67 // Update |io_sample_rate_ratio_|. SetRatio() will cause a reconstruction of | 64 // Update |io_sample_rate_ratio_|. SetRatio() will cause a reconstruction of |
68 // the kernels used for resampling. Not thread safe, do not call while | 65 // the kernels used for resampling. Not thread safe, do not call while |
69 // Resample() is in progress. | 66 // Resample() is in progress. |
70 void SetRatio(double io_sample_rate_ratio); | 67 void SetRatio(double io_sample_rate_ratio); |
71 | 68 |
72 float* get_kernel_for_testing() { return kernel_storage_.get(); } | 69 float* get_kernel_for_testing() { return kernel_storage_.get(); } |
73 | 70 |
74 private: | 71 private: |
75 FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, Convolve); | 72 FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, Convolve); |
76 FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, ConvolveBenchmark); | 73 FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, ConvolveBenchmark); |
77 | 74 |
78 void InitializeKernel(); | 75 void InitializeKernel(); |
76 void UpdateRegions(bool second_load); | |
79 | 77 |
80 // Compute convolution of |k1| and |k2| over |input_ptr|, resultant sums are | 78 // Compute convolution of |k1| and |k2| over |input_ptr|, resultant sums are |
81 // linearly interpolated using |kernel_interpolation_factor|. On x86, the | 79 // linearly interpolated using |kernel_interpolation_factor|. On x86, the |
82 // underlying implementation is chosen at run time based on SSE support. On | 80 // 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. | 81 // ARM, NEON support is chosen at compile time based on compilation flags. |
84 static float Convolve_C(const float* input_ptr, const float* k1, | 82 static float Convolve_C(const float* input_ptr, const float* k1, |
85 const float* k2, double kernel_interpolation_factor); | 83 const float* k2, double kernel_interpolation_factor); |
86 #if defined(ARCH_CPU_X86_FAMILY) | 84 #if defined(ARCH_CPU_X86_FAMILY) |
87 static float Convolve_SSE(const float* input_ptr, const float* k1, | 85 static float Convolve_SSE(const float* input_ptr, const float* k1, |
88 const float* k2, | 86 const float* k2, |
89 double kernel_interpolation_factor); | 87 double kernel_interpolation_factor); |
90 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON) | 88 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON) |
91 static float Convolve_NEON(const float* input_ptr, const float* k1, | 89 static float Convolve_NEON(const float* input_ptr, const float* k1, |
92 const float* k2, | 90 const float* k2, |
93 double kernel_interpolation_factor); | 91 double kernel_interpolation_factor); |
94 #endif | 92 #endif |
95 | 93 |
96 // The ratio of input / output sample rates. | 94 // The ratio of input / output sample rates. |
97 double io_sample_rate_ratio_; | 95 double io_sample_rate_ratio_; |
98 | 96 |
99 // An index on the source input buffer with sub-sample precision. It must be | 97 // An index on the source input buffer with sub-sample precision. It must be |
100 // double precision to avoid drift. | 98 // double precision to avoid drift. |
101 double virtual_source_idx_; | 99 double virtual_source_idx_; |
102 | 100 |
103 // The buffer is primed once at the very beginning of processing. | 101 // The buffer is primed once at the very beginning of processing. |
104 bool buffer_primed_; | 102 bool buffer_primed_; |
105 | 103 |
106 // Source of data for resampling. | 104 // Source of data for resampling. |
107 ReadCB read_cb_; | 105 const ReadCB read_cb_; |
106 | |
107 // The size (in samples) to request from each |read_cb_| execution. | |
108 const size_t request_frames_; | |
109 | |
110 // The number of source frames processed per pass. | |
111 size_t block_size_; | |
112 | |
113 // The size (in samples) of the internal buffer used by the resampler. | |
114 const size_t input_buffer_size_; | |
108 | 115 |
109 // Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize. | 116 // Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize. |
110 // The kernel offsets are sub-sample shifts of a windowed sinc shifted from | 117 // The kernel offsets are sub-sample shifts of a windowed sinc shifted from |
111 // 0.0 to 1.0 sample. | 118 // 0.0 to 1.0 sample. |
112 scoped_ptr<float[], base::ScopedPtrAlignedFree> kernel_storage_; | 119 scoped_ptr<float[], base::ScopedPtrAlignedFree> kernel_storage_; |
113 scoped_ptr<float[], base::ScopedPtrAlignedFree> kernel_pre_sinc_storage_; | 120 scoped_ptr<float[], base::ScopedPtrAlignedFree> kernel_pre_sinc_storage_; |
114 scoped_ptr<float[], base::ScopedPtrAlignedFree> kernel_window_storage_; | 121 scoped_ptr<float[], base::ScopedPtrAlignedFree> kernel_window_storage_; |
115 | 122 |
116 // Data from the source is copied into this buffer for each processing pass. | 123 // Data from the source is copied into this buffer for each processing pass. |
117 scoped_ptr<float[], base::ScopedPtrAlignedFree> input_buffer_; | 124 scoped_ptr<float[], base::ScopedPtrAlignedFree> input_buffer_; |
118 | 125 |
119 // Stores the runtime selection of which Convolve function to use. | 126 // Stores the runtime selection of which Convolve function to use. |
120 #if defined(ARCH_CPU_X86_FAMILY) && !defined(__SSE__) | 127 #if defined(ARCH_CPU_X86_FAMILY) && !defined(__SSE__) |
121 typedef float (*ConvolveProc)(const float*, const float*, const float*, | 128 typedef float (*ConvolveProc)(const float*, const float*, const float*, |
122 double); | 129 double); |
123 const ConvolveProc convolve_proc_; | 130 const ConvolveProc convolve_proc_; |
124 #endif | 131 #endif |
125 | 132 |
126 // Pointers to the various regions inside |input_buffer_|. See the diagram at | 133 // Pointers to the various regions inside |input_buffer_|. See the diagram at |
127 // the top of the .cc file for more information. | 134 // the top of the .cc file for more information. |
128 float* const r0_; | 135 float* r0_; |
129 float* const r1_; | 136 float* const r1_; |
130 float* const r2_; | 137 float* const r2_; |
131 float* const r3_; | 138 float* r3_; |
132 float* const r4_; | 139 float* r4_; |
133 float* const r5_; | |
134 | 140 |
135 DISALLOW_COPY_AND_ASSIGN(SincResampler); | 141 DISALLOW_COPY_AND_ASSIGN(SincResampler); |
136 }; | 142 }; |
137 | 143 |
138 } // namespace media | 144 } // namespace media |
139 | 145 |
140 #endif // MEDIA_BASE_SINC_RESAMPLER_H_ | 146 #endif // MEDIA_BASE_SINC_RESAMPLER_H_ |
OLD | NEW |