Make ChannelBuffer aware of frequency bands

Now the ChannelBuffer has 2 separate arrays, one for the full-band data and one for the splitted one. The corresponding accessors are added to the ChannelBuffer.
This is done to avoid having to refresh the bands pointers in AudioBuffer. It will also allow us to have a general accessor like data()[band][channel][sample].
All the files using the ChannelBuffer needed to be re-factored.
Tested with modules_unittests, common_audio_unittests, audioproc, audioproc_f, voe_cmd_test.

R=andrew@webrtc.org, kwiberg@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/36999004

Cr-Commit-Position: refs/heads/master@{#8318}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8318 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/common_audio/channel_buffer.h b/webrtc/common_audio/channel_buffer.h
index 31ebaa2..a5c4870 100644
--- a/webrtc/common_audio/channel_buffer.h
+++ b/webrtc/common_audio/channel_buffer.h
@@ -15,70 +15,96 @@
 
 #include "webrtc/base/checks.h"
 #include "webrtc/common_audio/include/audio_util.h"
+#include "webrtc/test/testsupport/gtest_prod_util.h"
 
 namespace webrtc {
 
-// Helper to encapsulate a contiguous data buffer with access to a pointer
-// array of the deinterleaved channels. The buffer is zero initialized at
-// creation.
+// Helper to encapsulate a contiguous data buffer, full or split into frequency
+// bands, with access to a pointer arrays of the deinterleaved channels and
+// bands. The buffer is zero initialized at creation.
+//
+// The buffer structure is showed below for a 2 channel and 2 bands case:
+//
+// |data_|:
+// { [ --- b1ch1 --- ] [ --- b2ch1 --- ] [ --- b1ch2 --- ] [ --- b2ch2 --- ] }
+//
+// The pointer arrays for the same example are as follows:
+//
+// |channels_|:
+// { [ b1ch1* ] [ b1ch2* ] [ b2ch1* ] [ b2ch2* ] }
+//
+// |bands_|:
+// { [ b1ch1* ] [ b2ch1* ] [ b1ch2* ] [ b2ch2* ] }
 template <typename T>
 class ChannelBuffer {
  public:
-  ChannelBuffer(int samples_per_channel, int num_channels)
-      : data_(new T[samples_per_channel * num_channels]),
-        channels_(new T*[num_channels]),
-        samples_per_channel_(samples_per_channel),
-        num_channels_(num_channels) {
-    Initialize();
+  ChannelBuffer(int num_frames,
+                int num_channels,
+                int num_bands = 1)
+      : data_(new T[num_frames * num_channels]),
+        channels_(new T*[num_channels * num_bands]),
+        bands_(new T*[num_channels * num_bands]),
+        num_frames_(num_frames),
+        num_frames_per_band_(num_frames / num_bands),
+        num_channels_(num_channels),
+        num_bands_(num_bands) {
+    memset(data_.get(), 0, size() * sizeof(T));
+    for (int i = 0; i < num_channels_; ++i) {
+      for (int j = 0; j < num_bands_; ++j) {
+        channels_[j * num_channels_ + i] =
+            &data_[i * num_frames_ + j * num_frames_per_band_];
+        bands_[i * num_bands_ + j] = channels_[j * num_channels_ + i];
+      }
+    }
   }
 
-  ChannelBuffer(const T* data, int samples_per_channel, int num_channels)
-      : data_(new T[samples_per_channel * num_channels]),
-        channels_(new T*[num_channels]),
-        samples_per_channel_(samples_per_channel),
-        num_channels_(num_channels) {
-    Initialize();
-    memcpy(data_.get(), data, length() * sizeof(T));
+  // Returns a pointer array to the full-band channels (or lower band channels).
+  // Usage:
+  // channels()[channel][sample].
+  // Where:
+  // 0 <= channel < |num_channels_|
+  // 0 <= sample < |num_frames_|
+  T* const* channels() { return channels(0); }
+  const T* const* channels() const { return channels(0); }
+
+  // Returns a pointer array to the channels for a specific band.
+  // Usage:
+  // channels(band)[channel][sample].
+  // Where:
+  // 0 <= band < |num_bands_|
+  // 0 <= channel < |num_channels_|
+  // 0 <= sample < |num_frames_per_band_|
+  const T* const* channels(int band) const {
+    DCHECK_LT(band, num_bands_);
+    DCHECK_GE(band, 0);
+    return &channels_[band * num_channels_];
   }
-
-  ChannelBuffer(const T* const* channels, int samples_per_channel,
-                int num_channels)
-      : data_(new T[samples_per_channel * num_channels]),
-        channels_(new T*[num_channels]),
-        samples_per_channel_(samples_per_channel),
-        num_channels_(num_channels) {
-    Initialize();
-    for (int i = 0; i < num_channels_; ++i)
-      CopyFrom(channels[i], i);
-  }
-
-  ~ChannelBuffer() {}
-
-  void CopyFrom(const void* channel_ptr, int i) {
-    DCHECK_LT(i, num_channels_);
-    memcpy(channels_[i], channel_ptr, samples_per_channel_ * sizeof(T));
-  }
-
-  T* data() { return data_.get(); }
-  const T* data() const { return data_.get(); }
-
-  const T* channel(int i) const {
-    DCHECK_GE(i, 0);
-    DCHECK_LT(i, num_channels_);
-    return channels_[i];
-  }
-  T* channel(int i) {
+  T* const* channels(int band) {
     const ChannelBuffer<T>* t = this;
-    return const_cast<T*>(t->channel(i));
+    return const_cast<T* const*>(t->channels(band));
   }
 
-  T* const* channels() { return channels_.get(); }
-  const T* const* channels() const { return channels_.get(); }
+  // Returns a pointer array to the bands for a specific channel.
+  // Usage:
+  // bands(channel)[band][sample].
+  // Where:
+  // 0 <= channel < |num_channels_|
+  // 0 <= band < |num_bands_|
+  // 0 <= sample < |num_frames_per_band_|
+  const T* const* bands(int channel) const {
+    DCHECK_LT(channel, num_channels_);
+    DCHECK_GE(channel, 0);
+    return &bands_[channel * num_bands_];
+  }
+  T* const* bands(int channel) {
+    const ChannelBuffer<T>* t = this;
+    return const_cast<T* const*>(t->bands(channel));
+  }
 
   // Sets the |slice| pointers to the |start_frame| position for each channel.
   // Returns |slice| for convenience.
   const T* const* Slice(T** slice, int start_frame) const {
-    DCHECK_LT(start_frame, samples_per_channel_);
+    DCHECK_LT(start_frame, num_frames_);
     for (int i = 0; i < num_channels_; ++i)
       slice[i] = &channels_[i][start_frame];
     return slice;
@@ -88,21 +114,25 @@
     return const_cast<T**>(t->Slice(slice, start_frame));
   }
 
-  int samples_per_channel() const { return samples_per_channel_; }
+  int num_frames() const { return num_frames_; }
+  int num_frames_per_band() const { return num_frames_per_band_; }
   int num_channels() const { return num_channels_; }
-  int length() const { return samples_per_channel_ * num_channels_; }
+  int num_bands() const { return num_bands_; }
+  size_t size() const {return num_frames_ * num_channels_; }
 
- private:
-  void Initialize() {
-    memset(data_.get(), 0, sizeof(T) * length());
-    for (int i = 0; i < num_channels_; ++i)
-      channels_[i] = &data_[i * samples_per_channel_];
+  void SetDataForTesting(const T* data, size_t size) {
+    CHECK_EQ(size, this->size());
+    memcpy(data_.get(), data, size * sizeof(*data));
   }
 
+ private:
   scoped_ptr<T[]> data_;
   scoped_ptr<T*[]> channels_;
-  const int samples_per_channel_;
+  scoped_ptr<T*[]> bands_;
+  const int num_frames_;
+  const int num_frames_per_band_;
   const int num_channels_;
+  const int num_bands_;
 };
 
 // One int16_t and one float ChannelBuffer that are kept in sync. The sync is
@@ -113,15 +143,17 @@
 // fbuf() until the next call to any of the other functions.
 class IFChannelBuffer {
  public:
-  IFChannelBuffer(int samples_per_channel, int num_channels);
+  IFChannelBuffer(int num_frames, int num_channels, int num_bands = 1);
 
   ChannelBuffer<int16_t>* ibuf();
   ChannelBuffer<float>* fbuf();
   const ChannelBuffer<int16_t>* ibuf_const() const;
   const ChannelBuffer<float>* fbuf_const() const;
 
+  int num_frames() const { return ibuf_.num_frames(); }
+  int num_frames_per_band() const { return ibuf_.num_frames_per_band(); }
   int num_channels() const { return ibuf_.num_channels(); }
-  int samples_per_channel() const { return ibuf_.samples_per_channel(); }
+  int num_bands() const { return ibuf_.num_bands(); }
 
  private:
   void RefreshF() const;