Skip to content

fix(demux): reconstruct clamped Matroska H264 timestamps - #762

Merged
superuser404notfound merged 2 commits into
superuser404notfound:mainfrom
oxyc:fix/matroska-h264-frame-cadence
Oct 14, 2026
Merged

superuser404notfound merged 2 commits into
superuser404notfound:mainfrom
oxyc:fix/matroska-h264-frame-cadence

Conversation

@oxyc

@oxyc oxyc commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Reconstruct clamped timestamps in narrowly identified CFR H.264 Matroska streams. The affected files contain duplicated timestamp slots and missing CFR slots,
producing alternating 0 ms / 80 ms presentation intervals in 25 fps video.

What changed

  • Detect the affected shape using an exact whole-tick cadence, valid picture-order ranks, and multiple corroborating one-frame clamps.
  • Reconstruct PTS and DTS from the IDR anchor, picture order, and decode order.
  • Reanchor after seeks and IDRs, and fail closed on overflow or any unexpected timestamp shape.
  • Add regression coverage for missing DTS and independent one-frame-forward PTS/DTS clamps.

Test plan

  • Device / OS: Apple TV 4K (3rd generation, AppleTV14,1), tvOS 27.0 (24J361)

  • Source media (container / video codec + profile / audio / HDR-DV): Matroska, 1920×1080 at 25 fps / H.264 [email protected] / AC-3 48 kHz stereo / SDR

  • Result: The affected stream changed from persistent alternating 0 ms / 80 ms decoded-frame intervals to a steady 40 ms cadence and was visually smooth. Native
    playback requested 25 fps on the 50 Hz display with no rebuffering.

  • Reproducer: synthetic 25 fps H.264 Matroska fixture, with packets 3 and 7 clamped one frame forward to create duplicate and
    missing timestamp slots.

  • Focused tests: all 13 Matroska timestamp tests pass.

  • Full suite: 4,347 of 4,348 tests pass. One unrelated slow-peer networking test timed out twice.

Build the synthetic fixture with FFmpeg
ffmpeg -f lavfi -i testsrc2=size=320x180:rate=25 -t 1 \
  -c:v libx264 -profile:v high -level:v 4.0 -pix_fmt yuv420p \
  -x264-params 'keyint=25:min-keyint=25:scenecut=0:bframes=3:b-adapt=0' \
  -an healthy.mkv

ffmpeg -i healthy.mkv -map 0:v:0 -c copy \
  -bsf:v "setts=pts='if(eq(N,3)+eq(N,7),PTS+0.04/TB,PTS)':dts='if(eq(N,3)+eq(N,7),DTS+0.04/TB,DTS)'" \
  clamped.mkv

Checklist

  • CHANGELOG.md updated
  • Commit messages follow Conventional Commits (feat(...), fix(...), chore(...))
  • The fix lives in the engine, not in a host-side workaround
  • Public API changes are intentional and documented — no public API changes

@oxyc oxyc changed the title Fix/matroska h264 frame cadence fix(demux): reconstruct clamped Matroska H264 timestamps Oct 13, 2026
superuser404notfound pushed a commit that referenced this pull request Oct 14, 2026
…es (#762)

The clamped-CFR verdict and its rewriter derived PTS from picture order and
DTS from decode order without checking that one does not precede the other.
A stream whose ranks lag their decode index by more than the reorder delay
would have been handed out with PTS < DTS. Both now fail closed on that, the
verdict back to the previous path and the rewriter to untouched packets.

Also covers the path #762 changed for #511 files: a coding-order ladder on a
whole-tick lattice still classifies as a permutation, not a reconstruction.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WJcXkGQrMiBPixVEFTSBJS
@superuser404notfound
superuser404notfound merged commit 8b976ab into superuser404notfound:main Oct 14, 2026
4 checks passed
@superuser404notfound

Copy link
Copy Markdown
Owner

Thank you @oxyc, this is merged in 8b976ab. A very clean contribution: fail closed throughout, a numeric fixture, and a reproducer that took one command to build.

Merge: the only conflict was CHANGELOG.md. The entry sat under the [Unreleased] section that has since shipped as 7.36.0, so it now lives in the current [Unreleased] → Fixed. The code is unchanged.

Verification on the merged tree:

  • Full suite green (4431 swift-testing tests, XCTest 0 failures).
  • aetherctl on a 20 s fixture built from your recipe, with clamps at N mod 25 ∈ {3, 7, 15} so every GOP carries them:
main #762
swdecode frame times 0.000 0.080 0.080 0.120 0.160 0.240 0.240 … 0.000 0.040 0.080 0.120 0.160 0.200 0.240 …
play --present-times, distinct frames per second (median) 22 25
same, with a seek to 4.5 s 22 25

The healthy twin reads 25 on both builds, so the head-of-stream hold for whole-tick CFR candidates costs nothing measurable.

Follow-up a65688e (mine, on top):

  • Neither the verdict nor the rewriter checked that the reconstructed PTS does not precede the reconstructed DTS. A stream whose ranks lag their decode index by more than the reorder delay would have gone out with PTS < DTS. Both now fail closed on that: the verdict falls back to the previous path, the rewriter to untouched packets. The new test fails without the guard and passes with it.
  • A test that a fix(demux): restore Matroska H264 presentation timestamp ownership #511 coding-order ladder on a whole-tick lattice (nominalStep: 40) still classifies as .permute. Your change routes those files through the new branch first, and the existing fix(demux): restore Matroska H264 presentation timestamp ownership #511 tests all ran with nominalStep nil.

Two things I left as they are, in case your asset can answer them:

  1. In steady state the rewriter accepts a DTS one step ahead on a picture whose PTS is on the lattice, which the verdict never accepts. In a real MKV the DTS is synthesized by libavformat, so I could not prove the forward DTS always lands on the clamped packet itself, and tightening it could stop the repair partway through your file. If your asset never logs CFR reconstruction stopped over a full playback with that check tightened, it can be made symmetric.
  2. The rewriter re-anchors only on an IDR with POC 0. An open-GOP encode whose later keyframes are non-IDR I pictures would therefore keep the repair off after any seek, without a log line.

Not released yet; it goes out with the next AetherEngine release.

@ijuniorfu

Copy link
Copy Markdown

CFRRewriter has a private initializer

@superuser404notfound

Copy link
Copy Markdown
Owner

Follow-up for @ijuniorfu's note above: CFRRewriter's memberwise initializer was private on toolchains before Swift 6.4. Fixed in #789 (2eb5915), tracked in #787. Unreleased, ships together with this PR.

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.

3 participants