oxideav-vp8 0.2.10

Pure-Rust VP8 video codec — orphan-rebuild scaffold pending clean-room re-implementation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
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
//! VP8 dequantization and inverse transforms per RFC 6386 §14.
//!
//! This module implements the pieces of §14 that RFC 6386 specifies
//! **completely in its body**, all operating on small caller-supplied
//! arrays in raster (natural) order:
//!
//! * **§14.1 dequantization tables** — the `dc_qlookup[128]` /
//!   `ac_qlookup[128]` tables transcribed verbatim, plus a Y-plane
//!   dequant-factor computation. The spec states (§14.1 page 77):
//!   *"Lookup values from the above two tables are directly used in the
//!   DC and AC coefficients in Y1, respectively."* So the Y1-DC factor
//!   is `dc_qlookup[clamp(yac_qi + ydc_delta)]` and the Y1-AC factor is
//!   `ac_qlookup[clamp(yac_qi)]`, where the qindex is clamped to the
//!   `0..=127` table range. The Y2 and chroma factors require the
//!   scaling / clamping rules from §14.1 / the §20.4 `dixie.c` annex
//!   (which is part of RFC 6386); those six-factor derivations live in
//!   the [`crate::dequant`] module. This module provides only the raw
//!   lookup tables and the Y1-only factor helper.
//! * **§14.3 inverse WHT** — `inverse_wht_4x4`, a faithful port of the
//!   `vp8_short_inv_walsh4x4_c` C source given in the RFC body, plus
//!   `inverse_wht_4x4_dc_only`, the single-DC fast path
//!   (`vp8_short_inv_walsh4x4_1_c`).
//! * **§14.4 inverse DCT** — `inverse_dct_4x4`, a faithful port of the
//!   `short_idct4x4llm_c` C source (the two `sqrt(2)·cos(pi/8)` /
//!   `sqrt(2)·sin(pi/8)` fixed-point constants, two passes of 1-D
//!   inverse DCT).
//! * **§14.5 summation** — `add_residue` / `clamp255`, summing a
//!   prediction block and a residue block with the §14.5 saturating
//!   `clamp255`.
//!
//! All transform inputs/outputs are 16-bit signed integers exactly as
//! the spec mandates (*"all stored as arrays of 16-bit signed
//! integers"*), and the spec notes a conforming decoder must reproduce
//! the rounding bit-for-bit.
//!
//! # What is NOT in this module (out of scope for §14 standalone)
//!
//! * The **zig-zag → raster reordering** of the §13 scan-ordered
//!   coefficients. §13 says the coefficients "follow a so-called
//!   zig-zag order" (§13 page 60) but the spec body never gives the
//!   permutation array — it lives only in the reference `idct_add.c`
//!   (Section 20.8), which is off-limits. This is a documented gap; the
//!   per-macroblock integration round must supply the reordering before
//!   calling the raster-order transforms here.
//! * The **Y2-DC / Y2-AC / chroma-DC / chroma-AC dequant factors** —
//!   the scaling and clamping rules from §14.1 / §20.4. These now live
//!   in [`crate::dequant`] ([`crate::dequant::MbDequantFactors`]).
//! * The §14.2 macroblock-level orchestration (Y2 → 16 Y DC seeding,
//!   the 24/25-block walk) — that is the integration round's job; this
//!   module provides only the per-block primitives.
//!
//! # Resolved spec references
//!
//! * **§14.1 page 77 Y2 / chroma dequant scaling.** §14.1 page 77 says
//!   the Y2 and chroma factors *"undergo either scaling or clamping
//!   before the multiplies. Details ... can be found in related lookup
//!   functions in dixie.c (Section 20.4)."* Section 20.4 is part of RFC
//!   6386 (the reference-decoder annex), so the scaling / clamping rules
//!   it gives are in-spec; they are implemented in [`crate::dequant`].
//! * **§13 page 60 zig-zag scan order.** The 16-entry scan-to-raster
//!   permutation is given by the §20.16 `zigzag[16]` table (also part of
//!   RFC 6386); the per-MB token walk applies it (see
//!   [`crate::dct_tokens`]).

/// Number of entries in each §14.1 dequantization lookup table; the
/// 7-bit quantiser index (`L(7)`, §9.6) plus deltas is clamped into
/// `0..QINDEX_RANGE`.
pub const QINDEX_RANGE: usize = 128;

/// The §14.1 `dc_qlookup[QINDEX_RANGE]` table, transcribed verbatim
/// from RFC 6386 page 77.
pub static DC_QLOOKUP: [i16; QINDEX_RANGE] = [
    4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, //
    16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, //
    24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, //
    36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 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, 76, 77, 78, 79, 80, 81, 82, 83, 84, //
    85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, //
    104, 106, 108, 110, 112, 114, 116, 118, 122, 124, 126, 128, 130, //
    132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, //
];

/// The §14.1 `ac_qlookup[QINDEX_RANGE]` table, transcribed verbatim
/// from RFC 6386 page 77.
pub static AC_QLOOKUP: [i16; QINDEX_RANGE] = [
    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, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, //
    80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, //
    106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, //
    140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, //
    181, 185, 189, 193, 197, 201, 205, 209, 213, 217, 221, 225, 229, //
    234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, //
];

/// Clamp a (possibly delta-adjusted) quantiser index into the
/// `0..=QINDEX_RANGE-1` lookup-table domain. The base `yac_qi` is a
/// 7-bit value (`0..=127`); applying a signed delta (§9.6) can push it
/// out of range, and the lookup must saturate.
#[inline]
pub fn clamp_qindex(q: i32) -> usize {
    q.clamp(0, (QINDEX_RANGE - 1) as i32) as usize
}

/// The Y1-plane dequantization factors, derived from §14.1 page 77 and
/// the §9.6 quantiser-index layout.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Y1DequantFactors {
    /// Factor for coefficient 0 (DC): `dc_qlookup[clamp(yac_qi + ydc_delta)]`.
    pub dc: i16,
    /// Factor for coefficients 1..=15 (AC): `ac_qlookup[clamp(yac_qi)]`.
    pub ac: i16,
}

impl Y1DequantFactors {
    /// Compute the Y1 DC and AC dequant factors from the frame-level
    /// base index `yac_qi` (§9.6, the always-present `L(7)`) and the
    /// signed Y-DC delta `ydc_delta` (§9.6 `ydc_delta`). The §14.1 rule
    /// is that Y1 uses the lookup values directly: DC from `dc_qlookup`
    /// indexed by `yac_qi + ydc_delta`, AC from `ac_qlookup` indexed by
    /// `yac_qi`. Both indices are clamped into the table range.
    pub fn from_indices(yac_qi: i32, ydc_delta: i32) -> Self {
        Y1DequantFactors {
            dc: DC_QLOOKUP[clamp_qindex(yac_qi + ydc_delta)],
            ac: AC_QLOOKUP[clamp_qindex(yac_qi)],
        }
    }
}

/// Dequantize a 4×4 sub-block in place: coefficient 0 (DC) is
/// multiplied by `dc_factor`, coefficients 1..=15 (AC) by `ac_factor`.
///
/// §14.1 page 76: *"each (quantized) coefficient ... is multiplied by
/// one of six dequantization factors ... the multiplies are computed
/// and stored using 16-bit signed integers."* The product is computed
/// in `i32` and stored back as `i16`. The `coeffs` array is assumed to
/// be in raster (natural) order — the caller is responsible for the
/// zig-zag → raster reordering (see the module-level gap note), which
/// does not affect the DC-vs-AC factor selection because coefficient 0
/// is the DC in either ordering.
pub fn dequant_block(coeffs: &mut [i16; 16], dc_factor: i16, ac_factor: i16) {
    coeffs[0] = (coeffs[0] as i32 * dc_factor as i32) as i16;
    for c in coeffs.iter_mut().skip(1) {
        *c = (*c as i32 * ac_factor as i32) as i16;
    }
}

/// The §14.5 saturating clamp: `v < 0 ? 0 : (v < 255 ? v : 255)`
/// (RFC 6386 page 11 `clamp255`).
#[inline]
pub fn clamp255(v: i32) -> u8 {
    v.clamp(0, 255) as u8
}

/// Inverse Walsh-Hadamard transform of the 25th (Y2) block, per the
/// `vp8_short_inv_walsh4x4_c` C source of RFC 6386 §14.3.
///
/// `input` and `output` are 4×4 in row-major (raster) order. The
/// output values are the dequantized DC residues that seed the 0th
/// coefficient of each of the sixteen Y sub-blocks (§14.2): output
/// element at row `i`, column `j` feeds Y sub-block `i*4 + j`.
///
/// All arithmetic is performed exactly as the spec C source specifies
/// (two passes, the `(x + 3) >> 3` rounding in the second pass), so the
/// rounding matches bit-for-bit.
pub fn inverse_wht_4x4(input: &[i16; 16], output: &mut [i16; 16]) {
    // Dispatch: SIMD path on nightly+`simd`, scalar otherwise.
    #[cfg(feature = "simd")]
    {
        inverse_wht_4x4_simd(input, output);
    }
    #[cfg(not(feature = "simd"))]
    {
        inverse_wht_4x4_scalar(input, output);
    }
}

