rusty_aac 1.0.0

Pure-Rust AAC decoder (LC, Main, LTP, HE-AAC v1/v2 SBR+PS, ER LD/ELD; sample-exact vs FFmpeg on the ISO conformance suite) and AAC-LC encoder (~5x faster than FFmpeg's native encoder, PEAQ-measured). No C/FFI. ADTS/MP4/LATM-LOAS, 7.1 + PCE layouts.
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
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
//! AAC numerical core: inverse quantization and the IMDCT synthesis filterbank
//! (windows + overlap-add). These must match ISO 14496-3 exactly — they are the
//! parts where any deviation produces audibly wrong / incompatible output, so
//! they're written straight from the spec and checked by the MDCT↔IMDCT
//! perfect-reconstruction (TDAC) property.
//!
//! The direct O(N²) transforms are kept as oracles; the decoder runs the fast
//! paths (an f32 radix-2 DCT-IV for power-of-two lengths, the mixed-radix plan
//! for 960/480/120).

// These primitives are exercised by the tests now and wired into the decode
// path in the spectral/synthesis stages; allow until then.
#![allow(dead_code)]

use std::f64::consts::PI;
use std::sync::OnceLock;

/// Inverse quantization of a spectral coefficient (ISO 14496-3 §10.3):
/// `x = sign(q) · |q|^(4/3)`. The scalefactor gain is applied separately.
pub fn dequant(q: i32) -> f32 {
    // `|q|^(4/3)` for every magnitude a legal stream can produce (escape codes
    // reach 8191, pulses add up to 15), precomputed with the same f64 `powf` and
    // stored as f32. `(±m) as f32 == ±(m as f32)`, so the lookup is bit-identical
    // to evaluating the formula — which was a libm call per coefficient and ~40%
    // of LC decode. Larger (corrupt-stream) magnitudes take the formula.
    dequant_with(pow43_table(), q)
}

/// The `|q|^(4/3)` table, for callers that hoist it out of a coefficient loop.
pub fn pow43_table() -> &'static [f32] {
    static POW43: OnceLock<Vec<f32>> = OnceLock::new();
    POW43.get_or_init(|| (0..POW43_LEN).map(|i| dequant_formula(i as i32)).collect())
}

/// [`dequant`] against an already-fetched [`pow43_table`].
#[inline(always)]
pub fn dequant_with(t: &[f32], q: i32) -> f32 {
    match t.get(q.unsigned_abs() as usize) {
        Some(&m) => {
            if q < 0 {
                -m
            } else {
                m
            }
        }
        None => dequant_formula(q),
    }
}

/// Magnitudes covered by the `dequant` table.
const POW43_LEN: usize = 8192 + 16;

/// The defining formula (the table's source and oracle).
fn dequant_formula(q: i32) -> f32 {
    let m = (q.unsigned_abs() as f64).powf(4.0 / 3.0);
    (q.signum() as f64 * m) as f32
}

/// Scalefactor gain `2^(0.25·(sf − 100))`, the per-band multiplier applied to
/// dequantized coefficients.
pub fn sf_gain(sf: i32) -> f32 {
    2f64.powf(0.25 * (sf as f64 - 100.0)) as f32
}

/// IMDCT (ISO 14496-3 §4.6.11.2): `N/2` spectral coefficients → `N` time
/// samples. `out[i] = (2/N)·Σ_k spec[k]·cos((2π/N)(i+n0)(k+½))`, `n0=(N/2+1)/2`.
pub fn imdct(spec: &[f32]) -> Vec<f32> {
    let half = spec.len();
    let n = half * 2;
    if n == 0 {
        return Vec::new();
    }
    let n0 = (n / 2 + 1) as f64 / 2.0; // N/4 + 1/2
    let scale = 2.0 / n as f64;
    let w = 2.0 * PI / n as f64;
    let mut out = vec![0f32; n];
    for (i, o) in out.iter_mut().enumerate() {
        let mut acc = 0f64;
        let a = w * (i as f64 + n0);
        for (k, &s) in spec.iter().enumerate() {
            acc += s as f64 * (a * (k as f64 + 0.5)).cos();
        }
        *o = (scale * acc) as f32;
    }
    out
}

/// Forward MDCT — the analysis transform paired with [`imdct`]. Used to validate
/// the IMDCT via perfect reconstruction (and useful for an eventual encoder).
/// `N` time samples → `N/2` coefficients.
///
/// The `2.0` factor makes this the exact inverse of the spec's `2/N` IMDCT under
/// Princen-Bradley windows, so `imdct(mdct(·))` with overlap-add is identity.
pub fn mdct(time: &[f32]) -> Vec<f32> {
    let n = time.len();
    let half = n / 2;
    if n == 0 {
        return Vec::new();
    }
    let n0 = (n / 2 + 1) as f64 / 2.0;
    let w = 2.0 * PI / n as f64;
    let mut out = vec![0f32; half];
    for (k, o) in out.iter_mut().enumerate() {
        let mut acc = 0f64;
        for (i, &t) in time.iter().enumerate() {
            acc += t as f64 * (w * (i as f64 + n0) * (k as f64 + 0.5)).cos();
        }
        *o = (2.0 * acc) as f32;
    }
    out
}

/// In-place radix-2 Cooley–Tukey FFT (forward, `exp(-i2πkn/N)`); `re.len()` must be
/// a power of two and equal `im.len()`. Pure in-house `f64` — the fast MDCT's engine.
/// Uses precomputed per-stage twiddles (`tw_c`/`tw_s`, flattened over stages) so a
/// transform is pure multiply-add — no runtime trig or twiddle recurrence.
fn fft(re: &mut [f64], im: &mut [f64], tw_c: &[f64], tw_s: &[f64]) {
    let n = re.len();
    // Bit-reversal permutation.
    let mut j = 0usize;
    for i in 1..n {
        let mut bit = n >> 1;
        while j & bit != 0 {
            j ^= bit;
            bit >>= 1;
        }
        j |= bit;
        if i < j {
            re.swap(i, j);
            im.swap(i, j);
        }
    }
    // Butterfly stages, indexing the flattened per-stage twiddle tables.
    let mut off = 0usize;
    let mut len = 2usize;
    while len <= n {
        let half = len / 2;
        let mut base = 0;
        while base < n {
            for k in 0..half {
                let (a, b) = (base + k, base + k + half);
                let (cr, ci) = (tw_c[off + k], tw_s[off + k]);
                let tr = cr * re[b] - ci * im[b];
                let ti = cr * im[b] + ci * re[b];
                re[b] = re[a] - tr;
                im[b] = im[a] - ti;
                re[a] += tr;
                im[a] += ti;
            }
            base += len;
        }
        off += half;
        len <<= 1;
    }
}

