Skip to content

feat(spec)!: bind clip timing with media.timing - #73

Merged
gregogalante merged 1 commit into
mainfrom
feat/clip-timing
Oct 4, 2026
Merged

gregogalante merged 1 commit into
mainfrom
feat/clip-timing

Conversation

@gregogalante

Copy link
Copy Markdown
Member

Summary

Makes clip timing binding normative (design reviews/design-clip-timing.md, accepted 4 October 2026). Before: a re-mux keeping every sample could freeze a frame (stts), reorder frames in a GOP (ctts), rescale both tracks (mdhd), trim a located segment (elst) or change the audio sample rate (esds), and the clip still read verified clip.

  • §5 Timing (new): per-segment record timing(n); segments[].timing = SHA-256(timing(n)); core field media.timing required wherever segments is (absent → no proof found). The segment message does not change, so no segment is re-signed.
  • §5 Presentation: the audio sample entry's esds joins X in config.
  • §6.1 / §7 / §8 / §11, schema, threat model §5.1, c2pa-interop §2.2: updated. New label timing differs: amber, never red.
  • tools/src/timing.ts: reference record, hash, root, conversion and edit rule, written to be ported. container.ts reads ctts, the media edit and esds. remux.ts can rescale, retime and write ctts.
  • Corpus 7.0.0 (187 vectors), breaking. Every rebuildable video vector was re-signed or re-minted with the field (33, 38, 39, 86–94, 143–147, 156, 158–163, 165–168, 171, 173). No existing verdict moved. Device captures 36, 37, 48 and 85 keep their bytes and no proof found.

Record and core field

timing(n) = uint32 BE v
            || ( int64 BE (dts_i − dts_0) || int64 BE cts_i ) × v
            || uint64 BE (end_n − dts_0)
            || uint32 BE a
            || ( int64 BE (adts_j − adts_0) || uint32 BE adur_j ) × a
  • i: the segment's video samples, IDR first, in decode order. dts = cumulative stts.
  • cts: from ctts (v0 unsigned, v1 signed), 0 without one.
  • end_n: the last sample's DTS plus its own stts duration. Never taken from mdhd.
  • j: the audio frames §5 assigns by DTS on the presentation timeline. adts_0 is the first of them, adur is the stts duration.
  • Every value is in its own track's mdhd timescale.

media.timing = { video_timescale: uint32≥1, audio_timescale?: uint32≥1 (absent, never null/0, without a soun track), root: b64url SHA-256(T(0)‖…‖T(count−1)) }. segments[n].timing is base64url of 32 bytes and is required on every entry.

Verifier algorithm

  1. Shape. media.timing missing or malformed, or any entry without timing → no proof found.
  2. Root. If entries cover 0…segment_count−1 exactly, recompute the root from the entries. A mismatch → tampered (needs no demux).
  3. Clip (media.hash ≠, GOPs located, presentation OK):
    • If an entry is missing → timing differs (the root cannot authenticate the entries).
    • Otherwise, for each located segment, read timing(n) from the file and convert each value with t × ts_signed / ts_received. A non-integer result, missing audio timescale or overflow → differs. Hash the converted record and compare it with the entry.
    • Media edit on the video or audio track (m, d, M, T), with the segment span [s, e): it trims when m > s or (d+1)·T ≤ (e−m)·M. The span is min/max of dts+cts(+dur) for video, and first adts to last adts+adur for audio.
    • Any of these → frames not compared, timing differs, segments.verified=[].
  4. Original (media.hash matches): build the records of all segments from the file and convert them. A root mismatch → authentic + timing differs (amber cap). The edit rule is not applied.

Sealer requirements

  • Read the record back from the finalized file, before the trailer. Do not use encoder PTS or requested timescales (vector 185 shows what goes wrong).
  • Write media.timing and every entry's timing, and add the audio esds to config. Sign the core after the read-back. Segment messages are untouched.
  • A cutter keeps every segments[] entry (vector 186).

Vectors

# Case Expected
175 166's original, debug has every record authentic [0,1,2]
176 core without media.timing no proof found
177 entry 1 timing not in root tampered [0,1,2]
178 166 cut at the end verified clip [0,1]
179 89 + one frame frozen 2 s (stts) frames not compared, timing differs
180 89 + two frames swapped (ctts) frames not compared, timing differs
181 89, both mdhd ×2, durations kept frames not compared, timing differs
182 89, timescales and durations ×2 verified clip [1,2]
183 89, esds 48→44.1 kHz frames not compared, presentation differs
184 89, media edit ends inside seg 2 frames not compared, timing differs
185 original, timing signed in µs authentic + timing differs
186 89, entry 0 dropped from proof frames not compared, timing differs
187 entry without timing no proof found

Decisions beyond the design

  • end_n is defined uniformly: last DTS + duration.
  • audio_timescale is absent on silent files.
  • An incomplete entry set → timing differs (186).
  • An entry without timing → no proof found (187).
  • The edit rule applies to clips only, with one movie tick of slack.
  • Design vector 4 became the end cut (178), since 89 itself now carries the timing-kept case.

Recorded as open in §11:

  • an edit running past the end of the media
  • mp4a fixed fields
  • esds inside a wave box (no vector)

Gates (Node 22.23.2)

These were run locally; npm ci was skipped because the lockfile is unchanged.

  • typecheck
  • test (280 tests)
  • validate
  • manifest:check
  • conformance:check (187/187)
  • generate:edge-cases
  • generate twice (byte-stable)

C2PA vectors were re-minted with c2pa-node 0.9.8 and c2patool 0.27.16.

🤖 Generated with Claude Code

A clip's timing was bound by nothing: content_hash covers NAL units and
audio frame bytes, media.presentation how a player is told to show them,
and a re-mux that kept every sample could freeze a frame (stts), reorder
frames in a GOP (ctts), rescale both tracks in sync (mdhd), trim inside a
located segment (elst) or declare another audio sample rate (esds) and
still read verified clip.

Implements reviews/design-clip-timing.md, accepted 4 October 2026:

- §5 Timing: timing(n), a fixed-width record of each segment's video
  DTS deltas, composition offsets, end (last DTS + duration) and the
  audio frames §5 assigns to it, in each track's own mdhd timescale.
  segments[].timing = SHA-256(timing(n)); media.timing = { video_timescale,
  audio_timescale (absent without audio), root } is a required core field.
  The 96-byte segment message is unchanged: no segment is re-signed.
- Verifier: root recomputed from complete entries (mismatch: tampered);
  on a clip each located segment's record is read back and converted
  exactly into the signed timescales; a differing record, an entry set the
  root cannot authenticate, or a media edit trimming a located segment is
  frames not compared with the new label "timing differs"; on an original
  it is the writer's false claim, authentic capped amber.
- The audio sample entry's esds joins media.presentation.config.
- tools/src/timing.ts is the reference record, written to be ported.

Corpus 7.0.0 (187 vectors): every rebuildable video vector re-signed or
re-minted with the field, no existing verdict moved; device captures 36,
37, 48, 85 keep their bytes; 175-187 are new.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gregogalante
gregogalante merged commit 1f9ffac into main Oct 4, 2026
2 checks passed
@gregogalante
gregogalante deleted the feat/clip-timing branch October 4, 2026 06:29
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