alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 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 | |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 11 | #include "modules/audio_processing/gain_controller2.h" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 12 | |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 13 | #include <memory> |
| 14 | #include <utility> |
| 15 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 16 | #include "common_audio/include/audio_util.h" |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 17 | #include "modules/audio_processing/agc2/cpu_features.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/audio_processing/audio_buffer.h" |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 19 | #include "modules/audio_processing/include/audio_frame_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 22 | #include "rtc_base/logging.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 23 | #include "rtc_base/strings/string_builder.h" |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 24 | #include "system_wrappers/include/field_trial.h" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 27 | namespace { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 28 | |
| 29 | using Agc2Config = AudioProcessing::Config::GainController2; |
| 30 | |
| 31 | constexpr int kUnspecifiedAnalogLevel = -1; |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 32 | constexpr int kLogLimiterStatsPeriodMs = 30'000; |
| 33 | constexpr int kFrameLengthMs = 10; |
| 34 | constexpr int kLogLimiterStatsPeriodNumFrames = |
| 35 | kLogLimiterStatsPeriodMs / kFrameLengthMs; |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 36 | |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 37 | // Detects the available CPU features and applies any kill-switches. |
| 38 | AvailableCpuFeatures GetAllowedCpuFeatures() { |
| 39 | AvailableCpuFeatures features = GetAvailableCpuFeatures(); |
| 40 | if (field_trial::IsEnabled("WebRTC-Agc2SimdSse2KillSwitch")) { |
| 41 | features.sse2 = false; |
| 42 | } |
| 43 | if (field_trial::IsEnabled("WebRTC-Agc2SimdAvx2KillSwitch")) { |
| 44 | features.avx2 = false; |
| 45 | } |
| 46 | if (field_trial::IsEnabled("WebRTC-Agc2SimdNeonKillSwitch")) { |
| 47 | features.neon = false; |
| 48 | } |
| 49 | return features; |
| 50 | } |
| 51 | |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 52 | // Creates an adaptive digital gain controller if enabled. |
Alessio Bazzica | 2fa4618 | 2021-10-26 14:08:23 +0200 | [diff] [blame] | 53 | std::unique_ptr<AdaptiveDigitalGainController> CreateAdaptiveDigitalController( |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 54 | const Agc2Config::AdaptiveDigital& config, |
| 55 | int sample_rate_hz, |
| 56 | int num_channels, |
| 57 | ApmDataDumper* data_dumper) { |
| 58 | if (config.enabled) { |
Alessio Bazzica | 2fa4618 | 2021-10-26 14:08:23 +0200 | [diff] [blame] | 59 | return std::make_unique<AdaptiveDigitalGainController>( |
| 60 | data_dumper, config, sample_rate_hz, num_channels); |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 61 | } |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 65 | } // namespace |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 66 | |
Niels Möller | 7a66900 | 2022-06-27 09:47:02 +0200 | [diff] [blame^] | 67 | std::atomic<int> GainController2::instance_count_(0); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 68 | |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 69 | GainController2::GainController2(const Agc2Config& config, |
| 70 | int sample_rate_hz, |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 71 | int num_channels, |
| 72 | bool use_internal_vad) |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 73 | : cpu_features_(GetAllowedCpuFeatures()), |
Niels Möller | 7a66900 | 2022-06-27 09:47:02 +0200 | [diff] [blame^] | 74 | data_dumper_(instance_count_.fetch_add(1) + 1), |
Alessio Bazzica | 60f675f | 2021-10-15 15:36:11 +0200 | [diff] [blame] | 75 | fixed_gain_applier_( |
| 76 | /*hard_clip_samples=*/false, |
| 77 | /*initial_gain_factor=*/DbToRatio(config.fixed_digital.gain_db)), |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 78 | adaptive_digital_controller_( |
| 79 | CreateAdaptiveDigitalController(config.adaptive_digital, |
| 80 | sample_rate_hz, |
| 81 | num_channels, |
| 82 | &data_dumper_)), |
| 83 | limiter_(sample_rate_hz, &data_dumper_, /*histogram_name_prefix=*/"Agc2"), |
| 84 | calls_since_last_limiter_log_(0), |
| 85 | analog_level_(kUnspecifiedAnalogLevel) { |
| 86 | RTC_DCHECK(Validate(config)); |
| 87 | data_dumper_.InitiateNewSetOfRecordings(); |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 88 | const bool use_vad = config.adaptive_digital.enabled; |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 89 | if (use_vad && use_internal_vad) { |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 90 | // TODO(bugs.webrtc.org/7494): Move `vad_reset_period_ms` from adaptive |
| 91 | // digital to gain controller 2 config. |
| 92 | vad_ = std::make_unique<VoiceActivityDetectorWrapper>( |
| 93 | config.adaptive_digital.vad_reset_period_ms, cpu_features_, |
| 94 | sample_rate_hz); |
| 95 | } |
Per Åhgren | 2bd85ab | 2020-01-03 10:36:34 +0100 | [diff] [blame] | 96 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 97 | |
| 98 | GainController2::~GainController2() = default; |
| 99 | |
Alessio Bazzica | d66a605 | 2021-04-29 16:13:25 +0200 | [diff] [blame] | 100 | void GainController2::Initialize(int sample_rate_hz, int num_channels) { |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 101 | RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz || |
| 102 | sample_rate_hz == AudioProcessing::kSampleRate16kHz || |
| 103 | sample_rate_hz == AudioProcessing::kSampleRate32kHz || |
| 104 | sample_rate_hz == AudioProcessing::kSampleRate48kHz); |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 105 | // TODO(bugs.webrtc.org/7494): Initialize `fixed_gain_applier_`. |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 106 | limiter_.SetSampleRate(sample_rate_hz); |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 107 | if (vad_) { |
| 108 | vad_->Initialize(sample_rate_hz); |
| 109 | } |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 110 | if (adaptive_digital_controller_) { |
| 111 | adaptive_digital_controller_->Initialize(sample_rate_hz, num_channels); |
Alessio Bazzica | d66a605 | 2021-04-29 16:13:25 +0200 | [diff] [blame] | 112 | } |
Alessio Bazzica | 8aaa604 | 2021-03-31 15:16:05 +0200 | [diff] [blame] | 113 | data_dumper_.InitiateNewSetOfRecordings(); |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 114 | calls_since_last_limiter_log_ = 0; |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 115 | analog_level_ = kUnspecifiedAnalogLevel; |
| 116 | } |
| 117 | |
| 118 | void GainController2::SetFixedGainDb(float gain_db) { |
| 119 | const float gain_factor = DbToRatio(gain_db); |
| 120 | if (fixed_gain_applier_.GetGainFactor() != gain_factor) { |
| 121 | // Reset the limiter to quickly react on abrupt level changes caused by |
| 122 | // large changes of the fixed gain. |
| 123 | limiter_.Reset(); |
| 124 | } |
| 125 | fixed_gain_applier_.SetGainFactor(gain_factor); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 128 | void GainController2::Process(absl::optional<float> speech_probability, |
| 129 | AudioBuffer* audio) { |
Alessio Bazzica | 8aaa604 | 2021-03-31 15:16:05 +0200 | [diff] [blame] | 130 | data_dumper_.DumpRaw("agc2_notified_analog_level", analog_level_); |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 131 | AudioFrameView<float> float_frame(audio->channels(), audio->num_channels(), |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 132 | audio->num_frames()); |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 133 | if (vad_) { |
| 134 | speech_probability = vad_->Analyze(float_frame); |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 135 | } else if (speech_probability.has_value()) { |
| 136 | RTC_DCHECK_GE(speech_probability.value(), 0.0f); |
| 137 | RTC_DCHECK_LE(speech_probability.value(), 1.0f); |
| 138 | } |
| 139 | if (speech_probability.has_value()) { |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 140 | data_dumper_.DumpRaw("agc2_speech_probability", speech_probability.value()); |
| 141 | } |
Alessio Bazzica | 60f675f | 2021-10-15 15:36:11 +0200 | [diff] [blame] | 142 | fixed_gain_applier_.ApplyGain(float_frame); |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 143 | if (adaptive_digital_controller_) { |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 144 | RTC_DCHECK(speech_probability.has_value()); |
| 145 | adaptive_digital_controller_->Process( |
| 146 | float_frame, speech_probability.value(), limiter_.LastAudioLevel()); |
Alex Loiko | e583174 | 2018-08-24 11:28:36 +0200 | [diff] [blame] | 147 | } |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 148 | limiter_.Process(float_frame); |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 149 | |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 150 | // Periodically log limiter stats. |
| 151 | if (++calls_since_last_limiter_log_ == kLogLimiterStatsPeriodNumFrames) { |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 152 | calls_since_last_limiter_log_ = 0; |
| 153 | InterpolatedGainCurve::Stats stats = limiter_.GetGainCurveStats(); |
| 154 | RTC_LOG(LS_INFO) << "AGC2 limiter stats" |
| 155 | << " | identity: " << stats.look_ups_identity_region |
| 156 | << " | knee: " << stats.look_ups_knee_region |
| 157 | << " | limiter: " << stats.look_ups_limiter_region |
| 158 | << " | saturation: " << stats.look_ups_saturation_region; |
| 159 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Alex Loiko | a837dd7 | 2018-08-06 16:32:12 +0200 | [diff] [blame] | 162 | void GainController2::NotifyAnalogLevel(int level) { |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 163 | if (analog_level_ != level && adaptive_digital_controller_) { |
| 164 | adaptive_digital_controller_->HandleInputGainChange(); |
Alex Loiko | a837dd7 | 2018-08-06 16:32:12 +0200 | [diff] [blame] | 165 | } |
| 166 | analog_level_ = level; |
| 167 | } |
| 168 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 169 | bool GainController2::Validate( |
| 170 | const AudioProcessing::Config::GainController2& config) { |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 171 | const auto& fixed = config.fixed_digital; |
| 172 | const auto& adaptive = config.adaptive_digital; |
Alessio Bazzica | a850e6c | 2021-10-04 13:35:55 +0200 | [diff] [blame] | 173 | return fixed.gain_db >= 0.0f && fixed.gain_db < 50.f && |
| 174 | adaptive.headroom_db >= 0.0f && adaptive.max_gain_db > 0.0f && |
| 175 | adaptive.initial_gain_db >= 0.0f && |
Alessio Bazzica | 1ac4f2a | 2021-09-24 14:59:30 +0200 | [diff] [blame] | 176 | adaptive.max_gain_change_db_per_second > 0.0f && |
| 177 | adaptive.max_output_noise_level_dbfs <= 0.0f; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | } // namespace webrtc |