/// Precomputed MDCT twiddles for one block size: pre-rotation, post-rotation, and
/// the FFT's per-stage factors — so a transform touches no `cos`/`sin` at runtime.
struct MdctTwiddles {
    pre_c: Vec<f64>,
    pre_s: Vec<f64>,
    post_c: Vec<f64>,
    post_s: Vec<f64>,
    fft_c: Vec<f64>,
    fft_s: Vec<f64>,
}

impl MdctTwiddles {
    fn build(n: usize) -> MdctTwiddles {
        // N/4-FFT MDCT: fold N→L=N/2, an M=N/4 complex FFT, pre/post rotations
        // (derived + verified against the direct oracle — see mdct_fast).
        let l = n / 2;
        let m = n / 4;
        let (mut pre_c, mut pre_s) = (vec![0f64; m], vec![0f64; m]);
        for p in 0..m {
            let th = PI * (4.0 * p as f64 + 1.0) / (4.0 * l as f64);
            pre_c[p] = th.cos();
            pre_s[p] = th.sin();
        }
        let (mut post_c, mut post_s) = (vec![0f64; m], vec![0f64; m]);
        for p in 0..m {
            let ph = PI * p as f64 / l as f64;
            post_c[p] = ph.cos();
            post_s[p] = ph.sin();
        }
        // FFT stage twiddles for the M-point transform (flattened over stages).
        let (mut fft_c, mut fft_s) = (Vec::new(), Vec::new());
        let mut len = 2usize;
        while len <= m {
            for k in 0..len / 2 {
                let ang = -2.0 * PI * k as f64 / len as f64;
                fft_c.push(ang.cos());
                fft_s.push(ang.sin());
            }
            len <<= 1;
        }
        MdctTwiddles {
            pre_c,
            pre_s,
            post_c,
            post_s,
            fft_c,
            fft_s,
        }
    }
}

/// The AAC block sizes (2048 long, 256 short) get cached twiddles; any other size
/// (tests) builds a fresh set.
fn mdct_twiddles(n: usize) -> Option<&'static MdctTwiddles> {
    static LONG: OnceLock<MdctTwiddles> = OnceLock::new();
    static SHORT: OnceLock<MdctTwiddles> = OnceLock::new();
    match n {
        2048 => Some(LONG.get_or_init(|| MdctTwiddles::build(2048))),
        256 => Some(SHORT.get_or_init(|| MdctTwiddles::build(256))),
        _ => None,
    }
}

/// Fast forward MDCT (O(N log N)) — numerically matches the direct [`mdct`] (kept as
/// the scalar oracle). The textbook N/4 method: fold the N real inputs to L=N/2 (TDAC),
/// pack into M=N/4 complex with a pre-rotation, one **M-point** FFT, then a post-rotation
/// unpacks the N/2 coefficients. 4× fewer FFT points than a length-N transform. `N` is a
/// power of two (≥4); time samples → `N/2` coeffs.
pub fn mdct_fast(x: &[f32]) -> Vec<f32> {
    let n = x.len();
    if n < 4 {
        return mdct(x); // tiny sizes: fall back to the direct oracle
    }
    let (l, m) = (n / 2, n / 4);
    let owned;
    let tw = match mdct_twiddles(n) {
        Some(t) => t,
        None => {
            owned = MdctTwiddles::build(n);
            &owned
        }
    };
    // TDAC fold x[0..N] → y[mm] (computed on the fly), for the two indices each p needs.
    let (l2, l32) = (l / 2, 3 * l / 2);
    let fold = |mm: usize| -> f64 {
        if mm < l2 {
            -(x[l32 - 1 - mm] as f64) - (x[mm + l32] as f64)
        } else {
            (x[mm - l2] as f64) - (x[l32 - 1 - mm] as f64)
        }
    };
    // Pack + pre-rotate into M complex: v[p] = (y[2p] + i·y[L-1-2p])·e^{-iπ(4p+1)/4L}.
    let (mut re, mut im) = (vec![0f64; m], vec![0f64; m]);
    for p in 0..m {
        let (yr, yi) = (fold(2 * p), fold(l - 1 - 2 * p));
        re[p] = yr * tw.pre_c[p] + yi * tw.pre_s[p];
        im[p] = yi * tw.pre_c[p] - yr * tw.pre_s[p];
    }
    fft(&mut re, &mut im, &tw.fft_c, &tw.fft_s);
    // Post-rotate W = V·e^{-iπp/L}; X[2p]=Re(W), X[L-1-2p]=-Im(W); output scaled ×2.
    let mut out = vec![0f32; l];
    for p in 0..m {
        let (vr, vi) = (re[p], im[p]);
        out[2 * p] = (2.0 * (vr * tw.post_c[p] + vi * tw.post_s[p])) as f32;
        out[l - 1 - 2 * p] = (2.0 * (vr * tw.post_s[p] - vi * tw.post_c[p])) as f32;
    }
    out
}

