diff options
Diffstat (limited to 'webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc')
-rw-r--r-- | webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc | 190 |
1 files changed, 119 insertions, 71 deletions
diff --git a/webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc b/webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc index 4630e44807..c82b184b38 100644 --- a/webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc +++ b/webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc @@ -10,7 +10,8 @@ #include <string> #include "testing/gtest/include/gtest/gtest.h" -#include "webrtc/modules/audio_coding/codecs/opus/include/opus_interface.h" +#include "webrtc/base/checks.h" +#include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" #include "webrtc/modules/audio_coding/codecs/opus/opus_inst.h" #include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h" #include "webrtc/test/testsupport/fileutils.h" @@ -35,17 +36,18 @@ class OpusTest : public TestWithParam<::testing::tuple<int, int>> { protected: OpusTest(); - void TestDtxEffect(bool dtx); + void TestDtxEffect(bool dtx, int block_length_ms); // Prepare |speech_data_| for encoding, read from a hard-coded file. // After preparation, |speech_data_.GetNextBlock()| returns a pointer to a // block of |block_length_ms| milliseconds. The data is looped every // |loop_length_ms| milliseconds. - void PrepareSpeechData(int channel, int block_length_ms, int loop_length_ms); + void PrepareSpeechData(size_t channel, + int block_length_ms, + int loop_length_ms); int EncodeDecode(WebRtcOpusEncInst* encoder, - const int16_t* input_audio, - size_t input_samples, + rtc::ArrayView<const int16_t> input_audio, WebRtcOpusDecInst* decoder, int16_t* output_audio, int16_t* audio_type); @@ -53,13 +55,16 @@ class OpusTest : public TestWithParam<::testing::tuple<int, int>> { void SetMaxPlaybackRate(WebRtcOpusEncInst* encoder, opus_int32 expect, int32_t set); + void CheckAudioBounded(const int16_t* audio, size_t samples, size_t channels, + uint16_t bound) const; + WebRtcOpusEncInst* opus_encoder_; WebRtcOpusDecInst* opus_decoder_; AudioLoop speech_data_; uint8_t bitstream_[kMaxBytes]; size_t encoded_bytes_; - int channels_; + size_t channels_; int application_; }; @@ -67,11 +72,11 @@ OpusTest::OpusTest() : opus_encoder_(NULL), opus_decoder_(NULL), encoded_bytes_(0), - channels_(::testing::get<0>(GetParam())), + channels_(static_cast<size_t>(::testing::get<0>(GetParam()))), application_(::testing::get<1>(GetParam())) { } -void OpusTest::PrepareSpeechData(int channel, int block_length_ms, +void OpusTest::PrepareSpeechData(size_t channel, int block_length_ms, int loop_length_ms) { const std::string file_name = webrtc::test::ResourcePath((channel == 1) ? @@ -95,14 +100,25 @@ void OpusTest::SetMaxPlaybackRate(WebRtcOpusEncInst* encoder, EXPECT_EQ(expect, bandwidth); } +void OpusTest::CheckAudioBounded(const int16_t* audio, size_t samples, + size_t channels, uint16_t bound) const { + for (size_t i = 0; i < samples; ++i) { + for (size_t c = 0; c < channels; ++c) { + ASSERT_GE(audio[i * channels + c], -bound); + ASSERT_LE(audio[i * channels + c], bound); + } + } +} + int OpusTest::EncodeDecode(WebRtcOpusEncInst* encoder, - const int16_t* input_audio, - size_t input_samples, + rtc::ArrayView<const int16_t> input_audio, WebRtcOpusDecInst* decoder, int16_t* output_audio, int16_t* audio_type) { - int encoded_bytes_int = WebRtcOpus_Encode(encoder, input_audio, input_samples, - kMaxBytes, bitstream_); + int encoded_bytes_int = WebRtcOpus_Encode( + encoder, input_audio.data(), + rtc::CheckedDivExact(input_audio.size(), channels_), + kMaxBytes, bitstream_); EXPECT_GE(encoded_bytes_int, 0); encoded_bytes_ = static_cast<size_t>(encoded_bytes_int); int est_len = WebRtcOpus_DurationEst(decoder, bitstream_, encoded_bytes_); @@ -115,8 +131,9 @@ int OpusTest::EncodeDecode(WebRtcOpusEncInst* encoder, // Test if encoder/decoder can enter DTX mode properly and do not enter DTX when // they should not. This test is signal dependent. -void OpusTest::TestDtxEffect(bool dtx) { - PrepareSpeechData(channels_, 20, 2000); +void OpusTest::TestDtxEffect(bool dtx, int block_length_ms) { + PrepareSpeechData(channels_, block_length_ms, 2000); + const size_t samples = kOpusRateKhz * block_length_ms; // Create encoder memory. EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_, @@ -129,22 +146,20 @@ void OpusTest::TestDtxEffect(bool dtx) { channels_ == 1 ? 32000 : 64000)); // Set input audio as silence. - int16_t* silence = new int16_t[kOpus20msFrameSamples * channels_]; - memset(silence, 0, sizeof(int16_t) * kOpus20msFrameSamples * channels_); + std::vector<int16_t> silence(samples * channels_, 0); // Setting DTX. EXPECT_EQ(0, dtx ? WebRtcOpus_EnableDtx(opus_encoder_) : WebRtcOpus_DisableDtx(opus_encoder_)); int16_t audio_type; - int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_]; + int16_t* output_data_decode = new int16_t[samples * channels_]; for (int i = 0; i < 100; ++i) { - EXPECT_EQ(kOpus20msFrameSamples, + EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode( - opus_encoder_, speech_data_.GetNextBlock(), - kOpus20msFrameSamples, opus_decoder_, output_data_decode, - &audio_type))); + opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_, + output_data_decode, &audio_type))); // If not DTX, it should never enter DTX mode. If DTX, we do not care since // whether it enters DTX depends on the signal type. if (!dtx) { @@ -158,10 +173,10 @@ void OpusTest::TestDtxEffect(bool dtx) { // We input some silent segments. In DTX mode, the encoder will stop sending. // However, DTX may happen after a while. for (int i = 0; i < 30; ++i) { - EXPECT_EQ(kOpus20msFrameSamples, + EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode( - opus_encoder_, silence, kOpus20msFrameSamples, opus_decoder_, - output_data_decode, &audio_type))); + opus_encoder_, silence, opus_decoder_, output_data_decode, + &audio_type))); if (!dtx) { EXPECT_GT(encoded_bytes_, 1U); EXPECT_EQ(0, opus_encoder_->in_dtx_mode); @@ -177,21 +192,47 @@ void OpusTest::TestDtxEffect(bool dtx) { // When Opus is in DTX, it wakes up in a regular basis. It sends two packets, // one with an arbitrary size and the other of 1-byte, then stops sending for - // 19 frames. - const int cycles = 5; - for (int j = 0; j < cycles; ++j) { - // DTX mode is maintained 19 frames. - for (int i = 0; i < 19; ++i) { - EXPECT_EQ(kOpus20msFrameSamples, + // a certain number of frames. + + // |max_dtx_frames| is the maximum number of frames Opus can stay in DTX. + const int max_dtx_frames = 400 / block_length_ms + 1; + + // We run |kRunTimeMs| milliseconds of pure silence. + const int kRunTimeMs = 2000; + + // We check that, after a |kCheckTimeMs| milliseconds (given that the CNG in + // Opus needs time to adapt), the absolute values of DTX decoded signal are + // bounded by |kOutputValueBound|. + const int kCheckTimeMs = 1500; + +#if defined(OPUS_FIXED_POINT) + const uint16_t kOutputValueBound = 20; +#else + const uint16_t kOutputValueBound = 2; +#endif + + int time = 0; + while (time < kRunTimeMs) { + // DTX mode is maintained for maximum |max_dtx_frames| frames. + int i = 0; + for (; i < max_dtx_frames; ++i) { + time += block_length_ms; + EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode( - opus_encoder_, silence, kOpus20msFrameSamples, - opus_decoder_, output_data_decode, &audio_type))); + opus_encoder_, silence, opus_decoder_, output_data_decode, + &audio_type))); if (dtx) { + if (encoded_bytes_ > 1) + break; EXPECT_EQ(0U, encoded_bytes_) // Send 0 byte. << "Opus should have entered DTX mode."; EXPECT_EQ(1, opus_encoder_->in_dtx_mode); EXPECT_EQ(1, opus_decoder_->in_dtx_mode); EXPECT_EQ(2, audio_type); // Comfort noise. + if (time >= kCheckTimeMs) { + CheckAudioBounded(output_data_decode, samples, channels_, + kOutputValueBound); + } } else { EXPECT_GT(encoded_bytes_, 1U); EXPECT_EQ(0, opus_encoder_->in_dtx_mode); @@ -200,27 +241,31 @@ void OpusTest::TestDtxEffect(bool dtx) { } } - // Quit DTX after 19 frames. - EXPECT_EQ(kOpus20msFrameSamples, - static_cast<size_t>(EncodeDecode( - opus_encoder_, silence, kOpus20msFrameSamples, opus_decoder_, - output_data_decode, &audio_type))); + if (dtx) { + // With DTX, Opus must stop transmission for some time. + EXPECT_GT(i, 1); + } - EXPECT_GT(encoded_bytes_, 1U); + // We expect a normal payload. EXPECT_EQ(0, opus_encoder_->in_dtx_mode); EXPECT_EQ(0, opus_decoder_->in_dtx_mode); EXPECT_EQ(0, audio_type); // Speech. // Enters DTX again immediately. - EXPECT_EQ(kOpus20msFrameSamples, + time += block_length_ms; + EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode( - opus_encoder_, silence, kOpus20msFrameSamples, opus_decoder_, - output_data_decode, &audio_type))); + opus_encoder_, silence, opus_decoder_, output_data_decode, + &audio_type))); if (dtx) { EXPECT_EQ(1U, encoded_bytes_); // Send 1 byte. EXPECT_EQ(1, opus_encoder_->in_dtx_mode); EXPECT_EQ(1, opus_decoder_->in_dtx_mode); EXPECT_EQ(2, audio_type); // Comfort noise. + if (time >= kCheckTimeMs) { + CheckAudioBounded(output_data_decode, samples, channels_, + kOutputValueBound); + } } else { EXPECT_GT(encoded_bytes_, 1U); EXPECT_EQ(0, opus_encoder_->in_dtx_mode); @@ -232,10 +277,10 @@ void OpusTest::TestDtxEffect(bool dtx) { silence[0] = 10000; if (dtx) { // Verify that encoder/decoder can jump out from DTX mode. - EXPECT_EQ(kOpus20msFrameSamples, + EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode( - opus_encoder_, silence, kOpus20msFrameSamples, opus_decoder_, - output_data_decode, &audio_type))); + opus_encoder_, silence, opus_decoder_, output_data_decode, + &audio_type))); EXPECT_GT(encoded_bytes_, 1U); EXPECT_EQ(0, opus_encoder_->in_dtx_mode); EXPECT_EQ(0, opus_decoder_->in_dtx_mode); @@ -244,7 +289,6 @@ void OpusTest::TestDtxEffect(bool dtx) { // Free memory. delete[] output_data_decode; - delete[] silence; EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); } @@ -314,10 +358,9 @@ TEST_P(OpusTest, OpusEncodeDecode) { int16_t audio_type; int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_]; EXPECT_EQ(kOpus20msFrameSamples, - static_cast<size_t>(EncodeDecode( - opus_encoder_, speech_data_.GetNextBlock(), - kOpus20msFrameSamples, opus_decoder_, output_data_decode, - &audio_type))); + static_cast<size_t>( + EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), + opus_decoder_, output_data_decode, &audio_type))); // Free memory. delete[] output_data_decode; @@ -374,10 +417,9 @@ TEST_P(OpusTest, OpusDecodeInit) { int16_t audio_type; int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_]; EXPECT_EQ(kOpus20msFrameSamples, - static_cast<size_t>(EncodeDecode( - opus_encoder_, speech_data_.GetNextBlock(), - kOpus20msFrameSamples, opus_decoder_, output_data_decode, - &audio_type))); + static_cast<size_t>( + EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), + opus_decoder_, output_data_decode, &audio_type))); WebRtcOpus_DecoderInit(opus_decoder_); @@ -444,11 +486,15 @@ TEST_P(OpusTest, OpusEnableDisableDtx) { } TEST_P(OpusTest, OpusDtxOff) { - TestDtxEffect(false); + TestDtxEffect(false, 10); + TestDtxEffect(false, 20); + TestDtxEffect(false, 40); } TEST_P(OpusTest, OpusDtxOn) { - TestDtxEffect(true); + TestDtxEffect(true, 10); + TestDtxEffect(true, 20); + TestDtxEffect(true, 40); } TEST_P(OpusTest, OpusSetPacketLossRate) { @@ -513,10 +559,9 @@ TEST_P(OpusTest, OpusDecodePlc) { int16_t audio_type; int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_]; EXPECT_EQ(kOpus20msFrameSamples, - static_cast<size_t>(EncodeDecode( - opus_encoder_, speech_data_.GetNextBlock(), - kOpus20msFrameSamples, opus_decoder_, output_data_decode, - &audio_type))); + static_cast<size_t>( + EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), + opus_decoder_, output_data_decode, &audio_type))); // Call decoder PLC. int16_t* plc_buffer = new int16_t[kOpus20msFrameSamples * channels_]; @@ -542,10 +587,11 @@ TEST_P(OpusTest, OpusDurationEstimation) { EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_)); // 10 ms. We use only first 10 ms of a 20 ms block. - int encoded_bytes_int = WebRtcOpus_Encode(opus_encoder_, - speech_data_.GetNextBlock(), - kOpus10msFrameSamples, - kMaxBytes, bitstream_); + auto speech_block = speech_data_.GetNextBlock(); + int encoded_bytes_int = WebRtcOpus_Encode( + opus_encoder_, speech_block.data(), + rtc::CheckedDivExact(speech_block.size(), 2 * channels_), + kMaxBytes, bitstream_); EXPECT_GE(encoded_bytes_int, 0); EXPECT_EQ(kOpus10msFrameSamples, static_cast<size_t>(WebRtcOpus_DurationEst( @@ -553,10 +599,11 @@ TEST_P(OpusTest, OpusDurationEstimation) { static_cast<size_t>(encoded_bytes_int)))); // 20 ms - encoded_bytes_int = WebRtcOpus_Encode(opus_encoder_, - speech_data_.GetNextBlock(), - kOpus20msFrameSamples, - kMaxBytes, bitstream_); + speech_block = speech_data_.GetNextBlock(); + encoded_bytes_int = WebRtcOpus_Encode( + opus_encoder_, speech_block.data(), + rtc::CheckedDivExact(speech_block.size(), channels_), + kMaxBytes, bitstream_); EXPECT_GE(encoded_bytes_int, 0); EXPECT_EQ(kOpus20msFrameSamples, static_cast<size_t>(WebRtcOpus_DurationEst( @@ -594,10 +641,11 @@ TEST_P(OpusTest, OpusDecodeRepacketized) { OpusRepacketizer* rp = opus_repacketizer_create(); for (int idx = 0; idx < kPackets; idx++) { - encoded_bytes_ = WebRtcOpus_Encode(opus_encoder_, - speech_data_.GetNextBlock(), - kOpus20msFrameSamples, kMaxBytes, - bitstream_); + auto speech_block = speech_data_.GetNextBlock(); + encoded_bytes_ = + WebRtcOpus_Encode(opus_encoder_, speech_block.data(), + rtc::CheckedDivExact(speech_block.size(), channels_), + kMaxBytes, bitstream_); EXPECT_EQ(OPUS_OK, opus_repacketizer_cat(rp, bitstream_, encoded_bytes_)); } |