/// Scalar §14.3 inverse WHT — the literal RFC 6386 listing.
///
/// Bit-for-bit byte-exact with the spec C source. The public
/// [`inverse_wht_4x4`] dispatches here on stable builds (and on nightly
/// without the `simd` feature); the `simd` feature swaps in
/// [`inverse_wht_4x4_simd`], which is itself byte-exact against this
/// implementation.
#[allow(dead_code)] // Used by `inverse_wht_4x4` only on the !simd path.
fn inverse_wht_4x4_scalar(input: &[i16; 16], output: &mut [i16; 16]) {
    // First pass: operate down each column (spec: ip[0], ip[4], ip[8],
    // ip[12] with ip++ stepping across the four columns). Intermediate
    // results are written back into `output` at the matching column.
    let mut tmp = [0i32; 16];
    for col in 0..4 {
        let i0 = input[col] as i32;
        let i4 = input[4 + col] as i32;
        let i8 = input[8 + col] as i32;
        let i12 = input[12 + col] as i32;

        let a1 = i0 + i12;
        let b1 = i4 + i8;
        let c1 = i4 - i8;
        let d1 = i0 - i12;

        tmp[col] = a1 + b1;
        tmp[4 + col] = c1 + d1;
        tmp[8 + col] = a1 - b1;
        tmp[12 + col] = d1 - c1;
    }

    // Second pass: operate across each row (spec: ip[0..=3] with ip+=4
    // stepping down the rows), with the `(x + 3) >> 3` rounding.
    for row in 0..4 {
        let base = row * 4;
        let r0 = tmp[base];
        let r1 = tmp[base + 1];
        let r2 = tmp[base + 2];
        let r3 = tmp[base + 3];

        let a1 = r0 + r3;
        let b1 = r1 + r2;
        let c1 = r1 - r2;
        let d1 = r0 - r3;

        let a2 = a1 + b1;
        let b2 = c1 + d1;
        let c2 = a1 - b1;
        let d2 = d1 - c1;

        output[base] = ((a2 + 3) >> 3) as i16;
        output[base + 1] = ((b2 + 3) >> 3) as i16;
        output[base + 2] = ((c2 + 3) >> 3) as i16;
        output[base + 3] = ((d2 + 3) >> 3) as i16;
    }
}

/// SIMD §14.3 inverse WHT — `core::simd::Simd<i32, 4>` rewrite of
/// [`inverse_wht_4x4_scalar`].
///
/// The §14.3 WHT first pass operates "down each column" on four
/// independent columns; that maps directly onto a 4-lane vector where
/// each lane holds one column. We load the four `(i0, i4, i8, i12)`
/// row-vectors as four `Simd<i32, 4>` lanes, apply the §14.3
/// add/subtract butterfly across them, transpose the 4×4 result so the
/// second pass (which operates "across each row") can again run as four
/// lane-wide adds/subs, apply the `(x + 3) >> 3` rounding, and store the
/// result as `i16`. No external SIMD layout reference was consulted —
/// the layout is derived from the §14.3 listing in RFC 6386 directly
/// (one lane per column ⇒ first-pass vectorises; transpose ⇒
/// second-pass vectorises).
///
/// Byte-exact against [`inverse_wht_4x4_scalar`] on every test fixture.
#[cfg(feature = "simd")]
fn inverse_wht_4x4_simd(input: &[i16; 16], output: &mut [i16; 16]) {
    use core::simd::Simd;

    // Each row-vector holds the four columns of one input row. Promote
    // to i32 once, up-front, so the lane butterflies stay in i32 (the
    // intermediate can exceed i16 range — the scalar path uses i32 too).
    let row0 = Simd::<i32, 4>::from_array([
        input[0] as i32,
        input[1] as i32,
        input[2] as i32,
        input[3] as i32,
    ]);
    let row1 = Simd::<i32, 4>::from_array([
        input[4] as i32,
        input[5] as i32,
        input[6] as i32,
        input[7] as i32,
    ]);
    let row2 = Simd::<i32, 4>::from_array([
        input[8] as i32,
        input[9] as i32,
        input[10] as i32,
        input[11] as i32,
    ]);
    let row3 = Simd::<i32, 4>::from_array([
        input[12] as i32,
        input[13] as i32,
        input[14] as i32,
        input[15] as i32,
    ]);

    // First pass — §14.3 column butterfly across the four lanes.
    let a1 = row0 + row3;
    let b1 = row1 + row2;
    let c1 = row1 - row2;
    let d1 = row0 - row3;

    let t0 = a1 + b1;
    let t1 = c1 + d1;
    let t2 = a1 - b1;
    let t3 = d1 - c1;

    // Transpose the 4×4 i32 matrix so each row-vector now holds a row
    // of the intermediate (rather than a column). `Simd::from_array` is
    // free at compile time; we materialise the rows explicitly to keep
    // the layout obvious. r_i[j] = t_j[i].
    let t0a = t0.to_array();
    let t1a = t1.to_array();
    let t2a = t2.to_array();
    let t3a = t3.to_array();

    let r0 = Simd::<i32, 4>::from_array([t0a[0], t1a[0], t2a[0], t3a[0]]);
    let r1 = Simd::<i32, 4>::from_array([t0a[1], t1a[1], t2a[1], t3a[1]]);
    let r2 = Simd::<i32, 4>::from_array([t0a[2], t1a[2], t2a[2], t3a[2]]);
    let r3v = Simd::<i32, 4>::from_array([t0a[3], t1a[3], t2a[3], t3a[3]]);

    // Second pass — §14.3 row butterfly. Same shape as the first pass,
    // operating on (r0, r1, r2, r3v).
    let a2 = r0 + r3v;
    let b2 = r1 + r2;
    let c2 = r1 - r2;
    let d2 = r0 - r3v;

    let u0 = a2 + b2;
    let u1 = c2 + d2;
    let u2 = a2 - b2;
    let u3 = d2 - c2;

    // §14.3 rounding: (x + 3) >> 3. Apply as lane-wide adds + shifts.
    let three = Simd::<i32, 4>::splat(3);
    let o0 = (u0 + three) >> Simd::<i32, 4>::splat(3);
    let o1 = (u1 + three) >> Simd::<i32, 4>::splat(3);
    let o2 = (u2 + three) >> Simd::<i32, 4>::splat(3);
    let o3 = (u3 + three) >> Simd::<i32, 4>::splat(3);

    // Now: o_i[j] is the second-pass result of the i-th source row's
    // j-th column → output[i*4 + j]. Re-transpose back so output writes
    // one row at a time.
    let o0a = o0.to_array();
    let o1a = o1.to_array();
    let o2a = o2.to_array();
    let o3a = o3.to_array();

    // o_i was computed from r_{0..3v}, each of which holds the i-th
    // *column* of the post-first-pass matrix. So o0[j] is the
    // butterfly output for row j, col 0 — i.e. output[j*4 + 0]. The
    // store therefore stripes across o_i:
    for j in 0..4 {
        output[j * 4] = o0a[j] as i16;
        output[j * 4 + 1] = o1a[j] as i16;
        output[j * 4 + 2] = o2a[j] as i16;
        output[j * 4 + 3] = o3a[j] as i16;
    }
}

/// The single-non-zero-DC fast path of the inverse WHT, per
/// `vp8_short_inv_walsh4x4_1_c` of RFC 6386 §14.3. When only
/// `input[0]` is non-zero, every output element is `(input[0] + 3) >> 3`.
pub fn inverse_wht_4x4_dc_only(dc: i16, output: &mut [i16; 16]) {
    let a1 = ((dc as i32) + 3) >> 3;
    for o in output.iter_mut() {
        *o = a1 as i16;
    }
}

/// 16-bit fixed-point `sqrt(2) * cos(pi/8) - 1`, per RFC 6386 §14.4.
const COSPI8_SQRT2_MINUS1: i32 = 20091;
/// 16-bit fixed-point `sqrt(2) * sin(pi/8)`, per RFC 6386 §14.4.
const SINPI8_SQRT2: i32 = 35468;

