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#include "testing/gtest/include/gtest/gtest.h"
12#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
13#include "webrtc/modules/audio_coding/codecs/isac/fix/source/filterbank_internal.h"
14#include "webrtc/modules/audio_coding/codecs/isac/fix/source/filterbank_tables.h"
15#include "webrtc/modules/audio_coding/codecs/isac/fix/source/settings.h"
16#include "webrtc/system_wrappers/interface/cpu_features_wrapper.h"
17#include "webrtc/typedefs.h"
18
19class FilterBanksTest : public testing::Test {
20 protected:
21  // Pass a function pointer to the Tester function.
22  void CalculateResidualEnergyTester(AllpassFilter2FixDec16
23                                     AllpassFilter2FixDec16Function) {
24    const int kSamples = QLOOKAHEAD;
25    const int kState = 2;
26    int16_t data_ch1[kSamples] = {0};
27    int16_t data_ch2[kSamples] = {0};
28    int32_t state_ch1[kState] = {0};
29    int32_t state_ch2[kState] = {0};
30    const int32_t out_state_ch1[kState] = {-809122714, 1645972152};
31    const int32_t out_state_ch2[kState] = {428019288, 1057309936};
32    const int32_t out_data_ch1[kSamples] = {0, 0, 347, 10618, 16718, -7089,
33        32767, 16913, 27042, 8377, -22973, -28372, -27603, -14804, 398, -25332,
34        -11200, 18044, 25223, -6839, 1116, -23984, 32717, 7364};
35    const int32_t out_data_ch2[kSamples] = {0, 0, 3010, 22351, 21106, 16969,
36        -2095, -664, 3513, -30980, 32767, -23839, 13335, 20289, -6831, 339,
37        -17207, 32767, 4959, 6177, 32767, 16599, -4747, 20504};
38    int sign = 1;
39
40    for (int i = 0; i < kSamples; i++) {
41      sign *= -1;
42      data_ch1[i] = sign * WEBRTC_SPL_WORD32_MAX / (i * i + 1);
43      data_ch2[i] = sign * WEBRTC_SPL_WORD32_MIN / (i * i + 1);
44    };
45
46    AllpassFilter2FixDec16Function(data_ch1,
47                                   data_ch2,
48                                   WebRtcIsacfix_kUpperApFactorsQ15,
49                                   WebRtcIsacfix_kLowerApFactorsQ15,
50                                   kSamples,
51                                   state_ch1,
52                                   state_ch2);
53
54    for (int i = 0; i < kSamples; i++) {
55      EXPECT_EQ(out_data_ch1[i], data_ch1[i]);
56      EXPECT_EQ(out_data_ch2[i], data_ch2[i]);
57    }
58    for (int i = 0; i < kState; i++) {
59      EXPECT_EQ(out_state_ch1[i], state_ch1[i]);
60      EXPECT_EQ(out_state_ch2[i], state_ch2[i]);
61    }
62  }
63};
64
65TEST_F(FilterBanksTest, AllpassFilter2FixDec16Test) {
66  CalculateResidualEnergyTester(WebRtcIsacfix_AllpassFilter2FixDec16C);
67#ifdef WEBRTC_DETECT_ARM_NEON
68  if ((WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON) != 0) {
69    CalculateResidualEnergyTester(WebRtcIsacfix_AllpassFilter2FixDec16Neon);
70  }
71#elif defined(WEBRTC_ARCH_ARM_NEON)
72  CalculateResidualEnergyTester(WebRtcIsacfix_AllpassFilter2FixDec16Neon);
73#endif
74}
75
76TEST_F(FilterBanksTest, HighpassFilterFixDec32Test) {
77  const int kSamples = 20;
78  int16_t in[kSamples];
79  int32_t state[2] = {12345, 987654};
80#ifdef WEBRTC_ARCH_ARM_V7
81  int32_t out[kSamples] = {-1040, -1035, -22875, -1397, -27604, 20018, 7917,
82    -1279, -8552, -14494, -7558, -23537, -27258, -30554, -32768, -3432, -32768,
83    25215, -27536, 22436};
84#else
85  int32_t out[kSamples] = {-1040, -1035, -22875, -1397, -27604, 20017, 7915,
86    -1280, -8554, -14496, -7561, -23541, -27263, -30560, -32768, -3441, -32768,
87    25203, -27550, 22419};
88#endif
89  HighpassFilterFixDec32 WebRtcIsacfix_HighpassFilterFixDec32;
90#if defined(MIPS_DSP_R1_LE)
91  WebRtcIsacfix_HighpassFilterFixDec32 =
92      WebRtcIsacfix_HighpassFilterFixDec32MIPS;
93#else
94  WebRtcIsacfix_HighpassFilterFixDec32 = WebRtcIsacfix_HighpassFilterFixDec32C;
95#endif
96
97  for (int i = 0; i < kSamples; i++) {
98    in[i] = WEBRTC_SPL_WORD32_MAX / (i + 1);
99  }
100
101  WebRtcIsacfix_HighpassFilterFixDec32(in, kSamples,
102      WebRtcIsacfix_kHPStCoeffOut1Q30, state);
103
104  for (int i = 0; i < kSamples; i++) {
105    EXPECT_EQ(out[i], in[i]);
106  }
107}
108