tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | */ |
kwiberg | 91d9756 | 2016-02-14 01:10:03 -0800 | [diff] [blame] | 10 | |
| 11 | #include <memory> |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/audio_coding/codecs/opus/opus_inst.h" |
| 15 | #include "modules/audio_coding/codecs/opus/opus_interface.h" |
| 16 | #include "modules/audio_coding/neteq/tools/audio_loop.h" |
| 17 | #include "rtc_base/checks.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 18 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "test/testsupport/file_utils.h" |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 21 | |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 24 | namespace { |
| 25 | // Equivalent to SDP params |
| 26 | // {{"channel_mapping", "0,1,2,3"}, {"coupled_streams", "2"}}. |
| 27 | constexpr unsigned char kQuadChannelMapping[] = {0, 1, 2, 3}; |
| 28 | constexpr int kQuadCoupledStreams = 2; |
| 29 | |
| 30 | constexpr unsigned char kStereoChannelMapping[] = {0, 1}; |
| 31 | constexpr int kStereoCoupledStreams = 1; |
| 32 | |
| 33 | constexpr unsigned char kMonoChannelMapping[] = {0}; |
| 34 | constexpr int kMonoCoupledStreams = 0; |
| 35 | |
| 36 | void CreateSingleOrMultiStreamEncoder(WebRtcOpusEncInst** opus_encoder, |
| 37 | int channels, |
| 38 | int application, |
| 39 | bool force_multistream = false) { |
| 40 | if (!force_multistream && (channels == 1 || channels == 2)) { |
| 41 | EXPECT_EQ(0, WebRtcOpus_EncoderCreate(opus_encoder, channels, application)); |
| 42 | } else if (force_multistream && channels == 1) { |
| 43 | EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate( |
| 44 | opus_encoder, channels, application, kMonoCoupledStreams, |
| 45 | kMonoChannelMapping)); |
| 46 | } else if (force_multistream && channels == 2) { |
| 47 | EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate( |
| 48 | opus_encoder, channels, application, kStereoCoupledStreams, |
| 49 | kStereoChannelMapping)); |
| 50 | } else if (channels == 4) { |
| 51 | EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate( |
| 52 | opus_encoder, channels, application, kQuadCoupledStreams, |
| 53 | kQuadChannelMapping)); |
| 54 | } else { |
| 55 | EXPECT_TRUE(false) << channels; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void CreateSingleOrMultiStreamDecoder(WebRtcOpusDecInst** opus_decoder, |
| 60 | int channels, |
| 61 | bool force_multistream = false) { |
| 62 | if (!force_multistream && (channels == 1 || channels == 2)) { |
| 63 | EXPECT_EQ(0, WebRtcOpus_DecoderCreate(opus_decoder, channels)); |
| 64 | } else if (channels == 1) { |
| 65 | EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(opus_decoder, channels, |
| 66 | kMonoCoupledStreams, |
| 67 | kMonoChannelMapping)); |
| 68 | } else if (channels == 2) { |
| 69 | EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(opus_decoder, channels, |
| 70 | kStereoCoupledStreams, |
| 71 | kStereoChannelMapping)); |
| 72 | } else if (channels == 4) { |
| 73 | EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(opus_decoder, channels, |
| 74 | kQuadCoupledStreams, |
| 75 | kQuadChannelMapping)); |
| 76 | } else { |
| 77 | EXPECT_TRUE(false) << channels; |
| 78 | } |
| 79 | } |
| 80 | } // namespace |
| 81 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 82 | using test::AudioLoop; |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 83 | using ::testing::TestWithParam; |
| 84 | using ::testing::Values; |
| 85 | using ::testing::Combine; |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 86 | |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 87 | // Maximum number of bytes in output bitstream. |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 88 | const size_t kMaxBytes = 2000; |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 89 | // Sample rate of Opus. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 90 | const size_t kOpusRateKhz = 48; |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 91 | // Number of samples-per-channel in a 20 ms frame, sampled at 48 kHz. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 92 | const size_t kOpus20msFrameSamples = kOpusRateKhz * 20; |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 93 | // Number of samples-per-channel in a 10 ms frame, sampled at 48 kHz. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 94 | const size_t kOpus10msFrameSamples = kOpusRateKhz * 10; |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 95 | |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 96 | class OpusTest : public TestWithParam<::testing::tuple<int, int, bool>> { |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 97 | protected: |
| 98 | OpusTest(); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 99 | |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 100 | void TestDtxEffect(bool dtx, int block_length_ms); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 101 | |
soren | 28dc285 | 2017-04-06 05:48:36 -0700 | [diff] [blame] | 102 | void TestCbrEffect(bool dtx, int block_length_ms); |
| 103 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 104 | // Prepare |speech_data_| for encoding, read from a hard-coded file. |
| 105 | // After preparation, |speech_data_.GetNextBlock()| returns a pointer to a |
| 106 | // block of |block_length_ms| milliseconds. The data is looped every |
| 107 | // |loop_length_ms| milliseconds. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 108 | void PrepareSpeechData(size_t channel, |
| 109 | int block_length_ms, |
| 110 | int loop_length_ms); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 111 | |
| 112 | int EncodeDecode(WebRtcOpusEncInst* encoder, |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 113 | rtc::ArrayView<const int16_t> input_audio, |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 114 | WebRtcOpusDecInst* decoder, |
| 115 | int16_t* output_audio, |
| 116 | int16_t* audio_type); |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 117 | |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 118 | void SetMaxPlaybackRate(WebRtcOpusEncInst* encoder, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 119 | opus_int32 expect, |
| 120 | int32_t set); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 121 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 122 | void CheckAudioBounded(const int16_t* audio, |
| 123 | size_t samples, |
| 124 | size_t channels, |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 125 | uint16_t bound) const; |
| 126 | |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 127 | WebRtcOpusEncInst* opus_encoder_; |
| 128 | WebRtcOpusDecInst* opus_decoder_; |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 129 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 130 | AudioLoop speech_data_; |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 131 | uint8_t bitstream_[kMaxBytes]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 132 | size_t encoded_bytes_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 133 | size_t channels_; |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 134 | int application_; |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 135 | bool force_multistream_; |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | OpusTest::OpusTest() |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 139 | : opus_encoder_(NULL), |
| 140 | opus_decoder_(NULL), |
| 141 | encoded_bytes_(0), |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 142 | channels_(static_cast<size_t>(::testing::get<0>(GetParam()))), |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 143 | application_(::testing::get<1>(GetParam())), |
| 144 | force_multistream_(::testing::get<2>(GetParam())) {} |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 145 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 146 | void OpusTest::PrepareSpeechData(size_t channel, |
| 147 | int block_length_ms, |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 148 | int loop_length_ms) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 149 | std::map<int, std::string> channel_to_basename = { |
| 150 | {1, "audio_coding/testfile32kHz"}, |
| 151 | {2, "audio_coding/teststereo32kHz"}, |
| 152 | {4, "audio_coding/speech_4_channels_48k_one_second"}}; |
| 153 | std::map<int, std::string> channel_to_suffix = { |
| 154 | {1, "pcm"}, {2, "pcm"}, {4, "wav"}}; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 155 | const std::string file_name = webrtc::test::ResourcePath( |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 156 | channel_to_basename[channel], channel_to_suffix[channel]); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 157 | if (loop_length_ms < block_length_ms) { |
| 158 | loop_length_ms = block_length_ms; |
| 159 | } |
| 160 | EXPECT_TRUE(speech_data_.Init(file_name, |
| 161 | loop_length_ms * kOpusRateKhz * channel, |
| 162 | block_length_ms * kOpusRateKhz * channel)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 163 | } |
| 164 | |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 165 | void OpusTest::SetMaxPlaybackRate(WebRtcOpusEncInst* encoder, |
| 166 | opus_int32 expect, |
| 167 | int32_t set) { |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 168 | opus_int32 bandwidth; |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 169 | EXPECT_EQ(0, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, set)); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 170 | EXPECT_EQ(0, WebRtcOpus_GetMaxPlaybackRate(opus_encoder_, &bandwidth)); |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 171 | EXPECT_EQ(expect, bandwidth); |
| 172 | } |
| 173 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 174 | void OpusTest::CheckAudioBounded(const int16_t* audio, |
| 175 | size_t samples, |
| 176 | size_t channels, |
| 177 | uint16_t bound) const { |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 178 | for (size_t i = 0; i < samples; ++i) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 179 | for (size_t c = 0; c < channels; ++c) { |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 180 | ASSERT_GE(audio[i * channels + c], -bound); |
| 181 | ASSERT_LE(audio[i * channels + c], bound); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 186 | int OpusTest::EncodeDecode(WebRtcOpusEncInst* encoder, |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 187 | rtc::ArrayView<const int16_t> input_audio, |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 188 | WebRtcOpusDecInst* decoder, |
| 189 | int16_t* output_audio, |
| 190 | int16_t* audio_type) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 191 | int encoded_bytes_int = |
| 192 | WebRtcOpus_Encode(encoder, input_audio.data(), |
| 193 | rtc::CheckedDivExact(input_audio.size(), channels_), |
| 194 | kMaxBytes, bitstream_); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 195 | EXPECT_GE(encoded_bytes_int, 0); |
| 196 | encoded_bytes_ = static_cast<size_t>(encoded_bytes_int); |
minyuel | 6d92bf5 | 2015-09-23 15:20:39 +0200 | [diff] [blame] | 197 | int est_len = WebRtcOpus_DurationEst(decoder, bitstream_, encoded_bytes_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 198 | int act_len = WebRtcOpus_Decode(decoder, bitstream_, encoded_bytes_, |
| 199 | output_audio, audio_type); |
minyuel | 6d92bf5 | 2015-09-23 15:20:39 +0200 | [diff] [blame] | 200 | EXPECT_EQ(est_len, act_len); |
| 201 | return act_len; |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // Test if encoder/decoder can enter DTX mode properly and do not enter DTX when |
| 205 | // they should not. This test is signal dependent. |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 206 | void OpusTest::TestDtxEffect(bool dtx, int block_length_ms) { |
| 207 | PrepareSpeechData(channels_, block_length_ms, 2000); |
| 208 | const size_t samples = kOpusRateKhz * block_length_ms; |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 209 | |
| 210 | // Create encoder memory. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 211 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 212 | force_multistream_); |
| 213 | CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, |
| 214 | force_multistream_); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 215 | |
| 216 | // Set bitrate. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 217 | EXPECT_EQ( |
| 218 | 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 219 | |
| 220 | // Set input audio as silence. |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 221 | std::vector<int16_t> silence(samples * channels_, 0); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 222 | |
| 223 | // Setting DTX. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 224 | EXPECT_EQ(0, dtx ? WebRtcOpus_EnableDtx(opus_encoder_) |
| 225 | : WebRtcOpus_DisableDtx(opus_encoder_)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 226 | |
| 227 | int16_t audio_type; |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 228 | int16_t* output_data_decode = new int16_t[samples * channels_]; |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 229 | |
| 230 | for (int i = 0; i < 100; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 231 | EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode( |
| 232 | opus_encoder_, speech_data_.GetNextBlock(), |
| 233 | opus_decoder_, output_data_decode, &audio_type))); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 234 | // If not DTX, it should never enter DTX mode. If DTX, we do not care since |
| 235 | // whether it enters DTX depends on the signal type. |
| 236 | if (!dtx) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 237 | EXPECT_GT(encoded_bytes_, 1U); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 238 | EXPECT_EQ(0, opus_encoder_->in_dtx_mode); |
| 239 | EXPECT_EQ(0, opus_decoder_->in_dtx_mode); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 240 | EXPECT_EQ(0, audio_type); // Speech. |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // We input some silent segments. In DTX mode, the encoder will stop sending. |
| 245 | // However, DTX may happen after a while. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 246 | for (int i = 0; i < 30; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 247 | EXPECT_EQ(samples, static_cast<size_t>( |
| 248 | EncodeDecode(opus_encoder_, silence, opus_decoder_, |
| 249 | output_data_decode, &audio_type))); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 250 | if (!dtx) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 251 | EXPECT_GT(encoded_bytes_, 1U); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 252 | EXPECT_EQ(0, opus_encoder_->in_dtx_mode); |
| 253 | EXPECT_EQ(0, opus_decoder_->in_dtx_mode); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 254 | EXPECT_EQ(0, audio_type); // Speech. |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 255 | } else if (encoded_bytes_ == 1) { |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 256 | EXPECT_EQ(1, opus_encoder_->in_dtx_mode); |
| 257 | EXPECT_EQ(1, opus_decoder_->in_dtx_mode); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 258 | EXPECT_EQ(2, audio_type); // Comfort noise. |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 263 | // When Opus is in DTX, it wakes up in a regular basis. It sends two packets, |
| 264 | // one with an arbitrary size and the other of 1-byte, then stops sending for |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 265 | // a certain number of frames. |
| 266 | |
| 267 | // |max_dtx_frames| is the maximum number of frames Opus can stay in DTX. |
| 268 | const int max_dtx_frames = 400 / block_length_ms + 1; |
| 269 | |
| 270 | // We run |kRunTimeMs| milliseconds of pure silence. |
minyue | 58e08cb | 2016-02-24 03:49:19 -0800 | [diff] [blame] | 271 | const int kRunTimeMs = 4500; |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 272 | |
| 273 | // We check that, after a |kCheckTimeMs| milliseconds (given that the CNG in |
| 274 | // Opus needs time to adapt), the absolute values of DTX decoded signal are |
| 275 | // bounded by |kOutputValueBound|. |
minyue | 58e08cb | 2016-02-24 03:49:19 -0800 | [diff] [blame] | 276 | const int kCheckTimeMs = 4000; |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 277 | |
| 278 | #if defined(OPUS_FIXED_POINT) |
minyuel | 7e937e9 | 2016-02-29 10:24:15 +0100 | [diff] [blame] | 279 | // Fixed-point Opus generates a random (comfort) noise, which has a less |
| 280 | // predictable value bound than its floating-point Opus. This value depends on |
| 281 | // input signal, and the time window for checking the output values (between |
| 282 | // |kCheckTimeMs| and |kRunTimeMs|). |
| 283 | const uint16_t kOutputValueBound = 30; |
| 284 | |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 285 | #else |
minyue | 58e08cb | 2016-02-24 03:49:19 -0800 | [diff] [blame] | 286 | const uint16_t kOutputValueBound = 2; |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 287 | #endif |
| 288 | |
| 289 | int time = 0; |
| 290 | while (time < kRunTimeMs) { |
| 291 | // DTX mode is maintained for maximum |max_dtx_frames| frames. |
| 292 | int i = 0; |
| 293 | for (; i < max_dtx_frames; ++i) { |
| 294 | time += block_length_ms; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 295 | EXPECT_EQ(samples, static_cast<size_t>( |
| 296 | EncodeDecode(opus_encoder_, silence, opus_decoder_, |
| 297 | output_data_decode, &audio_type))); |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 298 | if (dtx) { |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 299 | if (encoded_bytes_ > 1) |
| 300 | break; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 301 | EXPECT_EQ(0U, encoded_bytes_) // Send 0 byte. |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 302 | << "Opus should have entered DTX mode."; |
| 303 | EXPECT_EQ(1, opus_encoder_->in_dtx_mode); |
| 304 | EXPECT_EQ(1, opus_decoder_->in_dtx_mode); |
| 305 | EXPECT_EQ(2, audio_type); // Comfort noise. |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 306 | if (time >= kCheckTimeMs) { |
| 307 | CheckAudioBounded(output_data_decode, samples, channels_, |
| 308 | kOutputValueBound); |
| 309 | } |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 310 | } else { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 311 | EXPECT_GT(encoded_bytes_, 1U); |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 312 | EXPECT_EQ(0, opus_encoder_->in_dtx_mode); |
| 313 | EXPECT_EQ(0, opus_decoder_->in_dtx_mode); |
| 314 | EXPECT_EQ(0, audio_type); // Speech. |
| 315 | } |
| 316 | } |
| 317 | |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 318 | if (dtx) { |
| 319 | // With DTX, Opus must stop transmission for some time. |
| 320 | EXPECT_GT(i, 1); |
| 321 | } |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 322 | |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 323 | // We expect a normal payload. |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 324 | EXPECT_EQ(0, opus_encoder_->in_dtx_mode); |
| 325 | EXPECT_EQ(0, opus_decoder_->in_dtx_mode); |
| 326 | EXPECT_EQ(0, audio_type); // Speech. |
| 327 | |
| 328 | // Enters DTX again immediately. |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 329 | time += block_length_ms; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 330 | EXPECT_EQ(samples, static_cast<size_t>( |
| 331 | EncodeDecode(opus_encoder_, silence, opus_decoder_, |
| 332 | output_data_decode, &audio_type))); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 333 | if (dtx) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 334 | EXPECT_EQ(1U, encoded_bytes_); // Send 1 byte. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 335 | EXPECT_EQ(1, opus_encoder_->in_dtx_mode); |
| 336 | EXPECT_EQ(1, opus_decoder_->in_dtx_mode); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 337 | EXPECT_EQ(2, audio_type); // Comfort noise. |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 338 | if (time >= kCheckTimeMs) { |
| 339 | CheckAudioBounded(output_data_decode, samples, channels_, |
| 340 | kOutputValueBound); |
| 341 | } |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 342 | } else { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 343 | EXPECT_GT(encoded_bytes_, 1U); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 344 | EXPECT_EQ(0, opus_encoder_->in_dtx_mode); |
| 345 | EXPECT_EQ(0, opus_decoder_->in_dtx_mode); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 346 | EXPECT_EQ(0, audio_type); // Speech. |
| 347 | } |
| 348 | } |
| 349 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 350 | silence[0] = 10000; |
| 351 | if (dtx) { |
| 352 | // Verify that encoder/decoder can jump out from DTX mode. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 353 | EXPECT_EQ(samples, static_cast<size_t>( |
| 354 | EncodeDecode(opus_encoder_, silence, opus_decoder_, |
| 355 | output_data_decode, &audio_type))); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 356 | EXPECT_GT(encoded_bytes_, 1U); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 357 | EXPECT_EQ(0, opus_encoder_->in_dtx_mode); |
| 358 | EXPECT_EQ(0, opus_decoder_->in_dtx_mode); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 359 | EXPECT_EQ(0, audio_type); // Speech. |
| 360 | } |
| 361 | |
| 362 | // Free memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 363 | delete[] output_data_decode; |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 364 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| 365 | EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 366 | } |
| 367 | |
soren | 28dc285 | 2017-04-06 05:48:36 -0700 | [diff] [blame] | 368 | // Test if CBR does what we expect. |
| 369 | void OpusTest::TestCbrEffect(bool cbr, int block_length_ms) { |
| 370 | PrepareSpeechData(channels_, block_length_ms, 2000); |
| 371 | const size_t samples = kOpusRateKhz * block_length_ms; |
| 372 | |
| 373 | int32_t max_pkt_size_diff = 0; |
| 374 | int32_t prev_pkt_size = 0; |
| 375 | |
| 376 | // Create encoder memory. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 377 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 378 | force_multistream_); |
| 379 | CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, |
| 380 | force_multistream_); |
soren | 28dc285 | 2017-04-06 05:48:36 -0700 | [diff] [blame] | 381 | |
| 382 | // Set bitrate. |
| 383 | EXPECT_EQ( |
| 384 | 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000)); |
| 385 | |
| 386 | // Setting CBR. |
| 387 | EXPECT_EQ(0, cbr ? WebRtcOpus_EnableCbr(opus_encoder_) |
| 388 | : WebRtcOpus_DisableCbr(opus_encoder_)); |
| 389 | |
| 390 | int16_t audio_type; |
| 391 | std::vector<int16_t> audio_out(samples * channels_); |
| 392 | for (int i = 0; i < 100; ++i) { |
| 393 | EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode( |
| 394 | opus_encoder_, speech_data_.GetNextBlock(), |
| 395 | opus_decoder_, audio_out.data(), &audio_type))); |
| 396 | |
| 397 | if (prev_pkt_size > 0) { |
| 398 | int32_t diff = std::abs((int32_t)encoded_bytes_ - prev_pkt_size); |
| 399 | max_pkt_size_diff = std::max(max_pkt_size_diff, diff); |
| 400 | } |
Mirko Bonadei | 737e073 | 2017-10-19 09:00:17 +0200 | [diff] [blame] | 401 | prev_pkt_size = rtc::checked_cast<int32_t>(encoded_bytes_); |
soren | 28dc285 | 2017-04-06 05:48:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | if (cbr) { |
| 405 | EXPECT_EQ(max_pkt_size_diff, 0); |
| 406 | } else { |
| 407 | EXPECT_GT(max_pkt_size_diff, 0); |
| 408 | } |
| 409 | |
| 410 | // Free memory. |
| 411 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| 412 | EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); |
| 413 | } |
| 414 | |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 415 | // Test failing Create. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 416 | TEST(OpusTest, OpusCreateFail) { |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 417 | WebRtcOpusEncInst* opus_encoder; |
| 418 | WebRtcOpusDecInst* opus_decoder; |
| 419 | |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 420 | // Test to see that an invalid pointer is caught. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 421 | EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(NULL, 1, 0)); |
| 422 | // Invalid channel number. |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 423 | EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 257, 0)); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 424 | // Invalid applciation mode. |
| 425 | EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 1, 2)); |
| 426 | |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 427 | EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(NULL, 1)); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 428 | // Invalid channel number. |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 429 | EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(&opus_decoder, 257)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // Test failing Free. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 433 | TEST(OpusTest, OpusFreeFail) { |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 434 | // Test to see that an invalid pointer is caught. |
| 435 | EXPECT_EQ(-1, WebRtcOpus_EncoderFree(NULL)); |
| 436 | EXPECT_EQ(-1, WebRtcOpus_DecoderFree(NULL)); |
| 437 | } |
| 438 | |
| 439 | // Test normal Create and Free. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 440 | TEST_P(OpusTest, OpusCreateFree) { |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 441 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 442 | force_multistream_); |
| 443 | CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, |
| 444 | force_multistream_); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 445 | EXPECT_TRUE(opus_encoder_ != NULL); |
| 446 | EXPECT_TRUE(opus_decoder_ != NULL); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 447 | // Free encoder and decoder memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 448 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| 449 | EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 452 | #define ENCODER_CTL(inst, vargs) \ |
| 453 | inst->encoder \ |
| 454 | ? opus_encoder_ctl(inst->encoder, vargs) \ |
| 455 | : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs) |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 456 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 457 | TEST_P(OpusTest, OpusEncodeDecode) { |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 458 | PrepareSpeechData(channels_, 20, 20); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 459 | |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 460 | // Create encoder memory. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 461 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 462 | force_multistream_); |
| 463 | CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, |
| 464 | force_multistream_); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 465 | |
| 466 | // Set bitrate. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 467 | EXPECT_EQ( |
| 468 | 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 469 | |
| 470 | // Check number of channels for decoder. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 471 | EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_)); |
| 472 | |
| 473 | // Check application mode. |
| 474 | opus_int32 app; |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 475 | ENCODER_CTL(opus_encoder_, OPUS_GET_APPLICATION(&app)); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 476 | EXPECT_EQ(application_ == 0 ? OPUS_APPLICATION_VOIP : OPUS_APPLICATION_AUDIO, |
| 477 | app); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 478 | |
| 479 | // Encode & decode. |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 480 | int16_t audio_type; |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 481 | int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_]; |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 482 | EXPECT_EQ(kOpus20msFrameSamples, |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 483 | static_cast<size_t>( |
| 484 | EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), |
| 485 | opus_decoder_, output_data_decode, &audio_type))); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 486 | |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 487 | // Free memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 488 | delete[] output_data_decode; |
| 489 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| 490 | EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 491 | } |
| 492 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 493 | TEST_P(OpusTest, OpusSetBitRate) { |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 494 | // Test without creating encoder memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 495 | EXPECT_EQ(-1, WebRtcOpus_SetBitRate(opus_encoder_, 60000)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 496 | |
| 497 | // Create encoder memory, try with different bitrates. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 498 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 499 | force_multistream_); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 500 | EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 30000)); |
| 501 | EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 60000)); |
| 502 | EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 300000)); |
| 503 | EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 600000)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 504 | |
| 505 | // Free memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 506 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 507 | } |
| 508 | |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 509 | TEST_P(OpusTest, OpusSetComplexity) { |
minyue@webrtc.org | 0454688 | 2014-03-07 08:55:48 +0000 | [diff] [blame] | 510 | // Test without creating encoder memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 511 | EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 9)); |
minyue@webrtc.org | 0454688 | 2014-03-07 08:55:48 +0000 | [diff] [blame] | 512 | |
| 513 | // Create encoder memory, try with different complexities. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 514 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 515 | force_multistream_); |
minyue@webrtc.org | 0454688 | 2014-03-07 08:55:48 +0000 | [diff] [blame] | 516 | |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 517 | EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 0)); |
| 518 | EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 10)); |
| 519 | EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 11)); |
minyue@webrtc.org | 0454688 | 2014-03-07 08:55:48 +0000 | [diff] [blame] | 520 | |
| 521 | // Free memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 522 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
minyue@webrtc.org | 0454688 | 2014-03-07 08:55:48 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 525 | TEST_P(OpusTest, OpusSetBandwidth) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 526 | if (channels_ > 2) { |
| 527 | // TODO(webrtc:10217): investigate why multi-stream Opus reports |
| 528 | // narrowband when it's configured with FULLBAND. |
| 529 | return; |
| 530 | } |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 531 | PrepareSpeechData(channels_, 20, 20); |
| 532 | |
| 533 | int16_t audio_type; |
| 534 | std::unique_ptr<int16_t[]> output_data_decode( |
| 535 | new int16_t[kOpus20msFrameSamples * channels_]()); |
| 536 | |
| 537 | // Test without creating encoder memory. |
| 538 | EXPECT_EQ(-1, |
| 539 | WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND)); |
| 540 | EXPECT_EQ(-1, WebRtcOpus_GetBandwidth(opus_encoder_)); |
| 541 | |
| 542 | // Create encoder memory, try with different bandwidths. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 543 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 544 | force_multistream_); |
| 545 | CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, |
| 546 | force_multistream_); |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 547 | |
| 548 | EXPECT_EQ(-1, WebRtcOpus_SetBandwidth(opus_encoder_, |
| 549 | OPUS_BANDWIDTH_NARROWBAND - 1)); |
| 550 | EXPECT_EQ(0, |
| 551 | WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND)); |
| 552 | EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_, |
| 553 | output_data_decode.get(), &audio_type); |
| 554 | EXPECT_EQ(OPUS_BANDWIDTH_NARROWBAND, WebRtcOpus_GetBandwidth(opus_encoder_)); |
| 555 | EXPECT_EQ(0, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND)); |
| 556 | EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_, |
| 557 | output_data_decode.get(), &audio_type); |
| 558 | EXPECT_EQ(OPUS_BANDWIDTH_FULLBAND, WebRtcOpus_GetBandwidth(opus_encoder_)); |
| 559 | EXPECT_EQ( |
| 560 | -1, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND + 1)); |
| 561 | EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_, |
| 562 | output_data_decode.get(), &audio_type); |
| 563 | EXPECT_EQ(OPUS_BANDWIDTH_FULLBAND, WebRtcOpus_GetBandwidth(opus_encoder_)); |
| 564 | |
| 565 | // Free memory. |
| 566 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| 567 | EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); |
| 568 | } |
| 569 | |
minyue | c8299f9 | 2016-09-27 02:08:47 -0700 | [diff] [blame] | 570 | TEST_P(OpusTest, OpusForceChannels) { |
| 571 | // Test without creating encoder memory. |
| 572 | EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 1)); |
| 573 | |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 574 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 575 | force_multistream_); |
| 576 | ASSERT_NE(nullptr, opus_encoder_); |
minyue | c8299f9 | 2016-09-27 02:08:47 -0700 | [diff] [blame] | 577 | |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 578 | if (channels_ >= 2) { |
minyue | c8299f9 | 2016-09-27 02:08:47 -0700 | [diff] [blame] | 579 | EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 3)); |
| 580 | EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 2)); |
| 581 | EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1)); |
| 582 | EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0)); |
| 583 | } else { |
| 584 | EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 2)); |
| 585 | EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1)); |
| 586 | EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0)); |
| 587 | } |
| 588 | |
| 589 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| 590 | } |
| 591 | |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 592 | // Encode and decode one frame, initialize the decoder and |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 593 | // decode once more. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 594 | TEST_P(OpusTest, OpusDecodeInit) { |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 595 | PrepareSpeechData(channels_, 20, 20); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 596 | |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 597 | // Create encoder memory. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 598 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 599 | force_multistream_); |
| 600 | CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, |
| 601 | force_multistream_); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 602 | |
| 603 | // Encode & decode. |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 604 | int16_t audio_type; |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 605 | int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_]; |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 606 | EXPECT_EQ(kOpus20msFrameSamples, |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 607 | static_cast<size_t>( |
| 608 | EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), |
| 609 | opus_decoder_, output_data_decode, &audio_type))); |
minyue@webrtc.org | 52bc4f4 | 2014-12-04 11:00:50 +0000 | [diff] [blame] | 610 | |
Karl Wiberg | 4376648 | 2015-08-27 15:22:11 +0200 | [diff] [blame] | 611 | WebRtcOpus_DecoderInit(opus_decoder_); |
minyue@webrtc.org | 52bc4f4 | 2014-12-04 11:00:50 +0000 | [diff] [blame] | 612 | |
| 613 | EXPECT_EQ(kOpus20msFrameSamples, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 614 | static_cast<size_t>( |
| 615 | WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_, |
| 616 | output_data_decode, &audio_type))); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 617 | |
| 618 | // Free memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 619 | delete[] output_data_decode; |
| 620 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| 621 | EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 622 | } |
| 623 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 624 | TEST_P(OpusTest, OpusEnableDisableFec) { |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 625 | // Test without creating encoder memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 626 | EXPECT_EQ(-1, WebRtcOpus_EnableFec(opus_encoder_)); |
| 627 | EXPECT_EQ(-1, WebRtcOpus_DisableFec(opus_encoder_)); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 628 | |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 629 | // Create encoder memory. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 630 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 631 | force_multistream_); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 632 | |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 633 | EXPECT_EQ(0, WebRtcOpus_EnableFec(opus_encoder_)); |
| 634 | EXPECT_EQ(0, WebRtcOpus_DisableFec(opus_encoder_)); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 635 | |
| 636 | // Free memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 637 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 638 | } |
| 639 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 640 | TEST_P(OpusTest, OpusEnableDisableDtx) { |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 641 | // Test without creating encoder memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 642 | EXPECT_EQ(-1, WebRtcOpus_EnableDtx(opus_encoder_)); |
| 643 | EXPECT_EQ(-1, WebRtcOpus_DisableDtx(opus_encoder_)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 644 | |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 645 | // Create encoder memory. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 646 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 647 | force_multistream_); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 648 | |
| 649 | opus_int32 dtx; |
| 650 | |
| 651 | // DTX is off by default. |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 652 | ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 653 | EXPECT_EQ(0, dtx); |
| 654 | |
| 655 | // Test to enable DTX. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 656 | EXPECT_EQ(0, WebRtcOpus_EnableDtx(opus_encoder_)); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 657 | ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 658 | EXPECT_EQ(1, dtx); |
| 659 | |
| 660 | // Test to disable DTX. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 661 | EXPECT_EQ(0, WebRtcOpus_DisableDtx(opus_encoder_)); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 662 | ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 663 | EXPECT_EQ(0, dtx); |
| 664 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 665 | // Free memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 666 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 667 | } |
| 668 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 669 | TEST_P(OpusTest, OpusDtxOff) { |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 670 | TestDtxEffect(false, 10); |
| 671 | TestDtxEffect(false, 20); |
| 672 | TestDtxEffect(false, 40); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 673 | } |
| 674 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 675 | TEST_P(OpusTest, OpusDtxOn) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 676 | if (channels_ > 2) { |
| 677 | // TODO(webrtc:10218): adapt the test to the sizes and order of multi-stream |
| 678 | // DTX packets. |
| 679 | return; |
| 680 | } |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 681 | TestDtxEffect(true, 10); |
| 682 | TestDtxEffect(true, 20); |
| 683 | TestDtxEffect(true, 40); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 684 | } |
| 685 | |
soren | 28dc285 | 2017-04-06 05:48:36 -0700 | [diff] [blame] | 686 | TEST_P(OpusTest, OpusCbrOff) { |
| 687 | TestCbrEffect(false, 10); |
| 688 | TestCbrEffect(false, 20); |
| 689 | TestCbrEffect(false, 40); |
| 690 | } |
| 691 | |
| 692 | TEST_P(OpusTest, OpusCbrOn) { |
| 693 | TestCbrEffect(true, 10); |
| 694 | TestCbrEffect(true, 20); |
| 695 | TestCbrEffect(true, 40); |
| 696 | } |
| 697 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 698 | TEST_P(OpusTest, OpusSetPacketLossRate) { |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 699 | // Test without creating encoder memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 700 | EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 701 | |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 702 | // Create encoder memory. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 703 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 704 | force_multistream_); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 705 | |
| 706 | EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50)); |
| 707 | EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, -1)); |
| 708 | EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 101)); |
| 709 | |
| 710 | // Free memory. |
| 711 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| 712 | } |
| 713 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 714 | TEST_P(OpusTest, OpusSetMaxPlaybackRate) { |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 715 | // Test without creating encoder memory. |
| 716 | EXPECT_EQ(-1, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, 20000)); |
| 717 | |
| 718 | // Create encoder memory. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 719 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 720 | force_multistream_); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 721 | |
| 722 | SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 48000); |
| 723 | SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 24001); |
| 724 | SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 24000); |
| 725 | SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 16001); |
| 726 | SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 16000); |
| 727 | SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 12001); |
| 728 | SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 12000); |
| 729 | SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 8001); |
| 730 | SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 8000); |
| 731 | SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 4000); |
| 732 | |
| 733 | // Free memory. |
| 734 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| 735 | } |
| 736 | |
| 737 | // Test PLC. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 738 | TEST_P(OpusTest, OpusDecodePlc) { |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 739 | PrepareSpeechData(channels_, 20, 20); |
| 740 | |
| 741 | // Create encoder memory. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 742 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 743 | force_multistream_); |
| 744 | CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, |
| 745 | force_multistream_); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 746 | |
| 747 | // Set bitrate. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 748 | EXPECT_EQ( |
| 749 | 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000)); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 750 | |
| 751 | // Check number of channels for decoder. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 752 | EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_)); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 753 | |
| 754 | // Encode & decode. |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 755 | int16_t audio_type; |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 756 | int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_]; |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 757 | EXPECT_EQ(kOpus20msFrameSamples, |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 758 | static_cast<size_t>( |
| 759 | EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), |
| 760 | opus_decoder_, output_data_decode, &audio_type))); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 761 | |
minyue@webrtc.org | 33ccdfa | 2014-12-04 12:14:12 +0000 | [diff] [blame] | 762 | // Call decoder PLC. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 763 | int16_t* plc_buffer = new int16_t[kOpus20msFrameSamples * channels_]; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 764 | EXPECT_EQ(kOpus20msFrameSamples, static_cast<size_t>(WebRtcOpus_DecodePlc( |
| 765 | opus_decoder_, plc_buffer, 1))); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 766 | |
| 767 | // Free memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 768 | delete[] plc_buffer; |
| 769 | delete[] output_data_decode; |
| 770 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| 771 | EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | // Duration estimation. |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 775 | TEST_P(OpusTest, OpusDurationEstimation) { |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 776 | PrepareSpeechData(channels_, 20, 20); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 777 | |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 778 | // Create. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 779 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 780 | force_multistream_); |
| 781 | CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, |
| 782 | force_multistream_); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 783 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 784 | // 10 ms. We use only first 10 ms of a 20 ms block. |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 785 | auto speech_block = speech_data_.GetNextBlock(); |
| 786 | int encoded_bytes_int = WebRtcOpus_Encode( |
| 787 | opus_encoder_, speech_block.data(), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 788 | rtc::CheckedDivExact(speech_block.size(), 2 * channels_), kMaxBytes, |
| 789 | bitstream_); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 790 | EXPECT_GE(encoded_bytes_int, 0); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 791 | EXPECT_EQ( |
| 792 | kOpus10msFrameSamples, |
| 793 | static_cast<size_t>(WebRtcOpus_DurationEst( |
| 794 | opus_decoder_, bitstream_, static_cast<size_t>(encoded_bytes_int)))); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 795 | |
| 796 | // 20 ms |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 797 | speech_block = speech_data_.GetNextBlock(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 798 | encoded_bytes_int = |
| 799 | WebRtcOpus_Encode(opus_encoder_, speech_block.data(), |
| 800 | rtc::CheckedDivExact(speech_block.size(), channels_), |
| 801 | kMaxBytes, bitstream_); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 802 | EXPECT_GE(encoded_bytes_int, 0); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 803 | EXPECT_EQ( |
| 804 | kOpus20msFrameSamples, |
| 805 | static_cast<size_t>(WebRtcOpus_DurationEst( |
| 806 | opus_decoder_, bitstream_, static_cast<size_t>(encoded_bytes_int)))); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 807 | |
| 808 | // Free memory. |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 809 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| 810 | EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 811 | } |
| 812 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 813 | TEST_P(OpusTest, OpusDecodeRepacketized) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 814 | if (channels_ > 2) { |
| 815 | // As per the Opus documentation |
| 816 | // https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/group__opus__repacketizer.html#details, |
| 817 | // multiple streams are not supported. |
| 818 | return; |
| 819 | } |
minyue | a613eb6 | 2017-03-14 14:33:30 -0700 | [diff] [blame] | 820 | constexpr size_t kPackets = 6; |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 821 | |
| 822 | PrepareSpeechData(channels_, 20, 20 * kPackets); |
| 823 | |
| 824 | // Create encoder memory. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 825 | CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_, |
| 826 | force_multistream_); |
| 827 | ASSERT_NE(nullptr, opus_encoder_); |
| 828 | CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, |
| 829 | force_multistream_); |
| 830 | ASSERT_NE(nullptr, opus_decoder_); |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 831 | |
| 832 | // Set bitrate. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 833 | EXPECT_EQ( |
| 834 | 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000)); |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 835 | |
| 836 | // Check number of channels for decoder. |
| 837 | EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_)); |
| 838 | |
| 839 | // Encode & decode. |
| 840 | int16_t audio_type; |
kwiberg | 91d9756 | 2016-02-14 01:10:03 -0800 | [diff] [blame] | 841 | std::unique_ptr<int16_t[]> output_data_decode( |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 842 | new int16_t[kPackets * kOpus20msFrameSamples * channels_]); |
| 843 | OpusRepacketizer* rp = opus_repacketizer_create(); |
| 844 | |
minyue | a613eb6 | 2017-03-14 14:33:30 -0700 | [diff] [blame] | 845 | size_t num_packets = 0; |
| 846 | constexpr size_t kMaxCycles = 100; |
| 847 | for (size_t idx = 0; idx < kMaxCycles; ++idx) { |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 848 | auto speech_block = speech_data_.GetNextBlock(); |
| 849 | encoded_bytes_ = |
| 850 | WebRtcOpus_Encode(opus_encoder_, speech_block.data(), |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 851 | rtc::CheckedDivExact(speech_block.size(), channels_), |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 852 | kMaxBytes, bitstream_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 853 | if (opus_repacketizer_cat(rp, bitstream_, |
| 854 | rtc::checked_cast<opus_int32>(encoded_bytes_)) == |
| 855 | OPUS_OK) { |
minyue | a613eb6 | 2017-03-14 14:33:30 -0700 | [diff] [blame] | 856 | ++num_packets; |
| 857 | if (num_packets == kPackets) { |
| 858 | break; |
| 859 | } |
| 860 | } else { |
| 861 | // Opus repacketizer cannot guarantee a success. We try again if it fails. |
| 862 | opus_repacketizer_init(rp); |
| 863 | num_packets = 0; |
| 864 | } |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 865 | } |
minyue | a613eb6 | 2017-03-14 14:33:30 -0700 | [diff] [blame] | 866 | EXPECT_EQ(kPackets, num_packets); |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 867 | |
| 868 | encoded_bytes_ = opus_repacketizer_out(rp, bitstream_, kMaxBytes); |
| 869 | |
| 870 | EXPECT_EQ(kOpus20msFrameSamples * kPackets, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 871 | static_cast<size_t>(WebRtcOpus_DurationEst( |
| 872 | opus_decoder_, bitstream_, encoded_bytes_))); |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 873 | |
| 874 | EXPECT_EQ(kOpus20msFrameSamples * kPackets, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 875 | static_cast<size_t>( |
| 876 | WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_, |
| 877 | output_data_decode.get(), &audio_type))); |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 878 | |
| 879 | // Free memory. |
| 880 | opus_repacketizer_destroy(rp); |
| 881 | EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); |
| 882 | EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); |
| 883 | } |
| 884 | |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 885 | INSTANTIATE_TEST_SUITE_P(VariousMode, |
| 886 | OpusTest, |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 887 | ::testing::ValuesIn({ |
| 888 | std::make_tuple(1, 0, true), |
| 889 | std::make_tuple(1, 1, true), |
| 890 | std::make_tuple(2, 0, false), |
| 891 | std::make_tuple(4, 0, false), |
| 892 | std::make_tuple(1, 1, false), |
| 893 | std::make_tuple(4, 1, false), |
| 894 | })); |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 895 | |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 896 | } // namespace webrtc |