Make HighPassFilter not a ProcessingComponent anymore (bit exact).

BUG=webrtc:5298

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

Cr-Commit-Position: refs/heads/master@{#10939}
diff --git a/webrtc/modules/audio_processing/audio_processing_impl.cc b/webrtc/modules/audio_processing/audio_processing_impl.cc
index 48f23b1..a38d73e 100644
--- a/webrtc/modules/audio_processing/audio_processing_impl.cc
+++ b/webrtc/modules/audio_processing/audio_processing_impl.cc
@@ -81,7 +81,6 @@
       : echo_cancellation(nullptr),
         echo_control_mobile(nullptr),
         gain_control(nullptr),
-        high_pass_filter(nullptr),
         level_estimator(nullptr),
         noise_suppression(nullptr),
         voice_detection(nullptr) {}
@@ -89,7 +88,7 @@
   EchoCancellationImpl* echo_cancellation;
   EchoControlMobileImpl* echo_control_mobile;
   GainControlImpl* gain_control;
-  HighPassFilterImpl* high_pass_filter;
+  rtc::scoped_ptr<HighPassFilterImpl> high_pass_filter;
   LevelEstimatorImpl* level_estimator;
   NoiseSuppressionImpl* noise_suppression;
   VoiceDetectionImpl* voice_detection;
@@ -243,8 +242,8 @@
         new EchoControlMobileImpl(this, &crit_render_, &crit_capture_);
     public_submodules_->gain_control =
         new GainControlImpl(this, &crit_capture_, &crit_capture_);
-    public_submodules_->high_pass_filter =
-        new HighPassFilterImpl(this, &crit_capture_);
+    public_submodules_->high_pass_filter.reset(
+        new HighPassFilterImpl(&crit_capture_));
     public_submodules_->level_estimator =
         new LevelEstimatorImpl(this, &crit_capture_);
     public_submodules_->noise_suppression =
@@ -261,8 +260,6 @@
     private_submodules_->component_list.push_back(
         public_submodules_->gain_control);
     private_submodules_->component_list.push_back(
-        public_submodules_->high_pass_filter);
-    private_submodules_->component_list.push_back(
         public_submodules_->level_estimator);
     private_submodules_->component_list.push_back(
         public_submodules_->noise_suppression);
@@ -406,6 +403,8 @@
 
   InitializeIntelligibility();
 
+  InitializeHighPassFilter();
+
 #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
   if (debug_dump_.debug_file->Open()) {
     int err = WriteInitMessage();
@@ -767,7 +766,7 @@
     ca->set_num_channels(1);
   }
 
-  RETURN_ON_ERR(public_submodules_->high_pass_filter->ProcessCaptureAudio(ca));
+  public_submodules_->high_pass_filter->ProcessCaptureAudio(ca);
   RETURN_ON_ERR(public_submodules_->gain_control->AnalyzeCaptureAudio(ca));
   RETURN_ON_ERR(public_submodules_->noise_suppression->AnalyzeCaptureAudio(ca));
   RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio(ca));
@@ -1147,7 +1146,7 @@
 HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
   // Adding a lock here has no effect as it allows any access to the submodule
   // from the returned pointer.
-  return public_submodules_->high_pass_filter;
+  return public_submodules_->high_pass_filter.get();
 }
 
 LevelEstimator* AudioProcessingImpl::level_estimator() const {
@@ -1179,6 +1178,9 @@
       enabled_count++;
     }
   }
+  if (public_submodules_->high_pass_filter->is_enabled()) {
+    enabled_count++;
+  }
 
   // Data is unchanged if no components are enabled, or if only
   // public_submodules_->level_estimator
@@ -1293,6 +1295,11 @@
   }
 }
 
