xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/matched_filter_lag_aggregator.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_AUDIO_PROCESSING_AEC3_MATCHED_FILTER_LAG_AGGREGATOR_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_MATCHED_FILTER_LAG_AGGREGATOR_H_
13 
14 #include <vector>
15 
16 #include "absl/types/optional.h"
17 #include "api/audio/echo_canceller3_config.h"
18 #include "modules/audio_processing/aec3/delay_estimate.h"
19 #include "modules/audio_processing/aec3/matched_filter.h"
20 
21 namespace webrtc {
22 
23 class ApmDataDumper;
24 
25 // Aggregates lag estimates produced by the MatchedFilter class into a single
26 // reliable combined lag estimate.
27 class MatchedFilterLagAggregator {
28  public:
29   MatchedFilterLagAggregator(ApmDataDumper* data_dumper,
30                              size_t max_filter_lag,
31                              const EchoCanceller3Config::Delay& delay_config);
32 
33   MatchedFilterLagAggregator() = delete;
34   MatchedFilterLagAggregator(const MatchedFilterLagAggregator&) = delete;
35   MatchedFilterLagAggregator& operator=(const MatchedFilterLagAggregator&) =
36       delete;
37 
38   ~MatchedFilterLagAggregator();
39 
40   // Resets the aggregator.
41   void Reset(bool hard_reset);
42 
43   // Aggregates the provided lag estimates.
44   absl::optional<DelayEstimate> Aggregate(
45       const absl::optional<const MatchedFilter::LagEstimate>& lag_estimate);
46 
47   // Returns whether a reliable delay estimate has been found.
ReliableDelayFound()48   bool ReliableDelayFound() const { return significant_candidate_found_; }
49 
50   // Returns the delay candidate that is computed by looking at the highest peak
51   // on the matched filters.
GetDelayAtHighestPeak()52   int GetDelayAtHighestPeak() const {
53     return highest_peak_aggregator_.candidate();
54   }
55 
56  private:
57   class PreEchoLagAggregator {
58    public:
59     PreEchoLagAggregator(size_t max_filter_lag, size_t down_sampling_factor);
60     void Reset();
61     void Aggregate(int pre_echo_lag);
pre_echo_candidate()62     int pre_echo_candidate() const { return pre_echo_candidate_; }
63     void Dump(ApmDataDumper* const data_dumper);
64 
65    private:
66     const int block_size_log2_;
67     std::array<int, 250> histogram_data_;
68     std::vector<int> histogram_;
69     int histogram_data_index_ = 0;
70     int pre_echo_candidate_ = 0;
71   };
72 
73   class HighestPeakAggregator {
74    public:
75     explicit HighestPeakAggregator(size_t max_filter_lag);
76     void Reset();
77     void Aggregate(int lag);
candidate()78     int candidate() const { return candidate_; }
histogram()79     rtc::ArrayView<const int> histogram() const { return histogram_; }
80 
81    private:
82     std::vector<int> histogram_;
83     std::array<int, 250> histogram_data_;
84     int histogram_data_index_ = 0;
85     int candidate_ = -1;
86   };
87 
88   ApmDataDumper* const data_dumper_;
89   bool significant_candidate_found_ = false;
90   const EchoCanceller3Config::Delay::DelaySelectionThresholds thresholds_;
91   const int headroom_;
92   HighestPeakAggregator highest_peak_aggregator_;
93   std::unique_ptr<PreEchoLagAggregator> pre_echo_lag_aggregator_;
94 };
95 }  // namespace webrtc
96 
97 #endif  // MODULES_AUDIO_PROCESSING_AEC3_MATCHED_FILTER_LAG_AGGREGATOR_H_
98