broadcast-common 13.0.0

Foundation for the rust-broadcast crates: symmetric Parse/Serialize traits, CRC-32/MPEG-2, BCD + MJD time codecs, bit I/O, 33-bit/PCR clock arithmetic, checked field widths, container-mux and Stage traits. no_std + alloc.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
//! Generic 33-bit wrapping-clock helpers.
//!
//! ISO/IEC 13818-1 §2.4.3.7 samples a 90 kHz clock into a 33-bit PTS/DTS
//! field; ANSI/SCTE 35 §9.2 `pts_time` reuses the identical 2^33 modulus so a
//! splice cue can be compared against the same clock. Both wrap roughly every
//! 26.5 hours, and any long-lived consumer that needs an ever-growing
//! timeline, or just needs to compare two nearby samples correctly across a
//! wrap boundary, needs the same handful of primitives. Before this module
//! existed, four crates (`timed-metadata`, `transmux`, `media-doctor`,
//! `compliance-probe`) each hand-rolled their own copy; an overrun or
//! wrap-direction fix in one reached none of the others. This module is now
//! the single owner both algorithms live in.
//!
//! `transmux` (a container-muxing hub) cannot take a dependency on
//! `timed-metadata` (a DPI/timed-metadata *signalling* conversion crate several
//! layers up the stack, pulling in `scte35-splice`/`mp4-emsg`) without an
//! inverted, heavy dependency edge, and the primitive itself has no
//! dependencies of its own — so it lives here, in the crate every one of the
//! four already depends on, rather than promoting one sibling to depend on
//! another.
//!
//! The values themselves are [`Clock33`] (a validated 33-bit 90 kHz sample) and
//! [`Clock27`] (a validated 27 MHz `base * 300 + ext`), so `mpeg-ts`'s `Pcr`,
//! `mpeg-ps`'s `Scr` and `mpeg-pes`'s `Pts`/`Dts` are all this one pair rather
//! than separate unchecked copies.
//!
//! Beyond the two validated types, two independent operations live here,
//! because they answer different questions and must not be collapsed into
//! one:
//!
//! - [`unwrap_delta`] — extend a running **unwrapped** (ever-growing, signed)
//!   accumulator by the next raw sample, correcting for exactly one wrap in
//!   *either* direction. Used to turn a repeating hardware counter into an
//!   absolute timeline (PTS/DTS unrolling across a capture, including
//!   B-frame reordering that dips slightly backward without crossing a
//!   wrap).
//! - [`pcr_forward_distance`] / [`pcr_signed_distance`] / [`pcr_from_parts`] /
//!   [`pcr_split`] — the same modular arithmetic for the 27 MHz PCR clock
//!   (`base * 300 + extension`, wrapping at `2^33 * 300`, which is *not* a power
//!   of two, so `wrapping_sub(..) % M` is wrong across a wrap).
//! - [`wrapping_forward_distance`] — the modular forward distance from one
//!   already-comparable raw value to another, with no accumulator or history
//!   at all. Used to classify a single pair of values as "in order" vs
//!   "wrapped/out of order" when the caller already knows the two are
//!   supposed to be close in time (e.g. a decode-order monotonicity check,
//!   or a splice cue's `pts_time` judged against a reference "now").

/// The 33-bit modulus (2^33) shared by MPEG-2 Systems PTS/DTS (ISO/IEC
/// 13818-1 §2.4.3.7) and SCTE-35 `pts_time` (ANSI/SCTE 35 §9.2) — both a
/// 90 kHz clock sampled into a 33-bit field.
pub const WRAP_33BIT: u64 = 1 << 33;

/// Half of [`WRAP_33BIT`] — the threshold distinguishing a genuine backward
/// step from a legal wrap.
pub const WRAP_33BIT_HALF: u64 = WRAP_33BIT / 2;

/// Extend a running unwrapped 33-bit clock by the delta to the next raw
/// value, correcting for a single wrap in either direction.
///
/// The delta is computed on the wrapped clock (a signed value in
/// `(-2^32, 2^32]`; a step of exactly half the range resolves forward, the
/// same tie rule as [`signed_distance`]), then applied to the unwrapped accumulator — so an
/// ordinary small backward step (e.g. B-frame PTS reordering) is preserved
/// as-is, and only a near-full-range jump is treated as a wrap.
/// `prev_unwrapped` need not itself be in `[0, 2^33)`; after the first wrap
/// it grows (or, in a reorder that dips across the origin before any wrap
/// has happened, can go slightly negative) without bound.
///
/// This is deliberately **bidirectional**: a naive "epoch counter that only
/// ever increments" unroller (which is what this replaced in
/// `timed-metadata`) gets a rare-but-real case wrong — a small backward
/// reorder that happens to straddle the wrap boundary (e.g. previous raw `2`,
/// next raw `2^33 - 3`, a legitimate 5-tick backward step) is
/// indistinguishable, from an epoch-counter's point of view, from a huge
/// forward jump, and it reports the latter. Computing the delta first and
/// only then deciding whether it wrapped gets both directions right.
#[must_use]
pub fn unwrap_delta(prev_unwrapped: i128, prev_raw: u64, raw: u64) -> i128 {
    let mut delta = raw as i128 - prev_raw as i128;
    if delta > WRAP_33BIT_HALF as i128 {
        delta -= WRAP_33BIT as i128; // wrapped backward across 2^33
    } else if delta <= -(WRAP_33BIT_HALF as i128) {
        delta += WRAP_33BIT as i128; // wrapped forward across 2^33
    }
    prev_unwrapped + delta
}

