@blockcast/mmt-container 2.2.0-main.ea9223dc11de → 2.2.0-main.ec45bee1b9cc

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 (132) hide show
  1. package/README.md +125 -2
  2. package/dist/__tests__/ad-creative-signer.d.ts +11 -0
  3. package/dist/__tests__/ad-creative-signer.d.ts.map +1 -0
  4. package/dist/__tests__/test-utils.d.ts +1 -0
  5. package/dist/__tests__/test-utils.d.ts.map +1 -1
  6. package/dist/ad-beacon.d.ts +39 -0
  7. package/dist/ad-beacon.d.ts.map +1 -0
  8. package/dist/ad-beacon.js +23 -0
  9. package/dist/ad-beacon.js.map +7 -0
  10. package/dist/ad-creative-record.d.ts +65 -0
  11. package/dist/ad-creative-record.d.ts.map +1 -0
  12. package/dist/ad-creative-record.js +2281 -0
  13. package/dist/ad-creative-record.js.map +7 -0
  14. package/dist/ad-creative-timeline.d.ts +103 -0
  15. package/dist/ad-creative-timeline.d.ts.map +1 -0
  16. package/dist/ad-creative-timeline.js +2550 -0
  17. package/dist/ad-creative-timeline.js.map +7 -0
  18. package/dist/block-policy.d.ts +128 -14
  19. package/dist/block-policy.d.ts.map +1 -1
  20. package/dist/block-policy.js +472 -27
  21. package/dist/block-policy.js.map +2 -2
  22. package/dist/browser-runtime.js +1039 -120
  23. package/dist/browser-runtime.js.map +4 -4
  24. package/dist/carp-catalog.d.ts +1 -0
  25. package/dist/carp-catalog.d.ts.map +1 -1
  26. package/dist/carp-catalog.js +34 -0
  27. package/dist/carp-catalog.js.map +4 -4
  28. package/dist/catalog-fec-params.js.map +1 -1
  29. package/dist/catalog-network-source.d.ts +1 -17
  30. package/dist/catalog-network-source.d.ts.map +1 -1
  31. package/dist/catalog-network-source.js +25 -19
  32. package/dist/catalog-network-source.js.map +3 -3
  33. package/dist/cmaf-block-assembler.d.ts +10 -2
  34. package/dist/cmaf-block-assembler.d.ts.map +1 -1
  35. package/dist/cmaf-block-assembler.js +30 -8
  36. package/dist/cmaf-block-assembler.js.map +3 -3
  37. package/dist/codec-utils.d.ts.map +1 -1
  38. package/dist/codec-utils.js +16 -3
  39. package/dist/codec-utils.js.map +2 -2
  40. package/dist/conceal-timeline.d.ts +24 -1
  41. package/dist/conceal-timeline.d.ts.map +1 -1
  42. package/dist/conceal-timeline.js +201 -4
  43. package/dist/conceal-timeline.js.map +3 -3
  44. package/dist/datagram-leg-deadline.d.ts +4 -0
  45. package/dist/datagram-leg-deadline.d.ts.map +1 -1
  46. package/dist/datagram-leg-deadline.js.map +2 -2
  47. package/dist/event-timeline-mmtp.d.ts +17 -0
  48. package/dist/event-timeline-mmtp.d.ts.map +1 -0
  49. package/dist/event-timeline-mmtp.js +3117 -0
  50. package/dist/event-timeline-mmtp.js.map +7 -0
  51. package/dist/fec-manager.d.ts +24 -3
  52. package/dist/fec-manager.d.ts.map +1 -1
  53. package/dist/fec-manager.js +531 -38
  54. package/dist/fec-manager.js.map +2 -2
  55. package/dist/feed-gate.d.ts +114 -0
  56. package/dist/feed-gate.d.ts.map +1 -0
  57. package/dist/feed-gate.js +175 -0
  58. package/dist/feed-gate.js.map +7 -0
  59. package/dist/index.d.ts +3 -2
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +13 -1
  62. package/dist/index.js.map +2 -2
  63. package/dist/iso-bmff-timing.d.ts +16 -0
  64. package/dist/iso-bmff-timing.d.ts.map +1 -1
  65. package/dist/iso-bmff-timing.js +28 -0
  66. package/dist/iso-bmff-timing.js.map +2 -2
  67. package/dist/mfu-reassembler.d.ts +2 -0
  68. package/dist/mfu-reassembler.d.ts.map +1 -1
  69. package/dist/mfu-reassembler.js +4 -2
  70. package/dist/mfu-reassembler.js.map +2 -2
  71. package/dist/mmtp-container-parser.d.ts +12 -0
  72. package/dist/mmtp-container-parser.d.ts.map +1 -1
  73. package/dist/mmtp-container-parser.js +73 -3
  74. package/dist/mmtp-container-parser.js.map +2 -2
  75. package/dist/mmtp-container-parser.no-modules.js +103 -25
  76. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  77. package/dist/mmtp-decode-pipeline.js +73 -3
  78. package/dist/mmtp-decode-pipeline.js.map +2 -2
  79. package/dist/mmtp-router.d.ts +20 -2
  80. package/dist/mmtp-router.d.ts.map +1 -1
  81. package/dist/mmtp-router.js +150 -23
  82. package/dist/mmtp-router.js.map +3 -3
  83. package/dist/multicast-auth-catalog.js.map +1 -1
  84. package/dist/multicast-input.js.map +1 -1
  85. package/dist/multicast-manager.d.ts +23 -2
  86. package/dist/multicast-manager.d.ts.map +1 -1
  87. package/dist/multicast-manager.js +749 -87
  88. package/dist/multicast-manager.js.map +3 -3
  89. package/dist/multicast-transport.d.ts +8 -0
  90. package/dist/multicast-transport.d.ts.map +1 -1
  91. package/dist/multicast-transport.js +56 -23
  92. package/dist/multicast-transport.js.map +2 -2
  93. package/dist/no-modules.js +98 -22
  94. package/dist/no-modules.js.map +4 -4
  95. package/dist/repair-fetch.d.ts +15 -7
  96. package/dist/repair-fetch.d.ts.map +1 -1
  97. package/dist/repair-fetch.js +34 -7
  98. package/dist/repair-fetch.js.map +2 -2
  99. package/dist/wasm-mfu-reassembler.js.map +2 -2
  100. package/dist/wasm-mmtp-container-parser.js +73 -3
  101. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  102. package/package.json +17 -5
  103. package/schemas/catalog.schema.json +7 -2
  104. package/src/ad-beacon.ts +54 -0
  105. package/src/ad-creative-record.ts +276 -0
  106. package/src/ad-creative-timeline.ts +410 -0
  107. package/src/block-policy.ts +673 -42
  108. package/src/carp-catalog.ts +9 -0
  109. package/src/catalog-network-source.ts +3 -37
  110. package/src/cmaf-block-assembler.ts +17 -0
  111. package/src/codec-utils.ts +19 -3
  112. package/src/conceal-timeline.ts +60 -5
  113. package/src/datagram-leg-deadline.ts +4 -0
  114. package/src/event-timeline-mmtp.ts +55 -0
  115. package/src/fec-manager.ts +84 -12
  116. package/src/feed-gate.ts +295 -0
  117. package/src/index.ts +21 -0
  118. package/src/iso-bmff-timing.ts +33 -0
  119. package/src/mfu-reassembler.ts +4 -0
  120. package/src/mmtp-container-parser.ts +51 -5
  121. package/src/mmtp-router.ts +116 -4
  122. package/src/multicast-manager.ts +31 -0
  123. package/src/multicast-transport.ts +27 -2
  124. package/src/repair-fetch.ts +70 -19
  125. package/test-vectors/catalog/README.md +1 -0
  126. package/test-vectors/catalog/negative/event-timeline-empty-event-type.json +41 -0
  127. package/test-vectors/catalog/positive/event-timeline-ad-creative.json +41 -0
  128. package/test-vectors/catalog/positive/event-timeline-wallclock-no-depends.json +38 -0
  129. package/test-vectors/catalog/positive/multicast-event-timeline.json +81 -0
  130. package/test-vectors/catalog/section-codes.json +1 -0
  131. package/test-vectors/frame-needs-v1.json +295 -0
  132. package/test-vectors/repair-policy-v1.json +98 -18
