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

Side by Side Diff: webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc

Issue 1725143003: Changed AudioEncoder::Encode to take an rtc::Buffer* instead of uint8_t* and a maximum size. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fixed issues from comments, rewrote MockAudioEncoderHelper Created 4 years, 10 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
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 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
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 } 93 }
94 } // namespace 94 } // namespace
95 95
96 class AudioDecoderTest : public ::testing::Test { 96 class AudioDecoderTest : public ::testing::Test {
97 protected: 97 protected:
98 AudioDecoderTest() 98 AudioDecoderTest()
99 : input_audio_( 99 : input_audio_(
100 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 100 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
101 32000), 101 32000),
102 codec_input_rate_hz_(32000), // Legacy default value. 102 codec_input_rate_hz_(32000), // Legacy default value.
103 encoded_(NULL),
104 frame_size_(0), 103 frame_size_(0),
105 data_length_(0), 104 data_length_(0),
106 encoded_bytes_(0),
107 channels_(1), 105 channels_(1),
108 payload_type_(17), 106 payload_type_(17),
109 decoder_(NULL) {} 107 decoder_(NULL) {}
110 108
111 virtual ~AudioDecoderTest() {} 109 virtual ~AudioDecoderTest() {}
112 110
113 virtual void SetUp() { 111 virtual void SetUp() {
114 if (audio_encoder_) 112 if (audio_encoder_)
115 codec_input_rate_hz_ = audio_encoder_->SampleRateHz(); 113 codec_input_rate_hz_ = audio_encoder_->SampleRateHz();
116 // Create arrays. 114 // Create arrays.
117 ASSERT_GT(data_length_, 0u) << "The test must set data_length_ > 0"; 115 ASSERT_GT(data_length_, 0u) << "The test must set data_length_ > 0";
118 // Longest encoded data is produced by PCM16b with 2 bytes per sample.
119 encoded_ = new uint8_t[data_length_ * 2];
120 // Logging to view input and output in Matlab. 116 // Logging to view input and output in Matlab.
121 // Use 'gyp -Denable_data_logging=1' to enable logging. 117 // Use 'gyp -Denable_data_logging=1' to enable logging.
122 DataLog::CreateLog(); 118 DataLog::CreateLog();
123 DataLog::AddTable("CodecTest"); 119 DataLog::AddTable("CodecTest");
124 DataLog::AddColumn("CodecTest", "input", 1); 120 DataLog::AddColumn("CodecTest", "input", 1);
125 DataLog::AddColumn("CodecTest", "output", 1); 121 DataLog::AddColumn("CodecTest", "output", 1);
126 } 122 }
127 123
128 virtual void TearDown() { 124 virtual void TearDown() {
129 delete decoder_; 125 delete decoder_;
130 decoder_ = NULL; 126 decoder_ = NULL;
131 // Delete arrays.
132 delete [] encoded_;
133 encoded_ = NULL;
134 // Close log. 127 // Close log.
135 DataLog::ReturnLog(); 128 DataLog::ReturnLog();
136 } 129 }
137 130
138 virtual void InitEncoder() { } 131 virtual void InitEncoder() { }
139 132
140 // TODO(henrik.lundin) Change return type to size_t once most/all overriding 133 // TODO(henrik.lundin) Change return type to size_t once most/all overriding
141 // implementations are gone. 134 // implementations are gone.
142 virtual int EncodeFrame(const int16_t* input, 135 virtual int EncodeFrame(const int16_t* input,
143 size_t input_len_samples, 136 size_t input_len_samples,
144 uint8_t* output) { 137 rtc::Buffer* output) {
145 encoded_info_.encoded_bytes = 0; 138 AudioEncoder::EncodedInfo encoded_info;
139 encoded_info.encoded_bytes = 0;
146 const size_t samples_per_10ms = audio_encoder_->SampleRateHz() / 100; 140 const size_t samples_per_10ms = audio_encoder_->SampleRateHz() / 100;
147 RTC_CHECK_EQ(samples_per_10ms * audio_encoder_->Num10MsFramesInNextPacket(), 141 RTC_CHECK_EQ(samples_per_10ms * audio_encoder_->Num10MsFramesInNextPacket(),
148 input_len_samples); 142 input_len_samples);
149 std::unique_ptr<int16_t[]> interleaved_input( 143 std::unique_ptr<int16_t[]> interleaved_input(
150 new int16_t[channels_ * samples_per_10ms]); 144 new int16_t[channels_ * samples_per_10ms]);
151 for (size_t i = 0; i < audio_encoder_->Num10MsFramesInNextPacket(); ++i) { 145 for (size_t i = 0; i < audio_encoder_->Num10MsFramesInNextPacket(); ++i) {
152 EXPECT_EQ(0u, encoded_info_.encoded_bytes); 146 EXPECT_EQ(0u, encoded_info.encoded_bytes);
153 147
154 // Duplicate the mono input signal to however many channels the test 148 // Duplicate the mono input signal to however many channels the test
155 // wants. 149 // wants.
156 test::InputAudioFile::DuplicateInterleaved(input + i * samples_per_10ms, 150 test::InputAudioFile::DuplicateInterleaved(input + i * samples_per_10ms,
157 samples_per_10ms, channels_, 151 samples_per_10ms, channels_,
158 interleaved_input.get()); 152 interleaved_input.get());
159 153
160 encoded_info_ = audio_encoder_->Encode( 154 encoded_info = audio_encoder_->Encode(
161 0, rtc::ArrayView<const int16_t>(interleaved_input.get(), 155 0, rtc::ArrayView<const int16_t>(interleaved_input.get(),
162 audio_encoder_->NumChannels() * 156 audio_encoder_->NumChannels() *
163 audio_encoder_->SampleRateHz() / 157 audio_encoder_->SampleRateHz() /
164 100), 158 100),
165 data_length_ * 2, output); 159 output);
166 } 160 }
167 EXPECT_EQ(payload_type_, encoded_info_.payload_type); 161 EXPECT_EQ(payload_type_, encoded_info.payload_type);
168 return static_cast<int>(encoded_info_.encoded_bytes); 162 return static_cast<int>(encoded_info.encoded_bytes);
169 } 163 }
170 164
171 // Encodes and decodes audio. The absolute difference between the input and 165 // Encodes and decodes audio. The absolute difference between the input and
172 // output is compared vs |tolerance|, and the mean-squared error is compared 166 // output is compared vs |tolerance|, and the mean-squared error is compared
173 // with |mse|. The encoded stream should contain |expected_bytes|. For stereo 167 // with |mse|. The encoded stream should contain |expected_bytes|. For stereo
174 // audio, the absolute difference between the two channels is compared vs 168 // audio, the absolute difference between the two channels is compared vs
175 // |channel_diff_tolerance|. 169 // |channel_diff_tolerance|.
176 void EncodeDecodeTest(size_t expected_bytes, int tolerance, double mse, 170 void EncodeDecodeTest(size_t expected_bytes, int tolerance, double mse,
177 int delay = 0, int channel_diff_tolerance = 0) { 171 int delay = 0, int channel_diff_tolerance = 0) {
178 ASSERT_GE(tolerance, 0) << "Test must define a tolerance >= 0"; 172 ASSERT_GE(tolerance, 0) << "Test must define a tolerance >= 0";
179 ASSERT_GE(channel_diff_tolerance, 0) << 173 ASSERT_GE(channel_diff_tolerance, 0) <<
180 "Test must define a channel_diff_tolerance >= 0"; 174 "Test must define a channel_diff_tolerance >= 0";
181 size_t processed_samples = 0u; 175 size_t processed_samples = 0u;
182 encoded_bytes_ = 0u; 176 rtc::Buffer encoded;
177 size_t encoded_bytes = 0u;
183 InitEncoder(); 178 InitEncoder();
184 std::vector<int16_t> input; 179 std::vector<int16_t> input;
185 std::vector<int16_t> decoded; 180 std::vector<int16_t> decoded;
186 while (processed_samples + frame_size_ <= data_length_) { 181 while (processed_samples + frame_size_ <= data_length_) {
187 // Extend input vector with |frame_size_|. 182 // Extend input vector with |frame_size_|.
188 input.resize(input.size() + frame_size_, 0); 183 input.resize(input.size() + frame_size_, 0);
189 // Read from input file. 184 // Read from input file.
190 ASSERT_GE(input.size() - processed_samples, frame_size_); 185 ASSERT_GE(input.size() - processed_samples, frame_size_);
191 ASSERT_TRUE(input_audio_.Read( 186 ASSERT_TRUE(input_audio_.Read(
192 frame_size_, codec_input_rate_hz_, &input[processed_samples])); 187 frame_size_, codec_input_rate_hz_, &input[processed_samples]));
193 size_t enc_len = EncodeFrame( 188 size_t enc_len = EncodeFrame(
194 &input[processed_samples], frame_size_, &encoded_[encoded_bytes_]); 189 &input[processed_samples], frame_size_, &encoded);
195 // Make sure that frame_size_ * channels_ samples are allocated and free. 190 // Make sure that frame_size_ * channels_ samples are allocated and free.
196 decoded.resize((processed_samples + frame_size_) * channels_, 0); 191 decoded.resize((processed_samples + frame_size_) * channels_, 0);
197 AudioDecoder::SpeechType speech_type; 192 AudioDecoder::SpeechType speech_type;
198 size_t dec_len = decoder_->Decode( 193 size_t dec_len = decoder_->Decode(
199 &encoded_[encoded_bytes_], enc_len, codec_input_rate_hz_, 194 &encoded.data()[encoded_bytes], enc_len, codec_input_rate_hz_,
200 frame_size_ * channels_ * sizeof(int16_t), 195 frame_size_ * channels_ * sizeof(int16_t),
201 &decoded[processed_samples * channels_], &speech_type); 196 &decoded[processed_samples * channels_], &speech_type);
202 EXPECT_EQ(frame_size_ * channels_, dec_len); 197 EXPECT_EQ(frame_size_ * channels_, dec_len);
203 encoded_bytes_ += enc_len; 198 encoded_bytes += enc_len;
204 processed_samples += frame_size_; 199 processed_samples += frame_size_;
205 } 200 }
206 // For some codecs it doesn't make sense to check expected number of bytes, 201 // For some codecs it doesn't make sense to check expected number of bytes,
207 // since the number can vary for different platforms. Opus and iSAC are 202 // since the number can vary for different platforms. Opus and iSAC are
208 // such codecs. In this case expected_bytes is set to 0. 203 // such codecs. In this case expected_bytes is set to 0.
209 if (expected_bytes) { 204 if (expected_bytes) {
210 EXPECT_EQ(expected_bytes, encoded_bytes_); 205 EXPECT_EQ(expected_bytes, encoded_bytes);
211 } 206 }
212 CompareInputOutput( 207 CompareInputOutput(
213 input, decoded, processed_samples, channels_, tolerance, delay); 208 input, decoded, processed_samples, channels_, tolerance, delay);
214 if (channels_ == 2) 209 if (channels_ == 2)
215 CompareTwoChannels( 210 CompareTwoChannels(
216 decoded, processed_samples, channels_, channel_diff_tolerance); 211 decoded, processed_samples, channels_, channel_diff_tolerance);
217 EXPECT_LE( 212 EXPECT_LE(
218 MseInputOutput(input, decoded, processed_samples, channels_, delay), 213 MseInputOutput(input, decoded, processed_samples, channels_, delay),
219 mse); 214 mse);
220 } 215 }
221 216
222 // Encodes a payload and decodes it twice with decoder re-init before each 217 // Encodes a payload and decodes it twice with decoder re-init before each
223 // decode. Verifies that the decoded result is the same. 218 // decode. Verifies that the decoded result is the same.
224 void ReInitTest() { 219 void ReInitTest() {
225 InitEncoder(); 220 InitEncoder();
226 std::unique_ptr<int16_t[]> input(new int16_t[frame_size_]); 221 std::unique_ptr<int16_t[]> input(new int16_t[frame_size_]);
227 ASSERT_TRUE( 222 ASSERT_TRUE(
228 input_audio_.Read(frame_size_, codec_input_rate_hz_, input.get())); 223 input_audio_.Read(frame_size_, codec_input_rate_hz_, input.get()));
229 size_t enc_len = EncodeFrame(input.get(), frame_size_, encoded_); 224 rtc::Buffer encoded;
225 size_t enc_len = EncodeFrame(input.get(), frame_size_, &encoded);
230 size_t dec_len; 226 size_t dec_len;
231 AudioDecoder::SpeechType speech_type1, speech_type2; 227 AudioDecoder::SpeechType speech_type1, speech_type2;
232 decoder_->Reset(); 228 decoder_->Reset();
233 std::unique_ptr<int16_t[]> output1(new int16_t[frame_size_ * channels_]); 229 std::unique_ptr<int16_t[]> output1(new int16_t[frame_size_ * channels_]);
234 dec_len = decoder_->Decode(encoded_, enc_len, codec_input_rate_hz_, 230 dec_len = decoder_->Decode(encoded.data(), enc_len, codec_input_rate_hz_,
235 frame_size_ * channels_ * sizeof(int16_t), 231 frame_size_ * channels_ * sizeof(int16_t),
236 output1.get(), &speech_type1); 232 output1.get(), &speech_type1);
237 ASSERT_LE(dec_len, frame_size_ * channels_); 233 ASSERT_LE(dec_len, frame_size_ * channels_);
238 EXPECT_EQ(frame_size_ * channels_, dec_len); 234 EXPECT_EQ(frame_size_ * channels_, dec_len);
239 // Re-init decoder and decode again. 235 // Re-init decoder and decode again.
240 decoder_->Reset(); 236 decoder_->Reset();
241 std::unique_ptr<int16_t[]> output2(new int16_t[frame_size_ * channels_]); 237 std::unique_ptr<int16_t[]> output2(new int16_t[frame_size_ * channels_]);
242 dec_len = decoder_->Decode(encoded_, enc_len, codec_input_rate_hz_, 238 dec_len = decoder_->Decode(encoded.data(), enc_len, codec_input_rate_hz_,
243 frame_size_ * channels_ * sizeof(int16_t), 239 frame_size_ * channels_ * sizeof(int16_t),
244 output2.get(), &speech_type2); 240 output2.get(), &speech_type2);
245 ASSERT_LE(dec_len, frame_size_ * channels_); 241 ASSERT_LE(dec_len, frame_size_ * channels_);
246 EXPECT_EQ(frame_size_ * channels_, dec_len); 242 EXPECT_EQ(frame_size_ * channels_, dec_len);
247 for (unsigned int n = 0; n < frame_size_; ++n) { 243 for (unsigned int n = 0; n < frame_size_; ++n) {
248 ASSERT_EQ(output1[n], output2[n]) << "Exit test on first diff; n = " << n; 244 ASSERT_EQ(output1[n], output2[n]) << "Exit test on first diff; n = " << n;
249 } 245 }
250 EXPECT_EQ(speech_type1, speech_type2); 246 EXPECT_EQ(speech_type1, speech_type2);
251 } 247 }
252 248
253 // Call DecodePlc and verify that the correct number of samples is produced. 249 // Call DecodePlc and verify that the correct number of samples is produced.
254 void DecodePlcTest() { 250 void DecodePlcTest() {
255 InitEncoder(); 251 InitEncoder();
256 std::unique_ptr<int16_t[]> input(new int16_t[frame_size_]); 252 std::unique_ptr<int16_t[]> input(new int16_t[frame_size_]);
257 ASSERT_TRUE( 253 ASSERT_TRUE(
258 input_audio_.Read(frame_size_, codec_input_rate_hz_, input.get())); 254 input_audio_.Read(frame_size_, codec_input_rate_hz_, input.get()));
259 size_t enc_len = EncodeFrame(input.get(), frame_size_, encoded_); 255 rtc::Buffer encoded;
256 size_t enc_len = EncodeFrame(input.get(), frame_size_, &encoded);
260 AudioDecoder::SpeechType speech_type; 257 AudioDecoder::SpeechType speech_type;
261 decoder_->Reset(); 258 decoder_->Reset();
262 std::unique_ptr<int16_t[]> output(new int16_t[frame_size_ * channels_]); 259 std::unique_ptr<int16_t[]> output(new int16_t[frame_size_ * channels_]);
263 size_t dec_len = decoder_->Decode(encoded_, enc_len, codec_input_rate_hz_, 260 size_t dec_len = decoder_->Decode(encoded.data(), enc_len,
261 codec_input_rate_hz_,
264 frame_size_ * channels_ * sizeof(int16_t), 262 frame_size_ * channels_ * sizeof(int16_t),
265 output.get(), &speech_type); 263 output.get(), &speech_type);
266 EXPECT_EQ(frame_size_ * channels_, dec_len); 264 EXPECT_EQ(frame_size_ * channels_, dec_len);
267 // Call DecodePlc and verify that we get one frame of data. 265 // Call DecodePlc and verify that we get one frame of data.
268 // (Overwrite the output from the above Decode call, but that does not 266 // (Overwrite the output from the above Decode call, but that does not
269 // matter.) 267 // matter.)
270 dec_len = decoder_->DecodePlc(1, output.get()); 268 dec_len = decoder_->DecodePlc(1, output.get());
271 EXPECT_EQ(frame_size_ * channels_, dec_len); 269 EXPECT_EQ(frame_size_ * channels_, dec_len);
272 } 270 }
273 271
274 test::ResampleInputAudioFile input_audio_; 272 test::ResampleInputAudioFile input_audio_;
275 int codec_input_rate_hz_; 273 int codec_input_rate_hz_;
276 uint8_t* encoded_;
277 size_t frame_size_; 274 size_t frame_size_;
278 size_t data_length_; 275 size_t data_length_;
279 size_t encoded_bytes_;
280 size_t channels_; 276 size_t channels_;
281 const int payload_type_; 277 const int payload_type_;
282 AudioEncoder::EncodedInfo encoded_info_;
283 AudioDecoder* decoder_; 278 AudioDecoder* decoder_;
284 std::unique_ptr<AudioEncoder> audio_encoder_; 279 std::unique_ptr<AudioEncoder> audio_encoder_;
285 }; 280 };
286 281
287 class AudioDecoderPcmUTest : public AudioDecoderTest { 282 class AudioDecoderPcmUTest : public AudioDecoderTest {
288 protected: 283 protected:
289 AudioDecoderPcmUTest() : AudioDecoderTest() { 284 AudioDecoderPcmUTest() : AudioDecoderTest() {
290 frame_size_ = 160; 285 frame_size_ = 160;
291 data_length_ = 10 * frame_size_; 286 data_length_ = 10 * frame_size_;
292 decoder_ = new AudioDecoderPcmU(1); 287 decoder_ = new AudioDecoderPcmU(1);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 audio_encoder_.reset(new AudioEncoderIlbc(config)); 336 audio_encoder_.reset(new AudioEncoderIlbc(config));
342 } 337 }
343 338
344 // Overload the default test since iLBC's function WebRtcIlbcfix_NetEqPlc does 339 // Overload the default test since iLBC's function WebRtcIlbcfix_NetEqPlc does
345 // not return any data. It simply resets a few states and returns 0. 340 // not return any data. It simply resets a few states and returns 0.
346 void DecodePlcTest() { 341 void DecodePlcTest() {
347 InitEncoder(); 342 InitEncoder();
348 std::unique_ptr<int16_t[]> input(new int16_t[frame_size_]); 343 std::unique_ptr<int16_t[]> input(new int16_t[frame_size_]);
349 ASSERT_TRUE( 344 ASSERT_TRUE(
350 input_audio_.Read(frame_size_, codec_input_rate_hz_, input.get())); 345 input_audio_.Read(frame_size_, codec_input_rate_hz_, input.get()));
351 size_t enc_len = EncodeFrame(input.get(), frame_size_, encoded_); 346 rtc::Buffer encoded;
347 size_t enc_len = EncodeFrame(input.get(), frame_size_, &encoded);
352 AudioDecoder::SpeechType speech_type; 348 AudioDecoder::SpeechType speech_type;
353 decoder_->Reset(); 349 decoder_->Reset();
354 std::unique_ptr<int16_t[]> output(new int16_t[frame_size_ * channels_]); 350 std::unique_ptr<int16_t[]> output(new int16_t[frame_size_ * channels_]);
355 size_t dec_len = decoder_->Decode(encoded_, enc_len, codec_input_rate_hz_, 351 size_t dec_len = decoder_->Decode(encoded.data(), enc_len,
352 codec_input_rate_hz_,
356 frame_size_ * channels_ * sizeof(int16_t), 353 frame_size_ * channels_ * sizeof(int16_t),
357 output.get(), &speech_type); 354 output.get(), &speech_type);
358 EXPECT_EQ(frame_size_, dec_len); 355 EXPECT_EQ(frame_size_, dec_len);
359 // Simply call DecodePlc and verify that we get 0 as return value. 356 // Simply call DecodePlc and verify that we get 0 as return value.
360 EXPECT_EQ(0U, decoder_->DecodePlc(1, output.get())); 357 EXPECT_EQ(0U, decoder_->DecodePlc(1, output.get()));
361 } 358 }
362 }; 359 };
363 360
364 class AudioDecoderIsacFloatTest : public AudioDecoderTest { 361 class AudioDecoderIsacFloatTest : public AudioDecoderTest {
365 protected: 362 protected:
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 EXPECT_TRUE(CodecSupported(NetEqDecoder::kDecoderCNGnb)); 739 EXPECT_TRUE(CodecSupported(NetEqDecoder::kDecoderCNGnb));
743 EXPECT_TRUE(CodecSupported(NetEqDecoder::kDecoderCNGwb)); 740 EXPECT_TRUE(CodecSupported(NetEqDecoder::kDecoderCNGwb));
744 EXPECT_TRUE(CodecSupported(NetEqDecoder::kDecoderCNGswb32kHz)); 741 EXPECT_TRUE(CodecSupported(NetEqDecoder::kDecoderCNGswb32kHz));
745 EXPECT_TRUE(CodecSupported(NetEqDecoder::kDecoderCNGswb48kHz)); 742 EXPECT_TRUE(CodecSupported(NetEqDecoder::kDecoderCNGswb48kHz));
746 EXPECT_TRUE(CodecSupported(NetEqDecoder::kDecoderArbitrary)); 743 EXPECT_TRUE(CodecSupported(NetEqDecoder::kDecoderArbitrary));
747 EXPECT_EQ(has_opus, CodecSupported(NetEqDecoder::kDecoderOpus)); 744 EXPECT_EQ(has_opus, CodecSupported(NetEqDecoder::kDecoderOpus));
748 EXPECT_EQ(has_opus, CodecSupported(NetEqDecoder::kDecoderOpus_2ch)); 745 EXPECT_EQ(has_opus, CodecSupported(NetEqDecoder::kDecoderOpus_2ch));
749 } 746 }
750 747
751 } // namespace webrtc 748 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698