blob: f0cbbc85dad4b2956acbf21786d4674879bc2383 [file] [log] [blame]
peahe0eae3c2016-12-14 01:16:23 -08001/*
2 * Copyright (c) 2016 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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "modules/audio_processing/aec3/echo_canceller3.h"
peahe0eae3c2016-12-14 01:16:23 -080011
peahd0263542017-01-03 04:20:34 -080012#include <sstream>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/audio_processing/logging/apm_data_dumper.h"
15#include "rtc_base/atomicops.h"
peahe0eae3c2016-12-14 01:16:23 -080016
17namespace webrtc {
18
peahd0263542017-01-03 04:20:34 -080019namespace {
20
peahcf02cf12017-04-05 14:18:07 -070021enum class EchoCanceller3ApiCall { kCapture, kRender };
22
peahd0263542017-01-03 04:20:34 -080023bool DetectSaturation(rtc::ArrayView<const float> y) {
24 for (auto y_k : y) {
peah522d71b2017-02-23 05:16:26 -080025 if (y_k >= 32700.0f || y_k <= -32700.0f) {
peahd0263542017-01-03 04:20:34 -080026 return true;
27 }
28 }
29 return false;
30}
31
32void FillSubFrameView(AudioBuffer* frame,
33 size_t sub_frame_index,
34 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
35 RTC_DCHECK_GE(1, sub_frame_index);
36 RTC_DCHECK_LE(0, sub_frame_index);
37 RTC_DCHECK_EQ(frame->num_bands(), sub_frame_view->size());
38 for (size_t k = 0; k < sub_frame_view->size(); ++k) {
39 (*sub_frame_view)[k] = rtc::ArrayView<float>(
40 &frame->split_bands_f(0)[k][sub_frame_index * kSubFrameLength],
41 kSubFrameLength);
42 }
43}
44
45void FillSubFrameView(std::vector<std::vector<float>>* frame,
46 size_t sub_frame_index,
47 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
48 RTC_DCHECK_GE(1, sub_frame_index);
49 RTC_DCHECK_EQ(frame->size(), sub_frame_view->size());
50 for (size_t k = 0; k < frame->size(); ++k) {
51 (*sub_frame_view)[k] = rtc::ArrayView<float>(
52 &(*frame)[k][sub_frame_index * kSubFrameLength], kSubFrameLength);
53 }
54}
55
56void ProcessCaptureFrameContent(
57 AudioBuffer* capture,
peah69221db2017-01-27 03:28:19 -080058 bool level_change,
peahd0263542017-01-03 04:20:34 -080059 bool saturated_microphone_signal,
60 size_t sub_frame_index,
61 FrameBlocker* capture_blocker,
62 BlockFramer* output_framer,
63 BlockProcessor* block_processor,
64 std::vector<std::vector<float>>* block,
65 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
66 FillSubFrameView(capture, sub_frame_index, sub_frame_view);
67 capture_blocker->InsertSubFrameAndExtractBlock(*sub_frame_view, block);
peah69221db2017-01-27 03:28:19 -080068 block_processor->ProcessCapture(level_change, saturated_microphone_signal,
69 block);
peahd0263542017-01-03 04:20:34 -080070 output_framer->InsertBlockAndExtractSubFrame(*block, sub_frame_view);
71}
72
73void ProcessRemainingCaptureFrameContent(
peah69221db2017-01-27 03:28:19 -080074 bool level_change,
peahd0263542017-01-03 04:20:34 -080075 bool saturated_microphone_signal,
76 FrameBlocker* capture_blocker,
77 BlockFramer* output_framer,
78 BlockProcessor* block_processor,
79 std::vector<std::vector<float>>* block) {
80 if (!capture_blocker->IsBlockAvailable()) {
81 return;
82 }
83
84 capture_blocker->ExtractBlock(block);
peah69221db2017-01-27 03:28:19 -080085 block_processor->ProcessCapture(level_change, saturated_microphone_signal,
86 block);
peahd0263542017-01-03 04:20:34 -080087 output_framer->InsertBlock(*block);
88}
89
peahcf02cf12017-04-05 14:18:07 -070090void BufferRenderFrameContent(
peahd0263542017-01-03 04:20:34 -080091 std::vector<std::vector<float>>* render_frame,
92 size_t sub_frame_index,
93 FrameBlocker* render_blocker,
94 BlockProcessor* block_processor,
95 std::vector<std::vector<float>>* block,
96 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
97 FillSubFrameView(render_frame, sub_frame_index, sub_frame_view);
98 render_blocker->InsertSubFrameAndExtractBlock(*sub_frame_view, block);
peahcf02cf12017-04-05 14:18:07 -070099 block_processor->BufferRender(*block);
peahd0263542017-01-03 04:20:34 -0800100}
101
peahcf02cf12017-04-05 14:18:07 -0700102void BufferRemainingRenderFrameContent(FrameBlocker* render_blocker,
peahd0263542017-01-03 04:20:34 -0800103 BlockProcessor* block_processor,
104 std::vector<std::vector<float>>* block) {
105 if (!render_blocker->IsBlockAvailable()) {
peahcf02cf12017-04-05 14:18:07 -0700106 return;
peahd0263542017-01-03 04:20:34 -0800107 }
108 render_blocker->ExtractBlock(block);
peahcf02cf12017-04-05 14:18:07 -0700109 block_processor->BufferRender(*block);
peahd0263542017-01-03 04:20:34 -0800110}
111
peahcf02cf12017-04-05 14:18:07 -0700112void CopyBufferIntoFrame(AudioBuffer* buffer,
113 size_t num_bands,
114 size_t frame_length,
115 std::vector<std::vector<float>>* frame) {
peahd0263542017-01-03 04:20:34 -0800116 RTC_DCHECK_EQ(num_bands, frame->size());
peah522d71b2017-02-23 05:16:26 -0800117 RTC_DCHECK_EQ(frame_length, (*frame)[0].size());
peahcf02cf12017-04-05 14:18:07 -0700118 for (size_t k = 0; k < num_bands; ++k) {
119 rtc::ArrayView<float> buffer_view(&buffer->split_bands_f(0)[k][0],
120 frame_length);
121 std::copy(buffer_view.begin(), buffer_view.end(), (*frame)[k].begin());
122 }
peahd0263542017-01-03 04:20:34 -0800123}
124
125// [B,A] = butter(2,100/4000,'high')
126const CascadedBiQuadFilter::BiQuadCoefficients
127 kHighPassFilterCoefficients_8kHz = {{0.94598f, -1.89195f, 0.94598f},
128 {-1.88903f, 0.89487f}};
129const int kNumberOfHighPassBiQuads_8kHz = 1;
130
131// [B,A] = butter(2,100/8000,'high')
132const CascadedBiQuadFilter::BiQuadCoefficients
133 kHighPassFilterCoefficients_16kHz = {{0.97261f, -1.94523f, 0.97261f},
134 {-1.94448f, 0.94598f}};
135const int kNumberOfHighPassBiQuads_16kHz = 1;
136
peahd0263542017-01-03 04:20:34 -0800137} // namespace
138
139class EchoCanceller3::RenderWriter {
140 public:
141 RenderWriter(ApmDataDumper* data_dumper,
142 SwapQueue<std::vector<std::vector<float>>,
143 Aec3RenderQueueItemVerifier>* render_transfer_queue,
144 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter,
145 int sample_rate_hz,
146 int frame_length,
147 int num_bands);
148 ~RenderWriter();
Alex Loiko890988c2017-08-31 10:25:48 +0200149 void Insert(AudioBuffer* input);
peahd0263542017-01-03 04:20:34 -0800150
151 private:
152 ApmDataDumper* data_dumper_;
153 const int sample_rate_hz_;
154 const size_t frame_length_;
155 const int num_bands_;
156 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter_;
157 std::vector<std::vector<float>> render_queue_input_frame_;
158 SwapQueue<std::vector<std::vector<float>>, Aec3RenderQueueItemVerifier>*
159 render_transfer_queue_;
160 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderWriter);
161};
162
163EchoCanceller3::RenderWriter::RenderWriter(
164 ApmDataDumper* data_dumper,
165 SwapQueue<std::vector<std::vector<float>>, Aec3RenderQueueItemVerifier>*
166 render_transfer_queue,
167 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter,
168 int sample_rate_hz,
169 int frame_length,
170 int num_bands)
171 : data_dumper_(data_dumper),
172 sample_rate_hz_(sample_rate_hz),
173 frame_length_(frame_length),
174 num_bands_(num_bands),
175 render_highpass_filter_(std::move(render_highpass_filter)),
176 render_queue_input_frame_(num_bands_,
177 std::vector<float>(frame_length_, 0.f)),
178 render_transfer_queue_(render_transfer_queue) {
179 RTC_DCHECK(data_dumper);
180}
181
182EchoCanceller3::RenderWriter::~RenderWriter() = default;
183
peahcf02cf12017-04-05 14:18:07 -0700184void EchoCanceller3::RenderWriter::Insert(AudioBuffer* input) {
peahd0263542017-01-03 04:20:34 -0800185 RTC_DCHECK_EQ(1, input->num_channels());
peahd0263542017-01-03 04:20:34 -0800186 RTC_DCHECK_EQ(frame_length_, input->num_frames_per_band());
Gustaf Ullberg7d042782018-01-16 13:39:27 +0100187 RTC_DCHECK_EQ(num_bands_, input->num_bands());
188
189 // TODO(bugs.webrtc.org/8759) Temporary work-around.
190 if (num_bands_ != static_cast<int>(input->num_bands()))
191 return;
192
peahd0263542017-01-03 04:20:34 -0800193 data_dumper_->DumpWav("aec3_render_input", frame_length_,
peahcf02cf12017-04-05 14:18:07 -0700194 &input->split_bands_f(0)[0][0],
peahd0263542017-01-03 04:20:34 -0800195 LowestBandRate(sample_rate_hz_), 1);
196
peahcf02cf12017-04-05 14:18:07 -0700197 CopyBufferIntoFrame(input, num_bands_, frame_length_,
198 &render_queue_input_frame_);
peahd0263542017-01-03 04:20:34 -0800199
200 if (render_highpass_filter_) {
201 render_highpass_filter_->Process(render_queue_input_frame_[0]);
202 }
203
peah925e9d72017-04-10 04:18:38 -0700204 static_cast<void>(render_transfer_queue_->Insert(&render_queue_input_frame_));
peahd0263542017-01-03 04:20:34 -0800205}
206
peahe0eae3c2016-12-14 01:16:23 -0800207int EchoCanceller3::instance_count_ = 0;
208
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200209EchoCanceller3::EchoCanceller3(const EchoCanceller3Config& config,
210 int sample_rate_hz,
211 bool use_highpass_filter)
Per Ã…hgren8ba58612017-12-01 23:01:44 +0100212 : EchoCanceller3(config,
213 sample_rate_hz,
peahd0263542017-01-03 04:20:34 -0800214 use_highpass_filter,
215 std::unique_ptr<BlockProcessor>(
peah697a5902017-06-30 07:06:10 -0700216 BlockProcessor::Create(config, sample_rate_hz))) {}
Per Ã…hgren8ba58612017-12-01 23:01:44 +0100217EchoCanceller3::EchoCanceller3(const EchoCanceller3Config& config,
218 int sample_rate_hz,
peahd0263542017-01-03 04:20:34 -0800219 bool use_highpass_filter,
220 std::unique_ptr<BlockProcessor> block_processor)
221 : data_dumper_(
222 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
223 sample_rate_hz_(sample_rate_hz),
224 num_bands_(NumBandsForRate(sample_rate_hz_)),
225 frame_length_(rtc::CheckedDivExact(LowestBandRate(sample_rate_hz_), 100)),
226 output_framer_(num_bands_),
227 capture_blocker_(num_bands_),
228 render_blocker_(num_bands_),
229 render_transfer_queue_(
Per Ã…hgren8ba58612017-12-01 23:01:44 +0100230 kRenderTransferQueueSizeFrames,
peahd0263542017-01-03 04:20:34 -0800231 std::vector<std::vector<float>>(
232 num_bands_,
233 std::vector<float>(frame_length_, 0.f)),
234 Aec3RenderQueueItemVerifier(num_bands_, frame_length_)),
235 block_processor_(std::move(block_processor)),
236 render_queue_output_frame_(num_bands_,
237 std::vector<float>(frame_length_, 0.f)),
238 block_(num_bands_, std::vector<float>(kBlockSize, 0.f)),
239 sub_frame_view_(num_bands_) {
peah21920892017-02-08 05:08:56 -0800240 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
241
peahd0263542017-01-03 04:20:34 -0800242 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter;
243 if (use_highpass_filter) {
244 render_highpass_filter.reset(new CascadedBiQuadFilter(
245 sample_rate_hz_ == 8000 ? kHighPassFilterCoefficients_8kHz
246 : kHighPassFilterCoefficients_16kHz,
247 sample_rate_hz_ == 8000 ? kNumberOfHighPassBiQuads_8kHz
248 : kNumberOfHighPassBiQuads_16kHz));
249 capture_highpass_filter_.reset(new CascadedBiQuadFilter(
250 sample_rate_hz_ == 8000 ? kHighPassFilterCoefficients_8kHz
251 : kHighPassFilterCoefficients_16kHz,
252 sample_rate_hz_ == 8000 ? kNumberOfHighPassBiQuads_8kHz
253 : kNumberOfHighPassBiQuads_16kHz));
254 }
peahe0eae3c2016-12-14 01:16:23 -0800255
peahd0263542017-01-03 04:20:34 -0800256 render_writer_.reset(
257 new RenderWriter(data_dumper_.get(), &render_transfer_queue_,
258 std::move(render_highpass_filter), sample_rate_hz_,
259 frame_length_, num_bands_));
260
261 RTC_DCHECK_EQ(num_bands_, std::max(sample_rate_hz_, 16000) / 16000);
262 RTC_DCHECK_GE(kMaxNumBands, num_bands_);
peahe0eae3c2016-12-14 01:16:23 -0800263}
264
265EchoCanceller3::~EchoCanceller3() = default;
266
peahcf02cf12017-04-05 14:18:07 -0700267void EchoCanceller3::AnalyzeRender(AudioBuffer* render) {
peahd0263542017-01-03 04:20:34 -0800268 RTC_DCHECK_RUNS_SERIALIZED(&render_race_checker_);
269 RTC_DCHECK(render);
peahcf02cf12017-04-05 14:18:07 -0700270 data_dumper_->DumpRaw("aec3_call_order",
271 static_cast<int>(EchoCanceller3ApiCall::kRender));
272
peahd0263542017-01-03 04:20:34 -0800273 return render_writer_->Insert(render);
peahe0eae3c2016-12-14 01:16:23 -0800274}
275
peahd0263542017-01-03 04:20:34 -0800276void EchoCanceller3::AnalyzeCapture(AudioBuffer* capture) {
277 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
278 RTC_DCHECK(capture);
peahebe77782017-02-27 07:29:21 -0800279 data_dumper_->DumpWav("aec3_capture_analyze_input", capture->num_frames(),
peahd0263542017-01-03 04:20:34 -0800280 capture->channels_f()[0], sample_rate_hz_, 1);
281
282 saturated_microphone_signal_ = false;
283 for (size_t k = 0; k < capture->num_channels(); ++k) {
284 saturated_microphone_signal_ |=
285 DetectSaturation(rtc::ArrayView<const float>(capture->channels_f()[k],
286 capture->num_frames()));
287 if (saturated_microphone_signal_) {
288 break;
289 }
290 }
291}
peahe0eae3c2016-12-14 01:16:23 -0800292
peah69221db2017-01-27 03:28:19 -0800293void EchoCanceller3::ProcessCapture(AudioBuffer* capture, bool level_change) {
peahd0263542017-01-03 04:20:34 -0800294 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
295 RTC_DCHECK(capture);
peahe0eae3c2016-12-14 01:16:23 -0800296 RTC_DCHECK_EQ(1u, capture->num_channels());
peahd0263542017-01-03 04:20:34 -0800297 RTC_DCHECK_EQ(num_bands_, capture->num_bands());
peahe0eae3c2016-12-14 01:16:23 -0800298 RTC_DCHECK_EQ(frame_length_, capture->num_frames_per_band());
peahcf02cf12017-04-05 14:18:07 -0700299 data_dumper_->DumpRaw("aec3_call_order",
300 static_cast<int>(EchoCanceller3ApiCall::kCapture));
peahd0263542017-01-03 04:20:34 -0800301
302 rtc::ArrayView<float> capture_lower_band =
303 rtc::ArrayView<float>(&capture->split_bands_f(0)[0][0], frame_length_);
304
305 data_dumper_->DumpWav("aec3_capture_input", capture_lower_band,
306 LowestBandRate(sample_rate_hz_), 1);
307
peahcf02cf12017-04-05 14:18:07 -0700308 EmptyRenderQueue();
peahd0263542017-01-03 04:20:34 -0800309
310 if (capture_highpass_filter_) {
311 capture_highpass_filter_->Process(capture_lower_band);
312 }
313
peah69221db2017-01-27 03:28:19 -0800314 ProcessCaptureFrameContent(
315 capture, level_change, saturated_microphone_signal_, 0, &capture_blocker_,
316 &output_framer_, block_processor_.get(), &block_, &sub_frame_view_);
peahd0263542017-01-03 04:20:34 -0800317
318 if (sample_rate_hz_ != 8000) {
319 ProcessCaptureFrameContent(
peah69221db2017-01-27 03:28:19 -0800320 capture, level_change, saturated_microphone_signal_, 1,
peahd0263542017-01-03 04:20:34 -0800321 &capture_blocker_, &output_framer_, block_processor_.get(), &block_,
322 &sub_frame_view_);
323 }
324
325 ProcessRemainingCaptureFrameContent(
peah69221db2017-01-27 03:28:19 -0800326 level_change, saturated_microphone_signal_, &capture_blocker_,
peahd0263542017-01-03 04:20:34 -0800327 &output_framer_, block_processor_.get(), &block_);
328
329 data_dumper_->DumpWav("aec3_capture_output", frame_length_,
330 &capture->split_bands_f(0)[0][0],
331 LowestBandRate(sample_rate_hz_), 1);
peahe0eae3c2016-12-14 01:16:23 -0800332}
333
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100334EchoControl::Metrics EchoCanceller3::GetMetrics() const {
335 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
336 Metrics metrics;
337 block_processor_->GetMetrics(&metrics);
338 return metrics;
339}
340
peahcf02cf12017-04-05 14:18:07 -0700341void EchoCanceller3::EmptyRenderQueue() {
peahd0263542017-01-03 04:20:34 -0800342 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
peahd0263542017-01-03 04:20:34 -0800343 bool frame_to_buffer =
344 render_transfer_queue_.Remove(&render_queue_output_frame_);
345 while (frame_to_buffer) {
peahcf02cf12017-04-05 14:18:07 -0700346 BufferRenderFrameContent(&render_queue_output_frame_, 0, &render_blocker_,
347 block_processor_.get(), &block_, &sub_frame_view_);
peahd0263542017-01-03 04:20:34 -0800348
349 if (sample_rate_hz_ != 8000) {
peahcf02cf12017-04-05 14:18:07 -0700350 BufferRenderFrameContent(&render_queue_output_frame_, 1, &render_blocker_,
351 block_processor_.get(), &block_,
352 &sub_frame_view_);
peahd0263542017-01-03 04:20:34 -0800353 }
354
peahcf02cf12017-04-05 14:18:07 -0700355 BufferRemainingRenderFrameContent(&render_blocker_, block_processor_.get(),
356 &block_);
peahd0263542017-01-03 04:20:34 -0800357
358 frame_to_buffer =
359 render_transfer_queue_.Remove(&render_queue_output_frame_);
360 }
peahd0263542017-01-03 04:20:34 -0800361}
peahe0eae3c2016-12-14 01:16:23 -0800362} // namespace webrtc