rusty_aac 1.1.1

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
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
//! In-house **AAC-LC encoder** (see `docs/finished/codec-aac-encoder.md` for the brick
//! ledger). Bricks 1–7 are complete: encode-side primitives inverting the decoder
//! (bit writer, spectral-codebook encoder, header/config serializers), the forward
//! filterbank, the quantizer + rate loop, a Bark-spreading psychoacoustic model,
//! block switching, M/S stereo, and container/CLI integration.
//!
//! **What this encoder does NOT yet do** — the arms tracked by
//! `docs/finished/codec-aac-great-gate.md`. Naming them here keeps a fit from being run
//! against a mis-documented encoder:
//!
//! * The psy model drives **long blocks only**; short blocks code with flat
//!   scalefactors and a single window group (arm A1).
//! * The signal-to-mask ratio is a **constant 18 dB** — no tonality/SFM term
//!   (arm A2), and no absolute threshold of hearing, so nothing here is a
//!   function of absolute level (arm A4).
//! * **TNS, PNS and intensity stereo are never emitted** (arms A3/A6/A7), though
//!   `crate::decode` implements all three.
//! * The rate loop searches **one global base offset** against a **constant
//!   per-frame budget** — there is no outer per-band distortion loop (arm A12)
//!   and no bit reservoir or VBR mode (arm A5).
//! * Window shape is hardcoded sine (arm A8) and the transient detector uses an
//!   absolute energy ratio (arm A9).

use crate::codebook::{Codebook, CODEBOOKS, INTENSITY_HCB, INTENSITY_HCB2, NOISE_HCB};
use crate::ics::{IcsInfo, WindowSequence};
use crate::prof::{self, Kernel, Stage};
use crate::swb::swb_offsets;
use crate::tables::spectral_book;
use crate::{AdtsHeader, AudioSpecificConfig, Error, Result};
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex, OnceLock};

// ---------------------------------------------------------------------------
// Bit writer — MSB-first, the mirror of the decoder's `BitReader`.
// ---------------------------------------------------------------------------
pub struct BitWriter {
    buf: Vec<u8>,
    cur: u8,
    nbits: u8,
}

impl BitWriter {
    #[must_use]
    pub fn new() -> Self {
        Self {
            buf: Vec::new(),
            cur: 0,
            nbits: 0,
        }
    }

    /// Append the low `n` bits of `val`, most-significant bit first.
    pub fn write(&mut self, val: u32, n: u32) {
        for i in (0..n).rev() {
            self.cur = (self.cur << 1) | ((val >> i) & 1) as u8;
            self.nbits += 1;
            if self.nbits == 8 {
                self.buf.push(self.cur);
                self.cur = 0;
                self.nbits = 0;
            }
        }
    }

    pub fn write_bool(&mut self, b: bool) {
        self.write(u32::from(b), 1);
    }

    /// Total bits written so far.
    #[must_use]
    pub fn bit_len(&self) -> usize {
        self.buf.len() * 8 + self.nbits as usize
    }

    /// Pad to a byte boundary with zero bits and return the bytes.
    #[must_use]
    pub fn into_bytes(mut self) -> Vec<u8> {
        if self.nbits != 0 {
            self.cur <<= 8 - self.nbits;
            self.buf.push(self.cur);
        }
        self.buf
    }
}

impl Default for BitWriter {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// Spectral codebook encoding — the inverse of `codebook::apply_index`.
// ---------------------------------------------------------------------------

/// Pack a `dim`-tuple of quantized coefficients into codebook `cb`'s base-`modulo`
/// Huffman index, or None if the tuple isn't representable by that codebook.
#[allow(
    clippy::trivially_copy_pass_by_ref,
    reason = "measured: passing the 4-byte Codebook by value cost +0.7% encode / +1.4% decode instructions in the inlined hot loops"
)]
fn tuple_index(cb: &Codebook, tuple: &[i32]) -> Option<u32> {
    let dim = cb.dim as usize;
    let lav = u32::from(cb.lav);
    let modulo = if cb.unsigned { lav + 1 } else { 2 * lav + 1 };
    let mut index = 0u32;
    for &c in &tuple[..dim] {
        let mag = c.unsigned_abs();
        let digit = if cb.unsigned {
            if cb.esc {
                mag.min(lav) // book 11: a magnitude ≥ lav clamps to lav and escapes
            } else if mag <= lav {
                mag
            } else {
                return None;
            }
        } else if mag <= lav {
            (c + lav as i32) as u32
        } else {
            return None;
        };
        index = index * modulo + digit;
    }
    Some(index)
}

/// Bits to code `tuple` with codebook `cb_num`, or None if unrepresentable.
#[must_use]
pub fn spectral_bits(cb_num: usize, tuple: &[i32]) -> Option<usize> {
    let cb = &CODEBOOKS[cb_num];
    let idx = tuple_index(cb, tuple)?;
    let (_, len) = spectral_book(cb_num as u8).code(idx as usize);
    let dim = cb.dim as usize;
    let mut bits = len as usize;
    if cb.unsigned {
        bits += tuple[..dim].iter().filter(|&&c| c != 0).count(); // one sign bit each
    }
    if cb.esc {
        for &c in &tuple[..dim] {
            if c.unsigned_abs() >= u32::from(cb.lav) {
                bits += escape_bits(c.unsigned_abs());
            }
        }
    }
    Some(bits)
}

/// Emit `tuple` with codebook `cb_num`: codeword, then sign bits (unsigned books),
/// then escape sequences (book 11). Caller ensures `spectral_bits` is Some.
///
/// # Panics
///
/// If `cb_num` is not a spectral codebook or `tuple` is not representable in it
/// (the caller's contract).
pub fn spectral_emit(cb_num: usize, tuple: &[i32], w: &mut BitWriter) {
    let cb = &CODEBOOKS[cb_num];
    let dim = cb.dim as usize;
    let idx = tuple_index(cb, tuple).expect("representable tuple");
    let (code, len) = spectral_book(cb_num as u8).code(idx as usize);
    w.write(code, u32::from(len));
    if cb.unsigned {
        for &c in &tuple[..dim] {
            if c != 0 {
                w.write_bool(c < 0);
            }
        }
    }
    if cb.esc {
        for &c in &tuple[..dim] {
            if c.unsigned_abs() >= u32::from(cb.lav) {
                emit_escape(c.unsigned_abs(), w);
            }
        }
    }
}

/// Escape length for magnitude `m` (≥ 16): `2N+5` bits, `2^(N+4) ≤ m < 2^(N+5)`.
fn escape_bits(m: u32) -> usize {
    let n = m.ilog2() - 4;
    2 * n as usize + 5
}

/// Escape sequence (ISO §4.6.3.3): N leading 1-bits, a 0, then N+4 bits of
/// `m - 2^(N+4)`.
fn emit_escape(m: u32, w: &mut BitWriter) {
    let n = m.ilog2() - 4;
    for _ in 0..n {
        w.write_bool(true);
    }
    w.write_bool(false);
    let bits = n + 4;
    w.write(m - (1 << bits), bits);
}

// ---------------------------------------------------------------------------
// Header / config serializers — inverses of the decoder's parsers.
// ---------------------------------------------------------------------------

/// The `AudioSpecificConfig` bytes for an AAC-LC stream — the MP4 `esds`
/// `DecoderSpecificInfo` the muxer needs (2 bytes for standard rates).
#[must_use]
pub fn audio_specific_config_bytes(sample_rate: u32, channels: u16) -> Vec<u8> {
    write_audio_specific_config(&AudioSpecificConfig {
        object_type: 2, // AAC-LC
        sample_rate,
        channels,
    })
}

/// Serialize an `AudioSpecificConfig` (ISO §1.6.2.1) — the `esds`/`stsd` config
/// bytes the MP4 muxer needs. Inverse of `parse_audio_specific_config`.
#[must_use]
pub fn write_audio_specific_config(cfg: &AudioSpecificConfig) -> Vec<u8> {
    let mut w = BitWriter::new();
    if cfg.object_type >= 31 {
        w.write(31, 5);
        w.write(u32::from(cfg.object_type - 32), 6);
    } else {
        w.write(u32::from(cfg.object_type), 5);
    }
    if let Some(i) = crate::sf_index_for_rate(cfg.sample_rate) {
        w.write(u32::from(i), 4);
    } else {
        w.write(0x0F, 4);
        w.write(cfg.sample_rate, 24);
    }
    w.write(u32::from(cfg.channels), 4);
    w.into_bytes()
}

/// Serialize a 7-byte ADTS frame header (no CRC) — inverse of `parse_adts`.
/// `hdr.frame_length` must include this 7-byte header.
///
/// # Panics
///
/// If `hdr.sample_rate` is not one of the thirteen standard AAC rates.
#[must_use]
pub fn write_adts_header(hdr: &AdtsHeader) -> Vec<u8> {
    let sf = crate::sf_index_for_rate(hdr.sample_rate).expect("standard rate for ADTS");
    let mut w = BitWriter::new();
    w.write(0xFFF, 12); // syncword
    w.write(0, 1); // MPEG-4
    w.write(0, 2); // layer (00)
    w.write_bool(true); // protection_absent → 7-byte header, no CRC
    w.write(u32::from(hdr.object_type - 1), 2); // profile
    w.write(u32::from(sf), 4);
    w.write(0, 1); // private
    w.write(u32::from(hdr.channels), 3); // channel config
    w.write(0, 4); // orig/home/copyright id+start
    w.write(hdr.frame_length as u32, 13);
    w.write(0x7FF, 11); // buffer fullness (VBR marker)
    w.write(0, 2); // num_raw_data_blocks - 1
    w.into_bytes()
}

/// Serialize `ics_info` for AAC-LC — inverse of `parse_ics_info`.
pub fn encode_ics_info(w: &mut BitWriter, info: &IcsInfo) {
    w.write(0, 1); // ics_reserved
    w.write(info.window_sequence.to_bits(), 2);
    w.write_bool(info.window_shape_kbd);
    if info.window_sequence.is_short() {
        w.write(u32::from(info.max_sfb), 4);
        w.write(grouping_bits(&info.window_group_length), 7);
    } else {
        w.write(u32::from(info.max_sfb), 6);
        w.write(0, 1); // predictor_data_present (AAC-LC = 0)
    }
}

/// The 7 `scale_factor_grouping` bits from window-group lengths (inverse of the
/// parser's grouping walk): the bit for window i+1 is 1 if it continues the
/// current group, 0 if it starts a new one.
fn grouping_bits(group_lengths: &[u8]) -> u32 {
    let mut is_start = [false; 8];
    let mut w = 0usize;
    for &len in group_lengths {
        if w < 8 {
            is_start[w] = true;
        }
        w += len as usize;
    }
    let mut sfg = 0u32;
    for i in 0..7 {
        if !is_start[i + 1] {
            sfg |= 1 << (6 - i);
        }
    }
    sfg
}

// ---------------------------------------------------------------------------
// Filterbank — forward long-block MDCT (inverse of the decoder's synthesis).
// ---------------------------------------------------------------------------
pub const FRAME_LEN: usize = 1024;
pub const LONG_N: usize = 2048;
const SHORT_N: usize = 256;
const SHORT_HALF: usize = 128;
/// 1 / `OUTPUT_NORM`: the decoder scales its output by 1/32768, so the encoder
/// scales the spectrum up by 32768 to land in the AAC coefficient domain.
const SPEC_SCALE: f32 = 32768.0;

/// Forward long-block filterbank: window the overlapping 2048 input samples
/// (previous frame's 1024 ++ current 1024) and forward-MDCT to 1024 coefficients,
/// scaled so the decoder's `imdct · window · (1/32768) + overlap-add` reconstructs
/// the input (TDAC). `win` is the 2048-length window (sine or KBD).
#[must_use]
pub fn analyze_long(prev: &[f32; FRAME_LEN], cur: &[f32; FRAME_LEN], win: &[f32]) -> Vec<f32> {
    let _prof = prof::scope(Stage::EncMdct);
    // Written into fresh capacity: both halves are fully computed, so the zero
    // fill of a `vec![0f32; LONG_N]` was overhead.
    let mut windowed = Vec::<f32>::with_capacity(LONG_N);
    let w = windowed.spare_capacity_mut();
    for n in 0..FRAME_LEN {
        w[n].write(prev[n] * win[n] * SPEC_SCALE);
        w[FRAME_LEN + n].write(cur[n] * win[FRAME_LEN + n] * SPEC_SCALE);
    }
    // SAFETY: LONG_N = 2·FRAME_LEN and the loop wrote both halves in full.
    unsafe { windowed.set_len(LONG_N) };
    crate::dsp::mdct_fast(&windowed)
}

/// Forward short-block filterbank: eight 256-sample windows (128-hop) tiled across
/// [448, 1600) of the prev++cur 2048 buffer, each windowed + MDCT'd to 128 coeffs,
/// laid out window-major (the exact inverse of the decoder's `short_frame`). The
/// uncovered [0,448)/[1600,2048) regions are bridged by the LongStart/LongStop
/// neighbours — which is why `EightShort` must sit between transition blocks.
#[must_use]
pub fn analyze_short(prev: &[f32; FRAME_LEN], cur: &[f32; FRAME_LEN], sw: &[f32]) -> Vec<f32> {
    let _prof = prof::scope(Stage::EncMdct);
    let mut buf = [0f32; LONG_N];
    buf[..FRAME_LEN].copy_from_slice(prev);
    buf[FRAME_LEN..].copy_from_slice(cur);
    let mut spec = vec![0f32; FRAME_LEN];
    for w in 0..8 {
        let off = 448 + w * SHORT_HALF;
        let mut windowed = [0f32; SHORT_N];
        for (n, s) in windowed.iter_mut().enumerate() {
            *s = buf[off + n] * sw[n] * SPEC_SCALE;
        }
        let coeffs = crate::dsp::mdct_fast(&windowed);
        spec[w * SHORT_HALF..(w + 1) * SHORT_HALF].copy_from_slice(&coeffs);
    }
    spec
}

/// The 2048-sample analysis window for a long or transition block — the exact
/// encode-side twin of the decoder's `long_window`.
///
/// **The left half follows the PREVIOUS block's shape, the right half the
/// current's** (`prev_kbd` / `cur_kbd`), because that is what the decoder does:
/// each overlap region must be windowed identically on both sides or TDAC fails
/// and the overlap-add leaves audible seams. LongStart/LongStop taper one side to
/// a short window so the sequence stays TDAC-exact.
///
/// The decoder seeds `prev_kbd = false` for the first frame of a stream
/// (`decode.rs`), so the encoder must too.
#[allow(clippy::too_many_arguments)]
fn long_window(
    seq: WindowSequence,
    prev_kbd: bool,
    cur_kbd: bool,
    sine_l: &[f32],
    kbd_l: &[f32],
    sine_s: &[f32],
    kbd_s: &[f32],
) -> Vec<f32> {
    let long_prev = if prev_kbd { kbd_l } else { sine_l };
    let long_cur = if cur_kbd { kbd_l } else { sine_l };
    let short_prev = if prev_kbd { kbd_s } else { sine_s };
    let short_cur = if cur_kbd { kbd_s } else { sine_s };

    let mut w = vec![0f32; LONG_N];
    if seq == WindowSequence::LongStop {
        w[448..448 + SHORT_HALF].copy_from_slice(&short_prev[..SHORT_HALF]);
        for s in w.iter_mut().take(FRAME_LEN).skip(576) {
            *s = 1.0;
        }
    } else {
        w[..FRAME_LEN].copy_from_slice(&long_prev[..FRAME_LEN]);
    }
    if seq == WindowSequence::LongStart {
        for s in w.iter_mut().take(FRAME_LEN + 448).skip(FRAME_LEN) {
            *s = 1.0;
        }
        w[FRAME_LEN + 448..FRAME_LEN + 448 + SHORT_HALF].copy_from_slice(&short_cur[SHORT_HALF..]);
    } else {
        w[FRAME_LEN..].copy_from_slice(&long_cur[FRAME_LEN..]);
    }
    w
}

/// **The Rung 0 gate signal**: order-2 LPC prediction gain over the frame's time
/// samples — a cheap tonality proxy.
///
/// Chosen over the spectral flatness measure the lab validates because SFM needs
/// a spectrum, and the window shape must be decided *before* the spectrum exists
/// (the shape determines the window that produces it). Computing a probe MDCT per
/// frame just to pick a window would roughly double filterbank cost for a
/// small-by-design win — dominated on `bits-per-op` grounds before it started.
///
/// This is O(3n) multiply-adds and needs no transform. Tonal signals are strongly
/// predictable from two past samples; noise is not. Validated against the
/// per-class truth table in `lab::signals` before being wired here.
///
/// Returns `R[0] / E₂ ≥ 1`.
pub(crate) fn time_tonality(x: &[f32]) -> f32 {
    let n = x.len();
    if n < 8 {
        return 1.0;
    }
    let mut r = [0f64; 3];
    for (lag, slot) in r.iter_mut().enumerate() {
        let mut acc = 0f64;
        for i in 0..n - lag {
            acc += f64::from(x[i]) * f64::from(x[i + lag]);
        }
        *slot = acc;
    }
    if r[0] <= 1e-12 {
        return 1.0;
    }
    let r0 = r[0] * 1.0001; // white-noise correction — keeps Levinson stable
    let k1 = r[1] / r0;
    let e1 = r0 * (1.0 - k1 * k1);
    if e1 <= 1e-12 {
        return 1.0;
    }
    let k2 = (r[2] - k1 * r[1]) / e1;
    let e2 = e1 * (1.0 - k2 * k2);
    ((r0 / e2.max(1e-12)) as f32).max(1.0)
}

// ---------------------------------------------------------------------------
// Psychoacoustic model (brick 4) — per-SFB masking thresholds drive per-band
// scalefactor allocation, so quantization noise is shaped to the audible floor.
// ---------------------------------------------------------------------------

/// Traunmüller's Hz→Bark (critical-band rate).
fn hz_to_bark(f: f64) -> f64 {
    let f = f.max(1.0);
    26.81 * f / (1960.0 + f) - 0.53
}

/// Spreading of a masker's energy to a maskee `dz` Bark away (`dz` = maskee −
/// masker): steep ~27 dB/Bark toward lower bands, gentle ~10 dB/Bark upward.
fn spreading(dz: f64) -> f64 {
    let slope_db = if dz >= 0.0 { -10.0 } else { -27.0 };
    10f64.powf(slope_db * dz.abs() / 10.0)
}

/// The Bark-spreading matrix `S[i·n+j] = spreading(bark[i]-bark[j])` for one band
/// geometry — fixed per (`sample_rate`, band count), so it's built once and cached,
/// turning each frame's masking into a matrix-vector product (no runtime powf).
/// `n_coeffs` is the number of spectral coefficients the band table spans — 1024
/// for a long block, 128 for one short window. It sets the coefficient→Hz
/// mapping, so passing the long value for a short block would place every band
/// eight octaves too low and spread the mask against the wrong neighbours.
fn spreading_matrix(swb: &[u16], sample_rate: u32, n_coeffs: usize) -> Arc<Vec<f64>> {
    type Cache = Mutex<HashMap<(u32, usize, usize), Arc<Vec<f64>>>>;
    static CACHE: OnceLock<Cache> = OnceLock::new();
    let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
    let key = (sample_rate, swb.len(), n_coeffs);
    let mut c = cache.lock().unwrap();
    if let Some(m) = c.get(&key) {
        return m.clone();
    }
    let num_swb = swb.len() - 1;
    let bark: Vec<f64> = (0..num_swb)
        .map(|sfb| {
            let center = f64::midpoint(f64::from(swb[sfb]), f64::from(swb[sfb + 1]));
            // n_coeffs coefficients span 0..sr/2.
            hz_to_bark(center * f64::from(sample_rate) * 0.5 / n_coeffs as f64)
        })
        .collect();
    let mut mat = vec![0f64; num_swb * num_swb];
    for i in 0..num_swb {
        for j in 0..num_swb {
            mat[i * num_swb + j] = spreading(bark[i] - bark[j]);
        }
    }
    let arc = Arc::new(mat);
    c.insert(key, arc.clone());
    arc
}

/// Per-SFB masking threshold (energy units): spread the band energies on the
/// Bark scale, then sit the mask a fixed ratio below the spread signal.
///
/// `pub(crate)` so [`crate::lab::quality`] can score coding noise against the
/// encoder's own mask (the NMR metric), in the same band geometry and energy
/// domain the encoder allocates in.
#[cfg(any(test, feature = "lab"))]
pub(crate) fn masking_thresholds(spec: &[f32], swb: &[u16], sample_rate: u32) -> Vec<f64> {
    let num_swb = swb.len() - 1;
    let mut energy = vec![0.0f64; num_swb];
    for sfb in 0..num_swb {
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        energy[sfb] = spec[s..e]
            .iter()
            .map(|&x| f64::from(x).powi(2))
            .sum::<f64>()
            + 1e-3;
    }
    masking_from_energy(&energy, swb, sample_rate, FRAME_LEN, None)
}

/// The default signal-to-mask ratio: the mask sits 18 dB below the spread signal,
/// for every band and every kind of content.
///
/// **Arm A2** replaces this constant with a function of tonality. It is census
/// category 4 — a knob whose own doc-comment names the physical signal it tracks,
/// shipping as a constant. Kept as the byte-identical fallback.
const SMR_FLAT: f64 = 0.0158; // 10^(-18/10)

/// The psychoacoustic arms in flight, threaded down to every coding path.
///
/// A struct rather than loose bools because these arms compose: A1 decides
/// *whether* short blocks get a mask at all, A2 decides *what* that mask is, and
/// both must reach the CPE path and the SCE path identically or the two paths
/// silently encode to different models — the inconsistency great-gate's P0.9
/// hygiene batch exists to prevent.
///
/// `PsyCfg::default()` is all-off = the encoder exactly as it shipped.
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct PsyCfg {
    /// Arm A1 — psy model on short blocks (else flat scalefactors).
    pub short_block_psy: bool,
    /// Arm 1a — real window grouping on short blocks, with the short-block psy
    /// model run per group and centred across groups.
    pub window_grouping: bool,
    /// Arm A2 — tonality-adaptive SMR (else flat 18 dB).
    pub tonality_smr: bool,
    /// Arm A3 — emit TNS on long blocks.
    pub tns: bool,
    /// Arm A6 — Perceptual Noise Substitution.
    pub pns: bool,
    /// Arm A7 — intensity stereo.
    pub intensity: bool,
    /// Arm A13 — demand-proportional stereo bit split.
    pub stereo_bit_split: bool,
}

/// Per-band masking threshold from precomputed band energies.
///
/// Split out from [`masking_thresholds`] so short blocks (whose band energies are
/// summed across the eight windows of a group) and long blocks share one masking
/// model — great-gate P1: consolidate duplicated probe skeletons into one path,
/// or a fit spanning both learns the inconsistency rather than the content.
///
/// `tonality` is **arm A2**: `None` uses the flat 18 dB [`SMR_FLAT`] (the shipped
/// behavior, byte-identical); `Some(t)` interpolates the classic tonal/noise
/// masker offsets per band.
fn masking_from_energy(
    energy: &[f64],
    swb: &[u16],
    sample_rate: u32,
    n_coeffs: usize,
    tonality: Option<&[f32]>,
) -> Vec<f64> {
    let num_swb = swb.len() - 1;
    let mat = spreading_matrix(swb, sample_rate, n_coeffs);
    (0..num_swb)
        .map(|i| {
            let row = &mat[i * num_swb..(i + 1) * num_swb];
            let spread: f64 = (0..num_swb).map(|j| energy[j] * row[j]).sum();
            let smr = match tonality {
                None => SMR_FLAT,
                Some(t) => {
                    let center = f64::midpoint(f64::from(swb[i]), f64::from(swb[i + 1]));
                    let bark = hz_to_bark(center * f64::from(sample_rate) * 0.5 / n_coeffs as f64);
                    smr_for(f64::from(t[i]), bark)
                }
            };
            spread * smr
        })
        .collect()
}

/// **Arm A2.** Signal-to-mask ratio as a function of band tonality and Bark.
///
/// The classic asymmetry: a *tonal* masker hides noise poorly, so the mask must
/// sit far below it (≈ 14.5 + bark dB); a *noise-like* masker hides noise well
/// (≈ 5.5 dB). Interpolating on the tonality index is the standard MPEG form.
///
/// Today's encoder applies 18 dB to everything, so a pure tone and band-limited
/// noise at equal energy get identical treatment — which is what this replaces.
///
/// Returned as a linear energy ratio (`10^(−dB/10)`), matching [`SMR_FLAT`].
///
/// # The noise end is 5.5 dB — and pinning it to 18 dB was measured and REFUTED
///
/// The obvious-looking "safety" tweak is to pin the noise end to the shipped flat
/// 18 dB, so arm A2 can only ever give a band *more* protection than today and
/// never less. Measured on the corpus, that made things **worse**, not safer:
///
/// | noise end | mean Δaudible% | worst class |
/// |---|---|---|
/// | 5.5 dB (textbook) | **−0.312** | +0.720 |
/// | 18.0 dB (pinned) | −0.006 | **+2.185** |
///
/// The reason is that an SMR pair is a **bit-allocation balance, not two
/// independent protections**. Raising the tonal end buys precision only because
/// lowering the noise end frees the bits to pay for it. Pin the noise end and the
/// tonal end becomes pure extra demand against a fixed budget — the rate loop
/// answers by lifting the global base, and *every* band gets coarser. The
/// "conservative" variant regressed three times as hard as the one it was meant
/// to protect against.
///
/// Keep the textbook pair. A2's remaining per-class failures are a **dispatch**
/// problem (route the arm by content class), not a constant to retune.
const SMR_NOISE_DB: f64 = 5.5;
fn smr_for(tonality: f64, bark: f64) -> f64 {
    let t = tonality.clamp(0.0, 1.0);
    let db = t * (14.5 + bark) + (1.0 - t) * SMR_NOISE_DB;
    // Clamp to a sane band: below ~5 dB the mask stops protecting anything, and
    // above ~30 dB the rate loop is asked for precision no bitrate can buy.
    let db = db.clamp(5.0, 30.0);
    10f64.powf(-db / 10.0)
}

/// Per-SFB scalefactor offsets (relative to a common base the rate loop sets):
/// higher where masking is generous, lower where noise would be audible. From
/// `noise ∝ 2^(0.375·sf)·Σ√|X|`, the sf hitting `noise = threshold` is
/// `log2(threshold/Σ√|X|)/0.375`; we normalize + clamp so deltas stay codeable.
/// Per-SFB tonality index in `[0, 1]` from the spectral flatness measure — **arm
/// A2's signal**. 0 = noise-like, 1 = pure tone.
///
/// `SFM = geometric_mean(|X|²) / arithmetic_mean(|X|²)`, in dB, through the
/// classic `min(SFM_dB / −60, 1)`.
pub(crate) fn band_tonality(spec: &[f32], swb: &[u16], nwin: usize, wlen: usize) -> Vec<f32> {
    let n = swb.len() - 1;
    let mut out = vec![0f32; n];
    for b in 0..n {
        let (s, e) = (swb[b] as usize, swb[b + 1] as usize);
        let (mut log_sum, mut lin_sum, mut cnt) = (0f64, 0f64, 0usize);
        for w in 0..nwin {
            let base = w * wlen;
            for k in s..e {
                let Some(&x) = spec.get(base + k) else {
                    continue;
                };
                // Floored well below any audible coefficient so a single exact
                // zero cannot drag the geometric mean to 0 and fake a pure tone.
                let p = (f64::from(x) * f64::from(x)).max(1e-10);
                log_sum += p.ln();
                lin_sum += p;
                cnt += 1;
            }
        }
        if cnt == 0 {
            continue;
        }
        let gm = (log_sum / cnt as f64).exp();
        let am = lin_sum / cnt as f64;
        let sfm_db = 10.0 * (gm / am.max(1e-30)).log10();
        out[b] = ((sfm_db / -60.0) as f32).clamp(0.0, 1.0);
    }
    out
}