/// Fast IMDCT (O(N log N)) — numerically matches the direct [`imdct`] (kept as the
/// scalar oracle). `N/2` coefficients → `N` time samples.
///
/// Derivation: the MDCT factors as `C = D·F`, where `F` is the TDAC fold
/// (`N → L = N/2`, the `fold` closure in [`mdct_fast`]) and `D` is the `L`-point
/// DCT-IV. The spec IMDCT is `(2/N)·Cᵀ = (2/N)·Fᵀ·D` (DCT-IV is symmetric), so it is
/// the SAME pre-rotation / `N/4`-point FFT / post-rotation core run on the
/// coefficients (which yields `2·D·X`), followed by the transpose of the fold — an
/// unfold that writes every output sample exactly once.
pub fn imdct_fast(spec: &[f32]) -> Vec<f32> {
    let l = spec.len();
    let n = l * 2;
    if n < 8 {
        return imdct(spec);
    }
    let m = n / 4;
    let owned;
    let tw = match mdct_twiddles(n) {
        Some(t) => t,
        None => {
            owned = MdctTwiddles::build(n);
            &owned
        }
    };
    let (mut re, mut im) = (vec![0f64; m], vec![0f64; m]);
    for p in 0..m {
        let (yr, yi) = (spec[2 * p] as f64, spec[l - 1 - 2 * p] as f64);
        re[p] = yr * tw.pre_c[p] + yi * tw.pre_s[p];
        im[p] = yi * tw.pre_c[p] - yr * tw.pre_s[p];
    }
    fft(&mut re, &mut im, &tw.fft_c, &tw.fft_s);
    // z = D·X (the core produces 2·D·X; the ×2 and the spec's 2/N fold into 1/N).
    let scale = 1.0 / n as f64;
    let mut z = vec![0f64; l];
    for p in 0..m {
        let (vr, vi) = (re[p], im[p]);
        z[2 * p] = 2.0 * (vr * tw.post_c[p] + vi * tw.post_s[p]) * scale;
        z[l - 1 - 2 * p] = 2.0 * (vr * tw.post_s[p] - vi * tw.post_c[p]) * scale;
    }
    // Unfold = Fᵀ: the fold read y[mm] from these x positions, so scatter back.
    let (l2, l32) = (l / 2, 3 * l / 2);
    let mut out = vec![0f32; n];
    for (mm, &v) in z.iter().enumerate() {
        if mm < l2 {
            out[l32 - 1 - mm] = -v as f32;
            out[mm + l32] = -v as f32;
        } else {
            out[mm - l2] = v as f32;
            out[l32 - 1 - mm] = -v as f32;
        }
    }
    out
}

// ---------------------------------------------------------------------------
// Mixed-radix (2/3/5) transforms for the non-power-of-two frame lengths
// (960/120 for GA, 480 for LD/ELD) — and the half-length IMDCT the decoder's
// synthesis works in.
// ---------------------------------------------------------------------------

/// A complex FFT plan for any size whose prime factors are 2, 3 and 5.
struct MixedFft {
    n: usize,
    /// e^{-2πi t/n}, t in 0..n.
    w: Vec<(f64, f64)>,
}

impl MixedFft {
    fn new(n: usize) -> MixedFft {
        let w = (0..n)
            .map(|t| {
                let a = -2.0 * PI * t as f64 / n as f64;
                (a.cos(), a.sin())
            })
            .collect();
        MixedFft { n, w }
    }

    fn radix(n: usize) -> usize {
        if n % 4 == 0 {
            4
        } else if n % 2 == 0 {
            2
        } else if n % 3 == 0 {
            3
        } else if n % 5 == 0 {
            5
        } else {
            n // prime remainder: direct DFT
        }
    }

    /// out[k] = Σ_j x[off + j·stride] · e^{-2πi jk/n}, recursive decimation in time.
    fn rec(&self, x: &[(f64, f64)], off: usize, stride: usize, n: usize, out: &mut [(f64, f64)]) {
        if n == 1 {
            out[0] = x[off];
            return;
        }
        let r = Self::radix(n);
        let m = n / r;
        for j1 in 0..r {
            self.rec(
                x,
                off + j1 * stride,
                stride * r,
                m,
                &mut out[j1 * m..(j1 + 1) * m],
            );
        }
        let step = self.n / n; // W_n^x = W_N^(x·step)
        let mut t = [(0f64, 0f64); 5];
        let mut tmp = vec![(0f64, 0f64); if r > 5 { r } else { 0 }];
        for k1 in 0..m {
            let buf: &mut [(f64, f64)] = if r <= 5 { &mut t[..r] } else { &mut tmp[..] };
            for (j1, b) in buf.iter_mut().enumerate() {
                let (wr, wi) = self.w[(j1 * k1 * step) % self.n];
                let (vr, vi) = out[j1 * m + k1];
                *b = (vr * wr - vi * wi, vr * wi + vi * wr);
            }
            for k2 in 0..r {
                let (mut sr, mut si) = (0f64, 0f64);
                for (j1, &(br, bi)) in buf.iter().enumerate() {
                    let (wr, wi) = self.w[((j1 * k2) % r) * (self.n / r)];
                    sr += br * wr - bi * wi;
                    si += br * wi + bi * wr;
                }
                out[k1 + m * k2] = (sr, si);
            }
        }
    }

    fn forward(&self, x: &[(f64, f64)]) -> Vec<(f64, f64)> {
        let mut out = vec![(0f64, 0f64); self.n];
        self.rec(x, 0, 1, self.n, &mut out);
        out
    }
}

/// Twiddles + FFT for an `L`-coefficient DCT-IV core (the MDCT/IMDCT engine).
struct Dct4Plan {
    l: usize,
    pre: Vec<(f64, f64)>,
    post: Vec<(f64, f64)>,
    fft: MixedFft,
}

impl Dct4Plan {
    fn new(l: usize) -> Dct4Plan {
        let m = l / 2;
        let pre = (0..m)
            .map(|p| {
                let th = PI * (4.0 * p as f64 + 1.0) / (4.0 * l as f64);
                (th.cos(), th.sin())
            })
            .collect();
        let post = (0..m)
            .map(|p| {
                let ph = PI * p as f64 / l as f64;
                (ph.cos(), ph.sin())
            })
            .collect();
        Dct4Plan {
            l,
            pre,
            post,
            fft: MixedFft::new(m),
        }
    }

    /// Returns `2·D·y` (D = the L-point DCT-IV), exactly the core `mdct_fast` uses.
    fn run(&self, y: impl Fn(usize) -> f64) -> Vec<f64> {
        let (l, m) = (self.l, self.l / 2);
        let v: Vec<(f64, f64)> = (0..m)
            .map(|p| {
                let (yr, yi) = (y(2 * p), y(l - 1 - 2 * p));
                let (c, s) = self.pre[p];
                (yr * c + yi * s, yi * c - yr * s)
            })
            .collect();
        let f = self.fft.forward(&v);
        let mut z = vec![0f64; l];
        for p in 0..m {
            let (vr, vi) = f[p];
            let (c, s) = self.post[p];
            z[2 * p] = 2.0 * (vr * c + vi * s);
            z[l - 1 - 2 * p] = 2.0 * (vr * s - vi * c);
        }
        z
    }
}

