oxideav-vp8 0.2.2

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
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
//! VP8 DCT-coefficient token decoding per RFC 6386 §13.
//!
//! This module walks the `coeff_tree` of §13.2 against the
//! `coeff_probs[4][8][3][11]` probability table to recover the sixteen
//! quantised DCT coefficients of a single 4×4 sub-block. Encoded
//! coefficients are written into a caller-supplied `[i16; 16]` in the
//! spec's "implicit position" order — i.e. positions `firstCoeff ..
//! firstCoeff + n` of the (already-zig-zagged) sub-block array. After
//! a `dct_eob` token the remaining positions are left at zero.
//!
//! **What is in this module:**
//!
//! * The §13.2 token-tree walker (`treed_read_coef`), with the §13.2
//!   "skip the dct_eob branch when the previous coefficient was a
//!   DCT_0" optimisation. Implemented as a one-step descent past the
//!   first internal node rather than a separate tree.
//! * The §13.2 `DCTextra` extra-bits decoder over the six fixed
//!   probability tables `Pcat1..Pcat6`.
//! * The §13.2 sign bit (read at probability 128).
//! * The §13.3 `coeff_bands[16]` position-to-band mapping that selects
//!   the second probability-table index.
//! * The §13.3 "previous-token class" `ctx3` rollover that selects the
//!   third probability-table index (0 if previous was zero, 1 if its
//!   absolute value was 1, 2 if its absolute value was > 1).
//! * The §13.3 plane-type discriminator: 0 = Y after Y2, 1 = Y2,
//!   2 = U or V, 3 = Y in the absence of Y2.
//! * The §13.5 default token-probability table (transcribed verbatim).
//! * A `merge_default_token_probs` helper that overlays a
//!   `TokenProbUpdates` (from `coded_header::Vp8CodedHeader`) onto the
//!   default table, producing a complete `coeff_probs[4][8][3][11]`
//!   ready for `decode_block` calls.
//! * The §13.3 per-macroblock token walk (`decode_mb_coeffs`): it
//!   aggregates the 24/25 blocks of one macroblock — the §14.2 Y2 (WHT)
//!   block first when present, then the sixteen Y DCT blocks, then the
//!   four U and four V chroma blocks — threading the above / left
//!   non-zero predictor context through the §20.16
//!   `left_context_index` / `above_context_index` slot tables, honouring
//!   the §13.1 `mb_skip_coeff` short-circuit (with the §20.16
//!   `reset_mb_context` Y2-preserving rule), selecting the
//!   `YAfterY2` / `YNoY2` plane per `has_y2`, and reordering each block
//!   into raster order via the §20.16 `zigzag[16]` table. It produces a
//!   [`crate::frame::MbCoeffs`].
//!
//! **What is NOT in this module yet:**
//!
//! * Dequantisation — multiplying the recovered `i16` coefficients by
//!   the §14.1 `dc_qlookup` / `ac_qlookup` table values. `decode_mb_coeffs`
//!   emits the **raw quantized** token coefficients (in raster order);
//!   the §14.1 Y2 / chroma dequant scaling remains a documented spec gap
//!   (§14.1 page 77 defers it to `dixie.c` §20.4).
//! * The inverse WHT / IDCT (§14.2 / §14.3).
//!
//! # Spec-pseudocode caveat
//!
//! RFC 6386 §13.3 page 67's pseudocode ends each iteration with the
//! literal statement `prevCoeffWasZero = true;` — i.e. *unconditionally
//! true*. That is plainly a transcription error: the field is named
//! `prevCoeffWasZero` and is used at the next iteration's tree entry
//! to decide whether to skip the `dct_eob` branch (which is illegal
//! after a zero). Setting it unconditionally true would mean **every
//! coefficient after the first** allows EOB-after-non-zero, which
//! contradicts §13.2's "if the preceding coefficient is a DCT_0,
//! decoding will skip the first branch" statement. We treat the
//! correct semantics as `prevCoeffWasZero = (token == DCT_0)`. The
//! `prev_token_skips_eob_branch` test exercises both directions to
//! prove this branch decision matters.

use crate::bool_decoder::{BoolDecoder, BoolDecoderError};
use crate::coded_header::TokenProbUpdates;

/// Twelve DCT-token alphabet symbols per RFC 6386 §13.2. Order is
/// fixed to match the spec; the integer discriminants are also the
/// `dct_token` enum values.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DctToken {
    /// Coefficient value 0.
    Dct0 = 0,
    /// Coefficient value 1.
    Dct1 = 1,
    /// Coefficient value 2.
    Dct2 = 2,
    /// Coefficient value 3.
    Dct3 = 3,
    /// Coefficient value 4.
    Dct4 = 4,
    /// Range `5..=6` (size 2; 1 extra bit).
    Cat1 = 5,
    /// Range `7..=10` (size 4; 2 extra bits).
    Cat2 = 6,
    /// Range `11..=18` (size 8; 3 extra bits).
    Cat3 = 7,
    /// Range `19..=34` (size 16; 4 extra bits).
    Cat4 = 8,
    /// Range `35..=66` (size 32; 5 extra bits).
    Cat5 = 9,
    /// Range `67..=2048` (size 1982; 11 extra bits).
    Cat6 = 10,
    /// End of block — the remaining coefficients of this sub-block
    /// are zero.
    Eob = 11,
}

/// The §13.3 "plane type" — the outermost index into
/// `coeff_probs[4][8][3][11]`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlockType {
    /// Y luma block when the macroblock also has a Y2 block — i.e.
    /// the DC slot is part of Y2 and Y DCT starts at coefficient 1.
    YAfterY2 = 0,
    /// The Y2 block itself (one per macroblock, holds the 16 Y DCs).
    Y2 = 1,
    /// U or V chroma block.
    UV = 2,
    /// Y luma block in a macroblock without Y2 (B_PRED / SPLITMV) —
    /// Y DCT starts at coefficient 0.
    YNoY2 = 3,
}

impl BlockType {
    /// The §13.3 `firstCoeff` index — the first coefficient position
    /// the token loop visits. `1` for [`BlockType::YAfterY2`], `0` for
    /// every other plane.
    pub const fn first_coeff(self) -> usize {
        match self {
            BlockType::YAfterY2 => 1,
            BlockType::Y2 | BlockType::UV | BlockType::YNoY2 => 0,
        }
    }

    /// The numeric `coeff_probs` outermost index per §13.3.
    pub const fn plane_index(self) -> usize {
        self as usize
    }
}

/// Out-of-band conditions surfaced by [`decode_block`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DctTokenError {
    /// Underlying bool-decoder ran out of bytes mid-token.
    BoolDecoder(BoolDecoderError),
    /// The token tree walked into a leaf index outside `0..=11`. The
    /// tree-table transcription is constant data, so this only fires
    /// if `decode_block` is given a tree-table table corrupted by a
    /// caller — kept as an error rather than `panic!` so all
    /// failure modes flow through `Result`.
    InvalidTokenIndex,
}

impl From<BoolDecoderError> for DctTokenError {
    fn from(e: BoolDecoderError) -> Self {
        DctTokenError::BoolDecoder(e)
    }
}

impl core::fmt::Display for DctTokenError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            DctTokenError::BoolDecoder(e) => write!(f, "vp8 dct token: bool decoder: {e}"),
            DctTokenError::InvalidTokenIndex => {
                write!(f, "vp8 dct token: token tree leaf index out of range")
            }
        }
    }
}

impl std::error::Error for DctTokenError {}

/// §13.2 `coeff_tree` — the eleven-internal-node, twelve-leaf token
/// tree. Entry `2*i` is the left child of internal node `i`; entry
/// `2*i + 1` is the right child. Non-negative entries point to the
/// next internal node; non-positive entries `-t` denote the leaf
/// `DctToken::from(t as u8)`.
///
/// Encoded verbatim from the §13.2 listing. The leading `-Eob` entry
/// is at index 0; when the previous coefficient was zero the walker
/// starts at index 2 (skipping the EOB branch) — see [`decode_block`].
const COEFF_TREE: [i8; 22] = [
    -(DctToken::Eob as i8),
    2, // eob = "0"
    -(DctToken::Dct0 as i8),
    4, // 0   = "10"
    -(DctToken::Dct1 as i8),
    6, // 1   = "110"
    8,
    12,
    -(DctToken::Dct2 as i8),
    10, // 2   = "11100"
    -(DctToken::Dct3 as i8),
    -(DctToken::Dct4 as i8),
    14,
    16,
    -(DctToken::Cat1 as i8),
    -(DctToken::Cat2 as i8),
    18,
    20,
    -(DctToken::Cat3 as i8),
    -(DctToken::Cat4 as i8),
    -(DctToken::Cat5 as i8),
    -(DctToken::Cat6 as i8),
];