/// The modular forward distance from `from` to `to` on the 33-bit clock:
/// `(to - from) mod 2^33`, always in `[0, 2^33)`.
///
/// A distance greater than [`WRAP_33BIT_HALF`] means `to` is "behind" `from`
/// on the wrapped clock, not genuinely more than `2^32` ticks ahead — the
/// same wrap-vs-past ambiguity [`unwrap_delta`] resolves using history; this
/// function resolves it using only the half-range convention (no state),
/// which is enough when the caller already knows the two values are
/// supposed to be close in time.
#[must_use]
pub fn wrapping_forward_distance(from: u64, to: u64) -> u64 {
    to.wrapping_sub(from) % WRAP_33BIT
}

/// `(a + b) mod 2^33` — e.g. an SCTE-35 `pts_time` shifted by its
/// `pts_adjustment`: `add(WRAP_33BIT - 1, 1) == 0`, and inputs at or above
/// the modulus are reduced first, so `add(WRAP_33BIT + 3, 0) == 3`.
#[must_use]
pub fn add(a: u64, b: u64) -> u64 {
    let a = a % WRAP_33BIT;
    let b = b % WRAP_33BIT;
    // Each reduced operand is < 2^33, so the sum cannot overflow a u64.
    (a + b) % WRAP_33BIT
}

/// `(a + delta) mod 2^33` where `delta` may be negative; the result is always
/// in `[0, 2^33)` — e.g. stepping five ticks back past zero wraps forward:
/// `add_signed(5, -10) == WRAP_33BIT - 5`.
#[must_use]
pub fn add_signed(a: u64, delta: i64) -> u64 {
    // i128 intermediates: `delta` alone can be `i64::MIN`, and the sum must
    // not wrap before the Euclidean reduction.
    let sum = i128::from(a % WRAP_33BIT) + i128::from(delta);
    let reduced = sum.rem_euclid(i128::from(WRAP_33BIT));
    // `rem_euclid` by 2^33 is in `[0, 2^33)`, so the cast is lossless.
    reduced as u64
}

/// The shortest signed distance from `from` to `to` on the 2^33 circle, in
/// `(-2^32, 2^32]` — e.g. crossing the wrap forward reads as a small positive
/// step, `signed_distance(WRAP_33BIT - 10, 5) == 15`, while stepping back
/// across it reads negative, `signed_distance(5, WRAP_33BIT - 10) == -15`.
#[must_use]
pub fn signed_distance(from: u64, to: u64) -> i64 {
    let forward = wrapping_forward_distance(from % WRAP_33BIT, to % WRAP_33BIT);
    if forward <= WRAP_33BIT_HALF {
        forward as i64 // `forward <= 2^32`: lossless
    } else {
        let back = WRAP_33BIT - forward;
        // `back` is in `[1, 2^32)` here, so the negation cannot overflow.
        -(back as i64)
    }
}

// ── 27 MHz PCR clock (ISO/IEC 13818-1 §2.4.3.5) ─────────────────────────────

/// MPEG-2 Systems 90 kHz system-clock rate: the unit of PTS/DTS and of the
/// 33-bit PCR/ESCR/SCR *base* (ISO/IEC 13818-1 §2.4.3.5 / §2.4.3.7).
pub const PTS_HZ: u64 = PTS_TIMESCALE.as_u64();

/// [`PTS_HZ`] as a [`Timescale`](crate::timescale::Timescale).
pub const PTS_TIMESCALE: crate::timescale::Timescale = crate::timescale!(90_000);

/// The 27 MHz system-clock rate: the unit of a full `PCR` (`base * 300 +
/// extension`), ESCR and SCR value (ISO/IEC 13818-1 §2.4.2.1 / §2.4.3.5).
pub const PCR_HZ: u64 = PCR_TIMESCALE.as_u64();

/// [`PCR_HZ`] as a [`Timescale`](crate::timescale::Timescale).
pub const PCR_TIMESCALE: crate::timescale::Timescale = crate::timescale!(27_000_000);

/// `base * 300 + extension`: how many 27 MHz ticks one 90 kHz base tick spans
/// (`PCR_HZ / PTS_HZ`), and the modulus of the 9-bit `extension`.
pub const PCR_EXT_MODULUS: u64 = PCR_HZ / PTS_HZ;

/// The wrap period of a full PCR on the 27 MHz clock: `2^33 * 300` ticks
/// (~26.5 h). It is NOT a power of two, so a wrapped difference computed with
/// `u64::wrapping_sub(..) % PCR_WRAP_27MHZ` is wrong whenever the PCR wraps or
/// steps backward (2^64 is not a multiple of this modulus, so the u64 wrap
/// contributes a bogus `2^64 mod PCR_WRAP_27MHZ` offset): use
/// [`pcr_forward_distance`] / [`pcr_signed_distance`].
pub const PCR_WRAP_27MHZ: u64 = WRAP_33BIT * PCR_EXT_MODULUS;

