@blockcast/mmt-container 2.2.0-main.cc080e5a56f2 → 2.2.0-main.cc3fe0684403

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/README.md +5 -2
  2. package/dist/block-policy.d.ts +88 -16
  3. package/dist/block-policy.d.ts.map +1 -1
  4. package/dist/block-policy.js +164 -56
  5. package/dist/block-policy.js.map +3 -3
  6. package/dist/browser-runtime.js +1485 -172
  7. package/dist/browser-runtime.js.map +4 -4
  8. package/dist/carp-catalog.d.ts +40 -1
  9. package/dist/carp-catalog.d.ts.map +1 -1
  10. package/dist/carp-catalog.js +163 -10
  11. package/dist/carp-catalog.js.map +4 -4
  12. package/dist/catalog-fec-params.d.ts +1 -1
  13. package/dist/catalog-fec-params.d.ts.map +1 -1
  14. package/dist/catalog-fec-params.js +48 -1
  15. package/dist/catalog-fec-params.js.map +3 -3
  16. package/dist/catalog-network-source.js +34 -0
  17. package/dist/catalog-network-source.js.map +3 -3
  18. package/dist/cmaf-block-assembler.js +92 -1
  19. package/dist/cmaf-block-assembler.js.map +2 -2
  20. package/dist/codec-utils.d.ts.map +1 -1
  21. package/dist/codec-utils.js +92 -1
  22. package/dist/codec-utils.js.map +2 -2
  23. package/dist/datagram-leg-deadline.d.ts +118 -0
  24. package/dist/datagram-leg-deadline.d.ts.map +1 -0
  25. package/dist/datagram-leg-deadline.js +217 -0
  26. package/dist/datagram-leg-deadline.js.map +7 -0
  27. package/dist/delivery-catalog.js +68 -0
  28. package/dist/delivery-catalog.js.map +4 -4
  29. package/dist/delta-fallback-config.js +39 -1
  30. package/dist/delta-fallback-config.js.map +4 -4
  31. package/dist/fec-manager.d.ts +155 -10
  32. package/dist/fec-manager.d.ts.map +1 -1
  33. package/dist/fec-manager.js +596 -81
  34. package/dist/fec-manager.js.map +4 -4
  35. package/dist/fec-state.d.ts +17 -5
  36. package/dist/fec-state.d.ts.map +1 -1
  37. package/dist/fec-state.js +41 -1
  38. package/dist/fec-state.js.map +3 -3
  39. package/dist/fec-worker-setup.js +34 -0
  40. package/dist/fec-worker-setup.js.map +3 -3
  41. package/dist/held-layer-admission.js +34 -0
  42. package/dist/held-layer-admission.js.map +3 -3
  43. package/dist/index.d.ts +7 -5
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +15 -1
  46. package/dist/index.js.map +2 -2
  47. package/dist/iso-block-locator.d.ts +16 -0
  48. package/dist/iso-block-locator.d.ts.map +1 -1
  49. package/dist/iso-block-locator.js +62 -0
  50. package/dist/iso-block-locator.js.map +3 -3
  51. package/dist/mfu-reassembler.js +34 -0
  52. package/dist/mfu-reassembler.js.map +3 -3
  53. package/dist/mmtp-container-parser.d.ts +30 -2
  54. package/dist/mmtp-container-parser.d.ts.map +1 -1
  55. package/dist/mmtp-container-parser.js +140 -2
  56. package/dist/mmtp-container-parser.js.map +3 -3
  57. package/dist/mmtp-container-parser.no-modules.js +293 -18
  58. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  59. package/dist/mmtp-decode-pipeline.js +140 -2
  60. package/dist/mmtp-decode-pipeline.js.map +3 -3
  61. package/dist/mmtp-fec-session.js +34 -0
  62. package/dist/mmtp-fec-session.js.map +3 -3
  63. package/dist/mmtp-mode-selection.d.ts +48 -0
  64. package/dist/mmtp-mode-selection.d.ts.map +1 -0
  65. package/dist/mmtp-mode-selection.js +79 -0
  66. package/dist/mmtp-mode-selection.js.map +7 -0
  67. package/dist/mmtp-router.d.ts +66 -8
  68. package/dist/mmtp-router.d.ts.map +1 -1
  69. package/dist/mmtp-router.js +348 -27
  70. package/dist/mmtp-router.js.map +4 -4
  71. package/dist/multicast-auth-catalog.js +42 -2
  72. package/dist/multicast-auth-catalog.js.map +3 -3
  73. package/dist/multicast-input.js +42 -2
  74. package/dist/multicast-input.js.map +3 -3
  75. package/dist/multicast-manager.d.ts +108 -1
  76. package/dist/multicast-manager.d.ts.map +1 -1
  77. package/dist/multicast-manager.js +1067 -138
  78. package/dist/multicast-manager.js.map +4 -4
  79. package/dist/multicast-transport.js +68 -0
  80. package/dist/multicast-transport.js.map +4 -4
  81. package/dist/multicast-unwrap.js +34 -0
  82. package/dist/multicast-unwrap.js.map +3 -3
  83. package/dist/no-modules.d.ts +1 -0
  84. package/dist/no-modules.d.ts.map +1 -1
  85. package/dist/no-modules.js +295 -19
  86. package/dist/no-modules.js.map +4 -4
  87. package/dist/phase14-catalog-override.d.ts +2 -1
  88. package/dist/phase14-catalog-override.d.ts.map +1 -1
  89. package/dist/phase14-catalog-override.js +69 -2
  90. package/dist/phase14-catalog-override.js.map +3 -3
  91. package/dist/repair-fetch.d.ts.map +1 -1
  92. package/dist/repair-fetch.js +50 -0
  93. package/dist/repair-fetch.js.map +3 -3
  94. package/dist/repair-layer-subscriptions.js +34 -0
  95. package/dist/repair-layer-subscriptions.js.map +3 -3
  96. package/dist/signaling.js +34 -0
  97. package/dist/signaling.js.map +3 -3
  98. package/dist/timestamp-codec.js +34 -0
  99. package/dist/timestamp-codec.js.map +3 -3
  100. package/dist/wasm-mfu-reassembler.js +34 -0
  101. package/dist/wasm-mfu-reassembler.js.map +3 -3
  102. package/dist/wasm-mmtp-container-parser.js +140 -2
  103. package/dist/wasm-mmtp-container-parser.js.map +3 -3
  104. package/package.json +5 -5
  105. package/schemas/catalog.schema.json +2 -2
  106. package/src/block-policy.ts +324 -83
  107. package/src/carp-catalog.ts +165 -5
  108. package/src/catalog-fec-params.ts +1 -0
  109. package/src/codec-utils.ts +121 -1
  110. package/src/datagram-leg-deadline.ts +342 -0
  111. package/src/fec-manager.ts +534 -41
  112. package/src/fec-state.ts +21 -5
  113. package/src/index.ts +38 -0
  114. package/src/iso-block-locator.ts +34 -0
  115. package/src/mmtp-container-parser.ts +57 -11
  116. package/src/mmtp-mode-selection.ts +80 -0
  117. package/src/mmtp-router.ts +287 -42
  118. package/src/multicast-manager.ts +218 -10
  119. package/src/no-modules.ts +3 -0
  120. package/src/phase14-catalog-override.ts +14 -3
  121. package/src/repair-fetch.ts +23 -0
  122. package/test-vectors/catalog/README.md +36 -2
  123. package/test-vectors/catalog/mmtp-siblings/ambiguous-sibling.json +158 -0
  124. package/test-vectors/catalog/mmtp-siblings/ladder-bitrate-only.json +231 -0
  125. package/test-vectors/catalog/mmtp-siblings/ladder-four-rungs.json +405 -0
  126. package/test-vectors/catalog/mmtp-siblings/ladder-other-names.json +231 -0
  127. package/test-vectors/catalog/mmtp-siblings/mmtp-mode-unknown.json +143 -0
  128. package/test-vectors/catalog/mmtp-siblings/multicast-endpoint-lists-datagram-track.json +143 -0
  129. package/test-vectors/catalog/mmtp-siblings/no-sibling.json +79 -0
  130. package/test-vectors/catalog/mmtp-siblings/rendition-mismatch.json +143 -0
  131. package/test-vectors/catalog/mmtp-siblings/stream-delivery-mode.json +143 -0
  132. package/test-vectors/catalog/mmtp-siblings/timescale-mismatch.json +143 -0
  133. package/test-vectors/catalog/mmtp-siblings/valid-pair.json +142 -0
  134. package/test-vectors/catalog/section-codes.json +3 -3
  135. package/test-vectors/catalog-timing.json +559 -1
  136. package/test-vectors/catalog-validation.json +266 -7
  137. package/test-vectors/moq/mmtp-mode-selection.json +259 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blockcast/mmt-container",