/// §13.2 `Pcat1` extra-bits probability list (terminator removed).
const PCAT1: &[u8] = &[159];
/// §13.2 `Pcat2`.
const PCAT2: &[u8] = &[165, 145];
/// §13.2 `Pcat3`.
const PCAT3: &[u8] = &[173, 148, 140];
/// §13.2 `Pcat4`.
const PCAT4: &[u8] = &[176, 155, 140, 135];
/// §13.2 `Pcat5`.
const PCAT5: &[u8] = &[180, 157, 141, 134, 130];
/// §13.2 `Pcat6` — eleven probabilities for the wide cat6 range.
const PCAT6: &[u8] = &[254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129];

/// §13.2 base values for cat1..cat6 (`categoryBase[6]`).
const CAT_BASE: [u16; 6] = [5, 7, 11, 19, 35, 67];

/// §13.3 `coeff_bands[16]` — position-to-band lookup for the second
/// probability-table dimension.
pub const COEFF_BANDS: [usize; 16] = [0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7];

/// Complete `coeff_probs[plane][band][prev_ctx][position]` table.
/// The four outer dimensions match RFC 6386 §13.3's `Prob
/// coeff_probs[4][8][3][num_dct_tokens - 1]` declaration.
pub type CoeffProbs = [[[[u8; 11]; 3]; 8]; 4];