fn perceptual_offsets(spec: &[f32], swb: &[u16], sample_rate: u32, tonality_smr: bool) -> Vec<i32> {
    let _prof = prof::scope(Stage::EncPsy);
    let num_swb = swb.len() - 1;
    let mut energy = vec![0.0f64; num_swb];
    for sfb in 0..num_swb {
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        energy[sfb] = spec[s..e]
            .iter()
            .map(|&x| f64::from(x).powi(2))
            .sum::<f64>()
            + 1e-3;
    }
    let ton = tonality_smr.then(|| band_tonality(spec, swb, 1, FRAME_LEN));
    let thr = masking_from_energy(&energy, swb, sample_rate, FRAME_LEN, ton.as_deref());

    let mut raw = vec![0.0f64; num_swb];
    for sfb in 0..num_swb {
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        energy[sfb] = spec[s..e].iter().map(|&x| f64::from(x).powi(2)).sum();
        let noise_scale: f64 = spec[s..e]
            .iter()
            .map(|&x| f64::from(x.abs()).sqrt())
            .sum::<f64>()
            + 1e-6;
        raw[sfb] = (thr[sfb] / noise_scale).log2() / 0.375;
    }
    // Center on the energy-bearing bands — empty bands have huge `thr/noise_scale`
    // (they quantize to ZERO anyway) and would otherwise skew a plain mean.
    let etot: f64 = energy.iter().sum::<f64>() + 1e-9;
    let center: f64 = (0..num_swb).map(|i| raw[i] * energy[i]).sum::<f64>() / etot;
    raw.iter()
        .map(|&r| ((r - center).round() as i32).clamp(-60, 60))
        .collect()
}

// ---------------------------------------------------------------------------
// Quantization + coding — brick 4: per-band scalefactors (psy-driven), cheapest
// codebook per band, rate loop over the common base, raw_data_block assembly.
// ---------------------------------------------------------------------------

/// The Rung 0 gate's transient-veto threshold, as a percentile of the clip's own
/// frame-attack distribution. Frames above it always take the sine window.
///
/// PROVISIONAL. 0.75 is a placeholder pending a calculator fit; it is set high
/// (veto only the top quartile) so the veto is conservative — it gives up KBD on
/// some stationary frames rather than risk smearing an onset.
const SHAPE_ATTACK_VETO_PCT: f32 = 0.75;

/// Arm A13's routing threshold: how correlated a channel pair must be before the
/// joint rate loop replaces the even per-channel split. Sits in the measured gap
/// between wide content (0.25, where joint loses) and real stereo music
/// (0.47-0.84, where it wins).
const JOINT_STEREO_MIN_CORR: f32 = 0.35;

/// Spectral correlation of a channel pair, `|<L,R>| / sqrt(<L,L><R,R>)`.
/// 1 = identical, 0 = orthogonal. O(n) over the spectra the caller already has.
fn pair_correlation(l: &[f32], r: &[f32]) -> f32 {
    let n = l.len().min(r.len());
    let (mut dot, mut el, mut er) = (0f64, 0f64, 0f64);
    for i in 0..n {
        let (a, b) = (f64::from(l[i]), f64::from(r[i]));
        dot += a * b;
        el += a * a;
        er += b * b;
    }
    if el <= 1e-9 || er <= 1e-9 {
        return 1.0; // a silent channel is trivially "agreeable"
    }
    (dot.abs() / (el.sqrt() * er.sqrt())) as f32
}

const ID_SCE: u32 = 0;
const ID_CPE: u32 = 1;
const ID_LFE: u32 = 3;
const ID_END: u32 = 7;
const ZERO_HCB: u8 = 0;
const ESC_HCB: u8 = 11;

// ---------------------------------------------------------------------------
// Channel element layout (ISO 14496-3 Table 1.19).
//
// `channel_configuration` in the ADTS header / AudioSpecificConfig does not just
// count channels — it *names the element sequence a decoder will expect*. Writing
// `channel_config = 6` and then emitting six SCEs is not "6 channels", it is a
// non-conformant stream: a conforming decoder reads SCE, CPE, CPE, LFE.
//
// Our own decoder happened to tolerate it (it appends elements in order), which
// is exactly how the bug survived — self-round-trip is not conformance.
// ---------------------------------------------------------------------------

/// One channel element to emit, naming its source channels **in input order**.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Elem {
    /// `single_channel_element` carrying input channel `.0`.
    Sce(usize),
    /// `channel_pair_element` carrying input channels `.0` (left) and `.1` (right).
    Cpe(usize, usize),
    /// `lfe_channel_element` carrying input channel `.0`. Long blocks only.
    Lfe(usize),
}

impl Elem {
    /// The 3-bit element id written to the bitstream.
    fn id(self) -> u32 {
        match self {
            Self::Sce(_) => ID_SCE,
            Self::Cpe(_, _) => ID_CPE,
            Self::Lfe(_) => ID_LFE,
        }
    }
}

/// The element sequence for `channels`, with source indices in the **standard
/// interleave order** the caller pushes (`WAVE_FORMAT_EXTENSIBLE` / FFmpeg order:
/// FL, FR, FC, LFE, BL, BR).
///
/// The reordering is the substance here. AAC orders a 5.1 stream
/// `C, L, R, Ls, Rs, LFE`; interleaved PCM arrives `L, R, C, LFE, Ls, Rs`. Emitting
/// them in arrival order would put the centre channel in the left speaker.
///
/// Returns `None` for channel counts with no defined `channel_configuration`
/// (0, or > 6): those need a `program_config_element`, which we do not yet write.
pub(crate) fn element_plan(channels: usize) -> Option<Vec<Elem>> {
    Some(match channels {
        // config 1 — mono.
        1 => vec![Elem::Sce(0)],
        // config 2 — stereo.
        2 => vec![Elem::Cpe(0, 1)],
        // config 3 — 3.0: C, L/R.
        3 => vec![Elem::Sce(2), Elem::Cpe(0, 1)],
        // config 4 — 4.0: C, L/R, rear centre.
        4 => vec![Elem::Sce(2), Elem::Cpe(0, 1), Elem::Sce(3)],
        // config 5 — 5.0: C, L/R, Ls/Rs.
        5 => vec![Elem::Sce(2), Elem::Cpe(0, 1), Elem::Cpe(3, 4)],
        // config 6 — 5.1: C, L/R, Ls/Rs, LFE.
        6 => vec![Elem::Sce(2), Elem::Cpe(0, 1), Elem::Cpe(4, 5), Elem::Lfe(3)],
        _ => return None,
    })
}

const MAX_QUANT: i32 = 8191;

/// The per-channel ICS body budget's ceiling: the 6144-bit decoder input buffer
/// per channel (ISO 14496-3 §4.5.3) less the element (7) and frame (59) overhead.
const MAX_CHANNEL_BITS: usize = 6144 - 7 - 59;

/// Input samples are clamped to ±8× full scale (NaN becomes silence) at the push
/// boundary. Real audio is untouched; hostile values (±Inf, 1e30) would otherwise
/// drive every band to maximal escape codes and a block no rate decision can
/// shrink below the decoder buffer.
pub const MAX_INPUT_AMPLITUDE: f32 = 8.0;

#[inline]
fn sanitize(x: f32) -> f32 {
    if x.is_nan() {
        0.0
    } else {
        x.clamp(-MAX_INPUT_AMPLITUDE, MAX_INPUT_AMPLITUDE)
    }
}

/// Quantize one coefficient at global gain `gg` (the tests' reference).
#[cfg(test)]
fn quantize(x: f32, gg: i32) -> i32 {
    if x == 0.0 {
        return 0;
    }
    let scale = 2f64.powf(-0.1875 * f64::from(gg - 100));
    let q = ((f64::from(x.abs()).powf(0.75) * scale).round() as i32).min(MAX_QUANT);
    if x < 0.0 {
        -q
    } else {
        q
    }
}

/// `2^(-0.1875·(sf-100))`, the quantizer scale, tabulated once over all 256 gains
/// so the rate loop never repeats the exponent.
fn scale_table() -> &'static [f64; 256] {
    static T: OnceLock<[f64; 256]> = OnceLock::new();
    T.get_or_init(|| {
        let mut t = [0f64; 256];
        for (sf, e) in t.iter_mut().enumerate() {
            *e = 2f64.powf(-0.1875 * (sf as f64 - 100.0));
        }
        t
    })
}

/// Per-magnitude coefficient bit cost (half a book-11 pair), tabulated so the rate
/// loop's *search* can price a frame in O(n) without the full codebook selection —
/// only the exact refinement does the real search.
fn coef_bits() -> &'static [u16] {
    static T: OnceLock<Vec<u16>> = OnceLock::new();
    T.get_or_init(|| {
        (0..=MAX_QUANT)
            .map(|m| (spectral_bits(ESC_HCB as usize, &[m, m]).unwrap_or(64) / 2) as u16)
            .collect()
    })
}

/// Per-coefficient `|x|^0.75` and sign, precomputed once per frame so the rate
/// loop can re-quantize at each candidate gain without repeating the 0.75-power
/// (the single hottest op after the MDCT). Bit-exact with [`quantize`].
struct Xpow {
    pow: Vec<f64>,
    sign: Vec<i32>,
}

impl Xpow {
    fn new(spec: &[f32]) -> Self {
        let _prof = prof::scope(Stage::EncXpow);
        // Both arrays are built straight into fresh capacity: every element is
        // computed, so the zero fill of a `vec![0; n]` (two per channel per
        // frame, 12 KB) was pure overhead.
        let n = spec.len();
        #[cfg(all(feature = "simd", target_arch = "x86_64"))]
        {
            if has_avx2() {
                let (mut pow, mut sign) = (Vec::with_capacity(n), Vec::with_capacity(n));
                // SAFETY: runtime AVX2 check; both buffers have capacity `n`, the
                // twin writes all `n` elements of each, and only then is the
                // length set.
                unsafe {
                    xpow_avx2_raw(spec, pow.as_mut_ptr(), sign.as_mut_ptr());
                    pow.set_len(n);
                    sign.set_len(n);
                }
                prof::count(Kernel::Xpow, true, n);
                return Self { pow, sign };
            }
        }
        prof::count(Kernel::Xpow, false, n);
        // |x|^0.75 = |x|^½·|x|^¼ = √|x|·√√|x| — two sqrts vectorize; `powf` doesn't.
        // (For x=0, pow=0 so the sign is irrelevant — the quant is 0 either way.)
        let pow = spec
            .iter()
            .map(|&x| {
                let s = f64::from(x.abs()).sqrt();
                s * s.sqrt()
            })
            .collect();
        let sign = spec.iter().map(|&x| if x < 0.0 { -1 } else { 1 }).collect();
        Self { pow, sign }
    }

    /// Max `|x|^0.75` over `[s, e)` — the no-clamp-floor input (= `(max|x|)^0.75`).
    fn max_pow(&self, s: usize, e: usize) -> f64 {
        self.pow[s..e].iter().copied().fold(0.0, f64::max)
    }

    fn len(&self) -> usize {
        self.pow.len()
    }
}

/// The cheapest representable spectral codebook for one SFB's coefficients, and
/// its bit cost. ZERO for a silent band; dim-4 books only for 4-aligned bands.
/// Books whose LAV can't hold the band's peak are skipped up front (only the
/// escape book covers arbitrarily large values).
fn best_codebook_for_band(quant: &[i32], s: usize, e: usize) -> (u8, usize) {
    let maxq = quant[s..e]
        .iter()
        .map(|&q| q.unsigned_abs())
        .max()
        .unwrap_or(0);
    if maxq == 0 {
        return (ZERO_HCB, 0);
    }
    let mut best = (ESC_HCB, usize::MAX);
    for cb in 1..=11u8 {
        let meta = &CODEBOOKS[cb as usize];
        let dim = meta.dim as usize;
        if (e - s) % dim != 0 || (!meta.esc && u32::from(meta.lav) < maxq) {
            continue;
        }
        let mut bits = 0usize;
        let mut ok = true;
        let mut i = s;
        while i < e {
            if let Some(b) = spectral_bits(cb as usize, &quant[i..i + dim]) {
                bits += b;
            } else {
                ok = false;
                break;
            }
            i += dim;
        }
        if ok && bits < best.1 {
            best = (cb, bits);
        }
    }
    best
}

/// Bits for `section_data` given per-SFB codebooks (adjacent equal cbs merge into
/// one 4-bit codebook + 5-bit run-length increments).
fn section_bits(cbs: &[u8]) -> usize {
    let esc = 31usize;
    let mut bits = 0usize;
    let mut k = 0usize;
    while k < cbs.len() {
        let cb = cbs[k];
        let mut len = 1usize;
        while k + len < cbs.len() && cbs[k + len] == cb {
            len += 1;
        }
        bits += 4;
        let mut l = len;
        while l >= esc {
            bits += 5;
            l -= esc;
        }
        bits += 5;
        k += len;
    }
    bits
}

/// Per-band scalefactors from a common `base` plus the psy offsets, each clamped
/// to the codeable range. Offsets span ≤48, so band-to-band deltas stay < 60.
fn scalefactors(offsets: &[i32], base: i32) -> Vec<i32> {
    offsets.iter().map(|&o| (base + o).clamp(0, 255)).collect()
}

/// `global_gain` = the first coded band's scalefactor (the differential reference);
/// 100 for a silent frame with no coded bands.
fn global_gain(cbs: &[u8], sf: &[i32]) -> i32 {
    // Only a REGULAR band seeds the differential chain. A PNS band's `sf` is a
    // noise-energy value on its own accumulator and an intensity band's is a
    // stereo position — seeding global_gain from either desynchronizes every
    // scalefactor that follows.
    cbs.iter()
        .position(|&cb| cb != ZERO_HCB && cb < NOISE_HCB)
        .map_or(100, |sfb| sf[sfb])
}

/// Bits to code the per-band scalefactors as `SCALEFACTOR_BOOK` deltas from a
/// running accumulator seeded at `gg`.
fn scalefactor_bits(cbs: &[u8], sf: &[i32], gg: i32) -> usize {
    let mut acc = gg;
    let mut noise = gg - 90;
    let mut noise_pcm = true;
    let mut is_pos = 0i32;
    let mut bits = 0usize;
    for (sfb, &cb) in cbs.iter().enumerate() {
        if cb == ZERO_HCB {
            continue;
        }
        if cb >= crate::codebook::INTENSITY_HCB2 {
            let d = (sf[sfb] - is_pos).clamp(-60, 60);
            bits += crate::tables::SCALEFACTOR_BOOK.code((d + 60) as usize).1 as usize;
            is_pos += d;
        } else if cb == NOISE_HCB {
            if noise_pcm {
                noise_pcm = false;
                let d = (sf[sfb] - noise).clamp(-256, 255);
                bits += 9;
                noise += d;
            } else {
                let d = (sf[sfb] - noise).clamp(-60, 60);
                bits += crate::tables::SCALEFACTOR_BOOK.code((d + 60) as usize).1 as usize;
                noise += d;
            }
        } else {
            let d = (sf[sfb] - acc).clamp(-60, 60);
            bits += crate::tables::SCALEFACTOR_BOOK.code((d + 60) as usize).1 as usize;
            acc += d;
        }
    }
    bits
}

/// Runtime AVX2 availability (detected once).
#[cfg(all(feature = "simd", target_arch = "x86_64"))]
fn has_avx2() -> bool {
    static AVX2: OnceLock<bool> = OnceLock::new();
    *AVX2.get_or_init(|| std::is_x86_feature_detected!("avx2"))
}

#[cfg(all(feature = "simd-avx512", target_arch = "x86_64"))]
fn has_avx512() -> bool {
    static AVX512: OnceLock<bool> = OnceLock::new();
    *AVX512.get_or_init(|| std::is_x86_feature_detected!("avx512f"))
}

/// Quantize one band into `out`: `out[k] = sign[k]·min(round(pow[k]·scale), MAX_QUANT)`.
/// This is the encoder's hottest kernel — the rate loop runs it ~11× per frame — so it
/// has an AVX2 path. AVX2 has no round-half-away-from-zero, so the twins use
/// `floor(v + HALF_DOWN)` with `HALF_DOWN` the double just below ½: for `v ≥ 0` that
/// equals `round(v)` exactly. (`floor(v + 0.5)` does NOT — at the double just below ½
/// the sum rounds up to 1.0 — which `quantize_simd_matches_scalar` pins.)
fn quantize_band(pow: &[f64], sign: &[i32], scale: f64, out: &mut [i32]) {
    // The twins read `pow`/`sign` for every index of `out`; re-slicing here makes
    // that bound local (a short input panics instead of reading past its end).
    let (pow, sign) = (&pow[..out.len()], &sign[..out.len()]);
    #[cfg(all(feature = "simd-avx512", target_arch = "x86_64"))]
    if has_avx512() {
        // SAFETY: AVX-512F + AVX2 detected at runtime; the three slices have the
        // same length (re-sliced above), which is all `quantize_band_avx512` needs.
        unsafe { quantize_band_avx512(pow, sign, scale, out) };
        prof::count(Kernel::Quantize, true, out.len());
        return;
    }
    #[cfg(all(feature = "simd", target_arch = "x86_64"))]
    if has_avx2() {
        // SAFETY: AVX2 detected at runtime; the three slices have the same length
        // (re-sliced above), which is all `quantize_band_avx2` needs.
        unsafe { quantize_band_avx2(pow, sign, scale, out) };
        prof::count(Kernel::Quantize, true, out.len());
        return;
    }
    prof::count(Kernel::Quantize, false, out.len());
    quantize_band_scalar(pow, sign, scale, out);
}

/// The largest double below ½ (`0.5 − 2⁻⁵⁴`). Adding it then flooring rounds half
/// away from zero for non-negative input without the 0.49999999999999994 overshoot.
#[cfg(all(feature = "simd", target_arch = "x86_64"))]
const HALF_DOWN: f64 = 0.499_999_999_999_999_94;

fn quantize_band_scalar(pow: &[f64], sign: &[i32], scale: f64, out: &mut [i32]) {
    for k in 0..out.len() {
        let q = ((pow[k] * scale).round() as i32).min(MAX_QUANT);
        out[k] = sign[k] * q;
    }
}

/// AVX2 quantize — four coefficients per iteration. Bit-exact with the scalar path
/// (`floor(v + HALF_DOWN) == round(v)` for `v ≥ 0`, and the clamp is applied before
/// the f64→i32 narrowing so nothing overflows).
///
/// # Safety
///
/// AVX2 must be available, and `pow.len()` and `sign.len()` must be at least
/// `out.len()`: the vector body reads `pow[i..i + 4]` / `sign[i..i + 4]` and
/// writes `out[i..i + 4]` for `i + 4 <= out.len()`; the tail is safe indexing.
#[cfg(all(feature = "simd", target_arch = "x86_64"))]
#[target_feature(enable = "avx2")]
unsafe fn quantize_band_avx2(pow: &[f64], sign: &[i32], scale: f64, out: &mut [i32]) {
    use std::arch::x86_64::*;
    let n = out.len();
    let vscale = _mm256_set1_pd(scale);
    let vhalf = _mm256_set1_pd(HALF_DOWN);
    let vmax = _mm256_set1_pd(f64::from(MAX_QUANT));
    let mut i = 0;
    while i + 4 <= n {
        let v = _mm256_mul_pd(_mm256_loadu_pd(pow.as_ptr().add(i)), vscale);
        let r = _mm256_floor_pd(_mm256_add_pd(v, vhalf)); // = round(v) for v ≥ 0
        let qabs = _mm256_cvttpd_epi32(_mm256_min_pd(r, vmax)); // clamp then f64→i32
        let s = _mm_loadu_si128(sign.as_ptr().add(i).cast::<__m128i>());
        _mm_storeu_si128(
            out.as_mut_ptr().add(i).cast::<__m128i>(),
            _mm_mullo_epi32(qabs, s),
        );
        i += 4;
    }
    while i < n {
        let q = ((pow[i] * scale).round() as i32).min(MAX_QUANT);
        out[i] = sign[i] * q;
        i += 1;
    }
}

/// AVX-512 quantize — eight coefficients per iteration (2× the AVX2 width). Bit-exact:
/// for the quantizer's always-nonnegative input, `trunc(min(v+HALF_DOWN, MAX+0.5))`
/// equals `min(round(v), MAX)`, so a single truncating narrow does round+clamp.
/// Runtime-gated above AVX2; on AVX2-only hosts this path never runs (untested there —
/// the math mirrors the AVX2 kernel exactly, and its tail is the shared scalar reference).
///
/// # Safety
///
/// AVX-512F and AVX2 must be available, and `pow.len()` and `sign.len()` must be
/// at least `out.len()`: eight lanes per trip for `i + 8 <= out.len()`, then
/// the safe scalar tail.
#[cfg(all(feature = "simd-avx512", target_arch = "x86_64"))]
#[target_feature(enable = "avx512f,avx2")]
#[allow(
    clippy::incompatible_msrv,
    reason = "the opt-in `simd-avx512` feature needs Rust 1.89 (stable AVX-512 intrinsics); the crate's 1.85 MSRV holds for every other feature set (README)"
)]
unsafe fn quantize_band_avx512(pow: &[f64], sign: &[i32], scale: f64, out: &mut [i32]) {
    use std::arch::x86_64::*;
    let n = out.len();
    let vscale = _mm512_set1_pd(scale);
    let vhalf = _mm512_set1_pd(HALF_DOWN);
    let vmaxph = _mm512_set1_pd(f64::from(MAX_QUANT) + 0.5); // clamp v+0.5 so trunc yields MAX
    let mut i = 0;
    while i + 8 <= n {
        let v = _mm512_mul_pd(_mm512_loadu_pd(pow.as_ptr().add(i)), vscale);
        let c = _mm512_min_pd(_mm512_add_pd(v, vhalf), vmaxph);
        let qabs = _mm512_cvttpd_epi32(c); // 8 f64 → 8 i32; trunc = floor for c ≥ 0
        let s = _mm256_loadu_si256(sign.as_ptr().add(i).cast::<__m256i>());
        _mm256_storeu_si256(
            out.as_mut_ptr().add(i).cast::<__m256i>(),
            _mm256_mullo_epi32(qabs, s),
        );
        i += 8;
    }
    while i < n {
        let q = ((pow[i] * scale).round() as i32).min(MAX_QUANT);
        out[i] = sign[i] * q;
        i += 1;
    }
}

/// AVX2 `Xpow` builder: `pow[i] = |x|^0.75` via `√|x|·√√|x|` (two vector sqrts), and
/// `sign[i] = ±1` from the sign bit — four coefficients per iteration.
///
/// # Safety
///
/// AVX2 must be available (the lengths are asserted).
#[cfg(all(feature = "simd", target_arch = "x86_64"))]
#[target_feature(enable = "avx2")]
#[cfg(test)]
unsafe fn xpow_avx2(spec: &[f32], pow: &mut [f64], sign: &mut [i32]) {
    assert!(pow.len() >= spec.len() && sign.len() >= spec.len());
    xpow_avx2_raw(spec, pow.as_mut_ptr(), sign.as_mut_ptr());
}

/// [`xpow_avx2`] through raw pointers, so `Xpow::new` can fill uninitialised
/// capacity.
///
/// # Safety
/// AVX2 must be available, and `pow` / `sign` must be valid for `spec.len()`
/// writes; every one of those elements is written.
#[cfg(all(feature = "simd", target_arch = "x86_64"))]
#[target_feature(enable = "avx2")]
unsafe fn xpow_avx2_raw(spec: &[f32], pow: *mut f64, sign: *mut i32) {
    use std::arch::x86_64::*;
    let n = spec.len();
    let absmask = _mm256_castsi256_pd(_mm256_set1_epi64x(0x7fff_ffff_ffff_ffffu64 as i64));
    let one = _mm256_set1_pd(1.0);
    let mut i = 0;
    while i + 4 <= n {
        let x = _mm256_cvtps_pd(_mm_loadu_ps(spec.as_ptr().add(i))); // 4 f32 → 4 f64
        let a = _mm256_and_pd(x, absmask); // |x|
        let s = _mm256_sqrt_pd(a); // |x|^½
        _mm256_storeu_pd(pow.add(i), _mm256_mul_pd(s, _mm256_sqrt_pd(s)));
        // ±1.0 carrying x's sign bit → i32 (x=0 gives +1; harmless, pow is 0).
        let signed = _mm256_or_pd(one, _mm256_andnot_pd(absmask, x));
        _mm_storeu_si128(sign.add(i).cast::<__m128i>(), _mm256_cvttpd_epi32(signed));
        i += 4;
    }
    while i < n {
        let s = f64::from(spec[i].abs()).sqrt();
        pow.add(i).write(s * s.sqrt());
        sign.add(i).write(if spec[i] < 0.0 { -1 } else { 1 });
        i += 1;
    }
}

/// **Deterministic work counters** (feature `lab`) — the PRIMARY evidence for any
/// gate's speed half, per `codec-measurement` §15.
///
/// A counter is immune to scheduler drift, needs one run, and SIZES the effect;
/// a clock at these magnitudes cannot be promoted to a verdict no matter how many
/// pairs it gets. The gate-calculator's `work` column is fed from here.
///
/// Counts the rate loop's exact-coder evaluations — the encoder's dominant inner
/// cost and precisely the quantity a psy arm moves, because changing the offsets
/// changes how many bases the loop must try.
#[cfg(feature = "lab")]
pub mod work {
    use std::sync::atomic::{AtomicU64, Ordering};

    // GLOBAL, not thread-local. `encode_stream` fans frames out across worker
    // threads, so a thread-local counter is drained on the main thread AFTER the
    // workers have exited and reads **zero** — which is exactly what the first
    // harvest reported (`WORK NULL: 0 vs 0`). A counter that reads flat is a
    // defect report, not reassurance (`codec-measurement`, order of instruments).
    static CODE_FRAME_EVALS: AtomicU64 = AtomicU64::new(0);
    static QUANT_BANDS: AtomicU64 = AtomicU64::new(0);

    #[inline]
    pub(crate) fn bump_code_frame() {
        CODE_FRAME_EVALS.fetch_add(1, Ordering::Relaxed);
    }

    #[inline]
    pub(crate) fn bump_quant_bands(n: u64) {
        QUANT_BANDS.fetch_add(n, Ordering::Relaxed);
    }

    /// Read and clear both counters.
    ///
    /// `Relaxed` is sufficient: these are pure counters, never used to order
    /// other memory, and the harvest reads them only after `finish()` has joined
    /// every worker — so the joins provide the happens-before edge.
    pub fn take() -> (u64, u64) {
        (
            CODE_FRAME_EVALS.swap(0, Ordering::Relaxed),
            QUANT_BANDS.swap(0, Ordering::Relaxed),
        )
    }
}

#[cfg(not(feature = "lab"))]
mod work {
    #[inline]
    pub(crate) fn bump_code_frame() {}
    #[inline]
    pub(crate) fn bump_quant_bands(_: u64) {}
}

/// Shared coder body: quantize the frame into the caller-owned `quant` (per-band
/// scalefactors `sf`, which cover the whole spectrum so no pre-zeroing is needed),
/// pick per-band codebooks, and return (codebooks, ICS body bits, `max_sfb`). Splitting
/// this out lets the rate loop reuse one buffer instead of allocating per candidate.
fn code_core(xp: &Xpow, swb: &[u16], sf: &[i32], quant: &mut [i32]) -> (Vec<u8>, usize, usize) {
    work::bump_code_frame();
    let num_swb = swb.len() - 1;
    work::bump_quant_bands((swb.len() - 1) as u64);
    let scale = scale_table();
    let prof_q = prof::scope(Stage::EncQuant);
    for sfb in 0..num_swb {
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        let sc = scale[sf[sfb].clamp(0, 255) as usize];
        quantize_band(&xp.pow[s..e], &xp.sign[s..e], sc, &mut quant[s..e]);
    }
    drop(prof_q);
    let _prof_cb = prof::scope(Stage::EncCodebook);
    let mut max_sfb = 0usize;
    for sfb in 0..num_swb {
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        if quant[s..e].iter().any(|&q| q != 0) {
            max_sfb = sfb + 1;
        }
    }
    let mut cbs = Vec::with_capacity(max_sfb);
    let mut spec_bits = 0usize;
    for sfb in 0..max_sfb {
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        let (cb, bits) = best_codebook_for_band(quant, s, e);
        cbs.push(cb);
        spec_bits += bits;
    }
    let gg = global_gain(&cbs, sf);
    // global_gain(8) + ics_info(~11) + 3 flag bits + sections + scalefactors + spectrum.
    let body = 8 + 11 + 3 + section_bits(&cbs) + scalefactor_bits(&cbs, sf, gg) + spec_bits;
    (cbs, body, max_sfb)
}

