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

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

Issue 2722533002: Fixing an overflow issue in NetEq::BackgroundNoise (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
11 #include "webrtc/modules/audio_coding/neteq/background_noise.h" 11 #include "webrtc/modules/audio_coding/neteq/background_noise.h"
12 12
13 #include <assert.h> 13 #include <assert.h>
14 #include <string.h> // memcpy 14 #include <string.h> // memcpy
15 15
16 #include <algorithm> // min, max 16 #include <algorithm> // min, max
17 #include <limits>
17 18
18 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h" 19 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h"
19 #include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h" 20 #include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
20 #include "webrtc/modules/audio_coding/neteq/cross_correlation.h" 21 #include "webrtc/modules/audio_coding/neteq/cross_correlation.h"
21 #include "webrtc/modules/audio_coding/neteq/post_decode_vad.h" 22 #include "webrtc/modules/audio_coding/neteq/post_decode_vad.h"
22 23
23 namespace webrtc { 24 namespace webrtc {
24 25
25 // static 26 // static
26 const size_t BackgroundNoise::kMaxLpcOrder; 27 const size_t BackgroundNoise::kMaxLpcOrder;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 fiter_output, lpc_coefficients, 95 fiter_output, lpc_coefficients,
95 kMaxLpcOrder + 1, kResidualLength); 96 kMaxLpcOrder + 1, kResidualLength);
96 int32_t residual_energy = WebRtcSpl_DotProductWithScale(fiter_output, 97 int32_t residual_energy = WebRtcSpl_DotProductWithScale(fiter_output,
97 fiter_output, 98 fiter_output,
98 kResidualLength, 99 kResidualLength,
99 0); 100 0);
100 101
101 // Check spectral flatness. 102 // Check spectral flatness.
102 // Comparing the residual variance with the input signal variance tells 103 // Comparing the residual variance with the input signal variance tells
103 // if the spectrum is flat or not. 104 // if the spectrum is flat or not.
104 // If 20 * residual_energy >= sample_energy << 6, the spectrum is flat 105 // If 5 * residual_energy >= sample_energy << 4, the spectrum is flat
105 // enough. Also ensure that the energy is non-zero. 106 // enough. Also ensure that the energy is non-zero.
106 if ((residual_energy * 20 >= (sample_energy << 6)) && 107 // If residual_energy is too large to fit in an int32_t after multiplying
108 // with 5, then simply skip the comparison, and consider the spectrum not
109 // flat enough.
110 constexpr int32_t kMaxEnergyToCompare =
111 std::numeric_limits<int32_t>::max() / 5;
112 if ((residual_energy <= kMaxEnergyToCompare) &&
113 (residual_energy * 5 >= (sample_energy << 4)) &&
107 (sample_energy > 0)) { 114 (sample_energy > 0)) {
kwiberg-webrtc 2017/02/28 10:31:35 Hmm. Wouldn't it make more sense to have (resid
hlundin-webrtc 2017/02/28 12:15:32 I went for the int64_t{x} suggestion. Thanks!
108 // Spectrum is flat enough; save filter parameters. 115 // Spectrum is flat enough; save filter parameters.
109 // |temp_signal| + |kVecLen| - |kMaxLpcOrder| points at the first of the 116 // |temp_signal| + |kVecLen| - |kMaxLpcOrder| points at the first of the
110 // |kMaxLpcOrder| samples in the residual signal, which will form the 117 // |kMaxLpcOrder| samples in the residual signal, which will form the
111 // filter state for the next noise generation. 118 // filter state for the next noise generation.
112 SaveParameters(channel_ix, lpc_coefficients, 119 SaveParameters(channel_ix, lpc_coefficients,
113 temp_signal + kVecLen - kMaxLpcOrder, sample_energy, 120 temp_signal + kVecLen - kMaxLpcOrder, sample_energy,
114 residual_energy); 121 residual_energy);
115 } 122 }
116 } else { 123 } else {
117 // Will only happen if post-decode VAD is disabled and |sample_energy| is 124 // Will only happen if post-decode VAD is disabled and |sample_energy| is
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 // Add 13 to the |scale_shift_|, since the random numbers table is in 253 // Add 13 to the |scale_shift_|, since the random numbers table is in
247 // Q13. 254 // Q13.
248 // TODO(hlundin): Move the "13" to where the |scale_shift_| is used? 255 // TODO(hlundin): Move the "13" to where the |scale_shift_| is used?
249 parameters.scale_shift = 256 parameters.scale_shift =
250 static_cast<int16_t>(13 + ((kLogResidualLength + norm_shift) / 2)); 257 static_cast<int16_t>(13 + ((kLogResidualLength + norm_shift) / 2));
251 258
252 initialized_ = true; 259 initialized_ = true;
253 } 260 }
254 261
255 } // namespace webrtc 262 } // 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