Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(442)

Side by Side Diff: webrtc/modules/audio_processing/aec3/echo_canceller3.cc

Issue 2584493002: Added first layer of the echo canceller 3 functionality (Closed)
Patch Set: Minor changes Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 #include "webrtc/modules/audio_processing/aec3/echo_canceller3.h" 10 #include "webrtc/modules/audio_processing/aec3/echo_canceller3.h"
11 11
12 #include "webrtc/base/atomicops.h" 12 #include "webrtc/base/atomicops.h"
13 #include "webrtc/system_wrappers/include/logging.h" 13 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h"
14 14
15 namespace webrtc { 15 namespace webrtc {
16 16
17 namespace {
18
19 bool DetectSaturation(rtc::ArrayView<const float> y) {
20 for (auto y_k : y) {
21 if (y_k >= 32767.0f || y_k <= -32768.0f) {
22 return true;
23 }
24 }
25 return false;
26 }
27
28 void FillSubFrameView(AudioBuffer* frame,
29 size_t sub_frame_index,
30 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
31 RTC_DCHECK_GE(1, sub_frame_index);
32 RTC_DCHECK_LE(0, sub_frame_index);
33 RTC_DCHECK_EQ(frame->num_bands(), sub_frame_view->size());
34 for (size_t k = 0; k < sub_frame_view->size(); ++k) {
35 (*sub_frame_view)[k] = rtc::ArrayView<float>(
36 &frame->split_bands_f(0)[k][sub_frame_index * kSubFrameLength],
37 kSubFrameLength);
38 }
39 }
40
41 void FillSubFrameView(std::vector<std::vector<float>>* frame,
42 size_t sub_frame_index,
43 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
44 RTC_DCHECK_GE(1, sub_frame_index);
45 RTC_DCHECK_LE(0, sub_frame_index);
ivoc 2016/12/22 13:38:13 Doesn't this hold for any size_t? (since values ar
peah-webrtc 2016/12/22 16:40:04 Good find! Done.
46 RTC_DCHECK_EQ(frame->size(), sub_frame_view->size());
47 for (size_t k = 0; k < frame->size(); ++k) {
48 (*sub_frame_view)[k] = rtc::ArrayView<float>(
49 &(*frame)[k][sub_frame_index * kSubFrameLength], kSubFrameLength);
50 }
51 }
52
53 void ProcessCaptureFrameContent(
54 AudioBuffer* capture,
55 bool known_echo_path_change,
56 bool saturated_microphone_signal,
57 size_t sub_frame_index,
58 FrameBlocker* capture_blocker,
59 BlockFramer* output_framer,
60 BlockProcessor* block_processor,
61 std::vector<std::vector<float>>* block,
62 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
63 FillSubFrameView(capture, sub_frame_index, sub_frame_view);
64 capture_blocker->InsertSubFrameAndExtractBlock(*sub_frame_view, block);
65 block_processor->ProcessCapture(known_echo_path_change,
66 saturated_microphone_signal, block);
67 output_framer->InsertBlockAndExtractSubFrame(*block, sub_frame_view);
68 }
69
70 void ProcessRemainingCaptureFrameContent(
71 bool known_echo_path_change,
72 bool saturated_microphone_signal,
73 FrameBlocker* capture_blocker,
74 BlockFramer* output_framer,
75 BlockProcessor* block_processor,
76 std::vector<std::vector<float>>* block) {
77 if (!capture_blocker->IsBlockAvailable()) {
78 return;
79 }
80
81 capture_blocker->ExtractBlock(block);
82 block_processor->ProcessCapture(known_echo_path_change,
83 saturated_microphone_signal, block);
84 output_framer->InsertBlock(*block);
85 }
86
87 bool BufferRenderFrameContent(
88 std::vector<std::vector<float>>* render_frame,
89 size_t sub_frame_index,
90 FrameBlocker* render_blocker,
91 BlockProcessor* block_processor,
92 std::vector<std::vector<float>>* block,
93 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
94 FillSubFrameView(render_frame, sub_frame_index, sub_frame_view);
95 render_blocker->InsertSubFrameAndExtractBlock(*sub_frame_view, block);
96 return block_processor->BufferRender(block);
97 }
98
99 bool BufferRemainingRenderFrameContent(FrameBlocker* render_blocker,
100 BlockProcessor* block_processor,
101 std::vector<std::vector<float>>* block) {
102 if (!render_blocker->IsBlockAvailable()) {
103 return false;
104 }
105 render_blocker->ExtractBlock(block);
106 return block_processor->BufferRender(block);
107 }
108
109 void CopyAudioBufferIntoFrame(AudioBuffer* buffer,
110 size_t num_bands,
111 size_t frame_length,
112 std::vector<std::vector<float>>* frame) {
113 RTC_DCHECK_EQ(num_bands, frame->size());
114 for (size_t i = 0; i < num_bands; ++i) {
115 rtc::ArrayView<float> buffer_view(&buffer->split_bands_f(0)[i][0],
116 frame_length);
117 std::copy(buffer_view.begin(), buffer_view.end(), (*frame)[i].begin());
118 }
119 }
120
121 // [B,A] = butter(2,100/4000,'high')
122 const CascadedBiQuadFilter::BiQuadCoefficients
123 kHighPassFilterCoefficients_8kHz = {
124 {0.945976856002790, -1.891953712005580, 0.945976856002790},
125 {-1.889033079394525, 0.894874344616636}};
126 const int kNumberOfHighPassBiQuads_8kHz = 1;
127
128 // [B,A] = butter(2,100/8000,'high')
129 const CascadedBiQuadFilter::BiQuadCoefficients
130 kHighPassFilterCoefficients_16kHz = {
131 {0.972613898499844, -1.945227796999688, 0.972613898499844},
132 {-1.944477657767094, 0.945977936232282}};
133 const int kNumberOfHighPassBiQuads_16kHz = 1;
134
135 static constexpr size_t kRenderTransferQueueSize = 30;
136
137 } // namespace
138
139 class 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();
149 bool Insert(AudioBuffer* render);
150
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
163 EchoCanceller3::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
182 EchoCanceller3::RenderWriter::~RenderWriter() = default;
183
184 bool EchoCanceller3::RenderWriter::Insert(AudioBuffer* input) {
185 RTC_DCHECK_EQ(1, input->num_channels());
186 RTC_DCHECK_EQ(frame_length_, input->num_frames_per_band());
187 data_dumper_->DumpWav("aec3_render_input", frame_length_,
188 &input->split_bands_f(0)[0][0],
189 LowestBandRate(sample_rate_hz_), 1);
190
191 CopyAudioBufferIntoFrame(input, num_bands_, frame_length_,
192 &render_queue_input_frame_);
193
194 if (render_highpass_filter_) {
195 render_highpass_filter_->Process(render_queue_input_frame_[0]);
196 }
197
198 return render_transfer_queue_->Insert(&render_queue_input_frame_);
199 }
200
17 int EchoCanceller3::instance_count_ = 0; 201 int EchoCanceller3::instance_count_ = 0;
18 202
19 EchoCanceller3::EchoCanceller3(int sample_rate_hz, bool use_anti_hum_filter) { 203 EchoCanceller3::EchoCanceller3(int sample_rate_hz, bool use_highpass_filter)
20 int band_sample_rate_hz = (sample_rate_hz == 8000 ? sample_rate_hz : 16000); 204 : EchoCanceller3(sample_rate_hz,
21 frame_length_ = rtc::CheckedDivExact(band_sample_rate_hz, 100); 205 use_highpass_filter,
22 206 std::unique_ptr<BlockProcessor>(BlockProcessor::Create(
23 LOG(LS_INFO) << "AEC3 created : " 207 sample_rate_hz,
24 << "{ instance_count: " << instance_count_ << "}"; 208 NumBandsForRate(sample_rate_hz)))) {}
209 EchoCanceller3::EchoCanceller3(int sample_rate_hz,
210 bool use_highpass_filter,
211 std::unique_ptr<BlockProcessor> block_processor)
212 : data_dumper_(new ApmDataDumper(instance_count_)),
213 sample_rate_hz_(sample_rate_hz),
214 num_bands_(NumBandsForRate(sample_rate_hz_)),
215 frame_length_(rtc::CheckedDivExact(LowestBandRate(sample_rate_hz_), 100)),
216 output_framer_(num_bands_),
217 capture_blocker_(num_bands_),
218 render_blocker_(num_bands_),
219 render_transfer_queue_(
220 kRenderTransferQueueSize,
221 std::vector<std::vector<float>>(
222 num_bands_,
223 std::vector<float>(frame_length_, 0.f)),
224 Aec3RenderQueueItemVerifier(num_bands_, frame_length_)),
225 block_processor_(std::move(block_processor)),
226 render_queue_output_frame_(num_bands_,
227 std::vector<float>(frame_length_, 0.f)),
228 block_(num_bands_, std::vector<float>(kBlockSize, 0.f)),
229 sub_frame_view_(num_bands_) {
230 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter;
231 if (use_highpass_filter) {
232 render_highpass_filter.reset(new CascadedBiQuadFilter(
233 sample_rate_hz_ == 8000 ? kHighPassFilterCoefficients_8kHz
234 : kHighPassFilterCoefficients_16kHz,
235 sample_rate_hz_ == 8000 ? kNumberOfHighPassBiQuads_8kHz
236 : kNumberOfHighPassBiQuads_16kHz));
237 capture_highpass_filter_.reset(new CascadedBiQuadFilter(
238 sample_rate_hz_ == 8000 ? kHighPassFilterCoefficients_8kHz
239 : kHighPassFilterCoefficients_16kHz,
240 sample_rate_hz_ == 8000 ? kNumberOfHighPassBiQuads_8kHz
241 : kNumberOfHighPassBiQuads_16kHz));
242 } else {
243 render_highpass_filter.reset(nullptr);
244 }
245
246 render_writer_.reset(
247 new RenderWriter(data_dumper_.get(), &render_transfer_queue_,
248 std::move(render_highpass_filter), sample_rate_hz_,
249 frame_length_, num_bands_));
250
251 RTC_DCHECK_EQ(num_bands_, std::max(sample_rate_hz_, 16000) / 16000);
252 RTC_DCHECK_GE(kMaxNumBands, num_bands_);
25 instance_count_ = rtc::AtomicOps::Increment(&instance_count_); 253 instance_count_ = rtc::AtomicOps::Increment(&instance_count_);
26 } 254 }
27 255
28 EchoCanceller3::~EchoCanceller3() = default; 256 EchoCanceller3::~EchoCanceller3() = default;
29 257
30 bool EchoCanceller3::AnalyzeRender(AudioBuffer* render) { 258 bool EchoCanceller3::AnalyzeRender(AudioBuffer* render) {
31 RTC_DCHECK_EQ(1u, render->num_channels()); 259 RTC_DCHECK_RUNS_SERIALIZED(&render_race_checker_);
32 RTC_DCHECK_EQ(frame_length_, render->num_frames_per_band()); 260 return render_writer_->Insert(render);
33 return true; 261 }
34 } 262
35 263 void EchoCanceller3::AnalyzeCapture(AudioBuffer* capture) {
36 void EchoCanceller3::AnalyzeCapture(AudioBuffer* capture) {} 264 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
265 data_dumper_->DumpWav("aec3_capture_analyze_input", frame_length_,
266 capture->channels_f()[0], sample_rate_hz_, 1);
267
268 saturated_microphone_signal_ = false;
269 for (size_t k = 0; k < capture->num_channels(); ++k) {
270 saturated_microphone_signal_ |=
271 DetectSaturation(rtc::ArrayView<const float>(capture->channels_f()[k],
272 capture->num_frames()));
273 if (saturated_microphone_signal_) {
274 break;
275 }
276 }
277 }
37 278
38 void EchoCanceller3::ProcessCapture(AudioBuffer* capture, 279 void EchoCanceller3::ProcessCapture(AudioBuffer* capture,
39 bool known_echo_path_change) { 280 bool known_echo_path_change) {
281 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
40 RTC_DCHECK_EQ(1u, capture->num_channels()); 282 RTC_DCHECK_EQ(1u, capture->num_channels());
41 RTC_DCHECK_EQ(frame_length_, capture->num_frames_per_band()); 283 RTC_DCHECK_EQ(frame_length_, capture->num_frames_per_band());
284
285 rtc::ArrayView<float> capture_lower_band =
286 rtc::ArrayView<float>(&capture->split_bands_f(0)[0][0], frame_length_);
287
288 data_dumper_->DumpWav("aec3_capture_input", capture_lower_band,
289 LowestBandRate(sample_rate_hz_), 1);
290
291 const bool render_buffer_overrun = EmptyRenderQueue();
292 RTC_DCHECK(!render_buffer_overrun);
293
294 if (capture_highpass_filter_) {
295 capture_highpass_filter_->Process(capture_lower_band);
296 }
297
298 ProcessCaptureFrameContent(capture, known_echo_path_change,
299 saturated_microphone_signal_, 0, &capture_blocker_,
300 &output_framer_, block_processor_.get(), &block_,
301 &sub_frame_view_);
302
303 if (sample_rate_hz_ != 8000) {
304 ProcessCaptureFrameContent(
305 capture, known_echo_path_change, saturated_microphone_signal_, 1,
306 &capture_blocker_, &output_framer_, block_processor_.get(), &block_,
307 &sub_frame_view_);
308 }
309
310 ProcessRemainingCaptureFrameContent(
311 known_echo_path_change, saturated_microphone_signal_, &capture_blocker_,
312 &output_framer_, block_processor_.get(), &block_);
313
314 data_dumper_->DumpWav("aec3_capture_output", frame_length_,
315 &capture->split_bands_f(0)[0][0],
316 LowestBandRate(sample_rate_hz_), 1);
42 } 317 }
43 318
44 std::string EchoCanceller3::ToString( 319 std::string EchoCanceller3::ToString(
45 const AudioProcessing::Config::EchoCanceller3& config) { 320 const AudioProcessing::Config::EchoCanceller3& config) {
46 std::stringstream ss; 321 std::stringstream ss;
47 ss << "{" 322 ss << "{"
48 << "enabled: " << (config.enabled ? "true" : "false") << "}"; 323 << "enabled: " << (config.enabled ? "true" : "false") << "}";
49 return ss.str(); 324 return ss.str();
50 } 325 }
51 326
52 bool EchoCanceller3::Validate( 327 bool EchoCanceller3::Validate(
53 const AudioProcessing::Config::EchoCanceller3& config) { 328 const AudioProcessing::Config::EchoCanceller3& config) {
54 return true; 329 return true;
55 } 330 }
56 331
332 bool EchoCanceller3::EmptyRenderQueue() {
333 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
334 bool render_buffer_overrun = false;
335 bool frame_to_buffer =
336 render_transfer_queue_.Remove(&render_queue_output_frame_);
337 while (frame_to_buffer) {
338 render_buffer_overrun |= BufferRenderFrameContent(
339 &render_queue_output_frame_, 0, &render_blocker_,
340 block_processor_.get(), &block_, &sub_frame_view_);
341
342 if (sample_rate_hz_ != 8000) {
343 render_buffer_overrun |= BufferRenderFrameContent(
344 &render_queue_output_frame_, 1, &render_blocker_,
345 block_processor_.get(), &block_, &sub_frame_view_);
346 }
347
348 render_buffer_overrun |= BufferRemainingRenderFrameContent(
349 &render_blocker_, block_processor_.get(), &block_);
350
351 frame_to_buffer =
352 render_transfer_queue_.Remove(&render_queue_output_frame_);
353 }
354 return render_buffer_overrun;
355 }
356
57 } // namespace webrtc 357 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698