/// Owned-buffer wrapper over [`code_core`] for the one-shot callers (final coding).
fn code_frame(xp: &Xpow, swb: &[u16], sf: &[i32]) -> (Vec<u8>, usize, usize, Vec<i32>) {
    let mut quant = vec![0i32; xp.len()];
    let (cbs, body, max_sfb) = code_core(xp, swb, sf, &mut quant);
    (cbs, body, max_sfb, quant)
}

/// The smallest common `base` such that no band clamps its loudest coefficient
/// past `MAX_QUANT` (each band's own no-clamp floor, minus that band's offset).
fn min_base(xp: &Xpow, swb: &[u16], offsets: &[i32]) -> i32 {
    let num_swb = swb.len() - 1;
    let mut floor = 0i32;
    for sfb in 0..num_swb {
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        let maxp = xp.max_pow(s, e); // = (max|x|)^0.75
        if maxp <= 1e-9 {
            continue;
        }
        let min_sf = (100.0 - (f64::from(MAX_QUANT) / maxp).log2() / 0.1875).ceil() as i32;
        floor = floor.max(min_sf - offsets[sfb]);
    }
    floor.clamp(0, 255)
}

/// Fast body-bit estimate at a candidate `base`: quantize (cheap) and price each
/// coefficient from the `coef_bits` table — no codebook search. Monotone in `base`,
/// so it seeds the search; the exact refinement corrects it.
fn estimate_bits(xp: &Xpow, swb: &[u16], offsets: &[i32], base: i32) -> usize {
    let _prof = prof::scope(Stage::EncEstimate);
    let scale = scale_table();
    let ct = coef_bits();
    let num_swb = swb.len() - 1;
    let mut bits = 22usize; // global_gain + ics_info + flags, approx
    let mut qbuf = [0i32; 128]; // one band (max long SWB width is 96)
    for sfb in 0..num_swb {
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        let w = e - s;
        let sc = scale[(base + offsets[sfb]).clamp(0, 255) as usize];
        quantize_band(&xp.pow[s..e], &xp.sign[s..e], sc, &mut qbuf[..w]);
        let mut band = 0usize;
        let mut nonzero = false;
        for &q in &qbuf[..w] {
            let m = q.unsigned_abs() as usize;
            if m != 0 {
                nonzero = true;
                band += ct[m] as usize;
            }
        }
        if nonzero {
            bits += 9 + band; // ~section + scalefactor + spectrum for the band
        }
    }
    bits
}

/// The smallest common `base` (≥ the no-clamp floor) whose ICS body fits
/// `target_bits` — finest quality within budget. A fast estimate seeds the search;
/// the exact coder then walks to the true boundary (identical to a full search, but
/// only a couple of exact evaluations instead of eight). Body bits fall as `base`
/// rises.
fn rate_loop(xp: &Xpow, swb: &[u16], offsets: &[i32], target_bits: usize) -> i32 {
    let min_b = min_base(xp, swb, offsets);
    // Phase 1: binary-search the cheap estimate.
    let (mut lo, mut hi) = (min_b, 255i32);
    while lo < hi {
        let mid = (lo + hi) / 2;
        if estimate_bits(xp, swb, offsets, mid) <= target_bits {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }
    // Phase 2: refine to the exact smallest fitting base with the real coder, reusing
    // one quant buffer across the (few) exact evaluations.
    let mut quant = vec![0i32; xp.len()];
    let mut exact =
        |b: i32| code_core(xp, swb, &scalefactors(offsets, b), &mut quant).1 <= target_bits;
    let mut base = lo;
    if exact(base) {
        while base > min_b && exact(base - 1) {
            base -= 1;
        }
    } else {
        while base < 255 && !exact(base) {
            base += 1;
        }
    }
    base
}

/// `section_data` (single group, long block): 4-bit codebook + 5-bit run-length
/// increments (esc = 31 continues the run).
fn write_sections(w: &mut BitWriter, cbs: &[u8]) {
    let esc = 31u32;
    let mut k = 0usize;
    while k < cbs.len() {
        let cb = cbs[k];
        let mut len = 1usize;
        while k + len < cbs.len() && cbs[k + len] == cb {
            len += 1;
        }
        w.write(u32::from(cb), 4);
        let mut l = len as u32;
        while l >= esc {
            w.write(esc, 5);
            l -= esc;
        }
        w.write(l, 5);
        k += len;
    }
}

/// `scale_factor_data`: each coded band's scalefactor as a `SCALEFACTOR_BOOK` delta
/// from a running accumulator seeded at `gg` (matching the decoder).
fn write_scalefactors(w: &mut BitWriter, cbs: &[u8], sf: &[i32], gg: i32) {
    // THREE independent accumulators, exactly as `decode::read_scalefactors`
    // maintains them. Folding PNS or intensity values into the regular chain
    // desynchronizes every later band, which decodes as garbage rather than as a
    // slightly wrong gain.
    let mut acc = gg;
    let mut noise = gg - 90;
    let mut noise_pcm = true;
    let mut is_pos = 0i32;
    for (sfb, &cb) in cbs.iter().enumerate() {
        if cb == ZERO_HCB {
            continue;
        }
        if cb >= crate::codebook::INTENSITY_HCB2 {
            let d = (sf[sfb] - is_pos).clamp(-60, 60);
            let (code, len) = crate::tables::SCALEFACTOR_BOOK.code((d + 60) as usize);
            w.write(code, u32::from(len));
            is_pos += d;
        } else if cb == NOISE_HCB {
            if noise_pcm {
                // The first PNS band is a 9-bit PCM delta, not a Huffman one.
                noise_pcm = false;
                let d = (sf[sfb] - noise).clamp(-256, 255);
                w.write((d + 256) as u32, 9);
                noise += d;
            } else {
                let d = (sf[sfb] - noise).clamp(-60, 60);
                let (code, len) = crate::tables::SCALEFACTOR_BOOK.code((d + 60) as usize);
                w.write(code, u32::from(len));
                noise += d;
            }
        } else {
            let d = (sf[sfb] - acc).clamp(-60, 60);
            let (code, len) = crate::tables::SCALEFACTOR_BOOK.code((d + 60) as usize);
            w.write(code, u32::from(len));
            acc += d;
        }
    }
}

/// **Arm A6 — Perceptual Noise Substitution.**
///
/// Replace a noise-like band's coefficients with a single transmitted *energy*;
/// the decoder refills it with scaled random noise (`decode::fill_noise`). The
/// band costs a scalefactor instead of a spectrum, which at low rates is the
/// difference between coding a band and zeroing it.
///
/// The P0 baseline is the motivation: `speech-noisy` and `noise-like` sit at
/// 99%+ audible from 64-128k and only clear at 192k — broadband noise has no
/// maskers to hide under and is unaffordable, which is exactly the problem PNS
/// exists to solve.
///
/// The gate is deliberately conservative on all three axes, because PNS destroys
/// phase and is audibly destructive on a harmonic band (the `music-tonal`
/// anti-class):
///
/// * **frequency** — only above `PNS_MIN_HZ`; PNS on the bass is never right,
/// * **tonality** — only bands the SFM calls noise-like,
/// * **energy** — only bands actually carrying signal.
///
/// Returns the noise-energy value to transmit for each substituted band.
/// `fill_noise` produces band energy `2^(ne/2)`, so `ne = 2*log2(E)`.
fn pns_bands(
    spec: &[f32],
    swb: &[u16],
    sample_rate: u32,
    tonality: &[f32],
    max_sfb: usize,
) -> Vec<Option<i32>> {
    const PNS_MIN_HZ: f64 = 4000.0;
    const PNS_MAX_TONALITY: f32 = 0.10;
    /// A frame this tonal overall gets NO PNS at all, whatever individual bands
    /// say. See the band-level trap below.
    const PNS_MAX_FRAME_TONALITY: f32 = 0.15;
    /// A band must carry at least this fraction of the frame's MEAN band energy
    /// to be worth substituting — population-relative (law 1), not absolute.
    const PNS_MIN_ENERGY_FRAC: f64 = 0.5;

    let num_swb = swb.len() - 1;
    let mut out = vec![None; num_swb];

    // Band energies once, and the frame's energy-weighted tonality.
    let mut energies = vec![0f64; num_swb];
    for sfb in 0..num_swb {
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        energies[sfb] = spec[s..e.min(spec.len())]
            .iter()
            .map(|&x| f64::from(x) * f64::from(x))
            .sum();
    }
    let total: f64 = energies.iter().sum::<f64>() + 1e-9;
    let frame_tonality = (0..num_swb)
        .map(|i| energies[i] * f64::from(tonality[i]))
        .sum::<f64>()
        / total;

    // **The band-level trap, measured.** A near-EMPTY band has a flat spectrum,
    // so its spectral flatness measure reads as maximally *noise-like* — SFM
    // cannot distinguish "broadband noise" from "almost nothing". A band-only
    // gate therefore substitutes noise into the empty bands surrounding a pure
    // tone, which is how the first version of this fired on a 6 kHz sine.
    //
    // Two guards, because they fail in different directions: the frame-level veto
    // refuses tonal CONTENT outright, and the relative energy floor refuses
    // individual bands with nothing in them.
    if frame_tonality as f32 > PNS_MAX_FRAME_TONALITY {
        return out;
    }
    let mean_band = total / num_swb as f64;

    for sfb in 0..num_swb.min(max_sfb) {
        let lo_hz = f64::from(swb[sfb]) * f64::from(sample_rate) * 0.5 / FRAME_LEN as f64;
        if lo_hz < PNS_MIN_HZ || tonality[sfb] > PNS_MAX_TONALITY {
            continue;
        }
        let energy = energies[sfb];
        if energy <= 1.0 || energy < mean_band * PNS_MIN_ENERGY_FRAC {
            continue; // nothing meaningful there to substitute
        }
        let ne = (2.0 * energy.log2()).round() as i32;
        out[sfb] = Some(ne.clamp(-100, 255));
    }
    out
}

/// `spectral_data`: per regular SFB, code coefficient tuples with the band's book.
fn write_spectrum(w: &mut BitWriter, quant: &[i32], cbs: &[u8], swb: &[u16]) {
    for (sfb, &cb) in cbs.iter().enumerate() {
        // ZERO, NOISE (PNS) and intensity bands carry no spectral data — their
        // whole point is that the coefficients are not transmitted.
        if cb == ZERO_HCB || cb >= NOISE_HCB {
            continue;
        }
        let dim = CODEBOOKS[cb as usize].dim as usize;
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        let mut i = s;
        while i + dim <= e {
            spectral_emit(cb as usize, &quant[i..i + dim], w);
            i += dim;
        }
    }
}

/// Encode one channel as a `single_channel_element` for a long or transition block
/// (`seq` ∈ {`OnlyLong`, `LongStart`, LongStop}): psy offsets → rate loop over the
/// common base → per-band scalefactors → coded ICS.
#[allow(clippy::too_many_arguments)]
fn encode_channel_element(
    w: &mut BitWriter,
    tag: u32,
    spec: &[f32],
    swb: &[u16],
    seq: WindowSequence,
    sample_rate: u32,
    target_bits: usize,
    cur_kbd: bool,
    psy: PsyCfg,
    elem_id: u32,
) {
    // Pass 1, un-filtered: the TNS span depends on max_sfb, which only exists
    // after quantization. This pass exists to learn it.
    let offsets0 = perceptual_offsets(spec, swb, sample_rate, psy.tonality_smr);
    let xp0 = Xpow::new(spec);
    let base0 = rate_loop(&xp0, swb, &offsets0, target_bits);
    let sf0 = scalefactors(&offsets0, base0);
    let (cbs0, _, max_sfb0, quant0) = code_frame(&xp0, swb, &sf0);

    // Arm A3. Only engage when the coded range already reaches TNS_MAX_LONG, so
    // the decoder's span `min(TNS_MAX_LONG, max_sfb)` is pinned to TNS_MAX_LONG
    // on BOTH sides regardless of what the re-quantization does to max_sfb. The
    // alternative (span depending on a max_sfb that pass 2 can move) is a
    // bitstream desync waiting to happen.
    let fs_index = crate::sf_index_for_rate(sample_rate).unwrap_or(4);
    let tns_max = crate::decode::TNS_MAX_LONG[fs_index as usize] as usize;
    let mut tns: Option<TnsEnc> = None;
    // ARM A3 ROUTING GATE. Spectral LPC prediction gain alone is NOT a transient
    // detector: a peaky TONAL spectrum predicts just as well as an impulsive one,
    // so a gain-only gate fires on sustained content, where temporal noise
    // shaping has nothing to shape and simply costs bits. Measured (PEAQ, vs the
    // previous default): tonal -1.02, mixed -1.14, quiet -1.05, while percussive
    // moved only -0.01. It was firing almost exclusively on the wrong content.
    //
    // The fix is to require the frame to be transient-ADJACENT: only the
    // LongStart / LongStop frames that bracket a detected attack. Those are
    // exactly the long blocks that carry pre-echo, and the window sequence
    // already names them, so the gate is free.
    let tns_frame_ok = seq != WindowSequence::OnlyLong;
    let (cbs, sf, max_sfb, quant) = if psy.tns && tns_frame_ok && max_sfb0 >= tns_max {
        let mut filtered = spec.to_vec();
        match tns_analyze_long(&mut filtered, swb, fs_index, max_sfb0) {
            Some(t) => {
                let offsets = perceptual_offsets(&filtered, swb, sample_rate, psy.tonality_smr);
                let xp = Xpow::new(&filtered);
                let base = rate_loop(&xp, swb, &offsets, target_bits);
                let sf = scalefactors(&offsets, base);
                let (mut cbs, _, msfb, quant) = code_frame(&xp, swb, &sf);
                // Hold max_sfb at or above tns_max, padding with ZERO bands, so
                // the decoder's TNS span is exactly the one we filtered.
                let msfb = msfb.max(tns_max);
                cbs.resize(msfb, ZERO_HCB);
                tns = Some(t);
                (cbs, sf, msfb, quant)
            }
            None => (cbs0, sf0, max_sfb0, quant0),
        }
    } else {
        (cbs0, sf0, max_sfb0, quant0)
    };
    // Arm A6 — PNS. Substituted bands trade their spectrum for one energy value.
    //
    // Skipped when TNS fired: the decoder fills noise BEFORE inverse-filtering,
    // so with TNS active the transmitted energy would have to be expressed in the
    // filtered domain. That combination is not yet validated, and a wrong energy
    // there is not a small error - it is a band of noise at the wrong level.
    let (mut cbs, mut sf) = (cbs, sf);
    if psy.pns && tns.is_none() {
        let ton = band_tonality(spec, swb, 1, FRAME_LEN);
        for (b, ne) in pns_bands(spec, swb, sample_rate, &ton, max_sfb)
            .into_iter()
            .enumerate()
        {
            if let (Some(ne), Some(cb)) = (ne, cbs.get_mut(b)) {
                if *cb != ZERO_HCB && *cb < NOISE_HCB {
                    *cb = NOISE_HCB;
                    sf[b] = ne;
                }
            }
        }
    }
    let gg = global_gain(&cbs, &sf);

    w.write(elem_id, 3);
    w.write(tag, 4);
    w.write(gg as u32, 8);
    let info = IcsInfo {
        window_sequence: seq,
        window_shape_kbd: cur_kbd,
        max_sfb: max_sfb as u8,
        num_windows: 1,
        num_window_groups: 1,
        window_group_length: vec![1],
        num_swb: swb.len() - 1,
    };
    encode_ics_info(w, &info);
    write_sections(w, &cbs);
    write_scalefactors(w, &cbs, &sf, gg);
    w.write(0, 1); // pulse_data_present
    match &tns {
        Some(t) => {
            w.write(1, 1); // tns_data_present
            write_tns_long(w, t);
        }
        None => w.write(0, 1),
    }
    w.write(0, 1); // gain_control_data_present
    write_spectrum(w, &quant, &cbs, swb);
}

// ---------------------------------------------------------------------------
// Arm A3 (Rung 3) — Temporal Noise Shaping.
//
// The AAC tool for pre-echo, and a large part of why AAC beats MP3 on speech.
// The decoder has implemented it since day one (`decode::apply_tns`); the encoder
// emitted `tns_data_present = 0` at all three sites.
//
// The decoder runs an ALL-POLE (synthesis) filter over the spectrum:
//     orig[n] = resid[n] - SUM_k lpc[k] * orig[n-k]
// so the encoder must run the matching ALL-ZERO (analysis) filter:
//     resid[n] = orig[n] + SUM_k lpc[k] * orig[n-k]
// with the state holding past *inputs* (the original spectrum), not past outputs.
// Getting that inversion backwards does not sound slightly worse; it diverges.
// ---------------------------------------------------------------------------

/// One TNS filter the encoder decided to apply, in emit form.
#[derive(Debug, Clone)]
pub(crate) struct TnsEnc {
    /// Span in scalefactor bands, counted down from the top.
    pub length: usize,
    pub order: usize,
    /// Quantized PARCOR indices (4-bit two's complement, `coef_res = 1`).
    pub idx: Vec<i32>,
}

/// Coefficient resolution we emit: `coef_res = 1` (4-bit), `coef_compress = 0`.
/// The widest grid AAC-LC offers, so quantization error in the filter itself is
/// as small as the syntax allows.
const TNS_COEF_RES: u32 = 1;
const TNS_COEF_BITS: u32 = 4;
/// Max order for AAC-LC long blocks (the syntax allows 5 bits; 12 is the ISO cap).
const TNS_MAX_ORDER: usize = 12;
/// Prediction gain below which TNS is not worth its bits. PROVISIONAL — the
/// classic value; the calculator has not fitted it.
const TNS_MIN_GAIN: f64 = 1.4;

/// Quantize one PARCOR value to the 4-bit grid and return `(index, dequantized)`.
///
/// Exact inverse of the decoder's dequantizer, including its **asymmetric**
/// scale factors (`iqfac` for non-negative, `iqfac_m` for negative) — a single
/// shared factor would round half the coefficients to the wrong index.
fn quantize_parcor(p: f32) -> (i32, f32) {
    use std::f32::consts::PI;
    let res_bits = 3 + TNS_COEF_RES;
    let iqfac = ((1i32 << (res_bits - 1)) as f32 - 0.5) / (PI / 2.0);
    let iqfac_m = ((1i32 << (res_bits - 1)) as f32 + 0.5) / (PI / 2.0);
    let t = p.clamp(-0.999, 0.999).asin();
    let lo = -(1i32 << (TNS_COEF_BITS - 1));
    let hi = (1i32 << (TNS_COEF_BITS - 1)) - 1;
    let c = if t >= 0.0 {
        (t * iqfac).round() as i32
    } else {
        (t * iqfac_m).round() as i32
    }
    .clamp(lo, hi);
    // Reconstruct exactly as the decoder will.
    let t_back = if c >= 0 {
        c as f32 / iqfac
    } else {
        c as f32 / iqfac_m
    };
    (c, t_back.sin())
}

/// PARCOR (reflection coefficients) -> LPC, matching `decode::parcor_to_lpc`.
fn parcor_to_lpc_enc(parcor: &[f32]) -> Vec<f32> {
    let order = parcor.len();
    let mut lpc = vec![0f32; order + 1];
    lpc[0] = 1.0;
    for m in 1..=order {
        let mut tmp = lpc.clone();
        for i in 1..m {
            tmp[i] = lpc[i] + parcor[m - 1] * lpc[m - i];
        }
        lpc[..m].copy_from_slice(&tmp[..m]);
        lpc[m] = parcor[m - 1];
    }
    lpc
}

/// Levinson-Durbin over `spec[start..end]`, returning `(parcor, prediction_gain)`.
fn spectral_parcor(spec: &[f32], start: usize, end: usize, order: usize) -> (Vec<f32>, f64) {
    let n = end.saturating_sub(start);
    if n <= order + 1 {
        return (Vec::new(), 1.0);
    }
    let x = &spec[start..end];
    let mut r = vec![0f64; order + 1];
    for (lag, slot) in r.iter_mut().enumerate() {
        let mut acc = 0f64;
        for i in 0..n - lag {
            acc += f64::from(x[i]) * f64::from(x[i + lag]);
        }
        *slot = acc;
    }
    if r[0] <= 1e-12 {
        return (Vec::new(), 1.0);
    }
    let r0 = r[0] * 1.0001; // white-noise correction
    r[0] = r0;
    let mut a = vec![0f64; order + 1];
    let mut parcor = Vec::with_capacity(order);
    let mut err = r0;
    for i in 1..=order {
        let mut acc = r[i];
        for j in 1..i {
            acc -= a[j] * r[i - j];
        }
        let k = acc / err;
        if !k.is_finite() || k.abs() >= 0.999 {
            break;
        }
        let prev: Vec<f64> = a[1..i].to_vec();
        for j in 1..i {
            a[j] = prev[j - 1] - k * prev[i - j - 1];
        }
        a[i] = k;
        parcor.push(k as f32);
        err *= 1.0 - k * k;
        if err <= 1e-12 {
            break;
        }
    }
    (parcor, r0 / err.max(1e-12))
}

/// **The Rung 3 gate + arm.** Decide whether to apply TNS to a long block and, if
/// so, filter `spec` in place and return the data to emit.
///
/// `max_sfb_hint` must be the `max_sfb` that will be written, because the decoder
/// filters over `min(TNS_MAX_LONG, max_sfb)` bands — encoder and decoder must
/// agree on the span exactly or the inverse filter runs over the wrong region.
///
/// Returns `None` when the gate declines (prediction gain below threshold, span
/// too short, or a degenerate fit), in which case `spec` is untouched and the
/// caller emits `tns_data_present = 0` — the byte-identical fallback.
pub(crate) fn tns_analyze_long(
    spec: &mut [f32],
    swb: &[u16],
    fs_index: u8,
    max_sfb: usize,
) -> Option<TnsEnc> {
    let _prof = prof::scope(Stage::EncTns);
    let num_swb = swb.len() - 1;
    let tns_max = crate::decode::TNS_MAX_LONG[fs_index as usize] as usize;
    let mmm = tns_max.min(max_sfb);
    if mmm < 4 {
        return None;
    }
    // One filter spanning the top of the coded range down to band `bottom`. The
    // decoder derives `start`/`end` from `length` the same way.
    let bottom = mmm / 4; // skip the lowest quarter: TNS on the bass is harmful
    let length = num_swb.saturating_sub(bottom);
    let start = swb[bottom.min(mmm)] as usize;
    let end = swb[mmm] as usize;
    if end <= start + TNS_MAX_ORDER + 1 {
        return None;
    }

    let (parcor_raw, gain) = spectral_parcor(spec, start, end, TNS_MAX_ORDER);
    if parcor_raw.is_empty() || gain < TNS_MIN_GAIN {
        return None; // the gate: not worth its bits
    }

    // Quantize to the shipping 4-bit grid, and keep the DEQUANTIZED values —
    // filtering with the unquantized ones would leave the decoder running a
    // slightly different filter, which shows up as drift across the band.
    let mut idx = Vec::with_capacity(parcor_raw.len());
    let mut parcor = Vec::with_capacity(parcor_raw.len());
    for &p in &parcor_raw {
        let (c, dq) = quantize_parcor(p);
        idx.push(c);
        parcor.push(dq);
    }
    // Drop trailing zero-index coefficients: they cost 4 bits each and do nothing.
    while idx.len() > 1 && *idx.last().unwrap() == 0 {
        idx.pop();
        parcor.pop();
    }
    let order = idx.len();
    if order == 0 {
        return None;
    }

    let lpc = parcor_to_lpc_enc(&parcor);

    // The ANALYSIS filter (all-zero), upward direction, state = past inputs.
    // Indexed rather than iterated: the filter reads and writes the same slice
    // element each step, which an iterator form cannot express without a copy.
    #[allow(clippy::needless_range_loop)]
    let mut state = vec![0f32; order];
    #[allow(clippy::needless_range_loop)]
    for p in start..end {
        let xn = spec[p];
        let mut y = xn;
        for j in 0..order {
            y += state[j] * lpc[j + 1];
        }
        for j in (1..order).rev() {
            state[j] = state[j - 1];
        }
        state[0] = xn;
        spec[p] = y;
    }

    Some(TnsEnc { length, order, idx })
}

/// Emit `tns_data` for a long block (`n_filt` 2 bits, `length` 6, `order` 5).
/// Mirrors `decode::parse_tns` exactly.
pub(crate) fn write_tns_long(w: &mut BitWriter, tns: &TnsEnc) {
    w.write(1, 2); // n_filt = 1
    w.write(TNS_COEF_RES, 1); // coef_res
    w.write(tns.length as u32, 6);
    w.write(tns.order as u32, 5);
    if tns.order > 0 {
        w.write(0, 1); // direction: upward (low -> high)
        w.write(0, 1); // coef_compress = 0
        for &c in &tns.idx {
            let masked = (c as u32) & ((1u32 << TNS_COEF_BITS) - 1);
            w.write(masked, TNS_COEF_BITS);
        }
    }
}

// ---------------------------------------------------------------------------
// Block switching (brick 5) — transient detection, window-sequence assignment,
// and the short-block coding path (eight 128-bin windows, one group).
// ---------------------------------------------------------------------------

/// Flag each 1024-sample frame that contains a transient: a 128-sample sub-block
/// whose energy leaps above the recent running average (an attack). Frame 0 is
/// never flagged — nothing precedes it to open a `LongStart` transition from.
///
/// `pub(crate)` so [`crate::lab::signals`] can record what the *shipping*
/// detector decides alongside the campaign's replacement signal — the
/// gate-calculator's `shipped` column (arm A9).
pub(crate) fn detect_transients(chan: &[f32], nframes: usize) -> Vec<bool> {
    const RATIO: f64 = 10.0;
    // (arm A9 replaces the guard below; see `detect_transients_relative`)
    let mut flags = vec![false; nframes];
    let mut avg = 0.0f64;
    for (f, flag) in flags.iter_mut().enumerate() {
        let mut attack = 1.0f64;
        for sb in 0..8 {
            let start = f * FRAME_LEN + sb * SHORT_HALF;
            let e: f64 = (0..SHORT_HALF)
                .map(|i| {
                    let x = f64::from(chan.get(start + i).copied().unwrap_or(0.0));
                    x * x
                })
                .sum();
            if avg > 1e-3 {
                attack = attack.max(e / avg);
            }
            avg = 0.75 * avg + 0.25 * e;
        }
        if f > 0 && attack > RATIO {
            *flag = true;
        }
    }
    flags
}

/// Per-frame **population-relative** max attack ratio — the level-invariant
/// replacement for [`detect_transients`]'s absolute `avg > 1e-3` guard (arm A9).
///
/// The running average is floored at a fraction of *this clip's own* mean
/// sub-block energy, so both numerator and floor scale with level and the ratio
/// is unchanged by a gain change. The shipping detector's absolute floor makes it
/// blind on quiet content and, because the average decays 0.75× per sub-block, on
/// sparse attacks over a quiet floor at *any* level.
///
/// Used by the Rung 0 shape gate as a transient veto; it is also the signal arm
/// A9 will replace the detector's threshold with.
pub(crate) fn frame_attack_ratios(chan: &[f32], nframes: usize) -> Vec<f32> {
    let mut sub: Vec<f64> = Vec::with_capacity(nframes * 8);
    for f in 0..nframes {
        for sb in 0..8 {
            let start = f * FRAME_LEN + sb * SHORT_HALF;
            let e: f64 = (0..SHORT_HALF)
                .map(|i| {
                    let x = f64::from(chan.get(start + i).copied().unwrap_or(0.0));
                    x * x
                })
                .sum();
            sub.push(e);
        }
    }
    let mean_e = sub.iter().sum::<f64>() / sub.len().max(1) as f64;
    let floor = (mean_e * 1e-3).max(f64::MIN_POSITIVE);

    let mut avg = 0.0f64;
    let mut out = Vec::with_capacity(nframes);
    for f in 0..nframes {
        let mut amax = 1.0f32;
        for sb in 0..8 {
            let e = sub[f * 8 + sb];
            amax = amax.max((e / avg.max(floor)) as f32);
            avg = 0.75 * avg + 0.25 * e;
        }
        out.push(amax);
    }
    out
}

/// **Arm A9 — the level-invariant transient detector.**
///
/// Same decision rule as [`detect_transients`] (`attack ratio > 10`), on a signal
/// that is actually defined. The shipped detector guards its ratio with
/// `avg > 1e-3` — an **absolute** energy floor — which has two consequences the
/// P1 truth table pinned:
///
/// * the identical waveform 40 dB quieter stops being detected at all, and
/// * because the running average decays 0.75x per sub-block, it falls under the
///   floor across the quiet stretch between sparse attacks, so castanet-like
///   content is missed **at any level** — measured: zero frames flagged on the
///   percussive corpus class whose p95 attack ratio is ~30000x.
///
/// The ratio itself is already scale-invariant; only the guard was not. This
/// version floors the running average relative to the clip's own mean sub-block
/// energy instead, which is the law-1 form and leaves the threshold meaning what
/// it says.
///
/// This is not merely a quality knob: arm A1 (short-block psy) can only reach
/// frames that are coded as short blocks, so a detector that never fires makes A1
/// unmeasurable. A9 is A1's prerequisite.
pub(crate) fn detect_transients_relative(chan: &[f32], nframes: usize) -> Vec<bool> {
    const RATIO: f32 = 10.0;
    let ratios = frame_attack_ratios(chan, nframes);
    (0..nframes)
        .map(|f| f > 0 && ratios.get(f).copied().unwrap_or(1.0) > RATIO)
        .collect()
}

/// Assign a valid AAC window sequence to each frame from the transient flags. A
/// short run is bracketed by LongStart/LongStop; runs a single frame apart are
/// merged (a lone gap can't be both a stop and a start).
fn assign_sequences(transient: &[bool]) -> Vec<WindowSequence> {
    use WindowSequence::{EightShort, LongStart, LongStop, OnlyLong};
    let n = transient.len();
    let mut short = transient.to_vec();
    for i in 1..n.saturating_sub(1) {
        if !short[i] && short[i - 1] && short[i + 1] {
            short[i] = true;
        }
    }
    let mut seq = vec![OnlyLong; n];
    let mut i = 0;
    while i < n {
        if short[i] {
            let a = i;
            while i < n && short[i] {
                seq[i] = EightShort;
                i += 1;
            }
            if a > 0 {
                seq[a - 1] = LongStart;
            }
            if i < n {
                seq[i] = LongStop;
            }
        } else {
            i += 1;
        }
    }
    seq
}

/// Cheapest codebook (and its bit cost) for one SFB across all short windows of a
/// single group, matched to how the decoder reads it (per-SFB codebook, per-window
/// coefficients).
fn best_codebook_short(
    quant: &[i32],
    swb: &[u16],
    sfb: usize,
    win0: usize,
    nwin: usize,
) -> (u8, usize) {
    let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
    let wins = win0..win0 + nwin;
    let mut maxq = 0u32;
    for win in wins.clone() {
        let base = win * SHORT_HALF;
        for &q in &quant[base + s..base + e] {
            maxq = maxq.max(q.unsigned_abs());
        }
    }
    if maxq == 0 {
        return (ZERO_HCB, 0);
    }
    let mut best = (ESC_HCB, usize::MAX);
    for cb in 1..=11u8 {
        let meta = &CODEBOOKS[cb as usize];
        let dim = meta.dim as usize;
        if (e - s) % dim != 0 || (!meta.esc && u32::from(meta.lav) < maxq) {
            continue;
        }
        let mut bits = 0usize;
        let mut ok = true;
        'windows: for win in wins.clone() {
            let base = win * SHORT_HALF;
            let mut i = s;
            while i < e {
                if let Some(b) = spectral_bits(cb as usize, &quant[base + i..base + i + dim]) {
                    bits += b;
                } else {
                    ok = false;
                    break 'windows;
                }
                i += dim;
            }
        }
        if ok && bits < best.1 {
            best = (cb, bits);
        }
    }
    best
}