/// Inverse DCT of a 4×4 Y / U / V sub-block, per the
/// `short_idct4x4llm_c` C source of RFC 6386 §14.4.
///
/// `input` and `output` are 4×4 in row-major (raster) order. The
/// implementation is two passes of the On2 4-point 1-D inverse DCT with
/// the two fixed-point constants above; the second pass applies the
/// `(x + 4) >> 3` rounding the spec mandates for bit-exact output.
///
/// The spec's C source uses `pitch` (a byte pitch) and `shortpitch =
/// pitch >> 1`. For a self-contained 4×4 → 4×4 transform the natural
/// `shortpitch` is 4 (one row of `short`s), so the column stride in the
/// first pass and the row stride in the second pass are both 4. This is
/// the standard packed-4×4 case and is what this function implements.
///
/// Dispatch: SIMD path on nightly + `simd`, scalar otherwise. The SIMD
/// path is byte-exact against the scalar listing on every test fixture.
pub fn inverse_dct_4x4(input: &[i16; 16], output: &mut [i16; 16]) {
    #[cfg(feature = "simd")]
    {
        inverse_dct_4x4_simd(input, output);
    }
    #[cfg(not(feature = "simd"))]
    {
        inverse_dct_4x4_scalar(input, output);
    }
}

/// Fused §14.4 inverse DCT + §14.5 add-residue clamp, writing the
/// reconstructed sub-block directly into a strided plane.
///
/// `plane` is the prediction raster (row-major, row stride `stride`);
/// `(sub_row, sub_col)` selects the 4×4 sub-block (top-left pixel at
/// `(sub_row*4, sub_col*4)`). On entry the sub-block holds the §18
/// prediction; on return it holds `clamp255(prediction + residue)`.
///
/// Output is bit-identical to the unfused
/// `inverse_dct_4x4` → extract → `add_residue_4x4` → insert sequence:
/// the §14.4 transform arithmetic is unchanged and the per-pixel
/// `clamp255(plane[p] + residue)` matches `add_residue_4x4` exactly
/// (`fused_idct_add_into_matches_unfused_sequence` proves it on both
/// paths).
///
/// Path split: under nightly + `simd` the second IDCT pass is folded
/// with a lane-wide §14.5 add-clamp store
/// ([`inverse_dct_4x4_add_into_simd`]), which the round-286 A/B
/// (`idct_add_residue_fusion`) measured faster than the unfused
/// sequence. On the scalar (stable / default-CI) path the unfused
/// sequence already autovectorises the full 16-element add-clamp as one
/// chunk and a strided fold regressed it, so the scalar path keeps the
/// proven extract → `add_residue_4x4` → insert shape — same single
/// helper for callers, no scalar slowdown.
#[inline]
pub fn inverse_dct_4x4_add_into(
    input: &[i16; 16],
    plane: &mut [u8],
    stride: usize,
    sub_row: usize,
    sub_col: usize,
) {
    // §14.4 DC-only fast path. When every AC coefficient is zero (only
    // `input[0]` is non-zero — the common case for well-predicted inter
    // residue), both separable passes carry only the DC term: pass 1
    // leaves `tmp[0]=tmp[4]=tmp[8]=tmp[12]=input[0]` (the rest zero), and
    // pass 2's row butterfly then yields `(input[0] + 4) >> 3` for all
    // sixteen outputs. So the residue is the single uniform value
    // `(input[0] + 4) >> 3` added to every predictor pixel — identical to
    // the full transform, with no butterfly. Bit-exact against the
    // general path (`dc_only_add_into_matches_general_path`).
    if input[1..].iter().all(|&c| c == 0) {
        let dc = ((input[0] as i32) + 4) >> 3;
        if dc != 0 {
            let y0 = sub_row * 4;
            let x0 = sub_col * 4;
            for r in 0..4 {
                let base = (y0 + r) * stride + x0;
                for p in &mut plane[base..base + 4] {
                    *p = (*p as i32 + dc).clamp(0, 255) as u8;
                }
            }
        }
        // dc == 0: residue is all-zero, prediction is already the output.
        return;
    }
    #[cfg(feature = "simd")]
    {
        inverse_dct_4x4_add_into_simd(input, plane, stride, sub_row, sub_col);
    }
    #[cfg(not(feature = "simd"))]
    {
        // Scalar path: the unfused contiguous-buffer sequence. Building
        // the full [i16;16] residue then a single 16-wide add-clamp into
        // a contiguous [u8;16] autovectorises better than a row-at-a-time
        // strided fold (the round-286 A/B measured the fold ~6 % slower
        // on the scalar path).
        let mut residue = [0i16; 16];
        inverse_dct_4x4_scalar(input, &mut residue);
        let y0 = sub_row * 4;
        let x0 = sub_col * 4;
        let mut pred = [0u8; 16];
        for r in 0..4 {
            let src = (y0 + r) * stride + x0;
            pred[r * 4..r * 4 + 4].copy_from_slice(&plane[src..src + 4]);
        }
        let mut summed = [0u8; 16];
        add_residue_4x4(&pred, &residue, &mut summed);
        for r in 0..4 {
            let dst = (y0 + r) * stride + x0;
            plane[dst..dst + 4].copy_from_slice(&summed[r * 4..r * 4 + 4]);
        }
    }
}

/// SIMD fused §14.4 IDCT + §14.5 add-clamp — the lane layout of
/// [`inverse_dct_4x4_simd`] with the second-pass rounded outputs added
/// to the strided predictor and clamped in place.
#[cfg(feature = "simd")]
fn inverse_dct_4x4_add_into_simd(
    input: &[i16; 16],
    plane: &mut [u8],
    stride: usize,
    sub_row: usize,
    sub_col: usize,
) {
    use core::simd::Simd;
    use core::simd::cmp::SimdOrd;

    let row0 = Simd::<i32, 4>::from_array([
        input[0] as i32,
        input[1] as i32,
        input[2] as i32,
        input[3] as i32,
    ]);
    let row1 = Simd::<i32, 4>::from_array([
        input[4] as i32,
        input[5] as i32,
        input[6] as i32,
        input[7] as i32,
    ]);
    let row2 = Simd::<i32, 4>::from_array([
        input[8] as i32,
        input[9] as i32,
        input[10] as i32,
        input[11] as i32,
    ]);
    let row3 = Simd::<i32, 4>::from_array([
        input[12] as i32,
        input[13] as i32,
        input[14] as i32,
        input[15] as i32,
    ]);

    let sin = Simd::<i32, 4>::splat(SINPI8_SQRT2);
    let cos_m1 = Simd::<i32, 4>::splat(COSPI8_SQRT2_MINUS1);
    let sh16 = Simd::<i32, 4>::splat(16);

    // First pass — §14.4 column butterfly.
    let a1 = row0 + row2;
    let b1 = row0 - row2;

    let t1_c = (row1 * sin) >> sh16;
    let t2_c = row3 + ((row3 * cos_m1) >> sh16);
    let c1 = t1_c - t2_c;

    let t1_d = row1 + ((row1 * cos_m1) >> sh16);
    let t2_d = (row3 * sin) >> sh16;
    let d1 = t1_d + t2_d;

    let t0 = a1 + d1;
    let t1 = b1 + c1;
    let t2 = b1 - c1;
    let t3 = a1 - d1;

    // Transpose for the second pass.
    let t0a = t0.to_array();
    let t1a = t1.to_array();
    let t2a = t2.to_array();
    let t3a = t3.to_array();

    let s0 = Simd::<i32, 4>::from_array([t0a[0], t1a[0], t2a[0], t3a[0]]);
    let s1 = Simd::<i32, 4>::from_array([t0a[1], t1a[1], t2a[1], t3a[1]]);
    let s2 = Simd::<i32, 4>::from_array([t0a[2], t1a[2], t2a[2], t3a[2]]);
    let s3 = Simd::<i32, 4>::from_array([t0a[3], t1a[3], t2a[3], t3a[3]]);

    // Second pass — §14.4 row butterfly.
    let a2 = s0 + s2;
    let b2 = s0 - s2;

    let t1_c2 = (s1 * sin) >> sh16;
    let t2_c2 = s3 + ((s3 * cos_m1) >> sh16);
    let c2 = t1_c2 - t2_c2;

    let t1_d2 = s1 + ((s1 * cos_m1) >> sh16);
    let t2_d2 = (s3 * sin) >> sh16;
    let d2 = t1_d2 + t2_d2;

    let four = Simd::<i32, 4>::splat(4);
    let sh3 = Simd::<i32, 4>::splat(3);
    // o_k[j] = rounded residue for row j, column k.
    let o0 = (a2 + d2 + four) >> sh3;
    let o3 = (a2 - d2 + four) >> sh3;
    let o1 = (b2 + c2 + four) >> sh3;
    let o2 = (b2 - c2 + four) >> sh3;

    // Transpose the four column-vectors back into four row-vectors so
    // each holds one raster row's residue (lane k = column k). This lets
    // the §14.5 add-clamp run lane-wide against the four predictor bytes
    // of that row — one SIMD add + lane clamp + narrow per row, rather
    // than sixteen scalar `clamp255` calls.
    let o0a = o0.to_array();
    let o1a = o1.to_array();
    let o2a = o2.to_array();
    let o3a = o3.to_array();
    let res_rows = [
        Simd::<i32, 4>::from_array([o0a[0], o1a[0], o2a[0], o3a[0]]),
        Simd::<i32, 4>::from_array([o0a[1], o1a[1], o2a[1], o3a[1]]),
        Simd::<i32, 4>::from_array([o0a[2], o1a[2], o2a[2], o3a[2]]),
        Simd::<i32, 4>::from_array([o0a[3], o1a[3], o2a[3], o3a[3]]),
    ];

    let lo = Simd::<i32, 4>::splat(0);
    let hi = Simd::<i32, 4>::splat(255);
    let y0 = sub_row * 4;
    let x0 = sub_col * 4;
    for (j, res) in res_rows.iter().enumerate() {
        let dst = (y0 + j) * stride + x0;
        let p = &mut plane[dst..dst + 4];
        let pred = Simd::<i32, 4>::from_array([p[0] as i32, p[1] as i32, p[2] as i32, p[3] as i32]);
        // clamp255(pred + residue), lane-wide.
        let summed = (pred + res).simd_clamp(lo, hi).to_array();
        p[0] = summed[0] as u8;
        p[1] = summed[1] as u8;
        p[2] = summed[2] as u8;
        p[3] = summed[3] as u8;
    }
}

