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 "common_audio/vad/vad_sp.h"
12 
13 #include "rtc_base/checks.h"
14 #include "common_audio/signal_processing/include/signal_processing_library.h"
15 #include "common_audio/vad/vad_core.h"
16 
17 // Allpass filter coefficients, upper and lower, in Q13.
18 // Upper: 0.64, Lower: 0.17.
19 static const int16_t kAllPassCoefsQ13[2] = { 5243, 1392 };  // Q13.
20 static const int16_t kSmoothingDown = 6553;  // 0.2 in Q15.
21 static const int16_t kSmoothingUp = 32439;  // 0.99 in Q15.
22 
23 // TODO(bjornv): Move this function to vad_filterbank.c.
24 // Downsampling filter based on splitting filter and allpass functions.
WebRtcVad_Downsampling(const int16_t * signal_in,int16_t * signal_out,int32_t * filter_state,size_t in_length)25 void WebRtcVad_Downsampling(const int16_t* signal_in,
26                             int16_t* signal_out,
27                             int32_t* filter_state,
28                             size_t in_length) {
29   int16_t tmp16_1 = 0, tmp16_2 = 0;
30   int32_t tmp32_1 = filter_state[0];
31   int32_t tmp32_2 = filter_state[1];
32   size_t n = 0;
33   // Downsampling by 2 gives half length.
34   size_t half_length = (in_length >> 1);
35 
36   // Filter coefficients in Q13, filter state in Q0.
37   for (n = 0; n < half_length; n++) {
38     // All-pass filtering upper branch.
39     tmp16_1 = (int16_t) ((tmp32_1 >> 1) +
40         ((kAllPassCoefsQ13[0] * *signal_in) >> 14));
41     *signal_out = tmp16_1;
42     tmp32_1 = (int32_t)(*signal_in++) - ((kAllPassCoefsQ13[0] * tmp16_1) >> 12);
43 
44     // All-pass filtering lower branch.
45     tmp16_2 = (int16_t) ((tmp32_2 >> 1) +
46         ((kAllPassCoefsQ13[1] * *signal_in) >> 14));
47     *signal_out++ += tmp16_2;
48     tmp32_2 = (int32_t)(*signal_in++) - ((kAllPassCoefsQ13[1] * tmp16_2) >> 12);
49   }
50   // Store the filter states.
51   filter_state[0] = tmp32_1;
52   filter_state[1] = tmp32_2;
53 }
54 
55 // Inserts |feature_value| into |low_value_vector|, if it is one of the 16
56 // smallest values the last 100 frames. Then calculates and returns the median
57 // of the five smallest values.
WebRtcVad_FindMinimum(VadInstT * self,int16_t feature_value,int channel)58 int16_t WebRtcVad_FindMinimum(VadInstT* self,
59                               int16_t feature_value,
60                               int channel) {
61   int i = 0, j = 0;
62   int position = -1;
63   // Offset to beginning of the 16 minimum values in memory.
64   const int offset = (channel << 4);
65   int16_t current_median = 1600;
66   int16_t alpha = 0;
67   int32_t tmp32 = 0;
68   // Pointer to memory for the 16 minimum values and the age of each value of
69   // the |channel|.
70   int16_t* age = &self->index_vector[offset];
71   int16_t* smallest_values = &self->low_value_vector[offset];
72 
73   RTC_DCHECK_LT(channel, kNumChannels);
74 
75   // Each value in |smallest_values| is getting 1 loop older. Update |age|, and
76   // remove old values.
77   for (i = 0; i < 16; i++) {
78     if (age[i] != 100) {
79       age[i]++;
80     } else {
81       // Too old value. Remove from memory and shift larger values downwards.
82       for (j = i; j < 15; j++) {
83         smallest_values[j] = smallest_values[j + 1];
84         age[j] = age[j + 1];
85       }
86       age[15] = 101;
87       smallest_values[15] = 10000;
88     }
89   }
90 
91   // Check if |feature_value| is smaller than any of the values in
92   // |smallest_values|. If so, find the |position| where to insert the new value
93   // (|feature_value|).
94   if (feature_value < smallest_values[7]) {
95     if (feature_value < smallest_values[3]) {
96       if (feature_value < smallest_values[1]) {
97         if (feature_value < smallest_values[0]) {
98           position = 0;
99         } else {
100           position = 1;
101         }
102       } else if (feature_value < smallest_values[2]) {
103         position = 2;
104       } else {
105         position = 3;
106       }
107     } else if (feature_value < smallest_values[5]) {
108       if (feature_value < smallest_values[4]) {
109         position = 4;
110       } else {
111         position = 5;
112       }
113     } else if (feature_value < smallest_values[6]) {
114       position = 6;
115     } else {
116       position = 7;
117     }
118   } else if (feature_value < smallest_values[15]) {
119     if (feature_value < smallest_values[11]) {
120       if (feature_value < smallest_values[9]) {
121         if (feature_value < smallest_values[8]) {
122           position = 8;
123         } else {
124           position = 9;
125         }
126       } else if (feature_value < smallest_values[10]) {
127         position = 10;
128       } else {
129         position = 11;
130       }
131     } else if (feature_value < smallest_values[13]) {
132       if (feature_value < smallest_values[12]) {
133         position = 12;
134       } else {
135         position = 13;
136       }
137     } else if (feature_value < smallest_values[14]) {
138       position = 14;
139     } else {
140       position = 15;
141     }
142   }
143 
144   // If we have detected a new small value, insert it at the correct position
145   // and shift larger values up.
146   if (position > -1) {
147     for (i = 15; i > position; i--) {
148       smallest_values[i] = smallest_values[i - 1];
149       age[i] = age[i - 1];
150     }
151     smallest_values[position] = feature_value;
152     age[position] = 1;
153   }
154 
155   // Get |current_median|.
156   if (self->frame_counter > 2) {
157     current_median = smallest_values[2];
158   } else if (self->frame_counter > 0) {
159     current_median = smallest_values[0];
160   }
161 
162   // Smooth the median value.
163   if (self->frame_counter > 0) {
164     if (current_median < self->mean_value[channel]) {
165       alpha = kSmoothingDown;  // 0.2 in Q15.
166     } else {
167       alpha = kSmoothingUp;  // 0.99 in Q15.
168     }
169   }
170   tmp32 = (alpha + 1) * self->mean_value[channel];
171   tmp32 += (WEBRTC_SPL_WORD16_MAX - alpha) * current_median;
172   tmp32 += 16384;
173   self->mean_value[channel] = (int16_t) (tmp32 >> 15);
174 
175   return self->mean_value[channel];
176 }
177