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

Side by Side Diff: webrtc/modules/audio_coding/codecs/isac/unittest.cc

Issue 1225093005: Split iSAC encoder/decoder: Test more cases (and make sure they work) (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: rebase Created 5 years, 5 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
« no previous file with comments | « webrtc/modules/audio_coding/codecs/isac/main/source/isac.c ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 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 10
11 #include <algorithm> 11 #include <algorithm>
12 #include <numeric> 12 #include <numeric>
13 #include <sstream> 13 #include <sstream>
14 #include <vector> 14 #include <vector>
15 15
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "webrtc/base/buffer.h" 17 #include "webrtc/base/buffer.h"
18 #include "webrtc/modules/audio_coding/codecs/isac/fix/interface/audio_encoder_is acfix.h" 18 #include "webrtc/modules/audio_coding/codecs/isac/fix/interface/audio_encoder_is acfix.h"
19 #include "webrtc/modules/audio_coding/codecs/isac/main/interface/audio_encoder_i sac.h" 19 #include "webrtc/modules/audio_coding/codecs/isac/main/interface/audio_encoder_i sac.h"
20 #include "webrtc/modules/audio_coding/neteq/tools/input_audio_file.h" 20 #include "webrtc/modules/audio_coding/neteq/tools/input_audio_file.h"
21 #include "webrtc/test/testsupport/fileutils.h" 21 #include "webrtc/test/testsupport/fileutils.h"
22 22
23 namespace webrtc { 23 namespace webrtc {
24 24
25 namespace { 25 namespace {
26 26
27 const int kIsacNumberOfSamples = 32 * 60; // 60 ms at 32 kHz
28
27 std::vector<int16_t> LoadSpeechData() { 29 std::vector<int16_t> LoadSpeechData() {
28 webrtc::test::InputAudioFile input_file( 30 webrtc::test::InputAudioFile input_file(
29 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm")); 31 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"));
30 static const int kIsacNumberOfSamples = 32 * 60; // 60 ms at 32 kHz
31 std::vector<int16_t> speech_data(kIsacNumberOfSamples); 32 std::vector<int16_t> speech_data(kIsacNumberOfSamples);
32 input_file.Read(kIsacNumberOfSamples, speech_data.data()); 33 input_file.Read(kIsacNumberOfSamples, speech_data.data());
33 return speech_data; 34 return speech_data;
34 } 35 }
35 36
36 template <typename T> 37 template <typename T>
37 IsacBandwidthInfo GetBwInfo(typename T::instance_type* inst) { 38 IsacBandwidthInfo GetBwInfo(typename T::instance_type* inst) {
38 IsacBandwidthInfo bi; 39 IsacBandwidthInfo bi;
39 T::GetBandwidthInfo(inst, &bi); 40 T::GetBandwidthInfo(inst, &bi);
40 EXPECT_TRUE(bi.in_use); 41 EXPECT_TRUE(bi.in_use);
41 return bi; 42 return bi;
42 } 43 }
43 44
45 // Encodes one packet. Returns the packet duration in milliseconds.
44 template <typename T> 46 template <typename T>
45 rtc::Buffer EncodePacket(typename T::instance_type* inst, 47 int EncodePacket(typename T::instance_type* inst,
46 const IsacBandwidthInfo* bi, 48 const IsacBandwidthInfo* bi,
47 const int16_t* speech_data, 49 const int16_t* speech_data,
48 int framesize_ms) { 50 rtc::Buffer* output) {
49 rtc::Buffer output(1000); 51 output->SetSize(1000);
50 for (int i = 0;; ++i) { 52 for (int duration_ms = 10;; duration_ms += 10) {
51 if (bi) 53 if (bi)
52 T::SetBandwidthInfo(inst, bi); 54 T::SetBandwidthInfo(inst, bi);
53 int encoded_bytes = T::Encode(inst, speech_data, output.data()); 55 int encoded_bytes = T::Encode(inst, speech_data, output->data());
54 if (i + 1 == framesize_ms / 10) { 56 if (encoded_bytes > 0 || duration_ms >= 60) {
55 EXPECT_GT(encoded_bytes, 0); 57 EXPECT_GT(encoded_bytes, 0);
56 EXPECT_LE(static_cast<size_t>(encoded_bytes), output.size()); 58 EXPECT_LE(static_cast<size_t>(encoded_bytes), output->size());
57 output.SetSize(encoded_bytes); 59 output->SetSize(encoded_bytes);
58 return output; 60 return duration_ms;
59 } 61 }
60 EXPECT_EQ(0, encoded_bytes);
61 } 62 }
62 } 63 }
63 64
65 template <typename T>
66 std::vector<int16_t> DecodePacket(typename T::instance_type* inst,
67 const rtc::Buffer& encoded) {
68 std::vector<int16_t> decoded(kIsacNumberOfSamples);
69 int16_t speech_type;
70 int nsamples = T::DecodeInternal(inst, encoded.data(), encoded.size(),
71 &decoded.front(), &speech_type);
72 EXPECT_GT(nsamples, 0);
73 EXPECT_LE(static_cast<size_t>(nsamples), decoded.size());
74 decoded.resize(nsamples);
75 return decoded;
76 }
77
64 class BoundedCapacityChannel final { 78 class BoundedCapacityChannel final {
65 public: 79 public:
66 BoundedCapacityChannel(int rate_bits_per_second) 80 BoundedCapacityChannel(int sample_rate_hz, int rate_bits_per_second)
67 : current_time_rtp_(0), 81 : current_time_rtp_(0),
68 channel_rate_bytes_per_sample_(rate_bits_per_second / 82 channel_rate_bytes_per_sample_(rate_bits_per_second /
69 (8.0 * kSamplesPerSecond)) {} 83 (8.0 * sample_rate_hz)) {}
70 84
71 // Simulate sending the given number of bytes at the given RTP time. Returns 85 // Simulate sending the given number of bytes at the given RTP time. Returns
72 // the new current RTP time after the sending is done. 86 // the new current RTP time after the sending is done.
73 int Send(int send_time_rtp, int nbytes) { 87 int Send(int send_time_rtp, int nbytes) {
74 current_time_rtp_ = std::max(current_time_rtp_, send_time_rtp) + 88 current_time_rtp_ = std::max(current_time_rtp_, send_time_rtp) +
75 nbytes / channel_rate_bytes_per_sample_; 89 nbytes / channel_rate_bytes_per_sample_;
76 return current_time_rtp_; 90 return current_time_rtp_;
77 } 91 }
78 92
79 private: 93 private:
80 int current_time_rtp_; 94 int current_time_rtp_;
81 // The somewhat strange unit for channel rate, bytes per sample, is because 95 // The somewhat strange unit for channel rate, bytes per sample, is because
82 // RTP time is measured in samples: 96 // RTP time is measured in samples:
83 const double channel_rate_bytes_per_sample_; 97 const double channel_rate_bytes_per_sample_;
84 static const int kSamplesPerSecond = 16000;
85 };
86
87 template <typename T, bool adaptive>
88 struct TestParam {};
89
90 template <>
91 struct TestParam<IsacFloat, true> {
92 static const int time_to_settle = 200;
93 static int ExpectedRateBitsPerSecond(int rate_bits_per_second) {
94 return rate_bits_per_second;
95 }
96 };
97
98 template <>
99 struct TestParam<IsacFix, true> {
100 static const int time_to_settle = 350;
101 static int ExpectedRateBitsPerSecond(int rate_bits_per_second) {
102 // For some reason, IsacFix fails to adapt to the channel's actual
103 // bandwidth. Instead, it settles on a few hundred packets at 10kbit/s,
104 // then a few hundred at 5kbit/s, then a few hundred at 10kbit/s, and so
105 // on. The 200 packets starting at 350 are in the middle of the first
106 // 10kbit/s run.
107 return 10000;
108 }
109 };
110
111 template <>
112 struct TestParam<IsacFloat, false> {
113 static const int time_to_settle = 0;
114 static int ExpectedRateBitsPerSecond(int rate_bits_per_second) {
115 return 32000;
116 }
117 };
118
119 template <>
120 struct TestParam<IsacFix, false> {
121 static const int time_to_settle = 0;
122 static int ExpectedRateBitsPerSecond(int rate_bits_per_second) {
123 return 16000;
124 }
125 }; 98 };
126 99
127 // Test that the iSAC encoder produces identical output whether or not we use a 100 // Test that the iSAC encoder produces identical output whether or not we use a
128 // conjoined encoder+decoder pair or a separate encoder and decoder that 101 // conjoined encoder+decoder pair or a separate encoder and decoder that
129 // communicate BW estimation info explicitly. 102 // communicate BW estimation info explicitly.
130 template <typename T, bool adaptive> 103 template <typename T, bool adaptive>
131 void TestGetSetBandwidthInfo(const int16_t* speech_data, 104 void TestGetSetBandwidthInfo(const int16_t* speech_data,
132 int rate_bits_per_second) { 105 int rate_bits_per_second,
133 using Param = TestParam<T, adaptive>; 106 int sample_rate_hz,
134 const int framesize_ms = adaptive ? 60 : 30; 107 int frame_size_ms) {
108 const int bit_rate = 32000;
135 109
136 // Conjoined encoder/decoder pair: 110 // Conjoined encoder/decoder pair:
137 typename T::instance_type* encdec; 111 typename T::instance_type* encdec;
138 ASSERT_EQ(0, T::Create(&encdec)); 112 ASSERT_EQ(0, T::Create(&encdec));
139 ASSERT_EQ(0, T::EncoderInit(encdec, adaptive ? 0 : 1)); 113 ASSERT_EQ(0, T::EncoderInit(encdec, adaptive ? 0 : 1));
140 ASSERT_EQ(0, T::DecoderInit(encdec)); 114 ASSERT_EQ(0, T::DecoderInit(encdec));
115 ASSERT_EQ(0, T::SetEncSampRate(encdec, sample_rate_hz));
116 if (adaptive)
117 ASSERT_EQ(0, T::ControlBwe(encdec, bit_rate, frame_size_ms, false));
118 else
119 ASSERT_EQ(0, T::Control(encdec, bit_rate, frame_size_ms));
141 120
142 // Disjoint encoder/decoder pair: 121 // Disjoint encoder/decoder pair:
143 typename T::instance_type* enc; 122 typename T::instance_type* enc;
144 ASSERT_EQ(0, T::Create(&enc)); 123 ASSERT_EQ(0, T::Create(&enc));
145 ASSERT_EQ(0, T::EncoderInit(enc, adaptive ? 0 : 1)); 124 ASSERT_EQ(0, T::EncoderInit(enc, adaptive ? 0 : 1));
125 ASSERT_EQ(0, T::SetEncSampRate(enc, sample_rate_hz));
126 if (adaptive)
127 ASSERT_EQ(0, T::ControlBwe(enc, bit_rate, frame_size_ms, false));
128 else
129 ASSERT_EQ(0, T::Control(enc, bit_rate, frame_size_ms));
146 typename T::instance_type* dec; 130 typename T::instance_type* dec;
147 ASSERT_EQ(0, T::Create(&dec)); 131 ASSERT_EQ(0, T::Create(&dec));
148 ASSERT_EQ(0, T::DecoderInit(dec)); 132 ASSERT_EQ(0, T::DecoderInit(dec));
133 T::SetInitialBweBottleneck(dec, bit_rate);
134 T::SetEncSampRateInDecoder(dec, sample_rate_hz);
149 135
150 // 0. Get initial BW info from decoder. 136 // 0. Get initial BW info from decoder.
151 auto bi = GetBwInfo<T>(dec); 137 auto bi = GetBwInfo<T>(dec);
152 138
153 BoundedCapacityChannel channel1(rate_bits_per_second), 139 BoundedCapacityChannel channel1(sample_rate_hz, rate_bits_per_second),
154 channel2(rate_bits_per_second); 140 channel2(sample_rate_hz, rate_bits_per_second);
155 std::vector<size_t> packet_sizes; 141
156 for (int i = 0; i < Param::time_to_settle + 200; ++i) { 142 int elapsed_time_ms = 0;
143 for (int i = 0; elapsed_time_ms < 10000; ++i) {
157 std::ostringstream ss; 144 std::ostringstream ss;
158 ss << " i = " << i; 145 ss << " i = " << i;
159 SCOPED_TRACE(ss.str()); 146 SCOPED_TRACE(ss.str());
160 147
161 // 1. Encode 6 * 10 ms (adaptive) or 3 * 10 ms (nonadaptive). The separate 148 // 1. Encode 3 * 10 ms or 6 * 10 ms. The separate encoder is given the BW
162 // encoder is given the BW info before each encode call. 149 // info before each encode call.
163 auto bitstream1 = 150 rtc::Buffer bitstream1, bitstream2;
164 EncodePacket<T>(encdec, nullptr, speech_data, framesize_ms); 151 int duration1_ms =
165 auto bitstream2 = EncodePacket<T>(enc, &bi, speech_data, framesize_ms); 152 EncodePacket<T>(encdec, nullptr, speech_data, &bitstream1);
153 int duration2_ms = EncodePacket<T>(enc, &bi, speech_data, &bitstream2);
154 EXPECT_EQ(duration1_ms, duration2_ms);
155 if (adaptive)
156 EXPECT_TRUE(duration1_ms == 30 || duration1_ms == 60);
157 else
158 EXPECT_EQ(frame_size_ms, duration1_ms);
159 ASSERT_EQ(bitstream1.size(), bitstream2.size());
166 EXPECT_EQ(bitstream1, bitstream2); 160 EXPECT_EQ(bitstream1, bitstream2);
167 if (i > Param::time_to_settle)
168 packet_sizes.push_back(bitstream1.size());
169 161
170 // 2. Deliver the encoded data to the decoders (but don't actually ask them 162 // 2. Deliver the encoded data to the decoders.
171 // to decode it; that's not necessary). Then get new BW info from the 163 const int send_time = elapsed_time_ms * (sample_rate_hz / 1000);
172 // separate decoder.
173 const int samples_per_packet = 16 * framesize_ms;
174 const int send_time = i * samples_per_packet;
175 EXPECT_EQ(0, T::UpdateBwEstimate( 164 EXPECT_EQ(0, T::UpdateBwEstimate(
176 encdec, bitstream1.data(), bitstream1.size(), i, send_time, 165 encdec, bitstream1.data(), bitstream1.size(), i, send_time,
177 channel1.Send(send_time, bitstream1.size()))); 166 channel1.Send(send_time, bitstream1.size())));
178 EXPECT_EQ(0, T::UpdateBwEstimate( 167 EXPECT_EQ(0, T::UpdateBwEstimate(
179 dec, bitstream2.data(), bitstream2.size(), i, send_time, 168 dec, bitstream2.data(), bitstream2.size(), i, send_time,
180 channel2.Send(send_time, bitstream2.size()))); 169 channel2.Send(send_time, bitstream2.size())));
170
171 // 3. Decode, and get new BW info from the separate decoder.
172 ASSERT_EQ(0, T::SetDecSampRate(encdec, sample_rate_hz));
173 ASSERT_EQ(0, T::SetDecSampRate(dec, sample_rate_hz));
174 auto decoded1 = DecodePacket<T>(encdec, bitstream1);
175 auto decoded2 = DecodePacket<T>(dec, bitstream2);
176 EXPECT_EQ(decoded1, decoded2);
181 bi = GetBwInfo<T>(dec); 177 bi = GetBwInfo<T>(dec);
178
179 elapsed_time_ms += duration1_ms;
182 } 180 }
183 181
184 EXPECT_EQ(0, T::Free(encdec)); 182 EXPECT_EQ(0, T::Free(encdec));
185 EXPECT_EQ(0, T::Free(enc)); 183 EXPECT_EQ(0, T::Free(enc));
186 EXPECT_EQ(0, T::Free(dec)); 184 EXPECT_EQ(0, T::Free(dec));
185 }
187 186
188 // The average send bitrate is close to the channel's capacity. 187 enum class IsacType { Fix, Float };
189 double avg_size =
190 std::accumulate(packet_sizes.begin(), packet_sizes.end(), 0) /
191 static_cast<double>(packet_sizes.size());
192 double avg_rate_bits_per_second = 8.0 * avg_size / (framesize_ms * 1e-3);
193 double expected_rate_bits_per_second =
194 Param::ExpectedRateBitsPerSecond(rate_bits_per_second);
195 EXPECT_GT(avg_rate_bits_per_second / expected_rate_bits_per_second, 0.95);
196 EXPECT_LT(avg_rate_bits_per_second / expected_rate_bits_per_second, 1.06);
197 188
198 // The largest packet isn't that large, and the smallest not that small. 189 std::ostream& operator<<(std::ostream& os, IsacType t) {
199 size_t min_size = *std::min_element(packet_sizes.begin(), packet_sizes.end()); 190 os << (t == IsacType::Fix ? "fix" : "float");
200 size_t max_size = *std::max_element(packet_sizes.begin(), packet_sizes.end()); 191 return os;
201 double size_range = max_size - min_size;
202 EXPECT_LE(size_range / avg_size, 0.16);
203 } 192 }
204 193
194 struct IsacTestParam {
195 IsacType isac_type;
196 bool adaptive;
197 int channel_rate_bits_per_second;
198 int sample_rate_hz;
199 int frame_size_ms;
200
201 friend std::ostream& operator<<(std::ostream& os, const IsacTestParam& itp) {
202 os << '{' << itp.isac_type << ','
203 << (itp.adaptive ? "adaptive" : "nonadaptive") << ','
204 << itp.channel_rate_bits_per_second << ',' << itp.sample_rate_hz << ','
205 << itp.frame_size_ms << '}';
206 return os;
207 }
208 };
209
210 class IsacCommonTest : public testing::TestWithParam<IsacTestParam> {};
211
205 } // namespace 212 } // namespace
206 213
207 TEST(IsacCommonTest, GetSetBandwidthInfoFloat12kAdaptive) { 214 TEST_P(IsacCommonTest, GetSetBandwidthInfo) {
208 TestGetSetBandwidthInfo<IsacFloat, true>(LoadSpeechData().data(), 12000); 215 auto p = GetParam();
216 auto test_fun = [p] {
217 if (p.isac_type == IsacType::Fix) {
218 if (p.adaptive)
219 return TestGetSetBandwidthInfo<IsacFix, true>;
220 else
221 return TestGetSetBandwidthInfo<IsacFix, false>;
222 } else {
223 if (p.adaptive)
224 return TestGetSetBandwidthInfo<IsacFloat, true>;
225 else
226 return TestGetSetBandwidthInfo<IsacFloat, false>;
227 }
228 }();
229 test_fun(LoadSpeechData().data(), p.channel_rate_bits_per_second,
230 p.sample_rate_hz, p.frame_size_ms);
209 } 231 }
210 232
211 TEST(IsacCommonTest, GetSetBandwidthInfoFloat15kAdaptive) { 233 std::vector<IsacTestParam> TestCases() {
212 TestGetSetBandwidthInfo<IsacFloat, true>(LoadSpeechData().data(), 15000); 234 static const IsacType types[] = {IsacType::Fix, IsacType::Float};
235 static const bool adaptives[] = {true, false};
236 static const int channel_rates[] = {12000, 15000, 19000, 22000};
237 static const int sample_rates[] = {16000, 32000};
238 static const int frame_sizes[] = {30, 60};
239 std::vector<IsacTestParam> cases;
240 for (IsacType type : types)
241 for (bool adaptive : adaptives)
242 for (int channel_rate : channel_rates)
243 for (int sample_rate : sample_rates)
244 if (!(type == IsacType::Fix && sample_rate == 32000))
245 for (int frame_size : frame_sizes)
246 if (!(sample_rate == 32000 && frame_size == 60))
247 cases.push_back(
248 {type, adaptive, channel_rate, sample_rate, frame_size});
249 return cases;
213 } 250 }
214 251
215 TEST(IsacCommonTest, GetSetBandwidthInfoFloat19kAdaptive) { 252 INSTANTIATE_TEST_CASE_P(, IsacCommonTest, testing::ValuesIn(TestCases()));
216 TestGetSetBandwidthInfo<IsacFloat, true>(LoadSpeechData().data(), 19000);
217 }
218
219 TEST(IsacCommonTest, GetSetBandwidthInfoFloat22kAdaptive) {
220 TestGetSetBandwidthInfo<IsacFloat, true>(LoadSpeechData().data(), 22000);
221 }
222
223 TEST(IsacCommonTest, GetSetBandwidthInfoFix12kAdaptive) {
224 TestGetSetBandwidthInfo<IsacFix, true>(LoadSpeechData().data(), 12000);
225 }
226
227 TEST(IsacCommonTest, GetSetBandwidthInfoFix15kAdaptive) {
228 TestGetSetBandwidthInfo<IsacFix, true>(LoadSpeechData().data(), 15000);
229 }
230
231 TEST(IsacCommonTest, GetSetBandwidthInfoFix19kAdaptive) {
232 TestGetSetBandwidthInfo<IsacFix, true>(LoadSpeechData().data(), 19000);
233 }
234
235 TEST(IsacCommonTest, GetSetBandwidthInfoFix22kAdaptive) {
236 TestGetSetBandwidthInfo<IsacFix, true>(LoadSpeechData().data(), 22000);
237 }
238
239 TEST(IsacCommonTest, GetSetBandwidthInfoFloat12k) {
240 TestGetSetBandwidthInfo<IsacFloat, false>(LoadSpeechData().data(), 12000);
241 }
242
243 TEST(IsacCommonTest, GetSetBandwidthInfoFloat15k) {
244 TestGetSetBandwidthInfo<IsacFloat, false>(LoadSpeechData().data(), 15000);
245 }
246
247 TEST(IsacCommonTest, GetSetBandwidthInfoFloat19k) {
248 TestGetSetBandwidthInfo<IsacFloat, false>(LoadSpeechData().data(), 19000);
249 }
250
251 TEST(IsacCommonTest, GetSetBandwidthInfoFloat22k) {
252 TestGetSetBandwidthInfo<IsacFloat, false>(LoadSpeechData().data(), 22000);
253 }
254
255 TEST(IsacCommonTest, GetSetBandwidthInfoFix12k) {
256 TestGetSetBandwidthInfo<IsacFix, false>(LoadSpeechData().data(), 12000);
257 }
258
259 TEST(IsacCommonTest, GetSetBandwidthInfoFix15k) {
260 TestGetSetBandwidthInfo<IsacFix, false>(LoadSpeechData().data(), 15000);
261 }
262
263 TEST(IsacCommonTest, GetSetBandwidthInfoFix19k) {
264 TestGetSetBandwidthInfo<IsacFix, false>(LoadSpeechData().data(), 19000);
265 }
266
267 TEST(IsacCommonTest, GetSetBandwidthInfoFix22k) {
268 TestGetSetBandwidthInfo<IsacFix, false>(LoadSpeechData().data(), 22000);
269 }
270 253
271 } // namespace webrtc 254 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_coding/codecs/isac/main/source/isac.c ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698