Skip to content

Fix post-echo artifacts by pre-clamping scalefactor deltas (Fixes #40)#100

Merged
fabiangreffrath merged 1 commit intoknik0:masterfrom
nschimme:preclamp-sf
Apr 15, 2026
Merged

Fix post-echo artifacts by pre-clamping scalefactor deltas (Fixes #40)#100
fabiangreffrath merged 1 commit intoknik0:masterfrom
nschimme:preclamp-sf

Conversation

@nschimme
Copy link
Copy Markdown
Contributor

@nschimme nschimme commented Apr 14, 2026

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

  • Pre-Quantization Clamping: 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) value while the encoder used the smaller (natural) gain. Moving the delta clamp inside qlevel()—prior to quantization—ensures strict mathematical synchronization between the bitstream and physical acoustics.
  • Refactored Gain Extraction: The mathematical clamping and Huffman overflow limits have been extracted into a single gain_with_overflow_clamp() helper to prevent peak clipping when the gain is artificially boosted by the clamp.
  • Preserved IS Stereo Biases: Variable names in 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 from AACstereo() are correctly tracked without corrupting the delta chain.
  • global_gain Simplification: Migrating the delta chain into qlevel() allowed for the cleanup of the bottom half of BlocQuant(). The global_gain is now cleanly derived from the first active band sequentially.

Verification

image

decoded-preclamp.wav.zip

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.
Copy link
Copy Markdown
Collaborator

@fabiangreffrath fabiangreffrath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@fabiangreffrath fabiangreffrath merged commit b0e56e9 into knik0:master Apr 15, 2026
6 checks passed
@nschimme
Copy link
Copy Markdown
Contributor Author

We have a rather epic 100 count PR for this too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sample with heavy distortion at default settings (and up to ~160kbps)

2 participants