/// A full 27 MHz clock value from its parts: `base * 300 + extension`
/// (ISO/IEC 13818-1 §2.4.3.5; the same arithmetic as ESCR §2.4.3.7 and the
/// program-stream SCR §2.5.3.4). Wrapping arithmetic: an out-of-range `base`
/// (wider than 33 bits) yields a meaningless value rather than a panic, like
/// every parser-fed helper in this crate.
#[must_use]
pub const fn pcr_from_parts(base: u64, extension: u16) -> u64 {
    base.wrapping_mul(PCR_EXT_MODULUS)
        .wrapping_add(extension as u64)
}

/// Split a 27 MHz clock value into `(base, extension)`: `base = ticks / 300`
/// truncated to its 33-bit wire width, `extension = ticks % 300` (always < 300,
/// so it fits the 9-bit field). Round-trips with [`pcr_from_parts`] for any
/// `ticks < PCR_WRAP_27MHZ`.
#[must_use]
pub const fn pcr_split(ticks: u64) -> (u64, u16) {
    (
        (ticks / PCR_EXT_MODULUS) & (WRAP_33BIT - 1),
        (ticks % PCR_EXT_MODULUS) as u16,
    )
}

/// The modular forward distance from `from` to `to` on the 27 MHz PCR clock:
/// `(to - from) mod 2^33*300`, always in `[0, PCR_WRAP_27MHZ)`. Inputs at or
/// above the modulus are reduced first. This is the PCR analogue of
/// [`wrapping_forward_distance`] and the only correct way to take a PCR delta
/// across a wrap (see [`PCR_WRAP_27MHZ`] for why `wrapping_sub` is not).
#[must_use]
pub fn pcr_forward_distance(from: u64, to: u64) -> u64 {
    // Both reduced operands are < M < 2^42, so `to + M - from` cannot overflow.
    (to % PCR_WRAP_27MHZ + PCR_WRAP_27MHZ - from % PCR_WRAP_27MHZ) % PCR_WRAP_27MHZ
}

/// The shortest signed distance from `from` to `to` on the PCR circle, in
/// `(-PCR_WRAP_27MHZ/2, PCR_WRAP_27MHZ/2]`: a forward wrap reads as a small
/// positive step, a small backward step (also across the wrap) as negative.
#[must_use]
pub fn pcr_signed_distance(from: u64, to: u64) -> i64 {
    let forward = pcr_forward_distance(from, to);
    if forward <= PCR_WRAP_27MHZ / 2 {
        forward as i64 // `forward <= M/2 < 2^41`: lossless
    } else {
        -((PCR_WRAP_27MHZ - forward) as i64) // in `[1, M/2)`: lossless
    }
}

// ── 5-byte marker-bit timestamp (PTS / DTS / DTS_next_AU) ───────────────────

/// All three `marker_bit`s of a 5-byte marked timestamp set to `1` (the value
/// every PES `PTS`/`DTS` must carry, ISO/IEC 13818-1 §2.4.3.7).
pub const MARKERS_ALL_SET: u8 = 0b111;

/// A decoded 5-byte marker-bit timestamp field.
///
/// Wire layout (ISO/IEC 13818-1 §2.4.3.7 Table 2-21 `PTS`/`DTS`; §2.4.3.5
/// `DTS_next_AU` uses the same shape): `prefix(4) ts[32:30](3) marker(1)
/// ts[29:15](15) marker(1) ts[14:0](15) marker(1)`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Marked {
    /// The leading 4 bits (`0b0010` PTS-only, `0b0011` PTS-with-DTS, `0b0001`
    /// DTS, or a seamless-splice `splice_type`).
    pub prefix: u8,
    /// The 33-bit timestamp.
    pub timestamp: u64,
    /// The three `marker_bit`s: bit 2 = byte 0 bit 0, bit 1 = byte 2 bit 0,
    /// bit 0 = byte 4 bit 0. A conformant field is [`MARKERS_ALL_SET`].
    pub markers: u8,
}

/// Decode a 5-byte marker-bit timestamp field. Never fails: the caller
/// validates `prefix` and `markers` against its own spec clause and maps a
/// mismatch into its own error type.
#[must_use]
pub fn unpack_marked(b: &[u8; 5]) -> Marked {
    let hi = u64::from((b[0] >> 1) & 0x07); // [32:30]
    let mid = (u64::from(b[1]) << 7) | u64::from(b[2] >> 1); // [29:15]
    let lo = (u64::from(b[3]) << 7) | u64::from(b[4] >> 1); // [14:0]
    Marked {
        prefix: b[0] >> 4,
        timestamp: (hi << 30) | (mid << 15) | lo,
        markers: ((b[0] & 1) << 2) | ((b[2] & 1) << 1) | (b[4] & 1),
    }
}

/// Encode a 5-byte marker-bit timestamp field with explicit marker bits (see
/// [`Marked::markers`]); `timestamp` is reduced modulo 2^33 and only the low
/// nibble of `prefix` / low 3 bits of `markers` are used (the wire widths).
#[must_use]
pub fn pack_marked_with(timestamp: u64, prefix: u8, markers: u8) -> [u8; 5] {
    let ts = timestamp & (WRAP_33BIT - 1);
    [
        ((prefix & 0x0F) << 4) | ((((ts >> 30) & 0x07) as u8) << 1) | ((markers >> 2) & 1),
        ((ts >> 22) & 0xFF) as u8,
        ((((ts >> 15) & 0x7F) as u8) << 1) | ((markers >> 1) & 1),
        ((ts >> 7) & 0xFF) as u8,
        (((ts & 0x7F) as u8) << 1) | (markers & 1),
    ]
}

