Skip to content

fix(video): recover H.264 with missing composition offsets - #411

Closed
orut34iop wants to merge 1 commit into
superuser404notfound:mainfrom
orut34iop:codex/fix-h264-missing-composition-timestamps
Closed

orut34iop wants to merge 1 commit into
superuser404notfound:mainfrom
orut34iop:codex/fix-h264-missing-composition-timestamps

Conversation

@orut34iop

Copy link
Copy Markdown
Contributor

Summary

Recover seekable progressive H.264 MP4 VOD whose sample table omitted composition offsets despite B-frame reordering. A bounded, fail-closed engine probe confirms the malformed timestamp signature, routes only that session through software decode, and schedules frames on libavcodec's monotonic best_effort_timestamp presentation axis.

Fixes #409

What changed

  • Add a bounded MP4/H.264 head probe that exits healthy on the first real PTS != DTS pair and requires sufficient equal-timestamp, non-key, decoded-frame, raw-PTS-regression, and monotonic best-effort evidence before selecting repair.
  • Arm the existing AVIO read deadline, bound the rewind, require rewind after every read attempt (including EOF/error), and discard or fail rather than hand a sampled demuxer to playback at an unknown position.
  • Pair the confirmed software-route decision with an internal per-session best-effort timestamp policy, applied immediately after avcodec_receive_frame so captions, deinterlacing, seek filtering, and rendering share one repaired clock.
  • Preserve the existing VC-1 (software path) playback judder due to missing frame PTS on I/P pictures #407 AV_NOPTS_VALUE fallback and preserve the confirmed route/policy across audio switches and seekable-custom-source background reloads; reset it for every new public load.
  • Add healthy/malformed synthetic MP4 integration fixtures, threshold/budget/overflow/rewind/routing/lifecycle tests, format documentation, and a changelog entry. No public API changes.

This is adjacent to #407 but covers a different failure: here every raw PTS is present, so the #407 NOPTS fallback correctly does nothing; the present raw axis itself regresses.

Test plan

  • Device / OS: Apple TV 4K (3rd generation), tvOS 26.6 beta (23L773).
  • Source media: seekable MP4 / progressive H.264 with B-frame reordering / SDR / audio not involved; all 64 sampled packet pairs had PTS == DTS, while decoded raw PTS regressed 16 times and best-effort PTS advanced 58 times with zero regressions.
  • Result: before the patch, continuous forward/backward judder began on the first frame and remained after seeking; with the repaired engine candidate, normal playback and post-seek playback were smooth. A healthy comparison MP4 remained smooth.
  • Synthetic coverage: byte-identical H.264 Main elementary streams in 96x64, 30000/1001 fps, 66-frame MP4 twins; the healthy twin carries ctts, while the malformed twin was stream-copied with setts=pts=DTS and has no composition offsets.
  • swift build — passed.
  • swift test — 2,001 tests in 287 suites passed.
  • swift build --build-tests --package-path Probes/TransportProbe — passed.
  • xcodebuild build -scheme AetherEngine -destination 'generic/platform=tvOS Simulator' ... — passed.
  • TransportProbe tvOS Simulator device harness build-for-testing — passed.
  • Local Xcode has only the tvOS 26.5 platform installed, so iOS Simulator and visionOS Simulator destination selection could not run locally; the corresponding upstream CI jobs cover both SDKs.

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)

@superuser404notfound

Copy link
Copy Markdown
Owner

Thanks for this, and especially for the twin fixtures. One encode muxed twice with the composition offsets removed from one is exactly the asset this needed, and I have kept that pair as the regression test for the fix.

The diagnosis holds. I reproduced it on the Apple decoder rather than only in libavcodec, by decoding both twins through AVAssetReader and matching each decoded frame's content against the healthy twin:

healthy twin ctts removed
pictures shown at a time belonging to a different picture 0 of 66 45 of 66
backward steps in the content order presented 0 30

What I could not keep is the remedy. Measured on the same pair, best_effort_timestamp resolves to pkt_dts of the packet being fed, so it is the decode ladder shifted by the reorder delay rather than the presentation axis:

  • +2002 ticks (2 frames, 67 ms) on 63 of 66 pictures, permanently. has_b_frames=2 is the x264 default, so that is what a repaired file gets in the common case; at a 3-frame delay it is 100 ms of lip sync error.
  • the first interval is 3 frame durations instead of 1, so the opening picture is held three times too long.
  • at EOF the flush regresses: ..., 65065, 59059, 65065. That is a backward step of 4004 ticks plus a duplicate, which is the artifact class the patch sets out to remove. The bestEffortRegressions == 0 gate is measured over the first 64 packets, so it structurally cannot see that tail.

It also trades hardware decode away for what is a container metadata defect.

The information the container dropped is still in the bitstream, so #413 rebuilds it instead of routing around it. libavcodec's H.264 parser reads each access unit's picture order count without decoding a pixel, and it accepts MP4's length-prefixed AVCC payload directly, so the repair costs one slice header parse per video packet and no Annex-B conversion. The packet is then rewritten to what the muxer should have written: presentation by display rank, decode pulled back by the reorder delay, which is also what keeps PTS >= DTS. Across three fixture pairs, 432 packets, both edit-list shapes and seven IDR boundaries, every repaired packet matches its healthy twin's PTS and DTS exactly, and the served loopback output presents all 301 frames at the same times the healthy twin's output does. The source keeps the native path and hardware decode.

Two things from this patch went straight into that one: the fail-closed shape of the detection (leave on the first real PTS/DTS offset, refuse anything unproven), and the fixtures. The bounded rewind handling here is careful work and would have been the right shape had the axis been usable, so closing this in favour of #413 is about the remedy only, not the report or the analysis behind it.

@superuser404notfound

Copy link
Copy Markdown
Owner

Superseded by #413, which repairs the same defect at the demuxer boundary and keeps the native path. The fixtures from this PR are the regression test there, and the reasoning is in the comment above.

superuser404notfound pushed a commit that referenced this pull request Aug 24, 2026
… an MP4 dropped with its ctts table

Repairs progressive H.264 MP4 whose writer omitted the composition-offset table while the bitstream still reorders pictures. Picture order comes from libavcodec's H.264 parser without decoding a pixel, and the packet is rewritten at the demuxer boundary to the timeline a correct muxer would have written, so the source keeps the native path and hardware decode. Supersedes #411; reported in #409, whose retest is still open.
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.

Progressive H.264 MP4 without CTTS judders on the native path

2 participants