[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / media / base / sinc_resampler.h
blobe12f3814264007a47407e4bc82a3280676a7aa9e
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.
5 #ifndef MEDIA_BASE_SINC_RESAMPLER_H_
6 #define MEDIA_BASE_SINC_RESAMPLER_H_
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"
15 namespace media {
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,
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,
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),
37 // Callback type for providing more data into the resampler. Expects |frames|
38 // of data to be rendered into |destination|; zero padded if not enough frames
39 // are available to satisfy the request.
40 typedef base::Callback<void(int frames, float* destination)> ReadCB;
42 // Constructs a SincResampler with the specified |read_cb|, which is used to
43 // acquire audio data for resampling. |io_sample_rate_ratio| is the ratio
44 // of input / output sample rates. |request_frames| controls the size in
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 int request_frames,
50 const ReadCB& read_cb);
51 ~SincResampler();
53 // Resample |frames| of data from |read_cb_| into |destination|.
54 void Resample(int frames, float* destination);
56 // The maximum size in frames that guarantees Resample() will only make a
57 // single call to |read_cb_| for more data. Note: If PrimeWithSilence() is
58 // not called, chunk size will grow after the first two Resample() calls by
59 // kKernelSize / (2 * io_sample_rate_ratio). See the .cc file for details.
60 int ChunkSize() const { return chunk_size_; }
62 // Guarantees that ChunkSize() will not change between calls by initializing
63 // the input buffer with silence. Note, this will cause the first few samples
64 // of output to be biased towards silence. Must be called again after Flush().
65 void PrimeWithSilence();
67 // Flush all buffered data and reset internal indices. Not thread safe, do
68 // not call while Resample() is in progress. Note, if PrimeWithSilence() was
69 // previously called it must be called again after the Flush().
70 void Flush();
72 // Update |io_sample_rate_ratio_|. SetRatio() will cause a reconstruction of
73 // the kernels used for resampling. Not thread safe, do not call while
74 // Resample() is in progress.
75 void SetRatio(double io_sample_rate_ratio);
77 float* get_kernel_for_testing() { return kernel_storage_.get(); }
79 // Return number of input frames consumed by a callback but not yet processed.
80 // Since input/output ratio can be fractional, so can this value.
81 // Zero before first call to Resample().
82 double BufferedFrames() const;
84 private:
85 FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, Convolve);
86 FRIEND_TEST_ALL_PREFIXES(SincResamplerPerfTest, Convolve);
88 void InitializeKernel();
89 void UpdateRegions(bool second_load);
91 // Compute convolution of |k1| and |k2| over |input_ptr|, resultant sums are
92 // linearly interpolated using |kernel_interpolation_factor|. On x86, the
93 // underlying implementation is chosen at run time based on SSE support. On
94 // ARM, NEON support is chosen at compile time based on compilation flags.
95 static float Convolve_C(const float* input_ptr, const float* k1,
96 const float* k2, double kernel_interpolation_factor);
97 #if defined(ARCH_CPU_X86_FAMILY)
98 static float Convolve_SSE(const float* input_ptr, const float* k1,
99 const float* k2,
100 double kernel_interpolation_factor);
101 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
102 static float Convolve_NEON(const float* input_ptr, const float* k1,
103 const float* k2,
104 double kernel_interpolation_factor);
105 #endif
107 // The ratio of input / output sample rates.
108 double io_sample_rate_ratio_;
110 // An index on the source input buffer with sub-sample precision. It must be
111 // double precision to avoid drift.
112 double virtual_source_idx_;
114 // The buffer is primed once at the very beginning of processing.
115 bool buffer_primed_;
117 // Source of data for resampling.
118 const ReadCB read_cb_;
120 // The size (in samples) to request from each |read_cb_| execution.
121 const int request_frames_;
123 // The number of source frames processed per pass.
124 int block_size_;
126 // Cached value used for ChunkSize(). The maximum size in frames that
127 // guarantees Resample() will only ask for input at most once.
128 int chunk_size_;
130 // The size (in samples) of the internal buffer used by the resampler.
131 const int input_buffer_size_;
133 // Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize.
134 // The kernel offsets are sub-sample shifts of a windowed sinc shifted from
135 // 0.0 to 1.0 sample.
136 scoped_ptr<float[], base::AlignedFreeDeleter> kernel_storage_;
137 scoped_ptr<float[], base::AlignedFreeDeleter> kernel_pre_sinc_storage_;
138 scoped_ptr<float[], base::AlignedFreeDeleter> kernel_window_storage_;
140 // Data from the source is copied into this buffer for each processing pass.
141 scoped_ptr<float[], base::AlignedFreeDeleter> input_buffer_;
143 // Pointers to the various regions inside |input_buffer_|. See the diagram at
144 // the top of the .cc file for more information.
145 float* r0_;
146 float* const r1_;
147 float* const r2_;
148 float* r3_;
149 float* r4_;
151 DISALLOW_COPY_AND_ASSIGN(SincResampler);
154 } // namespace media
156 #endif // MEDIA_BASE_SINC_RESAMPLER_H_