Chromium Code Reviews

Side by Side Diff: webrtc/modules/audio_processing/aec3/block_framer_unittest.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.
Jump to:
View unified diff |
OLDNEW
(Empty)
1 /*
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
11 #include "webrtc/modules/audio_processing/aec3/block_framer.h"
12
13 #include <sstream>
14 #include <string>
15 #include <vector>
16
17 #include "webrtc/modules/audio_processing/aec3/aec3_constants.h"
18 #include "webrtc/test/gtest.h"
19
20 namespace webrtc {
21 namespace {
22
23 void SetupSubFrameView(std::vector<std::vector<float>>* sub_frame,
24 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
25 for (size_t k = 0; k < sub_frame_view->size(); ++k) {
26 (*sub_frame_view)[k] =
27 rtc::ArrayView<float>(&(*sub_frame)[k][0], (*sub_frame)[k].size());
aleloi 2016/12/23 14:28:38 I suggest to replace address of first element with
peah-webrtc 2017/01/02 08:45:10 I agree! Done.
28 }
29 }
30
31 float ComputeSampleValue(size_t chunk_counter,
32 size_t chunk_size,
33 size_t band,
34 size_t sample_index,
35 int offset) {
36 float value =
37 static_cast<int>(chunk_counter * chunk_size + sample_index) + offset;
38 return value > 0 ? 5000 * band + value : 0;
39 }
40
41 bool VerifySubFrame(size_t sub_frame_counter,
42 int offset,
43 const std::vector<rtc::ArrayView<float>>& sub_frame_view) {
44 for (size_t k = 0; k < sub_frame_view.size(); ++k) {
45 for (size_t i = 0; i < sub_frame_view[k].size(); ++i) {
46 const float reference_value =
47 ComputeSampleValue(sub_frame_counter, kSubFrameLength, k, i, offset);
48 if (reference_value != sub_frame_view[k][i]) {
49 return false;
50 }
51 }
52 }
53 return true;
54 }
55
56 void FillBlock(size_t block_counter, std::vector<std::vector<float>>* block) {
57 for (size_t k = 0; k < block->size(); ++k) {
58 for (size_t i = 0; i < (*block)[0].size(); ++i) {
59 (*block)[k][i] = ComputeSampleValue(block_counter, kBlockSize, k, i, 0);
60 }
61 }
62 }
63
64 // Verifies that the BlockFramer is able to produce the expected frame content.
65 void RunFramerTest(int sample_rate_hz) {
66 constexpr size_t kNumSubFramesToProcess = 2;
67 const size_t num_bands = NumBandsForRate(sample_rate_hz);
68
69 std::vector<std::vector<float>> block(num_bands,
70 std::vector<float>(kBlockSize, 0.f));
71 std::vector<std::vector<float>> output_sub_frame(
72 num_bands, std::vector<float>(kSubFrameLength, 0.f));
73 std::vector<rtc::ArrayView<float>> output_sub_frame_view(num_bands);
74 SetupSubFrameView(&output_sub_frame, &output_sub_frame_view);
75 BlockFramer framer(num_bands);
76
77 size_t block_index = 0;
78 for (size_t sub_frame_index = 0; sub_frame_index < kNumSubFramesToProcess;
79 ++sub_frame_index) {
80 FillBlock(block_index++, &block);
81 framer.InsertBlockAndExtractSubFrame(block, &output_sub_frame_view);
82 EXPECT_TRUE(VerifySubFrame(sub_frame_index, -64, output_sub_frame_view));
aleloi 2016/12/23 14:28:38 Perhaps the BlockFramer should start with 16 zeroe
peah-webrtc 2017/01/02 08:45:10 Afaics, that does not work. 16 zeros would produce
83
84 if ((sub_frame_index + 1) % 4 == 0) {
85 FillBlock(block_index++, &block);
86 framer.InsertBlock(block);
87 }
88 }
89 }
90
91 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
92 // Verifies that the BlockFramer crashes if the InsertBlockAndExtractSubFrame
93 // method is called for inputs with the wrong number of bands or band lengths.
94 void RunWronglySizedInsertAndExtractParametersTest(int sample_rate_hz,
95 size_t num_block_bands,
96 size_t block_length,
97 size_t num_sub_frame_bands,
98 size_t sub_frame_length) {
99 const size_t correct_num_bands = NumBandsForRate(sample_rate_hz);
100
101 std::vector<std::vector<float>> block(num_block_bands,
102 std::vector<float>(block_length, 0.f));
103 std::vector<std::vector<float>> output_sub_frame(
104 num_sub_frame_bands, std::vector<float>(sub_frame_length, 0.f));
105 std::vector<rtc::ArrayView<float>> output_sub_frame_view(
106 output_sub_frame.size());
107 SetupSubFrameView(&output_sub_frame, &output_sub_frame_view);
108 BlockFramer framer(correct_num_bands);
109 EXPECT_DEATH(
110 framer.InsertBlockAndExtractSubFrame(block, &output_sub_frame_view), "");
111 }
112
113 // Verifies that the BlockFramer crashes if the InsertBlock method is called for
114 // inputs with the wrong number of bands or band lengths.
115 void RunWronglySizedInsertParameterTest(int sample_rate_hz,
116 size_t num_block_bands,
117 size_t block_length) {
118 const size_t correct_num_bands = NumBandsForRate(sample_rate_hz);
119
120 std::vector<std::vector<float>> correct_block(
121 correct_num_bands, std::vector<float>(kBlockSize, 0.f));
122 std::vector<std::vector<float>> wrong_block(
123 num_block_bands, std::vector<float>(block_length, 0.f));
124 std::vector<std::vector<float>> output_sub_frame(
125 correct_num_bands, std::vector<float>(kSubFrameLength, 0.f));
126 std::vector<rtc::ArrayView<float>> output_sub_frame_view(
127 output_sub_frame.size());
128 SetupSubFrameView(&output_sub_frame, &output_sub_frame_view);
129 BlockFramer framer(correct_num_bands);
130 framer.InsertBlockAndExtractSubFrame(correct_block, &output_sub_frame_view);
131 framer.InsertBlockAndExtractSubFrame(correct_block, &output_sub_frame_view);
132 framer.InsertBlockAndExtractSubFrame(correct_block, &output_sub_frame_view);
133 framer.InsertBlockAndExtractSubFrame(correct_block, &output_sub_frame_view);
134
135 EXPECT_DEATH(framer.InsertBlock(wrong_block), "");
136 }
137
138 // Verifies that the BlockFramer crashes if the InsertBlock method is called
139 // after a wrong number of previous InsertBlockAndExtractSubFrame method calls
140 // have been made.
141 void RunWronglyInsertOrderTest(int sample_rate_hz,
142 size_t num_preceeding_api_calls) {
143 const size_t correct_num_bands = NumBandsForRate(sample_rate_hz);
144
145 std::vector<std::vector<float>> block(correct_num_bands,
146 std::vector<float>(kBlockSize, 0.f));
147 std::vector<std::vector<float>> output_sub_frame(
148 correct_num_bands, std::vector<float>(kSubFrameLength, 0.f));
149 std::vector<rtc::ArrayView<float>> output_sub_frame_view(
150 output_sub_frame.size());
151 SetupSubFrameView(&output_sub_frame, &output_sub_frame_view);
152 BlockFramer framer(correct_num_bands);
153 for (size_t k = 0; k < num_preceeding_api_calls; ++k) {
154 framer.InsertBlockAndExtractSubFrame(block, &output_sub_frame_view);
155 }
156
157 EXPECT_DEATH(framer.InsertBlock(block), "");
158 }
159 #endif
160
161 } // namespace
162
163 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
164 TEST(BlockFramer, WrongNumberOfBandsInBlockForInsertBlockAndExtractSubFrame) {
165 for (auto rate : {8000, 16000, 32000, 48000}) {
166 std::ostringstream ss;
hlundin-webrtc 2016/12/23 13:38:12 Break out the generation of the string into a sepa
peah-webrtc 2017/01/02 08:45:10 Done.
167 ss << "Sample rate: " << rate;
168 SCOPED_TRACE(ss.str());
169
170 const size_t correct_num_bands = NumBandsForRate(rate);
171 const size_t wrong_num_bands = (correct_num_bands % 3) + 1;
172 RunWronglySizedInsertAndExtractParametersTest(
173 rate, wrong_num_bands, kBlockSize, correct_num_bands, kSubFrameLength);
174 }
175 }
176
177 TEST(BlockFramer,
178 WrongNumberOfBandsInSubFrameForInsertBlockAndExtractSubFrame) {
179 for (auto rate : {8000, 16000, 32000, 48000}) {
180 std::ostringstream ss;
181 ss << "Sample rate: " << rate;
182 SCOPED_TRACE(ss.str());
183
184 const size_t correct_num_bands = NumBandsForRate(rate);
185 const size_t wrong_num_bands = (correct_num_bands % 3) + 1;
186 RunWronglySizedInsertAndExtractParametersTest(
187 rate, correct_num_bands, kBlockSize, wrong_num_bands, kSubFrameLength);
188 }
189 }
190
191 TEST(BlockFramer, WrongNumberOfSamplesInBlockForInsertBlockAndExtractSubFrame) {
192 for (auto rate : {8000, 16000, 32000, 48000}) {
193 std::ostringstream ss;
194 ss << "Sample rate: " << rate;
195 SCOPED_TRACE(ss.str());
196
197 const size_t correct_num_bands = NumBandsForRate(rate);
198 RunWronglySizedInsertAndExtractParametersTest(
199 rate, correct_num_bands, kBlockSize - 1, correct_num_bands,
200 kSubFrameLength);
201 }
202 }
203
204 TEST(BlockFramer,
205 WrongNumberOfSamplesInSubFrameForInsertBlockAndExtractSubFrame) {
206 for (auto rate : {8000, 16000, 32000, 48000}) {
207 std::ostringstream ss;
208 ss << "Sample rate: " << rate;
209 SCOPED_TRACE(ss.str());
210
211 const size_t correct_num_bands = NumBandsForRate(rate);
212 RunWronglySizedInsertAndExtractParametersTest(rate, correct_num_bands,
213 kBlockSize, correct_num_bands,
214 kSubFrameLength - 1);
215 }
216 }
217
218 TEST(BlockFramer, WrongNumberOfBandsInBlockForInsertBlock) {
219 for (auto rate : {8000, 16000, 32000, 48000}) {
220 std::ostringstream ss;
221 ss << "Sample rate: " << rate;
222 SCOPED_TRACE(ss.str());
223
224 const size_t correct_num_bands = NumBandsForRate(rate);
225 const size_t wrong_num_bands = (correct_num_bands % 3) + 1;
226 RunWronglySizedInsertParameterTest(rate, wrong_num_bands, kBlockSize);
227 }
228 }
229
230 TEST(BlockFramer, WrongNumberOfSamplesInBlockForInsertBlock) {
231 for (auto rate : {8000, 16000, 32000, 48000}) {
232 std::ostringstream ss;
233 ss << "Sample rate: " << rate;
234 SCOPED_TRACE(ss.str());
235
236 const size_t correct_num_bands = NumBandsForRate(rate);
237 RunWronglySizedInsertParameterTest(rate, correct_num_bands, kBlockSize - 1);
238 }
239 }
240
241 TEST(BlockFramer, WrongNumberOfPreceedingApiCallsForInsertBlock) {
242 for (auto rate : {8000, 16000, 32000, 48000}) {
243 for (size_t num_calls = 0; num_calls < 4; ++num_calls) {
244 std::ostringstream ss;
245 ss << "Sample rate: " << rate;
246 ss << ", Num preceeding InsertBlockAndExtractSubFrame calls: "
247 << num_calls;
248
249 SCOPED_TRACE(ss.str());
250 RunWronglyInsertOrderTest(rate, num_calls);
251 }
252 }
253 }
254 #endif
255
256 TEST(BlockFramer, FrameBitexactness) {
257 for (auto rate : {8000, 16000, 32000, 48000}) {
258 std::ostringstream ss;
259 ss << "Sample rate: " << rate;
260 SCOPED_TRACE(ss.str());
261 RunFramerTest(rate);
262 }
263 }
264
265 } // namespace webrtc
OLDNEW

Powered by Google App Engine