Home | History | Annotate | Download | only in base
      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 
      5 #ifndef MEDIA_BASE_SINC_RESAMPLER_H_
      6 #define MEDIA_BASE_SINC_RESAMPLER_H_
      7 
      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"
     14 
     15 namespace media {
     16 
     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,
     25 
     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,
     29 
     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),
     35   };
     36 
     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;
     41 
     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   virtual ~SincResampler();
     52 
     53   // Resample |frames| of data from |read_cb_| into |destination|.
     54   void Resample(int frames, float* destination);
     55 
     56   // The maximum size in frames that guarantees Resample() will only make a
     57   // single call to |read_cb_| for more data.
     58   int ChunkSize() const;
     59 
     60   // Flush all buffered data and reset internal indices.  Not thread safe, do
     61   // not call while Resample() is in progress.
     62   void Flush();
     63 
     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 
     69   float* get_kernel_for_testing() { return kernel_storage_.get(); }
     70 
     71  private:
     72   FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, Convolve);
     73   FRIEND_TEST_ALL_PREFIXES(SincResamplerPerfTest, Convolve);
     74 
     75   void InitializeKernel();
     76   void UpdateRegions(bool second_load);
     77 
     78   // Compute convolution of |k1| and |k2| over |input_ptr|, resultant sums are
     79   // linearly interpolated using |kernel_interpolation_factor|.  On x86, the
     80   // underlying implementation is chosen at run time based on SSE support.  On
     81   // ARM, NEON support is chosen at compile time based on compilation flags.
     82   static float Convolve_C(const float* input_ptr, const float* k1,
     83                           const float* k2, double kernel_interpolation_factor);
     84 #if defined(ARCH_CPU_X86_FAMILY)
     85   static float Convolve_SSE(const float* input_ptr, const float* k1,
     86                             const float* k2,
     87                             double kernel_interpolation_factor);
     88 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
     89   static float Convolve_NEON(const float* input_ptr, const float* k1,
     90                              const float* k2,
     91                              double kernel_interpolation_factor);
     92 #endif
     93 
     94   // The ratio of input / output sample rates.
     95   double io_sample_rate_ratio_;
     96 
     97   // An index on the source input buffer with sub-sample precision.  It must be
     98   // double precision to avoid drift.
     99   double virtual_source_idx_;
    100 
    101   // The buffer is primed once at the very beginning of processing.
    102   bool buffer_primed_;
    103 
    104   // Source of data for resampling.
    105   const ReadCB read_cb_;
    106 
    107   // The size (in samples) to request from each |read_cb_| execution.
    108   const int request_frames_;
    109 
    110   // The number of source frames processed per pass.
    111   int block_size_;
    112 
    113   // The size (in samples) of the internal buffer used by the resampler.
    114   const int input_buffer_size_;
    115 
    116   // Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize.
    117   // The kernel offsets are sub-sample shifts of a windowed sinc shifted from
    118   // 0.0 to 1.0 sample.
    119   scoped_ptr<float[], base::AlignedFreeDeleter> kernel_storage_;
    120   scoped_ptr<float[], base::AlignedFreeDeleter> kernel_pre_sinc_storage_;
    121   scoped_ptr<float[], base::AlignedFreeDeleter> kernel_window_storage_;
    122 
    123   // Data from the source is copied into this buffer for each processing pass.
    124   scoped_ptr<float[], base::AlignedFreeDeleter> input_buffer_;
    125 
    126   // Pointers to the various regions inside |input_buffer_|.  See the diagram at
    127   // the top of the .cc file for more information.
    128   float* r0_;
    129   float* const r1_;
    130   float* const r2_;
    131   float* r3_;
    132   float* r4_;
    133 
    134   DISALLOW_COPY_AND_ASSIGN(SincResampler);
    135 };
    136 
    137 }  // namespace media
    138 
    139 #endif  // MEDIA_BASE_SINC_RESAMPLER_H_
    140