Keep track of the user-facing number of channels in a ChannelBuffer

Before this change the ChannelBuffer had a fixed number of channels. This meant for example that when the Beamformer would reduce the number of channels to one, the merging filter bank was still merging all the channels, which was unnecessary since they were not processed and just discarded later. This change doesn't change the signal at all. It just reflects the number of channels in the ChannelBuffer, reducing the complexity.

R=henrik.lundin@webrtc.org, peah@webrtc.org, tina.legrand@webrtc.org

Review URL: https://codereview.webrtc.org/2053773002 .

Cr-Commit-Position: refs/heads/master@{#13352}
diff --git a/webrtc/common_audio/channel_buffer.h b/webrtc/common_audio/channel_buffer.h
index faecec9..d6661de 100644
--- a/webrtc/common_audio/channel_buffer.h
+++ b/webrtc/common_audio/channel_buffer.h
@@ -48,13 +48,14 @@
         bands_(new T*[num_channels * num_bands]),
         num_frames_(num_frames),
         num_frames_per_band_(num_frames / num_bands),
+        num_allocated_channels_(num_channels),
         num_channels_(num_channels),
         num_bands_(num_bands) {
-    for (size_t i = 0; i < num_channels_; ++i) {
+    for (size_t i = 0; i < num_allocated_channels_; ++i) {
       for (size_t j = 0; j < num_bands_; ++j) {
-        channels_[j * num_channels_ + i] =
+        channels_[j * num_allocated_channels_ + i] =
             &data_[i * num_frames_ + j * num_frames_per_band_];
-        bands_[i * num_bands_ + j] = channels_[j * num_channels_ + i];
+        bands_[i * num_bands_ + j] = channels_[j * num_allocated_channels_ + i];
       }
     }
   }
@@ -63,7 +64,7 @@
   // Usage:
   // channels()[channel][sample].
   // Where:
-  // 0 <= channel < |num_channels_|
+  // 0 <= channel < |num_allocated_channels_|
   // 0 <= sample < |num_frames_|
   T* const* channels() { return channels(0); }
   const T* const* channels() const { return channels(0); }
@@ -73,11 +74,11 @@
   // channels(band)[channel][sample].
   // Where:
   // 0 <= band < |num_bands_|
-  // 0 <= channel < |num_channels_|
+  // 0 <= channel < |num_allocated_channels_|
   // 0 <= sample < |num_frames_per_band_|
   const T* const* channels(size_t band) const {
     RTC_DCHECK_LT(band, num_bands_);
-    return &channels_[band * num_channels_];
+    return &channels_[band * num_allocated_channels_];
   }
   T* const* channels(size_t band) {
     const ChannelBuffer<T>* t = this;
@@ -118,7 +119,12 @@
   size_t num_frames_per_band() const { return num_frames_per_band_; }
   size_t num_channels() const { return num_channels_; }
   size_t num_bands() const { return num_bands_; }
-  size_t size() const {return num_frames_ * num_channels_; }
+  size_t size() const {return num_frames_ * num_allocated_channels_; }
+
+  void set_num_channels(size_t num_channels) {
+    RTC_DCHECK_LE(num_channels, num_allocated_channels_);
+    num_channels_ = num_channels;
+  }
 
   void SetDataForTesting(const T* data, size_t size) {
     RTC_CHECK_EQ(size, this->size());
@@ -131,7 +137,10 @@
   std::unique_ptr<T* []> bands_;
   const size_t num_frames_;
   const size_t num_frames_per_band_;
-  const size_t num_channels_;
+  // Number of channels the internal buffer holds.
+  const size_t num_allocated_channels_;
+  // Number of channels the user sees.
+  size_t num_channels_;
   const size_t num_bands_;
 };
 
@@ -152,7 +161,13 @@
 
   size_t num_frames() const { return ibuf_.num_frames(); }
   size_t num_frames_per_band() const { return ibuf_.num_frames_per_band(); }
-  size_t num_channels() const { return ibuf_.num_channels(); }
+  size_t num_channels() const {
+    return ivalid_ ? ibuf_.num_channels() : fbuf_.num_channels();
+  }
+  void set_num_channels(size_t num_channels) {
+    ibuf_.set_num_channels(num_channels);
+    fbuf_.set_num_channels(num_channels);
+  }
   size_t num_bands() const { return ibuf_.num_bands(); }
 
  private: