oxideav-opus 0.0.12

Opus audio codec — orphan-rebuild scaffold pending clean-room re-implementation.
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
//! SILK + CELT decoder state-reset policy across mode transitions
//! (RFC 6716 §4.5.2 "State Reset", p. 127).
//!
//! Round 26 (`celt_redundancy`) decoded the §4.5.1 redundancy-flag
//! metadata at the tail of every SILK-only or Hybrid Opus frame.
//! §4.5.2 picks up at the next normative step: deciding, given the
//! previous frame's mode, the current frame's mode, and whether the
//! transition carries a redundant CELT frame, which of the two
//! sub-decoders (SILK, CELT) must be reset, and — when a redundant
//! CELT frame is present — whether that reset happens *before* the
//! redundant frame or *between* the redundant frame and the main
//! frame.
//!
//! ## Rule transcription
//!
//! The four normative sentences of §4.5.2 are:
//!
//! 1. *"The SILK state is reset before every SILK-only or Hybrid
//!    frame where the previous frame was CELT-only."*
//! 2. *"The CELT state is reset every time the operating mode changes
//!    and the new mode is either Hybrid or CELT-only, except when the
//!    transition uses redundancy as described above."*
//! 3. *"When switching from SILK-only or Hybrid to CELT-only with
//!    redundancy, the CELT state is reset before decoding the
//!    redundant CELT frame embedded in the SILK-only or Hybrid frame,
//!    but it is not reset before decoding the following CELT-only
//!    frame."*
//! 4. *"When switching from CELT-only mode to SILK-only or Hybrid
//!    mode with redundancy, the CELT decoder is not reset for
//!    decoding the redundant CELT frame."*
//!
//! ## The "operating mode changes" predicate
//!
//! Rule 2 keys off whether the operating mode actually changes. Two
//! consecutive Hybrid frames, or two consecutive CELT-only frames, do
//! NOT trigger a CELT reset under rule 2: §4.5.2 only resets CELT
//! "every time the operating mode changes and the new mode is either
//! Hybrid or CELT-only", and a mode that did not change is the
//! complement of "the operating mode changes". The same predicate
//! also gates rule 1 (the SILK-reset clause), where "where the
//! previous frame was CELT-only" implicitly requires the current
//! frame to NOT be CELT-only (a CELT-only → CELT-only sequence does
//! not match because the *current* frame would not be SILK-only or
//! Hybrid).
//!
//! ## The redundancy exception
//!
//! Rules 2 + 3 carve out three sub-cases that need to be tracked
//! separately because they place the reset boundary differently:
//!
//! * **SILK/Hybrid → CELT-only WITH redundancy.** Rule 3 places the
//!   CELT reset *before the redundant CELT frame*, then keeps state
//!   live across the redundant frame into the following CELT-only
//!   frame ("but it is not reset before decoding the following
//!   CELT-only frame"). [`CeltResetPlacement::BeforeRedundantOnly`]
//!   captures this.
//! * **CELT-only → SILK-only/Hybrid WITH redundancy.** Rule 4
//!   forbids resetting CELT before the redundant CELT frame. SILK
//!   resets per rule 1 (since the predecessor was CELT-only and the
//!   current frame is SILK-only or Hybrid). The CELT decoder is not
//!   reset in this transition at all from §4.5.2's standpoint.
//!   [`CeltResetPlacement::None`].
//! * **Hybrid → Hybrid, CELT-only → CELT-only.** Mode does not
//!   change; rule 2 does not fire even when redundancy happens to be
//!   present. [`CeltResetPlacement::None`].
//!
//! All other mode-changing transitions to Hybrid or CELT-only fall
//! under the rule-2 default: [`CeltResetPlacement::BeforeFrame`].
//!
//! ## Provenance
//!
//! Every clause, every transition outcome, the carve-out for
//! redundancy, and the "before-redundant vs. before-frame" placement
//! is transcribed from RFC 6716 §4.5.2 in
//! `docs/audio/opus/rfc6716-opus.txt` (pp. 127). No external library
//! source was consulted. The companion §4.5.3 transition figure
//! (p. 128) is a non-normative summary and was not used to seed any
//! new rules — only as a cross-check that the four rules above
//! reproduce the figure's reset markers (`;` SILK reset, `|` SILK +
//! CELT resets).