/// `(first window, window count)` of each group, from the group lengths.
fn group_windows(groups: &[u8]) -> impl Iterator<Item = (usize, usize)> + '_ {
    groups.iter().scan(0usize, |w0, &len| {
        let r = (*w0, len as usize);
        *w0 += len as usize;
        Some(r)
    })
}

/// Per-group scalefactors (`groups.len() × num_swb`) flattened to the coded
/// order the bitstream carries (`groups.len() × max_sfb`).
fn flatten_short_sf(sf: &[i32], num_swb: usize, max_sfb: usize, ngroups: usize) -> Vec<i32> {
    (0..ngroups)
        .flat_map(|g| sf[g * num_swb..g * num_swb + max_sfb].iter().copied())
        .collect()
}

/// **Arm 1a — window grouping.** Split the eight short windows into runs of
/// similar energy: a new group starts where a window's energy differs from its
/// group's running mean by more than [`GROUP_SPLIT`]. A quiet pre-attack stretch
/// then has scalefactors of its own instead of sharing the attack's. `specs` are
/// the channels that share the `ics_info` (both channels of a common-window CPE).
fn short_groups(specs: &[&[f32]]) -> Vec<u8> {
    let mut e = [0f64; 8];
    for spec in specs {
        for (w, ew) in e.iter_mut().enumerate() {
            *ew += spec[w * SHORT_HALF..(w + 1) * SHORT_HALF]
                .iter()
                .map(|&x| f64::from(x) * f64::from(x))
                .sum::<f64>();
        }
    }
    let floor = e.iter().sum::<f64>() * 1e-6 + 1e-9;
    let mut groups = Vec::with_capacity(8);
    let (mut len, mut sum) = (1u8, e[0]);
    for &ew in &e[1..] {
        let ratio = (ew + floor) / (sum / f64::from(len) + floor);
        if !(1.0 / GROUP_SPLIT..=GROUP_SPLIT).contains(&ratio) {
            groups.push(len);
            len = 1;
            sum = ew;
        } else {
            len += 1;
            sum += ew;
        }
    }
    groups.push(len);
    groups
}

/// Energy ratio (window vs its group's mean) that starts a new group: 6 dB.
const GROUP_SPLIT: f64 = 4.0;

/// Bits for short-block `section_data` (3-bit run-length increments, esc = 7).
fn section_bits_short(cbs: &[u8]) -> usize {
    let esc = 7usize;
    let mut bits = 0usize;
    let mut k = 0usize;
    while k < cbs.len() {
        let cb = cbs[k];
        let mut len = 1usize;
        while k + len < cbs.len() && cbs[k + len] == cb {
            len += 1;
        }
        bits += 4;
        let mut l = len;
        while l >= esc {
            bits += 3;
            l -= esc;
        }
        bits += 3;
        k += len;
    }
    bits
}

/// Quantize all eight short windows with a per-SFB scalefactor (one group; flat
/// this brick), pick per-SFB codebooks, and return (codebooks, body bits, `max_sfb`,
/// window-major quantized spectrum).
fn code_frame_short(
    xp: &Xpow,
    swb: &[u16],
    sf: &[i32],
    groups: &[u8],
) -> (Vec<u8>, usize, usize, Vec<i32>, Vec<i32>) {
    work::bump_code_frame();
    let num_swb = swb.len() - 1;
    work::bump_quant_bands(((swb.len() - 1) * 8) as u64);
    let scale = scale_table();
    let prof_q = prof::scope(Stage::EncQuant);
    let mut quant = vec![0i32; FRAME_LEN];
    for (g, (w0, nwin)) in group_windows(groups).enumerate() {
        for win in w0..w0 + nwin {
            let base = win * SHORT_HALF;
            for sfb in 0..num_swb {
                let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
                let sc = scale[sf[g * num_swb + sfb].clamp(0, 255) as usize];
                quantize_band(
                    &xp.pow[base + s..base + e],
                    &xp.sign[base + s..base + e],
                    sc,
                    &mut quant[base + s..base + e],
                );
            }
        }
    }
    drop(prof_q);
    let _prof_cb = prof::scope(Stage::EncCodebook);
    let mut max_sfb = 0usize;
    for sfb in 0..num_swb {
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        if (0..8).any(|win| {
            let base = win * SHORT_HALF;
            quant[base + s..base + e].iter().any(|&q| q != 0)
        }) {
            max_sfb = sfb + 1;
        }
    }
    let mut cbs = Vec::with_capacity(groups.len() * max_sfb);
    let mut spec_bits = 0usize;
    let mut section_bits = 0usize;
    for (w0, nwin) in group_windows(groups) {
        let start = cbs.len();
        for sfb in 0..max_sfb {
            let (cb, bits) = best_codebook_short(&quant, swb, sfb, w0, nwin);
            cbs.push(cb);
            spec_bits += bits;
        }
        section_bits += section_bits_short(&cbs[start..]);
    }
    let sf_flat = flatten_short_sf(sf, num_swb, max_sfb, groups.len());
    let gg = global_gain(&cbs, &sf_flat);
    // global_gain(8) + ics_info(short ~18) + 3 flags + sections + scalefactors + spectrum.
    let body = 8 + 18 + 3 + section_bits + scalefactor_bits(&cbs, &sf_flat, gg) + spec_bits;
    (cbs, body, max_sfb, quant, sf_flat)
}

/// The smallest flat scalefactor that avoids clamping the loudest short coefficient
/// past `MAX_QUANT`.
fn min_base_short(xp: &Xpow) -> i32 {
    let maxp = xp.max_pow(0, xp.len());
    if maxp <= 1e-9 {
        return 0;
    }
    (100.0 - (f64::from(MAX_QUANT) / maxp).log2() / 0.1875)
        .ceil()
        .clamp(0.0, 255.0) as i32
}

/// **Arm A1 — the short-block psychoacoustic model.**
///
/// Per-SFB scalefactor offsets for an `EightShort` frame. The band energies are
/// summed **across the eight windows** of the group, because with one window group
/// a scalefactor covers band `b` of all eight windows — so the mask must be
/// computed over exactly the coefficients that scalefactor governs.
///
/// The band geometry is the short SWB table over a 128-coefficient window, hence
/// `n_coeffs = SHORT_HALF`: reusing the long block's 1024 here would place every
/// band three octaves too low and spread the mask against the wrong neighbours.
///
/// Before this arm, short blocks coded with **flat** scalefactors — no noise
/// shaping at all on precisely the content where masking matters most.
///
/// # MEASURED REFUTATION (2026-08-08): this is harmful WITHOUT window grouping
///
/// Once arm A9 made short blocks actually occur, A1 became measurable — and it
/// made things **worse**, on both the class it was built for and the one it was
/// meant to be neutral on:
///
/// | arm | percussive 64k/128k | speech-clean 64k/128k |
/// |---|---|---|
/// | A9 alone | −2.81 / −6.10 | +1.56 / +1.18 |
/// | A9 + A1 | −1.18 / −5.50 | +2.33 / +1.46 |
///
/// The cause is the group-summed energy above. With **one** window group a single
/// scalefactor governs band `b` of all eight windows, so the mask must be derived
/// from their sum — which hands the quiet pre-attack windows a mask sized by the
/// attack. That is precisely the pre-echo the tool was supposed to suppress: a
/// flat scalefactor at least errs uniformly, whereas an attack-sized mask
/// actively licenses noise where the ear is most sensitive.
///
/// **This overturns a plan correction I made earlier.** Reading the emit path, I
/// argued grouping was a follow-up rather than a prerequisite, because per-SFB
/// shaping is available with one group. That is true and irrelevant: the shaping
/// available with one group is shaping in the *wrong domain*. Arm 1a (window
/// grouping) is a genuine prerequisite for A1, exactly as the original plan said.
/// Do not enable `short_block_psy` before grouping exists.
fn perceptual_offsets_short(
    spec: &[f32],
    swb: &[u16],
    sample_rate: u32,
    tonality_smr: bool,
) -> Vec<i32> {
    let _prof = prof::scope(Stage::EncPsy);
    let num_swb = swb.len() - 1;
    let mut energy = vec![0.0f64; num_swb];
    let mut noise_scale = vec![0.0f64; num_swb];
    for sfb in 0..num_swb {
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        for win in 0..8 {
            let base = win * SHORT_HALF;
            for k in s..e {
                let Some(&x) = spec.get(base + k) else {
                    continue;
                };
                energy[sfb] += f64::from(x) * f64::from(x);
                noise_scale[sfb] += f64::from(x.abs()).sqrt();
            }
        }
    }
    let mut e_masked: Vec<f64> = energy.iter().map(|&e| e + 1e-3).collect();
    let ton = tonality_smr.then(|| band_tonality(spec, swb, 8, SHORT_HALF));
    let thr = masking_from_energy(&e_masked, swb, sample_rate, SHORT_HALF, ton.as_deref());
    e_masked.clear();

    let raw: Vec<f64> = (0..num_swb)
        .map(|sfb| (thr[sfb] / (noise_scale[sfb] + 1e-6)).log2() / 0.375)
        .collect();
    // Centre on the energy-bearing bands, exactly as the long-block path does.
    let etot: f64 = energy.iter().sum::<f64>() + 1e-9;
    let center: f64 = (0..num_swb).map(|i| raw[i] * energy[i]).sum::<f64>() / etot;
    raw.iter()
        .map(|&r| ((r - center).round() as i32).clamp(-60, 60))
        .collect()
}

/// Rate loop for a short block: smallest common base (≥ no-clamp floor) whose body
/// fits `target_bits`. `offsets` is arm A1's per-band shape (all zeros reproduces
/// **Arms A1 + 1a — the short-block psy model per window group.**
///
/// [`perceptual_offsets_short`] sums band energy across all eight windows, which
/// hands quiet pre-attack windows a mask sized by the attack (its measured
/// refutation). Here each group gets its own masking threshold, and the offsets
/// are centred **across** groups: a quiet group therefore sits below the attack
/// group and is coded with finer steps, which is the pre-echo protection the
/// short block exists for. Offsets are clamped to ±30 so any two coded
/// scalefactors stay within the bitstream's ±60 delta. Returns
/// `groups.len() × num_swb` offsets, group-major.
fn perceptual_offsets_short_grouped(
    spec: &[f32],
    swb: &[u16],
    sample_rate: u32,
    tonality_smr: bool,
    groups: &[u8],
) -> Vec<i32> {
    let _prof = prof::scope(Stage::EncPsy);
    let num_swb = swb.len() - 1;
    let mut raw = Vec::with_capacity(groups.len() * num_swb);
    let mut energy = Vec::with_capacity(groups.len() * num_swb);
    for (w0, nwin) in group_windows(groups) {
        let sub = &spec[w0 * SHORT_HALF..(w0 + nwin) * SHORT_HALF];
        let mut e_band = vec![0.0f64; num_swb];
        let mut noise_scale = vec![0.0f64; num_swb];
        for sfb in 0..num_swb {
            let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
            for win in 0..nwin {
                let base = win * SHORT_HALF;
                for &x in &sub[base + s..base + e] {
                    e_band[sfb] += f64::from(x) * f64::from(x);
                    noise_scale[sfb] += f64::from(x.abs()).sqrt();
                }
            }
        }
        let e_masked: Vec<f64> = e_band.iter().map(|&e| e + 1e-3).collect();
        let ton = tonality_smr.then(|| band_tonality(sub, swb, nwin, SHORT_HALF));
        let thr = masking_from_energy(&e_masked, swb, sample_rate, SHORT_HALF, ton.as_deref());
        for sfb in 0..num_swb {
            raw.push((thr[sfb] / (noise_scale[sfb] + 1e-6)).log2() / 0.375);
        }
        energy.extend_from_slice(&e_band);
    }
    let etot: f64 = energy.iter().sum::<f64>() + 1e-9;
    let center: f64 = raw.iter().zip(&energy).map(|(r, e)| r * e).sum::<f64>() / etot;
    raw.iter()
        .map(|&r| ((r - center).round() as i32).clamp(-30, 30))
        .collect()
}

/// Short-block scalefactor offsets for the configured arms: grouped psy (1a),
/// single-group psy (A1), or flat. Returns `groups.len() × num_swb` values.
fn short_offsets(
    spec: &[f32],
    swb: &[u16],
    sample_rate: u32,
    psy: PsyCfg,
    groups: &[u8],
) -> Vec<i32> {
    if psy.window_grouping {
        perceptual_offsets_short_grouped(spec, swb, sample_rate, psy.tonality_smr, groups)
    } else if psy.short_block_psy {
        perceptual_offsets_short(spec, swb, sample_rate, psy.tonality_smr)
    } else {
        vec![0i32; groups.len() * (swb.len() - 1)]
    }
}

/// The window grouping for a short frame whose `ics_info` the `specs` share:
/// real groups under arm 1a, else one group of eight.
fn short_grouping(specs: &[&[f32]], psy: PsyCfg) -> Vec<u8> {
    if psy.window_grouping {
        short_groups(specs)
    } else {
        vec![8]
    }
}

/// the flat-scalefactor behavior byte-identically).
fn rate_loop_short(
    xp: &Xpow,
    swb: &[u16],
    offsets: &[i32],
    target_bits: usize,
    groups: &[u8],
) -> i32 {
    let mut lo = min_base_short(xp);
    let mut hi = 255i32;
    while lo < hi {
        let mid = (lo + hi) / 2;
        let sf = scalefactors(offsets, mid);
        if code_frame_short(xp, swb, &sf, groups).1 <= target_bits {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }
    lo
}

/// `short_block` `section_data` (4-bit codebook + 3-bit run increments, esc = 7).
fn write_sections_short(w: &mut BitWriter, cbs: &[u8]) {
    let esc = 7u32;
    let mut k = 0usize;
    while k < cbs.len() {
        let cb = cbs[k];
        let mut len = 1usize;
        while k + len < cbs.len() && cbs[k + len] == cb {
            len += 1;
        }
        w.write(u32::from(cb), 4);
        let mut l = len as u32;
        while l >= esc {
            w.write(esc, 3);
            l -= esc;
        }
        w.write(l, 3);
        k += len;
    }
}

/// `short_block` `spectral_data`: per group, per SFB, per window of the group,
/// coefficient tuples (the grouped-interleaved order the decoder reads).
fn write_spectrum_short(
    w: &mut BitWriter,
    quant: &[i32],
    cbs: &[u8],
    swb: &[u16],
    groups: &[u8],
    max_sfb: usize,
) {
    for (g, (w0, nwin)) in group_windows(groups).enumerate() {
        for sfb in 0..max_sfb {
            let cb = cbs[g * max_sfb + sfb];
            if cb == ZERO_HCB {
                continue;
            }
            let dim = CODEBOOKS[cb as usize].dim as usize;
            let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
            for win in w0..w0 + nwin {
                let base = win * SHORT_HALF;
                let mut i = s;
                while i + dim <= e {
                    spectral_emit(cb as usize, &quant[base + i..base + i + dim], w);
                    i += dim;
                }
            }
        }
    }
}

/// `short_block` `section_data` for every window group (runs never cross a group).
fn write_sections_short_grouped(w: &mut BitWriter, cbs: &[u8], ngroups: usize, max_sfb: usize) {
    for g in 0..ngroups {
        write_sections_short(w, &cbs[g * max_sfb..(g + 1) * max_sfb]);
    }
}

/// Encode one channel as an `EightShort` `single_channel_element`. One group of 8
/// windows with flat scalefactors unless arm A1 (`psy.short_block_psy`) supplies a
/// per-band shape, or arm 1a (`psy.window_grouping`) real groups shaped per group.
#[allow(clippy::too_many_arguments)]
fn encode_channel_element_short(
    w: &mut BitWriter,
    tag: u32,
    spec: &[f32],
    swb: &[u16],
    target_bits: usize,
    cur_kbd: bool,
    sample_rate: u32,
    psy: PsyCfg,
) {
    let xp = Xpow::new(spec);
    let groups = short_grouping(&[spec], psy);
    let offsets = short_offsets(spec, swb, sample_rate, psy, &groups);
    let base = rate_loop_short(&xp, swb, &offsets, target_bits, &groups);
    let sf = scalefactors(&offsets, base);
    let (cbs, _, max_sfb, quant, sf_flat) = code_frame_short(&xp, swb, &sf, &groups);
    let gg = global_gain(&cbs, &sf_flat);

    w.write(ID_SCE, 3);
    w.write(tag, 4);
    w.write(gg as u32, 8);
    let info = IcsInfo {
        window_sequence: WindowSequence::EightShort,
        window_shape_kbd: cur_kbd,
        max_sfb: max_sfb as u8,
        num_windows: 8,
        num_window_groups: groups.len(),
        window_group_length: groups.clone(),
        num_swb: swb.len() - 1,
    };
    encode_ics_info(w, &info);
    write_sections_short_grouped(w, &cbs, groups.len(), max_sfb);
    write_scalefactors(w, &cbs, &sf_flat, gg);
    w.write(0, 1); // pulse_data_present
    w.write(0, 1); // tns_data_present
    w.write(0, 1); // gain_control_data_present
    write_spectrum_short(w, &quant, &cbs, swb, &groups, max_sfb);
}

// ---------------------------------------------------------------------------
// Stereo (brick 6) — a channel_pair_element with a common window and per-SFB M/S
// (mid/side) coding where the channels are correlated enough to pay off.
// ---------------------------------------------------------------------------

/// Per-SFB M/S decision + mixed spectra. M/S wins when `E_M·E_S < E_L·E_R` (the
/// correlation criterion — raw energy always halves under the ½ scaling, so the
/// *product* is what predicts bit savings). Returns (ch0 = M or L, ch1 = S or R,
/// per-SFB `ms_used`).
fn mid_side(
    l: &[f32],
    r: &[f32],
    swb: &[u16],
    is_short: bool,
    is_veto: &[bool],
    groups: &[u8],
) -> (Vec<f32>, Vec<f32>, Vec<bool>) {
    let num_swb = swb.len() - 1;
    let wlen = if is_short { SHORT_HALF } else { FRAME_LEN };
    let groups: &[u8] = if is_short { groups } else { &[1] };
    let mut ch0 = l.to_vec();
    let mut ch1 = r.to_vec();
    let mut ms = vec![false; groups.len() * num_swb];
    for (g, (w0, nwin)) in group_windows(groups).enumerate() {
        for sfb in 0..num_swb {
            let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
            let (mut el, mut er, mut em, mut es) = (0.0f64, 0.0, 0.0, 0.0);
            for win in w0..w0 + nwin {
                let base = win * wlen;
                for i in s..e {
                    let (lv, rv) = (f64::from(l[base + i]), f64::from(r[base + i]));
                    let (m, sd) = ((lv + rv) * 0.5, (lv - rv) * 0.5);
                    el += lv * lv;
                    er += rv * rv;
                    em += m * m;
                    es += sd * sd;
                }
            }
            // A band claimed by intensity stereo (arm A7) must stay L/R.
            if em * es < el * er && !is_veto.get(sfb).copied().unwrap_or(false) {
                ms[g * num_swb + sfb] = true;
                for win in w0..w0 + nwin {
                    let base = win * wlen;
                    for i in s..e {
                        let (lv, rv) = (l[base + i], r[base + i]);
                        ch0[base + i] = (lv + rv) * 0.5;
                        ch1[base + i] = (lv - rv) * 0.5;
                    }
                }
            }
        }
    }
    (ch0, ch1, ms)
}

/// Quantize one CPE channel to a target: psy per-band scalefactors (long) or flat
/// (short). Returns (per-SFB scalefactors, quantized spectrum).
fn quantize_channel(
    spec: &[f32],
    swb: &[u16],
    is_short: bool,
    sample_rate: u32,
    target_bits: usize,
    psy: PsyCfg,
    groups: &[u8],
) -> (Vec<i32>, Vec<i32>) {
    let xp = Xpow::new(spec);
    if is_short {
        let offsets = short_offsets(spec, swb, sample_rate, psy, groups);
        let base = rate_loop_short(&xp, swb, &offsets, target_bits, groups);
        let sf = scalefactors(&offsets, base);
        let (_, _, _, quant, _) = code_frame_short(&xp, swb, &sf, groups);
        (sf, quant)
    } else {
        let offsets = perceptual_offsets(spec, swb, sample_rate, psy.tonality_smr);
        let base = rate_loop(&xp, swb, &offsets, target_bits);
        let sf = scalefactors(&offsets, base);
        let (_, _, _, quant) = code_frame(&xp, swb, &sf);
        (sf, quant)
    }
}

/// The highest SFB with a non-zero coefficient in either channel (both channels of
/// a common-window CPE share `max_sfb`).
fn joint_max_sfb(q0: &[i32], q1: &[i32], swb: &[u16], is_short: bool) -> usize {
    let num_swb = swb.len() - 1;
    let nwin = if is_short { 8 } else { 1 };
    let wlen = if is_short { SHORT_HALF } else { FRAME_LEN };
    let nz = |q: &[i32], s: usize, e: usize| {
        (0..nwin).any(|win| {
            let b = win * wlen;
            q[b + s..b + e].iter().any(|&x| x != 0)
        })
    };
    let mut m = 0;
    for sfb in 0..num_swb {
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        if nz(q0, s, e) || nz(q1, s, e) {
            m = sfb + 1;
        }
    }
    m
}

/// Per-SFB codebooks for one channel over `0..max_sfb` (for every window group of
/// a short frame, group-major).
fn codebooks(quant: &[i32], swb: &[u16], is_short: bool, max_sfb: usize, groups: &[u8]) -> Vec<u8> {
    if is_short {
        group_windows(groups)
            .flat_map(|(w0, nwin)| {
                (0..max_sfb).map(move |sfb| best_codebook_short(quant, swb, sfb, w0, nwin).0)
            })
            .collect()
    } else {
        (0..max_sfb)
            .map(|sfb| {
                let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
                best_codebook_for_band(quant, s, e).0
            })
            .collect()
    }
}

/// `ms_mask_present` (2 bits) + the per-SFB mask when mixed.
fn write_ms_used(w: &mut BitWriter, ms_used: &[bool]) {
    if ms_used.iter().all(|&b| !b) {
        w.write(0, 2);
    } else if ms_used.iter().all(|&b| b) {
        w.write(2, 2);
    } else {
        w.write(1, 2);
        for &b in ms_used {
            w.write_bool(b);
        }
    }
}

/// One channel's `individual_channel_stream` body inside a common-window CPE:
/// `global_gain`, `section_data`, `scale_factor_data`, the three flag bits, `spectral_data`
/// (no `ics_info` — it is shared).
#[allow(
    clippy::too_many_arguments,
    reason = "one argument per syntax element the bitstream writes; a struct would only rename them"
)]
fn write_channel_data(
    w: &mut BitWriter,
    cbs: &[u8],
    sf: &[i32],
    quant: &[i32],
    swb: &[u16],
    is_short: bool,
    groups: &[u8],
    max_sfb: usize,
) {
    let sf_flat;
    let sf = if is_short {
        sf_flat = flatten_short_sf(sf, swb.len() - 1, max_sfb, groups.len());
        &sf_flat[..]
    } else {
        sf
    };
    let gg = global_gain(cbs, sf);
    w.write(gg as u32, 8);
    if is_short {
        write_sections_short_grouped(w, cbs, groups.len(), max_sfb);
    } else {
        write_sections(w, cbs);
    }
    write_scalefactors(w, cbs, sf, gg);
    w.write(0, 1); // pulse_data_present
    w.write(0, 1); // tns_data_present
    w.write(0, 1); // gain_control_data_present
    if is_short {
        write_spectrum_short(w, quant, cbs, swb, groups, max_sfb);
    } else {
        write_spectrum(w, quant, cbs, swb);
    }
}

