blob: 3ec74cc09323709eacf59598491f1d21967a9c2e [file] [log] [blame]
peahd0263542017-01-03 04:20:34 -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 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/frame_blocker.h"
peahd0263542017-01-03 04:20:34 -080012
peahd0263542017-01-03 04:20:34 -080013#include <string>
14#include <vector>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_processing/aec3/aec3_common.h"
17#include "modules/audio_processing/aec3/block_framer.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020018#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "test/gtest.h"
peahd0263542017-01-03 04:20:34 -080020
21namespace webrtc {
22namespace {
23
24float ComputeSampleValue(size_t chunk_counter,
25 size_t chunk_size,
26 size_t band,
27 size_t sample_index,
28 int offset) {
29 float value =
Per Åhgrend112c752019-09-02 13:56:56 +000030 static_cast<int>(chunk_counter * chunk_size + sample_index) + offset;
peahd0263542017-01-03 04:20:34 -080031 return value > 0 ? 5000 * band + value : 0;
32}
33
34void FillSubFrame(size_t sub_frame_counter,
35 int offset,
Per Åhgrend112c752019-09-02 13:56:56 +000036 std::vector<std::vector<float>>* sub_frame) {
37 for (size_t k = 0; k < sub_frame->size(); ++k) {
38 for (size_t i = 0; i < (*sub_frame)[0].size(); ++i) {
39 (*sub_frame)[k][i] =
40 ComputeSampleValue(sub_frame_counter, kSubFrameLength, k, i, offset);
peahd0263542017-01-03 04:20:34 -080041 }
42 }
43}
44
Per Åhgrend112c752019-09-02 13:56:56 +000045void FillSubFrameView(size_t sub_frame_counter,
46 int offset,
47 std::vector<std::vector<float>>* sub_frame,
48 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
peahd0263542017-01-03 04:20:34 -080049 FillSubFrame(sub_frame_counter, offset, sub_frame);
Per Åhgrend112c752019-09-02 13:56:56 +000050 for (size_t k = 0; k < sub_frame_view->size(); ++k) {
51 (*sub_frame_view)[k] =
52 rtc::ArrayView<float>(&(*sub_frame)[k][0], (*sub_frame)[k].size());
peahd0263542017-01-03 04:20:34 -080053 }
54}
55
Per Åhgrend112c752019-09-02 13:56:56 +000056bool VerifySubFrame(size_t sub_frame_counter,
57 int offset,
58 const std::vector<rtc::ArrayView<float>>& sub_frame_view) {
59 std::vector<std::vector<float>> reference_sub_frame(
60 sub_frame_view.size(), std::vector<float>(sub_frame_view[0].size(), 0.f));
peahd0263542017-01-03 04:20:34 -080061 FillSubFrame(sub_frame_counter, offset, &reference_sub_frame);
Per Åhgrend112c752019-09-02 13:56:56 +000062 for (size_t k = 0; k < sub_frame_view.size(); ++k) {
63 for (size_t i = 0; i < sub_frame_view[k].size(); ++i) {
64 if (reference_sub_frame[k][i] != sub_frame_view[k][i]) {
65 return false;
peahd0263542017-01-03 04:20:34 -080066 }
67 }
68 }
69 return true;
70}
71
72bool VerifyBlock(size_t block_counter,
73 int offset,
Per Åhgrend112c752019-09-02 13:56:56 +000074 const std::vector<std::vector<float>>& block) {
75 for (size_t k = 0; k < block.size(); ++k) {
76 for (size_t i = 0; i < block[k].size(); ++i) {
77 const float reference_value =
78 ComputeSampleValue(block_counter, kBlockSize, k, i, offset);
79 if (reference_value != block[k][i]) {
80 return false;
peahd0263542017-01-03 04:20:34 -080081 }
82 }
83 }
84 return true;
85}
86
87// Verifies that the FrameBlocker properly forms blocks out of the frames.
Per Åhgrend112c752019-09-02 13:56:56 +000088void RunBlockerTest(int sample_rate_hz) {
peahd0263542017-01-03 04:20:34 -080089 constexpr size_t kNumSubFramesToProcess = 20;
90 const size_t num_bands = NumBandsForRate(sample_rate_hz);
91
Per Åhgrend112c752019-09-02 13:56:56 +000092 std::vector<std::vector<float>> block(num_bands,
93 std::vector<float>(kBlockSize, 0.f));
94 std::vector<std::vector<float>> input_sub_frame(
95 num_bands, std::vector<float>(kSubFrameLength, 0.f));
96 std::vector<rtc::ArrayView<float>> input_sub_frame_view(num_bands);
97 FrameBlocker blocker(num_bands);
peahd0263542017-01-03 04:20:34 -080098
99 size_t block_counter = 0;
100 for (size_t sub_frame_index = 0; sub_frame_index < kNumSubFramesToProcess;
101 ++sub_frame_index) {
102 FillSubFrameView(sub_frame_index, 0, &input_sub_frame,
103 &input_sub_frame_view);
104
105 blocker.InsertSubFrameAndExtractBlock(input_sub_frame_view, &block);
106 VerifyBlock(block_counter++, 0, block);
107
108 if ((sub_frame_index + 1) % 4 == 0) {
109 EXPECT_TRUE(blocker.IsBlockAvailable());
110 } else {
111 EXPECT_FALSE(blocker.IsBlockAvailable());
112 }
113 if (blocker.IsBlockAvailable()) {
114 blocker.ExtractBlock(&block);
115 VerifyBlock(block_counter++, 0, block);
116 }
117 }
118}
119
120// Verifies that the FrameBlocker and BlockFramer work well together and produce
121// the expected output.
Per Åhgrend112c752019-09-02 13:56:56 +0000122void RunBlockerAndFramerTest(int sample_rate_hz) {
peahd0263542017-01-03 04:20:34 -0800123 const size_t kNumSubFramesToProcess = 20;
124 const size_t num_bands = NumBandsForRate(sample_rate_hz);
125
Per Åhgrend112c752019-09-02 13:56:56 +0000126 std::vector<std::vector<float>> block(num_bands,
127 std::vector<float>(kBlockSize, 0.f));
128 std::vector<std::vector<float>> input_sub_frame(
129 num_bands, std::vector<float>(kSubFrameLength, 0.f));
130 std::vector<std::vector<float>> output_sub_frame(
131 num_bands, std::vector<float>(kSubFrameLength, 0.f));
132 std::vector<rtc::ArrayView<float>> output_sub_frame_view(num_bands);
133 std::vector<rtc::ArrayView<float>> input_sub_frame_view(num_bands);
134 FrameBlocker blocker(num_bands);
135 BlockFramer framer(num_bands);
peahd0263542017-01-03 04:20:34 -0800136
137 for (size_t sub_frame_index = 0; sub_frame_index < kNumSubFramesToProcess;
138 ++sub_frame_index) {
139 FillSubFrameView(sub_frame_index, 0, &input_sub_frame,
140 &input_sub_frame_view);
141 FillSubFrameView(sub_frame_index, 0, &output_sub_frame,
142 &output_sub_frame_view);
143
144 blocker.InsertSubFrameAndExtractBlock(input_sub_frame_view, &block);
145 framer.InsertBlockAndExtractSubFrame(block, &output_sub_frame_view);
146
147 if ((sub_frame_index + 1) % 4 == 0) {
148 EXPECT_TRUE(blocker.IsBlockAvailable());
149 } else {
150 EXPECT_FALSE(blocker.IsBlockAvailable());
151 }
152 if (blocker.IsBlockAvailable()) {
153 blocker.ExtractBlock(&block);
154 framer.InsertBlock(block);
155 }
Per Åhgrend112c752019-09-02 13:56:56 +0000156 EXPECT_TRUE(VerifySubFrame(sub_frame_index, -64, output_sub_frame_view));
peahd0263542017-01-03 04:20:34 -0800157 }
158}
159
160#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
161// Verifies that the FrameBlocker crashes if the InsertSubFrameAndExtractBlock
162// method is called for inputs with the wrong number of bands or band lengths.
Per Åhgrend112c752019-09-02 13:56:56 +0000163void RunWronglySizedInsertAndExtractParametersTest(int sample_rate_hz,
164 size_t num_block_bands,
165 size_t block_length,
166 size_t num_sub_frame_bands,
167 size_t sub_frame_length) {
peahd0263542017-01-03 04:20:34 -0800168 const size_t correct_num_bands = NumBandsForRate(sample_rate_hz);
169
Per Åhgrend112c752019-09-02 13:56:56 +0000170 std::vector<std::vector<float>> block(num_block_bands,
171 std::vector<float>(block_length, 0.f));
172 std::vector<std::vector<float>> input_sub_frame(
173 num_sub_frame_bands, std::vector<float>(sub_frame_length, 0.f));
174 std::vector<rtc::ArrayView<float>> input_sub_frame_view(
175 input_sub_frame.size());
peahd0263542017-01-03 04:20:34 -0800176 FillSubFrameView(0, 0, &input_sub_frame, &input_sub_frame_view);
Per Åhgrend112c752019-09-02 13:56:56 +0000177 FrameBlocker blocker(correct_num_bands);
peahd0263542017-01-03 04:20:34 -0800178 EXPECT_DEATH(
179 blocker.InsertSubFrameAndExtractBlock(input_sub_frame_view, &block), "");
180}
181
182// Verifies that the FrameBlocker crashes if the ExtractBlock method is called
183// for inputs with the wrong number of bands or band lengths.
184void RunWronglySizedExtractParameterTest(int sample_rate_hz,
185 size_t num_block_bands,
186 size_t block_length) {
187 const size_t correct_num_bands = NumBandsForRate(sample_rate_hz);
188
Per Åhgrend112c752019-09-02 13:56:56 +0000189 std::vector<std::vector<float>> correct_block(
190 correct_num_bands, std::vector<float>(kBlockSize, 0.f));
191 std::vector<std::vector<float>> wrong_block(
192 num_block_bands, std::vector<float>(block_length, 0.f));
193 std::vector<std::vector<float>> input_sub_frame(
194 correct_num_bands, std::vector<float>(kSubFrameLength, 0.f));
195 std::vector<rtc::ArrayView<float>> input_sub_frame_view(
196 input_sub_frame.size());
peahd0263542017-01-03 04:20:34 -0800197 FillSubFrameView(0, 0, &input_sub_frame, &input_sub_frame_view);
Per Åhgrend112c752019-09-02 13:56:56 +0000198 FrameBlocker blocker(correct_num_bands);
peahd0263542017-01-03 04:20:34 -0800199 blocker.InsertSubFrameAndExtractBlock(input_sub_frame_view, &correct_block);
200 blocker.InsertSubFrameAndExtractBlock(input_sub_frame_view, &correct_block);
201 blocker.InsertSubFrameAndExtractBlock(input_sub_frame_view, &correct_block);
202 blocker.InsertSubFrameAndExtractBlock(input_sub_frame_view, &correct_block);
203
204 EXPECT_DEATH(blocker.ExtractBlock(&wrong_block), "");
205}
206
207// Verifies that the FrameBlocker crashes if the ExtractBlock method is called
208// after a wrong number of previous InsertSubFrameAndExtractBlock method calls
209// have been made.
210void RunWrongExtractOrderTest(int sample_rate_hz,
211 size_t num_preceeding_api_calls) {
Per Åhgrend112c752019-09-02 13:56:56 +0000212 const size_t correct_num_bands = NumBandsForRate(sample_rate_hz);
peahd0263542017-01-03 04:20:34 -0800213
Per Åhgrend112c752019-09-02 13:56:56 +0000214 std::vector<std::vector<float>> block(correct_num_bands,
215 std::vector<float>(kBlockSize, 0.f));
216 std::vector<std::vector<float>> input_sub_frame(
217 correct_num_bands, std::vector<float>(kSubFrameLength, 0.f));
218 std::vector<rtc::ArrayView<float>> input_sub_frame_view(
219 input_sub_frame.size());
peahd0263542017-01-03 04:20:34 -0800220 FillSubFrameView(0, 0, &input_sub_frame, &input_sub_frame_view);
Per Åhgrend112c752019-09-02 13:56:56 +0000221 FrameBlocker blocker(correct_num_bands);
peahd0263542017-01-03 04:20:34 -0800222 for (size_t k = 0; k < num_preceeding_api_calls; ++k) {
223 blocker.InsertSubFrameAndExtractBlock(input_sub_frame_view, &block);
224 }
225
226 EXPECT_DEATH(blocker.ExtractBlock(&block), "");
227}
228#endif
229
Per Åhgrend112c752019-09-02 13:56:56 +0000230std::string ProduceDebugText(int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200231 rtc::StringBuilder ss;
peahd0263542017-01-03 04:20:34 -0800232 ss << "Sample rate: " << sample_rate_hz;
Jonas Olsson84df1c72018-09-14 16:59:32 +0200233 return ss.Release();
peahd0263542017-01-03 04:20:34 -0800234}
235
236} // namespace
237
238#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
239TEST(FrameBlocker, WrongNumberOfBandsInBlockForInsertSubFrameAndExtractBlock) {
Per Åhgrend112c752019-09-02 13:56:56 +0000240 for (auto rate : {8000, 16000, 32000, 48000}) {
241 SCOPED_TRACE(ProduceDebugText(rate));
242 const size_t correct_num_bands = NumBandsForRate(rate);
243 const size_t wrong_num_bands = (correct_num_bands % 3) + 1;
244 RunWronglySizedInsertAndExtractParametersTest(
245 rate, wrong_num_bands, kBlockSize, correct_num_bands, kSubFrameLength);
peahd0263542017-01-03 04:20:34 -0800246 }
247}
248
249TEST(FrameBlocker,
250 WrongNumberOfBandsInSubFrameForInsertSubFrameAndExtractBlock) {
Per Åhgrend112c752019-09-02 13:56:56 +0000251 for (auto rate : {8000, 16000, 32000, 48000}) {
252 SCOPED_TRACE(ProduceDebugText(rate));
253 const size_t correct_num_bands = NumBandsForRate(rate);
254 const size_t wrong_num_bands = (correct_num_bands % 3) + 1;
255 RunWronglySizedInsertAndExtractParametersTest(
256 rate, correct_num_bands, kBlockSize, wrong_num_bands, kSubFrameLength);
peahd0263542017-01-03 04:20:34 -0800257 }
258}
259
260TEST(FrameBlocker,
261 WrongNumberOfSamplesInBlockForInsertSubFrameAndExtractBlock) {
Per Åhgrend112c752019-09-02 13:56:56 +0000262 for (auto rate : {8000, 16000, 32000, 48000}) {
263 SCOPED_TRACE(ProduceDebugText(rate));
264 const size_t correct_num_bands = NumBandsForRate(rate);
265 RunWronglySizedInsertAndExtractParametersTest(
266 rate, correct_num_bands, kBlockSize - 1, correct_num_bands,
267 kSubFrameLength);
peahd0263542017-01-03 04:20:34 -0800268 }
269}
270
271TEST(FrameBlocker,
272 WrongNumberOfSamplesInSubFrameForInsertSubFrameAndExtractBlock) {
Per Åhgrend112c752019-09-02 13:56:56 +0000273 for (auto rate : {8000, 16000, 32000, 48000}) {
274 SCOPED_TRACE(ProduceDebugText(rate));
275 const size_t correct_num_bands = NumBandsForRate(rate);
276 RunWronglySizedInsertAndExtractParametersTest(rate, correct_num_bands,
277 kBlockSize, correct_num_bands,
278 kSubFrameLength - 1);
Per Åhgrenf3a197e2019-08-30 08:54:09 +0200279 }
280}
281
Per Åhgrena66395e2019-08-30 08:54:09 +0200282TEST(FrameBlocker, WrongNumberOfBandsInBlockForExtractBlock) {
Per Åhgrend112c752019-09-02 13:56:56 +0000283 for (auto rate : {8000, 16000, 32000, 48000}) {
284 SCOPED_TRACE(ProduceDebugText(rate));
285 const size_t correct_num_bands = NumBandsForRate(rate);
286 const size_t wrong_num_bands = (correct_num_bands % 3) + 1;
287 RunWronglySizedExtractParameterTest(rate, wrong_num_bands, kBlockSize);
Per Åhgrena66395e2019-08-30 08:54:09 +0200288 }
289}
290
291TEST(FrameBlocker, WrongNumberOfSamplesInBlockForExtractBlock) {
Per Åhgrend112c752019-09-02 13:56:56 +0000292 for (auto rate : {8000, 16000, 32000, 48000}) {
293 SCOPED_TRACE(ProduceDebugText(rate));
294 const size_t correct_num_bands = NumBandsForRate(rate);
295 RunWronglySizedExtractParameterTest(rate, correct_num_bands,
296 kBlockSize - 1);
Per Åhgrena66395e2019-08-30 08:54:09 +0200297 }
298}
299
300TEST(FrameBlocker, WrongNumberOfPreceedingApiCallsForExtractBlock) {
Per Åhgrend112c752019-09-02 13:56:56 +0000301 for (auto rate : {8000, 16000, 32000, 48000}) {
302 for (size_t num_calls = 0; num_calls < 4; ++num_calls) {
303 rtc::StringBuilder ss;
304 ss << "Sample rate: " << rate;
305 ss << ", Num preceeding InsertSubFrameAndExtractBlock calls: "
306 << num_calls;
Per Åhgrena66395e2019-08-30 08:54:09 +0200307
Per Åhgrend112c752019-09-02 13:56:56 +0000308 SCOPED_TRACE(ss.str());
309 RunWrongExtractOrderTest(rate, num_calls);
Per Åhgrena66395e2019-08-30 08:54:09 +0200310 }
311 }
312}
313
peahd0263542017-01-03 04:20:34 -0800314// Verifiers that the verification for null sub_frame pointer works.
315TEST(FrameBlocker, NullBlockParameter) {
Per Åhgrend112c752019-09-02 13:56:56 +0000316 std::vector<std::vector<float>> sub_frame(
317 1, std::vector<float>(kSubFrameLength, 0.f));
318 std::vector<rtc::ArrayView<float>> sub_frame_view(sub_frame.size());
peahd0263542017-01-03 04:20:34 -0800319 FillSubFrameView(0, 0, &sub_frame, &sub_frame_view);
320 EXPECT_DEATH(
Per Åhgrend112c752019-09-02 13:56:56 +0000321 FrameBlocker(1).InsertSubFrameAndExtractBlock(sub_frame_view, nullptr),
peahd0263542017-01-03 04:20:34 -0800322 "");
323}
324
325#endif
326
327TEST(FrameBlocker, BlockBitexactness) {
Per Åhgrend112c752019-09-02 13:56:56 +0000328 for (auto rate : {8000, 16000, 32000, 48000}) {
329 SCOPED_TRACE(ProduceDebugText(rate));
330 RunBlockerTest(rate);
peahd0263542017-01-03 04:20:34 -0800331 }
332}
333
334TEST(FrameBlocker, BlockerAndFramer) {
Per Åhgrend112c752019-09-02 13:56:56 +0000335 for (auto rate : {8000, 16000, 32000, 48000}) {
336 SCOPED_TRACE(ProduceDebugText(rate));
337 RunBlockerAndFramerTest(rate);
peahd0263542017-01-03 04:20:34 -0800338 }
339}
340
341} // namespace webrtc