oxideav-h264 0.1.9

Pure-Rust H.264 / AVC (ITU-T H.264 | ISO/IEC 14496-10) codec for oxideav — under spec-driven rewrite, no decode/encode functionality yet
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
//! §8.3 — Intra prediction.
//!
//! All Intra_NxN and Intra_16x16 / chroma modes per ITU-T Rec. H.264
//! (08/2024). Pure pixel-level transforms — no bitstream, no context.
//!
//! Sample reference layout mirrors the spec's p[x, y]:
//!   p[-1, -1]       = top_left (scalar)
//!   p[0..=3,  -1]   = top
//!   p[4..=7,  -1]   = top_right   (for 4x4; for 8x8 p[8..=15, -1])
//!   p[-1,   0..=3]  = left        (for 4x4; for 8x8 0..=7)
//!
//! All prediction outputs are clipped to the bit-depth range
//! `0..=(1<<bit_depth)-1` (Clip1_Y / Clip1_C in the spec).

#![allow(dead_code)]

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

/// §5 — Clip1(x) = Clip3(0, (1<<BitDepth)-1, x).
#[inline]
fn clip1(x: i32, bit_depth: u32) -> i32 {
    let max = (1i32 << bit_depth) - 1;
    if x < 0 {
        0
    } else if x > max {
        max
    } else {
        x
    }
}

/// DC fallback when no neighbours are available.
#[inline]
fn dc_default(bit_depth: u32) -> i32 {
    1 << (bit_depth - 1)
}

// ---------------------------------------------------------------------------
// Mode enums
// ---------------------------------------------------------------------------

/// §8.3.1.1 Table 8-2.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Intra4x4Mode {
    Vertical,
    Horizontal,
    Dc,
    DiagonalDownLeft,
    DiagonalDownRight,
    VerticalRight,
    HorizontalDown,
    VerticalLeft,
    HorizontalUp,
}

impl Intra4x4Mode {
    pub fn from_index(idx: u8) -> Option<Self> {
        Some(match idx {
            0 => Self::Vertical,
            1 => Self::Horizontal,
            2 => Self::Dc,
            3 => Self::DiagonalDownLeft,
            4 => Self::DiagonalDownRight,
            5 => Self::VerticalRight,
            6 => Self::HorizontalDown,
            7 => Self::VerticalLeft,
            8 => Self::HorizontalUp,
            _ => return None,
        })
    }

    pub fn as_index(self) -> u8 {
        match self {
            Self::Vertical => 0,
            Self::Horizontal => 1,
            Self::Dc => 2,
            Self::DiagonalDownLeft => 3,
            Self::DiagonalDownRight => 4,
            Self::VerticalRight => 5,
            Self::HorizontalDown => 6,
            Self::VerticalLeft => 7,
            Self::HorizontalUp => 8,
        }
    }
}

/// §8.3.2.1 Table 8-3.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Intra8x8Mode {
    Vertical,
    Horizontal,
    Dc,
    DiagonalDownLeft,
    DiagonalDownRight,
    VerticalRight,
    HorizontalDown,
    VerticalLeft,
    HorizontalUp,
}

impl Intra8x8Mode {
    pub fn from_index(idx: u8) -> Option<Self> {
        Some(match idx {
            0 => Self::Vertical,
            1 => Self::Horizontal,
            2 => Self::Dc,
            3 => Self::DiagonalDownLeft,
            4 => Self::DiagonalDownRight,
            5 => Self::VerticalRight,
            6 => Self::HorizontalDown,
            7 => Self::VerticalLeft,
            8 => Self::HorizontalUp,
            _ => return None,
        })
    }

    pub fn as_index(self) -> u8 {
        match self {
            Self::Vertical => 0,
            Self::Horizontal => 1,
            Self::Dc => 2,
            Self::DiagonalDownLeft => 3,
            Self::DiagonalDownRight => 4,
            Self::VerticalRight => 5,
            Self::HorizontalDown => 6,
            Self::VerticalLeft => 7,
            Self::HorizontalUp => 8,
        }
    }
}

/// §8.3.3 Table 8-4.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Intra16x16Mode {
    Vertical,
    Horizontal,
    Dc,
    Plane,
}

impl Intra16x16Mode {
    pub fn from_index(idx: u8) -> Option<Self> {
        Some(match idx {
            0 => Self::Vertical,
            1 => Self::Horizontal,
            2 => Self::Dc,
            3 => Self::Plane,
            _ => return None,
        })
    }

    pub fn as_index(self) -> u8 {
        match self {
            Self::Vertical => 0,
            Self::Horizontal => 1,
            Self::Dc => 2,
            Self::Plane => 3,
        }
    }
}

/// §8.3.4 Table 8-5.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum IntraChromaMode {
    Dc,
    Horizontal,
    Vertical,
    Plane,
}

impl IntraChromaMode {
    pub fn from_index(idx: u8) -> Option<Self> {
        Some(match idx {
            0 => Self::Dc,
            1 => Self::Horizontal,
            2 => Self::Vertical,
            3 => Self::Plane,
            _ => return None,
        })
    }

    pub fn as_index(self) -> u8 {
        match self {
            Self::Dc => 0,
            Self::Horizontal => 1,
            Self::Vertical => 2,
            Self::Plane => 3,
        }
    }
}

// ---------------------------------------------------------------------------
// Neighbour availability and sample containers
// ---------------------------------------------------------------------------

/// Availability flags for neighbour samples.
/// Per §8.3.1.1 — some Intra_4x4 modes (Diagonal_Down_Left,
/// Vertical_Left) read p[4..=7, -1]. When those samples are not
/// available but p[0..=3, -1] is, the spec substitutes p[3, -1]
/// into p[4..=7, -1]. That substitution happens in `resolve_top_right`.
#[derive(Debug, Copy, Clone, Default)]
pub struct Neighbour4x4Availability {
    pub top_left: bool,
    pub top: bool,
    pub top_right: bool,
    pub left: bool,
}

// ---------------------------------------------------------------------------
// §8.3.1.2 / §8.3.2.2 / §8.3.3 / §8.3.4 — bitstream-conformance
// availability constraints ("shall be used only when ... marked as
// available"). A conforming encoder never signals a prediction mode
// whose required reference samples are unavailable; when a bitstream
// does, decoding is undefined, so the decoder surfaces it as invalid
// data instead of inventing substitute samples.
// ---------------------------------------------------------------------------

/// §8.3.1.2.1–.9 — true iff `mode` is permitted with this neighbour
/// availability. DC (§8.3.1.2.4) defines fallbacks for every
/// availability combination and is always permitted. Diagonal-Down-Left
/// / Vertical-Left need only p[0..=3, -1]: the §8.3.1.2 substitution
/// rule fills p[4..=7, -1] from p[3, -1]. Modes reading p[-1, -1]
/// (Diagonal-Down-Right, Vertical-Right, Horizontal-Down) require the
/// corner too — Intra_4x4 has no corner-substitution rule.
pub fn intra_4x4_mode_permitted(mode: Intra4x4Mode, av: &Neighbour4x4Availability) -> bool {
    match mode {
        Intra4x4Mode::Dc => true,
        Intra4x4Mode::Vertical | Intra4x4Mode::DiagonalDownLeft | Intra4x4Mode::VerticalLeft => {
            av.top
        }
        Intra4x4Mode::Horizontal | Intra4x4Mode::HorizontalUp => av.left,
        Intra4x4Mode::DiagonalDownRight
        | Intra4x4Mode::VerticalRight
        | Intra4x4Mode::HorizontalDown => av.top && av.left && av.top_left,
    }
}

/// §8.3.2.2.2–.9 — Intra_8x8 variant. Unlike Intra_4x4, the §8.3.2.2.1
/// reference-sample filtering derives p'[-1, -1] from p[0, -1] /
/// p[-1, 0] when the corner is unavailable, so the corner-reading modes
/// only require top + left here.
pub fn intra_8x8_mode_permitted(mode: Intra8x8Mode, av: &Neighbour4x4Availability) -> bool {
    match mode {
        Intra8x8Mode::Dc => true,
        Intra8x8Mode::Vertical | Intra8x8Mode::DiagonalDownLeft | Intra8x8Mode::VerticalLeft => {
            av.top
        }
        Intra8x8Mode::Horizontal | Intra8x8Mode::HorizontalUp => av.left,
        Intra8x8Mode::DiagonalDownRight
        | Intra8x8Mode::VerticalRight
        | Intra8x8Mode::HorizontalDown => av.top && av.left,
    }
}

/// §8.3.3.1–.4 — Intra_16x16 variant. Plane (§8.3.3.4) reads
/// p[-1, -1] with no substitution rule, so it needs the corner as well.
pub fn intra_16x16_mode_permitted(mode: Intra16x16Mode, av: &Neighbour4x4Availability) -> bool {
    match mode {
        Intra16x16Mode::Dc => true,
        Intra16x16Mode::Vertical => av.top,
        Intra16x16Mode::Horizontal => av.left,
        Intra16x16Mode::Plane => av.top && av.left && av.top_left,
    }
}

/// §8.3.4.1–.4 — chroma variant (DC has per-block fallbacks; Plane
/// reads p[-1, -1] directly).
pub fn intra_chroma_mode_permitted(mode: IntraChromaMode, av: &Neighbour4x4Availability) -> bool {
    match mode {
        IntraChromaMode::Dc => true,
        IntraChromaMode::Horizontal => av.left,
        IntraChromaMode::Vertical => av.top,
        IntraChromaMode::Plane => av.top && av.left && av.top_left,
    }
}

/// Reference samples for a 4x4 predictor, per §8.3.1.1.
/// Layout:
///   p[-1, -1]   = top_left
///   p[0..=3, -1] = top
///   p[4..=7, -1] = top_right
///   p[-1, 0..=3] = left
#[derive(Debug, Copy, Clone)]
pub struct Samples4x4 {
    pub top_left: i32,
    pub top: [i32; 4],
    pub top_right: [i32; 4],
    pub left: [i32; 4],
    pub availability: Neighbour4x4Availability,
}

/// §8.3.2.1 — reference samples for an 8x8 block.
/// Layout:
///   p[-1, -1]   = top_left
///   p[0..=7, -1] = top
///   p[8..=15, -1] = top_right
///   p[-1, 0..=7] = left
#[derive(Debug, Copy, Clone)]
pub struct Samples8x8 {
    pub top_left: i32,
    pub top: [i32; 8],
    pub top_right: [i32; 8],
    pub left: [i32; 8],
    pub availability: Neighbour4x4Availability,
}

/// §8.3.3 — reference samples for a 16x16 block.
#[derive(Debug, Copy, Clone)]
pub struct Samples16x16 {
    pub top_left: i32,
    pub top: [i32; 16],
    pub left: [i32; 16],
    pub availability: Neighbour4x4Availability,
}

/// §8.3.4 — ChromaArrayType gates the chroma block size.
///   ChromaArrayType == 1 (4:2:0): 8x8
///   ChromaArrayType == 2 (4:2:2): 8x16
/// ChromaArrayType == 3 (4:4:4) is treated as luma (use the luma
/// predict_* helpers instead).
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ChromaArrayType {
    Yuv420,
    Yuv422,
}

impl ChromaArrayType {
    /// MbWidthC: chroma block width in samples.
    pub fn width(self) -> usize {
        8
    }
    /// MbHeightC: chroma block height in samples.
    pub fn height(self) -> usize {
        match self {
            Self::Yuv420 => 8,
            Self::Yuv422 => 16,
        }
    }
}

/// Reference samples for an 8x8 or 8x16 chroma block per §8.3.4.
#[derive(Debug, Clone)]
pub struct SamplesChroma {
    pub top_left: i32,
    pub top: Vec<i32>,  // length MbWidthC (== 8)
    pub left: Vec<i32>, // length MbHeightC (8 or 16)
    pub availability: Neighbour4x4Availability,
}

// ---------------------------------------------------------------------------
// §8.3.1 Intra_4x4 prediction
// ---------------------------------------------------------------------------

/// Build the full p[0..=7, -1] array applying §8.3.1.2's substitution
/// rule: "When samples p[x, -1], with x = 4..7, are marked as 'not
/// available for Intra_4x4 prediction,' and the sample p[3, -1] is
/// marked as 'available for Intra_4x4 prediction,' the sample value
/// of p[3, -1] is substituted for sample values p[x, -1], with x = 4..7".
fn top_row_4x4(s: &Samples4x4) -> [i32; 8] {
    let mut p = [0i32; 8];
    p[0..4].copy_from_slice(&s.top);
    if s.availability.top_right {
        p[4..8].copy_from_slice(&s.top_right);
    } else if s.availability.top {
        // §8.3.1.2 substitution rule.
        for slot in &mut p[4..8] {
            *slot = s.top[3];
        }
    }
    p
}

/// Predict a 4x4 block. Output is row-major 4x4 (16 samples).
pub fn predict_4x4(mode: Intra4x4Mode, samples: &Samples4x4, bit_depth: u32, out: &mut [i32; 16]) {
    match mode {
        Intra4x4Mode::Vertical => predict_4x4_vertical(samples, out),
        Intra4x4Mode::Horizontal => predict_4x4_horizontal(samples, out),
        Intra4x4Mode::Dc => predict_4x4_dc(samples, bit_depth, out),
        Intra4x4Mode::DiagonalDownLeft => predict_4x4_ddl(samples, out),
        Intra4x4Mode::DiagonalDownRight => predict_4x4_ddr(samples, out),
        Intra4x4Mode::VerticalRight => predict_4x4_vr(samples, out),
        Intra4x4Mode::HorizontalDown => predict_4x4_hd(samples, out),
        Intra4x4Mode::VerticalLeft => predict_4x4_vl(samples, out),
        Intra4x4Mode::HorizontalUp => predict_4x4_hu(samples, out),
    }
}

#[inline]
fn at4(x: usize, y: usize) -> usize {
    y * 4 + x
}

// §8.3.1.2.1 — Intra_4x4_Vertical.
// pred4x4[x, y] = p[x, -1]
fn predict_4x4_vertical(s: &Samples4x4, out: &mut [i32; 16]) {
    for y in 0..4 {
        for x in 0..4 {
            out[at4(x, y)] = s.top[x];
        }
    }
}

// §8.3.1.2.2 — Intra_4x4_Horizontal.
// pred4x4[x, y] = p[-1, y]
fn predict_4x4_horizontal(s: &Samples4x4, out: &mut [i32; 16]) {
    for y in 0..4 {
        for x in 0..4 {
            out[at4(x, y)] = s.left[y];
        }
    }
}

// §8.3.1.2.3 — Intra_4x4_DC.
fn predict_4x4_dc(s: &Samples4x4, bit_depth: u32, out: &mut [i32; 16]) {
    let dc = if s.availability.top && s.availability.left {
        // (8-48)
        (s.top[0]
            + s.top[1]
            + s.top[2]
            + s.top[3]
            + s.left[0]
            + s.left[1]
            + s.left[2]
            + s.left[3]
            + 4)
            >> 3
    } else if s.availability.left {
        // (8-49) only left available
        (s.left[0] + s.left[1] + s.left[2] + s.left[3] + 2) >> 2
    } else if s.availability.top {
        // (8-50) only top available
        (s.top[0] + s.top[1] + s.top[2] + s.top[3] + 2) >> 2
    } else {
        // (8-51) neither available
        dc_default(bit_depth)
    };
    for sample in out.iter_mut() {
        *sample = dc;
    }
}

// §8.3.1.2.4 — Intra_4x4_Diagonal_Down_Left.
fn predict_4x4_ddl(s: &Samples4x4, out: &mut [i32; 16]) {
    let p = top_row_4x4(s);
    for y in 0..4 {
        for x in 0..4 {
            let v = if x == 3 && y == 3 {
                // (8-52) pred4x4[3,3] = (p[6,-1] + 3*p[7,-1] + 2) >> 2
                (p[6] + 3 * p[7] + 2) >> 2
            } else {
                // (8-53) (p[x+y,-1] + 2*p[x+y+1,-1] + p[x+y+2,-1] + 2) >> 2
                (p[x + y] + 2 * p[x + y + 1] + p[x + y + 2] + 2) >> 2
            };
            out[at4(x, y)] = v;
        }
    }
}

// §8.3.1.2.5 — Intra_4x4_Diagonal_Down_Right.
fn predict_4x4_ddr(s: &Samples4x4, out: &mut [i32; 16]) {
    // Reference samples: top_left plus p[0..=3,-1] and p[-1,0..=3].
    // Use helpers to read p[x,-1] for x in -1..=3 and p[-1,y] for y in -1..=3.
    let tl = s.top_left;
    let t = &s.top;
    let l = &s.left;
    // Helper to fetch p[x,-1] for x in -1..=3.
    let p_top = |x: i32| -> i32 {
        if x == -1 {
            tl
        } else {
            t[x as usize]
        }
    };
    // Helper to fetch p[-1,y] for y in -1..=3.
    let p_left = |y: i32| -> i32 {
        if y == -1 {
            tl
        } else {
            l[y as usize]
        }
    };
    for y in 0..4i32 {
        for x in 0..4i32 {
            let v = if x > y {
                // (8-54) (p[x-y-2,-1] + 2*p[x-y-1,-1] + p[x-y,-1] + 2) >> 2
                (p_top(x - y - 2) + 2 * p_top(x - y - 1) + p_top(x - y) + 2) >> 2
            } else if x < y {
                // (8-55) (p[-1,y-x-2] + 2*p[-1,y-x-1] + p[-1,y-x] + 2) >> 2
                (p_left(y - x - 2) + 2 * p_left(y - x - 1) + p_left(y - x) + 2) >> 2
            } else {
                // x == y: (8-56) (p[0,-1] + 2*p[-1,-1] + p[-1,0] + 2) >> 2
                (p_top(0) + 2 * tl + p_left(0) + 2) >> 2
            };
            out[at4(x as usize, y as usize)] = v;
        }
    }
}

// §8.3.1.2.6 — Intra_4x4_Vertical_Right.
fn predict_4x4_vr(s: &Samples4x4, out: &mut [i32; 16]) {
    let tl = s.top_left;
    let t = &s.top;
    let l = &s.left;
    let p_top = |x: i32| -> i32 {
        if x == -1 {
            tl
        } else {
            t[x as usize]
        }
    };
    let p_left = |y: i32| -> i32 {
        if y == -1 {
            tl
        } else {
            l[y as usize]
        }
    };
    for y in 0..4i32 {
        for x in 0..4i32 {
            let z_vr = 2 * x - y;
            let v = match z_vr {
                0 | 2 | 4 | 6 => {
                    // (8-57) (p[x-(y>>1)-1,-1] + p[x-(y>>1),-1] + 1) >> 1
                    let xp = x - (y >> 1) - 1;
                    (p_top(xp) + p_top(xp + 1) + 1) >> 1
                }
                1 | 3 | 5 => {
                    // (8-58) (p[x-(y>>1)-2,-1] + 2*p[x-(y>>1)-1,-1] + p[x-(y>>1),-1] + 2) >> 2
                    let xp = x - (y >> 1) - 2;
                    (p_top(xp) + 2 * p_top(xp + 1) + p_top(xp + 2) + 2) >> 2
                }
                -1 => {
                    // (8-59) (p[-1,0] + 2*p[-1,-1] + p[0,-1] + 2) >> 2
                    (p_left(0) + 2 * tl + p_top(0) + 2) >> 2
                }
                _ => {
                    // z_vr == -2 or -3: (8-60) (p[-1,y-1] + 2*p[-1,y-2] + p[-1,y-3] + 2) >> 2
                    (p_left(y - 1) + 2 * p_left(y - 2) + p_left(y - 3) + 2) >> 2
                }
            };
            out[at4(x as usize, y as usize)] = v;
        }
    }
}

// §8.3.1.2.7 — Intra_4x4_Horizontal_Down.
fn predict_4x4_hd(s: &Samples4x4, out: &mut [i32; 16]) {
    let tl = s.top_left;
    let t = &s.top;
    let l = &s.left;
    let p_top = |x: i32| -> i32 {
        if x == -1 {
            tl
        } else {
            t[x as usize]
        }
    };
    let p_left = |y: i32| -> i32 {
        if y == -1 {
            tl
        } else {
            l[y as usize]
        }
    };
    for y in 0..4i32 {
        for x in 0..4i32 {
            let z_hd = 2 * y - x;
            let v = match z_hd {
                0 | 2 | 4 | 6 => {
                    // (8-61) (p[-1,y-(x>>1)-1] + p[-1,y-(x>>1)] + 1) >> 1
                    let yp = y - (x >> 1) - 1;
                    (p_left(yp) + p_left(yp + 1) + 1) >> 1
                }
                1 | 3 | 5 => {
                    // (8-62) (p[-1,y-(x>>1)-2] + 2*p[-1,y-(x>>1)-1] + p[-1,y-(x>>1)] + 2) >> 2
                    let yp = y - (x >> 1) - 2;
                    (p_left(yp) + 2 * p_left(yp + 1) + p_left(yp + 2) + 2) >> 2
                }
                -1 => {
                    // (8-63) (p[-1,0] + 2*p[-1,-1] + p[0,-1] + 2) >> 2
                    (p_left(0) + 2 * tl + p_top(0) + 2) >> 2
                }
                _ => {
                    // z_hd == -2 or -3: (8-64) (p[x-1,-1] + 2*p[x-2,-1] + p[x-3,-1] + 2) >> 2
                    (p_top(x - 1) + 2 * p_top(x - 2) + p_top(x - 3) + 2) >> 2
                }
            };
            out[at4(x as usize, y as usize)] = v;
        }
    }
}

// §8.3.1.2.8 — Intra_4x4_Vertical_Left.
fn predict_4x4_vl(s: &Samples4x4, out: &mut [i32; 16]) {
    let p = top_row_4x4(s);
    for y in 0..4i32 {
        for x in 0..4i32 {
            let v = if y == 0 || y == 2 {
                // (8-65) (p[x+(y>>1),-1] + p[x+(y>>1)+1,-1] + 1) >> 1
                let xp = (x + (y >> 1)) as usize;
                (p[xp] + p[xp + 1] + 1) >> 1
            } else {
                // y == 1 or 3: (8-66) (p[x+(y>>1),-1] + 2*p[x+(y>>1)+1,-1] + p[x+(y>>1)+2,-1] + 2) >> 2
                let xp = (x + (y >> 1)) as usize;
                (p[xp] + 2 * p[xp + 1] + p[xp + 2] + 2) >> 2
            };
            out[at4(x as usize, y as usize)] = v;
        }
    }
}

