pixon 0.8.0

SIMD-dispatched color conversion for the FFmpeg AVPixelFormat space: one-call decode of validated source frames to RGB / RGBA / Luma / HSV, fused resampling while converting, colorimetry-honest matrices and chroma siting, no_std-friendly.
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
//! Chroma-siting-aware 4:2:0 upsampling for `Yuv420p` (#302).
//!
//! Covers: the centered-siting horizontal upsample kernel against a
//! hand-computed oracle; the default / co-sited path staying byte-identical
//! to the pre-#302 nearest-neighbor decode (the regression guard); the
//! centered RGB / RGBA / HSV identity decodes matching an independent
//! "upsample-then-4:4:4" reference; SIMD-vs-scalar parity of the centered
//! path; and that the centered phase actually shifts chroma horizontally.

use super::*;
use crate::{ChromaLocation, ColorMatrix};

const W: u32 = 16;
const H: u32 = 8;

/// A `Yuv420p` frame with flat luma and a per-column chroma ramp, so the
/// horizontal chroma phase is observable (a solid chroma frame would make
/// every siting identical).
fn ramp_yuv420p() -> (Vec<u8>, Vec<u8>, Vec<u8>) {
  let w = W as usize;
  let h = H as usize;
  let cw = w / 2;
  let ch = h / 2;
  let y = std::vec![128u8; w * h];
  let mut u = std::vec![0u8; cw * ch];
  let mut v = std::vec![0u8; cw * ch];
  for r in 0..ch {
    for c in 0..cw {
      // Distinct per-column ramps for U and V; the `+ r` keeps chroma rows
      // from being identical so a (hypothetical) vertical mistake would show.
      u[r * cw + c] = (16 + c * 24 + r * 3).min(240) as u8;
      v[r * cw + c] = (240 - c * 24).max(16) as u8;
    }
  }
  (y, u, v)
}

/// Independent reference for the centered-siting horizontal upsample — the
/// MPEG-1 / JPEG phase-0.5 `1/4`–`3/4` weights with edge clamp. Mirrors the
/// documented formula but is written separately from the production kernel so
/// it is a real oracle.
fn ref_upsample_center_h(c_half: &[u8], width: usize) -> Vec<u8> {
  let half = width / 2;
  let mut out = std::vec![0u8; width];
  for j in 0..half {
    let l = c_half[j.saturating_sub(1)] as i32;
    let m = c_half[j] as i32;
    let r = c_half[if j + 1 < half { j + 1 } else { j }] as i32;
    out[2 * j] = ((l + 3 * m + 2) >> 2) as u8;
    out[2 * j + 1] = ((3 * m + r + 2) >> 2) as u8;
  }
  out
}

/// Builds the full-resolution U / V planes a centered-siting `Yuv420p`
/// decode should reconstruct: each luma row `r` takes chroma row `r / 2`
/// (the walker's vertical replication, unchanged by #302) horizontally
/// upsampled with the centered weights. Feeding these to a `Yuv444p`
/// conversion is the end-to-end oracle for the centered path.
fn ref_full_chroma(u420: &[u8], v420: &[u8]) -> (Vec<u8>, Vec<u8>) {
  let w = W as usize;
  let h = H as usize;
  let cw = w / 2;
  let mut u444 = std::vec![0u8; w * h];
  let mut v444 = std::vec![0u8; w * h];
  for r in 0..h {
    let cr = r / 2;
    let urow = ref_upsample_center_h(&u420[cr * cw..cr * cw + cw], w);
    let vrow = ref_upsample_center_h(&v420[cr * cw..cr * cw + cw], w);
    u444[r * w..r * w + w].copy_from_slice(&urow);
    v444[r * w..r * w + w].copy_from_slice(&vrow);
  }
  (u444, v444)
}

fn convert_rgb(loc: ChromaLocation, simd: bool) -> Vec<u8> {
  let (yp, up, vp) = ramp_yuv420p();
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut rgb = std::vec![0u8; (W * H * 3) as usize];
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(loc.clone())
    .with_simd(simd);
  yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
  rgb
}

// ---- kernel oracle ---------------------------------------------------------

#[test]
fn center_upsample_kernel_matches_hand_computed() {
  // c = [0, 0, 100, 100] (half = 4, width = 8). Centered reconstruction
  // ramps the step and shifts it right of the co-sited boundary:
  //   even 2j = (c[j-1] + 3·c[j] + 2) >> 2, odd 2j+1 = (3·c[j] + c[j+1] + 2) >> 2.
  let c_half = [0u8, 0, 100, 100];
  let mut out = [0u8; 8];
  crate::row::scalar::chroma_upsample_2to1_center_h(&c_half, &mut out, 8);
  assert_eq!(out, [0, 0, 0, 25, 75, 100, 100, 100]);
}

#[test]
fn center_upsample_kernel_clamps_edges() {
  // Width 4: left edge even = c[0] exactly, right edge odd = c[last] exactly.
  let c_half = [10u8, 20];
  let mut out = [0u8; 4];
  crate::row::scalar::chroma_upsample_2to1_center_h(&c_half, &mut out, 4);
  assert_eq!(out, [10, 13, 18, 20]);
  assert_eq!(out[0], c_half[0], "left edge even column is co-sited");
  assert_eq!(out[3], c_half[1], "right edge odd column is co-sited");
}