+void AudioProcessingImpl::InitializeHighPassFilter() {
+  public_submodules_->high_pass_filter->Initialize(num_output_channels(),
+                                                   proc_sample_rate_hz());
+}
+
 void AudioProcessingImpl::MaybeUpdateHistograms() {
   static const int kMinDiffDelayMs = 60;
 
diff --git a/webrtc/modules/audio_processing/audio_processing_impl.h b/webrtc/modules/audio_processing/audio_processing_impl.h
index 4a290a8..1ca5a8c 100644
--- a/webrtc/modules/audio_processing/audio_processing_impl.h
+++ b/webrtc/modules/audio_processing/audio_processing_impl.h
@@ -193,6 +193,8 @@
       EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
   void InitializeIntelligibility()
       EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
+  void InitializeHighPassFilter()
+      EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
   int InitializeLocked(const ProcessingConfig& config)
       EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
 
diff --git a/webrtc/modules/audio_processing/high_pass_filter_impl.cc b/webrtc/modules/audio_processing/high_pass_filter_impl.cc
index 2ad0a50..795dcbd 100644
--- a/webrtc/modules/audio_processing/high_pass_filter_impl.cc
+++ b/webrtc/modules/audio_processing/high_pass_filter_impl.cc
@@ -10,165 +10,115 @@
 
 #include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
 
-#include <assert.h>
-
 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
 #include "webrtc/modules/audio_processing/audio_buffer.h"
 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
-#include "webrtc/typedefs.h"
-
 
 namespace webrtc {
 namespace {
-const int16_t kFilterCoefficients8kHz[5] =
-    {3798, -7596, 3798, 7807, -3733};
-
-const int16_t kFilterCoefficients[5] =
-    {4012, -8024, 4012, 8002, -3913};
-
-struct FilterState {
-  int16_t y[4];
-  int16_t x[2];
-  const int16_t* ba;
-};
-
-int InitializeFilter(FilterState* hpf, int sample_rate_hz) {
-  assert(hpf != NULL);
-
-  if (sample_rate_hz == AudioProcessing::kSampleRate8kHz) {
-    hpf->ba = kFilterCoefficients8kHz;
-  } else {
-    hpf->ba = kFilterCoefficients;
-  }
-
-  WebRtcSpl_MemSetW16(hpf->x, 0, 2);
-  WebRtcSpl_MemSetW16(hpf->y, 0, 4);
-
-  return AudioProcessing::kNoError;
-}
-
-int Filter(FilterState* hpf, int16_t* data, size_t length) {
-  assert(hpf != NULL);
-
-  int32_t tmp_int32 = 0;
-  int16_t* y = hpf->y;
-  int16_t* x = hpf->x;
-  const int16_t* ba = hpf->ba;
-
-  for (size_t i = 0; i < length; i++) {
-    //  y[i] = b[0] * x[i] + b[1] * x[i-1] + b[2] * x[i-2]
-    //         + -a[1] * y[i-1] + -a[2] * y[i-2];
-
-    tmp_int32 = y[1] * ba[3];  // -a[1] * y[i-1] (low part)
-    tmp_int32 += y[3] * ba[4];  // -a[2] * y[i-2] (low part)
-    tmp_int32 = (tmp_int32 >> 15);
-    tmp_int32 += y[0] * ba[3];  // -a[1] * y[i-1] (high part)
-    tmp_int32 += y[2] * ba[4];  // -a[2] * y[i-2] (high part)
-    tmp_int32 = (tmp_int32 << 1);
-
-    tmp_int32 += data[i] * ba[0];  // b[0]*x[0]
-    tmp_int32 += x[0] * ba[1];  // b[1]*x[i-1]
-    tmp_int32 += x[1] * ba[2];  // b[2]*x[i-2]
-
-    // Update state (input part)
-    x[1] = x[0];
-    x[0] = data[i];
-
-    // Update state (filtered part)
-    y[2] = y[0];
-    y[3] = y[1];
-    y[0] = static_cast<int16_t>(tmp_int32 >> 13);
-    y[1] = static_cast<int16_t>(
-        (tmp_int32 - (static_cast<int32_t>(y[0]) << 13)) << 2);
-
-    // Rounding in Q12, i.e. add 2^11
-    tmp_int32 += 2048;
-
-    // Saturate (to 2^27) so that the HP filtered signal does not overflow
-    tmp_int32 = WEBRTC_SPL_SAT(static_cast<int32_t>(134217727),
-                               tmp_int32,
-                               static_cast<int32_t>(-134217728));
-
-    // Convert back to Q0 and use rounding.
-    data[i] = (int16_t)(tmp_int32 >> 12);
-  }
-
-  return AudioProcessing::kNoError;
-}
+const int16_t kFilterCoefficients8kHz[5] = {3798, -7596, 3798, 7807, -3733};
+const int16_t kFilterCoefficients[5] = {4012, -8024, 4012, 8002, -3913};
 }  // namespace
 
-typedef FilterState Handle;
+class HighPassFilterImpl::BiquadFilter {
+ public:
+  explicit BiquadFilter(int sample_rate_hz) :
+      ba_(sample_rate_hz == AudioProcessing::kSampleRate8kHz ?
+          kFilterCoefficients8kHz : kFilterCoefficients)
+  {
+    std::memset(x_, 0, sizeof(x_));
+    std::memset(y_, 0, sizeof(y_));
+  }
 
-HighPassFilterImpl::HighPassFilterImpl(const AudioProcessing* apm,
-                                       rtc::CriticalSection* crit)
-    : ProcessingComponent(), apm_(apm), crit_(crit) {
-  RTC_DCHECK(apm);
-  RTC_DCHECK(crit);
+  void Process(int16_t* data, size_t length) {
+    const int16_t* const ba = ba_;
+    int16_t* x = x_;
+    int16_t* y = y_;
+    int32_t tmp_int32 = 0;
+
+    for (size_t i = 0; i < length; i++) {
+      //  y[i] = b[0] * x[i] +  b[1] * x[i-1] +  b[2] * x[i-2]
+      //                     + -a[1] * y[i-1] + -a[2] * y[i-2];
+
+      tmp_int32 = y[1] * ba[3];      // -a[1] * y[i-1] (low part)
+      tmp_int32 += y[3] * ba[4];     // -a[2] * y[i-2] (low part)
+      tmp_int32 = (tmp_int32 >> 15);
+      tmp_int32 += y[0] * ba[3];     // -a[1] * y[i-1] (high part)
+      tmp_int32 += y[2] * ba[4];     // -a[2] * y[i-2] (high part)
+      tmp_int32 = (tmp_int32 << 1);
+
+      tmp_int32 += data[i] * ba[0];  // b[0] * x[0]
+      tmp_int32 += x[0] * ba[1];     // b[1] * x[i-1]
+      tmp_int32 += x[1] * ba[2];     // b[2] * x[i-2]
+
+      // Update state (input part).
+      x[1] = x[0];
+      x[0] = data[i];
+
+      // Update state (filtered part).
+      y[2] = y[0];
+      y[3] = y[1];
+      y[0] = static_cast<int16_t>(tmp_int32 >> 13);
+      y[1] = static_cast<int16_t>(
+          (tmp_int32 - (static_cast<int32_t>(y[0]) << 13)) << 2);
+
+      // Rounding in Q12, i.e. add 2^11.
+      tmp_int32 += 2048;
+
+      // Saturate (to 2^27) so that the HP filtered signal does not overflow.
+      tmp_int32 = WEBRTC_SPL_SAT(static_cast<int32_t>(134217727),
+                                 tmp_int32,
+                                 static_cast<int32_t>(-134217728));
+
+      // Convert back to Q0 and use rounding.
+      data[i] = static_cast<int16_t>(tmp_int32 >> 12);
+    }
+  }
+
+ private:
+  const int16_t* const ba_ = nullptr;
+  int16_t x_[2];
+  int16_t y_[4];
+};
+
+HighPassFilterImpl::HighPassFilterImpl(rtc::CriticalSection* crit)
+    : crit_(crit) {
+  RTC_DCHECK(crit_);
 }
 
 HighPassFilterImpl::~HighPassFilterImpl() {}
 
-int HighPassFilterImpl::ProcessCaptureAudio(AudioBuffer* audio) {
+void HighPassFilterImpl::Initialize(int channels, int sample_rate_hz) {
+  std::vector<rtc::scoped_ptr<BiquadFilter>> new_filters(channels);
+  for (int i = 0; i < channels; i++) {
+    new_filters[i].reset(new BiquadFilter(sample_rate_hz));
+  }
   rtc::CritScope cs(crit_);
-  int err = AudioProcessing::kNoError;
+  filters_.swap(new_filters);
+}
 
-  if (!is_component_enabled()) {
-    return AudioProcessing::kNoError;
+void HighPassFilterImpl::ProcessCaptureAudio(AudioBuffer* audio) {
+  rtc::CritScope cs(crit_);
+  if (!enabled_) {
+    return;
   }
 
-  assert(audio->num_frames_per_band() <= 160);
-
-  for (int i = 0; i < num_handles(); i++) {
-    Handle* my_handle = static_cast<Handle*>(handle(i));
-    err = Filter(my_handle,
-                 audio->split_bands(i)[kBand0To8kHz],
-                 audio->num_frames_per_band());
-
-    if (err != AudioProcessing::kNoError) {
-      return GetHandleError(my_handle);
-    }
+  RTC_DCHECK_GE(160u, audio->num_frames_per_band());
+  RTC_DCHECK_EQ(filters_.size(), static_cast<size_t>(audio->num_channels()));
+  for (size_t i = 0; i < filters_.size(); i++) {
+    filters_[i]->Process(audio->split_bands(i)[kBand0To8kHz],
+                         audio->num_frames_per_band());
   }
-
-  return AudioProcessing::kNoError;
 }
 
 int HighPassFilterImpl::Enable(bool enable) {
   rtc::CritScope cs(crit_);
-  return EnableComponent(enable);
+  enabled_ = enable;
+  return AudioProcessing::kNoError;
 }
 
 bool HighPassFilterImpl::is_enabled() const {
   rtc::CritScope cs(crit_);
-  return is_component_enabled();
-}
-
-void* HighPassFilterImpl::CreateHandle() const {
-  return new FilterState;
-}
-
-void HighPassFilterImpl::DestroyHandle(void* handle) const {
-  delete static_cast<Handle*>(handle);
-}
-
-int HighPassFilterImpl::InitializeHandle(void* handle) const {
-  // TODO(peah): Remove dependency on apm for the
-  // capture side sample rate.
-  rtc::CritScope cs(crit_);
-  return InitializeFilter(static_cast<Handle*>(handle),
-                          apm_->proc_sample_rate_hz());
-}
-
-int HighPassFilterImpl::ConfigureHandle(void* /*handle*/) const {
-  return AudioProcessing::kNoError;  // Not configurable.
-}
-
-int HighPassFilterImpl::num_handles_required() const {
-  return apm_->num_output_channels();
-}
-
-int HighPassFilterImpl::GetHandleError(void* handle) const {
-  // The component has no detailed errors.
-  assert(handle != NULL);
-  return AudioProcessing::kUnspecifiedError;
+  return enabled_;
 }
 }  // namespace webrtc
diff --git a/webrtc/modules/audio_processing/high_pass_filter_impl.h b/webrtc/modules/audio_processing/high_pass_filter_impl.h
index 6f8079e..b2a0717 100644
--- a/webrtc/modules/audio_processing/high_pass_filter_impl.h
+++ b/webrtc/modules/audio_processing/high_pass_filter_impl.h
@@ -12,39 +12,31 @@
 #define WEBRTC_MODULES_AUDIO_PROCESSING_HIGH_PASS_FILTER_IMPL_H_
 
 #include "webrtc/base/criticalsection.h"
+#include "webrtc/base/scoped_ptr.h"
 #include "webrtc/modules/audio_processing/include/audio_processing.h"
-#include "webrtc/modules/audio_processing/processing_component.h"
 
 namespace webrtc {
 
 class AudioBuffer;
 
-class HighPassFilterImpl : public HighPassFilter,
-                           public ProcessingComponent {
+class HighPassFilterImpl : public HighPassFilter {
  public:
-  HighPassFilterImpl(const AudioProcessing* apm, rtc::CriticalSection* crit);
-  virtual ~HighPassFilterImpl();
+  explicit HighPassFilterImpl(rtc::CriticalSection* crit);
+  ~HighPassFilterImpl() override;
 
-  int ProcessCaptureAudio(AudioBuffer* audio);
+  // TODO(peah): Fold into ctor, once public API is removed.
+  void Initialize(int channels, int sample_rate_hz);
+  void ProcessCaptureAudio(AudioBuffer* audio);
 
   // HighPassFilter implementation.
+  int Enable(bool enable) override;
   bool is_enabled() const override;
 
  private:
-  // HighPassFilter implementation.
-  int Enable(bool enable) override;
-
-  // ProcessingComponent implementation.
-  void* CreateHandle() const override;
-  int InitializeHandle(void* handle) const override;
-  int ConfigureHandle(void* handle) const override;
-  void DestroyHandle(void* handle) const override;
-  int num_handles_required() const override;
-  int GetHandleError(void* handle) const override;
-
-  const AudioProcessing* apm_;
-
-  rtc::CriticalSection* const crit_;
+  class BiquadFilter;
+  rtc::CriticalSection* const crit_ = nullptr;
+  bool enabled_ GUARDED_BY(crit_) = false;
+  std::vector<rtc::scoped_ptr<BiquadFilter>> filters_ GUARDED_BY(crit_);
 };
 }  // namespace webrtc