fn dct4_plan(l: usize) -> &'static Dct4Plan {
    use std::collections::HashMap;
    use std::sync::Mutex;
    static PLANS: OnceLock<Mutex<HashMap<usize, &'static Dct4Plan>>> = OnceLock::new();
    let map = PLANS.get_or_init(|| Mutex::new(HashMap::new()));
    let mut g = map.lock().unwrap_or_else(|e| e.into_inner());
    *g.entry(l)
        .or_insert_with(|| Box::leak(Box::new(Dct4Plan::new(l))))
}

/// Half-length IMDCT for any even `L` with 2/3/5 factors: `L` coefficients →
/// the MIDDLE `L` samples of the `2L`-sample spec IMDCT (with the spec's `2/N`
/// scale), i.e. samples `L/2 .. 3L/2`. This is the buffer FFmpeg's synthesis
/// windows from; the outer quarters follow by TDAC symmetry. `gain` scales the
/// result (the decoder passes 1/32768 to land in float [-1, 1]).
pub fn imdct_half(spec: &[f32], out: &mut [f32], gain: f64) {
    let l = spec.len();
    if let Some(plan) = pow2_dct4(l) {
        return plan.imdct_half(spec, out, gain as f32);
    }
    let plan = dct4_plan(l);
    let z = plan.run(|i| spec[i] as f64);
    // z = 2·D·X; the spec IMDCT is (2/N)·Fᵀ·D·X with N = 2L, so scale by 1/(2L)·…
    // middle[k] = −(D·X)[L−1−k]·(2/N)·…  — see `imdct_fast`'s unfold.
    let scale = gain / (2 * l) as f64;
    for k in 0..l {
        out[k] = (-z[l - 1 - k] * scale) as f32;
    }
}

/// An in-place iterative radix-2 complex FFT in f32: `sign` -1 computes
/// `Σ x·e^{-2πi jk/n}`, +1 the unscaled inverse.
///
/// It carries every IMDCT and both SBR QMF banks (~46% of HE-AAC decode CPU), so
/// the butterfly stages have SIMD twins: AVX on x86-64 (runtime-detected, four
/// butterflies per 256-bit op) and NEON on aarch64 (baseline, two per op). Both
/// perform exactly the scalar products and sums (no FMA), so all three paths are
/// **bit-identical** (`fft_simd_matches_scalar`).
pub(crate) struct Radix2Fft {
    n: usize,
    rev: Vec<u16>,
    /// Per-stage twiddles laid out contiguously: the stage with half-length `h`
    /// (h = 2, 4, ..., n/2) holds `w[j·n/(2h)]` for `j < h` at offset `h - 2`, so a
    /// vector of butterflies loads its twiddles in one go.
    stw: Vec<[f32; 2]>,
    /// The AVX twin is usable (detected once, when the plan is built).
    #[cfg(all(feature = "simd", target_arch = "x86_64"))]
    avx: bool,
}

impl Radix2Fft {
    pub(crate) fn new(n: usize, sign: f64) -> Radix2Fft {
        assert!(n.is_power_of_two() && n >= 2);
        let bits = n.trailing_zeros();
        let rev = (0..n)
            .map(|i| ((i as u32).reverse_bits() >> (32 - bits)) as u16)
            .collect();
        let tw: Vec<[f32; 2]> = (0..n / 2)
            .map(|t| {
                let a = sign * 2.0 * PI * t as f64 / n as f64;
                [a.cos() as f32, a.sin() as f32]
            })
            .collect();
        let mut stw = Vec::with_capacity(n.saturating_sub(2));
        let mut half = 2;
        while half < n {
            let step = n / (2 * half);
            stw.extend((0..half).map(|j| tw[j * step]));
            half *= 2;
        }
        Radix2Fft {
            n,
            rev,
            stw,
            #[cfg(all(feature = "simd", target_arch = "x86_64"))]
            avx: std::is_x86_feature_detected!("avx"),
        }
    }

    pub(crate) fn run(&self, buf: &mut [[f32; 2]]) {
        let buf = &mut buf[..self.n];
        #[cfg(all(feature = "simd", target_arch = "x86_64"))]
        if self.avx {
            crate::prof::count(crate::prof::Kernel::Fft, true, self.n);
            // SAFETY: AVX detected at construction; `buf` is exactly `n` long and
            // `stw` holds every stage's twiddles (see `run_avx`).
            unsafe { self.run_avx(buf) };
            return;
        }
        #[cfg(all(feature = "simd", target_arch = "aarch64"))]
        {
            crate::prof::count(crate::prof::Kernel::Fft, true, self.n);
            // SAFETY: NEON is baseline on aarch64; same bounds as `run_avx`.
            unsafe { self.run_neon(buf) };
            return;
        }
        #[allow(unreachable_code)]
        {
            crate::prof::count(crate::prof::Kernel::Fft, false, self.n);
            self.run_scalar(buf);
        }
    }

    /// Bit-reversal permutation plus the twiddle-free first stage (shared by
    /// every path).
    #[inline(always)]
    fn permute_and_first_stage(&self, buf: &mut [[f32; 2]]) {
        for i in 0..self.n {
            let j = self.rev[i] as usize;
            if i < j {
                buf.swap(i, j);
            }
        }
        for pair in buf.chunks_exact_mut(2) {
            let (a, b) = (pair[0], pair[1]);
            pair[0] = [a[0] + b[0], a[1] + b[1]];
            pair[1] = [a[0] - b[0], a[1] - b[1]];
        }
    }

    /// One radix-2 stage of half-length `half`, scalar.
    #[inline(always)]
    fn stage_scalar(&self, buf: &mut [[f32; 2]], half: usize) {
        let w = &self.stw[half - 2..2 * half - 2];
        for block in buf.chunks_exact_mut(2 * half) {
            let (lo, hi) = block.split_at_mut(half);
            for ((a, b), w) in lo.iter_mut().zip(hi.iter_mut()).zip(w) {
                let t = [b[0] * w[0] - b[1] * w[1], b[0] * w[1] + b[1] * w[0]];
                let x = *a;
                *a = [x[0] + t[0], x[1] + t[1]];
                *b = [x[0] - t[0], x[1] - t[1]];
            }
        }
    }