3
- "version": "2.2.0-main.cc080e5a56f2",
3
+ "version": "2.2.0-main.cc3fe0684403",
4
4
  "description": "MMTP/LOC container parsing for MMT over MoQ and SSM",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -169,10 +169,10 @@
169
169
  "test:watch": "vitest"
170
170
  },
171
171
  "dependencies": {
172
- "@blockcast/fec-worker": "2.2.0-main.cc080e5a56f2",
173
- "@blockcast/mmt-base": "2.2.0-main.cc080e5a56f2",
174
- "@blockcast/mmt-fec": "2.2.0-main.cc080e5a56f2",
175
- "@blockcast/mmt-manifest-verify": "2.2.0-main.cc080e5a56f2",
172
+ "@blockcast/fec-worker": "2.2.0-main.cc3fe0684403",
173
+ "@blockcast/mmt-base": "2.2.0-main.cc3fe0684403",
174
+ "@blockcast/mmt-fec": "2.2.0-main.cc3fe0684403",
175
+ "@blockcast/mmt-manifest-verify": "2.2.0-main.cc3fe0684403",
176
176
  "@noble/curves": "^1.9.7",
177
177
  "@noble/hashes": "^1.8.0",
178
178
  "ajv": "^8.17.1",
@@ -129,7 +129,7 @@
129
129
  "maximum": 16777215,
130
130
  "description": "fec-repair only. Repair symbols per block carried by this layer (P_i)."
131
131
  },
132
- "mmtpMode": { "const": "mfu" },
132
+ "mmtpMode": { "type": "string", "minLength": 1 },
133
133
  "timescale": { "$ref": "#/$defs/positiveInteger" },
134
134
  "groupDurationMs": { "$ref": "#/$defs/positiveInteger" },
135
135
  "groupDurationTicks": { "$ref": "#/$defs/positiveInteger" },
