blob: 106762afbb17265b9deccd76faef7ceb4e0f931d [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 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 */
10
11// Unit tests for Normal class.
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_coding/neteq/normal.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014
kwiberg2d0c3322016-02-14 09:28:33 -080015#include <memory>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/signal_processing/include/signal_processing_library.h"
19#include "modules/audio_coding/neteq/audio_multi_vector.h"
20#include "modules/audio_coding/neteq/background_noise.h"
21#include "modules/audio_coding/neteq/expand.h"
22#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
23#include "modules/audio_coding/neteq/mock/mock_expand.h"
24#include "modules/audio_coding/neteq/random_vector.h"
25#include "modules/audio_coding/neteq/statistics_calculator.h"
26#include "modules/audio_coding/neteq/sync_buffer.h"
27#include "test/gtest.h"
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +000028
29using ::testing::_;
minyue5bd33972016-05-02 04:46:11 -070030using ::testing::Invoke;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000031
32namespace webrtc {
33
minyue5bd33972016-05-02 04:46:11 -070034namespace {
35
36int ExpandProcess120ms(AudioMultiVector* output) {
37 AudioMultiVector dummy_audio(1, 11520u);
38 dummy_audio.CopyTo(output);
39 return 0;
40}
41
Yves Gerey665174f2018-06-19 15:03:05 +020042} // namespace
minyue5bd33972016-05-02 04:46:11 -070043
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044TEST(Normal, CreateAndDestroy) {
45 MockDecoderDatabase db;
46 int fs = 8000;
47 size_t channels = 1;
48 BackgroundNoise bgn(channels);
49 SyncBuffer sync_buffer(1, 1000);
50 RandomVector random_vector;
Henrik Lundinbef77e22015-08-18 14:58:09 +020051 StatisticsCalculator statistics;
52 Expand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, channels);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000053 Normal normal(fs, &db, bgn, &expand);
54 EXPECT_CALL(db, Die()); // Called when |db| goes out of scope.
55}
56
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +000057TEST(Normal, AvoidDivideByZero) {
58 WebRtcSpl_Init();
59 MockDecoderDatabase db;
60 int fs = 8000;
61 size_t channels = 1;
62 BackgroundNoise bgn(channels);
63 SyncBuffer sync_buffer(1, 1000);
64 RandomVector random_vector;
Henrik Lundinbef77e22015-08-18 14:58:09 +020065 StatisticsCalculator statistics;
66 MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs,
67 channels);
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +000068 Normal normal(fs, &db, bgn, &expand);
69
70 int16_t input[1000] = {0};
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +000071 AudioMultiVector output(channels);
72
73 // Zero input length.
Henrik Lundin6dc82e82018-05-22 10:40:23 +020074 EXPECT_EQ(0, normal.Process(input, 0, kModeExpand, &output));
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +000075 EXPECT_EQ(0u, output.Size());
76
77 // Try to make energy_length >> scaling = 0;
78 EXPECT_CALL(expand, SetParametersForNormalAfterExpand());
79 EXPECT_CALL(expand, Process(_));
80 EXPECT_CALL(expand, Reset());
81 // If input_size_samples < 64, then energy_length in Normal::Process() will
82 // be equal to input_size_samples. Since the input is all zeros, decoded_max
83 // will be zero, and scaling will be >= 6. Thus, energy_length >> scaling = 0,
84 // and using this as a denominator would lead to problems.
85 int input_size_samples = 63;
86 EXPECT_EQ(input_size_samples,
Yves Gerey665174f2018-06-19 15:03:05 +020087 normal.Process(input, input_size_samples, kModeExpand, &output));
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +000088
89 EXPECT_CALL(db, Die()); // Called when |db| goes out of scope.
90 EXPECT_CALL(expand, Die()); // Called when |expand| goes out of scope.
91}
92
93TEST(Normal, InputLengthAndChannelsDoNotMatch) {
94 WebRtcSpl_Init();
95 MockDecoderDatabase db;
96 int fs = 8000;
97 size_t channels = 2;
98 BackgroundNoise bgn(channels);
99 SyncBuffer sync_buffer(channels, 1000);
100 RandomVector random_vector;
Henrik Lundinbef77e22015-08-18 14:58:09 +0200101 StatisticsCalculator statistics;
102 MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs,
103 channels);
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +0000104 Normal normal(fs, &db, bgn, &expand);
105
106 int16_t input[1000] = {0};
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +0000107 AudioMultiVector output(channels);
108
109 // Let the number of samples be one sample less than 80 samples per channel.
110 size_t input_len = 80 * channels - 1;
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200111 EXPECT_EQ(0, normal.Process(input, input_len, kModeExpand, &output));
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +0000112 EXPECT_EQ(0u, output.Size());
113
114 EXPECT_CALL(db, Die()); // Called when |db| goes out of scope.
115 EXPECT_CALL(expand, Die()); // Called when |expand| goes out of scope.
116}
117
minyue5bd33972016-05-02 04:46:11 -0700118TEST(Normal, LastModeExpand120msPacket) {
119 WebRtcSpl_Init();
120 MockDecoderDatabase db;
121 const int kFs = 48000;
122 const size_t kPacketsizeBytes = 11520u;
123 const size_t kChannels = 1;
124 BackgroundNoise bgn(kChannels);
125 SyncBuffer sync_buffer(kChannels, 1000);
126 RandomVector random_vector;
127 StatisticsCalculator statistics;
128 MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, kFs,
129 kChannels);
130 Normal normal(kFs, &db, bgn, &expand);
131
132 int16_t input[kPacketsizeBytes] = {0};
minyue5bd33972016-05-02 04:46:11 -0700133 AudioMultiVector output(kChannels);
134
135 EXPECT_CALL(expand, SetParametersForNormalAfterExpand());
136 EXPECT_CALL(expand, Process(_)).WillOnce(Invoke(ExpandProcess120ms));
137 EXPECT_CALL(expand, Reset());
138 EXPECT_EQ(static_cast<int>(kPacketsizeBytes),
Yves Gerey665174f2018-06-19 15:03:05 +0200139 normal.Process(input, kPacketsizeBytes, kModeExpand, &output));
minyue5bd33972016-05-02 04:46:11 -0700140
141 EXPECT_EQ(kPacketsizeBytes, output.Size());
142
143 EXPECT_CALL(db, Die()); // Called when |db| goes out of scope.
144 EXPECT_CALL(expand, Die()); // Called when |expand| goes out of scope.
145}
146
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000147// TODO(hlundin): Write more tests.
148
149} // namespace webrtc