    /// The scalar reference (and the fallback on CPUs without the twin ISA).
    pub(crate) fn run_scalar(&self, buf: &mut [[f32; 2]]) {
        let buf = &mut buf[..self.n];
        self.permute_and_first_stage(buf);
        let mut half = 2;
        while half < self.n {
            self.stage_scalar(buf, half);
            half *= 2;
        }
    }

    /// AVX twin: stages with `half >= 4` run four butterflies per 256-bit op; the
    /// complex multiply is `b·Re(w) -/+ swap(b)·Im(w)` via `addsub`, the same two
    /// products and one sum per lane as the scalar path (bit-identical).
    ///
    /// # Safety
    /// AVX must be available, and `buf.len() == self.n`. Each vector touches
    /// `buf[s + j .. s + j + 4]` and `buf[s + j + half .. +4]` with
    /// `s + 2·half <= n` and `j + 4 <= half`, and twiddles `stw[half - 2 + j .. +4]`
    /// with `2·half - 2 <= stw.len() = n - 2`.
    #[cfg(all(feature = "simd", target_arch = "x86_64"))]
    #[target_feature(enable = "avx")]
    unsafe fn run_avx(&self, buf: &mut [[f32; 2]]) {
        use std::arch::x86_64::*;
        let n = self.n;
        self.permute_and_first_stage(buf);
        let p = buf.as_mut_ptr() as *mut f32;
        let mut half = 2;
        while half < n {
            if half < 4 {
                self.stage_scalar(buf, half);
            } else {
                let wbase = self.stw.as_ptr().add(half - 2) as *const f32;
                let mut s = 0;
                while s < n {
                    let mut j = 0;
                    while j < half {
                        let pa = p.add(2 * (s + j));
                        let pb = p.add(2 * (s + j + half));
                        let a = _mm256_loadu_ps(pa);
                        let b = _mm256_loadu_ps(pb);
                        let w = _mm256_loadu_ps(wbase.add(2 * j));
                        let wr = _mm256_moveldup_ps(w);
                        let wi = _mm256_movehdup_ps(w);
                        let bs = _mm256_permute_ps::<0b10_11_00_01>(b);
                        let t = _mm256_addsub_ps(_mm256_mul_ps(b, wr), _mm256_mul_ps(bs, wi));
                        _mm256_storeu_ps(pa, _mm256_add_ps(a, t));
                        _mm256_storeu_ps(pb, _mm256_sub_ps(a, t));
                        j += 4;
                    }
                    s += 2 * half;
                }
            }
            half *= 2;
        }
    }

    /// NEON twin: two butterflies per 128-bit op from `half >= 2`; the even-lane
    /// sign of the complex multiply is applied by flipping the sign bit (exact),
    /// so the sums match the scalar path bit for bit.
    ///
    /// # Safety
    /// `buf.len() == self.n`; bounds as for `run_avx` with two-wide vectors.
    #[cfg(all(feature = "simd", target_arch = "aarch64"))]
    #[target_feature(enable = "neon")]
    unsafe fn run_neon(&self, buf: &mut [[f32; 2]]) {
        use std::arch::aarch64::*;
        let n = self.n;
        self.permute_and_first_stage(buf);
        let p = buf.as_mut_ptr() as *mut f32;
        let mask = [0x8000_0000u32, 0, 0x8000_0000, 0];
        let neg_even = vld1q_u32(mask.as_ptr());
        let mut half = 2;
        while half < n {
            let wbase = self.stw.as_ptr().add(half - 2) as *const f32;
            let mut s = 0;
            while s < n {
                let mut j = 0;
                while j < half {
                    let pa = p.add(2 * (s + j));
                    let pb = p.add(2 * (s + j + half));
                    let a = vld1q_f32(pa);
                    let b = vld1q_f32(pb);
                    let w = vld1q_f32(wbase.add(2 * j));
                    let wr = vtrn1q_f32(w, w);
                    let wi = vtrn2q_f32(w, w);
                    let x = vmulq_f32(b, wr);
                    let y = vmulq_f32(vrev64q_f32(b), wi);
                    let y = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(y), neg_even));
                    let t = vaddq_f32(x, y);
                    vst1q_f32(pa, vaddq_f32(a, t));
                    vst1q_f32(pb, vsubq_f32(a, t));
                    j += 2;
                }
                s += 2 * half;
            }
            half *= 2;
        }
    }
}

/// The DCT-IV core for power-of-two lengths in f32: pre-rotation, an `L/2`-point
/// FFT, post-rotation — the same factorisation as [`Dct4Plan`], allocation-free.
pub(crate) struct Pow2Dct4 {
    l: usize,
    pre: Vec<[f32; 2]>,
    post: Vec<[f32; 2]>,
    fft: Radix2Fft,
}

impl Pow2Dct4 {
    fn new(l: usize) -> Pow2Dct4 {
        let m = l / 2;
        let pre = (0..m)
            .map(|p| {
                let th = PI * (4.0 * p as f64 + 1.0) / (4.0 * l as f64);
                [th.cos() as f32, th.sin() as f32]
            })
            .collect();
        let post = (0..m)
            .map(|p| {
                let ph = PI * p as f64 / l as f64;
                [ph.cos() as f32, ph.sin() as f32]
            })
            .collect();
        Pow2Dct4 {
            l,
            pre,
            post,
            fft: Radix2Fft::new(m, -1.0),
        }
    }

    /// Pre-rotation: `v[p] = (x[2p] + i·x[L-1-2p])·e^{-iθ_p}`. Written over
    /// `chunks_exact` / `rchunks_exact` views (the even entries from the front,
    /// the odd ones mirrored from the back) so there is no index arithmetic or
    /// bounds check per element and LLVM vectorises the stride-2 and reversed
    /// accesses.
    #[inline(always)]
    fn rotate_in(&self, x: &[f32], v: &mut [[f32; 2]]) {
        let x = &x[..self.l];
        for (((vp, a), b), &[c, s]) in v
            .iter_mut()
            .zip(x.chunks_exact(2))
            .zip(x.rchunks_exact(2))
            .zip(&self.pre)
        {
            let (yr, yi) = (a[0], b[1]);
            *vp = [yr * c + yi * s, yi * c - yr * s];
        }
    }

