blob: 8ca87c7ca175611611ecd107eb80a632441afc1d [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"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
Alessio Bazzica08d2a702020-11-20 16:26:24 +010023#include "rtc_base/logging.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020024#include "rtc_base/strings/string_builder.h"
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020025#include "system_wrappers/include/field_trial.h"
alessiob3ec96df2017-05-22 06:57:06 -070026
27namespace webrtc {
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +020028namespace {
Alessio Bazzica38901042021-10-14 12:14:21 +020029
30using Agc2Config = AudioProcessing::Config::GainController2;
31
32constexpr int kUnspecifiedAnalogLevel = -1;
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +020033constexpr int kLogLimiterStatsPeriodMs = 30'000;
34constexpr int kFrameLengthMs = 10;
35constexpr int kLogLimiterStatsPeriodNumFrames =
36 kLogLimiterStatsPeriodMs / kFrameLengthMs;
Alessio Bazzica38901042021-10-14 12:14:21 +020037
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020038// Detects the available CPU features and applies any kill-switches.
39AvailableCpuFeatures GetAllowedCpuFeatures() {
40 AvailableCpuFeatures features = GetAvailableCpuFeatures();
41 if (field_trial::IsEnabled("WebRTC-Agc2SimdSse2KillSwitch")) {
42 features.sse2 = false;
43 }
44 if (field_trial::IsEnabled("WebRTC-Agc2SimdAvx2KillSwitch")) {
45 features.avx2 = false;
46 }
47 if (field_trial::IsEnabled("WebRTC-Agc2SimdNeonKillSwitch")) {
48 features.neon = false;
49 }
50 return features;
51}
52
Alessio Bazzica38901042021-10-14 12:14:21 +020053// Creates an adaptive digital gain controller if enabled.
54std::unique_ptr<AdaptiveAgc> CreateAdaptiveDigitalController(
55 const Agc2Config::AdaptiveDigital& config,
56 int sample_rate_hz,
57 int num_channels,
58 ApmDataDumper* data_dumper) {
59 if (config.enabled) {
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020060 // TODO(bugs.webrtc.org/7494): Also init with sample rate and num
61 // channels.
Alessio Bazzica38901042021-10-14 12:14:21 +020062 auto controller = std::make_unique<AdaptiveAgc>(data_dumper, config);
63 // TODO(bugs.webrtc.org/7494): Remove once passed to the ctor.
64 controller->Initialize(sample_rate_hz, num_channels);
65 return controller;
66 }
67 return nullptr;
68}
69
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +020070} // namespace
alessiob3ec96df2017-05-22 06:57:06 -070071
alessiob3ec96df2017-05-22 06:57:06 -070072int GainController2::instance_count_ = 0;
73
Alessio Bazzica38901042021-10-14 12:14:21 +020074GainController2::GainController2(const Agc2Config& config,
75 int sample_rate_hz,
76 int num_channels)
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020077 : cpu_features_(GetAllowedCpuFeatures()),
78 data_dumper_(rtc::AtomicOps::Increment(&instance_count_)),
Alessio Bazzica60f675f2021-10-15 15:36:11 +020079 fixed_gain_applier_(
80 /*hard_clip_samples=*/false,
81 /*initial_gain_factor=*/DbToRatio(config.fixed_digital.gain_db)),
Alessio Bazzica38901042021-10-14 12:14:21 +020082 adaptive_digital_controller_(
83 CreateAdaptiveDigitalController(config.adaptive_digital,
84 sample_rate_hz,
85 num_channels,
86 &data_dumper_)),
87 limiter_(sample_rate_hz, &data_dumper_, /*histogram_name_prefix=*/"Agc2"),
88 calls_since_last_limiter_log_(0),
89 analog_level_(kUnspecifiedAnalogLevel) {
90 RTC_DCHECK(Validate(config));
91 data_dumper_.InitiateNewSetOfRecordings();
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020092 const bool use_vad = config.adaptive_digital.enabled;
93 if (use_vad) {
94 // TODO(bugs.webrtc.org/7494): Move `vad_reset_period_ms` from adaptive
95 // digital to gain controller 2 config.
96 vad_ = std::make_unique<VoiceActivityDetectorWrapper>(
97 config.adaptive_digital.vad_reset_period_ms, cpu_features_,
98 sample_rate_hz);
99 }
Per Åhgren2bd85ab2020-01-03 10:36:34 +0100100}
alessiob3ec96df2017-05-22 06:57:06 -0700101
102GainController2::~GainController2() = default;
103
Alessio Bazzicad66a6052021-04-29 16:13:25 +0200104void GainController2::Initialize(int sample_rate_hz, int num_channels) {
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200105 RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz ||
106 sample_rate_hz == AudioProcessing::kSampleRate16kHz ||
107 sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
108 sample_rate_hz == AudioProcessing::kSampleRate48kHz);
Alessio Bazzica38901042021-10-14 12:14:21 +0200109 // TODO(bugs.webrtc.org/7494): Initialize `fixed_gain_applier_`.
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +0100110 limiter_.SetSampleRate(sample_rate_hz);
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +0200111 if (vad_) {
112 vad_->Initialize(sample_rate_hz);
113 }
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +0200114 if (adaptive_digital_controller_) {
115 adaptive_digital_controller_->Initialize(sample_rate_hz, num_channels);
Alessio Bazzicad66a6052021-04-29 16:13:25 +0200116 }
Alessio Bazzica8aaa6042021-03-31 15:16:05 +0200117 data_dumper_.InitiateNewSetOfRecordings();
Alessio Bazzica08d2a702020-11-20 16:26:24 +0100118 calls_since_last_limiter_log_ = 0;
Alessio Bazzica38901042021-10-14 12:14:21 +0200119 analog_level_ = kUnspecifiedAnalogLevel;
120}
121
122void GainController2::SetFixedGainDb(float gain_db) {
123 const float gain_factor = DbToRatio(gain_db);
124 if (fixed_gain_applier_.GetGainFactor() != gain_factor) {
125 // Reset the limiter to quickly react on abrupt level changes caused by
126 // large changes of the fixed gain.
127 limiter_.Reset();
128 }
129 fixed_gain_applier_.SetGainFactor(gain_factor);
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200130}
131
alessiob3ec96df2017-05-22 06:57:06 -0700132void GainController2::Process(AudioBuffer* audio) {
Alessio Bazzica8aaa6042021-03-31 15:16:05 +0200133 data_dumper_.DumpRaw("agc2_notified_analog_level", analog_level_);
Per Åhgrend47941e2019-08-22 11:51:13 +0200134 AudioFrameView<float> float_frame(audio->channels(), audio->num_channels(),
Alex Loikoe36e8bb2018-02-16 11:54:07 +0100135 audio->num_frames());
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +0200136 absl::optional<float> speech_probability;
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +0200137 if (vad_) {
138 speech_probability = vad_->Analyze(float_frame);
139 data_dumper_.DumpRaw("agc2_speech_probability", speech_probability.value());
140 }
Alessio Bazzica60f675f2021-10-15 15:36:11 +0200141 fixed_gain_applier_.ApplyGain(float_frame);
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +0200142 if (adaptive_digital_controller_) {
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +0200143 RTC_DCHECK(speech_probability.has_value());
144 adaptive_digital_controller_->Process(
145 float_frame, speech_probability.value(), limiter_.LastAudioLevel());
Alex Loikoe5831742018-08-24 11:28:36 +0200146 }
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +0100147 limiter_.Process(float_frame);
Alessio Bazzica08d2a702020-11-20 16:26:24 +0100148
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +0200149 // Periodically log limiter stats.
150 if (++calls_since_last_limiter_log_ == kLogLimiterStatsPeriodNumFrames) {
Alessio Bazzica08d2a702020-11-20 16:26:24 +0100151 calls_since_last_limiter_log_ = 0;
152 InterpolatedGainCurve::Stats stats = limiter_.GetGainCurveStats();
153 RTC_LOG(LS_INFO) << "AGC2 limiter stats"
154 << " | identity: " << stats.look_ups_identity_region
155 << " | knee: " << stats.look_ups_knee_region
156 << " | limiter: " << stats.look_ups_limiter_region
157 << " | saturation: " << stats.look_ups_saturation_region;
158 }
alessiob3ec96df2017-05-22 06:57:06 -0700159}
160
Alex Loikoa837dd72018-08-06 16:32:12 +0200161void GainController2::NotifyAnalogLevel(int level) {
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +0200162 if (analog_level_ != level && adaptive_digital_controller_) {
163 adaptive_digital_controller_->HandleInputGainChange();
Alex Loikoa837dd72018-08-06 16:32:12 +0200164 }
165 analog_level_ = level;
166}
167
alessiob3ec96df2017-05-22 06:57:06 -0700168bool GainController2::Validate(
169 const AudioProcessing::Config::GainController2& config) {
Alessio Bazzica0c83e152020-10-14 12:49:54 +0200170 const auto& fixed = config.fixed_digital;
171 const auto& adaptive = config.adaptive_digital;
Alessio Bazzicaa850e6c2021-10-04 13:35:55 +0200172 return fixed.gain_db >= 0.0f && fixed.gain_db < 50.f &&
173 adaptive.headroom_db >= 0.0f && adaptive.max_gain_db > 0.0f &&
174 adaptive.initial_gain_db >= 0.0f &&
Alessio Bazzica1ac4f2a2021-09-24 14:59:30 +0200175 adaptive.max_gain_change_db_per_second > 0.0f &&
176 adaptive.max_output_noise_level_dbfs <= 0.0f;
alessiob3ec96df2017-05-22 06:57:06 -0700177}
178
179} // namespace webrtc