use crate::celt_redundancy::RedundancyDecision;
use crate::framing::OperatingMode;

/// Where the CELT decoder reset (if any) is placed during a
/// transition, per RFC 6716 §4.5.2.
///
/// A transition that triggers a CELT reset may need to apply it
/// *before* the main frame (the rule-2 default), or *before the
/// redundant CELT frame* but not before the following main CELT-only
/// frame (the rule-3 carve-out for SILK/Hybrid → CELT-only with
/// redundancy). When no CELT reset fires at all, this is
/// [`CeltResetPlacement::None`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CeltResetPlacement {
    /// §4.5.2 does not require a CELT decoder reset for this
    /// transition. Either the operating mode did not change, or the
    /// transition is CELT-only → SILK/Hybrid with redundancy where
    /// rule 4 explicitly forbids resetting the CELT decoder for the
    /// redundant frame.
    None,
    /// Rule-2 default: reset CELT immediately before decoding the
    /// new-mode frame. Applies to every mode-changing transition
    /// whose new mode is Hybrid or CELT-only and that does NOT carry
    /// a §4.5.1 redundant CELT frame.
    BeforeFrame,
    /// Rule-3 carve-out: SILK-only/Hybrid → CELT-only with
    /// redundancy. The CELT decoder is reset before decoding the
    /// 5 ms redundant CELT frame embedded in the trailing bytes of
    /// the previous SILK-only/Hybrid frame, and is NOT reset before
    /// the following CELT-only frame.
    BeforeRedundantOnly,
}

/// The outcome of §4.5.2 for one frame transition.
///
/// A SILK reset is a single bit (rule 1 either fires or it does
/// not); the CELT reset carries placement information per
/// [`CeltResetPlacement`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StateReset {
    /// Whether the SILK decoder must be reset before decoding the
    /// current frame, per rule 1.
    pub silk: bool,
    /// Where the CELT decoder reset (if any) is placed, per rules
    /// 2–4.
    pub celt: CeltResetPlacement,
}

impl StateReset {
    /// Convenience: `true` iff [`Self::celt`] is anything other than
    /// [`CeltResetPlacement::None`].
    pub fn celt_resets(&self) -> bool {
        !matches!(self.celt, CeltResetPlacement::None)
    }

    /// Convenience: `true` iff neither sub-decoder needs to be reset
    /// for this transition.
    pub fn is_noop(&self) -> bool {
        !self.silk && !self.celt_resets()
    }
}

/// Whether the upcoming transition carries a §4.5.1 redundant CELT
/// frame, distilled from a [`RedundancyDecision`].
///
/// §4.5.2 only cares about the *presence* of redundancy, not its
/// position (Table 65 / §4.5.1.2) or its size (§4.5.1.3). An
/// `Invalid` decision means the §4.5.1.3 size claim overflowed the
/// remaining bytes; the §4.5.1.3 RECOMMENDATION is to stop decoding
/// the Opus frame entirely, so for the §4.5.2 reset-policy step the
/// transition is treated as having NO usable redundant frame.
fn redundancy_is_present(decision: RedundancyDecision) -> bool {
    decision.is_present()
}

