opus1 0.3.0

Pure-Rust implementation of the Opus audio codec (RFC 6716), following libopus 1.x
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
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
use crate::mathops::mac;
use crate::modes::CeltMode;
use crate::range_coder::{RANGE_BUF_MAX, RangeCoder};

#[cfg(not(feature = "std"))]
use crate::compat::Math;
use crate::fixedvec::FixedVec;

pub const PRED_COEF: [f32; 4] = [
    29440.0 / 32768.0,
    26112.0 / 32768.0,
    21248.0 / 32768.0,
    16384.0 / 32768.0,
];
pub const BETA_COEF: [f32; 4] = [
    30147.0 / 32768.0,
    22282.0 / 32768.0,
    12124.0 / 32768.0,
    6554.0 / 32768.0,
];
pub const BETA_INTRA: f32 = 4915.0 / 32768.0;

pub const E_PROB_MODEL: [[[u8; 42]; 2]; 4] = [
    [
        [
            72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128, 64, 128, 92, 78, 92, 79,
            92, 78, 90, 79, 116, 41, 115, 40, 114, 40, 132, 26, 132, 26, 145, 17, 161, 12, 176, 10,
            177, 11,
        ],
        [
            24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132, 55, 132, 61, 114, 70,
            96, 74, 88, 75, 88, 87, 74, 89, 66, 91, 67, 100, 59, 108, 50, 120, 40, 122, 37, 97, 43,
            78, 50,
        ],
    ],
    [
        [
            83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74, 93, 74, 109, 40, 114, 36, 117,
            34, 117, 34, 143, 17, 145, 18, 146, 19, 162, 12, 165, 10, 178, 7, 189, 6, 190, 8, 177,
            9,
        ],
        [
            23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91, 73, 91, 78, 89, 86, 80, 92,
            66, 93, 64, 102, 59, 103, 60, 104, 60, 117, 52, 123, 44, 138, 35, 133, 31, 97, 38, 77,
            45,
        ],
    ],
    [
        [
            61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38, 112, 38, 124, 26, 132, 27,
            136, 19, 140, 20, 155, 14, 159, 16, 158, 18, 170, 13, 177, 10, 187, 8, 192, 6, 175, 9,
            159, 10,
        ],
        [
            21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73, 87, 72, 92, 75, 98, 72, 105,
            58, 107, 54, 115, 52, 114, 55, 112, 56, 129, 51, 132, 40, 150, 33, 140, 29, 98, 35, 77,
            42,
        ],
    ],
    [
        [
            42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36, 119, 33, 127, 33, 134,
            34, 139, 21, 147, 23, 152, 20, 158, 25, 154, 26, 166, 21, 173, 16, 184, 13, 184, 10,
            150, 13, 139, 15,
        ],
        [
            22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72, 96, 67, 101, 73, 107, 72,
            113, 55, 118, 52, 125, 52, 118, 52, 117, 55, 135, 49, 137, 39, 157, 32, 145, 29, 97,
            33, 77, 40,
        ],
    ],
];

pub const SMALL_ENERGY_ICDF: [u8; 3] = [2, 1, 0];

fn loss_distortion(
    e_bands: &[f32],
    old_e_bands: &[f32],
    start: usize,
    end: usize,
    len: usize,
    channels: usize,
) -> f32 {
    let mut dist = 0.0f32;
    for c in 0..channels {
        let off = c * len;
        for i in start..end.min(len) {
            let d = e_bands[off + i] - old_e_bands[off + i];
            dist = mac(dist, d, d);
        }
    }
    dist.min(200.0)
}

