feat(mp4): parse and preserve the tkhd transformation matrix - #538
Merged
Conversation
nchitkara-xai
force-pushed
the
tkhd-preserve-matrix
branch
from
July 25, 2026 01:46
4719986 to
b33878f
Compare
tobbee
approved these changes
Jul 25, 2026
tobbee
left a comment
Collaborator
There was a problem hiding this comment.
@nchitkara-xai Thanks for the PR. Looks good to me, but as you noted, the same matrix operations should apply to mdhd.
DecodeTkhdSR skipped the matrix and EncodeSW always wrote the unity matrix, so rotation set by phone cameras was lost on a decode/encode round trip. Parse it into TkhdBox.Matrix and encode it back. A zero-value Matrix still encodes as unity for backwards compatibility.
tobbee
force-pushed
the
tkhd-preserve-matrix
branch
from
July 25, 2026 16:24
b33878f to
fbcf4eb
Compare
Collaborator
|
One may also want to output the matrix (at least the non-trivial one) in the Info() output. |
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.
DecodeTkhdSR skips the 36-byte transformation matrix and EncodeSW always writes the unity matrix, so rotation set by phone cameras is lost on a decode/encode round trip and cannot be read programmatically.
This parses the matrix into a new TkhdBox.Matrix [9]int32 field and encodes it back. A zero-value Matrix still encodes as the unity matrix, so existing code that builds TkhdBox directly gives the same output as before. CreateTkhd sets the new UnityMatrix() explicitly.
One consequence is that an all-zero matrix read from a file comes back as unity. A zero matrix is degenerate, so this seemed like the right trade-off for backwards compatibility, but happy to change it. mvhd skips its matrix in the same way and could get the same treatment in a follow-up.