// ---- default / co-sited path is byte-identical (regression guard) ----------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn default_and_cosited_sitings_are_byte_identical() {
  // The pre-#302 baseline: a sink that never sets a chroma location.
  let baseline = convert_rgb(ChromaLocation::Unspecified, true);

  // Unspecified, Unknown, and the fully co-sited default (`Left`, `v = 0.5`)
  // keep the exact nearest-neighbor decode — bit-for-bit equal to the baseline
  // even though the chroma plane is a non-trivial ramp. `BottomLeft` (`v = 1`)
  // and `TopLeft` (`v = 0`) are EXCLUDED: both are co-sited horizontally
  // (`h = 0`) but VERTICALLY sited, so they fold a vertical box blend (backward
  // for BottomLeft, forward for TopLeft) and are covered by their own tests.
  for loc in [
    ChromaLocation::Unspecified,
    ChromaLocation::other("unassigned-99"),
    ChromaLocation::Left,
  ] {
    assert_eq!(
      convert_rgb(loc.clone(), true),
      baseline,
      "siting {loc:?} must keep the byte-identical default decode"
    );
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn default_path_does_not_allocate_chroma_scratch() {
  let (yp, up, vp) = ramp_yuv420p();
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut rgb = std::vec![0u8; (W * H * 3) as usize];
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(ChromaLocation::Left);
  yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
  let chroma_len = sink.chroma_full.len();
  drop(sink);
  assert_eq!(
    chroma_len, 0,
    "co-sited path must not grow the chroma scratch"
  );
}

// ---- centered path correctness ---------------------------------------------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn center_rgb_matches_upsample_then_444_reference() {
  let (yp, up, vp) = ramp_yuv420p();

  // Reference: horizontally upsample chroma (centered weights) to full
  // resolution, then run the ordinary 4:4:4 decode.
  let (u444, v444) = ref_full_chroma(&up, &vp);
  let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
  let mut rgb_ref = std::vec![0u8; (W * H * 3) as usize];
  let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb_ref)
    .unwrap();
  yuv444p_to(
    &ref_src,
    false,
    ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
  )
  .unwrap();

  assert_eq!(
    convert_rgb(ChromaLocation::Center, true),
    rgb_ref,
    "centered 4:2:0 RGB must equal upsample-then-4:4:4"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn center_grows_chroma_scratch_to_full_width() {
  let (yp, up, vp) = ramp_yuv420p();
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut rgb = std::vec![0u8; (W * H * 3) as usize];
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(ChromaLocation::Center);
  yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
  let chroma_len = sink.chroma_full.len();
  drop(sink);
  assert_eq!(
    chroma_len,
    2 * W as usize,
    "centered path stages U+V at full width"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_forward_vblend_differs_from_center_and_bottom() {
  // Top shares Center's horizontal (centered) phase and now consumes its
  // vertical phase through the FORWARD one-row delay: the ODD output row is the
  // box average of its chroma row and the NEXT one. On a fixture whose chroma
  // varies across rows Top therefore DIFFERS from Center (horizontal-only) AND
  // from Bottom (which box-blends the EVEN row with the PREVIOUS chroma row).
  let center = convert_rgb(ChromaLocation::Center, true);
  assert_ne!(
    convert_rgb(ChromaLocation::Top, true),
    center,
    "Top's forward vertical box blend must differ from Center on a vertically-varying ramp"
  );
  assert_ne!(
    convert_rgb(ChromaLocation::Top, true),
    convert_rgb(ChromaLocation::Bottom, true),
    "Top (forward, odd rows) must differ from Bottom (backward, even rows)"
  );
  assert_ne!(
    convert_rgb(ChromaLocation::Bottom, true),
    center,
    "Bottom's vertical box blend must differ from Center on a vertically-varying chroma ramp"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn centered_phase_differs_from_default() {
  // The whole point: on a chroma ramp the centered phase must move chroma
  // relative to the co-sited / nearest-neighbor default.
  assert_ne!(
    convert_rgb(ChromaLocation::Center, true),
    convert_rgb(ChromaLocation::Left, true),
    "centered siting must shift chroma vs the co-sited default"
  );
}

// ---- SIMD vs scalar parity -------------------------------------------------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn centered_path_simd_matches_scalar() {
  assert_eq!(
    convert_rgb(ChromaLocation::Center, true),
    convert_rgb(ChromaLocation::Center, false),
    "centered path must be bit-identical across the SIMD and scalar tiers"
  );
}

// ---- RGBA / HSV identity outputs also honor siting -------------------------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn center_rgba_and_hsv_match_444_reference() {
  let (yp, up, vp) = ramp_yuv420p();
  let (u444, v444) = ref_full_chroma(&up, &vp);

  // RGBA-only path.
  {
    let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
    let mut rgba = std::vec![0u8; (W * H * 4) as usize];
    let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
      .with_rgba(&mut rgba)
      .unwrap()
      .with_chroma_location(ChromaLocation::Center);
    yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

    let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
    let mut rgba_ref = std::vec![0u8; (W * H * 4) as usize];
    let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
      .with_rgba(&mut rgba_ref)
      .unwrap();
    yuv444p_to(
      &ref_src,
      false,
      ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
    assert_eq!(
      rgba, rgba_ref,
      "centered RGBA must equal upsample-then-4:4:4"
    );
  }

  // HSV-direct path (no RGB / RGBA attached).
  {
    let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
    let (mut h, mut s, mut v) = (
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
    );
    let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
      .with_hsv(&mut h, &mut s, &mut v)
      .unwrap()
      .with_chroma_location(ChromaLocation::Center);
    yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

    let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
    let (mut hr, mut sr, mut vr) = (
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
    );
    let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
      .with_hsv(&mut hr, &mut sr, &mut vr)
      .unwrap();
    yuv444p_to(
      &ref_src,
      false,
      ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
    assert_eq!(
      (h, s, v),
      (hr, sr, vr),
      "centered HSV must equal upsample-then-4:4:4"
    );
  }
}

// ---- end-to-end ColorSpec flow (no manual with_chroma_location) ------------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn color_spec_center_drives_decode_without_manual_chroma_call() {
  use crate::{ColorInfo, ColorSpec, DynamicRange, PixelFormat, Primaries, Transfer, YuvOptions};

  let (yp, up, vp) = ramp_yuv420p();
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);

  // Drive the decode from a ColorSpec carrying ChromaLocation::Center via the
  // NORMAL path: YuvOptions::from_color_spec(spec) for the walk (matrix +
  // range) and the sink's ColorSpec entry point for the siting — with NO
  // manual `with_chroma_location` call.
  let info = ColorInfo::new(
    Primaries::Bt709,
    Transfer::Bt709,
    ColorMatrix::Bt601,
    DynamicRange::Limited,
    ChromaLocation::Center,
  );
  let spec = ColorSpec::from_info(PixelFormat::Yuv420p, info);
  let opts = YuvOptions::from_color_spec(&spec);
  let mut rgb = std::vec![0u8; (W * H * 3) as usize];
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_color_spec(&spec)
    .unwrap();
  yuv420p_to(&src, opts.full_range(), &mut sink).unwrap();
  drop(sink);

  // `opts` mirrors `convert_rgb`'s limited-range Bt601 walk, so the
  // spec-driven output must (a) differ from the default (no siting) and
  // (b) match the explicit centered path bit-for-bit.
  assert_ne!(
    rgb,
    convert_rgb(ChromaLocation::Unspecified, true),
    "ColorSpec ChromaLocation::Center must change the decode via the options path"
  );
  assert_eq!(
    rgb,
    convert_rgb(ChromaLocation::Center, true),
    "ColorSpec-driven centered decode must equal the explicit centered path"
  );
}

// ---- centered siting + ChromaDerivedNcl (#303 cross-feature seam) -----------

/// The centered chroma-siting decode must honour
/// [`KernelMatrix::ChromaDerivedNcl`] just like the non-centered path: its
/// Kr/Kb are derived from the ColorSpec's primaries, NOT the BT.709 fallback.
/// Before the fix the centered branch called the 4:4:4 kernels with only the
/// matrix tag, so a non-BT709 ChromaDerivedNcl decoded with BT.709
/// coefficients (wrong colors) and could even run on SIMD — this test fails
/// against that bug. Covers RGB and RGBA, and the scalar-routing invariant.
#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn center_chroma_derived_ncl_uses_primaries_not_bt709() {
  use crate::{
    ColorInfo, ColorSpec, DynamicRange, PixelFormat, Primaries, Transfer,
    row::{
      yuv_444_to_rgb_row, yuv_444_to_rgb_row_primaries, yuv_444_to_rgba_row,
      yuv_444_to_rgba_row_primaries,
    },
  };

  let w = W as usize;
  let h = H as usize;
  let (yp, up, vp) = ramp_yuv420p();
  // The centered full-width chroma the decode reconstructs per row.
  let (u444, v444) = ref_full_chroma(&up, &vp);
  // A non-BT709 primary set: ChromaDerivedNcl here must diverge from BT.709.
  let prim = Primaries::Bt2020;
  let info = ColorInfo::new(
    prim.clone(),
    Transfer::Bt709,
    ColorMatrix::ChromaDerivedNcl,
    DynamicRange::Limited,
    ChromaLocation::Center,
  );
  let spec = ColorSpec::from_info(PixelFormat::Yuv420p, info);

  // Drive the centered Yuv420p decode via the ColorSpec (limited-range).
  let decode_rgb = |simd: bool| {
    let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
    let mut rgb = std::vec![0u8; w * h * 3];
    let mut sink = MixedSinker::<Yuv420p>::new(w, h)
      .with_rgb(&mut rgb)
      .unwrap()
      .with_color_spec(&spec)
      .unwrap()
      .with_simd(simd);
    yuv420p_to(
      &src,
      false,
      sink.set_kernel_matrix(KernelMatrix::ChromaDerivedNcl),
    )
    .unwrap();
    rgb
  };

  // Exact reference: same centered-upsampled chroma, decoded 4:4:4 with the
  // primaries-derived coefficients (vs the BT.709 fallback the bug produced).
  let mut rgb_derived = std::vec![0u8; w * h * 3];
  let mut rgb_bt709 = std::vec![0u8; w * h * 3];
  for r in 0..h {
    let (ys, us, vs) = (
      &yp[r * w..r * w + w],
      &u444[r * w..r * w + w],
      &v444[r * w..r * w + w],
    );
    yuv_444_to_rgb_row_primaries(
      ys,
      us,
      vs,
      &mut rgb_derived[r * w * 3..(r + 1) * w * 3],
      w,
      KernelMatrix::ChromaDerivedNcl,
      &prim,
      false,
      true,
    );
    yuv_444_to_rgb_row(
      ys,
      us,
      vs,
      &mut rgb_bt709[r * w * 3..(r + 1) * w * 3],
      w,
      KernelMatrix::Bt709,
      false,
      true,
    );
  }
  let rgb = decode_rgb(true);
  assert_eq!(
    rgb, rgb_derived,
    "centered ChromaDerivedNcl RGB must use the primaries-derived coefficients"
  );
  assert_ne!(
    rgb, rgb_bt709,
    "centered ChromaDerivedNcl RGB must NOT decode as the BT.709 fallback (the bug)"
  );
  assert_ne!(
    rgb_derived, rgb_bt709,
    "sanity: Bt2020-derived and BT.709 coefficients differ on the chroma ramp"
  );
  // Scalar-routing invariant: ChromaDerivedNcl is deterministic across tiers
  // (it is forced onto the scalar kernel), so SIMD == scalar.
  assert_eq!(
    rgb,
    decode_rgb(false),
    "centered ChromaDerivedNcl must be scalar-routed (no SIMD/scalar split)"
  );

  // ---- RGBA twin ----
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut rgba = std::vec![0u8; w * h * 4];
  let mut sink = MixedSinker::<Yuv420p>::new(w, h)
    .with_rgba(&mut rgba)
    .unwrap()
    .with_color_spec(&spec)
    .unwrap();
  yuv420p_to(
    &src,
    false,
    sink.set_kernel_matrix(KernelMatrix::ChromaDerivedNcl),
  )
  .unwrap();
  drop(sink);

  let mut rgba_derived = std::vec![0u8; w * h * 4];
  let mut rgba_bt709 = std::vec![0u8; w * h * 4];
  for r in 0..h {
    let (ys, us, vs) = (
      &yp[r * w..r * w + w],
      &u444[r * w..r * w + w],
      &v444[r * w..r * w + w],
    );
    yuv_444_to_rgba_row_primaries(
      ys,
      us,
      vs,
      &mut rgba_derived[r * w * 4..(r + 1) * w * 4],
      w,
      KernelMatrix::ChromaDerivedNcl,
      &prim,
      false,
      true,
    );
    yuv_444_to_rgba_row(
      ys,
      us,
      vs,
      &mut rgba_bt709[r * w * 4..(r + 1) * w * 4],
      w,
      KernelMatrix::Bt709,
      false,
      true,
    );
  }
  assert_eq!(
    rgba, rgba_derived,
    "centered ChromaDerivedNcl RGBA must use the primaries-derived coefficients"
  );
  assert_ne!(
    rgba, rgba_bt709,
    "centered ChromaDerivedNcl RGBA must NOT decode as the BT.709 fallback (the bug)"
  );
}

// ---- preflight-ordering atomicity (#302, cf. #180) -------------------------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn centered_alloc_failure_leaves_outputs_untouched() {
  use crate::resample::ResampleError;

  // A sink requesting luma PLUS a centered RGB decode whose chroma-scratch
  // allocation fails must leave EVERY output buffer — luma included —
  // untouched on the error path: the centered scratch is reserved (fallibly)
  // BEFORE any output row is written, so a refusal can't half-update the
  // frame.
  let (yp, up, vp) = ramp_yuv420p();
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut luma = std::vec![0xABu8; (W * H) as usize];
  let mut rgb = std::vec![0xCDu8; (W * H * 3) as usize];
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_luma(&mut luma)
    .unwrap()
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(ChromaLocation::Center);

  super::super::arm_chroma_full_alloc_failure();
  let err = yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap_err();
  drop(sink);

  assert!(
    matches!(
      err,
      MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
    ),
    "centered chroma-scratch refusal must surface as a recoverable AllocationFailed, got {err:?}"
  );
  assert!(
    luma.iter().all(|&b| b == 0xAB),
    "luma must be untouched on the centered alloc-failure path"
  );
  assert!(
    rgb.iter().all(|&b| b == 0xCD),
    "rgb must be untouched on the centered alloc-failure path"
  );
}

// Gated on `yuva`: reuses the crate's existing RGB-scratch allocation
// failpoint (`arm_rgb_scratch_alloc_failure`, itself `yuva`-gated). Under
// `--all-features` both `yuv-planar` and `yuva` are on, so it runs.
#[cfg(feature = "yuva")]
#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgb_scratch_alloc_failure_leaves_outputs_untouched() {
  use crate::resample::ResampleError;

  // Codex R3's exact case: luma + RGBA + HSV with NO rgb output. That is
  // `want_hsv && want_rgba && !want_rgb` → `need_rgb_kernel` with no caller
  // RGB buffer, so the decode grows the RGB row scratch
  // (`rgb_row_buf_or_scratch`'s scratch arm). With that allocation armed to
  // fail, the up-front preflight returns AllocationFailed BEFORE any output
  // row — luma included — is written.
  let (yp, up, vp) = ramp_yuv420p();
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut luma = std::vec![0xABu8; (W * H) as usize];
  let mut rgba = std::vec![0xCDu8; (W * H * 4) as usize];
  let (mut hh, mut ss, mut vv) = (
    std::vec![0xCDu8; (W * H) as usize],
    std::vec![0xCDu8; (W * H) as usize],
    std::vec![0xCDu8; (W * H) as usize],
  );
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_luma(&mut luma)
    .unwrap()
    .with_rgba(&mut rgba)
    .unwrap()
    .with_hsv(&mut hh, &mut ss, &mut vv)
    .unwrap();

  super::super::arm_rgb_scratch_alloc_failure();
  let err = yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap_err();
  drop(sink);

  assert!(
    matches!(
      err,
      MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
    ),
    "RGB-scratch refusal must surface as a recoverable AllocationFailed, got {err:?}"
  );
  assert!(
    luma.iter().all(|&b| b == 0xAB),
    "luma must be untouched on the rgb-scratch alloc-failure path"
  );
}