#[allow(clippy::too_many_arguments)]
fn quant_coarse_energy_impl(
    m: &CeltMode,
    start: usize,
    end: usize,
    e_bands: &[f32],
    old_e_bands: &mut [f32],
    budget: u32,
    tell_start: i32,
    prob_model: &[u8; 42],
    error: &mut [f32],
    enc: &mut RangeCoder,
    channels: usize,
    lm: usize,
    intra: bool,
    max_decay: f32,
    lfe: bool,
) -> i32 {
    let coef = if intra { 0.0 } else { PRED_COEF[lm] };
    let beta = if intra { BETA_INTRA } else { BETA_COEF[lm] };
    let mut prev = [0.0f32; 2];
    let mut badness = 0i32;

    if tell_start + 3 <= budget as i32 {
        enc.encode_bit_logp(intra, 3);
    }

    for i in start..end {
        for c in 0..channels {
            let x = e_bands[c * m.nb_ebands + i];
            let old_e_val = old_e_bands[c * m.nb_ebands + i];
            let old_e = old_e_val.max(-9.0);
            // C `x - MULT16_32_Q15(coef,oldE) - prev[c]`: the product is
            // fused into the first subtraction on 64-bit ARM.
            let f = mac(x, -coef, old_e) - prev[c];

            let mut qi = ((f + 0.5).floor() as i32).clamp(-32767, 32767);

            // Prevent the energy from going down too quickly (for example
            // in bands that have just one bin).
            let decay_bound = old_e_val.max(-28.0) - max_decay;
            if qi < 0 && x < decay_bound {
                qi = qi.saturating_add(((decay_bound - x) as i32).max(0));
                if qi > 0 {
                    qi = 0;
                }
            }
            // `badness` counts how far the coded value is from this one.
            // libopus takes it after the limit above (celt/quant_bands.c,
            // quant_coarse_energy_impl: `qi0 = qi;` follows the decay
            // bound). The limit is the same in the intra and in the inter
            // pass, so it must not count against either of them.
            let qi0 = qi;

            let tell = enc.tell();
            let bits_left = budget as i32 - tell - 3 * channels as i32 * (end - i) as i32;
            if i != start && bits_left < 30 {
                if bits_left < 24 {
                    qi = qi.min(1);
                }
                if bits_left < 16 {
                    qi = qi.max(-1);
                }
            }
            if lfe && i >= 2 {
                qi = qi.min(0);
            }

            if tell + 15 <= budget as i32 {
                let prob_idx = 2 * i.min(20);
                let fs = (prob_model[prob_idx] as u32) << 7;
                let decay = (prob_model[prob_idx + 1] as i32) << 6;
                enc.laplace_encode(&mut qi, fs, decay);
            } else if tell + 2 <= budget as i32 {
                qi = qi.clamp(-1, 1);
                enc.encode_icdf(
                    (2 * qi) ^ (if qi < 0 { -1 } else { 0 }),
                    &SMALL_ENERGY_ICDF,
                    2,
                );
            } else if tell < budget as i32 {
                qi = qi.min(0);
                enc.encode_bit_logp(qi != 0, 1);
            } else {
                qi = -1;
            }

            badness = badness.saturating_add(qi0.saturating_sub(qi).saturating_abs());

            let q = qi as f32;
            error[c * m.nb_ebands + i] = f - q;
            // C `MULT16_32_Q15(coef,oldE) + prev[c] + q` and
            // `prev[c] + q - MULT16_32_Q15(beta,q)`: each product is fused
            // into the addition after it on 64-bit ARM.
            let tmp = mac(prev[c], coef, old_e) + q;
            old_e_bands[c * m.nb_ebands + i] = tmp;
            prev[c] = mac(prev[c] + q, -beta, q);
        }
    }

    if lfe { 0 } else { badness }
}

