blob: 6791745eaf17866c78c209ecbce4079e036b9fad [file] [log] [blame]
Karl Wiberg22242942015-07-03 04:04:33 +02001/*
2 * Copyright (c) 2015 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 <algorithm>
12#include <numeric>
Karl Wiberg22242942015-07-03 04:04:33 +020013#include <vector>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/audio_coding/codecs/isac/fix/include/audio_encoder_isacfix.h"
16#include "modules/audio_coding/codecs/isac/main/include/audio_encoder_isac.h"
17#include "modules/audio_coding/neteq/tools/input_audio_file.h"
18#include "rtc_base/buffer.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010019#include "rtc_base/numerics/safe_conversions.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020020#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "test/testsupport/file_utils.h"
Karl Wiberg22242942015-07-03 04:04:33 +020023
24namespace webrtc {
25
26namespace {
27
kwiberg3258db22015-07-14 18:54:36 -070028const int kIsacNumberOfSamples = 32 * 60; // 60 ms at 32 kHz
29
Karl Wiberg22242942015-07-03 04:04:33 +020030std::vector<int16_t> LoadSpeechData() {
31 webrtc::test::InputAudioFile input_file(
32 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"));
Karl Wiberg22242942015-07-03 04:04:33 +020033 std::vector<int16_t> speech_data(kIsacNumberOfSamples);
34 input_file.Read(kIsacNumberOfSamples, speech_data.data());
35 return speech_data;
36}
37
38template <typename T>
39IsacBandwidthInfo GetBwInfo(typename T::instance_type* inst) {
40 IsacBandwidthInfo bi;
41 T::GetBandwidthInfo(inst, &bi);
42 EXPECT_TRUE(bi.in_use);
43 return bi;
44}
45
kwiberg3258db22015-07-14 18:54:36 -070046// Encodes one packet. Returns the packet duration in milliseconds.
Karl Wiberg22242942015-07-03 04:04:33 +020047template <typename T>
kwiberg3258db22015-07-14 18:54:36 -070048int EncodePacket(typename T::instance_type* inst,
49 const IsacBandwidthInfo* bi,
50 const int16_t* speech_data,
51 rtc::Buffer* output) {
52 output->SetSize(1000);
53 for (int duration_ms = 10;; duration_ms += 10) {
Karl Wiberg22242942015-07-03 04:04:33 +020054 if (bi)
55 T::SetBandwidthInfo(inst, bi);
kwiberg3258db22015-07-14 18:54:36 -070056 int encoded_bytes = T::Encode(inst, speech_data, output->data());
57 if (encoded_bytes > 0 || duration_ms >= 60) {
Karl Wiberg22242942015-07-03 04:04:33 +020058 EXPECT_GT(encoded_bytes, 0);
kwiberg3258db22015-07-14 18:54:36 -070059 EXPECT_LE(static_cast<size_t>(encoded_bytes), output->size());
60 output->SetSize(encoded_bytes);
61 return duration_ms;
Karl Wiberg22242942015-07-03 04:04:33 +020062 }
Karl Wiberg22242942015-07-03 04:04:33 +020063 }
64}
65
kwiberg3258db22015-07-14 18:54:36 -070066template <typename T>
67std::vector<int16_t> DecodePacket(typename T::instance_type* inst,
68 const rtc::Buffer& encoded) {
69 std::vector<int16_t> decoded(kIsacNumberOfSamples);
70 int16_t speech_type;
71 int nsamples = T::DecodeInternal(inst, encoded.data(), encoded.size(),
72 &decoded.front(), &speech_type);
73 EXPECT_GT(nsamples, 0);
74 EXPECT_LE(static_cast<size_t>(nsamples), decoded.size());
75 decoded.resize(nsamples);
76 return decoded;
77}
78
Karl Wiberg22242942015-07-03 04:04:33 +020079class BoundedCapacityChannel final {
80 public:
kwiberg3258db22015-07-14 18:54:36 -070081 BoundedCapacityChannel(int sample_rate_hz, int rate_bits_per_second)
Karl Wiberg22242942015-07-03 04:04:33 +020082 : current_time_rtp_(0),
83 channel_rate_bytes_per_sample_(rate_bits_per_second /
kwiberg3258db22015-07-14 18:54:36 -070084 (8.0 * sample_rate_hz)) {}
Karl Wiberg22242942015-07-03 04:04:33 +020085
86 // Simulate sending the given number of bytes at the given RTP time. Returns
87 // the new current RTP time after the sending is done.
88 int Send(int send_time_rtp, int nbytes) {
89 current_time_rtp_ = std::max(current_time_rtp_, send_time_rtp) +
90 nbytes / channel_rate_bytes_per_sample_;
91 return current_time_rtp_;
92 }
93
94 private:
95 int current_time_rtp_;
96 // The somewhat strange unit for channel rate, bytes per sample, is because
97 // RTP time is measured in samples:
98 const double channel_rate_bytes_per_sample_;
Karl Wiberg22242942015-07-03 04:04:33 +020099};
100
101// Test that the iSAC encoder produces identical output whether or not we use a
102// conjoined encoder+decoder pair or a separate encoder and decoder that
103// communicate BW estimation info explicitly.
104template <typename T, bool adaptive>
105void TestGetSetBandwidthInfo(const int16_t* speech_data,
kwiberg3258db22015-07-14 18:54:36 -0700106 int rate_bits_per_second,
107 int sample_rate_hz,
108 int frame_size_ms) {
109 const int bit_rate = 32000;
Karl Wiberg22242942015-07-03 04:04:33 +0200110
111 // Conjoined encoder/decoder pair:
112 typename T::instance_type* encdec;
113 ASSERT_EQ(0, T::Create(&encdec));
114 ASSERT_EQ(0, T::EncoderInit(encdec, adaptive ? 0 : 1));
Karl Wiberg43766482015-08-27 15:22:11 +0200115 T::DecoderInit(encdec);
kwiberg3258db22015-07-14 18:54:36 -0700116 ASSERT_EQ(0, T::SetEncSampRate(encdec, sample_rate_hz));
117 if (adaptive)
118 ASSERT_EQ(0, T::ControlBwe(encdec, bit_rate, frame_size_ms, false));
119 else
120 ASSERT_EQ(0, T::Control(encdec, bit_rate, frame_size_ms));
Karl Wiberg22242942015-07-03 04:04:33 +0200121
122 // Disjoint encoder/decoder pair:
123 typename T::instance_type* enc;
124 ASSERT_EQ(0, T::Create(&enc));
125 ASSERT_EQ(0, T::EncoderInit(enc, adaptive ? 0 : 1));
kwiberg3258db22015-07-14 18:54:36 -0700126 ASSERT_EQ(0, T::SetEncSampRate(enc, sample_rate_hz));
127 if (adaptive)
128 ASSERT_EQ(0, T::ControlBwe(enc, bit_rate, frame_size_ms, false));
129 else
130 ASSERT_EQ(0, T::Control(enc, bit_rate, frame_size_ms));
Karl Wiberg22242942015-07-03 04:04:33 +0200131 typename T::instance_type* dec;
132 ASSERT_EQ(0, T::Create(&dec));
Karl Wiberg43766482015-08-27 15:22:11 +0200133 T::DecoderInit(dec);
kwiberg3258db22015-07-14 18:54:36 -0700134 T::SetInitialBweBottleneck(dec, bit_rate);
135 T::SetEncSampRateInDecoder(dec, sample_rate_hz);
Karl Wiberg22242942015-07-03 04:04:33 +0200136
137 // 0. Get initial BW info from decoder.
138 auto bi = GetBwInfo<T>(dec);
139
kwiberg3258db22015-07-14 18:54:36 -0700140 BoundedCapacityChannel channel1(sample_rate_hz, rate_bits_per_second),
141 channel2(sample_rate_hz, rate_bits_per_second);
142
143 int elapsed_time_ms = 0;
144 for (int i = 0; elapsed_time_ms < 10000; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200145 rtc::StringBuilder ss;
Karl Wiberg22242942015-07-03 04:04:33 +0200146 ss << " i = " << i;
147 SCOPED_TRACE(ss.str());
148
kwiberg3258db22015-07-14 18:54:36 -0700149 // 1. Encode 3 * 10 ms or 6 * 10 ms. The separate encoder is given the BW
150 // info before each encode call.
151 rtc::Buffer bitstream1, bitstream2;
152 int duration1_ms =
153 EncodePacket<T>(encdec, nullptr, speech_data, &bitstream1);
154 int duration2_ms = EncodePacket<T>(enc, &bi, speech_data, &bitstream2);
155 EXPECT_EQ(duration1_ms, duration2_ms);
156 if (adaptive)
157 EXPECT_TRUE(duration1_ms == 30 || duration1_ms == 60);
158 else
159 EXPECT_EQ(frame_size_ms, duration1_ms);
160 ASSERT_EQ(bitstream1.size(), bitstream2.size());
Karl Wiberg22242942015-07-03 04:04:33 +0200161 EXPECT_EQ(bitstream1, bitstream2);
Karl Wiberg22242942015-07-03 04:04:33 +0200162
kwiberg3258db22015-07-14 18:54:36 -0700163 // 2. Deliver the encoded data to the decoders.
164 const int send_time = elapsed_time_ms * (sample_rate_hz / 1000);
Karl Wiberg22242942015-07-03 04:04:33 +0200165 EXPECT_EQ(0, T::UpdateBwEstimate(
166 encdec, bitstream1.data(), bitstream1.size(), i, send_time,
Mirko Bonadei737e0732017-10-19 09:00:17 +0200167 channel1.Send(send_time,
168 rtc::checked_cast<int>(bitstream1.size()))));
Karl Wiberg22242942015-07-03 04:04:33 +0200169 EXPECT_EQ(0, T::UpdateBwEstimate(
170 dec, bitstream2.data(), bitstream2.size(), i, send_time,
Mirko Bonadei737e0732017-10-19 09:00:17 +0200171 channel2.Send(send_time,
172 rtc::checked_cast<int>(bitstream2.size()))));
kwiberg3258db22015-07-14 18:54:36 -0700173
174 // 3. Decode, and get new BW info from the separate decoder.
175 ASSERT_EQ(0, T::SetDecSampRate(encdec, sample_rate_hz));
176 ASSERT_EQ(0, T::SetDecSampRate(dec, sample_rate_hz));
177 auto decoded1 = DecodePacket<T>(encdec, bitstream1);
178 auto decoded2 = DecodePacket<T>(dec, bitstream2);
179 EXPECT_EQ(decoded1, decoded2);
Karl Wiberg22242942015-07-03 04:04:33 +0200180 bi = GetBwInfo<T>(dec);
kwiberg3258db22015-07-14 18:54:36 -0700181
182 elapsed_time_ms += duration1_ms;
Karl Wiberg22242942015-07-03 04:04:33 +0200183 }
184
185 EXPECT_EQ(0, T::Free(encdec));
186 EXPECT_EQ(0, T::Free(enc));
187 EXPECT_EQ(0, T::Free(dec));
Karl Wiberg22242942015-07-03 04:04:33 +0200188}
189
kwiberg3258db22015-07-14 18:54:36 -0700190enum class IsacType { Fix, Float };
191
192std::ostream& operator<<(std::ostream& os, IsacType t) {
193 os << (t == IsacType::Fix ? "fix" : "float");
194 return os;
195}
196
197struct IsacTestParam {
198 IsacType isac_type;
199 bool adaptive;
200 int channel_rate_bits_per_second;
201 int sample_rate_hz;
202 int frame_size_ms;
203
204 friend std::ostream& operator<<(std::ostream& os, const IsacTestParam& itp) {
205 os << '{' << itp.isac_type << ','
206 << (itp.adaptive ? "adaptive" : "nonadaptive") << ','
207 << itp.channel_rate_bits_per_second << ',' << itp.sample_rate_hz << ','
208 << itp.frame_size_ms << '}';
209 return os;
210 }
211};
212
213class IsacCommonTest : public testing::TestWithParam<IsacTestParam> {};
214
Karl Wiberg22242942015-07-03 04:04:33 +0200215} // namespace
216
kwiberg3258db22015-07-14 18:54:36 -0700217TEST_P(IsacCommonTest, GetSetBandwidthInfo) {
218 auto p = GetParam();
219 auto test_fun = [p] {
220 if (p.isac_type == IsacType::Fix) {
221 if (p.adaptive)
222 return TestGetSetBandwidthInfo<IsacFix, true>;
223 else
224 return TestGetSetBandwidthInfo<IsacFix, false>;
225 } else {
226 if (p.adaptive)
227 return TestGetSetBandwidthInfo<IsacFloat, true>;
228 else
229 return TestGetSetBandwidthInfo<IsacFloat, false>;
230 }
231 }();
232 test_fun(LoadSpeechData().data(), p.channel_rate_bits_per_second,
233 p.sample_rate_hz, p.frame_size_ms);
Karl Wiberg22242942015-07-03 04:04:33 +0200234}
235
kwiberg3258db22015-07-14 18:54:36 -0700236std::vector<IsacTestParam> TestCases() {
237 static const IsacType types[] = {IsacType::Fix, IsacType::Float};
238 static const bool adaptives[] = {true, false};
239 static const int channel_rates[] = {12000, 15000, 19000, 22000};
240 static const int sample_rates[] = {16000, 32000};
241 static const int frame_sizes[] = {30, 60};
242 std::vector<IsacTestParam> cases;
243 for (IsacType type : types)
244 for (bool adaptive : adaptives)
245 for (int channel_rate : channel_rates)
246 for (int sample_rate : sample_rates)
247 if (!(type == IsacType::Fix && sample_rate == 32000))
248 for (int frame_size : frame_sizes)
249 if (!(sample_rate == 32000 && frame_size == 60))
250 cases.push_back(
251 {type, adaptive, channel_rate, sample_rate, frame_size});
252 return cases;
Karl Wiberg22242942015-07-03 04:04:33 +0200253}
254
kwiberg3258db22015-07-14 18:54:36 -0700255INSTANTIATE_TEST_CASE_P(, IsacCommonTest, testing::ValuesIn(TestCases()));
Karl Wiberg22242942015-07-03 04:04:33 +0200256
257} // namespace webrtc