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" |
| 10 #include "base/memory/aligned_memory.h" |
9 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "build/build_config.h" |
10 #include "media/base/media_export.h" | 13 #include "media/base/media_export.h" |
11 | 14 |
12 namespace media { | 15 namespace media { |
13 | 16 |
14 // SincResampler is a high-quality single-channel sample-rate converter. | 17 // SincResampler is a high-quality single-channel sample-rate converter. |
15 class MEDIA_EXPORT SincResampler { | 18 class MEDIA_EXPORT SincResampler { |
16 public: | 19 public: |
17 // Callback type for providing more data into the resampler. Expects |frames| | 20 // Callback type for providing more data into the resampler. Expects |frames| |
18 // of data to be rendered into |destination|; zero padded if not enough frames | 21 // of data to be rendered into |destination|; zero padded if not enough frames |
19 // are available to satisfy the request. | 22 // are available to satisfy the request. |
20 typedef base::Callback<void(float* destination, int frames)> ReadCB; | 23 typedef base::Callback<void(float* destination, int frames)> ReadCB; |
21 | 24 |
22 // Constructs a SincResampler with the specified |read_cb|, which is used to | 25 // Constructs a SincResampler with the specified |read_cb|, which is used to |
23 // acquire audio data for resampling. |io_sample_rate_ratio| is the ratio of | 26 // acquire audio data for resampling. |io_sample_rate_ratio| is the ratio of |
24 // input / output sample rates. | 27 // input / output sample rates. |
25 SincResampler(double io_sample_rate_ratio, const ReadCB& read_cb); | 28 SincResampler(double io_sample_rate_ratio, const ReadCB& read_cb); |
26 virtual ~SincResampler(); | 29 virtual ~SincResampler(); |
27 | 30 |
28 // Resample |frames| of data from |read_cb_| into |destination|. | 31 // Resample |frames| of data from |read_cb_| into |destination|. |
29 void Resample(float* destination, int frames); | 32 void Resample(float* destination, int frames); |
30 | 33 |
31 // The maximum size in frames that guarantees Resample() will only make a | 34 // The maximum size in frames that guarantees Resample() will only make a |
32 // single call to |read_cb_| for more data. | 35 // single call to |read_cb_| for more data. |
33 int ChunkSize(); | 36 int ChunkSize(); |
34 | 37 |
35 private: | 38 private: |
| 39 FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, Convolve); |
| 40 FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, ConvolveBenchmark); |
| 41 |
36 void InitializeKernel(); | 42 void InitializeKernel(); |
37 | 43 |
| 44 // Compute convolution of |k1| and |k2| over |input_ptr|, resultant sums are |
| 45 // linearly interpolated using |kernel_interpolation_factor|. The underlying |
| 46 // implementation is chosen at run time based on SSE support. |
| 47 static float Convolve(const float* input_ptr, const float* k1, |
| 48 const float* k2, double kernel_interpolation_factor); |
| 49 static float Convolve_C(const float* input_ptr, const float* k1, |
| 50 const float* k2, double kernel_interpolation_factor); |
| 51 static float Convolve_SSE(const float* input_ptr, const float* k1, |
| 52 const float* k2, |
| 53 double kernel_interpolation_factor); |
| 54 |
38 // The ratio of input / output sample rates. | 55 // The ratio of input / output sample rates. |
39 double io_sample_rate_ratio_; | 56 double io_sample_rate_ratio_; |
40 | 57 |
41 // An index on the source input buffer with sub-sample precision. It must be | 58 // An index on the source input buffer with sub-sample precision. It must be |
42 // double precision to avoid drift. | 59 // double precision to avoid drift. |
43 double virtual_source_idx_; | 60 double virtual_source_idx_; |
44 | 61 |
45 // The buffer is primed once at the very beginning of processing. | 62 // The buffer is primed once at the very beginning of processing. |
46 bool buffer_primed_; | 63 bool buffer_primed_; |
47 | 64 |
48 // Source of data for resampling. | 65 // Source of data for resampling. |
49 ReadCB read_cb_; | 66 ReadCB read_cb_; |
50 | 67 |
51 // Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize. | 68 // Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize. |
52 // The kernel offsets are sub-sample shifts of a windowed sinc shifted from | 69 // The kernel offsets are sub-sample shifts of a windowed sinc shifted from |
53 // 0.0 to 1.0 sample. | 70 // 0.0 to 1.0 sample. |
54 scoped_array<float> kernel_storage_; | 71 scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> kernel_storage_; |
55 | 72 |
56 // Data from the source is copied into this buffer for each processing pass. | 73 // Data from the source is copied into this buffer for each processing pass. |
57 scoped_array<float> input_buffer_; | 74 scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> input_buffer_; |
58 | 75 |
59 // Pointers to the various regions inside |input_buffer_|. See the diagram at | 76 // Pointers to the various regions inside |input_buffer_|. See the diagram at |
60 // the top of the .cc file for more information. | 77 // the top of the .cc file for more information. |
61 float* const r0_; | 78 float* const r0_; |
62 float* const r1_; | 79 float* const r1_; |
63 float* const r2_; | 80 float* const r2_; |
64 float* const r3_; | 81 float* const r3_; |
65 float* const r4_; | 82 float* const r4_; |
66 float* const r5_; | 83 float* const r5_; |
67 }; | 84 }; |
68 | 85 |
69 } // namespace media | 86 } // namespace media |
70 | 87 |
71 #endif // MEDIA_BASE_SINC_RESAMPLER_H_ | 88 #endif // MEDIA_BASE_SINC_RESAMPLER_H_ |
OLD | NEW |