#[allow(clippy::too_many_arguments)]
pub fn quant_coarse_energy_advanced(
    m: &CeltMode,
    start: usize,
    end: usize,
    eff_end: usize,
    e_bands: &[f32],
    old_e_bands: &mut [f32],
    budget: u32,
    error: &mut [f32],
    enc: &mut RangeCoder,
    channels: usize,
    lm: usize,
    nb_available_bytes: usize,
    force_intra: bool,
    delayed_intra: &mut f32,
    mut two_pass: bool,
    loss_rate: i32,
    lfe: bool,
) {
    let mut intra = force_intra
        || (!two_pass
            && *delayed_intra > 2.0 * channels as f32 * (end.saturating_sub(start)) as f32
            && nb_available_bytes > (end.saturating_sub(start)) * channels);

    let intra_bias = ((budget as f32) * (*delayed_intra) * (loss_rate as f32)
        / ((channels as f32) * 512.0)) as i32;
    let new_distortion =
        loss_distortion(e_bands, old_e_bands, start, eff_end, m.nb_ebands, channels);

    let tell = enc.tell();
    if tell + 3 > budget as i32 {
        two_pass = false;
        intra = false;
    }

    let mut max_decay = if end - start > 10 {
        16.0f32.min(0.125 * nb_available_bytes as f32)
    } else {
        16.0f32
    };
    if lfe {
        max_decay = 3.0;
    }

    if intra || !two_pass {
        // Only one pass runs, so it codes straight into the caller's arrays
        // and into `enc`. Nothing has to be saved.
        quant_coarse_energy_impl(
            m,
            start,
            end,
            e_bands,
            old_e_bands,
            budget,
            tell,
            &E_PROB_MODEL[lm][intra as usize],
            error,
            enc,
            channels,
            lm,
            intra,
            max_decay,
            lfe,
        );
    } else {
        // Two passes: the intra pass first, then the inter pass from the same
        // coder state. The better one stays (C: quant_bands.c:295-341).
        //
        // The coder is not cloned for this, because a clone copies the byte
        // buffer too. Both passes only add range coder bytes from
        // `start_offs` on. So it is enough to save the 44 bytes of coder
        // words and, for the intra pass, the bytes it wrote.
        //
        // Compared with full copies, the coder ends with the same words and
        // the same bytes below `offs`. Above `offs` the buffer may still hold
        // bytes of the pass that lost. Later symbols overwrite them and
        // `done()` clears the rest. `done()` skips that only when the coder
        // has an error, and then the packet is not valid anyway.
        let start_state = enc.save_state();
        let start_offs = enc.offs as usize;
        let mut old_e_bands_intra: FixedVec<f32, 42> = FixedVec::from_slice(old_e_bands);
        let mut error_intra: FixedVec<f32, 42> = FixedVec::from_slice(error);

        let badness1 = quant_coarse_energy_impl(
            m,
            start,
            end,
            e_bands,
            &mut old_e_bands_intra,
            budget,
            tell,
            &E_PROB_MODEL[lm][1],
            &mut error_intra,
            enc,
            channels,
            lm,
            true,
            max_decay,
            lfe,
        );
        let tell_intra = crate::range_coder::tell_frac_inline!(enc);

        let intra_state = enc.save_state();
        let intra_offs = enc.offs as usize;
        let mut intra_bytes: FixedVec<u8, RANGE_BUF_MAX> = FixedVec::new();
        intra_bytes.extend_from_slice(&enc.buf[start_offs..intra_offs]);

        enc.restore_state(&start_state);
        let badness2 = quant_coarse_energy_impl(
            m,
            start,
            end,
            e_bands,
            old_e_bands,
            budget,
            tell,
            &E_PROB_MODEL[lm][0],
            error,
            enc,
            channels,
            lm,
            false,
            max_decay,
            lfe,
        );

        if badness1 < badness2
            || (badness1 == badness2 && crate::range_coder::tell_frac_inline!(enc) + intra_bias > tell_intra)
        {
            enc.restore_state(&intra_state);
            enc.buf[start_offs..intra_offs].copy_from_slice(&intra_bytes);
            old_e_bands.copy_from_slice(&old_e_bands_intra);
            error.copy_from_slice(&error_intra);
            intra = true;
        }
    }

    if intra {
        *delayed_intra = new_distortion;
    } else {
        let pred2 = PRED_COEF[lm] * PRED_COEF[lm];
        *delayed_intra = mac(new_distortion, pred2, *delayed_intra);
    }
}

#[allow(clippy::too_many_arguments)]
pub fn quant_coarse_energy(
    m: &CeltMode,
    start: usize,
    end: usize,
    e_bands: &[f32],
    old_e_bands: &mut [f32],
    budget: u32,
    error: &mut [f32],
    enc: &mut RangeCoder,
    channels: usize,
    lm: usize,
    force_intra: bool,
    nb_available_bytes: usize,
) {
    let mut delayed_intra = 0.0f32;
    quant_coarse_energy_advanced(
        m,
        start,
        end,
        end,
        e_bands,
        old_e_bands,
        budget,
        error,
        enc,
        channels,
        lm,
        nb_available_bytes,
        force_intra,
        &mut delayed_intra,
        false,
        0,
        false,
    );
}

