blob: 70b598df9c0699879d451ea51b6e6181778e0774 [file] [log] [blame]
alessiob3ec96df2017-05-22 06:57:06 -07001/*
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 Loikoe36e8bb2018-02-16 11:54:07 +010011#include "modules/audio_processing/gain_controller2.h"
alessiob3ec96df2017-05-22 06:57:06 -070012
Alessio Bazzica38901042021-10-14 12:14:21 +020013#include <memory>
14#include <utility>
15
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010016#include "common_audio/include/audio_util.h"
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020017#include "modules/audio_processing/agc2/cpu_features.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_processing/audio_buffer.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010019#include "modules/audio_processing/include/audio_frame_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_processing/logging/apm_data_dumper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
Alessio Bazzica08d2a702020-11-20 16:26:24 +010022#include "rtc_base/logging.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020023#include "rtc_base/strings/string_builder.h"
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020024#include "system_wrappers/include/field_trial.h"
alessiob3ec96df2017-05-22 06:57:06 -070025
26namespace webrtc {
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +020027namespace {
Alessio Bazzica38901042021-10-14 12:14:21 +020028
29using Agc2Config = AudioProcessing::Config::GainController2;
30
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +020031constexpr int kLogLimiterStatsPeriodMs = 30'000;
32constexpr int kFrameLengthMs = 10;
33constexpr int kLogLimiterStatsPeriodNumFrames =
34 kLogLimiterStatsPeriodMs / kFrameLengthMs;
Alessio Bazzica38901042021-10-14 12:14:21 +020035
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020036// Detects the available CPU features and applies any kill-switches.
37AvailableCpuFeatures GetAllowedCpuFeatures() {
38 AvailableCpuFeatures features = GetAvailableCpuFeatures();
39 if (field_trial::IsEnabled("WebRTC-Agc2SimdSse2KillSwitch")) {
40 features.sse2 = false;
41 }
42 if (field_trial::IsEnabled("WebRTC-Agc2SimdAvx2KillSwitch")) {
43 features.avx2 = false;
44 }
45 if (field_trial::IsEnabled("WebRTC-Agc2SimdNeonKillSwitch")) {
46 features.neon = false;
47 }
48 return features;
49}
50
Alessio Bazzica38901042021-10-14 12:14:21 +020051// Creates an adaptive digital gain controller if enabled.
Alessio Bazzica2fa46182021-10-26 14:08:23 +020052std::unique_ptr<AdaptiveDigitalGainController> CreateAdaptiveDigitalController(
Alessio Bazzica38901042021-10-14 12:14:21 +020053 const Agc2Config::AdaptiveDigital& config,
54 int sample_rate_hz,
55 int num_channels,
56 ApmDataDumper* data_dumper) {
57 if (config.enabled) {
Alessio Bazzica2fa46182021-10-26 14:08:23 +020058 return std::make_unique<AdaptiveDigitalGainController>(
59 data_dumper, config, sample_rate_hz, num_channels);
Alessio Bazzica38901042021-10-14 12:14:21 +020060 }
61 return nullptr;
62}
63
Hanna Silend7cfbe32022-11-02 19:12:20 +010064// Creates an input volume controller if `enabled` is true.
65std::unique_ptr<InputVolumeController> CreateInputVolumeController(
66 bool enabled,
67 int num_channels) {
68 if (enabled) {
69 return std::make_unique<InputVolumeController>(
Hanna Silen27fed452022-11-22 15:00:58 +010070 num_channels, InputVolumeController::Config());
Hanna Silend7cfbe32022-11-02 19:12:20 +010071 }
72 return nullptr;
73}
74
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +020075} // namespace
alessiob3ec96df2017-05-22 06:57:06 -070076
Niels Möller7a669002022-06-27 09:47:02 +020077std::atomic<int> GainController2::instance_count_(0);
alessiob3ec96df2017-05-22 06:57:06 -070078
Alessio Bazzica38901042021-10-14 12:14:21 +020079GainController2::GainController2(const Agc2Config& config,
80 int sample_rate_hz,
Hanna Silen0c1ad292022-06-16 16:35:45 +020081 int num_channels,
82 bool use_internal_vad)
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020083 : cpu_features_(GetAllowedCpuFeatures()),
Niels Möller7a669002022-06-27 09:47:02 +020084 data_dumper_(instance_count_.fetch_add(1) + 1),
Alessio Bazzica60f675f2021-10-15 15:36:11 +020085 fixed_gain_applier_(
86 /*hard_clip_samples=*/false,
87 /*initial_gain_factor=*/DbToRatio(config.fixed_digital.gain_db)),
Alessio Bazzica38901042021-10-14 12:14:21 +020088 adaptive_digital_controller_(
89 CreateAdaptiveDigitalController(config.adaptive_digital,
90 sample_rate_hz,
91 num_channels,
92 &data_dumper_)),
Hanna Silend7cfbe32022-11-02 19:12:20 +010093 input_volume_controller_(
94 CreateInputVolumeController(config.input_volume_controller.enabled,
95 num_channels)),
Alessio Bazzica38901042021-10-14 12:14:21 +020096 limiter_(sample_rate_hz, &data_dumper_, /*histogram_name_prefix=*/"Agc2"),
Alessio Bazzicafcf1af32022-09-07 17:14:26 +020097 calls_since_last_limiter_log_(0) {
Alessio Bazzica38901042021-10-14 12:14:21 +020098 RTC_DCHECK(Validate(config));
99 data_dumper_.InitiateNewSetOfRecordings();
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +0200100 const bool use_vad = config.adaptive_digital.enabled;
Hanna Silen0c1ad292022-06-16 16:35:45 +0200101 if (use_vad && use_internal_vad) {
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +0200102 // TODO(bugs.webrtc.org/7494): Move `vad_reset_period_ms` from adaptive
103 // digital to gain controller 2 config.
104 vad_ = std::make_unique<VoiceActivityDetectorWrapper>(
105 config.adaptive_digital.vad_reset_period_ms, cpu_features_,
106 sample_rate_hz);
107 }
Hanna Silend7cfbe32022-11-02 19:12:20 +0100108 if (input_volume_controller_) {
109 input_volume_controller_->Initialize();
110 }
Per Åhgren2bd85ab2020-01-03 10:36:34 +0100111}
alessiob3ec96df2017-05-22 06:57:06 -0700112
113GainController2::~GainController2() = default;
114
Hanna Silend7cfbe32022-11-02 19:12:20 +0100115// TODO(webrtc:7494): Pass the flag also to the other components.
116void GainController2::SetCaptureOutputUsed(bool capture_output_used) {
117 if (input_volume_controller_) {
118 input_volume_controller_->HandleCaptureOutputUsedChange(
119 capture_output_used);
120 }
121}
122
Alessio Bazzica38901042021-10-14 12:14:21 +0200123void GainController2::SetFixedGainDb(float gain_db) {
124 const float gain_factor = DbToRatio(gain_db);
125 if (fixed_gain_applier_.GetGainFactor() != gain_factor) {
126 // Reset the limiter to quickly react on abrupt level changes caused by
127 // large changes of the fixed gain.
128 limiter_.Reset();
129 }
130 fixed_gain_applier_.SetGainFactor(gain_factor);
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200131}
132
Hanna Silend7cfbe32022-11-02 19:12:20 +0100133void GainController2::Analyze(int applied_input_volume,
134 const AudioBuffer& audio_buffer) {
135 RTC_DCHECK_GE(applied_input_volume, 0);
136 RTC_DCHECK_LE(applied_input_volume, 255);
137
138 if (input_volume_controller_) {
139 input_volume_controller_->set_stream_analog_level(applied_input_volume);
140 input_volume_controller_->AnalyzePreProcess(audio_buffer);
141 }
142}
143
144absl::optional<int> GainController2::GetRecommendedInputVolume() const {
145 return input_volume_controller_
146 ? absl::optional<int>(
147 input_volume_controller_->recommended_analog_level())
148 : absl::nullopt;
149}
150
Hanna Silen0c1ad292022-06-16 16:35:45 +0200151void GainController2::Process(absl::optional<float> speech_probability,
Alessio Bazzicafcf1af32022-09-07 17:14:26 +0200152 bool input_volume_changed,
Hanna Silen0c1ad292022-06-16 16:35:45 +0200153 AudioBuffer* audio) {
Alessio Bazzicafcf1af32022-09-07 17:14:26 +0200154 data_dumper_.DumpRaw("agc2_applied_input_volume_changed",
155 input_volume_changed);
156 if (input_volume_changed && !!adaptive_digital_controller_) {
157 adaptive_digital_controller_->HandleInputGainChange();
158 }
159
Per Åhgrend47941e2019-08-22 11:51:13 +0200160 AudioFrameView<float> float_frame(audio->channels(), audio->num_channels(),
Alex Loikoe36e8bb2018-02-16 11:54:07 +0100161 audio->num_frames());
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +0200162 if (vad_) {
163 speech_probability = vad_->Analyze(float_frame);
Hanna Silen0c1ad292022-06-16 16:35:45 +0200164 } else if (speech_probability.has_value()) {
165 RTC_DCHECK_GE(speech_probability.value(), 0.0f);
166 RTC_DCHECK_LE(speech_probability.value(), 1.0f);
167 }
168 if (speech_probability.has_value()) {
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +0200169 data_dumper_.DumpRaw("agc2_speech_probability", speech_probability.value());
170 }
Hanna Silend7cfbe32022-11-02 19:12:20 +0100171
172 if (input_volume_controller_) {
Hanna Silen27fed452022-11-22 15:00:58 +0100173 // TODO(bugs.webrtc.org/7494): A temprorary check, remove once not needed.
174 RTC_DCHECK(adaptive_digital_controller_);
Hanna Silend7cfbe32022-11-02 19:12:20 +0100175 absl::optional<float> speech_level;
176 if (adaptive_digital_controller_) {
177 speech_level =
178 adaptive_digital_controller_->GetSpeechLevelDbfsIfConfident();
179 }
Hanna Silen27fed452022-11-22 15:00:58 +0100180 RTC_DCHECK(speech_probability.has_value());
181 if (speech_probability.has_value()) {
182 input_volume_controller_->Process(*speech_probability, speech_level);
183 }
Hanna Silend7cfbe32022-11-02 19:12:20 +0100184 }
185
Alessio Bazzica60f675f2021-10-15 15:36:11 +0200186 fixed_gain_applier_.ApplyGain(float_frame);
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +0200187 if (adaptive_digital_controller_) {
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +0200188 RTC_DCHECK(speech_probability.has_value());
189 adaptive_digital_controller_->Process(
190 float_frame, speech_probability.value(), limiter_.LastAudioLevel());
Alex Loikoe5831742018-08-24 11:28:36 +0200191 }
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +0100192 limiter_.Process(float_frame);
Alessio Bazzica08d2a702020-11-20 16:26:24 +0100193
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +0200194 // Periodically log limiter stats.
195 if (++calls_since_last_limiter_log_ == kLogLimiterStatsPeriodNumFrames) {
Alessio Bazzica08d2a702020-11-20 16:26:24 +0100196 calls_since_last_limiter_log_ = 0;
197 InterpolatedGainCurve::Stats stats = limiter_.GetGainCurveStats();
198 RTC_LOG(LS_INFO) << "AGC2 limiter stats"
199 << " | identity: " << stats.look_ups_identity_region
200 << " | knee: " << stats.look_ups_knee_region
201 << " | limiter: " << stats.look_ups_limiter_region
202 << " | saturation: " << stats.look_ups_saturation_region;
203 }
alessiob3ec96df2017-05-22 06:57:06 -0700204}
205
206bool GainController2::Validate(
207 const AudioProcessing::Config::GainController2& config) {
Alessio Bazzica0c83e152020-10-14 12:49:54 +0200208 const auto& fixed = config.fixed_digital;
209 const auto& adaptive = config.adaptive_digital;
Alessio Bazzicaa850e6c2021-10-04 13:35:55 +0200210 return fixed.gain_db >= 0.0f && fixed.gain_db < 50.f &&
211 adaptive.headroom_db >= 0.0f && adaptive.max_gain_db > 0.0f &&
212 adaptive.initial_gain_db >= 0.0f &&
Alessio Bazzica1ac4f2a2021-09-24 14:59:30 +0200213 adaptive.max_gain_change_db_per_second > 0.0f &&
214 adaptive.max_output_noise_level_dbfs <= 0.0f;
alessiob3ec96df2017-05-22 06:57:06 -0700215}
216
217} // namespace webrtc