Skip to content

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

Open
oxyc wants to merge 2 commits into
superuser404notfound:mainfrom
oxyc:fix/matroska-h264-frame-cadence
Open

oxyc wants to merge 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 High@L4.0 / 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 and others added 2 commits October 13, 2026 07:50
Co-Authored-By: OpenAI Codex <noreply@openai.com>
Co-Authored-By: OpenAI Codex <noreply@openai.com>
@oxyc oxyc changed the title Fix/matroska h264 frame cadence fix(demux): reconstruct clamped Matroska H264 timestamps Oct 13, 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.

1 participant