// §8.3.1.2.9 — Intra_4x4_Horizontal_Up.
fn predict_4x4_hu(s: &Samples4x4, out: &mut [i32; 16]) {
    let l = &s.left;
    for y in 0..4i32 {
        for x in 0..4i32 {
            let z_hu = x + 2 * y;
            let v = match z_hu {
                0 | 2 | 4 => {
                    // (8-67) (p[-1,y+(x>>1)] + p[-1,y+(x>>1)+1] + 1) >> 1
                    let yp = (y + (x >> 1)) as usize;
                    (l[yp] + l[yp + 1] + 1) >> 1
                }
                1 | 3 => {
                    // (8-68) (p[-1,y+(x>>1)] + 2*p[-1,y+(x>>1)+1] + p[-1,y+(x>>1)+2] + 2) >> 2
                    let yp = (y + (x >> 1)) as usize;
                    let p2 = if yp + 2 < 4 { l[yp + 2] } else { l[3] };
                    let p1 = if yp + 1 < 4 { l[yp + 1] } else { l[3] };
                    (l[yp] + 2 * p1 + p2 + 2) >> 2
                }
                5 => {
                    // (8-69) (p[-1,2] + 3*p[-1,3] + 2) >> 2
                    (l[2] + 3 * l[3] + 2) >> 2
                }
                _ => {
                    // zHU > 5: (8-70) p[-1,3]
                    l[3]
                }
            };
            out[at4(x as usize, y as usize)] = v;
        }
    }
}

// ---------------------------------------------------------------------------
// §8.3.2 Intra_8x8 prediction
// ---------------------------------------------------------------------------

/// Build a 16-entry array of p'[x, -1] after applying the reference
/// sample filter's substitution rule for x = 8..15 when p[8..=15,-1]
/// is not available.
fn top_row_8x8_raw(s: &Samples8x8) -> [i32; 16] {
    let mut p = [0i32; 16];
    p[0..8].copy_from_slice(&s.top);
    if s.availability.top_right {
        p[8..16].copy_from_slice(&s.top_right);
    } else if s.availability.top {
        // §8.3.2.2 — "When samples p[x, -1], with x = 8..15, are
        // marked as 'not available for Intra_8x8 prediction,' and the
        // sample p[7, -1] is marked as 'available for Intra_8x8
        // prediction,' the sample value of p[7, -1] is substituted".
        for slot in &mut p[8..16] {
            *slot = s.top[7];
        }
    }
    p
}

/// §8.3.2.2.1 — reference sample filter for Intra_8x8. Given raw
/// reference samples, produce the filtered p' used by the prediction
/// process.
pub fn filter_samples_8x8(raw: &Samples8x8, _bit_depth: u32) -> Samples8x8 {
    let raw_top = top_row_8x8_raw(raw);

    // Availability of p[-1, -1] and p[0,-1] and p[-1,0] is needed.
    let tl_avail = raw.availability.top_left;
    let top_avail = raw.availability.top;
    let left_avail = raw.availability.left;

    // Filtered top row p'[x, -1], x = 0..15. Only computed when top
    // samples p[x, -1] with x = 0..15 are available (which — with
    // substitution — is implied by top_avail).
    let mut filt_top = [0i32; 16];
    let mut filt_top_right = [0i32; 8];

    if top_avail {
        // p'[0,-1]: (8-78) with p[-1,-1] available, else (8-79).
        filt_top[0] = if tl_avail {
            (raw.top_left + 2 * raw_top[0] + raw_top[1] + 2) >> 2
        } else {
            (3 * raw_top[0] + raw_top[1] + 2) >> 2
        };
        // p'[x,-1], x = 1..14: (8-80)
        for x in 1..15 {
            filt_top[x] = (raw_top[x - 1] + 2 * raw_top[x] + raw_top[x + 1] + 2) >> 2;
        }
        // p'[15,-1]: (8-81)
        filt_top[15] = (raw_top[14] + 3 * raw_top[15] + 2) >> 2;
    }

    // Split filt_top into top (0..8) and top_right (8..16).
    let mut filt_top_8 = [0i32; 8];
    filt_top_8.copy_from_slice(&filt_top[0..8]);
    filt_top_right.copy_from_slice(&filt_top[8..16]);

    // p'[-1,-1]: derivation depends on availability of p[0,-1] and p[-1,0].
    // Per §8.3.2.2.1 "When the sample p[-1, -1] is marked as 'available'".
    let filt_tl = if tl_avail {
        if !top_avail || !left_avail {
            // One of p[0,-1] / p[-1,0] unavailable.
            if top_avail {
                // p[0,-1] available, p[-1,0] not: (8-82)
                (3 * raw.top_left + raw_top[0] + 2) >> 2
            } else if left_avail {
                // p[-1,0] available, p[0,-1] not: (8-83)
                (3 * raw.top_left + raw.left[0] + 2) >> 2
            } else {
                // Both p[0,-1] and p[-1,0] unavailable.
                // Per spec NOTE: p'[-1,-1] is not used, but assign
                // raw.top_left as a harmless default.
                raw.top_left
            }
        } else {
            // Both p[0,-1] and p[-1,0] available: (8-84)
            (raw_top[0] + 2 * raw.top_left + raw.left[0] + 2) >> 2
        }
    } else {
        raw.top_left
    };

    // Filtered left column p'[-1, y], y = 0..7.
    let mut filt_left = [0i32; 8];
    if left_avail {
        // p'[-1, 0]: (8-85) with p[-1,-1] available, else (8-86).
        filt_left[0] = if tl_avail {
            (raw.top_left + 2 * raw.left[0] + raw.left[1] + 2) >> 2
        } else {
            (3 * raw.left[0] + raw.left[1] + 2) >> 2
        };
        // p'[-1, y], y = 1..6: (8-87) — indexed loop matches spec eq.
        #[allow(clippy::needless_range_loop)] // spec §8.3.2.2.1 eq. (8-87)
        for y in 1..7 {
            filt_left[y] = (raw.left[y - 1] + 2 * raw.left[y] + raw.left[y + 1] + 2) >> 2;
        }
        // p'[-1, 7]: (8-88)
        filt_left[7] = (raw.left[6] + 3 * raw.left[7] + 2) >> 2;
    }

    Samples8x8 {
        top_left: filt_tl,
        top: filt_top_8,
        top_right: filt_top_right,
        left: filt_left,
        availability: raw.availability,
    }
}

/// Build p'[x, -1] for x = 0..=15 from a filtered Samples8x8 (top
/// plus top_right concatenated).
fn filtered_top_16(s: &Samples8x8) -> [i32; 16] {
    let mut p = [0i32; 16];
    p[0..8].copy_from_slice(&s.top);
    p[8..16].copy_from_slice(&s.top_right);
    p
}

/// Predict an 8x8 block. `samples` is assumed to already contain the
/// filtered reference samples (i.e. the caller has run
/// `filter_samples_8x8`). `out` is row-major 8x8 (64 samples).
pub fn predict_8x8(mode: Intra8x8Mode, samples: &Samples8x8, bit_depth: u32, out: &mut [i32; 64]) {
    match mode {
        Intra8x8Mode::Vertical => predict_8x8_vertical(samples, out),
        Intra8x8Mode::Horizontal => predict_8x8_horizontal(samples, out),
        Intra8x8Mode::Dc => predict_8x8_dc(samples, bit_depth, out),
        Intra8x8Mode::DiagonalDownLeft => predict_8x8_ddl(samples, out),
        Intra8x8Mode::DiagonalDownRight => predict_8x8_ddr(samples, out),
        Intra8x8Mode::VerticalRight => predict_8x8_vr(samples, out),
        Intra8x8Mode::HorizontalDown => predict_8x8_hd(samples, out),
        Intra8x8Mode::VerticalLeft => predict_8x8_vl(samples, out),
        Intra8x8Mode::HorizontalUp => predict_8x8_hu(samples, out),
    }
}

#[inline]
fn at8(x: usize, y: usize) -> usize {
    y * 8 + x
}

// §8.3.2.2.2 — Intra_8x8_Vertical. pred[x,y] = p'[x,-1]
fn predict_8x8_vertical(s: &Samples8x8, out: &mut [i32; 64]) {
    for y in 0..8 {
        for x in 0..8 {
            out[at8(x, y)] = s.top[x];
        }
    }
}

// §8.3.2.2.3 — Intra_8x8_Horizontal. pred[x,y] = p'[-1,y]
fn predict_8x8_horizontal(s: &Samples8x8, out: &mut [i32; 64]) {
    for y in 0..8 {
        for x in 0..8 {
            out[at8(x, y)] = s.left[y];
        }
    }
}

// §8.3.2.2.4 — Intra_8x8_DC.
fn predict_8x8_dc(s: &Samples8x8, bit_depth: u32, out: &mut [i32; 64]) {
    let dc = if s.availability.top && s.availability.left {
        // (8-91)
        let sum_top: i32 = s.top.iter().sum();
        let sum_left: i32 = s.left.iter().sum();
        (sum_top + sum_left + 8) >> 4
    } else if s.availability.left {
        // (8-92)
        let sum_left: i32 = s.left.iter().sum();
        (sum_left + 4) >> 3
    } else if s.availability.top {
        // (8-93)
        let sum_top: i32 = s.top.iter().sum();
        (sum_top + 4) >> 3
    } else {
        // (8-94)
        dc_default(bit_depth)
    };
    for sample in out.iter_mut() {
        *sample = dc;
    }
}

// §8.3.2.2.5 — Intra_8x8_Diagonal_Down_Left.
fn predict_8x8_ddl(s: &Samples8x8, out: &mut [i32; 64]) {
    let p = filtered_top_16(s);
    for y in 0..8 {
        for x in 0..8 {
            let v = if x == 7 && y == 7 {
                // (8-95) (p'[14,-1] + 3*p'[15,-1] + 2) >> 2
                (p[14] + 3 * p[15] + 2) >> 2
            } else {
                // (8-96) (p'[x+y,-1] + 2*p'[x+y+1,-1] + p'[x+y+2,-1] + 2) >> 2
                (p[x + y] + 2 * p[x + y + 1] + p[x + y + 2] + 2) >> 2
            };
            out[at8(x, y)] = v;
        }
    }
}

// §8.3.2.2.6 — Intra_8x8_Diagonal_Down_Right.
fn predict_8x8_ddr(s: &Samples8x8, out: &mut [i32; 64]) {
    let tl = s.top_left;
    let t = &s.top;
    let l = &s.left;
    let p_top = |x: i32| -> i32 {
        if x == -1 {
            tl
        } else {
            t[x as usize]
        }
    };
    let p_left = |y: i32| -> i32 {
        if y == -1 {
            tl
        } else {
            l[y as usize]
        }
    };
    for y in 0..8i32 {
        for x in 0..8i32 {
            let v = if x > y {
                // (8-97) (p'[x-y-2,-1] + 2*p'[x-y-1,-1] + p'[x-y,-1] + 2) >> 2
                (p_top(x - y - 2) + 2 * p_top(x - y - 1) + p_top(x - y) + 2) >> 2
            } else if x < y {
                // (8-98) (p'[-1,y-x-2] + 2*p'[-1,y-x-1] + p'[-1,y-x] + 2) >> 2
                (p_left(y - x - 2) + 2 * p_left(y - x - 1) + p_left(y - x) + 2) >> 2
            } else {
                // (8-99) (p'[0,-1] + 2*p'[-1,-1] + p'[-1,0] + 2) >> 2
                (p_top(0) + 2 * tl + p_left(0) + 2) >> 2
            };
            out[at8(x as usize, y as usize)] = v;
        }
    }
}