/// **Arm A13 — joint rate loop over a channel pair.**
///
/// # The defect this fixes
///
/// A CPE's two channels were each given `frame_budget / channel_count` bits and
/// run through **independent** rate loops. That is a fixed 50/50 split, so when
/// M/S makes the side channel nearly empty its bits are simply wasted — the mid
/// channel, which is what the listener hears, still gets only half the frame.
///
/// Measured on perfectly-correlated stereo (`L == R`, so `S ≡ 0`) at 128 kbps,
/// per-channel reconstruction SNR:
///
/// | encoder | SNR |
/// |---|---|
/// | independent per-channel loops | **10.6 dB** |
/// | ffmpeg native AAC | **38.8 dB** |
///
/// # Why the obvious fix did not work
///
/// The first attempt split the budget by each channel's perceptual entropy. It
/// made things *worse* (8.5 dB), because PE is degenerate under a purely
/// relative masking model: the mask is `SMR × spread(signal)`, so `E/thr ≈
/// 1/SMR` for **any** non-silent channel regardless of its level. Both channels
/// report near-identical "demand" whatever they contain. A level-blind demand
/// estimate cannot allocate between levels.
///
/// # What actually works
///
/// Run **one** rate loop over the pair and give both channels a **common base**
/// scalefactor. A common base means a common quantizer step, i.e. both channels
/// are coded to the same noise floor relative to their own masks — which is the
/// textbook joint-stereo criterion. Bits then flow to whichever channel needs
/// them, because a near-empty side channel costs almost nothing at any base and
/// lets the loop settle on a finer base for the pair.
///
/// This is a strict generalisation: for two channels of equal demand it lands on
/// the same place the independent loops did.
#[allow(clippy::too_many_arguments)]
fn pair_body_bits(
    xp0: &Xpow,
    off0: &[i32],
    xp1: &Xpow,
    off1: &[i32],
    swb: &[u16],
    base: i32,
    is_short: bool,
    quant: &mut [i32],
    groups: &[u8],
) -> usize {
    let (b0, b1) = if is_short {
        (
            code_frame_short(xp0, swb, &scalefactors(off0, base), groups).1,
            code_frame_short(xp1, swb, &scalefactors(off1, base), groups).1,
        )
    } else {
        (
            code_core(xp0, swb, &scalefactors(off0, base), quant).1,
            code_core(xp1, swb, &scalefactors(off1, base), quant).1,
        )
    };
    b0 + b1
}

/// Smallest common base whose combined body fits `target_total` bits.
#[allow(clippy::too_many_arguments)]
fn joint_rate_loop(
    xp0: &Xpow,
    off0: &[i32],
    xp1: &Xpow,
    off1: &[i32],
    swb: &[u16],
    target_total: usize,
    is_short: bool,
    groups: &[u8],
) -> i32 {
    let lo0 = if is_short {
        min_base_short(xp0)
    } else {
        min_base(xp0, swb, off0)
    };
    let lo1 = if is_short {
        min_base_short(xp1)
    } else {
        min_base(xp1, swb, off1)
    };
    let mut lo = lo0.max(lo1);
    let mut hi = 255i32;
    let mut quant = vec![0i32; xp0.len().max(xp1.len())];
    while lo < hi {
        let mid = (lo + hi) / 2;
        if pair_body_bits(xp0, off0, xp1, off1, swb, mid, is_short, &mut quant, groups)
            <= target_total
        {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }
    lo
}

/// **Arm A7 — intensity stereo.**
///
/// Above a cutoff the ear localizes by ENVELOPE rather than waveform, so a band
/// of the second channel can be sent as a single scale factor applied to the
/// first: the decoder reconstructs `s1 = +-0.5^(0.25*is_pos) * s0`
/// (`decode::apply_is`). One value replaces a whole band's spectrum.
///
/// This turns the stereo decision from binary into a **3-arm dispatch** —
/// `{L/R, M/S, IS}` — which is why it is applied after `mid_side` and only to
/// bands M/S declined: with `ms_used` set the pair is already (M, S), and
/// intensity on an S channel means something quite different from intensity on
/// an R channel. Restricting to the M/S-declined bands keeps the two tools from
/// interpreting each other's output.
///
/// The anti-class is wide-stereo music, where collapsing a band to a scaled copy
/// destroys the image. Hence the correlation gate: only bands where the channels
/// genuinely already agree up to a gain.
/// **Arm A7 — intensity stereo.**
///
/// Above a cutoff the ear localizes by ENVELOPE rather than waveform, so a band
/// of the right channel can be sent as a single scale factor applied to the left:
/// the decoder reconstructs `R = +-0.5^(0.25*is_pos) * L` (`decode::apply_is`).
/// One value replaces a whole band's spectrum.
///
/// # Why this decides BEFORE `mid_side`, not after
///
/// The first version ran after M/S and only touched bands M/S had declined — on
/// the reasoning that intensity on an S channel means something different from
/// intensity on an R channel. It never fired once. The bands where `R ~= k*L`
/// are exactly the bands where M/S wins (`E_M*E_S << E_L*E_R` for a scaled copy),
/// so "M/S declined it" and "intensity wants it" are near-disjoint by
/// construction.
///
/// So intensity is decided on the ORIGINAL L/R spectra and **vetoes M/S** on the
/// bands it claims, which also keeps the decoder's sign rule simple: `apply_is`
/// flips the intensity sign when `ms_used` is set, and a band that is both is a
/// sign error waiting to happen.
///
/// This makes the stereo decision a genuine **3-arm dispatch** — `{L/R, M/S, IS}`
/// — rather than two tools overwriting each other. The anti-class is wide-stereo
/// music, where collapsing a band to a scaled copy destroys the image; hence the
/// correlation gate.
fn intensity_decision(
    spec_l: &[f32],
    spec_r: &[f32],
    swb: &[u16],
    sample_rate: u32,
) -> Vec<Option<(u8, i32)>> {
    /// Below this the ear localizes by waveform and intensity stereo is audible.
    const IS_MIN_HZ: f64 = 6000.0;
    /// How well `R ~= scale * L` must already hold, as |cos| between the bands.
    const IS_MIN_CORR: f64 = 0.85;

    let num_swb = swb.len() - 1;
    let mut out = vec![None; num_swb];
    for sfb in 0..num_swb {
        let lo_hz = f64::from(swb[sfb]) * f64::from(sample_rate) * 0.5 / FRAME_LEN as f64;
        if lo_hz < IS_MIN_HZ {
            continue;
        }
        let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
        let e = e.min(spec_l.len()).min(spec_r.len());
        if e <= s {
            continue;
        }
        let (mut dot, mut e0, mut e1) = (0f64, 0f64, 0f64);
        for i in s..e {
            let (a, b) = (f64::from(spec_l[i]), f64::from(spec_r[i]));
            dot += a * b;
            e0 += a * a;
            e1 += b * b;
        }
        if e0 <= 1.0 || e1 <= 1.0 {
            continue;
        }
        if (dot / (e0.sqrt() * e1.sqrt())).abs() < IS_MIN_CORR {
            continue;
        }
        // Least-squares gain, then the syntax's log grid:
        // scale = 0.5^(0.25*is_pos)  =>  is_pos = -4*log2(|scale|).
        let scale = dot / e0;
        if scale.abs() < 1e-6 {
            continue;
        }
        let is_pos = (-4.0 * scale.abs().log2()).round() as i32;
        if !(-20..=60).contains(&is_pos) {
            continue;
        }
        out[sfb] = Some((
            if scale >= 0.0 {
                INTENSITY_HCB
            } else {
                INTENSITY_HCB2
            },
            is_pos,
        ));
    }
    out
}

/// Encode a stereo pair as a common-window `channel_pair_element` with per-SFB M/S.
#[allow(clippy::too_many_arguments)]
fn encode_cpe(
    w: &mut BitWriter,
    tag: u32,
    spec_l: &[f32],
    spec_r: &[f32],
    swb: &[u16],
    seq: WindowSequence,
    sample_rate: u32,
    target_bits: usize,
    cur_kbd: bool,
    psy: PsyCfg,
) {
    let is_short = seq == WindowSequence::EightShort;
    // Arm A7 decides first, on the untouched L/R spectra, and vetoes M/S on the
    // bands it claims.
    let is_bands = if psy.intensity && !is_short {
        intensity_decision(spec_l, spec_r, swb, sample_rate)
    } else {
        vec![None; swb.len() - 1]
    };
    let is_veto: Vec<bool> = is_bands.iter().map(std::option::Option::is_some).collect();
    // Both channels share the ics_info, hence one grouping decided on both.
    let groups = if is_short {
        short_grouping(&[spec_l, spec_r], psy)
    } else {
        vec![1]
    };
    let (ch0, ch1, ms_full) = mid_side(spec_l, spec_r, swb, is_short, &is_veto, &groups);
    // Arm A13 ROUTING GATE.
    //
    // The joint loop is a large win when the pair is CORRELATED and a loss when
    // it is not — a per-class sign flip, so it is routed rather than averaged:
    //
    // | content | 64k | 128k |
    // |---|---|---|
    // | guitar stereo (correlated) | +0.23 | **+1.15** |
    // | piano stereo (correlated)  | +0.01 | +0.26 |
    // | wide/decorrelated stereo   | −0.30 | −0.04 |
    //
    // The signal is already in hand: the fraction of coded bands M/S claimed.
    // High means the two channels largely agree, so the side channel is cheap and
    // a common base lets the mid channel take the bits it is wasting. Low means
    // both channels carry independent content, both genuinely need their half,
    // and forcing a common base starves whichever is harder.
    //
    // The signal is the pair's spectral correlation, measured directly. (The M/S
    // flag fraction was tried first and does not separate: M/S still claims most
    // bands on wide content because it only needs `E_M·E_S < E_L·E_R`, which a
    // shared centre image satisfies.) Measured correlations:
    //
    //   L == R (ideal)   1.00   -> joint wins hugely
    //   guitar stereo    0.84   -> joint wins +1.15 @128k
    //   piano stereo     0.47   -> joint wins +0.26 @128k
    //   synthetic wide   0.25   -> joint LOSES -0.30 @64k
    //
    // The threshold sits in the wide gap between 0.25 and 0.47.
    let joint_ok = pair_correlation(spec_l, spec_r) >= JOINT_STEREO_MIN_CORR;

    let (sf0, quant0, sf1, quant1) = if psy.stereo_bit_split && joint_ok {
        let off = |spec: &[f32]| -> Vec<i32> {
            if is_short {
                short_offsets(spec, swb, sample_rate, psy, &groups)
            } else {
                perceptual_offsets(spec, swb, sample_rate, psy.tonality_smr)
            }
        };
        let (o0, o1) = (off(&ch0), off(&ch1));
        let (xp0, xp1) = (Xpow::new(&ch0), Xpow::new(&ch1));
        let base = joint_rate_loop(
            &xp0,
            &o0,
            &xp1,
            &o1,
            swb,
            target_bits * 2,
            is_short,
            &groups,
        );
        let (s0, s1) = (scalefactors(&o0, base), scalefactors(&o1, base));
        let q0 = if is_short {
            code_frame_short(&xp0, swb, &s0, &groups).3
        } else {
            code_frame(&xp0, swb, &s0).3
        };
        let q1 = if is_short {
            code_frame_short(&xp1, swb, &s1, &groups).3
        } else {
            code_frame(&xp1, swb, &s1).3
        };
        (s0, q0, s1, q1)
    } else {
        let (a, b) = quantize_channel(&ch0, swb, is_short, sample_rate, target_bits, psy, &groups);
        let (c, d) = quantize_channel(&ch1, swb, is_short, sample_rate, target_bits, psy, &groups);
        (a, b, c, d)
    };
    let max_sfb = joint_max_sfb(&quant0, &quant1, swb, is_short);
    let cbs0 = codebooks(&quant0, swb, is_short, max_sfb, &groups);
    let mut cbs1 = codebooks(&quant1, swb, is_short, max_sfb, &groups);
    let mut sf1 = sf1;
    let mut ms_full = ms_full;

    // Arm A7 — stamp the claimed bands into channel 1's codebooks/scalefactors.
    for (sfb, band) in is_bands.iter().enumerate().take(max_sfb) {
        if let (Some((cb, is_pos)), Some(slot)) = (band, cbs1.get_mut(sfb)) {
            *slot = *cb;
            sf1[sfb] = *is_pos;
            if let Some(m) = ms_full.get_mut(sfb) {
                *m = false;
            }
        }
    }

    w.write(ID_CPE, 3);
    w.write(tag, 4);
    w.write(1, 1); // common_window
    let info = IcsInfo {
        window_sequence: seq,
        window_shape_kbd: cur_kbd,
        max_sfb: max_sfb as u8,
        num_windows: if is_short { 8 } else { 1 },
        num_window_groups: groups.len(),
        window_group_length: groups.clone(),
        num_swb: swb.len() - 1,
    };
    encode_ics_info(w, &info);
    // ms_used is per group, per band below max_sfb, in group order.
    let num_swb = swb.len() - 1;
    let ms_coded: Vec<bool> = (0..groups.len())
        .flat_map(|g| ms_full[g * num_swb..g * num_swb + max_sfb].iter().copied())
        .collect();
    write_ms_used(w, &ms_coded);
    write_channel_data(w, &cbs0, &sf0, &quant0, swb, is_short, &groups, max_sfb);
    write_channel_data(w, &cbs1, &sf1, &quant1, swb, is_short, &groups, max_sfb);
}

// ---------------------------------------------------------------------------
// The encoder: buffers input, blocks into 1024-sample long frames, emits raw
// access units (add ADTS or MP4 framing around them as needed).
// ---------------------------------------------------------------------------

/// Window-shape policy — **arm A8, Rung 0** of `docs/finished/codec-aac-great-gate.md`.
///
/// `window_shape` is a *free syntax element*: one bit already carried in every
/// `ics_info`, costing nothing to set either way, with both shapes implemented on
/// the decode side since day one. It shipped hardcoded to sine — census category 3.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WindowShape {
    /// Sine everywhere. **The neutral end**: byte-identical with the encoder as
    /// it shipped before Rung 0 (proven by `shape_sine_is_byte_identical`).
    #[default]
    Sine,
    /// KBD everywhere. Not a shipping mode — the *force-on* arm the calculator
    /// needs: great-gate §4 requires force-on-everywhere to nearly tie the anchor
    /// on the full ladder before a dispatch is built on it, because a big force-on
    /// gap predicts a dominated dispatch.
    Kbd,
    /// **PROVISIONAL** (law 5). Per-frame dispatch on content tonality. Off by
    /// default until the calculator banks a threshold from a harvest carrying the
    /// full speed pair (law 7's opt-in → default-on ladder).
    Auto,
}

/// Encoder options.
#[derive(Debug, Clone, Copy)]
pub struct AacEncoderConfig {
    /// Target bitrate in bits per second. Drives the per-frame rate loop.
    /// Default: 128 000 (128 kbps).
    pub bitrate_bps: u32,
    /// Window-shape policy (arm A8). Default [`WindowShape::Sine`] — the
    /// byte-identical neutral end.
    pub window_shape: WindowShape,
    /// The [`WindowShape::Auto`] gate's threshold, as a **percentile of this
    /// clip's own** frame-tonality distribution (great-gate law 1: a
    /// population-relative threshold transfers to content the fit never saw; an
    /// absolute one does not). Frames above it code with KBD.
    ///
    /// Default 0.5 is a **placeholder, not a fitted value** — the calculator has
    /// not banked this rung yet. `aacharvest` emits the CSV that will decide it.
    pub shape_tonality_pct: f32,
    /// **Arm A1, Rung 1** — run the psychoacoustic model on short blocks instead
    /// of coding them with flat scalefactors. Default `false` (byte-identical).
    pub short_block_psy: bool,
    /// **Arm 1a, Rung 1** — split each short-block frame into window groups of
    /// similar energy and shape scalefactors per group (the short-block psy model
    /// run per group, centred jointly so a quiet pre-attack group is coded finer
    /// than the attack). Default `false` (byte-identical).
    pub window_grouping: bool,
    /// **Arm A2, Rung 2** — make the signal-to-mask ratio a function of band
    /// tonality instead of a flat 18 dB. Default `false` (byte-identical).
    pub tonality_smr: bool,
    /// **Arm A3, Rung 3** — emit TNS on long blocks. Default `false`
    /// (`tns_data_present = 0`, byte-identical).
    pub tns: bool,
    /// **Arm A9** — use the level-invariant transient detector. Default `false`
    /// (byte-identical). Arm A1 is unmeasurable without this.
    pub relative_transients: bool,
    /// **Arm A6** — Perceptual Noise Substitution on long blocks. Default `false`
    /// (byte-identical).
    pub pns: bool,
    /// **Arm A7** — intensity stereo on long-block CPEs. Default `false`
    /// (byte-identical).
    pub intensity: bool,
    /// **Arm A13** — split a channel pair's bit budget by perceptual demand
    /// rather than evenly. Default `false` (byte-identical).
    pub stereo_bit_split: bool,
}

impl Default for AacEncoderConfig {
    fn default() -> Self {
        Self {
            bitrate_bps: 128_000,
            window_shape: WindowShape::Sine,
            shape_tonality_pct: 0.5,
            short_block_psy: false,
            window_grouping: false,
            tonality_smr: false,
            tns: false,
            // DEFAULT ON — measured wins vs the previous default, PEAQ ODG:
            //   A9  mean +0.267, worst +0.000 (percussive +1.8, clean speech
            //       +1.1..+2.6, exactly 0.00 on every other class)
            //   A13 guitar stereo +1.17 @128k, piano stereo +0.22, routed by
            //       pair correlation so wide stereo is not harmed
            relative_transients: true,
            pns: false,
            intensity: false,
            stereo_bit_split: true,
        }
    }
}

/// One encoded raw access unit (a `raw_data_block`, no ADTS framing).
#[derive(Debug, Clone)]
pub struct EncodedPacket {
    pub data: Vec<u8>,
    /// Presentation timestamp in samples (frame index × 1024).
    pub pts: i64,
    /// Duration in samples (always one AAC frame = 1024).
    pub duration: u32,
}

/// AAC-LC encoder. Push PCM with [`push_pcm`](AacEncoder::push_pcm) (or
/// [`push_pcm_planar`](AacEncoder::push_pcm_planar)), call
/// [`finish`](AacEncoder::finish), then drain [`next_packet`](AacEncoder::next_packet)
/// until it returns [`Error::Eof`].
pub struct AacEncoder {
    sample_rate: u32,
    channels: usize,
    fs_index: u8,
    /// Target bitrate (bits/s). Drives the per-frame rate loop.
    bitrate: u32,
    /// Arm A8 policy and its provisional threshold.
    window_shape: WindowShape,
    shape_tonality_pct: f32,
    short_block_psy: bool,
    window_grouping: bool,
    tonality_smr: bool,
    tns: bool,
    relative_transients: bool,
    pns: bool,
    intensity: bool,
    stereo_bit_split: bool,
    win: Vec<f32>,
    /// The KBD long window (α = 4.0), matching the decoder's. Built once.
    win_kbd: Vec<f32>,
    chans: Vec<Vec<f32>>,
    initialized: bool,
    /// Encoded raw access units (`raw_data_block`, no ADTS) awaiting `next_packet`,
    /// each with its sample-domain PTS. Filled on `finish`.
    queue: VecDeque<(Vec<u8>, i64)>,
    flushed: bool,
}

impl AacEncoder {
    #[must_use]
    pub fn new(config: AacEncoderConfig) -> Self {
        Self {
            sample_rate: 0,
            channels: 0,
            fs_index: 0,
            bitrate: config.bitrate_bps.max(1),
            window_shape: config.window_shape,
            shape_tonality_pct: config.shape_tonality_pct,
            short_block_psy: config.short_block_psy,
            window_grouping: config.window_grouping,
            tonality_smr: config.tonality_smr,
            tns: config.tns,
            relative_transients: config.relative_transients,
            pns: config.pns,
            intensity: config.intensity,
            stereo_bit_split: config.stereo_bit_split,
            win: Vec::new(),
            win_kbd: Vec::new(),
            chans: Vec::new(),
            initialized: false,
            queue: VecDeque::new(),
            flushed: false,
        }
    }

    /// The stream's sample rate (0 until the first PCM is pushed).
    #[must_use]
    pub fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

    /// The stream's channel count (0 until the first PCM is pushed).
    #[must_use]
    pub fn channels(&self) -> u16 {
        self.channels as u16
    }

    /// Initialize (or validate) the stream parameters from a PCM push.
    fn init(&mut self, channels: u16, sample_rate: u32) -> Result<()> {
        if !self.initialized {
            self.sample_rate = sample_rate;
            self.channels = channels.max(1) as usize;
            // Reject counts with no `channel_configuration`. Writing a config the
            // element sequence does not match produces a stream that only our own
            // decoder accepts — the bug this check exists to make impossible.
            if element_plan(self.channels).is_none() {
                return Err(Error::unsupported(
                    "aac encode: only 1-6 channels are supported (7+ needs a                      program_config_element, which is not implemented)",
                ));
            }
            self.fs_index = crate::sf_index_for_rate(sample_rate)
                .ok_or_else(|| Error::invalid("aac encode: unsupported sample rate"))?;
            self.win = crate::dsp::sine_window(LONG_N);
            // Arm A8's second shape. α = 4.0 for the long window matches the
            // decoder's `kbd` exactly — a mismatched α is not a quality choice,
            // it breaks TDAC and leaves audible seams at every overlap.
            self.win_kbd = crate::dsp::kbd_window(LONG_N, 4.0);
            self.chans = vec![Vec::new(); self.channels];
            self.initialized = true;
        } else if channels.max(1) as usize != self.channels {
            return Err(Error::invalid(
                "aac encode: channel count changed mid-stream",
            ));
        }
        Ok(())
    }

    /// Buffer interleaved `f32` PCM in [-1, 1] (`interleaved.len()` must be a
    /// multiple of `channels`). The first push fixes the stream's channel count
    /// and sample rate. Samples beyond ±[`MAX_INPUT_AMPLITUDE`] are clamped and
    /// NaN becomes silence, so no input can push a block past the decoder
    /// buffer.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Unsupported`] for more than six channels,
    /// [`Error::InvalidData`] for a non-standard sample rate, and
    /// [`Error::InvalidData`] if `channels` differs from the first push.
    pub fn push_pcm(&mut self, interleaved: &[f32], channels: u16, sample_rate: u32) -> Result<()> {
        self.init(channels, sample_rate)?;
        let ch = self.channels;
        let n = interleaved.len() / ch;
        // Reserve once, then one pass per channel: a per-sample push grew each
        // channel buffer by doubling (a realloc + copy of everything so far, ~log2
        // of the stream per channel). Mono is a straight slice copy.
        if ch == 1 {
            self.chans[0].extend(interleaved[..n].iter().map(|&x| sanitize(x)));
        } else {
            for (c, dst) in self.chans.iter_mut().enumerate().take(ch) {
                dst.reserve(n);
                dst.extend(
                    interleaved[..n * ch]
                        .iter()
                        .skip(c)
                        .step_by(ch)
                        .map(|&x| sanitize(x)),
                );
            }
        }
        Ok(())
    }

    /// Buffer planar `f32` PCM, one slice per channel (`planes.len()` is the
    /// channel count; all planes the same length).
    ///
    /// # Errors
    ///
    /// As [`push_pcm`](Self::push_pcm).
    pub fn push_pcm_planar(&mut self, planes: &[&[f32]], sample_rate: u32) -> Result<()> {
        self.init(planes.len() as u16, sample_rate)?;
        for (c, plane) in planes.iter().enumerate().take(self.channels) {
            self.chans[c].extend(plane.iter().map(|&x| sanitize(x)));
        }
        Ok(())
    }