/// Scalar §14.4 inverse DCT — the literal RFC 6386 listing.
///
/// Bit-for-bit byte-exact with the spec C source. The public
/// [`inverse_dct_4x4`] dispatches here on stable builds (and on nightly
/// without the `simd` feature); the `simd` feature swaps in
/// [`inverse_dct_4x4_simd`], which is itself byte-exact against this
/// implementation.
#[allow(dead_code)] // Used by `inverse_dct_4x4` only on the !simd path.
fn inverse_dct_4x4_scalar(input: &[i16; 16], output: &mut [i16; 16]) {
    // §14.4 arithmetic runs in the 32-bit-`int` domain of the reference
    // `short_idct4x4llm_c` listing, which relies on two's-complement
    // wrap-around: the first-pass intermediates can reach ~1.3e5 for
    // near-`i16::MAX` inputs, and the second-pass `r * SINPI8_SQRT2`
    // product (~4.5e9 at that magnitude) exceeds `i32::MAX`. The listing's
    // `int` overflow wraps, and the §14.4 bit-exactness contract (a
    // conforming decoder reproduces the rounding bit-for-bit) is defined
    // against that wrapped result — which is exactly what the release
    // build and the `simd` lane path (documented wrapping `i32` multiply)
    // already produce. Hostile post-dequant coefficients can drive those
    // magnitudes (the §14.1 dequant `(coeff * factor) as i16` truncation
    // can land anywhere in the `i16` range), so the scalar path uses
    // explicit `wrapping_*` ops to match that semantics without tripping a
    // debug-assertion overflow panic on such input.
    let mut tmp = [0i32; 16];

    // First pass: operate down each column (spec: ip[0], ip[4], ip[8],
    // ip[12] with ip++ stepping across columns; results written with a
    // `shortpitch` stride into `op`).
    for col in 0..4 {
        let i0 = input[col] as i32;
        let i4 = input[4 + col] as i32;
        let i8 = input[8 + col] as i32;
        let i12 = input[12 + col] as i32;

        let a1 = i0.wrapping_add(i8);
        let b1 = i0.wrapping_sub(i8);

        let t1 = i4.wrapping_mul(SINPI8_SQRT2) >> 16;
        let t2 = i12.wrapping_add(i12.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
        let c1 = t1.wrapping_sub(t2);

        let t1 = i4.wrapping_add(i4.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
        let t2 = i12.wrapping_mul(SINPI8_SQRT2) >> 16;
        let d1 = t1.wrapping_add(t2);

        tmp[col] = a1.wrapping_add(d1);
        tmp[12 + col] = a1.wrapping_sub(d1);
        tmp[4 + col] = b1.wrapping_add(c1);
        tmp[8 + col] = b1.wrapping_sub(c1);
    }

    // Second pass: operate across each row (spec: ip[0..=3] reading the
    // intermediate buffer, ip += shortpitch stepping down the rows),
    // with the `(x + 4) >> 3` rounding.
    for row in 0..4 {
        let base = row * 4;
        let r0 = tmp[base];
        let r1 = tmp[base + 1];
        let r2 = tmp[base + 2];
        let r3 = tmp[base + 3];

        let a1 = r0.wrapping_add(r2);
        let b1 = r0.wrapping_sub(r2);

        let t1 = r1.wrapping_mul(SINPI8_SQRT2) >> 16;
        let t2 = r3.wrapping_add(r3.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
        let c1 = t1.wrapping_sub(t2);

        let t1 = r1.wrapping_add(r1.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
        let t2 = r3.wrapping_mul(SINPI8_SQRT2) >> 16;
        let d1 = t1.wrapping_add(t2);

        output[base] = (a1.wrapping_add(d1).wrapping_add(4) >> 3) as i16;
        output[base + 3] = (a1.wrapping_sub(d1).wrapping_add(4) >> 3) as i16;
        output[base + 1] = (b1.wrapping_add(c1).wrapping_add(4) >> 3) as i16;
        output[base + 2] = (b1.wrapping_sub(c1).wrapping_add(4) >> 3) as i16;
    }
}

/// SIMD §14.4 inverse DCT — `core::simd::Simd<i32, 4>` rewrite of
/// [`inverse_dct_4x4_scalar`].
///
/// The §14.4 inverse DCT is the same separable two-pass shape as the
/// §14.3 WHT: both passes operate on four independent 1-D 4-point
/// transforms. The first pass takes one column at a time; if we hold
/// the input matrix as four `Simd<i32, 4>` row-vectors (lane `j` of row
/// `i` carries `input[i*4 + j]`), then each lane of the row-vector
/// already corresponds to one column of the matrix, and the entire
/// first-pass column butterfly maps onto four parallel SIMD adds, subs,
/// and fixed-point multiplies.
///
/// The fixed-point multiplies — `(x * SINPI8_SQRT2) >> 16` and
/// `x + ((x * COSPI8_SQRT2_MINUS1) >> 16)` — vectorise as
/// `Simd::splat(SINPI8_SQRT2) * v >> Simd::splat(16)` etc. The
/// `Simd<i32, 4> * Simd<i32, 4>` lane product is full-width (the SIMD
/// API specifies wrapping `i32` multiply matching scalar `i32::wrapping_mul`),
/// then the `>> 16` lane shift drops the low 16 bits exactly like the
/// scalar `(x * K) >> 16` expression. The `>> 16` shift is arithmetic
/// for signed `Simd<i32, 4>` (Rust spec: `>>` on signed integers is
/// arithmetic shift right) so the high-bit sign-extension matches the
/// scalar listing.
///
/// After the first pass we transpose the 4×4 to put each row of the
/// intermediate into a row-vector, run the second pass (same butterfly,
/// different constants in the lane shuffle since the second pass writes
/// `output[base], output[base+3], output[base+1], output[base+2]`), and
/// apply the `(x + 4) >> 3` rounding lane-wide.
///
/// No external SIMD reference consulted — the layout is derived from
/// the §14.4 listing in RFC 6386 directly (one lane per column ⇒
/// first-pass vectorises; transpose ⇒ second-pass vectorises). Byte-
/// exact against [`inverse_dct_4x4_scalar`] on every test fixture.
#[cfg(feature = "simd")]
fn inverse_dct_4x4_simd(input: &[i16; 16], output: &mut [i16; 16]) {
    use core::simd::Simd;

    // Row-vectors: row_i[j] = input[i*4 + j]. Lane j is column j.
    let row0 = Simd::<i32, 4>::from_array([
        input[0] as i32,
        input[1] as i32,
        input[2] as i32,
        input[3] as i32,
    ]);
    let row1 = Simd::<i32, 4>::from_array([
        input[4] as i32,
        input[5] as i32,
        input[6] as i32,
        input[7] as i32,
    ]);
    let row2 = Simd::<i32, 4>::from_array([
        input[8] as i32,
        input[9] as i32,
        input[10] as i32,
        input[11] as i32,
    ]);
    let row3 = Simd::<i32, 4>::from_array([
        input[12] as i32,
        input[13] as i32,
        input[14] as i32,
        input[15] as i32,
    ]);

    // Splatted constants. Holding the splats in locals lets LLVM keep
    // them in registers across the two passes.
    let sin = Simd::<i32, 4>::splat(SINPI8_SQRT2);
    let cos_m1 = Simd::<i32, 4>::splat(COSPI8_SQRT2_MINUS1);
    let sh16 = Simd::<i32, 4>::splat(16);

    // First pass — §14.4 column butterfly, four columns in parallel.
    // (row0, row1, row2, row3) play the role of (i0, i4, i8, i12).
    let a1 = row0 + row2;
    let b1 = row0 - row2;

    let t1_c = (row1 * sin) >> sh16;
    let t2_c = row3 + ((row3 * cos_m1) >> sh16);
    let c1 = t1_c - t2_c;

    let t1_d = row1 + ((row1 * cos_m1) >> sh16);
    let t2_d = (row3 * sin) >> sh16;
    let d1 = t1_d + t2_d;

    // Intermediate rows: per the scalar listing,
    //   tmp[col]      = a1 + d1   (row 0)
    //   tmp[4 + col]  = b1 + c1   (row 1)
    //   tmp[8 + col]  = b1 - c1   (row 2)
    //   tmp[12 + col] = a1 - d1   (row 3)
    let t0 = a1 + d1;
    let t1 = b1 + c1;
    let t2 = b1 - c1;
    let t3 = a1 - d1;

    // Transpose so each row-vector now carries one row of the
    // intermediate (lane j of new_row_i = old t_i[j]) ⇒ each new
    // row-vector is one input to a second-pass 4-point 1-D DCT.
    let t0a = t0.to_array();
    let t1a = t1.to_array();
    let t2a = t2.to_array();
    let t3a = t3.to_array();

    let s0 = Simd::<i32, 4>::from_array([t0a[0], t1a[0], t2a[0], t3a[0]]);
    let s1 = Simd::<i32, 4>::from_array([t0a[1], t1a[1], t2a[1], t3a[1]]);
    let s2 = Simd::<i32, 4>::from_array([t0a[2], t1a[2], t2a[2], t3a[2]]);
    let s3 = Simd::<i32, 4>::from_array([t0a[3], t1a[3], t2a[3], t3a[3]]);

    // Second pass — §14.4 row butterfly. Same shape: (s0, s1, s2, s3)
    // play the role of (r0, r1, r2, r3) in the scalar listing, with
    // each lane now being one row of the intermediate.
    let a2 = s0 + s2;
    let b2 = s0 - s2;

    let t1_c2 = (s1 * sin) >> sh16;
    let t2_c2 = s3 + ((s3 * cos_m1) >> sh16);
    let c2 = t1_c2 - t2_c2;

    let t1_d2 = s1 + ((s1 * cos_m1) >> sh16);
    let t2_d2 = (s3 * sin) >> sh16;
    let d2 = t1_d2 + t2_d2;

    // §14.4 rounding: (x + 4) >> 3.
    let four = Simd::<i32, 4>::splat(4);
    let sh3 = Simd::<i32, 4>::splat(3);
    let o0 = (a2 + d2 + four) >> sh3; // output[base]
    let o3 = (a2 - d2 + four) >> sh3; // output[base + 3]
    let o1 = (b2 + c2 + four) >> sh3; // output[base + 1]
    let o2 = (b2 - c2 + four) >> sh3; // output[base + 2]

    // Each o_k holds, at lane j, the second-pass output for row j at
    // column k. Stripe back into raster order: output[j*4 + k] = o_k[j].
    let o0a = o0.to_array();
    let o1a = o1.to_array();
    let o2a = o2.to_array();
    let o3a = o3.to_array();
    for j in 0..4 {
        output[j * 4] = o0a[j] as i16;
        output[j * 4 + 1] = o1a[j] as i16;
        output[j * 4 + 2] = o2a[j] as i16;
        output[j * 4 + 3] = o3a[j] as i16;
    }
}

/// Differential probe for the fuzz harness: run the §14.3 inverse WHT on
/// `input` through BOTH [`inverse_wht_4x4_scalar`] and
/// [`inverse_wht_4x4_simd`] and return `(scalar, simd)` so the caller can
/// assert byte-equality.
///
/// Only compiled under the `simd` feature (without it there is exactly one
/// implementation and nothing to compare). Behaviour-neutral: nothing in
/// the decode or encode pipeline calls this — it exists so the
/// `decode_stream_token_descent` fuzz target can turn any scalar/SIMD
/// divergence on attacker-shaped inputs into a finding. `input` must stay
/// inside the §14.2 coefficient envelope (the same contract the public
/// [`inverse_wht_4x4`] carries).
#[cfg(feature = "simd")]
#[doc(hidden)]
pub fn inverse_wht_4x4_parity_pair(input: &[i16; 16]) -> ([i16; 16], [i16; 16]) {
    let mut scalar = [0i16; 16];
    inverse_wht_4x4_scalar(input, &mut scalar);
    let mut simd = [0i16; 16];
    inverse_wht_4x4_simd(input, &mut simd);
    (scalar, simd)
}

/// Differential probe for the fuzz harness: run the §14.4 inverse DCT on
/// `input` through BOTH [`inverse_dct_4x4_scalar`] and
/// [`inverse_dct_4x4_simd`] and return `(scalar, simd)` so the caller can
/// assert byte-equality.
///
/// Only compiled under the `simd` feature (without it there is exactly one
/// implementation and nothing to compare). Behaviour-neutral: nothing in
/// the decode or encode pipeline calls this — it exists so the
/// `decode_stream_token_descent` fuzz target can turn any scalar/SIMD
/// divergence on attacker-shaped inputs into a finding. `input` must stay
/// inside the §14.2 coefficient envelope (the same contract the public
/// [`inverse_dct_4x4`] carries).
#[cfg(feature = "simd")]
#[doc(hidden)]
pub fn inverse_dct_4x4_parity_pair(input: &[i16; 16]) -> ([i16; 16], [i16; 16]) {
    let mut scalar = [0i16; 16];
    inverse_dct_4x4_scalar(input, &mut scalar);
    let mut simd = [0i16; 16];
    inverse_dct_4x4_simd(input, &mut simd);
    (scalar, simd)
}

/// §14.5 summation of predictor and residue for one 4×4 sub-block.
///
/// Each output pixel is `clamp255(prediction[i] + residue[i])`, with
/// the sum computed at 32-bit precision before saturation, exactly as
/// §14.5 page 83 specifies. `prediction` holds 8-bit pixels (carried as
/// `u8`); `residue` holds the inverse-transformed 16-bit residual.
pub fn add_residue_4x4(prediction: &[u8; 16], residue: &[i16; 16], out: &mut [u8; 16]) {
    for i in 0..16 {
        out[i] = clamp255(prediction[i] as i32 + residue[i] as i32);
    }
}

/// §14.5 summation over an arbitrary-length block (e.g. a 16×16 luma or
/// 8×8 chroma macroblock buffer). `prediction`, `residue`, and `out`
/// must all have the same length. Each pixel is
/// `clamp255(prediction[i] + residue[i])`.
///
/// # Panics
///
/// Panics if the three slices do not have equal length.
pub fn add_residue(prediction: &[u8], residue: &[i16], out: &mut [u8]) {
    assert_eq!(prediction.len(), residue.len(), "length mismatch");
    assert_eq!(prediction.len(), out.len(), "length mismatch");
    for i in 0..prediction.len() {
        out[i] = clamp255(prediction[i] as i32 + residue[i] as i32);
    }
}

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

    #[test]
    fn qlookup_tables_have_full_range() {
        assert_eq!(DC_QLOOKUP.len(), QINDEX_RANGE);
        assert_eq!(AC_QLOOKUP.len(), QINDEX_RANGE);
    }

    #[test]
    fn qlookup_spot_checks_match_spec() {
        // RFC 6386 §14.1 page 77, first and last entries of each table.
        assert_eq!(DC_QLOOKUP[0], 4);
        assert_eq!(DC_QLOOKUP[127], 157);
        assert_eq!(AC_QLOOKUP[0], 4);
        assert_eq!(AC_QLOOKUP[127], 284);
        // The tables diverge: dc[7] = 10 (repeated 10), ac[7] = 11.
        assert_eq!(DC_QLOOKUP[6], 10);
        assert_eq!(DC_QLOOKUP[7], 10);
        assert_eq!(AC_QLOOKUP[6], 10);
        assert_eq!(AC_QLOOKUP[7], 11);
        // A mid-table sample where ac transitions from a +1 run into a
        // +2 run (page 77: ...55, then 56, 57, 58, 60, 62, 64...):
        // index 52 → 56, 53 → 57, 54 → 58, 55 → 60 (the first +2 step).
        assert_eq!(AC_QLOOKUP[52], 56);
        assert_eq!(AC_QLOOKUP[54], 58);
        assert_eq!(AC_QLOOKUP[55], 60);
    }

    #[test]
    fn qindex_clamp_saturates_both_ends() {
        assert_eq!(clamp_qindex(-5), 0);
        assert_eq!(clamp_qindex(0), 0);
        assert_eq!(clamp_qindex(63), 63);
        assert_eq!(clamp_qindex(127), 127);
        assert_eq!(clamp_qindex(200), 127);
    }

    #[test]
    fn y1_factors_use_dc_lookup_for_dc_ac_lookup_for_ac() {
        // yac_qi = 20, ydc_delta = +3 → dc index 23, ac index 20.
        let f = Y1DequantFactors::from_indices(20, 3);
        assert_eq!(f.dc, DC_QLOOKUP[23]);
        assert_eq!(f.ac, AC_QLOOKUP[20]);
        // A negative delta that underflows past 0 is clamped.
        let f2 = Y1DequantFactors::from_indices(2, -10);
        assert_eq!(f2.dc, DC_QLOOKUP[0]);
        assert_eq!(f2.ac, AC_QLOOKUP[2]);
        // A delta that overflows past 127 is clamped.
        let f3 = Y1DequantFactors::from_indices(120, 50);
        assert_eq!(f3.dc, DC_QLOOKUP[127]);
        assert_eq!(f3.ac, AC_QLOOKUP[120]);
    }

    #[test]
    fn dequant_block_scales_dc_and_ac_separately() {
        let mut c = [0i16; 16];
        c[0] = 3; // DC
        c[1] = 2; // AC
        c[15] = -4; // AC
        dequant_block(&mut c, 7, 5);
        assert_eq!(c[0], 21); // 3 * 7
        assert_eq!(c[1], 10); // 2 * 5
        assert_eq!(c[15], -20); // -4 * 5
    }

    #[test]
    fn clamp255_matches_spec_definition() {
        assert_eq!(clamp255(-1), 0);
        assert_eq!(clamp255(-1000), 0);
        assert_eq!(clamp255(0), 0);
        assert_eq!(clamp255(128), 128);
        assert_eq!(clamp255(255), 255);
        assert_eq!(clamp255(256), 255);
        assert_eq!(clamp255(99999), 255);
    }

    #[test]
    fn wht_dc_only_matches_general_path() {
        // A block with only input[0] non-zero must give the same result
        // through the general path and the fast path.
        for dc in [0i16, 1, 3, 7, 8, -8, 100, -100, 1000] {
            let mut input = [0i16; 16];
            input[0] = dc;
            let mut general = [0i16; 16];
            inverse_wht_4x4(&input, &mut general);
            let mut fast = [0i16; 16];
            inverse_wht_4x4_dc_only(dc, &mut fast);
            assert_eq!(general, fast, "dc={dc}");
            // Every element should equal (dc + 3) >> 3.
            let expect = (((dc as i32) + 3) >> 3) as i16;
            assert!(general.iter().all(|&v| v == expect), "dc={dc}");
        }
    }

    #[test]
    fn wht_zero_input_is_zero_output() {
        let input = [0i16; 16];
        let mut output = [0i16; 16];
        inverse_wht_4x4(&input, &mut output);
        assert_eq!(output, [0i16; 16]);
    }

    #[test]
    fn wht_hand_derived_two_value_input() {
        // Hand-derive the spec C source for an input with two non-zero
        // entries: input[0] = 8 (position row0 col0), input[4] = 8
        // (position row1 col0). Walk both passes by hand.
        //
        // First pass, per column. Only col 0 has non-zero input:
        //   i0 = 8, i4 = 8, i8 = 0, i12 = 0
        //   a1 = 8+0 = 8;  b1 = 8+0 = 8;  c1 = 8-0 = 8;  d1 = 8-0 = 8
        //   tmp[0]  = a1+b1 = 16
        //   tmp[4]  = c1+d1 = 16
        //   tmp[8]  = a1-b1 = 0
        //   tmp[12] = d1-c1 = 0
        // Cols 1..3 are all zero.
        //
        // Second pass, per row (each row r has tmp[r*4]=col0 value,
        // rest zero):
        //   row0: r0=16,0,0,0 → a1=16+0=16, b1=0+0=0, c1=0-0=0,
        //         d1=16-0=16; a2=16, b2=16, c2=16, d2=16;
        //         out = (16+3)>>3 = 2 for all four → [2,2,2,2]
        //   row1: r0=16 → same as row0 → [2,2,2,2]
        //   row2: r0=0  → all zero → [0,0,0,0]
        //   row3: r0=0  → all zero → [0,0,0,0]
        let mut input = [0i16; 16];
        input[0] = 8;
        input[4] = 8;
        let mut output = [0i16; 16];
        inverse_wht_4x4(&input, &mut output);
        let expect = [
            2, 2, 2, 2, //
            2, 2, 2, 2, //
            0, 0, 0, 0, //
            0, 0, 0, 0, //
        ];
        assert_eq!(output, expect);
    }

    #[test]
    fn dct_zero_input_is_zero_output() {
        let input = [0i16; 16];
        let mut output = [0i16; 16];
        inverse_dct_4x4(&input, &mut output);
        assert_eq!(output, [0i16; 16]);
    }

    #[test]
    fn dct_dc_only_is_uniform_block() {
        // With only the DC coefficient non-zero, the inverse DCT yields
        // a (nearly) flat block. Hand-derive for input[0] = 8:
        // First pass col 0: i0=8, i4=0, i8=0, i12=0.
        //   a1 = 8+0 = 8; b1 = 8-0 = 8;
        //   c1 = (0*S>>16) - (0 + (0*C>>16)) = 0;
        //   d1 = (0 + (0*C>>16)) + (0*S>>16) = 0;
        //   tmp[0] = a1+d1 = 8; tmp[12] = a1-d1 = 8;
        //   tmp[4] = b1+c1 = 8; tmp[8]  = b1-c1 = 8.
        // Cols 1..3 zero → every tmp[*] in col 0 = 8, rest 0.
        // Second pass each row r: r0 = 8 (col0), r1=r2=r3=0.
        //   a1 = 8+0 = 8; b1 = 8-0 = 8; c1 = 0; d1 = 0;
        //   out[0] = (8+0+4)>>3 = 1; out[3] = (8-0+4)>>3 = 1;
        //   out[1] = (8+0+4)>>3 = 1; out[2] = (8-0+4)>>3 = 1.
        // So the whole 4x4 is 1.
        let mut input = [0i16; 16];
        input[0] = 8;
        let mut output = [0i16; 16];
        inverse_dct_4x4(&input, &mut output);
        assert_eq!(output, [1i16; 16]);
    }

    #[test]
    fn dct_dc_rounding_with_larger_value() {
        // input[0] = 64. First pass: each col-0 tmp = 64. Second pass:
        // out = (64 + 4) >> 3 = 8 for every pixel.
        let mut input = [0i16; 16];
        input[0] = 64;
        let mut output = [0i16; 16];
        inverse_dct_4x4(&input, &mut output);
        assert_eq!(output, [8i16; 16]);
    }

    /// Regression (round 405): the §14.4 inverse DCT must not panic on
    /// hostile post-dequant coefficients. §14.1 dequant stores
    /// `(coeff * factor) as i16`, whose truncation can land anywhere in
    /// the `i16` range; a full-range block drives the second-pass
    /// `r * SINPI8_SQRT2` product past `i32::MAX`. The reference-C listing
    /// wraps in its 32-bit `int` domain and the bit-exactness contract is
    /// defined against that wrapped result, so the scalar path must wrap
    /// (matching release mode and the `simd` lane path) rather than trip a
    /// debug-assertion overflow panic. Before the fix this panicked with
    /// "attempt to multiply with overflow"; the decode-side reachability
    /// (`frame::decode_keyframe` → `inverse_dct_4x4_scalar`) was captured
    /// by the `panic_free_keyframe_reconstruct` fuzz target on the scalar
    /// build.
    #[test]
    fn dct_extreme_coefficients_wrap_without_panic() {
        // Independent wrapping re-derivation of the §14.4 two-pass listing
        // (the 32-bit-`int` reference semantics), used as the oracle.
        fn wrapping_reference(input: &[i16; 16]) -> [i16; 16] {
            let mut tmp = [0i32; 16];
            for col in 0..4 {
                let i0 = input[col] as i32;
                let i4 = input[4 + col] as i32;
                let i8 = input[8 + col] as i32;
                let i12 = input[12 + col] as i32;
                let a1 = i0.wrapping_add(i8);
                let b1 = i0.wrapping_sub(i8);
                let t1 = i4.wrapping_mul(SINPI8_SQRT2) >> 16;
                let t2 = i12.wrapping_add(i12.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
                let c1 = t1.wrapping_sub(t2);
                let t1b = i4.wrapping_add(i4.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
                let t2b = i12.wrapping_mul(SINPI8_SQRT2) >> 16;
                let d1 = t1b.wrapping_add(t2b);
                tmp[col] = a1.wrapping_add(d1);
                tmp[12 + col] = a1.wrapping_sub(d1);
                tmp[4 + col] = b1.wrapping_add(c1);
                tmp[8 + col] = b1.wrapping_sub(c1);
            }
            let mut out = [0i16; 16];
            for row in 0..4 {
                let base = row * 4;
                let r0 = tmp[base];
                let r1 = tmp[base + 1];
                let r2 = tmp[base + 2];
                let r3 = tmp[base + 3];
                let a1 = r0.wrapping_add(r2);
                let b1 = r0.wrapping_sub(r2);
                let t1 = r1.wrapping_mul(SINPI8_SQRT2) >> 16;
                let t2 = r3.wrapping_add(r3.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
                let c1 = t1.wrapping_sub(t2);
                let t1b = r1.wrapping_add(r1.wrapping_mul(COSPI8_SQRT2_MINUS1) >> 16);
                let t2b = r3.wrapping_mul(SINPI8_SQRT2) >> 16;
                let d1 = t1b.wrapping_add(t2b);
                out[base] = (a1.wrapping_add(d1).wrapping_add(4) >> 3) as i16;
                out[base + 3] = (a1.wrapping_sub(d1).wrapping_add(4) >> 3) as i16;
                out[base + 1] = (b1.wrapping_add(c1).wrapping_add(4) >> 3) as i16;
                out[base + 2] = (b1.wrapping_sub(c1).wrapping_add(4) >> 3) as i16;
            }
            out
        }

        let hostile = [
            [i16::MAX; 16],
            [i16::MIN; 16],
            [
                i16::MAX,
                i16::MIN,
                i16::MAX,
                i16::MIN,
                i16::MIN,
                i16::MAX,
                i16::MIN,
                i16::MAX,
                i16::MAX,
                i16::MIN,
                i16::MAX,
                i16::MIN,
                i16::MIN,
                i16::MAX,
                i16::MIN,
                i16::MAX,
            ],
            [
                -32768, 32767, -1, 12345, -30000, 32767, -32768, 9999, 32767, -32768, 32767,
                -32768, 1, -2, 3, -4,
            ],
        ];
        for input in &hostile {
            let mut output = [0i16; 16];
            // Must not panic under debug assertions.
            inverse_dct_4x4(input, &mut output);
            assert_eq!(
                output,
                wrapping_reference(input),
                "IDCT must match the 32-bit wrapping reference on {input:?}"
            );
        }
    }

    #[test]
    fn dct_first_ac_coefficient_uses_cosine_constants() {
        // Place a single non-zero in input[1] (row 0, col 1) and verify
        // the output exactly matches a from-scratch evaluation of the
        // two-pass spec arithmetic, proving the SINPI8_SQRT2 /
        // COSPI8_SQRT2_MINUS1 constants are wired into the right lanes.
        let mut input = [0i16; 16];
        input[1] = 100;
        let mut output = [0i16; 16];
        inverse_dct_4x4(&input, &mut output);
        // Independent re-derivation of the spec C source.
        let mut tmp = [0i32; 16];
        for col in 0..4 {
            let i0 = input[col] as i32;
            let i4 = input[4 + col] as i32;
            let i8 = input[8 + col] as i32;
            let i12 = input[12 + col] as i32;
            let a1 = i0 + i8;
            let b1 = i0 - i8;
            let t1 = (i4 * SINPI8_SQRT2) >> 16;
            let t2 = i12 + ((i12 * COSPI8_SQRT2_MINUS1) >> 16);
            let c1 = t1 - t2;
            let t1b = i4 + ((i4 * COSPI8_SQRT2_MINUS1) >> 16);
            let t2b = (i12 * SINPI8_SQRT2) >> 16;
            let d1 = t1b + t2b;
            tmp[col] = a1 + d1;
            tmp[12 + col] = a1 - d1;
            tmp[4 + col] = b1 + c1;
            tmp[8 + col] = b1 - c1;
        }
        let mut expect = [0i16; 16];
        for row in 0..4 {
            let base = row * 4;
            let r0 = tmp[base];
            let r1 = tmp[base + 1];
            let r2 = tmp[base + 2];
            let r3 = tmp[base + 3];
            let a1 = r0 + r2;
            let b1 = r0 - r2;
            let t1 = (r1 * SINPI8_SQRT2) >> 16;
            let t2 = r3 + ((r3 * COSPI8_SQRT2_MINUS1) >> 16);
            let c1 = t1 - t2;
            let t1b = r1 + ((r1 * COSPI8_SQRT2_MINUS1) >> 16);
            let t2b = (r3 * SINPI8_SQRT2) >> 16;
            let d1 = t1b + t2b;
            expect[base] = ((a1 + d1 + 4) >> 3) as i16;
            expect[base + 3] = ((a1 - d1 + 4) >> 3) as i16;
            expect[base + 1] = ((b1 + c1 + 4) >> 3) as i16;
            expect[base + 2] = ((b1 - c1 + 4) >> 3) as i16;
        }
        assert_eq!(output, expect);
        // And it must not be a flat block (an AC coefficient creates a
        // gradient), which proves the constants actually contributed.
        assert!(output.iter().any(|&v| v != output[0]));
    }

    #[test]
    fn dct_is_separable_two_pass_roundtrip_shape() {
        // A small full-block sanity check: known input, output computed
        // by the same spec arithmetic re-derived inline. This guards
        // against an accidental transpose of the row/column passes.
        let input = [
            16, 4, 0, -2, //
            8, 1, 0, 0, //
            -4, 0, 0, 0, //
            2, 0, 0, 0, //
        ];
        let mut output = [0i16; 16];
        inverse_dct_4x4(&input, &mut output);

        let mut tmp = [0i32; 16];
        for col in 0..4 {
            let i0 = input[col] as i32;
            let i4 = input[4 + col] as i32;
            let i8 = input[8 + col] as i32;
            let i12 = input[12 + col] as i32;
            let a1 = i0 + i8;
            let b1 = i0 - i8;
            let t1 = (i4 * SINPI8_SQRT2) >> 16;
            let t2 = i12 + ((i12 * COSPI8_SQRT2_MINUS1) >> 16);
            let c1 = t1 - t2;
            let t1b = i4 + ((i4 * COSPI8_SQRT2_MINUS1) >> 16);
            let t2b = (i12 * SINPI8_SQRT2) >> 16;
            let d1 = t1b + t2b;
            tmp[col] = a1 + d1;
            tmp[12 + col] = a1 - d1;
            tmp[4 + col] = b1 + c1;
            tmp[8 + col] = b1 - c1;
        }
        let mut expect = [0i16; 16];
        for row in 0..4 {
            let base = row * 4;
            let r0 = tmp[base];
            let r1 = tmp[base + 1];
            let r2 = tmp[base + 2];
            let r3 = tmp[base + 3];
            let a1 = r0 + r2;
            let b1 = r0 - r2;
            let t1 = (r1 * SINPI8_SQRT2) >> 16;
            let t2 = r3 + ((r3 * COSPI8_SQRT2_MINUS1) >> 16);
            let c1 = t1 - t2;
            let t1b = r1 + ((r1 * COSPI8_SQRT2_MINUS1) >> 16);
            let t2b = (r3 * SINPI8_SQRT2) >> 16;
            let d1 = t1b + t2b;
            expect[base] = ((a1 + d1 + 4) >> 3) as i16;
            expect[base + 3] = ((a1 - d1 + 4) >> 3) as i16;
            expect[base + 1] = ((b1 + c1 + 4) >> 3) as i16;
            expect[base + 2] = ((b1 - c1 + 4) >> 3) as i16;
        }
        assert_eq!(output, expect);
    }

    #[test]
    fn add_residue_4x4_saturates() {
        let pred = [200u8; 16];
        let mut residue = [0i16; 16];
        residue[0] = 100; // 200 + 100 = 300 → 255
        residue[1] = -250; // 200 - 250 = -50 → 0
        residue[2] = 30; // 200 + 30 = 230
        let mut out = [0u8; 16];
        add_residue_4x4(&pred, &residue, &mut out);
        assert_eq!(out[0], 255);
        assert_eq!(out[1], 0);
        assert_eq!(out[2], 230);
        assert_eq!(out[3], 200); // unchanged
    }

    #[test]
    fn fused_idct_add_into_matches_unfused_sequence() {
        // The round-286 fused helper `inverse_dct_4x4_add_into` must be
        // byte-identical to the prior four-stage sequence
        //   inverse_dct_4x4 → extract 4×4 → add_residue_4x4 → insert 4×4
        // over the full §14.2 coefficient envelope, every strided
        // position, and every predictor byte. Drive a deterministic
        // mixed-frequency sweep that stresses both clamp saturations.
        let strides: [(usize, usize); 4] = [(16, 4), (8, 2), (16, 1), (8, 1)];
        let mut seed: u32 = 0x1357_9bdf;
        let mut next = || {
            // xorshift32, deterministic.
            seed ^= seed << 13;
            seed ^= seed >> 17;
            seed ^= seed << 5;
            seed
        };

        for &(stride, sub_max) in &strides {
            let plane_h = sub_max * 4;
            for sub_row in 0..sub_max {
                for sub_col in 0..(stride / 4) {
                    for _ in 0..8 {
                        // Random predictor plane.
                        let mut plane_fused = vec![0u8; stride * plane_h];
                        for p in plane_fused.iter_mut() {
                            *p = (next() & 0xff) as u8;
                        }
                        let mut plane_ref = plane_fused.clone();

                        // Random §14.2-envelope coefficient block: DC up to
                        // a few thousand, AC skirt, mixed signs.
                        let mut coeffs = [0i16; 16];
                        coeffs[0] = ((next() % 4000) as i32 - 2000) as i16;
                        for c in coeffs.iter_mut().skip(1) {
                            *c = ((next() % 600) as i32 - 300) as i16;
                        }

                        // Unfused reference sequence.
                        let mut residue = [0i16; 16];
                        inverse_dct_4x4(&coeffs, &mut residue);
                        let y0 = sub_row * 4;
                        let x0 = sub_col * 4;
                        let mut pred = [0u8; 16];
                        for r in 0..4 {
                            let src = (y0 + r) * stride + x0;
                            pred[r * 4..r * 4 + 4].copy_from_slice(&plane_ref[src..src + 4]);
                        }
                        let mut summed = [0u8; 16];
                        add_residue_4x4(&pred, &residue, &mut summed);
                        for r in 0..4 {
                            let dst = (y0 + r) * stride + x0;
                            plane_ref[dst..dst + 4].copy_from_slice(&summed[r * 4..r * 4 + 4]);
                        }

                        // Fused helper.
                        inverse_dct_4x4_add_into(
                            &coeffs,
                            &mut plane_fused,
                            stride,
                            sub_row,
                            sub_col,
                        );

                        assert_eq!(
                            plane_fused, plane_ref,
                            "fused != unfused for stride={stride} sub=({sub_row},{sub_col})"
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn dc_only_add_into_matches_general_path() {
        // The §14.4 DC-only fast path in `inverse_dct_4x4_add_into` must
        // be byte-identical to the full-transform unfused reference for
        // every DC magnitude (both clamp saturations) at every strided
        // sub-block position. The reference deliberately reconstructs the
        // residue through the general two-pass `inverse_dct_4x4` so it is
        // independent of the fast-path arithmetic being validated.
        let strides: [(usize, usize); 4] = [(16, 4), (8, 2), (16, 1), (8, 1)];
        // Sweep DCs that round to negative, zero, small, and large
        // residues, plus the saturating extremes of the §14.2 envelope.
        let dcs: [i16; 11] = [-2114, -1000, -64, -4, -3, 0, 3, 4, 64, 1000, 2114];
        let mut seed: u32 = 0x2468_ace0;
        let mut next = || {
            seed ^= seed << 13;
            seed ^= seed >> 17;
            seed ^= seed << 5;
            seed
        };

        for &(stride, sub_max) in &strides {
            let plane_h = sub_max * 4;
            for sub_row in 0..sub_max {
                for sub_col in 0..(stride / 4) {
                    for &dc in &dcs {
                        let mut plane_fast = vec![0u8; stride * plane_h];
                        for p in plane_fast.iter_mut() {
                            *p = (next() & 0xff) as u8;
                        }
                        let mut plane_ref = plane_fast.clone();

                        let mut coeffs = [0i16; 16];
                        coeffs[0] = dc;

                        // General-path reference: full IDCT → add-clamp.
                        let mut residue = [0i16; 16];
                        inverse_dct_4x4(&coeffs, &mut residue);
                        let y0 = sub_row * 4;
                        let x0 = sub_col * 4;
                        let mut pred = [0u8; 16];
                        for r in 0..4 {
                            let src = (y0 + r) * stride + x0;
                            pred[r * 4..r * 4 + 4].copy_from_slice(&plane_ref[src..src + 4]);
                        }
                        let mut summed = [0u8; 16];
                        add_residue_4x4(&pred, &residue, &mut summed);
                        for r in 0..4 {
                            let dst = (y0 + r) * stride + x0;
                            plane_ref[dst..dst + 4].copy_from_slice(&summed[r * 4..r * 4 + 4]);
                        }

                        // Fast path under test.
                        inverse_dct_4x4_add_into(
                            &coeffs,
                            &mut plane_fast,
                            stride,
                            sub_row,
                            sub_col,
                        );

                        assert_eq!(
                            plane_fast, plane_ref,
                            "DC-only fast path != general path for dc={dc} \
                             stride={stride} sub=({sub_row},{sub_col})"
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn add_residue_slice_matches_4x4() {
        let pred = [128u8; 64];
        let mut residue = [0i16; 64];
        residue[10] = 50;
        residue[20] = -200;
        residue[63] = 127;
        let mut out = [0u8; 64];
        add_residue(&pred, &residue, &mut out);
        assert_eq!(out[10], 178);
        assert_eq!(out[20], 0);
        assert_eq!(out[63], 255);
        assert_eq!(out[0], 128);
    }

    #[test]
    fn add_residue_zero_residue_is_identity() {
        let pred = [37u8; 16];
        let residue = [0i16; 16];
        let mut out = [0u8; 16];
        add_residue_4x4(&pred, &residue, &mut out);
        assert_eq!(out, pred);
    }

    // -------------------------------------------------------------
    // SIMD vs scalar byte-exact equivalence tests.
    //
    // These run on every build configuration. On stable / `!simd` they
    // exercise only the scalar path twice; that's harmless and keeps
    // CI green. On nightly + `simd` they're the primary safety net:
    // the public `inverse_wht_4x4` / `inverse_dct_4x4` dispatch to
    // SIMD, and the `_scalar` variants are still reachable as
    // module-private fallbacks, so the assertions compare the two.
    // -------------------------------------------------------------

    /// 21 stress inputs covering DC-only, single AC lanes (every
    /// position), mixed gradients, near-i16 extremes, and signed-zero
    /// edge cases.
    fn stress_inputs() -> Vec<[i16; 16]> {
        let mut v: Vec<[i16; 16]> = Vec::new();
        // All-zero.
        v.push([0i16; 16]);
        // DC-only.
        for &dc in &[1i16, 7, 8, -8, 64, -64, 1000, -1000, 4096, -4096] {
            let mut a = [0i16; 16];
            a[0] = dc;
            v.push(a);
        }
        // Single-AC at each position.
        for pos in 1..16 {
            let mut a = [0i16; 16];
            a[pos] = 100;
            v.push(a);
        }
        // A mixed low-frequency pattern (the bench's SAMPLE_INPUT).
        v.push([
            320, -64, 16, -4, //
            -48, 32, -16, 8, //
            24, -12, 8, -4, //
            -8, 4, -2, 1,
        ]);
        // A high-AC near-extreme pattern (kept inside the i32-safe
        // headroom — §14.4 doesn't promise wraparound matching beyond
        // the i16 quantised range).
        v.push([
            512, 256, 128, 64, //
            256, 128, 64, 32, //
            128, 64, 32, 16, //
            64, 32, 16, 8,
        ]);
        v.push([
            -512, 256, -128, 64, //
            256, -128, 64, -32, //
            -128, 64, -32, 16, //
            64, -32, 16, -8,
        ]);
        v
    }

    #[test]
    fn dct_simd_matches_scalar_on_stress_inputs() {
        for (idx, input) in stress_inputs().iter().enumerate() {
            let mut via_public = [0i16; 16];
            inverse_dct_4x4(input, &mut via_public);
            let mut via_scalar = [0i16; 16];
            inverse_dct_4x4_scalar(input, &mut via_scalar);
            assert_eq!(
                via_public, via_scalar,
                "input #{idx} ({input:?}): public dispatch ≠ scalar listing"
            );
        }
    }

    #[test]
    fn wht_simd_matches_scalar_on_stress_inputs() {
        for (idx, input) in stress_inputs().iter().enumerate() {
            let mut via_public = [0i16; 16];
            inverse_wht_4x4(input, &mut via_public);
            let mut via_scalar = [0i16; 16];
            inverse_wht_4x4_scalar(input, &mut via_scalar);
            assert_eq!(
                via_public, via_scalar,
                "input #{idx} ({input:?}): public dispatch ≠ scalar listing"
            );
        }
    }
}