// §8.3.2.2.7 — Intra_8x8_Vertical_Right.
fn predict_8x8_vr(s: &Samples8x8, out: &mut [i32; 64]) {
    let tl = s.top_left;
    let p = filtered_top_16(s);
    let l = &s.left;
    let p_top = |x: i32| -> i32 {
        if x == -1 {
            tl
        } else {
            p[x as usize]
        }
    };
    let p_left = |y: i32| -> i32 {
        if y == -1 {
            tl
        } else {
            l[y as usize]
        }
    };
    for y in 0..8i32 {
        for x in 0..8i32 {
            let z_vr = 2 * x - y;
            let v = match z_vr {
                0 | 2 | 4 | 6 | 8 | 10 | 12 | 14 => {
                    // (8-100) (p'[x-(y>>1)-1,-1] + p'[x-(y>>1),-1] + 1) >> 1
                    let xp = x - (y >> 1) - 1;
                    (p_top(xp) + p_top(xp + 1) + 1) >> 1
                }
                1 | 3 | 5 | 7 | 9 | 11 | 13 => {
                    // (8-101) (p'[x-(y>>1)-2,-1] + 2*p'[x-(y>>1)-1,-1] + p'[x-(y>>1),-1] + 2) >> 2
                    let xp = x - (y >> 1) - 2;
                    (p_top(xp) + 2 * p_top(xp + 1) + p_top(xp + 2) + 2) >> 2
                }
                -1 => {
                    // (8-102) (p'[-1,0] + 2*p'[-1,-1] + p'[0,-1] + 2) >> 2
                    (p_left(0) + 2 * tl + p_top(0) + 2) >> 2
                }
                _ => {
                    // -2..=-7: (8-103) (p'[-1,y-2*x-1] + 2*p'[-1,y-2*x-2] + p'[-1,y-2*x-3] + 2) >> 2
                    (p_left(y - 2 * x - 1) + 2 * p_left(y - 2 * x - 2) + p_left(y - 2 * x - 3) + 2)
                        >> 2
                }
            };
            out[at8(x as usize, y as usize)] = v;
        }
    }
}

// §8.3.2.2.8 — Intra_8x8_Horizontal_Down.
fn predict_8x8_hd(s: &Samples8x8, out: &mut [i32; 64]) {
    let tl = s.top_left;
    let t = &s.top;
    let l = &s.left;
    let p_top = |x: i32| -> i32 {
        if x == -1 {
            tl
        } else {
            t[x as usize]
        }
    };
    let p_left = |y: i32| -> i32 {
        if y == -1 {
            tl
        } else {
            l[y as usize]
        }
    };
    for y in 0..8i32 {
        for x in 0..8i32 {
            let z_hd = 2 * y - x;
            let v = match z_hd {
                0 | 2 | 4 | 6 | 8 | 10 | 12 | 14 => {
                    // (8-104) (p'[-1,y-(x>>1)-1] + p'[-1,y-(x>>1)] + 1) >> 1
                    let yp = y - (x >> 1) - 1;
                    (p_left(yp) + p_left(yp + 1) + 1) >> 1
                }
                1 | 3 | 5 | 7 | 9 | 11 | 13 => {
                    // (8-105) (p'[-1,y-(x>>1)-2] + 2*p'[-1,y-(x>>1)-1] + p'[-1,y-(x>>1)] + 2) >> 2
                    let yp = y - (x >> 1) - 2;
                    (p_left(yp) + 2 * p_left(yp + 1) + p_left(yp + 2) + 2) >> 2
                }
                -1 => {
                    // (8-106) (p'[-1,0] + 2*p'[-1,-1] + p'[0,-1] + 2) >> 2
                    (p_left(0) + 2 * tl + p_top(0) + 2) >> 2
                }
                _ => {
                    // -2..=-7: (8-107) (p'[x-2*y-1,-1] + 2*p'[x-2*y-2,-1] + p'[x-2*y-3,-1] + 2) >> 2
                    (p_top(x - 2 * y - 1) + 2 * p_top(x - 2 * y - 2) + p_top(x - 2 * y - 3) + 2)
                        >> 2
                }
            };
            out[at8(x as usize, y as usize)] = v;
        }
    }
}

// §8.3.2.2.9 — Intra_8x8_Vertical_Left.
fn predict_8x8_vl(s: &Samples8x8, out: &mut [i32; 64]) {
    let p = filtered_top_16(s);
    for y in 0..8 {
        for x in 0..8 {
            let v = if y == 0 || y == 2 || y == 4 || y == 6 {
                // (8-108) (p'[x+(y>>1),-1] + p'[x+(y>>1)+1,-1] + 1) >> 1
                let xp = x + (y >> 1);
                (p[xp] + p[xp + 1] + 1) >> 1
            } else {
                // (8-109) (p'[x+(y>>1),-1] + 2*p'[x+(y>>1)+1,-1] + p'[x+(y>>1)+2,-1] + 2) >> 2
                let xp = x + (y >> 1);
                (p[xp] + 2 * p[xp + 1] + p[xp + 2] + 2) >> 2
            };
            out[at8(x, y)] = v;
        }
    }
}

// §8.3.2.2.10 — Intra_8x8_Horizontal_Up.
fn predict_8x8_hu(s: &Samples8x8, out: &mut [i32; 64]) {
    let l = &s.left;
    for y in 0..8i32 {
        for x in 0..8i32 {
            let z_hu = x + 2 * y;
            let v = match z_hu {
                0 | 2 | 4 | 6 | 8 | 10 | 12 => {
                    // (8-110) (p'[-1,y+(x>>1)] + p'[-1,y+(x>>1)+1] + 1) >> 1
                    let yp = (y + (x >> 1)) as usize;
                    (l[yp] + l[yp + 1] + 1) >> 1
                }
                1 | 3 | 5 | 7 | 9 | 11 => {
                    // (8-111) (p'[-1,y+(x>>1)] + 2*p'[-1,y+(x>>1)+1] + p'[-1,y+(x>>1)+2] + 2) >> 2
                    let yp = (y + (x >> 1)) as usize;
                    let p1 = if yp + 1 < 8 { l[yp + 1] } else { l[7] };
                    let p2 = if yp + 2 < 8 { l[yp + 2] } else { l[7] };
                    (l[yp] + 2 * p1 + p2 + 2) >> 2
                }
                13 => {
                    // (8-112) (p'[-1,6] + 3*p'[-1,7] + 2) >> 2
                    (l[6] + 3 * l[7] + 2) >> 2
                }
                _ => {
                    // z_hu > 13: (8-113) p'[-1,7]
                    l[7]
                }
            };
            out[at8(x as usize, y as usize)] = v;
        }
    }
}

// ---------------------------------------------------------------------------
// §8.3.3 Intra_16x16 prediction
// ---------------------------------------------------------------------------

/// Predict a 16x16 block. `out` is row-major 16x16 (256 samples).
pub fn predict_16x16(
    mode: Intra16x16Mode,
    samples: &Samples16x16,
    bit_depth: u32,
    out: &mut [i32; 256],
) {
    match mode {
        Intra16x16Mode::Vertical => predict_16x16_vertical(samples, out),
        Intra16x16Mode::Horizontal => predict_16x16_horizontal(samples, out),
        Intra16x16Mode::Dc => predict_16x16_dc(samples, bit_depth, out),
        Intra16x16Mode::Plane => predict_16x16_plane(samples, bit_depth, out),
    }
}

#[inline]
fn at16(x: usize, y: usize) -> usize {
    y * 16 + x
}

// §8.3.3.1 — Intra_16x16_Vertical. (8-116) pred[x,y] = p[x,-1]
fn predict_16x16_vertical(s: &Samples16x16, out: &mut [i32; 256]) {
    for y in 0..16 {
        for x in 0..16 {
            out[at16(x, y)] = s.top[x];
        }
    }
}

// §8.3.3.2 — Intra_16x16_Horizontal. (8-117) pred[x,y] = p[-1,y]
fn predict_16x16_horizontal(s: &Samples16x16, out: &mut [i32; 256]) {
    for y in 0..16 {
        for x in 0..16 {
            out[at16(x, y)] = s.left[y];
        }
    }
}

// §8.3.3.3 — Intra_16x16_DC.
fn predict_16x16_dc(s: &Samples16x16, bit_depth: u32, out: &mut [i32; 256]) {
    let dc = if s.availability.top && s.availability.left {
        // (8-118)
        let sum_top: i32 = s.top.iter().sum();
        let sum_left: i32 = s.left.iter().sum();
        (sum_top + sum_left + 16) >> 5
    } else if s.availability.left {
        // (8-119)
        let sum_left: i32 = s.left.iter().sum();
        (sum_left + 8) >> 4
    } else if s.availability.top {
        // (8-120)
        let sum_top: i32 = s.top.iter().sum();
        (sum_top + 8) >> 4
    } else {
        // (8-121)
        dc_default(bit_depth)
    };
    for sample in out.iter_mut() {
        *sample = dc;
    }
}

// §8.3.3.4 — Intra_16x16_Plane.
fn predict_16x16_plane(s: &Samples16x16, bit_depth: u32, out: &mut [i32; 256]) {
    // (8-126) H = sum over x'=0..=7 of (x'+1) * (p[8+x', -1] - p[6-x', -1])
    // (8-127) V = sum over y'=0..=7 of (y'+1) * (p[-1, 8+y'] - p[-1, 6-y'])
    //
    // Note that p[7,-1] for x'=-1 is not referenced; the loop starts
    // at x'=0 which uses p[8,-1] and p[6,-1]. When x'=7 we use p[15,-1]
    // and p[-1,-1]. When y'=7 we use p[-1,15] and p[-1,-1].
    let p_top = |x: i32| -> i32 {
        if x == -1 {
            s.top_left
        } else {
            s.top[x as usize]
        }
    };
    let p_left = |y: i32| -> i32 {
        if y == -1 {
            s.top_left
        } else {
            s.left[y as usize]
        }
    };

    let mut h: i32 = 0;
    for xp in 0..8i32 {
        h += (xp + 1) * (p_top(8 + xp) - p_top(6 - xp));
    }
    let mut v: i32 = 0;
    for yp in 0..8i32 {
        v += (yp + 1) * (p_left(8 + yp) - p_left(6 - yp));
    }

    // (8-123) a = 16 * (p[-1,15] + p[15,-1])
    let a = 16 * (s.left[15] + s.top[15]);
    // (8-124) b = (5 * H + 32) >> 6
    let b = (5 * h + 32) >> 6;
    // (8-125) c = (5 * V + 32) >> 6
    let c = (5 * v + 32) >> 6;

    for y in 0..16i32 {
        for x in 0..16i32 {
            // (8-122) pred[x,y] = Clip1_Y((a + b*(x-7) + c*(y-7) + 16) >> 5)
            let raw = (a + b * (x - 7) + c * (y - 7) + 16) >> 5;
            out[at16(x as usize, y as usize)] = clip1(raw, bit_depth);
        }
    }
}

// ---------------------------------------------------------------------------
// §8.3.4 Intra chroma prediction
// ---------------------------------------------------------------------------