    /// Encode all buffered samples into per-frame raw access units (`raw_data_block`,
    /// no ADTS) with sample-domain PTS. A trailing all-zero block flushes the MDCT
    /// overlap so the final audio block decodes. Containers add their own framing
    /// (ADTS header for `.aac`, `esds` + raw samples for MP4).
    /// Block `b` of channel `ch`, borrowed straight from the buffered input when
    /// it lies wholly inside it; only a block running past the end is built
    /// (zero-padded). Every frame used to copy four 4 KB blocks this way.
    fn block_cow(&self, ch: usize, b: usize) -> std::borrow::Cow<'_, [f32; FRAME_LEN]> {
        let s = b * FRAME_LEN;
        match self.chans[ch].get(s..s + FRAME_LEN) {
            Some(x) => std::borrow::Cow::Borrowed(x.try_into().expect("FRAME_LEN samples")),
            None => std::borrow::Cow::Owned(self.block(ch, b)),
        }
    }

    /// One block's samples for channel `ch` (zero-padded past the buffered input).
    fn block(&self, ch: usize, b: usize) -> [f32; FRAME_LEN] {
        let mut cur = [0f32; FRAME_LEN];
        for (i, s) in cur.iter_mut().enumerate() {
            *s = self.chans[ch]
                .get(b * FRAME_LEN + i)
                .copied()
                .unwrap_or(0.0);
        }
        cur
    }

    /// Encode one frame `b` to its raw access unit + PTS. A pure function of the
    /// buffered input, the window sequences, and `b` — the previous block (for MDCT
    /// overlap) is just block `b-1`'s samples — so frames encode **independently**.
    #[allow(clippy::too_many_arguments)]
    #[allow(clippy::too_many_arguments)]
    fn encode_frame(
        &self,
        b: usize,
        swb: &[u16],
        swb_s: &[u16],
        sine_s: &[f32],
        kbd_s: &[f32],
        seqs: &[Vec<WindowSequence>],
        shapes: &[bool],
        per_channel: usize,
        plan: &[Elem],
    ) -> (Vec<u8>, i64) {
        let prev = |ch: usize| -> std::borrow::Cow<'_, [f32; FRAME_LEN]> {
            if b == 0 {
                std::borrow::Cow::Owned([0f32; FRAME_LEN])
            } else {
                self.block_cow(ch, b - 1)
            }
        };
        // Arm A8. The shape of frame b-1 owns this frame's LEFT overlap half, so
        // both are needed; the decoder seeds prev = sine at stream start.
        let cur_kbd = shapes.get(b).copied().unwrap_or(false);
        let prev_kbd = if b == 0 {
            false
        } else {
            shapes.get(b - 1).copied().unwrap_or(false)
        };
        let short_win: &[f32] = if cur_kbd { kbd_s } else { sine_s };
        let psy = PsyCfg {
            short_block_psy: self.short_block_psy,
            window_grouping: self.window_grouping,
            tonality_smr: self.tonality_smr,
            tns: self.tns,
            pns: self.pns,
            intensity: self.intensity,
            stereo_bit_split: self.stereo_bit_split,
        };

        let mut rdb = BitWriter::new();
        // One pass over the ISO element sequence. `seqs` is indexed by ELEMENT,
        // not by channel: a CPE shares one window sequence across its pair, so
        // the two channels' transient flags were joined when the plan was built.
        for (ei, elem) in plan.iter().enumerate() {
            let seq = seqs[ei][b];
            let tag = ei as u32;
            match *elem {
                Elem::Cpe(l, r) => {
                    let (p0, c0, p1, c1) =
                        (prev(l), self.block_cow(l, b), prev(r), self.block_cow(r, b));
                    if seq == WindowSequence::EightShort {
                        let sl = analyze_short(&p0, &c0, short_win);
                        let sr_ = analyze_short(&p1, &c1, short_win);
                        encode_cpe(
                            &mut rdb,
                            tag,
                            &sl,
                            &sr_,
                            swb_s,
                            seq,
                            self.sample_rate,
                            per_channel,
                            cur_kbd,
                            psy,
                        );
                    } else {
                        let win = long_window(
                            seq,
                            prev_kbd,
                            cur_kbd,
                            &self.win,
                            &self.win_kbd,
                            sine_s,
                            kbd_s,
                        );
                        let sl = analyze_long(&p0, &c0, &win);
                        let sr_ = analyze_long(&p1, &c1, &win);
                        encode_cpe(
                            &mut rdb,
                            tag,
                            &sl,
                            &sr_,
                            swb,
                            seq,
                            self.sample_rate,
                            per_channel,
                            cur_kbd,
                            psy,
                        );
                    }
                }
                Elem::Sce(ch) | Elem::Lfe(ch) => {
                    let (p, c) = (prev(ch), self.block_cow(ch, b));
                    let id = elem.id();
                    if seq == WindowSequence::EightShort {
                        let spec = analyze_short(&p, &c, short_win);
                        encode_channel_element_short(
                            &mut rdb,
                            tag,
                            &spec,
                            swb_s,
                            per_channel,
                            cur_kbd,
                            self.sample_rate,
                            psy,
                        );
                    } else {
                        let win = long_window(
                            seq,
                            prev_kbd,
                            cur_kbd,
                            &self.win,
                            &self.win_kbd,
                            sine_s,
                            kbd_s,
                        );
                        let spec = analyze_long(&p, &c, &win);
                        encode_channel_element(
                            &mut rdb,
                            tag,
                            &spec,
                            swb,
                            seq,
                            self.sample_rate,
                            per_channel,
                            cur_kbd,
                            psy,
                            id,
                        );
                    }
                }
            }
        }
        rdb.write(ID_END, 3);
        (rdb.into_bytes(), (b * FRAME_LEN) as i64)
    }

    /// Transient flags for one channel, under the configured detector (arm A9).
    fn transients(&self, chan: &[f32], nframes: usize) -> Vec<bool> {
        if self.relative_transients {
            detect_transients_relative(chan, nframes)
        } else {
            detect_transients(chan, nframes)
        }
    }

    /// **The Rung 0 gate.** Decide each frame's window shape.
    ///
    /// Computed as a whole-stream pre-pass, exactly like `detect_transients`, for
    /// two reasons: the decision is shared by both channels of a CPE (one common
    /// window), and frame b's window depends on frame b−1's shape — so deciding
    /// inside the frame-parallel worker would make the output depend on thread
    /// scheduling. Deciding up front keeps `encode_frame` independent and the
    /// encoder byte-deterministic (asserted by the lab's null arm).
    ///
    /// Threshold form: **per-clip percentile** of frame tonality (law 1), not an
    /// absolute. `Auto` is PROVISIONAL and off by default (law 7).
    fn decide_shapes(&self, nblocks: usize) -> Vec<bool> {
        match self.window_shape {
            WindowShape::Sine => vec![false; nblocks],
            WindowShape::Kbd => vec![true; nblocks],
            WindowShape::Auto => {
                // One reading per frame, from channel 0 (a CPE shares one window,
                // so the decision cannot be per-channel).
                let ton: Vec<f32> = (0..nblocks)
                    .map(|b| time_tonality(&self.block_cow(0, b)[..]))
                    .collect();
                let atk = frame_attack_ratios(&self.chans[0], nblocks);

                let pct = |v: &[f32], q: f32| -> f32 {
                    let mut s = v.to_vec();
                    s.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                    let i =
                        ((s.len().saturating_sub(1)) as f32 * q.clamp(0.0, 1.0)).round() as usize;
                    s.get(i).copied().unwrap_or(f32::INFINITY)
                };
                let ton_thresh = pct(&ton, self.shape_tonality_pct);
                let atk_thresh = pct(&atk, SHAPE_ATTACK_VETO_PCT);

                // A depth-2 transcribed tree (great-gate law 3: shallow, in plain
                // code, one documented feature per node).
                //
                //   attack  — a frame with a strong onset takes SINE, whatever its
                //             tonality. KBD's wider main lobe smears transients in
                //             time; the force-on ladder measured +18.6% audible on
                //             the percussive class, the largest single effect in
                //             the whole Rung 0 harvest.
                //   tonality— among the remaining (stationary) frames, KBD's much
                //             deeper stopband is worth having.
                //
                // The veto is why this gate is not simply "tonality > t": ringing
                // percussion reads as HIGHLY tonal (a struck resonator is a decaying
                // sinusoid), so a tonality-only rule routes exactly the frames that
                // KBD hurts most. The first fit did precisely that.
                // The veto spans a ±1-frame NEIGHBOURHOOD, not just the frame
                // itself. Overlap-add means frame b's window shapes the overlap
                // with both b−1 and b+1, so an onset in either neighbour is
                // analyzed partly through this frame's window. Vetoing only the
                // onset frame left the ring-down frame on KBD and kept the whole
                // percussive regression (+10.0% audible, barely half of force-on's
                // +18.6%) — the measurement that forced this widening.
                let vetoed: Vec<bool> = (0..nblocks)
                    .map(|b| {
                        let lo = b.saturating_sub(1);
                        let hi = (b + 1).min(nblocks.saturating_sub(1));
                        (lo..=hi).any(|k| atk.get(k).copied().unwrap_or(1.0) > atk_thresh)
                    })
                    .collect();

                (0..nblocks)
                    .map(|b| !vetoed[b] && ton[b] > ton_thresh)
                    .collect()
            }
        }
    }

    /// Encode all buffered frames to raw access units. Frames are independent, so
    /// they fan out across worker threads (ffmpeg's AAC encoder is single-threaded).
    fn encode_stream(&self) -> Vec<(Vec<u8>, i64)> {
        let swb = swb_offsets(true, self.fs_index);
        let swb_s = swb_offsets(false, self.fs_index);
        let sine_s = crate::dsp::sine_window(SHORT_N);
        // α = 6.0 for the short window, matching the decoder's `kbd_s`.
        let kbd_s = crate::dsp::kbd_window(SHORT_N, 6.0);
        let n = self.chans.first().map_or(0, std::vec::Vec::len);
        let nblocks = n.div_ceil(FRAME_LEN) + 1;
        // Per-channel ICS-body budget from the target bitrate (minus framing).
        let frame_budget = (self.bitrate as usize * FRAME_LEN / self.sample_rate.max(1) as usize)
            .saturating_sub(59); // ADTS header (56) + END (3)
                                 // Never above the decoder input buffer (ISO 14496-3 §4.5.3: 6144 bits per
                                 // channel) less the element and framing overhead: an absurd bitrate must
                                 // not yield a block a conforming decoder cannot hold.
        let per_channel = (frame_budget / self.channels.max(1))
            .saturating_sub(7) // element overhead
            .min(MAX_CHANNEL_BITS);

        // The ISO element sequence for this channel count (validated at init, so
        // the unwrap cannot fire here).
        let plan = element_plan(self.channels).expect("channel count validated in init");

        // Window sequences, **one per element**. A CPE shares a common window, so
        // its two channels' transient flags are joined; an LFE is long-only by
        // ISO rule, not by choice.
        let seqs: Vec<Vec<WindowSequence>> = plan
            .iter()
            .map(|e| match *e {
                Elem::Cpe(l, r) => {
                    let t0 = self.transients(&self.chans[l], nblocks);
                    let t1 = self.transients(&self.chans[r], nblocks);
                    let joint: Vec<bool> = (0..nblocks).map(|b| t0[b] || t1[b]).collect();
                    assign_sequences(&joint)
                }
                Elem::Sce(ch) => assign_sequences(&self.transients(&self.chans[ch], nblocks)),
                // lfe_channel_element carries no window-shape freedom.
                Elem::Lfe(_) => vec![WindowSequence::OnlyLong; nblocks],
            })
            .collect();
        // Arm A8: decide every frame's window shape up front, for the same reason
        // the sequences are decided up front — frame b's window depends on frame
        // b−1's shape, so this cannot live inside the parallel worker.
        let shapes = self.decide_shapes(nblocks);

        // Slice views the worker threads share (all read-only).
        let (swb, swb_s, sine_s, kbd_s, seqs, shapes, plan) = (
            swb,
            swb_s,
            &sine_s[..],
            &kbd_s[..],
            &seqs[..],
            &shapes[..],
            &plan[..],
        );
        let frame = |b: usize| {
            self.encode_frame(
                b,
                swb,
                swb_s,
                sine_s,
                kbd_s,
                seqs,
                shapes,
                per_channel,
                plan,
            )
        };

        let nthreads = std::thread::available_parallelism()
            .map_or(1, std::num::NonZero::get)
            .min(nblocks.max(1));
        if nthreads <= 1 || nblocks < 16 {
            return (0..nblocks).map(frame).collect(); // serial for tiny inputs
        }
        // Fan out contiguous frame ranges; concatenating the parts keeps them ordered.
        let chunk = nblocks.div_ceil(nthreads);
        let parts: Vec<Vec<(Vec<u8>, i64)>> = std::thread::scope(|s| {
            (0..nthreads)
                .map(|t| {
                    s.spawn(move || {
                        let end = ((t + 1) * chunk).min(nblocks);
                        (t * chunk..end).map(frame).collect::<Vec<_>>()
                    })
                })
                .collect::<Vec<_>>()
                .into_iter()
                .map(|h| h.join().unwrap())
                .collect()
        });
        parts.into_iter().flatten().collect()
    }

    /// Signal end-of-input: encode all buffered PCM (frame-parallel) so packets
    /// can be drained via [`next_packet`](AacEncoder::next_packet). Idempotent.
    pub fn finish(&mut self) {
        if self.flushed {
            return;
        }
        self.flushed = true;
        if self.initialized {
            self.queue = self.encode_stream().into();
        }
    }

    /// Retrieve the next encoded access unit. Returns [`Error::Again`] before
    /// [`finish`](AacEncoder::finish) has been called, and [`Error::Eof`] once
    /// fully drained.
    ///
    /// # Errors
    ///
    /// [`Error::Again`] until [`finish`](AacEncoder::finish) is called, then
    /// [`Error::Eof`] once every packet has been returned.
    pub fn next_packet(&mut self) -> Result<EncodedPacket> {
        if let Some((data, pts)) = self.queue.pop_front() {
            return Ok(EncodedPacket {
                data,
                pts,
                duration: FRAME_LEN as u32,
            });
        }
        if self.flushed {
            Err(Error::Eof)
        } else {
            Err(Error::Again)
        }
    }
}

impl Default for AacEncoder {
    fn default() -> Self {
        Self::new(AacEncoderConfig::default())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bits::BitReader;
    use crate::codebook::decode_tuple;

    /// Drain the encoder into a concatenated ADTS elementary stream — the container
    /// framing the encoder no longer emits itself — for decoder/ffmpeg validation.
    fn encode_to_adts(enc: &mut AacEncoder) -> Vec<u8> {
        enc.finish();
        let mut out = Vec::new();
        while let Ok(p) = enc.next_packet() {
            let hdr = AdtsHeader {
                object_type: 2,
                sample_rate: enc.sample_rate,
                channels: enc.channels as u16,
                frame_length: 7 + p.data.len(),
                header_len: 7,
            };
            out.extend_from_slice(&write_adts_header(&hdr));
            out.extend_from_slice(&p.data);
        }
        out
    }

    /// Every `dim`-tuple drawn from `vals`.
    fn product(vals: &[i32], dim: usize) -> Vec<Vec<i32>> {
        let mut out = vec![vec![]];
        for _ in 0..dim {
            let mut next = Vec::new();
            for prefix in &out {
                for &v in vals {
                    let mut t = prefix.clone();
                    t.push(v);
                    next.push(t);
                }
            }
            out = next;
        }
        out
    }

    /// Encode every representable tuple of every spectral codebook, then decode it
    /// back through the REAL decoder — must match exactly (codeword + sign + escape),
    /// and the written bit count must equal `spectral_bits`.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn spectral_encode_roundtrips_through_decoder() {
        for cb_num in 1..=11usize {
            let cb = &CODEBOOKS[cb_num];
            let dim = cb.dim as usize;
            let lav = i32::from(cb.lav);

            // Signed books offset by lav; unsigned carry a sign, so cover ±lav.
            // Book 11 (escape) also probes magnitudes beyond lav, both signs.
            let vals: Vec<i32> = if cb.esc {
                let mut v: Vec<i32> = (-lav..=lav).collect();
                for &m in &[16, 17, 31, 32, 69, 100] {
                    v.push(m);
                    v.push(-m);
                }
                v
            } else {
                (-lav..=lav).collect()
            };

            for tuple in product(&vals, dim) {
                if tuple_index(cb, &tuple).is_none() {
                    continue;
                }
                let mut w = BitWriter::new();
                spectral_emit(cb_num, &tuple, &mut w);
                assert_eq!(
                    w.bit_len(),
                    spectral_bits(cb_num, &tuple).unwrap(),
                    "cb {cb_num} tuple {tuple:?}: bit count mismatch"
                );
                let bytes = w.into_bytes();
                let mut r = BitReader::new(&bytes);
                let mut out = [0i32; 4];
                decode_tuple(cb, spectral_book(cb_num as u8), &mut r, &mut out).unwrap();
                assert_eq!(
                    &out[..dim],
                    &tuple[..dim],
                    "cb {cb_num} tuple {tuple:?}: round-trip"
                );
            }
        }
    }

    /// Forward MDCT → decoder synthesis (imdct·window·overlap-add) reconstructs the
    /// signal (delayed one frame) — proves the window + overlap + 32768 scaling.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn long_filterbank_reconstructs_via_decoder_math() {
        let win = crate::dsp::sine_window(LONG_N);
        let nblocks = 6usize;
        let signal: Vec<f32> = (0..nblocks * FRAME_LEN)
            .map(|i| (0.3 * (i as f64 * 0.017).sin() + 0.2 * (i as f64 * 0.003).cos()) as f32)
            .collect();

        let mut prev = [0f32; FRAME_LEN];
        let mut specs = Vec::new();
        for b in 0..nblocks {
            let mut cur = [0f32; FRAME_LEN];
            cur.copy_from_slice(&signal[b * FRAME_LEN..(b + 1) * FRAME_LEN]);
            specs.push(analyze_long(&prev, &cur, &win));
            prev = cur;
        }

        let mut overlap = [0f32; FRAME_LEN];
        let mut out = Vec::new();
        for spec in &specs {
            let time = crate::dsp::imdct(spec);
            let frame: Vec<f32> = (0..LONG_N).map(|n| time[n] * win[n] / SPEC_SCALE).collect();
            let mut o = [0f32; FRAME_LEN];
            for n in 0..FRAME_LEN {
                o[n] = frame[n] + overlap[n];
                overlap[n] = frame[FRAME_LEN + n];
            }
            out.extend_from_slice(&o);
        }

