fix(codecs): skip RIFF pad byte after odd-sized WAV chunks#6452
Open
chuenchen309 wants to merge 1 commit into
Open
fix(codecs): skip RIFF pad byte after odd-sized WAV chunks#6452chuenchen309 wants to merge 1 commit into
chuenchen309 wants to merge 1 commit into
Conversation
RIFF chunks are word-aligned: a chunk whose size is odd is followed by a single pad byte that is not counted in the chunk size. The inline WAV decoder consumed exactly `chunk_size` bytes for skipped ancillary chunks (LIST/bext/cue/...) and for the fmt chunk, then read the next chunk header immediately — one byte too early whenever the preceding chunk was odd. The misaligned header then parses garbage, so the real `data` chunk is never found and every audio frame is silently dropped (0 frames, no error). This bites any spec-compliant WAV that carries an odd-length metadata chunk before `data`, which tools like ffmpeg/sox routinely produce. Fold the pad byte into the skip length for ancillary chunks, and skip the trailing pad byte for an odd-sized fmt chunk, so the next header stays word-aligned. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
What
RIFF chunks are word-aligned: a chunk whose size is odd is followed by a single pad byte that is not counted in the chunk size. The inline WAV decoder (
_WavInlineDecoder) consumed exactlychunk_sizebytes for skipped ancillary chunks (LIST/bext/cue/...) and for thefmtchunk, then read the next chunk header immediately — one byte too early whenever the preceding chunk was odd. The misaligned header parses garbage, so the realdatachunk is never found and every frame is silently dropped (0 frames, no error).This hits any spec-compliant WAV that carries an odd-length metadata chunk before
data, which tools like ffmpeg/sox routinely emit.Repro (before fix):
Fix
Fold the pad byte into the skip length for ancillary chunks (
chunk_size + (chunk_size & 1)), and skip the trailing pad byte for an odd-sizedfmtchunk, so the following chunk header stays word-aligned.Testing
test_wav_inline_decoder_odd_ancillary_chunk(even control + odd case, fed byte-by-byte to exercise the incremental state machine across the pad byte) — red on the odd case before the fix, green after.tests/test_audio_decoder.pyunit suite: 15 passed, 1 skipped (cloud-cred test), no regressions.make check(ruff format-check + ruff lint + strict mypy) clean.AI disclosure
Investigated and implemented with Claude Code (Claude Opus 4.8); I reviewed the diff, wrote and ran the repro, ran the full test suite and
make checkmyself before opening this PR.