Skip to content

feat(mp4): parse and preserve the tkhd transformation matrix - #538

Merged
tobbee merged 1 commit into
Eyevinn:masterfrom
nchitkara-xai:tkhd-preserve-matrix
Jul 25, 2026
Merged

feat(mp4): parse and preserve the tkhd transformation matrix#538
tobbee merged 1 commit into
Eyevinn:masterfrom
nchitkara-xai:tkhd-preserve-matrix

Conversation

@nchitkara-xai

@nchitkara-xai nchitkara-xai commented Jul 25, 2026

Copy link
Copy Markdown

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.

@nchitkara-xai
nchitkara-xai force-pushed the tkhd-preserve-matrix branch from 4719986 to b33878f Compare July 25, 2026 01:46

@tobbee tobbee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@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
tobbee force-pushed the tkhd-preserve-matrix branch from b33878f to fbcf4eb Compare July 25, 2026 16:24
@tobbee

tobbee commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

One may also want to output the matrix (at least the non-trivial one) in the Info() output.

@tobbee
tobbee merged commit 9dbc0bd into Eyevinn:master Jul 25, 2026
9 checks passed
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