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

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

Issue 2718943004: Avoid overflow in NetEq's TimeStretch::SpeechDetection (Closed)
Patch Set: Created 3 years, 9 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 | « no previous file | 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
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 bool TimeStretch::SpeechDetection(int32_t vec1_energy, int32_t vec2_energy, 175 bool TimeStretch::SpeechDetection(int32_t vec1_energy, int32_t vec2_energy,
176 size_t peak_index, int scaling) const { 176 size_t peak_index, int scaling) const {
177 // 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).
178 // If (vec1_energy + vec2_energy) / (2 * peak_index) <= 178 // If (vec1_energy + vec2_energy) / (2 * peak_index) <=
179 // 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
180 // active speech. 180 // active speech.
181 // Rewrite the inequality as: 181 // Rewrite the inequality as:
182 // (vec1_energy + vec2_energy) / 16 <= peak_index * background_noise_energy. 182 // (vec1_energy + vec2_energy) / 16 <= peak_index * background_noise_energy.
183 // The two sides of the inequality will be denoted |left_side| and 183 // The two sides of the inequality will be denoted |left_side| and
184 // |right_side|. 184 // |right_side|.
185 int32_t left_side = (vec1_energy + vec2_energy) / 16; 185 int32_t left_side = rtc::saturated_cast<int32_t>(
186 (static_cast<int64_t>(vec1_energy) + vec2_energy) / 16);
186 int32_t right_side; 187 int32_t right_side;
187 if (background_noise_.initialized()) { 188 if (background_noise_.initialized()) {
188 right_side = background_noise_.Energy(master_channel_); 189 right_side = background_noise_.Energy(master_channel_);
189 } else { 190 } else {
190 // If noise parameters have not been estimated, use a fixed threshold. 191 // If noise parameters have not been estimated, use a fixed threshold.
191 right_side = 75000; 192 right_side = 75000;
192 } 193 }
193 int right_scale = 16 - WebRtcSpl_NormW32(right_side); 194 int right_scale = 16 - WebRtcSpl_NormW32(right_side);
194 right_scale = std::max(0, right_scale); 195 right_scale = std::max(0, right_scale);
195 left_side = left_side >> right_scale; 196 left_side = left_side >> right_scale;
196 right_side = 197 right_side =
197 rtc::checked_cast<int32_t>(peak_index) * (right_side >> right_scale); 198 rtc::checked_cast<int32_t>(peak_index) * (right_side >> right_scale);
198 199
199 // Scale |left_side| properly before comparing with |right_side|. 200 // Scale |left_side| properly before comparing with |right_side|.
200 // (|scaling| is the scale factor before energy calculation, thus the scale 201 // (|scaling| is the scale factor before energy calculation, thus the scale
201 // factor for the energy is 2 * scaling.) 202 // factor for the energy is 2 * scaling.)
202 if (WebRtcSpl_NormW32(left_side) < 2 * scaling) { 203 if (WebRtcSpl_NormW32(left_side) < 2 * scaling) {
203 // Cannot scale only |left_side|, must scale |right_side| too. 204 // Cannot scale only |left_side|, must scale |right_side| too.
204 int temp_scale = WebRtcSpl_NormW32(left_side); 205 int temp_scale = WebRtcSpl_NormW32(left_side);
205 left_side = left_side << temp_scale; 206 left_side = left_side << temp_scale;
206 right_side = right_side >> (2 * scaling - temp_scale); 207 right_side = right_side >> (2 * scaling - temp_scale);
207 } else { 208 } else {
208 left_side = left_side << 2 * scaling; 209 left_side = left_side << 2 * scaling;
209 } 210 }
210 return left_side > right_side; 211 return left_side > right_side;
211 } 212 }
212 213
213 } // namespace webrtc 214 } // namespace webrtc
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698