/// Compute the §4.5.2 state-reset policy for a transition from
/// `prev_mode` to `next_mode`, given the §4.5.1 redundancy decision
/// already taken on the *previous* frame (i.e. the SILK-only or
/// Hybrid frame that may have appended a 5 ms redundant CELT frame
/// to its tail) OR on the *current* frame (when the current frame is
/// SILK-only or Hybrid and the previous frame was CELT-only with a
/// redundant trailer — §4.5.1 places that redundancy on the *frame
/// whose tail it lives in*, which is always the SILK-only or Hybrid
/// side of the transition).
///
/// The caller passes [`RedundancyDecision::NotPresent`] when no
/// redundant CELT frame is part of the transition; this is the safe
/// default on the very first frame of a stream and on any transition
/// the §4.5.1 dispatcher routes around.
///
/// ## Mapping of the four §4.5.2 rules
///
/// * **Rule 1** is `next ∈ {SilkOnly, Hybrid} && prev == CeltOnly`.
/// * **Rule 2** is `next ∈ {Hybrid, CeltOnly} && prev != next`
///   unless rule 3 or rule 4 overrides.
/// * **Rule 3** overrides rule 2 when `prev ∈ {SilkOnly, Hybrid}`,
///   `next == CeltOnly`, and redundancy is present: the CELT reset
///   moves from "before the new-mode frame" to "before the redundant
///   CELT frame only" ([`CeltResetPlacement::BeforeRedundantOnly`]).
/// * **Rule 4** overrides rule 2 when `prev == CeltOnly`,
///   `next ∈ {SilkOnly, Hybrid}`, and redundancy is present: the
///   CELT decoder is NOT reset for the redundant frame, and rule 2
///   does not fire anyway because the new mode is SILK-only, leaving
///   [`CeltResetPlacement::None`].
pub fn decide_state_resets(
    prev_mode: OperatingMode,
    next_mode: OperatingMode,
    redundancy: RedundancyDecision,
) -> StateReset {
    let silk = silk_reset_required(prev_mode, next_mode);
    let celt = celt_reset_placement(prev_mode, next_mode, redundancy_is_present(redundancy));
    StateReset { silk, celt }
}

/// Rule 1: SILK is reset before every SILK-only or Hybrid frame
/// whose predecessor was CELT-only.
fn silk_reset_required(prev_mode: OperatingMode, next_mode: OperatingMode) -> bool {
    matches!(next_mode, OperatingMode::SilkOnly | OperatingMode::Hybrid)
        && matches!(prev_mode, OperatingMode::CeltOnly)
}