/// Predict a chroma block. `out` must be exactly `8 * chroma_type.height()`
/// samples (64 for 4:2:0, 128 for 4:2:2), row-major width-first.
pub fn predict_chroma(
    mode: IntraChromaMode,
    samples: &SamplesChroma,
    chroma_type: ChromaArrayType,
    bit_depth: u32,
    out: &mut [i32],
) {
    let w = chroma_type.width();
    let h = chroma_type.height();
    assert_eq!(out.len(), w * h, "chroma output buffer size mismatch");
    assert_eq!(samples.top.len(), w, "chroma top row length mismatch");
    assert_eq!(samples.left.len(), h, "chroma left column length mismatch");

    match mode {
        IntraChromaMode::Dc => predict_chroma_dc(samples, chroma_type, bit_depth, out),
        IntraChromaMode::Horizontal => predict_chroma_horizontal(samples, chroma_type, out),
        IntraChromaMode::Vertical => predict_chroma_vertical(samples, chroma_type, out),
        IntraChromaMode::Plane => predict_chroma_plane(samples, chroma_type, bit_depth, out),
    }
}

// §8.3.4.1 — Intra_Chroma_DC.
// Chroma is partitioned into 4x4 sub-blocks for DC derivation.
// For 4:2:0 (8x8): 4 sub-blocks at (xO,yO) in {(0,0),(4,0),(0,4),(4,4)}.
// For 4:2:2 (8x16): 8 sub-blocks at (xO,yO) in
//   {(0,0),(4,0),(0,4),(4,4),(0,8),(4,8),(0,12),(4,12)}.
// The spec's decision tree by (xO, yO) gives different DC formulas.
fn predict_chroma_dc(
    s: &SamplesChroma,
    chroma_type: ChromaArrayType,
    bit_depth: u32,
    out: &mut [i32],
) {
    let w = chroma_type.width();
    let h = chroma_type.height();
    let default_dc = dc_default(bit_depth);

    // Iterate over 4x4 sub-blocks, indexed by their top-left (xO, yO).
    let mut y_o = 0;
    while y_o < h {
        let mut x_o = 0;
        while x_o < w {
            let dc = chroma_dc_subblock(s, x_o, y_o, w, h, default_dc);
            // Fill the 4x4 sub-block with `dc`.
            for yy in 0..4 {
                for xx in 0..4 {
                    out[(y_o + yy) * w + (x_o + xx)] = dc;
                }
            }
            x_o += 4;
        }
        y_o += 4;
    }
}

/// Compute DC for a single 4x4 chroma sub-block at (xO, yO).
fn chroma_dc_subblock(
    s: &SamplesChroma,
    x_o: usize,
    y_o: usize,
    _w: usize,
    _h: usize,
    default_dc: i32,
) -> i32 {
    // Availability of the four relevant reference samples:
    //   top samples p[xO..xO+3, -1]
    //   left samples p[-1, yO..yO+3]
    let top_avail = s.availability.top;
    let left_avail = s.availability.left;

    let sum_top = || -> i32 { s.top[x_o] + s.top[x_o + 1] + s.top[x_o + 2] + s.top[x_o + 3] };
    let sum_left = || -> i32 { s.left[y_o] + s.left[y_o + 1] + s.left[y_o + 2] + s.left[y_o + 3] };

    if x_o == 0 && y_o == 0 {
        // (xO, yO) == (0, 0): use both top and left. §8.3.4.1
        if top_avail && left_avail {
            // (8-132)
            (sum_top() + sum_left() + 4) >> 3
        } else if top_avail {
            // (8-133) uses only top  -- wait, (8-133) is left-only.
            // Re-read: (8-133) uses p[-1, y'+yO], y'=0..3. That's the
            // left column. (8-134) uses p[x'+xO, -1]. That's the top row.
            // (8-134) top-only: (sum_top + 2) >> 2
            (sum_top() + 2) >> 2
        } else if left_avail {
            // (8-133) left-only: (sum_left + 2) >> 2
            (sum_left() + 2) >> 2
        } else {
            // (8-135) neither: (1 << (BitDepth - 1))
            default_dc
        }
    } else if x_o > 0 && y_o == 0 {
        // Top row, not leftmost: prefer top samples.
        // (8-136) top available: (sum_top + 2) >> 2
        // (8-137) top not available but left available: (sum_left + 2) >> 2
        // (8-138) neither: default
        if top_avail {
            (sum_top() + 2) >> 2
        } else if left_avail {
            (sum_left() + 2) >> 2
        } else {
            default_dc
        }
    } else if x_o == 0 && y_o > 0 {
        // Left column, not top: prefer left samples.
        // (8-139) left available: (sum_left + 2) >> 2
        // (8-140) left not available but top available: (sum_top + 2) >> 2
        // (8-141) neither: default
        if left_avail {
            (sum_left() + 2) >> 2
        } else if top_avail {
            (sum_top() + 2) >> 2
        } else {
            default_dc
        }
    } else {
        // x_o > 0 && y_o > 0: the spec folds this into the
        // "otherwise" branch of the xO>0, yO>0 case which uses
        // BOTH when available. The standard treatment (per a close
        // reading of §8.3.4.1's ordered cases) mirrors (xO, yO) == (0,0):
        // use both top and left if available, else whichever is
        // available, else default.
        if top_avail && left_avail {
            (sum_top() + sum_left() + 4) >> 3
        } else if top_avail {
            (sum_top() + 2) >> 2
        } else if left_avail {
            (sum_left() + 2) >> 2
        } else {
            default_dc
        }
    }
}

// §8.3.4.2 — Intra_Chroma_Horizontal. (8-142) pred[x,y] = p[-1, y]
fn predict_chroma_horizontal(s: &SamplesChroma, chroma_type: ChromaArrayType, out: &mut [i32]) {
    let w = chroma_type.width();
    let h = chroma_type.height();
    for y in 0..h {
        for x in 0..w {
            out[y * w + x] = s.left[y];
        }
    }
}

// §8.3.4.3 — Intra_Chroma_Vertical. (8-143) pred[x,y] = p[x, -1]
fn predict_chroma_vertical(s: &SamplesChroma, chroma_type: ChromaArrayType, out: &mut [i32]) {
    let w = chroma_type.width();
    let h = chroma_type.height();
    for y in 0..h {
        for x in 0..w {
            out[y * w + x] = s.top[x];
        }
    }
}