@@ -150,7 +150,7 @@
150
150
  "then": {
151
151
  "required": ["mmtpMode", "timescale"],
152
152
  "properties": {
153
- "mmtpMode": { "const": "mfu" },
153
+ "mmtpMode": { "type": "string", "minLength": 1 },
154
154
  "timescale": { "$ref": "#/$defs/positiveInteger" },
155
155
  "groupDurationMs": { "$ref": "#/$defs/positiveInteger" }
156
156
  },
@@ -81,7 +81,17 @@ export type ClassifierReason =
81
81
  | 'rap-not-mpu-start'
82
82
  | 'mpu-longer-than-gop'
83
83
  | 'block-not-contiguous'
84
+ /**
85
+ * MPU-start RAPs are not exactly one keyframe interval apart per MPU:
86
+ * released RAPs in the media timescale, or, once each frame is its own
87
+ * movie fragment, RAP fragments G fragments apart per MPU (P3).
88
+ */
84
89
  | 'rap-spacing-mismatch'
90
+ /**
91
+ * An MPU of several movie fragments whose fragments hold several samples:
92
+ * neither sample_number nor the fragment number states a frame's MPU
93
+ * position (P3).
94
+ */
85
95
  | 'mpu-multiple-fragments'
86
96
  | 'block-wider-than-interleave'
87
97
 
@@ -123,6 +133,8 @@ export type BlockClassifierGeometry =
123
133
  readonly trackKind: 'video'
124
134
  readonly mode: 'all-protected'
125
135
  readonly reason: ClassifierReason
136
+ /** The catalog interval when one is stated: gap decisions use it (keyframeInstantBetween). */
137
+ readonly keyframeInterval?: KeyframeInterval
126
138
  }
127
139
 
128
140
  /**
@@ -166,34 +178,31 @@ export function deriveBlockClassifierGeometry(
166
178
  }
167
179
  if (input.trackKind === 'audio') return { trackKind: 'audio' }
168
180
 
169
- if (!isPositiveFinite(input.framerate)) {
170
- return { trackKind: 'video', mode: 'all-protected', reason: 'framerate-absent' }
171
- }
172
- const framerate = input.framerate
173
-
174
- let keyframeInterval: KeyframeInterval
181
+ // Every video geometry keeps the catalog interval: gap decisions need it in
182
+ // either mode (fec-multicast-00 Block Classification item 3).
183
+ let keyframeInterval: KeyframeInterval | undefined
175
184
  if (isPositiveFinite(input.keyframeIntervalTicks) && isPositiveFinite(input.timescale)) {
176
185
  keyframeInterval = { ticks: input.keyframeIntervalTicks, timescale: input.timescale }
177
186
  } else if (isPositiveFinite(input.keyframeIntervalMs)) {
178
187
  keyframeInterval = { ticks: input.keyframeIntervalMs, timescale: 1000 }
179
- } else {
180
- return { trackKind: 'video', mode: 'all-protected', reason: 'keyframe-cadence-absent' }
181
188
  }
189
+ const protect = (reason: ClassifierReason): BlockClassifierGeometry => ({
190
+ trackKind: 'video',
191
+ mode: 'all-protected',
192
+ reason,
193
+ ...(keyframeInterval === undefined ? {} : { keyframeInterval }),
194
+ })
195
+
196
+ if (!isPositiveFinite(input.framerate)) return protect('framerate-absent')
197
+ const framerate = input.framerate
198
+ if (keyframeInterval === undefined) return protect('keyframe-cadence-absent')
182
199
 
183
200
  const gopFrames = exactInteger(keyframeInterval.ticks * framerate, keyframeInterval.timescale)
184
- if (gopFrames === null) {
185
- return {
186
- trackKind: 'video',
187
- mode: 'all-protected',
188
- reason: 'keyframe-interval-not-integral',
189
- }
190
- }
201
+ if (gopFrames === null) return protect('keyframe-interval-not-integral')
191
202
 
192
203
  const frameDurationMs = 1000 / framerate
193
204
  const interleaveUnits = deriveFecInterleaveDepth(input.interleaveDepthMs, frameDurationMs)
194
- if (interleaveUnits >= gopFrames) {
195
- return { trackKind: 'video', mode: 'all-protected', reason: 'block-spans-gop' }
196
- }
205
+ if (interleaveUnits >= gopFrames) return protect('block-spans-gop')
197
206
 
198
207
  return {
199
208
  trackKind: 'video',
@@ -207,14 +216,15 @@ export function deriveBlockClassifierGeometry(
207
216
 
208
217
  /**
209
218
  * The keyframe interval expressed in a media track's timescale, or null when
210
- * the geometry is not `classifying` or the interval is not an exact integer
211
- * number of media ticks.
219
+ * the geometry states none or the interval is not an exact integer number of
220
+ * media ticks (fec-multicast-00 Block Classification item 3: an inexact one
221
+ * gives no usable interval).
212
222
  */