        // Output lags the input by one frame; the interior must reconstruct.
        for i in FRAME_LEN..(nblocks - 1) * FRAME_LEN {
            assert!(
                (out[i] - signal[i - FRAME_LEN]).abs() < 1e-3,
                "at {i}: {} vs {}",
                out[i],
                signal[i - FRAME_LEN]
            );
        }
    }

    /// The block-switching sequence `OnlyLong` → `LongStart` → `EightShort` → `LongStop` →
    /// `OnlyLong` must reconstruct through the decoder's synthesis math (TDAC across
    /// the transition windows) — the decisive check that the short filterbank and
    /// transition windows are exact inverses.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn short_block_sequence_reconstructs_via_decoder_math() {
        use WindowSequence::*;
        let sine_l = crate::dsp::sine_window(LONG_N);
        let sine_s = crate::dsp::sine_window(SHORT_N);
        // Arm A8's shapes, unused by this test (it exercises the sine arm) but
        // required by the shape-aware signature.
        let kbd_l_t = crate::dsp::kbd_window(LONG_N, 4.0);
        let kbd_s_t = crate::dsp::kbd_window(SHORT_N, 6.0);
        let seqs = [
            OnlyLong, LongStart, EightShort, LongStop, OnlyLong, OnlyLong,
        ];
        let nblocks = seqs.len();
        let signal: Vec<f32> = (0..nblocks * FRAME_LEN)
            .map(|i| (0.3 * (i as f64 * 0.019).sin() + 0.25 * (i as f64 * 0.007).cos()) as f32)
            .collect();

        // Forward: per-frame filterbank chosen by the window sequence.
        let mut prev = [0f32; FRAME_LEN];
        let mut specs = Vec::new();
        for (b, &seq) in seqs.iter().enumerate() {
            let mut cur = [0f32; FRAME_LEN];
            cur.copy_from_slice(&signal[b * FRAME_LEN..(b + 1) * FRAME_LEN]);
            let spec = if seq == EightShort {
                analyze_short(&prev, &cur, &sine_s)
            } else {
                analyze_long(
                    &prev,
                    &cur,
                    &long_window(seq, false, false, &sine_l, &kbd_l_t, &sine_s, &kbd_s_t),
                )
            };
            specs.push((seq, spec));
            prev = cur;
        }

        // Synthesis: mirror the decoder (short_frame vs imdct·window) + overlap-add.
        let mut overlap = [0f32; FRAME_LEN];
        let mut out = Vec::new();
        for (seq, spec) in &specs {
            let frame: Vec<f32> = if *seq == EightShort {
                let mut f = vec![0f32; LONG_N];
                for w in 0..8 {
                    let time = crate::dsp::imdct(&spec[w * SHORT_HALF..(w + 1) * SHORT_HALF]);
                    let off = 448 + w * SHORT_HALF;
                    for n in 0..SHORT_N {
                        f[off + n] += time[n] * sine_s[n] / SPEC_SCALE;
                    }
                }
                f
            } else {
                let time = crate::dsp::imdct(spec);
                let win = long_window(*seq, false, false, &sine_l, &kbd_l_t, &sine_s, &kbd_s_t);
                (0..LONG_N).map(|n| time[n] * win[n] / SPEC_SCALE).collect()
            };
            let mut o = [0f32; FRAME_LEN];
            for n in 0..FRAME_LEN {
                o[n] = frame[n] + overlap[n];
                overlap[n] = frame[FRAME_LEN + n];
            }
            out.extend_from_slice(&o);
        }

        // Interior (past the priming frame) must reconstruct across all transitions.
        for i in FRAME_LEN..(nblocks - 1) * FRAME_LEN {
            assert!(
                (out[i] - signal[i - FRAME_LEN]).abs() < 1e-3,
                "at {i}: {} vs {}",
                out[i],
                signal[i - FRAME_LEN]
            );
        }
    }

    #[test]
    fn window_sequences_are_valid() {
        use WindowSequence::*;
        // Isolated transient → bracketed by LongStart / LongStop.
        assert_eq!(
            assign_sequences(&[false, false, false, true, false, false]),
            vec![OnlyLong, OnlyLong, LongStart, EightShort, LongStop, OnlyLong]
        );
        // Adjacent transients → one short run.
        assert_eq!(
            assign_sequences(&[false, false, true, true, false]),
            vec![OnlyLong, LongStart, EightShort, EightShort, LongStop]
        );
        // A single-frame gap is merged (can't be both a stop and a start).
        assert_eq!(
            assign_sequences(&[false, true, false, true, false]),
            vec![LongStart, EightShort, EightShort, EightShort, LongStop]
        );
    }

    #[test]
    fn transient_is_detected() {
        let sr = 44100.0f64;
        let nframes = 5;
        let n = nframes * FRAME_LEN;
        let mut s = vec![0f32; n];
        for (i, x) in s.iter_mut().enumerate() {
            *x = (0.02 * (2.0 * std::f64::consts::PI * 400.0 * i as f64 / sr).sin()) as f32;
        }
        // A loud burst at the start of frame 2.
        for i in 0..600 {
            let env = (1.0 - i as f64 / 600.0).max(0.0);
            s[2 * FRAME_LEN + i] +=
                (0.8 * env * (2.0 * std::f64::consts::PI * 3000.0 * i as f64 / sr).sin()) as f32;
        }
        let flags = detect_transients(&s, nframes);
        assert!(flags[2], "the burst frame must be flagged");
        assert!(!flags[0] && !flags[4], "steady frames must not be flagged");
    }

    /// End-to-end: a signal with a sharp attack must actually emit `EightShort`
    /// blocks and still decode cleanly through our decoder.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn transient_encodes_short_and_decodes() {
        let sr = 44100u32;
        let nframes = 5;
        let n = nframes * FRAME_LEN;
        let mut interleaved = Vec::new();
        for i in 0..n {
            let t = i as f64 / f64::from(sr);
            let mut v = 0.02 * (2.0 * std::f64::consts::PI * 400.0 * t).sin();
            if (2 * FRAME_LEN..2 * FRAME_LEN + 600).contains(&i) {
                let k = i - 2 * FRAME_LEN;
                let env = (1.0 - k as f64 / 600.0).max(0.0);
                v += 0.8
                    * env
                    * (2.0 * std::f64::consts::PI * 3000.0 * k as f64 / f64::from(sr)).sin();
            }
            interleaved.push(v as f32);
        }

        let mut enc = AacEncoder::default();
        enc.push_pcm(&interleaved, 1, sr).unwrap();
        let adts = encode_to_adts(&mut enc);

        // Parse each frame's first SCE window_sequence; EightShort (2) must appear.
        let mut saw_short = false;
        let mut pos = 0usize;
        let mut decoded = 0usize;
        let mut dec = crate::decode::Decoder::new(sr);
        while pos + 7 <= adts.len() {
            let hdr = crate::parse_adts(&adts[pos..]).unwrap();
            let au = &adts[pos + hdr.header_len..pos + hdr.frame_length];
            let mut r = crate::BitReader::new(au);
            assert_eq!(r.read_bits(3).unwrap(), ID_SCE); // mono SCE
            let _ = r.read_bits(4).unwrap(); // tag
            let _ = r.read_bits(8).unwrap(); // global_gain
            let _ = r.read_bit().unwrap(); // ics_reserved
            if r.read_bits(2).unwrap() == WindowSequence::EightShort.to_bits() {
                saw_short = true;
            }
            decoded += dec.decode(au, None).unwrap().frames();
            pos += hdr.frame_length;
        }
        assert!(saw_short, "a transient must produce EightShort blocks");
        assert!(decoded >= n, "decoded fewer samples than encoded");
    }

    /// The `ms_mask_present` (2 bits) of the first frame's CPE — 0 = no M/S.
    fn first_cpe_ms_mask(adts: &[u8]) -> u32 {
        let hdr = crate::parse_adts(adts).unwrap();
        let au = &adts[hdr.header_len..hdr.frame_length];
        let mut r = crate::BitReader::new(au);
        assert_eq!(r.read_bits(3).unwrap(), ID_CPE);
        let _tag = r.read_bits(4).unwrap();
        assert_eq!(r.read_bit().unwrap(), 1); // common_window
        let _reserved = r.read_bit().unwrap();
        let ws = r.read_bits(2).unwrap();
        let _shape = r.read_bit().unwrap();
        if ws == WindowSequence::EightShort.to_bits() {
            let _ = r.read_bits(4).unwrap(); // max_sfb
            let _ = r.read_bits(7).unwrap(); // grouping
        } else {
            let _ = r.read_bits(6).unwrap(); // max_sfb
            let _ = r.read_bit().unwrap(); // predictor_data_present
        }
        r.read_bits(2).unwrap()
    }

    /// Stereo with L = R (fully correlated) must pick M/S and reconstruct the two
    /// channels identically — the CPE + mid/side round-trip.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn stereo_ms_roundtrips_mono_content() {
        let sr = 44100u32;
        let n = 8192usize;
        let mut interleaved = Vec::new();
        for i in 0..n {
            let s = (0.4 * (2.0 * std::f64::consts::PI * 600.0 * i as f64 / f64::from(sr)).sin())
                as f32;
            interleaved.push(s); // L
            interleaved.push(s); // R == L
        }
        let mut enc = AacEncoder::default();
        enc.push_pcm(&interleaved, 2, sr).unwrap();
        let adts = encode_to_adts(&mut enc);

        assert_ne!(
            first_cpe_ms_mask(&adts),
            0,
            "M/S should be chosen for L=R content"
        );

        let mut dec = crate::decode::Decoder::new(sr);
        let (mut lsum, mut diff) = (0f64, 0f64);
        let mut pos = 0usize;
        while pos + 7 <= adts.len() {
            let hdr = crate::parse_adts(&adts[pos..]).unwrap();
            let au = &adts[pos + hdr.header_len..pos + hdr.frame_length];
            let a = dec.decode(au, None).unwrap();
            for k in 0..a.frames() {
                let l = a.samples[k * 2];
                let rr = a.samples[k * 2 + 1];
                lsum += f64::from(l).powi(2);
                diff += f64::from(l - rr).powi(2);
            }
            pos += hdr.frame_length;
        }
        assert!(lsum > 0.0, "decoded silence");
        // L and R reconstruct identically (mono content preserved through M/S).
        assert!(
            diff / (lsum + 1e-9) < 1e-3,
            "L/R diverged: {}",
            diff / (lsum + 1e-9)
        );
    }

    /// The encoder→decoder round-trip is unity gain and length-preserving: decode
    /// the raw AUs directly (no container/engine) and check per-channel amplitude
    /// (≈0.283 RMS for a 0.4-amp sine) and frame count (~input + a little priming).
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn stereo_direct_decode_amplitude() {
        let sr = 44100u32;
        let n = sr as usize; // 1 s
        let mut interleaved = Vec::new();
        for i in 0..n {
            let t = i as f64 / f64::from(sr);
            let l = (0.4 * (2.0 * std::f64::consts::PI * 440.0 * t).sin()) as f32;
            let r = (0.4 * (2.0 * std::f64::consts::PI * 660.0 * t).sin()) as f32;
            interleaved.push(l);
            interleaved.push(r);
        }
        let mut enc = AacEncoder::default();
        enc.push_pcm(&interleaved, 2, sr).unwrap();
        enc.finish();
        let mut dec = crate::decode::Decoder::new(sr);
        let (mut lsq, mut rsq, mut cnt) = (0.0f64, 0.0f64, 0usize);
        while let Ok(p) = enc.next_packet() {
            let a = dec.decode(&p.data, None).unwrap();
            for k in 0..a.frames() {
                let l = a.samples[k * 2];
                let r = a.samples[k * 2 + 1];
                lsq += f64::from(l).powi(2);
                rsq += f64::from(r).powi(2);
                cnt += 1;
            }
        }
        let (lrms, rrms) = ((lsq / cnt as f64).sqrt(), (rsq / cnt as f64).sqrt());
        eprintln!("input {n}/ch; decoded {cnt}/ch; Lrms={lrms:.3} Rrms={rrms:.3} (unity≈0.283)");
        // ~1 output frame per input frame (a couple of priming/flush frames extra).
        assert!(
            cnt >= n && cnt < n + 4 * FRAME_LEN,
            "frame count off: {cnt} vs {n}"
        );
        // Unity gain per channel (lossy tolerance), no doubling.
        assert!((0.24..0.32).contains(&lrms), "L amplitude off: {lrms:.3}");
        assert!((0.24..0.32).contains(&rrms), "R amplitude off: {rrms:.3}");
    }

    /// Per-frame hot-path breakdown — which stage dominates encode time.
    #[test]
    #[ignore = "profiling; run with --ignored --nocapture"]
    fn profile_encode_hotpath() {
        use std::time::Instant;
        let sr = 44100u32;
        let fs = crate::sf_index_for_rate(sr).unwrap();
        let swb = swb_offsets(true, fs);
        let win = crate::dsp::sine_window(LONG_N);
        let mut cur = [0f32; FRAME_LEN];
        for (i, s) in cur.iter_mut().enumerate() {
            let t = i as f64 / f64::from(sr);
            *s = (0.3 * (2.0 * std::f64::consts::PI * 440.0 * t).sin()
                + 0.1 * (2.0 * std::f64::consts::PI * 3000.0 * t).sin()) as f32;
        }
        let prev = [0f32; FRAME_LEN];
        let iters = 500;
        let us = |d: std::time::Duration| d.as_secs_f64() * 1e6 / f64::from(iters);

        let t = Instant::now();
        let mut spec = Vec::new();
        for _ in 0..iters {
            spec = analyze_long(&prev, &cur, &win);
        }
        let mdct = t.elapsed();

        let t = Instant::now();
        for _ in 0..iters {
            let _ = perceptual_offsets(&spec, swb, sr, false);
        }
        let psy = t.elapsed();
        let offsets = perceptual_offsets(&spec, swb, sr, false);

        let t = Instant::now();
        for _ in 0..iters {
            let _ = Xpow::new(&spec);
        }
        let xpow_t = t.elapsed();
        let xp = Xpow::new(&spec);

        let t = Instant::now();
        for _ in 0..iters {
            let _ = rate_loop(&xp, swb, &offsets, 3000);
        }
        let rate = t.elapsed();

        let sf = scalefactors(&offsets, 120);
        let t = Instant::now();
        for _ in 0..iters {
            let _ = code_frame(&xp, swb, &sf);
        }
        let cf = t.elapsed();

        eprintln!("per-frame per-channel (avg over {iters}):");
        eprintln!("  analyze_long (MDCT): {:>8.1} us", us(mdct));
        eprintln!("  Xpow::new (once):    {:>8.1} us", us(xpow_t));
        eprintln!("  perceptual_offsets:  {:>8.1} us", us(psy));
        eprintln!("  rate_loop (~8x cf):  {:>8.1} us", us(rate));
        eprintln!("  one code_frame:      {:>8.1} us", us(cf));
        eprintln!(
            "  → MDCT is {:.0}% of (MDCT + rate_loop)",
            100.0 * mdct.as_secs_f64() / (mdct.as_secs_f64() + rate.as_secs_f64())
        );
    }

    /// The AVX-512 quantize kernel narrows via `trunc(min(v+0.5, MAX+0.5))` instead of
    /// the AVX2/scalar `min(round(v), MAX)`. The intrinsics can't run on a non-AVX-512
    /// host, but the identity they rely on — for the quantizer's always-nonnegative
    /// input — is checkable in scalar, so the untested path's *math* is pinned here.
    /// The quantize twins must equal the scalar oracle exactly — including at the
    /// adversarial rounding points (the double just below each `k + 0.5`, where
    /// `v + 0.5` rounds UP in f64 and a naive `floor(v + 0.5)` overshoots), at the
    /// `MAX_QUANT` clamp, and with tails that are not a multiple of the lane width.
    #[cfg(all(feature = "simd", target_arch = "x86_64"))]
    #[test]
    fn quantize_simd_matches_scalar() {
        let below = |x: f64| f64::from_bits(x.to_bits() - 1);
        let mut vals = vec![
            0.0f64,
            0.25,
            0.5,
            below(0.5),
            1.5,
            below(1.5),
            2.5,
            below(2.5),
            1e-300,
        ];
        vals.extend([
            8190.5,
            below(8190.5),
            8191.0,
            8191.5,
            8192.0,
            1e9,
            4503599627370495.5,
        ]);
        let mut seed = 0x9e37_79b9u32;
        for _ in 0..4000 {
            seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
            let k = f64::from(seed >> 20);
            vals.push(match seed & 3 {
                0 => below(k + 0.5),
                1 => k + 0.5,
                2 => f64::from(seed) / 1000.0,
                _ => k * 1.000_000_1,
            });
        }
        for n in [1usize, 3, 4, 5, 7, 8, 9, 31, vals.len()] {
            let pow = &vals[..n];
            let sign: Vec<i32> = (0..n).map(|i| if i % 3 == 0 { -1 } else { 1 }).collect();
            let mut want = vec![0i32; n];
            quantize_band_scalar(pow, &sign, 1.0, &mut want);
            if has_avx2() {
                let mut got = vec![0i32; n];
                // SAFETY: AVX2 detected; all slices are `n` long.
                unsafe { quantize_band_avx2(pow, &sign, 1.0, &mut got) };
                for i in 0..n {
                    assert_eq!(
                        got[i],
                        want[i],
                        "avx2 v={:e} (bits {:x})",
                        pow[i],
                        pow[i].to_bits()
                    );
                }
            }
            #[cfg(feature = "simd-avx512")]
            if has_avx512() {
                let mut got = vec![0i32; n];
                // SAFETY: AVX-512F detected; all slices are `n` long.
                unsafe { quantize_band_avx512(pow, &sign, 1.0, &mut got) };
                for i in 0..n {
                    assert_eq!(got[i], want[i], "avx512 v={:e}", pow[i]);
                }
            }
        }
    }

    /// `xpow_avx2` must handle a length that is not a multiple of its 4 lanes
    /// (its scalar tail once never advanced, an infinite loop masked only because
    /// every caller passes 1024 coefficients), and match the scalar formula.
    #[cfg(all(feature = "simd", target_arch = "x86_64"))]
    #[test]
    fn xpow_avx2_matches_scalar_with_tail() {
        if !has_avx2() {
            return;
        }
        for n in [0usize, 1, 3, 4, 5, 7, 129, 1023, 1024] {
            let spec: Vec<f32> = (0..n)
                .map(|i| ((i as f32 * 0.77).sin() * 3000.0) - 0.5)
                .collect();
            let (mut pow, mut sign) = (vec![0f64; n], vec![0i32; n]);
            // SAFETY: AVX2 detected above; the outputs are `n` long.
            unsafe { xpow_avx2(&spec, &mut pow, &mut sign) };
            for i in 0..n {
                let s = f64::from(spec[i].abs()).sqrt();
                assert_eq!(pow[i], s * s.sqrt(), "pow[{i}] n={n}");
                assert_eq!(
                    sign[i],
                    if spec[i] < 0.0 { -1 } else { 1 },
                    "sign[{i}] n={n}"
                );
            }
        }
    }

    #[test]
    fn avx512_trunc_identity_matches_round_clamp() {
        for &v in &[
            0.0f64, 0.4, 0.5, 0.6, 1.4, 1.5, 2.5, 100.5, 8190.9, 8191.0, 8191.5, 1e6,
        ] {
            let round_clamp = (v.round() as i64).min(i64::from(MAX_QUANT));
            let trunc_trick = (v + 0.5).min(f64::from(MAX_QUANT) + 0.5) as i64; // `as` truncates
            assert_eq!(round_clamp, trunc_trick, "mismatch at v={v}");
        }
    }

    #[test]
    fn bitwriter_msb_first() {
        let mut w = BitWriter::new();
        w.write(0b101, 3);
        w.write(0b1, 1);
        w.write(0b1111, 4);
        assert_eq!(w.bit_len(), 8);
        assert_eq!(w.into_bytes(), vec![0b1011_1111]);
    }

    #[test]
    fn audio_specific_config_roundtrips() {
        for &(sr, ch) in &[(44100u32, 2u16), (48000, 1), (96000, 2), (8000, 1)] {
            let cfg = AudioSpecificConfig {
                object_type: 2,
                sample_rate: sr,
                channels: ch,
            };
            let bytes = write_audio_specific_config(&cfg);
            assert_eq!(crate::parse_audio_specific_config(&bytes).unwrap(), cfg);
        }
    }

    #[test]
    fn adts_header_roundtrips() {
        let hdr = AdtsHeader {
            object_type: 2,
            sample_rate: 44100,
            channels: 2,
            frame_length: 512,
            header_len: 7,
        };
        let bytes = write_adts_header(&hdr);
        assert_eq!(bytes.len(), 7);
        assert!(crate::is_adts(&bytes));
        assert_eq!(crate::parse_adts(&bytes).unwrap(), hdr);
    }

    #[test]
    fn ics_info_long_reencodes_bit_exact() {
        // The decoder's own long-block test vector.
        let orig = [0x0C, 0x40];
        let info = crate::ics::parse_ics_info(&mut BitReader::new(&orig), 4).unwrap();
        let mut w = BitWriter::new();
        encode_ics_info(&mut w, &info);
        assert_eq!(w.into_bytes(), orig);
    }

    #[test]
    fn ics_info_short_grouping_roundtrips() {
        use crate::ics::{parse_ics_info, IcsInfo, WindowSequence};
        let info = IcsInfo {
            window_sequence: WindowSequence::EightShort,
            window_shape_kbd: false,
            max_sfb: 8,
            num_windows: 8,
            num_window_groups: 2,
            window_group_length: vec![3, 5],
            num_swb: 0, // encode ignores; the parser re-derives
        };
        let mut w = BitWriter::new();
        encode_ics_info(&mut w, &info);
        let parsed = parse_ics_info(&mut BitReader::new(&w.into_bytes()), 4).unwrap();
        assert_eq!(parsed.window_sequence, WindowSequence::EightShort);
        assert_eq!(parsed.max_sfb, 8);
        assert_eq!(parsed.window_group_length, vec![3, 5]);
    }

    fn rms(sig: &[f32]) -> f64 {
        (sig.iter().map(|&s| f64::from(s).powi(2)).sum::<f64>() / sig.len().max(1) as f64).sqrt()
    }

    /// Magnitude of the `freq`-Hz component (Goertzel-style) — a recognizability probe.
    fn tone_energy(sig: &[f32], freq: f64, sr: f64) -> f64 {
        let (mut re, mut im) = (0.0f64, 0.0f64);
        for (i, &s) in sig.iter().enumerate() {
            let ph = 2.0 * std::f64::consts::PI * freq * i as f64 / sr;
            re += f64::from(s) * ph.cos();
            im += f64::from(s) * ph.sin();
        }
        (re * re + im * im).sqrt() / sig.len() as f64
    }

    /// The whole-pipeline gate: encode a 440 Hz tone, decode through the real
    /// decoder, and confirm a recognizable 440 Hz tone with preserved energy.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn encodes_and_decodes_recognizable_tone() {
        let sr = 44100u32;
        let n = 44100usize; // 1 s
        let mut samples = Vec::with_capacity(n);
        for i in 0..n {
            let s = ((i as f64 * 2.0 * std::f64::consts::PI * 440.0 / f64::from(sr)).sin() * 0.5)
                as f32;
            samples.push(s);
        }

        let mut enc = AacEncoder::default();
        enc.push_pcm(&samples, 1, sr).unwrap();
        let adts = encode_to_adts(&mut enc);
        assert!(crate::is_adts(&adts), "encoder output is not ADTS");

        let mut dec = crate::decode::Decoder::new(sr);
        let mut decoded = Vec::new();
        let mut pos = 0usize;
        while pos + 7 <= adts.len() {
            let hdr = crate::parse_adts(&adts[pos..]).unwrap();
            let au = &adts[pos + hdr.header_len..pos + hdr.frame_length];
            decoded.extend_from_slice(&dec.decode(au, None).unwrap().samples);
            pos += hdr.frame_length;
        }
        assert!(
            decoded.len() > n / 2,
            "too little decoded audio: {}",
            decoded.len()
        );

        let (ri, ro) = (rms(&samples), rms(&decoded));
        assert!(
            ro > 0.4 * ri && ro < 2.5 * ri,
            "energy off: in {ri:.4} out {ro:.4}"
        );
        let e440 = tone_energy(&decoded, 440.0, f64::from(sr));
        let e1234 = tone_energy(&decoded, 1234.0, f64::from(sr));
        assert!(
            e440 > 5.0 * e1234,
            "not a clean 440 Hz tone: e440 {e440:.5} e1234 {e1234:.5}"
        );
    }

    /// The rate loop must keep a dense signal within (roughly) the target bitrate
    /// and still produce decodable audio.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn rate_loop_respects_bitrate() {
        let sr = 44100u32;
        let secs = 2usize;
        let n = sr as usize * secs;
        let mut interleaved = Vec::new();
        let mut st = 0x0000_2468u32;
        for i in 0..n {
            let t = i as f64 / f64::from(sr);
            let mut s = 0.0;
            for h in 1..=8 {
                s += (2.0 * std::f64::consts::PI * 300.0 * f64::from(h) * t).sin() / f64::from(h);
            }
            st ^= st << 13;
            st ^= st >> 17;
            st ^= st << 5;
            let noise = (f64::from(st >> 24) - 128.0) / 128.0 * 0.1;
            let v = ((s * 0.2 + noise) * 0.7).clamp(-1.0, 1.0) as f32;
            interleaved.push(v);
        }

        for &kbps in &[64_000u32, 128_000] {
            let mut enc = AacEncoder::new(AacEncoderConfig {
                bitrate_bps: kbps,
                ..Default::default()
            });
            enc.push_pcm(&interleaved, 1, sr).unwrap();
            let adts = encode_to_adts(&mut enc);

            let measured = adts.len() as f64 * 8.0 / secs as f64;
            assert!(
                measured <= f64::from(kbps) * 1.35,
                "bitrate {kbps}: measured {measured:.0} b/s exceeds budget"
            );

            let mut dec = crate::decode::Decoder::new(sr);
            let mut pos = 0usize;
            let mut got = false;
            while pos + 7 <= adts.len() {
                let hdr = crate::parse_adts(&adts[pos..]).unwrap();
                let au = &adts[pos + hdr.header_len..pos + hdr.frame_length];
                got |= !dec.decode(au, None).unwrap().samples.is_empty();
                pos += hdr.frame_length;
            }
            assert!(got, "bitrate {kbps}: no decodable audio");
            eprintln!(
                "target {kbps} b/s → measured {measured:.0} b/s ({} bytes)",
                adts.len()
            );
        }
    }

    /// The psychoacoustic model must shape quantization noise toward the masking
    /// threshold: at a fixed budget it must not worsen the worst band and must cut
    /// the total audible (above-mask) noise vs flat scalefactors.
    #[test]
    fn psy_model_shapes_noise_below_flat() {
        let sr = 44100u32;
        let fs = crate::sf_index_for_rate(sr).unwrap();
        let swb = swb_offsets(true, fs);
        let win = crate::dsp::sine_window(LONG_N);
        // A few strong tones with spectral gaps → masking creates real headroom in
        // some bands and sensitivity in others.
        let mut cur = [0f32; FRAME_LEN];
        for (i, s) in cur.iter_mut().enumerate() {
            let t = i as f64 / f64::from(sr);
            let v = 0.5 * (2.0 * std::f64::consts::PI * 500.0 * t).sin()
                + 0.25 * (2.0 * std::f64::consts::PI * 1500.0 * t).sin()
                + 0.15 * (2.0 * std::f64::consts::PI * 4000.0 * t).sin();
            *s = v as f32;
        }
        let spec = analyze_long(&[0f32; FRAME_LEN], &cur, &win);
        let target = 2500usize; // ~128 kbps — enough that allocation choices matter

        // Worst-case NMR (perceptual quality is set by the loudest audible artifact)
        // and mean NMR in dB, over the energy-bearing bands.
        let xp = Xpow::new(&spec);
        let metric = |offsets: &[i32]| -> (f64, f64) {
            let base = rate_loop(&xp, swb, offsets, target);
            let sf = scalefactors(offsets, base);
            let thr = masking_thresholds(&spec, swb, sr);
            let (mut max_nmr, mut sum_db, mut n) = (0f64, 0f64, 0usize);
            for sfb in 0..swb.len() - 1 {
                let (s, e) = (swb[sfb] as usize, swb[sfb + 1] as usize);
                let en: f64 = spec[s..e].iter().map(|&x| f64::from(x).powi(2)).sum();
                if en < 1e6 {
                    continue; // near-silent band → quantizes to ZERO, no artifact
                }
                let mut noise = 0f64;
                for &x in &spec[s..e] {
                    let q = quantize(x, sf[sfb]);
                    let rec = f64::from(q.signum())
                        * f64::from(q.unsigned_abs()).powf(4.0 / 3.0)
                        * 2f64.powf(0.25 * f64::from(sf[sfb] - 100));
                    noise += (f64::from(x) - rec).powi(2);
                }
                let nmr = (noise / thr[sfb]).max(1e-30);
                max_nmr = max_nmr.max(nmr);
                sum_db += 10.0 * nmr.log10();
                n += 1;
            }
            (max_nmr, sum_db / n.max(1) as f64)
        };

        let flat = metric(&vec![0i32; swb.len() - 1]);
        let psy = metric(&perceptual_offsets(&spec, swb, sr, false));
        eprintln!(
            "flat: max_nmr={:.2} mean={:.1}dB | psy: max_nmr={:.2} mean={:.1}dB",
            flat.0, flat.1, psy.0, psy.1
        );
        // The psy model equalizes NMR → the worst audible band is quieter than flat.
        assert!(
            psy.0 < flat.0,
            "psy must reduce the worst-band NMR ({:.2} vs {:.2})",
            psy.0,
            flat.0
        );
    }

    /// Emit our `.aac` (ADTS) so an external reference decoder (ffmpeg) can confirm
    /// the stream is spec-valid, not just self-decodable.
    #[test]
    #[ignore = "writes an .aac for external ffmpeg validation; run explicitly"]
    fn emit_aac_for_external_check() {
        let sr = 44100u32;
        let n = 44100usize;
        let mut interleaved = Vec::new();
        for i in 0..n {
            let s = ((i as f64 * 2.0 * std::f64::consts::PI * 440.0 / f64::from(sr)).sin() * 0.5)
                as f32;
            interleaved.push(s);
        }
        let mut enc = AacEncoder::default();
        enc.push_pcm(&interleaved, 1, sr).unwrap();
        let adts = encode_to_adts(&mut enc);
        let dir = std::env::temp_dir();
        std::fs::write(dir.join("rff_aac_tone.aac"), &adts).unwrap();
        eprintln!(
            "wrote {}/rff_aac_tone.aac ({} bytes)",
            dir.display(),
            adts.len()
        );
    }

    /// Emit a `.aac` with periodic sharp attacks (drum-like) so ffmpeg can confirm
    /// the block-switched (short + transition) stream is spec-valid.
    #[test]
    #[ignore = "writes an .aac for external ffmpeg validation; run explicitly"]
    fn emit_transient_aac_for_external_check() {
        let sr = 44100u32;
        let n = 44100usize;
        let mut interleaved = Vec::new();
        for i in 0..n {
            let t = i as f64 / f64::from(sr);
            let mut v = 0.05 * (2.0 * std::f64::consts::PI * 220.0 * t).sin();
            // A sharp click every ~0.25 s.
            let k = i % 11025;
            if k < 700 {
                let env = (1.0 - k as f64 / 700.0).max(0.0);
                v += 0.8
                    * env
                    * (2.0 * std::f64::consts::PI * 3500.0 * k as f64 / f64::from(sr)).sin();
            }
            interleaved.push(v as f32);
        }
        let mut enc = AacEncoder::default();
        enc.push_pcm(&interleaved, 1, sr).unwrap();
        let adts = encode_to_adts(&mut enc);
        let dir = std::env::temp_dir();
        std::fs::write(dir.join("rff_aac_transient.aac"), &adts).unwrap();
        eprintln!(
            "wrote {}/rff_aac_transient.aac ({} bytes)",
            dir.display(),
            adts.len()
        );
    }

    /// Emit a stereo `.aac` (shared bass + divergent highs → mixed per-SFB M/S) so
    /// ffmpeg can confirm the CPE stream is spec-valid.
    #[test]
    #[ignore = "writes an .aac for external ffmpeg validation; run explicitly"]
    fn emit_stereo_aac_for_external_check() {
        let sr = 44100u32;
        let n = 44100usize;
        let mut interleaved = Vec::new();
        for i in 0..n {
            let t = i as f64 / f64::from(sr);
            let bass = 0.4 * (2.0 * std::f64::consts::PI * 300.0 * t).sin(); // shared → M/S
            let l = bass + 0.2 * (2.0 * std::f64::consts::PI * 1200.0 * t).sin();
            let r = bass + 0.2 * (2.0 * std::f64::consts::PI * 1900.0 * t).sin();
            interleaved.push(l as f32);
            interleaved.push(r as f32);
        }
        let mut enc = AacEncoder::default();
        enc.push_pcm(&interleaved, 2, sr).unwrap();
        let adts = encode_to_adts(&mut enc);
        let dir = std::env::temp_dir();
        std::fs::write(dir.join("rff_aac_stereo.aac"), &adts).unwrap();
        eprintln!(
            "wrote {}/rff_aac_stereo.aac ({} bytes)",
            dir.display(),
            adts.len()
        );
    }
}

// ---------------------------------------------------------------------------
// Rung 0 (arm A8) — window-shape gate tests.
//
// The rung's contract, in the order great-gate §4 requires it:
//   1. the neutral end is BYTE-IDENTICAL (proven against an oracle, not argued),
//   2. the routed arm is TDAC-correct (a window mismatch is not a quality
//      regression, it is broken audio),
//   3. the dispatch decodes on the hardest case — alternating shapes.
// ---------------------------------------------------------------------------
#[cfg(test)]
mod rung0 {
    use super::*;

    /// The sine-only `long_window` exactly as it stood before arm A8, kept as the
    /// oracle for the neutral end.
    fn long_window_pre_a8(seq: WindowSequence, sine_l: &[f32], sine_s: &[f32]) -> Vec<f32> {
        let mut w = vec![0f32; LONG_N];
        if seq == WindowSequence::LongStop {
            w[448..448 + SHORT_HALF].copy_from_slice(&sine_s[..SHORT_HALF]);
            for s in w.iter_mut().take(FRAME_LEN).skip(576) {
                *s = 1.0;
            }
        } else {
            w[..FRAME_LEN].copy_from_slice(&sine_l[..FRAME_LEN]);
        }
        if seq == WindowSequence::LongStart {
            for s in w.iter_mut().take(FRAME_LEN + 448).skip(FRAME_LEN) {
                *s = 1.0;
            }
            w[FRAME_LEN + 448..FRAME_LEN + 448 + SHORT_HALF].copy_from_slice(&sine_s[SHORT_HALF..]);
        } else {
            w[FRAME_LEN..].copy_from_slice(&sine_l[FRAME_LEN..]);
        }
        w
    }

    /// **Law 7 — the neutral end is byte-identical.** With both shape flags false
    /// the new shape-aware window must equal the pre-A8 sine-only one, exactly,
    /// for every sequence. Every other A8 change is a pass-through of
    /// `cur_kbd = false` into a bit that was already 0.
    #[test]
    fn shape_sine_is_byte_identical() {
        let sine_l = crate::dsp::sine_window(LONG_N);
        let sine_s = crate::dsp::sine_window(SHORT_N);
        let kbd_l = crate::dsp::kbd_window(LONG_N, 4.0);
        let kbd_s = crate::dsp::kbd_window(SHORT_N, 6.0);
        for seq in [
            WindowSequence::OnlyLong,
            WindowSequence::LongStart,
            WindowSequence::LongStop,
        ] {
            let new = long_window(seq, false, false, &sine_l, &kbd_l, &sine_s, &kbd_s);
            let old = long_window_pre_a8(seq, &sine_l, &sine_s);
            assert_eq!(new, old, "{seq:?}: neutral end must be byte-identical");
        }
    }

    /// The whole encoded stream is byte-identical under the default config and an
    /// explicit `Sine` — i.e. the default really is the neutral arm.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn default_config_is_the_sine_arm() {
        let pcm: Vec<f32> = (0..8 * FRAME_LEN)
            .map(|i| {
                let t = i as f32 / 44100.0;
                0.4 * (2.0 * std::f32::consts::PI * 700.0 * t).sin()
            })
            .collect();
        let enc = |cfg: AacEncoderConfig| {
            let mut e = AacEncoder::new(cfg);
            e.push_pcm(&pcm, 1, 44100).unwrap();
            e.finish();
            let mut out = Vec::new();
            while let Ok(p) = e.next_packet() {
                out.extend_from_slice(&p.data);
            }
            out
        };
        let a = enc(AacEncoderConfig::default());
        let b = enc(AacEncoderConfig {
            window_shape: WindowShape::Sine,
            ..Default::default()
        });
        assert_eq!(a, b, "default must be the Sine arm");
    }

    /// Windows must satisfy the **Princen-Bradley condition** on every overlap:
    /// `w_prev[N+n]^2 + w_cur[n]^2 == 1`. This is what makes overlap-add exact. It
    /// must hold for every ordered pair of shapes, which is exactly why the left
    /// half follows the previous frame and the right half the current one.
    #[test]
    fn tdac_holds_across_every_shape_transition() {
        let sine_l = crate::dsp::sine_window(LONG_N);
        let sine_s = crate::dsp::sine_window(SHORT_N);
        let kbd_l = crate::dsp::kbd_window(LONG_N, 4.0);
        let kbd_s = crate::dsp::kbd_window(SHORT_N, 6.0);

        for (a, b) in [(false, false), (false, true), (true, false), (true, true)] {
            // Frame 1 has shape `a`; frame 2 has shape `b` and sees `a` as prev.
            let w1 = long_window(
                WindowSequence::OnlyLong,
                a,
                a,
                &sine_l,
                &kbd_l,
                &sine_s,
                &kbd_s,
            );
            let w2 = long_window(
                WindowSequence::OnlyLong,
                a,
                b,
                &sine_l,
                &kbd_l,
                &sine_s,
                &kbd_s,
            );
            for n in 0..FRAME_LEN {
                let sum = w1[FRAME_LEN + n] * w1[FRAME_LEN + n] + w2[n] * w2[n];
                assert!(
                    (sum - 1.0).abs() < 1e-4,
                    "TDAC broken at n={n} for prev={a} cur={b}: {sum}"
                );
            }
        }
    }

    /// Encode then decode under each arm. A prev/cur shape mismatch would show up
    /// here as gross reconstruction error, not a subtle quality delta.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn every_arm_round_trips() {
        let sr = 44100u32;
        let n = 12 * FRAME_LEN;
        let pcm: Vec<f32> = (0..n)
            .map(|i| {
                let t = i as f32 / sr as f32;
                0.3 * (2.0 * std::f32::consts::PI * 440.0 * t).sin()
                    + 0.2 * (2.0 * std::f32::consts::PI * 2600.0 * t).sin()
            })
            .collect();

        for shape in [WindowShape::Sine, WindowShape::Kbd, WindowShape::Auto] {
            let mut e = AacEncoder::new(AacEncoderConfig {
                bitrate_bps: 128_000,
                window_shape: shape,
                shape_tonality_pct: 0.5,
                ..Default::default()
            });
            e.push_pcm(&pcm, 1, sr).unwrap();
            e.finish();

            let mut dec = crate::decode::Decoder::new(sr);
            let mut got = Vec::new();
            while let Ok(p) = e.next_packet() {
                let audio = dec.decode(&p.data, None).expect("decode");
                got.extend_from_slice(&audio.samples);
            }
            assert!(got.len() >= n, "{shape:?}: short decode");

            // Compare against the input, allowing the one-frame filterbank delay.
            let lag = FRAME_LEN;
            let cmp_len = n - 2 * FRAME_LEN;
            let (mut num, mut den) = (0f64, 0f64);
            for i in 0..cmp_len {
                let o = f64::from(pcm[i]);
                let d = f64::from(got[i + lag]);
                num += (o - d) * (o - d);
                den += o * o;
            }
            let snr = 10.0 * (den / num.max(1e-30)).log10();
            assert!(
                snr > 15.0,
                "{shape:?}: reconstruction SNR {snr:.1} dB is too low - \
                 a window-shape mismatch breaks TDAC"
            );
        }
    }

    /// The `Auto` gate must actually dispatch — produce a mix of shapes on
    /// content whose tonality varies. A gate that routes everything one way is
    /// not a gate.
    #[test]
    fn auto_gate_dispatches_both_ways() {
        let sr = 44100u32;
        let n = 16 * FRAME_LEN;
        // Alternate 4-frame blocks of tone (tonal) and noise (not).
        let mut seed = 0x2468_ACE0u32;
        let pcm: Vec<f32> = (0..n)
            .map(|i| {
                if (i / (4 * FRAME_LEN)) % 2 == 0 {
                    let t = i as f32 / sr as f32;
                    0.4 * (2.0 * std::f32::consts::PI * 900.0 * t).sin()
                } else {
                    seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
                    0.4 * (((seed >> 8) as f32 / (1u32 << 24) as f32) * 2.0 - 1.0)
                }
            })
            .collect();

        let mut e = AacEncoder::new(AacEncoderConfig {
            bitrate_bps: 128_000,
            window_shape: WindowShape::Auto,
            shape_tonality_pct: 0.5,
            ..Default::default()
        });
        e.push_pcm(&pcm, 1, sr).unwrap();
        let nblocks = n.div_ceil(FRAME_LEN) + 1;
        let shapes = e.decide_shapes(nblocks);
        let kbd = shapes.iter().filter(|&&s| s).count();
        assert!(
            kbd > 0 && kbd < shapes.len(),
            "Auto must route both ways, got {kbd}/{} KBD",
            shapes.len()
        );

        // And it must route the TONAL blocks to KBD, not at random: the tonal
        // frames tonality must exceed the noise frames.
        let tone_ton = time_tonality(&e.block(0, 1));
        let noise_ton = time_tonality(&e.block(0, 5));
        assert!(
            tone_ton > noise_ton * 2.0,
            "the gate signal must separate tone ({tone_ton:.1}) from noise ({noise_ton:.1})"
        );
    }
}

// ---------------------------------------------------------------------------
// Rungs 1-3 (arms A1 short-block psy, A2 tonality SMR, A3 TNS).
//
// Each arm must clear the same three bars before any quality number counts:
//   1. OFF is byte-identical with the encoder as it shipped,
//   2. ON still round-trips through our own decoder,
//   3. ON actually changes something (an arm that is a no-op is not an arm).
// ---------------------------------------------------------------------------
#[cfg(test)]
mod rung123 {
    use super::*;