/// Encode a conformant PTS/DTS field: every `marker_bit` set to `1`.
#[must_use]
pub fn pack_marked(timestamp: u64, prefix: u8) -> [u8; 5] {
    pack_marked_with(timestamp, prefix, MARKERS_ALL_SET)
}

// ── Validated clock values ──────────────────────────────────────────────────

/// A clock reading that does not fit the field it is carried in: a 33-bit
/// PTS/DTS/`pts_time` sample, or a 27 MHz PCR/ESCR/SCR base (beyond the
/// `2^33 * 300` wrap) or extension (`>= 300`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ClockOutOfRange {
    /// Which field was rejected: `"33-bit ticks"`, `"27 MHz base"`,
    /// `"27 MHz extension"` or `"27 MHz ticks"`.
    pub field: &'static str,
    /// The rejected value, widened to `u64` for reporting.
    pub value: u64,
}

impl core::fmt::Display for ClockOutOfRange {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{} value {} is out of range", self.field, self.value)
    }
}

impl core::error::Error for ClockOutOfRange {}

impl ClockOutOfRange {
    /// A rejected 27 MHz base: a value at or above the 33-bit wire width.
    ///
    /// The constructors here never produce one (a base is either already a
    /// [`Clock33`] or checked by [`Clock27::from_parts`]); a type that kept
    /// wire parts unvalidated reports an over-wide base with this.
    #[must_use]
    pub const fn base(value: u64) -> Self {
        Self {
            field: "27 MHz base",
            value,
        }
    }
}

/// A validated 33-bit 90 kHz clock sample (PTS, DTS, SCTE-35 `pts_time`).
///
/// ISO/IEC 13818-1 §2.4.3.7 samples the 90 kHz system clock into a 33-bit
/// field. This type is that value *after* the wire: always in `[0, 2^33)`, so
/// the modular helpers here never have to reduce their input and a caller
/// cannot hand them a value that silently wraps somewhere else. `mpeg-pes`'s
/// `Pts`/`Dts`, SCTE-35's `pts_time`/`pts_adjustment` and `timed-metadata`'s
/// timeline all carry this type.
///
/// `Default` is the origin (`0`). [`From<Clock33> for u64`], [`TryFrom<u64>`]
/// and [`TryFrom<i64>`] convert.
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Clock33(u64);

impl Clock33 {
    /// The origin of the clock (`0`).
    pub const ZERO: Self = Self(0);

    /// Construct from a raw tick count, rejecting any value at or above
    /// [`WRAP_33BIT`].
    ///
    /// # Errors
    /// [`ClockOutOfRange`] (`field: "33-bit ticks"`) when `ticks >= 2^33`.
    pub const fn try_new(ticks: u64) -> Result<Self, ClockOutOfRange> {
        if ticks >= WRAP_33BIT {
            Err(ClockOutOfRange {
                field: "33-bit ticks",
                value: ticks,
            })
        } else {
            Ok(Self(ticks))
        }
    }

    /// The raw tick count, always in `[0, 2^33)`.
    #[must_use]
    pub const fn ticks(self) -> u64 {
        self.0
    }

    /// `(self + b) mod 2^33` — see [`add`].
    #[must_use]
    pub const fn wrapping_add(self, b: Self) -> Self {
        // Both operands are < 2^33, so the sum cannot overflow a u64.
        Self((self.0 + b.0) % WRAP_33BIT)
    }

    /// `(self + delta) mod 2^33` for a possibly negative step — see
    /// [`add_signed`].
    #[must_use]
    pub fn wrapping_add_signed(self, delta: i64) -> Self {
        Self(add_signed(self.0, delta))
    }

    /// The shortest signed distance from `self` to `to` — see
    /// [`signed_distance`].
    #[must_use]
    pub fn signed_distance_to(self, to: Self) -> i64 {
        signed_distance(self.0, to.0)
    }
}

impl From<Clock33> for u64 {
    fn from(c: Clock33) -> Self {
        c.0
    }
}

impl TryFrom<u64> for Clock33 {
    type Error = ClockOutOfRange;
    fn try_from(ticks: u64) -> Result<Self, Self::Error> {
        Self::try_new(ticks)
    }
}

impl TryFrom<i64> for Clock33 {
    type Error = ClockOutOfRange;
    fn try_from(ticks: i64) -> Result<Self, Self::Error> {
        // A negative sample cannot name a position on the clock: report the
        // magnitude, which is the width that matters.
        Self::try_new(ticks.unsigned_abs())
    }
}

impl core::fmt::Debug for Clock33 {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "Clock33({})", self.0)
    }
}