213
223
  export function keyframeIntervalMediaTicks(
214
224
  geometry: BlockClassifierGeometry,
215
225
  mediaTimescale: number,
216
226
  ): bigint | null {
217
- if (geometry.trackKind !== 'video' || geometry.mode !== 'classifying') return null
227
+ if (geometry.trackKind !== 'video' || geometry.keyframeInterval === undefined) return null
218
228
  if (!Number.isSafeInteger(mediaTimescale) || mediaTimescale <= 0) return null
219
229
  const { ticks, timescale } = geometry.keyframeInterval
220
230
  if (!Number.isSafeInteger(ticks) || !Number.isSafeInteger(timescale)) return null
@@ -224,6 +234,19 @@ export function keyframeIntervalMediaTicks(
224
234
  return scaled / divisor
225
235
  }
226
236
 
237
+ /**
238
+ * fec-multicast-00 Block Classification item 3 for two released frames:
239
+ * whether `(from, to]` holds `start + k * interval` for some k >= 1, `start`
240
+ * the decode time of an MPU's first RAP, all in one media timescale. `from`
241
+ * is exclusive: the frame at `from` is held and complete, so an instant there
242
+ * is that frame itself (an on-cadence RAP), not a lost one. The draft's closed
243
+ * interval is for bounding packets, which may be part of a RAP.
244
+ */
245
+ export function keyframeInstantBetween(start: bigint, interval: bigint, from: bigint, to: bigint): boolean {
246
+ const k = from >= start ? (from - start) / interval + 1n : 1n
247
+ return start + k * interval <= to
248
+ }
249
+
227
250
  // ─── Evidence ───────────────────────────────────────────────────────
228
251
 
229
252
  /**
@@ -237,13 +260,18 @@ export interface SourceSymbolEvidence {
237
260
  /** MPU_sequence_number, when parsed from this packet. */
238
261
  readonly mpu?: number
239
262
  /**
240
- * 0-based sample position within the MPU: the MFU DU-header
263
+ * 0-based sample position within its movie fragment: the MFU DU-header
241
264
  * `sample_number` minus one (ISO/IEC 23008-1 numbers samples from 1, and
242
265
  * the parser rejects `sample_number` 0). Present only when this packet
243
- * carried a DU header.
266
+ * carried a DU header. It is the MPU position only while the MPU is one
267
+ * movie fragment; BlockClassifier derives MPU positions (P3).
244
268
  */
245
269
  readonly idx?: number
246
- /** movie_fragment_sequence_number from the DU header, when present. */
270
+ /**
271
+ * movie_fragment_sequence_number from the DU header, when present. When
272
+ * each frame is its own fragment, it positions the frame from its MPU's
273
+ * RAP fragment (P3).
274
+ */
247
275
  readonly movieFragment?: number
248
276
  /**
249
277
  * DU-header `offset`: the data unit's authored byte position within its
@@ -279,8 +307,9 @@ export type ClassifierChange =
279
307
  interface BlockEvidence {
280
308
  rapSeen: boolean
281
309
  mpus: Set<number>
282
- idxMin?: number
283
- idxMax?: number
310
+ /** The lowest and highest MPU positions its DU headers proved (P3). */
311
+ positionMin?: number
312
+ positionMax?: number
284
313
  cls: BlockClass
285
314
  }
286
315
 
@@ -292,14 +321,49 @@ interface DataUnitStart {
292
321
  readonly sbn: number
293
322
  }
294
323
 
295
- /** Authored order of two data-unit starts of one MPU. */
324
+ /**
325
+ * Authored order of two data-unit starts of one MPU. Fragment numbers wrap
326
+ * at 2^32, so they compare in RFC 1982 serial order.
327
+ */
296
328
  function compareDataUnitStarts(
297
329
  a: Omit<DataUnitStart, 'sbn'>,
298
330
  b: Omit<DataUnitStart, 'sbn'>,
299
331
  ): number {
300
- return a.movieFragment - b.movieFragment || a.idx - b.idx || a.offset - b.offset
332
+ return ((a.movieFragment - b.movieFragment) | 0) || a.idx - b.idx || a.offset - b.offset
333
+ }
334
+
335
+ /**
336
+ * How many MPUs behind the newest released RAP a stale release can be. The
337
+ * release gate holds a frame for seconds at most, so a late copy is a few
338
+ * MPUs back; anything further is another MPU sequence. With MPUs so short
339
+ * that a late copy lies further back, it re-anchors there: the newest MPU's
340
+ * gaps read rap-unknown until the next MPU's RAP, never a wrong measure.
341
+ */
342
+ const LATE_RAP_MAX_MPUS_BACK = 64
343
+
344
+ /** One MPU's movie fragments (P3). */
345
+ interface MpuLayout {
346
+ /** Serial-earliest movie_fragment_sequence_number of its DU headers. */
347
+ low: number
348
+ /** The fragment of its first RAP-flagged DU header: position 0. */
349
+ anchor?: number
350
+ }
351
+
352
+ /** What one DU header proves about its MPU (BlockClassifier `#place`). */
353
+ interface Placement {
354
+ /** Its 0-based MPU position, when the wire proves it. */
355
+ readonly position?: number
356
+ /**
357
+ * It shows a RAP inside an MPU, or an MPU longer than G: MPU boundaries
358
+ * no longer bound the stream's RAPs (`rap-not-mpu-start`).
359
+ */
360
+ readonly rapNotMpuStart: boolean
361
+ /** A refuted layout premise. */
362
+ readonly violation?: ClassifierReason
301
363
  }
302
364
 