// ---- bottom-sited VERTICAL phase (#302 deferred vertical part) --------------

/// A `Yuv420p` frame with flat luma and a per-ROW chroma ramp, so the vertical
/// chroma phase is observable in isolation (constant within each chroma row,
/// stepped between rows — a horizontal-only siting leaves it untouched, while a
/// vertical blend visibly moves it).
fn vramp_yuv420p() -> (Vec<u8>, Vec<u8>, Vec<u8>) {
  let w = W as usize;
  let h = H as usize;
  let cw = w / 2;
  let ch = h / 2;
  let y = std::vec![128u8; w * h];
  let mut u = std::vec![0u8; cw * ch];
  let mut v = std::vec![0u8; cw * ch];
  for r in 0..ch {
    for c in 0..cw {
      // Flat across columns, a strong step between chroma rows: only a VERTICAL
      // reconstruction changes these, so Bottom != the horizontal-only sitings.
      u[r * cw + c] = (20 + r * 40).min(240) as u8;
      v[r * cw + c] = (220 - r * 40).max(16) as u8;
    }
  }
  (y, u, v)
}

/// Independent reference for the bottom-sited (`v = 1`) full reconstruction: per
/// luma row `r`, the EVEN rows take the vertical box average of chroma rows
/// `r/2 - 1` (clamped to `r/2` at the top edge) and `r/2`; the ODD rows take
/// chroma row `r/2` directly. Each resulting half-row is then horizontally
/// upsampled with the SAME centered `1/4`–`3/4` weights as Center (Bottom is
/// `h = 0.5`). Written separately from the production kernels so it is a true
/// oracle.
fn ref_full_chroma_bottom(u420: &[u8], v420: &[u8]) -> (Vec<u8>, Vec<u8>) {
  let w = W as usize;
  let h = H as usize;
  let cw = w / 2;
  let ch = h / 2;
  let mut u444 = std::vec![0u8; w * h];
  let mut v444 = std::vec![0u8; w * h];
  let vblend = |plane: &[u8], cr: usize, prev: usize| -> Vec<u8> {
    (0..cw)
      .map(|c| {
        let a = plane[prev * cw + c] as u32;
        let b = plane[cr * cw + c] as u32;
        ((a + b + 1) >> 1) as u8
      })
      .collect::<Vec<u8>>()
  };
  for r in 0..h {
    let cr = r / 2;
    let (uhalf, vhalf) = if r & 1 == 0 {
      // Even row: vertical box blend of chroma rows cr-1 (clamp to cr) and cr.
      let prev = cr.saturating_sub(1);
      (vblend(u420, cr, prev), vblend(v420, cr, prev))
    } else {
      // Odd row: co-sited with chroma row cr (no vertical blend).
      let _ = ch;
      (
        u420[cr * cw..cr * cw + cw].to_vec(),
        v420[cr * cw..cr * cw + cw].to_vec(),
      )
    };
    let urow = ref_upsample_center_h(&uhalf, w);
    let vrow = ref_upsample_center_h(&vhalf, w);
    u444[r * w..r * w + w].copy_from_slice(&urow);
    v444[r * w..r * w + w].copy_from_slice(&vrow);
  }
  (u444, v444)
}

fn convert_rgb_with(loc: ChromaLocation, simd: bool, yp: &[u8], up: &[u8], vp: &[u8]) -> Vec<u8> {
  let src = Yuv420pFrame::new(yp, up, vp, W, H, W, W / 2, W / 2);
  let mut rgb = std::vec![0u8; (W * H * 3) as usize];
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(loc.clone())
    .with_simd(simd);
  yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
  rgb
}

// ---- bottom-sited vertical kernel oracle -----------------------------------

#[test]
fn bottom_even_kernel_matches_hand_computed() {
  // prev = [0, 0, 100, 100], cur = [40, 40, 60, 60] (half = 4, width = 8).
  // Vertical box blend e = (prev + cur + 1) >> 1 = [20, 20, 80, 80], then the
  // centered horizontal 1/4-3/4 reconstruction with edge clamp:
  //   2j   = (e[j-1] + 3 e[j] + 2) >> 2,  2j+1 = (3 e[j] + e[j+1] + 2) >> 2.
  let prev = [0u8, 0, 100, 100];
  let cur = [40u8, 40, 60, 60];
  let mut out = [0u8; 8];
  crate::row::scalar::chroma_upsample_420_bottom_even_h(&prev, &cur, &mut out, 8);
  // e = [20,20,80,80] -> [20, 20, 20, 35, 65, 80, 80, 80].
  assert_eq!(out, [20, 20, 20, 35, 65, 80, 80, 80]);
}

#[test]
fn bottom_even_kernel_equals_center_when_rows_match() {
  // When prev == cur the vertical box blend is a no-op, so the bottom-even
  // kernel must reproduce the plain horizontal centered upsample exactly.
  let cur = [10u8, 40, 90, 30];
  let mut bottom = [0u8; 8];
  let mut center = [0u8; 8];
  crate::row::scalar::chroma_upsample_420_bottom_even_h(&cur, &cur, &mut bottom, 8);
  crate::row::scalar::chroma_upsample_2to1_center_h(&cur, &mut center, 8);
  assert_eq!(
    bottom, center,
    "prev == cur must collapse the vertical blend to the horizontal centered path"
  );
}

