NetEQ: BackgroundNoise::Update returns true when the filter is updated

Bug: webrtc:10690
Change-Id: I17ff7dc1cffc8c46987d0a9ff8c6633ce9dcc8d3
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/144040
Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28411}
diff --git a/modules/audio_coding/neteq/background_noise.cc b/modules/audio_coding/neteq/background_noise.cc
index 5bb9d7e..c0dcc5e 100644
--- a/modules/audio_coding/neteq/background_noise.cc
+++ b/modules/audio_coding/neteq/background_noise.cc
@@ -45,12 +45,13 @@
   }
 }
 
-void BackgroundNoise::Update(const AudioMultiVector& input,
+bool BackgroundNoise::Update(const AudioMultiVector& input,
                              const PostDecodeVad& vad) {
+  bool filter_params_saved = false;
   if (vad.running() && vad.active_speech()) {
     // Do not update the background noise parameters if we know that the signal
     // is active speech.
-    return;
+    return filter_params_saved;
   }
 
   int32_t auto_correlation[kMaxLpcOrder + 1];
@@ -62,6 +63,7 @@
     ChannelParameters& parameters = channel_parameters_[channel_ix];
     int16_t temp_signal_array[kVecLen + kMaxLpcOrder] = {0};
     int16_t* temp_signal = &temp_signal_array[kMaxLpcOrder];
+    RTC_DCHECK_GE(input.Size(), kVecLen);
     input[channel_ix].CopyTo(kVecLen, input.Size() - kVecLen, temp_signal);
     int32_t sample_energy =
         CalculateAutoCorrelation(temp_signal, kVecLen, auto_correlation);
@@ -70,26 +72,26 @@
          sample_energy < parameters.energy_update_threshold) ||
         (vad.running() && !vad.active_speech())) {
       // Generate LPC coefficients.
-      if (auto_correlation[0] > 0) {
-        // Regardless of whether the filter is actually updated or not,
-        // update energy threshold levels, since we have in fact observed
-        // a low energy signal.
-        if (sample_energy < parameters.energy_update_threshold) {
-          // Never go under 1.0 in average sample energy.
-          parameters.energy_update_threshold = std::max(sample_energy, 1);
-          parameters.low_energy_update_threshold = 0;
-        }
-
-        // Only update BGN if filter is stable, i.e., if return value from
-        // Levinson-Durbin function is 1.
-        if (WebRtcSpl_LevinsonDurbin(auto_correlation, lpc_coefficients,
-                                     reflection_coefficients,
-                                     kMaxLpcOrder) != 1) {
-          return;
-        }
-      } else {
+      if (auto_correlation[0] <= 0) {
         // Center value in auto-correlation is not positive. Do not update.
-        return;
+        return filter_params_saved;
+      }
+
+      // Regardless of whether the filter is actually updated or not,
+      // update energy threshold levels, since we have in fact observed
+      // a low energy signal.
+      if (sample_energy < parameters.energy_update_threshold) {
+        // Never go under 1.0 in average sample energy.
+        parameters.energy_update_threshold = std::max(sample_energy, 1);
+        parameters.low_energy_update_threshold = 0;
+      }
+
+      // Only update BGN if filter is stable, i.e., if return value from
+      // Levinson-Durbin function is 1.
+      if (WebRtcSpl_LevinsonDurbin(auto_correlation, lpc_coefficients,
+                                   reflection_coefficients,
+                                   kMaxLpcOrder) != 1) {
+        return filter_params_saved;
       }
 
       // Generate the CNG gain factor by looking at the energy of the residual.
@@ -113,6 +115,7 @@
         SaveParameters(channel_ix, lpc_coefficients,
                        temp_signal + kVecLen - kMaxLpcOrder, sample_energy,
                        residual_energy);
+        filter_params_saved = true;
       }
     } else {
       // Will only happen if post-decode VAD is disabled and |sample_energy| is
@@ -121,7 +124,7 @@
       IncrementEnergyThreshold(channel_ix, sample_energy);
     }
   }
-  return;
+  return filter_params_saved;
 }
 
 void BackgroundNoise::GenerateBackgroundNoise(
diff --git a/modules/audio_coding/neteq/background_noise.h b/modules/audio_coding/neteq/background_noise.h
index 0286320..5191179 100644
--- a/modules/audio_coding/neteq/background_noise.h
+++ b/modules/audio_coding/neteq/background_noise.h
@@ -37,7 +37,8 @@
 
   // Updates the parameter estimates based on the signal currently in the
   // |sync_buffer|, and on the latest decision in |vad| if it is running.
-  void Update(const AudioMultiVector& sync_buffer, const PostDecodeVad& vad);
+  // Returns true if the filter parameters are updated.
+  bool Update(const AudioMultiVector& sync_buffer, const PostDecodeVad& vad);
 
   // Generates background noise given a random vector and writes the output to
   // |buffer|.