365
+ const UNPLACED: Placement = { rapNotMpuStart: false }
366
+
303
367
  /**
304
368
  * Per-stream block classifier.
305
369
  *
@@ -322,14 +386,33 @@ function compareDataUnitStarts(
322
386
  * from the previous block (`block-wider-than-interleave`). The producer
323
387
  * counts a media-unit boundary only once the open block holds a symbol.
324
388
  * - P3: an MPU starts with its only RAP and holds at most G frames
325
- * (`rap-not-mpu-start`, `mpu-longer-than-gop`, `rap-spacing-mismatch`), in
326
- * one movie fragment (`mpu-multiple-fragments`).
389
+ * (`rap-not-mpu-start`, `mpu-longer-than-gop`, `rap-spacing-mismatch`),
390
+ * and its DU headers state each frame's MPU position in one of two layouts
391
+ * (`mpu-multiple-fragments` when an MPU shows two fragments and a DU
392
+ * header states sample_number above 1, which is neither):
393
+ * - one movie fragment per MPU (ATSC 3.0): the position is
394
+ * sample_number − 1. This is the rule until some MPU of the sequence
395
+ * shows two fragments.
396
+ * - one fragment per frame, numbered one per frame (FFmpeg's MoQ MMT
397
+ * muxer, every DU header stating sample_number 1): the position is the
398
+ * fragment's distance from the fragment of the MPU's first RAP-flagged
399
+ * DU header (its anchor). The RAP's own fragment is position 0. Each
400
+ * anchor must lie exactly G fragments per MPU from the anchor received
401
+ * before it, which is kept past the horizon, and from the anchor
402
+ * received last (`rap-spacing-mismatch`, as after a dropped frame).
403
+ * Once two anchors kept that spacing, fragment numbers count frames:
404
+ * later fragments get positions, and an anchor arriving past the
405
+ * spacing is a RAP inside its MPU or follows an MPU longer than G
406
+ * (`rap-not-mpu-start`). An MPU whose anchor was not received has no
407
+ * positions; its blocks stay unknown. A second RAP-flagged fragment, or
408
+ * a DU header in a fragment before the anchor, refutes the MPU start
409
+ * (`rap-not-mpu-start`).
327
410
  *
328
411
  * P1 and P2 are what make a block's frames contiguous and lie within
329
- * [idxMax − D + 1, idxMin + D − 1] of its MPU. What refutes P1 is a pair of
330
- * source packets whose wire-stated authored order and SBN order disagree:
331
- * a later MPU in an earlier block, a later data unit of one MPU (by
332
- * movie_fragment_sequence_number, sample_number, then DU offset) in an
412
+ * [positionMax − D + 1, positionMin + D − 1] of its MPU. What refutes P1 is
413
+ * a pair of source packets whose wire-stated authored order and SBN order
414
+ * disagree: a later MPU in an earlier block, a later data unit of one MPU
415
+ * (by movie_fragment_sequence_number, sample_number, then DU offset) in an
333
416
  * earlier block, or one data unit in two blocks. That is the signature of a
334
417
  * round-robin interleave, which scatters one run over several concurrent
335
418
  * blocks. Non-adjacency alone is not: a sample larger than K packets
@@ -357,9 +440,25 @@ export class BlockClassifier {
357
440
  #mpuSbnSpans = new Map<number, { min: number; max: number }>()
358
441
  #dataUnitStarts = new Map<number, DataUnitStart[]>()
359
442
  #highestMpu: number | null = null
360
- /** MPU → movie_fragment_sequence_number observed in it. */
361
- #mpuFragments = new Map<number, number>()
362
- #lastRap: { mpu: number; presentationTime: bigint; mediaTimescale: number } | null = null
443
+ /**
444
+ * P3 layout, kept for the MPUs within the classification horizon and valid
445
+ * within one continuous SBN sequence (`noteSourceDiscontinuity`).
446
+ */
447
+ #mpus = new Map<number, MpuLayout>()
448
+ /** An MPU of this sequence showed two movie fragments: each frame is one. */
449
+ #fragmentPerFrame = false
450
+ /** A DU header of this sequence stated sample_number above 1. */
451
+ #multiSampleFragment = false
452
+ /**
453
+ * The anchor received last, whether or not its MPU is still in the
454
+ * horizon. Each new anchor, and the anchor of every DU header's MPU, is
455
+ * checked against it.
456
+ */
457
+ #lastAnchor: { mpu: number; fragment: number } | null = null
458
+ /** Two anchors were exactly G fragments per MPU apart. */
459
+ #fragmentsProven = false
460
+ /** The first RAP released of the latest MPU a RAP was released in, recorded in every mode. */
461
+ #lastRap: { mpu: number; presentationTime: bigint; mediaTimescale: number; decodeTime?: bigint } | null = null
363
462
 
