OLD | NEW |
---|---|
(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 <string> | |
12 #include <vector> | |
13 | |
14 #include "webrtc/modules/audio_processing/aec3/aec3_constants.h" | |
15 #include "webrtc/modules/audio_processing/aec3/block_framer.h" | |
16 #include "webrtc/modules/audio_processing/aec3/frame_blocker.h" | |
hlundin-webrtc
2016/12/22 10:23:57
To the top.
peah-webrtc
2016/12/22 16:40:04
Done.
| |
17 #include "webrtc/test/gtest.h" | |
18 | |
19 namespace webrtc { | |
20 namespace { | |
21 | |
22 float ComputeSampleValue(size_t chunk_counter, | |
23 size_t chunk_size, | |
24 size_t band, | |
25 size_t sample_index, | |
26 int offset) { | |
27 float value = | |
28 static_cast<int>(chunk_counter * chunk_size + sample_index) + offset; | |
29 return value > 0 ? 5000 * band + value : 0; | |
30 } | |
31 | |
32 void FillSubFrame(size_t sub_frame_counter, | |
33 int offset, | |
34 std::vector<std::vector<float>>* sub_frame) { | |
35 for (size_t k = 0; k < sub_frame->size(); ++k) { | |
36 for (size_t i = 0; i < (*sub_frame)[0].size(); ++i) { | |
37 (*sub_frame)[k][i] = | |
38 ComputeSampleValue(sub_frame_counter, kSubFrameLength, k, i, offset); | |
39 } | |
40 } | |
41 } | |
42 | |
43 void FillSubFrameView(size_t sub_frame_counter, | |
44 int offset, | |
45 std::vector<std::vector<float>>* sub_frame, | |
46 std::vector<rtc::ArrayView<float>>* sub_frame_view) { | |
47 FillSubFrame(sub_frame_counter, offset, sub_frame); | |
48 for (size_t k = 0; k < sub_frame_view->size(); ++k) { | |
49 (*sub_frame_view)[k] = | |
50 rtc::ArrayView<float>(&(*sub_frame)[k][0], (*sub_frame)[k].size()); | |
51 } | |
52 } | |
53 | |
54 bool VerifySubFrame(size_t sub_frame_counter, | |
55 int offset, | |
56 const std::vector<rtc::ArrayView<float>>& sub_frame_view) { | |
57 std::vector<std::vector<float>> reference_sub_frame( | |
58 sub_frame_view.size(), std::vector<float>(sub_frame_view[0].size(), 0.f)); | |
59 FillSubFrame(sub_frame_counter, offset, &reference_sub_frame); | |
60 for (size_t k = 0; k < sub_frame_view.size(); ++k) { | |
61 for (size_t i = 0; i < sub_frame_view[k].size(); ++i) { | |
62 if (reference_sub_frame[k][i] != sub_frame_view[k][i]) { | |
63 return false; | |
64 } | |
65 } | |
66 } | |
67 return true; | |
68 } | |
69 | |
70 bool VerifyBlock(size_t block_counter, | |
71 int offset, | |
72 const std::vector<std::vector<float>>& block) { | |
73 for (size_t k = 0; k < block.size(); ++k) { | |
74 for (size_t i = 0; i < block[k].size(); ++i) { | |
75 const float reference_value = | |
76 ComputeSampleValue(block_counter, kBlockSize, k, i, offset); | |
77 if (reference_value != block[k][i]) { | |
78 return false; | |
79 } | |
80 } | |
81 } | |
82 return true; | |
83 } | |
84 | |
85 // Verifies that the FrameBlocker properly forms blocks out ouf the frames. | |
hlundin-webrtc
2016/12/22 10:23:57
ouf?
peah-webrtc
2016/12/22 16:40:04
Done.
| |
86 void RunBlockerTest(int sample_rate_hz) { | |
87 const size_t kNumSubFramesToProcess = 20; | |
hlundin-webrtc
2016/12/22 10:23:57
constexpr
peah-webrtc
2016/12/22 16:40:04
Done.
| |
88 const size_t num_bands = NumBandsForRate(sample_rate_hz); | |
89 | |
90 std::vector<std::vector<float>> block(num_bands, | |
91 std::vector<float>(kBlockSize, 0.f)); | |
92 std::vector<std::vector<float>> input_sub_frame( | |
93 num_bands, std::vector<float>(kSubFrameLength, 0.f)); | |
94 std::vector<rtc::ArrayView<float>> input_sub_frame_view(num_bands); | |
95 FrameBlocker blocker(num_bands); | |
96 | |
97 size_t block_counter = 0; | |
98 for (size_t sub_frame_index = 0; sub_frame_index < kNumSubFramesToProcess; | |
99 ++sub_frame_index) { | |
100 FillSubFrameView(sub_frame_index, 0, &input_sub_frame, | |
101 &input_sub_frame_view); | |
102 | |
103 blocker.InsertSubFrameAndExtractBlock(input_sub_frame_view, &block); | |
104 VerifyBlock(block_counter++, 0, block); | |
105 | |
106 if ((sub_frame_index + 1) % 4 == 0) { | |
107 EXPECT_TRUE(blocker.IsBlockAvailable()); | |
108 } else { | |
109 EXPECT_FALSE(blocker.IsBlockAvailable()); | |
110 } | |
111 if (blocker.IsBlockAvailable()) { | |
112 blocker.ExtractBlock(&block); | |
113 VerifyBlock(block_counter++, 0, block); | |
114 } | |
115 } | |
116 } | |
117 | |
118 // Verifies that the FrameBlocker and BlockFramer work well together and produce | |
119 // the expected output. | |
120 void RunBlockerAndFramerTest(int sample_rate_hz) { | |
121 const size_t kNumSubFramesToProcess = 20; | |
122 const size_t num_bands = NumBandsForRate(sample_rate_hz); | |
123 | |
124 std::vector<std::vector<float>> block(num_bands, | |
125 std::vector<float>(kBlockSize, 0.f)); | |
126 std::vector<std::vector<float>> input_sub_frame( | |
127 num_bands, std::vector<float>(kSubFrameLength, 0.f)); | |
128 std::vector<std::vector<float>> output_sub_frame( | |
129 num_bands, std::vector<float>(kSubFrameLength, 0.f)); | |
130 std::vector<rtc::ArrayView<float>> output_sub_frame_view(num_bands); | |
131 std::vector<rtc::ArrayView<float>> input_sub_frame_view(num_bands); | |
132 FrameBlocker blocker(num_bands); | |
133 BlockFramer framer(num_bands); | |
134 | |
135 for (size_t sub_frame_index = 0; sub_frame_index < kNumSubFramesToProcess; | |
136 ++sub_frame_index) { | |
137 FillSubFrameView(sub_frame_index, 0, &input_sub_frame, | |
138 &input_sub_frame_view); | |
139 FillSubFrameView(sub_frame_index, 0, &output_sub_frame, | |
140 &output_sub_frame_view); | |
141 | |
142 blocker.InsertSubFrameAndExtractBlock(input_sub_frame_view, &block); | |
143 framer.InsertBlockAndExtractSubFrame(block, &output_sub_frame_view); | |
144 | |
145 if ((sub_frame_index + 1) % 4 == 0) { | |
146 EXPECT_TRUE(blocker.IsBlockAvailable()); | |
147 } else { | |
148 EXPECT_FALSE(blocker.IsBlockAvailable()); | |
149 } | |
150 if (blocker.IsBlockAvailable()) { | |
151 blocker.ExtractBlock(&block); | |
152 framer.InsertBlock(block); | |
153 } | |
154 EXPECT_TRUE(VerifySubFrame(sub_frame_index, -64, output_sub_frame_view)); | |
155 } | |
156 } | |
157 | |
158 } // namespace | |
159 | |
160 TEST(FrameBlocker, BlockBitexactness) { | |
161 for (auto rate : {8000, 16000, 32000, 48000}) { | |
162 std::ostringstream ss; | |
163 ss << "Sample rate: " << rate; | |
164 SCOPED_TRACE(ss.str()); | |
165 | |
166 RunBlockerTest(rate); | |
167 } | |
168 } | |
169 | |
170 TEST(FrameBlocker, BlockerAndFramer) { | |
171 for (auto rate : {8000, 16000, 32000, 48000}) { | |
172 std::ostringstream ss; | |
173 ss << "Sample rate: " << rate; | |
174 SCOPED_TRACE(ss.str()); | |
175 | |
176 RunBlockerAndFramerTest(rate); | |
177 } | |
178 } | |
179 | |
hlundin-webrtc
2016/12/22 10:23:57
Add some (death) tests to verify that things blow
peah-webrtc
2016/12/22 16:40:04
Done.
| |
180 } // namespace webrtc | |
OLD | NEW |