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

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

Issue 2571483002: Fix for integer overflow in NetEq. (Closed)
Patch Set: More comments. Created 4 years 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
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 657 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 &(audio_history[signal_length - kUnvoicedLpcOrder]), 668 &(audio_history[signal_length - kUnvoicedLpcOrder]),
669 sizeof(int16_t) * kUnvoicedLpcOrder); 669 sizeof(int16_t) * kUnvoicedLpcOrder);
670 memcpy(unvoiced_vector - kUnvoicedLpcOrder, 670 memcpy(unvoiced_vector - kUnvoicedLpcOrder,
671 &(audio_history[signal_length - 128 - kUnvoicedLpcOrder]), 671 &(audio_history[signal_length - 128 - kUnvoicedLpcOrder]),
672 sizeof(int16_t) * kUnvoicedLpcOrder); 672 sizeof(int16_t) * kUnvoicedLpcOrder);
673 WebRtcSpl_FilterMAFastQ12(&audio_history[signal_length - 128], 673 WebRtcSpl_FilterMAFastQ12(&audio_history[signal_length - 128],
674 unvoiced_vector, 674 unvoiced_vector,
675 parameters.ar_filter, 675 parameters.ar_filter,
676 kUnvoicedLpcOrder + 1, 676 kUnvoicedLpcOrder + 1,
677 128); 677 128);
678 int16_t unvoiced_prescale; 678 const int unvoiced_max_abs = [&] {
679 if (WebRtcSpl_MaxAbsValueW16(unvoiced_vector, 128) > 4000) { 679 const int16_t max_abs = WebRtcSpl_MaxAbsValueW16(unvoiced_vector, 128);
680 unvoiced_prescale = 4; 680 // Since WebRtcSpl_MaxAbsValueW16 returns 2^15 - 1 when the input contains
681 } else { 681 // -2^15, we have to conservatively bump the return value by 1
682 unvoiced_prescale = 0; 682 // if it is 2^15 - 1.
683 } 683 return max_abs == WEBRTC_SPL_WORD16_MAX ? max_abs + 1 : max_abs;
684 }();
685 // Pick the smallest n such that 2^n > unvoiced_max_abs; then the maximum
686 // value of the dot product is less than 2^7 * 2^(2*n) = 2^(2*n + 7), so to
687 // prevent overflows we want 2n + 7 <= 31, which means we should shift by
688 // 2n + 7 - 31 bits, if this value is greater than zero.
689 int unvoiced_prescale =
690 std::max(0, 2 * WebRtcSpl_GetSizeInBits(unvoiced_max_abs) - 24);
691
684 int32_t unvoiced_energy = WebRtcSpl_DotProductWithScale(unvoiced_vector, 692 int32_t unvoiced_energy = WebRtcSpl_DotProductWithScale(unvoiced_vector,
685 unvoiced_vector, 693 unvoiced_vector,
686 128, 694 128,
687 unvoiced_prescale); 695 unvoiced_prescale);
688 696
689 // Normalize |unvoiced_energy| to 28 or 29 bits to preserve sqrt() accuracy. 697 // Normalize |unvoiced_energy| to 28 or 29 bits to preserve sqrt() accuracy.
690 int16_t unvoiced_scale = WebRtcSpl_NormW32(unvoiced_energy) - 3; 698 int16_t unvoiced_scale = WebRtcSpl_NormW32(unvoiced_energy) - 3;
691 // Make sure we do an odd number of shifts since we already have 7 shifts 699 // Make sure we do an odd number of shifts since we already have 7 shifts
692 // from dividing with 128 earlier. This will make the total scale factor 700 // from dividing with 128 earlier. This will make the total scale factor
693 // even, which is suitable for the sqrt. 701 // even, which is suitable for the sqrt.
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 const size_t kMaxRandSamples = RandomVector::kRandomTableSize; 969 const size_t kMaxRandSamples = RandomVector::kRandomTableSize;
962 while (samples_generated < length) { 970 while (samples_generated < length) {
963 size_t rand_length = std::min(length - samples_generated, kMaxRandSamples); 971 size_t rand_length = std::min(length - samples_generated, kMaxRandSamples);
964 random_vector_->IncreaseSeedIncrement(seed_increment); 972 random_vector_->IncreaseSeedIncrement(seed_increment);
965 random_vector_->Generate(rand_length, &random_vector[samples_generated]); 973 random_vector_->Generate(rand_length, &random_vector[samples_generated]);
966 samples_generated += rand_length; 974 samples_generated += rand_length;
967 } 975 }
968 } 976 }
969 977
970 } // namespace webrtc 978 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_coding/acm2/audio_coding_module_unittest.cc ('k') | webrtc/modules/audio_coding/neteq/neteq_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698