Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 1 | /* |
| 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 Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #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 Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 19 | #include "rtc_base/numerics/safe_conversions.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 20 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "test/gtest.h" |
| 22 | #include "test/testsupport/fileutils.h" |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
| 26 | namespace { |
| 27 | |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 28 | const int kIsacNumberOfSamples = 32 * 60; // 60 ms at 32 kHz |
| 29 | |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 30 | std::vector<int16_t> LoadSpeechData() { |
| 31 | webrtc::test::InputAudioFile input_file( |
| 32 | webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm")); |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 33 | std::vector<int16_t> speech_data(kIsacNumberOfSamples); |
| 34 | input_file.Read(kIsacNumberOfSamples, speech_data.data()); |
| 35 | return speech_data; |
| 36 | } |
| 37 | |
| 38 | template <typename T> |
| 39 | IsacBandwidthInfo 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 | |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 46 | // Encodes one packet. Returns the packet duration in milliseconds. |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 47 | template <typename T> |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 48 | int 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 Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 54 | if (bi) |
| 55 | T::SetBandwidthInfo(inst, bi); |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 56 | int encoded_bytes = T::Encode(inst, speech_data, output->data()); |
| 57 | if (encoded_bytes > 0 || duration_ms >= 60) { |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 58 | EXPECT_GT(encoded_bytes, 0); |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 59 | EXPECT_LE(static_cast<size_t>(encoded_bytes), output->size()); |
| 60 | output->SetSize(encoded_bytes); |
| 61 | return duration_ms; |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 62 | } |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 66 | template <typename T> |
| 67 | std::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 Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 79 | class BoundedCapacityChannel final { |
| 80 | public: |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 81 | BoundedCapacityChannel(int sample_rate_hz, int rate_bits_per_second) |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 82 | : current_time_rtp_(0), |
| 83 | channel_rate_bytes_per_sample_(rate_bits_per_second / |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 84 | (8.0 * sample_rate_hz)) {} |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 85 | |
| 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 Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 99 | }; |
| 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. |
| 104 | template <typename T, bool adaptive> |
| 105 | void TestGetSetBandwidthInfo(const int16_t* speech_data, |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 106 | int rate_bits_per_second, |
| 107 | int sample_rate_hz, |
| 108 | int frame_size_ms) { |
| 109 | const int bit_rate = 32000; |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 110 | |
| 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 Wiberg | 4376648 | 2015-08-27 15:22:11 +0200 | [diff] [blame] | 115 | T::DecoderInit(encdec); |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 116 | 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 Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 121 | |
| 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)); |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 126 | 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 Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 131 | typename T::instance_type* dec; |
| 132 | ASSERT_EQ(0, T::Create(&dec)); |
Karl Wiberg | 4376648 | 2015-08-27 15:22:11 +0200 | [diff] [blame] | 133 | T::DecoderInit(dec); |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 134 | T::SetInitialBweBottleneck(dec, bit_rate); |
| 135 | T::SetEncSampRateInDecoder(dec, sample_rate_hz); |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 136 | |
| 137 | // 0. Get initial BW info from decoder. |
| 138 | auto bi = GetBwInfo<T>(dec); |
| 139 | |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 140 | 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 Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 145 | rtc::StringBuilder ss; |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 146 | ss << " i = " << i; |
| 147 | SCOPED_TRACE(ss.str()); |
| 148 | |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 149 | // 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 Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 161 | EXPECT_EQ(bitstream1, bitstream2); |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 162 | |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 163 | // 2. Deliver the encoded data to the decoders. |
| 164 | const int send_time = elapsed_time_ms * (sample_rate_hz / 1000); |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 165 | EXPECT_EQ(0, T::UpdateBwEstimate( |
| 166 | encdec, bitstream1.data(), bitstream1.size(), i, send_time, |
Mirko Bonadei | 737e073 | 2017-10-19 09:00:17 +0200 | [diff] [blame] | 167 | channel1.Send(send_time, |
| 168 | rtc::checked_cast<int>(bitstream1.size())))); |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 169 | EXPECT_EQ(0, T::UpdateBwEstimate( |
| 170 | dec, bitstream2.data(), bitstream2.size(), i, send_time, |
Mirko Bonadei | 737e073 | 2017-10-19 09:00:17 +0200 | [diff] [blame] | 171 | channel2.Send(send_time, |
| 172 | rtc::checked_cast<int>(bitstream2.size())))); |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 173 | |
| 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 Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 180 | bi = GetBwInfo<T>(dec); |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 181 | |
| 182 | elapsed_time_ms += duration1_ms; |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | EXPECT_EQ(0, T::Free(encdec)); |
| 186 | EXPECT_EQ(0, T::Free(enc)); |
| 187 | EXPECT_EQ(0, T::Free(dec)); |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 188 | } |
| 189 | |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 190 | enum class IsacType { Fix, Float }; |
| 191 | |
| 192 | std::ostream& operator<<(std::ostream& os, IsacType t) { |
| 193 | os << (t == IsacType::Fix ? "fix" : "float"); |
| 194 | return os; |
| 195 | } |
| 196 | |
| 197 | struct 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 | |
| 213 | class IsacCommonTest : public testing::TestWithParam<IsacTestParam> {}; |
| 214 | |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 215 | } // namespace |
| 216 | |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 217 | TEST_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 Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 234 | } |
| 235 | |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 236 | std::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 Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 253 | } |
| 254 | |
kwiberg | 3258db2 | 2015-07-14 18:54:36 -0700 | [diff] [blame] | 255 | INSTANTIATE_TEST_CASE_P(, IsacCommonTest, testing::ValuesIn(TestCases())); |
Karl Wiberg | 2224294 | 2015-07-03 04:04:33 +0200 | [diff] [blame] | 256 | |
| 257 | } // namespace webrtc |