    /// The unscaled DCT-IV: `out[m] = Σ x[k]·cos(π/L·(m+½)(k+½))`, using
    /// `scratch` (at least L/2 entries) for the FFT.
    pub(crate) fn dct4(&self, x: &[f32], out: &mut [f32], scratch: &mut [[f32; 2]]) {
        let m = self.l / 2;
        let v = &mut scratch[..m];
        self.rotate_in(x, v);
        self.fft.run(v);
        // out[2q] comes from v[q]; out[2q+1] = out[L-1-2p] from v[p], p = m-1-q.
        let post = v.iter().zip(&self.post);
        for ((o, (&[vr, vi], &[c, s])), (&[ur, ui], &[uc, us])) in out[..self.l]
            .chunks_exact_mut(2)
            .zip(post.clone())
            .zip(post.rev())
        {
            o[0] = vr * c + vi * s;
            o[1] = ur * us - ui * uc;
        }
    }

    /// As [`imdct_half`]: `out[k] = -(D·X)[L-1-k]·gain/L`.
    fn imdct_half(&self, spec: &[f32], out: &mut [f32], gain: f32) {
        let m = self.l / 2;
        // A stack scratch sized to the call: zero-filling a worst-case 1024-entry
        // array cost 8 KB of memset per short window, whose need is 512 bytes.
        match m {
            0..=64 => self.imdct_half_in(spec, out, gain, &mut [[0f32; 2]; 64]),
            65..=512 => self.imdct_half_in(spec, out, gain, &mut [[0f32; 2]; 512]),
            _ => self.imdct_half_in(spec, out, gain, &mut [[0f32; 2]; 1024]),
        }
    }

    #[inline(always)]
    fn imdct_half_in(&self, spec: &[f32], out: &mut [f32], gain: f32, buf: &mut [[f32; 2]]) {
        let m = self.l / 2;
        let v = &mut buf[..m];
        self.rotate_in(spec, v);
        self.fft.run(v);
        let scale = gain / self.l as f32;
        // out[2q] = -(D·X)[L-1-2q]·s from v[q]; out[2q+1] = -(D·X)[2p]·s, p = m-1-q.
        let post = v.iter().zip(&self.post);
        for ((o, (&[vr, vi], &[c, s])), (&[ur, ui], &[uc, us])) in out[..self.l]
            .chunks_exact_mut(2)
            .zip(post.clone())
            .zip(post.rev())
        {
            o[0] = -(vr * s - vi * c) * scale;
            o[1] = -(ur * uc + ui * us) * scale;
        }
    }

    /// The pre-restructure indexed forms, kept as the bit-exact oracle.
    #[cfg(test)]
    fn dct4_reference(&self, x: &[f32], out: &mut [f32]) {
        let (l, m) = (self.l, self.l / 2);
        let mut v = vec![[0f32; 2]; m];
        for (p, vp) in v.iter_mut().enumerate() {
            let (yr, yi) = (x[2 * p], x[l - 1 - 2 * p]);
            let [c, s] = self.pre[p];
            *vp = [yr * c + yi * s, yi * c - yr * s];
        }
        self.fft.run_scalar(&mut v);
        for (p, &[vr, vi]) in v.iter().enumerate() {
            let [c, s] = self.post[p];
            out[2 * p] = vr * c + vi * s;
            out[l - 1 - 2 * p] = vr * s - vi * c;
        }
    }

    #[cfg(test)]
    fn imdct_half_reference(&self, spec: &[f32], out: &mut [f32], gain: f32) {
        let (l, m) = (self.l, self.l / 2);
        let mut v = vec![[0f32; 2]; m];
        for (p, vp) in v.iter_mut().enumerate() {
            let (yr, yi) = (spec[2 * p], spec[l - 1 - 2 * p]);
            let [c, s] = self.pre[p];
            *vp = [yr * c + yi * s, yi * c - yr * s];
        }
        self.fft.run_scalar(&mut v);
        let scale = gain / l as f32;
        for (p, &[vr, vi]) in v.iter().enumerate() {
            let [c, s] = self.post[p];
            out[l - 1 - 2 * p] = -(vr * c + vi * s) * scale;
            out[2 * p] = -(vr * s - vi * c) * scale;
        }
    }
}

/// The cached power-of-two DCT-IV plan for `l` (16..=2048), if `l` qualifies.
pub(crate) fn pow2_dct4(l: usize) -> Option<&'static Pow2Dct4> {
    static PLANS: [OnceLock<Pow2Dct4>; 12] = [const { OnceLock::new() }; 12];
    if !l.is_power_of_two() || !(16..=2048).contains(&l) {
        return None;
    }
    Some(PLANS[l.trailing_zeros() as usize].get_or_init(|| Pow2Dct4::new(l)))
}

/// Forward MDCT with the same normalisation as [`mdct`] (`2·Σ`), any 2/3/5 size.
pub fn mdct_any(x: &[f32]) -> Vec<f32> {
    let n = x.len();
    let (l, l2, l32) = (n / 2, n / 4, 3 * n / 4);
    let plan = dct4_plan(l);
    let fold = |mm: usize| -> f64 {
        if mm < l2 {
            -(x[l32 - 1 - mm] as f64) - (x[mm + l32] as f64)
        } else {
            (x[mm - l2] as f64) - (x[l32 - 1 - mm] as f64)
        }
    };
    plan.run(fold).iter().map(|&v| v as f32).collect()
}

/// AAC sine analysis/synthesis window: `w[n] = sin(π/N·(n+½))`.
pub fn sine_window(n: usize) -> Vec<f32> {
    (0..n)
        .map(|i| (PI / n as f64 * (i as f64 + 0.5)).sin() as f32)
        .collect()
}

/// Modified Bessel function of the first kind, order 0 (series form).
fn bessel_i0(x: f64) -> f64 {
    let mut sum = 1.0;
    let mut term = 1.0;
    let half_x = x / 2.0;
    for k in 1..50 {
        term *= (half_x / k as f64).powi(2);
        sum += term;
        if term < 1e-12 * sum {
            break;
        }
    }
    sum
}