impl core::fmt::Display for Clock33 {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// A validated 27 MHz clock value: `base * 300 + ext` (ISO/IEC 13818-1
/// §2.4.3.5 PCR, §2.4.3.7 ESCR, §2.5.3.4 program-stream SCR).
///
/// Construction ([`from_parts`](Self::from_parts),
/// [`from_27mhz`](Self::from_27mhz)) keeps `ext < 300`, so a value built that
/// way is always in `[0, PCR_WRAP_27MHZ)`. A *parsed* field may carry a 9-bit
/// extension in `300..=511` (off the clock): [`from_wire_parts`](Self::from_wire_parts)
/// keeps it losslessly and [`to_clock27`](Self::to_clock27) is the checked way
/// to use it. This is the one representation of a PCR/ESCR/SCR: `mpeg-ts`'s
/// `Pcr` and `mpeg-ps`'s `Scr` wrap it and add only their wire codecs.
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Clock27 {
    /// The 33-bit base (27 MHz ticks / 300).
    pub base: Clock33,
    /// The 9-bit extension (27 MHz ticks within the current base tick).
    pub ext: u16,
}

/// Serializes as the 27 MHz tick count: a PCR is stored as the one number
/// its wire fields code, so a snapshot survives the lossless `from_wire_parts`
/// round trip.
#[cfg(feature = "serde")]
impl serde::Serialize for Clock27 {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_u64(self.ticks())
    }
}

impl Clock27 {
    /// Construct from the two wire parts, checking both widths.
    ///
    /// # Errors
    /// [`ClockOutOfRange`] when `base >= 2^33` (`field: "27 MHz base"`) or
    /// `ext >= 300` (`field: "27 MHz extension"`).
    pub const fn from_parts(base: u64, ext: u16) -> Result<Self, ClockOutOfRange> {
        let base = match Clock33::try_new(base) {
            Ok(base) => base,
            Err(_) => {
                return Err(ClockOutOfRange {
                    field: "27 MHz base",
                    value: base,
                });
            }
        };
        if ext >= PCR_EXT_MODULUS as u16 {
            return Err(ClockOutOfRange {
                field: "27 MHz extension",
                value: ext as u64,
            });
        }
        Ok(Self { base, ext })
    }

    /// Split an absolute 27 MHz value into its two wire parts, the inverse of
    /// [`from_parts`](Self::from_parts) for any `ticks < PCR_WRAP_27MHZ`.
    ///
    /// # Errors
    /// [`ClockOutOfRange`] (`field: "27 MHz ticks"`) when
    /// `ticks >= PCR_WRAP_27MHZ`.
    pub fn from_27mhz(ticks: u64) -> Result<Self, ClockOutOfRange> {
        if ticks >= PCR_WRAP_27MHZ {
            return Err(ClockOutOfRange {
                field: "27 MHz ticks",
                value: ticks,
            });
        }
        let (base, ext) = pcr_split(ticks);
        // `ticks < PCR_WRAP_27MHZ` puts both parts in range by construction.
        Self::from_parts(base, ext)
    }

    /// The position `ticks` names on the 27 MHz clock, taking any count:
    /// `ticks` itself when it is below [`PCR_WRAP_27MHZ`], and `ticks`
    /// reduced modulo the wrap otherwise (a reading from a free-running counter
    /// that has passed the wrap).
    ///
    /// Infallible: the reduction puts both wire parts in range, so this is the
    /// constructor for a raw count that may be past the wrap, and
    /// [`from_27mhz`](Self::from_27mhz) the checked one that reports it.
    #[must_use]
    pub const fn from_27mhz_wrapping(ticks: u64) -> Self {
        let (base, ext) = pcr_split(ticks % PCR_WRAP_27MHZ);
        Self {
            // `ticks % PCR_WRAP_27MHZ` is 33 bits (base) times 300 (ext), so
            // both parts are in range by construction.
            base: Clock33(base),
            ext,
        }
    }

    /// The full value on the 27 MHz clock: `base * 300 + ext`.
    ///
    /// A constructed value is a real time reading. For a value from
    /// [`from_wire_parts`](Self::from_wire_parts) with an extension at or above
    /// 300 this is not a time on the clock, so callers check
    /// [`is_valid`](Self::is_valid) or [`to_clock27`](Self::to_clock27) first.
    #[must_use]
    pub const fn ticks(self) -> u64 {
        pcr_from_parts(self.base.ticks(), self.ext)
    }

    /// The value in seconds.
    #[must_use]
    pub fn seconds(self) -> f64 {
        self.ticks() as f64 / PCR_HZ as f64
    }

    /// The shortest signed distance from `self` to `to` — see
    /// [`pcr_signed_distance`].
    #[must_use]
    pub fn signed_distance_to(self, to: Self) -> i64 {
        pcr_signed_distance(self.ticks(), to.ticks())
    }

    /// Wrap the two parts exactly as the **wire** carried them, without
    /// validating.
    ///
    /// A PCR/ESCR/SCR field is 33-bit base + 9-bit extension, so real
    /// captures do carry extensions in `300..=511` — a value off the 27 MHz
    /// clock that this type cannot hold. Parsing must stay lossless (the
    /// workspace's byte-exact round-trip invariant), so a parser uses this to
    /// keep the wire bytes, and a caller that needs a real clock reading asks
    /// [`to_clock27`](Self::to_clock27) for the checked view.
    ///
    /// Code that *constructs* a clock value uses [`from_parts`](Self::from_parts)
    /// instead, which rejects those parts.
    #[must_use]
    pub const fn from_wire_parts(base: u64, ext: u16) -> Self {
        Self {
            // The 33-bit field cannot exceed the clock's range; the wire
            // width, not validation, decides here.
            base: Clock33(base & (WRAP_33BIT - 1)),
            ext,
        }
    }

