Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: webrtc/modules/audio_coding/neteq/time_stretch.cc

Issue 1908623002: Avoiding overflow in cross correlation in NetEq. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: turn off ubsan as it was Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/modules/audio_coding/neteq/neteq.gypi ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_coding/neteq/time_stretch.h" 11 #include "webrtc/modules/audio_coding/neteq/time_stretch.h"
12 12
13 #include <algorithm> // min, max 13 #include <algorithm> // min, max
14 #include <memory> 14 #include <memory>
15 15
16 #include "webrtc/base/safe_conversions.h" 16 #include "webrtc/base/safe_conversions.h"
17 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h" 17 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h"
18 #include "webrtc/modules/audio_coding/neteq/background_noise.h" 18 #include "webrtc/modules/audio_coding/neteq/background_noise.h"
19 #include "webrtc/modules/audio_coding/neteq/cross_correlation.h"
19 #include "webrtc/modules/audio_coding/neteq/dsp_helper.h" 20 #include "webrtc/modules/audio_coding/neteq/dsp_helper.h"
20 21
21 namespace webrtc { 22 namespace webrtc {
22 23
23 TimeStretch::ReturnCodes TimeStretch::Process(const int16_t* input, 24 TimeStretch::ReturnCodes TimeStretch::Process(const int16_t* input,
24 size_t input_len, 25 size_t input_len,
25 bool fast_mode, 26 bool fast_mode,
26 AudioMultiVector* output, 27 AudioMultiVector* output,
27 size_t* length_change_samples) { 28 size_t* length_change_samples) {
28 // Pre-calculate common multiplication with |fs_mult_|. 29 // Pre-calculate common multiplication with |fs_mult_|.
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 break; 152 break;
152 case kNoStretch: 153 case kNoStretch:
153 case kError: 154 case kError:
154 *length_change_samples = 0; 155 *length_change_samples = 0;
155 break; 156 break;
156 } 157 }
157 return return_value; 158 return return_value;
158 } 159 }
159 160
160 void TimeStretch::AutoCorrelation() { 161 void TimeStretch::AutoCorrelation() {
161 // Set scaling factor for cross correlation to protect against overflow.
162 int scaling = kLogCorrelationLen - WebRtcSpl_NormW32(
163 max_input_value_ * max_input_value_);
164 scaling = std::max(0, scaling);
165
166 // Calculate correlation from lag kMinLag to lag kMaxLag in 4 kHz domain. 162 // Calculate correlation from lag kMinLag to lag kMaxLag in 4 kHz domain.
167 int32_t auto_corr[kCorrelationLen]; 163 int32_t auto_corr[kCorrelationLen];
168 WebRtcSpl_CrossCorrelation(auto_corr, &downsampled_input_[kMaxLag], 164 CrossCorrelationWithAutoShift(
169 &downsampled_input_[kMaxLag - kMinLag], 165 &downsampled_input_[kMaxLag], &downsampled_input_[kMaxLag - kMinLag],
170 kCorrelationLen, kMaxLag - kMinLag, scaling, -1); 166 kCorrelationLen, kMaxLag - kMinLag, -1, auto_corr);
171 167
172 // Normalize correlation to 14 bits and write to |auto_correlation_|. 168 // Normalize correlation to 14 bits and write to |auto_correlation_|.
173 int32_t max_corr = WebRtcSpl_MaxAbsValueW32(auto_corr, kCorrelationLen); 169 int32_t max_corr = WebRtcSpl_MaxAbsValueW32(auto_corr, kCorrelationLen);
174 scaling = std::max(0, 17 - WebRtcSpl_NormW32(max_corr)); 170 int scaling = std::max(0, 17 - WebRtcSpl_NormW32(max_corr));
175 WebRtcSpl_VectorBitShiftW32ToW16(auto_correlation_, kCorrelationLen, 171 WebRtcSpl_VectorBitShiftW32ToW16(auto_correlation_, kCorrelationLen,
176 auto_corr, scaling); 172 auto_corr, scaling);
177 } 173 }
178 174
179 bool TimeStretch::SpeechDetection(int32_t vec1_energy, int32_t vec2_energy, 175 bool TimeStretch::SpeechDetection(int32_t vec1_energy, int32_t vec2_energy,
180 size_t peak_index, int scaling) const { 176 size_t peak_index, int scaling) const {
181 // Check if the signal seems to be active speech or not (simple VAD). 177 // Check if the signal seems to be active speech or not (simple VAD).
182 // If (vec1_energy + vec2_energy) / (2 * peak_index) <= 178 // If (vec1_energy + vec2_energy) / (2 * peak_index) <=
183 // 8 * background_noise_energy, then we say that the signal contains no 179 // 8 * background_noise_energy, then we say that the signal contains no
184 // active speech. 180 // active speech.
(...skipping 23 matching lines...) Expand all
208 int temp_scale = WebRtcSpl_NormW32(left_side); 204 int temp_scale = WebRtcSpl_NormW32(left_side);
209 left_side = left_side << temp_scale; 205 left_side = left_side << temp_scale;
210 right_side = right_side >> (2 * scaling - temp_scale); 206 right_side = right_side >> (2 * scaling - temp_scale);
211 } else { 207 } else {
212 left_side = left_side << 2 * scaling; 208 left_side = left_side << 2 * scaling;
213 } 209 }
214 return left_side > right_side; 210 return left_side > right_side;
215 } 211 }
216 212
217 } // namespace webrtc 213 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_coding/neteq/neteq.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698