blob: d0b240dfc6a6f8d41a854f554f5d88571165b185 [file] [log] [blame]
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +00001/*
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 */
kwiberg91d97562016-02-14 01:10:03 -080010
11#include <memory>
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000012#include <string>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#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 Wiberge40468b2017-11-22 10:42:26 +010018#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "test/testsupport/file_utils.h"
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000021
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000022namespace webrtc {
23
Alex Loiko50b8c392019-04-03 15:12:01 +020024namespace {
25// Equivalent to SDP params
26// {{"channel_mapping", "0,1,2,3"}, {"coupled_streams", "2"}}.
27constexpr unsigned char kQuadChannelMapping[] = {0, 1, 2, 3};
Alex Loikoe5b94162019-04-08 17:19:41 +020028constexpr int kQuadTotalStreams = 2;
Alex Loiko50b8c392019-04-03 15:12:01 +020029constexpr int kQuadCoupledStreams = 2;
30
31constexpr unsigned char kStereoChannelMapping[] = {0, 1};
Alex Loikoe5b94162019-04-08 17:19:41 +020032constexpr int kStereoTotalStreams = 1;
Alex Loiko50b8c392019-04-03 15:12:01 +020033constexpr int kStereoCoupledStreams = 1;
34
35constexpr unsigned char kMonoChannelMapping[] = {0};
Alex Loikoe5b94162019-04-08 17:19:41 +020036constexpr int kMonoTotalStreams = 1;
Alex Loiko50b8c392019-04-03 15:12:01 +020037constexpr int kMonoCoupledStreams = 0;
38
39void CreateSingleOrMultiStreamEncoder(WebRtcOpusEncInst** opus_encoder,
40 int channels,
41 int application,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020042 bool use_multistream,
43 int encoder_sample_rate_hz) {
44 EXPECT_TRUE(channels == 1 || channels == 2 || use_multistream);
45 if (use_multistream) {
46 EXPECT_EQ(encoder_sample_rate_hz, 48000);
47 if (channels == 1) {
48 EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
49 opus_encoder, channels, application, kMonoTotalStreams,
50 kMonoCoupledStreams, kMonoChannelMapping));
51 } else if (channels == 2) {
52 EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
53 opus_encoder, channels, application, kStereoTotalStreams,
54 kStereoCoupledStreams, kStereoChannelMapping));
55 } else if (channels == 4) {
56 EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
57 opus_encoder, channels, application, kQuadTotalStreams,
58 kQuadCoupledStreams, kQuadChannelMapping));
59 } else {
60 EXPECT_TRUE(false) << channels;
61 }
Alex Loiko50b8c392019-04-03 15:12:01 +020062 } else {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020063 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(opus_encoder, channels, application,
64 encoder_sample_rate_hz));
Alex Loiko50b8c392019-04-03 15:12:01 +020065 }
66}
67
68void CreateSingleOrMultiStreamDecoder(WebRtcOpusDecInst** opus_decoder,
69 int channels,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020070 bool use_multistream) {
71 EXPECT_TRUE(channels == 1 || channels == 2 || use_multistream);
72 if (use_multistream) {
73 if (channels == 1) {
74 EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(
75 opus_decoder, channels, kMonoTotalStreams,
76 kMonoCoupledStreams, kMonoChannelMapping));
77 } else if (channels == 2) {
78 EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(
79 opus_decoder, channels, kStereoTotalStreams,
80 kStereoCoupledStreams, kStereoChannelMapping));
81 } else if (channels == 4) {
82 EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(
83 opus_decoder, channels, kQuadTotalStreams,
84 kQuadCoupledStreams, kQuadChannelMapping));
85 } else {
86 EXPECT_TRUE(false) << channels;
87 }
Alex Loiko50b8c392019-04-03 15:12:01 +020088 } else {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020089 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(opus_decoder, channels));
Alex Loiko50b8c392019-04-03 15:12:01 +020090 }
91}
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020092
Alex Loiko50b8c392019-04-03 15:12:01 +020093} // namespace
94
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000095using test::AudioLoop;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000096using ::testing::TestWithParam;
97using ::testing::Values;
98using ::testing::Combine;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000099
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000100// Maximum number of bytes in output bitstream.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100101const size_t kMaxBytes = 2000;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000102// Sample rate of Opus.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200103const size_t kOpusDecodeRateKhz = 48;
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000104// Number of samples-per-channel in a 20 ms frame, sampled at 48 kHz.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200105const size_t kOpus20msFrameDecodeSamples = kOpusDecodeRateKhz * 20;
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000106// Number of samples-per-channel in a 10 ms frame, sampled at 48 kHz.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200107const size_t kOpus10msFrameDecodeSamples = kOpusDecodeRateKhz * 10;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000108
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200109class OpusTest : public TestWithParam<::testing::tuple<int, int, bool, int>> {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000110 protected:
111 OpusTest();
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000112
minyue3cea2562015-11-10 03:49:26 -0800113 void TestDtxEffect(bool dtx, int block_length_ms);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000114
soren28dc2852017-04-06 05:48:36 -0700115 void TestCbrEffect(bool dtx, int block_length_ms);
116
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000117 // Prepare |speech_data_| for encoding, read from a hard-coded file.
118 // After preparation, |speech_data_.GetNextBlock()| returns a pointer to a
119 // block of |block_length_ms| milliseconds. The data is looped every
120 // |loop_length_ms| milliseconds.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200121 void PrepareSpeechData(int block_length_ms, int loop_length_ms);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000122
123 int EncodeDecode(WebRtcOpusEncInst* encoder,
kwiberg288886b2015-11-06 01:21:35 -0800124 rtc::ArrayView<const int16_t> input_audio,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000125 WebRtcOpusDecInst* decoder,
126 int16_t* output_audio,
127 int16_t* audio_type);
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000128
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000129 void SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
Yves Gerey665174f2018-06-19 15:03:05 +0200130 opus_int32 expect,
131 int32_t set);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000132
Yves Gerey665174f2018-06-19 15:03:05 +0200133 void CheckAudioBounded(const int16_t* audio,
134 size_t samples,
135 size_t channels,
minyue3cea2562015-11-10 03:49:26 -0800136 uint16_t bound) const;
137
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000138 WebRtcOpusEncInst* opus_encoder_;
139 WebRtcOpusDecInst* opus_decoder_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000140
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000141 AudioLoop speech_data_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000142 uint8_t bitstream_[kMaxBytes];
Peter Kastingdce40cf2015-08-24 14:52:23 -0700143 size_t encoded_bytes_;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200144 const size_t channels_;
145 const int application_;
146 const bool use_multistream_;
147 const int encoder_sample_rate_hz_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000148};
149
150OpusTest::OpusTest()
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000151 : opus_encoder_(NULL),
152 opus_decoder_(NULL),
153 encoded_bytes_(0),
Peter Kasting69558702016-01-12 16:26:35 -0800154 channels_(static_cast<size_t>(::testing::get<0>(GetParam()))),
Alex Loiko50b8c392019-04-03 15:12:01 +0200155 application_(::testing::get<1>(GetParam())),
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200156 use_multistream_(::testing::get<2>(GetParam())),
157 encoder_sample_rate_hz_(::testing::get<3>(GetParam())) {}
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000158
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200159void OpusTest::PrepareSpeechData(int block_length_ms, int loop_length_ms) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100160 std::map<int, std::string> channel_to_basename = {
161 {1, "audio_coding/testfile32kHz"},
162 {2, "audio_coding/teststereo32kHz"},
163 {4, "audio_coding/speech_4_channels_48k_one_second"}};
164 std::map<int, std::string> channel_to_suffix = {
165 {1, "pcm"}, {2, "pcm"}, {4, "wav"}};
Yves Gerey665174f2018-06-19 15:03:05 +0200166 const std::string file_name = webrtc::test::ResourcePath(
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200167 channel_to_basename[channels_], channel_to_suffix[channels_]);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000168 if (loop_length_ms < block_length_ms) {
169 loop_length_ms = block_length_ms;
170 }
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200171 const int sample_rate_khz =
172 rtc::CheckedDivExact(encoder_sample_rate_hz_, 1000);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000173 EXPECT_TRUE(speech_data_.Init(file_name,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200174 loop_length_ms * sample_rate_khz * channels_,
175 block_length_ms * sample_rate_khz * channels_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000176}
177
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000178void OpusTest::SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
179 opus_int32 expect,
180 int32_t set) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000181 opus_int32 bandwidth;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000182 EXPECT_EQ(0, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, set));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100183 EXPECT_EQ(0, WebRtcOpus_GetMaxPlaybackRate(opus_encoder_, &bandwidth));
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000184 EXPECT_EQ(expect, bandwidth);
185}
186
Yves Gerey665174f2018-06-19 15:03:05 +0200187void OpusTest::CheckAudioBounded(const int16_t* audio,
188 size_t samples,
189 size_t channels,
190 uint16_t bound) const {
minyue3cea2562015-11-10 03:49:26 -0800191 for (size_t i = 0; i < samples; ++i) {
Peter Kasting69558702016-01-12 16:26:35 -0800192 for (size_t c = 0; c < channels; ++c) {
minyue3cea2562015-11-10 03:49:26 -0800193 ASSERT_GE(audio[i * channels + c], -bound);
194 ASSERT_LE(audio[i * channels + c], bound);
195 }
196 }
197}
198
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000199int OpusTest::EncodeDecode(WebRtcOpusEncInst* encoder,
kwiberg288886b2015-11-06 01:21:35 -0800200 rtc::ArrayView<const int16_t> input_audio,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000201 WebRtcOpusDecInst* decoder,
202 int16_t* output_audio,
203 int16_t* audio_type) {
Yves Gerey665174f2018-06-19 15:03:05 +0200204 int encoded_bytes_int =
205 WebRtcOpus_Encode(encoder, input_audio.data(),
206 rtc::CheckedDivExact(input_audio.size(), channels_),
207 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700208 EXPECT_GE(encoded_bytes_int, 0);
209 encoded_bytes_ = static_cast<size_t>(encoded_bytes_int);
minyuel6d92bf52015-09-23 15:20:39 +0200210 int est_len = WebRtcOpus_DurationEst(decoder, bitstream_, encoded_bytes_);
Yves Gerey665174f2018-06-19 15:03:05 +0200211 int act_len = WebRtcOpus_Decode(decoder, bitstream_, encoded_bytes_,
212 output_audio, audio_type);
minyuel6d92bf52015-09-23 15:20:39 +0200213 EXPECT_EQ(est_len, act_len);
214 return act_len;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000215}
216
217// Test if encoder/decoder can enter DTX mode properly and do not enter DTX when
218// they should not. This test is signal dependent.
minyue3cea2562015-11-10 03:49:26 -0800219void OpusTest::TestDtxEffect(bool dtx, int block_length_ms) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200220 PrepareSpeechData(block_length_ms, 2000);
221 const size_t input_samples =
222 rtc::CheckedDivExact(encoder_sample_rate_hz_, 1000) * block_length_ms;
223 const size_t output_samples = kOpusDecodeRateKhz * block_length_ms;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000224
225 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200226 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200227 use_multistream_, encoder_sample_rate_hz_);
228 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000229
230 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200231 EXPECT_EQ(
232 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000233
234 // Set input audio as silence.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200235 std::vector<int16_t> silence(input_samples * channels_, 0);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000236
237 // Setting DTX.
Yves Gerey665174f2018-06-19 15:03:05 +0200238 EXPECT_EQ(0, dtx ? WebRtcOpus_EnableDtx(opus_encoder_)
239 : WebRtcOpus_DisableDtx(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000240
241 int16_t audio_type;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200242 int16_t* output_data_decode = new int16_t[output_samples * channels_];
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000243
244 for (int i = 0; i < 100; ++i) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200245 EXPECT_EQ(output_samples,
246 static_cast<size_t>(EncodeDecode(
247 opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
248 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000249 // If not DTX, it should never enter DTX mode. If DTX, we do not care since
250 // whether it enters DTX depends on the signal type.
251 if (!dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700252 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000253 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
254 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000255 EXPECT_EQ(0, audio_type); // Speech.
256 }
257 }
258
259 // We input some silent segments. In DTX mode, the encoder will stop sending.
260 // However, DTX may happen after a while.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000261 for (int i = 0; i < 30; ++i) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200262 EXPECT_EQ(output_samples, static_cast<size_t>(EncodeDecode(
263 opus_encoder_, silence, opus_decoder_,
264 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000265 if (!dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700266 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000267 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
268 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000269 EXPECT_EQ(0, audio_type); // Speech.
Peter Kasting728d9032015-06-11 14:31:38 -0700270 } else if (encoded_bytes_ == 1) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000271 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
272 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000273 EXPECT_EQ(2, audio_type); // Comfort noise.
274 break;
275 }
276 }
277
Minyue Li092041c2015-05-11 12:19:35 +0200278 // When Opus is in DTX, it wakes up in a regular basis. It sends two packets,
279 // one with an arbitrary size and the other of 1-byte, then stops sending for
minyue3cea2562015-11-10 03:49:26 -0800280 // a certain number of frames.
281
282 // |max_dtx_frames| is the maximum number of frames Opus can stay in DTX.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200283 // TODO(kwiberg): Why does this number depend on the encoding sample rate?
284 const int max_dtx_frames =
285 (encoder_sample_rate_hz_ == 16000 ? 800 : 400) / block_length_ms + 1;
minyue3cea2562015-11-10 03:49:26 -0800286
287 // We run |kRunTimeMs| milliseconds of pure silence.
minyue58e08cb2016-02-24 03:49:19 -0800288 const int kRunTimeMs = 4500;
minyue3cea2562015-11-10 03:49:26 -0800289
290 // We check that, after a |kCheckTimeMs| milliseconds (given that the CNG in
291 // Opus needs time to adapt), the absolute values of DTX decoded signal are
292 // bounded by |kOutputValueBound|.
minyue58e08cb2016-02-24 03:49:19 -0800293 const int kCheckTimeMs = 4000;
minyue3cea2562015-11-10 03:49:26 -0800294
295#if defined(OPUS_FIXED_POINT)
minyuel7e937e92016-02-29 10:24:15 +0100296 // Fixed-point Opus generates a random (comfort) noise, which has a less
297 // predictable value bound than its floating-point Opus. This value depends on
298 // input signal, and the time window for checking the output values (between
299 // |kCheckTimeMs| and |kRunTimeMs|).
300 const uint16_t kOutputValueBound = 30;
301
minyue3cea2562015-11-10 03:49:26 -0800302#else
minyue58e08cb2016-02-24 03:49:19 -0800303 const uint16_t kOutputValueBound = 2;
minyue3cea2562015-11-10 03:49:26 -0800304#endif
305
306 int time = 0;
307 while (time < kRunTimeMs) {
308 // DTX mode is maintained for maximum |max_dtx_frames| frames.
309 int i = 0;
310 for (; i < max_dtx_frames; ++i) {
311 time += block_length_ms;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200312 EXPECT_EQ(output_samples, static_cast<size_t>(EncodeDecode(
313 opus_encoder_, silence, opus_decoder_,
314 output_data_decode, &audio_type)));
Minyue Li092041c2015-05-11 12:19:35 +0200315 if (dtx) {
minyue3cea2562015-11-10 03:49:26 -0800316 if (encoded_bytes_ > 1)
317 break;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700318 EXPECT_EQ(0U, encoded_bytes_) // Send 0 byte.
Minyue Li092041c2015-05-11 12:19:35 +0200319 << "Opus should have entered DTX mode.";
320 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
321 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
322 EXPECT_EQ(2, audio_type); // Comfort noise.
minyue3cea2562015-11-10 03:49:26 -0800323 if (time >= kCheckTimeMs) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200324 CheckAudioBounded(output_data_decode, output_samples, channels_,
minyue3cea2562015-11-10 03:49:26 -0800325 kOutputValueBound);
326 }
Minyue Li092041c2015-05-11 12:19:35 +0200327 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700328 EXPECT_GT(encoded_bytes_, 1U);
Minyue Li092041c2015-05-11 12:19:35 +0200329 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
330 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
331 EXPECT_EQ(0, audio_type); // Speech.
332 }
333 }
334
minyue3cea2562015-11-10 03:49:26 -0800335 if (dtx) {
336 // With DTX, Opus must stop transmission for some time.
337 EXPECT_GT(i, 1);
338 }
Minyue Li092041c2015-05-11 12:19:35 +0200339
minyue3cea2562015-11-10 03:49:26 -0800340 // We expect a normal payload.
Minyue Li092041c2015-05-11 12:19:35 +0200341 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
342 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
343 EXPECT_EQ(0, audio_type); // Speech.
344
345 // Enters DTX again immediately.
minyue3cea2562015-11-10 03:49:26 -0800346 time += block_length_ms;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200347 EXPECT_EQ(output_samples, static_cast<size_t>(EncodeDecode(
348 opus_encoder_, silence, opus_decoder_,
349 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000350 if (dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700351 EXPECT_EQ(1U, encoded_bytes_); // Send 1 byte.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000352 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
353 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000354 EXPECT_EQ(2, audio_type); // Comfort noise.
minyue3cea2562015-11-10 03:49:26 -0800355 if (time >= kCheckTimeMs) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200356 CheckAudioBounded(output_data_decode, output_samples, channels_,
minyue3cea2562015-11-10 03:49:26 -0800357 kOutputValueBound);
358 }
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000359 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700360 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000361 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
362 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000363 EXPECT_EQ(0, audio_type); // Speech.
364 }
365 }
366
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000367 silence[0] = 10000;
368 if (dtx) {
369 // Verify that encoder/decoder can jump out from DTX mode.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200370 EXPECT_EQ(output_samples, static_cast<size_t>(EncodeDecode(
371 opus_encoder_, silence, opus_decoder_,
372 output_data_decode, &audio_type)));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700373 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000374 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
375 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000376 EXPECT_EQ(0, audio_type); // Speech.
377 }
378
379 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000380 delete[] output_data_decode;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000381 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
382 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000383}
384
soren28dc2852017-04-06 05:48:36 -0700385// Test if CBR does what we expect.
386void OpusTest::TestCbrEffect(bool cbr, int block_length_ms) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200387 PrepareSpeechData(block_length_ms, 2000);
388 const size_t output_samples = kOpusDecodeRateKhz * block_length_ms;
soren28dc2852017-04-06 05:48:36 -0700389
390 int32_t max_pkt_size_diff = 0;
391 int32_t prev_pkt_size = 0;
392
393 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200394 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200395 use_multistream_, encoder_sample_rate_hz_);
396 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_);
soren28dc2852017-04-06 05:48:36 -0700397
398 // Set bitrate.
399 EXPECT_EQ(
400 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
401
402 // Setting CBR.
403 EXPECT_EQ(0, cbr ? WebRtcOpus_EnableCbr(opus_encoder_)
404 : WebRtcOpus_DisableCbr(opus_encoder_));
405
406 int16_t audio_type;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200407 std::vector<int16_t> audio_out(output_samples * channels_);
soren28dc2852017-04-06 05:48:36 -0700408 for (int i = 0; i < 100; ++i) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200409 EXPECT_EQ(output_samples,
410 static_cast<size_t>(
411 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
412 opus_decoder_, audio_out.data(), &audio_type)));
soren28dc2852017-04-06 05:48:36 -0700413
414 if (prev_pkt_size > 0) {
415 int32_t diff = std::abs((int32_t)encoded_bytes_ - prev_pkt_size);
416 max_pkt_size_diff = std::max(max_pkt_size_diff, diff);
417 }
Mirko Bonadei737e0732017-10-19 09:00:17 +0200418 prev_pkt_size = rtc::checked_cast<int32_t>(encoded_bytes_);
soren28dc2852017-04-06 05:48:36 -0700419 }
420
421 if (cbr) {
422 EXPECT_EQ(max_pkt_size_diff, 0);
423 } else {
424 EXPECT_GT(max_pkt_size_diff, 0);
425 }
426
427 // Free memory.
428 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
429 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
430}
431
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000432// Test failing Create.
henrika1d34fe92015-06-16 10:04:20 +0200433TEST(OpusTest, OpusCreateFail) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000434 WebRtcOpusEncInst* opus_encoder;
435 WebRtcOpusDecInst* opus_decoder;
436
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000437 // Test to see that an invalid pointer is caught.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200438 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(NULL, 1, 0, 48000));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000439 // Invalid channel number.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200440 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 257, 0, 48000));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000441 // Invalid applciation mode.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200442 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 1, 2, 48000));
443 // Invalid sample rate.
444 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 1, 0, 12345));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000445
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000446 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(NULL, 1));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000447 // Invalid channel number.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100448 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(&opus_decoder, 257));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000449}
450
451// Test failing Free.
henrika1d34fe92015-06-16 10:04:20 +0200452TEST(OpusTest, OpusFreeFail) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000453 // Test to see that an invalid pointer is caught.
454 EXPECT_EQ(-1, WebRtcOpus_EncoderFree(NULL));
455 EXPECT_EQ(-1, WebRtcOpus_DecoderFree(NULL));
456}
457
458// Test normal Create and Free.
henrika1d34fe92015-06-16 10:04:20 +0200459TEST_P(OpusTest, OpusCreateFree) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200460 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200461 use_multistream_, encoder_sample_rate_hz_);
462 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000463 EXPECT_TRUE(opus_encoder_ != NULL);
464 EXPECT_TRUE(opus_decoder_ != NULL);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000465 // Free encoder and decoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000466 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
467 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000468}
469
Alex Loiko50b8c392019-04-03 15:12:01 +0200470#define ENCODER_CTL(inst, vargs) \
471 inst->encoder \
472 ? opus_encoder_ctl(inst->encoder, vargs) \
473 : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs)
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100474
henrika1d34fe92015-06-16 10:04:20 +0200475TEST_P(OpusTest, OpusEncodeDecode) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200476 PrepareSpeechData(20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000477
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000478 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200479 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200480 use_multistream_, encoder_sample_rate_hz_);
481 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000482
483 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200484 EXPECT_EQ(
485 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000486
487 // Check number of channels for decoder.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000488 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
489
490 // Check application mode.
491 opus_int32 app;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100492 ENCODER_CTL(opus_encoder_, OPUS_GET_APPLICATION(&app));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000493 EXPECT_EQ(application_ == 0 ? OPUS_APPLICATION_VOIP : OPUS_APPLICATION_AUDIO,
494 app);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000495
496 // Encode & decode.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000497 int16_t audio_type;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200498 int16_t* output_data_decode =
499 new int16_t[kOpus20msFrameDecodeSamples * channels_];
500 EXPECT_EQ(kOpus20msFrameDecodeSamples,
kwiberg288886b2015-11-06 01:21:35 -0800501 static_cast<size_t>(
502 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
503 opus_decoder_, output_data_decode, &audio_type)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000504
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000505 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000506 delete[] output_data_decode;
507 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
508 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000509}
510
henrika1d34fe92015-06-16 10:04:20 +0200511TEST_P(OpusTest, OpusSetBitRate) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000512 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000513 EXPECT_EQ(-1, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000514
515 // Create encoder memory, try with different bitrates.
Alex Loiko50b8c392019-04-03 15:12:01 +0200516 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200517 use_multistream_, encoder_sample_rate_hz_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000518 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 30000));
519 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
520 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 300000));
521 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 600000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000522
523 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000524 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000525}
526
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000527TEST_P(OpusTest, OpusSetComplexity) {
minyue@webrtc.org04546882014-03-07 08:55:48 +0000528 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000529 EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 9));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000530
531 // Create encoder memory, try with different complexities.
Alex Loiko50b8c392019-04-03 15:12:01 +0200532 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200533 use_multistream_, encoder_sample_rate_hz_);
minyue@webrtc.org04546882014-03-07 08:55:48 +0000534
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000535 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 0));
536 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 10));
537 EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 11));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000538
539 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000540 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000541}
542
Alex Luebseeb27652017-11-20 11:13:56 -0800543TEST_P(OpusTest, OpusSetBandwidth) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100544 if (channels_ > 2) {
545 // TODO(webrtc:10217): investigate why multi-stream Opus reports
546 // narrowband when it's configured with FULLBAND.
547 return;
548 }
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200549 PrepareSpeechData(20, 20);
Alex Luebseeb27652017-11-20 11:13:56 -0800550
551 int16_t audio_type;
552 std::unique_ptr<int16_t[]> output_data_decode(
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200553 new int16_t[kOpus20msFrameDecodeSamples * channels_]());
Alex Luebseeb27652017-11-20 11:13:56 -0800554
555 // Test without creating encoder memory.
556 EXPECT_EQ(-1,
557 WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
558 EXPECT_EQ(-1, WebRtcOpus_GetBandwidth(opus_encoder_));
559
560 // Create encoder memory, try with different bandwidths.
Alex Loiko50b8c392019-04-03 15:12:01 +0200561 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200562 use_multistream_, encoder_sample_rate_hz_);
563 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_);
Alex Luebseeb27652017-11-20 11:13:56 -0800564
565 EXPECT_EQ(-1, WebRtcOpus_SetBandwidth(opus_encoder_,
566 OPUS_BANDWIDTH_NARROWBAND - 1));
567 EXPECT_EQ(0,
568 WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
569 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
570 output_data_decode.get(), &audio_type);
571 EXPECT_EQ(OPUS_BANDWIDTH_NARROWBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
572 EXPECT_EQ(0, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND));
573 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
574 output_data_decode.get(), &audio_type);
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200575 EXPECT_EQ(encoder_sample_rate_hz_ == 16000 ? OPUS_BANDWIDTH_WIDEBAND
576 : OPUS_BANDWIDTH_FULLBAND,
577 WebRtcOpus_GetBandwidth(opus_encoder_));
Alex Luebseeb27652017-11-20 11:13:56 -0800578 EXPECT_EQ(
579 -1, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND + 1));
580 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
581 output_data_decode.get(), &audio_type);
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200582 EXPECT_EQ(encoder_sample_rate_hz_ == 16000 ? OPUS_BANDWIDTH_WIDEBAND
583 : OPUS_BANDWIDTH_FULLBAND,
584 WebRtcOpus_GetBandwidth(opus_encoder_));
Alex Luebseeb27652017-11-20 11:13:56 -0800585
586 // Free memory.
587 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
588 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
589}
590
minyuec8299f92016-09-27 02:08:47 -0700591TEST_P(OpusTest, OpusForceChannels) {
592 // Test without creating encoder memory.
593 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
594
Alex Loiko50b8c392019-04-03 15:12:01 +0200595 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200596 use_multistream_, encoder_sample_rate_hz_);
Alex Loiko50b8c392019-04-03 15:12:01 +0200597 ASSERT_NE(nullptr, opus_encoder_);
minyuec8299f92016-09-27 02:08:47 -0700598
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100599 if (channels_ >= 2) {
minyuec8299f92016-09-27 02:08:47 -0700600 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 3));
601 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
602 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
603 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
604 } else {
605 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
606 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
607 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
608 }
609
610 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
611}
612
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000613// Encode and decode one frame, initialize the decoder and
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000614// decode once more.
henrika1d34fe92015-06-16 10:04:20 +0200615TEST_P(OpusTest, OpusDecodeInit) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200616 PrepareSpeechData(20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000617
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000618 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200619 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200620 use_multistream_, encoder_sample_rate_hz_);
621 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000622
623 // Encode & decode.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000624 int16_t audio_type;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200625 int16_t* output_data_decode =
626 new int16_t[kOpus20msFrameDecodeSamples * channels_];
627 EXPECT_EQ(kOpus20msFrameDecodeSamples,
kwiberg288886b2015-11-06 01:21:35 -0800628 static_cast<size_t>(
629 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
630 opus_decoder_, output_data_decode, &audio_type)));
minyue@webrtc.org52bc4f42014-12-04 11:00:50 +0000631
Karl Wiberg43766482015-08-27 15:22:11 +0200632 WebRtcOpus_DecoderInit(opus_decoder_);
minyue@webrtc.org52bc4f42014-12-04 11:00:50 +0000633
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200634 EXPECT_EQ(kOpus20msFrameDecodeSamples,
Yves Gerey665174f2018-06-19 15:03:05 +0200635 static_cast<size_t>(
636 WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_,
637 output_data_decode, &audio_type)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000638
639 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000640 delete[] output_data_decode;
641 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
642 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000643}
644
henrika1d34fe92015-06-16 10:04:20 +0200645TEST_P(OpusTest, OpusEnableDisableFec) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000646 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000647 EXPECT_EQ(-1, WebRtcOpus_EnableFec(opus_encoder_));
648 EXPECT_EQ(-1, WebRtcOpus_DisableFec(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000649
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000650 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200651 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200652 use_multistream_, encoder_sample_rate_hz_);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000653
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000654 EXPECT_EQ(0, WebRtcOpus_EnableFec(opus_encoder_));
655 EXPECT_EQ(0, WebRtcOpus_DisableFec(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000656
657 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000658 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000659}
660
henrika1d34fe92015-06-16 10:04:20 +0200661TEST_P(OpusTest, OpusEnableDisableDtx) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000662 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000663 EXPECT_EQ(-1, WebRtcOpus_EnableDtx(opus_encoder_));
664 EXPECT_EQ(-1, WebRtcOpus_DisableDtx(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000665
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000666 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200667 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200668 use_multistream_, encoder_sample_rate_hz_);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000669
670 opus_int32 dtx;
671
672 // DTX is off by default.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100673 ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000674 EXPECT_EQ(0, dtx);
675
676 // Test to enable DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000677 EXPECT_EQ(0, WebRtcOpus_EnableDtx(opus_encoder_));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100678 ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000679 EXPECT_EQ(1, dtx);
680
681 // Test to disable DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000682 EXPECT_EQ(0, WebRtcOpus_DisableDtx(opus_encoder_));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100683 ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000684 EXPECT_EQ(0, dtx);
685
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000686 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000687 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000688}
689
henrika1d34fe92015-06-16 10:04:20 +0200690TEST_P(OpusTest, OpusDtxOff) {
minyue3cea2562015-11-10 03:49:26 -0800691 TestDtxEffect(false, 10);
692 TestDtxEffect(false, 20);
693 TestDtxEffect(false, 40);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000694}
695
henrika1d34fe92015-06-16 10:04:20 +0200696TEST_P(OpusTest, OpusDtxOn) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100697 if (channels_ > 2) {
698 // TODO(webrtc:10218): adapt the test to the sizes and order of multi-stream
699 // DTX packets.
700 return;
701 }
minyue3cea2562015-11-10 03:49:26 -0800702 TestDtxEffect(true, 10);
703 TestDtxEffect(true, 20);
704 TestDtxEffect(true, 40);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000705}
706
soren28dc2852017-04-06 05:48:36 -0700707TEST_P(OpusTest, OpusCbrOff) {
708 TestCbrEffect(false, 10);
709 TestCbrEffect(false, 20);
710 TestCbrEffect(false, 40);
711}
712
713TEST_P(OpusTest, OpusCbrOn) {
714 TestCbrEffect(true, 10);
715 TestCbrEffect(true, 20);
716 TestCbrEffect(true, 40);
717}
718
henrika1d34fe92015-06-16 10:04:20 +0200719TEST_P(OpusTest, OpusSetPacketLossRate) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000720 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000721 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000722
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000723 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200724 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200725 use_multistream_, encoder_sample_rate_hz_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000726
727 EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
728 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, -1));
729 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 101));
730
731 // Free memory.
732 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
733}
734
henrika1d34fe92015-06-16 10:04:20 +0200735TEST_P(OpusTest, OpusSetMaxPlaybackRate) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000736 // Test without creating encoder memory.
737 EXPECT_EQ(-1, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, 20000));
738
739 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200740 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200741 use_multistream_, encoder_sample_rate_hz_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000742
743 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 48000);
744 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 24001);
745 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 24000);
746 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 16001);
747 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 16000);
748 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 12001);
749 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 12000);
750 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 8001);
751 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 8000);
752 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 4000);
753
754 // Free memory.
755 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
756}
757
758// Test PLC.
henrika1d34fe92015-06-16 10:04:20 +0200759TEST_P(OpusTest, OpusDecodePlc) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200760 PrepareSpeechData(20, 20);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000761
762 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200763 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200764 use_multistream_, encoder_sample_rate_hz_);
765 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000766
767 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200768 EXPECT_EQ(
769 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000770
771 // Check number of channels for decoder.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000772 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000773
774 // Encode & decode.
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000775 int16_t audio_type;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200776 int16_t* output_data_decode =
777 new int16_t[kOpus20msFrameDecodeSamples * channels_];
778 EXPECT_EQ(kOpus20msFrameDecodeSamples,
kwiberg288886b2015-11-06 01:21:35 -0800779 static_cast<size_t>(
780 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
781 opus_decoder_, output_data_decode, &audio_type)));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000782
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000783 // Call decoder PLC.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200784 int16_t* plc_buffer = new int16_t[kOpus20msFrameDecodeSamples * channels_];
785 EXPECT_EQ(
786 kOpus20msFrameDecodeSamples,
787 static_cast<size_t>(WebRtcOpus_DecodePlc(opus_decoder_, plc_buffer, 1)));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000788
789 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000790 delete[] plc_buffer;
791 delete[] output_data_decode;
792 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
793 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000794}
795
796// Duration estimation.
henrika1d34fe92015-06-16 10:04:20 +0200797TEST_P(OpusTest, OpusDurationEstimation) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200798 PrepareSpeechData(20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000799
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000800 // Create.
Alex Loiko50b8c392019-04-03 15:12:01 +0200801 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200802 use_multistream_, encoder_sample_rate_hz_);
803 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000804
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000805 // 10 ms. We use only first 10 ms of a 20 ms block.
kwiberg288886b2015-11-06 01:21:35 -0800806 auto speech_block = speech_data_.GetNextBlock();
807 int encoded_bytes_int = WebRtcOpus_Encode(
808 opus_encoder_, speech_block.data(),
Yves Gerey665174f2018-06-19 15:03:05 +0200809 rtc::CheckedDivExact(speech_block.size(), 2 * channels_), kMaxBytes,
810 bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700811 EXPECT_GE(encoded_bytes_int, 0);
Yves Gerey665174f2018-06-19 15:03:05 +0200812 EXPECT_EQ(
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200813 kOpus10msFrameDecodeSamples,
Yves Gerey665174f2018-06-19 15:03:05 +0200814 static_cast<size_t>(WebRtcOpus_DurationEst(
815 opus_decoder_, bitstream_, static_cast<size_t>(encoded_bytes_int))));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000816
817 // 20 ms
kwiberg288886b2015-11-06 01:21:35 -0800818 speech_block = speech_data_.GetNextBlock();
Yves Gerey665174f2018-06-19 15:03:05 +0200819 encoded_bytes_int =
820 WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
821 rtc::CheckedDivExact(speech_block.size(), channels_),
822 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700823 EXPECT_GE(encoded_bytes_int, 0);
Yves Gerey665174f2018-06-19 15:03:05 +0200824 EXPECT_EQ(
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200825 kOpus20msFrameDecodeSamples,
Yves Gerey665174f2018-06-19 15:03:05 +0200826 static_cast<size_t>(WebRtcOpus_DurationEst(
827 opus_decoder_, bitstream_, static_cast<size_t>(encoded_bytes_int))));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000828
829 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000830 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
831 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000832}
833
henrika1d34fe92015-06-16 10:04:20 +0200834TEST_P(OpusTest, OpusDecodeRepacketized) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100835 if (channels_ > 2) {
836 // As per the Opus documentation
837 // https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/group__opus__repacketizer.html#details,
838 // multiple streams are not supported.
839 return;
840 }
minyuea613eb62017-03-14 14:33:30 -0700841 constexpr size_t kPackets = 6;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000842
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200843 PrepareSpeechData(20, 20 * kPackets);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000844
845 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200846 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200847 use_multistream_, encoder_sample_rate_hz_);
Alex Loiko50b8c392019-04-03 15:12:01 +0200848 ASSERT_NE(nullptr, opus_encoder_);
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200849 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_);
Alex Loiko50b8c392019-04-03 15:12:01 +0200850 ASSERT_NE(nullptr, opus_decoder_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000851
852 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200853 EXPECT_EQ(
854 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000855
856 // Check number of channels for decoder.
857 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
858
859 // Encode & decode.
860 int16_t audio_type;
kwiberg91d97562016-02-14 01:10:03 -0800861 std::unique_ptr<int16_t[]> output_data_decode(
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200862 new int16_t[kPackets * kOpus20msFrameDecodeSamples * channels_]);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000863 OpusRepacketizer* rp = opus_repacketizer_create();
864
minyuea613eb62017-03-14 14:33:30 -0700865 size_t num_packets = 0;
866 constexpr size_t kMaxCycles = 100;
867 for (size_t idx = 0; idx < kMaxCycles; ++idx) {
kwiberg288886b2015-11-06 01:21:35 -0800868 auto speech_block = speech_data_.GetNextBlock();
869 encoded_bytes_ =
870 WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
Peter Kasting69558702016-01-12 16:26:35 -0800871 rtc::CheckedDivExact(speech_block.size(), channels_),
kwiberg288886b2015-11-06 01:21:35 -0800872 kMaxBytes, bitstream_);
Yves Gerey665174f2018-06-19 15:03:05 +0200873 if (opus_repacketizer_cat(rp, bitstream_,
874 rtc::checked_cast<opus_int32>(encoded_bytes_)) ==
875 OPUS_OK) {
minyuea613eb62017-03-14 14:33:30 -0700876 ++num_packets;
877 if (num_packets == kPackets) {
878 break;
879 }
880 } else {
881 // Opus repacketizer cannot guarantee a success. We try again if it fails.
882 opus_repacketizer_init(rp);
883 num_packets = 0;
884 }
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000885 }
minyuea613eb62017-03-14 14:33:30 -0700886 EXPECT_EQ(kPackets, num_packets);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000887
888 encoded_bytes_ = opus_repacketizer_out(rp, bitstream_, kMaxBytes);
889
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200890 EXPECT_EQ(kOpus20msFrameDecodeSamples * kPackets,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700891 static_cast<size_t>(WebRtcOpus_DurationEst(
892 opus_decoder_, bitstream_, encoded_bytes_)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000893
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200894 EXPECT_EQ(kOpus20msFrameDecodeSamples * kPackets,
Yves Gerey665174f2018-06-19 15:03:05 +0200895 static_cast<size_t>(
896 WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_,
897 output_data_decode.get(), &audio_type)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000898
899 // Free memory.
900 opus_repacketizer_destroy(rp);
901 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
902 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
903}
904
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100905INSTANTIATE_TEST_SUITE_P(VariousMode,
906 OpusTest,
Alex Loiko50b8c392019-04-03 15:12:01 +0200907 ::testing::ValuesIn({
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200908 std::make_tuple(1, 0, false, 16000),
909 std::make_tuple(1, 1, false, 16000),
910 std::make_tuple(2, 0, false, 16000),
911 std::make_tuple(2, 1, false, 16000),
912 std::make_tuple(1, 0, false, 48000),
913 std::make_tuple(1, 1, false, 48000),
914 std::make_tuple(2, 0, false, 48000),
915 std::make_tuple(2, 1, false, 48000),
916 std::make_tuple(1, 0, true, 48000),
917 std::make_tuple(2, 1, true, 48000),
918 std::make_tuple(4, 0, true, 48000),
919 std::make_tuple(4, 1, true, 48000),
Alex Loiko50b8c392019-04-03 15:12:01 +0200920 }));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000921
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000922} // namespace webrtc