    /// The checked view of these parts: `Ok` when they name a position on the
    /// 27 MHz clock.
    ///
    /// # Errors
    /// [`ClockOutOfRange`] when `ext >= 300`. The base is always a [`Clock33`],
    /// so it cannot be out of range.
    pub const fn to_clock27(self) -> Result<Self, ClockOutOfRange> {
        if self.ext >= PCR_EXT_MODULUS as u16 {
            Err(ClockOutOfRange {
                field: "27 MHz extension",
                value: self.ext as u64,
            })
        } else {
            Ok(self)
        }
    }

    /// Whether [`ticks`](Self::ticks) is `base * 300 + ext` (an extension
    /// below 300) rather than a wire value off the 27 MHz clock.
    #[must_use]
    pub const fn is_valid(self) -> bool {
        self.ext < PCR_EXT_MODULUS as u16
    }
}

impl From<Clock27> for u64 {
    fn from(c: Clock27) -> Self {
        c.ticks()
    }
}

impl core::fmt::Debug for Clock27 {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "Clock27({})", self.ticks())
    }
}

impl core::fmt::Display for Clock27 {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.ticks())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn unwrap_delta_half_range_step_agrees_with_signed_distance() {
        let step = signed_distance(WRAP_33BIT_HALF, 0);
        assert_eq!(step, WRAP_33BIT_HALF as i64);
        assert_eq!(unwrap_delta(0, WRAP_33BIT_HALF, 0), i128::from(step));
        assert_eq!(unwrap_delta(0, 0, WRAP_33BIT_HALF), i128::from(step));
    }

    #[test]
    fn unwrap_delta_forward_wrap_advances_by_one_modulus() {
        // prev near the top of the range, next small: a legitimate forward
        // wrap of +8 ticks, not a ~2^33-tick backward jump.
        let prev_unwrapped = (WRAP_33BIT - 10) as i128;
        let got = unwrap_delta(prev_unwrapped, WRAP_33BIT - 10, 5);
        assert_eq!(got, prev_unwrapped + 15);
        assert_eq!(got, 5 + WRAP_33BIT as i128);
    }

    #[test]
    fn unwrap_delta_small_forward_step_is_identity_shift() {
        assert_eq!(unwrap_delta(1_000, 1_000, 2_000), 2_000);
    }

    #[test]
    fn unwrap_delta_small_backward_step_is_preserved_not_wrapped() {
        // Ordinary B-frame reordering: a small backward step within an
        // epoch must NOT be treated as a wrap.
        assert_eq!(unwrap_delta(2_000, 2_000, 1_995), 1_995);
    }

    /// MUTATION-PROOF: a reorder that straddles the origin (previous raw
    /// value small, next raw value near the top of the range, representing a
    /// genuine small *backward* step across 0) must unwrap to a small
    /// negative delta, not a huge forward jump. This is exactly the case a
    /// naive forward-only epoch counter (what `timed_metadata::Timeline`
    /// used before this module existed) gets wrong. Verified by temporarily
    /// deleting the `delta > WRAP_33BIT_HALF` branch below (so only forward
    /// wraps are corrected): this test then fails with `got = 2^33 - 3`
    /// instead of `-3`, confirming the branch is load-bearing. Restored.
    #[test]
    fn unwrap_delta_backward_reorder_across_origin_stays_small_and_negative() {
        let got = unwrap_delta(2, 2, WRAP_33BIT - 3);
        assert_eq!(got, -3);
    }

    #[test]
    fn wrapping_forward_distance_small_forward_is_small() {
        assert_eq!(wrapping_forward_distance(100, 105), 5);
    }

    #[test]
    fn wrapping_forward_distance_wraps_at_modulus() {
        assert_eq!(wrapping_forward_distance(WRAP_33BIT - 1, 0), 1);
    }

    #[test]
    fn wrapping_forward_distance_backward_step_is_large() {
        // A backward step of 5 reports as (modulus - 5): a huge forward
        // distance, which callers threshold against `WRAP_33BIT_HALF` to
        // classify as "actually behind", not "far ahead".
        let d = wrapping_forward_distance(105, 100);
        assert_eq!(d, WRAP_33BIT - 5);
        assert!(d > WRAP_33BIT_HALF);
    }

    #[test]
    fn add_wraps_at_modulus() {
        assert_eq!(add(WRAP_33BIT - 1, 1), 0);
        assert_eq!(add(WRAP_33BIT - 10, 20), 10);
    }

    #[test]
    fn add_reduces_inputs_at_or_above_the_modulus() {
        assert_eq!(add(WRAP_33BIT + 3, 0), 3);
    }

    #[test]
    fn add_signed_negative_delta_wraps_below_zero() {
        assert_eq!(add_signed(5, -10), WRAP_33BIT - 5);
    }

    #[test]
    fn add_signed_extreme_deltas_stay_in_range() {
        for delta in [i64::MIN, i64::MAX] {
            let got = add_signed(0, delta);
            assert!(
                got < WRAP_33BIT,
                "add_signed(0, {delta}) = {got} escaped [0, 2^33)"
            );
        }
    }

    #[test]
    fn signed_distance_matches_doc_examples() {
        assert_eq!(signed_distance(WRAP_33BIT - 10, 5), 15);
        assert_eq!(signed_distance(5, WRAP_33BIT - 10), -15);
    }

    #[test]
    fn signed_distance_half_range_boundary_is_positive() {
        // The range is (-2^32, 2^32]: exactly half the modulus reads as +2^32,
        // one tick past it flips to the negative shortest path.
        assert_eq!(signed_distance(0, WRAP_33BIT_HALF), WRAP_33BIT_HALF as i64);
        assert_eq!(
            signed_distance(0, WRAP_33BIT_HALF + 1),
            -((WRAP_33BIT_HALF - 1) as i64)
        );
    }

    #[test]
    fn signed_distance_reduces_inputs_at_or_above_the_modulus() {
        assert_eq!(signed_distance(WRAP_33BIT + 5, WRAP_33BIT + 8), 3);
    }
    #[test]
    fn clock33_rejects_every_out_of_width_value() {
        assert_eq!(
            Clock33::try_new(WRAP_33BIT - 1),
            Ok(Clock33(WRAP_33BIT - 1))
        );
        for bad in [WRAP_33BIT, WRAP_33BIT + 1, u64::MAX] {
            let err = Clock33::try_new(bad).unwrap_err();
            assert_eq!((err.field, err.value), ("33-bit ticks", bad));
            assert_eq!(Clock33::try_from(bad).unwrap_err(), err);
        }
        assert_eq!(Clock33::try_from(7u64), Ok(Clock33(7)));
        assert_eq!(Clock33::try_from(0u64), Ok(Clock33::ZERO));
    }

    #[test]
    fn clock33_arithmetic_wraps_and_agrees_with_the_free_functions() {
        let top = Clock33::try_new(WRAP_33BIT - 1).expect("in range");
        let one = Clock33::try_new(1).expect("in range");
        assert_eq!(top.wrapping_add(one), Clock33::ZERO);
        assert_eq!(top.wrapping_add_signed(1), Clock33::ZERO);
        assert_eq!(top.wrapping_add_signed(2), one);
        let five = Clock33::try_new(5).expect("in range");
        assert_eq!(five.signed_distance_to(top), -6);
        assert_eq!(top.signed_distance_to(five), 6);
        assert_eq!(u64::from(top), WRAP_33BIT - 1);
    }

    #[test]
    fn clock27_validates_both_wire_widths() {
        assert_eq!(
            Clock27::from_parts(WRAP_33BIT, 0).unwrap_err().field,
            "27 MHz base"
        );
        assert_eq!(
            Clock27::from_27mhz(PCR_WRAP_27MHZ).unwrap_err(),
            ClockOutOfRange {
                field: "27 MHz ticks",
                value: PCR_WRAP_27MHZ
            }
        );
        // 300..=511 fits the 9-bit wire field but not the clock.
        assert_eq!(
            Clock27::from_parts(0, 300).unwrap_err(),
            ClockOutOfRange {
                field: "27 MHz extension",
                value: 300
            }
        );
        assert!(Clock27::from_parts(WRAP_33BIT - 1, 299).is_ok());
    }

    #[test]
    fn clock27_wire_parts_keep_off_clock_values_and_report_them() {
        for ext in [0u16, 1, 299, 300, 408, 511] {
            let c = Clock27::from_wire_parts(0x1_2345_6789, ext);
            assert_eq!((c.base.ticks(), c.ext), (0x1_2345_6789, ext));
            assert_eq!(c.is_valid(), ext < 300);
            match c.to_clock27() {
                Ok(checked) => {
                    assert!(ext < 300);
                    assert_eq!(checked, c);
                }
                Err(err) => {
                    assert!(ext >= 300);
                    assert_eq!((err.field, err.value), ("27 MHz extension", u64::from(ext)));
                }
            }
        }
        // The base is a 33-bit wire field, kept as its low 33 bits.
        assert_eq!(Clock27::from_wire_parts(WRAP_33BIT | 5, 0).base.ticks(), 5);
        assert_eq!(ClockOutOfRange::base(WRAP_33BIT).field, "27 MHz base");
    }

    #[test]
    fn clock27_round_trips_a_full_revolution() {
        for ticks in [0, 1, 299, 300, 27_000_000, PCR_WRAP_27MHZ - 1] {
            let c = Clock27::from_27mhz(ticks).expect("in range");
            assert_eq!(c.ticks(), ticks);
            assert_eq!(u64::from(c), ticks);
            assert_eq!(Clock27::from_parts(c.base.ticks(), c.ext), Ok(c));
        }
        let a = Clock27::from_27mhz(PCR_WRAP_27MHZ - 5).expect("in range");
        let b = Clock27::from_27mhz(10).expect("in range");
        assert_eq!(a.signed_distance_to(b), 15);
    }

    #[test]
    fn pcr_constants_are_the_spec_values() {
        assert_eq!(PCR_EXT_MODULUS, 300);
        assert_eq!(PCR_WRAP_27MHZ, (1u64 << 33) * 300);
        assert_eq!(PCR_WRAP_27MHZ, 2_576_980_377_600);
        assert_eq!(PCR_HZ, PTS_HZ * PCR_EXT_MODULUS);
    }

    /// A count past the wrap is reduced onto the clock, a count below it is
    /// taken as it is, and the result is always a real position.
    #[test]
    fn a_wrapping_27mhz_constructor_reduces_onto_the_clock() {
        for ticks in [0, 1, 299, 300, 27_000_000, PCR_WRAP_27MHZ - 1] {
            let c = Clock27::from_27mhz_wrapping(ticks);
            assert_eq!(c.ticks(), ticks);
            assert!(c.is_valid());
        }
        let wrapped = Clock27::from_27mhz_wrapping(PCR_WRAP_27MHZ + 300);
        assert_eq!(wrapped.ticks(), 300);
        assert!(wrapped.is_valid());
        assert_eq!(
            Clock27::from_27mhz_wrapping(u64::MAX).ticks(),
            u64::MAX % PCR_WRAP_27MHZ
        );
    }

    #[test]
    fn pcr_parts_round_trip() {
        for ticks in [0, 1, 299, 300, 301, 27_000_000, PCR_WRAP_27MHZ - 1] {
            let (base, ext) = pcr_split(ticks);
            assert!(ext < 300 && base < WRAP_33BIT);
            assert_eq!(pcr_from_parts(base, ext), ticks);
        }
        // An over-wide base wraps instead of panicking.
        let _ = pcr_from_parts(u64::MAX, 299);
    }

    #[test]
    fn pcr_forward_distance_across_the_wrap() {
        let m = PCR_WRAP_27MHZ;
        assert_eq!(pcr_forward_distance(m - 5, 10), 15);
        assert_eq!(pcr_forward_distance(10, m - 5), m - 15);
        assert_eq!(pcr_forward_distance(7, 7), 0);
        assert_eq!(pcr_forward_distance(0, m - 1), m - 1);
        // Inputs at or above the modulus are reduced first.
        assert_eq!(pcr_forward_distance(m + 3, 5), 2);
    }

    /// The bug this exists to prevent: `to.wrapping_sub(from) % M` is wrong
    /// whenever `to < from`, because 2^64 mod M != 0.
    #[test]
    fn pcr_forward_distance_is_not_wrapping_sub_mod_m() {
        let m = PCR_WRAP_27MHZ;
        let (from, to) = (m - 5, 10u64);
        let naive = to.wrapping_sub(from) % m;
        assert_ne!(naive, 15, "the naive form is wrong across a wrap");
        assert_eq!(pcr_forward_distance(from, to), 15);
    }

    #[test]
    fn pcr_signed_distance_is_the_shortest_path() {
        let m = PCR_WRAP_27MHZ;
        assert_eq!(pcr_signed_distance(m - 10, 5), 15);
        assert_eq!(pcr_signed_distance(5, m - 10), -15);
        assert_eq!(pcr_signed_distance(0, m / 2), (m / 2) as i64);
        assert_eq!(pcr_signed_distance(0, m / 2 + 1), -((m / 2 - 1) as i64));
        assert_eq!(pcr_signed_distance(100, 100), 0);
    }
    /// A REAL 5-byte PTS field: the first PES of `fixtures/ts/m6-single.ts`
    /// (private_stream_1, `PTS_DTS_flags = 10`). The expected value was decoded
    /// independently by slicing the 40-bit string as
    /// `prefix(4) ts[32:30](3) '1' ts[29:15](15) '1' ts[14:0](15) '1'`.
    #[test]
    fn marked_decodes_and_re_encodes_a_real_capture_pts_field() {
        let real = [0x29, 0xFD, 0x91, 0xD2, 0x29];
        let m = unpack_marked(&real);
        assert_eq!(
            m,
            Marked {
                prefix: 0b0010,
                timestamp: 5_358_512_404,
                markers: MARKERS_ALL_SET
            }
        );
        assert_eq!(pack_marked(m.timestamp, m.prefix), real);
    }

    #[test]
    fn marked_round_trips_every_boundary_and_marker_combination() {
        for ts in [0, 1, 90_000, 0x1_2345_6789, WRAP_33BIT - 1] {
            for prefix in [0b0001, 0b0010, 0b0011, 0x0F, 0] {
                for markers in 0..8u8 {
                    let b = pack_marked_with(ts, prefix, markers);
                    let m = unpack_marked(&b);
                    assert_eq!(
                        (m.prefix, m.timestamp, m.markers),
                        (prefix, ts, markers),
                        "ts={ts:#x} prefix={prefix:#x} markers={markers:#05b}"
                    );
                }
            }
        }
    }

    #[test]
    fn marked_pack_reduces_modulo_2_pow_33_and_masks_the_prefix() {
        assert_eq!(pack_marked(WRAP_33BIT + 5, 0b0010), pack_marked(5, 0b0010));
        // Only the low nibble of the prefix is on the wire.
        assert_eq!(pack_marked(7, 0xF2), pack_marked(7, 0x02));
    }

    #[test]
    fn marked_a_cleared_marker_is_visible_to_the_caller() {
        let mut b = pack_marked(0x1234, 0b0010);
        b[2] &= !1; // clear the middle marker bit
        assert_eq!(unpack_marked(&b).markers, 0b101);
    }
}