blob: f1907bbd922eb115a1671f64ba54fb923d554b3c [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
31constexpr int kUnspecifiedAnalogLevel = -1;
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +020032constexpr int kLogLimiterStatsPeriodMs = 30'000;
33constexpr int kFrameLengthMs = 10;
34constexpr int kLogLimiterStatsPeriodNumFrames =
35 kLogLimiterStatsPeriodMs / kFrameLengthMs;
Alessio Bazzica38901042021-10-14 12:14:21 +020036
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020037// Detects the available CPU features and applies any kill-switches.
38AvailableCpuFeatures 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 Bazzica38901042021-10-14 12:14:21 +020052// Creates an adaptive digital gain controller if enabled.
Alessio Bazzica2fa46182021-10-26 14:08:23 +020053std::unique_ptr<AdaptiveDigitalGainController> CreateAdaptiveDigitalController(
Alessio Bazzica38901042021-10-14 12:14:21 +020054 const Agc2Config::AdaptiveDigital& config,
55 int sample_rate_hz,
56 int num_channels,
57 ApmDataDumper* data_dumper) {
58 if (config.enabled) {
Alessio Bazzica2fa46182021-10-26 14:08:23 +020059 return std::make_unique<AdaptiveDigitalGainController>(
60 data_dumper, config, sample_rate_hz, num_channels);
Alessio Bazzica38901042021-10-14 12:14:21 +020061 }
62 return nullptr;
63}
64
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +020065} // namespace
alessiob3ec96df2017-05-22 06:57:06 -070066
Niels Möller7a669002022-06-27 09:47:02 +020067std::atomic<int> GainController2::instance_count_(0);
alessiob3ec96df2017-05-22 06:57:06 -070068
Alessio Bazzica38901042021-10-14 12:14:21 +020069GainController2::GainController2(const Agc2Config& config,
70 int sample_rate_hz,
Hanna Silen0c1ad292022-06-16 16:35:45 +020071 int num_channels,
72 bool use_internal_vad)
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020073 : cpu_features_(GetAllowedCpuFeatures()),
Niels Möller7a669002022-06-27 09:47:02 +020074 data_dumper_(instance_count_.fetch_add(1) + 1),
Alessio Bazzica60f675f2021-10-15 15:36:11 +020075 fixed_gain_applier_(
76 /*hard_clip_samples=*/false,
77 /*initial_gain_factor=*/DbToRatio(config.fixed_digital.gain_db)),
Alessio Bazzica38901042021-10-14 12:14:21 +020078 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 Bazzicab4d4ae22021-10-15 13:57:56 +020088 const bool use_vad = config.adaptive_digital.enabled;
Hanna Silen0c1ad292022-06-16 16:35:45 +020089 if (use_vad && use_internal_vad) {
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020090 // 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 Åhgren2bd85ab2020-01-03 10:36:34 +010096}
alessiob3ec96df2017-05-22 06:57:06 -070097
98GainController2::~GainController2() = default;
99
Alessio Bazzicad66a6052021-04-29 16:13:25 +0200100void GainController2::Initialize(int sample_rate_hz, int num_channels) {
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200101 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 Bazzica38901042021-10-14 12:14:21 +0200105 // TODO(bugs.webrtc.org/7494): Initialize `fixed_gain_applier_`.
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +0100106 limiter_.SetSampleRate(sample_rate_hz);
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +0200107 if (vad_) {
108 vad_->Initialize(sample_rate_hz);
109 }
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +0200110 if (adaptive_digital_controller_) {
111 adaptive_digital_controller_->Initialize(sample_rate_hz, num_channels);
Alessio Bazzicad66a6052021-04-29 16:13:25 +0200112 }
Alessio Bazzica8aaa6042021-03-31 15:16:05 +0200113 data_dumper_.InitiateNewSetOfRecordings();
Alessio Bazzica08d2a702020-11-20 16:26:24 +0100114 calls_since_last_limiter_log_ = 0;
Alessio Bazzica38901042021-10-14 12:14:21 +0200115 analog_level_ = kUnspecifiedAnalogLevel;
116}
117
118void 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 Bazzica270f7b52017-10-13 11:05:17 +0200126}
127
Hanna Silen0c1ad292022-06-16 16:35:45 +0200128void GainController2::Process(absl::optional<float> speech_probability,
129 AudioBuffer* audio) {
Alessio Bazzica8aaa6042021-03-31 15:16:05 +0200130 data_dumper_.DumpRaw("agc2_notified_analog_level", analog_level_);
Per Åhgrend47941e2019-08-22 11:51:13 +0200131 AudioFrameView<float> float_frame(audio->channels(), audio->num_channels(),
Alex Loikoe36e8bb2018-02-16 11:54:07 +0100132 audio->num_frames());
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +0200133 if (vad_) {
134 speech_probability = vad_->Analyze(float_frame);
Hanna Silen0c1ad292022-06-16 16:35:45 +0200135 } 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 Bazzicab4d4ae22021-10-15 13:57:56 +0200140 data_dumper_.DumpRaw("agc2_speech_probability", speech_probability.value());
141 }
Alessio Bazzica60f675f2021-10-15 15:36:11 +0200142 fixed_gain_applier_.ApplyGain(float_frame);
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +0200143 if (adaptive_digital_controller_) {
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +0200144 RTC_DCHECK(speech_probability.has_value());
145 adaptive_digital_controller_->Process(
146 float_frame, speech_probability.value(), limiter_.LastAudioLevel());
Alex Loikoe5831742018-08-24 11:28:36 +0200147 }
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +0100148 limiter_.Process(float_frame);
Alessio Bazzica08d2a702020-11-20 16:26:24 +0100149
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +0200150 // Periodically log limiter stats.
151 if (++calls_since_last_limiter_log_ == kLogLimiterStatsPeriodNumFrames) {
Alessio Bazzica08d2a702020-11-20 16:26:24 +0100152 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 }
alessiob3ec96df2017-05-22 06:57:06 -0700160}
161
Alex Loikoa837dd72018-08-06 16:32:12 +0200162void GainController2::NotifyAnalogLevel(int level) {
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +0200163 if (analog_level_ != level && adaptive_digital_controller_) {
164 adaptive_digital_controller_->HandleInputGainChange();
Alex Loikoa837dd72018-08-06 16:32:12 +0200165 }
166 analog_level_ = level;
167}
168
alessiob3ec96df2017-05-22 06:57:06 -0700169bool GainController2::Validate(
170 const AudioProcessing::Config::GainController2& config) {
Alessio Bazzica0c83e152020-10-14 12:49:54 +0200171 const auto& fixed = config.fixed_digital;
172 const auto& adaptive = config.adaptive_digital;
Alessio Bazzicaa850e6c2021-10-04 13:35:55 +0200173 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 Bazzica1ac4f2a2021-09-24 14:59:30 +0200176 adaptive.max_gain_change_db_per_second > 0.0f &&
177 adaptive.max_output_noise_level_dbfs <= 0.0f;
alessiob3ec96df2017-05-22 06:57:06 -0700178}
179
180} // namespace webrtc