NetEq: Remove background noise fill during long expansions

NetEq was (up until this CL) capable of fading over to generating a
constant background noise when voice expansion had lasted too long.
However, the code has for a really long time only ever used the "off"
mode, which meant that long expansions are faded down to complete
silence (only zeros), i.e., background noise fill was not used.
Removing the other two modes ("on" and "fade") simplifies the code.

Bug: webrtc:9180
Change-Id: Ia2d46960208f3d75c9659ad3f027c52e5ecfb6b0
Reviewed-on: https://webrtc-review.googlesource.com/71485
Commit-Queue: Henrik Lundin <henrik.lundin@webrtc.org>
Reviewed-by: Minyue Li <minyue@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22969}
diff --git a/modules/audio_coding/neteq/background_noise.cc b/modules/audio_coding/neteq/background_noise.cc
index eda5c75..50ffa86 100644
--- a/modules/audio_coding/neteq/background_noise.cc
+++ b/modules/audio_coding/neteq/background_noise.cc
@@ -27,8 +27,7 @@
 
 BackgroundNoise::BackgroundNoise(size_t num_channels)
     : num_channels_(num_channels),
-      channel_parameters_(new ChannelParameters[num_channels_]),
-      mode_(NetEq::kBgnOn) {
+      channel_parameters_(new ChannelParameters[num_channels_]) {
   Reset();
 }
 
@@ -39,7 +38,6 @@
   for (size_t channel = 0; channel < num_channels_; ++channel) {
     channel_parameters_[channel].Reset();
   }
-  // Keep _bgnMode as it is.
 }
 
 void BackgroundNoise::Update(const AudioMultiVector& input,
diff --git a/modules/audio_coding/neteq/background_noise.h b/modules/audio_coding/neteq/background_noise.h
index 718f41d..a6f1395 100644
--- a/modules/audio_coding/neteq/background_noise.h
+++ b/modules/audio_coding/neteq/background_noise.h
@@ -68,11 +68,6 @@
 
   // Accessors.
   bool initialized() const { return initialized_; }
-  NetEq::BackgroundNoiseMode mode() const { return mode_; }
-
-  // Sets the mode of the background noise playout for cases when there is long
-  // duration of packet loss.
-  void set_mode(NetEq::BackgroundNoiseMode mode) { mode_ = mode; }
 
  private:
   static const int kThresholdIncrement = 229;  // 0.0035 in Q16.
@@ -128,7 +123,6 @@
   size_t num_channels_;
   std::unique_ptr<ChannelParameters[]> channel_parameters_;
   bool initialized_;
-  NetEq::BackgroundNoiseMode mode_;
 
   RTC_DISALLOW_COPY_AND_ASSIGN(BackgroundNoise);
 };
diff --git a/modules/audio_coding/neteq/expand.cc b/modules/audio_coding/neteq/expand.cc
index 3fb09ea..03bcc77 100644
--- a/modules/audio_coding/neteq/expand.cc
+++ b/modules/audio_coding/neteq/expand.cc
@@ -911,46 +911,10 @@
 
     // Unmute the background noise.
     int16_t bgn_mute_factor = background_noise_->MuteFactor(channel);
