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

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: Added more crash tests 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 <sstream>
13
12 #include "webrtc/base/atomicops.h" 14 #include "webrtc/base/atomicops.h"
13 #include "webrtc/system_wrappers/include/logging.h" 15 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h"
14 16
15 namespace webrtc { 17 namespace webrtc {
16 18
19 namespace {
20
21 bool DetectSaturation(rtc::ArrayView<const float> y) {
22 for (auto y_k : y) {
23 if (y_k >= 32767.0f || y_k <= -32768.0f) {
24 return true;
25 }
26 }
27 return false;
28 }
29
30 void FillSubFrameView(AudioBuffer* frame,
31 size_t sub_frame_index,
32 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
33 RTC_DCHECK_GE(1, sub_frame_index);
34 RTC_DCHECK_LE(0, sub_frame_index);
35 RTC_DCHECK_EQ(frame->num_bands(), sub_frame_view->size());
36 for (size_t k = 0; k < sub_frame_view->size(); ++k) {
37 (*sub_frame_view)[k] = rtc::ArrayView<float>(
38 &frame->split_bands_f(0)[k][sub_frame_index * kSubFrameLength],
39 kSubFrameLength);
40 }
41 }
42
43 void FillSubFrameView(std::vector<std::vector<float>>* frame,
44 size_t sub_frame_index,
45 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
46 RTC_DCHECK_GE(1, sub_frame_index);
47 RTC_DCHECK_EQ(frame->size(), sub_frame_view->size());
48 for (size_t k = 0; k < frame->size(); ++k) {
49 (*sub_frame_view)[k] = rtc::ArrayView<float>(
50 &(*frame)[k][sub_frame_index * kSubFrameLength], kSubFrameLength);
51 }
52 }
53
54 void ProcessCaptureFrameContent(
55 AudioBuffer* capture,
56 bool known_echo_path_change,
57 bool saturated_microphone_signal,
58 size_t sub_frame_index,
59 FrameBlocker* capture_blocker,
60 BlockFramer* output_framer,
61 BlockProcessor* block_processor,
62 std::vector<std::vector<float>>* block,
63 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
64 FillSubFrameView(capture, sub_frame_index, sub_frame_view);
65 capture_blocker->InsertSubFrameAndExtractBlock(*sub_frame_view, block);
66 block_processor->ProcessCapture(known_echo_path_change,
67 saturated_microphone_signal, block);
68 output_framer->InsertBlockAndExtractSubFrame(*block, sub_frame_view);
69 }
70
71 void ProcessRemainingCaptureFrameContent(
72 bool known_echo_path_change,
73 bool saturated_microphone_signal,
74 FrameBlocker* capture_blocker,
75 BlockFramer* output_framer,
76 BlockProcessor* block_processor,
77 std::vector<std::vector<float>>* block) {
78 if (!capture_blocker->IsBlockAvailable()) {
79 return;
80 }
81
82 capture_blocker->ExtractBlock(block);
83 block_processor->ProcessCapture(known_echo_path_change,
84 saturated_microphone_signal, block);
85 output_framer->InsertBlock(*block);
86 }
87
88 bool BufferRenderFrameContent(
89 std::vector<std::vector<float>>* render_frame,
90 size_t sub_frame_index,
91 FrameBlocker* render_blocker,
92 BlockProcessor* block_processor,
93 std::vector<std::vector<float>>* block,
94 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
95 FillSubFrameView(render_frame, sub_frame_index, sub_frame_view);
96 render_blocker->InsertSubFrameAndExtractBlock(*sub_frame_view, block);
97 return block_processor->BufferRender(block);
98 }
99
100 bool BufferRemainingRenderFrameContent(FrameBlocker* render_blocker,
101 BlockProcessor* block_processor,
102 std::vector<std::vector<float>>* block) {
103 if (!render_blocker->IsBlockAvailable()) {
104 return false;
105 }
106 render_blocker->ExtractBlock(block);
107 return block_processor->BufferRender(block);
108 }
109
110 void CopyAudioBufferIntoFrame(AudioBuffer* buffer,
111 size_t num_bands,
112 size_t frame_length,
113 std::vector<std::vector<float>>* frame) {
114 RTC_DCHECK_EQ(num_bands, frame->size());
115 for (size_t i = 0; i < num_bands; ++i) {
116 rtc::ArrayView<float> buffer_view(&buffer->split_bands_f(0)[i][0],
117 frame_length);
118 std::copy(buffer_view.begin(), buffer_view.end(), (*frame)[i].begin());
119 }
120 }
121
122 // [B,A] = butter(2,100/4000,'high')
123 const CascadedBiQuadFilter::BiQuadCoefficients
124 kHighPassFilterCoefficients_8kHz = {
125 {0.945976856002790, -1.891953712005580, 0.945976856002790},
126 {-1.889033079394525, 0.894874344616636}};
127 const int kNumberOfHighPassBiQuads_8kHz = 1;
128
129 // [B,A] = butter(2,100/8000,'high')
130 const CascadedBiQuadFilter::BiQuadCoefficients
131 kHighPassFilterCoefficients_16kHz = {
132 {0.972613898499844, -1.945227796999688, 0.972613898499844},
133 {-1.944477657767094, 0.945977936232282}};
134 const int kNumberOfHighPassBiQuads_16kHz = 1;
135
136 static constexpr size_t kRenderTransferQueueSize = 30;
137
138 } // namespace
139
140 class EchoCanceller3::RenderWriter {
141 public:
142 RenderWriter(ApmDataDumper* data_dumper,
143 SwapQueue<std::vector<std::vector<float>>,
144 Aec3RenderQueueItemVerifier>* render_transfer_queue,
145 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter,
146 int sample_rate_hz,
147 int frame_length,
148 int num_bands);
149 ~RenderWriter();
150 bool Insert(AudioBuffer* render);
151
152 private:
153 ApmDataDumper* data_dumper_;
154 const int sample_rate_hz_;
155 const size_t frame_length_;
156 const int num_bands_;
157 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter_;
158 std::vector<std::vector<float>> render_queue_input_frame_;
159 SwapQueue<std::vector<std::vector<float>>, Aec3RenderQueueItemVerifier>*
160 render_transfer_queue_;
161 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderWriter);
162 };
163
164 EchoCanceller3::RenderWriter::RenderWriter(
165 ApmDataDumper* data_dumper,
166 SwapQueue<std::vector<std::vector<float>>, Aec3RenderQueueItemVerifier>*
167 render_transfer_queue,
168 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter,
169 int sample_rate_hz,
170 int frame_length,
171 int num_bands)
172 : data_dumper_(data_dumper),
173 sample_rate_hz_(sample_rate_hz),
174 frame_length_(frame_length),
175 num_bands_(num_bands),
176 render_highpass_filter_(std::move(render_highpass_filter)),
177 render_queue_input_frame_(num_bands_,
178 std::vector<float>(frame_length_, 0.f)),
179 render_transfer_queue_(render_transfer_queue) {
180 RTC_DCHECK(data_dumper);
181 }
182
183 EchoCanceller3::RenderWriter::~RenderWriter() = default;
184
185 bool EchoCanceller3::RenderWriter::Insert(AudioBuffer* input) {
186 RTC_DCHECK_EQ(1, input->num_channels());
187 RTC_DCHECK_EQ(num_bands_, input->num_bands());
188 RTC_DCHECK_EQ(frame_length_, input->num_frames_per_band());
189 data_dumper_->DumpWav("aec3_render_input", frame_length_,
190 &input->split_bands_f(0)[0][0],
191 LowestBandRate(sample_rate_hz_), 1);
192
193 CopyAudioBufferIntoFrame(input, num_bands_, frame_length_,
194 &render_queue_input_frame_);
195
196 if (render_highpass_filter_) {
197 render_highpass_filter_->Process(render_queue_input_frame_[0]);
198 }
199
200 return render_transfer_queue_->Insert(&render_queue_input_frame_);
201 }
202
17 int EchoCanceller3::instance_count_ = 0; 203 int EchoCanceller3::instance_count_ = 0;
18 204
19 EchoCanceller3::EchoCanceller3(int sample_rate_hz, bool use_anti_hum_filter) { 205 EchoCanceller3::EchoCanceller3(int sample_rate_hz, bool use_highpass_filter)
20 int band_sample_rate_hz = (sample_rate_hz == 8000 ? sample_rate_hz : 16000); 206 : EchoCanceller3(sample_rate_hz,
21 frame_length_ = rtc::CheckedDivExact(band_sample_rate_hz, 100); 207 use_highpass_filter,
22 208 std::unique_ptr<BlockProcessor>(
23 LOG(LS_INFO) << "AEC3 created : " 209 BlockProcessor::Create(sample_rate_hz))) {}
24 << "{ instance_count: " << instance_count_ << "}"; 210 EchoCanceller3::EchoCanceller3(int sample_rate_hz,
211 bool use_highpass_filter,
212 std::unique_ptr<BlockProcessor> block_processor)
213 : data_dumper_(new ApmDataDumper(instance_count_)),
214 sample_rate_hz_(sample_rate_hz),
215 num_bands_(NumBandsForRate(sample_rate_hz_)),
216 frame_length_(rtc::CheckedDivExact(LowestBandRate(sample_rate_hz_), 100)),
217 output_framer_(num_bands_),
218 capture_blocker_(num_bands_),
219 render_blocker_(num_bands_),
220 render_transfer_queue_(
221 kRenderTransferQueueSize,
222 std::vector<std::vector<float>>(
223 num_bands_,
224 std::vector<float>(frame_length_, 0.f)),
225 Aec3RenderQueueItemVerifier(num_bands_, frame_length_)),
226 block_processor_(std::move(block_processor)),
227 render_queue_output_frame_(num_bands_,
228 std::vector<float>(frame_length_, 0.f)),
229 block_(num_bands_, std::vector<float>(kBlockSize, 0.f)),
230 sub_frame_view_(num_bands_) {
231 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter;
232 if (use_highpass_filter) {
233 render_highpass_filter.reset(new CascadedBiQuadFilter(
234 sample_rate_hz_ == 8000 ? kHighPassFilterCoefficients_8kHz
235 : kHighPassFilterCoefficients_16kHz,
236 sample_rate_hz_ == 8000 ? kNumberOfHighPassBiQuads_8kHz
237 : kNumberOfHighPassBiQuads_16kHz));
238 capture_highpass_filter_.reset(new CascadedBiQuadFilter(
239 sample_rate_hz_ == 8000 ? kHighPassFilterCoefficients_8kHz
240 : kHighPassFilterCoefficients_16kHz,
241 sample_rate_hz_ == 8000 ? kNumberOfHighPassBiQuads_8kHz
242 : kNumberOfHighPassBiQuads_16kHz));
243 } else {
244 render_highpass_filter.reset(nullptr);
ivoc 2016/12/23 10:32:33 I think it's safe to remove this one as well. From
peah-webrtc 2017/01/02 08:45:10 Great! I think you are correct! It really looks li
245 }
246
247 render_writer_.reset(
248 new RenderWriter(data_dumper_.get(), &render_transfer_queue_,
249 std::move(render_highpass_filter), sample_rate_hz_,
250 frame_length_, num_bands_));
251
252 RTC_DCHECK_EQ(num_bands_, std::max(sample_rate_hz_, 16000) / 16000);
253 RTC_DCHECK_GE(kMaxNumBands, num_bands_);
25 instance_count_ = rtc::AtomicOps::Increment(&instance_count_); 254 instance_count_ = rtc::AtomicOps::Increment(&instance_count_);
ivoc 2016/12/23 10:32:33 See other comment about rtc::AtomicOps::Increment.
peah-webrtc 2017/01/02 08:45:10 Done.
26 } 255 }
27 256
28 EchoCanceller3::~EchoCanceller3() = default; 257 EchoCanceller3::~EchoCanceller3() = default;
29 258
30 bool EchoCanceller3::AnalyzeRender(AudioBuffer* render) { 259 bool EchoCanceller3::AnalyzeRender(AudioBuffer* render) {
31 RTC_DCHECK_EQ(1u, render->num_channels()); 260 RTC_DCHECK_RUNS_SERIALIZED(&render_race_checker_);
32 RTC_DCHECK_EQ(frame_length_, render->num_frames_per_band()); 261 return render_writer_->Insert(render);
33 return true; 262 }
34 } 263
35 264 void EchoCanceller3::AnalyzeCapture(AudioBuffer* capture) {
36 void EchoCanceller3::AnalyzeCapture(AudioBuffer* capture) {} 265 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
266 data_dumper_->DumpWav("aec3_capture_analyze_input", frame_length_,
267 capture->channels_f()[0], sample_rate_hz_, 1);
268
269 saturated_microphone_signal_ = false;
270 for (size_t k = 0; k < capture->num_channels(); ++k) {
271 saturated_microphone_signal_ |=
272 DetectSaturation(rtc::ArrayView<const float>(capture->channels_f()[k],
273 capture->num_frames()));
274 if (saturated_microphone_signal_) {
275 break;
276 }
277 }
278 }
37 279
38 void EchoCanceller3::ProcessCapture(AudioBuffer* capture, 280 void EchoCanceller3::ProcessCapture(AudioBuffer* capture,
39 bool known_echo_path_change) { 281 bool known_echo_path_change) {
282 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
40 RTC_DCHECK_EQ(1u, capture->num_channels()); 283 RTC_DCHECK_EQ(1u, capture->num_channels());
284 RTC_DCHECK_EQ(num_bands_, capture->num_bands());
41 RTC_DCHECK_EQ(frame_length_, capture->num_frames_per_band()); 285 RTC_DCHECK_EQ(frame_length_, capture->num_frames_per_band());
286
287 rtc::ArrayView<float> capture_lower_band =
288 rtc::ArrayView<float>(&capture->split_bands_f(0)[0][0], frame_length_);
289
290 data_dumper_->DumpWav("aec3_capture_input", capture_lower_band,
291 LowestBandRate(sample_rate_hz_), 1);
292
293 const bool render_buffer_overrun = EmptyRenderQueue();
294 RTC_DCHECK(!render_buffer_overrun);
295
296 if (capture_highpass_filter_) {
297 capture_highpass_filter_->Process(capture_lower_band);
298 }
299
300 ProcessCaptureFrameContent(capture, known_echo_path_change,
301 saturated_microphone_signal_, 0, &capture_blocker_,
302 &output_framer_, block_processor_.get(), &block_,
303 &sub_frame_view_);
304
305 if (sample_rate_hz_ != 8000) {
306 ProcessCaptureFrameContent(
307 capture, known_echo_path_change, saturated_microphone_signal_, 1,
308 &capture_blocker_, &output_framer_, block_processor_.get(), &block_,
309 &sub_frame_view_);
310 }
311
312 ProcessRemainingCaptureFrameContent(
313 known_echo_path_change, saturated_microphone_signal_, &capture_blocker_,
314 &output_framer_, block_processor_.get(), &block_);
315
316 data_dumper_->DumpWav("aec3_capture_output", frame_length_,
317 &capture->split_bands_f(0)[0][0],
318 LowestBandRate(sample_rate_hz_), 1);
42 } 319 }
43 320
44 std::string EchoCanceller3::ToString( 321 std::string EchoCanceller3::ToString(
45 const AudioProcessing::Config::EchoCanceller3& config) { 322 const AudioProcessing::Config::EchoCanceller3& config) {
46 std::stringstream ss; 323 std::stringstream ss;
47 ss << "{" 324 ss << "{"
48 << "enabled: " << (config.enabled ? "true" : "false") << "}"; 325 << "enabled: " << (config.enabled ? "true" : "false") << "}";
49 return ss.str(); 326 return ss.str();
50 } 327 }
51 328
52 bool EchoCanceller3::Validate( 329 bool EchoCanceller3::Validate(
53 const AudioProcessing::Config::EchoCanceller3& config) { 330 const AudioProcessing::Config::EchoCanceller3& config) {
54 return true; 331 return true;
55 } 332 }
56 333
334 bool EchoCanceller3::EmptyRenderQueue() {
335 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
336 bool render_buffer_overrun = false;
337 bool frame_to_buffer =
338 render_transfer_queue_.Remove(&render_queue_output_frame_);
339 while (frame_to_buffer) {
340 render_buffer_overrun |= BufferRenderFrameContent(
341 &render_queue_output_frame_, 0, &render_blocker_,
342 block_processor_.get(), &block_, &sub_frame_view_);
343
344 if (sample_rate_hz_ != 8000) {
345 render_buffer_overrun |= BufferRenderFrameContent(
346 &render_queue_output_frame_, 1, &render_blocker_,
347 block_processor_.get(), &block_, &sub_frame_view_);
348 }
349
350 render_buffer_overrun |= BufferRemainingRenderFrameContent(
351 &render_blocker_, block_processor_.get(), &block_);
352
353 frame_to_buffer =
354 render_transfer_queue_.Remove(&render_queue_output_frame_);
355 }
356 return render_buffer_overrun;
357 }
358
57 } // namespace webrtc 359 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698