Debian Codegraph @

Xpjproject/third_party/webrtc_aec3/src/modules/audio_processing/agc2/adaptive_agc.cc

raw symbols SCIP
1 /*
2 * Copyright (c) 2018 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 #include "modules/audio_processing/agc2/adaptive_agc.h"
12
13 #include "common_audio/include/audio_util.h"
14 #include "modules/audio_processing/agc2/cpu_features.h"
15 #include "modules/audio_processing/agc2/vad_with_level.h"
16 #include "modules/audio_processing/logging/apm_data_dumper.h"
17 #include "rtc_base/checks.h"
18 #include "rtc_base/logging.h"
19
20 namespace webrtc {
21 namespace {
22
23 using AdaptiveDigitalConfig =
24 AudioProcessing::Config::GainController2::AdaptiveDigital;
25 using NoiseEstimatorType =
26 AudioProcessing::Config::GainController2::NoiseEstimator;
27
28 constexpr int kGainApplierAdjacentSpeechFramesThreshold = 1;
29 constexpr float kMaxGainChangePerSecondDb = 3.0f;
30 constexpr float kMaxOutputNoiseLevelDbfs = -50.0f;
31
32 // Detects the available CPU features and applies any kill-switches.
33 AvailableCpuFeatures GetAllowedCpuFeatures(
34 const AdaptiveDigitalConfig& config) {
35 AvailableCpuFeatures features = GetAvailableCpuFeatures();
36 if (!config.sse2_allowed) {
37 features.sse2 = false;
38 }
39 if (!config.avx2_allowed) {
40 features.avx2 = false;
41 }
42 if (!config.neon_allowed) {
43 features.neon = false;
44 }
45 return features;
46 }
47
48 std::unique_ptr<NoiseLevelEstimator> CreateNoiseLevelEstimator(
49 NoiseEstimatorType estimator_type,
50 ApmDataDumper* apm_data_dumper) {
51 switch (estimator_type) {
52 case NoiseEstimatorType::kStationaryNoise:
53 return CreateStationaryNoiseEstimator(apm_data_dumper);
54 case NoiseEstimatorType::kNoiseFloor:
55 return CreateNoiseFloorEstimator(apm_data_dumper);
56 }
57 }
58
59 constexpr NoiseEstimatorType kDefaultNoiseLevelEstimatorType =
60 NoiseEstimatorType::kNoiseFloor;
61
62 } // namespace
63
64 AdaptiveAgc::AdaptiveAgc(ApmDataDumper* apm_data_dumper)
65 : speech_level_estimator_(apm_data_dumper),
66 gain_controller_(apm_data_dumper,
67 kGainApplierAdjacentSpeechFramesThreshold,
68 kMaxGainChangePerSecondDb,
69 kMaxOutputNoiseLevelDbfs),
70 apm_data_dumper_(apm_data_dumper),
71 noise_level_estimator_(
72 CreateNoiseLevelEstimator(kDefaultNoiseLevelEstimatorType,
73 apm_data_dumper)),
74 saturation_protector_(
75 CreateSaturationProtector(kSaturationProtectorInitialHeadroomDb,
76 kSaturationProtectorExtraHeadroomDb,
77 kGainApplierAdjacentSpeechFramesThreshold,
78 apm_data_dumper)) {
79 RTC_DCHECK(apm_data_dumper);
80 }
81
82 AdaptiveAgc::AdaptiveAgc(ApmDataDumper* apm_data_dumper,
83 const AdaptiveDigitalConfig& config)
84 : speech_level_estimator_(apm_data_dumper,
85 config.adjacent_speech_frames_threshold),
86 vad_(config.vad_reset_period_ms, GetAllowedCpuFeatures(config)),
87 gain_controller_(apm_data_dumper,
88 config.adjacent_speech_frames_threshold,
89 config.max_gain_change_db_per_second,
90 config.max_output_noise_level_dbfs),
91 apm_data_dumper_(apm_data_dumper),
92 noise_level_estimator_(
93 CreateNoiseLevelEstimator(config.noise_estimator, apm_data_dumper)),
94 saturation_protector_(
95 CreateSaturationProtector(kSaturationProtectorInitialHeadroomDb,
96 kSaturationProtectorExtraHeadroomDb,
97 config.adjacent_speech_frames_threshold,
98 apm_data_dumper)) {
99 RTC_DCHECK(apm_data_dumper);
100 RTC_DCHECK(noise_level_estimator_);
101 RTC_DCHECK(saturation_protector_);
102 if (!config.use_saturation_protector) {
103 RTC_LOG(LS_WARNING) << "The saturation protector cannot be disabled.";
104 }
105 }
106
107 AdaptiveAgc::~AdaptiveAgc() = default;
108
109 void AdaptiveAgc::Process(AudioFrameView<float> frame, float limiter_envelope) {
110 AdaptiveDigitalGainApplier::FrameInfo info;
111
112 VadLevelAnalyzer::Result vad_result = vad_.AnalyzeFrame(frame);
113 info.speech_probability = vad_result.speech_probability;
114 apm_data_dumper_->DumpRaw("agc2_speech_probability",
115 vad_result.speech_probability);
116 apm_data_dumper_->DumpRaw("agc2_input_rms_dbfs", vad_result.rms_dbfs);
117 apm_data_dumper_->DumpRaw("agc2_input_peak_dbfs", vad_result.peak_dbfs);
118
119 speech_level_estimator_.Update(vad_result);
120 info.speech_level_dbfs = speech_level_estimator_.level_dbfs();
121 info.speech_level_reliable = speech_level_estimator_.IsConfident();
122 apm_data_dumper_->DumpRaw("agc2_speech_level_dbfs", info.speech_level_dbfs);
123 apm_data_dumper_->DumpRaw("agc2_speech_level_reliable",
124 info.speech_level_reliable);
125
126 info.noise_rms_dbfs = noise_level_estimator_->Analyze(frame);
127 apm_data_dumper_->DumpRaw("agc2_noise_rms_dbfs", info.noise_rms_dbfs);
128
129 saturation_protector_->Analyze(info.speech_probability, vad_result.peak_dbfs,
130 info.speech_level_dbfs);
131 info.headroom_db = saturation_protector_->HeadroomDb();
132 apm_data_dumper_->DumpRaw("agc2_headroom_db", info.headroom_db);
133
134 info.limiter_envelope_dbfs = FloatS16ToDbfs(limiter_envelope);
135 apm_data_dumper_->DumpRaw("agc2_limiter_envelope_dbfs",
136 info.limiter_envelope_dbfs);
137
138 gain_controller_.Process(info, frame);
139 }
140
141 void AdaptiveAgc::HandleInputGainChange() {
142 speech_level_estimator_.Reset();
143 saturation_protector_->Reset();
144 }
145
146 } // namespace webrtc