Fix mdhd/mvhd version 1 (64-bit) atom parsing — half duration bug#2626
Open
tjiddy wants to merge 1 commit intoBorewit:masterfrom
Open
Fix mdhd/mvhd version 1 (64-bit) atom parsing — half duration bug#2626tjiddy wants to merge 1 commit intoBorewit:masterfrom
tjiddy wants to merge 1 commit intoBorewit:masterfrom
Conversation
Version 1 mdhd and mvhd atoms use 64-bit fields for creation time, modification time, and duration, shifting all subsequent field offsets by 12 bytes compared to version 0. The parser was hardcoded for version 0 offsets only, causing it to read the wrong bytes for timeScale and duration on version 1 atoms. This resulted in exactly half the correct duration being reported for M4B audiobook files that use version 1 mdhd atoms (typically files produced by Audible/Libation with 22050 Hz sample rate). The fix checks the version field and uses the correct offsets for each version. Adds a 64-bit SecondsSinceMacEpoch token for the larger timestamp fields in version 1 atoms.
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.
Summary
Fixes incorrect duration parsing for MP4/M4B files that use version 1 (64-bit)
mdhdandmvhdatoms. These atoms report exactly half the correct duration because the parser was hardcoded for version 0 field offsets only.Root Cause
The ISO 14496-12 spec defines two versions of
mdhd/mvhd:Version 1 shifts the
timeScalefield from offset 12 to offset 20, anddurationfrom offset 16 (4 bytes) to offset 24 (8 bytes). The parser read all fields at version 0 offsets regardless of version, causing it to read incorrect values from the middle of 64-bit fields.Evidence
Tested with 6 M4B audiobook files. All version 1 files reported exactly half duration; all version 0 files were correct:
Changes
MdhdAtom.get(): Check version field; use 64-bit offsets andUINT64_BEfor version 1MvhdAtom.get(): Same version-aware parsing (same bug, same fix)SecondsSinceMacEpoch64token for 64-bit timestamp fields in version 1 atomsTests
All 568 existing tests pass. No regressions.