/// §13.5 default token-probability table (`default_coeff_probs[4][8][3][11]`),
/// transcribed verbatim from RFC 6386.
pub const DEFAULT_COEFF_PROBS: CoeffProbs = [
    // plane 0 — Y after Y2 (DCT starts at coefficient 1)
    [
        [
            [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
            [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
            [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
        ],
        [
            [253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128],
            [189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128],
            [106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128],
        ],
        [
            [1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128],
            [181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128],
            [78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128],
        ],
        [
            [1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128],
            [184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128],
            [77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128],
        ],
        [
            [1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128],
            [170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128],
            [37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128],
        ],
        [
            [1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128],
            [207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128],
            [102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128],
        ],
        [
            [1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128],
            [177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128],
            [80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128],
        ],
        [
            [1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
            [246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
            [255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
        ],
    ],
    // plane 1 — Y2
    [
        [
            [198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62],
            [131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1],
            [68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128],
        ],
        [
            [1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128],
            [184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128],
            [81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128],
        ],
        [
            [1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128],
            [99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128],
            [23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128],
        ],
        [
            [1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128],
            [109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128],
            [44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128],
        ],
        [
            [1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128],
            [94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128],
            [22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128],
        ],
        [
            [1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128],
            [124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128],
            [35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128],
        ],
        [
            [1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128],
            [121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128],
            [45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128],
        ],
        [
            [1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128],
            [203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128],
            [137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128],
        ],
    ],
    // plane 2 — U or V
    [
        [
            [253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128],
            [175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128],
            [73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128],
        ],
        [
            [1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128],
            [239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128],
            [155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128],
        ],
        [
            [1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128],
            [201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128],
            [69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128],
        ],
        [
            [1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128],
            [223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128],
            [141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128],
        ],
        [
            [1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128],
            [190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128],
            [149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
        ],
        [
            [1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128],
            [247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128],
            [240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128],
        ],
        [
            [1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128],
            [213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128],
            [55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128],
        ],
        [
            [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
            [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
            [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
        ],
    ],
    // plane 3 — Y in the absence of Y2 (DCT starts at coefficient 0)
    [
        [
            [202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255],
            [126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128],
            [61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128],
        ],
        [
            [1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128],
            [166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128],
            [39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128],
        ],
        [
            [1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128],
            [124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128],
            [24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128],
        ],
        [
            [1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128],
            [149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128],
            [28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128],
        ],
        [
            [1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128],
            [123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128],
            [20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128],
        ],
        [
            [1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128],
            [168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128],
            [47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128],
        ],
        [
            [1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128],
            [141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128],
            [42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128],
        ],
        [
            [1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
            [244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
            [238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
        ],
    ],
];

/// Overlay an in-frame `token_prob_update()` block onto the §13.5
/// default token-probability table. The output is the resolved
/// `coeff_probs[4][8][3][11]` used by the per-frame DCT decode loop.
///
/// Each entry of `updates` that is `Some(p)` replaces the
/// corresponding default position; `None` leaves the default in
/// place. The on-the-wire ordering of `updates` matches RFC 6386
/// §19.2's `token_prob_update()` four-nested-loop sweep, so the
/// `[plane][band][prev_ctx][pos]` indexing on the type alias is
/// already aligned.
pub fn merge_default_token_probs(updates: &TokenProbUpdates) -> CoeffProbs {
    let mut out = DEFAULT_COEFF_PROBS;
    for (plane_idx, plane) in updates.iter().enumerate() {
        for (band_idx, band) in plane.iter().enumerate() {
            for (prev_idx, ctx) in band.iter().enumerate() {
                for (pos_idx, slot) in ctx.iter().enumerate() {
                    if let Some(p) = *slot {
                        out[plane_idx][band_idx][prev_idx][pos_idx] = p;
                    }
                }
            }
        }
    }
    out
}

/// Walk the §13.2 coefficient tree starting at `start_index` (0 for
/// "may decode dct_eob", 2 for "skip dct_eob — previous coefficient
/// was zero"). The probability for each internal node `i` is
/// `probs[i >> 1]`, matching RFC 6386 §8.1 `treed_read`.
fn treed_read_coef(
    dec: &mut BoolDecoder<'_>,
    probs: &[u8; 11],
    start_index: i8,
) -> Result<DctToken, DctTokenError> {
    let mut i: i8 = start_index;
    loop {
        let prob = probs[(i as usize) >> 1];
        let bit = dec.read_bool(prob)? as usize;
        let next = COEFF_TREE[i as usize + bit];
        if next <= 0 {
            let leaf = -next as u8;
            return Ok(match leaf {
                0 => DctToken::Dct0,
                1 => DctToken::Dct1,
                2 => DctToken::Dct2,
                3 => DctToken::Dct3,
                4 => DctToken::Dct4,
                5 => DctToken::Cat1,
                6 => DctToken::Cat2,
                7 => DctToken::Cat3,
                8 => DctToken::Cat4,
                9 => DctToken::Cat5,
                10 => DctToken::Cat6,
                11 => DctToken::Eob,
                _ => return Err(DctTokenError::InvalidTokenIndex),
            });
        }
        i = next;
    }
}

/// Decode the variable-length "extra bits" trailing a `Cat1..Cat6`
/// token (RFC 6386 §13.2 `DCTextra`). `cat_probs` is the cat's
/// fixed probability list (`Pcat1..Pcat6`, terminator omitted).
fn read_extra_bits(dec: &mut BoolDecoder<'_>, cat_probs: &[u8]) -> Result<u16, BoolDecoderError> {
    let mut v: u16 = 0;
    for &p in cat_probs {
        let bit = dec.read_bool(p)? as u16;
        v = (v << 1) | bit;
    }
    Ok(v)
}

/// Decode one DCT-token sub-block per RFC 6386 §13.3 and return the
/// number of non-zero coefficients written. The recovered values are
/// written into `coeffs[firstCoeff..16]` (positions in the
/// already-zig-zagged sub-block order); positions past the implicit
/// `dct_eob` are left untouched (caller is expected to pre-zero
/// `coeffs`).
///
/// * `block_type` selects the §13.3 plane index and the
///   `firstCoeff` value.
/// * `coeff_probs` is the resolved `coeff_probs[4][8][3][11]`
///   produced by [`merge_default_token_probs`].
/// * `above_has_nonzero` / `left_has_nonzero` are the §13.3
///   "neighbour block has at least one non-zero coefficient"
///   predictors for the *first* coefficient only. Off-frame
///   neighbours are passed as `false` per §13.3 page 64.
/// * Returns `Ok(non_zero_count)` — the number of non-zero
///   coefficients decoded. `coeffs` is updated in place. A return
///   value of 0 means the block was all-zero (either an immediate
///   EOB at `firstCoeff` or a sequence of `DCT_0` tokens followed
///   by EOB).
pub fn decode_block(
    dec: &mut BoolDecoder<'_>,
    block_type: BlockType,
    coeff_probs: &CoeffProbs,
    above_has_nonzero: bool,
    left_has_nonzero: bool,
    coeffs: &mut [i16; 16],
) -> Result<usize, DctTokenError> {
    // §13.3 page 65: the third context-index (`ctx3`) is seeded by
    // the count of non-zero neighbours for the very first coefficient
    // only. After the first coefficient the field rolls over to the
    // class of the last decoded coefficient (see end of loop).
    let mut ctx3: usize = (above_has_nonzero as usize) + (left_has_nonzero as usize);
    let plane = block_type.plane_index();
    let first_coeff = block_type.first_coeff();

    // §13.3 page 67 calls the "skip dct_eob branch" flag
    // `prevCoeffWasZero`. It is `false` for the first coefficient
    // because there is no previous coefficient at all (i.e. we DO
    // permit an immediate dct_eob). After the first iteration the
    // value is updated to `(token == DCT_0)` — see the trailing
    // assignment in the loop.
    let mut prev_was_zero = false;

    let mut non_zero_count = 0usize;

    let mut i = first_coeff;
    while i < 16 {
        let band = COEFF_BANDS[i];
        let probs = &coeff_probs[plane][band][ctx3];

        // §13.2: skip the eob branch if previous coefficient was a DCT_0.
        // The eob branch is the i=0/i=1 edge of the tree; entering at
        // index 2 (the DCT_0 branch) bypasses it.
        let start = if prev_was_zero { 2i8 } else { 0i8 };
        let token = treed_read_coef(dec, probs, start)?;

        if token == DctToken::Eob {
            break;
        }

        let abs_value: u16 = match token {
            DctToken::Dct0 => 0,
            DctToken::Dct1 => 1,
            DctToken::Dct2 => 2,
            DctToken::Dct3 => 3,
            DctToken::Dct4 => 4,
            DctToken::Cat1 => CAT_BASE[0] + read_extra_bits(dec, PCAT1)?,
            DctToken::Cat2 => CAT_BASE[1] + read_extra_bits(dec, PCAT2)?,
            DctToken::Cat3 => CAT_BASE[2] + read_extra_bits(dec, PCAT3)?,
            DctToken::Cat4 => CAT_BASE[3] + read_extra_bits(dec, PCAT4)?,
            DctToken::Cat5 => CAT_BASE[4] + read_extra_bits(dec, PCAT5)?,
            DctToken::Cat6 => CAT_BASE[5] + read_extra_bits(dec, PCAT6)?,
            DctToken::Eob => unreachable!("eob handled above"),
        };

        if abs_value != 0 {
            // Non-zero coefficients carry a sign bit at fixed
            // probability 128 (§13.2 page 62).
            let sign = dec.read_bool(128)?;
            // §13.2 cat6 max value is 67 + (2^11 - 1) = 2114, which
            // still fits in i16 with sign. Cast is safe.
            let signed = abs_value as i16;
            coeffs[i] = if sign { -signed } else { signed };
            non_zero_count += 1;
        } else {
            coeffs[i] = 0;
        }

        // §13.3 page 67: rollover of `ctx3` to the absolute-value
        // class of the coefficient we just decoded.
        ctx3 = if abs_value == 0 {
            0
        } else if abs_value == 1 {
            1
        } else {
            2
        };

        // Spec-typo correction (see module-level doc comment): set
        // `prev_was_zero` to whether the token was DCT_0, NOT the
        // literal `true` the spec listing types.
        prev_was_zero = token == DctToken::Dct0;

        i += 1;
    }

    Ok(non_zero_count)
}

// ===========================================================================
// §13.3 per-macroblock token walk
// ===========================================================================

/// §13 page 60 zig-zag scan order, transcribed from the §20.16 (tokens.c)
/// reference annex `zigzag[16]`. `ZIGZAG[c]` is the raster (natural-order)
/// position of the coefficient at scan position `c`. The per-block token
/// loop fills coefficients in scan order; this table places each into its
/// raster slot for the §14 raster-order inverse transforms.
pub const ZIGZAG: [usize; 16] = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15];

/// Number of per-plane non-zero predictor slots a macroblock maintains in
/// each direction (above / left): four Y columns/rows, two U, two V, and
/// one Y2 — the `token_entropy_ctx_t = int[4 + 2 + 2 + 1]` of the §20.16
/// reference annex.
pub const MB_ENTROPY_CTX_LEN: usize = 4 + 2 + 2 + 1;

/// The §20.16 `left_context_index[25]` — maps a macroblock's 25 residual
/// blocks (16 Y, then 4 U, 4 V, then the Y2 block as block 24) to a slot
/// in the nine-entry "left" non-zero predictor vector. Y subblocks share a
/// left slot per subblock *row* (slots 0..3); U and V each occupy two slots
/// (4/5 and 6/7); Y2 is slot 8.
const LEFT_CONTEXT_INDEX: [usize; 25] = [
    0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, // 16 Y
    4, 4, 5, 5, // 4 U
    6, 6, 7, 7, // 4 V
    8, // Y2
];

/// The §20.16 `above_context_index[25]` — the companion mapping for the
/// "above" predictor vector. Y subblocks share an above slot per subblock
/// *column* (slots 0..3); U/V/Y2 mirror [`LEFT_CONTEXT_INDEX`].
const ABOVE_CONTEXT_INDEX: [usize; 25] = [
    0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, // 16 Y
    4, 5, 4, 5, // 4 U
    6, 7, 6, 7, // 4 V
    8, // Y2
];

/// One macroblock's worth of non-zero coefficient predictors for a single
/// direction (above *or* left), per the §13.3 "aggregate coefficient
/// predictor" description: a single Y2 predictor, two each for U and V, and
/// four for Y. A decoder maintains one [`MbEntropyCtx`] per macroblock
/// column for the "above" row and a single rolling "left" instance.
///
/// Each entry is `true` if the most-recently-decoded block mapping to that
/// slot had at least one non-zero coefficient. Off-frame predictors are
/// `false` (the §13.3 "non-existent predictors ... taken to be empty"
/// rule); a freshly [`Default`]-constructed context satisfies that.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct MbEntropyCtx {
    /// The nine non-zero flags, indexed by [`LEFT_CONTEXT_INDEX`] /
    /// [`ABOVE_CONTEXT_INDEX`]: slots 0..3 = Y, 4..5 = U, 6..7 = V,
    /// 8 = Y2.
    pub nonzero: [bool; MB_ENTROPY_CTX_LEN],
}

impl MbEntropyCtx {
    /// Apply the §20.16 `reset_mb_context` rule for a skipped macroblock.
    ///
    /// The eight Y/U/V slots are always cleared (a skipped macroblock has
    /// no residue, so every same-plane neighbour predictor it contributes
    /// is empty). The Y2 slot (8) is cleared **only** when this macroblock
    /// would have carried a Y2 block (i.e. a non-`B_PRED` / non-`SPLITMV`
    /// mode); otherwise it is preserved so that the most-recent Y2-bearing
    /// macroblock in this column / row still drives the Y2 predictor, per
    /// the §13.3 "most recent macroblock ... that has a Y2 block" rule.
    fn reset_for_skip(&mut self, has_y2: bool) {
        for slot in self.nonzero.iter_mut().take(8) {
            *slot = false;
        }
        if has_y2 {
            self.nonzero[8] = false;
        }
    }
}

/// Out-of-band conditions surfaced by [`decode_mb_coeffs`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MbCoeffError {
    /// A per-block token decode failed (bool-decoder ran dry, or a
    /// corrupted tree table). Carries the residual block index
    /// (`0..=24`, dixie order: 0..15 Y, 16..19 U, 20..23 V, 24 Y2) and
    /// the underlying error.
    Block {
        /// The §20.16 residual block index that failed.
        index: usize,
        /// The per-block decode error.
        source: DctTokenError,
    },
}

impl core::fmt::Display for MbCoeffError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            MbCoeffError::Block { index, source } => {
                write!(f, "vp8 mb coeffs: residual block {index}: {source}")
            }
        }
    }
}

impl std::error::Error for MbCoeffError {}

/// Reorder a scan-order coefficient block into raster (natural) order via
/// the §20.16 zig-zag table. `scan[c]` (scan position `c`) is placed at
/// raster position `ZIGZAG[c]`.
#[inline]
fn scan_to_raster(scan: &[i16; 16]) -> [i16; 16] {
    let mut raster = [0i16; 16];
    for (c, &v) in scan.iter().enumerate() {
        raster[ZIGZAG[c]] = v;
    }
    raster
}

/// Decode one macroblock's worth of DCT/WHT coefficient tokens per RFC
/// 6386 §13.3, threading the above / left non-zero predictor context the
/// §13.3 third-dimension probability index requires.
///
/// This is the integration layer over [`decode_block`]: it walks all the
/// residual blocks of one macroblock — the §14.2 Y2 (WHT) block first when
/// present, then the sixteen Y 4×4 DCT blocks, then the four U and four V
/// chroma blocks, in the §13 "residual_data()" order — selecting the
/// correct [`BlockType`] plane and the correct above / left predictor slot
/// per the §20.16 `left_context_index` / `above_context_index` tables, and
/// updating those slots with each block's non-zero status for the
/// macroblocks below and to the right (§13.3 "after each block is decoded,
/// the two predictors referenced by the block are replaced").
///
/// # Inputs
///
/// * `dec` — the §13.2 boolean decoder positioned at this macroblock's
///   residual record in the (second) DCT partition.
/// * `has_y2` — whether this macroblock carries a Y2 block. `true` for the
///   four 16×16 luma modes (and inter modes other than `SPLITMV`); `false`
///   for `B_PRED` / `SPLITMV`, whose Y blocks instead carry their own DC.
///   The caller derives this from the decoded [`crate::macroblock::IntraYMode`]
///   (`!= B`).
/// * `mb_skip_coeff` — the §11.1 / §13.1 skip flag. When `true` no tokens
///   are read; the returned coefficients are all zero and the predictor
///   context is updated per the §20.16 `reset_mb_context` rule (clear all
///   Y/U/V slots; clear Y2 only when `has_y2`).
/// * `coeff_probs` — the resolved `coeff_probs[4][8][3][11]` from
///   [`merge_default_token_probs`].
/// * `above` / `left` — the macroblock's incoming non-zero predictor
///   contexts. Both are read for the *first* coefficient of each block and
///   updated in place as blocks are decoded. The caller maintains a row of
///   `above` contexts (one per macroblock column) and a single rolling
///   `left` context, clearing both at the appropriate frame / row edges.
///
/// # Output
///
/// The macroblock's coefficients as an [`crate::frame::MbCoeffs`], with
/// each 4×4 block placed in **raster (natural) order** (the layout the §14
/// inverse transforms consume). The coefficients are the **raw quantized
/// token values** — the §14.1 Y2 / chroma dequant scaling is a documented
/// spec gap (§14.1 page 77 defers it to `dixie.c` §20.4), so this function
/// does **not** multiply by any dequant factor. The caller must apply
/// §14.1 dequantization before the inverse transforms once that gap is
/// filled.
pub fn decode_mb_coeffs(
    dec: &mut BoolDecoder<'_>,
    has_y2: bool,
    mb_skip_coeff: bool,
    coeff_probs: &CoeffProbs,
    above: &mut MbEntropyCtx,
    left: &mut MbEntropyCtx,
) -> Result<crate::frame::MbCoeffs, MbCoeffError> {
    let mut out = crate::frame::MbCoeffs::default();

    // §13.1: a skip macroblock reads no tokens. Update the predictor
    // context per the §20.16 `reset_mb_context` rule and return zeros.
    if mb_skip_coeff {
        above.reset_for_skip(has_y2);
        left.reset_for_skip(has_y2);
        return Ok(out);
    }

    // Decode one residual block: select the predictor slots, run the
    // per-coefficient token loop, reorder to raster, and write back the
    // non-zero status into both predictor vectors. `block_index` is the
    // §20.16 residual block index (0..=24).
    let decode_one = |dec: &mut BoolDecoder<'_>,
                      block_index: usize,
                      block_type: BlockType,
                      above: &mut MbEntropyCtx,
                      left: &mut MbEntropyCtx|
     -> Result<[i16; 16], MbCoeffError> {
        let a_slot = ABOVE_CONTEXT_INDEX[block_index];
        let l_slot = LEFT_CONTEXT_INDEX[block_index];
        let mut scan = [0i16; 16];
        let nz = decode_block(
            dec,
            block_type,
            coeff_probs,
            above.nonzero[a_slot],
            left.nonzero[l_slot],
            &mut scan,
        )
        .map_err(|source| MbCoeffError::Block {
            index: block_index,
            source,
        })?;
        // §13.3: replace the two referenced predictors with this
        // block's (non-)empty state for blocks below and to the right.
        let has_coeffs = nz != 0;
        above.nonzero[a_slot] = has_coeffs;
        left.nonzero[l_slot] = has_coeffs;
        Ok(scan_to_raster(&scan))
    };

    // §13 / §14.2 residual order: Y2 (when present) → 16 Y → 4 U → 4 V.
    // The Y luma plane type is `YAfterY2` (DCT starts at coefficient 1)
    // when a Y2 block carries the DCs, else `YNoY2` (DCT starts at 0).
    if has_y2 {
        // Y2 is residual block 24 in the §20.16 index tables.
        out.y2 = decode_one(dec, 24, BlockType::Y2, above, left)?;
    }

    let y_plane = if has_y2 {
        BlockType::YAfterY2
    } else {
        BlockType::YNoY2
    };
    for (i, y_block) in out.y.iter_mut().enumerate() {
        *y_block = decode_one(dec, i, y_plane, above, left)?;
    }

    // U occupies residual blocks 16..=19, V occupies 20..=23.
    for (i, u_block) in out.u.iter_mut().enumerate() {
        *u_block = decode_one(dec, 16 + i, BlockType::UV, above, left)?;
    }
    for (i, v_block) in out.v.iter_mut().enumerate() {
        *v_block = decode_one(dec, 20 + i, BlockType::UV, above, left)?;
    }

    Ok(out)
}

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

    // Test-side bool encoder (mirrors the one inside
    // `bool_decoder::tests`). Lives only in tests; never exported.
    struct TestEncoder {
        out: Vec<u8>,
        range: u32,
        bottom: u32,
        bit_count: i32,
    }

    impl TestEncoder {
        fn new() -> Self {
            Self {
                out: Vec::new(),
                range: 255,
                bottom: 0,
                bit_count: 24,
            }
        }

        fn write_bool(&mut self, prob: u8, val: bool) {
            let split = 1 + (((self.range - 1) * prob as u32) >> 8);
            if val {
                self.bottom = self.bottom.wrapping_add(split);
                self.range -= split;
            } else {
                self.range = split;
            }
            while self.range < 128 {
                self.range <<= 1;
                if (self.bottom >> 31) & 1 == 1 {
                    let mut i = self.out.len();
                    while i > 0 {
                        i -= 1;
                        if self.out[i] == 255 {
                            self.out[i] = 0;
                        } else {
                            self.out[i] = self.out[i].wrapping_add(1);
                            break;
                        }
                    }
                }
                self.bottom <<= 1;
                self.bit_count -= 1;
                if self.bit_count == 0 {
                    let byte = (self.bottom >> 24) as u8;
                    self.out.push(byte);
                    self.bottom &= (1 << 24) - 1;
                    self.bit_count = 8;
                }
            }
        }

        fn finish(mut self) -> Vec<u8> {
            let c = self.bit_count;
            let v = self.bottom;
            if v & (1u32 << (32 - c)) != 0 {
                let mut i = self.out.len();
                while i > 0 {
                    i -= 1;
                    if self.out[i] == 255 {
                        self.out[i] = 0;
                    } else {
                        self.out[i] = self.out[i].wrapping_add(1);
                        break;
                    }
                }
            }
            let mut v = v;
            v <<= c & 7;
            let mut c_shift = c >> 3;
            while c_shift > 0 {
                v <<= 8;
                c_shift -= 1;
            }
            for _ in 0..4 {
                self.out.push((v >> 24) as u8);
                v <<= 8;
            }
            self.out
        }
    }

    /// Encode a single DctToken against the given probability vector by
    /// walking the same tree the decoder will walk. Returns the (start
    /// internal node, list of (prob_index, bit) pairs to write).
    ///
    /// `start_at_index` is `0` if we may emit eob, `2` if not (i.e.
    /// when the previous coefficient was a DCT_0).
    fn encode_token(token: DctToken, start_at_index: i8) -> Vec<(usize, bool)> {
        // We replay the tree-walk and at each step record (i>>1, bit)
        // that the decoder would have read. We do this by knowing the
        // tree statically.
        let target = token as i8;
        let mut out = Vec::new();
        // Helper: at internal node `i`, the two children are
        // COEFF_TREE[i] (left, bit=0) and COEFF_TREE[i+1] (right, bit=1).
        fn descend(i: i8, target: i8, path: &mut Vec<(usize, bool)>) -> bool {
            for &bit in &[false, true] {
                let child = COEFF_TREE[i as usize + bit as usize];
                if child <= 0 {
                    if -child == target {
                        path.push(((i as usize) >> 1, bit));
                        return true;
                    }
                } else {
                    path.push(((i as usize) >> 1, bit));
                    if descend(child, target, path) {
                        return true;
                    }
                    path.pop();
                }
            }
            false
        }
        assert!(descend(start_at_index, target, &mut out));
        out
    }

    /// Encode a full block of coefficients against the resolved coeff
    /// probability table and return the bool-encoder byte stream that
    /// `decode_block` should consume.
    fn encode_block(
        coeffs: &[i16; 16],
        block_type: BlockType,
        coeff_probs: &CoeffProbs,
        above_has_nonzero: bool,
        left_has_nonzero: bool,
    ) -> Vec<u8> {
        let mut enc = TestEncoder::new();
        let plane = block_type.plane_index();
        let first_coeff = block_type.first_coeff();
        let mut ctx3 = (above_has_nonzero as usize) + (left_has_nonzero as usize);
        let mut prev_was_zero = false;

        // Find the position of the last non-zero coefficient — we will
        // emit an EOB token immediately after that position (or at
        // first_coeff if the block is empty).
        let mut last_non_zero: i32 = -1;
        for (idx, c) in coeffs.iter().enumerate().take(16) {
            if idx >= first_coeff && *c != 0 {
                last_non_zero = idx as i32;
            }
        }

        let mut i = first_coeff as i32;
        while i < 16 {
            let band = COEFF_BANDS[i as usize];
            let probs = &coeff_probs[plane][band][ctx3];

            let emit_eob = i > last_non_zero;
            let abs_value: u16;
            let sign: bool;
            let token: DctToken;
            if emit_eob {
                token = DctToken::Eob;
                abs_value = 0;
                sign = false;
            } else {
                let v = coeffs[i as usize];
                sign = v < 0;
                abs_value = v.unsigned_abs();
                token = if abs_value == 0 {
                    DctToken::Dct0
                } else if abs_value == 1 {
                    DctToken::Dct1
                } else if abs_value == 2 {
                    DctToken::Dct2
                } else if abs_value == 3 {
                    DctToken::Dct3
                } else if abs_value == 4 {
                    DctToken::Dct4
                } else if abs_value <= 6 {
                    DctToken::Cat1
                } else if abs_value <= 10 {
                    DctToken::Cat2
                } else if abs_value <= 18 {
                    DctToken::Cat3
                } else if abs_value <= 34 {
                    DctToken::Cat4
                } else if abs_value <= 66 {
                    DctToken::Cat5
                } else {
                    DctToken::Cat6
                };
            }

            let start = if prev_was_zero { 2i8 } else { 0i8 };
            for (i_half, bit) in encode_token(token, start) {
                enc.write_bool(probs[i_half], bit);
            }
            if token == DctToken::Eob {
                break;
            }

            if let Some((base, plist)) = match token {
                DctToken::Cat1 => Some((CAT_BASE[0], PCAT1)),
                DctToken::Cat2 => Some((CAT_BASE[1], PCAT2)),
                DctToken::Cat3 => Some((CAT_BASE[2], PCAT3)),
                DctToken::Cat4 => Some((CAT_BASE[3], PCAT4)),
                DctToken::Cat5 => Some((CAT_BASE[4], PCAT5)),
                DctToken::Cat6 => Some((CAT_BASE[5], PCAT6)),
                _ => None,
            } {
                let extra = abs_value - base;
                let n = plist.len();
                for (j, &p) in plist.iter().enumerate() {
                    let bit = ((extra >> (n - 1 - j)) & 1) == 1;
                    enc.write_bool(p, bit);
                }
            }

            if abs_value != 0 {
                enc.write_bool(128, sign);
            }

            ctx3 = if abs_value == 0 {
                0
            } else if abs_value == 1 {
                1
            } else {
                2
            };
            prev_was_zero = token == DctToken::Dct0;
            i += 1;
        }

        enc.finish()
    }

    #[test]
    fn default_probs_table_shape() {
        // Cheap sanity check on the §13.5 transcription: every entry
        // is a u8 in the legal range and the table has 4*8*3*11=1056
        // probabilities.
        let mut count = 0;
        for plane in &DEFAULT_COEFF_PROBS {
            for band in plane {
                for ctx in band {
                    for &_p in ctx {
                        count += 1;
                    }
                }
            }
        }
        assert_eq!(count, 4 * 8 * 3 * 11);
    }

    #[test]
    fn default_probs_first_row_is_uniform_128() {
        // Plane 0 (Y after Y2) band 0 is all-128 in the RFC listing —
        // unused since plane 0 starts at coefficient 1, so band 0
        // never reads. Spot-check the first row.
        for ctx in &DEFAULT_COEFF_PROBS[0][0] {
            for &p in ctx {
                assert_eq!(p, 128);
            }
        }
    }

    #[test]
    fn default_probs_known_values() {
        // A handful of specific cells from §13.5 — would catch a
        // transcription typo on any of the four touched planes.
        assert_eq!(DEFAULT_COEFF_PROBS[0][1][0][0], 253);
        assert_eq!(DEFAULT_COEFF_PROBS[1][0][0][10], 62);
        assert_eq!(DEFAULT_COEFF_PROBS[2][7][2][0], 128); // chroma plane band 7 all-128
        assert_eq!(DEFAULT_COEFF_PROBS[3][0][0][0], 202);
        assert_eq!(DEFAULT_COEFF_PROBS[3][7][2][1], 1); // last block: { 238, 1, 255, ... }
    }

    #[test]
    fn coeff_bands_is_spec_listing() {
        assert_eq!(
            COEFF_BANDS,
            [0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7]
        );
    }

    #[test]
    fn block_type_first_coeff_and_plane_index() {
        assert_eq!(BlockType::YAfterY2.first_coeff(), 1);
        assert_eq!(BlockType::Y2.first_coeff(), 0);
        assert_eq!(BlockType::UV.first_coeff(), 0);
        assert_eq!(BlockType::YNoY2.first_coeff(), 0);
        assert_eq!(BlockType::YAfterY2.plane_index(), 0);
        assert_eq!(BlockType::Y2.plane_index(), 1);
        assert_eq!(BlockType::UV.plane_index(), 2);
        assert_eq!(BlockType::YNoY2.plane_index(), 3);
    }

    #[test]
    fn merge_no_updates_is_identity() {
        let updates: TokenProbUpdates = [[[[None; 11]; 3]; 8]; 4];
        let merged = merge_default_token_probs(&updates);
        assert_eq!(merged, DEFAULT_COEFF_PROBS);
    }

    #[test]
    fn merge_overlays_present_entries() {
        let mut updates: TokenProbUpdates = [[[[None; 11]; 3]; 8]; 4];
        updates[2][3][1][5] = Some(42);
        updates[0][0][0][0] = Some(7);
        let merged = merge_default_token_probs(&updates);
        assert_eq!(merged[2][3][1][5], 42);
        assert_eq!(merged[0][0][0][0], 7);
        // Untouched cells still match defaults.
        assert_eq!(merged[3][5][2][1], DEFAULT_COEFF_PROBS[3][5][2][1]);
    }

    #[test]
    fn decode_immediate_eob_yields_all_zero_block() {
        // Block: 16 zeros; first token is dct_eob.
        let probs = &DEFAULT_COEFF_PROBS;
        let coeffs = [0i16; 16];
        let buf = encode_block(&coeffs, BlockType::Y2, probs, false, false);
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let mut out = [0i16; 16];
        let nz = decode_block(&mut dec, BlockType::Y2, probs, false, false, &mut out).unwrap();
        assert_eq!(nz, 0);
        assert_eq!(out, [0i16; 16]);
    }

    #[test]
    fn decode_roundtrip_single_dct1_at_position_0() {
        let probs = &DEFAULT_COEFF_PROBS;
        let mut coeffs = [0i16; 16];
        coeffs[0] = 1;
        let buf = encode_block(&coeffs, BlockType::Y2, probs, false, false);
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let mut out = [0i16; 16];
        let nz = decode_block(&mut dec, BlockType::Y2, probs, false, false, &mut out).unwrap();
        assert_eq!(nz, 1);
        assert_eq!(out[0], 1);
        for &v in out.iter().skip(1) {
            assert_eq!(v, 0);
        }
    }

    #[test]
    fn decode_roundtrip_negative_value() {
        let probs = &DEFAULT_COEFF_PROBS;
        let mut coeffs = [0i16; 16];
        coeffs[0] = -3;
        coeffs[1] = -2;
        let buf = encode_block(&coeffs, BlockType::Y2, probs, true, true);
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let mut out = [0i16; 16];
        let nz = decode_block(&mut dec, BlockType::Y2, probs, true, true, &mut out).unwrap();
        assert_eq!(nz, 2);
        assert_eq!(out[0], -3);
        assert_eq!(out[1], -2);
    }

    #[test]
    fn decode_roundtrip_each_cat_range() {
        // One value from inside each cat1..cat6 range. Verifies the
        // base + extra-bits arithmetic at the cat boundary, halfway
        // inside the range, and at the cat ceiling.
        for &val in &[5i16, 6, 7, 10, 11, 18, 19, 34, 35, 66, 67, 100, 2048] {
            let probs = &DEFAULT_COEFF_PROBS;
            let mut coeffs = [0i16; 16];
            coeffs[0] = val;
            let buf = encode_block(&coeffs, BlockType::Y2, probs, false, false);
            let mut dec = BoolDecoder::init(&buf).unwrap();
            let mut out = [0i16; 16];
            let nz = decode_block(&mut dec, BlockType::Y2, probs, false, false, &mut out).unwrap();
            assert_eq!(nz, 1, "val={val}");
            assert_eq!(out[0], val, "val={val}");
        }
    }

    #[test]
    fn decode_y_after_y2_skips_dc_position() {
        // Plane 0 (`YAfterY2`) starts at coefficient 1; position 0
        // must stay zero even if the encoded bits would otherwise
        // ascribe a token there. Verify by encoding a value at
        // position 1.
        let probs = &DEFAULT_COEFF_PROBS;
        let mut coeffs = [0i16; 16];
        coeffs[1] = 7; // cat2
        let buf = encode_block(&coeffs, BlockType::YAfterY2, probs, false, false);
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let mut out = [0i16; 16];
        let nz =
            decode_block(&mut dec, BlockType::YAfterY2, probs, false, false, &mut out).unwrap();
        assert_eq!(nz, 1);
        assert_eq!(out[0], 0);
        assert_eq!(out[1], 7);
    }

    #[test]
    fn decode_dense_block_zigzag_positions() {
        // A non-trivial block with values at multiple positions
        // including the rollover from DCT_0 -> non-zero (covering
        // the §13.3 `prev_was_zero` / `ctx3` rollover).
        let probs = &DEFAULT_COEFF_PROBS;
        let mut coeffs = [0i16; 16];
        coeffs[0] = 3;
        coeffs[1] = 0;
        coeffs[2] = -1;
        coeffs[3] = 2;
        coeffs[5] = 15; // cat3
        coeffs[8] = -25; // cat4
        let buf = encode_block(&coeffs, BlockType::UV, probs, true, false);
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let mut out = [0i16; 16];
        let nz = decode_block(&mut dec, BlockType::UV, probs, true, false, &mut out).unwrap();
        assert_eq!(nz, 5);
        assert_eq!(out, coeffs);
    }

    #[test]
    fn decode_under_overlaid_probability_updates() {
        // Use a TokenProbUpdates that overrides a handful of entries
        // and verify decode still round-trips. Catches a bug where
        // `decode_block` accidentally hits `DEFAULT_COEFF_PROBS`
        // instead of the caller-supplied table.
        let mut updates: TokenProbUpdates = [[[[None; 11]; 3]; 8]; 4];
        updates[2][1][0][0] = Some(200);
        updates[2][1][0][1] = Some(50);
        updates[2][2][1][2] = Some(99);
        let probs = merge_default_token_probs(&updates);

        let mut coeffs = [0i16; 16];
        coeffs[0] = 4;
        coeffs[1] = -1;
        coeffs[3] = 12; // cat3
        let buf = encode_block(&coeffs, BlockType::UV, &probs, false, false);
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let mut out = [0i16; 16];
        let nz = decode_block(&mut dec, BlockType::UV, &probs, false, false, &mut out).unwrap();
        assert_eq!(nz, 3);
        assert_eq!(out, coeffs);
    }

    #[test]
    fn prev_token_skips_eob_branch() {
        // Encode a block where the second coefficient is reached
        // through the "previous was zero" path. The decoder must
        // start the second token's tree-walk at index 2 (skipping
        // the dct_eob branch). To prove this matters: emit a long
        // run of zeros that would, under the buggy spec literal
        // `prev_was_zero = true`, be interpreted as eob.
        //
        // We rely on the encode helper applying the same "start at
        // 2 when prev was zero" rule; if `decode_block` did the
        // wrong thing, the round-trip would fail.
        let probs = &DEFAULT_COEFF_PROBS;
        let mut coeffs = [0i16; 16];
        coeffs[0] = 0;
        coeffs[1] = 0;
        coeffs[2] = 0;
        coeffs[3] = 5; // cat1
        let buf = encode_block(&coeffs, BlockType::Y2, probs, false, false);
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let mut out = [0i16; 16];
        let nz = decode_block(&mut dec, BlockType::Y2, probs, false, false, &mut out).unwrap();
        assert_eq!(nz, 1);
        assert_eq!(out, coeffs);
    }

    #[test]
    fn neighbour_predictor_count_seeds_ctx3() {
        // The `ctx3` for the very first coefficient is the count
        // (0/1/2) of non-zero neighbours. Encoding with all four
        // cases (00, 01, 10, 11) and round-tripping makes sure the
        // decoder picks the same row.
        let probs = &DEFAULT_COEFF_PROBS;
        for (above, left) in [(false, false), (true, false), (false, true), (true, true)] {
            let mut coeffs = [0i16; 16];
            coeffs[0] = -7; // cat2
            coeffs[2] = 1;
            let buf = encode_block(&coeffs, BlockType::UV, probs, above, left);
            let mut dec = BoolDecoder::init(&buf).unwrap();
            let mut out = [0i16; 16];
            let nz = decode_block(&mut dec, BlockType::UV, probs, above, left, &mut out).unwrap();
            assert_eq!(nz, 2, "above={above} left={left}");
            assert_eq!(out, coeffs, "above={above} left={left}");
        }
    }

    #[test]
    fn block_full_to_position_15_no_eob_emitted() {
        // A block that uses all 16 positions emits no EOB at all —
        // the §13.3 loop runs to completion with i = 16. Verify
        // the decoder handles that termination correctly.
        let probs = &DEFAULT_COEFF_PROBS;
        let coeffs = [1i16; 16];
        let buf = encode_block(&coeffs, BlockType::Y2, probs, false, false);
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let mut out = [0i16; 16];
        let nz = decode_block(&mut dec, BlockType::Y2, probs, false, false, &mut out).unwrap();
        assert_eq!(nz, 16);
        assert_eq!(out, coeffs);
    }

    #[test]
    fn cat6_high_magnitude_roundtrip() {
        // cat6 with the largest legal magnitude (67 + 2047 = 2114).
        // Exercises the 11-extra-bit DCTextra path.
        let probs = &DEFAULT_COEFF_PROBS;
        let mut coeffs = [0i16; 16];
        coeffs[0] = 2114;
        let buf = encode_block(&coeffs, BlockType::Y2, probs, false, false);
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let mut out = [0i16; 16];
        let nz = decode_block(&mut dec, BlockType::Y2, probs, false, false, &mut out).unwrap();
        assert_eq!(nz, 1);
        assert_eq!(out[0], 2114);
    }

    // -----------------------------------------------------------------
    // §13.3 per-macroblock walk tests
    // -----------------------------------------------------------------

    /// Reorder a raster-order block back into scan order (the inverse of
    /// `scan_to_raster`) so a test can specify the expected raster layout
    /// and feed `encode_block` (which works in scan order).
    fn raster_to_scan(raster: &[i16; 16]) -> [i16; 16] {
        let mut scan = [0i16; 16];
        for (c, slot) in scan.iter_mut().enumerate() {
            *slot = raster[ZIGZAG[c]];
        }
        scan
    }

    /// A single shared bool-encoder that emits the whole macroblock
    /// residual record contiguously (the per-block `TestEncoder` cannot be
    /// concatenated). Mirrors `decode_mb_coeffs` exactly: block order,
    /// plane selection, zig-zag, and predictor threading.
    struct MbEncoder {
        enc: TestEncoder,
    }

    impl MbEncoder {
        fn new() -> Self {
            Self {
                enc: TestEncoder::new(),
            }
        }

        fn write_block(
            &mut self,
            block_index: usize,
            block_type: BlockType,
            raster: &[i16; 16],
            coeff_probs: &CoeffProbs,
            above: &mut MbEntropyCtx,
            left: &mut MbEntropyCtx,
        ) {
            let a_slot = ABOVE_CONTEXT_INDEX[block_index];
            let l_slot = LEFT_CONTEXT_INDEX[block_index];
            let above_nz = above.nonzero[a_slot];
            let left_nz = left.nonzero[l_slot];
            let scan = raster_to_scan(raster);

            // Replay the §13.3 token loop, writing bits into the shared
            // encoder (this is `encode_block`'s body, inlined to share the
            // encoder state across blocks).
            let plane = block_type.plane_index();
            let first_coeff = block_type.first_coeff();
            let mut ctx3 = (above_nz as usize) + (left_nz as usize);
            let mut prev_was_zero = false;
            let mut last_non_zero: i32 = -1;
            for (idx, c) in scan.iter().enumerate().take(16) {
                if idx >= first_coeff && *c != 0 {
                    last_non_zero = idx as i32;
                }
            }
            let mut nonzero_count = 0usize;
            let mut i = first_coeff as i32;
            while i < 16 {
                let band = COEFF_BANDS[i as usize];
                let probs = &coeff_probs[plane][band][ctx3];
                let emit_eob = i > last_non_zero;
                let abs_value: u16;
                let sign: bool;
                let token: DctToken;
                if emit_eob {
                    token = DctToken::Eob;
                    abs_value = 0;
                    sign = false;
                } else {
                    let v = scan[i as usize];
                    sign = v < 0;
                    abs_value = v.unsigned_abs();
                    token = if abs_value == 0 {
                        DctToken::Dct0
                    } else if abs_value == 1 {
                        DctToken::Dct1
                    } else if abs_value == 2 {
                        DctToken::Dct2
                    } else if abs_value == 3 {
                        DctToken::Dct3
                    } else if abs_value == 4 {
                        DctToken::Dct4
                    } else if abs_value <= 6 {
                        DctToken::Cat1
                    } else if abs_value <= 10 {
                        DctToken::Cat2
                    } else if abs_value <= 18 {
                        DctToken::Cat3
                    } else if abs_value <= 34 {
                        DctToken::Cat4
                    } else if abs_value <= 66 {
                        DctToken::Cat5
                    } else {
                        DctToken::Cat6
                    };
                }
                let start = if prev_was_zero { 2i8 } else { 0i8 };
                for (i_half, bit) in encode_token(token, start) {
                    self.enc.write_bool(probs[i_half], bit);
                }
                if token == DctToken::Eob {
                    break;
                }
                if let Some((base, plist)) = match token {
                    DctToken::Cat1 => Some((CAT_BASE[0], PCAT1)),
                    DctToken::Cat2 => Some((CAT_BASE[1], PCAT2)),
                    DctToken::Cat3 => Some((CAT_BASE[2], PCAT3)),
                    DctToken::Cat4 => Some((CAT_BASE[3], PCAT4)),
                    DctToken::Cat5 => Some((CAT_BASE[4], PCAT5)),
                    DctToken::Cat6 => Some((CAT_BASE[5], PCAT6)),
                    _ => None,
                } {
                    let extra = abs_value - base;
                    let n = plist.len();
                    for (j, &p) in plist.iter().enumerate() {
                        let bit = ((extra >> (n - 1 - j)) & 1) == 1;
                        self.enc.write_bool(p, bit);
                    }
                }
                if abs_value != 0 {
                    self.enc.write_bool(128, sign);
                    nonzero_count += 1;
                }
                ctx3 = if abs_value == 0 {
                    0
                } else if abs_value == 1 {
                    1
                } else {
                    2
                };
                prev_was_zero = token == DctToken::Dct0;
                i += 1;
            }
            // Mirror the decoder's predictor update.
            let has_coeffs = nonzero_count != 0;
            above.nonzero[a_slot] = has_coeffs;
            left.nonzero[l_slot] = has_coeffs;
        }

        fn finish(self) -> Vec<u8> {
            self.enc.finish()
        }
    }

    /// Encode a whole macroblock contiguously and return the byte stream.
    /// `above` / `left` are threaded and left holding the post-MB context.
    #[allow(clippy::too_many_arguments)]
    fn encode_mb_shared(
        has_y2: bool,
        coeff_probs: &CoeffProbs,
        above: &mut MbEntropyCtx,
        left: &mut MbEntropyCtx,
        y2_raster: &[i16; 16],
        y_raster: &[[i16; 16]; 16],
        u_raster: &[[i16; 16]; 4],
        v_raster: &[[i16; 16]; 4],
    ) -> Vec<u8> {
        let mut mb = MbEncoder::new();
        if has_y2 {
            mb.write_block(24, BlockType::Y2, y2_raster, coeff_probs, above, left);
        }
        let y_plane = if has_y2 {
            BlockType::YAfterY2
        } else {
            BlockType::YNoY2
        };
        for (i, b) in y_raster.iter().enumerate() {
            mb.write_block(i, y_plane, b, coeff_probs, above, left);
        }
        for (i, b) in u_raster.iter().enumerate() {
            mb.write_block(16 + i, BlockType::UV, b, coeff_probs, above, left);
        }
        for (i, b) in v_raster.iter().enumerate() {
            mb.write_block(20 + i, BlockType::UV, b, coeff_probs, above, left);
        }
        mb.finish()
    }

    #[test]
    fn zigzag_is_a_permutation_of_0_15() {
        // §20.16 zigzag[16] must be a bijection on 0..16 so
        // scan_to_raster / raster_to_scan round-trip.
        let mut seen = [false; 16];
        for &r in &ZIGZAG {
            assert!(!seen[r], "duplicate raster index {r}");
            seen[r] = true;
        }
        assert!(seen.iter().all(|&s| s));
        // Round-trip a distinct-valued block.
        let raster: [i16; 16] = core::array::from_fn(|i| (i as i16) - 8);
        assert_eq!(scan_to_raster(&raster_to_scan(&raster)), raster);
    }

    #[test]
    fn context_index_tables_match_section_20_16() {
        // Spot-check the §20.16 left/above slot tables against the RFC
        // annex listing, including the Y2 (block 24) slot.
        assert_eq!(LEFT_CONTEXT_INDEX[0], 0);
        assert_eq!(LEFT_CONTEXT_INDEX[4], 1);
        assert_eq!(LEFT_CONTEXT_INDEX[15], 3);
        assert_eq!(LEFT_CONTEXT_INDEX[16], 4); // first U
        assert_eq!(LEFT_CONTEXT_INDEX[20], 6); // first V
        assert_eq!(LEFT_CONTEXT_INDEX[24], 8); // Y2
        assert_eq!(ABOVE_CONTEXT_INDEX[0], 0);
        assert_eq!(ABOVE_CONTEXT_INDEX[4], 0);
        assert_eq!(ABOVE_CONTEXT_INDEX[5], 1);
        assert_eq!(ABOVE_CONTEXT_INDEX[16], 4);
        assert_eq!(ABOVE_CONTEXT_INDEX[24], 8);
    }

    #[test]
    fn mb_skip_yields_zero_coeffs_and_resets_context() {
        // A skip MB reads no tokens and zeroes its Y/U/V predictor slots.
        // With has_y2 = true the Y2 slot is also cleared.
        let probs = &DEFAULT_COEFF_PROBS;
        let mut above = MbEntropyCtx {
            nonzero: [true; MB_ENTROPY_CTX_LEN],
        };
        let mut left = MbEntropyCtx {
            nonzero: [true; MB_ENTROPY_CTX_LEN],
        };
        // No bytes are consumed; a trivial buffer suffices.
        let buf = [0u8; 8];
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let mb = decode_mb_coeffs(&mut dec, true, true, probs, &mut above, &mut left).unwrap();
        assert_eq!(mb.y2, [0i16; 16]);
        assert_eq!(mb.y, [[0i16; 16]; 16]);
        assert_eq!(mb.u, [[0i16; 16]; 4]);
        assert_eq!(mb.v, [[0i16; 16]; 4]);
        assert_eq!(above.nonzero, [false; MB_ENTROPY_CTX_LEN]);
        assert_eq!(left.nonzero, [false; MB_ENTROPY_CTX_LEN]);
    }

    #[test]
    fn mb_skip_bpred_preserves_y2_context() {
        // §20.16 reset_mb_context: a skipped B_PRED MB (no Y2) clears the
        // eight Y/U/V slots but preserves the Y2 slot (8).
        let probs = &DEFAULT_COEFF_PROBS;
        let mut above = MbEntropyCtx {
            nonzero: [true; MB_ENTROPY_CTX_LEN],
        };
        let mut left = MbEntropyCtx {
            nonzero: [true; MB_ENTROPY_CTX_LEN],
        };
        let buf = [0u8; 8];
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let _ = decode_mb_coeffs(&mut dec, false, true, probs, &mut above, &mut left).unwrap();
        for slot in 0..8 {
            assert!(!above.nonzero[slot], "above slot {slot} not cleared");
            assert!(!left.nonzero[slot], "left slot {slot} not cleared");
        }
        assert!(above.nonzero[8], "above Y2 slot must be preserved");
        assert!(left.nonzero[8], "left Y2 slot must be preserved");
    }

    #[test]
    fn synthetic_mb_decodes_to_expected_per_block_layout() {
        // Build a macroblock with distinctive coefficients in each plane,
        // encode it contiguously, and verify decode_mb_coeffs recovers the
        // exact per-block raster layout — including the Y2 block, the
        // YAfterY2 first-coefficient-1 luma plane, and chroma.
        let probs = &DEFAULT_COEFF_PROBS;

        let mut y2 = [0i16; 16];
        y2[0] = 9;
        y2[3] = -4;
        y2[15] = 2;

        // Each Y block: a recognisable value at raster position 1 (AC,
        // since the YAfterY2 plane starts decoding at scan coefficient 1).
        // Raster position 0 (the DC) must stay 0 — it is owned by Y2 and
        // never decoded for the YAfterY2 plane.
        let mut y = [[0i16; 16]; 16];
        for (k, blk) in y.iter_mut().enumerate() {
            blk[1] = (k as i16) - 8; // distinct per block, some negative
            if k % 3 == 0 {
                blk[5] = 3;
            }
        }

        let mut u = [[0i16; 16]; 4];
        let mut v = [[0i16; 16]; 4];
        for (k, blk) in u.iter_mut().enumerate() {
            blk[0] = (k as i16) + 1; // DC present (UV plane starts at 0)
        }
        for (k, blk) in v.iter_mut().enumerate() {
            blk[0] = -((k as i16) + 1);
            blk[2] = 7;
        }

        let mut enc_above = MbEntropyCtx::default();
        let mut enc_left = MbEntropyCtx::default();
        let buf = encode_mb_shared(true, probs, &mut enc_above, &mut enc_left, &y2, &y, &u, &v);

        let mut dec_above = MbEntropyCtx::default();
        let mut dec_left = MbEntropyCtx::default();
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let mb =
            decode_mb_coeffs(&mut dec, true, false, probs, &mut dec_above, &mut dec_left).unwrap();

        assert_eq!(mb.y2, y2, "Y2 block mismatch");
        assert_eq!(mb.y, y, "Y blocks mismatch");
        assert_eq!(mb.u, u, "U blocks mismatch");
        assert_eq!(mb.v, v, "V blocks mismatch");

        // The decoder's post-MB context must equal the encoder's.
        assert_eq!(dec_above, enc_above, "above context drift");
        assert_eq!(dec_left, enc_left, "left context drift");

        // Sanity on the resulting context: Y2 had coeffs (slot 8 set);
        // every Y column/row slot set (each Y block has a nonzero AC);
        // U/V slots set.
        assert!(dec_above.nonzero[8], "Y2 above predictor should be set");
        for slot in 0..8 {
            assert!(dec_above.nonzero[slot], "above slot {slot} should be set");
        }
    }

    #[test]
    fn empty_block_clears_its_predictor_slot() {
        // A block that decodes to all-zero (immediate EOB) must clear its
        // predictor slot even if it was set on entry, so the slot reflects
        // the LAST block mapped to it. We encode a Y MB where Y block 0
        // (slots above=0, left=0) is empty while Y block 1 (above=1,
        // left=0) is non-empty, then assert the resulting context.
        let probs = &DEFAULT_COEFF_PROBS;
        let y2 = [0i16; 16];
        let mut y = [[0i16; 16]; 16];
        // Y block 0 empty; Y block 1 has an AC coeff. Leave the rest empty.
        y[1][1] = 5;
        let u = [[0i16; 16]; 4];
        let v = [[0i16; 16]; 4];

        let mut enc_above = MbEntropyCtx::default();
        let mut enc_left = MbEntropyCtx::default();
        let buf = encode_mb_shared(true, probs, &mut enc_above, &mut enc_left, &y2, &y, &u, &v);
        let mut dec_above = MbEntropyCtx::default();
        let mut dec_left = MbEntropyCtx::default();
        let mut dec = BoolDecoder::init(&buf).unwrap();
        let mb =
            decode_mb_coeffs(&mut dec, true, false, probs, &mut dec_above, &mut dec_left).unwrap();
        assert_eq!(mb.y, y);
        // Above slot for Y column 0 (blocks 0,4,8,12): the last decoded
        // block touching above-slot 0 is Y block 12 (empty) → false.
        assert!(
            !dec_above.nonzero[0],
            "above slot 0 should be cleared by later empty block"
        );
        // Above slot for Y column 1 (blocks 1,5,9,13): last is block 13
        // (empty) → false; but block 1 was non-empty earlier — confirm the
        // slot reflects the *last* block, not the earlier non-empty one.
        assert!(
            !dec_above.nonzero[1],
            "above slot 1 must reflect last (empty) block"
        );
        assert_eq!(dec_above, enc_above);
        assert_eq!(dec_left, enc_left);
    }

    #[test]
    fn two_adjacent_mbs_propagate_left_context() {
        // §13.3: the left predictor of MB(col=1) is the right column of
        // MB(col=0). Decode two horizontally-adjacent MBs sharing a single
        // rolling `left` context (and independent `above` rows), and prove
        // the second MB's first-coefficient context depends on the first
        // MB's right-edge blocks.
        //
        // We construct MB0 so that its right-column Y blocks (3,7,11,15 ->
        // left slot 3) and right-column U/V are non-empty, then verify the
        // identical MB1 token bytes decode correctly only when the carried
        // `left` context is used. The strongest proof: decode MB1 with the
        // propagated context vs a fresh (all-false) context and show the
        // recovered coefficients differ (a context mismatch desyncs the
        // bool decoder, corrupting the stream).
        let probs = &DEFAULT_COEFF_PROBS;

        // MB0: give every block a non-empty residue so all left slots end
        // up set (especially the right-column-driven slot 3, and U/V).
        let y2_0 = {
            let mut b = [0i16; 16];
            b[0] = 2;
            b
        };
        let mut y0 = [[0i16; 16]; 16];
        for blk in y0.iter_mut() {
            blk[1] = 1;
        }
        let mut u0 = [[0i16; 16]; 4];
        let mut v0 = [[0i16; 16]; 4];
        for blk in u0.iter_mut() {
            blk[0] = 1;
        }
        for blk in v0.iter_mut() {
            blk[0] = 1;
        }

        // MB1: distinctive coefficients so we can assert exact recovery.
        let y2_1 = {
            let mut b = [0i16; 16];
            b[0] = -5;
            b[1] = 2;
            b
        };
        let mut y1 = [[0i16; 16]; 16];
        for (k, blk) in y1.iter_mut().enumerate() {
            blk[1] = (k as i16) - 5;
        }
        let mut u1 = [[0i16; 16]; 4];
        let mut v1 = [[0i16; 16]; 4];
        for (k, blk) in u1.iter_mut().enumerate() {
            blk[0] = (k as i16) + 2;
        }
        for (k, blk) in v1.iter_mut().enumerate() {
            blk[0] = -((k as i16) + 2);
        }

        // Encode both MBs with the shared rolling `left` and a per-column
        // `above` row of two contexts (mirroring a 2-wide MB row).
        let mut enc_left = MbEntropyCtx::default();
        let mut enc_above0 = MbEntropyCtx::default();
        let mut enc_above1 = MbEntropyCtx::default();
        let buf0 = encode_mb_shared(
            true,
            probs,
            &mut enc_above0,
            &mut enc_left,
            &y2_0,
            &y0,
            &u0,
            &v0,
        );
        // After MB0, `enc_left` holds MB0's right-edge context — exactly
        // what MB1 must consume. Capture it for the negative control.
        let left_after_mb0 = enc_left;
        let buf1 = encode_mb_shared(
            true,
            probs,
            &mut enc_above1,
            &mut enc_left,
            &y2_1,
            &y1,
            &u1,
            &v1,
        );

        // Decode MB0, then MB1 with the propagated `left`.
        let mut dec_left = MbEntropyCtx::default();
        let mut dec_above0 = MbEntropyCtx::default();
        let mut dec_above1 = MbEntropyCtx::default();
        let mut dec0 = BoolDecoder::init(&buf0).unwrap();
        let mb0 = decode_mb_coeffs(
            &mut dec0,
            true,
            false,
            probs,
            &mut dec_above0,
            &mut dec_left,
        )
        .unwrap();
        assert_eq!(mb0.y, y0);
        assert_eq!(
            dec_left, left_after_mb0,
            "MB0 must yield MB1's left context"
        );

        let mut dec1 = BoolDecoder::init(&buf1).unwrap();
        let mb1 = decode_mb_coeffs(
            &mut dec1,
            true,
            false,
            probs,
            &mut dec_above1,
            &mut dec_left,
        )
        .unwrap();
        assert_eq!(mb1.y2, y2_1, "MB1 Y2 with propagated left context");
        assert_eq!(mb1.y, y1, "MB1 Y with propagated left context");
        assert_eq!(mb1.u, u1, "MB1 U with propagated left context");
        assert_eq!(mb1.v, v1, "MB1 V with propagated left context");

        // Negative control: decoding MB1 with a FRESH (all-false) left
        // context picks the wrong probability rows on the first
        // coefficient of every block, desyncing the range decoder. The
        // recovered coefficients must NOT match the encoded MB1 — proving
        // the propagation is load-bearing, not incidental.
        let mut wrong_left = MbEntropyCtx::default();
        let mut wrong_above = MbEntropyCtx::default();
        let mut dec_wrong = BoolDecoder::init(&buf1).unwrap();
        let mb_wrong = decode_mb_coeffs(
            &mut dec_wrong,
            true,
            false,
            probs,
            &mut wrong_above,
            &mut wrong_left,
        );
        // It may decode (to garbage) or error; either way it must not
        // reproduce the correct MB1 coefficients.
        if let Ok(garbage) = mb_wrong {
            assert!(
                garbage.y != y1 || garbage.y2 != y2_1 || garbage.u != u1 || garbage.v != v1,
                "fresh-left decode unexpectedly matched — propagation not load-bearing?"
            );
        }
    }
}