AEC3: Refactor AecState

This CL introduces a major refactoring of AecState for the purpose of
simplifying further improvements to the logic in this code.

The changes have successfully been tested for bitexactness.

Bug: webrtc:8671
Change-Id: If98efde55a22c76b093089a11a0562daac7e16e6
Reviewed-on: https://webrtc-review.googlesource.com/c/102362
Commit-Queue: Per Åhgren <peah@webrtc.org>
Reviewed-by: Gustaf Ullberg <gustaf@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24996}
This commit is contained in:
Per Åhgren 2018-10-04 15:37:54 +02:00 committed by Commit Bot
parent 433eafe1f5
commit c5a38ad143
8 changed files with 572 additions and 297 deletions

View File

@ -30,11 +30,6 @@ bool EnableErleResetsAtGainChanges() {
return !field_trial::IsEnabled("WebRTC-Aec3ResetErleAtGainChangesKillSwitch");
}
float ComputeGainRampupIncrease(const EchoCanceller3Config& config) {
const auto& c = config.echo_removal_control.gain_rampup;
return powf(1.f / c.first_non_zero_gain, 1.f / c.non_zero_gain_blocks);
}
constexpr size_t kBlocksSinceConvergencedFilterInit = 10000;
constexpr size_t kBlocksSinceConsistentEstimateInit = 10000;
@ -42,21 +37,55 @@ constexpr size_t kBlocksSinceConsistentEstimateInit = 10000;
int AecState::instance_count_ = 0;
void AecState::GetResidualEchoScaling(
rtc::ArrayView<float> residual_scaling) const {
bool filter_has_had_time_to_converge;
if (config_.filter.conservative_initial_phase) {
filter_has_had_time_to_converge =
strong_not_saturated_render_blocks_ >= 1.5f * kNumBlocksPerSecond;
} else {
filter_has_had_time_to_converge =
strong_not_saturated_render_blocks_ >= 0.8f * kNumBlocksPerSecond;
}
echo_audibility_.GetResidualEchoScaling(filter_has_had_time_to_converge,
residual_scaling);
}
absl::optional<float> AecState::ErleUncertainty() const {
bool filter_has_had_time_to_converge;
if (config_.filter.conservative_initial_phase) {
filter_has_had_time_to_converge =
strong_not_saturated_render_blocks_ >= 1.5f * kNumBlocksPerSecond;
} else {
filter_has_had_time_to_converge =
strong_not_saturated_render_blocks_ >= 0.8f * kNumBlocksPerSecond;
}
if (!filter_has_had_time_to_converge) {
return 1.f;
}
return absl::nullopt;
}
AecState::AecState(const EchoCanceller3Config& config)
: data_dumper_(
new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
config_(config),
erle_estimator_(config.erle.min, config.erle.max_l, config.erle.max_h),
max_render_(config_.filter.main.length_blocks, 0.f),
gain_rampup_increase_(ComputeGainRampupIncrease(config_)),
initial_state_(config_),
delay_state_(config_),
transparent_state_(config_),
filter_quality_state_(config_),
saturation_detector_(config_),
erl_estimator_(2 * kNumBlocksPerSecond),
erle_estimator_(2 * kNumBlocksPerSecond,
config_.erle.min,
config_.erle.max_l,
config_.erle.max_h),
suppression_gain_limiter_(config_),
filter_analyzer_(config_),
blocks_since_converged_filter_(kBlocksSinceConvergencedFilterInit),
active_blocks_since_consistent_filter_estimate_(
kBlocksSinceConsistentEstimateInit),
echo_audibility_(
config.echo_audibility.use_stationarity_properties_at_init),
reverb_model_estimator_(config),
config_.echo_audibility.use_stationarity_properties_at_init),
reverb_model_estimator_(config_),
enable_erle_resets_at_gain_changes_(EnableErleResetsAtGainChanges()) {}
AecState::~AecState() = default;
@ -65,24 +94,16 @@ void AecState::HandleEchoPathChange(
const EchoPathVariability& echo_path_variability) {
const auto full_reset = [&]() {
filter_analyzer_.Reset();
blocks_since_last_saturation_ = 0;
usable_linear_estimate_ = false;
capture_signal_saturation_ = false;
echo_saturation_ = false;
std::fill(max_render_.begin(), max_render_.end(), 0.f);
blocks_with_proper_filter_adaptation_ = 0;
blocks_since_reset_ = 0;
filter_has_had_time_to_converge_ = false;
render_received_ = false;
strong_not_saturated_render_blocks_ = 0;
blocks_with_active_render_ = 0;
initial_state_ = true;
suppression_gain_limiter_.Reset();
blocks_since_converged_filter_ = kBlocksSinceConvergencedFilterInit;
diverged_blocks_ = 0;
if (config_.echo_removal_control.linear_and_stable_echo_path) {
converged_filter_seen_ = false;
}
erle_estimator_.Reset();
initial_state_.Reset();
transparent_state_.Reset();
saturation_detector_.Reset();
erle_estimator_.Reset(true);
erl_estimator_.Reset();
filter_quality_state_.Reset();
};
// TODO(peah): Refine the reset scheme according to the type of gain and
@ -91,10 +112,9 @@ void AecState::HandleEchoPathChange(
if (echo_path_variability.delay_change !=
EchoPathVariability::DelayAdjustment::kNone) {
full_reset();
}
if (enable_erle_resets_at_gain_changes_ &&
echo_path_variability.gain_change) {
erle_estimator_.Reset();
} else if (enable_erle_resets_at_gain_changes_ &&
echo_path_variability.gain_change) {
erle_estimator_.Reset(false);
}
subtractor_output_analyzer_.HandleEchoPathChange();
}
@ -112,250 +132,361 @@ void AecState::Update(
// Analyze the filter output.
subtractor_output_analyzer_.Update(subtractor_output);
const bool converged_filter = subtractor_output_analyzer_.ConvergedFilter();
const bool diverged_filter = subtractor_output_analyzer_.DivergedFilter();
// Analyze the filter and compute the delays.
// Analyze the properties of the filter.
filter_analyzer_.Update(adaptive_filter_impulse_response,
adaptive_filter_frequency_response, render_buffer);
filter_delay_blocks_ = filter_analyzer_.DelayBlocks();
if (external_delay &&
(!external_delay_ || external_delay_->delay != external_delay->delay)) {
frames_since_external_delay_change_ = 0;
external_delay_ = external_delay;
}
if (blocks_with_proper_filter_adaptation_ < 2 * kNumBlocksPerSecond &&
external_delay_) {
filter_delay_blocks_ = config_.delay.delay_headroom_blocks;
}
if (filter_analyzer_.Consistent()) {
internal_delay_ = filter_analyzer_.DelayBlocks();
} else {
internal_delay_ = absl::nullopt;
}
// Estimate the direct path delay of the filter.
delay_state_.Update(filter_analyzer_, external_delay,
strong_not_saturated_render_blocks_);
external_delay_seen_ = external_delay_seen_ || external_delay;
const std::vector<float>& aligned_render_block =
render_buffer.Block(-delay_state_.DirectPathFilterDelay())[0];
const std::vector<float>& x = render_buffer.Block(-filter_delay_blocks_)[0];
// Update render counters.
const float render_energy = std::inner_product(
aligned_render_block.begin(), aligned_render_block.end(),
aligned_render_block.begin(), 0.f);
const bool active_render =
render_energy > (config_.render_levels.active_render_limit *
config_.render_levels.active_render_limit) *
kFftLengthBy2;
blocks_with_active_render_ += active_render ? 1 : 0;
strong_not_saturated_render_blocks_ +=
active_render && !SaturatedCapture() ? 1 : 0;
// Update counters.
++capture_block_counter_;
++blocks_since_reset_;
const bool active_render_block = DetectActiveRender(x);
blocks_with_active_render_ += active_render_block ? 1 : 0;
blocks_with_proper_filter_adaptation_ +=
active_render_block && !SaturatedCapture() ? 1 : 0;
// Update the limit on the echo suppression after an echo path change to avoid
// an initial echo burst.
// Update the limit on the echo suppr ession after an echo path change to
// avoid an initial echo burst.
suppression_gain_limiter_.Update(render_buffer.GetRenderActivity(),
transparent_mode_);
if (converged_filter) {
TransparentMode());
if (subtractor_output_analyzer_.ConvergedFilter()) {
suppression_gain_limiter_.Deactivate();
}
if (UseStationaryProperties()) {
if (config_.echo_audibility.use_stationary_properties) {
// Update the echo audibility evaluator.
echo_audibility_.Update(
render_buffer, FilterDelayBlocks(), external_delay_seen_,
render_buffer, delay_state_.DirectPathFilterDelay(),
delay_state_.ExternalDelayReported(),
config_.ep_strength.reverb_based_on_render ? ReverbDecay() : 0.f);
}
// Update the ERL and ERLE measures.
if (transition_triggered_) {
erle_estimator_.Reset();
}
if (blocks_since_reset_ >= 2 * kNumBlocksPerSecond) {
const auto& X2 = render_buffer.Spectrum(filter_delay_blocks_);
erle_estimator_.Update(X2, Y2, E2_main, converged_filter,
config_.erle.onset_detection);
if (converged_filter) {
erl_estimator_.Update(X2, Y2);
}
if (initial_state_.TransitionTriggered()) {
erle_estimator_.Reset(false);
}
const auto& X2 = render_buffer.Spectrum(delay_state_.DirectPathFilterDelay());
erle_estimator_.Update(X2, Y2, E2_main,
subtractor_output_analyzer_.ConvergedFilter(),
config_.erle.onset_detection);
erl_estimator_.Update(subtractor_output_analyzer_.ConvergedFilter(), X2, Y2);
// Detect and flag echo saturation.
if (config_.ep_strength.echo_can_saturate) {
echo_saturation_ = DetectEchoSaturation(x, EchoPathGain());
}
saturation_detector_.Update(aligned_render_block, SaturatedCapture(),
EchoPathGain());
if (config_.filter.conservative_initial_phase) {
filter_has_had_time_to_converge_ =
blocks_with_proper_filter_adaptation_ >= 1.5f * kNumBlocksPerSecond;
} else {
filter_has_had_time_to_converge_ =
blocks_with_proper_filter_adaptation_ >= 0.8f * kNumBlocksPerSecond;
}
// Update the decision on whether to use the initial state parameter set.
initial_state_.Update(active_render, SaturatedCapture());
if (!filter_should_have_converged_) {
filter_should_have_converged_ =
blocks_with_proper_filter_adaptation_ > 6 * kNumBlocksPerSecond;
}
// Detect whether the transparent mode should be activated.
transparent_state_.Update(delay_state_.DirectPathFilterDelay(),
filter_analyzer_.Consistent(),
subtractor_output_analyzer_.ConvergedFilter(),
subtractor_output_analyzer_.DivergedFilter(),
active_render, SaturatedCapture());
// Flag whether the initial state is still active.
bool prev_initial_state = initial_state_;
if (config_.filter.conservative_initial_phase) {
initial_state_ =
blocks_with_proper_filter_adaptation_ < 5 * kNumBlocksPerSecond;
} else {
initial_state_ = blocks_with_proper_filter_adaptation_ <
config_.filter.initial_state_seconds * kNumBlocksPerSecond;
}
transition_triggered_ = !initial_state_ && prev_initial_state;
// Update counters for the filter divergence and convergence.
diverged_blocks_ = diverged_filter ? diverged_blocks_ + 1 : 0;
if (diverged_blocks_ >= 60) {
blocks_since_converged_filter_ = kBlocksSinceConvergencedFilterInit;
} else {
blocks_since_converged_filter_ =
converged_filter ? 0 : blocks_since_converged_filter_ + 1;
}
if (converged_filter) {
active_blocks_since_converged_filter_ = 0;
} else if (active_render_block) {
++active_blocks_since_converged_filter_;
}
bool recently_converged_filter =
blocks_since_converged_filter_ < 60 * kNumBlocksPerSecond;
if (blocks_since_converged_filter_ > 20 * kNumBlocksPerSecond) {
converged_filter_count_ = 0;
} else if (converged_filter) {
++converged_filter_count_;
}
if (converged_filter_count_ > 50) {
finite_erl_ = true;
}
if (filter_analyzer_.Consistent() && filter_delay_blocks_ < 5) {
consistent_filter_seen_ = true;
active_blocks_since_consistent_filter_estimate_ = 0;
} else if (active_render_block) {
++active_blocks_since_consistent_filter_estimate_;
}
bool consistent_filter_estimate_not_seen;
if (!consistent_filter_seen_) {
consistent_filter_estimate_not_seen =
capture_block_counter_ > 5 * kNumBlocksPerSecond;
} else {
consistent_filter_estimate_not_seen =
active_blocks_since_consistent_filter_estimate_ >
30 * kNumBlocksPerSecond;
}
converged_filter_seen_ = converged_filter_seen_ || converged_filter;
// If no filter convergence is seen for a long time, reset the estimated
// properties of the echo path.
if (active_blocks_since_converged_filter_ > 60 * kNumBlocksPerSecond) {
converged_filter_seen_ = false;
finite_erl_ = false;
}
// After an amount of active render samples for which an echo should have been
// detected in the capture signal if the ERL was not infinite, flag that a
// transparent mode should be entered.
transparent_mode_ = !config_.ep_strength.bounded_erl && !finite_erl_;
transparent_mode_ =
transparent_mode_ &&
(consistent_filter_estimate_not_seen || !converged_filter_seen_);
transparent_mode_ = transparent_mode_ && filter_should_have_converged_;
usable_linear_estimate_ = !echo_saturation_;
if (config_.filter.conservative_initial_phase) {
usable_linear_estimate_ =
usable_linear_estimate_ && filter_has_had_time_to_converge_;
} else {
usable_linear_estimate_ =
usable_linear_estimate_ &&
((filter_has_had_time_to_converge_ && external_delay) ||
converged_filter_seen_);
}
if (config_.filter.conservative_initial_phase) {
usable_linear_estimate_ = usable_linear_estimate_ && external_delay;
}
if (!config_.echo_removal_control.linear_and_stable_echo_path) {
usable_linear_estimate_ =
usable_linear_estimate_ && recently_converged_filter;
}
usable_linear_estimate_ = usable_linear_estimate_ && !TransparentMode();
use_linear_filter_output_ = usable_linear_estimate_ && !TransparentMode();
// Analyze the quality of the filter.
filter_quality_state_.Update(saturation_detector_.SaturatedEcho(),
active_render, SaturatedCapture(),
TransparentMode(), external_delay,
subtractor_output_analyzer_.ConvergedFilter(),
subtractor_output_analyzer_.DivergedFilter());
// Update the reverb estimate.
const bool stationary_block =
config_.echo_audibility.use_stationary_properties &&
echo_audibility_.IsBlockStationary();
reverb_model_estimator_.Update(
filter_analyzer_.GetAdjustedFilter(), adaptive_filter_frequency_response,
erle_estimator_.GetInstLinearQualityEstimate(), filter_delay_blocks_,
usable_linear_estimate_, stationary_block);
reverb_model_estimator_.Update(filter_analyzer_.GetAdjustedFilter(),
adaptive_filter_frequency_response,
erle_estimator_.GetInstLinearQualityEstimate(),
delay_state_.DirectPathFilterDelay(),
UsableLinearEstimate(), stationary_block);
erle_estimator_.Dump(data_dumper_);
reverb_model_estimator_.Dump(data_dumper_.get());
data_dumper_->DumpRaw("aec3_erl", Erl());
data_dumper_->DumpRaw("aec3_erl_time_domain", ErlTimeDomain());
data_dumper_->DumpRaw("aec3_usable_linear_estimate", UsableLinearEstimate());
data_dumper_->DumpRaw("aec3_transparent_mode", transparent_mode_);
data_dumper_->DumpRaw("aec3_state_internal_delay",
internal_delay_ ? *internal_delay_ : -1);
data_dumper_->DumpRaw("aec3_transparent_mode", TransparentMode());
data_dumper_->DumpRaw("aec3_filter_delay", filter_analyzer_.DelayBlocks());
data_dumper_->DumpRaw("aec3_consistent_filter",
filter_analyzer_.Consistent());
data_dumper_->DumpRaw("aec3_suppression_gain_limit", SuppressionGainLimit());
data_dumper_->DumpRaw("aec3_initial_state", initial_state_);
data_dumper_->DumpRaw("aec3_initial_state",
initial_state_.InitialStateActive());
data_dumper_->DumpRaw("aec3_capture_saturation", SaturatedCapture());
data_dumper_->DumpRaw("aec3_echo_saturation", echo_saturation_);
data_dumper_->DumpRaw("aec3_converged_filter", converged_filter);
data_dumper_->DumpRaw("aec3_diverged_filter", diverged_filter);
data_dumper_->DumpRaw("aec3_echo_saturation",
saturation_detector_.SaturatedEcho());
data_dumper_->DumpRaw("aec3_converged_filter",
subtractor_output_analyzer_.ConvergedFilter());
data_dumper_->DumpRaw("aec3_diverged_filter",
subtractor_output_analyzer_.DivergedFilter());
data_dumper_->DumpRaw("aec3_external_delay_avaliable",
external_delay ? 1 : 0);
data_dumper_->DumpRaw("aec3_consistent_filter_estimate_not_seen",
consistent_filter_estimate_not_seen);
data_dumper_->DumpRaw("aec3_filter_should_have_converged",
filter_should_have_converged_);
data_dumper_->DumpRaw("aec3_filter_has_had_time_to_converge",
filter_has_had_time_to_converge_);
data_dumper_->DumpRaw("aec3_recently_converged_filter",
recently_converged_filter);
data_dumper_->DumpRaw("aec3_suppresion_gain_limiter_running",
IsSuppressionGainLimitActive());
data_dumper_->DumpRaw("aec3_filter_tail_freq_resp_est",
GetReverbFrequencyResponse());
}
bool AecState::DetectActiveRender(rtc::ArrayView<const float> x) const {
const float x_energy = std::inner_product(x.begin(), x.end(), x.begin(), 0.f);
return x_energy > (config_.render_levels.active_render_limit *
config_.render_levels.active_render_limit) *
kFftLengthBy2;
AecState::InitialState::InitialState(const EchoCanceller3Config& config)
: conservative_initial_phase_(config.filter.conservative_initial_phase),
initial_state_seconds_(config.filter.initial_state_seconds) {
Reset();
}
void AecState::InitialState::InitialState::Reset() {
initial_state_ = true;
strong_not_saturated_render_blocks_ = 0;
}
void AecState::InitialState::InitialState::Update(bool active_render,
bool saturated_capture) {
strong_not_saturated_render_blocks_ +=
active_render && !saturated_capture ? 1 : 0;
bool AecState::DetectEchoSaturation(rtc::ArrayView<const float> x,
float echo_path_gain) {
RTC_DCHECK_LT(0, x.size());
const float max_sample = fabs(*std::max_element(
x.begin(), x.end(), [](float a, float b) { return a * a < b * b; }));
// Set flag for potential presence of saturated echo
const float kMargin = 10.f;
float peak_echo_amplitude = max_sample * echo_path_gain * kMargin;
if (SaturatedCapture() && peak_echo_amplitude > 32000) {
blocks_since_last_saturation_ = 0;
// Flag whether the initial state is still active.
bool prev_initial_state = initial_state_;
if (conservative_initial_phase_) {
initial_state_ =
strong_not_saturated_render_blocks_ < 5 * kNumBlocksPerSecond;
} else {
++blocks_since_last_saturation_;
initial_state_ = strong_not_saturated_render_blocks_ <
initial_state_seconds_ * kNumBlocksPerSecond;
}
return blocks_since_last_saturation_ < 5;
// Flag whether the transition from the initial state has started.
transition_triggered_ = !initial_state_ && prev_initial_state;
}
AecState::FilterDelay::FilterDelay(const EchoCanceller3Config& config)
: delay_headroom_blocks_(config.delay.delay_headroom_blocks) {}
void AecState::FilterDelay::Update(
const FilterAnalyzer& filter_analyzer,
const absl::optional<DelayEstimate>& external_delay,
size_t blocks_with_proper_filter_adaptation) {
// Update the delay based on the external delay.
if (external_delay &&
(!external_delay_ || external_delay_->delay != external_delay->delay)) {
external_delay_ = external_delay;
external_delay_reported_ = true;
}
// Override the estimated delay if it is not certain that the filter has had
// time to converge.
const bool delay_estimator_may_not_have_converged =
blocks_with_proper_filter_adaptation < 2 * kNumBlocksPerSecond;
if (delay_estimator_may_not_have_converged && external_delay_) {
filter_delay_blocks_ = delay_headroom_blocks_;
} else {
filter_delay_blocks_ = filter_analyzer.DelayBlocks();
}
}
AecState::TransparentMode::TransparentMode(const EchoCanceller3Config& config)
: bounded_erl_(config.ep_strength.bounded_erl),
linear_and_stable_echo_path_(
config.echo_removal_control.linear_and_stable_echo_path),
active_blocks_since_sane_filter_(kBlocksSinceConsistentEstimateInit),
non_converged_sequence_size_(kBlocksSinceConvergencedFilterInit) {}
void AecState::TransparentMode::Reset() {
non_converged_sequence_size_ = kBlocksSinceConvergencedFilterInit;
diverged_sequence_size_ = 0;
strong_not_saturated_render_blocks_ = 0;
if (linear_and_stable_echo_path_) {
recent_convergence_during_activity_ = false;
}
}
void AecState::TransparentMode::Update(int filter_delay_blocks,
bool consistent_filter,
bool converged_filter,
bool diverged_filter,
bool active_render,
bool saturated_capture) {
++capture_block_counter_;
strong_not_saturated_render_blocks_ +=
active_render && !saturated_capture ? 1 : 0;
if (consistent_filter && filter_delay_blocks < 5) {
sane_filter_observed_ = true;
active_blocks_since_sane_filter_ = 0;
} else if (active_render) {
++active_blocks_since_sane_filter_;
}
bool sane_filter_recently_seen;
if (!sane_filter_observed_) {
sane_filter_recently_seen =
capture_block_counter_ <= 5 * kNumBlocksPerSecond;
} else {
sane_filter_recently_seen =
active_blocks_since_sane_filter_ <= 30 * kNumBlocksPerSecond;
}
if (converged_filter) {
recent_convergence_during_activity_ = true;
active_non_converged_sequence_size_ = 0;
non_converged_sequence_size_ = 0;
++num_converged_blocks_;
} else {
if (++non_converged_sequence_size_ > 20 * kNumBlocksPerSecond) {
num_converged_blocks_ = 0;
}
if (active_render &&
++active_non_converged_sequence_size_ > 60 * kNumBlocksPerSecond) {
recent_convergence_during_activity_ = false;
}
}
if (!diverged_filter) {
diverged_sequence_size_ = 0;
} else if (++diverged_sequence_size_ >= 60) {
// TODO(peah): Change these lines to ensure proper triggering of usable
// filter.
non_converged_sequence_size_ = kBlocksSinceConvergencedFilterInit;
}
if (active_non_converged_sequence_size_ > 60 * kNumBlocksPerSecond) {
finite_erl_recently_detected_ = false;
}
if (num_converged_blocks_ > 50) {
finite_erl_recently_detected_ = true;
}
if (bounded_erl_) {
transparency_activated_ = false;
} else if (finite_erl_recently_detected_) {
transparency_activated_ = false;
} else if (sane_filter_recently_seen && recent_convergence_during_activity_) {
transparency_activated_ = false;
} else {
const bool filter_should_have_converged =
strong_not_saturated_render_blocks_ > 6 * kNumBlocksPerSecond;
transparency_activated_ = filter_should_have_converged;
}
}
AecState::FilteringQualityAnalyzer::FilteringQualityAnalyzer(
const EchoCanceller3Config& config)
: conservative_initial_phase_(config.filter.conservative_initial_phase),
required_blocks_for_convergence_(
kNumBlocksPerSecond * (conservative_initial_phase_ ? 1.5f : 0.8f)),
linear_and_stable_echo_path_(
config.echo_removal_control.linear_and_stable_echo_path),
non_converged_sequence_size_(kBlocksSinceConvergencedFilterInit) {}
void AecState::FilteringQualityAnalyzer::Reset() {
usable_linear_estimate_ = false;
strong_not_saturated_render_blocks_ = 0;
if (linear_and_stable_echo_path_) {
recent_convergence_during_activity_ = false;
}
diverged_sequence_size_ = 0;
// TODO(peah): Change to ensure proper triggering of usable filter.
non_converged_sequence_size_ = 10000;
recent_convergence_ = true;
}
void AecState::FilteringQualityAnalyzer::Update(
bool saturated_echo,
bool active_render,
bool saturated_capture,
bool transparent_mode,
const absl::optional<DelayEstimate>& external_delay,
bool converged_filter,
bool diverged_filter) {
diverged_sequence_size_ = diverged_filter ? diverged_sequence_size_ + 1 : 0;
if (diverged_sequence_size_ >= 60) {
// TODO(peah): Change these lines to ensure proper triggering of usable
// filter.
non_converged_sequence_size_ = 10000;
recent_convergence_ = true;
}
if (converged_filter) {
non_converged_sequence_size_ = 0;
recent_convergence_ = true;
active_non_converged_sequence_size_ = 0;
recent_convergence_during_activity_ = true;
} else {
if (++non_converged_sequence_size_ >= 60 * kNumBlocksPerSecond) {
recent_convergence_ = false;
}
if (active_render &&
++active_non_converged_sequence_size_ > 60 * kNumBlocksPerSecond) {
recent_convergence_during_activity_ = false;
}
}
strong_not_saturated_render_blocks_ +=
active_render && !saturated_capture ? 1 : 0;
const bool filter_has_had_time_to_converge =
strong_not_saturated_render_blocks_ > required_blocks_for_convergence_;
usable_linear_estimate_ = filter_has_had_time_to_converge && external_delay;
if (!conservative_initial_phase_ && recent_convergence_during_activity_) {
usable_linear_estimate_ = true;
}
if (!linear_and_stable_echo_path_ && !recent_convergence_) {
usable_linear_estimate_ = false;
}
if (saturated_echo || transparent_mode) {
usable_linear_estimate_ = false;
}
}
AecState::SaturationDetector::SaturationDetector(
const EchoCanceller3Config& config)
: echo_can_saturate_(config.ep_strength.echo_can_saturate),
not_saturated_sequence_size_(1000) {}
void AecState::SaturationDetector::Reset() {
not_saturated_sequence_size_ = 0;
}
void AecState::SaturationDetector::Update(rtc::ArrayView<const float> x,
bool saturated_capture,
float echo_path_gain) {
if (!echo_can_saturate_) {
saturated_echo_ = false;
return;
}
RTC_DCHECK_LT(0, x.size());
if (saturated_capture) {
const float max_sample = fabs(*std::max_element(
x.begin(), x.end(), [](float a, float b) { return a * a < b * b; }));
// Set flag for potential presence of saturated echo
const float kMargin = 10.f;
float peak_echo_amplitude = max_sample * echo_path_gain * kMargin;
if (peak_echo_amplitude > 32000) {
not_saturated_sequence_size_ = 0;
saturated_echo_ = true;
return;
}
}
saturated_echo_ = ++not_saturated_sequence_size_ < 5;
}
} // namespace webrtc

View File

@ -32,7 +32,6 @@
#include "modules/audio_processing/aec3/subtractor_output.h"
#include "modules/audio_processing/aec3/subtractor_output_analyzer.h"
#include "modules/audio_processing/aec3/suppression_gain_limiter.h"
#include "rtc_base/constructormagic.h"
namespace webrtc {
@ -46,10 +45,14 @@ class AecState {
// Returns whether the echo subtractor can be used to determine the residual
// echo.
bool UsableLinearEstimate() const { return usable_linear_estimate_; }
bool UsableLinearEstimate() const {
return filter_quality_state_.LinearFilterUsable();
}
// Returns whether the echo subtractor output should be used as output.
bool UseLinearFilterOutput() const { return use_linear_filter_output_; }
bool UseLinearFilterOutput() const {
return filter_quality_state_.LinearFilterUsable();
}
// Returns the estimated echo path gain.
float EchoPathGain() const { return filter_analyzer_.Gain(); }
@ -59,10 +62,7 @@ class AecState {
// Returns the appropriate scaling of the residual echo to match the
// audibility.
void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const {
echo_audibility_.GetResidualEchoScaling(filter_has_had_time_to_converge_,
residual_scaling);
}
void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const;
// Returns whether the stationary properties of the signals are used in the
// aec.
@ -75,13 +75,11 @@ class AecState {
return erle_estimator_.Erle();
}
// Returns any uncertainty in the ERLE estimate.
absl::optional<float> ErleUncertainty() const {
if (!filter_has_had_time_to_converge_) {
return 1.f;
}
return absl::nullopt;
}
// Returns an offset to apply to the estimation of the residual echo
// computation. Returning nullopt means that no offset should be used, while
// any other value will be applied as a multiplier to the estimated residual
// echo.
absl::optional<float> ErleUncertainty() const;
// Returns the fullband ERLE estimate in log2 units.
float FullBandErleLog2() const { return erle_estimator_.FullbandErleLog2(); }
@ -95,16 +93,13 @@ class AecState {
float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
// Returns the delay estimate based on the linear filter.
int FilterDelayBlocks() const { return filter_delay_blocks_; }
// Returns the internal delay estimate based on the linear filter.
absl::optional<int> InternalDelay() const { return internal_delay_; }
int FilterDelayBlocks() const { return delay_state_.DirectPathFilterDelay(); }
// Returns whether the capture signal is saturated.
bool SaturatedCapture() const { return capture_signal_saturation_; }
// Returns whether the echo signal is saturated.
bool SaturatedEcho() const { return echo_saturation_; }
bool SaturatedEcho() const { return saturation_detector_.SaturatedEcho(); }
// Updates the capture signal saturation.
void UpdateCaptureSaturation(bool capture_signal_saturation) {
@ -112,7 +107,7 @@ class AecState {
}
// Returns whether the transparent mode is active
bool TransparentMode() const { return transparent_mode_; }
bool TransparentMode() const { return transparent_state_.Active(); }
// Takes appropriate action at an echo path change.
void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
@ -135,14 +130,11 @@ class AecState {
return suppression_gain_limiter_.IsActive();
}
// Returns whether the linear filter should have been able to properly adapt.
bool FilterHasHadTimeToConverge() const {
return filter_has_had_time_to_converge_;
}
// Returns whether the transition for going out of the initial stated has
// been triggered.
bool TransitionTriggered() const { return transition_triggered_; }
bool TransitionTriggered() const {
return initial_state_.TransitionTriggered();
}
// Updates the aec state.
void Update(const absl::optional<DelayEstimate>& external_delay,
@ -161,7 +153,6 @@ class AecState {
}
private:
bool DetectActiveRender(rtc::ArrayView<const float> x) const;
void UpdateSuppressorGainLimit(bool render_activity);
bool DetectEchoSaturation(rtc::ArrayView<const float> x,
float echo_path_gain);
@ -169,46 +160,169 @@ class AecState {
static int instance_count_;
std::unique_ptr<ApmDataDumper> data_dumper_;
const EchoCanceller3Config config_;
// Class for controlling the transition from the intial state, which in turn
// controls when the filter parameters for the initial state should be used.
class InitialState {
public:
explicit InitialState(const EchoCanceller3Config& config);
// Resets the state to again begin in the initial state.
void Reset();
// Updates the state based on new data.
void Update(bool active_render, bool saturated_capture);
// Returns whether the initial state is active or not.
bool InitialStateActive() const { return initial_state_; }
// Returns that the transition from the initial state has was started.
bool TransitionTriggered() const { return transition_triggered_; }
private:
const bool conservative_initial_phase_;
const float initial_state_seconds_;
bool transition_triggered_ = false;
bool initial_state_ = true;
size_t strong_not_saturated_render_blocks_ = 0;
} initial_state_;
// Class for choosing the direct-path delay relative to the beginning of the
// filter, as well as any other data related to the delay used within
// AecState.
class FilterDelay {
public:
explicit FilterDelay(const EchoCanceller3Config& config);
// Returns whether an external delay has been reported to the AecState (from
// the delay estimator).
bool ExternalDelayReported() const { return external_delay_reported_; }
// Returns the delay in blocks relative to the beginning of the filter that
// corresponds to the direct path of the echo.
int DirectPathFilterDelay() const { return filter_delay_blocks_; }
// Updates the delay estimates based on new data.
void Update(const FilterAnalyzer& filter_analyzer,
const absl::optional<DelayEstimate>& external_delay,
size_t blocks_with_proper_filter_adaptation);
private:
const int delay_headroom_blocks_;
bool external_delay_reported_ = false;
int filter_delay_blocks_ = 0;
absl::optional<DelayEstimate> external_delay_;
} delay_state_;
// Class for detecting and toggling the transparent mode which causes the
// suppressor to apply no suppression.
class TransparentMode {
public:
explicit TransparentMode(const EchoCanceller3Config& config);
// Returns whether the transparent mode should be active.
bool Active() const { return transparency_activated_; }
// Resets the state of the detector.
void Reset();
// Updates the detection deciscion based on new data.
void Update(int filter_delay_blocks,
bool consistent_filter,
bool converged_filter,
bool diverged_filter,
bool active_render,
bool saturated_capture);
private:
const bool bounded_erl_;
const bool linear_and_stable_echo_path_;
size_t capture_block_counter_ = 0;
bool transparency_activated_ = false;
size_t active_blocks_since_sane_filter_;
bool sane_filter_observed_ = false;
bool finite_erl_recently_detected_ = false;
size_t non_converged_sequence_size_;
size_t diverged_sequence_size_ = 0;
size_t active_non_converged_sequence_size_ = 0;
size_t num_converged_blocks_ = 0;
bool recent_convergence_during_activity_ = false;
size_t strong_not_saturated_render_blocks_ = 0;
} transparent_state_;
// Class for analyzing how well the linear filter is, and can be expected to,
// perform on the current signals. The purpose of this is for using to
// select the echo suppression functionality as well as the input to the echo
// suppressor.
class FilteringQualityAnalyzer {
public:
explicit FilteringQualityAnalyzer(const EchoCanceller3Config& config);
// Returns whether the the linear filter is can be used for the echo
// canceller output.
bool LinearFilterUsable() const { return usable_linear_estimate_; }
// Resets the state of the analyzer.
void Reset();
// Updates the analysis based on new data.
void Update(bool saturated_echo,
bool active_render,
bool saturated_capture,
bool transparent_mode,
const absl::optional<DelayEstimate>& external_delay,
bool converged_filter,
bool diverged_filter);
private:
const bool conservative_initial_phase_;
const float required_blocks_for_convergence_;
const bool linear_and_stable_echo_path_;
bool usable_linear_estimate_ = false;
size_t strong_not_saturated_render_blocks_ = 0;
size_t non_converged_sequence_size_;
size_t diverged_sequence_size_ = 0;
size_t active_non_converged_sequence_size_ = 0;
bool recent_convergence_during_activity_ = false;
bool recent_convergence_ = false;
} filter_quality_state_;
// Class for detecting whether the echo is to be considered to be saturated.
// The purpose of this is to allow customized behavior in the echo suppressor
// for when the echo is saturated.
class SaturationDetector {
public:
explicit SaturationDetector(const EchoCanceller3Config& config);
// Returns whether the echo is to be considered saturated.
bool SaturatedEcho() const { return saturated_echo_; };
// Resets the state of the detector.
void Reset();
// Updates the detection decision based on new data.
void Update(rtc::ArrayView<const float> x,
bool saturated_capture,
float echo_path_gain);
private:
const bool echo_can_saturate_;
size_t not_saturated_sequence_size_;
bool saturated_echo_ = false;
} saturation_detector_;
ErlEstimator erl_estimator_;
ErleEstimator erle_estimator_;
size_t capture_block_counter_ = 0;
size_t blocks_since_reset_ = 0;
size_t blocks_with_proper_filter_adaptation_ = 0;
size_t strong_not_saturated_render_blocks_ = 0;
size_t blocks_with_active_render_ = 0;
bool usable_linear_estimate_ = false;
bool capture_signal_saturation_ = false;
bool echo_saturation_ = false;
bool transparent_mode_ = false;
bool render_received_ = false;
int filter_delay_blocks_ = 0;
size_t blocks_since_last_saturation_ = 1000;
std::vector<float> max_render_;
bool filter_has_had_time_to_converge_ = false;
bool initial_state_ = true;
bool transition_triggered_ = false;
const float gain_rampup_increase_;
SuppressionGainUpperLimiter suppression_gain_limiter_;
FilterAnalyzer filter_analyzer_;
bool use_linear_filter_output_ = false;
absl::optional<int> internal_delay_;
size_t diverged_blocks_ = 0;
bool filter_should_have_converged_ = false;
size_t blocks_since_converged_filter_;
size_t active_blocks_since_consistent_filter_estimate_;
bool converged_filter_seen_ = false;
bool consistent_filter_seen_ = false;
bool external_delay_seen_ = false;
absl::optional<DelayEstimate> external_delay_;
size_t frames_since_external_delay_change_ = 0;
size_t converged_filter_count_ = 0;
bool finite_erl_ = false;
size_t active_blocks_since_converged_filter_ = 0;
EchoAudibility echo_audibility_;
ReverbModelEstimator reverb_model_estimator_;
SubtractorOutputAnalyzer subtractor_output_analyzer_;
bool enable_erle_resets_at_gain_changes_ = true;
RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
};
} // namespace webrtc

View File

@ -22,7 +22,8 @@ constexpr float kMaxErl = 1000.f;
} // namespace
ErlEstimator::ErlEstimator() {
ErlEstimator::ErlEstimator(size_t startup_phase_length_blocks_)
: startup_phase_length_blocks__(startup_phase_length_blocks_) {
erl_.fill(kMaxErl);
hold_counters_.fill(0);
erl_time_domain_ = kMaxErl;
@ -31,7 +32,12 @@ ErlEstimator::ErlEstimator() {
ErlEstimator::~ErlEstimator() = default;
void ErlEstimator::Update(rtc::ArrayView<const float> render_spectrum,
void ErlEstimator::Reset() {
blocks_since_reset_ = 0;
}
void ErlEstimator::Update(bool converged_filter,
rtc::ArrayView<const float> render_spectrum,
rtc::ArrayView<const float> capture_spectrum) {
RTC_DCHECK_EQ(kFftLengthBy2Plus1, render_spectrum.size());
RTC_DCHECK_EQ(kFftLengthBy2Plus1, capture_spectrum.size());
@ -41,6 +47,11 @@ void ErlEstimator::Update(rtc::ArrayView<const float> render_spectrum,
// Corresponds to WGN of power -46 dBFS.
constexpr float kX2Min = 44015068.0f;
if (++blocks_since_reset_ < startup_phase_length_blocks__ ||
!converged_filter) {
return;
}
// Update the estimates in a maximum statistics manner.
for (size_t k = 1; k < kFftLengthBy2; ++k) {
if (X2[k] > kX2Min) {

View File

@ -22,11 +22,15 @@ namespace webrtc {
// Estimates the echo return loss based on the signal spectra.
class ErlEstimator {
public:
ErlEstimator();
explicit ErlEstimator(size_t startup_phase_length_blocks_);
~ErlEstimator();
// Resets the ERL estimation.
void Reset();
// Updates the ERL estimate.
void Update(rtc::ArrayView<const float> render_spectrum,
void Update(bool converged_filter,
rtc::ArrayView<const float> render_spectrum,
rtc::ArrayView<const float> capture_spectrum);
// Returns the most recent ERL estimate.
@ -34,11 +38,12 @@ class ErlEstimator {
float ErlTimeDomain() const { return erl_time_domain_; }
private:
const size_t startup_phase_length_blocks__;
std::array<float, kFftLengthBy2Plus1> erl_;
std::array<int, kFftLengthBy2Minus1> hold_counters_;
float erl_time_domain_;
int hold_counter_time_domain_;
size_t blocks_since_reset_ = 0;
RTC_DISALLOW_COPY_AND_ASSIGN(ErlEstimator);
};

View File

@ -31,13 +31,13 @@ TEST(ErlEstimator, Estimates) {
std::array<float, kFftLengthBy2Plus1> X2;
std::array<float, kFftLengthBy2Plus1> Y2;
ErlEstimator estimator;
ErlEstimator estimator(0);
// Verifies that the ERL estimate is properly reduced to lower values.
X2.fill(500 * 1000.f * 1000.f);
Y2.fill(10 * X2[0]);
for (size_t k = 0; k < 200; ++k) {
estimator.Update(X2, Y2);
estimator.Update(true, X2, Y2);
}
VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 10.f);
@ -45,18 +45,18 @@ TEST(ErlEstimator, Estimates) {
// increases.
Y2.fill(10000 * X2[0]);
for (size_t k = 0; k < 998; ++k) {
estimator.Update(X2, Y2);
estimator.Update(true, X2, Y2);
}
VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 10.f);
// Verifies that the rate of increase is 3 dB.
estimator.Update(X2, Y2);
estimator.Update(true, X2, Y2);
VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 20.f);
// Verifies that the maximum ERL is achieved when there are no low RLE
// estimates.
for (size_t k = 0; k < 1000; ++k) {
estimator.Update(X2, Y2);
estimator.Update(true, X2, Y2);
}
VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 1000.f);
@ -64,7 +64,7 @@ TEST(ErlEstimator, Estimates) {
X2.fill(1000.f * 1000.f);
Y2.fill(10 * X2[0]);
for (size_t k = 0; k < 200; ++k) {
estimator.Update(X2, Y2);
estimator.Update(true, X2, Y2);
}
VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 1000.f);
}

View File

@ -16,19 +16,24 @@
namespace webrtc {
ErleEstimator::ErleEstimator(float min_erle,
ErleEstimator::ErleEstimator(size_t startup_phase_length_blocks_,
float min_erle,
float max_erle_lf,
float max_erle_hf)
: fullband_erle_estimator_(min_erle, max_erle_lf),
: startup_phase_length_blocks__(startup_phase_length_blocks_),
fullband_erle_estimator_(min_erle, max_erle_lf),
subband_erle_estimator_(min_erle, max_erle_lf, max_erle_hf) {
Reset();
Reset(true);
}
ErleEstimator::~ErleEstimator() = default;
void ErleEstimator::Reset() {
void ErleEstimator::Reset(bool delay_change) {
fullband_erle_estimator_.Reset();
subband_erle_estimator_.Reset();
if (delay_change) {
blocks_since_reset_ = 0;
}
}
void ErleEstimator::Update(rtc::ArrayView<const float> render_spectrum,
@ -43,6 +48,10 @@ void ErleEstimator::Update(rtc::ArrayView<const float> render_spectrum,
const auto& Y2 = capture_spectrum;
const auto& E2 = subtractor_spectrum;
if (++blocks_since_reset_ < startup_phase_length_blocks__) {
return;
}
subband_erle_estimator_.Update(X2, Y2, E2, converged_filter, onset_detection);
fullband_erle_estimator_.Update(X2, Y2, E2, converged_filter);
}

View File

@ -27,11 +27,14 @@ namespace webrtc {
// and another one is done using the aggreation of energy over all the subbands.
class ErleEstimator {
public:
ErleEstimator(float min_erle, float max_erle_lf, float max_erle_hf);
ErleEstimator(size_t startup_phase_length_blocks_,
float min_erle,
float max_erle_lf,
float max_erle_hf);
~ErleEstimator();
// Resets the fullband ERLE estimator and the subbands ERLE estimators.
void Reset();
void Reset(bool delay_change);
// Updates the ERLE estimates.
void Update(rtc::ArrayView<const float> render_spectrum,
@ -66,8 +69,10 @@ class ErleEstimator {
void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const;
private:
const size_t startup_phase_length_blocks__;
FullBandErleEstimator fullband_erle_estimator_;
SubbandErleEstimator subband_erle_estimator_;
size_t blocks_since_reset_ = 0;
};
} // namespace webrtc

View File

@ -68,7 +68,7 @@ TEST(ErleEstimator, VerifyErleIncreaseAndHold) {
std::array<float, kFftLengthBy2Plus1> E2;
std::array<float, kFftLengthBy2Plus1> Y2;
ErleEstimator estimator(kMinErle, kMaxErleLf, kMaxErleHf);
ErleEstimator estimator(0, kMinErle, kMaxErleLf, kMaxErleHf);
// Verifies that the ERLE estimate is properly increased to higher values.
FormFarendFrame(&X2, &E2, &Y2, kTrueErle);
@ -94,7 +94,7 @@ TEST(ErleEstimator, VerifyErleTrackingOnOnsets) {
std::array<float, kFftLengthBy2Plus1> E2;
std::array<float, kFftLengthBy2Plus1> Y2;
ErleEstimator estimator(kMinErle, kMaxErleLf, kMaxErleHf);
ErleEstimator estimator(0, kMinErle, kMaxErleLf, kMaxErleHf);
for (size_t burst = 0; burst < 20; ++burst) {
FormFarendFrame(&X2, &E2, &Y2, kTrueErleOnsets);