// ---- bottom-sited end-to-end correctness -----------------------------------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_rgb_matches_vblend_then_444_reference() {
  let (yp, up, vp) = vramp_yuv420p();

  // Reference: vertical-box-blend (even rows) + horizontal centered upsample to
  // full resolution, then the ordinary 4:4:4 decode.
  let (u444, v444) = ref_full_chroma_bottom(&up, &vp);
  let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
  let mut rgb_ref = std::vec![0u8; (W * H * 3) as usize];
  let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb_ref)
    .unwrap();
  yuv444p_to(
    &ref_src,
    false,
    ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
  )
  .unwrap();

  assert_eq!(
    convert_rgb_with(ChromaLocation::Bottom, true, &yp, &up, &vp),
    rgb_ref,
    "bottom-sited 4:2:0 RGB must equal vertical-blend + horizontal-upsample then 4:4:4"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_differs_from_center_and_default_vertically() {
  // On a purely-vertical chroma ramp, Bottom's even-row vertical blend must
  // move chroma both vs the horizontal-only Center (same horizontal phase, no
  // vertical) and vs the replicate default.
  let (yp, up, vp) = vramp_yuv420p();
  let bottom = convert_rgb_with(ChromaLocation::Bottom, true, &yp, &up, &vp);
  assert_ne!(
    bottom,
    convert_rgb_with(ChromaLocation::Center, true, &yp, &up, &vp),
    "Bottom (v=1) must differ from Center (horizontal-only) on a vertical chroma ramp"
  );
  assert_ne!(
    bottom,
    convert_rgb_with(ChromaLocation::Left, true, &yp, &up, &vp),
    "Bottom must differ from the vertical-replicate default"
  );
  // Top box-blends the ODD row FORWARD (with the next chroma row) where Bottom
  // blends the EVEN row backward, so the two differ on this ramp.
  assert_ne!(
    bottom,
    convert_rgb_with(ChromaLocation::Top, true, &yp, &up, &vp),
    "Top (forward, odd rows) must differ from Bottom's backward even-row blend"
  );
}

// ---- top-sited VERTICAL phase (RFC #238 forward one-row delay) --------------

/// Independent reference for the top-sited (`v = 0`) full reconstruction: per
/// luma row `r`, the EVEN rows take chroma row `r/2` directly (co-sited with the
/// top luma row); the ODD rows take the vertical box average of chroma rows
/// `r/2` and `r/2 + 1` (the FORWARD neighbour, clamped to `ch - 1` at the bottom
/// edge — so the trailing odd row collapses back to co-sited). Each half-row is
/// then horizontally upsampled — centered (`Top`, `h = 0.5`) or replicated
/// (`TopLeft`, `h = 0`). Written separately from the production kernels so it is
/// a true oracle for the forward one-row-delay reconstruction.
fn ref_full_chroma_top(u420: &[u8], v420: &[u8], centered: bool) -> (Vec<u8>, Vec<u8>) {
  let w = W as usize;
  let h = H as usize;
  let cw = w / 2;
  let ch = h / 2;
  let up_h = |half: &[u8]| -> Vec<u8> {
    if centered {
      ref_upsample_center_h(half, w)
    } else {
      ref_upsample_cosited_h(half, w)
    }
  };
  let vblend = |plane: &[u8], cr: usize, next: usize| -> Vec<u8> {
    (0..cw)
      .map(|c| {
        let a = plane[cr * cw + c] as u32;
        let b = plane[next * cw + c] as u32;
        ((a + b + 1) >> 1) as u8
      })
      .collect::<Vec<u8>>()
  };
  let mut u444 = std::vec![0u8; w * h];
  let mut v444 = std::vec![0u8; w * h];
  for r in 0..h {
    let cr = r / 2;
    let (uhalf, vhalf) = if r & 1 == 0 {
      (
        u420[cr * cw..cr * cw + cw].to_vec(),
        v420[cr * cw..cr * cw + cw].to_vec(),
      )
    } else {
      let next = (cr + 1).min(ch - 1);
      (vblend(u420, cr, next), vblend(v420, cr, next))
    };
    u444[r * w..r * w + w].copy_from_slice(&up_h(&uhalf));
    v444[r * w..r * w + w].copy_from_slice(&up_h(&vhalf));
  }
  (u444, v444)
}

/// End-to-end RGB reference for a top-sited siting: forward-reconstruct full
/// chroma, then the ordinary 4:4:4 decode.
fn top_ref_rgb(centered: bool, yp: &[u8], up: &[u8], vp: &[u8]) -> Vec<u8> {
  let (u444, v444) = ref_full_chroma_top(up, vp, centered);
  let ref_src = Yuv444pFrame::new(yp, &u444, &v444, W, H, W, W, W);
  let mut rgb_ref = std::vec![0u8; (W * H * 3) as usize];
  let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb_ref)
    .unwrap();
  yuv444p_to(
    &ref_src,
    false,
    ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
  )
  .unwrap();
  rgb_ref
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_rgb_matches_forward_reconstruct_reference() {
  // The core Top correctness proof: the streaming forward one-row-delay decode
  // must reproduce "reconstruct full chroma forward, then 4:4:4" bit-for-bit,
  // INCLUDING the trailing-odd-row bottom-edge clamp. Both SIMD tiers match.
  let (yp, up, vp) = vramp_yuv420p();
  let rgb_ref = top_ref_rgb(true, &yp, &up, &vp);
  assert_eq!(
    convert_rgb_with(ChromaLocation::Top, true, &yp, &up, &vp),
    rgb_ref,
    "top-sited 4:2:0 RGB (SIMD) must equal forward-blend + centered-upsample then 4:4:4"
  );
  assert_eq!(
    convert_rgb_with(ChromaLocation::Top, false, &yp, &up, &vp),
    rgb_ref,
    "top-sited 4:2:0 RGB (scalar) must match the same reference"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn topleft_rgb_matches_cosited_forward_reference() {
  // TopLeft is the co-sited-horizontal (`h = 0`) twin of Top: same forward
  // vertical blend, plain 2x horizontal replicate instead of the centered phase.
  let (yp, up, vp) = vramp_yuv420p();
  let rgb_ref = top_ref_rgb(false, &yp, &up, &vp);
  assert_eq!(
    convert_rgb_with(ChromaLocation::TopLeft, true, &yp, &up, &vp),
    rgb_ref,
    "topleft-sited RGB (SIMD) must equal forward-blend + co-sited-replicate then 4:4:4"
  );
  assert_eq!(
    convert_rgb_with(ChromaLocation::TopLeft, false, &yp, &up, &vp),
    rgb_ref,
    "topleft-sited RGB (scalar) must match the same reference"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_differs_from_topleft_horizontally() {
  // On a purely-HORIZONTAL chroma ramp the shared forward vertical blend is a
  // no-op, so only the horizontal phase separates Top (`h = 0.5` centered) from
  // TopLeft (`h = 0` co-sited) — they must differ.
  let (yp, up, vp) = ramp_yuv420p();
  assert_ne!(
    convert_rgb_with(ChromaLocation::Top, true, &yp, &up, &vp),
    convert_rgb_with(ChromaLocation::TopLeft, true, &yp, &up, &vp),
    "Top (h=0.5) must differ from TopLeft (h=0) on a horizontal chroma ramp"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_rgba_and_hsv_match_forward_reference() {
  // Exercise the RGBA-only and HSV-direct branches of the delayed colour decode:
  // both must reproduce the forward-reconstruct-then-4:4:4 reference.
  let (yp, up, vp) = vramp_yuv420p();
  let (u444, v444) = ref_full_chroma_top(&up, &vp, true);
  let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);

  // RGBA-only.
  {
    let mut rgba_ref = std::vec![0u8; (W * H * 4) as usize];
    let mut rs = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
      .with_rgba(&mut rgba_ref)
      .unwrap();
    yuv444p_to(&ref_src, false, rs.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

    let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
    let mut rgba = std::vec![0u8; (W * H * 4) as usize];
    let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
      .with_rgba(&mut rgba)
      .unwrap()
      .with_chroma_location(ChromaLocation::Top);
    yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
    assert_eq!(rgba, rgba_ref, "Top RGBA must match the forward reference");
  }

  // HSV-direct (no RGB / RGBA attached).
  {
    let (mut hh, mut ss, mut vv) = (
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
    );
    let mut rs = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
      .with_hsv(&mut hh, &mut ss, &mut vv)
      .unwrap();
    yuv444p_to(&ref_src, false, rs.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

    let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
    let (mut h2, mut s2, mut v2) = (
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
    );
    let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
      .with_hsv(&mut h2, &mut s2, &mut v2)
      .unwrap()
      .with_chroma_location(ChromaLocation::Top);
    yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
    assert_eq!(
      (h2, s2, v2),
      (hh, ss, vv),
      "Top HSV-direct must match the forward reference"
    );
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_maintains_forward_delay_buffers() {
  // A top-sited colour decode grows BOTH the one-row chroma lookback (`c[i]` for
  // the odd flush) and the forward-delay Y buffer (the held odd row's luma).
  let (yp, up, vp) = vramp_yuv420p();
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut rgb = std::vec![0u8; (W * H * 3) as usize];
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(ChromaLocation::Top);
  yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
  assert_eq!(
    sink.chroma_prev.len(),
    W as usize,
    "Top maintains the width-byte (half U + half V) chroma lookback"
  );
  assert_eq!(
    sink.chroma_top_y.len(),
    W as usize,
    "Top allocates the forward-delay Y buffer"
  );
  drop(sink);
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_survives_frame_reuse() {
  // Two frames through the SAME sink: the second frame's decode must NOT be
  // polluted by the first frame's held odd row or lookback (both reset in
  // begin_frame), so it equals a fresh forward-reconstruct reference.
  let (yp, up, vp) = vramp_yuv420p();
  let rgb_ref = top_ref_rgb(true, &yp, &up, &vp);
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut rgb = std::vec![0u8; (W * H * 3) as usize];
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(ChromaLocation::Top);
  yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
  yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
  drop(sink);
  assert_eq!(
    rgb, rgb_ref,
    "second frame must match a fresh Top reconstruct"
  );
}

/// Builds a `Yuv420p` row `r` of the [`vramp_yuv420p`] frame (chroma row
/// `r / 2`, the walker's vertical replication) for direct `process` feeding.
fn top_delay_row<'a>(yp: &'a [u8], up: &'a [u8], vp: &'a [u8], r: usize) -> Yuv420pRow<'a> {
  let w = W as usize;
  let cw = w / 2;
  let cr = r / 2;
  Yuv420pRow::for_tests(
    &yp[r * w..r * w + w],
    &up[cr * cw..cr * cw + cw],
    &vp[cr * cw..cr * cw + cw],
    r,
    KernelMatrix::Bt601,
    false,
  )
}

/// Drives the direct (identity) forward-delay Top path through a mid-frame
/// siting flip and asserts the per-frame freeze rejects it retry-atomically.
/// `held_loc` (a `top_v` siting) freezes on row 0 and DEFERS row 1 (odd →
/// `chroma_top_pending`); flipping to `flipped_loc` (a non-`top_v` siting)
/// before the even row 2 must be rejected with `ChromaSitingChanged` WITHOUT
/// emitting or clearing the held odd row; flipping back to `held_loc` and
/// retrying the SAME even row 2 then flushes it byte-for-byte like a clean
/// in-order decode — proving the reject mutated no delay state (#180) and no
/// stale-row corruption leaked.
fn top_flip_rejected_and_retryable(held_loc: ChromaLocation, flipped_loc: ChromaLocation) {
  let (yp, up, vp) = vramp_yuv420p();
  let w = W as usize;

  // Reference: a clean in-order decode of rows 0..3 at the held siting (no flip).
  let mut rgb_ref = std::vec![0u8; w * H as usize * 3];
  {
    let mut ref_sink = MixedSinker::<Yuv420p>::new(w, H as usize)
      .with_rgb(&mut rgb_ref)
      .unwrap()
      .with_chroma_location(held_loc.clone());
    crate::PixelSink::begin_frame(&mut ref_sink, W, H).unwrap();
    for r in 0..3 {
      crate::PixelSink::process(&mut ref_sink, top_delay_row(&yp, &up, &vp, r)).unwrap();
    }
  }

  // Subject: rows 0, 1 (deferred) at the held siting, then flip before row 2.
  let mut rgb = std::vec![0u8; w * H as usize * 3];
  let mut sink = MixedSinker::<Yuv420p>::new(w, H as usize)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(held_loc.clone());
  crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
  crate::PixelSink::process(&mut sink, top_delay_row(&yp, &up, &vp, 0)).unwrap();
  crate::PixelSink::process(&mut sink, top_delay_row(&yp, &up, &vp, 1)).unwrap();
  assert!(
    sink.chroma_top_pending.is_some(),
    "{held_loc:?}: the odd row must be held pending its even-row flush"
  );
  let held_y = sink.chroma_top_y.clone();

  sink.set_chroma_location(flipped_loc.clone());
  let err = crate::PixelSink::process(&mut sink, top_delay_row(&yp, &up, &vp, 2)).unwrap_err();
  assert!(
    matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
    "{held_loc:?} -> {flipped_loc:?}: mid-frame flip must be ChromaSitingChanged, got {err:?}"
  );
  // Retry-atomic (#180): the rejected flip left the held odd row untouched.
  assert!(
    sink.chroma_top_pending.is_some(),
    "{held_loc:?} -> {flipped_loc:?}: a rejected flip must leave the held odd row pending"
  );
  assert_eq!(
    sink.chroma_top_y, held_y,
    "{held_loc:?} -> {flipped_loc:?}: a rejected flip must not touch the buffered odd-row luma"
  );

  // Flip back and retry the SAME even row: it flushes the held odd row and emits
  // exactly as an unbroken decode would (no stale-row corruption).
  sink.set_chroma_location(held_loc.clone());
  crate::PixelSink::process(&mut sink, top_delay_row(&yp, &up, &vp, 2)).unwrap();
  drop(sink);
  assert_eq!(
    rgb, rgb_ref,
    "{held_loc:?}: retry after a rejected flip must match a clean in-order decode"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_mid_frame_flip_to_center_rejected_and_retryable() {
  // `Top` (centered h, forward v) → `Center` (centered h, co-sited v): the flip
  // differs ONLY on the vertical `top_v` axis of the identity-path freeze.
  top_flip_rejected_and_retryable(ChromaLocation::Top, ChromaLocation::Center);
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn topleft_mid_frame_flip_to_left_rejected_and_retryable() {
  // `TopLeft` (co-sited h, forward v) → `Left` (co-sited h, co-sited v): the flip
  // differs ONLY on the vertical `top_v` axis of the identity-path freeze.
  top_flip_rejected_and_retryable(ChromaLocation::TopLeft, ChromaLocation::Left);
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_rgba_and_hsv_match_vblend_then_444_reference() {
  let (yp, up, vp) = vramp_yuv420p();
  let (u444, v444) = ref_full_chroma_bottom(&up, &vp);

  // RGBA-only path.
  {
    let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
    let mut rgba = std::vec![0u8; (W * H * 4) as usize];
    let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
      .with_rgba(&mut rgba)
      .unwrap()
      .with_chroma_location(ChromaLocation::Bottom);
    yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

    let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
    let mut rgba_ref = std::vec![0u8; (W * H * 4) as usize];
    let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
      .with_rgba(&mut rgba_ref)
      .unwrap();
    yuv444p_to(
      &ref_src,
      false,
      ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
    assert_eq!(rgba, rgba_ref, "bottom RGBA must equal vblend-then-4:4:4");
  }

  // HSV-direct path (no RGB / RGBA attached).
  {
    let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
    let (mut h, mut s, mut v) = (
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
    );
    let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
      .with_hsv(&mut h, &mut s, &mut v)
      .unwrap()
      .with_chroma_location(ChromaLocation::Bottom);
    yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

    let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
    let (mut hr, mut sr, mut vr) = (
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
    );
    let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
      .with_hsv(&mut hr, &mut sr, &mut vr)
      .unwrap();
    yuv444p_to(
      &ref_src,
      false,
      ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
    assert_eq!(
      (h, s, v),
      (hr, sr, vr),
      "bottom HSV must equal vblend-then-4:4:4"
    );
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_path_simd_matches_scalar() {
  let (yp, up, vp) = vramp_yuv420p();
  assert_eq!(
    convert_rgb_with(ChromaLocation::Bottom, true, &yp, &up, &vp),
    convert_rgb_with(ChromaLocation::Bottom, false, &yp, &up, &vp),
    "bottom path must be bit-identical across the SIMD and scalar tiers"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_grows_chroma_prev_lookback() {
  let (yp, up, vp) = vramp_yuv420p();
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut rgb = std::vec![0u8; (W * H * 3) as usize];
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(ChromaLocation::Bottom);
  yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
  let prev_len = sink.chroma_prev.len();
  drop(sink);
  assert_eq!(
    prev_len, W as usize,
    "bottom-sited path stages a width-byte (half-width U+V) chroma lookback"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn non_bottom_sitings_do_not_grow_chroma_prev() {
  // Center / Left / Unspecified never touch the vertical lookback (the `Top`
  // forward blend now DOES maintain it, so it is covered by its own tests).
  for loc in [
    ChromaLocation::Center,
    ChromaLocation::Left,
    ChromaLocation::Unspecified,
  ] {
    let (yp, up, vp) = vramp_yuv420p();
    let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
    let mut rgb = std::vec![0u8; (W * H * 3) as usize];
    let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
      .with_rgb(&mut rgb)
      .unwrap()
      .with_chroma_location(loc.clone());
    yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
    let prev_len = sink.chroma_prev.len();
    drop(sink);
    assert_eq!(
      prev_len, 0,
      "siting {loc:?} must not grow the vertical chroma lookback"
    );
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_chroma_prev_alloc_failure_leaves_outputs_untouched() {
  use crate::resample::ResampleError;

  // A bottom-sited decode whose vertical-lookback allocation fails must leave
  // EVERY output buffer — luma included — untouched: the lookback is reserved
  // (fallibly) BEFORE any output row is written.
  let (yp, up, vp) = vramp_yuv420p();
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut luma = std::vec![0xABu8; (W * H) as usize];
  let mut rgb = std::vec![0xCDu8; (W * H * 3) as usize];
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_luma(&mut luma)
    .unwrap()
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(ChromaLocation::Bottom);

  super::super::arm_chroma_prev_alloc_failure();
  let err = yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap_err();
  drop(sink);

  assert!(
    matches!(
      err,
      MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
    ),
    "bottom chroma-lookback refusal must surface as a recoverable AllocationFailed, got {err:?}"
  );
  assert!(
    luma.iter().all(|&b| b == 0xAB),
    "luma must be untouched on the bottom lookback alloc-failure path"
  );
  assert!(
    rgb.iter().all(|&b| b == 0xCD),
    "rgb must be untouched on the bottom lookback alloc-failure path"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_luma_only_chroma_prev_alloc_failure_leaves_luma_untouched() {
  use crate::resample::ResampleError;

  // A LUMA-ONLY bottom-sited row also stages the vertical lookback (the
  // always-maintain path), and its `reserve_420_chroma_prev` must run BEFORE the
  // luma write — so a lookback alloc refusal on a luma-only frame leaves luma
  // untouched (no partial write), exactly like the colour path.
  let (yp, up, vp) = vramp_yuv420p();
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut luma = std::vec![0xABu8; (W * H) as usize];
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_luma(&mut luma)
    .unwrap()
    .with_chroma_location(ChromaLocation::Bottom);

  super::super::arm_chroma_prev_alloc_failure();
  let err = yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap_err();
  drop(sink);

  assert!(
    matches!(
      err,
      MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
    ),
    "luma-only bottom lookback refusal must surface as a recoverable AllocationFailed, got {err:?}"
  );
  assert!(
    luma.iter().all(|&b| b == 0xAB),
    "luma must be untouched when the luma-only bottom lookback reservation fails"
  );
}

// ---- bottom-sited lookback is row-order-safe (no STALE blend) ---------------

/// The RGB a `Yuv444p` decode produces for ONE row built from the centered
/// (horizontal-only, NO vertical) upsample of a given chroma row — the
/// well-defined "clamp" result a bottom-sited EVEN row must fall back to when
/// its vertical predecessor is not provably available. A genuine vertical blend
/// (with whatever stale data) would diverge from this on a vertically-varying
/// ramp.
fn ref_centered_row_rgb(
  yp: &[u8],
  up: &[u8],
  vp: &[u8],
  chroma_row: usize,
  out_row: usize,
) -> Vec<u8> {
  let w = W as usize;
  let cw = w / 2;
  let urow = ref_upsample_center_h(&up[chroma_row * cw..chroma_row * cw + cw], w);
  let vrow = ref_upsample_center_h(&vp[chroma_row * cw..chroma_row * cw + cw], w);
  // Decode just that one full-width 4:4:4 row.
  let mut rgb = std::vec![0u8; w * 3];
  crate::row::yuv_444_to_rgb_row(
    &yp[out_row * w..out_row * w + w],
    &urow,
    &vrow,
    &mut rgb,
    w,
    KernelMatrix::Bt601,
    false,
    true,
  );
  rgb
}

/// Drives a bottom-sited `Yuv420p` RGB decode by feeding an EXPLICIT sequence of
/// row indices through the public `process` API (no walker), so out-of-order /
/// skipped / replayed delivery can be exercised. Returns the full RGB plane;
/// rows never fed stay zero. `begin_frame` is called first (resetting the
/// vertical lookback).
fn drive_bottom_rows(yp: &[u8], up: &[u8], vp: &[u8], rows: &[usize]) -> Vec<u8> {
  let w = W as usize;
  let h = H as usize;
  let cw = w / 2;
  let mut rgb = std::vec![0u8; w * h * 3];
  let mut sink = MixedSinker::<Yuv420p>::new(w, h)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(ChromaLocation::Bottom);
  crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
  for &r in rows {
    let cr = r / 2;
    let row = Yuv420pRow::for_tests(
      &yp[r * w..r * w + w],
      &up[cr * cw..cr * cw + cw],
      &vp[cr * cw..cr * cw + cw],
      r,
      KernelMatrix::Bt601,
      false,
    );
    crate::PixelSink::process(&mut sink, row).unwrap();
  }
  drop(sink);
  rgb
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_even_row_without_valid_prev_clamps_not_stale() {
  // Feed ONLY row 2 (an even row, pair 1) — its vertical predecessor (chroma row
  // 0) was never staged, so the blend must CLAMP to the centered upsample of the
  // current chroma row (row 1), not blend with whatever happens to be in the
  // lookback buffer.
  let (yp, up, vp) = vramp_yuv420p();
  let w = W as usize;
  let rgb = drive_bottom_rows(&yp, &up, &vp, &[2]);
  let got_row2 = &rgb[2 * w * 3..3 * w * 3];
  let want = ref_centered_row_rgb(&yp, &up, &vp, 1, 2);
  assert_eq!(
    got_row2,
    &want[..],
    "an even row with no valid vertical predecessor must clamp to the centered \
     (horizontal-only) decode, never blend stale data"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_even_row_after_skip_clamps_not_stale() {
  // Feed rows 0, 1 (pair 0 → lookback now holds chroma row 0), then JUMP to row
  // 4 (even, pair 2) skipping pair 1. Row 4's predecessor is chroma row 1, but
  // the lookback holds the STALE chroma row 0; the validity tag (Some(0) !=
  // Some(2-1)) must force the clamp to the centered upsample of chroma row 2.
  let (yp, up, vp) = vramp_yuv420p();
  let w = W as usize;
  let rgb = drive_bottom_rows(&yp, &up, &vp, &[0, 1, 4]);
  let got_row4 = &rgb[4 * w * 3..5 * w * 3];
  let want = ref_centered_row_rgb(&yp, &up, &vp, 2, 4);
  assert_eq!(
    got_row4,
    &want[..],
    "an even row whose lookback holds a non-adjacent (stale) chroma row must \
     clamp, never blend the stale row"
  );
  // Sanity: the stale blend (chroma row 0 vs row 2) would be a DIFFERENT value,
  // so the clamp is observably not a coincidence on this vertical ramp.
  let stale = {
    let cw = w / 2;
    let mut e_u = std::vec![0u8; cw];
    let mut e_v = std::vec![0u8; cw];
    for c in 0..cw {
      e_u[c] = (((up[c] as u32) + (up[2 * cw + c] as u32) + 1) >> 1) as u8;
      e_v[c] = (((vp[c] as u32) + (vp[2 * cw + c] as u32) + 1) >> 1) as u8;
    }
    let urow = ref_upsample_center_h(&e_u, w);
    let vrow = ref_upsample_center_h(&e_v, w);
    let mut rgb = std::vec![0u8; w * 3];
    crate::row::yuv_444_to_rgb_row(
      &yp[4 * w..4 * w + w],
      &urow,
      &vrow,
      &mut rgb,
      w,
      KernelMatrix::Bt601,
      false,
      true,
    );
    rgb
  };
  assert_ne!(
    got_row4,
    &stale[..],
    "guard: the clamp must NOT coincide with the stale (row0xrow2) blend"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_two_frames_no_cross_frame_stale_blend() {
  // Frame 1 decoded fully in order (chroma plane A) leaves chroma row A[last] in
  // the lookback. After `begin_frame`, frame 2's row 0 (even, pair 0) has NO
  // predecessor and must clamp to the centered upsample of plane B's row 0 — it
  // must NOT box-blend B's row 0 with frame 1's last chroma row.
  let w = W as usize;
  let h = H as usize;
  let cw = w / 2;
  let ch = h / 2;
  let (ya, ua, va) = vramp_yuv420p();
  // Plane B: a distinct vertical ramp so a cross-frame blend would be visible.
  let yb = std::vec![128u8; w * h];
  let mut ub = std::vec![0u8; cw * ch];
  let mut vb = std::vec![0u8; cw * ch];
  for r in 0..ch {
    for c in 0..cw {
      ub[r * cw + c] = (200 - r * 30).max(16) as u8;
      vb[r * cw + c] = (30 + r * 30).min(240) as u8;
    }
  }

  let mut rgb = std::vec![0u8; w * h * 3];
  let mut sink = MixedSinker::<Yuv420p>::new(w, h)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(ChromaLocation::Bottom);
  // Frame 1, in order.
  crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
  for r in 0..h {
    let cr = r / 2;
    let row = Yuv420pRow::for_tests(
      &ya[r * w..r * w + w],
      &ua[cr * cw..cr * cw + cw],
      &va[cr * cw..cr * cw + cw],
      r,
      KernelMatrix::Bt601,
      false,
    );
    crate::PixelSink::process(&mut sink, row).unwrap();
  }
  // Frame 2: begin_frame resets the lookback; feed only row 0 (plane B).
  crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
  let row0 = Yuv420pRow::for_tests(
    &yb[0..w],
    &ub[0..cw],
    &vb[0..cw],
    0,
    KernelMatrix::Bt601,
    false,
  );
  crate::PixelSink::process(&mut sink, row0).unwrap();
  drop(sink);

  let got_row0 = &rgb[0..w * 3];
  let want = ref_centered_row_rgb(&yb, &ub, &vb, 0, 0);
  assert_eq!(
    got_row0,
    &want[..],
    "frame 2 row 0 must clamp to plane B's centered decode, never blend frame 1's chroma"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_luma_only_then_late_color_box_blends() {
  // The always-maintain lookback: rows 0, 1 are processed LUMA-ONLY (no colour
  // attached), so the colour upsample helper never runs — yet the bottom-sited
  // lookback must still be staged through them. After attaching RGB via
  // `set_rgb`, row 2 (Bottom even, pair 1) must correctly box-blend chroma rows
  // 0 and 1 (== the all-output reference), NOT clamp to chroma row 1.
  let (yp, up, vp) = vramp_yuv420p();
  let w = W as usize;
  let h = H as usize;
  let cw = w / 2;

  // All-output reference: the full Bottom reconstruction decoded via Yuv444p.
  let (u444, v444) = ref_full_chroma_bottom(&up, &vp);
  let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
  let mut rgb_ref = std::vec![0u8; w * h * 3];
  let mut ref_sink = MixedSinker::<Yuv444p>::new(w, h)
    .with_rgb(&mut rgb_ref)
    .unwrap();
  yuv444p_to(
    &ref_src,
    false,
    ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
  )
  .unwrap();
  drop(ref_sink);

  // Luma-only rows 0, 1, then attach RGB and decode rows 2, 3.
  let mut luma = std::vec![0u8; w * h];
  let mut rgb = std::vec![0u8; w * h * 3];
  {
    let mut sink = MixedSinker::<Yuv420p>::new(w, h)
      .with_luma(&mut luma)
      .unwrap()
      .with_chroma_location(ChromaLocation::Bottom);
    crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
    let feed = |sink: &mut MixedSinker<'_, Yuv420p>, r: usize| {
      let cr = r / 2;
      let row = Yuv420pRow::for_tests(
        &yp[r * w..r * w + w],
        &up[cr * cw..cr * cw + cw],
        &vp[cr * cw..cr * cw + cw],
        r,
        KernelMatrix::Bt601,
        false,
      );
      crate::PixelSink::process(sink, row).unwrap();
    };
    feed(&mut sink, 0);
    feed(&mut sink, 1);
    // Late colour attach.
    sink.set_rgb(&mut rgb).unwrap();
    feed(&mut sink, 2);
    feed(&mut sink, 3);
  }

  // Row 2 (Bottom even) must equal the all-output reference's row 2 — proving
  // the lookback (staged through the luma-only rows 0, 1) drove the box blend.
  let got_row2 = &rgb[2 * w * 3..3 * w * 3];
  assert_eq!(
    got_row2,
    &rgb_ref[2 * w * 3..3 * w * 3],
    "a luma-only-then-late-colour row 2 must box-blend chroma rows 0,1 (all-output reference)"
  );
  // And it must NOT be the clamp (centered upsample of chroma row 1 only) — the
  // clamp is what an unmaintained lookback would have produced.
  let clamp = ref_centered_row_rgb(&yp, &up, &vp, 1, 2);
  assert_ne!(
    got_row2,
    &clamp[..],
    "row 2 must NOT clamp — the lookback was maintained through the luma-only rows"
  );
}

// ---- no-output rows are invisible to the lookback (no-output invariant) ------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_no_output_row_does_not_grow_chroma_prev() {
  // A bottom-sited sink with NO outputs attached must honour the repo-wide
  // no-output invariant: every `process` call returns before the preflight, so
  // the vertical lookback is NEVER reserved (a no-output row must allocate
  // nothing — not even the lookback).
  let (yp, up, vp) = vramp_yuv420p();
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_chroma_location(ChromaLocation::Bottom);
  yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
  let prev_len = sink.chroma_prev.len();
  drop(sink);
  assert_eq!(
    prev_len, 0,
    "a no-output bottom-sited row must not reserve the vertical lookback"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_no_output_rows_do_not_enable_late_color_blend() {
  // The no-output invariant's correctness twin: rows 0, 1 are processed with NO
  // outputs (invisible — they must not prime the lookback). After attaching RGB
  // via `set_rgb`, row 2 (Bottom even, pair 1) must NOT box-blend through those
  // invisible rows; with no legitimately-output predecessor staged, it CLAMPS to
  // the centered upsample of the current chroma row. (Contrast
  // `bottom_luma_only_then_late_color_box_blends`, where rows 0,1 ARE outputs —
  // luma — and so DO prime the lookback and the later row box-blends.)
  let (yp, up, vp) = vramp_yuv420p();
  let w = W as usize;
  let cw = w / 2;

  let mut rgb = std::vec![0u8; w * H as usize * 3];
  {
    let mut sink =
      MixedSinker::<Yuv420p>::new(w, H as usize).with_chroma_location(ChromaLocation::Bottom);
    crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
    let feed = |sink: &mut MixedSinker<'_, Yuv420p>, r: usize| {
      let cr = r / 2;
      let row = Yuv420pRow::for_tests(
        &yp[r * w..r * w + w],
        &up[cr * cw..cr * cw + cw],
        &vp[cr * cw..cr * cw + cw],
        r,
        KernelMatrix::Bt601,
        false,
      );
      crate::PixelSink::process(sink, row).unwrap();
    };
    // Rows 0, 1: NO output attached — invisible, must not prime the lookback.
    feed(&mut sink, 0);
    feed(&mut sink, 1);
    // Late colour attach, then decode rows 2, 3.
    sink.set_rgb(&mut rgb).unwrap();
    feed(&mut sink, 2);
    feed(&mut sink, 3);
  }

  // Row 2 must CLAMP (centered upsample of chroma row 1), NOT box-blend chroma
  // rows 0,1 — because the predecessor (chroma row 0) only ever arrived through
  // invisible no-output rows that left the lookback unprimed.
  let got_row2 = &rgb[2 * w * 3..3 * w * 3];
  let clamp = ref_centered_row_rgb(&yp, &up, &vp, 1, 2);
  assert_eq!(
    got_row2,
    &clamp[..],
    "a no-output predecessor row must not enable a later colour even row to box-blend through it"
  );
  // Guard: the box-blend (the all-output reconstruction) is a DIFFERENT value,
  // so the clamp is observably the no-output-invisible behaviour, not a fluke.
  let (u444, v444) = ref_full_chroma_bottom(&up, &vp);
  let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
  let mut rgb_ref = std::vec![0u8; w * H as usize * 3];
  let mut ref_sink = MixedSinker::<Yuv444p>::new(w, H as usize)
    .with_rgb(&mut rgb_ref)
    .unwrap();
  yuv444p_to(
    &ref_src,
    false,
    ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
  )
  .unwrap();
  drop(ref_sink);
  assert_ne!(
    got_row2,
    &rgb_ref[2 * w * 3..3 * w * 3],
    "guard: the clamp must differ from the box-blend, so the no-output rows were truly invisible"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "constructs an absurd geometry; the no-op contract is the point, not Miri"
)]
fn bottom_no_output_row_large_geometry_does_not_overflow() {
  // The no-output guard must run BEFORE the `idx * w` single-plane offset
  // arithmetic. A no-output `process` call never ran an attach-time
  // `w x h x 1` validation (nothing was attached), so on a 32-bit target
  // (`usize == u32`) an absurd geometry where `idx * w` exceeds `u32::MAX`
  // would overflow that offset and panic under overflow checks. With no outputs
  // attached, `process` must return `Ok(())` having done NO row math and NO
  // allocation. On 64-bit (`idx * w` fits `u64`) this documents the
  // no-output-is-a-pure-no-op contract and trivially passes; on the i686 /
  // overflow-check CI it catches a regression if the guard ever moves back below
  // the arithmetic.
  //
  // w = 4, idx = 2^30 → idx * w = 2^32 = u32::MAX + 1 (overflows u32).
  let w: usize = 4;
  let idx: usize = 1 << 30; // 1_073_741_824
  let h: usize = idx + 1; // idx < height so the row-index check passes
  assert!(
    (idx as u64) * (w as u64) > u32::MAX as u64,
    "test geometry must exceed u32::MAX to exercise the 32-bit offset overflow"
  );

  let y = std::vec![128u8; w];
  let c = std::vec![128u8; w / 2];
  let mut sink = MixedSinker::<Yuv420p>::new(w, h).with_chroma_location(ChromaLocation::Bottom);
  // No outputs attached: the guard returns before `idx * w` (no overflow panic)
  // and before the bottom preflight (no allocation).
  let row = Yuv420pRow::for_tests(&y, &c, &c, idx, KernelMatrix::Bt601, false);
  crate::PixelSink::process(&mut sink, row).unwrap();
  let prev_len = sink.chroma_prev.len();
  drop(sink);
  assert_eq!(
    prev_len, 0,
    "a no-output large-geometry row must allocate nothing (lookback included)"
  );
}

// ---- bottom-LEFT-sited phase (co-sited h = 0 + bottom v = 1) ----------------

/// Independent reference for the co-sited (`h = 0`) horizontal upsample — a plain
/// 2× nearest-neighbor replicate. Written separately from the production kernel so
/// it is a real oracle.
fn ref_upsample_cosited_h(c_half: &[u8], width: usize) -> Vec<u8> {
  let half = width / 2;
  let mut out = std::vec![0u8; width];
  for j in 0..half {
    out[2 * j] = c_half[j];
    out[2 * j + 1] = c_half[j];
  }
  out
}

/// Independent reference for the bottom-left-sited (`h = 0`, `v = 1`) full
/// reconstruction: the [`ref_full_chroma_bottom`] vertical box blend, but fed to
/// the CO-SITED horizontal replicate ([`ref_upsample_cosited_h`]) instead of the
/// centered `1/4`–`3/4` weights. Written separately from the production kernels so
/// it is a true oracle.
fn ref_full_chroma_bottomleft(u420: &[u8], v420: &[u8]) -> (Vec<u8>, Vec<u8>) {
  let w = W as usize;
  let h = H as usize;
  let cw = w / 2;
  let mut u444 = std::vec![0u8; w * h];
  let mut v444 = std::vec![0u8; w * h];
  let vblend = |plane: &[u8], cr: usize, prev: usize| -> Vec<u8> {
    (0..cw)
      .map(|c| {
        let a = plane[prev * cw + c] as u32;
        let b = plane[cr * cw + c] as u32;
        ((a + b + 1) >> 1) as u8
      })
      .collect::<Vec<u8>>()
  };
  for r in 0..h {
    let cr = r / 2;
    let (uhalf, vhalf) = if r & 1 == 0 {
      let prev = cr.saturating_sub(1);
      (vblend(u420, cr, prev), vblend(v420, cr, prev))
    } else {
      (
        u420[cr * cw..cr * cw + cw].to_vec(),
        v420[cr * cw..cr * cw + cw].to_vec(),
      )
    };
    let urow = ref_upsample_cosited_h(&uhalf, w);
    let vrow = ref_upsample_cosited_h(&vhalf, w);
    u444[r * w..r * w + w].copy_from_slice(&urow);
    v444[r * w..r * w + w].copy_from_slice(&vrow);
  }
  (u444, v444)
}

#[test]
fn cosited_upsample_kernel_matches_hand_computed() {
  // Co-sited horizontal reconstruction is a plain 2× replicate.
  let c_half = [10u8, 20, 30, 40];
  let mut out = [0u8; 8];
  crate::row::scalar::chroma_upsample_2to1_cosited_h(&c_half, &mut out, 8);
  assert_eq!(out, [10, 10, 20, 20, 30, 30, 40, 40]);
}

#[test]
fn bottomleft_even_kernel_matches_hand_computed() {
  // prev = [0, 0, 100, 100], cur = [40, 40, 60, 60]; vertical box blend
  // e = (prev + cur + 1) >> 1 = [20, 20, 80, 80], then the co-sited 2× replicate.
  let prev = [0u8, 0, 100, 100];
  let cur = [40u8, 40, 60, 60];
  let mut out = [0u8; 8];
  crate::row::scalar::chroma_upsample_420_bottomleft_even_h(&prev, &cur, &mut out, 8);
  assert_eq!(out, [20, 20, 20, 20, 80, 80, 80, 80]);
}

#[test]
fn bottomleft_even_kernel_equals_cosited_when_rows_match() {
  // When prev == cur the vertical box blend is a no-op, so the bottom-left-even
  // kernel must reproduce the plain co-sited replicate exactly.
  let cur = [10u8, 40, 90, 30];
  let mut bl = [0u8; 8];
  let mut cosited = [0u8; 8];
  crate::row::scalar::chroma_upsample_420_bottomleft_even_h(&cur, &cur, &mut bl, 8);
  crate::row::scalar::chroma_upsample_2to1_cosited_h(&cur, &mut cosited, 8);
  assert_eq!(
    bl, cosited,
    "prev == cur must collapse the vertical blend to the co-sited replicate"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_rgb_matches_cosited_vblend_then_444_reference() {
  let (yp, up, vp) = vramp_yuv420p();

  // Reference: co-sited-horizontal + vertical-box-blend (even rows) to full
  // resolution, then the ordinary 4:4:4 decode.
  let (u444, v444) = ref_full_chroma_bottomleft(&up, &vp);
  let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
  let mut rgb_ref = std::vec![0u8; (W * H * 3) as usize];
  let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb_ref)
    .unwrap();
  yuv444p_to(
    &ref_src,
    false,
    ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
  )
  .unwrap();

  assert_eq!(
    convert_rgb_with(ChromaLocation::BottomLeft, true, &yp, &up, &vp),
    rgb_ref,
    "bottom-left 4:2:0 RGB must equal co-sited-replicate + vertical-blend then 4:4:4"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_differs_from_bottom_horizontally() {
  // On a purely-HORIZONTAL chroma ramp, BottomLeft (co-sited h) must differ from
  // Bottom (centered h) — the vertical fold is identical, only the horizontal
  // phase differs.
  let (yp, up, vp) = ramp_yuv420p();
  assert_ne!(
    convert_rgb_with(ChromaLocation::BottomLeft, true, &yp, &up, &vp),
    convert_rgb_with(ChromaLocation::Bottom, true, &yp, &up, &vp),
    "BottomLeft (h=0) must differ from Bottom (h=0.5) on a horizontal chroma ramp"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_differs_from_topleft_vertically() {
  // On a purely-VERTICAL chroma ramp, BottomLeft (`v = 1`, backward even-row
  // blend) must differ from TopLeft (`v = 0`, forward odd-row blend) — same
  // co-sited horizontal phase, only the vertical fold DIRECTION differs — and
  // from the vertical-replicate default.
  let (yp, up, vp) = vramp_yuv420p();
  let bl = convert_rgb_with(ChromaLocation::BottomLeft, true, &yp, &up, &vp);
  assert_ne!(
    bl,
    convert_rgb_with(ChromaLocation::TopLeft, true, &yp, &up, &vp),
    "BottomLeft (v=1 backward) must differ from TopLeft (v=0 forward) on a vertical ramp"
  );
  assert_ne!(
    bl,
    convert_rgb_with(ChromaLocation::Left, true, &yp, &up, &vp),
    "BottomLeft must differ from the vertical-replicate default"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_rgba_and_hsv_match_cosited_vblend_then_444_reference() {
  let (yp, up, vp) = vramp_yuv420p();
  let (u444, v444) = ref_full_chroma_bottomleft(&up, &vp);

  // RGBA-only path.
  {
    let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
    let mut rgba = std::vec![0u8; (W * H * 4) as usize];
    let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
      .with_rgba(&mut rgba)
      .unwrap()
      .with_chroma_location(ChromaLocation::BottomLeft);
    yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

    let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
    let mut rgba_ref = std::vec![0u8; (W * H * 4) as usize];
    let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
      .with_rgba(&mut rgba_ref)
      .unwrap();
    yuv444p_to(
      &ref_src,
      false,
      ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
    assert_eq!(
      rgba, rgba_ref,
      "bottom-left RGBA must equal cosited-vblend-then-4:4:4"
    );
  }

  // HSV-direct path (no RGB / RGBA attached).
  {
    let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
    let (mut h, mut s, mut v) = (
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
    );
    let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
      .with_hsv(&mut h, &mut s, &mut v)
      .unwrap()
      .with_chroma_location(ChromaLocation::BottomLeft);
    yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

    let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
    let (mut hr, mut sr, mut vr) = (
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
      std::vec![0u8; (W * H) as usize],
    );
    let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
      .with_hsv(&mut hr, &mut sr, &mut vr)
      .unwrap();
    yuv444p_to(
      &ref_src,
      false,
      ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
    assert_eq!(
      (h, s, v),
      (hr, sr, vr),
      "bottom-left HSV must equal cosited-vblend-then-4:4:4"
    );
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_path_simd_matches_scalar() {
  let (yp, up, vp) = vramp_yuv420p();
  assert_eq!(
    convert_rgb_with(ChromaLocation::BottomLeft, true, &yp, &up, &vp),
    convert_rgb_with(ChromaLocation::BottomLeft, false, &yp, &up, &vp),
    "bottom-left path must be bit-identical across the SIMD and scalar tiers"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_equals_cosited_on_flat_chroma() {
  // On spatially FLAT chroma the vertical blend is a no-op and the co-sited
  // horizontal phase is the default nearest-neighbor decode, so BottomLeft
  // collapses to the byte-identical co-sited decode.
  let w = W as usize;
  let h = H as usize;
  let yp = std::vec![128u8; w * h];
  let up = std::vec![90u8; (w / 2) * (h / 2)];
  let vp = std::vec![150u8; (w / 2) * (h / 2)];
  assert_eq!(
    convert_rgb_with(ChromaLocation::BottomLeft, true, &yp, &up, &vp),
    convert_rgb_with(ChromaLocation::Left, true, &yp, &up, &vp),
    "flat-chroma BottomLeft must collapse to the co-sited decode"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_grows_chroma_prev_lookback() {
  let (yp, up, vp) = vramp_yuv420p();
  let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
  let mut rgb = std::vec![0u8; (W * H * 3) as usize];
  let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_chroma_location(ChromaLocation::BottomLeft);
  yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
  let prev_len = sink.chroma_prev.len();
  drop(sink);
  assert_eq!(
    prev_len, W as usize,
    "bottom-left-sited path stages a width-byte (half-width U+V) chroma lookback"
  );
}