Home | History | Annotate | Download | only in common_audio
      1 /*
      2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_
     12 #define WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_
     13 
     14 #include <string.h>
     15 
     16 #include "webrtc/base/checks.h"
     17 #include "webrtc/base/scoped_ptr.h"
     18 #include "webrtc/common_audio/include/audio_util.h"
     19 #include "webrtc/test/testsupport/gtest_prod_util.h"
     20 
     21 namespace webrtc {
     22 
     23 // Helper to encapsulate a contiguous data buffer, full or split into frequency
     24 // bands, with access to a pointer arrays of the deinterleaved channels and
     25 // bands. The buffer is zero initialized at creation.
     26 //
     27 // The buffer structure is showed below for a 2 channel and 2 bands case:
     28 //
     29 // |data_|:
     30 // { [ --- b1ch1 --- ] [ --- b2ch1 --- ] [ --- b1ch2 --- ] [ --- b2ch2 --- ] }
     31 //
     32 // The pointer arrays for the same example are as follows:
     33 //
     34 // |channels_|:
     35 // { [ b1ch1* ] [ b1ch2* ] [ b2ch1* ] [ b2ch2* ] }
     36 //
     37 // |bands_|:
     38 // { [ b1ch1* ] [ b2ch1* ] [ b1ch2* ] [ b2ch2* ] }
     39 template <typename T>
     40 class ChannelBuffer {
     41  public:
     42   ChannelBuffer(size_t num_frames,
     43                 size_t num_channels,
     44                 size_t num_bands = 1)
     45       : data_(new T[num_frames * num_channels]()),
     46         channels_(new T*[num_channels * num_bands]),
     47         bands_(new T*[num_channels * num_bands]),
     48         num_frames_(num_frames),
     49         num_frames_per_band_(num_frames / num_bands),
     50         num_channels_(num_channels),
     51         num_bands_(num_bands) {
     52     for (size_t i = 0; i < num_channels_; ++i) {
     53       for (size_t j = 0; j < num_bands_; ++j) {
     54         channels_[j * num_channels_ + i] =
     55             &data_[i * num_frames_ + j * num_frames_per_band_];
     56         bands_[i * num_bands_ + j] = channels_[j * num_channels_ + i];
     57       }
     58     }
     59   }
     60 
     61   // Returns a pointer array to the full-band channels (or lower band channels).
     62   // Usage:
     63   // channels()[channel][sample].
     64   // Where:
     65   // 0 <= channel < |num_channels_|
     66   // 0 <= sample < |num_frames_|
     67   T* const* channels() { return channels(0); }
     68   const T* const* channels() const { return channels(0); }
     69 
     70   // Returns a pointer array to the channels for a specific band.
     71   // Usage:
     72   // channels(band)[channel][sample].
     73   // Where:
     74   // 0 <= band < |num_bands_|
     75   // 0 <= channel < |num_channels_|
     76   // 0 <= sample < |num_frames_per_band_|
     77   const T* const* channels(size_t band) const {
     78     RTC_DCHECK_LT(band, num_bands_);
     79     return &channels_[band * num_channels_];
     80   }
     81   T* const* channels(size_t band) {
     82     const ChannelBuffer<T>* t = this;
     83     return const_cast<T* const*>(t->channels(band));
     84   }
     85 
     86   // Returns a pointer array to the bands for a specific channel.
     87   // Usage:
     88   // bands(channel)[band][sample].
     89   // Where:
     90   // 0 <= channel < |num_channels_|
     91   // 0 <= band < |num_bands_|
     92   // 0 <= sample < |num_frames_per_band_|
     93   const T* const* bands(size_t channel) const {
     94     RTC_DCHECK_LT(channel, num_channels_);
     95     RTC_DCHECK_GE(channel, 0u);
     96     return &bands_[channel * num_bands_];
     97   }
     98   T* const* bands(size_t channel) {
     99     const ChannelBuffer<T>* t = this;
    100     return const_cast<T* const*>(t->bands(channel));
    101   }
    102 
    103   // Sets the |slice| pointers to the |start_frame| position for each channel.
    104   // Returns |slice| for convenience.
    105   const T* const* Slice(T** slice, size_t start_frame) const {
    106     RTC_DCHECK_LT(start_frame, num_frames_);
    107     for (size_t i = 0; i < num_channels_; ++i)
    108       slice[i] = &channels_[i][start_frame];
    109     return slice;
    110   }
    111   T** Slice(T** slice, size_t start_frame) {
    112     const ChannelBuffer<T>* t = this;
    113     return const_cast<T**>(t->Slice(slice, start_frame));
    114   }
    115 
    116   size_t num_frames() const { return num_frames_; }
    117   size_t num_frames_per_band() const { return num_frames_per_band_; }
    118   size_t num_channels() const { return num_channels_; }
    119   size_t num_bands() const { return num_bands_; }
    120   size_t size() const {return num_frames_ * num_channels_; }
    121 
    122   void SetDataForTesting(const T* data, size_t size) {
    123     RTC_CHECK_EQ(size, this->size());
    124     memcpy(data_.get(), data, size * sizeof(*data));
    125   }
    126 
    127  private:
    128   rtc::scoped_ptr<T[]> data_;
    129   rtc::scoped_ptr<T* []> channels_;
    130   rtc::scoped_ptr<T* []> bands_;
    131   const size_t num_frames_;
    132   const size_t num_frames_per_band_;
    133   const size_t num_channels_;
    134   const size_t num_bands_;
    135 };
    136 
    137 // One int16_t and one float ChannelBuffer that are kept in sync. The sync is
    138 // broken when someone requests write access to either ChannelBuffer, and
    139 // reestablished when someone requests the outdated ChannelBuffer. It is
    140 // therefore safe to use the return value of ibuf_const() and fbuf_const()
    141 // until the next call to ibuf() or fbuf(), and the return value of ibuf() and
    142 // fbuf() until the next call to any of the other functions.
    143 class IFChannelBuffer {
    144  public:
    145   IFChannelBuffer(size_t num_frames, size_t num_channels, size_t num_bands = 1);
    146 
    147   ChannelBuffer<int16_t>* ibuf();
    148   ChannelBuffer<float>* fbuf();
    149   const ChannelBuffer<int16_t>* ibuf_const() const;
    150   const ChannelBuffer<float>* fbuf_const() const;
    151 
    152   size_t num_frames() const { return ibuf_.num_frames(); }
    153   size_t num_frames_per_band() const { return ibuf_.num_frames_per_band(); }
    154   size_t num_channels() const { return ibuf_.num_channels(); }
    155   size_t num_bands() const { return ibuf_.num_bands(); }
    156 
    157  private:
    158   void RefreshF() const;
    159   void RefreshI() const;
    160 
    161   mutable bool ivalid_;
    162   mutable ChannelBuffer<int16_t> ibuf_;
    163   mutable bool fvalid_;
    164   mutable ChannelBuffer<float> fbuf_;
    165 };
    166 
    167 }  // namespace webrtc
    168 
    169 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_
    170