kjellander@webrtc.org | 64a897a | 2011-11-17 13:33:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | */ |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 10 | #include <memory> |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 11 | #include <string> |
kjellander@webrtc.org | 64a897a | 2011-11-17 13:33:11 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 13 | #include "modules/audio_coding/codecs/cng/webrtc_cng.h" |
| 14 | #include "test/gtest.h" |
| 15 | #include "test/testsupport/fileutils.h" |
kjellander@webrtc.org | 64a897a | 2011-11-17 13:33:11 +0000 | [diff] [blame] | 16 | |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 17 | namespace webrtc { |
| 18 | |
| 19 | enum { |
| 20 | kSidShortIntervalUpdate = 1, |
| 21 | kSidNormalIntervalUpdate = 100, |
| 22 | kSidLongIntervalUpdate = 10000 |
| 23 | }; |
| 24 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 25 | enum : size_t { |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 26 | kCNGNumParamsLow = 0, |
| 27 | kCNGNumParamsNormal = 8, |
| 28 | kCNGNumParamsHigh = WEBRTC_CNG_MAX_LPC_ORDER, |
| 29 | kCNGNumParamsTooHigh = WEBRTC_CNG_MAX_LPC_ORDER + 1 |
| 30 | }; |
| 31 | |
| 32 | enum { |
| 33 | kNoSid, |
| 34 | kForceSid |
| 35 | }; |
| 36 | |
| 37 | class CngTest : public ::testing::Test { |
| 38 | protected: |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 39 | virtual void SetUp(); |
| 40 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 41 | void TestCngEncode(int sample_rate_hz, int quality); |
| 42 | |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 43 | int16_t speech_data_[640]; // Max size of CNG internal buffers. |
| 44 | }; |
| 45 | |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 46 | void CngTest::SetUp() { |
| 47 | FILE* input_file; |
| 48 | const std::string file_name = |
| 49 | webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"); |
| 50 | input_file = fopen(file_name.c_str(), "rb"); |
| 51 | ASSERT_TRUE(input_file != NULL); |
tina.legrand@webrtc.org | 95c2364 | 2012-09-28 10:49:59 +0000 | [diff] [blame] | 52 | ASSERT_EQ(640, static_cast<int32_t>(fread(speech_data_, sizeof(int16_t), |
| 53 | 640, input_file))); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 54 | fclose(input_file); |
| 55 | input_file = NULL; |
| 56 | } |
| 57 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 58 | void CngTest::TestCngEncode(int sample_rate_hz, int quality) { |
| 59 | const size_t num_samples_10ms = rtc::CheckedDivExact(sample_rate_hz, 100); |
| 60 | rtc::Buffer sid_data; |
| 61 | |
| 62 | ComfortNoiseEncoder cng_encoder(sample_rate_hz, kSidNormalIntervalUpdate, |
| 63 | quality); |
| 64 | EXPECT_EQ(0U, cng_encoder.Encode(rtc::ArrayView<const int16_t>( |
| 65 | speech_data_, num_samples_10ms), |
| 66 | kNoSid, &sid_data)); |
| 67 | EXPECT_EQ(static_cast<size_t>(quality + 1), |
| 68 | cng_encoder.Encode( |
| 69 | rtc::ArrayView<const int16_t>(speech_data_, num_samples_10ms), |
| 70 | kForceSid, &sid_data)); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 71 | } |
| 72 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 73 | #if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 74 | // Create CNG encoder, init with faulty values, free CNG encoder. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 75 | TEST_F(CngTest, CngInitFail) { |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 76 | // Call with too few parameters. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 77 | EXPECT_DEATH({ ComfortNoiseEncoder(8000, kSidNormalIntervalUpdate, |
| 78 | kCNGNumParamsLow); }, ""); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 79 | // Call with too many parameters. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 80 | EXPECT_DEATH({ ComfortNoiseEncoder(8000, kSidNormalIntervalUpdate, |
| 81 | kCNGNumParamsTooHigh); }, ""); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Encode Cng with too long input vector. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 85 | TEST_F(CngTest, CngEncodeTooLong) { |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 86 | rtc::Buffer sid_data; |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 87 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 88 | // Create encoder. |
| 89 | ComfortNoiseEncoder cng_encoder(8000, kSidNormalIntervalUpdate, |
| 90 | kCNGNumParamsNormal); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 91 | // Run encoder with too much data. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 92 | EXPECT_DEATH( |
| 93 | cng_encoder.Encode(rtc::ArrayView<const int16_t>(speech_data_, 641), |
| 94 | kNoSid, &sid_data), |
| 95 | ""); |
| 96 | } |
| 97 | #endif // GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 98 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 99 | TEST_F(CngTest, CngEncode8000) { |
| 100 | TestCngEncode(8000, kCNGNumParamsNormal); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 101 | } |
| 102 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 103 | TEST_F(CngTest, CngEncode16000) { |
| 104 | TestCngEncode(16000, kCNGNumParamsNormal); |
| 105 | } |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 106 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 107 | TEST_F(CngTest, CngEncode32000) { |
| 108 | TestCngEncode(32000, kCNGNumParamsHigh); |
| 109 | } |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 110 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 111 | TEST_F(CngTest, CngEncode48000) { |
| 112 | TestCngEncode(48000, kCNGNumParamsNormal); |
| 113 | } |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 114 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 115 | TEST_F(CngTest, CngEncode64000) { |
| 116 | TestCngEncode(64000, kCNGNumParamsNormal); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Update SID parameters, for both 9 and 16 parameters. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 120 | TEST_F(CngTest, CngUpdateSid) { |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 121 | rtc::Buffer sid_data; |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 122 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 123 | // Create and initialize encoder and decoder. |
| 124 | ComfortNoiseEncoder cng_encoder(16000, kSidNormalIntervalUpdate, |
| 125 | kCNGNumParamsNormal); |
| 126 | ComfortNoiseDecoder cng_decoder; |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 127 | |
| 128 | // Run normal Encode and UpdateSid. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 129 | EXPECT_EQ(kCNGNumParamsNormal + 1, |
| 130 | cng_encoder.Encode(rtc::ArrayView<const int16_t>(speech_data_, 160), |
| 131 | kForceSid, &sid_data)); |
| 132 | cng_decoder.UpdateSid(sid_data); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 133 | |
| 134 | // Reinit with new length. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 135 | cng_encoder.Reset(16000, kSidNormalIntervalUpdate, kCNGNumParamsHigh); |
| 136 | cng_decoder.Reset(); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 137 | |
| 138 | // Expect 0 because of unstable parameters after switching length. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 139 | EXPECT_EQ(0U, |
| 140 | cng_encoder.Encode(rtc::ArrayView<const int16_t>(speech_data_, 160), |
| 141 | kForceSid, &sid_data)); |
| 142 | EXPECT_EQ( |
| 143 | kCNGNumParamsHigh + 1, |
| 144 | cng_encoder.Encode(rtc::ArrayView<const int16_t>(speech_data_ + 160, 160), |
| 145 | kForceSid, &sid_data)); |
| 146 | cng_decoder.UpdateSid( |
| 147 | rtc::ArrayView<const uint8_t>(sid_data.data(), kCNGNumParamsNormal + 1)); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Update SID parameters, with wrong parameters or without calling decode. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 151 | TEST_F(CngTest, CngUpdateSidErroneous) { |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 152 | rtc::Buffer sid_data; |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 153 | |
| 154 | // Encode. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 155 | ComfortNoiseEncoder cng_encoder(16000, kSidNormalIntervalUpdate, |
| 156 | kCNGNumParamsNormal); |
| 157 | ComfortNoiseDecoder cng_decoder; |
| 158 | EXPECT_EQ(kCNGNumParamsNormal + 1, |
| 159 | cng_encoder.Encode(rtc::ArrayView<const int16_t>(speech_data_, 160), |
| 160 | kForceSid, &sid_data)); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 161 | |
| 162 | // First run with valid parameters, then with too many CNG parameters. |
| 163 | // The function will operate correctly by only reading the maximum number of |
| 164 | // parameters, skipping the extra. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 165 | EXPECT_EQ(kCNGNumParamsNormal + 1, sid_data.size()); |
| 166 | cng_decoder.UpdateSid(sid_data); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 167 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 168 | // Make sure the input buffer is large enough. Since Encode() appends data, we |
| 169 | // need to set the size manually only afterwards, or the buffer will be bigger |
| 170 | // than anticipated. |
| 171 | sid_data.SetSize(kCNGNumParamsTooHigh + 1); |
| 172 | cng_decoder.UpdateSid(sid_data); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Test to generate cng data, by forcing SID. Both normal and faulty condition. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 176 | TEST_F(CngTest, CngGenerate) { |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 177 | rtc::Buffer sid_data; |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 178 | int16_t out_data[640]; |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 179 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 180 | // Create and initialize encoder and decoder. |
| 181 | ComfortNoiseEncoder cng_encoder(16000, kSidNormalIntervalUpdate, |
| 182 | kCNGNumParamsNormal); |
| 183 | ComfortNoiseDecoder cng_decoder; |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 184 | |
| 185 | // Normal Encode. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 186 | EXPECT_EQ(kCNGNumParamsNormal + 1, |
| 187 | cng_encoder.Encode(rtc::ArrayView<const int16_t>(speech_data_, 160), |
| 188 | kForceSid, &sid_data)); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 189 | |
| 190 | // Normal UpdateSid. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 191 | cng_decoder.UpdateSid(sid_data); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 192 | |
| 193 | // Two normal Generate, one with new_period. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 194 | EXPECT_TRUE(cng_decoder.Generate(rtc::ArrayView<int16_t>(out_data, 640), 1)); |
| 195 | EXPECT_TRUE(cng_decoder.Generate(rtc::ArrayView<int16_t>(out_data, 640), 0)); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 196 | |
| 197 | // Call Genereate with too much data. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 198 | EXPECT_FALSE(cng_decoder.Generate(rtc::ArrayView<int16_t>(out_data, 641), 0)); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // Test automatic SID. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 202 | TEST_F(CngTest, CngAutoSid) { |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 203 | rtc::Buffer sid_data; |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 204 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 205 | // Create and initialize encoder and decoder. |
| 206 | ComfortNoiseEncoder cng_encoder(16000, kSidNormalIntervalUpdate, |
| 207 | kCNGNumParamsNormal); |
| 208 | ComfortNoiseDecoder cng_decoder; |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 209 | |
| 210 | // Normal Encode, 100 msec, where no SID data should be generated. |
| 211 | for (int i = 0; i < 10; i++) { |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 212 | EXPECT_EQ(0U, cng_encoder.Encode( |
| 213 | rtc::ArrayView<const int16_t>(speech_data_, 160), kNoSid, &sid_data)); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // We have reached 100 msec, and SID data should be generated. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 217 | EXPECT_EQ(kCNGNumParamsNormal + 1, cng_encoder.Encode( |
| 218 | rtc::ArrayView<const int16_t>(speech_data_, 160), kNoSid, &sid_data)); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // Test automatic SID, with very short interval. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 222 | TEST_F(CngTest, CngAutoSidShort) { |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 223 | rtc::Buffer sid_data; |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 224 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 225 | // Create and initialize encoder and decoder. |
| 226 | ComfortNoiseEncoder cng_encoder(16000, kSidShortIntervalUpdate, |
| 227 | kCNGNumParamsNormal); |
| 228 | ComfortNoiseDecoder cng_decoder; |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 229 | |
| 230 | // First call will never generate SID, unless forced to. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 231 | EXPECT_EQ(0U, cng_encoder.Encode( |
| 232 | rtc::ArrayView<const int16_t>(speech_data_, 160), kNoSid, &sid_data)); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 233 | |
| 234 | // Normal Encode, 100 msec, SID data should be generated all the time. |
| 235 | for (int i = 0; i < 10; i++) { |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 236 | EXPECT_EQ(kCNGNumParamsNormal + 1, cng_encoder.Encode( |
| 237 | rtc::ArrayView<const int16_t>(speech_data_, 160), kNoSid, &sid_data)); |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 238 | } |
tina.legrand@webrtc.org | a4f9ba6 | 2012-09-28 07:12:00 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | } // namespace webrtc |