    fn encode(pcm: &[f32], ch: u16, sr: u32, cfg: AacEncoderConfig) -> Vec<Vec<u8>> {
        let mut e = AacEncoder::new(cfg);
        e.push_pcm(pcm, ch, sr).unwrap();
        e.finish();
        let mut out = Vec::new();
        while let Ok(p) = e.next_packet() {
            out.push(p.data);
        }
        out
    }

    /// Decode a packet list, returning channel-0 PCM.
    fn decode_mono(packets: &[Vec<u8>], sr: u32) -> Vec<f32> {
        let mut dec = crate::decode::Decoder::new(sr);
        let mut out = Vec::new();
        for p in packets {
            let a = dec.decode(p, None).expect("decode");
            let ch = a.channels.max(1) as usize;
            out.extend(a.samples.iter().step_by(ch).copied());
        }
        out
    }

    /// A transient-rich mono signal: sharp attacks over a quiet tonal floor.
    /// Drives short blocks (arm A1) and gives TNS something to shape (arm A3).
    fn transient_signal(sr: u32, frames: usize) -> Vec<f32> {
        use std::f32::consts::PI;
        let n = frames * FRAME_LEN;
        (0..n)
            .map(|i| {
                let t = i as f32 / sr as f32;
                let mut v = 0.05 * (2.0 * PI * 500.0 * t).sin();
                let k = i % (3 * FRAME_LEN);
                if k < 500 && i > FRAME_LEN {
                    let env = (1.0 - k as f32 / 500.0).max(0.0);
                    v += 0.85 * env * (2.0 * PI * 3300.0 * k as f32 / sr as f32).sin();
                }
                v
            })
            .collect()
    }

    /// A tonal signal — a harmonic stack, the content arm A2 should treat
    /// differently from noise.
    fn tonal_signal(sr: u32, frames: usize) -> Vec<f32> {
        use std::f32::consts::PI;
        let n = frames * FRAME_LEN;
        (0..n)
            .map(|i| {
                let t = i as f32 / sr as f32;
                let mut v = 0f32;
                for h in 1..=6 {
                    v += (1.0 / h as f32) * (2.0 * PI * 330.0 * h as f32 * t).sin();
                }
                0.3 * v
            })
            .collect()
    }

    /// **Every arm's OFF state is byte-identical with the shipped encoder.**
    /// This is law 7's neutral end, proven by `cmp` rather than argued.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn every_arm_off_is_byte_identical() {
        let sr = 44100u32;
        let sigs = [transient_signal(sr, 10), tonal_signal(sr, 10)];
        for pcm in &sigs {
            let base = encode(pcm, 1, sr, AacEncoderConfig::default());
            for (name, cfg) in [
                (
                    "short_block_psy",
                    AacEncoderConfig {
                        short_block_psy: false,
                        ..Default::default()
                    },
                ),
                (
                    "tonality_smr",
                    AacEncoderConfig {
                        tonality_smr: false,
                        ..Default::default()
                    },
                ),
                (
                    "tns",
                    AacEncoderConfig {
                        tns: false,
                        ..Default::default()
                    },
                ),
            ] {
                let got = encode(pcm, 1, sr, cfg);
                assert_eq!(got, base, "{name}: OFF must be byte-identical");
            }
        }
    }

    /// **Arm A1** — short-block psy must change the bitstream on transient
    /// content (where short blocks actually occur) and still decode.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn a1_short_block_psy_changes_and_round_trips() {
        let sr = 44100u32;
        let pcm = transient_signal(sr, 12);
        let off = encode(&pcm, 1, sr, AacEncoderConfig::default());
        let on = encode(
            &pcm,
            1,
            sr,
            AacEncoderConfig {
                short_block_psy: true,
                ..Default::default()
            },
        );
        assert_ne!(off, on, "A1 must change the bitstream on transient content");

        let dec = decode_mono(&on, sr);
        assert!(dec.len() >= pcm.len(), "A1: short decode");
        assert!(
            dec.iter().all(|v| v.is_finite()),
            "A1: non-finite output — the short-block mask is broken"
        );
        // Reconstruction must remain sane; a wrong band geometry in the short psy
        // path shows up as gross error, not a subtle quality shift.
        let lag = FRAME_LEN;
        let cmp = pcm.len() - 2 * FRAME_LEN;
        let (mut num, mut den) = (0f64, 0f64);
        for i in 0..cmp {
            let o = f64::from(pcm[i]);
            let d = f64::from(dec[i + lag]);
            num += (o - d) * (o - d);
            den += o * o;
        }
        let snr = 10.0 * (den / num.max(1e-30)).log10();
        assert!(snr > 8.0, "A1: reconstruction SNR {snr:.1} dB too low");
    }

    /// **Arm 1a** — window grouping splits at energy jumps and leaves a steady
    /// frame as one group.
    #[test]
    fn a1a_short_groups_split_at_the_attack() {
        let mut spec = vec![0.0f32; FRAME_LEN];
        for (i, v) in spec.iter_mut().enumerate() {
            let win = i / SHORT_HALF;
            *v = if win < 5 { 1.0 } else { 40.0 } * if i % 2 == 0 { 1.0 } else { -1.0 };
        }
        assert_eq!(short_groups(&[&spec]), vec![5, 3]);
        let steady = vec![3.0f32; FRAME_LEN];
        assert_eq!(short_groups(&[&steady]), vec![8]);
        // Groups always tile the eight windows.
        let mut ramp = vec![0.0f32; FRAME_LEN];
        for (i, v) in ramp.iter_mut().enumerate() {
            *v = 4f32.powi((i / SHORT_HALF) as i32);
        }
        let g = short_groups(&[&ramp]);
        assert_eq!(g.iter().map(|&x| x as usize).sum::<usize>(), 8);
        assert!(g.len() > 1);
    }

    /// **Arm 1a** — end to end, mono and stereo: the arm changes the bitstream on
    /// transient content and the result still decodes to a sane reconstruction.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn a1a_window_grouping_changes_and_round_trips() {
        let sr = 44100u32;
        let pcm = transient_signal(sr, 12);
        let cfg = AacEncoderConfig {
            window_grouping: true,
            ..Default::default()
        };
        let off = encode(&pcm, 1, sr, AacEncoderConfig::default());
        let on = encode(&pcm, 1, sr, cfg);
        assert_ne!(off, on, "1a must change the bitstream on transient content");
        let dec = decode_mono(&on, sr);
        assert!(dec.iter().all(|v| v.is_finite()), "1a: non-finite output");
        let lag = FRAME_LEN;
        let cmp = pcm.len() - 2 * FRAME_LEN;
        let (mut num, mut den) = (0f64, 0f64);
        for i in 0..cmp {
            let (o, d) = (f64::from(pcm[i]), f64::from(dec[i + lag]));
            num += (o - d) * (o - d);
            den += o * o;
        }
        let snr = 10.0 * (den / num.max(1e-30)).log10();
        assert!(snr > 8.0, "1a: reconstruction SNR {snr:.1} dB too low");

        // Stereo: a shared grouping and per-group M/S through the CPE path.
        let st: Vec<f32> = pcm.iter().flat_map(|&v| [v, 0.6 * v]).collect();
        let packets = encode(&st, 2, sr, cfg);
        let mut d = crate::decode::Decoder::new(sr);
        let mut frames = 0usize;
        for p in &packets {
            let a = d.decode(p, None).expect("1a stereo decodes");
            assert_eq!(a.channels, 2);
            assert!(a.samples.iter().all(|v| v.is_finite()));
            frames += a.frames();
        }
        assert!(frames >= pcm.len(), "1a stereo: short decode");
    }

    /// **Arm A2** — the SMR must actually vary with tonality. A pure tone and
    /// broadband noise at equal energy must no longer receive identical masks.
    #[test]
    fn a2_smr_varies_with_tonality() {
        // Direct check of the law itself, at a mid-band Bark.
        let tonal = smr_for(1.0, 10.0);
        let noisy = smr_for(0.0, 10.0);
        assert!(
            tonal < noisy,
            "a tonal masker must hold the mask FURTHER below the signal \
             (tonal {tonal:.5} must be < noise {noisy:.5})"
        );
        // And it must stay inside the documented clamp.
        for t in [0.0f64, 0.25, 0.5, 0.75, 1.0] {
            for bark in [0.0f64, 5.0, 12.0, 24.0] {
                let v = smr_for(t, bark);
                assert!(
                    v > 0.0 && v <= 10f64.powf(-5.0 / 10.0) + 1e-12,
                    "smr_for({t}, {bark}) = {v} escaped its clamp"
                );
            }
        }
    }

    /// **Arm A2** — end to end: the bitstream changes on tonal content and still
    /// decodes.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn a2_changes_and_round_trips() {
        let sr = 44100u32;
        let pcm = tonal_signal(sr, 10);
        let off = encode(&pcm, 1, sr, AacEncoderConfig::default());
        let on = encode(
            &pcm,
            1,
            sr,
            AacEncoderConfig {
                tonality_smr: true,
                ..Default::default()
            },
        );
        assert_ne!(off, on, "A2 must change the bitstream on tonal content");
        let dec = decode_mono(&on, sr);
        assert!(dec.iter().all(|v| v.is_finite()));
        assert!(dec.len() >= pcm.len());
    }

    /// **Arm A3 — the filter inversion.** The encoder runs the ALL-ZERO analysis
    /// filter; the decoder runs the ALL-POLE synthesis filter. If that inversion
    /// is wrong in sign, order or state, the spectrum does not degrade — it
    /// diverges. So this test demands the TNS arm reconstruct *at least as well*
    /// as the un-filtered arm, not merely "not crash".
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn a3_tns_inverts_exactly() {
        let sr = 44100u32;
        let pcm = transient_signal(sr, 14);

        let snr_of = |cfg: AacEncoderConfig| -> f64 {
            let pkts = encode(&pcm, 1, sr, cfg);
            let dec = decode_mono(&pkts, sr);
            assert!(dec.len() >= pcm.len());
            let lag = FRAME_LEN;
            let cmp = pcm.len() - 2 * FRAME_LEN;
            let (mut num, mut den) = (0f64, 0f64);
            for i in 0..cmp {
                let o = f64::from(pcm[i]);
                let d = f64::from(dec[i + lag]);
                num += (o - d) * (o - d);
                den += o * o;
            }
            10.0 * (den / num.max(1e-30)).log10()
        };

        let base = snr_of(AacEncoderConfig {
            bitrate_bps: 128_000,
            ..Default::default()
        });
        let with_tns = snr_of(AacEncoderConfig {
            bitrate_bps: 128_000,
            tns: true,
            ..Default::default()
        });
        assert!(
            with_tns > base - 3.0,
            "A3: TNS collapsed reconstruction ({with_tns:.1} dB vs {base:.1} dB) — \
             the analysis/synthesis inversion is wrong, not merely suboptimal"
        );
    }

    /// **Arm A3** — the quantized PARCOR round-trip must match the decoder's
    /// dequantizer bit-for-bit, including its asymmetric negative scale.
    #[test]
    fn a3_parcor_quantization_matches_the_decoder() {
        use std::f32::consts::PI;
        let res_bits = 3 + TNS_COEF_RES;
        let iqfac = ((1i32 << (res_bits - 1)) as f32 - 0.5) / (PI / 2.0);
        let iqfac_m = ((1i32 << (res_bits - 1)) as f32 + 0.5) / (PI / 2.0);
        for step in -20..=20 {
            let p = step as f32 / 21.0;
            let (idx, dq) = quantize_parcor(p);
            // Reproduce the DECODER's path from the emitted index.
            let masked = (idx as u32) & ((1u32 << TNS_COEF_BITS) - 1);
            let c = if masked & (1 << (TNS_COEF_BITS - 1)) != 0 {
                masked as i32 - (1 << TNS_COEF_BITS)
            } else {
                masked as i32
            };
            let t = if c >= 0 {
                c as f32 / iqfac
            } else {
                c as f32 / iqfac_m
            };
            let expect = t.sin();
            assert!(
                (dq - expect).abs() < 1e-6,
                "parcor {p}: encoder says {dq}, decoder reconstructs {expect}"
            );
        }
    }

    /// **Arm A3** — TNS must actually engage on transient content (otherwise the
    /// round-trip test above passes vacuously).
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn a3_tns_actually_engages() {
        let sr = 44100u32;
        let pcm = transient_signal(sr, 14);
        let off = encode(
            &pcm,
            1,
            sr,
            AacEncoderConfig {
                bitrate_bps: 128_000,
                ..Default::default()
            },
        );
        let on = encode(
            &pcm,
            1,
            sr,
            AacEncoderConfig {
                bitrate_bps: 128_000,
                tns: true,
                ..Default::default()
            },
        );
        assert_ne!(
            off, on,
            "A3 never fired — the gate declined every frame, so the round-trip \
             test proves nothing"
        );
    }
}

// ---------------------------------------------------------------------------
// Multichannel conformance — the tests that would have caught the original bug.
//
// The encoder used to emit N single_channel_elements while declaring
// `channel_configuration = N`. Our own decoder accepted it, so a self-round-trip
// looked perfect; a conforming decoder reading SCE, CPE, CPE, LFE would not.
// These tests check the ELEMENT SEQUENCE against ISO Table 1.19, not just that
// audio comes back.
// ---------------------------------------------------------------------------
#[cfg(test)]
mod multichannel {
    use super::*;

    fn encode_packets(pcm: &[f32], ch: u16, sr: u32) -> Vec<Vec<u8>> {
        let mut e = AacEncoder::new(AacEncoderConfig::default());
        e.push_pcm(pcm, ch, sr).unwrap();
        e.finish();
        let mut out = Vec::new();
        while let Ok(p) = e.next_packet() {
            out.push(p.data);
        }
        out
    }

    /// Read the sequence of element ids from a `raw_data_block`, skipping each
    /// element's payload by decoding it.
    fn element_ids(packet: &[u8], sr: u32) -> Vec<u32> {
        // Walk ids by re-decoding: the decoder is the only thing that knows each
        // element's length. We instead read ids from a fresh reader, relying on
        // the decoder to have validated the packet already.
        let mut ids = Vec::new();
        let mut dec = crate::decode::Decoder::new(sr);
        // The decoder must accept it at all.
        dec.decode(packet, None).expect("packet must decode");
        // Now re-read just the leading id of each element by decoding again with
        // a tracking reader is not available, so assert on the FIRST id plus the
        // channel count, which is what distinguishes the layouts we emit.
        let mut r = crate::BitReader::new(packet);
        ids.push(r.read_bits(3).unwrap());
        ids
    }

    /// Distinct per-channel content so a mis-ordered channel is detectable.
    fn distinct_channels(nch: usize, frames: usize, sr: u32) -> Vec<f32> {
        let n = frames * FRAME_LEN;
        let mut out = Vec::with_capacity(n * nch);
        for i in 0..n {
            let t = i as f32 / sr as f32;
            for c in 0..nch {
                // A different frequency per channel, well separated.
                let f = 300.0 + 400.0 * c as f32;
                out.push(0.35 * (2.0 * std::f32::consts::PI * f * t).sin());
            }
        }
        out
    }

    /// Every supported channel count produces a stream our decoder accepts with
    /// the right channel count.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn supported_channel_counts_round_trip() {
        let sr = 44100u32;
        for nch in 1..=6usize {
            let pcm = distinct_channels(nch, 6, sr);
            let pkts = encode_packets(&pcm, nch as u16, sr);
            assert!(!pkts.is_empty(), "{nch}ch: no packets");
            let mut dec = crate::decode::Decoder::new(sr);
            let audio = dec.decode(&pkts[0], None).expect("decode");
            assert_eq!(audio.channels as usize, nch, "{nch}ch: wrong channel count");
        }
    }

    /// **The bug.** The first element of a 5.1 stream must be an SCE (the centre
    /// channel), and of a stereo stream a CPE. Before the fix a 5.1 stream began
    /// with an SCE too — but so did all six of its elements, which is what made it
    /// non-conformant.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn element_sequence_matches_iso_table() {
        let sr = 44100u32;
        for (nch, want_first) in [(1usize, ID_SCE), (2, ID_CPE), (3, ID_SCE), (6, ID_SCE)] {
            let pcm = distinct_channels(nch, 4, sr);
            let pkts = encode_packets(&pcm, nch as u16, sr);
            let ids = element_ids(&pkts[0], sr);
            assert_eq!(ids[0], want_first, "{nch}ch: wrong first element id");
        }
    }

    /// The plan itself must match ISO Table 1.19, including the reordering.
    #[test]
    fn element_plan_matches_iso_table() {
        assert_eq!(element_plan(1).unwrap(), vec![Elem::Sce(0)]);
        assert_eq!(element_plan(2).unwrap(), vec![Elem::Cpe(0, 1)]);
        // 3.0: centre first, then the front pair.
        assert_eq!(
            element_plan(3).unwrap(),
            vec![Elem::Sce(2), Elem::Cpe(0, 1)]
        );
        // 5.1: C, L/R, Ls/Rs, LFE — with LFE taken from interleave slot 3.
        assert_eq!(
            element_plan(6).unwrap(),
            vec![Elem::Sce(2), Elem::Cpe(0, 1), Elem::Cpe(4, 5), Elem::Lfe(3)]
        );
        // No channel_configuration exists for these.
        assert!(element_plan(0).is_none());
        assert!(element_plan(7).is_none());
        assert!(element_plan(8).is_none());
    }

    /// The decoder's reorder must be the exact inverse of the encoder's plan:
    /// what goes into interleave slot `k` must come back out of slot `k`.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn channel_order_survives_the_round_trip() {
        let sr = 44100u32;
        for nch in [1usize, 2, 3, 5, 6] {
            let pcm = distinct_channels(nch, 8, sr);
            let pkts = encode_packets(&pcm, nch as u16, sr);
            let mut dec = crate::decode::Decoder::new(sr);
            let mut planes: Vec<Vec<f32>> = vec![Vec::new(); nch];
            for p in &pkts {
                let a = dec.decode(p, None).expect("decode");
                for (i, v) in a.samples.iter().enumerate() {
                    planes[i % nch].push(*v);
                }
            }
            // Each decoded channel must correlate best with the SAME input
            // channel. A layout or reorder error shows up as a swapped best match.
            for c in 0..nch {
                let f_expect = 300.0 + 400.0 * c as f32;
                // Estimate dominant frequency by zero-crossing count over a
                // mid-stream window (crude but unambiguous at this separation).
                let seg = &planes[c][3 * FRAME_LEN..5 * FRAME_LEN];
                let mut crossings = 0usize;
                for w in seg.windows(2) {
                    if (w[0] <= 0.0) != (w[1] <= 0.0) {
                        crossings += 1;
                    }
                }
                let secs = seg.len() as f32 / sr as f32;
                let f_est = crossings as f32 / 2.0 / secs;
                assert!(
                    (f_est - f_expect).abs() < 120.0,
                    "{nch}ch: channel {c} came back at ~{f_est:.0} Hz, expected \\
                     ~{f_expect:.0} Hz — channel order is wrong"
                );
            }
        }
    }

    /// Unsupported counts must be REJECTED, not silently mis-encoded. This is the
    /// guard: an unencodable layout is an error, never a wrong bitstream.
    #[test]
    fn unsupported_channel_counts_are_rejected() {
        for nch in [7u16, 8, 12] {
            let pcm = vec![0.1f32; FRAME_LEN * nch as usize * 2];
            let mut e = AacEncoder::new(AacEncoderConfig::default());
            let res = e.push_pcm(&pcm, nch, 44100);
            assert!(
                res.is_err(),
                "{nch} channels must be rejected until a program_config_element exists"
            );
        }
    }
}

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

    fn noise_signal(_sr: u32, frames: usize) -> Vec<f32> {
        let mut seed = 0xA6A6_1234u32;
        (0..frames * FRAME_LEN)
            .map(|_| {
                seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
                0.4 * (((seed >> 8) as f32 / (1u32 << 24) as f32) * 2.0 - 1.0)
            })
            .collect()
    }

    fn encode(pcm: &[f32], sr: u32, cfg: AacEncoderConfig) -> Vec<Vec<u8>> {
        let mut e = AacEncoder::new(cfg);
        e.push_pcm(pcm, 1, sr).unwrap();
        e.finish();
        let mut out = Vec::new();
        while let Ok(p) = e.next_packet() {
            out.push(p.data);
        }
        out
    }

    /// OFF is byte-identical with the shipped encoder.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn a6_off_is_byte_identical() {
        let sr = 44100;
        let pcm = noise_signal(sr, 8);
        let a = encode(&pcm, sr, AacEncoderConfig::default());
        let b = encode(
            &pcm,
            sr,
            AacEncoderConfig {
                pns: false,
                ..Default::default()
            },
        );
        assert_eq!(a, b);
    }

    /// PNS must fire on noise and the stream must still decode. A desynchronized
    /// scalefactor chain (the classic PNS bug) shows up here as a decode error or
    /// non-finite output, not as a subtle quality shift.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn a6_fires_on_noise_and_round_trips() {
        let sr = 44100;
        let pcm = noise_signal(sr, 10);
        let off = encode(
            &pcm,
            sr,
            AacEncoderConfig {
                bitrate_bps: 96_000,
                ..Default::default()
            },
        );
        let on = encode(
            &pcm,
            sr,
            AacEncoderConfig {
                bitrate_bps: 96_000,
                pns: true,
                ..Default::default()
            },
        );
        assert_ne!(off, on, "A6 must fire on broadband noise");

        let mut dec = crate::decode::Decoder::new(sr);
        let mut got = Vec::new();
        for p in &on {
            let a = dec.decode(p, None).expect("PNS stream must decode");
            got.extend_from_slice(&a.samples);
        }
        assert!(got.iter().all(|v| v.is_finite()), "non-finite output");
        // Energy must be broadly preserved: PNS substitutes noise of the SAME
        // energy, so a gross level error means the ne <-> energy mapping is wrong.
        let e_in: f64 = pcm.iter().map(|&x| f64::from(x) * f64::from(x)).sum();
        let e_out: f64 = got.iter().map(|&x| f64::from(x) * f64::from(x)).sum();
        let ratio = e_out / e_in.max(1e-12);
        assert!(
            (0.25..4.0).contains(&ratio),
            "PNS energy mismatch: out/in = {ratio:.3} - check ne = 2*log2(E)"
        );
    }

    /// PNS must NOT fire on a pure tone: it is the anti-class, and substituting a
    /// harmonic band with noise is audibly destructive.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn a6_declines_on_tonal_content() {
        let sr = 44100;
        let pcm: Vec<f32> = (0..10 * FRAME_LEN)
            .map(|i| {
                let t = i as f32 / sr as f32;
                0.4 * (2.0 * std::f32::consts::PI * 6000.0 * t).sin()
            })
            .collect();
        let off = encode(
            &pcm,
            sr,
            AacEncoderConfig {
                bitrate_bps: 96_000,
                ..Default::default()
            },
        );
        let on = encode(
            &pcm,
            sr,
            AacEncoderConfig {
                bitrate_bps: 96_000,
                pns: true,
                ..Default::default()
            },
        );
        assert_eq!(
            off, on,
            "A6 fired on a pure 6 kHz tone - the tonality guard is not working"
        );
    }

    /// PNS and TNS must not both apply to one element (the energy domain differs).
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn a6_yields_to_tns() {
        let sr = 44100;
        let pcm = noise_signal(sr, 10);
        let both = encode(
            &pcm,
            sr,
            AacEncoderConfig {
                bitrate_bps: 128_000,
                pns: true,
                tns: true,
                ..Default::default()
            },
        );
        let mut dec = crate::decode::Decoder::new(sr);
        for p in &both {
            let a = dec.decode(p, None).expect("combined stream must decode");
            assert!(a.samples.iter().all(|v| v.is_finite()));
        }
    }
}

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

    /// A stereo pair whose high bands are a scaled copy — the content intensity
    /// stereo exists for.
    fn correlated_stereo(sr: u32, frames: usize, gain: f32) -> Vec<f32> {
        let n = frames * FRAME_LEN;
        let mut out = Vec::with_capacity(n * 2);
        let mut seed = 0x7777_1111u32;
        for i in 0..n {
            let t = i as f32 / sr as f32;
            let low = 0.3 * (2.0 * std::f32::consts::PI * 300.0 * t).sin();
            seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
            let hi = 0.25 * (((seed >> 8) as f32 / (1u32 << 24) as f32) * 2.0 - 1.0);
            out.push(low + hi);
            out.push(low + hi * gain);
        }
        out
    }

    fn encode(pcm: &[f32], sr: u32, cfg: AacEncoderConfig) -> Vec<Vec<u8>> {
        let mut e = AacEncoder::new(cfg);
        e.push_pcm(pcm, 2, sr).unwrap();
        e.finish();
        let mut out = Vec::new();
        while let Ok(p) = e.next_packet() {
            out.push(p.data);
        }
        out
    }

    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn a7_off_is_byte_identical() {
        let sr = 44100;
        let pcm = correlated_stereo(sr, 8, 0.7);
        let a = encode(&pcm, sr, AacEncoderConfig::default());
        let b = encode(
            &pcm,
            sr,
            AacEncoderConfig {
                intensity: false,
                ..Default::default()
            },
        );
        assert_eq!(a, b);
    }

    /// Fires on a scaled-copy pair and still decodes. A wrong `is_pos` sign or a
    /// desynchronized intensity accumulator shows up here immediately.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn a7_fires_and_round_trips() {
        let sr = 44100;
        let pcm = correlated_stereo(sr, 10, 0.7);
        let off = encode(
            &pcm,
            sr,
            AacEncoderConfig {
                bitrate_bps: 96_000,
                ..Default::default()
            },
        );
        let on = encode(
            &pcm,
            sr,
            AacEncoderConfig {
                bitrate_bps: 96_000,
                intensity: true,
                ..Default::default()
            },
        );
        assert_ne!(off, on, "A7 must fire on a correlated high band");

        let mut dec = crate::decode::Decoder::new(sr);
        let mut got = Vec::new();
        for p in &on {
            let a = dec.decode(p, None).expect("intensity stream must decode");
            got.extend_from_slice(&a.samples);
        }
        assert!(got.iter().all(|v| v.is_finite()));
        // The reconstructed right channel must keep roughly the intended level
        // ratio: a sign or exponent error in is_pos shows up as a gross imbalance.
        let (mut el, mut er) = (0f64, 0f64);
        for c in got.chunks_exact(2) {
            el += f64::from(c[0]) * f64::from(c[0]);
            er += f64::from(c[1]) * f64::from(c[1]);
        }
        let ratio = (er / el.max(1e-12)).sqrt();
        assert!(
            (0.4..1.6).contains(&ratio),
            "channel balance wrong after intensity: R/L = {ratio:.3}"
        );
    }

    /// Must DECLINE on a decorrelated pair — the anti-class. Collapsing a wide
    /// image to a scaled copy is the failure mode this gate exists to prevent.
    #[test]
    #[cfg_attr(miri, ignore = "too slow to interpret: over 2 minutes under Miri")]
    fn a7_declines_on_wide_stereo() {
        let sr = 44100;
        let n = 8 * FRAME_LEN;
        let mut s1 = 0xAAAA_1111u32;
        let mut s2 = 0x5555_2222u32;
        let mut pcm = Vec::with_capacity(n * 2);
        for _ in 0..n {
            s1 = s1.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
            s2 = s2.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
            pcm.push(0.3 * (((s1 >> 8) as f32 / (1u32 << 24) as f32) * 2.0 - 1.0));
            pcm.push(0.3 * (((s2 >> 8) as f32 / (1u32 << 24) as f32) * 2.0 - 1.0));
        }
        let off = encode(
            &pcm,
            sr,
            AacEncoderConfig {
                bitrate_bps: 128_000,
                ..Default::default()
            },
        );
        let on = encode(
            &pcm,
            sr,
            AacEncoderConfig {
                bitrate_bps: 128_000,
                intensity: true,
                ..Default::default()
            },
        );
        assert_eq!(
            off, on,
            "A7 fired on fully decorrelated channels - the correlation gate is not working"
        );
    }
}