From 8264e376ea6f2fdaa3813ff1a8463e368d95083c Mon Sep 17 00:00:00 2001 From: Martin Storsjo Date: Fri, 10 Nov 2023 11:39:50 +0200 Subject: [PATCH] Saturate additions in getEnvSfbEnergy in SBR encoding This avoids wraparounds, which can trigger failed asserts with some inputs. This fixes https://github.com/mstorsjo/fdk-aac/issues/158. The implementation of saturated addition loses the contents of the lowest bit though, thus this change affects the output - but the change is said to be acceptable. --- libSBRenc/src/env_est.cpp | 4 ++-- test/ref-mono.txt | 4 ++-- test/ref-stereo.txt | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libSBRenc/src/env_est.cpp b/libSBRenc/src/env_est.cpp index cc8780a..e7eea37 100644 --- a/libSBRenc/src/env_est.cpp +++ b/libSBRenc/src/env_est.cpp @@ -637,8 +637,8 @@ static FIXP_DBL getEnvSfbEnergy( for (; l < stop_pos; l++) { nrg2 += YBuffer[l >> YBufferSzShift][k] >> sc1; } - accu1 += (nrg1 >> dynScale1); - accu2 += (nrg2 >> dynScale2); + accu1 = fAddSaturate(accu1, (nrg1 >> dynScale1)); + accu2 = fAddSaturate(accu2, (nrg2 >> dynScale2)); } /* This shift factor is always positive. See comment above. */ nrgSum += diff --git a/test/ref-mono.txt b/test/ref-mono.txt index 22a84f2..39fa6df 100644 --- a/test/ref-mono.txt +++ b/test/ref-mono.txt @@ -1,2 +1,2 @@ -encode hash: 516e51016cbba6068f1aa285a8fdd55b00ada6b0 -decode hash: 3b3b3a17cc5ba50ade75b3ef1a37f96017935fe5 +encode hash: 7f00e85a1eda67964e1b4044a5659a62e3952af2 +decode hash: af682b197a94e32fce09643857fa77b364574571 diff --git a/test/ref-stereo.txt b/test/ref-stereo.txt index 3de5276..a765c93 100644 --- a/test/ref-stereo.txt +++ b/test/ref-stereo.txt @@ -1,2 +1,2 @@ -encode hash: 1179940ca3a5c62de45b6823efbdbe42fce11de2 -decode hash: 85f2a154b46cc6cfa2792ff95c67b4200ce4463b +encode hash: 8411e71bf6b32576d287764cfa577ea0e9b73280 +decode hash: 30ad42bc97b53c03139cca5437b7d9a861de0e92