364
463
  constructor(geometry: BlockClassifierGeometry) {
365
464
  this.#geometry = geometry
@@ -394,7 +493,7 @@ export class BlockClassifier {
394
493
  this.#geometry = geometry
395
494
  this.#blocks.clear()
396
495
  this.#clearContiguityLedger()
397
- this.#mpuFragments.clear()
496
+ this.#clearMpuLayout()
398
497
  this.#lastRap = null
399
498
  this.#mpuBoundsRaps = true
400
499
  if (geometry.trackKind === 'video' && geometry.mode === 'all-protected') {
@@ -430,11 +529,16 @@ export class BlockClassifier {
430
529
  /**
431
530
  * The stream's SBN sequence restarted (FECManager source discontinuity:
432
531
  * a publisher restart, or the SS_ID space wrapped). Authored order and SBN
433
- * order are comparable only within one continuous sequence, so the P1
434
- * ledger starts again.
532
+ * order are comparable only within one continuous sequence, and a
533
+ * restarted publisher numbers MPUs and movie fragments again, so the P1
534
+ * ledger and the P3 layout start again.
435
535
  */
436
536
  noteSourceDiscontinuity(): void {
437
537
  this.#clearContiguityLedger()
538
+ this.#clearMpuLayout()
539
+ // A restarted publisher numbers MPUs again: its first RAP is no second
540
+ // RAP of the MPU the old sequence last released one in.
541
+ this.#lastRap = null
438
542
  }
439
543
 
440
544
  /** Register a block that exists without evidence (a vanished block). */
@@ -457,21 +561,23 @@ export class BlockClassifier {
457
561
  if (this.#geometry.trackKind === 'audio') return []
458
562
  const changes: ClassifierChange[] = []
459
563
  const block = this.#blockFor(sbn)
564
+ const placement = this.#place(evidence)
460
565
 
461
566
  if (evidence.rap) block.rapSeen = true
462
567
  if (evidence.mpu !== undefined) block.mpus.add(evidence.mpu)
463
- if (evidence.idx !== undefined) {
464
- block.idxMin = block.idxMin === undefined ? evidence.idx : Math.min(block.idxMin, evidence.idx)
465
- block.idxMax = block.idxMax === undefined ? evidence.idx : Math.max(block.idxMax, evidence.idx)
568
+ const { position } = placement
569
+ if (position !== undefined) {
570
+ block.positionMin =
571
+ block.positionMin === undefined ? position : Math.min(block.positionMin, position)
572
+ block.positionMax =
573
+ block.positionMax === undefined ? position : Math.max(block.positionMax, position)
466
574
  }
467
575
 
468
576
  // A RAP inside an MPU refutes "an MPU boundary bounds RAPs" whatever the
469
577
  // classifier mode, because gap decisions rely on it in every mode.
470
- if (evidence.rap && evidence.idx !== undefined && evidence.idx !== 0) {
471
- this.#mpuBoundsRaps = false
472
- }
578
+ if (placement.rapNotMpuStart) this.#mpuBoundsRaps = false
473
579
 
474
- const violation = this.#checkInvariants(sbn, block, evidence)
580
+ const violation = this.#checkInvariants(sbn, block, evidence, placement)
475
581
  if (violation !== null) changes.push(...this.#disable(violation))
476
582
 
477
583
  const next = this.#classify(block)
@@ -498,29 +604,63 @@ export class BlockClassifier {
498
604
  * RAP-bearing block is the one a wrong G calls delta), because the next
499
605
  * released RAP then belongs to m+2. Every MPU is one interval long, so the
500
606
  * span to any later released RAP proves or refutes all MPUs in between.
607
+ *
608
+ * In every mode it keeps each MPU's first released RAP, with its decode
609
+ * time, as the start keyframeFreeBetween measures the catalog interval
610
+ * from, and a second RAP released inside one MPU refutes `mpuBoundsRaps`
611
+ * when it lies off the catalog lattice (not a whole number of intervals
612
+ * after the first; draft-ramadan-moq-mmt-02 4.4.3 binds every keyframe to
613
+ * it): with no repair, released RAPs are the only evidence of where they
614
+ * lie. Without a usable interval, or a decode time on either, any second
615
+ * RAP with another presentation time refutes.
501
616
  */
502
617
  noteReleasedRap(
503
618
  mpu: number,
504
619
  presentationTime: bigint,
505
620
  mediaTimescale: number,
621
+ decodeTime?: bigint,
506
622
  ): ClassifierChange[] {
507
- if (this.#geometry.trackKind === 'audio' || this.#mode === 'all-protected') {
623
+ if (this.#geometry.trackKind === 'audio') return []
624
+ const previous = this.#lastRap
625
+ // A further RAP inside an already-anchored MPU is not an MPU start; the
626
+ // MPU keeps its first RAP as the spacing anchor (rap-not-mpu-start owns
627
+ // mid-MPU RAPs). The same RAP released again is no further one.
628
+ if (previous !== null && previous.mpu === mpu && previous.mediaTimescale === mediaTimescale) {
629
+ if (presentationTime === previous.presentationTime) return []
630
+ const interval = keyframeIntervalMediaTicks(this.#geometry, mediaTimescale)
631
+ if (
632
+ interval === null ||
633
+ decodeTime === undefined ||
634
+ previous.decodeTime === undefined ||
635
+ (decodeTime - previous.decodeTime) % interval !== 0n
636
+ ) {
637
+ this.#mpuBoundsRaps = false
638
+ }
508
639
  return []
509
640
  }
641
+ // A late copy of an earlier MPU's RAP (the gate reports stale releases
642
+ // too) moves no anchor back: the newest MPU's gaps are measured from its
643
+ // own. MPU sequence numbers wrap at 2^32, so "earlier" is serial; it is
644
+ // bounded, and a late copy is older in presentation time than the anchor.
645
+ // Any other RAP behind the anchor is a restarted MPU sequence nothing
646
+ // signalled, which re-anchors, or no same-MPU gap could ever be measured
647
+ // again. A restart inside the bound whose presentation time went back
648
+ // too still reads as late, and fails closed: its same-MPU gaps stay
649
+ // rap-unknown until its MPU numbers pass the old newest, and unless it
650
+ // repeats the old timeline the spacing checks then end the epoch
651
+ // all-protected.
652
+ if (previous !== null && previous.mediaTimescale === mediaTimescale) {
653
+ const back = (previous.mpu - mpu) >>> 0
654
+ if (back !== 0 && back <= LATE_RAP_MAX_MPUS_BACK && presentationTime < previous.presentationTime) return []
655
+ }
656
+ this.#lastRap = { mpu, presentationTime, mediaTimescale, ...(decodeTime === undefined ? {} : { decodeTime }) }
657
+ if (this.#mode === 'all-protected') return []
510
658
  // Verification compares integers in the media timescale. When the
511
659
  // catalog interval is not an exact tick count there, spacing is
512
660
  // unprovable: fail closed, naming the cause, rather than blaming the
513
661
  // encoder with rap-spacing-mismatch on a perfectly spaced RAP.
514
662
  const interval = keyframeIntervalMediaTicks(this.#geometry, mediaTimescale)
515
663
  if (interval === null) return this.#disable('keyframe-interval-not-media-integral')
516
- const previous = this.#lastRap
517
- // A further RAP inside an already-anchored MPU is not an MPU start; the
518
- // MPU keeps its first RAP as the spacing anchor (rap-not-mpu-start owns
519
- // mid-MPU RAPs).
520
- if (previous !== null && previous.mpu === mpu && previous.mediaTimescale === mediaTimescale) {
521
- return []
522
- }
523
- this.#lastRap = { mpu, presentationTime, mediaTimescale }
524
664
  if (
525
665
  previous === null ||
526
666
  mpu < previous.mpu ||
@@ -536,6 +676,23 @@ export class BlockClassifier {
536
676
  return this.#disable('rap-spacing-mismatch')
537
677
  }
538
678
 
679
+ /**
680
+ * Whether a gap inside MPU `mpu`, between the decode times `from` and `to`
681
+ * of the frames on either side, holds no keyframe: MPU boundaries still
682
+ * bound RAPs, the catalog states an interval exact in this timescale, and
683
+ * none of its instants after the MPU's first released RAP lies in
684
+ * `(from, to]` (fec-multicast-00 Block Classification item 3). Anything
685
+ * unknown counts as a keyframe.
686
+ */
687
+ keyframeFreeBetween(mpu: number, from: bigint, to: bigint, mediaTimescale: number): boolean {
688
+ const rap = this.#lastRap
689
+ if (!this.#mpuBoundsRaps || rap?.mpu !== mpu || rap.mediaTimescale !== mediaTimescale || rap.decodeTime === undefined) {
690
+ return false
691
+ }
692
+ const interval = keyframeIntervalMediaTicks(this.#geometry, mediaTimescale)
693
+ return interval !== null && !keyframeInstantBetween(rap.decodeTime, interval, from, to)
694
+ }
695
+
539
696
  #blockFor(sbn: number): BlockEvidence {
540
697
  let block = this.#blocks.get(sbn)
541
698
  if (!block) {
@@ -549,14 +706,13 @@ export class BlockClassifier {
549
706
  sbn: number,
550
707
  block: BlockEvidence,
551
708
  evidence: SourceSymbolEvidence,
709
+ placement: Placement,
552
710
  ): ClassifierReason | null {
553
711
  if (this.#mode === 'all-protected' || this.#geometry.trackKind !== 'video') return null
554
712
  const geometry = this.#geometry
555
713
  if (geometry.mode !== 'classifying') return null
556
- if (evidence.rap && evidence.idx !== undefined && evidence.idx !== 0) {
557
- return 'rap-not-mpu-start'
558
- }
559
- if (evidence.idx !== undefined && evidence.idx >= geometry.gopFrames) {
714
+ if (placement.rapNotMpuStart) return 'rap-not-mpu-start'
715
+ if (placement.position !== undefined && placement.position >= geometry.gopFrames) {
560
716
  return 'mpu-longer-than-gop'
561
717
  }
562
718
  // A block closes at the latest after D media units, so the DU headers it
@@ -565,32 +721,109 @@ export class BlockClassifier {
565
721
  // round-robin across concurrent blocks) and the window rule is void.
566
722
  if (
567
723
  block.mpus.size === 1 &&
568
- block.idxMin !== undefined &&
569
- block.idxMax !== undefined &&
570
- block.idxMax - block.idxMin >= geometry.interleaveUnits
724
+ block.positionMin !== undefined &&
725
+ block.positionMax !== undefined &&
726
+ block.positionMax - block.positionMin >= geometry.interleaveUnits
571
727
  ) {
572
728
  return 'block-wider-than-interleave'
573
729
  }
574
- if (evidence.mpu !== undefined && evidence.movieFragment !== undefined) {
575
- const fragment = this.#mpuFragments.get(evidence.mpu)
576
- if (fragment === undefined) {
577
- this.#mpuFragments.set(evidence.mpu, evidence.movieFragment)
578
- // One entry per MPU still inside the classification horizon.
579
- for (const mpu of this.#mpuFragments.keys()) {
580
- if (mpu + 1 < evidence.mpu) this.#mpuFragments.delete(mpu)
581
- }
582
- } else if (fragment !== evidence.movieFragment) {
583
- // sample_number is then a position within a movie fragment, not
584
- // within the MPU, and the window rule's geometry does not hold.
585
- return 'mpu-multiple-fragments'
586
- }
587
- }
730
+ if (placement.violation !== undefined) return placement.violation
588
731
  if (evidence.mpu !== undefined && this.#contradictsAuthoredOrder(sbn, evidence.mpu, evidence)) {
589
732
  return 'block-not-contiguous'
590
733
  }
591
734
  return null
592
735
  }
593
736
 
737
+ /**
738
+ * The MPU position a source symbol's DU header proves (P3), and what it
739
+ * refutes. Runs in every mode: mpuBoundsRaps needs its RAP verdict.
740
+ */
741
+ #place(evidence: SourceSymbolEvidence): Placement {
742
+ const { mpu, idx, movieFragment: fragment } = evidence
743
+ if (idx === undefined) return UNPLACED
744
+ const bySample: Placement = { position: idx, rapNotMpuStart: evidence.rap && idx !== 0 }
745
+ // Every wire DU header states both; evidence without them keeps the
746
+ // one-fragment rule unless the sequence is fragment-per-frame.
747
+ if (mpu === undefined || fragment === undefined) {
748
+ return this.#fragmentPerFrame && !this.#multiSampleFragment
749
+ ? { rapNotMpuStart: bySample.rapNotMpuStart }
750
+ : bySample
751
+ }
752
+ let layout = this.#mpus.get(mpu)
753
+ if (layout === undefined) {
754
+ layout = { low: fragment }
755
+ this.#mpus.set(mpu, layout)
756
+ // One entry per MPU still inside the classification horizon.
757
+ for (const other of this.#mpus.keys()) {
758
+ if (other + 1 < mpu) this.#mpus.delete(other)
759
+ }
760
+ } else if (fragment !== layout.low) {
761
+ this.#fragmentPerFrame = true
762
+ if (((fragment - layout.low) | 0) < 0) layout.low = fragment
763
+ }
764
+ if (idx !== 0) this.#multiSampleFragment = true
765
+ let arrival: number | undefined
766
+ if (evidence.rap && layout.anchor === undefined) {
767
+ layout.anchor = fragment
768
+ arrival = this.#anchorDrift(mpu, fragment)
769
+ // Proof takes two MPUs: an MPU's RAP header seen again proves nothing.
770
+ if (arrival === 0 && this.#lastAnchor?.mpu !== mpu) this.#fragmentsProven = true
771
+ this.#lastAnchor = { mpu, fragment }
772
+ }
773
+ // Until an MPU shows two fragments, sample_number − 1 is the position.
774
+ // Once one has, a sample_number above 1 leaves neither layout.
775
+ if (!this.#fragmentPerFrame) return bySample
776
+ if (this.#multiSampleFragment) return { ...bySample, violation: 'mpu-multiple-fragments' }
777
+ const { anchor } = layout
778
+ if (anchor === undefined) return UNPLACED
779
+ // A new anchor against the anchor received before it; any other DU
780
+ // header's anchor against the anchor received last, so an anchor that
781
+ // arrived before the layout was known is held to the spacing too.
782
+ const drift = arrival ?? this.#anchorDrift(mpu, anchor)
783
+ // RFC 1982 distance from the anchor: positive for a later fragment.
784
+ const offset = (fragment - anchor) | 0
785
+ return {
786
+ ...(offset === 0 || (offset > 0 && this.#fragmentsProven) ? { position: offset } : {}),
787
+ rapNotMpuStart:
788
+ (evidence.rap && offset !== 0) ||
789
+ ((layout.low - anchor) | 0) < 0 ||
790
+ // Fragments count frames: an anchor arriving past the spacing is a
791
+ // RAP inside its MPU, or an earlier MPU outran G.
792
+ (arrival !== undefined && arrival > 0 && this.#fragmentsProven),
793
+ ...(drift === undefined || drift === 0 ? {} : { violation: 'rap-spacing-mismatch' }),
794
+ }
795
+ }
796
+
797
+ /**
798
+ * How many fragments MPU m's anchor `fragment` lies past the start that
799
+ * the anchor received last predicts for MPU m: one frame per fragment and
800
+ * G frames per MPU put the anchors of MPUs m and m' exactly G * (m - m')
801
+ * fragments apart, modulo 2^32. Undefined without a last anchor, a
802
+ * cadence, or this layout.
803
+ */
804
+ #anchorDrift(mpu: number, fragment: number): number | undefined {
805
+ const geometry = this.#geometry
806
+ const last = this.#lastAnchor
807
+ if (
808
+ last === null ||
809
+ !this.#fragmentPerFrame ||
810
+ this.#multiSampleFragment ||
811
+ geometry.trackKind !== 'video' ||
812
+ geometry.mode !== 'classifying'
813
+ ) {
814
+ return undefined
815
+ }
816
+ return (fragment - last.fragment - geometry.gopFrames * (mpu - last.mpu)) | 0
817
+ }
818
+
819
+ #clearMpuLayout(): void {
820
+ this.#mpus.clear()
821
+ this.#fragmentPerFrame = false
822
+ this.#multiSampleFragment = false
823
+ this.#lastAnchor = null
824
+ this.#fragmentsProven = false
825
+ }
826
+
594
827
  #clearContiguityLedger(): void {
595
828
  this.#mpuSbnSpans.clear()
596
829
  this.#dataUnitStarts.clear()
@@ -698,14 +931,17 @@ export class BlockClassifier {
698
931
  if (geometry.trackKind !== 'video' || geometry.mode !== 'classifying') return 'unknown'
699
932
  if (
700
933
  block.mpus.size === 1 &&
701
- block.idxMin !== undefined &&
702
- block.idxMax !== undefined
934
+ block.positionMin !== undefined &&
935
+ block.positionMax !== undefined
703
936
  ) {
704
937
  // A block closes every D media units, so its frames are contiguous and
705
- // lie in [idxMax − D + 1, idxMin + D − 1] of MPU m. Index 0 is the only
706
- // RAP of an MPU and G bounds its length.
938
+ // lie in [positionMax − D + 1, positionMin + D − 1] of MPU m. Position
939
+ // 0 is the only RAP of an MPU and G bounds its length.
707
940
  const span = geometry.interleaveUnits - 1
708
- if (block.idxMax - span >= 1 && block.idxMin + span <= geometry.gopFrames - 1) {
941
+ if (
942
+ block.positionMax - span >= 1 &&
943
+ block.positionMin + span <= geometry.gopFrames - 1
944
+ ) {
709
945
  return 'delta'
710
946
  }
711
947
  }
@@ -746,7 +982,12 @@ export interface GapFrameEvidence {
746
982
  * same-MPU then no longer proves a gap lost only deltas.
747
983
  */
748
984
  readonly mpu?: number
749
- /** 0-based sample position within the MPU, when the geometry verifies it. */
985
+ /**
986
+ * 0-based sample position within its movie fragment (sample_number − 1).
987
+ * State it on both sides only when both frames are in one movie fragment:
988
+ * decideGap reads `after.idx − before.idx − 1` as the samples missed
989
+ * between them.
990
+ */
750
991
  readonly idx?: number
751
992
  /** Authored presentation time in `timescale` ticks. */
752
993
  readonly pts?: bigint