Skip to content

fix(mp4): separate sample duration defaults from run agreement - #729

Merged
tobbee merged 1 commit into
Eyevinn:masterfrom
perfloop-oss:perfloop-pr-open-7qrep4vmat
Nov 23, 2026
Merged

tobbee merged 1 commit into
Eyevinn:masterfrom
perfloop-oss:perfloop-pr-open-7qrep4vmat

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Common-duration lookup can reject uniformly timed samples when their explicit duration differs from a header default. It can also reuse a previous run's observed duration as an inherited default, accepting samples with different effective durations. Empty runs can make a valid fragment fail or report a duration without any samples.

Keep the header fallback separate from agreement between nonempty runs in Fragment.CommonSampleDuration. Explicit sample durations take precedence; a flagged tfhd default, including zero, takes precedence over trex. Empty runs are ignored when combining runs. A fragment without samples returns an error, which propagates through MediaSegment.CommonSampleDuration and fails the whole segment. Zero or differing effective durations still produce an error at fragment level.

A public caller is DASH-IF livesim2's RepData.readMP4Segment: s.CommonSampleDuration(r.initSeg.Moov.Mvex.Trex). It stores the duration when the call succeeds.

The tests in mp4/fragment_test.go build fragments in memory, then check the public segment lookup and unchanged sample fields. They cover header precedence, including a nonzero unflagged tfhd value, the explicit-then-inherited-zero order, empty runs, and agreeing or differing two-fragment segments.

Go duration and fragment-interval tests pass. Scoped static analysis and changed-file validators pass. make build, make test and make check were not run for this change.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

@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.

Thanks for this. The fix in Fragment.CommonSampleDuration is right: the precedence is now trex, then a flagged tfhd default, then explicit trun durations, and a previous run's duration no longer leaks into the next run as its default. I ran the new tests against master's code to see what they catch. The points below are what I would like changed before merging.

Code

  1. Drop the change to TrunBox.CommonSampleDuration. It is not needed for the fix: with master's mp4/trun.go and this PR's mp4/fragment.go, every case in TestFragmentCommonSampleDurationDefaults passes, since the fragment loop already skips empty runs. It changes the result of a public method for an empty run without the sample-duration flag, and I see no reason for that. Please revert mp4/trun.go, the two cases added to TestCommonDuration, and the second half of the changelog entry.

  2. Say in the changelog entry that a fragment without samples now gives an error. A traf with no runs, or with only empty runs, used to return the header default and now returns "no common sample duration". That is fine, but users of MediaSegment.CommonSampleDuration will notice it, since one such fragment fails the whole segment.

Tests

Of the 18 fragment cases, 8 fail on master and 10 pass both before and after the change. Some of those 10 are worth keeping as precedence checks, but the table can be about half as long.

  1. "unflagged tfhd ignored" does not test what its name says. The tfhd value 2 is set without the flag, so Encode does not write it and the decoded tfhd holds 0. The case only tests that trex is used.

  2. The byte comparison after the lookup cannot fail for the mutation it guards against. Filling defaults into trun.Samples, as AddSampleDefaultValues does, leaves the encoded bytes unchanged, because fields without their flag are not written. Please remove it, or compare the samples instead.

  3. Build the fragment in memory instead of encoding and decoding it. The round trip is most of the test body, it is not what is being tested, and it causes point 3.

  4. Keep one run order per pair, except where the order mattered. Only "explicit then inherited zero" depended on the order in the old code. "inherited zero then explicit", one of the two "equal" cases, one of the two "different" cases and "inherited then empty" add nothing.

  5. Leave run-level behaviour to TestCommonDuration. "differing explicit durations" is already covered there, and "explicit zero" is the same kind of check.

  6. Add a case with two fragments. MediaSegment.CommonSampleDuration is the function the tests call, but no case has more than one fragment. One segment where the fragments reach the same duration in different ways (a tfhd default in one, explicit durations in the other), and one where they differ, would cover it.

A set like this would be enough: explicit over trex, explicit over tfhd, tfhd over trex, flagged tfhd 0 over trex, trex only, explicit then inherited zero, explicit and inherited that differ, an empty run together with a non-empty one, only empty runs, plus the two-fragment cases.

Commits

  1. The test commit comes before the fix and fails on its own. Pull requests are rebase-merged here, so please squash the two commits or put the fix first.

@perfloop
perfloop Bot force-pushed the perfloop-pr-open-7qrep4vmat branch from 80b20cf to 25d1645 Compare November 23, 2026 02:21
@perfloop-agent

Copy link
Copy Markdown
Contributor Author

Reverted the run-level change and replaced the round trips with the compact in-memory cases you described. The tests now compare sample fields, keep a nonzero unflagged tfhd value, and cover agreeing and differing two-fragment segments; the changelog names the sample-less fragment error and its propagation. The contribution is squashed into one passing commit.

@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.

Thanks, all points are addressed. I ran the tests against master's code and against mutants of the fix: every case that should fail does, including the sample comparison and the two-fragment cases.

@tobbee
tobbee merged commit a8d959d into Eyevinn:master Nov 23, 2026
11 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