Skip to content

fix(mvhd, tkhd): treat volume as 8.8 fixed point - #589

Merged
rbouqueau merged 2 commits into
gpac:mainfrom
zzoraemon043:fix/header-volume-fixed-point
Nov 22, 2026
Merged

rbouqueau merged 2 commits into
gpac:mainfrom
zzoraemon043:fix/header-volume-fixed-point

Conversation

@zzoraemon043

@zzoraemon043 zzoraemon043 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

volume in mvhd and tkhd is 8.8 fixed point, but it was handled as an integer in three places:

  • parse() did >> 8, so 0.5 (0x0080) was read as 0 and written back as 0.
  • print() shifted the already-shifted value again, so full volume printed as volume: 0.
  • init() set mvhd.volume = 0x0100, and write() did << 8, so writeUint16(0x10000) stored 0. Files created with createFile().init() / addTrack() without a width got a muted mvhd.

Now volume is a plain number (1 = full volume, as getInfo() already returns), read with / 256 and written with Math.round(volume * 256). Integer volumes read the same as before.

tests/header-volume.test.ts covers reading, printing and rewriting both boxes, and the mvhd bytes written after init(). 6 of its 8 tests fail before the change. vitest run (Node 22): 529 passed. eslint, prettier --check and npm run types pass.

Reference: ISO/IEC 14496-12, 8.2.2 (MovieHeaderBox) and 8.3.2 (TrackHeaderBox), template int(16) volume = 0x0100 as a fixed 8.8 number.

@changeset-bot

changeset-bot Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0bd56fa

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
mp4box Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@rbouqueau

Copy link
Copy Markdown
Member

Thank you. I'm a bit concerned if we add one full test and 100+ lines of code each time we fix a bug. Tests are good but adding one at each PR fixing one field in a box is not sustainable.

Opinions?

@zzoraemon043

Copy link
Copy Markdown
Contributor Author

Fair point. Replaced the 104-line file with one 16-line round-trip case in file.test.ts (0bd56fa): build a file, write it, read it back, check mvhd 1 and tkhd 0.5. It fails on main. Happy to drop it if you'd rather not have a test per field fix.

@rbouqueau

Copy link
Copy Markdown
Member

Thank you so much. Merging.

@rbouqueau
rbouqueau added this pull request to the merge queue Nov 22, 2026
Merged via the queue into gpac:main with commit 1624d2c Nov 22, 2026
1 check passed
@github-actions github-actions Bot mentioned this pull request Nov 22, 2026
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.

2 participants