sinc_resampler.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifndef MEDIA_BASE_SINC_RESAMPLER_H_
  5. #define MEDIA_BASE_SINC_RESAMPLER_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/gtest_prod_util.h"
  9. #include "base/memory/aligned_memory.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "build/build_config.h"
  12. #include "media/base/media_export.h"
  13. namespace media {
  14. // SincResampler is a high-quality single-channel sample-rate converter.
  15. class MEDIA_EXPORT SincResampler {
  16. public:
  17. // The kernel size can be adjusted for quality (higher is better) at the
  18. // expense of performance. Must be a multiple of 32.
  19. // TODO(dalecurtis): Test performance to see if we can jack this up to 64+.
  20. static constexpr int kKernelSize = 32;
  21. // Default request size. Affects how often and for how much SincResampler
  22. // calls back for input. Must be greater than kKernelSize.
  23. static constexpr int kDefaultRequestSize = 512;
  24. // The kernel offset count is used for interpolation and is the number of
  25. // sub-sample kernel shifts. Can be adjusted for quality (higher is better)
  26. // at the expense of allocating more memory.
  27. static constexpr int kKernelOffsetCount = 32;
  28. static constexpr int kKernelStorageSize =
  29. kKernelSize * (kKernelOffsetCount + 1);
  30. // Callback type for providing more data into the resampler. Expects |frames|
  31. // of data to be rendered into |destination|; zero padded if not enough frames
  32. // are available to satisfy the request.
  33. typedef base::RepeatingCallback<void(int frames, float* destination)> ReadCB;
  34. // Constructs a SincResampler with the specified |read_cb|, which is used to
  35. // acquire audio data for resampling. |io_sample_rate_ratio| is the ratio
  36. // of input / output sample rates. |request_frames| controls the size in
  37. // frames of the buffer requested by each |read_cb| call. The value must be
  38. // greater than 1.5*kKernelSize. Specify kDefaultRequestSize if there are no
  39. // request size constraints.
  40. SincResampler(double io_sample_rate_ratio,
  41. int request_frames,
  42. const ReadCB read_cb);
  43. SincResampler(const SincResampler&) = delete;
  44. SincResampler& operator=(const SincResampler&) = delete;
  45. ~SincResampler();
  46. // Resample |frames| of data from |read_cb_| into |destination|.
  47. void Resample(int frames, float* destination);
  48. // The maximum size in frames that guarantees Resample() will only make a
  49. // single call to |read_cb_| for more data. Note: If PrimeWithSilence() is
  50. // not called, chunk size will grow after the first two Resample() calls by
  51. // kKernelSize / (2 * io_sample_rate_ratio). See the .cc file for details.
  52. int ChunkSize() const { return chunk_size_; }
  53. // Returns the max number of frames that could be requested (via multiple
  54. // calls to |read_cb_|) during one Resample(|output_frames_requested|) call.
  55. int GetMaxInputFramesRequested(int output_frames_requested) const;
  56. // Guarantees that ChunkSize() will not change between calls by initializing
  57. // the input buffer with silence. Note, this will cause the first few samples
  58. // of output to be biased towards silence. Must be called again after Flush().
  59. void PrimeWithSilence();
  60. // Flush all buffered data and reset internal indices. Not thread safe, do
  61. // not call while Resample() is in progress. Note, if PrimeWithSilence() was
  62. // previously called it must be called again after the Flush().
  63. void Flush();
  64. // Update |io_sample_rate_ratio_|. SetRatio() will cause a reconstruction of
  65. // the kernels used for resampling. Not thread safe, do not call while
  66. // Resample() is in progress.
  67. void SetRatio(double io_sample_rate_ratio);
  68. float* get_kernel_for_testing() { return kernel_storage_.get(); }
  69. // Return number of input frames consumed by a callback but not yet processed.
  70. // Since input/output ratio can be fractional, so can this value.
  71. // Zero before first call to Resample().
  72. double BufferedFrames() const;
  73. private:
  74. FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, Convolve);
  75. FRIEND_TEST_ALL_PREFIXES(SincResamplerPerfTest, Convolve_unoptimized_aligned);
  76. FRIEND_TEST_ALL_PREFIXES(SincResamplerPerfTest, Convolve_optimized_aligned);
  77. FRIEND_TEST_ALL_PREFIXES(SincResamplerPerfTest, Convolve_optimized_unaligned);
  78. void InitializeKernel();
  79. void UpdateRegions(bool second_load);
  80. // Compute convolution of |k1| and |k2| over |input_ptr|, resultant sums are
  81. // linearly interpolated using |kernel_interpolation_factor|. On x86, the
  82. // 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.
  84. static float Convolve_C(const float* input_ptr, const float* k1,
  85. const float* k2, double kernel_interpolation_factor);
  86. #if defined(ARCH_CPU_X86_FAMILY)
  87. static float Convolve_SSE(const float* input_ptr, const float* k1,
  88. const float* k2,
  89. double kernel_interpolation_factor);
  90. static float Convolve_AVX2(const float* input_ptr,
  91. const float* k1,
  92. const float* k2,
  93. double kernel_interpolation_factor);
  94. #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
  95. static float Convolve_NEON(const float* input_ptr, const float* k1,
  96. const float* k2,
  97. double kernel_interpolation_factor);
  98. #endif
  99. // Selects runtime specific CPU features like SSE. Must be called before
  100. // using SincResampler.
  101. void InitializeCPUSpecificFeatures();
  102. // The ratio of input / output sample rates.
  103. double io_sample_rate_ratio_;
  104. // An index on the source input buffer with sub-sample precision. It must be
  105. // double precision to avoid drift.
  106. double virtual_source_idx_;
  107. // The buffer is primed once at the very beginning of processing.
  108. bool buffer_primed_;
  109. // Source of data for resampling.
  110. const ReadCB read_cb_;
  111. // The size (in samples) to request from each |read_cb_| execution.
  112. const int request_frames_;
  113. // The number of source frames processed per pass.
  114. int block_size_;
  115. // Cached value used for ChunkSize(). The maximum size in frames that
  116. // guarantees Resample() will only ask for input at most once.
  117. int chunk_size_;
  118. // The size (in samples) of the internal buffer used by the resampler.
  119. const int input_buffer_size_;
  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. std::unique_ptr<float[], base::AlignedFreeDeleter> kernel_storage_;
  124. std::unique_ptr<float[], base::AlignedFreeDeleter> kernel_pre_sinc_storage_;
  125. std::unique_ptr<float[], base::AlignedFreeDeleter> kernel_window_storage_;
  126. // Data from the source is copied into this buffer for each processing pass.
  127. std::unique_ptr<float[], base::AlignedFreeDeleter> input_buffer_;
  128. // Stores the runtime selection of which Convolve function to use.
  129. using ConvolveProc = float (*)(const float*,
  130. const float*,
  131. const float*,
  132. double);
  133. ConvolveProc convolve_proc_;
  134. // Pointers to the various regions inside |input_buffer_|. See the diagram at
  135. // the top of the .cc file for more information.
  136. raw_ptr<float> r0_;
  137. const raw_ptr<float> r1_;
  138. const raw_ptr<float> r2_;
  139. raw_ptr<float> r3_;
  140. raw_ptr<float> r4_;
  141. };
  142. } // namespace media
  143. #endif // MEDIA_BASE_SINC_RESAMPLER_H_