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