Fix post-echo artifacts by pre-clamping scalefactor deltas (Fixes #40)#100
Merged
fabiangreffrath merged 1 commit intoknik0:masterfrom Apr 15, 2026
Merged
Fix post-echo artifacts by pre-clamping scalefactor deltas (Fixes #40)#100fabiangreffrath merged 1 commit intoknik0:masterfrom
fabiangreffrath merged 1 commit intoknik0:masterfrom
Conversation
Problem: The post-loop delta clamping in BlocQuant() modified sf[] after qlevel() had already quantized the spectral data. When a negative delta exceeded -SF_DELTA, the decoder reconstructed using the clamped (larger) sf value while the encoder used the smaller (natural) gain. This gain mismatch caused post-echo artifacts in quiet bands following loud bands. Solution: Relocate the delta clamp inside qlevel(), prior to the quantization step, ensuring the encoder and decoder always operate on identical gain values. To support this, gain calculations and Huffman overflow limits are extracted into a single gain_with_overflow_clamp() helper. Variable names are updated (sf_rel, sf_abs) to explicitly distinguish between relative bitstream values and absolute energy levels, ensuring the pre-loaded IS stereo biases from AACstereo() are safely preserved.
fabiangreffrath
approved these changes
Apr 15, 2026
Collaborator
fabiangreffrath
left a comment
There was a problem hiding this comment.
I have to admit that I don't understand most of the changes introduced here, but I trust you (and Claude 😉) and the results enough to just wave this through.
Contributor
Author
|
We have a rather epic 100 count PR for this too |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR eliminates the post-echo artifacts that occur in quiet bands immediately following loud bands. It resolves an architectural bug where the encoder and decoder were applying different gain values due to delayed scalefactor delta clamping.
Fixes #40
Architectural Changes
BlocQuant()modifiedsf[]afterqlevel()had already quantized the spectral data. When a negative delta exceeded-SF_DELTA, the decoder reconstructed using the clamped (larger) value while the encoder used the smaller (natural) gain. Moving the delta clamp insideqlevel()—prior to quantization—ensures strict mathematical synchronization between the bitstream and physical acoustics.gain_with_overflow_clamp()helper to prevent peak clipping when the gain is artificially boosted by the clamp.qlevel()have been updated (sf_rel,sf_abs) to explicitly distinguish between relative bitstream values and absolute energy levels. This ensures the pre-loaded IS stereo biases fromAACstereo()are correctly tracked without corrupting the delta chain.global_gainSimplification: Migrating the delta chain intoqlevel()allowed for the cleanup of the bottom half ofBlocQuant(). Theglobal_gainis now cleanly derived from the first active band sequentially.Verification
decoded-preclamp.wav.zip