From fe06dbdfa23171882955f72b6fa10ced9a367860 Mon Sep 17 00:00:00 2001 From: Ivo Creusen Date: Tue, 12 Jan 2021 16:01:30 +0100 Subject: [PATCH] Correction for the calculation of the abs max value The abs max of a 16 bit integer cannot be represented as a 16 bit integer, because abs(-2^16) is too large. To work around this, we can instead use the index of the max element, convert it to a 32-bit int and then take the absolute value. Bug: chromium:1158070, chromium:1146835, chromium:1161837 Change-Id: If56177c55ec62b4bd578986a5deae38a91bbc821 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/198123 Commit-Queue: Ivo Creusen Reviewed-by: Henrik Lundin Cr-Commit-Position: refs/heads/master@{#32950} --- modules/audio_coding/codecs/ilbc/enhancer_interface.c | 4 +++- modules/audio_coding/neteq/cross_correlation.cc | 7 ++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/audio_coding/codecs/ilbc/enhancer_interface.c b/modules/audio_coding/codecs/ilbc/enhancer_interface.c index 55a4105561..fb9740eb22 100644 --- a/modules/audio_coding/codecs/ilbc/enhancer_interface.c +++ b/modules/audio_coding/codecs/ilbc/enhancer_interface.c @@ -203,7 +203,9 @@ size_t // (o) Estimated lag in end of in[] regressor=in+tlag-1; /* scaling */ - max16 = WebRtcSpl_MaxAbsValueW16(regressor, plc_blockl + 3 - 1); + // Note that this is not abs-max, but it doesn't matter since we use only + // the square of it. + max16 = regressor[WebRtcSpl_MaxAbsIndexW16(regressor, plc_blockl + 3 - 1)]; const int64_t max_val = plc_blockl * max16 * max16; const int32_t factor = max_val >> 31; diff --git a/modules/audio_coding/neteq/cross_correlation.cc b/modules/audio_coding/neteq/cross_correlation.cc index 895fea32d1..7ee867aa9b 100644 --- a/modules/audio_coding/neteq/cross_correlation.cc +++ b/modules/audio_coding/neteq/cross_correlation.cc @@ -26,15 +26,16 @@ int CrossCorrelationWithAutoShift(const int16_t* sequence_1, int cross_correlation_step, int32_t* cross_correlation) { // Find the maximum absolute value of sequence_1 and 2. - const int16_t max_1 = WebRtcSpl_MaxAbsValueW16(sequence_1, sequence_1_length); + const int32_t max_1 = + abs(sequence_1[WebRtcSpl_MaxAbsIndexW16(sequence_1, sequence_1_length)]); const int sequence_2_shift = cross_correlation_step * (static_cast(cross_correlation_length) - 1); const int16_t* sequence_2_start = sequence_2_shift >= 0 ? sequence_2 : sequence_2 + sequence_2_shift; const size_t sequence_2_length = sequence_1_length + std::abs(sequence_2_shift); - const int16_t max_2 = - WebRtcSpl_MaxAbsValueW16(sequence_2_start, sequence_2_length); + const int32_t max_2 = abs(sequence_2_start[WebRtcSpl_MaxAbsIndexW16( + sequence_2_start, sequence_2_length)]); // In order to avoid overflow when computing the sum we should scale the // samples so that (in_vector_length * max_1 * max_2) will not overflow.