/// Rules 2–4: CELT placement.
fn celt_reset_placement(
    prev_mode: OperatingMode,
    next_mode: OperatingMode,
    redundancy_present: bool,
) -> CeltResetPlacement {
    // §4.5.2 rule 2 keys off "the operating mode changes AND the new
    // mode is either Hybrid or CELT-only". Same-mode transitions are
    // out of scope for rule 2 entirely; rules 3 and 4 are also
    // mode-changing rules, so any same-mode transition resolves to
    // None regardless of redundancy.
    if prev_mode == next_mode {
        return CeltResetPlacement::None;
    }

    match (prev_mode, next_mode) {
        // Rule 4: CELT-only → SILK-only. Rule 2 does not fire because
        // the new mode is SILK-only (which is not "Hybrid or
        // CELT-only"). Rule 4's "not reset for the redundant frame"
        // clause is the same outcome either way for the CELT side.
        // SILK still resets per rule 1, handled separately.
        (OperatingMode::CeltOnly, OperatingMode::SilkOnly) => CeltResetPlacement::None,

        // Rule 2 vs. rule 4 carve-out for CELT-only → Hybrid:
        //
        // * Without redundancy, rule 2 fires: mode changes AND new
        //   mode is Hybrid → CELT resets before the new-mode frame.
        // * With redundancy, rule 2's "except when the transition
        //   uses redundancy" exception applies, AND rule 4 forbids
        //   resetting CELT for the redundant frame itself. Net
        //   result: CELT is not reset by §4.5.2 policy.
        (OperatingMode::CeltOnly, OperatingMode::Hybrid) => {
            if redundancy_present {
                CeltResetPlacement::None
            } else {
                CeltResetPlacement::BeforeFrame
            }
        }

        // Rule 3 carve-out vs. rule-2 default for SILK-only/Hybrid →
        // CELT-only.
        (OperatingMode::SilkOnly | OperatingMode::Hybrid, OperatingMode::CeltOnly) => {
            if redundancy_present {
                CeltResetPlacement::BeforeRedundantOnly
            } else {
                CeltResetPlacement::BeforeFrame
            }
        }

        // SILK-only ↔ Hybrid. Rule 2: new mode is Hybrid → reset; new
        // mode is SILK-only → no reset (SILK-only is not "Hybrid or
        // CELT-only"). Redundancy plays no role: §4.5.2 only carves
        // out the redundancy exception when the new mode is
        // CELT-only (rule 3) or the previous mode was CELT-only
        // (rule 4).
        (OperatingMode::SilkOnly, OperatingMode::Hybrid) => CeltResetPlacement::BeforeFrame,
        (OperatingMode::Hybrid, OperatingMode::SilkOnly) => CeltResetPlacement::None,

        // Same-mode pairs are filtered above; the match is otherwise
        // exhaustive over the 3×3 product.
        (OperatingMode::SilkOnly, OperatingMode::SilkOnly)
        | (OperatingMode::Hybrid, OperatingMode::Hybrid)
        | (OperatingMode::CeltOnly, OperatingMode::CeltOnly) => CeltResetPlacement::None,
    }
}

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

    fn not_present() -> RedundancyDecision {
        RedundancyDecision::NotPresent
    }

    fn present() -> RedundancyDecision {
        RedundancyDecision::Present {
            position: crate::celt_redundancy::RedundancyPosition::End,
            size_bytes: 2,
        }
    }

    fn invalid() -> RedundancyDecision {
        RedundancyDecision::Invalid
    }

    // ----- StateReset helpers -----

    #[test]
    fn state_reset_celt_resets_is_true_when_celt_resets() {
        let r = StateReset {
            silk: false,
            celt: CeltResetPlacement::BeforeFrame,
        };
        assert!(r.celt_resets());
        let r2 = StateReset {
            silk: true,
            celt: CeltResetPlacement::BeforeRedundantOnly,
        };
        assert!(r2.celt_resets());
    }

    #[test]
    fn state_reset_celt_resets_is_false_when_celt_none() {
        let r = StateReset {
            silk: true,
            celt: CeltResetPlacement::None,
        };
        assert!(!r.celt_resets());
    }

    #[test]
    fn state_reset_is_noop_only_when_both_false() {
        let r = StateReset {
            silk: false,
            celt: CeltResetPlacement::None,
        };
        assert!(r.is_noop());

        assert!(!StateReset {
            silk: true,
            celt: CeltResetPlacement::None
        }
        .is_noop());
        assert!(!StateReset {
            silk: false,
            celt: CeltResetPlacement::BeforeFrame
        }
        .is_noop());
        assert!(!StateReset {
            silk: false,
            celt: CeltResetPlacement::BeforeRedundantOnly
        }
        .is_noop());
    }

    // ----- Rule 1 (SILK) -----

    #[test]
    fn rule1_silk_resets_celt_to_silk() {
        let r = decide_state_resets(
            OperatingMode::CeltOnly,
            OperatingMode::SilkOnly,
            not_present(),
        );
        assert!(r.silk, "SILK must reset on CELT-only → SILK-only");
    }

    #[test]
    fn rule1_silk_resets_celt_to_hybrid() {
        let r = decide_state_resets(
            OperatingMode::CeltOnly,
            OperatingMode::Hybrid,
            not_present(),
        );
        assert!(r.silk, "SILK must reset on CELT-only → Hybrid");
    }

    #[test]
    fn rule1_silk_does_not_reset_silk_to_silk() {
        let r = decide_state_resets(
            OperatingMode::SilkOnly,
            OperatingMode::SilkOnly,
            not_present(),
        );
        assert!(!r.silk);
    }

    #[test]
    fn rule1_silk_does_not_reset_hybrid_to_silk() {
        let r = decide_state_resets(
            OperatingMode::Hybrid,
            OperatingMode::SilkOnly,
            not_present(),
        );
        assert!(!r.silk);
    }

    #[test]
    fn rule1_silk_does_not_reset_when_next_is_celtonly() {
        // The predicate requires next ∈ {SilkOnly, Hybrid}; CELT-only
        // next never fires rule 1.
        for prev in [
            OperatingMode::SilkOnly,
            OperatingMode::Hybrid,
            OperatingMode::CeltOnly,
        ] {
            let r = decide_state_resets(prev, OperatingMode::CeltOnly, not_present());
            assert!(!r.silk, "rule 1 must not fire when next is CELT-only");
        }
    }

    #[test]
    fn rule1_silk_reset_is_independent_of_redundancy_presence() {
        // §4.5.2 rule 1 has no redundancy clause; the bit fires
        // identically regardless of whether the transition carries a
        // redundant CELT frame.
        for red in [not_present(), present(), invalid()] {
            let r = decide_state_resets(OperatingMode::CeltOnly, OperatingMode::SilkOnly, red);
            assert!(r.silk);
        }
    }

    // ----- Rule 2 (CELT default, no redundancy) -----

    #[test]
    fn rule2_celt_resets_silk_to_hybrid() {
        let r = decide_state_resets(
            OperatingMode::SilkOnly,
            OperatingMode::Hybrid,
            not_present(),
        );
        assert_eq!(r.celt, CeltResetPlacement::BeforeFrame);
    }

    #[test]
    fn rule2_celt_resets_silk_to_celtonly_without_redundancy() {
        let r = decide_state_resets(
            OperatingMode::SilkOnly,
            OperatingMode::CeltOnly,
            not_present(),
        );
        assert_eq!(r.celt, CeltResetPlacement::BeforeFrame);
    }

    #[test]
    fn rule2_celt_resets_hybrid_to_celtonly_without_redundancy() {
        let r = decide_state_resets(
            OperatingMode::Hybrid,
            OperatingMode::CeltOnly,
            not_present(),
        );
        assert_eq!(r.celt, CeltResetPlacement::BeforeFrame);
    }

    #[test]
    fn rule2_celt_does_not_reset_hybrid_to_silk() {
        // New mode is SILK-only, which is neither Hybrid nor CELT-only.
        let r = decide_state_resets(
            OperatingMode::Hybrid,
            OperatingMode::SilkOnly,
            not_present(),
        );
        assert_eq!(r.celt, CeltResetPlacement::None);
    }

    #[test]
    fn rule2_celt_does_not_reset_when_mode_unchanged() {
        // "every time the operating mode changes" — same-mode is out
        // of scope.
        for mode in [
            OperatingMode::SilkOnly,
            OperatingMode::Hybrid,
            OperatingMode::CeltOnly,
        ] {
            for red in [not_present(), present(), invalid()] {
                let r = decide_state_resets(mode, mode, red);
                assert_eq!(
                    r.celt,
                    CeltResetPlacement::None,
                    "same-mode {:?} must not reset CELT under any redundancy state",
                    mode
                );
            }
        }
    }

    // ----- Rule 3 (SILK/Hybrid → CELT-only with redundancy) -----

    #[test]
    fn rule3_silk_to_celtonly_with_redundancy_places_reset_before_redundant() {
        let r = decide_state_resets(OperatingMode::SilkOnly, OperatingMode::CeltOnly, present());
        assert_eq!(r.celt, CeltResetPlacement::BeforeRedundantOnly);
    }

    #[test]
    fn rule3_hybrid_to_celtonly_with_redundancy_places_reset_before_redundant() {
        let r = decide_state_resets(OperatingMode::Hybrid, OperatingMode::CeltOnly, present());
        assert_eq!(r.celt, CeltResetPlacement::BeforeRedundantOnly);
    }

    #[test]
    fn rule3_invalid_redundancy_falls_back_to_rule2_default() {
        // §4.5.1.3 RECOMMENDS the decoder stop on an Invalid decision;
        // for the §4.5.2 step we treat it as no usable redundancy, so
        // the rule-2 default placement applies.
        let r = decide_state_resets(OperatingMode::SilkOnly, OperatingMode::CeltOnly, invalid());
        assert_eq!(r.celt, CeltResetPlacement::BeforeFrame);
    }

    // ----- Rule 4 (CELT-only → SILK/Hybrid with redundancy) -----

    #[test]
    fn rule4_celt_to_silk_with_redundancy_does_not_reset_celt() {
        let r = decide_state_resets(OperatingMode::CeltOnly, OperatingMode::SilkOnly, present());
        assert_eq!(r.celt, CeltResetPlacement::None);
        // SILK still resets per rule 1.
        assert!(r.silk);
    }

    #[test]
    fn rule4_celt_to_hybrid_with_redundancy_does_not_reset_celt() {
        let r = decide_state_resets(OperatingMode::CeltOnly, OperatingMode::Hybrid, present());
        assert_eq!(r.celt, CeltResetPlacement::None);
        // SILK still resets per rule 1.
        assert!(r.silk);
    }

    #[test]
    fn rule4_celt_to_silk_without_redundancy_also_does_not_reset_celt() {
        // Rule 2's "new mode is either Hybrid or CELT-only" gate
        // already prevents CELT reset when the new mode is SILK-only,
        // so the result matches with-redundancy and without-redundancy
        // alike for CELT-only → SILK-only.
        let r = decide_state_resets(
            OperatingMode::CeltOnly,
            OperatingMode::SilkOnly,
            not_present(),
        );
        assert_eq!(r.celt, CeltResetPlacement::None);
    }

    #[test]
    fn rule4_celt_to_hybrid_without_redundancy_still_resets_celt() {
        // Without redundancy, rule 2 fires: mode changes AND new mode
        // is Hybrid → CELT resets before the new-mode frame.
        let r = decide_state_resets(
            OperatingMode::CeltOnly,
            OperatingMode::Hybrid,
            not_present(),
        );
        assert_eq!(r.celt, CeltResetPlacement::BeforeFrame);
        assert!(r.silk);
    }

    // ----- Full 3×3 × 2-redundancy cross-product cross-check -----

    #[test]
    fn full_transition_matrix_is_consistent() {
        // The 9 mode pairs × {present, not_present} cover the §4.5.2
        // decision space; this test pins every outcome explicitly so
        // any future code change has to update the table.
        use CeltResetPlacement as P;
        use OperatingMode::{CeltOnly as C, Hybrid as H, SilkOnly as S};

        // (prev, next, redundancy_present) => (silk, celt)
        let cases: &[(OperatingMode, OperatingMode, bool, bool, P)] = &[
            // Same-mode rows: no reset, regardless of redundancy.
            (S, S, false, false, P::None),
            (S, S, true, false, P::None),
            (H, H, false, false, P::None),
            (H, H, true, false, P::None),
            (C, C, false, false, P::None),
            (C, C, true, false, P::None),
            // SILK-only ↔ Hybrid (mode change, never involves
            // CELT-only — rule 3 / 4 do not apply, redundancy
            // irrelevant for CELT placement).
            (S, H, false, false, P::BeforeFrame),
            (S, H, true, false, P::BeforeFrame),
            (H, S, false, false, P::None),
            (H, S, true, false, P::None),
            // SILK-only/Hybrid → CELT-only.
            (S, C, false, false, P::BeforeFrame),
            (S, C, true, false, P::BeforeRedundantOnly),
            (H, C, false, false, P::BeforeFrame),
            (H, C, true, false, P::BeforeRedundantOnly),
            // CELT-only → SILK-only/Hybrid.
            (C, S, false, true, P::None),
            (C, S, true, true, P::None),
            (C, H, false, true, P::BeforeFrame),
            (C, H, true, true, P::None),
        ];

        for (prev, next, red_present, expected_silk, expected_celt) in cases {
            let red = if *red_present {
                present()
            } else {
                not_present()
            };
            let r = decide_state_resets(*prev, *next, red);
            assert_eq!(
                r.silk, *expected_silk,
                "silk mismatch on prev={:?}, next={:?}, red={}",
                prev, next, red_present
            );
            assert_eq!(
                r.celt, *expected_celt,
                "celt mismatch on prev={:?}, next={:?}, red={}",
                prev, next, red_present
            );
        }
    }

    // ----- §4.5.3 figure cross-checks -----
    //
    // The §4.5.3 transition figure (p. 128) is non-normative but
    // marks decoder resets with `;` (SILK reset only) and `|` (SILK
    // + CELT resets). The cases below pin the four §4.5.3 entries
    // that depend on the §4.5.2 carve-outs; matching them
    // independently validates that the rule transcription does not
    // contradict the figure's summary.

    #[test]
    fn figure18_silk_to_celt_with_redundancy_resets_celt_before_redundant() {
        // §4.5.3 row "SILK to CELT with Redundancy": the redundant R
        // sits inside the last SILK frame's tail, then a C → C → C
        // run follows. §4.5.2 rule 3 places CELT reset before the
        // redundant frame and NOT before the following C frame.
        let r = decide_state_resets(OperatingMode::SilkOnly, OperatingMode::CeltOnly, present());
        assert_eq!(r.celt, CeltResetPlacement::BeforeRedundantOnly);
        assert!(!r.silk, "SILK is not reset entering CELT-only");
    }

    #[test]
    fn figure18_celt_to_silk_with_redundancy_marks_silk_reset_only() {
        // §4.5.3 row "CELT to SILK with Redundancy": the `;` marker
        // before the S → S → S run indicates a SILK reset only —
        // rule 1 fires, rule 4 suppresses any CELT reset for the
        // redundant frame.
        let r = decide_state_resets(OperatingMode::CeltOnly, OperatingMode::SilkOnly, present());
        assert!(r.silk);
        assert_eq!(r.celt, CeltResetPlacement::None);
    }

    #[test]
    fn figure18_celt_to_hybrid_with_redundancy_marks_silk_reset_only() {
        // §4.5.3 row "CELT to Hybrid with Redundancy": the `|`
        // marker is documented as "CELT and SILK decoder resets" in
        // the key, but rule 4 explicitly says the CELT decoder is
        // NOT reset *for the redundant frame*; the `|` reflects the
        // CELT-decoder state at the H frame boundary in the original
        // figure where the redundant CELT decode itself implicitly
        // re-initialises CELT memories. From §4.5.2's standpoint
        // (which is what this module encodes), CELT is not reset by
        // policy at the H boundary, only by the redundant frame
        // having been decoded. We pin that distinction here.
        let r = decide_state_resets(OperatingMode::CeltOnly, OperatingMode::Hybrid, present());
        assert!(r.silk);
        assert_eq!(r.celt, CeltResetPlacement::None);
    }

    #[test]
    fn figure18_hybrid_to_wb_silk_resets_silk_only_under_rule_layer() {
        // §4.5.3 row "Hybrid to WB SILK": no redundancy is used; the
        // figure shows `c` (overlap buffer) carried into a `>` join
        // with the SILK frames. From §4.5.2, neither rule 1 (prev
        // was Hybrid, not CELT-only) nor rule 2 (new mode is
        // SILK-only) fires. The reset table is empty.
        let r = decide_state_resets(
            OperatingMode::Hybrid,
            OperatingMode::SilkOnly,
            not_present(),
        );
        assert!(r.is_noop());
    }

    // ----- redundancy_is_present helper -----

    #[test]
    fn redundancy_is_present_treats_invalid_as_absent() {
        assert!(!super::redundancy_is_present(RedundancyDecision::Invalid));
        assert!(!super::redundancy_is_present(
            RedundancyDecision::NotPresent
        ));
        assert!(super::redundancy_is_present(RedundancyDecision::Present {
            position: crate::celt_redundancy::RedundancyPosition::End,
            size_bytes: 2,
        }));
    }
}