/// Kaiser-Bessel-derived window of length `n` (ISO 14496-3 §4.6.11.2.4).
/// `alpha` is 4 for long blocks (N=2048), 6 for short (N=256).
pub fn kbd_window(n: usize, alpha: f64) -> Vec<f32> {
    let half = n / 2;
    // Cumulative Kaiser window over the first half.
    let mut cumulative = vec![0f64; half + 1];
    let mut running = 0.0;
    for p in 0..=half {
        let r = 2.0 * p as f64 / half as f64 - 1.0; // -1..1
        running += bessel_i0(PI * alpha * (1.0 - r * r).max(0.0).sqrt());
        cumulative[p] = running;
    }
    let total = cumulative[half];
    let mut w = vec![0f32; n];
    for i in 0..half {
        w[i] = (cumulative[i] / total).sqrt() as f32;
        w[n - 1 - i] = w[i];
    }
    w
}

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

    /// The fast FFT-based MDCT must match the direct O(N²) oracle at both block
    /// sizes, on raw and encoder-scaled (×32768) input.
    #[test]
    fn mdct_fast_matches_direct() {
        for &n in &[256usize, 2048] {
            for &scale in &[1.0f32, 32768.0] {
                let x: Vec<f32> = (0..n)
                    .map(|i| {
                        scale
                            * (0.3 * (i as f64 * 0.021).sin() + 0.2 * (i as f64 * 0.005).cos())
                                as f32
                    })
                    .collect();
                let (a, b) = (mdct(&x), mdct_fast(&x));
                let tol = 2e-3 * scale.max(1.0);
                for k in 0..n / 2 {
                    assert!(
                        (a[k] - b[k]).abs() < tol,
                        "n={n} scale={scale} k={k}: {} vs {}",
                        a[k],
                        b[k]
                    );
                }
            }
        }
    }

    /// The fast IMDCT must match the direct O(N²) oracle at every size the codecs
    /// use (2048/256 AAC-LC, 1920/240 for 960-frame and 1024/960 for LD/ELD once
    /// they exist fall back to the oracle until they get their own core), on
    /// broadband input at the decoder's ~16-bit scale.
    #[test]
    fn imdct_fast_matches_direct() {
        for &l in &[16usize, 128, 1024] {
            let x: Vec<f32> = (0..l)
                .map(|k| {
                    let k = k as f64;
                    (8000.0 * (k * 0.37).sin() * (-k / l as f64).exp() + 300.0 * (k * 1.9).cos())
                        as f32
                })
                .collect();
            let (a, b) = (imdct(&x), imdct_fast(&x));
            let peak = a.iter().fold(0f32, |m, v| m.max(v.abs()));
            for i in 0..2 * l {
                assert!(
                    (a[i] - b[i]).abs() <= 1e-4 * peak.max(1.0),
                    "l={l} i={i}: {} vs {}",
                    a[i],
                    b[i]
                );
            }
        }
    }

    /// The half IMDCT must equal the middle half of the direct oracle for every
    /// frame length the decoder uses, power-of-two or not.
    #[test]
    fn imdct_half_matches_middle_of_direct() {
        for &l in &[1024usize, 960, 512, 480, 128, 120] {
            let x: Vec<f32> = (0..l)
                .map(|k| (5000.0 * ((k as f64) * 0.61).sin() / (1.0 + k as f64 * 0.01)) as f32)
                .collect();
            let full = imdct(&x);
            let mut half = vec![0f32; l];
            imdct_half(&x, &mut half, 1.0);
            let peak = full.iter().fold(0f32, |m, v| m.max(v.abs()));
            for k in 0..l {
                assert!(
                    (full[l / 2 + k] - half[k]).abs() <= 1e-4 * peak.max(1.0),
                    "l={l} k={k}: {} vs {}",
                    full[l / 2 + k],
                    half[k]
                );
            }
        }
    }

    #[test]
    fn mdct_any_matches_direct() {
        for &n in &[1920usize, 240, 2048] {
            let x: Vec<f32> = (0..n)
                .map(|i| ((i as f64 * 0.013).sin() * 0.7) as f32)
                .collect();
            let (a, b) = (mdct(&x), mdct_any(&x));
            for k in 0..n / 2 {
                assert!(
                    (a[k] - b[k]).abs() < 2e-3,
                    "n={n} k={k}: {} vs {}",
                    a[k],
                    b[k]
                );
            }
        }
    }

    #[test]
    fn dequant_matches_spec_curve() {
        assert_eq!(dequant(0), 0.0);
        assert_eq!(dequant(1), 1.0);
        assert!((dequant(2) - 2f32.powf(4.0 / 3.0)).abs() < 1e-5);
        assert!((dequant(-3) + 3f32.powf(4.0 / 3.0)).abs() < 1e-4);
        // Monotonic and sign-preserving.
        assert!(dequant(10) > dequant(9));
        assert!(dequant(-5) < 0.0);
    }

    #[test]
    fn sf_gain_is_quarter_db_steps() {
        assert!((sf_gain(100) - 1.0).abs() < 1e-6); // sf 100 → unity
        assert!((sf_gain(104) - 2.0).abs() < 1e-5); // +4 → ×2
        assert!((sf_gain(96) - 0.5).abs() < 1e-6); // −4 → ×0.5
    }

    /// w[n]² + w[n+N/2]² = 1 is the Princen-Bradley condition both AAC windows
    /// must satisfy for the filterbank to reconstruct perfectly.
    fn assert_princen_bradley(w: &[f32]) {
        let half = w.len() / 2;
        for n in 0..half {
            let s = w[n] * w[n] + w[n + half] * w[n + half];
            assert!((s - 1.0).abs() < 1e-4, "PB violated at {n}: {s}");
        }
    }

    #[test]
    fn sine_window_satisfies_princen_bradley() {
        assert_princen_bradley(&sine_window(256));
    }

    #[test]
    fn kbd_window_satisfies_princen_bradley() {
        assert_princen_bradley(&kbd_window(256, 4.0));
        assert_princen_bradley(&kbd_window(256, 6.0));
    }

    /// The decisive correctness check: windowed MDCT → IMDCT → windowed
    /// overlap-add reconstructs the original signal in the steady-state region
    /// (TDAC). If the IMDCT/window math is wrong, this fails.
    #[test]
    fn mdct_imdct_overlap_add_perfectly_reconstructs() {
        let n = 64usize;
        let half = n / 2;
        let w = sine_window(n);
        // A deterministic, broadband test signal.
        let len = 4 * n;
        let signal: Vec<f32> = (0..len)
            .map(|i| (0.3 * (i as f64 * 0.21).sin() + 0.2 * (i as f64 * 0.05).cos()) as f32)
            .collect();

        let mut out = vec![0f32; len];
        let mut p = 0;
        while p + n <= len {
            let framed: Vec<f32> = (0..n).map(|i| signal[p + i] * w[i]).collect();
            let synth = imdct(&mdct(&framed));
            for i in 0..n {
                out[p + i] += synth[i] * w[i];
            }
            p += half;
        }

        // Interior (≥ one full frame from each edge) must match the input.
        for i in n..(len - n) {
            assert!(
                (out[i] - signal[i]).abs() < 1e-3,
                "reconstruction error at {i}: {} vs {}",
                out[i],
                signal[i]
            );
        }
    }
}

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

    /// The FFT SIMD twin (AVX / NEON, whichever this build dispatches to) must
    /// equal the scalar reference bit for bit at every size the codec uses.
    #[test]
    fn fft_simd_matches_scalar() {
        for bits in 1..=10 {
            let n = 1usize << bits;
            for sign in [-1.0, 1.0] {
                let fft = Radix2Fft::new(n, sign);
                let mut seed = 0x1234_5678u32 ^ n as u32;
                let mut r = || {
                    seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
                    ((seed >> 8) as f32 / (1u32 << 24) as f32 - 0.5) * 65536.0
                };
                let input: Vec<[f32; 2]> = (0..n).map(|_| [r(), r()]).collect();
                let (mut want, mut got) = (input.clone(), input.clone());
                fft.run_scalar(&mut want);
                fft.run(&mut got);
                for k in 0..n {
                    for c in 0..2 {
                        assert_eq!(
                            got[k][c].to_bits(),
                            want[k][c].to_bits(),
                            "n={n} sign={sign} k={k}"
                        );
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod dequant_table {
    /// The table lookup must equal the formula bit for bit, on both sides of the
    /// table boundary.
    #[test]
    fn dequant_table_matches_formula() {
        for q in -9000i32..=9000 {
            assert_eq!(
                super::dequant(q).to_bits(),
                super::dequant_formula(q).to_bits(),
                "q={q}"
            );
        }
        for q in [i32::MIN + 1, -100_000, 100_000, i32::MAX] {
            assert_eq!(
                super::dequant(q).to_bits(),
                super::dequant_formula(q).to_bits(),
                "q={q}"
            );
        }
    }
}

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

    /// The restructured rotations (and the FFT twin under them) must equal the
    /// original indexed forms bit for bit.
    #[test]
    fn dct4_and_imdct_half_match_reference() {
        for bits in 4..=11 {
            let l = 1usize << bits;
            let plan = pow2_dct4(l).unwrap();
            let x: Vec<f32> = (0..l)
                .map(|i| ((i as f32 * 0.37).sin() * 12345.0) + i as f32)
                .collect();
            let (mut got, mut want) = (vec![0f32; l], vec![0f32; l]);
            plan.dct4(&x, &mut got, &mut vec![[0f32; 2]; l / 2]);
            plan.dct4_reference(&x, &mut want);
            assert!(
                got.iter()
                    .zip(&want)
                    .all(|(a, b)| a.to_bits() == b.to_bits()),
                "dct4 l={l}"
            );
            plan.imdct_half(&x, &mut got, 0.37);
            plan.imdct_half_reference(&x, &mut want, 0.37);
            assert!(
                got.iter()
                    .zip(&want)
                    .all(|(a, b)| a.to_bits() == b.to_bits()),
                "imdct_half l={l}"
            );
        }
    }
}

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

    /// Per-call cost of the transform kernels, to price a SIMD twin against the
    /// stage it lives in. `cargo test --release -- --ignored --nocapture kernel_price`
    #[test]
    #[ignore]
    fn kernel_price() {
        for n in [32usize, 64, 512] {
            let fft = Radix2Fft::new(n, -1.0);
            let mut buf: Vec<[f32; 2]> = (0..n)
                .map(|i| [(i as f32).sin(), (i as f32).cos()])
                .collect();
            let iters = 2_000_000 / n;
            let mut best = f64::MAX;
            for _ in 0..7 {
                let t = std::time::Instant::now();
                for _ in 0..iters {
                    fft.run(std::hint::black_box(&mut buf));
                }
                best = best.min(t.elapsed().as_secs_f64() / iters as f64);
            }
            eprintln!("Radix2Fft::run n={n:4}: {:8.1} ns/call", best * 1e9);
        }
        for len in [64usize, 512] {
            let (src0, src1): (Vec<f32>, Vec<f32>) = (
                (0..len).map(|i| i as f32).collect(),
                (0..len).map(|i| -(i as f32)).collect(),
            );
            let win: Vec<f32> = (0..2 * len).map(|i| (i as f32 * 0.01).sin()).collect();
            let mut dst = vec![0f32; 2 * len];
            let iters = 4_000_000 / len;
            let mut best = f64::MAX;
            for _ in 0..7 {
                let t = std::time::Instant::now();
                for _ in 0..iters {
                    crate::decode::synth::fmul_window(
                        std::hint::black_box(&mut dst),
                        &src0,
                        &src1,
                        &win,
                        len,
                    );
                }
                best = best.min(t.elapsed().as_secs_f64() / iters as f64);
            }
            eprintln!("fmul_window len={len:4}: {:8.1} ns/call", best * 1e9);
        }
        for l in [64usize, 512, 1024] {
            let plan = pow2_dct4(l).unwrap();
            let x: Vec<f32> = (0..l).map(|i| (i as f32 * 0.3).sin()).collect();
            let mut out = vec![0f32; l];
            let iters = 2_000_000 / l;
            let mut best = f64::MAX;
            for _ in 0..7 {
                let t = std::time::Instant::now();
                for _ in 0..iters {
                    plan.imdct_half(std::hint::black_box(&x), &mut out, 1.0);
                }
                best = best.min(t.elapsed().as_secs_f64() / iters as f64);
            }
            eprintln!(
                "Pow2Dct4::imdct_half l={l:4}: {:8.1} ns/call (incl. FFT l/2)",
                best * 1e9
            );
        }
    }
}