-    NetEq::BackgroundNoiseMode bgn_mode = background_noise_->mode();
-    if (bgn_mode == NetEq::kBgnFade && too_many_expands &&
-        bgn_mute_factor > 0) {
-      // Fade BGN to zero.
-      // Calculate muting slope, approximately -2^18 / fs_hz.
-      int mute_slope;
-      if (fs_hz_ == 8000) {
-        mute_slope = -32;
-      } else if (fs_hz_ == 16000) {
-        mute_slope = -16;
-      } else if (fs_hz_ == 32000) {
-        mute_slope = -8;
-      } else {
-        mute_slope = -5;
-      }
-      // Use UnmuteSignal function with negative slope.
-      // |bgn_mute_factor| is in Q14. |mute_slope| is in Q20.
-      DspHelper::UnmuteSignal(noise_samples,
-                              num_noise_samples,
-                              &bgn_mute_factor,
-                              mute_slope,
-                              noise_samples);
-    } else if (bgn_mute_factor < 16384) {
-      // If mode is kBgnOn, or if kBgnFade has started fading,
-      // use regular |mute_slope|.
-      if (!stop_muting_ && bgn_mode != NetEq::kBgnOff &&
-          !(bgn_mode == NetEq::kBgnFade && too_many_expands)) {
-        DspHelper::UnmuteSignal(noise_samples,
-                                static_cast<int>(num_noise_samples),
-                                &bgn_mute_factor,
-                                mute_slope,
-                                noise_samples);
-      } else {
-        // kBgnOn and stop muting, or
-        // kBgnOff (mute factor is always 0), or
-        // kBgnFade has reached 0.
-        WebRtcSpl_AffineTransformVector(noise_samples, noise_samples,
-                                        bgn_mute_factor, 8192, 14,
-                                        num_noise_samples);
-      }
+    if (bgn_mute_factor < 16384) {
+      WebRtcSpl_AffineTransformVector(noise_samples, noise_samples,
+                                      bgn_mute_factor, 8192, 14,
+                                      num_noise_samples);
     }
     // Update mute_factor in BackgroundNoise class.
     background_noise_->SetMuteFactor(channel, bgn_mute_factor);
diff --git a/modules/audio_coding/neteq/include/neteq.h b/modules/audio_coding/neteq/include/neteq.h
index a3648e8..310a227 100644
--- a/modules/audio_coding/neteq/include/neteq.h
+++ b/modules/audio_coding/neteq/include/neteq.h
@@ -84,12 +84,6 @@
 // This is the interface class for NetEq.
 class NetEq {
  public:
-  enum BackgroundNoiseMode {
-    kBgnOn,    // Default behavior with eternal noise.
-    kBgnFade,  // Noise fades to zero after some time.
-    kBgnOff    // Background noise is always zero.
-  };
-
   struct Config {
     Config();
     Config(const Config&);
@@ -104,7 +98,6 @@
     bool enable_post_decode_vad = false;
     size_t max_packets_in_buffer = 50;
     int max_delay_ms = 2000;
-    BackgroundNoiseMode background_noise_mode = kBgnOff;
     NetEqPlayoutMode playout_mode = kPlayoutOn;
     bool enable_fast_accelerate = false;
     bool enable_muted_state = false;
diff --git a/modules/audio_coding/neteq/neteq.cc b/modules/audio_coding/neteq/neteq.cc
index 534c474..37e24ef 100644
--- a/modules/audio_coding/neteq/neteq.cc
+++ b/modules/audio_coding/neteq/neteq.cc
@@ -30,7 +30,6 @@
      << ", enable_post_decode_vad="
      << (enable_post_decode_vad ? "true" : "false")
      << ", max_packets_in_buffer=" << max_packets_in_buffer
-     << ", background_noise_mode=" << background_noise_mode
      << ", playout_mode=" << playout_mode
      << ", enable_fast_accelerate="
      << (enable_fast_accelerate ? " true": "false")
diff --git a/modules/audio_coding/neteq/neteq_impl.cc b/modules/audio_coding/neteq/neteq_impl.cc
index 742683c..80bfdaf 100644
--- a/modules/audio_coding/neteq/neteq_impl.cc
+++ b/modules/audio_coding/neteq/neteq_impl.cc
@@ -101,7 +101,6 @@
       reset_decoder_(false),
       ssrc_(0),
       first_packet_(true),
-      background_noise_mode_(config.background_noise_mode),
       playout_mode_(config.playout_mode),
       enable_fast_accelerate_(config.enable_fast_accelerate),
       nack_enabled_(false),
@@ -2088,7 +2087,6 @@
 
   // Delete BackgroundNoise object and create a new one.
   background_noise_.reset(new BackgroundNoise(channels));
-  background_noise_->set_mode(background_noise_mode_);
 
   // Reset random vector.
   random_vector_.Reset();
diff --git a/modules/audio_coding/neteq/neteq_impl.h b/modules/audio_coding/neteq/neteq_impl.h
index 5e58453..ce75ce0 100644
--- a/modules/audio_coding/neteq/neteq_impl.h
+++ b/modules/audio_coding/neteq/neteq_impl.h
@@ -430,7 +430,6 @@
       RTC_GUARDED_BY(crit_sect_);
   uint32_t ssrc_ RTC_GUARDED_BY(crit_sect_);
   bool first_packet_ RTC_GUARDED_BY(crit_sect_);
-  const BackgroundNoiseMode background_noise_mode_ RTC_GUARDED_BY(crit_sect_);
   NetEqPlayoutMode playout_mode_ RTC_GUARDED_BY(crit_sect_);
   bool enable_fast_accelerate_ RTC_GUARDED_BY(crit_sect_);
   std::unique_ptr<NackTracker> nack_ RTC_GUARDED_BY(crit_sect_);
diff --git a/modules/audio_coding/neteq/neteq_unittest.cc b/modules/audio_coding/neteq/neteq_unittest.cc
index 430ebdb..4cd3014 100644
--- a/modules/audio_coding/neteq/neteq_unittest.cc
+++ b/modules/audio_coding/neteq/neteq_unittest.cc
@@ -950,9 +950,6 @@
 
 class NetEqBgnTest : public NetEqDecodingTest {
  protected:
-  virtual void TestCondition(double sum_squared_noise,
-                             bool should_be_faded) = 0;
-
   void CheckBgn(int sampling_rate_hz) {
     size_t expected_samples_per_channel = 0;
     uint8_t payload_type = 0xFF;  // Invalid.
@@ -1044,7 +1041,7 @@
         for (size_t k = 0;
              k < output.num_channels_ * output.samples_per_channel_; ++k)
           sum_squared += output_data[k] * output_data[k];
-        TestCondition(sum_squared, n > kFadingThreshold);
+        EXPECT_EQ(0, sum_squared);
       } else {
         EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
       }
@@ -1053,53 +1050,7 @@
   }
 };
 
-class NetEqBgnTestOn : public NetEqBgnTest {
- protected:
-  NetEqBgnTestOn() : NetEqBgnTest() {
-    config_.background_noise_mode = NetEq::kBgnOn;
-  }
-
-  void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
-    EXPECT_NE(0, sum_squared_noise);
-  }
-};
-
-class NetEqBgnTestOff : public NetEqBgnTest {
- protected:
-  NetEqBgnTestOff() : NetEqBgnTest() {
-    config_.background_noise_mode = NetEq::kBgnOff;
-  }
-
-  void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
-    EXPECT_EQ(0, sum_squared_noise);
-  }
-};
-
-class NetEqBgnTestFade : public NetEqBgnTest {
- protected:
-  NetEqBgnTestFade() : NetEqBgnTest() {
-    config_.background_noise_mode = NetEq::kBgnFade;
-  }
-
-  void TestCondition(double sum_squared_noise, bool should_be_faded) {
-    if (should_be_faded)
-      EXPECT_EQ(0, sum_squared_noise);
-  }
-};
-
-TEST_F(NetEqBgnTestOn, RunTest) {
-  CheckBgn(8000);
-  CheckBgn(16000);
-  CheckBgn(32000);
-}
-
-TEST_F(NetEqBgnTestOff, RunTest) {
-  CheckBgn(8000);
-  CheckBgn(16000);
-  CheckBgn(32000);
-}
-
-TEST_F(NetEqBgnTestFade, RunTest) {
+TEST_F(NetEqBgnTest, RunTest) {
   CheckBgn(8000);
   CheckBgn(16000);
   CheckBgn(32000);