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

Side by Side Diff: webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc

Issue 2772773002: Adding cbr support for Opus (Closed)
Patch Set: Reverted everything but audio_encoder_opus and opus_interface Created 3 years, 8 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/opus/opus_interface.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) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 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 22 matching lines...) Expand all
33 const size_t kOpus20msFrameSamples = kOpusRateKhz * 20; 33 const size_t kOpus20msFrameSamples = kOpusRateKhz * 20;
34 // Number of samples-per-channel in a 10 ms frame, sampled at 48 kHz. 34 // Number of samples-per-channel in a 10 ms frame, sampled at 48 kHz.
35 const size_t kOpus10msFrameSamples = kOpusRateKhz * 10; 35 const size_t kOpus10msFrameSamples = kOpusRateKhz * 10;
36 36
37 class OpusTest : public TestWithParam<::testing::tuple<int, int>> { 37 class OpusTest : public TestWithParam<::testing::tuple<int, int>> {
38 protected: 38 protected:
39 OpusTest(); 39 OpusTest();
40 40
41 void TestDtxEffect(bool dtx, int block_length_ms); 41 void TestDtxEffect(bool dtx, int block_length_ms);
42 42
43 void TestCbrEffect(bool dtx, int block_length_ms);
44
43 // Prepare |speech_data_| for encoding, read from a hard-coded file. 45 // Prepare |speech_data_| for encoding, read from a hard-coded file.
44 // After preparation, |speech_data_.GetNextBlock()| returns a pointer to a 46 // After preparation, |speech_data_.GetNextBlock()| returns a pointer to a
45 // block of |block_length_ms| milliseconds. The data is looped every 47 // block of |block_length_ms| milliseconds. The data is looped every
46 // |loop_length_ms| milliseconds. 48 // |loop_length_ms| milliseconds.
47 void PrepareSpeechData(size_t channel, 49 void PrepareSpeechData(size_t channel,
48 int block_length_ms, 50 int block_length_ms,
49 int loop_length_ms); 51 int loop_length_ms);
50 52
51 int EncodeDecode(WebRtcOpusEncInst* encoder, 53 int EncodeDecode(WebRtcOpusEncInst* encoder,
52 rtc::ArrayView<const int16_t> input_audio, 54 rtc::ArrayView<const int16_t> input_audio,
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 EXPECT_EQ(0, opus_decoder_->in_dtx_mode); 295 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
294 EXPECT_EQ(0, audio_type); // Speech. 296 EXPECT_EQ(0, audio_type); // Speech.
295 } 297 }
296 298
297 // Free memory. 299 // Free memory.
298 delete[] output_data_decode; 300 delete[] output_data_decode;
299 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); 301 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
300 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); 302 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
301 } 303 }
302 304
305 // Test if CBR does what we expect.
306 void OpusTest::TestCbrEffect(bool cbr, int block_length_ms) {
307 PrepareSpeechData(channels_, block_length_ms, 2000);
308 const size_t samples = kOpusRateKhz * block_length_ms;
309
310 int32_t max_pkt_size_diff = 0;
311 int32_t prev_pkt_size = 0;
312
313 // Create encoder memory.
314 EXPECT_EQ(0,
315 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
316 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
317
318 // Set bitrate.
319 EXPECT_EQ(
320 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
321
322 // Setting CBR.
323 EXPECT_EQ(0, cbr ? WebRtcOpus_EnableCbr(opus_encoder_)
324 : WebRtcOpus_DisableCbr(opus_encoder_));
325
326 int16_t audio_type;
327 std::vector<int16_t> audio_out(samples * channels_);
328 for (int i = 0; i < 100; ++i) {
329 EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode(
330 opus_encoder_, speech_data_.GetNextBlock(),
331 opus_decoder_, audio_out.data(), &audio_type)));
332
333 if (prev_pkt_size > 0) {
334 int32_t diff = std::abs((int32_t)encoded_bytes_ - prev_pkt_size);
335 max_pkt_size_diff = std::max(max_pkt_size_diff, diff);
336 }
337 prev_pkt_size = encoded_bytes_;
338 }
339
340 if (cbr) {
341 EXPECT_EQ(max_pkt_size_diff, 0);
342 } else {
343 EXPECT_GT(max_pkt_size_diff, 0);
344 }
345
346 // Free memory.
347 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
348 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
349 }
350
303 // Test failing Create. 351 // Test failing Create.
304 TEST(OpusTest, OpusCreateFail) { 352 TEST(OpusTest, OpusCreateFail) {
305 WebRtcOpusEncInst* opus_encoder; 353 WebRtcOpusEncInst* opus_encoder;
306 WebRtcOpusDecInst* opus_decoder; 354 WebRtcOpusDecInst* opus_decoder;
307 355
308 // Test to see that an invalid pointer is caught. 356 // Test to see that an invalid pointer is caught.
309 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(NULL, 1, 0)); 357 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(NULL, 1, 0));
310 // Invalid channel number. 358 // Invalid channel number.
311 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 3, 0)); 359 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 3, 0));
312 // Invalid applciation mode. 360 // Invalid applciation mode.
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 TestDtxEffect(false, 20); 566 TestDtxEffect(false, 20);
519 TestDtxEffect(false, 40); 567 TestDtxEffect(false, 40);
520 } 568 }
521 569
522 TEST_P(OpusTest, OpusDtxOn) { 570 TEST_P(OpusTest, OpusDtxOn) {
523 TestDtxEffect(true, 10); 571 TestDtxEffect(true, 10);
524 TestDtxEffect(true, 20); 572 TestDtxEffect(true, 20);
525 TestDtxEffect(true, 40); 573 TestDtxEffect(true, 40);
526 } 574 }
527 575
576 TEST_P(OpusTest, OpusCbrOff) {
577 TestCbrEffect(false, 10);
578 TestCbrEffect(false, 20);
579 TestCbrEffect(false, 40);
580 }
581
582 TEST_P(OpusTest, OpusCbrOn) {
583 TestCbrEffect(true, 10);
584 TestCbrEffect(true, 20);
585 TestCbrEffect(true, 40);
586 }
587
528 TEST_P(OpusTest, OpusSetPacketLossRate) { 588 TEST_P(OpusTest, OpusSetPacketLossRate) {
529 // Test without creating encoder memory. 589 // Test without creating encoder memory.
530 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50)); 590 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
531 591
532 // Create encoder memory. 592 // Create encoder memory.
533 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_, 593 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
534 channels_, 594 channels_,
535 application_)); 595 application_));
536 596
537 EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50)); 597 EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); 753 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
694 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); 754 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
695 } 755 }
696 756
697 INSTANTIATE_TEST_CASE_P(VariousMode, 757 INSTANTIATE_TEST_CASE_P(VariousMode,
698 OpusTest, 758 OpusTest,
699 Combine(Values(1, 2), Values(0, 1))); 759 Combine(Values(1, 2), Values(0, 1)));
700 760
701 761
702 } // namespace webrtc 762 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_coding/codecs/opus/opus_interface.c ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698