henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_coding/neteq/normal.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 14 | |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 15 | #include <memory> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #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.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 28 | |
| 29 | using ::testing::_; |
minyue | 5bd3397 | 2016-05-02 04:46:11 -0700 | [diff] [blame] | 30 | using ::testing::Invoke; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
| 33 | |
minyue | 5bd3397 | 2016-05-02 04:46:11 -0700 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | int ExpandProcess120ms(AudioMultiVector* output) { |
| 37 | AudioMultiVector dummy_audio(1, 11520u); |
| 38 | dummy_audio.CopyTo(output); |
| 39 | return 0; |
| 40 | } |
| 41 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 42 | } // namespace |
minyue | 5bd3397 | 2016-05-02 04:46:11 -0700 | [diff] [blame] | 43 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 44 | TEST(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 Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 51 | StatisticsCalculator statistics; |
| 52 | Expand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, channels); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 53 | Normal normal(fs, &db, bgn, &expand); |
| 54 | EXPECT_CALL(db, Die()); // Called when |db| goes out of scope. |
| 55 | } |
| 56 | |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 57 | TEST(Normal, AvoidDivideByZero) { |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 58 | MockDecoderDatabase db; |
| 59 | int fs = 8000; |
| 60 | size_t channels = 1; |
| 61 | BackgroundNoise bgn(channels); |
| 62 | SyncBuffer sync_buffer(1, 1000); |
| 63 | RandomVector random_vector; |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 64 | StatisticsCalculator statistics; |
| 65 | MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, |
| 66 | channels); |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 67 | Normal normal(fs, &db, bgn, &expand); |
| 68 | |
| 69 | int16_t input[1000] = {0}; |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 70 | AudioMultiVector output(channels); |
| 71 | |
| 72 | // Zero input length. |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame^] | 73 | EXPECT_EQ(0, normal.Process(input, 0, NetEq::Mode::kExpand, &output)); |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 74 | EXPECT_EQ(0u, output.Size()); |
| 75 | |
| 76 | // Try to make energy_length >> scaling = 0; |
| 77 | EXPECT_CALL(expand, SetParametersForNormalAfterExpand()); |
| 78 | EXPECT_CALL(expand, Process(_)); |
| 79 | EXPECT_CALL(expand, Reset()); |
| 80 | // If input_size_samples < 64, then energy_length in Normal::Process() will |
| 81 | // be equal to input_size_samples. Since the input is all zeros, decoded_max |
| 82 | // will be zero, and scaling will be >= 6. Thus, energy_length >> scaling = 0, |
| 83 | // and using this as a denominator would lead to problems. |
| 84 | int input_size_samples = 63; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame^] | 85 | EXPECT_EQ(input_size_samples, normal.Process(input, input_size_samples, |
| 86 | NetEq::Mode::kExpand, &output)); |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 87 | |
| 88 | EXPECT_CALL(db, Die()); // Called when |db| goes out of scope. |
| 89 | EXPECT_CALL(expand, Die()); // Called when |expand| goes out of scope. |
| 90 | } |
| 91 | |
| 92 | TEST(Normal, InputLengthAndChannelsDoNotMatch) { |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 93 | MockDecoderDatabase db; |
| 94 | int fs = 8000; |
| 95 | size_t channels = 2; |
| 96 | BackgroundNoise bgn(channels); |
| 97 | SyncBuffer sync_buffer(channels, 1000); |
| 98 | RandomVector random_vector; |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 99 | StatisticsCalculator statistics; |
| 100 | MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, |
| 101 | channels); |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 102 | Normal normal(fs, &db, bgn, &expand); |
| 103 | |
| 104 | int16_t input[1000] = {0}; |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 105 | AudioMultiVector output(channels); |
| 106 | |
| 107 | // Let the number of samples be one sample less than 80 samples per channel. |
| 108 | size_t input_len = 80 * channels - 1; |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame^] | 109 | EXPECT_EQ(0, normal.Process(input, input_len, NetEq::Mode::kExpand, &output)); |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 110 | EXPECT_EQ(0u, output.Size()); |
| 111 | |
| 112 | EXPECT_CALL(db, Die()); // Called when |db| goes out of scope. |
| 113 | EXPECT_CALL(expand, Die()); // Called when |expand| goes out of scope. |
| 114 | } |
| 115 | |
minyue | 5bd3397 | 2016-05-02 04:46:11 -0700 | [diff] [blame] | 116 | TEST(Normal, LastModeExpand120msPacket) { |
minyue | 5bd3397 | 2016-05-02 04:46:11 -0700 | [diff] [blame] | 117 | MockDecoderDatabase db; |
| 118 | const int kFs = 48000; |
| 119 | const size_t kPacketsizeBytes = 11520u; |
| 120 | const size_t kChannels = 1; |
| 121 | BackgroundNoise bgn(kChannels); |
| 122 | SyncBuffer sync_buffer(kChannels, 1000); |
| 123 | RandomVector random_vector; |
| 124 | StatisticsCalculator statistics; |
| 125 | MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, kFs, |
| 126 | kChannels); |
| 127 | Normal normal(kFs, &db, bgn, &expand); |
| 128 | |
| 129 | int16_t input[kPacketsizeBytes] = {0}; |
minyue | 5bd3397 | 2016-05-02 04:46:11 -0700 | [diff] [blame] | 130 | AudioMultiVector output(kChannels); |
| 131 | |
| 132 | EXPECT_CALL(expand, SetParametersForNormalAfterExpand()); |
| 133 | EXPECT_CALL(expand, Process(_)).WillOnce(Invoke(ExpandProcess120ms)); |
| 134 | EXPECT_CALL(expand, Reset()); |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame^] | 135 | EXPECT_EQ( |
| 136 | static_cast<int>(kPacketsizeBytes), |
| 137 | normal.Process(input, kPacketsizeBytes, NetEq::Mode::kExpand, &output)); |
minyue | 5bd3397 | 2016-05-02 04:46:11 -0700 | [diff] [blame] | 138 | |
| 139 | EXPECT_EQ(kPacketsizeBytes, output.Size()); |
| 140 | |
| 141 | EXPECT_CALL(db, Die()); // Called when |db| goes out of scope. |
| 142 | EXPECT_CALL(expand, Die()); // Called when |expand| goes out of scope. |
| 143 | } |
| 144 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 145 | // TODO(hlundin): Write more tests. |
| 146 | |
| 147 | } // namespace webrtc |