@@ -37,11 +37,13 @@ function deriveBlockClassifierGeometry(input) {
37
37
  } else if (isPositiveFinite(input.keyframeIntervalMs)) {
38
38
  keyframeInterval = { ticks: input.keyframeIntervalMs, timescale: 1e3 };
39
39
  }
40
+ const groupDuration = isPositiveFinite(input.groupDurationMs) ? { groupDurationMs: input.groupDurationMs } : {};
40
41
  const protect = (reason) => ({
41
42
  trackKind: "video",
42
43
  mode: "all-protected",
43
44
  reason,
44
- ...keyframeInterval === void 0 ? {} : { keyframeInterval }
45
+ ...keyframeInterval === void 0 ? {} : { keyframeInterval },
46
+ ...groupDuration
45
47
  });
46
48
  if (!isPositiveFinite(input.framerate)) return protect("framerate-absent");
47
49
  const framerate = input.framerate;
@@ -57,9 +59,24 @@ function deriveBlockClassifierGeometry(input) {
57
59
  frameDurationMs,
58
60
  interleaveUnits,
59
61
  gopFrames,
60
- keyframeInterval
62
+ keyframeInterval,
63
+ ...groupDuration
61
64
  };
62
65
  }
66
+ function isPositiveSafeInteger(value) {
67
+ return Number.isSafeInteger(value) && value > 0;
68
+ }
69
+ function mpuRuleCatalogReason(geometry) {
70
+ const interval = geometry.keyframeInterval;
71
+ if (interval === void 0 || !isPositiveSafeInteger(interval.ticks) || !isPositiveSafeInteger(interval.timescale)) {
72
+ return "keyframe-interval-absent";
73
+ }
74
+ const groupMs = geometry.groupDurationMs;
75
+ if (!isPositiveFinite(groupMs)) return "group-duration-absent";
76
+ const { ticks, timescale } = interval;
77
+ const equal = Number.isSafeInteger(groupMs) ? BigInt(ticks) * 1000n === BigInt(groupMs) * BigInt(timescale) : Math.abs(ticks / timescale * 1e3 - groupMs) <= 2 * Number.EPSILON * groupMs;
78
+ return equal ? null : "keyframe-interval-not-group-duration";
79
+ }
63
80
  function keyframeIntervalMediaTicks(geometry, mediaTimescale) {
64
81
  if (geometry.trackKind !== "video" || geometry.keyframeInterval === void 0) return null;
65
82
  if (!Number.isSafeInteger(mediaTimescale) || mediaTimescale <= 0) return null;
@@ -77,6 +94,25 @@ function keyframeInstantBetween(start, interval, from, to) {
77
94
  function compareDataUnitStarts(a, b) {
78
95
  return a.movieFragment - b.movieFragment | 0 || a.idx - b.idx || a.offset - b.offset;
79
96
  }
97
+ function firstStartAtOrAfter(starts, sbn) {
98
+ let lo = 0;
99
+ let hi = starts.length;
100
+ while (lo < hi) {
101
+ const mid = lo + hi >>> 1;
102
+ if (starts[mid].sbn < sbn) lo = mid + 1;
103
+ else hi = mid;
104
+ }
105
+ return lo;
106
+ }
107
+ var ARRIVED_START_MAX = 4096;
108
+ function nextSampleHeader(headers, at) {
109
+ const of = headers[at];
110
+ for (let i = at + 1; i < headers.length; i++) {
111
+ const header = headers[i];
112
+ if (header.movieFragment !== of.movieFragment || header.idx !== of.idx) return header;
113
+ }
114
+ return void 0;
115
+ }
80
116
  var LATE_RAP_MAX_MPUS_BACK = 64;
81
117
  var UNPLACED = { rapNotMpuStart: false };
82
118
  var BlockClassifier = class {
@@ -101,6 +137,50 @@ var BlockClassifier = class {
101
137
  #mpuSbnSpans = /* @__PURE__ */ new Map();
102
138
  #dataUnitStarts = /* @__PURE__ */ new Map();
103
139
  #highestMpu = null;
140
+ /**
141
+ * The DU headers held, per MPU in authored order, for the MPU rule. Kept in
142
+ * every mode (the rule needs no catalog cadence) for the same horizon and
143
+ * the same continuous SBN sequence as the P1 ledger.
144
+ */
145
+ #headers = /* @__PURE__ */ new Map();
146
+ /**
147
+ * The source packets that arrived, per MPU. The MPU rule asks it whether
148
+ * every packet between two held DU headers arrived, and where. Same
149
+ * horizon as the headers.
150
+ */
151
+ #arrived = /* @__PURE__ */ new Map();
152
+ #heldHighestMpu = null;
153
+ /** Two DU headers of this sequence lay in blocks against their authored order: the MPU rule proves nothing. */
154
+ #headerOrderRefuted = false;
155
+ /**
156
+ * A DU header held carried RAP_flag and was not of its MPU's first sample:
157
+ * not the first sample of its movie fragment, or in another movie fragment
158
+ * than the first DU header held of its MPU. This stream puts a RAP inside
159
+ * an MPU, and neither rule proves anything for the rest of the epoch. It is
160
+ * the MPU rule's own reading, in every DU-header layout and of rebuilt
161
+ * headers too. The first case is read on a header of an MPU behind the two
162
+ * kept as well. `mpuBoundsRaps`, which the stall or conceal decision reads,
163
+ * is set as before and not from here.
164
+ */
165
+ #rapInsideMpu = false;
166
+ /** What the catalog lacks for the MPU rule, or null when it states a keyframe interval equal to the group duration. */
167
+ #mpuRuleCatalog = null;
168
+ /**
169
+ * The lowest and highest decode time known in an MPU, for the two MPUs of
170
+ * the highest numbers: those of the samples that held movie fragment
171
+ * metadata times (noteDecodeTimes). Valid within one MPU sequence, as the
172
+ * P1 ledger is; if MPU numbers go back with no source discontinuity, the
173
+ * new ones are not read until then, as their DU headers are not.
174
+ */
175
+ #decodeSpans = /* @__PURE__ */ new Map();
176
+ /**
177
+ * An MPU held a sample one keyframe interval or more after another by
178
+ * decode time, so it lasts longer than the interval. A track of an earlier
179
+ * revision whose interval equals its group duration can hold a further RAP
180
+ * only in such an MPU: neither rule proves anything for the rest of the
181
+ * epoch.
182
+ */
183
+ #mpuOutlastsInterval = false;
104
184
  /**
105
185
  * P3 layout, kept for the MPUs within the classification horizon and valid
106
186
  * within one continuous SBN sequence (`noteSourceDiscontinuity`).
@@ -143,6 +223,20 @@ var BlockClassifier = class {
143
223
  get mpuBoundsRaps() {
144
224
  return this.#mpuBoundsRaps;
145
225
  }
226
+ /**
227
+ * Why the MPU rule classes no block on this track now, or null while it
228
+ * can. A catalog reason is known from the start of the epoch; the others
229
+ * name what the stream showed. A block the rule would have classed reads
230
+ * `unknown` under every one of them, and so does one the window rule would
231
+ * have classed, under every one but `du-headers-out-of-order`.
232
+ */
233
+ get mpuRuleOff() {
234
+ if (this.#mpuRuleCatalog !== null) return this.#mpuRuleCatalog;
235
+ if (this.#rapInsideMpu) return "rap-inside-mpu";
236
+ if (this.#mpuOutlastsInterval) return "mpu-longer-than-keyframe-interval";
237
+ if (!this.#mpuBoundsRaps) return "mpu-bounds-raps-refuted";
238
+ return this.#headerOrderRefuted ? "du-headers-out-of-order" : null;
239
+ }
146
240
  /** Begin a fresh epoch (catalog change or track reset). */
147
241
  reset(geometry = this.#geometry) {
148
242
  this.#geometry = geometry;
@@ -151,6 +245,9 @@ var BlockClassifier = class {
151
245
  this.#clearMpuLayout();
152
246
  this.#lastRap = null;
153
247
  this.#mpuBoundsRaps = true;
248
+ this.#rapInsideMpu = false;
249
+ this.#mpuOutlastsInterval = false;
250
+ this.#mpuRuleCatalog = geometry.trackKind === "video" ? mpuRuleCatalogReason(geometry) : null;
154
251
  if (geometry.trackKind === "video" && geometry.mode === "all-protected") {
155
252
  this.#mode = "all-protected";
156
253
  this.#reason = geometry.reason;
@@ -191,9 +288,16 @@ var BlockClassifier = class {
191
288
  this.#clearMpuLayout();
192
289
  this.#lastRap = null;
193
290
  }
194
- /** Register a block that exists without evidence (a vanished block). */
291
+ /**
292
+ * Register a block that exists without evidence of its own: a vanished
293
+ * block, or one only repair symbols state. The DU headers already held
294
+ * around it class it now (the MPU rule); no later header need come between
295
+ * them to ask again.
296
+ */
195
297
  touch(sbn) {
196
- this.#blockFor(sbn);
298
+ const changes = [];
299
+ this.#reclassify(sbn, this.#blockFor(sbn), changes);
300
+ return changes;
197
301
  }
198
302
  /** Forget a resolved block. */
199
303
  forget(sbn) {
@@ -203,8 +307,15 @@ var BlockClassifier = class {
203
307
  if (this.#geometry.trackKind === "audio") return "audio";
204
308
  return this.#blocks.get(sbn)?.cls ?? "unknown";
205
309
  }
206
- /** Record one first-arrival source symbol's evidence for block `sbn`. */
207
- observe(sbn, evidence) {
310
+ /**
311
+ * Record one first-arrival source symbol's evidence for block `sbn`.
312
+ * `ssId` is its FEC source symbol id (the block's SS_Start plus its ESI,
313
+ * ISO/IEC 23008-1 C.5.2). The publisher numbers the packets of the FEC
314
+ * flow with it, one each, so the MPU rule reads from it whether every
315
+ * packet between two DU headers arrived. Without it the rule's two
316
+ * exceptions prove nothing.
317
+ */
318
+ observe(sbn, evidence, ssId) {
208
319
  if (this.#geometry.trackKind === "audio") return [];
209
320
  const changes = [];
210
321
  const block = this.#blockFor(sbn);
@@ -216,22 +327,137 @@ var BlockClassifier = class {
216
327
  block.positionMin = block.positionMin === void 0 ? position : Math.min(block.positionMin, position);
217
328
  block.positionMax = block.positionMax === void 0 ? position : Math.max(block.positionMax, position);
218
329
  }
219
- if (placement.rapNotMpuStart) this.#mpuBoundsRaps = false;
330
+ if (placement.rapNotMpuStart) this.#refuteMpuBounds(changes);
220
331
  const violation = this.#checkInvariants(sbn, block, evidence, placement);
221
332
  if (violation !== null) changes.push(...this.#disable(violation));
222
- const next = this.#classify(block);
223
- if (next !== block.cls && this.#allowed(block.cls, next)) {
224
- changes.push({
225
- kind: "reclassified",
226
- sbn,
227
- from: block.cls,
228
- to: next,
229
- evidence: this.#evidenceFor(block, next)
230
- });
231
- block.cls = next;
333
+ this.#noteArrived(sbn, evidence.mpu, ssId);
334
+ const bounds = this.#noteHeader(sbn, evidence, position, ssId, changes);
335
+ this.#reclassify(sbn, block, changes);
336
+ if (bounds) this.#reclassifyOthers(sbn, changes);
337
+ return changes;
338
+ }
339
+ /**
340
+ * A first-arrival source symbol placed in a repair-less span (IsoBlockLocator:
341
+ * the SS_IDs between two stated blocks whose every repair was lost). A span
342
+ * is no block: it can hold several producer blocks, so nothing block-scoped
343
+ * is read from it (no record, P1, P2 or MPU-rule bound) and its blocks stay
344
+ * unknown. What its DU header says of its MPU (P3) still counts (BLO-57323).
345
+ */
346
+ observeInSpan(evidence) {
347
+ if (this.#geometry.trackKind === "audio") return [];
348
+ const changes = [];
349
+ const placement = this.#place(evidence);
350
+ if (placement.rapNotMpuStart) this.#refuteMpuBounds(changes);
351
+ const geometry = this.#geometry;
352
+ if (this.#mode === "all-protected" || geometry.mode !== "classifying") return changes;
353
+ if (placement.rapNotMpuStart) changes.push(...this.#disable("rap-not-mpu-start"));
354
+ else if (placement.position !== void 0 && placement.position >= geometry.gopFrames) {
355
+ changes.push(...this.#disable("mpu-longer-than-gop"));
356
+ } else if (placement.violation !== void 0) changes.push(...this.#disable(placement.violation));
357
+ return changes;
358
+ }
359
+ /**
360
+ * The DU header of a source packet that a decode rebuilt, in block `sbn`.
361
+ * It bounds the blocks around that block for the MPU rule as a header that
362
+ * arrived does: the rule reads the DU headers the receiver holds. The
363
+ * rebuilt block is whole, so it gets no class and no record, and the
364
+ * packet is no evidence for P1 or P3, which rest on what arrived: it
365
+ * supports no class by the window rule, and can only end it. It is not
366
+ * counted as a packet that arrived either (#arrivedBetween).
367
+ */
368
+ observeRecovered(sbn, evidence) {
369
+ if (this.#geometry.trackKind === "audio") return [];
370
+ const { mpu, idx, movieFragment } = evidence;
371
+ if (mpu === void 0 || idx === void 0 || movieFragment === void 0) return [];
372
+ const changes = [];
373
+ if (idx !== 0) {
374
+ this.#multiSampleFragment = true;
375
+ const geometry = this.#geometry;
376
+ if (this.#fragmentPerFrame && geometry.trackKind === "video" && geometry.mode === "classifying") {
377
+ changes.push(...this.#disable("mpu-multiple-fragments"));
378
+ }
379
+ }
380
+ if (this.#noteHeader(sbn, evidence, this.#positionHeld(mpu, idx, movieFragment), void 0, changes)) {
381
+ this.#reclassifyOthers(sbn, changes);
232
382
  }
233
383
  return changes;
234
384
  }
385
+ /**
386
+ * Decode times the receiver can determine: `first` and `last` are those of
387
+ * the first and the last sample that one held movie fragment's metadata
388
+ * (FT=1) times, of MPU `mpu`, in ticks of `mediaTimescale`; a `trun` lists
389
+ * samples in decode order, so `first` is not after `last`. They are read
390
+ * for one thing, and only on a track the MPU rule applies to: an MPU with
391
+ * two samples a keyframe interval or more apart lasts longer than the
392
+ * interval, so its first sample need not be its only RAP. The rule then
393
+ * stops for the rest of the epoch, the window rule with it, and every delta
394
+ * class goes (fec-multicast-01 Block Classification). No class comes from
395
+ * a decode time. The MPU's first sample is never later than the earliest
396
+ * one known, so the known span is no longer than the real one.
397
+ */
398
+ noteDecodeTimes(mpu, first, last, mediaTimescale) {
399
+ const changes = [];
400
+ const geometry = this.#geometry;
401
+ if (geometry.trackKind !== "video" || this.#mpuRuleCatalog !== null || !isPositiveSafeInteger(mediaTimescale)) {
402
+ return changes;
403
+ }
404
+ const held = this.#decodeSpans.get(mpu);
405
+ const known = held?.mediaTimescale === mediaTimescale ? held : void 0;
406
+ let late = false;
407
+ if (held === void 0 && this.#decodeSpans.size >= 2) {
408
+ const lowest = Math.min(...this.#decodeSpans.keys());
409
+ late = mpu < lowest;
410
+ if (!late) this.#decodeSpans.delete(lowest);
411
+ }
412
+ const min = known !== void 0 && known.min < first ? known.min : first;
413
+ const max = known !== void 0 && known.max > last ? known.max : last;
414
+ if (!late) this.#decodeSpans.set(mpu, { min, max, mediaTimescale });
415
+ const { ticks, timescale } = geometry.keyframeInterval;
416
+ if ((max - min) * BigInt(timescale) >= BigInt(ticks) * BigInt(mediaTimescale)) {
417
+ this.#mpuOutlastsInterval = true;
418
+ this.#withdraw(changes);
419
+ }
420
+ return changes;
421
+ }
422
+ /** Ask again for every block held but `own`: a new DU header can bound any of them. */
423
+ #reclassifyOthers(own, changes) {
424
+ for (const [sbn, block] of this.#blocks) {
425
+ if (sbn !== own) this.#reclassify(sbn, block, changes);
426
+ }
427
+ }
428
+ /**
429
+ * Move delta blocks back to unknown: a premise their class stood on is
430
+ * gone. After DU headers out of block order, what the window rule still
431
+ * holds for is classed again as its blocks are seen.
432
+ */
433
+ #withdraw(changes) {
434
+ for (const [sbn, block] of this.#blocks) {
435
+ if (block.cls !== "delta") continue;
436
+ block.cls = "unknown";
437
+ changes.push({ kind: "reclassified", sbn, from: "delta", to: "unknown", evidence: "classifier-disabled" });
438
+ }
439
+ }
440
+ /**
441
+ * An MPU boundary stopped bounding this stream's RAPs: neither rule proves
442
+ * anything from now on, and every delta class goes.
443
+ */
444
+ #refuteMpuBounds(changes) {
445
+ if (!this.#mpuBoundsRaps) return;
446
+ this.#mpuBoundsRaps = false;
447
+ this.#withdraw(changes);
448
+ }
449
+ #reclassify(sbn, block, changes) {
450
+ const next = this.#classify(block, sbn);
451
+ if (next === block.cls || !this.#allowed(block.cls, next)) return;
452
+ changes.push({
453
+ kind: "reclassified",
454
+ sbn,
455
+ from: block.cls,
456
+ to: next,
457
+ evidence: this.#evidenceFor(block, next)
458
+ });
459
+ block.cls = next;
460
+ }
235
461
  /**
236
462
  * Record a RAP frame released in order. Released MPU-start RAPs of MPUs m
237
463
  * and m+n must be exactly n keyframe intervals apart in the media
@@ -259,7 +485,9 @@ var BlockClassifier = class {
259
485
  if (presentationTime === previous.presentationTime) return [];
260
486
  const interval2 = keyframeIntervalMediaTicks(this.#geometry, mediaTimescale);
261
487
  if (interval2 === null || decodeTime === void 0 || previous.decodeTime === void 0 || (decodeTime - previous.decodeTime) % interval2 !== 0n) {
262
- this.#mpuBoundsRaps = false;
488
+ const changes = [];
489
+ this.#refuteMpuBounds(changes);
490
+ return changes;
263
491
  }
264
492
  return [];
265
493
  }
@@ -364,6 +592,18 @@ var BlockClassifier = class {
364
592
  ...drift === void 0 || drift === 0 ? {} : { violation: "rap-spacing-mismatch" }
365
593
  };
366
594
  }
595
+ /**
596
+ * The MPU position #place gives a DU header, read from the layout the
597
+ * arrived headers built and recording nothing: where a rebuilt header
598
+ * stands for the catalog interval's check in the MPU rule.
599
+ */
600
+ #positionHeld(mpu, idx, fragment) {
601
+ if (!this.#fragmentPerFrame || this.#multiSampleFragment) return idx;
602
+ const anchor = this.#mpus.get(mpu)?.anchor;
603
+ if (anchor === void 0) return void 0;
604
+ const offset = fragment - anchor | 0;
605
+ return offset === 0 || offset > 0 && this.#fragmentsProven ? offset : void 0;
606
+ }
367
607
  /**
368
608
  * How many fragments MPU m's anchor `fragment` lies past the start that
369
609
  * the anchor received last predicts for MPU m: one frame per fragment and
@@ -390,6 +630,11 @@ var BlockClassifier = class {
390
630
  this.#mpuSbnSpans.clear();
391
631
  this.#dataUnitStarts.clear();
392
632
  this.#highestMpu = null;
633
+ this.#headers.clear();
634
+ this.#arrived.clear();
635
+ this.#heldHighestMpu = null;
636
+ this.#headerOrderRefuted = false;
637
+ this.#decodeSpans.clear();
393
638
  }
394
639
  /**
395
640
  * P1 check for one source packet of MPU `mpu` in block `sbn`, then record
@@ -480,18 +725,218 @@ var BlockClassifier = class {
480
725
  }
481
726
  return changes;
482
727
  }
483
- #classify(block) {
728
+ #classify(block, sbn) {
484
729
  if (block.rapSeen || block.mpus.size >= 2) return "keyframe";
485
- if (this.#mode === "all-protected") return "unknown";
730
+ if (block.cls === "delta") return "delta";
731
+ if (this.#windowHolds(block) || this.#mpuRuleHolds(block, sbn)) return "delta";
732
+ return "unknown";
733
+ }
734
+ #windowHolds(block) {
735
+ const off = this.mpuRuleOff;
736
+ if (this.#mode === "all-protected" || off !== null && off !== "du-headers-out-of-order") return false;
486
737
  const geometry = this.#geometry;
487
- if (geometry.trackKind !== "video" || geometry.mode !== "classifying") return "unknown";
488
- if (block.mpus.size === 1 && block.positionMin !== void 0 && block.positionMax !== void 0) {
489
- const span = geometry.interleaveUnits - 1;
490
- if (block.positionMax - span >= 1 && block.positionMin + span <= geometry.gopFrames - 1) {
491
- return "delta";
738
+ if (geometry.trackKind !== "video" || geometry.mode !== "classifying") return false;
739
+ if (block.mpus.size !== 1 || block.positionMin === void 0 || block.positionMax === void 0) {
740
+ return false;
741
+ }
742
+ const span = geometry.interleaveUnits - 1;
743
+ return block.positionMax - span >= 1 && block.positionMin + span <= geometry.gopFrames - 1;
744
+ }
745
+ /**
746
+ * Keep the header and arrival ledgers to the two newest MPUs. False for an
747
+ * MPU behind them, which is not kept: no block still asked lies there, and
748
+ * the ledgers stay bounded when MPU numbers go back with no source
749
+ * discontinuity (a restart nothing signalled, or the 2^32 wrap). The rule
750
+ * is then silent until the next one.
751
+ */
752
+ #noteHeldMpu(mpu) {
753
+ const highest = this.#heldHighestMpu;
754
+ if (highest !== null && mpu <= highest) return mpu + 1 >= highest;
755
+ this.#heldHighestMpu = mpu;
756
+ for (const held of [this.#headers, this.#arrived]) {
757
+ for (const other of [...held.keys()]) {
758
+ if (other + 1 < mpu) held.delete(other);
492
759
  }
493
760
  }
494
- return "unknown";
761
+ return true;
762
+ }
763
+ /** Record a source packet that arrived as source symbol `ssId`, for #arrivedBetween. */
764
+ #noteArrived(sbn, mpu, ssId) {
765
+ if (mpu === void 0 || ssId === void 0 || !this.#noteHeldMpu(mpu)) return;
766
+ const arrived = this.#arrived.get(mpu);
767
+ if (arrived === void 0) this.#arrived.set(mpu, { start: /* @__PURE__ */ new Map([[ssId, sbn]]), last: ssId });
768
+ else {
769
+ if (arrived.start.size < ARRIVED_START_MAX) arrived.start.set(ssId, sbn);
770
+ if (ssId > arrived.last) arrived.last = ssId;
771
+ }
772
+ }
773
+ /**
774
+ * Record a held DU header for the MPU rule; `ssId` is its source symbol id
775
+ * when it arrived, undefined for one that FEC rebuilt. True when it is new
776
+ * and in order: the blocks held are then asked again. Blocks follow
777
+ * authored order (P1), so the first header that lies against it withdraws
778
+ * every class, and the rule proves nothing for the rest of the sequence.
779
+ */
780
+ #noteHeader(sbn, evidence, position, ssId, changes) {
781
+ const { mpu, idx, movieFragment } = evidence;
782
+ if (this.#geometry.trackKind !== "video" || mpu === void 0 || idx === void 0 || movieFragment === void 0) {
783
+ return false;
784
+ }
785
+ if (!this.#noteHeldMpu(mpu)) {
786
+ if (evidence.rap && idx !== 0 && !this.#rapInsideMpu) {
787
+ this.#rapInsideMpu = true;
788
+ this.#withdraw(changes);
789
+ }
790
+ return false;
791
+ }
792
+ const header = {
793
+ movieFragment,
794
+ idx,
795
+ offset: evidence.offset ?? 0,
796
+ sbn,
797
+ rap: evidence.rap,
798
+ ...ssId === void 0 ? {} : { ssId },
799
+ ...position === void 0 ? {} : { position }
800
+ };
801
+ let headers = this.#headers.get(mpu);
802
+ if (headers === void 0) {
803
+ headers = [];
804
+ this.#headers.set(mpu, headers);
805
+ }
806
+ let at = 0;
807
+ let end = headers.length;
808
+ while (at < end) {
809
+ const mid = at + end >>> 1;
810
+ if (compareDataUnitStarts(headers[mid], header) < 0) at = mid + 1;
811
+ else end = mid;
812
+ }
813
+ if (at < headers.length && compareDataUnitStarts(headers[at], header) === 0) return false;
814
+ headers.splice(at, 0, header);
815
+ const first = headers[0];
816
+ if (!this.#rapInsideMpu && (header.rap && header.idx !== 0 || (at === 0 ? headers : [header]).some((held) => held.rap && held.movieFragment !== first.movieFragment))) {
817
+ this.#rapInsideMpu = true;
818
+ this.#withdraw(changes);
819
+ }
820
+ const before = headers[at - 1]?.sbn;
821
+ const after = headers[at + 1]?.sbn;
822
+ if (before !== void 0 && before > sbn || after !== void 0 && after < sbn) {
823
+ if (!this.#headerOrderRefuted) {
824
+ this.#headerOrderRefuted = true;
825
+ this.#withdraw(changes);
826
+ }
827
+ return false;
828
+ }
829
+ return true;
830
+ }
831
+ /**
832
+ * The MPU rule. L is the DU header held last before the block and U the
833
+ * one held first after it. Every packet carries its MPU number, and an MPU
834
+ * holds one RAP: its first sample.
835
+ *
836
+ * - R1, L and U in one MPU: the block holds no part of a RAP, provided the
837
+ * MPU's first sample ended before the block (#firstSampleEndedBefore).
838
+ * - R2, U in the next MPU: the block may hold the start of that MPU,
839
+ * unless that start arrived whole (#startArrivedWhole). The block is
840
+ * then the tail of L's MPU, and R1's proviso decides.
841
+ * - R3, more than one MPU apart: a whole MPU is gone, its RAP with it.
842
+ * - R4, no U held yet (or no L): not known. The next DU header asks again.
843
+ *
844
+ * It rests on the publisher keeping one RAP to an MPU, which no packet
845
+ * says. So it classes nothing unless the catalog states a keyframe interval
846
+ * equal to the group duration, and nothing once the stream showed
847
+ * otherwise: a RAP inside an MPU (mpuBoundsRaps, or a RAP-flagged DU header
848
+ * held that is not of its MPU's first sample: #noteHeader), an MPU longer than the
849
+ * keyframe interval by the decode times known (noteDecodeTimes), or DU
850
+ * headers against the block order. `mpuRuleOff` names which. `dep_counter`
851
+ * is not read.
852
+ */
853
+ #mpuRuleHolds(block, sbn) {
854
+ if (this.mpuRuleOff !== null) return false;
855
+ let lower;
856
+ let upper;
857
+ let lowerMpu = 0;
858
+ let upperMpu = 0;
859
+ let lowerAt = 0;
860
+ let upperAt = 0;
861
+ for (const [mpu, headers] of this.#headers) {
862
+ const before = firstStartAtOrAfter(headers, sbn) - 1;
863
+ const after = firstStartAtOrAfter(headers, sbn + 1);
864
+ const below = headers[before];
865
+ const above = headers[after];
866
+ if (below !== void 0 && (lower === void 0 || below.sbn > lower.sbn || below.sbn === lower.sbn && mpu > lowerMpu)) {
867
+ lower = below;
868
+ lowerMpu = mpu;
869
+ lowerAt = before;
870
+ }
871
+ if (above !== void 0 && (upper === void 0 || above.sbn < upper.sbn || above.sbn === upper.sbn && mpu < upperMpu)) {
872
+ upper = above;
873
+ upperMpu = mpu;
874
+ upperAt = after;
875
+ }
876
+ }
877
+ if (lower === void 0 || upper === void 0) return false;
878
+ if (block.mpus.size === 1 && !block.mpus.has(lowerMpu)) return false;
879
+ if (upperMpu === lowerMpu) {
880
+ if (upper.rap) return false;
881
+ const geometry = this.#geometry;
882
+ if (geometry.trackKind === "video" && geometry.mode === "classifying" && lower.position !== void 0 && upper.position !== void 0 && Math.floor(upper.position / geometry.gopFrames) > Math.floor(lower.position / geometry.gopFrames)) {
883
+ return false;
884
+ }
885
+ } else if (upperMpu !== lowerMpu + 1 || !this.#startArrivedWhole(upperMpu, upperAt)) {
886
+ return false;
887
+ }
888
+ return this.#firstSampleEndedBefore(sbn, lowerMpu, lowerAt);
889
+ }
890
+ /**
891
+ * R1's proviso for block `sbn`, where header `at` of MPU `mpu` is the one
892
+ * held last before it: no part of the MPU's first sample lies in the
893
+ * block. A sample start without RAP_flag is not the first sample. Any
894
+ * other header may be of it (a later data unit of a sample need not repeat
895
+ * the flag). That sample ended before the block only if every packet from
896
+ * the header to the next sample's DU header arrived, in earlier blocks.
897
+ */
898
+ #firstSampleEndedBefore(sbn, mpu, at) {
899
+ const headers = this.#headers.get(mpu);
900
+ const lower = headers[at];
901
+ if (lower.offset === 0 && !lower.rap) return true;
902
+ const next = nextSampleHeader(headers, at);
903
+ return next !== void 0 && this.#arrivedBetween(mpu, lower, next, sbn);
904
+ }
905
+ /**
906
+ * R2's exception: the start of MPU `mpu` arrived whole. Its header `at`,
907
+ * the DU header held first after the block, is of the MPU's RAP. Every
908
+ * packet before it arrived, back to the last packet of the MPU before: so
909
+ * did the MPU and movie fragment metadata sent before the sample. And
910
+ * every packet after it arrived, up to the next sample's DU header.
911
+ */
912
+ #startArrivedWhole(mpu, at) {
913
+ const headers = this.#headers.get(mpu);
914
+ const first = headers[at];
915
+ const arrived = this.#arrived.get(mpu);
916
+ const previous = this.#arrived.get(mpu - 1);
917
+ if (!first.rap || first.ssId === void 0 || arrived === void 0 || previous === void 0) return false;
918
+ let ssId = first.ssId - 1;
919
+ while (arrived.start.has(ssId)) ssId--;
920
+ if (ssId !== previous.last) return false;
921
+ const next = nextSampleHeader(headers, at);
922
+ return next !== void 0 && this.#arrivedBetween(mpu, first, next);
923
+ }
924
+ /**
925
+ * Whether every packet sent after DU header `from` and before DU header
926
+ * `to`, both of MPU `mpu`, arrived as a packet of that MPU, each in a block
927
+ * before `before` when one is given. The source symbol id counts every
928
+ * packet of the FEC flow, so no id may be missing between the two. Ids are
929
+ * read as plain numbers, as block starts are: past the 2^32 wrap of the id
930
+ * nothing is proved.
931
+ */
932
+ #arrivedBetween(mpu, from, to, before) {
933
+ const arrived = this.#arrived.get(mpu);
934
+ if (arrived === void 0 || from.ssId === void 0 || to.ssId === void 0) return false;
935
+ for (let ssId = from.ssId + 1; ssId !== to.ssId; ssId++) {
936
+ const sbn = arrived.start.get(ssId);
937
+ if (sbn === void 0 || before !== void 0 && sbn >= before) return false;
938
+ }
939
+ return true;
495
940
  }
496
941
  #allowed(from, to) {
497
942
  if (from === "keyframe") return false;
@@ -500,7 +945,7 @@ var BlockClassifier = class {
500
945
  return false;
501
946
  }
502
947
  #evidenceFor(block, to) {
503
- if (to === "delta") return "window";
948
+ if (to === "delta") return this.#windowHolds(block) ? "window" : "mpu-rule";
504
949
  return block.rapSeen ? "rap" : "mpu-span";
505
950
  }
506
951
  };