// §8.3.4.4 — Intra_Chroma_Plane.
fn predict_chroma_plane(
    s: &SamplesChroma,
    chroma_type: ChromaArrayType,
    bit_depth: u32,
    out: &mut [i32],
) {
    let w = chroma_type.width();
    let h = chroma_type.height();
    let is_yuv422 = chroma_type == ChromaArrayType::Yuv422;
    // xCF = (ChromaArrayType == 3) ? 4 : 0 — we never get here when ==3.
    let x_cf: i32 = 0;
    // yCF = (ChromaArrayType != 1) ? 4 : 0
    let y_cf: i32 = if is_yuv422 { 4 } else { 0 };

    let p_top = |x: i32| -> i32 {
        if x == -1 {
            s.top_left
        } else {
            s.top[x as usize]
        }
    };
    let p_left = |y: i32| -> i32 {
        if y == -1 {
            s.top_left
        } else {
            s.left[y as usize]
        }
    };

    // (8-148) H = sum over x'=0..=3+xCF of (x'+1) * (p[4+xCF+x', -1] - p[2+xCF-x', -1])
    let mut h_sum: i32 = 0;
    for xp in 0..=(3 + x_cf) {
        h_sum += (xp + 1) * (p_top(4 + x_cf + xp) - p_top(2 + x_cf - xp));
    }
    // (8-149) V = sum over y'=0..=3+yCF of (y'+1) * (p[-1, 4+yCF+y'] - p[-1, 2+yCF-y'])
    let mut v_sum: i32 = 0;
    for yp in 0..=(3 + y_cf) {
        v_sum += (yp + 1) * (p_left(4 + y_cf + yp) - p_left(2 + y_cf - yp));
    }

    // (8-145) a = 16 * (p[-1, MbHeightC - 1] + p[MbWidthC - 1, -1])
    let a = 16 * (s.left[h - 1] + s.top[w - 1]);
    // (8-146) b = ((34 - 29 * (ChromaArrayType == 3)) * H + 32) >> 6
    //         For ChromaArrayType != 3, coefficient is 34.
    let b = (34 * h_sum + 32) >> 6;
    // (8-147) c = ((34 - 29 * (ChromaArrayType != 1)) * V + 32) >> 6
    //         For 4:2:0 (== 1): coefficient is 34.
    //         For 4:2:2 (!= 1, != 3 since we're not 4:4:4 here): 34-29 = 5.
    let c_coef = if is_yuv422 { 5 } else { 34 };
    let c = (c_coef * v_sum + 32) >> 6;

    // (8-144) pred[x,y] = Clip1_C((a + b*(x-3-xCF) + c*(y-3-yCF) + 16) >> 5)
    for y in 0..h {
        for x in 0..w {
            let xi = x as i32;
            let yi = y as i32;
            let raw = (a + b * (xi - 3 - x_cf) + c * (yi - 3 - y_cf) + 16) >> 5;
            out[y * w + x] = clip1(raw, bit_depth);
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn all_avail() -> Neighbour4x4Availability {
        Neighbour4x4Availability {
            top_left: true,
            top: true,
            top_right: true,
            left: true,
        }
    }

    fn no_avail() -> Neighbour4x4Availability {
        Neighbour4x4Availability::default()
    }

    // ----- Mode enum round-trips -----

    #[test]
    fn intra4x4_mode_index_roundtrip() {
        for i in 0..=8u8 {
            let m = Intra4x4Mode::from_index(i).unwrap();
            assert_eq!(m.as_index(), i);
        }
        assert!(Intra4x4Mode::from_index(9).is_none());
    }

    #[test]
    fn intra8x8_mode_index_roundtrip() {
        for i in 0..=8u8 {
            let m = Intra8x8Mode::from_index(i).unwrap();
            assert_eq!(m.as_index(), i);
        }
    }

    #[test]
    fn intra16x16_mode_index_roundtrip() {
        for i in 0..=3u8 {
            let m = Intra16x16Mode::from_index(i).unwrap();
            assert_eq!(m.as_index(), i);
        }
        assert!(Intra16x16Mode::from_index(4).is_none());
    }

    #[test]
    fn intra_chroma_mode_index_roundtrip() {
        for i in 0..=3u8 {
            let m = IntraChromaMode::from_index(i).unwrap();
            assert_eq!(m.as_index(), i);
        }
    }

    // ----- Intra 4x4 tests -----

    fn mk_4x4(top: [i32; 4], top_right: [i32; 4], left: [i32; 4], tl: i32) -> Samples4x4 {
        Samples4x4 {
            top_left: tl,
            top,
            top_right,
            left,
            availability: all_avail(),
        }
    }

    #[test]
    fn intra_4x4_dc_all_neighbours() {
        // top=[10,20,30,40], left=[5,15,25,35] → (10+20+30+40+5+15+25+35+4)/8 = 184/8 = 23
        let s = mk_4x4([10, 20, 30, 40], [50, 60, 70, 80], [5, 15, 25, 35], 0);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::Dc, &s, 8, &mut out);
        for v in out.iter() {
            assert_eq!(*v, 23);
        }
    }

    #[test]
    fn intra_4x4_dc_no_neighbours() {
        let mut s = mk_4x4([0; 4], [0; 4], [0; 4], 0);
        s.availability = no_avail();
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::Dc, &s, 8, &mut out);
        for v in out.iter() {
            assert_eq!(*v, 128);
        }
    }

    #[test]
    fn intra_4x4_dc_only_left() {
        // left = [4,8,12,16] → (4+8+12+16+2)/4 = 42/4 = 10
        let mut s = mk_4x4([0; 4], [0; 4], [4, 8, 12, 16], 0);
        s.availability = Neighbour4x4Availability {
            top_left: false,
            top: false,
            top_right: false,
            left: true,
        };
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::Dc, &s, 8, &mut out);
        for v in out.iter() {
            assert_eq!(*v, 10);
        }
    }

    #[test]
    fn intra_4x4_dc_only_top() {
        // top = [1,3,5,7] → (1+3+5+7+2)/4 = 18/4 = 4
        let mut s = mk_4x4([1, 3, 5, 7], [0; 4], [0; 4], 0);
        s.availability = Neighbour4x4Availability {
            top_left: false,
            top: true,
            top_right: false,
            left: false,
        };
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::Dc, &s, 8, &mut out);
        for v in out.iter() {
            assert_eq!(*v, 4);
        }
    }

    #[test]
    fn intra_4x4_vertical() {
        let s = mk_4x4([11, 22, 33, 44], [0; 4], [0; 4], 0);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::Vertical, &s, 8, &mut out);
        for y in 0..4 {
            for x in 0..4 {
                assert_eq!(out[at4(x, y)], s.top[x]);
            }
        }
    }

    #[test]
    fn intra_4x4_horizontal() {
        let s = mk_4x4([0; 4], [0; 4], [7, 14, 21, 28], 0);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::Horizontal, &s, 8, &mut out);
        for y in 0..4 {
            for x in 0..4 {
                assert_eq!(out[at4(x, y)], s.left[y]);
            }
        }
    }

    #[test]
    fn intra_4x4_ddl_hand() {
        // top = [0,1,2,3], top_right = [4,5,6,7]
        // p = [0,1,2,3,4,5,6,7]
        // pred[0,0] = (p[0]+2*p[1]+p[2]+2)>>2 = (0+2+2+2)>>2 = 6>>2 = 1
        // pred[3,3] = (p[6]+3*p[7]+2)>>2 = (6+21+2)>>2 = 29>>2 = 7
        let s = mk_4x4([0, 1, 2, 3], [4, 5, 6, 7], [0; 4], 0);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::DiagonalDownLeft, &s, 8, &mut out);
        assert_eq!(out[at4(0, 0)], 1);
        assert_eq!(out[at4(3, 3)], 7);
        // pred[1,0] = (p[1]+2*p[2]+p[3]+2)>>2 = (1+4+3+2)>>2 = 10>>2 = 2
        assert_eq!(out[at4(1, 0)], 2);
        // pred[0,1] same as pred[1,0]: (p[1]+2*p[2]+p[3]+2)>>2 = 2
        assert_eq!(out[at4(0, 1)], 2);
    }

    #[test]
    fn intra_4x4_ddl_top_right_substitution() {
        // When top_right is not available but top is, replicate p[3,-1].
        // top = [10,20,30,40], top_right effectively = [40,40,40,40].
        // pred[3,0] = (p[3]+2*p[4]+p[5]+2)>>2 = (40+80+40+2)>>2 = 162>>2 = 40
        let mut s = mk_4x4([10, 20, 30, 40], [99; 4], [0; 4], 0);
        s.availability.top_right = false;
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::DiagonalDownLeft, &s, 8, &mut out);
        assert_eq!(out[at4(3, 0)], 40);
    }

    #[test]
    fn intra_4x4_ddr_hand() {
        // top=[10,20,30,40], left=[50,60,70,80], tl=100
        // pred[0,0] = (p[0,-1] + 2*p[-1,-1] + p[-1,0] + 2)>>2 = (10+200+50+2)>>2 = 262>>2 = 65
        // pred[1,0] (x>y): (p[-1,-1]+2*p[0,-1]+p[1,-1]+2)>>2 = (100+20+20+2)>>2
        //  = (100+2*10+20+2)>>2... wait: p[x-y-2,-1] with x=1,y=0 = p[-1,-1]=tl=100;
        //  p[x-y-1,-1] = p[0,-1]=10; p[x-y,-1]=p[1,-1]=20 → (100+20+20+2)>>2 = 142>>2 = 35
        let s = mk_4x4([10, 20, 30, 40], [0; 4], [50, 60, 70, 80], 100);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::DiagonalDownRight, &s, 8, &mut out);
        assert_eq!(out[at4(0, 0)], 65);
        assert_eq!(out[at4(1, 0)], 35);
        // pred[0,1] (x<y): (p[-1,y-x-2]+2*p[-1,y-x-1]+p[-1,y-x]+2)>>2
        //  y-x-2=-1 → tl=100; y-x-1=0 → left[0]=50; y-x=1 → left[1]=60
        //  (100+100+60+2)>>2 = 262>>2 = 65
        assert_eq!(out[at4(0, 1)], 65);
    }

    #[test]
    fn intra_4x4_vr_hand() {
        // top=[10,20,30,40], tl=50, left=[60,70,80,90]
        // pred[0,0]: zVR=0, (p[-1,-1]+p[0,-1]+1)>>1 = (50+10+1)>>1 = 61>>1 = 30
        let s = mk_4x4([10, 20, 30, 40], [0; 4], [60, 70, 80, 90], 50);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::VerticalRight, &s, 8, &mut out);
        assert_eq!(out[at4(0, 0)], 30);
        // pred[0,1]: x=0,y=1, zVR=-1 → (p[-1,0]+2*p[-1,-1]+p[0,-1]+2)>>2 = (60+100+10+2)>>2 = 172>>2 = 43
        assert_eq!(out[at4(0, 1)], 43);
    }

    #[test]
    fn intra_4x4_hd_hand() {
        let s = mk_4x4([10, 20, 30, 40], [0; 4], [60, 70, 80, 90], 50);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::HorizontalDown, &s, 8, &mut out);
        // pred[0,0]: zHD=0 → (p[-1,-1]+p[-1,0]+1)>>1 = (50+60+1)>>1 = 55
        assert_eq!(out[at4(0, 0)], 55);
        // pred[1,0]: zHD=-1 → (p[-1,0]+2*p[-1,-1]+p[0,-1]+2)>>2 = (60+100+10+2)>>2 = 43
        assert_eq!(out[at4(1, 0)], 43);
    }

    #[test]
    fn intra_4x4_vl_hand() {
        // top=[10,20,30,40], top_right=[50,60,70,80]
        // pred[0,0]: y=0 → (p[0,-1]+p[1,-1]+1)>>1 = (10+20+1)>>1 = 31>>1 = 15
        // pred[0,1]: y=1 → (p[0,-1]+2*p[1,-1]+p[2,-1]+2)>>2 = (10+40+30+2)>>2 = 82>>2 = 20
        let s = mk_4x4([10, 20, 30, 40], [50, 60, 70, 80], [0; 4], 0);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::VerticalLeft, &s, 8, &mut out);
        assert_eq!(out[at4(0, 0)], 15);
        assert_eq!(out[at4(0, 1)], 20);
    }

    #[test]
    fn intra_4x4_hu_hand() {
        // left = [10, 20, 30, 40]
        // pred[0,0]: zHU=0 → (p[-1,0]+p[-1,1]+1)>>1 = (10+20+1)>>1 = 15
        // pred[1,0]: zHU=1 → (p[-1,0]+2*p[-1,1]+p[-1,2]+2)>>2 = (10+40+30+2)>>2 = 20
        // pred[3,3]: zHU=9 >5 → p[-1,3] = 40
        let s = mk_4x4([0; 4], [0; 4], [10, 20, 30, 40], 0);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::HorizontalUp, &s, 8, &mut out);
        assert_eq!(out[at4(0, 0)], 15);
        assert_eq!(out[at4(1, 0)], 20);
        assert_eq!(out[at4(3, 3)], 40);
        // pred[1,2]: zHU=5 → (p[-1,2]+3*p[-1,3]+2)>>2 = (30+120+2)>>2 = 152>>2 = 38
        assert_eq!(out[at4(1, 2)], 38);
    }

    // ---- Additional systematic tests covering every 4x4 mode with a
    //      known reference pattern, verifying every output sample
    //      against hand-derived spec equations. ----

    #[test]
    fn intra_4x4_ddl_all_samples() {
        // §8.3.1.2.4 eqs. 8-52 / 8-53.
        // top + top_right = p[0..=7, -1].
        // Using p = [8,9,10,11,12,13,14,15] (ascending by 1).
        let s = mk_4x4([8, 9, 10, 11], [12, 13, 14, 15], [0; 4], 0);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::DiagonalDownLeft, &s, 8, &mut out);
        // pred[x,y] = (p[x+y] + 2*p[x+y+1] + p[x+y+2] + 2) >> 2 (eq. 8-53)
        //   except pred[3,3] = (p[6] + 3*p[7] + 2) >> 2 (eq. 8-52).
        let p = [8, 9, 10, 11, 12, 13, 14, 15];
        for y in 0..4 {
            for x in 0..4 {
                let expected = if x == 3 && y == 3 {
                    (p[6] + 3 * p[7] + 2) >> 2
                } else {
                    (p[x + y] + 2 * p[x + y + 1] + p[x + y + 2] + 2) >> 2
                };
                assert_eq!(out[at4(x, y)], expected, "DDL mismatch at ({}, {})", x, y);
            }
        }
    }

    #[test]
    fn intra_4x4_ddr_all_samples() {
        // §8.3.1.2.5 eqs. 8-54..8-56.
        // top=[10,20,30,40], left=[50,60,70,80], tl=5.
        let s = mk_4x4([10, 20, 30, 40], [0; 4], [50, 60, 70, 80], 5);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::DiagonalDownRight, &s, 8, &mut out);

        // Helper to read p[x,-1] or p[-1,y], index -1 = top_left.
        let p_top = [10, 20, 30, 40];
        let p_left = [50, 60, 70, 80];
        let tl = 5;
        let fetch_top = |x: i32| -> i32 {
            if x == -1 {
                tl
            } else {
                p_top[x as usize]
            }
        };
        let fetch_left = |y: i32| -> i32 {
            if y == -1 {
                tl
            } else {
                p_left[y as usize]
            }
        };

        for y in 0..4i32 {
            for x in 0..4i32 {
                let expected = if x > y {
                    // eq. 8-54
                    (fetch_top(x - y - 2) + 2 * fetch_top(x - y - 1) + fetch_top(x - y) + 2) >> 2
                } else if x < y {
                    // eq. 8-55
                    (fetch_left(y - x - 2) + 2 * fetch_left(y - x - 1) + fetch_left(y - x) + 2) >> 2
                } else {
                    // eq. 8-56
                    (fetch_top(0) + 2 * tl + fetch_left(0) + 2) >> 2
                };
                assert_eq!(
                    out[at4(x as usize, y as usize)],
                    expected,
                    "DDR mismatch at ({}, {})",
                    x,
                    y
                );
            }
        }
    }

    #[test]
    fn intra_4x4_vr_all_samples() {
        // §8.3.1.2.6 eqs. 8-57..8-60.
        // Distinct top/left to stress every zVR branch.
        let s = mk_4x4([100, 110, 120, 130], [0; 4], [50, 60, 70, 80], 5);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::VerticalRight, &s, 8, &mut out);

        let tl = 5;
        let top = [100, 110, 120, 130];
        let left = [50, 60, 70, 80];
        let p_top = |x: i32| -> i32 {
            if x == -1 {
                tl
            } else {
                top[x as usize]
            }
        };
        let p_left = |y: i32| -> i32 {
            if y == -1 {
                tl
            } else {
                left[y as usize]
            }
        };

        for y in 0..4i32 {
            for x in 0..4i32 {
                let z = 2 * x - y;
                let expected = match z {
                    0 | 2 | 4 | 6 => {
                        let xp = x - (y >> 1) - 1;
                        (p_top(xp) + p_top(xp + 1) + 1) >> 1
                    }
                    1 | 3 | 5 => {
                        let xp = x - (y >> 1) - 2;
                        (p_top(xp) + 2 * p_top(xp + 1) + p_top(xp + 2) + 2) >> 2
                    }
                    -1 => (p_left(0) + 2 * tl + p_top(0) + 2) >> 2,
                    _ => {
                        // z=-2 or -3: eq. 8-60
                        (p_left(y - 1) + 2 * p_left(y - 2) + p_left(y - 3) + 2) >> 2
                    }
                };
                assert_eq!(
                    out[at4(x as usize, y as usize)],
                    expected,
                    "VR mismatch at ({}, {})",
                    x,
                    y
                );
            }
        }
    }

    #[test]
    fn intra_4x4_hd_all_samples() {
        // §8.3.1.2.7 eqs. 8-61..8-64. Mirror of VR along the diagonal.
        let s = mk_4x4([100, 110, 120, 130], [0; 4], [50, 60, 70, 80], 5);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::HorizontalDown, &s, 8, &mut out);

        let tl = 5;
        let top = [100, 110, 120, 130];
        let left = [50, 60, 70, 80];
        let p_top = |x: i32| -> i32 {
            if x == -1 {
                tl
            } else {
                top[x as usize]
            }
        };
        let p_left = |y: i32| -> i32 {
            if y == -1 {
                tl
            } else {
                left[y as usize]
            }
        };

        for y in 0..4i32 {
            for x in 0..4i32 {
                let z = 2 * y - x;
                let expected = match z {
                    0 | 2 | 4 | 6 => {
                        let yp = y - (x >> 1) - 1;
                        (p_left(yp) + p_left(yp + 1) + 1) >> 1
                    }
                    1 | 3 | 5 => {
                        let yp = y - (x >> 1) - 2;
                        (p_left(yp) + 2 * p_left(yp + 1) + p_left(yp + 2) + 2) >> 2
                    }
                    -1 => (p_left(0) + 2 * tl + p_top(0) + 2) >> 2,
                    _ => {
                        // z=-2 or -3: eq. 8-64
                        (p_top(x - 1) + 2 * p_top(x - 2) + p_top(x - 3) + 2) >> 2
                    }
                };
                assert_eq!(
                    out[at4(x as usize, y as usize)],
                    expected,
                    "HD mismatch at ({}, {})",
                    x,
                    y
                );
            }
        }
    }

    #[test]
    fn intra_4x4_vl_all_samples() {
        // §8.3.1.2.8 eqs. 8-65 / 8-66.
        // p[0..=7] built from top+top_right.
        let s = mk_4x4([8, 9, 10, 11], [12, 13, 14, 15], [0; 4], 0);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::VerticalLeft, &s, 8, &mut out);
        let p = [8, 9, 10, 11, 12, 13, 14, 15];

        for y in 0..4i32 {
            for x in 0..4i32 {
                let xp = (x + (y >> 1)) as usize;
                let expected = if y == 0 || y == 2 {
                    (p[xp] + p[xp + 1] + 1) >> 1
                } else {
                    (p[xp] + 2 * p[xp + 1] + p[xp + 2] + 2) >> 2
                };
                assert_eq!(
                    out[at4(x as usize, y as usize)],
                    expected,
                    "VL mismatch at ({}, {})",
                    x,
                    y
                );
            }
        }
    }

    #[test]
    fn intra_4x4_hu_all_samples() {
        // §8.3.1.2.9 eqs. 8-67..8-70.
        let s = mk_4x4([0; 4], [0; 4], [50, 60, 70, 80], 0);
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::HorizontalUp, &s, 8, &mut out);
        let l = [50, 60, 70, 80];
        for y in 0..4i32 {
            for x in 0..4i32 {
                let z_hu = x + 2 * y;
                let expected = match z_hu {
                    0 | 2 | 4 => {
                        let yp = (y + (x >> 1)) as usize;
                        (l[yp] + l[yp + 1] + 1) >> 1
                    }
                    1 | 3 => {
                        let yp = (y + (x >> 1)) as usize;
                        // Clamp indices to 3 for the "out of bounds"
                        // positions the spec says are equal to l[3]
                        // via the extrapolation rule (Intra_4x4 only
                        // uses y=0..=3 left samples; index 4+ falls
                        // back to p[-1,3]).
                        let p1 = if yp + 1 < 4 { l[yp + 1] } else { l[3] };
                        let p2 = if yp + 2 < 4 { l[yp + 2] } else { l[3] };
                        (l[yp] + 2 * p1 + p2 + 2) >> 2
                    }
                    5 => (l[2] + 3 * l[3] + 2) >> 2,
                    _ => l[3],
                };
                assert_eq!(
                    out[at4(x as usize, y as usize)],
                    expected,
                    "HU mismatch at ({}, {})",
                    x,
                    y
                );
            }
        }
    }

    #[test]
    fn intra_4x4_ddl_top_row_substitution_applied() {
        // §8.3.1.2 substitution rule: when p[x,-1] for x=4..7 is not
        // available but p[3,-1] is, substitute p[3,-1].
        // With top=[10,10,10,10] and no top_right, p[4..7] must become 10
        // each, making every DDL sample equal to 10.
        let mut s = mk_4x4([10, 10, 10, 10], [99; 4], [0; 4], 0);
        s.availability.top_right = false;
        let mut out = [0i32; 16];
        predict_4x4(Intra4x4Mode::DiagonalDownLeft, &s, 8, &mut out);
        for v in out.iter() {
            assert_eq!(*v, 10);
        }
    }

    // ----- Intra 8x8 tests -----

    fn mk_8x8(top: [i32; 8], top_right: [i32; 8], left: [i32; 8], tl: i32) -> Samples8x8 {
        Samples8x8 {
            top_left: tl,
            top,
            top_right,
            left,
            availability: all_avail(),
        }
    }

    #[test]
    fn intra_8x8_filter_basic() {
        // All 100s -> filter should still produce 100s.
        let raw = mk_8x8([100; 8], [100; 8], [100; 8], 100);
        let filtered = filter_samples_8x8(&raw, 8);
        for v in filtered.top.iter() {
            assert_eq!(*v, 100);
        }
        for v in filtered.top_right.iter() {
            assert_eq!(*v, 100);
        }
        for v in filtered.left.iter() {
            assert_eq!(*v, 100);
        }
        assert_eq!(filtered.top_left, 100);
    }

    #[test]
    fn intra_8x8_filter_known() {
        // top = [0,4,8,12,16,20,24,28], top_right=[32,36,40,44,48,52,56,60]
        // tl = 50, left = [100,100,100,100,100,100,100,100]
        //
        // p'[0,-1] = (tl + 2*top[0] + top[1] + 2) >> 2 = (50+0+4+2)>>2 = 56>>2 = 14
        let raw = mk_8x8(
            [0, 4, 8, 12, 16, 20, 24, 28],
            [32, 36, 40, 44, 48, 52, 56, 60],
            [100; 8],
            50,
        );
        let filtered = filter_samples_8x8(&raw, 8);
        assert_eq!(filtered.top[0], 14);
        // p'[1,-1] = (top[0]+2*top[1]+top[2]+2)>>2 = (0+8+8+2)>>2 = 18>>2 = 4
        assert_eq!(filtered.top[1], 4);
        // p'[15,-1] = (top_right[6]+3*top_right[7]+2)>>2 = (56+180+2)>>2 = 238>>2 = 59
        assert_eq!(filtered.top_right[7], 59);
        // p'[-1,0] = (tl+2*left[0]+left[1]+2)>>2 = (50+200+100+2)>>2 = 352>>2 = 88
        assert_eq!(filtered.left[0], 88);
        // p'[-1,7] = (left[6]+3*left[7]+2)>>2 = (100+300+2)>>2 = 402>>2 = 100
        assert_eq!(filtered.left[7], 100);
        // p'[-1,-1] = (top[0]+2*tl+left[0]+2)>>2 = (0+100+100+2)>>2 = 202>>2 = 50
        assert_eq!(filtered.top_left, 50);
    }

    #[test]
    fn intra_8x8_dc_all() {
        // All 100s (filtered == raw). Sum = 800+800=1600, (1600+8)>>4 = 1608>>4 = 100.
        let raw = mk_8x8([100; 8], [100; 8], [100; 8], 100);
        let filtered = filter_samples_8x8(&raw, 8);
        let mut out = [0i32; 64];
        predict_8x8(Intra8x8Mode::Dc, &filtered, 8, &mut out);
        for v in out.iter() {
            assert_eq!(*v, 100);
        }
    }

    #[test]
    fn intra_8x8_dc_no_neighbours() {
        let mut raw = mk_8x8([0; 8], [0; 8], [0; 8], 0);
        raw.availability = no_avail();
        let filtered = filter_samples_8x8(&raw, 8);
        let mut out = [0i32; 64];
        predict_8x8(Intra8x8Mode::Dc, &filtered, 8, &mut out);
        for v in out.iter() {
            assert_eq!(*v, 128);
        }
    }

    #[test]
    fn intra_8x8_vertical() {
        let raw = mk_8x8([1, 2, 3, 4, 5, 6, 7, 8], [9; 8], [0; 8], 0);
        let filtered = filter_samples_8x8(&raw, 8);
        let mut out = [0i32; 64];
        predict_8x8(Intra8x8Mode::Vertical, &filtered, 8, &mut out);
        // Output rows should repeat the filtered top row.
        for y in 0..8 {
            for x in 0..8 {
                assert_eq!(out[at8(x, y)], filtered.top[x]);
            }
        }
    }

    #[test]
    fn intra_8x8_horizontal() {
        let raw = mk_8x8([0; 8], [0; 8], [10, 20, 30, 40, 50, 60, 70, 80], 0);
        let filtered = filter_samples_8x8(&raw, 8);
        let mut out = [0i32; 64];
        predict_8x8(Intra8x8Mode::Horizontal, &filtered, 8, &mut out);
        for y in 0..8 {
            for x in 0..8 {
                assert_eq!(out[at8(x, y)], filtered.left[y]);
            }
        }
    }

    #[test]
    fn intra_8x8_ddl_constant() {
        // All-constant reference → every prediction should equal that constant.
        let raw = mk_8x8([77; 8], [77; 8], [77; 8], 77);
        let filtered = filter_samples_8x8(&raw, 8);
        let mut out = [0i32; 64];
        predict_8x8(Intra8x8Mode::DiagonalDownLeft, &filtered, 8, &mut out);
        for v in out.iter() {
            assert_eq!(*v, 77);
        }
    }

    #[test]
    fn intra_8x8_ddr_constant() {
        let raw = mk_8x8([42; 8], [42; 8], [42; 8], 42);
        let filtered = filter_samples_8x8(&raw, 8);
        let mut out = [0i32; 64];
        predict_8x8(Intra8x8Mode::DiagonalDownRight, &filtered, 8, &mut out);
        for v in out.iter() {
            assert_eq!(*v, 42);
        }
    }

    // ----- Intra 16x16 tests -----

    fn mk_16x16(top: [i32; 16], left: [i32; 16], tl: i32) -> Samples16x16 {
        Samples16x16 {
            top_left: tl,
            top,
            left,
            availability: all_avail(),
        }
    }

    #[test]
    fn intra_16x16_vertical() {
        let mut top = [0i32; 16];
        for (i, t) in top.iter_mut().enumerate() {
            *t = i as i32 * 2;
        }
        let s = mk_16x16(top, [0; 16], 0);
        let mut out = [0i32; 256];
        predict_16x16(Intra16x16Mode::Vertical, &s, 8, &mut out);
        for y in 0..16 {
            for x in 0..16 {
                assert_eq!(out[at16(x, y)], top[x]);
            }
        }
    }

    #[test]
    fn intra_16x16_horizontal() {
        let mut left = [0i32; 16];
        for (i, l) in left.iter_mut().enumerate() {
            *l = i as i32 + 5;
        }
        let s = mk_16x16([0; 16], left, 0);
        let mut out = [0i32; 256];
        predict_16x16(Intra16x16Mode::Horizontal, &s, 8, &mut out);
        for y in 0..16 {
            for x in 0..16 {
                assert_eq!(out[at16(x, y)], left[y]);
            }
        }
    }

    #[test]
    fn intra_16x16_dc_all_100() {
        let s = mk_16x16([100; 16], [100; 16], 100);
        let mut out = [0i32; 256];
        predict_16x16(Intra16x16Mode::Dc, &s, 8, &mut out);
        for v in out.iter() {
            assert_eq!(*v, 100);
        }
    }

    #[test]
    fn intra_16x16_dc_no_neighbours() {
        let mut s = mk_16x16([0; 16], [0; 16], 0);
        s.availability = no_avail();
        let mut out = [0i32; 256];
        predict_16x16(Intra16x16Mode::Dc, &s, 8, &mut out);
        for v in out.iter() {
            assert_eq!(*v, 128);
        }
    }

    #[test]
    fn intra_16x16_plane_constant() {
        // All samples identical → H = V = 0, b = c = 0, a = 16*(v+v) = 32v
        // pred = (32v + 16) >> 5 = v (for v < 128 this is fine).
        let s = mk_16x16([80; 16], [80; 16], 80);
        let mut out = [0i32; 256];
        predict_16x16(Intra16x16Mode::Plane, &s, 8, &mut out);
        for v in out.iter() {
            assert_eq!(*v, 80);
        }
    }

    #[test]
    fn intra_16x16_plane_hand() {
        // Construct: top = 0..=15, left = 0..=15, tl = 0.
        // H = sum_{x'=0..7} (x'+1)*(top[8+x']-top[6-x'])
        //   x'=0: 1 * (8 - 6) = 2
        //   x'=1: 2 * (9 - 5) = 8
        //   x'=2: 3 * (10- 4) = 18
        //   x'=3: 4 * (11 - 3) = 32
        //   x'=4: 5 * (12 - 2) = 50
        //   x'=5: 6 * (13 - 1) = 72
        //   x'=6: 7 * (14 - 0) = 98
        //   x'=7: 8 * (15 - p[-1,-1]) = 8 * 15 = 120
        // H = 2+8+18+32+50+72+98+120 = 400
        // V = 400 (symmetry)
        // a = 16 * (left[15] + top[15]) = 16 * 30 = 480
        // b = (5*400 + 32) >> 6 = 2032 >> 6 = 31
        // c = 31
        // pred[7,7] = Clip1((480 + 31*0 + 31*0 + 16) >> 5) = 496>>5 = 15
        let mut top = [0i32; 16];
        let mut left = [0i32; 16];
        for i in 0..16 {
            top[i] = i as i32;
            left[i] = i as i32;
        }
        let s = mk_16x16(top, left, 0);
        let mut out = [0i32; 256];
        predict_16x16(Intra16x16Mode::Plane, &s, 8, &mut out);
        assert_eq!(out[at16(7, 7)], 15);
    }

    // ----- Intra chroma tests -----

    fn mk_chroma_420(top: Vec<i32>, left: Vec<i32>, tl: i32) -> SamplesChroma {
        SamplesChroma {
            top_left: tl,
            top,
            left,
            availability: all_avail(),
        }
    }

    #[test]
    fn intra_chroma_dc_420_all_avail() {
        // top=[10,20,30,40,50,60,70,80], left=[5,15,25,35,45,55,65,75]
        // Subblock (0,0): sum_top = 10+20+30+40=100, sum_left=5+15+25+35=80
        //   dc = (100+80+4)>>3 = 184>>3 = 23.
        // Subblock (4,0): top+right not leftmost → top-preferred.
        //   top samples p[4..=7,-1] = 50+60+70+80 = 260
        //   dc = (260+2)>>2 = 65
        // Subblock (0,4): leftmost non-top → left-preferred.
        //   left samples p[-1,4..=7] = 45+55+65+75 = 240
        //   dc = (240+2)>>2 = 60
        // Subblock (4,4): both available → use both.
        //   sum_top[4..=7] = 260, sum_left[4..=7] = 240
        //   dc = (260+240+4)>>3 = 504>>3 = 63
        let s = mk_chroma_420(
            vec![10, 20, 30, 40, 50, 60, 70, 80],
            vec![5, 15, 25, 35, 45, 55, 65, 75],
            0,
        );
        let mut out = vec![0i32; 64];
        predict_chroma(
            IntraChromaMode::Dc,
            &s,
            ChromaArrayType::Yuv420,
            8,
            &mut out,
        );
        // Top-left 4x4 (x=0..4, y=0..4) should all be 23.
        for y in 0..4 {
            for x in 0..4 {
                assert_eq!(out[y * 8 + x], 23, "TL subblock at ({},{})", x, y);
            }
        }
        for y in 0..4 {
            for x in 4..8 {
                assert_eq!(out[y * 8 + x], 65, "TR subblock at ({},{})", x, y);
            }
        }
        for y in 4..8 {
            for x in 0..4 {
                assert_eq!(out[y * 8 + x], 60, "BL subblock at ({},{})", x, y);
            }
        }
        for y in 4..8 {
            for x in 4..8 {
                assert_eq!(out[y * 8 + x], 63, "BR subblock at ({},{})", x, y);
            }
        }
    }

    #[test]
    fn intra_chroma_dc_420_no_neighbours() {
        let mut s = mk_chroma_420(vec![0; 8], vec![0; 8], 0);
        s.availability = no_avail();
        let mut out = vec![0i32; 64];
        predict_chroma(
            IntraChromaMode::Dc,
            &s,
            ChromaArrayType::Yuv420,
            8,
            &mut out,
        );
        for v in out.iter() {
            assert_eq!(*v, 128);
        }
    }

    #[test]
    fn intra_chroma_vertical_420() {
        let s = mk_chroma_420(vec![1, 2, 3, 4, 5, 6, 7, 8], vec![0; 8], 0);
        let mut out = vec![0i32; 64];
        predict_chroma(
            IntraChromaMode::Vertical,
            &s,
            ChromaArrayType::Yuv420,
            8,
            &mut out,
        );
        for y in 0..8 {
            for x in 0..8 {
                assert_eq!(out[y * 8 + x], s.top[x]);
            }
        }
    }

    #[test]
    fn intra_chroma_horizontal_420() {
        let s = mk_chroma_420(vec![0; 8], vec![10, 20, 30, 40, 50, 60, 70, 80], 0);
        let mut out = vec![0i32; 64];
        predict_chroma(
            IntraChromaMode::Horizontal,
            &s,
            ChromaArrayType::Yuv420,
            8,
            &mut out,
        );
        for y in 0..8 {
            for x in 0..8 {
                assert_eq!(out[y * 8 + x], s.left[y]);
            }
        }
    }

    #[test]
    fn intra_chroma_plane_420_constant() {
        let s = mk_chroma_420(vec![60; 8], vec![60; 8], 60);
        let mut out = vec![0i32; 64];
        predict_chroma(
            IntraChromaMode::Plane,
            &s,
            ChromaArrayType::Yuv420,
            8,
            &mut out,
        );
        for v in out.iter() {
            assert_eq!(*v, 60);
        }
    }

    #[test]
    fn intra_chroma_plane_420_hand() {
        // For 4:2:0: xCF=0, yCF=0, b coeff=34, c coeff=34.
        // H = sum_{x'=0..=3} (x'+1)*(p[4+x',-1]-p[2-x',-1])
        //   x'=0: 1*(top[4]-top[2])
        //   x'=1: 2*(top[5]-top[1])
        //   x'=2: 3*(top[6]-top[0])
        //   x'=3: 4*(top[7]-p[-1,-1])  — where p[2-x',-1]=p[-1,-1]=tl for x'=3.
        // top = [0,2,4,6,8,10,12,14], tl = 0.
        //   x'=0: 1*(8-4)=4
        //   x'=1: 2*(10-2)=16
        //   x'=2: 3*(12-0)=36
        //   x'=3: 4*(14-0)=56
        // H = 112
        // left mirrors top; V = 112.
        // a = 16 * (left[7] + top[7]) = 16*(14+14) = 448
        // b = (34*112 + 32) >> 6 = (3808+32)>>6 = 3840>>6 = 60
        // c = 60
        // pred[3,3] = Clip1((448 + 60*(3-3-0) + 60*(3-3-0) + 16) >> 5) = 464>>5 = 14
        let s = mk_chroma_420(
            vec![0, 2, 4, 6, 8, 10, 12, 14],
            vec![0, 2, 4, 6, 8, 10, 12, 14],
            0,
        );
        let mut out = vec![0i32; 64];
        predict_chroma(
            IntraChromaMode::Plane,
            &s,
            ChromaArrayType::Yuv420,
            8,
            &mut out,
        );
        assert_eq!(out[3 * 8 + 3], 14);
    }

    #[test]
    fn intra_chroma_vertical_422() {
        let s = SamplesChroma {
            top_left: 0,
            top: vec![1, 2, 3, 4, 5, 6, 7, 8],
            left: vec![0; 16],
            availability: all_avail(),
        };
        let mut out = vec![0i32; 128];
        predict_chroma(
            IntraChromaMode::Vertical,
            &s,
            ChromaArrayType::Yuv422,
            8,
            &mut out,
        );
        for y in 0..16 {
            for x in 0..8 {
                assert_eq!(out[y * 8 + x], s.top[x]);
            }
        }
    }

    #[test]
    fn intra_chroma_plane_422_constant() {
        let s = SamplesChroma {
            top_left: 60,
            top: vec![60; 8],
            left: vec![60; 16],
            availability: all_avail(),
        };
        let mut out = vec![0i32; 128];
        predict_chroma(
            IntraChromaMode::Plane,
            &s,
            ChromaArrayType::Yuv422,
            8,
            &mut out,
        );
        for v in out.iter() {
            assert_eq!(*v, 60);
        }
    }

    #[test]
    fn clip1_boundary() {
        assert_eq!(clip1(-1, 8), 0);
        assert_eq!(clip1(0, 8), 0);
        assert_eq!(clip1(255, 8), 255);
        assert_eq!(clip1(256, 8), 255);
        assert_eq!(clip1(1023, 10), 1023);
        assert_eq!(clip1(1024, 10), 1023);
    }
}