#[allow(clippy::too_many_arguments)]
pub fn unquant_coarse_energy(
    m: &CeltMode,
    start: usize,
    end: usize,
    old_e_bands: &mut [f32],
    intra: bool,
    dec: &mut RangeCoder,
    channels: usize,
    lm: usize,
) {
    let prob_model = &E_PROB_MODEL[lm][if intra { 1 } else { 0 }];
    let coef = if intra { 0.0 } else { PRED_COEF[lm] };
    let beta = if intra { BETA_INTRA } else { BETA_COEF[lm] };
    debug_assert!(channels <= 2);
    let mut prev = [0.0f32; 2];
    let budget = (dec.storage * 8) as i32;

    for i in start..end {
        for c in 0..channels {
            let tell = dec.tell();
            let qi = if budget - tell >= 15 {
                let prob_idx = 2 * i.min(20);
                let fs = (prob_model[prob_idx] as u32) << 7;
                let decay = (prob_model[prob_idx + 1] as i32) << 6;
                dec.laplace_decode(fs, decay)
            } else if budget - tell >= 2 {
                let s = dec.decode_icdf(&SMALL_ENERGY_ICDF, 2);
                (s >> 1) ^ -(s & 1)
            } else if budget - tell >= 1 {
                if dec.decode_bit_logp(1) { -1 } else { 0 }
            } else {
                -1
            };

            // Clamp in-place, matching C: oldEBands[i] = MAXG(-GCONST(9.f), oldEBands[i])
            old_e_bands[c * m.nb_ebands + i] = old_e_bands[c * m.nb_ebands + i].max(-9.0);
            let old_e = old_e_bands[c * m.nb_ebands + i];

            let q = qi as f32;
            // C `MULT16_32_Q15(coef,oldE) + prev[c] + q` and
            // `prev[c] + q - MULT16_32_Q15(beta,q)`: each product is fused
            // into the addition after it on 64-bit ARM.
            let tmp = mac(prev[c], coef, old_e) + q;
            old_e_bands[c * m.nb_ebands + i] = tmp;
            prev[c] = mac(prev[c] + q, -beta, q);
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub fn quant_fine_energy(
    m: &CeltMode,
    start: usize,
    end: usize,
    old_e_bands: &mut [f32],
    error: &mut [f32],
    fine_quant: &[i32],
    enc: &mut RangeCoder,
    channels: usize,
) {
    for i in start..end {
        for c in 0..channels {
            let bits = fine_quant[i];
            if bits <= 0 {
                continue;
            }
            let mut q = ((error[c * m.nb_ebands + i] + 0.5) * (1 << bits) as f32).floor() as i32;
            q = q.max(0).min((1 << bits) - 1);
            enc.enc_bits(q as u32, bits as u32);
            // C (quant_bands.c): offset = (q+.5)*(1<<(14-bits))*(1/16384) - .5;
            // (multiply-then-scale, not /2^bits; matters for byte-exactness)
            let offset = (q as f32 + 0.5) * (1i32 << (14 - bits)) as f32 * (1.0 / 16384.0) - 0.5;
            old_e_bands[c * m.nb_ebands + i] += offset;
            error[c * m.nb_ebands + i] -= offset;
        }
    }
}

pub fn unquant_fine_energy(
    m: &CeltMode,
    start: usize,
    end: usize,
    old_e_bands: &mut [f32],
    fine_quant: &[i32],
    dec: &mut RangeCoder,
    channels: usize,
) {
    for i in start..end {
        for c in 0..channels {
            let bits = fine_quant[i];
            if bits <= 0 {
                continue;
            }
            let q = dec.dec_bits(bits as u32);
            // C (quant_bands.c): offset = (q+.5)*(1<<(14-bits))*(1/16384) - .5;
            // (multiply-then-scale, not /2^bits; matters for byte-exactness)
            let offset = (q as f32 + 0.5) * (1i32 << (14 - bits)) as f32 * (1.0 / 16384.0) - 0.5;
            old_e_bands[c * m.nb_ebands + i] += offset;
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub fn quant_energy_finalise(
    m: &CeltMode,
    start: usize,
    end: usize,
    old_e_bands: &mut [f32],
    error: &mut [f32],
    fine_quant: &[i32],
    fine_priority: &[i32],
    bits_left: i32,
    enc: &mut RangeCoder,
    channels: usize,
) {
    let mut bits_left = bits_left;
    for priority in 0..2 {
        let mut i = start;
        while i < end && bits_left >= channels as i32 {
            if fine_quant[i] >= 8 || fine_priority[i] != priority {
                i += 1;
                continue;
            }
            let mut c = 0;
            while c < channels {
                let q2 = if error[i + c * m.nb_ebands] < 0.0 {
                    0
                } else {
                    1
                };
                enc.enc_bits(q2 as u32, 1);
                let offset =
                    (q2 as f32 - 0.5) * (1i32 << (14 - fine_quant[i] - 1)) as f32 * (1.0 / 16384.0);
                old_e_bands[i + c * m.nb_ebands] += offset;
                error[i + c * m.nb_ebands] -= offset;
                bits_left -= 1;
                c += 1;
            }
            i += 1;
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub fn unquant_energy_finalise(
    m: &CeltMode,
    start: usize,
    end: usize,
    old_e_bands: &mut [f32],
    fine_quant: &[i32],
    fine_priority: &[i32],
    bits_left: i32,
    dec: &mut RangeCoder,
    channels: usize,
) {
    let mut bits_left = bits_left;
    for priority in 0..2 {
        let mut i = start;
        while i < end && bits_left >= channels as i32 {
            if fine_quant[i] >= 8 || fine_priority[i] != priority {
                i += 1;
                continue;
            }
            let mut c = 0;
            while c < channels {
                let q2 = dec.dec_bits(1);
                let offset =
                    (q2 as f32 - 0.5) * (1i32 << (14 - fine_quant[i] - 1)) as f32 * (1.0 / 16384.0);
                old_e_bands[i + c * m.nb_ebands] += offset;
                bits_left -= 1;
                c += 1;
            }
            i += 1;
        }
    }
}

#[cfg(all(test, feature = "std"))]
mod tests {
    use super::*;
    use crate::range_coder::RangeCoder;

    #[test]
    fn test_coarse_fine_energy() {
        let mode = crate::modes::default_mode();
        let mut e_bands = vec![0.0; mode.nb_ebands];
        for (i, v) in e_bands.iter_mut().enumerate() {
            *v = 5.0 + (i as f32 * 0.5).sin() * 2.0;
        }

        let mut old_e_bands = vec![0.0; mode.nb_ebands];
        let mut error = vec![0.0; mode.nb_ebands];
        let mut enc = RangeCoder::new_encoder(1000);

        quant_coarse_energy(
            mode,
            0,
            mode.nb_ebands,
            &e_bands,
            &mut old_e_bands,
            10000,
            &mut error,
            &mut enc,
            1,
            3,
            false,
            80,
        );

        let mut fine_quant = vec![0; mode.nb_ebands];
        for (i, v) in fine_quant.iter_mut().enumerate() {
            *v = (i % 3) as i32;
        }

        quant_fine_energy(
            mode,
            0,
            mode.nb_ebands,
            &mut old_e_bands,
            &mut error,
            &fine_quant,
            &mut enc,
            1,
        );

        let mut fine_priority = vec![0i32; mode.nb_ebands];
        for (i, v) in fine_priority.iter_mut().enumerate() {
            *v = (i % 2) as i32;
        }

        quant_energy_finalise(
            mode,
            0,
            mode.nb_ebands,
            &mut old_e_bands,
            &mut error,
            &fine_quant,
            &fine_priority,
            10,
            &mut enc,
            1,
        );

        enc.done();
        let _compressed = &enc.buf;

        let mut dec = RangeCoder::new_decoder(&enc.buf);

        let mut decoded_old_e_bands = vec![0.0; mode.nb_ebands];
        let intra = dec.decode_bit_logp(3);
        unquant_coarse_energy(
            mode,
            0,
            mode.nb_ebands,
            &mut decoded_old_e_bands,
            intra,
            &mut dec,
            1,
            3,
        );

        unquant_fine_energy(
            mode,
            0,
            mode.nb_ebands,
            &mut decoded_old_e_bands,
            &fine_quant,
            &mut dec,
            1,
        );

        unquant_energy_finalise(
            mode,
            0,
            mode.nb_ebands,
            &mut decoded_old_e_bands,
            &fine_quant,
            &fine_priority,
            10,
            &mut dec,
            1,
        );

        for i in 0..mode.nb_ebands {
            if (decoded_old_e_bands[i] - old_e_bands[i]).abs() >= 1e-5 {
                println!(
                    "Mismatch at band {}: enc={} dec={} diff={}",
                    i,
                    old_e_bands[i],
                    decoded_old_e_bands[i],
                    (decoded_old_e_bands[i] - old_e_bands[i]).abs()
                );
            }
            assert!((decoded_old_e_bands[i] - old_e_bands[i]).abs() < 1e-5);
        }
    }

    /// `quant_coarse_energy_advanced` as it was before it stopped cloning the
    /// coder: every pass works on a full copy of the coder and of the arrays.
    /// The reference for `coarse_energy_matches_full_copy_reference`.
    #[allow(clippy::too_many_arguments)]
    fn reference_quant_coarse_energy_advanced(
        m: &CeltMode,
        start: usize,
        end: usize,
        eff_end: usize,
        e_bands: &[f32],
        old_e_bands: &mut [f32],
        budget: u32,
        error: &mut [f32],
        enc: &mut RangeCoder,
        channels: usize,
        lm: usize,
        nb_available_bytes: usize,
        force_intra: bool,
        delayed_intra: &mut f32,
        mut two_pass: bool,
        loss_rate: i32,
        lfe: bool,
    ) {
        let mut intra = force_intra
            || (!two_pass
                && *delayed_intra > 2.0 * channels as f32 * (end.saturating_sub(start)) as f32
                && nb_available_bytes > (end.saturating_sub(start)) * channels);
        let intra_bias = ((budget as f32) * (*delayed_intra) * (loss_rate as f32)
            / ((channels as f32) * 512.0)) as i32;
        let new_distortion =
            loss_distortion(e_bands, old_e_bands, start, eff_end, m.nb_ebands, channels);
        let tell = enc.tell();
        if tell + 3 > budget as i32 {
            two_pass = false;
            intra = false;
        }
        let mut max_decay = if end - start > 10 {
            16.0f32.min(0.125 * nb_available_bytes as f32)
        } else {
            16.0f32
        };
        if lfe {
            max_decay = 3.0;
        }

        let enc_start_state = enc.clone();
        let mut old_e_bands_intra = old_e_bands.to_vec();
        let mut error_intra = error.to_vec();
        let mut badness1 = 0i32;
        let mut tell_intra = 0i32;
        if two_pass || intra {
            badness1 = quant_coarse_energy_impl(
                m,
                start,
                end,
                e_bands,
                &mut old_e_bands_intra,
                budget,
                tell,
                &E_PROB_MODEL[lm][1],
                &mut error_intra,
                enc,
                channels,
                lm,
                true,
                max_decay,
                lfe,
            );
            tell_intra = enc.tell_frac();
        }
        if !intra {
            let enc_intra_state = enc.clone();
            *enc = enc_start_state.clone();
            let badness2 = quant_coarse_energy_impl(
                m,
                start,
                end,
                e_bands,
                old_e_bands,
                budget,
                tell,
                &E_PROB_MODEL[lm][0],
                error,
                enc,
                channels,
                lm,
                false,
                max_decay,
                lfe,
            );
            if two_pass
                && (badness1 < badness2
                    || (badness1 == badness2 && enc.tell_frac() + intra_bias > tell_intra))
            {
                *enc = enc_intra_state;
                old_e_bands.copy_from_slice(&old_e_bands_intra);
                error.copy_from_slice(&error_intra);
                intra = true;
            }
        } else {
            old_e_bands.copy_from_slice(&old_e_bands_intra);
            error.copy_from_slice(&error_intra);
        }
        if intra {
            *delayed_intra = new_distortion;
        } else {
            let pred2 = PRED_COEF[lm] * PRED_COEF[lm];
            *delayed_intra = mac(new_distortion, pred2, *delayed_intra);
        }
    }

    /// Small deterministic generator (xorshift64*).
    struct TestRng(u64);

    impl TestRng {
        fn next(&mut self) -> u32 {
            self.0 ^= self.0 >> 12;
            self.0 ^= self.0 << 25;
            self.0 ^= self.0 >> 27;
            (self.0.wrapping_mul(0x2545_F491_4F6C_DD1D) >> 32) as u32
        }
        /// Uniform in [-1, 1).
        fn unit(&mut self) -> f32 {
            (self.next() >> 8) as f32 / (1u32 << 23) as f32 - 1.0
        }
    }

    /// The words of a coder, for comparing two coders.
    fn coder_words(rc: &RangeCoder) -> [i64; 11] {
        [
            rc.storage as i64,
            rc.end_offs as i64,
            rc.end_window as i64,
            rc.nend_bits as i64,
            rc.nbits_total as i64,
            rc.offs as i64,
            rc.rng as i64,
            rc.val as i64,
            rc.ext as i64,
            rc.rem as i64,
            rc.error as i64,
        ]
    }

    /// The coarse energy coder saves the coder words and the bytes of the
    /// intra pass instead of cloning the coder. It must leave the same coder
    /// words, the same range coder bytes, the same arrays and the same packet
    /// as the version that clones.
    ///
    /// The cases cover mono and stereo, all four frame sizes, one and two
    /// passes, forced intra, the low-frequency channel, a band range that
    /// starts at band 17, budgets from "no room for the intra flag" to
    /// "everything fits", and band energies from steady to jumping by 20
    /// steps. The coder already holds symbols when the coarse energy starts;
    /// in variant 2 it also holds raw bits and a run of 0xFF bytes whose
    /// carry is still open. The buffer starts with bytes of an earlier frame.
    /// Six frames run in a row, so the prediction state and `delayed_intra`
    /// of one frame feed the next.
    #[test]
    fn coarse_energy_matches_full_copy_reference() {
        let mode = crate::modes::default_mode();
        let nb = mode.nb_ebands;
        let mut rng = TestRng(0x1234_5678_9ABC_DEF1);
        // How often each pass won where both ran, read back from the packet.
        let mut intra_wins = 0;
        let mut inter_wins = 0;

        for channels in 1..=2usize {
            for lm in 0..4usize {
                for &two_pass in &[true, false] {
                    for &(storage, budget_bits) in &[
                        (1275u32, 1275 * 8u32),
                        (160, 160 * 8),
                        (40, 40 * 8),
                        (12, 12 * 8),
                        (6, 40),
                        (3, 24),
                        (2, 10),
                        (160, 2),
                    ] {
                        for variant in 0..6u32 {
                            let force_intra = variant == 4;
                            let lfe = variant == 5;
                            let (start, end) = if variant == 3 { (17, nb) } else { (0, nb) };
                            let open_carry = variant == 2 && storage >= 40;
                            let n = channels * nb;

                            let mut enc_new = RangeCoder::new_encoder(storage);
                            enc_new.buf.fill(0x5A);
                            let mut old_new = [0f32; 42];
                            let mut old_ref = [0f32; 42];
                            let mut err_new = [0f32; 42];
                            let mut err_ref = [0f32; 42];
                            let mut delayed_new = 0f32;
                            let mut delayed_ref = 0f32;
                            let mut level = [0f32; 42];
                            for (i, l) in level.iter_mut().enumerate() {
                                *l = 8.0 - 0.3 * (i % nb) as f32 + 2.0 * rng.unit();
                            }

                            for frame in 0..6 {
                                let step = if frame == 3 { 20.0 } else { 0.7 };
                                let mut e_bands = [0f32; 42];
                                for i in 0..n {
                                    level[i] = (level[i] + step * rng.unit()).clamp(-28.0, 30.0);
                                    e_bands[i] = level[i];
                                }
                                let loss_rate = if frame == 4 { 20 } else { 0 };

                                // What the frame codes before the coarse energy.
                                enc_new.reset_for_encode(storage);
                                enc_new.encode_bit_logp(false, 15);
                                enc_new.encode_bit_logp(frame & 1 == 1, 1);
                                if open_carry {
                                    for _ in 0..12 {
                                        enc_new.encode(255, 256, 256);
                                    }
                                    enc_new.enc_bits(0x2B, 6);
                                    assert!(enc_new.ext > 0, "no open carry");
                                }
                                let mut enc_ref = enc_new.clone();
                                let flag_coded = enc_ref.tell() + 3 <= budget_bits as i32;

                                quant_coarse_energy_advanced(
                                    mode,
                                    start,
                                    end,
                                    end,
                                    &e_bands[..n],
                                    &mut old_new[..n],
                                    budget_bits,
                                    &mut err_new[..n],
                                    &mut enc_new,
                                    channels,
                                    lm,
                                    storage as usize,
                                    force_intra,
                                    &mut delayed_new,
                                    two_pass,
                                    loss_rate,
                                    lfe,
                                );
                                reference_quant_coarse_energy_advanced(
                                    mode,
                                    start,
                                    end,
                                    end,
                                    &e_bands[..n],
                                    &mut old_ref[..n],
                                    budget_bits,
                                    &mut err_ref[..n],
                                    &mut enc_ref,
                                    channels,
                                    lm,
                                    storage as usize,
                                    force_intra,
                                    &mut delayed_ref,
                                    two_pass,
                                    loss_rate,
                                    lfe,
                                );

                                let what = format!(
                                    "channels {channels} lm {lm} two_pass {two_pass} storage \
                                     {storage} budget {budget_bits} variant {variant} frame {frame}"
                                );
                                assert_eq!(coder_words(&enc_new), coder_words(&enc_ref), "{what}");
                                let offs = enc_ref.offs as usize;
                                assert_eq!(enc_new.buf[..offs], enc_ref.buf[..offs], "{what}");
                                let raw = (enc_ref.storage - enc_ref.end_offs) as usize;
                                assert_eq!(enc_new.buf[raw..], enc_ref.buf[raw..], "{what}");
                                assert_eq!(
                                    old_new.map(f32::to_bits),
                                    old_ref.map(f32::to_bits),
                                    "{what}"
                                );
                                assert_eq!(
                                    err_new.map(f32::to_bits),
                                    err_ref.map(f32::to_bits),
                                    "{what}"
                                );
                                assert_eq!(delayed_new.to_bits(), delayed_ref.to_bits(), "{what}");

                                // The finished packets are the same as well.
                                // `enc_new` itself is finished, so that the
                                // next frame starts on the bytes of this one.
                                enc_new.done();
                                enc_ref.done();
                                assert_eq!(enc_new.error, enc_ref.error, "{what}");
                                if enc_ref.error != 0 {
                                    continue;
                                }
                                let s = enc_ref.storage as usize;
                                assert_eq!(enc_new.buf[..s], enc_ref.buf[..s], "{what}");

                                // Read the intra flag back from the packet.
                                if two_pass && !force_intra && flag_coded {
                                    let mut dec = RangeCoder::new_decoder(&enc_new.buf[..s]);
                                    assert!(!dec.decode_bit_logp(15), "{what}");
                                    assert_eq!(dec.decode_bit_logp(1), frame & 1 == 1, "{what}");
                                    if open_carry {
                                        for _ in 0..12 {
                                            assert_eq!(dec.decode(256), 255, "{what}");
                                            dec.update(255, 256, 256);
                                        }
                                        assert_eq!(dec.dec_bits(6), 0x2B, "{what}");
                                    }
                                    if dec.decode_bit_logp(3) {
                                        intra_wins += 1;
                                    } else {
                                        inter_wins += 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        // Both outcomes of the two-pass decision must have been exercised,
        // because they restore the coder in different ways.
        assert!(intra_wins > 100, "intra chosen in only {intra_wins} cases");
        assert!(inter_wins > 100, "inter chosen in only {inter_wins} cases");
    }

    /// Regression test: extreme/corrupt energy values must not cause an
    /// "attempt to add with overflow" panic in `quant_coarse_energy_impl`.
    /// Previously `(qi0 - qi).abs()` overflowed i32 when the float->int cast
    /// of `qi` saturated near i32::MAX/MIN.
    #[test]
    fn test_coarse_energy_extreme_no_overflow() {
        let mode = crate::modes::default_mode();
        let n = mode.nb_ebands;

        for &extreme in &[f32::INFINITY, f32::NEG_INFINITY, f32::NAN, 1.0e30, -1.0e30] {
            let e_bands = vec![extreme; n];
            let mut old_e_bands = vec![0.0; n];
            let mut error = vec![0.0; n];
            let mut enc = RangeCoder::new_encoder(1000);

            // tiny budget forces the `else { qi = -1 }` path, which combined
            // with a saturated qi0 triggered the overflow at the badness line.
            quant_coarse_energy(
                mode,
                0,
                n,
                &e_bands,
                &mut old_e_bands,
                0,
                &mut error,
                &mut enc,
                1,
                3,
                false,
                80,
            );

            // large budget exercises the laplace-encode path with extreme qi.
            let mut old_e_bands2 = vec![0.0; n];
            let mut error2 = vec![0.0; n];
            let mut enc2 = RangeCoder::new_encoder(1000);
            quant_coarse_energy(
                mode,
                0,
                n,
                &e_bands,
                &mut old_e_bands2,
                10000,
                &mut error2,
                &mut enc2,
                1,
                3,
                false,
                80,
            );
        }
    }
}