oxideav-av1 0.1.21

Pure-Rust AV1 codec — orphan-rebuild scaffold pending clean-room re-implementation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
//! Per-block mode-info **writers** — §5.11.7 / §5.11.8 / §5.11.11 /
//! §5.11.22 intra-arm syntax elements.
//!
//! These are the encoder counterparts to the §5.11 per-block decoders
//! that already live in [`crate::cdf::PartitionWalker`] (`decode_skip`,
//! `decode_intra_segment_id`, `decode_intra_frame_y_mode`,
//! `decode_intra_block_mode_info`'s `y_mode` + `uv_mode` S() reads).
//!
//! Scope of this arc (r211): the *intra* arm only — `skip`,
//! `intra_segment_id`, `intra_frame_y_mode` (§5.11.7 path with the
//! §8.3.2 neighbour-CDF ctx), `y_mode` (§5.11.22 path with the
//! `Size_Group[ MiSize ]` ctx), and `uv_mode` (§5.11.22 line 6 with the
//! CFL-allowed selector). No inter mode_info, no MV encode, no
//! partition split, no coefficient encode.
//!
//! ## Stateless on purpose
//!
//! Mirroring [`SymbolWriter::write_symbol`]'s "caller-supplied CDF
//! slice" pattern, every writer here takes its §8.3.2 *context indices*
//! as inputs rather than a [`PartitionWalker`] reference. The caller is
//! responsible for deriving the §8.3.2 ctx from neighbour state (using
//! the existing public helpers — [`skip_ctx`], [`intra_mode_ctx`],
//! [`size_group`], [`segment_id_ctx`]) and feeding the same ctx the
//! decoder side would derive on the corresponding [`PartitionWalker`]
//! call.
//!
//! This keeps the writer module pure (no shared mutable grid state) and
//! makes the roundtrip tests below explicit about what the encode-side
//! ctx must equal. For multi-block encodes the caller threads a
//! [`PartitionWalker`] of its own and re-uses the §8.3.2 helpers; the
//! decode-side walker stamps its own grids on the roundtrip step.
//!
//! ## Spec provenance
//!
//! Sourced from `docs/video/av1/av1-spec.txt`:
//!   * §5.11.7  intra_frame_mode_info  (p.65)
//!   * §5.11.8  intra_segment_id        (p.65)
//!   * §5.11.9  read_segment_id        (p.66) — `neg_deinterleave`
//!   * §5.11.11 read_skip              (p.67)
//!   * §5.11.22 intra_block_mode_info   (p.72)
//!   * §8.3.2  context derivations    (p.361-378)
//!
//! [`PartitionWalker`]: crate::cdf::PartitionWalker
//! [`SymbolWriter::write_symbol`]: crate::encoder::symbol_writer::SymbolWriter::write_symbol
//! [`skip_ctx`]: crate::cdf::skip_ctx
//! [`intra_mode_ctx`]: crate::cdf::intra_mode_ctx
//! [`size_group`]: crate::cdf::size_group
//! [`segment_id_ctx`]: crate::cdf::segment_id_ctx

use crate::cdf::{
    block_height, block_width, ceil_log2_av1, cfl_alpha_u_ctx, cfl_alpha_v_ctx, comp_group_idx_ctx,
    comp_mode_ctx, comp_ref_type_ctx, compound_idx_ctx, compound_mode_ctx, count_refs,
    get_palette_color_context, interintra_ctx, interp_filter_ctx, is_directional,
    is_samedir_ref_pair, mi_height_log2, mi_width_log2, neg_interleave, palette_color_ctx,
    palette_uv_mode_ctx, palette_y_mode_ctx, ref_count_ctx, wedge_bits, CompoundTypeReadout,
    FindMvStackResult, InterIntraReadout, InterpolationFilterReadout, PalettePlane,
    PartitionWalker, TileCdfContext, BILINEAR, BLOCK_128X128, BLOCK_32X32, BLOCK_64X64, BLOCK_8X8,
    BLOCK_SIZES, BLOCK_SIZE_GROUPS, CFL_ALPHABET_SIZE, CFL_JOINT_SIGNS, CLASS0_SIZE,
    COMPOUND_AVERAGE, COMPOUND_DIFFWTD, COMPOUND_DISTANCE, COMPOUND_INTRA, COMPOUND_MODES,
    COMPOUND_MODE_CONTEXTS, COMPOUND_TYPES, COMPOUND_WEDGE, COMP_INTER_CONTEXTS, DC_PRED,
    DELTA_LF_SMALL, DELTA_Q_SMALL, DRL_MODE_CONTEXTS, EIGHTTAP, FRAME_LF_COUNT,
    GM_TYPE_TRANSLATION, INTERINTRA_MODES, INTERP_FILTERS, INTERP_FILTER_NONE,
    INTRABC_DELAY_PIXELS, INTRA_FILTER_MODES, INTRA_MODES, INTRA_MODE_CONTEXTS, IS_INTER_CONTEXTS,
    MAX_ANGLE_DELTA, MAX_REF_MV_STACK_SIZE, MAX_SEGMENTS, MI_SIZE, MODE_GLOBALMV,
    MODE_GLOBAL_GLOBALMV, MODE_NEARESTMV, MODE_NEAREST_NEARESTMV, MODE_NEARMV, MODE_NEAR_NEARMV,
    MODE_NEAR_NEWMV, MODE_NEWMV, MODE_NEW_NEARMV, MODE_NEW_NEWMV, MOTION_MODES, MOTION_MODE_OBMC,
    MOTION_MODE_SIMPLE, MOTION_MODE_WARPED_CAUSAL, MV_CLASSES, MV_CLASS_0, MV_COMPS, MV_CONTEXTS,
    MV_INTRABC_CONTEXT, MV_JOINT_HNZVNZ, MV_JOINT_HNZVZ, MV_JOINT_HZVNZ, MV_JOINT_ZERO,
    NEW_MV_CONTEXTS, NUM_4X4_BLOCKS_HIGH, NUM_4X4_BLOCKS_WIDE, PALETTE_BLOCK_SIZE_CONTEXTS,
    PALETTE_COLORS, REF_MV_CONTEXTS, SEGMENT_ID_CONTEXTS, SEGMENT_ID_PREDICTED_CONTEXTS,
    SKIP_CONTEXTS, SKIP_MODE_CONTEXTS, SWITCHABLE, UV_INTRA_MODES_CFL_ALLOWED,
    UV_INTRA_MODES_CFL_NOT_ALLOWED, WEDGE_TYPES, ZERO_MV_CONTEXTS,
};
use crate::encoder::symbol_writer::SymbolWriter;
use crate::Error;

/// `read_skip()` inverse per §5.11.11 (av1-spec p.67).
///
/// Spec body:
/// ```text
///   read_skip() {
///       if ( SegIdPreSkip && seg_feature_active( SEG_LVL_SKIP ) ) {
///           skip = 1
///       } else {
///           skip                                            S()
///       }
///   }
/// ```
///
/// `seg_skip_active` is the caller-combined precondition `SegIdPreSkip
/// && seg_feature_active( SEG_LVL_SKIP )` — mirrors the parameter
/// surface of [`crate::cdf::PartitionWalker::decode_skip`].
///
/// * When `seg_skip_active == true`: no symbol is emitted (the decoder
///   short-circuits to `skip = 1` without a bit read). `skip` MUST be
///   `1` — caller bug otherwise, surfaced as
///   [`Error::PartitionWalkOutOfRange`].
/// * When `seg_skip_active == false`: emits one §8.2.6 `S()` over
///   `Default_Skip_Cdf[ ctx ]`. `ctx` is the §8.3.2 derivation
///   `(AvailU ? Skips[r-1][c] : 0) + (AvailL ? Skips[r][c-1] : 0)`,
///   computable through the public [`crate::cdf::skip_ctx`] helper.
///   `ctx` MUST be in `0..SKIP_CONTEXTS = 3`.
pub fn write_skip(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    skip: u8,
    ctx: usize,
    seg_skip_active: bool,
) -> Result<(), Error> {
    if seg_skip_active {
        // §5.11.11 first branch — no bits.
        if skip != 1 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }
    if skip > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if ctx >= SKIP_CONTEXTS {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let cdf = cdfs.skip_cdf(ctx);
    writer.write_symbol(skip as u32, cdf)
}

/// `read_segment_id()` inverse per §5.11.9 (av1-spec p.66) — the
/// shared per-block segment-id writer the §5.11.8 `intra_segment_id`
/// and §5.11.19 `inter_segment_id` writers both compose. Mirrors
/// [`crate::cdf::PartitionWalker::decode_segment_id`].
///
/// Spec body (av1-spec p.66, §5.11.9):
///
/// ```text
///   read_segment_id( ) {
///       prev_ul = AvailU && AvailL ? PrevSegmentIds[r-1][c-1] : -1
///       prev_u  = AvailU           ? PrevSegmentIds[r-1][c  ] : -1
///       prev_l  = AvailL           ? PrevSegmentIds[r  ][c-1] : -1
///       if ( prev_u == -1 ) {
///           if ( prev_l == -1 ) pred = 0
///           else                pred = prev_l
///       } else if ( prev_l == -1 ) {
///           pred = prev_u
///       } else if ( prev_ul == prev_u ) {
///           pred = prev_u
///       } else {
///           pred = prev_l
///       }
///       if ( skip ) {
///           segment_id = pred
///       } else {
///           segment_id                                          S()
///           segment_id = neg_deinterleave( segment_id, pred,
///                                          LastActiveSegId + 1 )
///       }
///   }
/// ```
///
/// The §5.11.9 `pred` derivation is the *neighbour cascade* — the
/// caller threads it in pre-computed (the encoder owns a parallel
/// grid walk; see the round-trip tests below for the pattern). The
/// `last_active_seg_id` argument supplies `max = LastActiveSegId + 1`
/// for the `neg_deinterleave` inverse.
///
/// Two arms; both transcribe one decoder branch each:
///
/// * `skip != 0` — `segment_id = pred`, no symbol emitted. Caller-bug
///   shape: `segment_id != pred` ⇒ [`Error::PartitionWalkOutOfRange`].
/// * `skip == 0` — emits one §8.2.6 `S()` symbol against
///   `TileSegmentIdCdf[ ctx ]`. The on-wire `diff` is the inverse of
///   `neg_deinterleave`, computed analytically via
///   [`crate::cdf::neg_interleave`] (`O(1)` — no search). `ctx` is
///   the §8.3.2 derivation from [`crate::cdf::segment_id_ctx`].
///
/// Range guards mirror the decoder side and surface as
/// [`Error::PartitionWalkOutOfRange`]:
///   * `last_active_seg_id >= MAX_SEGMENTS = 8`.
///   * `segment_id >= max` (max = `last_active_seg_id + 1`).
///   * `pred >= max`.
///   * `ctx >= SEGMENT_ID_CONTEXTS = 3` on the `S()` arm.
///
/// Round-trip semantics: feeding the bytes this writer produces back
/// into [`crate::cdf::PartitionWalker::decode_segment_id`] (with the
/// matching `sub_size`/`mi_row`/`mi_col`/`skip`/`last_active_seg_id`
/// arguments) reconstructs the same `segment_id`. The decoder's
/// §5.11.5 grid-fill of the leaf footprint happens decoder-side; the
/// stateless writer does not maintain an encoder grid (caller responsibility).
pub fn write_segment_id(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    segment_id: u8,
    skip: u8,
    pred: u8,
    ctx: usize,
    last_active_seg_id: u8,
) -> Result<(), Error> {
    if (last_active_seg_id as usize) >= MAX_SEGMENTS {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let max = last_active_seg_id as u32 + 1;
    if segment_id as u32 >= max {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if pred as u32 >= max {
        return Err(Error::PartitionWalkOutOfRange);
    }
    // §5.11.9 `if ( skip ) segment_id = pred` — no bits emitted.
    if skip != 0 {
        if segment_id != pred {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }
    // §5.11.9 `else segment_id S()` — `ctx` from §8.3.2
    // `segment_id_ctx`.
    if ctx >= SEGMENT_ID_CONTEXTS {
        return Err(Error::PartitionWalkOutOfRange);
    }
    // Analytic inverse of `neg_deinterleave` via `neg_interleave`
    // (bijection on `0..max`, O(1) — replaces the prior O(8) search).
    let diff = neg_interleave(segment_id as u32, pred as u32, max);
    let cdf = cdfs.segment_id_cdf(ctx);
    writer.write_symbol(diff, cdf)
}

/// `intra_segment_id()` inverse per §5.11.8 (av1-spec p.65).
///
/// Spec body:
/// ```text
///   intra_segment_id( ) {
///       if ( segmentation_enabled )
///           read_segment_id( )
///       else
///           segment_id = 0
///       Lossless = LosslessArray[ segment_id ]
///   }
/// ```
///
/// Composes [`write_segment_id`] (§5.11.9) under the
/// `segmentation_enabled` gate. On the disabled branch no bits are
/// emitted and `segment_id` MUST be `0`; on the enabled branch the
/// `read_segment_id` inverse runs through its skip / non-skip
/// dispatch.
///
/// Parameters mirror [`crate::cdf::PartitionWalker::decode_intra_segment_id`]
/// plus `pred` (the §5.11.9 neighbour-cascade value the caller
/// already has on hand from the encoder's own grid walk) and `ctx`
/// (the §8.3.2 `segment_id_ctx` — see [`crate::cdf::segment_id_ctx`]).
/// Lossless resolution (the spec's
/// `Lossless = LosslessArray[ segment_id ]` step) is the caller's
/// concern — the writer's responsibility ends at the on-wire bits.
#[allow(clippy::too_many_arguments)]
pub fn write_intra_segment_id(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    segment_id: u8,
    skip: u8,
    pred: u8,
    ctx: usize,
    segmentation_enabled: bool,
    last_active_seg_id: u8,
) -> Result<(), Error> {
    // §5.11.8 disabled-branch: no bits written, segment_id MUST be 0.
    if !segmentation_enabled {
        if segment_id != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }
    // §5.11.8 enabled-branch: delegate to the §5.11.9
    // `read_segment_id` inverse.
    write_segment_id(
        writer,
        cdfs,
        segment_id,
        skip,
        pred,
        ctx,
        last_active_seg_id,
    )
}

/// `intra_frame_y_mode` S() inverse per §5.11.7 line 13 / §8.3.2
/// (av1-spec p.65 / p.361).
///
/// The decoder's §8.3.2 ctx derivation is:
/// ```text
///   abovemode = Intra_Mode_Context[ AvailU ? YModes[ r - 1 ][ c ] : DC_PRED ]
///   leftmode  = Intra_Mode_Context[ AvailL ? YModes[ r ][ c - 1 ] : DC_PRED ]
/// ```
/// then the CDF row is `TileIntraFrameYModeCdf[ abovemode ][ leftmode ]`
/// — the keyframe's INTRA_MODES-wide CDF the §8.2.6 S() reads from.
///
/// The caller pre-computes `abovemode_ctx` and `leftmode_ctx` through
/// [`crate::cdf::intra_mode_ctx`] (the public helper that performs the
/// `Intra_Mode_Context[]` mapping). Both ctx values MUST be in
/// `0..INTRA_MODE_CONTEXTS = 5`.
///
/// `y_mode` MUST be in `0..INTRA_MODES = 13`.
pub fn write_intra_frame_y_mode(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    y_mode: u8,
    abovemode_ctx: usize,
    leftmode_ctx: usize,
) -> Result<(), Error> {
    if (y_mode as usize) >= INTRA_MODES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if abovemode_ctx >= INTRA_MODE_CONTEXTS || leftmode_ctx >= INTRA_MODE_CONTEXTS {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let cdf = cdfs.intra_frame_y_mode_cdf(abovemode_ctx, leftmode_ctx);
    writer.write_symbol(y_mode as u32, cdf)
}

/// `y_mode` S() inverse per §5.11.22 (av1-spec p.72) — the
/// non-keyframe / inter-frame intra-block path.
///
/// The §8.3.2 ctx is `Size_Group[ MiSize ]` — a single index into
/// `TileYModeCdf[ ctx ]`. The caller passes the `sub_size` (§5.11.5
/// `MiSize`) and the writer invokes [`crate::cdf::size_group`] to
/// derive the ctx exactly as the decoder does.
///
/// `y_mode` MUST be in `0..INTRA_MODES = 13`; `size_group_ctx` MUST be
/// in `0..BLOCK_SIZE_GROUPS = 4`.
pub fn write_y_mode(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    y_mode: u8,
    size_group_ctx: usize,
) -> Result<(), Error> {
    if (y_mode as usize) >= INTRA_MODES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if size_group_ctx >= BLOCK_SIZE_GROUPS {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let cdf = cdfs
        .y_mode_cdf(size_group_ctx)
        .ok_or(Error::PartitionWalkOutOfRange)?;
    writer.write_symbol(y_mode as u32, cdf)
}

/// `uv_mode` S() inverse per §5.11.22 line 6 (av1-spec p.72).
///
/// The §8.3.2 selector picks between the CFL-allowed and
/// CFL-not-allowed CDF rows per the paragraph at av1-spec p.361:
/// ```text
///   • Lossless == 1 AND get_plane_residual_size( MiSize, 1 ) == BLOCK_4X4
///                                                     ⇒ CFL allowed
///   • Lossless == 0 AND Max( Block_Width, Block_Height ) <= 32
///                                                     ⇒ CFL allowed
///   • otherwise                                       ⇒ CFL not allowed
/// ```
/// Caller derives `cfl_allowed` (e.g. via
/// [`crate::cdf::cfl_allowed_for_uv_mode`]) and feeds it here; the
/// chosen row is `TileUVMode{Cfl{Allowed,NotAllowed}}Cdf[ YMode ]`.
///
/// `uv_mode` MUST be in `0..UV_INTRA_MODES_CFL_{ALLOWED,NOT_ALLOWED}`
/// per the `cfl_allowed` selector; `y_mode` MUST be in
/// `0..INTRA_MODES = 13`.
pub fn write_intra_uv_mode(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    y_mode: u8,
    uv_mode: u8,
    cfl_allowed: bool,
) -> Result<(), Error> {
    if (y_mode as usize) >= INTRA_MODES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let bound = if cfl_allowed {
        UV_INTRA_MODES_CFL_ALLOWED
    } else {
        UV_INTRA_MODES_CFL_NOT_ALLOWED
    };
    if (uv_mode as usize) >= bound {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let cdf = cdfs
        .uv_mode_cdf(cfl_allowed, y_mode as usize)
        .ok_or(Error::PartitionWalkOutOfRange)?;
    writer.write_symbol(uv_mode as u32, cdf)
}

/// `read_cfl_alphas()` inverse per §5.11.45 (av1-spec p.96) — r231.
///
/// Spec body:
/// ```text
///   read_cfl_alphas() {
///       cfl_alpha_signs                                  S()
///       signU = (cfl_alpha_signs + 1) / 3
///       signV = (cfl_alpha_signs + 1) % 3
///       if (signU != CFL_SIGN_ZERO) {
///           cfl_alpha_u                                  S()
///           CflAlphaU = 1 + cfl_alpha_u
///           if (signU == CFL_SIGN_NEG)
///               CflAlphaU = -CflAlphaU
///       } else {
///           CflAlphaU = 0
///       }
///       // (mirror arm for V)
///   }
/// ```
///
/// `alpha_u` and `alpha_v` are the signed §5.11.45 `CflAlpha{U,V}`
/// outputs the caller (e.g. the encoder's CFL picker) committed to.
/// Each MUST be in `-16..=-1 | 0 | 1..=16` per the §5.11.45 derivation
/// (`CflAlpha = ±(1 + cfl_alpha_*)` with `cfl_alpha_*` ∈
/// `0..CFL_ALPHABET_SIZE`), and not both `0` simultaneously (per
/// §6.10.36 the `(CFL_SIGN_ZERO, CFL_SIGN_ZERO)` joint-sign
/// combination is prohibited as redundant with `UV_DC_PRED`).
///
/// CDF selection per §8.3.2:
///   * `cfl_alpha_signs` reads `TileCflSignCdf`
///     (`Default_Cfl_Sign_Cdf`, 8-symbol, no `ctx`).
///   * `cfl_alpha_u` reads `TileCflAlphaCdf[ ctx_u ]` with
///     `ctx_u = cfl_alpha_u_ctx(signU, signV)` (only fires when
///     `signU != CFL_SIGN_ZERO`).
///   * `cfl_alpha_v` reads `TileCflAlphaCdf[ ctx_v ]` with
///     `ctx_v = cfl_alpha_v_ctx(signU, signV)` (only fires when
///     `signV != CFL_SIGN_ZERO`).
///
/// Returns [`Error::PartitionWalkOutOfRange`] for out-of-range
/// `alpha_u` / `alpha_v` or the prohibited `(0, 0)` pair.
pub fn write_cfl_alphas(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    alpha_u: i8,
    alpha_v: i8,
) -> Result<(), Error> {
    // §5.11.45 magnitude bounds: |CflAlpha| ∈ {0, 1..=CFL_ALPHABET_SIZE}.
    let abs_u: i32 = alpha_u.unsigned_abs() as i32;
    let abs_v: i32 = alpha_v.unsigned_abs() as i32;
    if abs_u as usize > CFL_ALPHABET_SIZE || abs_v as usize > CFL_ALPHABET_SIZE {
        return Err(Error::PartitionWalkOutOfRange);
    }
    // §6.10.36: (CFL_SIGN_ZERO, CFL_SIGN_ZERO) is prohibited.
    if alpha_u == 0 && alpha_v == 0 {
        return Err(Error::PartitionWalkOutOfRange);
    }
    // §5.11.45: signU = ZERO/NEG/POS for alpha 0 / <0 / >0.
    let sign_u: usize = match alpha_u.signum() {
        0 => 0,  // CFL_SIGN_ZERO
        -1 => 1, // CFL_SIGN_NEG
        _ => 2,  // CFL_SIGN_POS
    };
    let sign_v: usize = match alpha_v.signum() {
        0 => 0,
        -1 => 1,
        _ => 2,
    };
    // §5.11.45: `signU = (cfl_alpha_signs + 1) / 3`,
    // `signV = (cfl_alpha_signs + 1) % 3` ⇒ inverse
    // `cfl_alpha_signs = 3 * signU + signV - 1`. Result is in
    // `0..CFL_JOINT_SIGNS = 8`.
    let joint: i32 = 3 * (sign_u as i32) + (sign_v as i32) - 1;
    if !(0..(CFL_JOINT_SIGNS as i32)).contains(&joint) {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let signs_cdf = cdfs.cfl_sign_cdf();
    writer.write_symbol(joint as u32, signs_cdf)?;
    // §5.11.45 U arm.
    if sign_u != 0 {
        let raw_u = abs_u - 1; // `cfl_alpha_u = CflAlphaU.abs() - 1`.
        let ctx_u = cfl_alpha_u_ctx(sign_u, sign_v);
        let row_u = cdfs.cfl_alpha_cdf(ctx_u);
        writer.write_symbol(raw_u as u32, row_u)?;
    }
    // §5.11.45 V arm.
    if sign_v != 0 {
        let raw_v = abs_v - 1;
        let ctx_v = cfl_alpha_v_ctx(sign_u, sign_v);
        let row_v = cdfs.cfl_alpha_cdf(ctx_v);
        writer.write_symbol(raw_v as u32, row_v)?;
    }
    Ok(())
}

/// `read_is_inter()` inverse per §5.11.20 (av1-spec p.71) — the
/// per-block `is_inter` syntax element on the §5.11.18
/// `inter_frame_mode_info` path. Lives at the same dispatcher level
/// as [`write_skip`] / [`write_intra_segment_id`] and mirrors
/// [`crate::cdf::PartitionWalker::decode_is_inter`].
///
/// Spec body (av1-spec p.71, §5.11.20):
///
/// ```text
///   read_is_inter( ) {
///       if ( skip_mode ) {
///           is_inter = 1
///       } else if ( seg_feature_active( SEG_LVL_REF_FRAME ) ) {
///           is_inter = FeatureData[ segment_id ][ SEG_LVL_REF_FRAME ] != INTRA_FRAME
///       } else if ( seg_feature_active( SEG_LVL_GLOBALMV ) ) {
///           is_inter = 1
///       } else {
///           is_inter                                            S()
///       }
///   }
/// ```
///
/// The four-arm `if / else if / else if / else` chain is short-circuit
/// on the first match, exactly mirroring the decoder. Only the
/// fall-through `else` arm emits an `S()` symbol; the other three arms
/// write zero bits and demand that the caller-supplied `is_inter`
/// matches the spec-derived value.
///
/// ## Parameter surface
///
/// Mirrors [`crate::cdf::PartitionWalker::decode_is_inter`]:
///
/// * `skip_mode` — per-block §5.11.10 `SkipModes[r][c]` flag (0 or 1).
/// * `seg_ref_frame_active` — caller-precomputed
///   `seg_feature_active( SEG_LVL_REF_FRAME )` per §6.4.2.
/// * `seg_ref_frame_is_inter` — caller-precomputed
///   `FeatureData[ segment_id ][ SEG_LVL_REF_FRAME ] != INTRA_FRAME`.
///   Only consulted on the `seg_ref_frame_active == true` arm.
/// * `seg_globalmv_active` — caller-precomputed
///   `seg_feature_active( SEG_LVL_GLOBALMV )`. Only consulted on the
///   `else if` arm after the SEG_LVL_REF_FRAME gate.
/// * `ctx` — the §8.3.2 `is_inter` context computed via
///   [`crate::cdf::is_inter_ctx`] from the neighbour
///   `LeftIntra` / `AboveIntra` predicates (or `None` for an
///   unavailable neighbour per §5.11.18). MUST be in
///   `0..IS_INTER_CONTEXTS = 4`. Only consulted on the fall-through
///   `else` arm.
///
/// ## Out-of-range / mismatch cases (surfaced as `Error::PartitionWalkOutOfRange`)
///
/// * `is_inter > 1` — outside the §3 `IsInter` binary alphabet.
/// * `skip_mode == 1` and `is_inter != 1` — Arm 1 forces `is_inter = 1`.
/// * `seg_ref_frame_active == true` and
///   `is_inter != seg_ref_frame_is_inter as u8` — Arm 2 forces the
///   spec-derived value.
/// * `seg_globalmv_active == true` (with the two above false) and
///   `is_inter != 1` — Arm 3 forces `is_inter = 1`.
/// * `ctx >= IS_INTER_CONTEXTS` on the fall-through arm — invalid
///   §8.3.2 ctx.
///
/// ## §5.11.5 grid-fill is on the caller
///
/// The decoder's `decode_is_inter` stamps the resulting `is_inter`
/// over the block's `bh4 * bw4` footprint into `walker.is_inters[]`.
/// The writer is stateless (mirroring [`write_skip`]) and does not
/// maintain an encoder-side `IsInters[]` grid; the caller threads
/// whatever grid state the encode-side §8.3.2 ctx derivations need
/// (typically by a parallel [`crate::cdf::PartitionWalker`] used only
/// for its grid stamps — see the round-trip tests below).
#[allow(clippy::too_many_arguments)]
pub fn write_is_inter(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    is_inter: u8,
    ctx: usize,
    skip_mode: u8,
    seg_ref_frame_active: bool,
    seg_ref_frame_is_inter: bool,
    seg_globalmv_active: bool,
) -> Result<(), Error> {
    // §3 binary alphabet bound.
    if is_inter > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }
    // Arm 1: `if ( skip_mode ) is_inter = 1` — no bits.
    if skip_mode != 0 {
        if is_inter != 1 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }
    // Arm 2: SEG_LVL_REF_FRAME ⇒ is_inter = FeatureData != INTRA_FRAME, no bits.
    if seg_ref_frame_active {
        let expected = seg_ref_frame_is_inter as u8;
        if is_inter != expected {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }
    // Arm 3: SEG_LVL_GLOBALMV ⇒ is_inter = 1, no bits.
    if seg_globalmv_active {
        if is_inter != 1 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }
    // Arm 4 (fall-through): `is_inter S()` over `TileIsInterCdf[ ctx ]`.
    if ctx >= IS_INTER_CONTEXTS {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let cdf = cdfs.is_inter_cdf(ctx);
    writer.write_symbol(is_inter as u32, cdf)
}

/// `read_skip_mode()` inverse per §5.11.10 (av1-spec p.67) — the
/// per-block `skip_mode` syntax element on the §5.11.18
/// `inter_frame_mode_info` path. Lives at the same dispatcher level
/// as [`write_skip`] and mirrors
/// [`crate::cdf::PartitionWalker::decode_skip_mode`]. `skip_mode`
/// is the inter-frame "compound-reference shortcut" flag — it
/// gates Arm 1 of [`write_is_inter`] (`skip_mode == 1` ⇒
/// `is_inter = 1`, no symbol read).
///
/// Spec body (av1-spec p.67, §5.11.10):
///
/// ```text
///   read_skip_mode( ) {
///       if ( seg_feature_active( SEG_LVL_SKIP ) ||
///            seg_feature_active( SEG_LVL_REF_FRAME ) ||
///            seg_feature_active( SEG_LVL_GLOBALMV ) ||
///            !skip_mode_present ||
///            Block_Width[ MiSize ] < 8 ||
///            Block_Height[ MiSize ] < 8 ) {
///           skip_mode = 0
///       } else {
///           skip_mode                                            S()
///       }
///   }
/// ```
///
/// The six-condition disjunction collapses, per the decoder
/// reader's parameter surface, into:
///
/// * `seg_skip_mode_off` — caller-precomputed
///   `seg_feature_active( SEG_LVL_SKIP ) ||
///   seg_feature_active( SEG_LVL_REF_FRAME ) ||
///   seg_feature_active( SEG_LVL_GLOBALMV )`.
/// * `skip_mode_present` — the §5.9.21 frame-header scalar gating
///   skip-mode for the whole frame.
/// * `sub_size` — the §5.11.5 `MiSize` of the leaf block. The
///   small-block short-circuit
///   (`Block_Width[ MiSize ] < 8 || Block_Height[ MiSize ] < 8`) is
///   derived locally via the §9.3 [`block_width`] / [`block_height`]
///   tables, mirroring the decoder's own derivation. `sub_size`
///   MUST be in `0..BLOCK_SIZES = 22`.
///
/// On the fall-through `else` arm a single §8.2.6 `S()` symbol is
/// emitted against `TileSkipModeCdf[ ctx ]`. `ctx` is the §8.3.2
/// `skip_mode` context (the sum of the neighbour `SkipModes[]`
/// flags) the caller pre-computes via [`crate::cdf::skip_mode_ctx`]
/// from the §5.11.5 `SkipModes[]` grid state. `ctx` MUST be in
/// `0..SKIP_MODE_CONTEXTS = 3`.
///
/// ## Out-of-range / mismatch cases (surfaced as `Error::PartitionWalkOutOfRange`)
///
/// * `skip_mode > 1` — outside the §3 binary alphabet.
/// * `sub_size >= BLOCK_SIZES` — invalid §5.11.5 `MiSize`.
/// * any short-circuit arm fires (`seg_skip_mode_off`,
///   `!skip_mode_present`, or `sub_size` ⇒ `Block_W < 8 ||
///   Block_H < 8`) and `skip_mode != 0` — the spec forces
///   `skip_mode = 0` on every short-circuit arm.
/// * `ctx >= SKIP_MODE_CONTEXTS` on the fall-through arm — invalid
///   §8.3.2 ctx.
///
/// ## §5.11.5 grid-fill is on the caller
///
/// The decoder's `decode_skip_mode` stamps the resulting
/// `skip_mode` over the block's `bh4 * bw4` footprint into
/// `walker.skip_modes[]`. The writer is stateless (mirroring
/// [`write_skip`] / [`write_is_inter`]) and does not maintain an
/// encoder-side `SkipModes[]` grid; the caller threads whatever
/// grid state the encode-side §8.3.2 ctx derivations need
/// (typically by a parallel [`crate::cdf::PartitionWalker`] used
/// only for its grid stamps — see the round-trip tests below).
pub fn write_skip_mode(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    skip_mode: u8,
    ctx: usize,
    sub_size: usize,
    seg_skip_mode_off: bool,
    skip_mode_present: bool,
) -> Result<(), Error> {
    // §3 binary alphabet bound.
    if skip_mode > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }
    // §5.11.5 sub_size domain.
    if sub_size >= BLOCK_SIZES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    // §5.11.10 small-block short-circuit set
    // `Block_Width[ MiSize ] < 8 || Block_Height[ MiSize ] < 8`,
    // derived locally from sub_size via the §9.3 tables (mirrors
    // `decode_skip_mode`'s own derivation).
    let small_block = block_width(sub_size) < 8 || block_height(sub_size) < 8;
    // §5.11.10 short-circuit set: any-true ⇒ skip_mode = 0, no bits.
    if seg_skip_mode_off || !skip_mode_present || small_block {
        if skip_mode != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }
    // Fall-through `else`: `skip_mode S()` over
    // `TileSkipModeCdf[ ctx ]`.
    if ctx >= SKIP_MODE_CONTEXTS {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let cdf = cdfs.skip_mode_cdf(ctx);
    writer.write_symbol(skip_mode as u32, cdf)
}

/// `inter_segment_id( preSkip )` inverse per §5.11.19 (av1-spec p.71) —
/// the inter-frame per-block segment-id writer. Mirrors
/// [`crate::cdf::PartitionWalker::decode_inter_segment_id`]. Called
/// twice from the §5.11.18 `inter_frame_mode_info` writer: once with
/// `pre_skip = true` before the §5.11.11 `read_skip()` call (the §5.9.14
/// `SegIdPreSkip == 1` arm activates the early write) and once with
/// `pre_skip = false` after (the post-skip arm covers the
/// `SegIdPreSkip == 0` case and the temporal-update / skip-after-pre-skip
/// update of the segmentation-prediction context arrays).
///
/// Spec body (av1-spec p.71, §5.11.19):
///
/// ```text
///   inter_segment_id( preSkip ) {
///       if ( segmentation_enabled ) {
///           predictedSegmentId = get_segment_id( )
///           if ( segmentation_update_map ) {
///               if ( preSkip && !SegIdPreSkip ) {
///                   segment_id = 0
///                   return
///               }
///               if ( !preSkip ) {
///                   if ( skip ) {
///                       seg_id_predicted = 0
///                       for ( i = 0; i < Num_4x4_Blocks_Wide[ MiSize ]; i++ )
///                           AboveSegPredContext[ MiCol + i ] = seg_id_predicted
///                       for ( i = 0; i < Num_4x4_Blocks_High[ MiSize ]; i++ )
///                           LeftSegPredContext[ MiRow + i ] = seg_id_predicted
///                       read_segment_id( )
///                       return
///                   }
///               }
///               if ( segmentation_temporal_update == 1 ) {
///                   seg_id_predicted                                       S()
///                   if ( seg_id_predicted )
///                       segment_id = predictedSegmentId
///                   else
///                       read_segment_id( )
///                   for ( i = 0; i < Num_4x4_Blocks_Wide[ MiSize ]; i++ )
///                       AboveSegPredContext[ MiCol + i ] = seg_id_predicted
///                   for ( i = 0; i < Num_4x4_Blocks_High[ MiSize ]; i++ )
///                       LeftSegPredContext[ MiRow + i ] = seg_id_predicted
///               } else {
///                   read_segment_id( )
///               }
///           } else {
///               segment_id = predictedSegmentId
///           }
///       } else {
///           segment_id = 0
///       }
///   }
/// ```
///
/// ## Arm dispatch (encoder mirror)
///
/// The writer transcribes each spec arm; arms that the decoder
/// services with zero `S()` reads are also serviced here with zero
/// `write_symbol` calls:
///
/// 1. `!segmentation_enabled` — no bits; `segment_id` MUST be `0`.
/// 2. `!segmentation_update_map` — no bits; `segment_id` MUST equal
///    `predicted_segment_id` (the §5.11.21 `get_segment_id()` result
///    the caller pre-computes).
/// 3. `pre_skip && !seg_id_pre_skip` — no bits; `segment_id` MUST be
///    `0`. The post-skip call services the actual id read.
/// 4. `!pre_skip && skip != 0` (post-skip arm's skip-block branch) —
///    the decoder forces `seg_id_predicted = 0` (no `S()` read) and
///    immediately descends to `read_segment_id()` with `skip = 1`,
///    which inside §5.11.9 short-circuits to `segment_id = pred`.
///    The writer mirrors: stamp `seg_id_predicted = 0` is the
///    caller's grid responsibility; here we delegate to
///    [`write_segment_id`] with `skip = 1` (no bits). `segment_id`
///    MUST equal `pred` on this arm.
/// 5. `segmentation_temporal_update` — emits one §8.2.6 `S()` symbol
///    `seg_id_predicted` against `TileSegmentIdPredictedCdf[ ctx ]`
///    where `ctx` is the §8.3.2 `seg_pred_ctx` (= `LeftSegPredContext[
///    MiRow ] + AboveSegPredContext[ MiCol ]`, caller-derived in
///    `0..SEGMENT_ID_PREDICTED_CONTEXTS = 3`). Then either:
///    * `seg_id_predicted == 1` ⇒ adopt `predicted_segment_id` (no
///      further bits); `segment_id` MUST equal `predicted_segment_id`.
///    * `seg_id_predicted == 0` ⇒ delegate to [`write_segment_id`]
///      with `skip = 0` for the literal id read (one §8.2.6 `S()`
///      against `TileSegmentIdCdf[ seg_id_read_ctx ]`).
///
///    Per §6.10.9 spec-note "It is allowed for seg_id_predicted to be
///    equal to 0 even if the value coded for the segment_id is equal
///    to predictedSegmentId" — the caller chooses the flag and the
///    writer does not second-guess it.
/// 6. Fall-through (`segmentation_temporal_update == 0`) — delegate
///    to [`write_segment_id`] with `skip = 0` for the literal id
///    read.
///
/// ## Parameters
///
/// * `segment_id` — the final `segment_id ∈ 0..=last_active_seg_id`
///   the block should decode to (≤ `MAX_SEGMENTS - 1 = 7`).
/// * `pred` — the §5.11.9 neighbour-cascade predictor the caller
///   already has on hand (used only on arms 4 + 5-else + 6, where
///   `write_segment_id` is invoked).
/// * `seg_id_read_ctx` — the §8.3.2 `segment_id_ctx` value the
///   caller pre-computes via [`crate::cdf::segment_id_ctx`] for the
///   inner `write_segment_id` `S()` arm. Only consulted on arms 4,
///   5-else, and 6.
/// * `seg_pred_ctx` — the §8.3.2 `seg_id_predicted` ctx value the
///   caller pre-computes (= `LeftSegPredContext[ MiRow ] +
///   AboveSegPredContext[ MiCol ]`, in `0..3`). Only consulted on
///   arm 5.
/// * `pre_skip` — the §5.11.18 caller-axis (`true` on the pre-skip
///   call, `false` on the post-skip call).
/// * `skip` — the §5.11.11 `read_skip` return (passed `0` on the
///   pre-skip call — `read_skip` fires after `inter_segment_id( 1 )`).
/// * `seg_id_predicted` — caller's choice on arm 5: `1` to adopt the
///   `predicted_segment_id` (saves bits), `0` to code the literal id.
///   Ignored on every other arm.
/// * `segmentation_enabled` / `segmentation_update_map` /
///   `segmentation_temporal_update` / `seg_id_pre_skip` — the §5.9.14
///   frame-header derivations surfaced through
///   [`SegmentationParams`].
/// * `predicted_segment_id` — `get_segment_id()` result the caller
///   pre-computes from the §6.10 reference-frame walk.
/// * `last_active_seg_id` — §6.10.8 derivation
///   (`MAX_SEGMENTS - 1 = 7` upper bound).
///
/// ## Range / mismatch guards (`Error::PartitionWalkOutOfRange`)
///
/// * `last_active_seg_id >= MAX_SEGMENTS = 8`.
/// * `segment_id > last_active_seg_id`.
/// * `pred > last_active_seg_id`.
/// * `predicted_segment_id > last_active_seg_id`.
/// * `seg_pred_ctx >= SEGMENT_ID_PREDICTED_CONTEXTS = 3` on arm 5.
/// * `seg_id_read_ctx >= SEGMENT_ID_CONTEXTS = 3` on the
///   `write_segment_id` `S()` arm (re-checked inside the inner
///   writer).
/// * `seg_id_predicted > 1` on arm 5.
/// * Arm-precondition violations — `segment_id != 0` on arms 1 and
///   3; `segment_id != predicted_segment_id` on arm 2 and on the
///   `seg_id_predicted == 1` branch of arm 5; `segment_id != pred`
///   on arm 4.
///
/// ## Stateless — no encoder-side grids
///
/// Mirroring the rest of `encoder::block_mode_info`, no
/// `SegmentIds[]` / `AboveSegPredContext[]` / `LeftSegPredContext[]`
/// arrays are maintained here. The decoder stamps those grids
/// itself; an encoder caller typically threads a parallel
/// [`crate::cdf::PartitionWalker`] purely for the grid stamps and
/// uses [`crate::cdf::PartitionWalker::seg_pred_ctx`] (or the
/// equivalent neighbour-array lookup) to derive `seg_pred_ctx`.
///
/// [`SegmentationParams`]: crate::uncompressed_header_tail::SegmentationParams
#[allow(clippy::too_many_arguments)]
pub fn write_inter_segment_id(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    segment_id: u8,
    pred: u8,
    seg_id_read_ctx: usize,
    seg_pred_ctx: usize,
    pre_skip: bool,
    skip: u8,
    seg_id_predicted: u8,
    segmentation_enabled: bool,
    segmentation_update_map: bool,
    segmentation_temporal_update: bool,
    seg_id_pre_skip: bool,
    predicted_segment_id: u8,
    last_active_seg_id: u8,
) -> Result<(), Error> {
    // Up-front range guards (mirror `decode_inter_segment_id`).
    if (last_active_seg_id as usize) >= MAX_SEGMENTS {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let max = last_active_seg_id as u32 + 1;
    if segment_id as u32 >= max {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if pred as u32 >= max {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if predicted_segment_id as u32 >= max {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // Arm 1: §5.11.19 `if ( !segmentation_enabled ) segment_id = 0`.
    if !segmentation_enabled {
        if segment_id != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // Arm 2: §5.11.19 `else if ( !segmentation_update_map )
    // segment_id = predictedSegmentId`.
    if !segmentation_update_map {
        if segment_id != predicted_segment_id {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // Arm 3: §5.11.19 `if ( preSkip && !SegIdPreSkip ) { segment_id = 0;
    // return }` — the pre-skip caller defers to the post-skip call.
    if pre_skip && !seg_id_pre_skip {
        if segment_id != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // Arm 4: §5.11.19 `if ( !preSkip ) { if ( skip ) { seg_id_predicted
    // = 0; ... read_segment_id(); return } }`. The inner
    // `read_segment_id()` is called with `skip = 1`, which §5.11.9
    // short-circuits to `segment_id = pred` (no `S()` read). The
    // forced `seg_id_predicted = 0` grid stamp is the caller's job.
    if !pre_skip && skip != 0 {
        return write_segment_id(
            writer,
            cdfs,
            segment_id,
            /* skip = */ 1,
            pred,
            seg_id_read_ctx,
            last_active_seg_id,
        );
    }

    // Arm 5: §5.11.19 `if ( segmentation_temporal_update == 1 )` — code
    // the binary `seg_id_predicted` S(), then either adopt or
    // fall through.
    if segmentation_temporal_update {
        if seg_id_predicted > 1 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        if seg_pred_ctx >= SEGMENT_ID_PREDICTED_CONTEXTS {
            return Err(Error::PartitionWalkOutOfRange);
        }
        let cdf = cdfs.segment_id_predicted_cdf(seg_pred_ctx);
        writer.write_symbol(seg_id_predicted as u32, cdf)?;
        if seg_id_predicted != 0 {
            // §5.11.19 `if ( seg_id_predicted ) segment_id =
            // predictedSegmentId` — adopt the previous-frame id; no
            // further bits.
            if segment_id != predicted_segment_id {
                return Err(Error::PartitionWalkOutOfRange);
            }
            return Ok(());
        }
        // `seg_id_predicted == 0` ⇒ §5.11.19 `else read_segment_id()`.
        return write_segment_id(
            writer,
            cdfs,
            segment_id,
            /* skip = */ 0,
            pred,
            seg_id_read_ctx,
            last_active_seg_id,
        );
    }

    // Arm 6 (fall-through, `segmentation_temporal_update == 0`):
    // §5.11.19 `else read_segment_id()` with `skip = 0` (the post-skip
    // call's `skip == 1` arm was handled in Arm 4; the pre-skip call's
    // `skip` parameter is forced to `0` by the §5.11.18 dispatch
    // ordering).
    write_segment_id(
        writer,
        cdfs,
        segment_id,
        /* skip = */ 0,
        pred,
        seg_id_read_ctx,
        last_active_seg_id,
    )
}

/// Aggregate the §5.11.18 `inter_frame_mode_info` writer prefix produces.
///
/// The struct mirrors the decoder-side
/// [`crate::cdf::DecodedInterFrameModeInfo`] for the
/// *pre-cdef / pre-delta / pre-`if(is_inter)`* portion of §5.11.18 — i.e.
/// every value the writer commits to the bitstream before reaching the
/// §5.11.18 line 18 `read_cdef( )` call. The §5.11.18 lines 17-26
/// (`Lossless = LosslessArray[ segment_id ]`, `read_cdef`,
/// `read_delta_qindex`, `read_delta_lf`, `ReadDeltas = 0`, `read_is_inter`,
/// the `if ( is_inter ) inter_block_mode_info( ) else intra_block_mode_info( )`
/// terminal dispatch) are next-round targets — we surface `is_inter` and
/// `lossless` because `write_is_inter` *is* in scope and `LosslessArray[]`
/// is a pure caller-supplied lookup with no bits emitted.
///
/// Fields:
/// * `segment_id` — final `segment_id ∈ 0..=last_active_seg_id` after the
///   §5.11.18 pre-skip and (when `!seg_id_pre_skip`) post-skip
///   `inter_segment_id` writes.
/// * `skip_mode` — the §5.11.10 result (`0` on every short-circuit arm,
///   `0` or `1` on the fall-through S() arm).
/// * `skip` — `1` if `skip_mode == 1` (§5.11.18 lines 13-14 force);
///   otherwise the §5.11.11 `read_skip` result.
/// * `is_inter` — the §5.11.20 result the §5.11.18 line 22 read produces.
/// * `lossless` — `LosslessArray[ segment_id ]`, the §5.11.18 line 17
///   derivation against the caller-supplied lossless array. Mirrors the
///   decoder's same field on [`crate::cdf::DecodedInterFrameModeInfo`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InterFrameModeInfoPrefix {
    /// §5.11.18 lines 11 + 15-16 result. `0..=last_active_seg_id`.
    pub segment_id: u8,
    /// §5.11.18 line 12 result.
    pub skip_mode: u8,
    /// §5.11.18 line 10 + lines 13-14 result.
    pub skip: u8,
    /// §5.11.18 line 22 result.
    pub is_inter: u8,
    /// §5.11.18 line 17 result.
    pub lossless: bool,
    /// §5.11.18 line 18 result — `cdef_idx` returned by
    /// [`write_cdef`]. `None` on the §5.11.56 short-circuit set
    /// (`skip || coded_lossless || !enable_cdef || allow_intrabc`)
    /// or when an earlier leaf in the same 64×64 anchor already
    /// stamped the literal (the writer emits no bits on either arm).
    /// `Some(cdef_idx)` on the first-leaf-in-anchor write arm.
    pub cdef_idx: Option<i8>,
    /// §5.11.18 line 19 result — caller-passed `reduced_delta_q_index`
    /// surfaced for chaining. `0` on every short-circuit arm; the
    /// caller owns the `CurrentQIndex` accumulator the §5.11.12 spec
    /// updates via `Clip3` (the writer is pure on grid-state).
    pub reduced_delta_q_index: i32,
    /// §5.11.18 line 20 result — caller-passed `reduced_delta_lf`
    /// row surfaced for chaining. `[0; FRAME_LF_COUNT]` on every
    /// short-circuit arm; the caller owns the `DeltaLF[ i ]`
    /// accumulator the §5.11.13 spec updates via `Clip3`.
    pub reduced_delta_lf: [i32; FRAME_LF_COUNT],
}

/// §5.11.18 lines 18-20 input bundle — the per-block scalars the
/// `read_cdef()` / `read_delta_qindex()` / `read_delta_lf()` writers
/// consume. Bundled into a single argument so the
/// [`write_inter_frame_mode_info_prefix`] dispatcher's parameter
/// surface doesn't grow another 11 positional arguments.
///
/// All fields mirror the matching argument of [`write_cdef`] /
/// [`write_delta_qindex`] / [`write_delta_lf`]:
///
/// * §5.11.56 `read_cdef` — `cdef_idx` / `cdef_idx_prior_stamp` /
///   `cdef_bits` / `coded_lossless` / `enable_cdef` / `allow_intrabc`
///   / `anchor_already_stamped`.
/// * §5.11.12 `read_delta_qindex` — `reduced_delta_q_index` /
///   `read_deltas` / `use_128x128_superblock` / `delta_q_res`.
/// * §5.11.13 `read_delta_lf` — `reduced_delta_lf` / `read_deltas`
///   (shared with `delta_qindex`) / `delta_lf_present` /
///   `delta_lf_multi` / `mono_chrome` / `delta_lf_res`.
///
/// A `Default` impl yields the all-short-circuit configuration
/// (`enable_cdef = false`, `read_deltas = false`, zero deltas) —
/// suitable for callers that target the §5.11.18 lines 18-20
/// no-bits-emitted arms.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InterFrameDeltaSiteInputs {
    /// §5.11.56 `cdef_idx` value to commit on the first-leaf-in-anchor
    /// arm. Range-checked against `cdef_bits` (`0..(1 << cdef_bits)`).
    pub cdef_idx: i8,
    /// §5.11.56 prior-leaf `cdef_idx` stamp — passed straight through
    /// to [`write_cdef`]'s `anchor_already_stamped == true` arm so the
    /// writer can verify the caller's `cdef_idx` matches.
    pub cdef_idx_prior_stamp: i8,
    /// §5.9.19 `cdef_bits` field, `0..=3` (`f(2)` cap).
    pub cdef_bits: u32,
    /// §6.8.21 `CodedLossless` derivation. Short-circuits §5.11.56.
    pub coded_lossless: bool,
    /// §5.9.19 `enable_cdef` flag. Short-circuits §5.11.56.
    pub enable_cdef: bool,
    /// §5.9.20 `allow_intrabc` flag. Short-circuits §5.11.56.
    pub allow_intrabc: bool,
    /// §5.11.56 outer-`if` precondition: `anchor_already_stamped =
    /// (cdef_idx[r][c] != -1)` per the matching `decode_cdef`
    /// derivation, where `(r, c) = (mi_row, mi_col) & ~(cdefSize4 - 1)`.
    /// `true` ⇒ the writer emits no bits on this leaf and the caller
    /// MUST pass `cdef_idx == cdef_idx_prior_stamp`.
    pub anchor_already_stamped: bool,
    /// §5.11.12 `reducedDeltaQIndex` signed intermediate (the value
    /// the §5.11.12 spec computes between the sign-bit read and the
    /// `Clip3` update). The caller owns the `CurrentQIndex`
    /// accumulator the spec updates with this value (`CurrentQIndex
    /// = Clip3(1, 255, CurrentQIndex + (reducedDeltaQIndex << delta_q_res))`).
    pub reduced_delta_q_index: i32,
    /// §5.11.13 `reducedDeltaLfLevel` row — one entry per LF strength.
    /// Only the first `frameLfCount` entries (per §5.11.13 derivation)
    /// are consumed; entries beyond MUST be `0`.
    pub reduced_delta_lf: [i32; FRAME_LF_COUNT],
    /// §6.10.4 `ReadDeltas` flag — shared gate for §5.11.12 and
    /// §5.11.13.
    pub read_deltas: bool,
    /// §5.5.1 `use_128x128_superblock` flag — used for the per-call
    /// `sbSize` derivation that gates the superblock-skip
    /// short-circuit.
    pub use_128x128_superblock: bool,
    /// §5.9.17 `delta_q_res` field, `0..=3`.
    pub delta_q_res: u8,
    /// §5.9.18 `delta_lf_present` flag — gates §5.11.13 reads.
    pub delta_lf_present: bool,
    /// §5.9.18 `delta_lf_multi` flag — selects per-LF or single-LF
    /// row width via the `frameLfCount` derivation.
    pub delta_lf_multi: bool,
    /// §5.5.2 `mono_chrome` flag — narrows the multi-LF row to
    /// `FRAME_LF_COUNT - 2` entries.
    pub mono_chrome: bool,
    /// §5.9.18 `delta_lf_res` field, `0..=3`.
    pub delta_lf_res: u8,
}

impl Default for InterFrameDeltaSiteInputs {
    /// All-short-circuit defaults: `enable_cdef = false` (§5.11.56
    /// short-circuit), `read_deltas = false` (§5.11.12 / §5.11.13
    /// outer-`if` false), zero `reduced_delta_*` so the §5.11.12 /
    /// §5.11.13 short-circuit-validates pass. No bits emitted on
    /// lines 18-20.
    fn default() -> Self {
        Self {
            cdef_idx: 0,
            cdef_idx_prior_stamp: -1,
            cdef_bits: 0,
            coded_lossless: false,
            enable_cdef: false,
            allow_intrabc: false,
            anchor_already_stamped: false,
            reduced_delta_q_index: 0,
            reduced_delta_lf: [0; FRAME_LF_COUNT],
            read_deltas: false,
            use_128x128_superblock: false,
            delta_q_res: 0,
            delta_lf_present: false,
            delta_lf_multi: false,
            mono_chrome: false,
            delta_lf_res: 0,
        }
    }
}

/// `inter_frame_mode_info()` writer prefix per §5.11.18 (av1-spec p.71) —
/// the per-block syntax dispatcher for an inter-frame leaf. Composes
/// every leaf writer landed across r253-r258:
///
/// * §5.11.19 `inter_segment_id( 1 )` — pre-skip arm via
///   [`write_inter_segment_id`].
/// * §5.11.10 `read_skip_mode()` — via [`write_skip_mode`].
/// * §5.11.11 `read_skip()` (when `skip_mode == 0`) — via [`write_skip`].
/// * §5.11.19 `inter_segment_id( 0 )` (when `!seg_id_pre_skip`) —
///   post-skip arm via [`write_inter_segment_id`].
/// * §5.11.56 `read_cdef()` — via [`write_cdef`] (r258).
/// * §5.11.12 `read_delta_qindex()` — via [`write_delta_qindex`] (r258).
/// * §5.11.13 `read_delta_lf()` — via [`write_delta_lf`] (r258).
/// * §5.11.20 `read_is_inter()` — via [`write_is_inter`].
///
/// The spec body (av1-spec p.71) reads:
///
/// ```text
///   inter_frame_mode_info( ) {
///       use_intrabc = 0
///       LeftRefFrame[..]  = ...
///       AboveRefFrame[..] = ...
///       LeftIntra / AboveIntra / LeftSingle / AboveSingle = ...
///       skip = 0
///       inter_segment_id( 1 )                                 ← writer
///       read_skip_mode( )                                     ← writer
///       if ( skip_mode )
///           skip = 1
///       else
///           read_skip( )                                      ← writer
///       if ( !SegIdPreSkip )
///           inter_segment_id( 0 )                             ← writer
///       Lossless = LosslessArray[ segment_id ]
///       read_cdef( )                                          ← writer (r259)
///       read_delta_qindex( )                                  ← writer (r259)
///       read_delta_lf( )                                      ← writer (r259)
///       ReadDeltas = 0
///       read_is_inter( )                                      ← writer
///       if ( is_inter )
///           inter_block_mode_info( )                          (next round)
///       else
///           intra_block_mode_info( )                          (next round)
///   }
/// ```
///
/// ## Scope of this writer
///
/// The dispatcher writes the §5.11.18 *prefix*: the eight S()-emitting
/// sub-writers above. The line 23-26 terminal `if ( is_inter )` dispatch
/// into §5.11.22 `intra_block_mode_info` / §5.11.23 `inter_block_mode_info`
/// remains a next-round encoder-side target — neither has a writer
/// counterpart yet.
///
/// The writer's return value is an [`InterFrameModeInfoPrefix`]
/// aggregate covering every value committed to the bitstream so far,
/// including the `segment_id`-indexed `lossless` lookup (no bits) so
/// the caller can chain the §5.11.18 line 17 derivation without
/// re-fetching it.
///
/// ## Parameter surface
///
/// Mirrors the union of every composed sub-writer. The `is_inter_ctx`
/// argument is the §8.3.2 `is_inter` ctx the caller pre-computes via
/// [`crate::cdf::is_inter_ctx`]; the other ctx values (`skip_ctx`,
/// `skip_mode_ctx`, `seg_id_read_ctx`, `seg_pred_ctx`) are caller-supplied
/// just like the per-sub-writer entry points. The `sub_size`, segmentation
/// scalars, and pred values mirror their per-sub-writer counterparts
/// one-to-one.
///
/// ## Out-of-range / mismatch cases
///
/// All [`Error::PartitionWalkOutOfRange`] guards from the composed
/// sub-writers fire here unchanged (the dispatcher delegates without
/// short-circuiting). One extra guard is added up-front:
///
/// * `last_active_seg_id >= MAX_SEGMENTS` — invalid §6.10.8 derivation.
///   Caught up-front so the lossless lookup below is well-defined.
///
/// ## §5.11.5 grid-fill is on the caller
///
/// As with every sub-writer in this module, the dispatcher is stateless:
/// caller threads any encoder-side §8.3.2 ctx derivations / parallel
/// [`crate::cdf::PartitionWalker`] grid-fill themselves. The round-trip
/// tests below drive the byte run through
/// [`crate::cdf::PartitionWalker::decode_inter_frame_mode_info`]'s prefix
/// (i.e. through the same five composed `decode_*` calls in the same
/// order) and verify the segment_id / skip_mode / skip / is_inter values
/// match.
#[allow(clippy::too_many_arguments)]
pub fn write_inter_frame_mode_info_prefix(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    // Per-block grid coordinates (caller-side state only — the writer
    // is stateless on grid-fill; the parameters are kept symmetric
    // with the decoder for ergonomic call-site mirroring).
    sub_size: usize,
    // §5.11.18 leaf scalars to commit:
    segment_id: u8,
    skip_mode: u8,
    skip: u8,
    is_inter: u8,
    // §5.11.19 segment-id neighbour cascade (caller-precomputed):
    seg_pred: u8,
    seg_id_predicted: u8,
    // §8.3.2 ctx values (caller-precomputed):
    skip_mode_ctx_val: usize,
    skip_ctx_val: usize,
    seg_id_read_ctx: usize,
    seg_pred_ctx: usize,
    is_inter_ctx_val: usize,
    // §5.11.10 / §5.11.11 / §5.11.20 segmentation-feature collapsed flags:
    seg_skip_mode_off: bool,
    seg_skip_active: bool,
    seg_ref_frame_active: bool,
    seg_ref_frame_is_inter: bool,
    seg_globalmv_active: bool,
    // §5.9.14 segmentation scalars:
    segmentation_enabled: bool,
    segmentation_update_map: bool,
    segmentation_temporal_update: bool,
    seg_id_pre_skip: bool,
    predicted_segment_id: u8,
    last_active_seg_id: u8,
    // §5.9.21 frame-header scalar:
    skip_mode_present: bool,
    // §5.11.18 line 17 lookup:
    lossless_array: &[bool; MAX_SEGMENTS],
    // §5.11.18 lines 18-20 inputs — `read_cdef` / `read_delta_qindex` /
    // `read_delta_lf` per-block scalars. See
    // [`InterFrameDeltaSiteInputs`] for the field-by-field contract.
    delta_inputs: &InterFrameDeltaSiteInputs,
) -> Result<InterFrameModeInfoPrefix, Error> {
    if (last_active_seg_id as usize) >= MAX_SEGMENTS {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.18 line 11: `inter_segment_id( 1 )` — pre-skip arm. The
    // §5.11.19 spec body short-circuits when `segmentation_enabled == 0`
    // (writes `segment_id = 0`, no bits), when `segmentation_update_map
    // == 0` (adopts `predictedSegmentId`, no bits), or on the
    // `preSkip && !SegIdPreSkip` early-return (sets `segment_id = 0`,
    // no bits — the post-skip call services the real read).
    //
    // When `seg_id_pre_skip == true` the pre-skip call services the
    // actual segment_id read — pass caller's `segment_id`. When
    // `seg_id_pre_skip == false` the pre-skip call MUST go through
    // §5.11.19 Arm 1 / Arm 2 / Arm 3 short-circuits (one of them MUST
    // fire because Arm 4 / 5 / 6 only run with `pre_skip == true &&
    // seg_id_pre_skip == true` or `pre_skip == false`):
    //   * `!segmentation_enabled` ⇒ pass 0 (Arm 1's invariant).
    //   * `!segmentation_update_map` ⇒ pass `predicted_segment_id`
    //     (Arm 2's invariant).
    //   * else (Arm 3 fires because `pre_skip && !seg_id_pre_skip`) ⇒
    //     pass 0 (Arm 3's invariant).
    // The final value committed by the post-skip call below is the
    // caller's `segment_id`.
    let pre_skip_segment_id = if seg_id_pre_skip {
        segment_id
    } else if !segmentation_enabled {
        0
    } else if !segmentation_update_map {
        predicted_segment_id
    } else {
        0
    };
    write_inter_segment_id(
        writer,
        cdfs,
        pre_skip_segment_id,
        seg_pred,
        seg_id_read_ctx,
        seg_pred_ctx,
        /* pre_skip = */ true,
        /* skip = */ 0,
        seg_id_predicted,
        segmentation_enabled,
        segmentation_update_map,
        segmentation_temporal_update,
        seg_id_pre_skip,
        predicted_segment_id,
        last_active_seg_id,
    )?;

    // §5.11.18 line 12: `read_skip_mode( )`.
    write_skip_mode(
        writer,
        cdfs,
        skip_mode,
        skip_mode_ctx_val,
        sub_size,
        seg_skip_mode_off,
        skip_mode_present,
    )?;

    // §5.11.18 lines 13-14: `if ( skip_mode ) skip = 1 else read_skip( )`.
    // The compound-reference shortcut forces `skip = 1` without reading
    // the §5.11.11 bit. Validate the caller-supplied `skip` matches the
    // spec's invariant either way.
    if skip > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let final_skip: u8 = if skip_mode != 0 {
        if skip != 1 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        1
    } else {
        write_skip(writer, cdfs, skip, skip_ctx_val, seg_skip_active)?;
        skip
    };

    // §5.11.18 lines 15-16: `if ( !SegIdPreSkip ) inter_segment_id( 0 )`.
    // The post-skip arm fires only when the §5.9.14 SegIdPreSkip
    // derivation is false; otherwise the pre-skip call already committed
    // the segment_id.
    if !seg_id_pre_skip {
        write_inter_segment_id(
            writer,
            cdfs,
            segment_id,
            seg_pred,
            seg_id_read_ctx,
            seg_pred_ctx,
            /* pre_skip = */ false,
            final_skip,
            seg_id_predicted,
            segmentation_enabled,
            segmentation_update_map,
            segmentation_temporal_update,
            seg_id_pre_skip,
            predicted_segment_id,
            last_active_seg_id,
        )?;
    }

    // §5.11.18 line 17: `Lossless = LosslessArray[ segment_id ]`. Pure
    // caller-supplied lookup; segment_id has been range-checked inside
    // the §5.11.19 writers (each call rejects `segment_id >
    // last_active_seg_id`), and `last_active_seg_id < MAX_SEGMENTS`
    // was checked up-front, so the index is always in bounds.
    debug_assert!(
        (segment_id as usize) < MAX_SEGMENTS,
        "segment_id range-checked by write_inter_segment_id"
    );
    let lossless = lossless_array[segment_id as usize];

    // §5.11.18 line 18: `read_cdef( )`. The §5.11.56 spec body
    // short-circuits on `skip || coded_lossless || !enable_cdef ||
    // allow_intrabc` and on `anchor_already_stamped`; otherwise emits
    // one `L(cdef_bits)`. The writer returns `Ok(None)` for either
    // short-circuit and `Ok(Some(cdef_idx))` on the first-leaf write.
    let cdef_idx_committed = write_cdef(
        writer,
        delta_inputs.cdef_idx,
        delta_inputs.cdef_idx_prior_stamp,
        delta_inputs.cdef_bits,
        final_skip,
        delta_inputs.coded_lossless,
        delta_inputs.enable_cdef,
        delta_inputs.allow_intrabc,
        delta_inputs.anchor_already_stamped,
    )?;

    // §5.11.18 line 19: `read_delta_qindex( )`. The §5.11.12 spec body
    // short-circuits on `MiSize == sbSize && skip` or `!ReadDeltas`;
    // otherwise emits the S() + escape ladder + sign bit cascade for
    // the caller-supplied signed `reducedDeltaQIndex`.
    write_delta_qindex(
        writer,
        cdfs,
        sub_size,
        delta_inputs.reduced_delta_q_index,
        final_skip,
        delta_inputs.read_deltas,
        delta_inputs.use_128x128_superblock,
        delta_inputs.delta_q_res,
    )?;

    // §5.11.18 line 20: `read_delta_lf( )`. Per-iteration shape
    // identical to §5.11.12; gated by `ReadDeltas && delta_lf_present`.
    write_delta_lf(
        writer,
        cdfs,
        sub_size,
        &delta_inputs.reduced_delta_lf,
        final_skip,
        delta_inputs.read_deltas,
        delta_inputs.delta_lf_present,
        delta_inputs.delta_lf_multi,
        delta_inputs.mono_chrome,
        delta_inputs.use_128x128_superblock,
        delta_inputs.delta_lf_res,
    )?;

    // §5.11.18 line 21: `ReadDeltas = 0`. Caller-owned state per the
    // §6.10.4 derivation; the writer doesn't track it.

    // §5.11.18 line 22: `read_is_inter( )`.
    write_is_inter(
        writer,
        cdfs,
        is_inter,
        is_inter_ctx_val,
        skip_mode,
        seg_ref_frame_active,
        seg_ref_frame_is_inter,
        seg_globalmv_active,
    )?;

    Ok(InterFrameModeInfoPrefix {
        segment_id,
        skip_mode,
        skip: final_skip,
        is_inter,
        lossless,
        cdef_idx: cdef_idx_committed,
        reduced_delta_q_index: delta_inputs.reduced_delta_q_index,
        reduced_delta_lf: delta_inputs.reduced_delta_lf,
    })
}

/// `read_cdef()` inverse per §5.11.56 (av1-spec p.104) — the per-leaf
/// CDEF-index writer that mirrors
/// [`crate::cdf::PartitionWalker::decode_cdef`].
///
/// Spec body (av1-spec p.104):
///
/// ```text
///   read_cdef( ) {
///       if ( skip || CodedLossless || !enable_cdef || allow_intrabc) {
///           return
///       }
///       cdefSize4 = Num_4x4_Blocks_Wide[ BLOCK_64X64 ]
///       cdefMask4 = ~(cdefSize4 - 1)
///       r = MiRow & cdefMask4
///       c = MiCol & cdefMask4
///       if ( cdef_idx[ r ][ c ] == -1 ) {
///           cdef_idx[ r ][ c ]                          L(cdef_bits)
///           w4 = Num_4x4_Blocks_Wide[ MiSize ]
///           h4 = Num_4x4_Blocks_High[ MiSize ]
///           for ( i = r; i < r + h4 ; i += cdefSize4 ) {
///               for ( j = c; j < c + w4 ; j += cdefSize4 ) {
///                   cdef_idx[ i ][ j ] = cdef_idx[ r ][ c ]
///               }
///           }
///       }
///   }
/// ```
///
/// ## Arms (mirrors the decoder branch-by-branch)
///
/// * **Short-circuit set** — when *any* of `skip`, `coded_lossless`,
///   `!enable_cdef`, `allow_intrabc` is true the §5.11.56 outer
///   `if` fires and the function returns without consuming a bit.
///   The writer emits no symbol and returns `Ok(None)`.
/// * **Anchor already stamped** — `anchor_already_stamped == true`
///   indicates an earlier leaf in the same 64×64 anchor already
///   committed the `cdef_idx` literal (the decoder side reads zero
///   bits and observes the prior stamp). The writer emits no symbol
///   and returns `Ok(None)`. The caller must ensure `cdef_idx`
///   matches the prior stamp; mismatch is surfaced as
///   [`Error::PartitionWalkOutOfRange`].
/// * **First leaf in anchor** — emits one `L(cdef_bits)` literal
///   carrying `cdef_idx`. `cdef_bits` is the §5.9.19 frame-header
///   field clamped to `0..=3` by `f(2)`. `cdef_idx` MUST be in
///   `0..(1 << cdef_bits)`. Returns `Ok(Some(cdef_idx))` so the
///   caller can stamp its own encoder-side grid if it maintains one.
///
/// ## Parameter contract
///
/// `cdef_idx_prior_stamp` is the value the decoder side would read
/// from `cdef_idx[ r ][ c ]` before this call — supplied so the
/// writer's `anchor_already_stamped == true` arm can verify the
/// caller passes the same value (otherwise the decoder will diverge
/// at the matching `decode_cdef` call). Pass `-1` when
/// `anchor_already_stamped == false` (the §5.11.55 sentinel) — the
/// writer ignores the value on that arm anyway, but accepting an
/// explicit argument keeps the call sites uniform.
///
/// `cdef_idx` is the value the writer commits when the inner `if`
/// fires. It MUST fit in `cdef_bits` bits (`0..(1 << cdef_bits)`);
/// `cdef_bits == 0` accepts only `cdef_idx == 0` and emits zero
/// bits (the §8.2.5 `L(0)` literal). When `anchor_already_stamped
/// == true`, `cdef_idx` MUST equal `cdef_idx_prior_stamp`.
///
/// ## Range guards
///
/// All surfaced as [`Error::PartitionWalkOutOfRange`]:
///
/// * `cdef_bits > 3` (caller bug — §5.9.19 `f(2)` cap).
/// * `cdef_idx >= (1 << cdef_bits)` (caller bug — exceeds the L()
///   field width).
/// * `anchor_already_stamped == true` and `cdef_idx_prior_stamp !=
///   cdef_idx` (caller bug — would diverge from the decoder).
///
/// ## Stateless on grid-fill
///
/// Mirrors the rest of `encoder::block_mode_info`: the writer does
/// not maintain an encoder-side `cdef_idx[]` grid. The caller threads
/// its own (or uses a parallel [`crate::cdf::PartitionWalker`] —
/// the round-trip tests below use that exact pattern). The decoder
/// side stamps `cdef_idx[]` on the matching `decode_cdef` call
/// against its own walker.
#[allow(clippy::too_many_arguments)]
pub fn write_cdef(
    writer: &mut SymbolWriter,
    cdef_idx: i8,
    cdef_idx_prior_stamp: i8,
    cdef_bits: u32,
    skip: u8,
    coded_lossless: bool,
    enable_cdef: bool,
    allow_intrabc: bool,
    anchor_already_stamped: bool,
) -> Result<Option<i8>, Error> {
    // §5.9.19: cdef_bits is f(2) ⇒ 0..=3. Larger is a caller bug.
    if cdef_bits > 3 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.56 short-circuit set: any-true ⇒ no bits emitted.
    if skip != 0 || coded_lossless || !enable_cdef || allow_intrabc {
        return Ok(None);
    }

    // §5.11.56 outer `if`: anchor already stamped ⇒ no bits emitted.
    // The caller's `cdef_idx` MUST match the prior stamp; otherwise
    // the decoder will observe a different value than the writer
    // expects (caller bug — mid-anchor leaves carry the same value).
    if anchor_already_stamped {
        if cdef_idx_prior_stamp != cdef_idx {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(None);
    }

    // §5.11.56 inner branch: first leaf in this anchor ⇒ `L(cdef_bits)`.
    // §8.2.5 `L(n)` requires `value < (1 << n)`; `cdef_bits == 0`
    // accepts only `cdef_idx == 0` (an empty literal).
    if cdef_idx < 0 {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let value = cdef_idx as u32;
    if cdef_bits == 0 {
        if value != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        // §8.2.5 L(0) — no bits emitted.
        return Ok(Some(cdef_idx));
    }
    if value >= (1u32 << cdef_bits) {
        return Err(Error::PartitionWalkOutOfRange);
    }
    writer.write_literal(cdef_bits, value)?;
    Ok(Some(cdef_idx))
}

/// `read_delta_qindex()` inverse per §5.11.12 (av1-spec p.68) — the
/// per-block `CurrentQIndex` delta writer that mirrors
/// [`crate::cdf::PartitionWalker::decode_delta_qindex`].
///
/// Spec body (av1-spec p.68):
///
/// ```text
///   read_delta_qindex( ) {
///       sbSize = use_128x128_superblock ? BLOCK_128X128 : BLOCK_64X64
///       if ( MiSize == sbSize && skip )
///           return
///       if ( ReadDeltas ) {
///           delta_q_abs                                            S()
///           if ( delta_q_abs == DELTA_Q_SMALL ) {
///               delta_q_rem_bits                                   L(3)
///               delta_q_rem_bits++
///               delta_q_abs_bits                          L(delta_q_rem_bits)
///               delta_q_abs = delta_q_abs_bits + (1 << delta_q_rem_bits) + 1
///           }
///           if ( delta_q_abs ) {
///               delta_q_sign_bit                                   L(1)
///               reducedDeltaQIndex = delta_q_sign_bit ? -delta_q_abs : delta_q_abs
///               CurrentQIndex = Clip3(1, 255,
///                   CurrentQIndex + (reducedDeltaQIndex << delta_q_res))
///           }
///       }
///   }
/// ```
///
/// ## Caller-supplied `reduced_delta_q_index`
///
/// The writer's per-call deliverable is the §5.11.12 *signed*
/// `reducedDeltaQIndex` value (the same intermediate the decoder
/// computes between the `delta_q_sign_bit` read and the `Clip3`
/// update). The Clip3 update itself is the caller's job: it operates
/// on the running `CurrentQIndex` accumulator, which the encoder
/// owns in its frame-walk state (mirroring the
/// [`crate::cdf::PartitionWalker::set_current_q_index`] / `decode_delta_qindex`
/// return-value pattern). This keeps the writer pure on grid-state.
///
/// `delta_q_abs` is derived as `reduced_delta_q_index.unsigned_abs() as u32`.
///
/// ## Arms
///
/// * **Superblock-skip short-circuit** — when `sub_size == sbSize &&
///   skip != 0` the §5.11.12 outer `if` returns without reading.
///   The writer emits no bits. `reduced_delta_q_index` MUST be `0`
///   (caller bug otherwise).
/// * **`!read_deltas` short-circuit** — outer `if (ReadDeltas)` is
///   false ⇒ no bits emitted. `reduced_delta_q_index` MUST be `0`.
/// * **Literal branch** — `delta_q_abs ∈ 0..DELTA_Q_SMALL` ⇒ one
///   `S()` over `TileDeltaQCdf` carrying `delta_q_abs`. If
///   `delta_q_abs != 0`, follow with `L(1)` for the sign bit.
/// * **Escape branch** — `delta_q_abs >= DELTA_Q_SMALL` ⇒ one
///   `S()` carrying `DELTA_Q_SMALL`, then the escape ladder:
///   `delta_q_rem_bits = L(3)` carrying `n - 1` (where `n` is
///   `FloorLog2(delta_q_abs - 1) - 1` such that the spec's
///   `delta_q_abs = abs_bits + (1 << n) + 1` reconstructs the input),
///   then `delta_q_abs_bits = L(n)` carrying `delta_q_abs - (1 << n) - 1`,
///   then `L(1)` for the sign bit.
///
/// ## Range guards
///
/// All surfaced as [`Error::PartitionWalkOutOfRange`]:
///
/// * `sub_size >= BLOCK_SIZES` (caller bug — invalid block-size ordinal).
/// * `delta_q_res > 3` (caller bug — §5.9.17 `f(2)` cap).
/// * Short-circuit arms with non-zero `reduced_delta_q_index` (caller bug —
///   the decoder won't read the bits, so the writer mustn't emit them
///   either).
/// * `delta_q_abs > 511` — exceeds the §5.11.12 escape-ladder reach
///   (`delta_q_rem_bits ∈ 0..=7` ⇒ `n ∈ 1..=8` ⇒ `delta_q_abs ∈
///   3..=511` on the escape arm; combined with `0..=DELTA_Q_SMALL =
///   0..=3` on the literal arm the full range is `0..=511`).
#[allow(clippy::too_many_arguments)]
pub fn write_delta_qindex(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    sub_size: usize,
    reduced_delta_q_index: i32,
    skip: u8,
    read_deltas: bool,
    use_128x128_superblock: bool,
    delta_q_res: u8,
) -> Result<(), Error> {
    if sub_size >= BLOCK_SIZES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if delta_q_res > 3 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.12 superblock-skip short-circuit.
    let sb_size = if use_128x128_superblock {
        BLOCK_128X128
    } else {
        BLOCK_64X64
    };
    if sub_size == sb_size && skip != 0 {
        if reduced_delta_q_index != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // §5.11.12 outer gate.
    if !read_deltas {
        if reduced_delta_q_index != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // §5.11.12 |delta_q_abs| derivation. The spec's range is `0..=511`
    // (literal arm covers `0..=3`, escape arm covers `3..=511`).
    let abs_value: u32 = reduced_delta_q_index.unsigned_abs();
    if abs_value > 511 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    let cdf = cdfs.delta_q_cdf();
    if (abs_value as usize) < DELTA_Q_SMALL {
        // §5.11.12 literal branch: `delta_q_abs S()` carries the value directly.
        writer.write_symbol(abs_value, cdf)?;
    } else {
        // §5.11.12 escape branch: `delta_q_abs S()` carries
        // DELTA_Q_SMALL, then the rem_bits / abs_bits ladder.
        writer.write_symbol(DELTA_Q_SMALL as u32, cdf)?;
        // Solve `abs_value = abs_bits + (1 << n) + 1` for the smallest
        // `n >= 1` such that `abs_bits = abs_value - (1 << n) - 1 <
        // (1 << n)` (i.e. `abs_value < 2 * (1 << n) + 1`). The
        // equivalent unbiased rephrase: `n = FloorLog2(abs_value - 1)`,
        // valid for `abs_value >= 3` (which the literal-branch guard
        // above already ensures — `DELTA_Q_SMALL == 3`).
        debug_assert!(abs_value >= 3);
        let n = floor_log2(abs_value - 1);
        debug_assert!((1..=8).contains(&n));
        // Spec writes `delta_q_rem_bits = L(3) + 1` ⇒ `rem_bits + 1 = n`
        // ⇒ encode `rem_bits = n - 1`.
        let rem_bits_field = n - 1;
        debug_assert!(rem_bits_field <= 7, "L(3) field width");
        writer.write_literal(3, rem_bits_field)?;
        // `delta_q_abs_bits = abs_value - (1 << n) - 1`, encoded in `L(n)`.
        let abs_bits = abs_value - (1u32 << n) - 1;
        debug_assert!(abs_bits < (1u32 << n));
        writer.write_literal(n, abs_bits)?;
    }

    // §5.11.12 sign bit only when `delta_q_abs != 0`.
    if abs_value != 0 {
        let sign: u32 = if reduced_delta_q_index < 0 { 1 } else { 0 };
        writer.write_literal(1, sign)?;
    }

    Ok(())
}

/// `read_delta_lf()` inverse per §5.11.13 (av1-spec p.68) — the
/// per-block per-loop-filter-strength delta writer that mirrors
/// [`crate::cdf::PartitionWalker::decode_delta_lf`].
///
/// Spec body (av1-spec p.68):
///
/// ```text
///   read_delta_lf( ) {
///       sbSize = use_128x128_superblock ? BLOCK_128X128 : BLOCK_64X64
///       if ( MiSize == sbSize && skip )
///           return
///       if ( ReadDeltas && delta_lf_present ) {
///           frameLfCount = 1
///           if ( delta_lf_multi )
///               frameLfCount = ( NumPlanes > 1 ) ? FRAME_LF_COUNT
///                                                : ( FRAME_LF_COUNT - 2 )
///           for ( i = 0; i < frameLfCount; i++ ) {
///               delta_lf_abs                                        S()
///               if ( delta_lf_abs == DELTA_LF_SMALL ) {
///                   delta_lf_rem_bits                              L(3)
///                   n = delta_lf_rem_bits + 1
///                   delta_lf_abs_bits                              L(n)
///                   deltaLfAbs = delta_lf_abs_bits + (1 << n) + 1
///               } else {
///                   deltaLfAbs = delta_lf_abs
///               }
///               if ( deltaLfAbs ) {
///                   delta_lf_sign_bit                              L(1)
///                   reducedDeltaLfLevel = delta_lf_sign_bit ?
///                                            -deltaLfAbs : deltaLfAbs
///                   DeltaLF[ i ] = Clip3( -MAX_LOOP_FILTER, MAX_LOOP_FILTER,
///                       DeltaLF[ i ] + (reducedDeltaLfLevel << delta_lf_res) )
///               }
///           }
///       }
///   }
/// ```
///
/// ## Caller-supplied `reduced_delta_lf` row
///
/// The writer's per-iteration deliverable is the §5.11.13 *signed*
/// `reducedDeltaLfLevel[ i ]` value (the intermediate the decoder
/// computes between the `delta_lf_sign_bit` read and the `Clip3`
/// update). The Clip3 update is the caller's job; it operates on the
/// running `DeltaLF[ i ]` accumulator that the encoder owns in its
/// frame-walk state (mirroring the
/// [`crate::cdf::PartitionWalker::current_delta_lf`] /
/// `decode_delta_lf` return-value pattern).
///
/// `reduced_delta_lf` is fixed-length [`FRAME_LF_COUNT`]. The writer
/// reads only the first `frameLfCount` entries (`1` on the
/// `!delta_lf_multi` branch; either `FRAME_LF_COUNT` or
/// `FRAME_LF_COUNT - 2` on the multi branch per `mono_chrome`).
/// Entries beyond `frameLfCount` MUST be `0` (caller bug otherwise —
/// the decoder won't observe them).
///
/// ## Arms
///
/// Per-iteration shape mirrors `write_delta_qindex` exactly except
/// for the CDF source: `delta_lf_cdf()` (single-LF) or
/// `delta_lf_multi_cdf(i)` (multi-LF). The escape ladder, literal
/// branch, and sign bit are identical to §5.11.12.
///
/// ## Range guards
///
/// All surfaced as [`Error::PartitionWalkOutOfRange`]:
///
/// * `sub_size >= BLOCK_SIZES`.
/// * `delta_lf_res > 3` (caller bug — §5.9.18 `f(2)` cap).
/// * Short-circuit arms with any non-zero `reduced_delta_lf` entry.
/// * Any `reduced_delta_lf[ i ].unsigned_abs() > 511` (exceeds the
///   escape-ladder reach).
/// * Entries beyond `frameLfCount` being non-zero.
#[allow(clippy::too_many_arguments)]
pub fn write_delta_lf(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    sub_size: usize,
    reduced_delta_lf: &[i32; FRAME_LF_COUNT],
    skip: u8,
    read_deltas: bool,
    delta_lf_present: bool,
    delta_lf_multi: bool,
    mono_chrome: bool,
    use_128x128_superblock: bool,
    delta_lf_res: u8,
) -> Result<(), Error> {
    if sub_size >= BLOCK_SIZES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if delta_lf_res > 3 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.13 superblock-skip short-circuit (identical to §5.11.12).
    let sb_size = if use_128x128_superblock {
        BLOCK_128X128
    } else {
        BLOCK_64X64
    };
    let sb_short_circuit = sub_size == sb_size && skip != 0;
    let read_gate = read_deltas && delta_lf_present;

    if sb_short_circuit || !read_gate {
        if reduced_delta_lf.iter().any(|&v| v != 0) {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // §5.11.13 frameLfCount derivation.
    let frame_lf_count: usize = if delta_lf_multi {
        if mono_chrome {
            FRAME_LF_COUNT - 2
        } else {
            FRAME_LF_COUNT
        }
    } else {
        1
    };

    // Entries beyond frameLfCount must be 0 (decoder won't read them).
    for &v in reduced_delta_lf.iter().skip(frame_lf_count) {
        if v != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
    }

    for (i, &reduced) in reduced_delta_lf.iter().take(frame_lf_count).enumerate() {
        let abs_value: u32 = reduced.unsigned_abs();
        if abs_value > 511 {
            return Err(Error::PartitionWalkOutOfRange);
        }

        let cdf: &mut [u16] = if delta_lf_multi {
            cdfs.delta_lf_multi_cdf(i)
        } else {
            cdfs.delta_lf_cdf()
        };
        if (abs_value as usize) < DELTA_LF_SMALL {
            // §5.11.13 literal branch.
            writer.write_symbol(abs_value, cdf)?;
        } else {
            // §5.11.13 escape branch — same shape as §5.11.12.
            writer.write_symbol(DELTA_LF_SMALL as u32, cdf)?;
            debug_assert!(abs_value >= 3);
            let n = floor_log2(abs_value - 1);
            debug_assert!((1..=8).contains(&n));
            let rem_bits_field = n - 1;
            debug_assert!(rem_bits_field <= 7);
            writer.write_literal(3, rem_bits_field)?;
            let abs_bits = abs_value - (1u32 << n) - 1;
            debug_assert!(abs_bits < (1u32 << n));
            writer.write_literal(n, abs_bits)?;
        }

        if abs_value != 0 {
            let sign: u32 = if reduced < 0 { 1 } else { 0 };
            writer.write_literal(1, sign)?;
        }
    }

    Ok(())
}

/// `intra_angle_info_y( )` inverse per §5.11.42 (av1-spec p.95) — r260.
///
/// Spec body:
/// ```text
///   intra_angle_info_y() {
///       AngleDeltaY = 0
///       if ( MiSize >= BLOCK_8X8 ) {
///           if ( is_directional_mode( YMode ) ) {
///               angle_delta_y                                  S()
///               AngleDeltaY = angle_delta_y - MAX_ANGLE_DELTA
///           }
///       }
///   }
/// ```
///
/// Mirrors the §5.11.42 reader inlined into
/// [`crate::cdf::PartitionWalker::decode_intra_block_mode_info`] at
/// av1-spec p.72 line 4 (call site `intra_angle_info_y( )` inside
/// §5.11.22).
///
/// Inputs:
/// * `angle_delta_y` — the signed `AngleDeltaY` value the encoder
///   committed to (in `-MAX_ANGLE_DELTA..=MAX_ANGLE_DELTA = -3..=3`
///   per §3 / §5.11.42).
/// * `mi_size` — the §5.11.5 `MiSize` ordinal in `0..BLOCK_SIZES`.
/// * `y_mode` — the §5.11.22 `YMode` value in `0..INTRA_MODES = 13`
///   per §3, used for the §5.11.44 `is_directional_mode` short-circuit.
///
/// CDF selection per §8.3.2: `TileAngleDeltaCdf[ YMode - V_PRED ]`
/// when the §5.11.42 reader fires; the row is 8-wide (7 symbols +
/// the §8.2.6 counter). The reader writes `angle_delta_y = AngleDeltaY
/// + MAX_ANGLE_DELTA` (the inverse of the spec's
/// `AngleDeltaY = angle_delta_y - MAX_ANGLE_DELTA` recovery).
///
/// Short-circuits (no bits emitted) when:
/// * `mi_size < BLOCK_8X8`, OR
/// * `!is_directional_mode( YMode )` (§5.11.44: directional modes are
///   `V_PRED = 1 <= YMode <= D67_PRED = 8`).
///
/// In both short-circuit arms `angle_delta_y` MUST be `0` — caller
/// bug otherwise, surfaced as [`Error::PartitionWalkOutOfRange`].
pub fn write_intra_angle_info_y(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    angle_delta_y: i8,
    mi_size: usize,
    y_mode: u8,
) -> Result<(), Error> {
    if mi_size >= BLOCK_SIZES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if (y_mode as usize) >= INTRA_MODES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let max = MAX_ANGLE_DELTA as i8;
    if !(-max..=max).contains(&angle_delta_y) {
        return Err(Error::PartitionWalkOutOfRange);
    }

    let reads = mi_size >= BLOCK_8X8 && is_directional(y_mode);
    if !reads {
        // §5.11.42 short-circuit arm — `AngleDeltaY = 0`. Caller-bug
        // guard: any non-zero `angle_delta_y` here is a contract
        // violation (the decoder would reconstruct `0` regardless).
        if angle_delta_y != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // §5.11.42 reader: `angle_delta_y S()` against
    // `TileAngleDeltaCdf[ YMode - V_PRED ]`. The raw S() value is
    // `AngleDeltaY + MAX_ANGLE_DELTA`, in `0..(2 * MAX_ANGLE_DELTA + 1) = 7`.
    let raw: u32 = (angle_delta_y as i32 + MAX_ANGLE_DELTA as i32) as u32;
    let cdf = cdfs
        .angle_delta_cdf(y_mode as usize)
        .ok_or(Error::PartitionWalkOutOfRange)?;
    writer.write_symbol(raw, cdf)
}

/// `intra_angle_info_uv( )` inverse per §5.11.43 (av1-spec p.95) — r260.
///
/// Spec body (mirror of §5.11.42 with `UVMode` in place of `YMode`):
/// ```text
///   intra_angle_info_uv() {
///       AngleDeltaUV = 0
///       if ( MiSize >= BLOCK_8X8 ) {
///           if ( is_directional_mode( UVMode ) ) {
///               angle_delta_uv                                 S()
///               AngleDeltaUV = angle_delta_uv - MAX_ANGLE_DELTA
///           }
///       }
///   }
/// ```
///
/// Mirrors the §5.11.43 reader inlined into
/// [`crate::cdf::PartitionWalker::decode_intra_block_mode_info`] at
/// av1-spec p.73 line 10 (call site `intra_angle_info_uv( )` inside
/// the §5.11.22 `if ( HasChroma )` arm).
///
/// Inputs / CDF selection / short-circuits are the §5.11.42 mirror
/// with `uv_mode` in place of `y_mode` (the §3 directional-mode range
/// is the same: `V_PRED = 1..=D67_PRED = 8` for both planes since
/// `UVMode` shares the §3 intra-mode enumeration). `uv_mode` MUST be
/// in `0..UV_INTRA_MODES_CFL_ALLOWED = 14`; non-directional values
/// (including `UV_CFL_PRED = 13`) take the short-circuit arm.
pub fn write_intra_angle_info_uv(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    angle_delta_uv: i8,
    mi_size: usize,
    uv_mode: u8,
) -> Result<(), Error> {
    if mi_size >= BLOCK_SIZES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if (uv_mode as usize) >= UV_INTRA_MODES_CFL_ALLOWED {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let max = MAX_ANGLE_DELTA as i8;
    if !(-max..=max).contains(&angle_delta_uv) {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.43 / §5.11.44: the directional range is `V_PRED..=D67_PRED`
    // on both planes (the U-plane CFL signalling at `UV_CFL_PRED = 13`
    // is non-directional). `is_directional` returns `false` for
    // `UV_CFL_PRED` since `is_directional` checks the same §3 range.
    let reads = mi_size >= BLOCK_8X8 && is_directional(uv_mode);
    if !reads {
        if angle_delta_uv != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    let raw: u32 = (angle_delta_uv as i32 + MAX_ANGLE_DELTA as i32) as u32;
    let cdf = cdfs
        .angle_delta_cdf(uv_mode as usize)
        .ok_or(Error::PartitionWalkOutOfRange)?;
    writer.write_symbol(raw, cdf)
}

/// `filter_intra_mode_info( )` inverse per §5.11.24 (av1-spec p.74) — r260.
///
/// Spec body:
/// ```text
///   filter_intra_mode_info() {
///       use_filter_intra = 0
///       if ( enable_filter_intra &&
///            YMode == DC_PRED && PaletteSizeY == 0 &&
///            Max( Block_Width[ MiSize ], Block_Height[ MiSize ] ) <= 32 ) {
///           use_filter_intra                                   S()
///           if ( use_filter_intra ) {
///               filter_intra_mode                              S()
///           }
///       }
///   }
/// ```
///
/// Mirrors the §5.11.24 reader inlined into
/// [`crate::cdf::PartitionWalker::decode_intra_block_mode_info`] at
/// av1-spec p.73 line 16 (call site `filter_intra_mode_info( )` at
/// the tail of §5.11.22).
///
/// Inputs:
/// * `use_filter_intra` — `0` / `1`. When the outer gate is closed
///   MUST be `0`; otherwise the encoder's committed §5.11.24
///   `use_filter_intra` symbol.
/// * `filter_intra_mode` — `Some(mode)` when `use_filter_intra == 1`
///   (`mode` in `0..INTRA_FILTER_MODES = 5`), `None` otherwise.
/// * `mi_size` — `MiSize` ordinal in `0..BLOCK_SIZES`.
/// * `y_mode` — the §5.11.22 `YMode` value (gate fires only on
///   `DC_PRED = 0`).
/// * `palette_size_y` — the §5.11.46 `PaletteSizeY` derivation (gate
///   fires only on `PaletteSizeY == 0`). `u32` to match the
///   walker's grid-side type.
/// * `enable_filter_intra` — §5.5.2 sequence-header bit gating the
///   §5.11.24 outer arm.
///
/// CDF selection per §8.3.2: `TileFilterIntraCdf[ MiSize ]` for the
/// outer `use_filter_intra` S(); `TileFilterIntraModeCdf` (a single
/// context-free row over `INTRA_FILTER_MODES = 5` modes) for the
/// inner `filter_intra_mode` S().
///
/// Outer-gate short-circuits (no bits emitted, must satisfy
/// `use_filter_intra == 0 && filter_intra_mode == None`):
/// * `!enable_filter_intra`
/// * `y_mode != DC_PRED` (DC_PRED = 0)
/// * `palette_size_y != 0`
/// * `Max( Block_Width[ MiSize ], Block_Height[ MiSize ] ) > 32`
///
/// Inner-arm contract: when the outer gate fires, `use_filter_intra
/// == 1` requires `filter_intra_mode == Some(mode)` with `mode in
/// 0..INTRA_FILTER_MODES`; `use_filter_intra == 0` requires
/// `filter_intra_mode == None`.
#[allow(clippy::too_many_arguments)]
pub fn write_filter_intra_mode_info(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    use_filter_intra: u8,
    filter_intra_mode: Option<u8>,
    mi_size: usize,
    y_mode: u8,
    palette_size_y: u32,
    enable_filter_intra: bool,
) -> Result<(), Error> {
    if mi_size >= BLOCK_SIZES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if (y_mode as usize) >= INTRA_MODES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if use_filter_intra > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.24 outer gate. DC_PRED = 0 per §3.
    let bw = block_width(mi_size);
    let bh = block_height(mi_size);
    let outer_gate = enable_filter_intra && y_mode == 0 && palette_size_y == 0 && bw.max(bh) <= 32;

    if !outer_gate {
        // Caller-bug guard — the §5.11.24 body sets `use_filter_intra
        // = 0` unconditionally on the gate-closed arm; the decoder
        // reconstructs `use_filter_intra = 0` regardless of the
        // bitstream so a non-zero caller value is a contract
        // violation.
        if use_filter_intra != 0 || filter_intra_mode.is_some() {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // §5.11.24: `use_filter_intra S()` against
    // `TileFilterIntraCdf[ MiSize ]` (2-wide, no §8.3.2 ctx beyond
    // `MiSize`).
    let cdf = cdfs.filter_intra_cdf(mi_size);
    writer.write_symbol(use_filter_intra as u32, cdf)?;

    if use_filter_intra == 1 {
        // §5.11.24 inner arm: `filter_intra_mode S()` against
        // `TileFilterIntraModeCdf` (no §8.3.2 ctx, single row).
        let mode = filter_intra_mode.ok_or(Error::PartitionWalkOutOfRange)?;
        if (mode as usize) >= INTRA_FILTER_MODES {
            return Err(Error::PartitionWalkOutOfRange);
        }
        let row = cdfs.filter_intra_mode_cdf();
        writer.write_symbol(mode as u32, row)?;
    } else if filter_intra_mode.is_some() {
        // §5.11.24 contract: when `use_filter_intra == 0` the spec
        // body never reads `filter_intra_mode`; a `Some` value here
        // is a caller bug.
        return Err(Error::PartitionWalkOutOfRange);
    }

    Ok(())
}

/// `palette_mode_info( )` inverse per §5.11.46 (av1-spec p.97-98) —
/// r261. No-palette-arm only: commits `has_palette_y == 0` AND
/// `has_palette_uv == 0` to the bit stream, mirroring the §5.11.46
/// reader's outer-arm + inner-`has_palette_* = 0` branch.
///
/// Spec body (abbreviated to the surface this leaf covers):
/// ```text
///   palette_mode_info() {
///       bsizeCtx = Mi_Width_Log2[ MiSize ] + Mi_Height_Log2[ MiSize ] - 2
///       if ( YMode == DC_PRED ) {
///           has_palette_y                                 S()
///           if ( has_palette_y ) {
///               palette_size_y_minus_2                     S()  ← NOT
///               // … palette_colors_y[] reads …                 covered
///           }
///       }
///       if ( HasChroma && UVMode == DC_PRED ) {
///           has_palette_uv                                S()
///           if ( has_palette_uv ) {
///               palette_size_uv_minus_2                    S()  ← NOT
///               // … palette_colors_u[] / palette_colors_v[] reads …
///           }
///       }
///   }
/// ```
///
/// Mirrors the §5.11.46 reader inlined into
/// [`crate::cdf::PartitionWalker::decode_intra_block_mode_info`] at
/// av1-spec p.73 line 13-15 (call site `palette_mode_info( )` inside
/// §5.11.22). Restricted to the no-palette path because the
/// §5.11.46 palette-entries syntax (cache merge + `L(BitDepth)`
/// literal + `paletteBits` delta loop + trailing sort) is a much
/// larger write surface that needs its own arc; this leaf is the
/// shape the §5.11.22 dispatcher (next round) needs to satisfy its
/// `palette_size_y == 0` precondition for §5.11.24
/// `write_filter_intra_mode_info`.
///
/// Inputs:
/// * `has_palette_y` — MUST be `0` on the no-palette arm. Any
///   non-zero value is a contract violation surfaced as
///   [`Error::PaletteEntriesUnsupported`] (mirrors the reader's
///   short-circuit return on a non-zero decoded size).
/// * `has_palette_uv` — same shape as `has_palette_y` on the chroma
///   arm; MUST be `0`.
/// * `mi_size` — `MiSize` ordinal in `0..BLOCK_SIZES`.
/// * `y_mode` — the §5.11.22 `YMode` value (gate fires only on
///   `DC_PRED = 0`).
/// * `uv_mode` — the §5.11.22 `UVMode` value (gate fires only on
///   `has_chroma && DC_PRED`).  Pass `None` when `has_chroma ==
///   false` (matches the §5.11.22 line 5 `if (HasChroma)` guard the
///   dispatcher honours).
/// * `has_chroma` — §5.11.5 `HasChroma`.
/// * `allow_screen_content_tools` — §5.9.5 sequence-header bit;
///   gates the §5.11.46 outer arm.
/// * `above_palette_y` / `left_palette_y` — §8.3.2 neighbour-palette
///   booleans, fed to [`palette_y_mode_ctx`].
///
/// CDF selection per §8.3.2:
/// * `has_palette_y` against `TilePaletteYModeCdf[ bsizeCtx ][ ctx_y
///   ]` (2-wide row, `ctx_y = palette_y_mode_ctx(above, left)`).
/// * `has_palette_uv` against `TilePaletteUVModeCdf[ ctx_uv ]`
///   (2-wide row, `ctx_uv = palette_uv_mode_ctx(palette_size_y)` —
///   on the no-palette arm `palette_size_y == 0` so `ctx_uv == 0`).
///
/// Outer-gate short-circuits (no bits emitted) when:
/// * `!allow_screen_content_tools`, OR
/// * `mi_size < BLOCK_8X8`, OR
/// * `Block_Width[ MiSize ] > 64`, OR
/// * `Block_Height[ MiSize ] > 64`.
///
/// On the gate-off path `has_palette_y` MUST be `0` AND
/// `has_palette_uv` MUST be `0` — caller bug otherwise.
///
/// Inner-arm short-circuits (no bits emitted on that plane):
/// * Luma: `y_mode != DC_PRED` ⇒ `has_palette_y` MUST be `0`.
/// * Chroma: `!has_chroma || uv_mode != Some(DC_PRED)` ⇒
///   `has_palette_uv` MUST be `0`.
#[allow(clippy::too_many_arguments)]
pub fn write_palette_mode_info(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    has_palette_y: u8,
    has_palette_uv: u8,
    mi_size: usize,
    y_mode: u8,
    uv_mode: Option<u8>,
    has_chroma: bool,
    allow_screen_content_tools: bool,
    above_palette_y: bool,
    left_palette_y: bool,
) -> Result<(), Error> {
    if mi_size >= BLOCK_SIZES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if (y_mode as usize) >= INTRA_MODES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if let Some(uv) = uv_mode {
        // §5.11.22 line 6: `uv_mode` ∈ `0..UV_INTRA_MODES_CFL_ALLOWED
        // = 14`. CFL_PRED = 13 is the widest reachable; pass through
        // the looser bound here.
        if (uv as usize) >= UV_INTRA_MODES_CFL_ALLOWED {
            return Err(Error::PartitionWalkOutOfRange);
        }
    }
    if has_palette_y > 1 || has_palette_uv > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }
    // This arc covers the no-palette path only. Any caller that
    // commits a non-zero `has_palette_*` is asking for the
    // §5.11.46 palette-entries syntax that isn't yet wired
    // through this writer; surface a clear error so the dispatcher
    // can re-route once the arc lands.
    if has_palette_y != 0 || has_palette_uv != 0 {
        return Err(Error::PaletteEntriesUnsupported);
    }

    // §5.11.46 outer gate. Mirrors the reader's `palette_outer_gate`.
    let bw = block_width(mi_size) as u32;
    let bh = block_height(mi_size) as u32;
    let outer_gate = allow_screen_content_tools && mi_size >= BLOCK_8X8 && bw <= 64 && bh <= 64;

    if !outer_gate {
        // Gate-off path: the §5.11.46 spec body sets `PaletteSizeY =
        // 0` / `PaletteSizeUV = 0` unconditionally. The reader
        // reconstructs them as 0 regardless of the bit stream; a
        // non-zero `has_palette_*` was rejected above so the gate-off
        // arm emits zero bits and returns cleanly.
        return Ok(());
    }

    // §5.11.46: `bsizeCtx = Mi_Width_Log2[ MiSize ] +
    // Mi_Height_Log2[ MiSize ] - 2`. The §9.3 mapping guarantees
    // `bsizeCtx ∈ 0..PALETTE_BLOCK_SIZE_CONTEXTS = 7` for any MiSize
    // that passed the outer gate (BLOCK_8X8 has (1, 1) ⇒ bsizeCtx = 0,
    // BLOCK_64X64 has (4, 4) ⇒ bsizeCtx = 6).
    let bsize_ctx = mi_width_log2(mi_size) + mi_height_log2(mi_size) - 2;
    debug_assert!(
        bsize_ctx < PALETTE_BLOCK_SIZE_CONTEXTS,
        "§5.11.46 bsizeCtx must be in 0..PALETTE_BLOCK_SIZE_CONTEXTS"
    );

    // §5.11.46 luma arm: `if ( YMode == DC_PRED )`.
    if y_mode == 0 {
        let ctx_y = palette_y_mode_ctx(above_palette_y, left_palette_y);
        let row = cdfs.palette_y_mode_cdf(bsize_ctx, ctx_y);
        writer.write_symbol(has_palette_y as u32, row)?;
        // `has_palette_y == 1` short-circuited above with
        // PaletteEntriesUnsupported; nothing further to emit on the
        // luma arm.
    } else if has_palette_y != 0 {
        // §5.11.46 contract: when `y_mode != DC_PRED` the reader
        // never reads `has_palette_y`; non-zero here is a caller bug.
        // (Re-asserted because the early-return only catches the
        // gate-off arm.)
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.46 chroma arm: `if ( HasChroma && UVMode == DC_PRED )`.
    if has_chroma && uv_mode == Some(0) {
        // On the no-palette path `palette_size_y == 0`, so the
        // §8.3.2 chroma ctx is `palette_uv_mode_ctx(0) = 0`.
        let ctx_uv = palette_uv_mode_ctx(0);
        let row = cdfs.palette_uv_mode_cdf(ctx_uv);
        writer.write_symbol(has_palette_uv as u32, row)?;
    } else if has_palette_uv != 0 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    Ok(())
}

/// `palette_colors_y[]` writer per §5.11.46 (av1-spec p.97-98) —
/// r262. The luma-plane half of the §5.11.46 palette-entries syntax:
/// the inverse of [`crate::cdf::read_palette_entries_y`].
///
/// Spec body (Y arm of §5.11.46, post-`PaletteSizeY` read):
/// ```text
///   // §5.11.49 neighbour-merge cache, plane = 0 (luma).
///   cache[] = get_palette_cache( 0, mi_row, mi_col )
///   idx = 0
///   for ( i = 0; i < cache_n && idx < PaletteSizeY; i++ ) {
///       use_palette_color_cache_y                          L(1)
///       if ( use_palette_color_cache_y ) {
///           palette_colors_y[ idx++ ] = cache[ i ]
///       }
///   }
///   if ( idx < PaletteSizeY ) {
///       palette_colors_y[ idx++ ]                          L(BitDepth)
///   }
///   if ( idx < PaletteSizeY ) {
///       minBits  = BitDepth - 3
///       maxVal   = 1 << BitDepth
///       palette_num_extra_bits_y                           L(2)
///       paletteBits = minBits + palette_num_extra_bits_y
///   }
///   while ( idx < PaletteSizeY ) {
///       palette_delta_y                                    L(paletteBits)
///       palette_delta_y++
///       palette_colors_y[ idx ] = Clip1( palette_colors_y[ idx - 1 ]
///                                        + palette_delta_y )
///       range = maxVal - palette_colors_y[ idx ] - 1
///       paletteBits = Min( paletteBits, CeilLog2( range ) )
///       idx++
///   }
///   sort( palette_colors_y[ 0 .. PaletteSizeY - 1 ] )
/// ```
///
/// **Mirror invariant.** This writer takes `palette_colors_y` already
/// sorted ascending (the reader's trailing `sort()` step is the
/// canonical form). The cache + literal + paletteBits + delta loop
/// then re-derives the exact L(1) / L(BitDepth) / L(2) / L(paletteBits)
/// bit stream that, when fed back through
/// [`crate::cdf::read_palette_entries_y`], reconstructs the same
/// sorted entries — a property each round-trip test below verifies
/// directly.
///
/// **paletteBits derivation.** The encoder must choose
/// `palette_num_extra_bits_y` (`L(2)` → 0..=3) such that every
/// downstream `palette_delta_y` raw value fits in the *current*
/// `paletteBits`, which the reader refines after every entry by
/// `paletteBits = Min(paletteBits, CeilLog2(range))`. We simulate
/// the reader's refinement for each candidate `extra ∈ {0, 1, 2, 3}`
/// and pick the smallest one that admits encoding every remaining
/// delta. Such an `extra` always exists when `palette_colors_y` is
/// strictly ascending in `[0, 2^BitDepth)`: the maximum raw delta
/// `(palette_colors_y[idx] - palette_colors_y[idx - 1]) - 1` is
/// bounded by `2^BitDepth - 2`, which fits in `BitDepth` bits —
/// `minBits + 3 = BitDepth - 3 + 3 = BitDepth` — and the §5.11.46
/// refinement only *reduces* `paletteBits` along the way (since
/// `range = maxVal - colors[idx] - 1 < maxVal`).
///
/// **Caller contract.** Inputs must satisfy:
/// * `bit_depth ∈ {8, 10, 12}` (§5.5.2).
/// * `palette_size_y ∈ 2..=PALETTE_COLORS` (§5.11.46 contract — the
///   reader's outer arm only ever calls this with a non-zero size,
///   bounded above by `PALETTE_COLORS = 8`).
/// * `palette_colors_y[0 .. palette_size_y]` strictly ascending, each
///   entry in `[0, 2^bit_depth)`. Duplicate or descending entries
///   would surface non-positive deltas the spec doesn't admit on
///   this arm — [`Error::PartitionWalkOutOfRange`].
/// * `palette_colors_y[palette_size_y ..]` ignored (per the reader's
///   `[0u16; PALETTE_COLORS]` zero-fill convention).
/// * `walker` MUST be at the same §5.11.49 cache state the reader
///   would observe — caller threads its own
///   [`PartitionWalker`](crate::cdf::PartitionWalker) and stamps any
///   preceding decoded blocks into the `PaletteSizes` / `PaletteColors`
///   grids before this call.
///
/// On success, the encoder has committed the §5.11.46 luma palette
/// entries to the bit stream in exact lock-step with the
/// [`crate::cdf::read_palette_entries_y`] reader.
#[allow(clippy::too_many_arguments)]
pub fn write_palette_entries_y(
    writer: &mut SymbolWriter,
    bit_depth: u8,
    palette_size_y: usize,
    palette_colors_y: &[u16],
    walker: &PartitionWalker,
    mi_row: u32,
    mi_col: u32,
) -> Result<(), Error> {
    if !matches!(bit_depth, 8 | 10 | 12) {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if !(2..=PALETTE_COLORS).contains(&palette_size_y) {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if palette_colors_y.len() < palette_size_y {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let bd = bit_depth as u32;
    let max_val: u32 = 1u32 << bd;
    // §5.11.46 caller contract: entries strictly ascending and each
    // within [0, 2^BitDepth). Mirrors the reader's invariant after
    // the trailing sort + Clip1.
    for (i, &c) in palette_colors_y.iter().take(palette_size_y).enumerate() {
        if (c as u32) >= max_val {
            return Err(Error::PartitionWalkOutOfRange);
        }
        if i > 0 && (c as u32) <= palette_colors_y[i - 1] as u32 {
            return Err(Error::PartitionWalkOutOfRange);
        }
    }

    // §5.11.49 cache via the same walker accessor the reader uses.
    let mut cache_buf = [0u16; 2 * PALETTE_COLORS];
    let cache_n = walker.get_palette_cache(0, mi_row, mi_col, &mut cache_buf);

    // §5.11.46 cache-coded indices loop. For each cache slot we ask
    // "is this slot the next palette entry the decoder should adopt?"
    // and emit L(1). The match condition is `cache[i] ==
    // palette_colors_y[idx]` — the reader assigns `cache[i]` to
    // `palette_colors_y[idx]` and advances `idx` on use_cache == 1,
    // so we mirror that decision-tree exactly.
    let mut idx: usize = 0;
    {
        let mut i = 0usize;
        while i < cache_n && idx < palette_size_y {
            let use_cache = if cache_buf[i] == palette_colors_y[idx] {
                1u32
            } else {
                0u32
            };
            writer.write_literal(1, use_cache)?;
            if use_cache != 0 {
                idx += 1;
            }
            i += 1;
        }
    }

    // §5.11.46 first "new" entry as L(BitDepth) literal.
    if idx < palette_size_y {
        let v = palette_colors_y[idx] as u32;
        writer.write_literal(bd, v)?;
        idx += 1;
    }

    // §5.11.46 paletteBits derivation: pick the smallest `extra ∈
    // {0, 1, 2, 3}` such that every remaining `palette_delta_y` fits
    // in the running `paletteBits` after the reader's refinement.
    // When `idx == palette_size_y` (no entries remain in the delta
    // loop), the L(2) read is skipped on the reader side — we skip
    // the write to match.
    if idx < palette_size_y {
        // §5.11.46: `minBits = BitDepth - 3`. BitDepth ∈ {8, 10, 12}
        // ⇒ minBits ∈ {5, 7, 9} (never negative).
        let min_bits = bd - 3;
        let start_idx = idx;
        let extra = pick_palette_extra_bits_y(palette_colors_y, palette_size_y, start_idx, bd)?;
        writer.write_literal(2, extra)?;
        let mut palette_bits = min_bits + extra;
        // §5.11.46 delta loop — emit each delta minus 1 (the spec's
        // `palette_delta_y++` inverse) and refine paletteBits in
        // lock-step with the reader.
        while idx < palette_size_y {
            let prev = palette_colors_y[idx - 1] as u32;
            let cur = palette_colors_y[idx] as u32;
            // Strictly-ascending pre-check above guarantees cur > prev.
            let delta_raw = cur - prev - 1;
            // The picker guarantees the raw value fits, but cheap
            // defensive guard: a stray `paletteBits == 0` arm can
            // only encode delta_raw == 0; surface caller-bug otherwise.
            if palette_bits == 0 {
                if delta_raw != 0 {
                    return Err(Error::PartitionWalkOutOfRange);
                }
                // L(0) writes no bits — mirror writer's contract.
                writer.write_literal(0, 0)?;
            } else {
                if delta_raw >= (1u32 << palette_bits) {
                    return Err(Error::PartitionWalkOutOfRange);
                }
                writer.write_literal(palette_bits, delta_raw)?;
            }
            // §5.11.46 paletteBits refinement: identical to the
            // reader's. The Clip1 step is a no-op because the entry
            // is already in [0, 2^BitDepth).
            let range = max_val.saturating_sub(cur).saturating_sub(1);
            palette_bits = palette_bits.min(ceil_log2_av1(range));
            idx += 1;
        }
    }

    Ok(())
}

/// Pick the smallest `palette_num_extra_bits_y` (L(2) ⇒ 0..=3) such
/// that every remaining §5.11.46 Y-plane delta fits in the
/// reader-refined `paletteBits`. Returns
/// [`Error::PartitionWalkOutOfRange`] when no choice in 0..=3 admits
/// all deltas — this only happens on a malformed
/// `palette_colors_y` (the strict-ascending + bit-depth check before
/// this call rules out the conformant input).
fn pick_palette_extra_bits_y(
    palette_colors_y: &[u16],
    palette_size_y: usize,
    start_idx: usize,
    bd: u32,
) -> Result<u32, Error> {
    let min_bits = bd - 3;
    let max_val: u32 = 1u32 << bd;
    for extra in 0..=3u32 {
        let mut palette_bits = min_bits + extra;
        let mut ok = true;
        let mut idx = start_idx;
        while idx < palette_size_y {
            let prev = palette_colors_y[idx - 1] as u32;
            let cur = palette_colors_y[idx] as u32;
            let delta_raw = cur - prev - 1;
            // Capacity check mirroring the L(paletteBits) writer.
            // L(0) admits only the raw value 0.
            let capacity = if palette_bits >= 32 {
                u32::MAX
            } else {
                1u32 << palette_bits
            };
            if palette_bits == 0 {
                if delta_raw != 0 {
                    ok = false;
                    break;
                }
            } else if delta_raw >= capacity {
                ok = false;
                break;
            }
            let range = max_val.saturating_sub(cur).saturating_sub(1);
            palette_bits = palette_bits.min(ceil_log2_av1(range));
            idx += 1;
        }
        if ok {
            return Ok(extra);
        }
    }
    Err(Error::PartitionWalkOutOfRange)
}

/// `palette_colors_u[]` + `palette_colors_v[]` writer per §5.11.46
/// (av1-spec p.97-98) — r263. The UV-plane half of the §5.11.46
/// palette-entries syntax: the inverse of
/// [`crate::cdf::read_palette_entries_uv`].
///
/// Spec body (UV arms of §5.11.46, post-`PaletteSizeUV` read):
/// ```text
///   // U plane — mirrors Y, minus the `delta_u++` step and minus the
///   // `- 1` on the range derivation.
///   cache[] = get_palette_cache( 1, mi_row, mi_col )
///   idx = 0
///   for ( i = 0; i < cache_n && idx < PaletteSizeUV; i++ ) {
///       use_palette_color_cache_u                          L(1)
///       if ( use_palette_color_cache_u ) {
///           palette_colors_u[ idx++ ] = cache[ i ]
///       }
///   }
///   if ( idx < PaletteSizeUV ) {
///       palette_colors_u[ idx++ ]                          L(BitDepth)
///   }
///   if ( idx < PaletteSizeUV ) {
///       minBits     = BitDepth - 3
///       palette_num_extra_bits_u                           L(2)
///       paletteBits = minBits + palette_num_extra_bits_u
///   }
///   while ( idx < PaletteSizeUV ) {
///       palette_delta_u                                    L(paletteBits)
///       palette_colors_u[ idx ] = Clip1( palette_colors_u[ idx - 1 ]
///                                        + palette_delta_u )
///       range = ( 1 << BitDepth ) - palette_colors_u[ idx ]
///       paletteBits = Min( paletteBits, CeilLog2( range ) )
///       idx++
///   }
///   sort( palette_colors_u[ 0 .. PaletteSizeUV - 1 ] )
///
///   // V plane — two-arm dispatcher gated on `delta_encode_palette_colors_v`.
///   delta_encode_palette_colors_v                          L(1)
///   if ( delta_encode_palette_colors_v ) {
///       minBits     = BitDepth - 4
///       maxVal      = 1 << BitDepth
///       palette_num_extra_bits_v                           L(2)
///       paletteBits = minBits + palette_num_extra_bits_v
///       palette_colors_v[ 0 ]                              L(BitDepth)
///       for ( idx = 1; idx < PaletteSizeUV; idx++ ) {
///           palette_delta_v                                L(paletteBits)
///           if ( palette_delta_v ) {
///               palette_delta_sign_bit_v                   L(1)
///               if ( palette_delta_sign_bit_v ) palette_delta_v = -palette_delta_v
///           }
///           val = palette_colors_v[ idx - 1 ] + palette_delta_v
///           if ( val < 0 )       val += maxVal
///           if ( val >= maxVal ) val -= maxVal
///           palette_colors_v[ idx ] = Clip1( val )
///       }
///       // NOTE: no sort() on the V delta arm — source order preserved.
///   } else {
///       for ( idx = 0; idx < PaletteSizeUV; idx++ ) {
///           palette_colors_v[ idx ]                        L(BitDepth)
///       }
///   }
/// ```
///
/// **U-plane mirror invariant.** Caller passes `palette_colors_u` already
/// in the post-sort canonical order (non-strictly ascending — duplicate
/// entries are legal here because the U delta loop has no `++` step).
/// The cache + literal + paletteBits + delta loop then reconstructs the
/// exact L(1) / L(BitDepth) / L(2) / L(paletteBits) bit stream that,
/// when fed back through [`crate::cdf::read_palette_entries_uv`],
/// reproduces the same sorted U entries.
///
/// **U paletteBits derivation.** Mirrors the Y picker but with the
/// U-specific delta formula: `delta_raw = cur - prev` (no `- 1`) and
/// `range = maxVal - cur` (no `- 1`). The maximum raw delta is bounded
/// by `2^BitDepth - 1`, which fits in `BitDepth` bits — `minBits + 3
/// = BitDepth - 3 + 3 = BitDepth`. The §5.11.46 refinement only reduces
/// `paletteBits` along the way (since `range = maxVal - cur <= maxVal`).
///
/// **V-plane arm selection.** The `delta_encode_v` parameter chooses
/// the arm:
/// * `false` (direct-literal arm): each `palette_colors_v[idx]` emits as
///   `L(BitDepth)`. No monotonicity or sort requirement.
/// * `true` (signed-delta arm): the writer picks the smallest
///   `palette_num_extra_bits_v ∈ {0, 1, 2, 3}` such that every
///   `palette_delta_v` magnitude (`cur - prev`, allowing the §5.11.46
///   modular wrap to pick the smaller-magnitude alternative
///   `cur - prev ± maxVal`) fits in `paletteBits`. The wrap means the
///   writer can always find a representable signed delta with
///   `|delta| <= maxVal / 2`, which fits in `BitDepth - 1` bits — well
///   within the `minBits + 3 = BitDepth - 1` ceiling.
///
/// **Caller contract.** Inputs must satisfy:
/// * `bit_depth ∈ {8, 10, 12}` (§5.5.2).
/// * `palette_size_uv ∈ 2..=PALETTE_COLORS` (§5.11.46 contract).
/// * `palette_colors_u[0 .. palette_size_uv]` non-strictly ascending,
///   each entry in `[0, 2^bit_depth)`. Descending entries surface
///   [`Error::PartitionWalkOutOfRange`] (the U delta arm only encodes
///   non-negative `cur - prev`).
/// * `palette_colors_v[0 .. palette_size_uv]` arbitrary entries in
///   `[0, 2^bit_depth)`. Not required to be sorted (the §5.11.46 V arm
///   does not sort after decode).
/// * `walker` MUST be at the same §5.11.49 cache state the reader
///   would observe (caller threads its own [`PartitionWalker`] and
///   stamps any preceding decoded blocks into the `PaletteSizes` /
///   `PaletteColors` grids before this call).
///
/// On success, the encoder has committed the §5.11.46 UV palette
/// entries to the bit stream in exact lock-step with the
/// [`crate::cdf::read_palette_entries_uv`] reader.
#[allow(clippy::too_many_arguments)]
pub fn write_palette_entries_uv(
    writer: &mut SymbolWriter,
    bit_depth: u8,
    palette_size_uv: usize,
    palette_colors_u: &[u16],
    palette_colors_v: &[u16],
    delta_encode_v: bool,
    walker: &PartitionWalker,
    mi_row: u32,
    mi_col: u32,
) -> Result<(), Error> {
    if !matches!(bit_depth, 8 | 10 | 12) {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if !(2..=PALETTE_COLORS).contains(&palette_size_uv) {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if palette_colors_u.len() < palette_size_uv {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if palette_colors_v.len() < palette_size_uv {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let bd = bit_depth as u32;
    let max_val: u32 = 1u32 << bd;
    // §5.11.46 caller contract: U entries non-strictly ascending and
    // each within [0, 2^BitDepth). Mirrors the reader's invariant after
    // the trailing sort + Clip1. Duplicates are admitted because the U
    // delta loop has no `++` step (raw delta == 0 is legal).
    for (i, &c) in palette_colors_u.iter().take(palette_size_uv).enumerate() {
        if (c as u32) >= max_val {
            return Err(Error::PartitionWalkOutOfRange);
        }
        if i > 0 && (c as u32) < palette_colors_u[i - 1] as u32 {
            return Err(Error::PartitionWalkOutOfRange);
        }
    }
    // §5.11.46 caller contract: V entries each within [0, 2^BitDepth).
    // No monotonicity requirement (the §5.11.46 V arm does not sort).
    for &c in palette_colors_v.iter().take(palette_size_uv) {
        if (c as u32) >= max_val {
            return Err(Error::PartitionWalkOutOfRange);
        }
    }

    // -------- U plane --------
    // §5.11.49 cache via the same walker accessor the reader uses
    // (plane = 1 ⇒ U).
    let mut cache_buf = [0u16; 2 * PALETTE_COLORS];
    let cache_n = walker.get_palette_cache(1, mi_row, mi_col, &mut cache_buf);

    // §5.11.46 cache-coded indices loop for U. Same decision-tree as Y:
    // "is this cache slot the next palette entry?" → L(1).
    let mut idx: usize = 0;
    {
        let mut i = 0usize;
        while i < cache_n && idx < palette_size_uv {
            let use_cache = if cache_buf[i] == palette_colors_u[idx] {
                1u32
            } else {
                0u32
            };
            writer.write_literal(1, use_cache)?;
            if use_cache != 0 {
                idx += 1;
            }
            i += 1;
        }
    }

    // §5.11.46 first "new" U entry as L(BitDepth) literal.
    if idx < palette_size_uv {
        let v = palette_colors_u[idx] as u32;
        writer.write_literal(bd, v)?;
        idx += 1;
    }

    // §5.11.46 paletteBits derivation for U. Same `minBits = BitDepth -
    // 3` as Y; the L(2) read is skipped when no entries remain.
    if idx < palette_size_uv {
        let min_bits = bd - 3;
        let start_idx = idx;
        let extra = pick_palette_extra_bits_u(palette_colors_u, palette_size_uv, start_idx, bd)?;
        writer.write_literal(2, extra)?;
        let mut palette_bits = min_bits + extra;
        // §5.11.46 U delta loop: no `++`, range without `- 1`.
        while idx < palette_size_uv {
            let prev = palette_colors_u[idx - 1] as u32;
            let cur = palette_colors_u[idx] as u32;
            // Non-strict ascending invariant ⇒ cur >= prev.
            let delta_raw = cur - prev;
            if palette_bits == 0 {
                if delta_raw != 0 {
                    return Err(Error::PartitionWalkOutOfRange);
                }
                writer.write_literal(0, 0)?;
            } else {
                if delta_raw >= (1u32 << palette_bits) {
                    return Err(Error::PartitionWalkOutOfRange);
                }
                writer.write_literal(palette_bits, delta_raw)?;
            }
            // §5.11.46 paletteBits refinement: `range = (1 << BitDepth)
            // - palette_colors_u[idx]` (no `- 1`).
            let range = max_val.saturating_sub(cur);
            palette_bits = palette_bits.min(ceil_log2_av1(range));
            idx += 1;
        }
    }

    // -------- V plane --------
    // §5.11.46 V arm selector — L(1) `delta_encode_palette_colors_v`.
    writer.write_literal(1, if delta_encode_v { 1 } else { 0 })?;
    if delta_encode_v {
        // §5.11.46 V delta-encoded arm. `minBits = BitDepth - 4` ⇒
        // `minBits ∈ {4, 6, 8}` for `BitDepth ∈ {8, 10, 12}`.
        let min_bits_v = bd - 4;
        let extra_v = pick_palette_extra_bits_v(palette_colors_v, palette_size_uv, bd)?;
        writer.write_literal(2, extra_v)?;
        let palette_bits_v = min_bits_v + extra_v;
        // First V entry as L(BitDepth) literal.
        let first = palette_colors_v[0] as u32;
        writer.write_literal(bd, first)?;
        // §5.11.46 V delta loop: for each remaining entry, pick the
        // shorter-magnitude signed delta from the two §5.11.46
        // modular-wrap candidates `cur - prev` and `cur - prev ± maxVal`.
        for i in 1..palette_size_uv {
            let prev = palette_colors_v[i - 1] as i32;
            let cur = palette_colors_v[i] as i32;
            let signed_delta = pick_v_signed_delta(prev, cur, max_val as i32, palette_bits_v)?;
            // §5.11.46 emit: `L(paletteBits) palette_delta_v` (magnitude),
            // then on non-zero `L(1) palette_delta_sign_bit_v`.
            let magnitude = signed_delta.unsigned_abs();
            if palette_bits_v == 0 {
                if magnitude != 0 {
                    return Err(Error::PartitionWalkOutOfRange);
                }
                writer.write_literal(0, 0)?;
            } else {
                if magnitude >= (1u32 << palette_bits_v) {
                    return Err(Error::PartitionWalkOutOfRange);
                }
                writer.write_literal(palette_bits_v, magnitude)?;
            }
            if magnitude != 0 {
                let sign_bit = if signed_delta < 0 { 1u32 } else { 0u32 };
                writer.write_literal(1, sign_bit)?;
            }
        }
    } else {
        // §5.11.46 V direct-literal arm: each entry as L(BitDepth).
        for &c in palette_colors_v.iter().take(palette_size_uv) {
            writer.write_literal(bd, c as u32)?;
        }
    }

    Ok(())
}

/// §5.11.49 `palette_tokens( )` per-plane writer — r282. The exact
/// encode-side twin of [`crate::cdf::palette_tokens_plane`]: emits the
/// `color_index_map_{y,uv} NS(PaletteSize)` literal followed by the
/// anti-diagonal `palette_color_idx_{y,uv}` `S()` walk over the
/// on-screen rectangle, deriving the §5.11.50 colour context per
/// position from the same incrementally-reconstructed `ColorMap` the
/// reader maintains.
///
/// Parameters mirror the reader's, with `color_map` flipped from an
/// output to an input:
///
/// * `plane` — see [`PalettePlane`]. Selects the
///   [`TileCdfContext::palette_y_color_cdf`] /
///   [`TileCdfContext::palette_uv_color_cdf`] family.
/// * `palette_size` — `PaletteSize{Y,UV}` in `2..=PALETTE_COLORS`.
/// * `block_width` / `block_height` / `onscreen_width` /
///   `onscreen_height` — the §5.11.49 dimensions
///   ([`crate::cdf::palette_tokens_args`] computes them, including
///   the UV subsampling + `<4`-bump adjustment).
/// * `color_map` — the target `ColorMap{Y,UV}` palette-index plane,
///   row-major with row stride `stride`. Only the on-screen rectangle
///   is read (`0..onscreen_height` × `0..onscreen_width`); every read
///   sample MUST be `< palette_size` (the reader can only reconstruct
///   indices the `ColorOrder` permutation reaches).
/// * `stride` — row stride of `color_map`; `>= block_width`.
///
/// Internally the writer rebuilds the reader's working map sample by
/// sample (each §5.11.50 context reads only already-walked
/// neighbours), so the emitted symbol sequence — and the §8.3 CDF
/// adaptation it drives — is identical to what
/// [`crate::cdf::palette_tokens_plane`] consumes. The reader's
/// §5.11.49 border-fill writes are not mirrored: they happen after
/// every context-relevant sample has been walked and are therefore
/// unobservable on the bit stream.
///
/// Errors mirror the reader: [`crate::Error::InvalidPaletteWalkArgs`]
/// for caller bugs (`palette_size` out of `2..=PALETTE_COLORS`,
/// zero / oversize on-screen dims, `stride < block_width`,
/// `color_map` shorter than `block_height * stride`, an on-screen
/// sample `>= palette_size`), and
/// [`crate::Error::PaletteColorContextUnmapped`] when the §5.11.50
/// `ColorContextHash` lands on an unmapped `Palette_Color_Context[]`
/// entry (unreachable for realisable neighbour combinations).
#[allow(clippy::too_many_arguments)]
pub fn write_palette_tokens_plane(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    plane: PalettePlane,
    palette_size: usize,
    block_width: usize,
    block_height: usize,
    onscreen_width: usize,
    onscreen_height: usize,
    color_map: &[u8],
    stride: usize,
) -> Result<(), Error> {
    // Caller-bug guards — identical set to the reader's.
    if !(2..=PALETTE_COLORS).contains(&palette_size) {
        return Err(Error::InvalidPaletteWalkArgs);
    }
    if onscreen_width == 0 || onscreen_height == 0 {
        return Err(Error::InvalidPaletteWalkArgs);
    }
    if onscreen_width > block_width || onscreen_height > block_height {
        return Err(Error::InvalidPaletteWalkArgs);
    }
    if stride < block_width {
        return Err(Error::InvalidPaletteWalkArgs);
    }
    if color_map.len() < block_height.saturating_mul(stride) {
        return Err(Error::InvalidPaletteWalkArgs);
    }
    if (color_map[0] as usize) >= palette_size {
        return Err(Error::InvalidPaletteWalkArgs);
    }

    // §5.11.49: `color_index_map_{y,uv} NS(PaletteSize)` — the
    // top-left sample is coded as a non-symmetric literal.
    writer.write_ns(palette_size as u32, color_map[0] as u32)?;

    // Working map — the reader's `ColorMap` reconstruction, rebuilt
    // sample by sample so each §5.11.50 context derivation observes
    // exactly the state the reader's derivation observes (unwalked
    // cells hold the same `0` pre-fill the reader's buffer holds).
    let mut work = vec![0u8; block_height * stride];
    work[0] = color_map[0];

    // §5.11.49 anti-diagonal walk over the on-screen rectangle.
    for i in 1..(onscreen_height + onscreen_width - 1) {
        let j_hi = core::cmp::min(i, onscreen_width - 1);
        let j_lo = i.saturating_sub(onscreen_height - 1);
        let mut j = j_hi;
        loop {
            let r = i - j;
            let c = j;

            let desired = color_map[r * stride + c];
            if (desired as usize) >= palette_size {
                return Err(Error::InvalidPaletteWalkArgs);
            }

            // §5.11.50 colour context + `ColorOrder` permutation from
            // the working map (already-walked neighbours only).
            let pcc = get_palette_color_context(&work, stride, r, c, palette_size)
                .ok_or(Error::InvalidPaletteWalkArgs)?;
            let ctx = palette_color_ctx(pcc.color_context_hash)
                .ok_or(Error::PaletteColorContextUnmapped)?;

            // §5.11.49: the reader maps `palette_color_idx` through
            // `ColorOrder`; the writer inverts the permutation.
            let idx = pcc
                .color_order
                .iter()
                .take(palette_size)
                .position(|&v| v == desired)
                .ok_or(Error::InvalidPaletteWalkArgs)?;

            let cdf = match plane {
                PalettePlane::Y => cdfs.palette_y_color_cdf(palette_size, ctx),
                PalettePlane::Uv => cdfs.palette_uv_color_cdf(palette_size, ctx),
            }
            .ok_or(Error::InvalidPaletteWalkArgs)?;
            writer.write_symbol(idx as u32, cdf)?;
            work[r * stride + c] = desired;

            if j == j_lo {
                break;
            }
            j -= 1;
        }
    }

    Ok(())
}

/// Pick the smallest `palette_num_extra_bits_u` (L(2) ⇒ 0..=3) such
/// that every remaining §5.11.46 U-plane delta fits in the
/// reader-refined `paletteBits`. Returns
/// [`Error::PartitionWalkOutOfRange`] when no choice in 0..=3 admits
/// all deltas — this only happens on a malformed
/// `palette_colors_u` (the non-strict-ascending + bit-depth check
/// before this call rules out conformant input).
fn pick_palette_extra_bits_u(
    palette_colors_u: &[u16],
    palette_size_uv: usize,
    start_idx: usize,
    bd: u32,
) -> Result<u32, Error> {
    let min_bits = bd - 3;
    let max_val: u32 = 1u32 << bd;
    for extra in 0..=3u32 {
        let mut palette_bits = min_bits + extra;
        let mut ok = true;
        let mut idx = start_idx;
        while idx < palette_size_uv {
            let prev = palette_colors_u[idx - 1] as u32;
            let cur = palette_colors_u[idx] as u32;
            // U: no `++` ⇒ delta_raw = cur - prev.
            let delta_raw = cur - prev;
            let capacity = if palette_bits >= 32 {
                u32::MAX
            } else {
                1u32 << palette_bits
            };
            if palette_bits == 0 {
                if delta_raw != 0 {
                    ok = false;
                    break;
                }
            } else if delta_raw >= capacity {
                ok = false;
                break;
            }
            // §5.11.46 U refinement: `range = maxVal - cur` (no `- 1`).
            let range = max_val.saturating_sub(cur);
            palette_bits = palette_bits.min(ceil_log2_av1(range));
            idx += 1;
        }
        if ok {
            return Ok(extra);
        }
    }
    Err(Error::PartitionWalkOutOfRange)
}

/// Pick the smallest `palette_num_extra_bits_v` (L(2) ⇒ 0..=3) such
/// that every §5.11.46 V-plane signed delta magnitude fits in
/// `paletteBits = (BitDepth - 4) + extra`. Returns
/// [`Error::PartitionWalkOutOfRange`] when no choice admits all
/// deltas (a malformed input would be required to hit this — for any
/// `BitDepth ∈ {8, 10, 12}`, the smaller-magnitude wrap candidate is
/// bounded by `maxVal / 2 = 2^(BitDepth - 1)`, which fits in
/// `BitDepth - 1` bits, well within `minBits + 3 = BitDepth - 1`).
fn pick_palette_extra_bits_v(
    palette_colors_v: &[u16],
    palette_size_uv: usize,
    bd: u32,
) -> Result<u32, Error> {
    let min_bits = bd - 4;
    let max_val: i32 = 1i32 << bd;
    for extra in 0..=3u32 {
        let palette_bits = min_bits + extra;
        let mut ok = true;
        for i in 1..palette_size_uv {
            let prev = palette_colors_v[i - 1] as i32;
            let cur = palette_colors_v[i] as i32;
            if pick_v_signed_delta(prev, cur, max_val, palette_bits).is_err() {
                ok = false;
                break;
            }
        }
        if ok {
            return Ok(extra);
        }
    }
    Err(Error::PartitionWalkOutOfRange)
}

/// Pick the signed delta the §5.11.46 V delta-encoded arm should emit
/// for the transition `prev -> cur`. The reader applies modular wrap
/// to `prev + signed_delta` so there are up to three candidates:
/// `cur - prev`, `cur - prev + maxVal`, `cur - prev - maxVal`. We pick
/// the one with the smallest magnitude that fits in `paletteBits`
/// (magnitude < `2^paletteBits`). Ties on magnitude prefer the
/// natural (non-wrapped) candidate.
///
/// Returns [`Error::PartitionWalkOutOfRange`] if no candidate fits —
/// the picker's `extra` loop catches this and bumps `paletteBits`.
fn pick_v_signed_delta(prev: i32, cur: i32, max_val: i32, palette_bits: u32) -> Result<i32, Error> {
    let capacity: u32 = if palette_bits == 0 {
        // L(0) only encodes magnitude 0.
        1
    } else if palette_bits >= 32 {
        u32::MAX
    } else {
        1u32 << palette_bits
    };
    // The three §5.11.46 wrap candidates.
    let natural = cur - prev;
    let candidates = [natural, natural + max_val, natural - max_val];
    let mut best: Option<i32> = None;
    for &cand in &candidates {
        // Reader's L(paletteBits) read returns `raw ∈ [0, 2^paletteBits)`,
        // then the optional sign bit can negate it. So the encodable
        // signed range is `[-(2^paletteBits - 1), 2^paletteBits - 1]`
        // (magnitude < 2^paletteBits).
        let mag = cand.unsigned_abs();
        if mag >= capacity {
            continue;
        }
        // Verify the §5.11.46 reader's modular wrap on this candidate
        // actually reconstructs `cur` from `prev`: `prev + cand` then
        // wrapped into `[0, maxVal)` must equal `cur`.
        let mut val = prev + cand;
        if val < 0 {
            val += max_val;
        }
        if val >= max_val {
            val -= max_val;
        }
        if val != cur {
            continue;
        }
        match best {
            None => best = Some(cand),
            Some(b) => {
                if cand.unsigned_abs() < b.unsigned_abs() {
                    best = Some(cand);
                }
            }
        }
    }
    best.ok_or(Error::PartitionWalkOutOfRange)
}

/// `intra_block_mode_info( )` dispatcher per §5.11.22 (av1-spec p.72)
/// — r261. Composes the §5.11.22 leaf writers into the full §5.11.22
/// body for the **no-palette** + **no-CFL** + **DC_PRED** path,
/// matching the shape the §5.11.18 `intra_block_mode_info( )` call
/// site expects.
///
/// Scope of this arc (r261):
/// * `y_mode` (§5.11.22 line 2) via [`write_y_mode`].
/// * `intra_angle_info_y` (§5.11.22 line 4) via
///   [`write_intra_angle_info_y`] — non-directional `y_mode` ⇒
///   no-bits short-circuit.
/// * `uv_mode` (§5.11.22 line 6) via [`write_intra_uv_mode`] when
///   `has_chroma`.
/// * `intra_angle_info_uv` (§5.11.22 line 10) via
///   [`write_intra_angle_info_uv`] when `has_chroma`.
/// * `palette_mode_info` (§5.11.22 line 13-15) via
///   [`write_palette_mode_info`] — restricted to `has_palette_y == 0
///   && has_palette_uv == 0` per the leaf's contract.
/// * `filter_intra_mode_info` (§5.11.22 line 16) via
///   [`write_filter_intra_mode_info`].
///
/// **NOT covered by this arc** (caller must pass values consistent
/// with the no-palette / no-CFL path; non-trivial values surface
/// [`Error::PaletteEntriesUnsupported`] / [`Error::PartitionWalkOutOfRange`]):
/// * `read_cfl_alphas` (§5.11.22 line 8 when `UVMode == UV_CFL_PRED
///   = 13`). Callers compose [`write_cfl_alphas`] manually for the
///   CFL arm.
/// * Non-zero `has_palette_*` (§5.11.46 palette-entries syntax).
///
/// Inputs:
/// * `mi_size` — `MiSize` ordinal in `0..BLOCK_SIZES`.
/// * `y_mode` — the §5.11.22 `YMode` value in `0..INTRA_MODES`.
/// * `uv_mode` — `Some(mode)` when `has_chroma`, `None` otherwise.
///   `mode` MUST be in `0..UV_INTRA_MODES_CFL_ALLOWED` and MUST NOT
///   equal `UV_CFL_PRED = 13` on this arc (the CFL arm is owned by
///   the caller; passing `UV_CFL_PRED` is a contract violation
///   surfaced as [`Error::PartitionWalkOutOfRange`]).
/// * `angle_delta_y` / `angle_delta_uv` — signed deltas in
///   `-MAX_ANGLE_DELTA..=MAX_ANGLE_DELTA`. MUST be `0` when the
///   §5.11.42 / §5.11.43 short-circuit fires (`mi_size < BLOCK_8X8`
///   OR `!is_directional(mode)`).
/// * `cfl_allowed` — caller-derived §8.3.2 CFL-allowance flag
///   (e.g. via [`crate::cdf::cfl_allowed_for_uv_mode`]).
/// * `has_chroma` — §5.11.5 `HasChroma`.
/// * `allow_screen_content_tools` — §5.9.5 sequence-header bit.
/// * `enable_filter_intra` — §5.5.2 sequence-header bit.
/// * `use_filter_intra` — `0` / `1`; gated by the §5.11.24 outer
///   gate inside [`write_filter_intra_mode_info`].
/// * `filter_intra_mode` — `Some(mode)` when `use_filter_intra ==
///   1`, `None` otherwise.
/// * `above_palette_y` / `left_palette_y` — §8.3.2 neighbour-palette
///   booleans fed to [`palette_y_mode_ctx`].
///
/// On success, the encoder has committed the full §5.11.22
/// no-palette / no-CFL body to the bit stream + advanced the §8.3
/// adaptation state in lockstep with the
/// [`crate::cdf::PartitionWalker::decode_intra_block_mode_info`]
/// reader.
#[allow(clippy::too_many_arguments)]
pub fn write_intra_block_mode_info(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    mi_size: usize,
    y_mode: u8,
    uv_mode: Option<u8>,
    angle_delta_y: i8,
    angle_delta_uv: i8,
    cfl_allowed: bool,
    has_chroma: bool,
    allow_screen_content_tools: bool,
    enable_filter_intra: bool,
    use_filter_intra: u8,
    filter_intra_mode: Option<u8>,
    above_palette_y: bool,
    left_palette_y: bool,
) -> Result<(), Error> {
    // §5.11.22 caller-bug guards re-asserted here so a contract
    // violation surfaces before any partial S() write disturbs the
    // bit stream. Each leaf re-checks its own bounds.
    if mi_size >= BLOCK_SIZES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if (y_mode as usize) >= INTRA_MODES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if has_chroma {
        let uv = uv_mode.ok_or(Error::PartitionWalkOutOfRange)?;
        if (uv as usize) >= UV_INTRA_MODES_CFL_ALLOWED {
            return Err(Error::PartitionWalkOutOfRange);
        }
        // r261 arc does NOT compose `write_cfl_alphas`; the CFL arm
        // is reserved for a future round. UV_CFL_PRED = 13 per §3.
        if (uv as usize) == 13 {
            return Err(Error::PartitionWalkOutOfRange);
        }
    } else if uv_mode.is_some() {
        // §5.11.22 line 5 `if (HasChroma)` guard: when `has_chroma ==
        // false` the reader never touches `uv_mode`; passing a `Some`
        // value is a caller bug.
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.22 line 2: `y_mode S()` via the §8.3.2 `Size_Group[ MiSize ]`
    // ctx. The leaf takes the derived ctx; do the derivation here so
    // the dispatcher matches the §5.11.22 reader's `y_mode_ctx =
    // size_group(sub_size)` step verbatim.
    let size_group_ctx = crate::cdf::size_group(mi_size);
    write_y_mode(writer, cdfs, y_mode, size_group_ctx)?;

    // §5.11.22 line 4: `intra_angle_info_y( )` per §5.11.42.
    write_intra_angle_info_y(writer, cdfs, angle_delta_y, mi_size, y_mode)?;

    // §5.11.22 lines 5-10: `if ( HasChroma )` arm.
    if has_chroma {
        let uv = uv_mode.expect("validated above");
        // §5.11.22 line 6.
        write_intra_uv_mode(writer, cdfs, y_mode, uv, cfl_allowed)?;
        // §5.11.22 line 10.
        write_intra_angle_info_uv(writer, cdfs, angle_delta_uv, mi_size, uv)?;
    } else {
        // §5.11.22 lines 5-10 skipped entirely. Caller-bug guards:
        // both UV deltas MUST be zero (no bits would be reconstructed
        // on the monochrome arm).
        if angle_delta_uv != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
    }

    // §5.11.22 lines 13-15: `palette_mode_info( )` per §5.11.46 — the
    // r261 leaf restricts to has_palette_y == 0 / has_palette_uv == 0.
    write_palette_mode_info(
        writer,
        cdfs,
        /* has_palette_y = */ 0,
        /* has_palette_uv = */ 0,
        mi_size,
        y_mode,
        uv_mode,
        has_chroma,
        allow_screen_content_tools,
        above_palette_y,
        left_palette_y,
    )?;

    // §5.11.22 line 16: `filter_intra_mode_info( )` per §5.11.24. The
    // §5.11.46 leaf above committed `palette_size_y == 0`, so the
    // §5.11.24 outer gate's `PaletteSizeY == 0` clause is satisfied
    // mechanically on this arc.
    write_filter_intra_mode_info(
        writer,
        cdfs,
        use_filter_intra,
        filter_intra_mode,
        mi_size,
        y_mode,
        /* palette_size_y = */ 0,
        enable_filter_intra,
    )?;

    Ok(())
}

/// `palette_mode_info( )` writer per §5.11.46 (av1-spec p.97-98) —
/// r264. Superset of [`write_palette_mode_info`]: lifts the
/// no-palette precondition by threading the `palette_size_y_minus_2`
/// / `palette_size_uv_minus_2` S() symbols + the §5.11.46
/// palette-entries writers ([`write_palette_entries_y`] /
/// [`write_palette_entries_uv`]) into a single leaf.
///
/// Spec body (lifted to cover both arms):
/// ```text
///   if ( outer_gate ) {
///       bsizeCtx = Mi_Width_Log2[ MiSize ] + Mi_Height_Log2[ MiSize ] - 2
///       if ( YMode == DC_PRED ) {
///           has_palette_y                                 S()
///           if ( has_palette_y ) {
///               palette_size_y_minus_2                    S()
///               palette_colors_y[ 0 .. PaletteSizeY ]     (§5.11.46)
///           }
///       }
///       if ( HasChroma && UVMode == DC_PRED ) {
///           has_palette_uv                                S()
///           if ( has_palette_uv ) {
///               palette_size_uv_minus_2                   S()
///               palette_colors_u[ 0 .. PaletteSizeUV ]    (§5.11.46)
///               palette_colors_v[ 0 .. PaletteSizeUV ]    (§5.11.46)
///           }
///       }
///   }
/// ```
///
/// Mirrors the reader path inlined at the §5.11.46 sub-block of
/// [`crate::cdf::PartitionWalker::decode_intra_block_mode_info`] +
/// [`crate::cdf::read_palette_entries_y`] +
/// [`crate::cdf::read_palette_entries_uv`].
///
/// Inputs:
/// * `has_palette_y` — `0` or `1`; `1` triggers the
///   `palette_size_y_minus_2` S() + [`write_palette_entries_y`] call.
/// * `has_palette_uv` — `0` or `1`; `1` triggers the
///   `palette_size_uv_minus_2` S() + [`write_palette_entries_uv`]
///   call.
/// * `mi_size` — `MiSize` ordinal in `0..BLOCK_SIZES`.
/// * `y_mode` — the §5.11.22 `YMode` value; the luma arm fires only
///   on `YMode == DC_PRED = 0`.
/// * `uv_mode` — `Some(mode)` when `has_chroma`, `None` otherwise.
///   The chroma arm fires only on `UVMode == DC_PRED = 0`.
/// * `has_chroma` — §5.11.5 `HasChroma`.
/// * `allow_screen_content_tools` — §5.9.5 sequence-header bit that
///   gates the §5.11.46 outer arm.
/// * `above_palette_y` / `left_palette_y` — §8.3.2 neighbour-palette
///   booleans fed to [`palette_y_mode_ctx`].
/// * `bit_depth` — §6.7.2 `BitDepth` (8 / 10 / 12). Consumed only on
///   the entries-bearing arms (passed straight through to the
///   palette-entries writers).
/// * `palette_size_y` — `PaletteSizeY` in `2..=PALETTE_COLORS` when
///   `has_palette_y == 1`; ignored otherwise.
/// * `palette_colors_y` — sorted-ascending palette entries for the Y
///   plane (see [`write_palette_entries_y`] contract). Ignored when
///   `has_palette_y == 0`.
/// * `palette_size_uv` — `PaletteSizeUV` in `2..=PALETTE_COLORS` when
///   `has_palette_uv == 1`; ignored otherwise.
/// * `palette_colors_u` / `palette_colors_v` — see
///   [`write_palette_entries_uv`] contract. Ignored when
///   `has_palette_uv == 0`.
/// * `delta_encode_v` — §5.11.46 V-plane arm selector. Ignored when
///   `has_palette_uv == 0`.
/// * `walker` — the same [`PartitionWalker`] the dispatcher walks for
///   §5.11.49 `get_palette_cache` neighbour reads.
/// * `mi_row` / `mi_col` — the §5.11.49 block-origin coordinates fed
///   to the palette-cache accessor.
///
/// Caller-bug guards (in addition to the per-leaf re-checks):
/// * `palette_size_y` / `palette_size_uv` ∈ `2..=PALETTE_COLORS`
///   when the corresponding `has_palette_*` is `1`.
/// * `palette_size_y` / `palette_size_uv` ignored when `has_palette_*
///   == 0` (no entries syntax fires).
/// * `has_palette_y == 1` requires `YMode == DC_PRED`; reader never
///   reads `has_palette_y` otherwise.
/// * `has_palette_uv == 1` requires `has_chroma && uv_mode ==
///   Some(DC_PRED)`; reader never reads `has_palette_uv` otherwise.
/// * `has_palette_y == 1` or `has_palette_uv == 1` requires the
///   §5.11.46 outer gate (`allow_screen_content_tools && MiSize >=
///   BLOCK_8X8 && Block_W <= 64 && Block_H <= 64`).
///
/// On success, the encoder has committed the full §5.11.46
/// palette-mode-info body to the bit stream + advanced the §8.3 CDF
/// adaptation state in lockstep with the reader.
#[allow(clippy::too_many_arguments)]
pub fn write_palette_mode_info_with_entries(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    has_palette_y: u8,
    has_palette_uv: u8,
    mi_size: usize,
    y_mode: u8,
    uv_mode: Option<u8>,
    has_chroma: bool,
    allow_screen_content_tools: bool,
    above_palette_y: bool,
    left_palette_y: bool,
    bit_depth: u8,
    palette_size_y: usize,
    palette_colors_y: &[u16],
    palette_size_uv: usize,
    palette_colors_u: &[u16],
    palette_colors_v: &[u16],
    delta_encode_v: bool,
    walker: &PartitionWalker,
    mi_row: u32,
    mi_col: u32,
) -> Result<(), Error> {
    // §5.11.22 / §5.11.46 caller-bug guards. Re-asserted up-front so a
    // contract violation surfaces before any partial S() write
    // disturbs the bit stream. Each leaf re-checks its own bounds.
    if mi_size >= BLOCK_SIZES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if (y_mode as usize) >= INTRA_MODES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if let Some(uv) = uv_mode {
        if (uv as usize) >= UV_INTRA_MODES_CFL_ALLOWED {
            return Err(Error::PartitionWalkOutOfRange);
        }
    }
    if has_palette_y > 1 || has_palette_uv > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.46 outer gate. Mirrors the reader's `palette_outer_gate`.
    let bw = block_width(mi_size) as u32;
    let bh = block_height(mi_size) as u32;
    let outer_gate = allow_screen_content_tools && mi_size >= BLOCK_8X8 && bw <= 64 && bh <= 64;

    if !outer_gate {
        // Gate-off path: the §5.11.46 spec body sets `PaletteSizeY =
        // 0` / `PaletteSizeUV = 0` unconditionally. The reader
        // reconstructs them as 0 regardless of the bit stream; any
        // non-zero `has_palette_*` is a caller bug because the
        // reader never reads the symbol on this arm.
        if has_palette_y != 0 || has_palette_uv != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // §5.11.46: `bsizeCtx = Mi_Width_Log2[ MiSize ] +
    // Mi_Height_Log2[ MiSize ] - 2`. The §9.3 mapping guarantees
    // `bsizeCtx ∈ 0..PALETTE_BLOCK_SIZE_CONTEXTS = 7` for any MiSize
    // that passed the outer gate (BLOCK_8X8 has (1, 1) ⇒ bsizeCtx = 0,
    // BLOCK_64X64 has (4, 4) ⇒ bsizeCtx = 6).
    let bsize_ctx = mi_width_log2(mi_size) + mi_height_log2(mi_size) - 2;
    debug_assert!(
        bsize_ctx < PALETTE_BLOCK_SIZE_CONTEXTS,
        "§5.11.46 bsizeCtx must be in 0..PALETTE_BLOCK_SIZE_CONTEXTS"
    );

    // §5.11.46 luma arm: `if ( YMode == DC_PRED )`. DC_PRED = 0.
    if y_mode == 0 {
        let ctx_y = palette_y_mode_ctx(above_palette_y, left_palette_y);
        let row = cdfs.palette_y_mode_cdf(bsize_ctx, ctx_y);
        writer.write_symbol(has_palette_y as u32, row)?;
        if has_palette_y == 1 {
            // §5.11.46 caller-bug: when the luma arm fires, the
            // §5.11.46 contract pins `PaletteSizeY ∈ 2..=PALETTE_COLORS`.
            if !(2..=PALETTE_COLORS).contains(&palette_size_y) {
                return Err(Error::PartitionWalkOutOfRange);
            }
            // §5.11.46: `palette_size_y_minus_2 S()` against
            // `TilePaletteYSizeCdf[ bsizeCtx ]`. Encoded value in
            // `0..PALETTE_SIZES = 7`; PaletteSizeY = encoded + 2 is in
            // `2..=PALETTE_COLORS = 8`.
            let row_sz = cdfs.palette_y_size_cdf(bsize_ctx);
            let minus_2 = (palette_size_y - 2) as u32;
            writer.write_symbol(minus_2, row_sz)?;
            // §5.11.46 palette_colors_y[]: cache merge + cache-coded
            // indices + first-new L(BitDepth) + delta loop. Delegates
            // to the r262 leaf, threading the same walker the reader
            // walks for §5.11.49 `get_palette_cache(0, …)`.
            write_palette_entries_y(
                writer,
                bit_depth,
                palette_size_y,
                palette_colors_y,
                walker,
                mi_row,
                mi_col,
            )?;
        }
    } else if has_palette_y != 0 {
        // §5.11.46 contract: when `y_mode != DC_PRED` the reader
        // never reads `has_palette_y`; non-zero here is a caller bug.
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.46 chroma arm: `if ( HasChroma && UVMode == DC_PRED )`.
    if has_chroma && uv_mode == Some(0) {
        // §8.3.2 chroma ctx is `palette_uv_mode_ctx(PaletteSizeY)`. On
        // the luma-arm-not-fired or `has_palette_y == 0` arm the spec
        // sets `PaletteSizeY = 0` (the reader's `palette_size_y` local
        // stays at its 0 initialiser); on the `has_palette_y == 1`
        // arm `PaletteSizeY` is the value just committed.
        let ps_y = if y_mode == 0 && has_palette_y == 1 {
            palette_size_y
        } else {
            0
        };
        let ctx_uv = palette_uv_mode_ctx(ps_y);
        let row = cdfs.palette_uv_mode_cdf(ctx_uv);
        writer.write_symbol(has_palette_uv as u32, row)?;
        if has_palette_uv == 1 {
            if !(2..=PALETTE_COLORS).contains(&palette_size_uv) {
                return Err(Error::PartitionWalkOutOfRange);
            }
            // §5.11.46: `palette_size_uv_minus_2 S()` against
            // `TilePaletteUVSizeCdf[ bsizeCtx ]`.
            let row_sz = cdfs.palette_uv_size_cdf(bsize_ctx);
            let minus_2 = (palette_size_uv - 2) as u32;
            writer.write_symbol(minus_2, row_sz)?;
            // §5.11.46 palette_colors_u[] + palette_colors_v[] writer.
            write_palette_entries_uv(
                writer,
                bit_depth,
                palette_size_uv,
                palette_colors_u,
                palette_colors_v,
                delta_encode_v,
                walker,
                mi_row,
                mi_col,
            )?;
        }
    } else if has_palette_uv != 0 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    Ok(())
}

/// `intra_block_mode_info( )` dispatcher per §5.11.22 (av1-spec p.72)
/// — r265. Superset of [`write_intra_block_mode_info`] and r264's
/// no-CFL variant: composes [`write_palette_mode_info_with_entries`]
/// for the §5.11.46 palette-entries arm AND composes
/// [`write_cfl_alphas`] for the §5.11.22 line-8 `UVMode ==
/// UV_CFL_PRED` arm.
///
/// Scope of this arc (r265): identical to r264 except the §5.11.22
/// line-8 `if ( UVMode == UV_CFL_PRED ) read_cfl_alphas( )` clause
/// is now folded in. When `has_chroma && uv == UV_CFL_PRED = 13` the
/// dispatcher requires `cfl_alpha_u` / `cfl_alpha_v` to be `Some`
/// and threads them through [`write_cfl_alphas`] between
/// [`write_intra_uv_mode`] and [`write_intra_angle_info_uv`]. The
/// §5.11.24 `filter_intra_mode_info` outer gate's `PaletteSizeY ==
/// 0` clause is still enforced (the gate closes mechanically when
/// `has_palette_y == 1`).
///
/// Inputs: identical to [`write_intra_block_mode_info`] plus the
/// §5.11.46 palette-entries parameters surfaced by
/// [`write_palette_mode_info_with_entries`] plus the §5.11.45 CFL
/// alphas `cfl_alpha_u` / `cfl_alpha_v` — both `None` on the non-CFL
/// chroma arm, both `Some` (with at least one non-zero per §6.10.36
/// joint-sign constraint) on the CFL arm.
///
/// On success, the encoder has committed the full §5.11.22 body —
/// including any §5.11.46 palette-entries syntax and any §5.11.45
/// CFL syntax — to the bit stream + advanced the §8.3 CDF adaptation
/// state in lockstep with
/// [`crate::cdf::PartitionWalker::decode_intra_block_mode_info`].
#[allow(clippy::too_many_arguments)]
pub fn write_intra_block_mode_info_with_palette(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    mi_size: usize,
    y_mode: u8,
    uv_mode: Option<u8>,
    angle_delta_y: i8,
    angle_delta_uv: i8,
    cfl_allowed: bool,
    has_chroma: bool,
    allow_screen_content_tools: bool,
    enable_filter_intra: bool,
    use_filter_intra: u8,
    filter_intra_mode: Option<u8>,
    above_palette_y: bool,
    left_palette_y: bool,
    bit_depth: u8,
    has_palette_y: u8,
    palette_size_y: usize,
    palette_colors_y: &[u16],
    has_palette_uv: u8,
    palette_size_uv: usize,
    palette_colors_u: &[u16],
    palette_colors_v: &[u16],
    delta_encode_v: bool,
    cfl_alpha_u: Option<i8>,
    cfl_alpha_v: Option<i8>,
    walker: &PartitionWalker,
    mi_row: u32,
    mi_col: u32,
) -> Result<(), Error> {
    // §5.11.22 caller-bug guards re-asserted here so a contract
    // violation surfaces before any partial S() write disturbs the
    // bit stream. Each leaf re-checks its own bounds.
    check_intra_mode_info_inputs(
        mi_size,
        y_mode,
        uv_mode,
        cfl_allowed,
        has_chroma,
        cfl_alpha_u,
        cfl_alpha_v,
    )?;

    // §5.11.22 line 2: `y_mode S()` via the §8.3.2 `Size_Group[ MiSize ]`
    // ctx.
    let size_group_ctx = crate::cdf::size_group(mi_size);
    write_y_mode(writer, cdfs, y_mode, size_group_ctx)?;

    // §5.11.22 lines 4-16: shared with the §5.11.7 `else` arm.
    write_intra_mode_info_tail(
        writer,
        cdfs,
        mi_size,
        y_mode,
        uv_mode,
        angle_delta_y,
        angle_delta_uv,
        cfl_allowed,
        has_chroma,
        allow_screen_content_tools,
        enable_filter_intra,
        use_filter_intra,
        filter_intra_mode,
        above_palette_y,
        left_palette_y,
        bit_depth,
        has_palette_y,
        palette_size_y,
        palette_colors_y,
        has_palette_uv,
        palette_size_uv,
        palette_colors_u,
        palette_colors_v,
        delta_encode_v,
        cfl_alpha_u,
        cfl_alpha_v,
        walker,
        mi_row,
        mi_col,
    )
}

/// Caller-bug guard block shared by
/// [`write_intra_block_mode_info_with_palette`] (§5.11.22) and
/// [`write_intra_frame_else_arm`] (§5.11.7 `else` arm) — both bodies
/// share the same `uv_mode` / CFL / chroma input contract, so the
/// guards are asserted once at the dispatcher boundary, BEFORE any
/// S() write touches the bit stream.
fn check_intra_mode_info_inputs(
    mi_size: usize,
    y_mode: u8,
    uv_mode: Option<u8>,
    cfl_allowed: bool,
    has_chroma: bool,
    cfl_alpha_u: Option<i8>,
    cfl_alpha_v: Option<i8>,
) -> Result<(), Error> {
    if mi_size >= BLOCK_SIZES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if (y_mode as usize) >= INTRA_MODES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if has_chroma {
        let uv = uv_mode.ok_or(Error::PartitionWalkOutOfRange)?;
        if (uv as usize) >= UV_INTRA_MODES_CFL_ALLOWED {
            return Err(Error::PartitionWalkOutOfRange);
        }
        // §5.11.22 line 8: `UV_CFL_PRED = 13` per §3. On the
        // non-CFL chroma arm the caller MUST pass both alphas as
        // `None`; on the CFL arm both MUST be `Some`. The further
        // §5.11.45 magnitude / joint-sign guards live inside
        // [`write_cfl_alphas`] itself.
        if (uv as usize) == 13 {
            if cfl_alpha_u.is_none() || cfl_alpha_v.is_none() {
                return Err(Error::PartitionWalkOutOfRange);
            }
            // §5.11.22 line 6 / §8.3.2: the CFL-allowed UV CDF row
            // is the only one that carries `UV_CFL_PRED` (width 14);
            // the CFL-not-allowed row is width 13 and excludes it.
            // Rejecting `uv == UV_CFL_PRED` with `cfl_allowed ==
            // false` keeps the writer in lockstep with the reader's
            // CDF-row selection.
            if !cfl_allowed {
                return Err(Error::PartitionWalkOutOfRange);
            }
        } else if cfl_alpha_u.is_some() || cfl_alpha_v.is_some() {
            return Err(Error::PartitionWalkOutOfRange);
        }
    } else if uv_mode.is_some() {
        // §5.11.22 line 5 `if (HasChroma)` guard: when `has_chroma ==
        // false` the reader never touches `uv_mode`; passing a `Some`
        // value is a caller bug.
        return Err(Error::PartitionWalkOutOfRange);
    } else if cfl_alpha_u.is_some() || cfl_alpha_v.is_some() {
        // §5.11.22 line 5 closes the chroma arm entirely on
        // `has_chroma == false`; passing CFL alphas through is a
        // caller bug.
        return Err(Error::PartitionWalkOutOfRange);
    }
    Ok(())
}

/// Shared §5.11.22 / §5.11.7-`else`-arm tail writer — everything after
/// the per-arm Y-mode S() (`y_mode` with the `Size_Group[ MiSize ]`
/// ctx on §5.11.22, `intra_frame_y_mode` with the §8.3.2
/// neighbour-mode ctx pair on §5.11.7). Both spec bodies continue
/// identically from `intra_angle_info_y( )` onward (same syntax
/// elements, same CDF rows), so the tail is factored once and called
/// by both dispatchers. Caller guarantees
/// [`check_intra_mode_info_inputs`] has already passed.
#[allow(clippy::too_many_arguments)]
fn write_intra_mode_info_tail(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    mi_size: usize,
    y_mode: u8,
    uv_mode: Option<u8>,
    angle_delta_y: i8,
    angle_delta_uv: i8,
    cfl_allowed: bool,
    has_chroma: bool,
    allow_screen_content_tools: bool,
    enable_filter_intra: bool,
    use_filter_intra: u8,
    filter_intra_mode: Option<u8>,
    above_palette_y: bool,
    left_palette_y: bool,
    bit_depth: u8,
    has_palette_y: u8,
    palette_size_y: usize,
    palette_colors_y: &[u16],
    has_palette_uv: u8,
    palette_size_uv: usize,
    palette_colors_u: &[u16],
    palette_colors_v: &[u16],
    delta_encode_v: bool,
    cfl_alpha_u: Option<i8>,
    cfl_alpha_v: Option<i8>,
    walker: &PartitionWalker,
    mi_row: u32,
    mi_col: u32,
) -> Result<(), Error> {
    // §5.11.22 line 4: `intra_angle_info_y( )` per §5.11.42.
    write_intra_angle_info_y(writer, cdfs, angle_delta_y, mi_size, y_mode)?;

    // §5.11.22 lines 5-10: `if ( HasChroma )` arm.
    if has_chroma {
        let uv = uv_mode.expect("validated above");
        // §5.11.22 line 6.
        write_intra_uv_mode(writer, cdfs, y_mode, uv, cfl_allowed)?;
        // §5.11.22 line 8: `if ( UVMode == UV_CFL_PRED )
        // read_cfl_alphas( )` per §5.11.45. UV_CFL_PRED = 13 per §3.
        // The reader's `read_cfl_alphas` body sits between
        // `uv_mode S()` and `intra_angle_info_uv( )`; mirror that
        // ordering here so the bit positions of every following
        // symbol line up.
        if (uv as usize) == 13 {
            let alpha_u = cfl_alpha_u.expect("validated above");
            let alpha_v = cfl_alpha_v.expect("validated above");
            write_cfl_alphas(writer, cdfs, alpha_u, alpha_v)?;
        }
        // §5.11.22 line 10.
        write_intra_angle_info_uv(writer, cdfs, angle_delta_uv, mi_size, uv)?;
    } else if angle_delta_uv != 0 {
        // §5.11.22 lines 5-10 skipped entirely. Caller-bug guards:
        // both UV deltas MUST be zero (no bits would be reconstructed
        // on the monochrome arm).
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.22 lines 13-15: `palette_mode_info( )` per §5.11.46 — the
    // r264 leaf accepts non-zero `has_palette_*` and the entries
    // syntax.
    write_palette_mode_info_with_entries(
        writer,
        cdfs,
        has_palette_y,
        has_palette_uv,
        mi_size,
        y_mode,
        uv_mode,
        has_chroma,
        allow_screen_content_tools,
        above_palette_y,
        left_palette_y,
        bit_depth,
        palette_size_y,
        palette_colors_y,
        palette_size_uv,
        palette_colors_u,
        palette_colors_v,
        delta_encode_v,
        walker,
        mi_row,
        mi_col,
    )?;

    // §5.11.22 line 16: `filter_intra_mode_info( )` per §5.11.24. The
    // §5.11.24 outer gate's `PaletteSizeY == 0` clause closes
    // mechanically when `has_palette_y == 1`. Pass `PaletteSizeY` as
    // committed above so the writer matches the reader's gate.
    let palette_size_y_for_filter = if y_mode == 0 && has_palette_y == 1 {
        palette_size_y as u32
    } else {
        0
    };
    write_filter_intra_mode_info(
        writer,
        cdfs,
        use_filter_intra,
        filter_intra_mode,
        mi_size,
        y_mode,
        palette_size_y_for_filter,
        enable_filter_intra,
    )?;

    Ok(())
}

/// §5.11.7 `intra_frame_mode_info( )` `else` (non-intrabc) arm writer
/// (av1-spec p.65) — r280. Composes, in spec order, the write side of:
///
/// ```text
///   } else {
///       is_inter = 0
///       intra_frame_y_mode                                S()
///       YMode = intra_frame_y_mode
///       intra_angle_info_y( )
///       if ( HasChroma ) {
///           uv_mode                                       S()
///           UVMode = uv_mode
///           if ( UVMode == UV_CFL_PRED ) {
///               read_cfl_alphas( )
///           }
///           intra_angle_info_uv( )
///       }
///       PaletteSizeY = 0
///       PaletteSizeUV = 0
///       if ( MiSize >= BLOCK_8X8 &&
///              Block_Width[ MiSize ] <= 64 &&
///              Block_Height[ MiSize ] <= 64 &&
///              allow_screen_content_tools ) {
///           palette_mode_info( )
///       }
///       filter_intra_mode_info( )
///   }
/// ```
///
/// This is the keyframe / intra-only-frame sibling of the §5.11.22
/// [`write_intra_block_mode_info_with_palette`] dispatcher: the two
/// spec bodies are identical from `intra_angle_info_y( )` onward
/// (shared via the factored tail writer), and differ ONLY in the
/// leading Y-mode element — §5.11.7 codes `intra_frame_y_mode`
/// against `TileIntraFrameYModeCdf[ abovemode ][ leftmode ]` (the
/// §8.3.2 neighbour-mode ctx pair) where §5.11.22 codes `y_mode`
/// against `TileYModeCdf[ Size_Group[ MiSize ] ]`. The `is_inter = 0`
/// assignment is a no-bit fixed value (the caller stamps the
/// §5.11.5 `IsInters[][]` grid with `0` exactly as the reader does).
///
/// `abovemode_ctx` / `leftmode_ctx` are the §8.3.2 derivations
/// (av1-spec p.361):
///
/// ```text
///   abovemode = Intra_Mode_Context[ AvailU ? YModes[ MiRow - 1 ][ MiCol ] : DC_PRED ]
///   leftmode  = Intra_Mode_Context[ AvailL ? YModes[ MiRow ][ MiCol - 1 ] : DC_PRED ]
/// ```
///
/// computed by the caller from its own §5.11.5 `YModes[][]` grid via
/// [`crate::cdf::intra_mode_ctx`] (per the stateless-writer doctrine
/// of this module; the unavailable-neighbour identity is
/// `intra_mode_ctx(DC_PRED) = 0`). Both MUST be in
/// `0..INTRA_MODE_CONTEXTS = 5`.
///
/// Every other input maps 1:1 onto the
/// [`write_intra_block_mode_info_with_palette`] parameter of the same
/// name — including the §5.11.46 palette-entries surface and the
/// §5.11.45 CFL alphas (`None`/`None` on the non-CFL chroma arm,
/// `Some`/`Some` on the `uv_mode == UV_CFL_PRED` arm).
///
/// Mirrors
/// [`crate::cdf::PartitionWalker::decode_intra_frame_mode_info_else_arm`]
/// — on success the encoder has committed the full §5.11.7 `else`-arm
/// body to the bit stream + advanced the §8.3 CDF adaptation state in
/// lockstep with that reader. Caller-bug rejects
/// ([`Error::PartitionWalkOutOfRange`]) are the
/// [`write_intra_block_mode_info_with_palette`] set plus
/// out-of-range `abovemode_ctx` / `leftmode_ctx`.
#[allow(clippy::too_many_arguments)]
pub fn write_intra_frame_else_arm(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    mi_size: usize,
    y_mode: u8,
    uv_mode: Option<u8>,
    angle_delta_y: i8,
    angle_delta_uv: i8,
    cfl_allowed: bool,
    has_chroma: bool,
    allow_screen_content_tools: bool,
    enable_filter_intra: bool,
    use_filter_intra: u8,
    filter_intra_mode: Option<u8>,
    above_palette_y: bool,
    left_palette_y: bool,
    bit_depth: u8,
    has_palette_y: u8,
    palette_size_y: usize,
    palette_colors_y: &[u16],
    has_palette_uv: u8,
    palette_size_uv: usize,
    palette_colors_u: &[u16],
    palette_colors_v: &[u16],
    delta_encode_v: bool,
    cfl_alpha_u: Option<i8>,
    cfl_alpha_v: Option<i8>,
    walker: &PartitionWalker,
    mi_row: u32,
    mi_col: u32,
    abovemode_ctx: usize,
    leftmode_ctx: usize,
) -> Result<(), Error> {
    // §5.11.7 caller-bug guards (shared §5.11.22-shaped input
    // contract), asserted before any partial S() write disturbs the
    // bit stream. The ctx-pair bounds are re-checked inside the
    // `write_intra_frame_y_mode` leaf before its write fires, so the
    // whole guard set still precedes the first emitted symbol.
    check_intra_mode_info_inputs(
        mi_size,
        y_mode,
        uv_mode,
        cfl_allowed,
        has_chroma,
        cfl_alpha_u,
        cfl_alpha_v,
    )?;

    // §5.11.7: `intra_frame_y_mode S()` against
    // `TileIntraFrameYModeCdf[ abovemode ][ leftmode ]` per §8.3.2.
    write_intra_frame_y_mode(writer, cdfs, y_mode, abovemode_ctx, leftmode_ctx)?;

    // §5.11.7 body from `intra_angle_info_y( )` onward — identical to
    // the §5.11.22 tail element-for-element.
    write_intra_mode_info_tail(
        writer,
        cdfs,
        mi_size,
        y_mode,
        uv_mode,
        angle_delta_y,
        angle_delta_uv,
        cfl_allowed,
        has_chroma,
        allow_screen_content_tools,
        enable_filter_intra,
        use_filter_intra,
        filter_intra_mode,
        above_palette_y,
        left_palette_y,
        bit_depth,
        has_palette_y,
        palette_size_y,
        palette_colors_y,
        has_palette_uv,
        palette_size_uv,
        palette_colors_u,
        palette_colors_v,
        delta_encode_v,
        cfl_alpha_u,
        cfl_alpha_v,
        walker,
        mi_row,
        mi_col,
    )
}

/// `inter_block_mode_info( )` bootstrap per §5.11.23 (av1-spec p.74)
/// — r266. Lands the first three lines of the §5.11.23 body that emit
/// **zero S() symbols**: the `PaletteSizeY = 0` / `PaletteSizeUV = 0`
/// resets (§5.11.23 lines 2-3) and the §5.11.25 `read_ref_frames( )`
/// no-bit arms — arm 1 (`skip_mode`), arm 2 (`seg_feature_active(
/// SEG_LVL_REF_FRAME )`), and arm 3 (`seg_feature_active( SEG_LVL_SKIP
/// ) || seg_feature_active( SEG_LVL_GLOBALMV )`). The fall-through
/// `else` arm of §5.11.25 (the `comp_mode` / `comp_ref_type` /
/// `single_ref_p?` / `comp_bwdref_p?` / `comp_ref_p?` / `uni_comp_ref`
/// dispatcher — every read with `S()` payload) is **deferred** to a
/// follow-up dispatcher arc and rejected by this bootstrap as a
/// caller-bug.
///
/// ## Spec body (§5.11.23 lines 2-5 + §5.11.25 arms 1-3)
///
/// ```text
///   inter_block_mode_info( ) {
///       PaletteSizeY = 0                                 // §5.11.23 line 2
///       PaletteSizeUV = 0                                // §5.11.23 line 3
///       read_ref_frames( )                               // §5.11.25 dispatcher
///       isCompound = RefFrame[ 1 ] > INTRA_FRAME         // §5.11.23 line 5
///       ...                                              // (further reads — out of scope)
///   }
///
///   read_ref_frames( ) {
///       if ( skip_mode ) {                               // arm 1 — no bits
///           RefFrame[ 0 ] = SkipModeFrame[ 0 ]
///           RefFrame[ 1 ] = SkipModeFrame[ 1 ]
///       } else if ( seg_feature_active( SEG_LVL_REF_FRAME ) ) {   // arm 2 — no bits
///           RefFrame[ 0 ] = FeatureData[ segment_id ][ SEG_LVL_REF_FRAME ]
///           RefFrame[ 1 ] = NONE
///       } else if ( seg_feature_active( SEG_LVL_SKIP ) ||         // arm 3 — no bits
///                   seg_feature_active( SEG_LVL_GLOBALMV ) ) {
///           RefFrame[ 0 ] = LAST_FRAME
///           RefFrame[ 1 ] = NONE
///       } else {
///           // arm 4 — `comp_mode` etc. — deferred follow-up arc.
///       }
///   }
/// ```
///
/// ## Inputs (mirrors §5.11.10 / §5.11.20 caller surface)
///
/// * `skip_mode` — `1` if the §5.11.10 `read_skip_mode()` writer
///   committed `skip_mode == 1` for this block (i.e. the §5.11.18
///   line 12 result). Arm 1 selector.
/// * `skip_mode_frame` — the frame-level `SkipModeFrame[ 0..2 ]`
///   pair from §5.9.22 (`FrameRefs[ 0..NUM_REF_FRAMES - 1 ]` after
///   the §6.8.20 `set_frame_refs( )` walk). Only consulted on arm 1.
///   Each entry MUST be in `INTRA_FRAME..=ALTREF_FRAME` (`0..=7`,
///   i.e. an §3 ref-frame index — `NONE = -1` is **not** valid
///   for `SkipModeFrame`).
/// * `seg_ref_frame_active` — `seg_feature_active( SEG_LVL_REF_FRAME
///   )` per §6.4.2. Arm 2 selector (fires only when `skip_mode == 0`).
/// * `seg_ref_frame_data` — `FeatureData[ segment_id ][ SEG_LVL_REF_FRAME
///   ]` per §5.9.14. The §6.4.1 segmentation-feature alphabet bounds
///   this to `0..=ALTREF_FRAME = 7` (a positive §3 ref-frame index).
///   Only consulted on arm 2.
/// * `seg_skip_active` — `seg_feature_active( SEG_LVL_SKIP )` per
///   §6.4.2. Arm 3 selector (fires only when both arm-1 and arm-2
///   selectors are false).
/// * `seg_globalmv_active` — `seg_feature_active( SEG_LVL_GLOBALMV )`
///   per §6.4.2. Combined with `seg_skip_active` for arm 3.
///
/// ## Output
///
/// Returns the `(RefFrame[ 0 ], RefFrame[ 1 ])` pair as `(i8, i8)`
/// with the §3 `NONE = -1` sentinel reachable on slot 1 (arms 2 + 3
/// stamp `RefFrame[ 1 ] = NONE`; arm 1 propagates whatever
/// `skip_mode_frame[ 1 ]` carries, which is a positive index per
/// §5.9.22 spec). `PaletteSizeY` / `PaletteSizeUV` are implicit
/// (`0` per the §5.11.23 lines 2-3 resets) — the caller threads
/// them into any downstream §5.11.46 / §5.11.49 reads as
/// appropriate.
///
/// ## Out-of-range / mismatch cases (surfaced as `Error::PartitionWalkOutOfRange`)
///
/// * `skip_mode > 1` — outside the §3 binary alphabet.
/// * `skip_mode == 1` and either `skip_mode_frame[ 0 ]` or
///   `skip_mode_frame[ 1 ]` outside `INTRA_FRAME..=ALTREF_FRAME` —
///   §5.9.22 invariant violation.
/// * `seg_ref_frame_active == true` (with `skip_mode == 0`) and
///   `seg_ref_frame_data` outside `INTRA_FRAME..=ALTREF_FRAME` —
///   §6.4.1 segmentation-feature alphabet violation.
/// * All four arm selectors false — the `else` arm 4 (`comp_mode`
///   dispatcher) is deferred and rejected as caller-bug for this
///   bootstrap. Follow-up arc.
///
/// ## §5.11.5 grid-fill is on the caller
///
/// Stateless: this is a pure derivation (no symbols emitted, no
/// CDF accesses). The caller stamps the resulting `RefFrame[ 0..2
/// ]` pair onto the `bh4 * bw4` block footprint inside the
/// encoder-side `RefFrames[][][..]` grid (per §5.11.23 the same
/// stamp the decoder's [`crate::cdf::PartitionWalker`] performs
/// after `read_ref_frames` returns). No [`SymbolWriter`] reference
/// is needed because the bootstrap never writes a bit.
pub fn write_inter_block_mode_info_bootstrap(
    skip_mode: u8,
    skip_mode_frame: [i8; 2],
    seg_ref_frame_active: bool,
    seg_ref_frame_data: i8,
    seg_skip_active: bool,
    seg_globalmv_active: bool,
) -> Result<(i8, i8), Error> {
    // §3 binary alphabet bound on `skip_mode`.
    if skip_mode > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §3 ref-frame index range — `INTRA_FRAME = 0..=ALTREF_FRAME = 7`.
    // Helper closure for the validity checks below; an `i8` slot is
    // valid iff it lies in `0..=7`.
    let in_ref_range = |v: i8| (0..=7).contains(&v);

    // §5.11.25 arm 1: `if ( skip_mode ) RefFrame[ 0..2 ] = SkipModeFrame[ 0..2 ]`.
    // No bits emitted. §5.9.22 fills `SkipModeFrame[ 0..2 ]` with two
    // positive ref-frame indices (`NONE = -1` is invalid here per the
    // §5.9.22 derivation).
    if skip_mode == 1 {
        if !in_ref_range(skip_mode_frame[0]) || !in_ref_range(skip_mode_frame[1]) {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok((skip_mode_frame[0], skip_mode_frame[1]));
    }

    // §5.11.25 arm 2: `else if ( seg_feature_active( SEG_LVL_REF_FRAME ) )`.
    // No bits emitted. `FeatureData[ segment_id ][ SEG_LVL_REF_FRAME ]`
    // is a positive §3 ref-frame index per §6.4.1; `RefFrame[ 1 ] = NONE`.
    if seg_ref_frame_active {
        if !in_ref_range(seg_ref_frame_data) {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok((seg_ref_frame_data, -1));
    }

    // §5.11.25 arm 3: `else if ( seg_feature_active( SEG_LVL_SKIP ) ||
    //                            seg_feature_active( SEG_LVL_GLOBALMV ) )`.
    // No bits emitted. `RefFrame[ 0 ] = LAST_FRAME = 1`, `RefFrame[ 1 ] = NONE`.
    if seg_skip_active || seg_globalmv_active {
        return Ok((1, -1));
    }

    // §5.11.25 arm 4 (fall-through `else`): `comp_mode` / `comp_ref_type`
    // / `single_ref_p?` / `comp_bwdref_p?` / `comp_ref_p?` /
    // `uni_comp_ref` dispatcher. Out of scope for r266 — rejected as a
    // caller-bug so callers don't silently fall into a "RefFrame stays
    // unset" hole. Follow-up arc lands the four-arm S()-emitting body.
    Err(Error::PartitionWalkOutOfRange)
}

/// `comp_mode` writer per §5.11.25 `read_ref_frames( )` arm 4 (the
/// fall-through `else`) — av1-spec p.76. This is the **first** `S()`
/// of the four-arm reference-selection dispatcher that
/// [`write_inter_block_mode_info_bootstrap`] defers. It encodes
/// whether the block uses single- or compound-reference prediction.
///
/// ## Spec body (§5.11.25 arm 4, lines 1-5 — av1-spec p.76)
///
/// ```text
///   } else {                                            // arm 4
///       bw4 = Num_4x4_Blocks_Wide[ MiSize ]
///       bh4 = Num_4x4_Blocks_High[ MiSize ]
///       if ( reference_select && ( Min( bw4, bh4 ) >= 2 ) )
///            comp_mode                                              S()
///       else
///            comp_mode = SINGLE_REFERENCE
///       ...                                             // (COMPOUND/SINGLE bodies — follow-up)
///   }
/// ```
///
/// The `bw4 = Num_4x4_Blocks_Wide[ MiSize ]` / `bh4 =
/// Num_4x4_Blocks_High[ MiSize ]` derivations are folded in from the
/// public [`crate::cdf::NUM_4X4_BLOCKS_WIDE`] /
/// [`crate::cdf::NUM_4X4_BLOCKS_HIGH`] tables (§9.3 p.400) so the
/// caller only threads the `MiSize` index. The COMPOUND/SINGLE
/// reference-frame bodies (`comp_ref_type` / `single_ref_p?` /
/// `comp_bwdref_p?` / `uni_comp_ref` reads) are a deferred follow-up
/// arc — this writer only emits the `comp_mode` selector itself.
///
/// ## Inputs
///
/// * `comp_mode` — the §3 `CompMode` value the encoder chose for this
///   block: `SINGLE_REFERENCE = 0` or `COMPOUND_REFERENCE = 1`. MUST
///   be `0` or `1` (the §3 binary alphabet).
/// * `mi_size` — the block's `MiSize` (§3 block-size index), in
///   `0..BLOCK_SIZES = 22`. Indexes the §9.3 4×4-block-count tables to
///   derive `Min( bw4, bh4 )`.
/// * `reference_select` — frame-level `reference_select` (§5.9.23
///   `frame_reference_mode( )`). When `false`, the precondition fails
///   and `comp_mode` is forced to `SINGLE_REFERENCE` with no bit.
/// * `ctx` — the §8.3.2 `comp_mode` context computed via
///   [`crate::cdf::comp_mode_ctx`] from the neighbour single/intra
///   predicates. MUST be in `0..COMP_INTER_CONTEXTS = 5`. Only
///   consulted when the precondition holds (a symbol is emitted).
///
/// ## Out-of-range / mismatch cases (surfaced as `Error::PartitionWalkOutOfRange`)
///
/// * `comp_mode > 1` — outside the §3 binary `CompMode` alphabet.
/// * `mi_size >= BLOCK_SIZES` — invalid §3 block-size index.
/// * Precondition false (`!reference_select` or `Min( bw4, bh4 ) < 2`)
///   and `comp_mode != SINGLE_REFERENCE` — the spec forces
///   `comp_mode = SINGLE_REFERENCE` with no bit, so any other value is
///   a caller bug.
/// * Precondition true and `ctx >= COMP_INTER_CONTEXTS` — invalid
///   §8.3.2 ctx.
///
/// ## §5.11.5 grid-fill is on the caller
///
/// Stateless (mirrors [`write_is_inter`]): on the suppressed-bit path
/// no symbol is emitted; on the explicit path exactly one §8.2.6
/// `S()` over `TileCompModeCdf[ ctx ]` is written. The caller stamps
/// the resulting `comp_mode`-derived `RefFrame[ 0..2 ]` pair onto the
/// block footprint through its own [`crate::cdf::PartitionWalker`].
pub fn write_comp_mode(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    comp_mode: u8,
    mi_size: usize,
    reference_select: bool,
    ctx: usize,
) -> Result<(), Error> {
    // §3 binary alphabet bound on `CompMode`.
    if comp_mode > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }
    // §3 block-size index bound — needed to index the §9.3 tables.
    if mi_size >= BLOCK_SIZES {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.25 arm 4: `bw4 = Num_4x4_Blocks_Wide[ MiSize ]`,
    // `bh4 = Num_4x4_Blocks_High[ MiSize ]`.
    let bw4 = NUM_4X4_BLOCKS_WIDE[mi_size];
    let bh4 = NUM_4X4_BLOCKS_HIGH[mi_size];

    // `if ( reference_select && ( Min( bw4, bh4 ) >= 2 ) ) comp_mode S()
    //  else comp_mode = SINGLE_REFERENCE`.
    if reference_select && bw4.min(bh4) >= 2 {
        if ctx >= COMP_INTER_CONTEXTS {
            return Err(Error::PartitionWalkOutOfRange);
        }
        let cdf = cdfs.comp_mode_cdf(ctx);
        writer.write_symbol(comp_mode as u32, cdf)
    } else {
        // No bit: the spec forces `comp_mode = SINGLE_REFERENCE = 0`.
        if comp_mode != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        Ok(())
    }
}

/// COMPOUND_REFERENCE body writer per §5.11.25 `read_ref_frames( )`
/// arm 4 (av1-spec p.76-77) — r268. Encodes the `comp_ref_type` /
/// `uni_comp_ref` / `uni_comp_ref_p1` / `uni_comp_ref_p2` /
/// `comp_ref` / `comp_ref_p1` / `comp_ref_p2` / `comp_bwdref` /
/// `comp_bwdref_p1` cascade that follows a
/// `comp_mode == COMPOUND_REFERENCE` outcome of [`write_comp_mode`].
/// This is the exact algebraic inverse of the §5.11.25
/// `if ( comp_mode == COMPOUND_REFERENCE )` body: each branch
/// condition the reader evaluates on a decoded bit becomes a bit
/// derived here from the target `RefFrame[ 0..2 ]` pair.
///
/// ## Spec body (§5.11.25 COMPOUND_REFERENCE arm — av1-spec p.76-77)
///
/// ```text
///   if ( comp_mode == COMPOUND_REFERENCE ) {
///       comp_ref_type                                              S()
///       if ( comp_ref_type == UNIDIR_COMP_REFERENCE ) {
///            uni_comp_ref                                          S()
///            if ( uni_comp_ref ) {
///                RefFrame[0] = BWDREF_FRAME; RefFrame[1] = ALTREF_FRAME
///            } else {
///                uni_comp_ref_p1                                   S()
///                if ( uni_comp_ref_p1 ) {
///                     uni_comp_ref_p2                              S()
///                     if ( uni_comp_ref_p2 ) {
///                       RefFrame[0] = LAST_FRAME; RefFrame[1] = GOLDEN_FRAME
///                     } else {
///                       RefFrame[0] = LAST_FRAME; RefFrame[1] = LAST3_FRAME
///                     }
///                } else {
///                     RefFrame[0] = LAST_FRAME; RefFrame[1] = LAST2_FRAME
///                }
///            }
///       } else {
///            comp_ref                                              S()
///            if ( comp_ref == 0 ) {
///                comp_ref_p1                                       S()
///                RefFrame[ 0 ] = comp_ref_p1 ? LAST2_FRAME : LAST_FRAME
///            } else {
///                comp_ref_p2                                       S()
///                RefFrame[ 0 ] = comp_ref_p2 ? GOLDEN_FRAME : LAST3_FRAME
///            }
///            comp_bwdref                                           S()
///            if ( comp_bwdref == 0 ) {
///                comp_bwdref_p1                                    S()
///                RefFrame[ 1 ] = comp_bwdref_p1 ? ALTREF2_FRAME : BWDREF_FRAME
///            } else {
///                RefFrame[ 1 ] = ALTREF_FRAME
///            }
///       }
///   }
/// ```
///
/// ## Inverse derivations
///
/// `comp_ref_type` itself is derived, not caller-chosen: §6.10.24
/// defines `UNIDIR_COMP_REFERENCE = 0` as "both reference frames from
/// the same group" and `BIDIR_COMP_REFERENCE = 1` as "one from Group 1
/// and one from Group 2", which is exactly the §8.3.2
/// `is_samedir_ref_pair( ref0, ref1 )` predicate (av1-spec p.383):
/// `comp_ref_type = is_samedir_ref_pair( .. ) ? UNIDIR : BIDIR`. The
/// per-bit inverses then read off the §5.11.25 leaf assignments:
///
/// * `uni_comp_ref = ( pair == ( BWDREF_FRAME, ALTREF_FRAME ) )`
/// * `uni_comp_ref_p1 = ( RefFrame[ 1 ] != LAST2_FRAME )`
/// * `uni_comp_ref_p2 = ( RefFrame[ 1 ] == GOLDEN_FRAME )`
/// * `comp_ref = ( RefFrame[ 0 ] >= LAST3_FRAME )`
/// * `comp_ref_p1 = ( RefFrame[ 0 ] == LAST2_FRAME )`
/// * `comp_ref_p2 = ( RefFrame[ 0 ] == GOLDEN_FRAME )`
/// * `comp_bwdref = ( RefFrame[ 1 ] == ALTREF_FRAME )`
/// * `comp_bwdref_p1 = ( RefFrame[ 1 ] == ALTREF2_FRAME )`
///
/// ## Inputs
///
/// * `ref_frame` — the target `( RefFrame[ 0 ], RefFrame[ 1 ] )`
///   compound pair. The §5.11.25 tree reaches exactly sixteen pairs:
///   the four UNIDIR leaves `( BWDREF = 5, ALTREF = 7 )`,
///   `( LAST = 1, LAST2 = 2 )`, `( LAST = 1, LAST3 = 3 )`,
///   `( LAST = 1, GOLDEN = 4 )`, and the twelve BIDIR products of
///   `RefFrame[ 0 ] ∈ { LAST, LAST2, LAST3, GOLDEN } = Group 1` with
///   `RefFrame[ 1 ] ∈ { BWDREF, ALTREF2, ALTREF } = Group 2`
///   (§6.10.24). Any other pair is a caller bug.
/// * `avail_u` / `avail_l` / `above_single` / `left_single` /
///   `above_intra` / `left_intra` / `above_ref_frame` /
///   `left_ref_frame` — the §5.11.18 prologue neighbour-state octet,
///   identical to the surface
///   [`crate::cdf::PartitionWalker::decode_inter_block_mode_info`]
///   consumes. **Deviation from this module's caller-supplied-ctx
///   convention:** the COMPOUND body selects up to five distinct
///   §8.3.2 ctx values (`comp_ref_type` p.382 plus four
///   `ref_count_ctx` selections, one per emitted bit), all derived
///   from this one octet via the public [`comp_ref_type_ctx`] /
///   [`count_refs`] / [`ref_count_ctx`] helpers. Passing them
///   severally would invite mismatched-row bugs between sibling
///   symbols, so the writer derives every ctx internally — exactly
///   the lines the decoder's §5.11.25 reader executes.
///
/// ## Out-of-range / mismatch cases (surfaced as `Error::PartitionWalkOutOfRange`)
///
/// * Either `ref_frame` slot outside `LAST_FRAME..=ALTREF_FRAME`
///   (`1..=7`) — a compound pair never carries `INTRA_FRAME = 0` or
///   `NONE = -1` (§6.10.24).
/// * A same-group pair that is not one of the four UNIDIR leaves
///   (e.g. `( LAST2, LAST3 )` or `( BWDREF, ALTREF2 )`) — the
///   §5.11.25 UNIDIR sub-tree cannot express it.
/// * A different-group pair with `RefFrame[ 0 ]` from Group 2 (e.g.
///   `( BWDREF, LAST )`) — the §5.11.25 BIDIR sub-tree always puts
///   the Group-1 frame in slot 0 (§6.10.24).
///
/// ## §5.11.5 grid-fill is on the caller
///
/// Stateless (mirrors [`write_comp_mode`]): between two and five
/// §8.2.6 `S()` symbols are written depending on the leaf. The caller
/// stamps the pair onto the block footprint through its own
/// [`crate::cdf::PartitionWalker`] so subsequent blocks' §8.3.2
/// neighbour walks observe it.
#[allow(clippy::too_many_arguments)]
pub fn write_compound_ref_frames(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    ref_frame: [i32; 2],
    avail_u: bool,
    avail_l: bool,
    above_single: bool,
    left_single: bool,
    above_intra: bool,
    left_intra: bool,
    above_ref_frame: [i32; 2],
    left_ref_frame: [i32; 2],
) -> Result<(), Error> {
    let ref0 = ref_frame[0];
    let ref1 = ref_frame[1];

    // §6.10.24: compound pairs carry two LAST_FRAME..=ALTREF_FRAME
    // (1..=7) indices — INTRA_FRAME = 0 and NONE = -1 are invalid.
    if !(1..=7).contains(&ref0) || !(1..=7).contains(&ref1) {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §6.10.24 / §8.3.2 p.383: UNIDIR_COMP_REFERENCE = 0 ⇔ both refs
    // in the same direction group ⇔ `is_samedir_ref_pair( ref0, ref1 )`.
    let unidir = is_samedir_ref_pair(ref0, ref1);

    if unidir {
        // §5.11.25 UNIDIR leaves: (BWDREF = 5, ALTREF = 7),
        // (LAST = 1, LAST2 = 2), (LAST = 1, LAST3 = 3),
        // (LAST = 1, GOLDEN = 4). Any other same-group pair is
        // unreachable by the sub-tree.
        if !matches!((ref0, ref1), (5, 7) | (1, 2) | (1, 3) | (1, 4)) {
            return Err(Error::PartitionWalkOutOfRange);
        }
    } else if ref0 > 4 {
        // §6.10.24 BIDIR: slot 0 is the Group-1 (forward) frame
        // (LAST..=GOLDEN = 1..=4); slot 1 the Group-2 (backward)
        // frame. With `!is_samedir_ref_pair` and both slots in 1..=7,
        // `ref0 <= 4` forces `ref1 >= 5`; a Group-2 slot 0 cannot be
        // expressed.
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §8.3.2 `count_refs( frameType )` against the fixed neighbour
    // octet (av1-spec p.366) — every per-bit `ref_count_ctx` selection
    // below consumes these counts.
    let cnt = |frame_type: i32| {
        count_refs(
            frame_type,
            avail_u,
            above_ref_frame,
            avail_l,
            left_ref_frame,
        )
    };

    // §5.11.25: `comp_ref_type` S() over `TileCompRefTypeCdf[ ctx ]`
    // (§8.3.2 p.382).
    let crt_ctx = comp_ref_type_ctx(
        avail_u,
        avail_l,
        above_single,
        left_single,
        above_intra,
        left_intra,
        above_ref_frame,
        left_ref_frame,
    );
    let row = cdfs.comp_ref_type_cdf(crt_ctx);
    writer.write_symbol(!unidir as u32, row)?;

    if unidir {
        // §5.11.25: `uni_comp_ref` S() over `TileUniCompRefCdf[ ctx ][ 0 ]`.
        // §8.3.2 p.383: ctx as in the `single_ref_p1` selection (p.368)
        // — `fwdCount` (LAST + LAST2 + LAST3 + GOLDEN) vs `bwdCount`
        // (BWDREF + ALTREF2 + ALTREF).
        let ucr_ctx = ref_count_ctx(cnt(1) + cnt(2) + cnt(3) + cnt(4), cnt(5) + cnt(6) + cnt(7));
        // Inverse of `if ( uni_comp_ref ) { RefFrame = (BWDREF, ALTREF) }`.
        let uni_comp_ref = ((ref0, ref1) == (5, 7)) as u32;
        let row = cdfs.uni_comp_ref_cdf(ucr_ctx, 0);
        writer.write_symbol(uni_comp_ref, row)?;

        if uni_comp_ref == 0 {
            // §5.11.25: `uni_comp_ref_p1` S() over
            // `TileUniCompRefCdf[ ctx ][ 1 ]`. §8.3.2 p.383 ctx:
            // `last2Count` vs `last3GoldCount`.
            let p1_ctx = ref_count_ctx(cnt(2), cnt(3) + cnt(4));
            // Inverse: p1 == 0 ⇒ (LAST, LAST2); p1 == 1 ⇒ p2 leaf.
            let uni_comp_ref_p1 = (ref1 != 2) as u32;
            let row = cdfs.uni_comp_ref_cdf(p1_ctx, 1);
            writer.write_symbol(uni_comp_ref_p1, row)?;

            if uni_comp_ref_p1 == 1 {
                // §5.11.25: `uni_comp_ref_p2` S() over
                // `TileUniCompRefCdf[ ctx ][ 2 ]`. §8.3.2 p.383 ctx:
                // as in `comp_ref_p2` (p.367) — `last3Count` vs
                // `goldCount`.
                let p2_ctx = ref_count_ctx(cnt(3), cnt(4));
                // Inverse: p2 ⇒ (LAST, GOLDEN), else (LAST, LAST3).
                let uni_comp_ref_p2 = (ref1 == 4) as u32;
                let row = cdfs.uni_comp_ref_cdf(p2_ctx, 2);
                writer.write_symbol(uni_comp_ref_p2, row)?;
            }
        }
    } else {
        // §5.11.25: `comp_ref` S() over `TileCompRefCdf[ ctx ][ 0 ]`.
        // §8.3.2 p.366 ctx: `last12Count` vs `last3GoldCount`.
        let cr_ctx = ref_count_ctx(cnt(1) + cnt(2), cnt(3) + cnt(4));
        // Inverse of the two `RefFrame[ 0 ]` leaf groups:
        // comp_ref == 0 ⇒ { LAST, LAST2 }, == 1 ⇒ { LAST3, GOLDEN }.
        let comp_ref = (ref0 >= 3) as u32;
        let row = cdfs.comp_ref_cdf(cr_ctx, 0);
        writer.write_symbol(comp_ref, row)?;

        if comp_ref == 0 {
            // §5.11.25: `comp_ref_p1` S() over `TileCompRefCdf[ ctx ][ 1 ]`.
            // §8.3.2 p.367 ctx: `lastCount` vs `last2Count`. Inverse of
            // `RefFrame[ 0 ] = comp_ref_p1 ? LAST2_FRAME : LAST_FRAME`.
            let p1_ctx = ref_count_ctx(cnt(1), cnt(2));
            let comp_ref_p1 = (ref0 == 2) as u32;
            let row = cdfs.comp_ref_cdf(p1_ctx, 1);
            writer.write_symbol(comp_ref_p1, row)?;
        } else {
            // §5.11.25: `comp_ref_p2` S() over `TileCompRefCdf[ ctx ][ 2 ]`.
            // §8.3.2 p.367 ctx: `last3Count` vs `goldCount`. Inverse of
            // `RefFrame[ 0 ] = comp_ref_p2 ? GOLDEN_FRAME : LAST3_FRAME`.
            let p2_ctx = ref_count_ctx(cnt(3), cnt(4));
            let comp_ref_p2 = (ref0 == 4) as u32;
            let row = cdfs.comp_ref_cdf(p2_ctx, 2);
            writer.write_symbol(comp_ref_p2, row)?;
        }

        // §5.11.25: `comp_bwdref` S() over `TileCompBwdRefCdf[ ctx ][ 0 ]`.
        // §8.3.2 p.367 ctx: `brfarf2Count` vs `arfCount`. Inverse of
        // `if ( comp_bwdref == 0 ) .. else RefFrame[ 1 ] = ALTREF_FRAME`.
        let bw_ctx = ref_count_ctx(cnt(5) + cnt(6), cnt(7));
        let comp_bwdref = (ref1 == 7) as u32;
        let row = cdfs.comp_bwd_ref_cdf(bw_ctx, 0);
        writer.write_symbol(comp_bwdref, row)?;

        if comp_bwdref == 0 {
            // §5.11.25: `comp_bwdref_p1` S() over
            // `TileCompBwdRefCdf[ ctx ][ 1 ]`. §8.3.2 p.367 ctx:
            // `brfCount` vs `arf2Count`. Inverse of `RefFrame[ 1 ] =
            // comp_bwdref_p1 ? ALTREF2_FRAME : BWDREF_FRAME`.
            let p1_ctx = ref_count_ctx(cnt(5), cnt(6));
            let comp_bwdref_p1 = (ref1 == 6) as u32;
            let row = cdfs.comp_bwd_ref_cdf(p1_ctx, 1);
            writer.write_symbol(comp_bwdref_p1, row)?;
        }
    }

    Ok(())
}

/// SINGLE_REFERENCE body writer per §5.11.25 `read_ref_frames( )`
/// arm 4 (av1-spec p.77) — r269. Encodes the `single_ref_p1` /
/// `single_ref_p2` / `single_ref_p3` / `single_ref_p4` /
/// `single_ref_p5` / `single_ref_p6` cascade that follows a
/// `comp_mode == SINGLE_REFERENCE` outcome of [`write_comp_mode`] —
/// the sibling of [`write_compound_ref_frames`]. This is the exact
/// algebraic inverse of the §5.11.25 final `else` body: each branch
/// condition the reader evaluates on a decoded bit becomes a bit
/// derived here from the target `RefFrame[ 0 ]`.
///
/// ## Spec body (§5.11.25 SINGLE_REFERENCE arm — av1-spec p.77)
///
/// ```text
///   } else {
///       single_ref_p1                                              S()
///       if ( single_ref_p1 ) {
///            single_ref_p2                                         S()
///            if ( single_ref_p2 == 0 ) {
///                single_ref_p6                                     S()
///                RefFrame[ 0 ] = single_ref_p6 ? ALTREF2_FRAME : BWDREF_FRAME
///            } else {
///                RefFrame[ 0 ] = ALTREF_FRAME
///            }
///       } else {
///            single_ref_p3                                         S()
///            if ( single_ref_p3 ) {
///                single_ref_p5                                     S()
///                RefFrame[ 0 ] = single_ref_p5 ? GOLDEN_FRAME : LAST3_FRAME
///            } else {
///                single_ref_p4                                     S()
///                RefFrame[ 0 ] = single_ref_p4 ? LAST2_FRAME : LAST_FRAME
///            }
///       }
///       RefFrame[ 1 ] = NONE
///   }
/// ```
///
/// ## Inverse derivations
///
/// Reading the leaves back: the `single_ref_p1 == 1` half reaches
/// exactly the §6.10.24 Group-2 (backward) frames `{ BWDREF = 5,
/// ALTREF2 = 6, ALTREF = 7 }` and the `single_ref_p1 == 0` half the
/// Group-1 (forward) frames `{ LAST = 1, LAST2 = 2, LAST3 = 3,
/// GOLDEN = 4 }`, so:
///
/// * `single_ref_p1 = ( RefFrame[ 0 ] >= BWDREF_FRAME )`
/// * `single_ref_p2 = ( RefFrame[ 0 ] == ALTREF_FRAME )`
/// * `single_ref_p6 = ( RefFrame[ 0 ] == ALTREF2_FRAME )`
/// * `single_ref_p3 = ( RefFrame[ 0 ] >= LAST3_FRAME )`
/// * `single_ref_p5 = ( RefFrame[ 0 ] == GOLDEN_FRAME )`
/// * `single_ref_p4 = ( RefFrame[ 0 ] == LAST2_FRAME )`
///
/// Always three §8.2.6 `S()` symbols except the `ALTREF_FRAME` leaf
/// (`single_ref_p1` + `single_ref_p2` only).
///
/// ## §8.3.2 CDF row selections (av1-spec p.367-368)
///
/// Every cdf is `TileSingleRefCdf[ ctx ][ p ]` with `p` per the
/// §8.3.2 list (`single_ref_p1 → 0` .. `single_ref_p6 → 5`) and `ctx`
/// a `ref_count_ctx` selection over `count_refs` of the §5.11.18
/// neighbour state:
///
/// * `single_ref_p1` — `fwdCount` (LAST + LAST2 + LAST3 + GOLDEN) vs
///   `bwdCount` (BWDREF + ALTREF2 + ALTREF) (p.368).
/// * `single_ref_p2` — as in `comp_bwdref` (p.367): `brfarf2Count` vs
///   `arfCount`.
/// * `single_ref_p3` — as in `comp_ref` (p.366-367): `last12Count` vs
///   `last3GoldCount`.
/// * `single_ref_p4` — as in `comp_ref_p1` (p.367): `lastCount` vs
///   `last2Count`.
/// * `single_ref_p5` — as in `comp_ref_p2` (p.367): `last3Count` vs
///   `goldCount`.
/// * `single_ref_p6` — as in `comp_bwdref_p1` (p.367-368): `brfCount`
///   vs `arf2Count`.
///
/// ## Inputs
///
/// * `ref_frame` — the target `( RefFrame[ 0 ], RefFrame[ 1 ] )`
///   pair. Slot 0 MUST be in `LAST_FRAME..=ALTREF_FRAME` (`1..=7`);
///   slot 1 MUST be `NONE = -1` (the arm's final assignment) — kept
///   in the signature so the contract is pair-shaped like
///   [`write_compound_ref_frames`] and a compound pair can never be
///   silently truncated to its first slot.
/// * `avail_u` / `avail_l` / `above_ref_frame` / `left_ref_frame` —
///   the `count_refs` subset of the §5.11.18 prologue neighbour
///   state. Unlike the COMPOUND body, the SINGLE cascade selects no
///   `comp_ref_type`-style ctx, so the single/intra neighbour
///   predicates are not consumed (§8.3.2 p.366-368 use only
///   `count_refs`). All per-symbol ctx selections are derived
///   internally (same rationale as [`write_compound_ref_frames`]:
///   sibling symbols can never disagree on rows).
///
/// ## Out-of-range / mismatch cases (surfaced as `Error::PartitionWalkOutOfRange`)
///
/// * `ref_frame[ 0 ]` outside `LAST_FRAME..=ALTREF_FRAME` (`1..=7`)
///   — the cascade's seven leaves cover exactly that range
///   (`INTRA_FRAME = 0` is unreachable from §5.11.25 arm 4, which
///   runs only on non-intra blocks).
/// * `ref_frame[ 1 ] != NONE = -1` — the arm forces
///   `RefFrame[ 1 ] = NONE`; any other value is a caller bug.
///
/// ## §5.11.5 grid-fill is on the caller
///
/// Stateless (mirrors [`write_compound_ref_frames`]): two or three
/// §8.2.6 `S()` symbols are written depending on the leaf. The caller
/// stamps `( RefFrame[ 0 ], NONE )` onto the block footprint through
/// its own [`crate::cdf::PartitionWalker`] so subsequent blocks'
/// §8.3.2 neighbour walks observe it.
pub fn write_single_ref_frames(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    ref_frame: [i32; 2],
    avail_u: bool,
    avail_l: bool,
    above_ref_frame: [i32; 2],
    left_ref_frame: [i32; 2],
) -> Result<(), Error> {
    let ref0 = ref_frame[0];

    // §5.11.25: the SINGLE_REFERENCE cascade reaches exactly
    // LAST_FRAME = 1 ..= ALTREF_FRAME = 7 in slot 0 and forces
    // `RefFrame[ 1 ] = NONE = -1`.
    if !(1..=7).contains(&ref0) || ref_frame[1] != -1 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §8.3.2 `count_refs( frameType )` against the fixed neighbour
    // state (av1-spec p.366) — every per-bit `ref_count_ctx`
    // selection below consumes these counts.
    let cnt = |frame_type: i32| {
        count_refs(
            frame_type,
            avail_u,
            above_ref_frame,
            avail_l,
            left_ref_frame,
        )
    };

    // §5.11.25: `single_ref_p1` S() over `TileSingleRefCdf[ ctx ][ 0 ]`.
    // §8.3.2 p.368 ctx: `fwdCount` (LAST + LAST2 + LAST3 + GOLDEN) vs
    // `bwdCount` (BWDREF + ALTREF2 + ALTREF). Inverse of the two leaf
    // halves: p1 == 1 ⇒ Group 2 (BWDREF..ALTREF = 5..=7), p1 == 0 ⇒
    // Group 1 (LAST..GOLDEN = 1..=4) (§6.10.24).
    let p1_ctx = ref_count_ctx(cnt(1) + cnt(2) + cnt(3) + cnt(4), cnt(5) + cnt(6) + cnt(7));
    let single_ref_p1 = (ref0 >= 5) as u32;
    let row = cdfs.single_ref_cdf(p1_ctx, 0);
    writer.write_symbol(single_ref_p1, row)?;

    if single_ref_p1 != 0 {
        // §5.11.25: `single_ref_p2` S() over `TileSingleRefCdf[ ctx ][ 1 ]`.
        // §8.3.2 p.368 ctx: as in `comp_bwdref` (p.367) — `brfarf2Count`
        // vs `arfCount`. Inverse of `if ( single_ref_p2 == 0 ) .. else
        // RefFrame[ 0 ] = ALTREF_FRAME`.
        let p2_ctx = ref_count_ctx(cnt(5) + cnt(6), cnt(7));
        let single_ref_p2 = (ref0 == 7) as u32;
        let row = cdfs.single_ref_cdf(p2_ctx, 1);
        writer.write_symbol(single_ref_p2, row)?;

        if single_ref_p2 == 0 {
            // §5.11.25: `single_ref_p6` S() over
            // `TileSingleRefCdf[ ctx ][ 5 ]`. §8.3.2 p.368 ctx: as in
            // `comp_bwdref_p1` (p.367-368) — `brfCount` vs `arf2Count`.
            // Inverse of `RefFrame[ 0 ] = single_ref_p6 ?
            // ALTREF2_FRAME : BWDREF_FRAME`.
            let p6_ctx = ref_count_ctx(cnt(5), cnt(6));
            let single_ref_p6 = (ref0 == 6) as u32;
            let row = cdfs.single_ref_cdf(p6_ctx, 5);
            writer.write_symbol(single_ref_p6, row)?;
        }
    } else {
        // §5.11.25: `single_ref_p3` S() over `TileSingleRefCdf[ ctx ][ 2 ]`.
        // §8.3.2 p.368 ctx: as in `comp_ref` (p.366-367) — `last12Count`
        // vs `last3GoldCount`. Inverse of the two Group-1 leaf pairs:
        // p3 == 0 ⇒ { LAST, LAST2 }, p3 == 1 ⇒ { LAST3, GOLDEN }.
        let p3_ctx = ref_count_ctx(cnt(1) + cnt(2), cnt(3) + cnt(4));
        let single_ref_p3 = (ref0 >= 3) as u32;
        let row = cdfs.single_ref_cdf(p3_ctx, 2);
        writer.write_symbol(single_ref_p3, row)?;

        if single_ref_p3 != 0 {
            // §5.11.25: `single_ref_p5` S() over
            // `TileSingleRefCdf[ ctx ][ 4 ]`. §8.3.2 p.368 ctx: as in
            // `comp_ref_p2` (p.367) — `last3Count` vs `goldCount`.
            // Inverse of `RefFrame[ 0 ] = single_ref_p5 ?
            // GOLDEN_FRAME : LAST3_FRAME`.
            let p5_ctx = ref_count_ctx(cnt(3), cnt(4));
            let single_ref_p5 = (ref0 == 4) as u32;
            let row = cdfs.single_ref_cdf(p5_ctx, 4);
            writer.write_symbol(single_ref_p5, row)?;
        } else {
            // §5.11.25: `single_ref_p4` S() over
            // `TileSingleRefCdf[ ctx ][ 3 ]`. §8.3.2 p.368 ctx: as in
            // `comp_ref_p1` (p.367) — `lastCount` vs `last2Count`.
            // Inverse of `RefFrame[ 0 ] = single_ref_p4 ?
            // LAST2_FRAME : LAST_FRAME`.
            let p4_ctx = ref_count_ctx(cnt(1), cnt(2));
            let single_ref_p4 = (ref0 == 2) as u32;
            let row = cdfs.single_ref_cdf(p4_ctx, 3);
            writer.write_symbol(single_ref_p4, row)?;
        }
    }

    Ok(())
}

/// Full `read_ref_frames( )` writer per §5.11.25 (av1-spec p.76-77) —
/// the four-arm dispatcher composed from r266's no-bit arms
/// ([`write_inter_block_mode_info_bootstrap`]), r267's `comp_mode`
/// selector ([`write_comp_mode`]), r268's COMPOUND_REFERENCE body
/// ([`write_compound_ref_frames`]) and r269's SINGLE_REFERENCE body
/// ([`write_single_ref_frames`]).
///
/// Where the bootstrap *returns* the pair the no-bit arms force, this
/// dispatcher takes the caller's target `RefFrame[ 0..2 ]` pair and
/// **verifies** reachability: on arms 1-3 the pair must equal the
/// §5.11.25 forced assignment (no symbols emitted); on arm 4 it
/// drives the `comp_mode` + reference-body symbol cascade as the
/// exact inverse of the reader.
///
/// ## Arm selection (§5.11.25 p.76)
///
/// 1. `skip_mode == 1` ⇒ pair must equal `SkipModeFrame[ 0..2 ]`.
/// 2. `seg_ref_frame_active` ⇒ pair must equal
///    `( seg_ref_frame_data, NONE )`.
/// 3. `seg_skip_active || seg_globalmv_active` ⇒ pair must equal
///    `( LAST_FRAME, NONE )`.
/// 4. Else: `comp_mode` is derived from the target pair —
///    `RefFrame[ 1 ] == NONE = -1` ⇒ `SINGLE_REFERENCE = 0`,
///    `RefFrame[ 1 ]` in `LAST_FRAME..=ALTREF_FRAME` ⇒
///    `COMPOUND_REFERENCE = 1` (the §5.11.23
///    `isCompound = RefFrame[ 1 ] > INTRA_FRAME` derivation, p.74),
///    any other slot-1 value rejected. [`write_comp_mode`] emits (or
///    suppresses, per the §5.11.25 `reference_select &&
///    Min( bw4, bh4 ) >= 2` precondition) the selector over the
///    internally-derived [`comp_mode_ctx`], then the matching
///    reference body writes its cascade.
///
/// ## Inputs
///
/// Union of the composed writers' surfaces: the target pair +
/// `mi_size` (§9.3 table index for the `comp_mode` precondition), the
/// arm-selection sextet of
/// [`write_inter_block_mode_info_bootstrap`], frame-level
/// `reference_select` (§5.9.23), and the §5.11.18 neighbour-state
/// octet from which every §8.3.2 ctx is derived internally.
///
/// ## Out-of-range / mismatch cases (surfaced as `Error::PartitionWalkOutOfRange`)
///
/// All of the composed writers' rejects, plus: a target pair that
/// does not equal the arm-1/2/3 forced assignment, a slot-1 value
/// outside `{ NONE } ∪ LAST_FRAME..=ALTREF_FRAME` on arm 4, and a
/// compound target on the suppressed-`comp_mode` path (rejected
/// inside [`write_comp_mode`] — the spec forces `SINGLE_REFERENCE`
/// with no bit there).
///
/// ## §5.11.5 grid-fill is on the caller
///
/// Stateless like every piece it composes: zero to six §8.2.6 `S()`
/// symbols are written depending on arm and leaf. The caller stamps
/// the pair onto the block footprint through its own
/// [`crate::cdf::PartitionWalker`].
#[allow(clippy::too_many_arguments)]
pub fn write_ref_frames(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    ref_frame: [i32; 2],
    mi_size: usize,
    skip_mode: u8,
    skip_mode_frame: [i8; 2],
    seg_ref_frame_active: bool,
    seg_ref_frame_data: i8,
    seg_skip_active: bool,
    seg_globalmv_active: bool,
    reference_select: bool,
    avail_u: bool,
    avail_l: bool,
    above_single: bool,
    left_single: bool,
    above_intra: bool,
    left_intra: bool,
    above_ref_frame: [i32; 2],
    left_ref_frame: [i32; 2],
) -> Result<(), Error> {
    // §5.11.25 arms 1-3 (no bits): delegate the forced-pair
    // derivation (and the arm-priority ordering + input validation)
    // to the r266 bootstrap, then require the caller's target pair to
    // match it. `skip_mode > 1` falls into the gate and is rejected
    // by the bootstrap's own §3 binary-alphabet check.
    if skip_mode != 0 || seg_ref_frame_active || seg_skip_active || seg_globalmv_active {
        let (forced0, forced1) = write_inter_block_mode_info_bootstrap(
            skip_mode,
            skip_mode_frame,
            seg_ref_frame_active,
            seg_ref_frame_data,
            seg_skip_active,
            seg_globalmv_active,
        )?;
        if ref_frame != [forced0 as i32, forced1 as i32] {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // §5.11.25 arm 4. `comp_mode` is not caller-chosen: the §5.11.23
    // line 6 derivation `isCompound = RefFrame[ 1 ] > INTRA_FRAME`
    // (p.74) reads it off the target pair — `NONE = -1` ⇒ SINGLE,
    // `LAST_FRAME..=ALTREF_FRAME` ⇒ COMPOUND. `INTRA_FRAME = 0` (or
    // anything else) in slot 1 is unreachable from §5.11.25 and a
    // caller bug.
    let comp_mode: u8 = match ref_frame[1] {
        -1 => 0,
        1..=7 => 1,
        _ => return Err(Error::PartitionWalkOutOfRange),
    };

    // §8.3.2 `comp_mode` ctx (p.366) from the neighbour octet — the
    // same derivation the reader runs before its S().
    let ctx = comp_mode_ctx(
        avail_u,
        avail_l,
        above_single,
        left_single,
        above_intra,
        left_intra,
        above_ref_frame,
        left_ref_frame,
    );
    write_comp_mode(writer, cdfs, comp_mode, mi_size, reference_select, ctx)?;

    if comp_mode == 1 {
        // §5.11.25 `if ( comp_mode == COMPOUND_REFERENCE )` body.
        write_compound_ref_frames(
            writer,
            cdfs,
            ref_frame,
            avail_u,
            avail_l,
            above_single,
            left_single,
            above_intra,
            left_intra,
            above_ref_frame,
            left_ref_frame,
        )
    } else {
        // §5.11.25 final `else` (SINGLE_REFERENCE) body.
        write_single_ref_frames(
            writer,
            cdfs,
            ref_frame,
            avail_u,
            avail_l,
            above_ref_frame,
            left_ref_frame,
        )
    }
}

/// Single-prediction inter-mode writer per §5.11.23
/// `inter_block_mode_info( )` lines 9-22 (av1-spec p.74) — the
/// `else` arm of the §5.11.23 `YMode` derivation that fires when the
/// block is **not** compound, **not** `skip_mode`, and neither
/// `SEG_LVL_SKIP` nor `SEG_LVL_GLOBALMV` is active. This is the exact
/// algebraic inverse of the reader's `new_mv` / `zero_mv` / `ref_mv`
/// cascade.
///
/// ## Spec body (§5.11.23 single-prediction arm — av1-spec p.74)
///
/// ```text
///   new_mv                                                  S()
///   if ( new_mv == 0 ) {
///        YMode = NEWMV
///   } else {
///        zero_mv                                            S()
///        if ( zero_mv == 0 ) {
///            YMode = GLOBALMV
///        } else {
///            ref_mv                                         S()
///            YMode = (ref_mv == 0) ? NEARESTMV : NEARMV
///        }
///   }
/// ```
///
/// ## Inverse derivations
///
/// Each branch condition the reader evaluates on a decoded bit becomes
/// a bit derived here from the target `YMode`:
///
/// * `new_mv  = ( YMode != NEWMV )` — `new_mv == 0` ⇒ `NEWMV`.
/// * `zero_mv = ( YMode != GLOBALMV )` — only emitted when
///   `new_mv == 1`; `zero_mv == 0` ⇒ `GLOBALMV`.
/// * `ref_mv  = ( YMode == NEARMV )` — only emitted when
///   `new_mv == 1 && zero_mv == 1`; `ref_mv == 0` ⇒ `NEARESTMV`,
///   `ref_mv == 1` ⇒ `NEARMV`.
///
/// So `NEWMV` emits one symbol, `GLOBALMV` two, and `NEARESTMV` /
/// `NEARMV` three.
///
/// ## Inputs
///
/// * `y_mode` — the §5.11.23 single-prediction `YMode` the encoder
///   chose: one of `MODE_NEWMV = 17`, `MODE_GLOBALMV = 16`,
///   `MODE_NEARESTMV = 14`, `MODE_NEARMV = 15` (§6.10.22). Any other
///   value is a caller bug — the four compound / intra / GLOBALMV-by-
///   segment modes never reach this writer (the caller's earlier
///   §5.11.23 arms handle them).
/// * `new_mv_context` — §8.3.2 `NewMvContext` (`TileNewMvCdf[
///   NewMvContext ]`), in `0..NEW_MV_CONTEXTS = 6`. Always consulted
///   (every leaf emits `new_mv`).
/// * `zero_mv_context` — §8.3.2 `ZeroMvContext` (`TileZeroMvCdf[
///   ZeroMvContext ]`), in `0..ZERO_MV_CONTEXTS = 2`. Consulted only
///   when `new_mv == 1`.
/// * `ref_mv_context` — §8.3.2 `RefMvContext` (`TileRefMvCdf[
///   RefMvContext ]`), in `0..REF_MV_CONTEXTS = 6`. Consulted only
///   when `new_mv == 1 && zero_mv == 1`.
///
/// All three contexts are produced by the §7.10.2 `find_mv_stack( )`
/// process (`NewMvContext` / `ZeroMvContext` / `RefMvContext`). Like
/// every writer in this module the ctx is caller-supplied: the
/// encoder threads the same §7.10.2 outputs the decoder side derives.
///
/// ## Out-of-range / mismatch cases (surfaced as `Error::PartitionWalkOutOfRange`)
///
/// * `y_mode` not in `{ NEWMV, GLOBALMV, NEARESTMV, NEARMV }`.
/// * A context that would be **consulted** is out of range. Contexts
///   on un-taken branches are not validated (the symbol is never
///   emitted, mirroring the reader never reaching the corresponding
///   CDF row).
///
/// ## §5.11.5 grid-fill is on the caller
///
/// Stateless (mirrors [`write_comp_mode`]): one to three §8.2.6 `S()`
/// symbols are written depending on the mode. The caller stamps the
/// resulting `YMode` onto the block footprint through its own
/// [`crate::cdf::PartitionWalker`] so subsequent blocks' §8.3.2
/// neighbour walks observe it.
pub fn write_inter_single_mode(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    y_mode: u8,
    new_mv_context: usize,
    zero_mv_context: usize,
    ref_mv_context: usize,
) -> Result<(), Error> {
    // §6.10.22 single-prediction mode bound — the only four `YMode`
    // values the §5.11.23 single-prediction cascade can produce.
    if !matches!(
        y_mode,
        MODE_NEWMV | MODE_GLOBALMV | MODE_NEARESTMV | MODE_NEARMV
    ) {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.23: `new_mv` S() over `TileNewMvCdf[ NewMvContext ]`.
    // Inverse of `if ( new_mv == 0 ) YMode = NEWMV else …`.
    if new_mv_context >= NEW_MV_CONTEXTS {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let new_mv = (y_mode != MODE_NEWMV) as u32;
    let row = cdfs.new_mv_cdf(new_mv_context);
    writer.write_symbol(new_mv, row)?;

    if new_mv == 0 {
        // YMode == NEWMV — no further symbols.
        return Ok(());
    }

    // §5.11.23: `zero_mv` S() over `TileZeroMvCdf[ ZeroMvContext ]`.
    // Inverse of `if ( zero_mv == 0 ) YMode = GLOBALMV else …`.
    if zero_mv_context >= ZERO_MV_CONTEXTS {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let zero_mv = (y_mode != MODE_GLOBALMV) as u32;
    let row = cdfs.zero_mv_cdf(zero_mv_context);
    writer.write_symbol(zero_mv, row)?;

    if zero_mv == 0 {
        // YMode == GLOBALMV — no further symbols.
        return Ok(());
    }

    // §5.11.23: `ref_mv` S() over `TileRefMvCdf[ RefMvContext ]`.
    // Inverse of `YMode = (ref_mv == 0) ? NEARESTMV : NEARMV`.
    if ref_mv_context >= REF_MV_CONTEXTS {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let ref_mv = (y_mode == MODE_NEARMV) as u32;
    let row = cdfs.ref_mv_cdf(ref_mv_context);
    writer.write_symbol(ref_mv, row)?;

    Ok(())
}

/// `compound_mode` S() inverse per §5.11.23 (av1-spec p.74, lines 6-8
/// arm 3).
///
/// The compound sibling of [`write_inter_single_mode`]. When the
/// §5.11.23 inter-mode dispatch reaches its `is_compound` arm — block
/// is compound (`RefFrame[ 1 ] > INTRA_FRAME`), not `skip_mode`, and
/// neither `SEG_LVL_SKIP` nor `SEG_LVL_GLOBALMV` is active — the
/// reader codes a single §8.2.6 `S()` symbol over
/// `TileCompoundModeCdf[ ctx ]` and sets
///
/// ```text
///   YMode = NEAREST_NEARESTMV + compound_mode
/// ```
///
/// (§5.11.23 line 8, av1-spec p.74). `compound_mode` ranges over
/// `0..COMPOUND_MODES = 8`, so `YMode` ranges over the eight
/// compound-prediction modes `NEAREST_NEARESTMV = 18 ..= NEW_NEWMV =
/// 25` (§6.10.22). This writer is the exact algebraic inverse:
///
/// ```text
///   compound_mode = YMode - NEAREST_NEARESTMV
/// ```
///
/// One §8.2.6 `S()` is emitted unconditionally (the arm always codes a
/// symbol; there is no short-circuit leaf as in the single-prediction
/// cascade).
///
/// ## Inputs
///
/// * `y_mode` — the §5.11.23 compound-prediction `YMode` the encoder
///   chose: one of the eight compound modes
///   `MODE_NEAREST_NEARESTMV = 18 ..= MODE_NEW_NEWMV = 25`
///   (§6.10.22). Any other value is a caller bug — the single-pred /
///   intra / forced-mode arms never reach this writer.
/// * `compound_mode_context` — §8.3.2 `compound_mode` context, the
///   `TileCompoundModeCdf` row index in `0..COMPOUND_MODE_CONTEXTS =
///   8`. Produced by [`crate::cdf::compound_mode_ctx`] from the
///   §7.10.2 `RefMvContext` / `NewMvContext` outputs; like every
///   writer in this module the resolved ctx is caller-supplied, so
///   the encoder threads the same §8.3.2 mapping the decoder side
///   applies.
///
/// ## Out-of-range cases (surfaced as `Error::PartitionWalkOutOfRange`)
///
/// * `y_mode` not in `MODE_NEAREST_NEARESTMV ..= MODE_NEW_NEWMV`.
/// * `compound_mode_context >= COMPOUND_MODE_CONTEXTS` (the symbol is
///   always emitted, so the ctx is always validated — unlike the
///   single-prediction writer's consulted-only checks).
///
/// ## §5.11.5 grid-fill is on the caller
///
/// Stateless (mirrors [`write_inter_single_mode`]): one §8.2.6 `S()`
/// is written. The caller stamps the resulting `YMode` onto the block
/// footprint through its own [`crate::cdf::PartitionWalker`] so
/// subsequent blocks' §8.3.2 neighbour walks observe it.
pub fn write_compound_mode(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    y_mode: u8,
    compound_mode_context: usize,
) -> Result<(), Error> {
    // §6.10.22 compound-prediction mode bound — the eight `YMode`
    // values the §5.11.23 `is_compound` arm can produce, contiguous
    // from `NEAREST_NEARESTMV = 18` through `NEW_NEWMV = 25`.
    if !(MODE_NEAREST_NEARESTMV..=MODE_NEW_NEWMV).contains(&y_mode) {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if compound_mode_context >= COMPOUND_MODE_CONTEXTS {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.23 line 8 inverse: `compound_mode = YMode -
    // NEAREST_NEARESTMV`, in `0..COMPOUND_MODES = 8`.
    let compound_mode = (y_mode - MODE_NEAREST_NEARESTMV) as u32;
    debug_assert!(
        (compound_mode as usize) < COMPOUND_MODES,
        "§5.11.23 compound_mode in 0..COMPOUND_MODES"
    );

    // §5.11.23: single `compound_mode` S() over
    // `TileCompoundModeCdf[ ctx ]` (§8.3.2 p.378).
    let row = cdfs.compound_mode_cdf(compound_mode_context);
    writer.write_symbol(compound_mode, row)?;

    Ok(())
}

/// `has_nearmv()` per §5.11.23 (av1-spec p.75 — the helper definition
/// immediately following the `inter_block_mode_info` body). Returns
/// whether `y_mode` is one of the four NEARMV-bearing inter Y modes —
/// the modes whose §5.11.23 `RefMvIdx` loop iterates from `idx = 1`
/// through `idx < 3` rather than the `idx ∈ {0, 1}` window used by the
/// NEWMV / NEW_NEWMV arm.
///
/// Spec body:
///
/// ```text
///   has_nearmv( ) {
///       return (YMode == NEARMV || YMode == NEAR_NEARMV
///               || YMode == NEAR_NEWMV || YMode == NEW_NEARMV)
///   }
/// ```
///
/// Local twin of the decoder-side helper of the same name — the writer
/// module mirrors the reader's dispatch without widening the `cdf`
/// public surface.
#[inline]
fn has_nearmv(y_mode: u8) -> bool {
    matches!(
        y_mode,
        MODE_NEARMV | MODE_NEAR_NEARMV | MODE_NEAR_NEWMV | MODE_NEW_NEARMV
    )
}

/// `drl_mode` writer per §5.11.23 (av1-spec p.73-74, the `RefMvIdx`
/// loops following `assign_mv`'s `compound_mode` / single-mode
/// dispatch) — r272.
///
/// The dynamic-reference-list (DRL) index `RefMvIdx` selects which
/// candidate of the §7.10.2 `RefStackMv` the chosen motion-vector mode
/// draws its predictor from. The reader does **not** code `RefMvIdx`
/// directly: it walks the stack one slot at a time, coding a single
/// binary `drl_mode` symbol per reachable slot, where `drl_mode == 0`
/// means "stop here, use this slot" and `drl_mode == 1` means "continue
/// to the next slot". This writer is the exact algebraic inverse — it
/// re-derives that same bit sequence from the `RefMvIdx` the encoder
/// chose and emits one §8.2.6 `S()` per coded slot.
///
/// ## Spec body (§5.11.23 — av1-spec p.73-74)
///
/// ```text
///   RefMvIdx = 0
///   if ( YMode == NEWMV || YMode == NEW_NEWMV ) {
///       for ( idx = 0; idx < 2; idx++ ) {
///           if ( NumMvFound > idx + 1 ) {
///               drl_mode                                       S()
///               if ( drl_mode == 0 ) { RefMvIdx = idx; break }
///               RefMvIdx = idx + 1
///           }
///       }
///   } else if ( has_nearmv( ) ) {
///       RefMvIdx = 1
///       for ( idx = 1; idx < 3; idx++ ) {
///           if ( NumMvFound > idx + 1 ) {
///               drl_mode                                       S()
///               if ( drl_mode == 0 ) { RefMvIdx = idx; break }
///               RefMvIdx = idx + 1
///           }
///       }
///   }
/// ```
///
/// Both arms share one loop body parameterised only by the start index
/// (`0` for the NEWMV / NEW_NEWMV arm, `1` for the `has_nearmv( )`
/// arm). On the start iteration `RefMvIdx` already holds the start
/// value (`0` or `1`), so a `RefMvIdx` equal to the start with the
/// terminating slot unreachable emits no symbols at all. Any Y mode
/// outside the two arms (`GLOBALMV`, `NEARESTMV`, the compound
/// `GLOBAL_GLOBALMV` / `NEAREST_NEARESTMV` modes, …) codes no
/// `drl_mode` and is handled by the no-op fast path.
///
/// ## Inversion
///
/// For an arm starting at `start`, the decoder visits slots `start,
/// start+1, …` while `NumMvFound > idx + 1`, emitting `drl_mode = 1`
/// at every visited slot below the chosen `ref_mv_idx` and `drl_mode =
/// 0` at slot `ref_mv_idx` itself — unless `ref_mv_idx` is reached
/// only because the previous iteration's `RefMvIdx = idx + 1`
/// assignment ran off the end of the reachable window (`NumMvFound ==
/// ref_mv_idx + 1`), in which case no terminating `0` is coded. The
/// writer reproduces exactly this: walk `idx` from `start` upward,
/// emit `1` for each `idx < ref_mv_idx` that is reachable, then a
/// single `0` at `idx == ref_mv_idx` iff that slot is reachable.
///
/// ## Inputs
///
/// * `y_mode` — the §5.11.23 inter `YMode` the encoder chose. Only the
///   NEWMV / NEW_NEWMV and `has_nearmv( )` modes code `drl_mode`; every
///   other inter mode is a silent no-op (no symbol written), matching
///   the reader's `RefMvIdx = 0` / unconditioned arms.
/// * `ref_mv_idx` — the `RefMvIdx` the encoder selected, in
///   `0..MAX_REF_MV_STACK_SIZE`. Must be consistent with the arm: the
///   NEWMV / NEW_NEWMV arm admits `0..=2`, the `has_nearmv( )` arm
///   admits `1..=3` (the reader can never leave `RefMvIdx` outside
///   those windows for the respective arm).
/// * `num_mv_found` — `NumMvFound`, the §7.10.2 stack depth, in
///   `0..=MAX_REF_MV_STACK_SIZE`. Governs which slots are reachable.
/// * `drl_ctx_stack` — `DrlCtxStack[ idx ]` per §7.10.2.14, the §8.3.2
///   `TileDrlModeCdf` row index for each visited slot, each in
///   `0..DRL_MODE_CONTEXTS`. Must hold at least one entry per slot the
///   loop can visit (`ref_mv_idx` at most).
///
/// ## Out-of-range cases (surfaced as `Error::PartitionWalkOutOfRange`)
///
/// * `ref_mv_idx >= MAX_REF_MV_STACK_SIZE`.
/// * `num_mv_found > MAX_REF_MV_STACK_SIZE`.
/// * `ref_mv_idx` outside the arm's admissible window
///   (`> 2` for the NEWMV / NEW_NEWMV arm, `< 1` or `> 3` for the
///   `has_nearmv( )` arm) — a caller bug, since the reader cannot
///   produce such a value.
/// * a coded slot's `drl_ctx_stack[ idx ] >= DRL_MODE_CONTEXTS`, or
///   `drl_ctx_stack` shorter than the highest visited slot index.
///
/// ## §5.11.5 grid-fill is on the caller
///
/// Stateless: between zero and two §8.2.6 `S()` symbols are emitted.
/// The caller threads the resulting `RefMvIdx` onto its own
/// motion-vector assignment per §5.11.31.
pub fn write_drl_mode(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    y_mode: u8,
    ref_mv_idx: u32,
    num_mv_found: u32,
    drl_ctx_stack: &[u32],
) -> Result<(), Error> {
    // §7.10.2 invariants the reader can never violate.
    if (ref_mv_idx as usize) >= MAX_REF_MV_STACK_SIZE {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if (num_mv_found as usize) > MAX_REF_MV_STACK_SIZE {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.23 arm selection. `start` is the first `idx` the reader's
    // loop visits; the loop runs while `idx < start + 2`.
    let start: u32 = if y_mode == MODE_NEWMV || y_mode == MODE_NEW_NEWMV {
        // NEWMV / NEW_NEWMV arm: `RefMvIdx = 0`, `idx ∈ {0, 1}`, so
        // the reachable `RefMvIdx` window is `{0, 1, 2}`.
        if ref_mv_idx > 2 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        0
    } else if has_nearmv(y_mode) {
        // `has_nearmv( )` arm: `RefMvIdx = 1`, `idx ∈ {1, 2}`, so the
        // reachable `RefMvIdx` window is `{1, 2, 3}`.
        if !(1..=3).contains(&ref_mv_idx) {
            return Err(Error::PartitionWalkOutOfRange);
        }
        1
    } else {
        // Every other inter mode codes no `drl_mode`. The reader's
        // `RefMvIdx` stays `0` in this case; reject any other claimed
        // index as a caller bug rather than silently dropping it.
        if ref_mv_idx != 0 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    };

    // Walk `idx` from `start` while the slot is reachable
    // (`NumMvFound > idx + 1`) and `idx < start + 2`. Emit `1` for each
    // slot strictly below `ref_mv_idx`, then a terminating `0` at
    // `idx == ref_mv_idx` iff that slot is itself reachable.
    // `reconstructed` mirrors the reader's running `RefMvIdx` so a
    // caller-supplied `ref_mv_idx` that the reader could never decode
    // from this `num_mv_found` (e.g. a deep index with a shallow stack)
    // is rejected rather than silently mis-encoded.
    let mut reconstructed = start;
    let mut idx = start;
    while idx < start + 2 {
        if num_mv_found > idx + 1 {
            let slot = idx as usize;
            let &ctx = drl_ctx_stack
                .get(slot)
                .ok_or(Error::PartitionWalkOutOfRange)?;
            if ctx as usize >= DRL_MODE_CONTEXTS {
                return Err(Error::PartitionWalkOutOfRange);
            }
            // `drl_mode == 0` stops at this slot; `drl_mode == 1`
            // continues. Inverse of the reader: emit `0` exactly when
            // `idx == ref_mv_idx`.
            let drl_mode: u32 = (idx != ref_mv_idx) as u32;
            let row = cdfs.drl_mode_cdf(ctx as usize);
            writer.write_symbol(drl_mode, row)?;
            if drl_mode == 0 {
                // RefMvIdx = idx; break.
                reconstructed = idx;
                break;
            }
            // RefMvIdx = idx + 1; continue.
            reconstructed = idx + 1;
        }
        idx += 1;
    }

    // The bit sequence we just wrote must decode back to the same
    // `RefMvIdx`. If not, the caller asked for an index unreachable at
    // this stack depth — a caller bug, not a representable bitstream.
    if reconstructed != ref_mv_idx {
        return Err(Error::PartitionWalkOutOfRange);
    }

    Ok(())
}

/// MV-component writer per §5.11.32 `read_mv_component( comp )`
/// (av1-spec p.81-82) — the exact algebraic inverse of the reader's
/// `mv_sign` / `mv_class` / `mv_class0_*` / `mv_bit` / `mv_fr` /
/// `mv_hp` cascade for a single signed difference component.
///
/// ## Spec body (§5.11.32 — av1-spec p.81-82)
///
/// ```text
///   mv_sign                                                  S()
///   mv_class                                                 S()
///   if ( mv_class == MV_CLASS_0 ) {
///       mv_class0_bit                                        S()
///       if ( force_integer_mv ) mv_class0_fr = 3
///       else                    mv_class0_fr                S()
///       if ( allow_high_precision_mv ) mv_class0_hp         S()
///       else                           mv_class0_hp = 1
///       mag = ( ( mv_class0_bit << 3 ) |
///               ( mv_class0_fr  << 1 ) |
///                 mv_class0_hp ) + 1
///   } else {
///       d = 0
///       for ( i = 0; i < mv_class; i++ ) {
///           mv_bit                                          S()
///           d |= mv_bit << i
///       }
///       mag = CLASS0_SIZE << ( mv_class + 2 )
///       if ( force_integer_mv ) mv_fr = 3
///       else                    mv_fr                       S()
///       if ( allow_high_precision_mv ) mv_hp                S()
///       else                           mv_hp = 1
///       mag += ( ( d << 3 ) | ( mv_fr << 1 ) | mv_hp ) + 1
///   }
///   return mv_sign ? -mag : mag
/// ```
///
/// ## Inverse derivation
///
/// The reader returns a signed `diff` for the component. The encoder
/// is handed that `diff` and must reproduce the bitstream:
///
/// * `mv_sign = ( diff < 0 )`, `mag = |diff|`. The reader never
///   returns `0` from this function (a zero component is signalled by
///   `mv_joint` and skips the call), so `mag >= 1` is required.
/// * `offset = mag - 1`. For `MV_CLASS_0`, `offset` ∈ `0..=15` and
///   decomposes directly as `offset = (bit << 3) | (fr << 1) | hp`.
///   For class `c >= 1`, `mag = (CLASS0_SIZE << (c + 2)) + X + 1` with
///   `X = (d << 3) | (fr << 1) | hp` and `X ∈ 0..(2^(c+3) - 1)`, so
///   `offset = 2^(c+3) + X`. Hence `FloorLog2(offset) = c + 3`, giving
///   `c = FloorLog2(offset) - 3` for `offset >= 16`, and `c = 0`
///   otherwise.
/// * `mv_class0_fr` / `mv_fr` carry the two fractional bits, `mv_*_hp`
///   the half-pel bit, `mv_class0_bit` / the `mv_bit` ladder the
///   integer magnitude.
///
/// ## Precision gates
///
/// When `force_integer_mv == 1` the reader synthesises `fr = 3` rather
/// than reading it, so the encoder MUST be handed a `diff` whose
/// fractional field is exactly `3`; any other value is unrepresentable
/// and is rejected. Likewise `allow_high_precision_mv == 0` forces
/// `hp = 1`, so the half-pel bit must be `1`. These mirror the
/// reader's `else` arms and keep the writer a true inverse.
///
/// ## CDF selection (§8.3.2 — av1-spec p.493)
///
/// `mv_sign` → `TileMvSignCdf[ MvCtx ][ comp ]`, `mv_class` →
/// `TileMvClassCdf[ MvCtx ][ comp ]`, `mv_class0_bit` →
/// `TileMvClass0BitCdf[ MvCtx ][ comp ]`, `mv_class0_fr` →
/// `TileMvClass0FrCdf[ MvCtx ][ comp ][ mv_class0_bit ]`,
/// `mv_class0_hp` → `TileMvClass0HpCdf[ MvCtx ][ comp ]`, `mv_bit` →
/// `TileMvBitCdf[ MvCtx ][ comp ][ i ]`, `mv_fr` →
/// `TileMvFrCdf[ MvCtx ][ comp ]`, `mv_hp` → `TileMvHpCdf[ MvCtx ][ comp ]`.
///
/// ## Inputs
///
/// * `mv_ctx` — the §5.11.31 `MvCtx` (`0` for normal blocks,
///   `MV_INTRABC_CONTEXT = 1` for intra block copy).
/// * `comp` — the component index (`0` = horizontal, `1` = vertical).
/// * `diff` — the signed difference `Mv[ ref ][ comp ] - PredMv[ ref ][ comp ]`.
///   Must be non-zero (the caller only invokes this for components the
///   `mv_joint` marks non-zero).
/// * `force_integer_mv` / `allow_high_precision_mv` — the frame-level
///   precision flags the reader consults.
#[allow(clippy::too_many_arguments)]
pub fn write_read_mv_component(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    mv_ctx: usize,
    comp: usize,
    diff: i32,
    force_integer_mv: bool,
    allow_high_precision_mv: bool,
) -> Result<(), Error> {
    if mv_ctx >= MV_CONTEXTS || comp >= MV_COMPS {
        return Err(Error::PartitionWalkOutOfRange);
    }
    // The reader never returns 0 from this function — a zero component
    // is encoded by `mv_joint` and skips the call entirely.
    if diff == 0 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    let mv_sign: u32 = (diff < 0) as u32;
    let mag: u32 = diff.unsigned_abs();
    let offset: u32 = mag - 1;

    // mv_sign — emitted first.
    let sign_row = cdfs.mv_sign_cdf(mv_ctx, comp);
    writer.write_symbol(mv_sign, sign_row)?;

    // Class derivation: offset ∈ 0..=15 ⇒ MV_CLASS_0; otherwise
    // FloorLog2(offset) = mv_class + 3.
    let mv_class: u32 = if offset < (1 << 4) {
        MV_CLASS_0 as u32
    } else {
        floor_log2(offset) - 3
    };
    if mv_class as usize >= MV_CLASSES {
        // offset too large to encode (mag exceeds the MV range).
        return Err(Error::PartitionWalkOutOfRange);
    }

    // mv_class — symbol over the 11-ary alphabet.
    let class_row = cdfs.mv_class_cdf(mv_ctx, comp);
    writer.write_symbol(mv_class, class_row)?;

    if mv_class == MV_CLASS_0 as u32 {
        // offset = (mv_class0_bit << 3) | (mv_class0_fr << 1) | mv_class0_hp
        let mv_class0_bit: u32 = (offset >> 3) & 0x1;
        let mv_class0_fr: u32 = (offset >> 1) & 0x3;
        let mv_class0_hp: u32 = offset & 0x1;

        let bit_row = cdfs.mv_class0_bit_cdf(mv_ctx, comp);
        writer.write_symbol(mv_class0_bit, bit_row)?;

        if force_integer_mv {
            // Reader synthesises mv_class0_fr = 3 — the field must
            // already be 3, otherwise the value is unrepresentable.
            if mv_class0_fr != 3 {
                return Err(Error::PartitionWalkOutOfRange);
            }
        } else {
            let fr_row = cdfs.mv_class0_fr_cdf(mv_ctx, comp, mv_class0_bit as usize);
            writer.write_symbol(mv_class0_fr, fr_row)?;
        }

        if allow_high_precision_mv {
            let hp_row = cdfs.mv_class0_hp_cdf(mv_ctx, comp);
            writer.write_symbol(mv_class0_hp, hp_row)?;
        } else if mv_class0_hp != 1 {
            // Reader forces mv_class0_hp = 1.
            return Err(Error::PartitionWalkOutOfRange);
        }
    } else {
        // mag = (CLASS0_SIZE << (mv_class + 2)) + X + 1 with
        // X = (d << 3) | (mv_fr << 1) | mv_hp, so X = offset - 2^(c+3).
        let base: u32 = (CLASS0_SIZE as u32) << (mv_class + 2);
        let x: u32 = offset - base;
        let d: u32 = x >> 3;
        let mv_fr: u32 = (x >> 1) & 0x3;
        let mv_hp: u32 = x & 0x1;

        // mv_bit ladder: i = 0..mv_class-1, d |= mv_bit << i.
        for i in 0..(mv_class as usize) {
            let mv_bit: u32 = (d >> i) & 0x1;
            let bit_row = cdfs.mv_bit_cdf(mv_ctx, comp, i);
            writer.write_symbol(mv_bit, bit_row)?;
        }

        if force_integer_mv {
            if mv_fr != 3 {
                return Err(Error::PartitionWalkOutOfRange);
            }
        } else {
            let fr_row = cdfs.mv_fr_cdf(mv_ctx, comp);
            writer.write_symbol(mv_fr, fr_row)?;
        }

        if allow_high_precision_mv {
            let hp_row = cdfs.mv_hp_cdf(mv_ctx, comp);
            writer.write_symbol(mv_hp, hp_row)?;
        } else if mv_hp != 1 {
            return Err(Error::PartitionWalkOutOfRange);
        }
    }

    Ok(())
}

/// MV writer per §5.11.31 `read_mv( ref )` (av1-spec p.81) — the
/// algebraic inverse of the reader's `mv_joint` + per-component
/// `read_mv_component( )` dispatch.
///
/// ## Spec body (§5.11.31 — av1-spec p.81)
///
/// ```text
///   diffMv[ 0 ] = 0
///   diffMv[ 1 ] = 0
///   if ( use_intrabc ) MvCtx = MV_INTRABC_CONTEXT
///   else               MvCtx = 0
///   mv_joint                                                 S()
///   if ( mv_joint == MV_JOINT_HZVNZ || mv_joint == MV_JOINT_HNZVNZ )
///       diffMv[ 0 ] = read_mv_component( 0 )
///   if ( mv_joint == MV_JOINT_HNZVZ || mv_joint == MV_JOINT_HNZVNZ )
///       diffMv[ 1 ] = read_mv_component( 1 )
///   Mv[ ref ][ 0 ] = PredMv[ ref ][ 0 ] + diffMv[ 0 ]
///   Mv[ ref ][ 1 ] = PredMv[ ref ][ 1 ] + diffMv[ 1 ]
/// ```
///
/// ## Inverse derivation
///
/// The encoder holds the target `Mv[ ref ]` and the §7.10.2-derived
/// `PredMv[ ref ]`. The difference vector is
/// `diff[ c ] = Mv[ ref ][ c ] - PredMv[ ref ][ c ]` for each
/// component. The `mv_joint` symbol records which components are
/// non-zero:
///
/// | `diff[0]` | `diff[1]` | `mv_joint`        |
/// |-----------|-----------|-------------------|
/// | `== 0`    | `== 0`    | `MV_JOINT_ZERO`   |
/// | `== 0`    | `!= 0`    | `MV_JOINT_HNZVZ`  |
/// | `!= 0`    | `== 0`    | `MV_JOINT_HZVNZ`  |
/// | `!= 0`    | `!= 0`    | `MV_JOINT_HNZVNZ` |
///
/// Note the reader's component indexing: `read_mv_component( 0 )` (the
/// horizontal component, `diff[0]`) fires for `MV_JOINT_HZVNZ` /
/// `MV_JOINT_HNZVNZ`, and `read_mv_component( 1 )` (vertical,
/// `diff[1]`) for `MV_JOINT_HNZVZ` / `MV_JOINT_HNZVNZ`. The encoder
/// follows the same call order: component `0` then component `1`.
///
/// ## CDF selection (§8.3.2 — av1-spec p.493)
///
/// `mv_joint` → `TileMvJointCdf[ MvCtx ]`. The component reads select
/// their CDFs as documented on [`write_read_mv_component`].
///
/// ## Inputs
///
/// * `mv` — the target `Mv[ ref ]` as `[horizontal, vertical]`.
/// * `pred_mv` — the §7.10.2 `PredMv[ ref ]` predictor.
/// * `use_intrabc` — selects `MvCtx = MV_INTRABC_CONTEXT` when set.
/// * `force_integer_mv` / `allow_high_precision_mv` — frame-level
///   precision flags forwarded to each component write.
pub fn write_read_mv(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    mv: [i32; 2],
    pred_mv: [i32; 2],
    use_intrabc: bool,
    force_integer_mv: bool,
    allow_high_precision_mv: bool,
) -> Result<(), Error> {
    let mv_ctx: usize = if use_intrabc { MV_INTRABC_CONTEXT } else { 0 };

    let diff0: i32 = mv[0].wrapping_sub(pred_mv[0]);
    let diff1: i32 = mv[1].wrapping_sub(pred_mv[1]);

    let mv_joint: u32 = match (diff0 != 0, diff1 != 0) {
        (false, false) => MV_JOINT_ZERO as u32,
        (false, true) => MV_JOINT_HNZVZ as u32,
        (true, false) => MV_JOINT_HZVNZ as u32,
        (true, true) => MV_JOINT_HNZVNZ as u32,
    };

    let joint_row = cdfs.mv_joint_cdf(mv_ctx);
    writer.write_symbol(mv_joint, joint_row)?;

    // Component 0 (horizontal) fires for MV_JOINT_HZVNZ / MV_JOINT_HNZVNZ.
    if diff0 != 0 {
        write_read_mv_component(
            writer,
            cdfs,
            mv_ctx,
            0,
            diff0,
            force_integer_mv,
            allow_high_precision_mv,
        )?;
    }
    // Component 1 (vertical) fires for MV_JOINT_HNZVZ / MV_JOINT_HNZVNZ.
    if diff1 != 0 {
        write_read_mv_component(
            writer,
            cdfs,
            mv_ctx,
            1,
            diff1,
            force_integer_mv,
            allow_high_precision_mv,
        )?;
    }

    Ok(())
}

/// `PredMv[ refList ]` derivation per the §5.11.26 `assign_mv` non-intrabc
/// inter arm (av1-spec p.77-78) — selects the motion-vector predictor a
/// chosen inter mode draws from the §7.10.2 `find_mv_stack` outputs,
/// feeding it to [`write_read_mv`] as the `pred_mv` argument.
///
/// Spec body (av1-spec p.77-78, the `else if`/`else` arms of `assign_mv`
/// reached when `use_intrabc == 0`):
/// ```text
///   compMode = get_mode( i )
///   if ( compMode == GLOBALMV ) {
///     PredMv[ i ] = GlobalMvs[ i ]
///   } else {
///     pos = ( compMode == NEARESTMV ) ? 0 : RefMvIdx
///     if ( compMode == NEWMV && NumMvFound <= 1 )
///       pos = 0
///     PredMv[ i ] = RefStackMv[ pos ][ i ]
///   }
/// ```
///
/// The `RefMvIdx` here is the §5.11.23 `inter_block_mode_info` running
/// index after the `drl_mode` loop (`RefMvIdx = 1 + (has_nearmv ? …)`)
/// — i.e. the same value the caller hands [`write_drl_mode`]. For a
/// `NEARESTMV` list the predictor is always the top stack slot; for
/// `NEWMV` it collapses to slot 0 whenever the stack is short
/// (`NumMvFound <= 1`).
///
/// The `GlobalMvs` / `RefStackMv` arrays come straight from a
/// [`FindMvStackResult`]; `ref_list` is `0` on single-prediction blocks
/// and `0`/`1` on compound blocks.
///
/// The per-list `compMode` is resolved through the shared
/// [`crate::cdf::get_mode`] §get_mode mapping (the same decode-side
/// helper the reader uses), so encode and decode agree on the mode
/// decomposition by construction.
///
/// Caller-bug rejects (`Err(PartitionWalkOutOfRange)`):
/// * `y_mode` outside `MODE_NEARESTMV..=MODE_NEW_NEWMV` or `ref_list > 1`.
/// * a `RefStackMv` index (`pos`) at or beyond `NumMvFound` — the chosen
///   `RefMvIdx` is unreachable at the derived stack depth, so reading
///   `RefStackMv[ pos ]` would consume an undefined slot.
pub fn assign_mv_pred_mv(
    mv_stack: &FindMvStackResult,
    y_mode: u8,
    ref_list: u8,
    ref_mv_idx: u32,
) -> Result<[i32; 2], Error> {
    if !(MODE_NEARESTMV..=MODE_NEW_NEWMV).contains(&y_mode) || ref_list > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let comp_mode = crate::cdf::get_mode(y_mode, ref_list as usize);
    if comp_mode == MODE_GLOBALMV {
        return Ok(mv_stack.global_mvs[ref_list as usize]);
    }
    let mut pos: u32 = if comp_mode == MODE_NEARESTMV {
        0
    } else {
        ref_mv_idx
    };
    if comp_mode == MODE_NEWMV && mv_stack.num_mv_found <= 1 {
        pos = 0;
    }
    // §5.11.26: `RefStackMv[ pos ]` must be a populated slot. Slots 0
    // and 1 are ALWAYS populated: the §7.10.2.12 extra-search pads
    // `RefStackMv[ idx ]` for `idx = NumMvFound..2` with the global MV
    // ("for single prediction, NumMvFound is not incremented by the
    // addition of global motion candidates" — so `NumMvFound` may
    // remain 0 while both head slots carry readable values, and a
    // first-block NEWMV / NEARMV legitimately predicts from them;
    // r411 fix — the previous `pos >= NumMvFound` reject refused every
    // empty-neighbourhood NEWMV leaf). A `pos >= 2` is reachable only
    // through the §5.11.23 `drl_mode` loop, which requires
    // `NumMvFound > pos` — beyond that the slot is undefined; reject.
    if (pos >= 2 && pos >= mv_stack.num_mv_found) || pos as usize >= MAX_REF_MV_STACK_SIZE {
        return Err(Error::PartitionWalkOutOfRange);
    }
    Ok(mv_stack.ref_stack_mv[pos as usize][ref_list as usize])
}

/// `PredMv[ 0 ]` derivation per the §5.11.26 `assign_mv`
/// **intra-block-copy** arm (av1-spec p.77-78) — the predictor the
/// §5.11.7 `intra_frame_mode_info( )` `use_intrabc` path feeds to
/// `read_mv( 0 )`. On this arm `compMode` is forced to `NEWMV`, so an
/// MV difference is *always* coded: the caller passes the returned
/// predictor to [`write_read_mv`] with `use_intrabc = true` (selecting
/// `MvCtx = MV_INTRABC_CONTEXT`), `force_integer_mv = true` and
/// `allow_high_precision_mv = false` (intra block copy is
/// integer-only per §5.11.32).
///
/// Spec body (av1-spec p.77-78, the `if ( use_intrabc )` arm of
/// `assign_mv`):
/// ```text
///   PredMv[ 0 ] = RefStackMv[ 0 ][ 0 ]
///   if ( PredMv[ 0 ][ 0 ] == 0 && PredMv[ 0 ][ 1 ] == 0 ) {
///       PredMv[ 0 ] = RefStackMv[ 1 ][ 0 ]
///   }
///   if ( PredMv[ 0 ][ 0 ] == 0 && PredMv[ 0 ][ 1 ] == 0 ) {
///       sbSize = use_128x128_superblock ? BLOCK_128X128 : BLOCK_64X64
///       sbSize4 = Num_4x4_Blocks_High[ sbSize ]
///       if ( MiRow - sbSize4 < MiRowStart ) {
///           PredMv[ 0 ][ 0 ] = 0
///           PredMv[ 0 ][ 1 ] = -(sbSize4 * MI_SIZE + INTRABC_DELAY_PIXELS) * 8
///       } else {
///           PredMv[ 0 ][ 0 ] = -(sbSize4 * MI_SIZE * 8)
///           PredMv[ 0 ][ 1 ] = 0
///       }
///   }
/// ```
///
/// Unlike the inter arm ([`assign_mv_pred_mv`]), no `NumMvFound` bound
/// applies to the two `RefStackMv` reads: the §7.10.2.12 extra-search
/// single-prediction tail pads `RefStackMv[ idx ][ 0 ]` for `idx <
/// 2` with `GlobalMvs[ 0 ]` **without** incrementing `NumMvFound`
/// ("for single prediction, NumMvFound is not incremented by the
/// addition of global motion candidates"), so both slots are always
/// written before `assign_mv` reads them — and on an intra-block-copy
/// block `GlobalMvs[ 0 ]` is the zero vector (§7.10.2.1: `ref ==
/// INTRA_FRAME ⇒ mv = 0`), which is exactly what the zero-fallback
/// chain absorbs.
///
/// The zero-MV fallback points at the most recent column of
/// already-reconstructed superblocks: one superblock up
/// (`-(sbSize4 * MI_SIZE * 8)` in row component) when the block is not
/// in the tile's top superblock row, else `sbSize4 * MI_SIZE +
/// INTRABC_DELAY_PIXELS` luma samples to the left (the §3
/// `INTRABC_DELAY_PIXELS = 256` wave-front delay) in column component.
/// Components are `[ row, col ]` in 1/8-luma-sample units throughout
/// (`Mv[ ][ 0 ]` = vertical, `Mv[ ][ 1 ]` = horizontal).
///
/// `mi_row` / `mi_row_start` are the block's mode-info row and the
/// enclosing tile's `MiRowStart` (§6.10.4). A `mi_row < mi_row_start`
/// pair is rejected as a caller bug (`Err(PartitionWalkOutOfRange)`)
/// — a block cannot sit above its own tile.
pub fn assign_mv_pred_mv_intrabc(
    mv_stack: &FindMvStackResult,
    use_128x128_superblock: bool,
    mi_row: u32,
    mi_row_start: u32,
) -> Result<[i32; 2], Error> {
    if mi_row < mi_row_start {
        return Err(Error::PartitionWalkOutOfRange);
    }
    let mut pred_mv = mv_stack.ref_stack_mv[0][0];
    if pred_mv == [0, 0] {
        pred_mv = mv_stack.ref_stack_mv[1][0];
    }
    if pred_mv == [0, 0] {
        let sb_size = if use_128x128_superblock {
            BLOCK_128X128
        } else {
            BLOCK_64X64
        };
        let sb_size4 = NUM_4X4_BLOCKS_HIGH[sb_size] as u32;
        // `MiRow - sbSize4 < MiRowStart` on spec (unbounded) integers —
        // rearranged as `MiRow < MiRowStart + sbSize4` so the unsigned
        // subtraction cannot wrap.
        if mi_row < mi_row_start + sb_size4 {
            pred_mv = [
                0,
                -((sb_size4 as i32 * MI_SIZE as i32 + INTRABC_DELAY_PIXELS as i32) * 8),
            ];
        } else {
            pred_mv = [-(sb_size4 as i32 * MI_SIZE as i32 * 8), 0];
        }
    }
    Ok(pred_mv)
}

/// `use_intrabc` S() inverse per §5.11.7 (av1-spec p.65) — r279. The
/// exact encode-side twin of
/// [`crate::cdf::PartitionWalker::decode_use_intrabc`].
///
/// Spec body (av1-spec p.65):
/// ```text
///   if ( allow_intrabc ) {
///       use_intrabc                                          S()
///   } else {
///       use_intrabc = 0
///   }
/// ```
///
/// * When `allow_intrabc == true`: emits one §8.2.6 `S()` over
///   `TileIntrabcCdf` (the §8.3.2 selection is contextless — "the cdf
///   for use_intrabc is given by TileIntrabcCdf", no `[ctx]`
///   subscript).
/// * When `allow_intrabc == false`: the reader's fall-through assigns
///   `use_intrabc = 0` with no bit consumed, so no symbol is emitted.
///   `use_intrabc` MUST be `0` — caller bug otherwise
///   ([`Error::PartitionWalkOutOfRange`]), as the reader could never
///   reconstruct a `1`.
///
/// `allow_intrabc` is the §5.9.20 frame-header bit (itself reachable
/// only on intra frames whose §5.9.5 `allow_screen_content_tools` is
/// set); `use_intrabc` is the §3 binary alphabet value (`> 1` is
/// rejected as a caller bug).
pub fn write_use_intrabc(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    use_intrabc: u8,
    allow_intrabc: bool,
) -> Result<(), Error> {
    if use_intrabc > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if allow_intrabc {
        let row = cdfs.intrabc_cdf();
        writer.write_symbol(u32::from(use_intrabc), row)?;
    } else if use_intrabc != 0 {
        // §5.11.7 fall-through arm: the reader forces `use_intrabc = 0`
        // without a bit read; a `1` is unreachable.
        return Err(Error::PartitionWalkOutOfRange);
    }
    Ok(())
}

/// Per-list inputs to the §5.11.7 `use_intrabc` arm of
/// [`write_intra_frame_intrabc_arm`] — the target block vector plus the
/// §7.10.2 `find_mv_stack( 0 )` outputs and tile/superblock scalars the
/// §5.11.26 intrabc `PredMv` derivation
/// ([`assign_mv_pred_mv_intrabc`]) consumes.
#[derive(Debug, Clone, Copy)]
pub struct IntrabcArmInputs<'a> {
    /// The target `Mv[ 0 ]` as `[ row, col ]` in 1/8-luma-sample
    /// units. Intra block copy is integer-only, so each component's
    /// difference against the derived `PredMv[ 0 ]` must be a multiple
    /// of 8 (rejected by the §5.11.32 component writer otherwise).
    pub mv: [i32; 2],
    /// §7.10.2 `find_mv_stack( 0 )` outputs (`RefStackMv` slots 0/1 are
    /// the only fields the intrabc arm reads; the §7.10.2.12
    /// single-pred tail guarantees both are written).
    pub mv_stack: &'a FindMvStackResult,
    /// §5.5.2 sequence-header `use_128x128_superblock` bit (selects
    /// `sbSize4` in the §5.11.26 zero-MV fallback).
    pub use_128x128_superblock: bool,
    /// The block's `MiRow`.
    pub mi_row: u32,
    /// The enclosing tile's `MiRowStart` (§6.10.4).
    pub mi_row_start: u32,
}

/// Fixed §5.11.7 `use_intrabc`-arm assignments (av1-spec p.65) — the
/// no-bit mode-info state [`write_intra_frame_intrabc_arm`] returns so
/// the caller can stamp the §5.11.5 neighbour grids exactly as the
/// reader would:
///
/// ```text
///   is_inter = 1
///   YMode = DC_PRED
///   UVMode = DC_PRED
///   motion_mode = SIMPLE
///   compound_type = COMPOUND_AVERAGE
///   PaletteSizeY = 0
///   PaletteSizeUV = 0
///   interp_filter[ 0 ] = BILINEAR
///   interp_filter[ 1 ] = BILINEAR
/// ```
///
/// (`RefFrame[ 0 ] = INTRA_FRAME` / `RefFrame[ 1 ] = NONE` are set by
/// the §5.11.7 body *before* the `use_intrabc` read, on both arms, so
/// they are not part of this readout.) `mv` / `pred_mv` carry the
/// coded `Mv[ 0 ]` and the §5.11.26-derived `PredMv[ 0 ]`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IntrabcBlockInfo {
    /// §5.11.7 `is_inter = 1`.
    pub is_inter: u8,
    /// §5.11.7 `YMode = DC_PRED`.
    pub y_mode: u8,
    /// §5.11.7 `UVMode = DC_PRED`.
    pub uv_mode: u8,
    /// §5.11.7 `motion_mode = SIMPLE`.
    pub motion_mode: u8,
    /// §5.11.7 `compound_type = COMPOUND_AVERAGE`.
    pub compound_type: u8,
    /// §5.11.7 `PaletteSizeY = 0`.
    pub palette_size_y: u8,
    /// §5.11.7 `PaletteSizeUV = 0`.
    pub palette_size_uv: u8,
    /// §5.11.7 `interp_filter[ 0..2 ] = BILINEAR`.
    pub interp_filter: [u8; 2],
    /// The coded `Mv[ 0 ]` (`[ row, col ]`, 1/8-luma-sample units).
    pub mv: [i32; 2],
    /// The §5.11.26 `PredMv[ 0 ]` the MV difference was coded against.
    pub pred_mv: [i32; 2],
}

/// §5.11.7 `use_intrabc` region writer (av1-spec p.65) — r279.
/// Composes, in spec order, the write side of:
///
/// ```text
///   if ( allow_intrabc ) {
///       use_intrabc                                          S()
///   } else {
///       use_intrabc = 0
///   }
///   if ( use_intrabc ) {
///       is_inter = 1
///       YMode = DC_PRED
///       UVMode = DC_PRED
///       motion_mode = SIMPLE
///       compound_type = COMPOUND_AVERAGE
///       PaletteSizeY = 0
///       PaletteSizeUV = 0
///       interp_filter[ 0 ] = BILINEAR
///       interp_filter[ 1 ] = BILINEAR
///       find_mv_stack( 0 )
///       assign_mv( 0 )
///   }
/// ```
///
/// `use_intrabc` is derived from `intrabc.is_some()` and emitted via
/// [`write_use_intrabc`]. On the `Some` arm the §5.11.26
/// `assign_mv( 0 )` body forces `compMode = NEWMV`, so an MV
/// difference is *always* coded: `PredMv[ 0 ]` is derived via
/// [`assign_mv_pred_mv_intrabc`] from the caller-supplied §7.10.2
/// `find_mv_stack( 0 )` outputs, then [`write_read_mv`] emits the
/// difference under the intra-block-copy MV regime (`MvCtx =
/// MV_INTRABC_CONTEXT`, `force_integer_mv = 1` — §5.9.2 forces it on
/// every intra frame, and `allow_intrabc` requires an intra frame —
/// hence `allow_high_precision_mv = 0`).
///
/// Returns `Some(IntrabcBlockInfo)` carrying the arm's fixed no-bit
/// assignments (for §5.11.5 grid stamping) when the intrabc arm fired,
/// `None` when `use_intrabc == 0` (the caller continues with the
/// §5.11.7 `else` arm — `intra_frame_y_mode` etc.).
///
/// Caller-bug rejects ([`Error::PartitionWalkOutOfRange`]):
/// * `intrabc.is_some()` with `allow_intrabc == false` (the reader's
///   fall-through forces `use_intrabc = 0`).
/// * `mi_row < mi_row_start` (via [`assign_mv_pred_mv_intrabc`]).
/// * An `mv` whose difference against the derived predictor is not
///   integer-aligned (via the §5.11.32 component writer's
///   `force_integer_mv` check).
pub fn write_intra_frame_intrabc_arm(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    allow_intrabc: bool,
    intrabc: Option<&IntrabcArmInputs<'_>>,
) -> Result<Option<IntrabcBlockInfo>, Error> {
    let use_intrabc = u8::from(intrabc.is_some());
    write_use_intrabc(writer, cdfs, use_intrabc, allow_intrabc)?;
    let Some(inputs) = intrabc else {
        return Ok(None);
    };

    // §5.11.26 `assign_mv( 0 )` intrabc arm: `compMode = NEWMV` (forced),
    // so `read_mv( 0 )` always fires — derive `PredMv[ 0 ]` and emit
    // the difference.
    let pred_mv = assign_mv_pred_mv_intrabc(
        inputs.mv_stack,
        inputs.use_128x128_superblock,
        inputs.mi_row,
        inputs.mi_row_start,
    )?;
    write_read_mv(
        writer, cdfs, inputs.mv, pred_mv, /* use_intrabc = */ true,
        /* force_integer_mv = */ true, /* allow_high_precision_mv = */ false,
    )?;

    Ok(Some(IntrabcBlockInfo {
        is_inter: 1,
        y_mode: DC_PRED as u8,
        uv_mode: DC_PRED as u8,
        motion_mode: MOTION_MODE_SIMPLE,
        compound_type: COMPOUND_AVERAGE,
        palette_size_y: 0,
        palette_size_uv: 0,
        interp_filter: [BILINEAR, BILINEAR],
        mv: inputs.mv,
        pred_mv,
    }))
}

/// §5.11.23 tail inputs for [`write_inter_block_mode_info`] — r277.
/// Groups the readouts + gating scalars the four post-`assign_mv( )`
/// leaf writers consume ([`write_interintra_mode`] §5.11.28,
/// [`write_motion_mode`] §5.11.27, [`write_compound_type`] §5.11.29,
/// [`write_interpolation_filter`] §5.11.x), so the composition's
/// parameter surface stays readable. Every field maps 1:1 onto a leaf
/// writer's input of the same name; see the leaf docs for the spec
/// semantics and reject conditions.
///
/// The two walker-derived §5.11.27 quantities (`has_overlappable` —
/// §7.10.3 `has_overlappable_candidates( )` — and `num_samples` —
/// §7.10.4 `find_warp_samples( )` `NumSamples`) arrive precomputed
/// per the stateless-writer doctrine of this module; the §8.3.2
/// neighbour grid reads (`above_comp_group_idx` / `left_comp_group_idx`
/// — identity `0` when unavailable — `above_compound_idx` /
/// `left_compound_idx` — identity `1` — and `above_interp_filters` /
/// `left_interp_filters`) likewise.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InterBlockModeInfoTail {
    /// §5.11.28 target [`InterIntraReadout`].
    pub interintra: InterIntraReadout,
    /// §5.5.2 sequence-header `enable_interintra_compound` bit.
    pub enable_interintra_compound: bool,
    /// §5.11.27 target `motion_mode` ordinal.
    pub motion_mode: u8,
    /// §7.10.4 `NumSamples` from `find_warp_samples( )`.
    pub num_samples: u32,
    /// §5.9.2 frame-header `is_motion_mode_switchable` bit.
    pub is_motion_mode_switchable: bool,
    /// §5.9.2 frame-header `allow_warped_motion` bit.
    pub allow_warped_motion: bool,
    /// §5.11.27 `is_scaled( refFrame )` per `refFrame - LAST_FRAME`.
    pub is_scaled_per_ref: [bool; 7],
    /// §7.10.3 `has_overlappable_candidates( )` outcome.
    pub has_overlappable: bool,
    /// §5.11.29 target [`CompoundTypeReadout`].
    pub compound_type: CompoundTypeReadout,
    /// §5.5.2 sequence-header `enable_masked_compound` bit.
    pub enable_masked_compound: bool,
    /// §5.5.2 sequence-header `enable_jnt_comp` bit.
    pub enable_jnt_comp: bool,
    /// §7.8.1 equal-relative-distance outcome seeding the §8.3.2
    /// `compound_idx` ctx.
    pub dist_equal: bool,
    /// §8.3.2 `CompGroupIdxs[ MiRow - 1 ][ MiCol ]` (identity `0`).
    pub above_comp_group_idx: u8,
    /// §8.3.2 `CompGroupIdxs[ MiRow ][ MiCol - 1 ]` (identity `0`).
    pub left_comp_group_idx: u8,
    /// §8.3.2 `CompoundIdxs[ MiRow - 1 ][ MiCol ]` (identity `1`).
    pub above_compound_idx: u8,
    /// §8.3.2 `CompoundIdxs[ MiRow ][ MiCol - 1 ]` (identity `1`).
    pub left_compound_idx: u8,
    /// §5.11.x target [`InterpolationFilterReadout`].
    pub interp_filter: InterpolationFilterReadout,
    /// §5.9.10 frame-header `interpolation_filter` in
    /// `EIGHTTAP..=SWITCHABLE = 0..=4`.
    pub interpolation_filter: u8,
    /// §5.5.2 sequence-header `enable_dual_filter` bit.
    pub enable_dual_filter: bool,
    /// §8.3.2 `InterpFilters[ MiRow - 1 ][ MiCol ][ 0..2 ]`.
    pub above_interp_filters: [u8; 2],
    /// §8.3.2 `InterpFilters[ MiRow ][ MiCol - 1 ][ 0..2 ]`.
    pub left_interp_filters: [u8; 2],
    /// §5.9.24 `GmType[ 0..8 ]` — shared by the §5.11.27 GLOBALMV
    /// short-circuit and the §5.11.x `needs_interp_filter( )` gate.
    pub gm_type: [i32; 8],
}

impl InterBlockModeInfoTail {
    /// The everything-gated-shut tail: every §5.11.23 tail leaf is
    /// bit-silent on this configuration regardless of the head's arm.
    ///
    /// * §5.11.28 gate closed (`enable_interintra_compound == false`)
    ///   ⇒ `interintra = 0`, no bits;
    /// * §5.11.27 short-circuit (`is_motion_mode_switchable == false`)
    ///   ⇒ `motion_mode = SIMPLE`, no bits;
    /// * §5.11.29 with `enable_masked_compound == false` AND
    ///   `enable_jnt_comp == false` ⇒ the line-1/2 pre-sets survive on
    ///   every arm (`comp_group_idx = 0`, `compound_idx = 1`,
    ///   `compound_type = COMPOUND_AVERAGE`), no bits;
    /// * §5.11.x with `interpolation_filter = EIGHTTAP` (non-
    ///   SWITCHABLE) ⇒ both slots forced to `EIGHTTAP`, no bits.
    ///
    /// Useful as a baseline the caller patches per block, and as the
    /// regression fixture proving the composed writer's head bits are
    /// unchanged by the r277 tail fold.
    pub fn bit_silent() -> Self {
        Self {
            interintra: InterIntraReadout {
                interintra: 0,
                interintra_mode: None,
                wedge_interintra: None,
                wedge_index: None,
            },
            enable_interintra_compound: false,
            motion_mode: MOTION_MODE_SIMPLE,
            num_samples: 0,
            is_motion_mode_switchable: false,
            allow_warped_motion: false,
            is_scaled_per_ref: [false; 7],
            has_overlappable: false,
            compound_type: CompoundTypeReadout {
                comp_group_idx: 0,
                compound_idx: 1,
                compound_type: COMPOUND_AVERAGE,
                wedge_index: None,
                wedge_sign: None,
                mask_type: None,
            },
            enable_masked_compound: false,
            enable_jnt_comp: false,
            dist_equal: false,
            above_comp_group_idx: 0,
            left_comp_group_idx: 0,
            above_compound_idx: 1,
            left_compound_idx: 1,
            interp_filter: InterpolationFilterReadout {
                interp_filter: [EIGHTTAP, EIGHTTAP],
                read_from_bitstream: [false, false],
            },
            interpolation_filter: EIGHTTAP,
            enable_dual_filter: false,
            above_interp_filters: [EIGHTTAP, EIGHTTAP],
            left_interp_filters: [EIGHTTAP, EIGHTTAP],
            gm_type: [0; 8],
        }
    }
}

impl Default for InterBlockModeInfoTail {
    fn default() -> Self {
        Self::bit_silent()
    }
}

/// `inter_block_mode_info( )` composition writer per §5.11.23 (av1-spec
/// p.73-75) — r275, tail folded r277. Folds the r266-r274 leaf writers
/// ([`write_ref_frames`], [`write_compound_mode`] /
/// [`write_inter_single_mode`], [`write_drl_mode`], [`assign_mv_pred_mv`]
/// and [`write_read_mv`]) into the single §5.11.23 body the
/// `inter_frame_mode_info` chain reaches on an inter block. It is the
/// exact encode-side inverse of the decode-side
/// [`crate::cdf::PartitionWalker::decode_inter_block_mode_info`] from
/// `read_ref_frames( )` through the closing interpolation-filter loop.
///
/// ## Scope
///
/// Covers the full §5.11.23 body:
///
/// 1. `read_ref_frames( )` (§5.11.25) via [`write_ref_frames`].
/// 2. `isCompound = RefFrame[ 1 ] > INTRA_FRAME`.
/// 3. `find_mv_stack( isCompound )` (§7.10.2) — **not** re-run here; the
///    encoder hands the already-computed [`FindMvStackResult`] (the same
///    aggregate the decoder builds internally), carrying every §8.3.2
///    ctx the mode / drl / mv writers consume (`NewMvContext`,
///    `ZeroMvContext`, `RefMvContext`, `NumMvFound`, `DrlCtxStack`,
///    `RefStackMv`, `GlobalMvs`).
/// 4. The four-arm `YMode` dispatch: arm 1 (`skip_mode`) and arm 2
///    (`SEG_LVL_SKIP` / `SEG_LVL_GLOBALMV`) emit no mode symbol; arm 3
///    (`isCompound`) emits `compound_mode` via [`write_compound_mode`]
///    over the internally-derived [`crate::cdf::compound_mode_ctx`]; arm
///    4 (single-pred) emits the `new_mv` / `zero_mv` / `ref_mv` cascade
///    via [`write_inter_single_mode`] over the stack's three contexts.
/// 5. The `RefMvIdx` `drl_mode` loop via [`write_drl_mode`].
/// 6. `assign_mv( isCompound )`: for each reference list
///    `i in 0..1 + isCompound`, resolve `compMode = get_mode( YMode, i )`
///    and, **only when `compMode == NEWMV`**, derive `PredMv[ i ]` via
///    [`assign_mv_pred_mv`] and emit the MV difference via
///    [`write_read_mv`]. Non-NEWMV lists set `Mv[ i ] = PredMv[ i ]`
///    with no bits (§5.11.31 line `else Mv[ i ] = PredMv[ i ]`).
/// 7. The §5.11.23 tail in **spec order** (folded r277):
///    `read_interintra_mode( )` via [`write_interintra_mode`]
///    (§5.11.28), then `read_motion_mode( )` via [`write_motion_mode`]
///    (§5.11.27), then `read_compound_type( )` via
///    [`write_compound_type`] (§5.11.29), then the interpolation-
///    filter loop via [`write_interpolation_filter`] (§5.11.x). The
///    §5.11.28 inner-arm `RefFrame[ 1 ] = INTRA_FRAME` override is
///    applied between the first two — it is the §5.11.27 short-circuit
///    that silences `motion_mode` on interintra blocks, and the
///    §5.11.x writer also observes the post-override pair. Tail inputs
///    arrive grouped in [`InterBlockModeInfoTail`];
///    [`InterBlockModeInfoTail::bit_silent`] reproduces the pre-r277
///    head-only bit pattern (every tail leaf gated shut).
///
/// ## `YMode` consistency
///
/// `YMode` is caller-chosen but cross-checked against the active arm:
/// arm 1 forces `NEAREST_NEARESTMV`, arm 2 forces `GLOBALMV`, arm 3
/// admits only the eight compound modes
/// (`NEAREST_NEARESTMV..=NEW_NEWMV`), arm 4 only the four single-pred
/// modes (`NEWMV` / `GLOBALMV` / `NEARESTMV` / `NEARMV`). A `YMode`
/// inconsistent with the arm is a caller bug — the reader could never
/// produce it on that arm.
///
/// ## Inputs
///
/// * `ref_frame` — target `( RefFrame[ 0 ], RefFrame[ 1 ] )`; drives
///   `isCompound` and the §5.11.25 reference cascade.
/// * `y_mode` — the §5.11.23 `YMode` chosen by the encoder.
/// * `mv` — `Mv[ list ][ comp ]` for each active list; only the NEWMV
///   lists' values are consulted (the others are derived predictors).
/// * `ref_mv_idx` — the `RefMvIdx` the encoder selected, threaded to
///   both [`write_drl_mode`] and [`assign_mv_pred_mv`].
/// * `mv_stack` — the §7.10.2 [`FindMvStackResult`].
/// * `mi_size` — `MiSize` (§9.3 table index for the `comp_mode`
///   precondition).
/// * the arm-selection sextet + `reference_select` of
///   [`write_ref_frames`] / [`write_inter_block_mode_info_bootstrap`].
/// * the §5.11.18 neighbour-state octet from which [`write_ref_frames`]
///   derives every §8.3.2 reference ctx internally.
/// * `force_integer_mv` / `allow_high_precision_mv` — frame-level MV
///   precision flags forwarded to [`write_read_mv`].
///
/// ## Out-of-range / mismatch cases (`Err(PartitionWalkOutOfRange)`)
///
/// All of the composed writers' rejects, plus a `y_mode` inconsistent
/// with the active arm.
///
/// ## §5.11.5 grid-fill is on the caller
///
/// Stateless like every piece it composes. The caller stamps the
/// resulting `RefFrame` / `YMode` / `Mv` onto the block footprint
/// through its own [`crate::cdf::PartitionWalker`] so subsequent blocks'
/// §8.3.2 neighbour walks observe them.
/// r422 — the §5.11.23 **mode + MV prefix**: `read_ref_frames( )`
/// (§5.11.25 cascade), the four-arm `YMode` dispatch, the `RefMvIdx`
/// `drl_mode` loop and the `assign_mv( )` NEWMV differences — i.e.
/// everything [`write_inter_block_mode_info`] emits BEFORE the
/// §5.11.28/§5.11.27/§5.11.29/interp-filter tail. Factored out so the
/// search-side rate twin can price one `(RefFrame, YMode, Mv,
/// RefMvIdx)` candidate through the writer's own emission path
/// against a counting [`SymbolWriter`]; the emitting pass calls it
/// through [`write_inter_block_mode_info`], so pricing and committing
/// share one body by construction.
#[allow(clippy::too_many_arguments)]
pub fn write_inter_mode_mv_prefix(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    ref_frame: [i32; 2],
    y_mode: u8,
    mv: [[i32; 2]; 2],
    ref_mv_idx: u32,
    mv_stack: &FindMvStackResult,
    mi_size: usize,
    skip_mode: u8,
    skip_mode_frame: [i8; 2],
    seg_ref_frame_active: bool,
    seg_ref_frame_data: i8,
    seg_skip_active: bool,
    seg_globalmv_active: bool,
    reference_select: bool,
    avail_u: bool,
    avail_l: bool,
    above_single: bool,
    left_single: bool,
    above_intra: bool,
    left_intra: bool,
    above_ref_frame: [i32; 2],
    left_ref_frame: [i32; 2],
    force_integer_mv: bool,
    allow_high_precision_mv: bool,
) -> Result<(), Error> {
    // §5.11.23 line 4618: `read_ref_frames( )`. Drives the §5.11.25
    // four-arm reference cascade (zero to six S() depending on arm /
    // leaf) and verifies the target pair is reachable.
    write_ref_frames(
        writer,
        cdfs,
        ref_frame,
        mi_size,
        skip_mode,
        skip_mode_frame,
        seg_ref_frame_active,
        seg_ref_frame_data,
        seg_skip_active,
        seg_globalmv_active,
        reference_select,
        avail_u,
        avail_l,
        above_single,
        left_single,
        above_intra,
        left_intra,
        above_ref_frame,
        left_ref_frame,
    )?;

    // §5.11.23 line 4619: `isCompound = RefFrame[ 1 ] > INTRA_FRAME`.
    // INTRA_FRAME = 0; NONE = -1. Any slot-1 in LAST_FRAME..=ALTREF_FRAME
    // (1..=7) makes the block compound.
    let is_compound = ref_frame[1] > 0;

    // §5.11.23 lines 4621-4642: the four-arm `YMode` dispatch. The arm
    // is selected exactly as the reader selects it (spec order: skip_mode
    // first, then segmentation, then compound, then single-pred). The
    // caller-supplied `y_mode` is cross-checked against the active arm —
    // the reader could never emit a different mode on that arm.
    if skip_mode != 0 {
        // Arm 1: no S(); `YMode` is forced to NEAREST_NEARESTMV.
        if y_mode != MODE_NEAREST_NEARESTMV {
            return Err(Error::PartitionWalkOutOfRange);
        }
    } else if seg_skip_active || seg_globalmv_active {
        // Arm 2: no S(); `YMode` is forced to GLOBALMV.
        if y_mode != MODE_GLOBALMV {
            return Err(Error::PartitionWalkOutOfRange);
        }
    } else if is_compound {
        // Arm 3: `compound_mode` S() over `TileCompoundModeCdf[ ctx ]`,
        // then `YMode = NEAREST_NEARESTMV + compound_mode`. The §8.3.2
        // ctx is derived internally from the stack's RefMvContext /
        // NewMvContext (the same `compound_mode_ctx` mapping the reader
        // applies), so encode and decode agree by construction.
        let cm_ctx = compound_mode_ctx(
            mv_stack.ref_mv_context as usize,
            mv_stack.new_mv_context as usize,
        );
        write_compound_mode(writer, cdfs, y_mode, cm_ctx)?;
    } else {
        // Arm 4: single-pred `new_mv` / `zero_mv` / `ref_mv` cascade
        // over the stack's three contexts (§7.10.2.14).
        write_inter_single_mode(
            writer,
            cdfs,
            y_mode,
            mv_stack.new_mv_context as usize,
            mv_stack.zero_mv_context as usize,
            mv_stack.ref_mv_context as usize,
        )?;
    }

    // §5.11.23 lines 4643-4674: `RefMvIdx` `drl_mode` loop. A silent
    // no-op for every mode outside the NEWMV / NEW_NEWMV and
    // `has_nearmv( )` arms (matching the reader's `RefMvIdx = 0`).
    write_drl_mode(
        writer,
        cdfs,
        y_mode,
        ref_mv_idx,
        mv_stack.num_mv_found,
        &mv_stack.drl_ctx_stack,
    )?;

    // §5.11.23 line 4675: `assign_mv( isCompound )` per §5.11.31. The
    // inter path always has `use_intrabc == 0`, so for each reference
    // list the §5.11.31 body resolves `compMode = get_mode( i )` and
    // reads an MV difference only on the NEWMV lists; the others inherit
    // `PredMv[ i ]` with no bits emitted.
    let list_count: u8 = if is_compound { 2 } else { 1 };
    for i in 0..list_count {
        if crate::cdf::get_mode(y_mode, i as usize) == MODE_NEWMV {
            // §5.11.31: `if ( compMode == NEWMV ) read_mv( i )`. Derive
            // `PredMv[ i ]` from the stack and emit the difference.
            let pred_mv = assign_mv_pred_mv(mv_stack, y_mode, i, ref_mv_idx)?;
            write_read_mv(
                writer,
                cdfs,
                mv[i as usize],
                pred_mv,
                /* use_intrabc = */ false,
                force_integer_mv,
                allow_high_precision_mv,
            )?;
        }
        // Non-NEWMV lists: `Mv[ i ] = PredMv[ i ]`, no symbols.
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub fn write_inter_block_mode_info(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    ref_frame: [i32; 2],
    y_mode: u8,
    mv: [[i32; 2]; 2],
    ref_mv_idx: u32,
    mv_stack: &FindMvStackResult,
    mi_size: usize,
    skip_mode: u8,
    skip_mode_frame: [i8; 2],
    seg_ref_frame_active: bool,
    seg_ref_frame_data: i8,
    seg_skip_active: bool,
    seg_globalmv_active: bool,
    reference_select: bool,
    avail_u: bool,
    avail_l: bool,
    above_single: bool,
    left_single: bool,
    above_intra: bool,
    left_intra: bool,
    above_ref_frame: [i32; 2],
    left_ref_frame: [i32; 2],
    force_integer_mv: bool,
    allow_high_precision_mv: bool,
    tail: &InterBlockModeInfoTail,
) -> Result<(), Error> {
    // §3 binary alphabet bound on `skip_mode` (mirrors the bootstrap).
    if skip_mode > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.23 head — the reference cascade, the four-arm `YMode`
    // dispatch, the `drl_mode` loop and the `assign_mv( )` MV
    // differences, factored (r422) into [`write_inter_mode_mv_prefix`]
    // so the search-side rate twin prices mode candidates through the
    // EXACT emission path (single source of truth — the twin never
    // re-implements the cascade).
    write_inter_mode_mv_prefix(
        writer,
        cdfs,
        ref_frame,
        y_mode,
        mv,
        ref_mv_idx,
        mv_stack,
        mi_size,
        skip_mode,
        skip_mode_frame,
        seg_ref_frame_active,
        seg_ref_frame_data,
        seg_skip_active,
        seg_globalmv_active,
        reference_select,
        avail_u,
        avail_l,
        above_single,
        left_single,
        above_intra,
        left_intra,
        above_ref_frame,
        left_ref_frame,
        force_integer_mv,
        allow_high_precision_mv,
    )?;

    let is_compound = ref_frame[1] > 0;

    // ------- §5.11.23 tail (folded r277) — spec order: §5.11.28 →
    // §5.11.27 → §5.11.29 → §5.11.x interpolation-filter loop. -------

    // `read_interintra_mode( isCompound )` per §5.11.28.
    write_interintra_mode(
        writer,
        cdfs,
        &tail.interintra,
        mi_size,
        skip_mode,
        is_compound,
        tail.enable_interintra_compound,
    )?;

    // §5.11.28 inner-arm override: `RefFrame[ 1 ] = INTRA_FRAME = 0`.
    // The §5.11.27 / §5.11.x writers below observe the post-override
    // pair — slot-1 INTRA_FRAME is the §5.11.27 short-circuit that
    // silences `motion_mode` on interintra blocks (and the only way
    // slot 1 ever becomes INTRA_FRAME). `is_compound` is unaffected
    // (the §5.11.28 gate already required `!isCompound`).
    let ref_frame = if tail.interintra.interintra == 1 {
        [ref_frame[0], 0]
    } else {
        ref_frame
    };

    // `read_motion_mode( isCompound )` per §5.11.27.
    write_motion_mode(
        writer,
        cdfs,
        tail.motion_mode,
        mi_size,
        skip_mode,
        is_compound,
        ref_frame,
        y_mode,
        tail.num_samples,
        tail.is_motion_mode_switchable,
        tail.allow_warped_motion,
        force_integer_mv,
        tail.gm_type,
        tail.is_scaled_per_ref,
        tail.has_overlappable,
    )?;

    // `read_compound_type( isCompound )` per §5.11.29. The §8.3.2
    // `comp_group_idx` / `compound_idx` ctx walks consult
    // `AboveRefFrame[ 0 ] == ALTREF_FRAME` / `LeftRefFrame[ 0 ] ==
    // ALTREF_FRAME` on their `!AboveSingle` / `!LeftSingle` arms;
    // ALTREF_FRAME = 7 per §6.10.x.
    write_compound_type(
        writer,
        cdfs,
        &tail.compound_type,
        mi_size,
        skip_mode,
        is_compound,
        tail.interintra.interintra,
        tail.interintra.wedge_interintra.unwrap_or(0),
        tail.enable_masked_compound,
        tail.enable_jnt_comp,
        tail.dist_equal,
        avail_u,
        avail_l,
        above_single,
        left_single,
        above_ref_frame[0] == 7,
        left_ref_frame[0] == 7,
        tail.above_comp_group_idx,
        tail.left_comp_group_idx,
        tail.above_compound_idx,
        tail.left_compound_idx,
    )?;

    // The closing §5.11.x interpolation-filter loop.
    write_interpolation_filter(
        writer,
        cdfs,
        &tail.interp_filter,
        mi_size,
        skip_mode,
        is_compound,
        ref_frame,
        y_mode,
        tail.motion_mode,
        tail.interpolation_filter,
        tail.enable_dual_filter,
        tail.gm_type,
        avail_u,
        avail_l,
        above_ref_frame,
        left_ref_frame,
        tail.above_interp_filters,
        tail.left_interp_filters,
    )?;

    Ok(())
}

/// `read_interintra_mode( isCompound )` writer per §5.11.28 (av1-spec
/// p.79-80) — r276. First piece of the §5.11.23 tail after
/// `assign_mv( )` (the spec order is `read_interintra_mode` →
/// `read_motion_mode` → `read_compound_type`). Exact encode-side
/// inverse of [`crate::cdf::PartitionWalker::read_interintra_mode`]:
/// the caller hands the target [`InterIntraReadout`] (the same
/// aggregate the decoder returns) and the writer re-emits the S()
/// sequence the reader would consume to reproduce it.
///
/// ## Spec body (§5.11.28 — av1-spec p.79-80)
///
/// ```text
///   if ( !skip_mode && enable_interintra_compound && !isCompound &&
///        MiSize >= BLOCK_8X8 && MiSize <= BLOCK_32X32 ) {
///       interintra                                          S()
///       if ( interintra ) {
///           interintra_mode                                 S()
///           RefFrame[ 1 ] = INTRA_FRAME
///           AngleDeltaY = 0
///           AngleDeltaUV = 0
///           use_filter_intra = 0
///           wedge_interintra                                S()
///           if ( wedge_interintra ) {
///               wedge_index                                 S()
///               wedge_sign = 0
///           }
///       }
///   } else {
///       interintra = 0
///   }
/// ```
///
/// ## §8.3.2 CDF selections
///
/// * `interintra` / `interintra_mode`: `ctx = Size_Group[ MiSize ] - 1`
///   via [`interintra_ctx`] (the outer gate confines `MiSize` to
///   `BLOCK_8X8..=BLOCK_32X32` where `Size_Group ∈ {1, 2, 3}`).
/// * `wedge_interintra` / `wedge_index`: straight `MiSize` index.
///
/// ## Readout consistency
///
/// The target `readout` must be exactly producible by the reader on
/// this gate configuration:
///
/// * gate closed ⇒ `interintra == 0` with all three option fields
///   `None` (the spec's `else interintra = 0` arm reads no bits);
/// * gate open, `interintra == 0` ⇒ all three option fields `None`;
/// * gate open, `interintra == 1` ⇒ `interintra_mode ==
///   Some(< INTERINTRA_MODES)` and `wedge_interintra ∈ {Some(0),
///   Some(1)}`; `wedge_index == Some(< WEDGE_TYPES)` iff
///   `wedge_interintra == Some(1)`, `None` otherwise.
///
/// Any other shape is a caller bug surfaced as
/// `Err(PartitionWalkOutOfRange)` (the reader could never decode it).
///
/// ## §5.11.5 grid-fill is on the caller
///
/// Stateless: between zero and four §8.2.6 `S()` symbols. The spec
/// body's imperative overrides (`RefFrame[ 1 ] = INTRA_FRAME`,
/// `AngleDeltaY = AngleDeltaUV = 0`, `use_filter_intra = 0`,
/// `wedge_sign = 0`) are derivations the caller applies to its own
/// state, exactly as the decode-side dispatcher applies them from the
/// returned readout.
pub fn write_interintra_mode(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    readout: &InterIntraReadout,
    mi_size: usize,
    skip_mode: u8,
    is_compound: bool,
    enable_interintra_compound: bool,
) -> Result<(), Error> {
    // §3 binary alphabet bound on `skip_mode`.
    if skip_mode > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.28 outer gate: every clause must hold for the `interintra`
    // S() to fire (mirrors the reader's `gate_open`).
    let gate_open = skip_mode == 0
        && enable_interintra_compound
        && !is_compound
        && (BLOCK_8X8..=BLOCK_32X32).contains(&mi_size);

    if !gate_open {
        // `else interintra = 0` — no bits; the readout must carry the
        // exact no-read shape.
        if readout.interintra != 0
            || readout.interintra_mode.is_some()
            || readout.wedge_interintra.is_some()
            || readout.wedge_index.is_some()
        {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // §3 binary alphabet bound on `interintra`.
    if readout.interintra > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.28 `interintra` S() over `TileInterIntraCdf[ ctx ]`,
    // `ctx = Size_Group[ MiSize ] - 1`. The gate confines `MiSize` to
    // the band where `interintra_ctx` is `Some(_)`.
    let ctx = interintra_ctx(mi_size).ok_or(Error::PartitionWalkOutOfRange)?;
    let row = cdfs
        .inter_intra_cdf(ctx)
        .ok_or(Error::PartitionWalkOutOfRange)?;
    writer.write_symbol(readout.interintra as u32, row)?;

    if readout.interintra == 0 {
        // Inner arm closed: the reader leaves every option field `None`.
        if readout.interintra_mode.is_some()
            || readout.wedge_interintra.is_some()
            || readout.wedge_index.is_some()
        {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // Inner arm: `interintra_mode` S() over
    // `TileInterIntraModeCdf[ ctx ]` (same ctx).
    let ii_mode = readout
        .interintra_mode
        .ok_or(Error::PartitionWalkOutOfRange)?;
    if (ii_mode as usize) >= INTERINTRA_MODES {
        return Err(Error::PartitionWalkOutOfRange);
    }
    // `wedge_interintra` S() over `TileWedgeInterIntraCdf[ MiSize ]`
    // (straight `MiSize` index).
    let wedge_ii = readout
        .wedge_interintra
        .ok_or(Error::PartitionWalkOutOfRange)?;
    if wedge_ii > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }
    // `wedge_index` presence must match the `wedge_interintra` arm
    // before any symbol is committed.
    if (wedge_ii == 1) != readout.wedge_index.is_some() {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if let Some(wi) = readout.wedge_index {
        if (wi as usize) >= WEDGE_TYPES {
            return Err(Error::PartitionWalkOutOfRange);
        }
    }

    let row = cdfs
        .inter_intra_mode_cdf(ctx)
        .ok_or(Error::PartitionWalkOutOfRange)?;
    writer.write_symbol(ii_mode as u32, row)?;

    let row = cdfs
        .wedge_inter_intra_cdf(mi_size)
        .ok_or(Error::PartitionWalkOutOfRange)?;
    writer.write_symbol(wedge_ii as u32, row)?;

    if let Some(wi) = readout.wedge_index {
        // `wedge_index` S() over `TileWedgeIndexCdf[ MiSize ]` — the
        // same default CDF the §5.11.29 COMPOUND_WEDGE branch selects.
        let row = cdfs
            .wedge_index_cdf(mi_size)
            .ok_or(Error::PartitionWalkOutOfRange)?;
        writer.write_symbol(wi as u32, row)?;
    }

    Ok(())
}

/// `read_motion_mode( isCompound )` writer per §5.11.27 (av1-spec
/// p.79) — r276. Second piece of the §5.11.23 tail after
/// `assign_mv( )`. Exact encode-side inverse of
/// [`crate::cdf::PartitionWalker::read_motion_mode`]: the caller hands
/// the target §6.10.26 `motion_mode` ordinal (`SIMPLE = 0` / `OBMC = 1`
/// / `WARPED_CAUSAL = 2`) and the same gating scalars the decode side
/// consumes; the writer re-derives the active arm and emits zero or one
/// §8.2.6 `S()`.
///
/// ## Spec body (§5.11.27 — av1-spec p.79)
///
/// The body short-circuits to `motion_mode = SIMPLE` (no bits) on:
///
/// * `skip_mode == 1`;
/// * `!is_motion_mode_switchable`;
/// * `Min( Block_Width[ MiSize ], Block_Height[ MiSize ] ) < 8`;
/// * `!force_integer_mv && YMode ∈ { GLOBALMV, GLOBAL_GLOBALMV } &&
///   GmType[ RefFrame[ 0 ] ] > TRANSLATION`;
/// * `isCompound || RefFrame[ 1 ] == INTRA_FRAME ||
///   !has_overlappable_candidates( )`.
///
/// Otherwise it runs `find_warp_samples( )` (§7.10.4) and dispatches:
///
/// * `force_integer_mv || NumSamples == 0 || !allow_warped_motion ||
///   is_scaled( RefFrame[ 0 ] )` ⇒ **arm A**: `use_obmc` S() over
///   `TileUseObmcCdf[ MiSize ]`; `motion_mode = use_obmc ? OBMC :
///   SIMPLE` (WARPED_CAUSAL unreachable on this arm);
/// * otherwise ⇒ **arm B**: `motion_mode` S() over
///   `TileMotionModeCdf[ MiSize ]` (all three ordinals reachable).
///
/// ## Inputs
///
/// Mirrors the decode-side scalar surface; the two walker-derived
/// quantities arrive precomputed (the stateless-writer doctrine of
/// this module):
///
/// * `has_overlappable` — the §5.11.27 `has_overlappable_candidates()`
///   outcome over the encoder's own grid state.
/// * `num_samples` — `NumSamples` from §7.10.4 `find_warp_samples()`.
/// * `gm_type` — `GmType[ 0..8 ]` (§5.9.24), consulted on the
///   GLOBALMV arm for `RefFrame[ 0 ] ∈ 0..8`.
/// * `is_scaled_per_ref` — the §5.11.27 `is_scaled( refFrame )`
///   outcome per `refFrame - LAST_FRAME ∈ 0..7`.
///
/// ## Target consistency
///
/// A `motion_mode` the reader could never produce on the active arm is
/// a caller bug (`Err(PartitionWalkOutOfRange)`): the short-circuit
/// arms force `SIMPLE`; arm A admits only `SIMPLE` / `OBMC`; arm B
/// admits `0..MOTION_MODES`.
#[allow(clippy::too_many_arguments)]
pub fn write_motion_mode(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    motion_mode: u8,
    mi_size: usize,
    skip_mode: u8,
    is_compound: bool,
    ref_frame: [i32; 2],
    y_mode: u8,
    num_samples: u32,
    is_motion_mode_switchable: bool,
    allow_warped_motion: bool,
    force_integer_mv: bool,
    gm_type: [i32; 8],
    is_scaled_per_ref: [bool; 7],
    has_overlappable: bool,
) -> Result<(), Error> {
    // §3 binary alphabet bound on `skip_mode`.
    if skip_mode > 1 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.27 short-circuit arms — each forces SIMPLE with no bits.
    let forced_simple = skip_mode != 0
        || !is_motion_mode_switchable
        || core::cmp::min(block_width(mi_size), block_height(mi_size)) < 8
        || (!force_integer_mv
            && (y_mode == MODE_GLOBALMV || y_mode == MODE_GLOBAL_GLOBALMV)
            && (0..8).contains(&ref_frame[0])
            && gm_type[ref_frame[0] as usize] > GM_TYPE_TRANSLATION)
        // `RefFrame[ 1 ] == INTRA_FRAME` — INTRA_FRAME = 0.
        || is_compound
        || ref_frame[1] == 0
        || !has_overlappable;

    if forced_simple {
        if motion_mode != MOTION_MODE_SIMPLE {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // §5.11.27 post-`find_warp_samples` arm dispatch. `is_scaled(
    // RefFrame[ 0 ] )` is only meaningful for `RefFrame[ 0 ] ∈
    // LAST_FRAME..=ALTREF_FRAME = 1..=7` (single-pred with a non-INTRA
    // slot 0 — guaranteed past the short-circuits above).
    let ref0 = ref_frame[0];
    let scaled = if (1..=7).contains(&ref0) {
        is_scaled_per_ref[(ref0 - 1) as usize]
    } else {
        false
    };

    if force_integer_mv || num_samples == 0 || !allow_warped_motion || scaled {
        // Arm A: `use_obmc` S() over `TileUseObmcCdf[ MiSize ]`.
        // WARPED_CAUSAL is unreachable here.
        let use_obmc: u32 = match motion_mode {
            MOTION_MODE_SIMPLE => 0,
            MOTION_MODE_OBMC => 1,
            _ => return Err(Error::PartitionWalkOutOfRange),
        };
        let row = cdfs
            .use_obmc_cdf(mi_size)
            .ok_or(Error::PartitionWalkOutOfRange)?;
        writer.write_symbol(use_obmc, row)?;
    } else {
        // Arm B: `motion_mode` S() over `TileMotionModeCdf[ MiSize ]`.
        if (motion_mode as usize) >= MOTION_MODES {
            return Err(Error::PartitionWalkOutOfRange);
        }
        let row = cdfs
            .motion_mode_cdf(mi_size)
            .ok_or(Error::PartitionWalkOutOfRange)?;
        writer.write_symbol(motion_mode as u32, row)?;
    }

    Ok(())
}

/// `read_compound_type( isCompound )` writer per §5.11.29 (av1-spec
/// p.80-81) — r276. Third piece of the §5.11.23 tail after
/// `assign_mv( )`. Exact encode-side inverse of
/// [`crate::cdf::PartitionWalker::read_compound_type`]: the caller
/// hands the target [`CompoundTypeReadout`] (the same aggregate the
/// decoder returns) plus the gating scalars; the writer re-derives the
/// active arms and emits the matching S() / L(1) sequence.
///
/// ## Spec body (§5.11.29 — av1-spec p.80-81)
///
/// ```text
///   comp_group_idx = 0
///   compound_idx = 1
///   if ( skip_mode ) { compound_type = COMPOUND_AVERAGE; return }
///   if ( isCompound ) {
///       n = Wedge_Bits[ MiSize ]
///       if ( enable_masked_compound )
///           comp_group_idx                                   S()
///       if ( comp_group_idx == 0 ) {
///           if ( enable_jnt_comp ) {
///               compound_idx                                 S()
///               compound_type = compound_idx ? COMPOUND_AVERAGE
///                                            : COMPOUND_DISTANCE
///           } else {
///               compound_type = COMPOUND_AVERAGE
///           }
///       } else {
///           if ( n == 0 ) compound_type = COMPOUND_DIFFWTD
///           else          compound_type                      S()
///       }
///       if ( compound_type == COMPOUND_WEDGE ) {
///           wedge_index                                      S()
///           wedge_sign                                       L(1)
///       } else if ( compound_type == COMPOUND_DIFFWTD ) {
///           mask_type                                        L(1)
///       }
///   } else {
///       if ( interintra )
///           compound_type = wedge_interintra ? COMPOUND_WEDGE
///                                            : COMPOUND_INTRA
///       else
///           compound_type = COMPOUND_AVERAGE
///   }
/// ```
///
/// ## §8.3.2 CDF selections
///
/// * `comp_group_idx`: `TileCompGroupIdxCdf[ ctx ]` via
///   [`comp_group_idx_ctx`];
/// * `compound_idx`: `TileCompoundIdxCdf[ ctx ]` via
///   [`compound_idx_ctx`] (seeded with `dist_equal ? 3 : 0`);
/// * `compound_type`: `TileCompoundTypeCdf[ MiSize ]` (straight
///   index);
/// * `wedge_index`: `TileWedgeIndexCdf[ MiSize ]`;
/// * `wedge_sign` / `mask_type`: literal `L(1)`, no CDF.
///
/// ## Inputs
///
/// The scalar surface mirrors the decode side; the two neighbour grid
/// reads arrive precomputed (the stateless-writer doctrine of this
/// module): `above_comp_group_idx` / `left_comp_group_idx` carry the
/// `CompGroupIdxs[ MiRow - 1 ][ MiCol ]` / `CompGroupIdxs[ MiRow ][
/// MiCol - 1 ]` values (identity `0` when the neighbour is
/// unavailable) and `above_compound_idx` / `left_compound_idx` the
/// `CompoundIdxs` twins (identity `1`). Both ctx walks only consult
/// them on the `!AboveSingle` / `!LeftSingle` arms, exactly as the
/// reader does.
///
/// ## Readout consistency
///
/// The target `readout` must be exactly producible by the reader on
/// this gate configuration — the derived fields (`compound_type` on
/// every non-S() arm, `comp_group_idx` / `compound_idx` pre-sets, the
/// option-field presence pattern) are cross-checked and any mismatch
/// is a caller bug surfaced as `Err(PartitionWalkOutOfRange)`.
///
/// ## §5.11.5 grid-fill is on the caller
///
/// Stateless: between zero and two `S()` plus at most one `S()` + one
/// `L(1)` sub-branch. The caller stamps `comp_group_idx` /
/// `compound_idx` onto its own grids so subsequent blocks' §8.3.2
/// neighbour walks observe them.
#[allow(clippy::too_many_arguments)]
pub fn write_compound_type(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    readout: &CompoundTypeReadout,
    mi_size: usize,
    skip_mode: u8,
    is_compound: bool,
    interintra: u8,
    wedge_interintra: u8,
    enable_masked_compound: bool,
    enable_jnt_comp: bool,
    dist_equal: bool,
    avail_u: bool,
    avail_l: bool,
    above_single: bool,
    left_single: bool,
    above_ref_0_altref: bool,
    left_ref_0_altref: bool,
    above_comp_group_idx: u8,
    left_comp_group_idx: u8,
    above_compound_idx: u8,
    left_compound_idx: u8,
) -> Result<(), Error> {
    // §3 binary alphabet bounds.
    if skip_mode > 1
        || above_comp_group_idx > 1
        || left_comp_group_idx > 1
        || above_compound_idx > 1
        || left_compound_idx > 1
    {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.29 lines 3-6: `if ( skip_mode )` short-circuit — the
    // readout must carry the exact line-1/2 pre-sets, no bits.
    if skip_mode != 0 {
        if readout.comp_group_idx != 0
            || readout.compound_idx != 1
            || readout.compound_type != COMPOUND_AVERAGE
            || readout.wedge_index.is_some()
            || readout.wedge_sign.is_some()
            || readout.mask_type.is_some()
        {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    if !is_compound {
        // §5.11.29 else-arm: no bits; the §5.11.28 outcome alone
        // selects the type.
        let expected = if interintra != 0 {
            if wedge_interintra != 0 {
                COMPOUND_WEDGE
            } else {
                COMPOUND_INTRA
            }
        } else {
            COMPOUND_AVERAGE
        };
        if readout.comp_group_idx != 0
            || readout.compound_idx != 1
            || readout.compound_type != expected
            || readout.wedge_index.is_some()
            || readout.wedge_sign.is_some()
            || readout.mask_type.is_some()
        {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // §5.11.29 `n = Wedge_Bits[ MiSize ]`.
    let n = wedge_bits(mi_size);

    // §5.11.29 `if ( enable_masked_compound )` ⇒ `comp_group_idx S()`;
    // otherwise the line-1 pre-set `0` must survive.
    if enable_masked_compound {
        if readout.comp_group_idx > 1 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        let ctx = comp_group_idx_ctx(
            avail_u,
            above_single,
            above_comp_group_idx,
            above_ref_0_altref,
            avail_l,
            left_single,
            left_comp_group_idx,
            left_ref_0_altref,
        );
        let row = cdfs.comp_group_idx_cdf(ctx);
        writer.write_symbol(readout.comp_group_idx as u32, row)?;
    } else if readout.comp_group_idx != 0 {
        return Err(Error::PartitionWalkOutOfRange);
    }

    if readout.comp_group_idx == 0 {
        if enable_jnt_comp {
            // `compound_idx S()` ⇒ `compound_type = compound_idx ?
            // COMPOUND_AVERAGE : COMPOUND_DISTANCE`.
            if readout.compound_idx > 1 {
                return Err(Error::PartitionWalkOutOfRange);
            }
            let expected = if readout.compound_idx != 0 {
                COMPOUND_AVERAGE
            } else {
                COMPOUND_DISTANCE
            };
            if readout.compound_type != expected {
                return Err(Error::PartitionWalkOutOfRange);
            }
            let ctx = compound_idx_ctx(
                dist_equal,
                avail_u,
                above_single,
                above_compound_idx,
                above_ref_0_altref,
                avail_l,
                left_single,
                left_compound_idx,
                left_ref_0_altref,
            );
            let row = cdfs.compound_idx_cdf(ctx);
            writer.write_symbol(readout.compound_idx as u32, row)?;
        } else if readout.compound_idx != 1 || readout.compound_type != COMPOUND_AVERAGE {
            // Line-2 pre-set survives; type forced to AVERAGE.
            return Err(Error::PartitionWalkOutOfRange);
        }
    } else {
        // `comp_group_idx == 1`: the `compound_idx` pre-set survives.
        if readout.compound_idx != 1 {
            return Err(Error::PartitionWalkOutOfRange);
        }
        if n == 0 {
            if readout.compound_type != COMPOUND_DIFFWTD {
                return Err(Error::PartitionWalkOutOfRange);
            }
        } else {
            // `compound_type S()` over `TileCompoundTypeCdf[ MiSize ]`
            // — only the two §6.10.24 signalled ordinals
            // (COMPOUND_WEDGE / COMPOUND_DIFFWTD) are codeable.
            if (readout.compound_type as usize) >= COMPOUND_TYPES {
                return Err(Error::PartitionWalkOutOfRange);
            }
            let row = cdfs
                .compound_type_cdf(mi_size)
                .ok_or(Error::PartitionWalkOutOfRange)?;
            writer.write_symbol(readout.compound_type as u32, row)?;
        }
    }

    // §5.11.29 wedge / diffwtd sub-branches — validate the option-field
    // presence pattern before committing any further symbol.
    if readout.compound_type == COMPOUND_WEDGE {
        let wi = readout.wedge_index.ok_or(Error::PartitionWalkOutOfRange)?;
        let ws = readout.wedge_sign.ok_or(Error::PartitionWalkOutOfRange)?;
        if (wi as usize) >= WEDGE_TYPES || ws > 1 || readout.mask_type.is_some() {
            return Err(Error::PartitionWalkOutOfRange);
        }
        let row = cdfs
            .wedge_index_cdf(mi_size)
            .ok_or(Error::PartitionWalkOutOfRange)?;
        writer.write_symbol(wi as u32, row)?;
        // `wedge_sign L(1)`.
        writer.write_literal(1, ws as u32)?;
    } else if readout.compound_type == COMPOUND_DIFFWTD {
        let mt = readout.mask_type.ok_or(Error::PartitionWalkOutOfRange)?;
        if mt > 1 || readout.wedge_index.is_some() || readout.wedge_sign.is_some() {
            return Err(Error::PartitionWalkOutOfRange);
        }
        // `mask_type L(1)`.
        writer.write_literal(1, mt as u32)?;
    } else if readout.wedge_index.is_some()
        || readout.wedge_sign.is_some()
        || readout.mask_type.is_some()
    {
        return Err(Error::PartitionWalkOutOfRange);
    }

    Ok(())
}

/// §5.11.x interpolation-filter loop writer (av1-spec p.74, the
/// `if ( interpolation_filter == SWITCHABLE )` block closing
/// `inter_block_mode_info`) — r277. Fourth and last piece of the
/// §5.11.23 tail. Exact encode-side inverse of
/// [`crate::cdf::PartitionWalker::read_interpolation_filter`]: the
/// caller hands the target [`InterpolationFilterReadout`] (the same
/// aggregate the decoder returns) plus the gating scalars; the writer
/// re-derives the active arms and emits the matching S() sequence.
///
/// ## Spec body (av1-spec p.74)
///
/// ```text
///   if ( interpolation_filter == SWITCHABLE ) {
///       for ( dir = 0; dir < ( enable_dual_filter ? 2 : 1 ); dir++ ) {
///           if ( needs_interp_filter( ) ) {
///               interp_filter[ dir ]                          S()
///           } else {
///               interp_filter[ dir ] = EIGHTTAP
///           }
///       }
///       if ( !enable_dual_filter )
///           interp_filter[ 1 ] = interp_filter[ 0 ]
///   } else {
///       for ( dir = 0; dir < 2; dir++ )
///           interp_filter[ dir ] = interpolation_filter
///   }
/// ```
///
/// with `needs_interp_filter( )` per av1-spec p.75:
///
/// ```text
///   needs_interp_filter( ) {
///       large = ( Min( Block_Width[ MiSize ], Block_Height[ MiSize ] ) >= 8 )
///       if ( skip_mode || motion_mode == LOCALWARP )           return 0
///       else if ( large && YMode == GLOBALMV )
///           return GmType[ RefFrame[ 0 ] ] == TRANSLATION
///       else if ( large && YMode == GLOBAL_GLOBALMV )
///           return GmType[ RefFrame[ 0 ] ] == TRANSLATION ||
///                  GmType[ RefFrame[ 1 ] ] == TRANSLATION
///       else                                                   return 1
///   }
/// ```
///
/// ## §8.3.2 CDF selection
///
/// `interp_filter` codes against `TileInterpFilterCdf[ ctx ]` with
/// `ctx` from the §8.3.2 walk (av1-spec p.369) via
/// [`interp_filter_ctx`]. Per the stateless-writer doctrine of this
/// module the two neighbour grid reads arrive precomputed:
/// `above_ref_frame` / `left_ref_frame` carry the neighbours'
/// `RefFrames[ .. ][ 0..2 ]` pairs and `above_interp_filters` /
/// `left_interp_filters` their `InterpFilters[ .. ][ dir ]` pairs.
/// The walk accepts a neighbour's filter only when the neighbour is
/// available AND one of its two reference slots equals
/// `RefFrame[ 0 ]`; otherwise the spec's `3` sentinel
/// ([`INTERP_FILTER_NONE`]) stands in — exactly the reader's grid
/// resolution.
///
/// ## Readout consistency
///
/// The target `readout` must be exactly producible by the reader on
/// this gate configuration:
///
/// * `interpolation_filter != SWITCHABLE` ⇒ both slots equal the
///   frame-header value, both `read_from_bitstream == false`, no
///   bits;
/// * `SWITCHABLE && !needs_interp_filter( )` ⇒ the coded dir carries
///   `EIGHTTAP` with `read_from_bitstream == false`, no bits;
/// * `SWITCHABLE && needs_interp_filter( )` ⇒ the coded dir carries
///   an ordinal in `0..INTERP_FILTERS` (the 3-way S() alphabet —
///   `BILINEAR` is frame-level-only) with `read_from_bitstream ==
///   true`;
/// * `!enable_dual_filter` ⇒ slot 1 mirrors slot 0 with
///   `read_from_bitstream[ 1 ] == false` (the spec's post-loop
///   mirror reads no second symbol).
///
/// Any other shape is a caller bug surfaced as
/// `Err(PartitionWalkOutOfRange)` (the reader could never decode it).
///
/// ## §5.11.5 grid-fill is on the caller
///
/// Stateless: between zero and two §8.2.6 `S()` symbols. The caller
/// stamps `InterpFilters[ r + y ][ c + x ][ dir ] =
/// interp_filter[ dir ]` onto the block footprint through its own
/// [`crate::cdf::PartitionWalker`] so subsequent blocks' §8.3.2 ctx
/// walks observe the values.
#[allow(clippy::too_many_arguments)]
pub fn write_interpolation_filter(
    writer: &mut SymbolWriter,
    cdfs: &mut TileCdfContext,
    readout: &InterpolationFilterReadout,
    mi_size: usize,
    skip_mode: u8,
    is_compound: bool,
    ref_frame: [i32; 2],
    y_mode: u8,
    motion_mode: u8,
    interpolation_filter: u8,
    enable_dual_filter: bool,
    gm_type: [i32; 8],
    avail_u: bool,
    avail_l: bool,
    above_ref_frame: [i32; 2],
    left_ref_frame: [i32; 2],
    above_interp_filters: [u8; 2],
    left_interp_filters: [u8; 2],
) -> Result<(), Error> {
    // §5.11.x caller-bug guards — mirror the reader's bounds:
    // `interpolation_filter` in `EIGHTTAP..=SWITCHABLE = 0..=4` per
    // §6.8.9; `mi_size` per the §3 BLOCK_* enumeration; `ref_frame[0]`
    // in `INTRA_FRAME = 0..=ALTREF_FRAME = 7`; `ref_frame[1]` in
    // `NONE = -1..=ALTREF_FRAME = 7`; `skip_mode` binary; the
    // neighbour `InterpFilters` entries in `0..=BILINEAR = 0..=3`
    // (every value the §5.11.x reader can stamp).
    if skip_mode > 1 || mi_size >= BLOCK_SIZES || interpolation_filter > SWITCHABLE {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if !(0..=7).contains(&ref_frame[0]) || !(-1..=7).contains(&ref_frame[1]) {
        return Err(Error::PartitionWalkOutOfRange);
    }
    if above_interp_filters.iter().any(|&f| f > BILINEAR)
        || left_interp_filters.iter().any(|&f| f > BILINEAR)
    {
        return Err(Error::PartitionWalkOutOfRange);
    }

    // §5.11.x `else`-arm: `interp_filter[ dir ] = interpolation_filter`
    // for both dirs, no bits. The readout must carry the exact forced
    // shape.
    if interpolation_filter != SWITCHABLE {
        if readout.interp_filter != [interpolation_filter, interpolation_filter]
            || readout.read_from_bitstream != [false, false]
        {
            return Err(Error::PartitionWalkOutOfRange);
        }
        return Ok(());
    }

    // `needs_interp_filter( )` per av1-spec p.75 — dir-invariant, so
    // hoisted out of the per-direction loop (the reader evaluates it
    // per iteration with identical inputs).
    let large = core::cmp::min(block_width(mi_size), block_height(mi_size)) >= 8;
    let needs = if skip_mode != 0 || motion_mode == MOTION_MODE_WARPED_CAUSAL {
        false
    } else if large && y_mode == MODE_GLOBALMV {
        let r0 = ref_frame[0];
        (0..8).contains(&r0) && gm_type[r0 as usize] == GM_TYPE_TRANSLATION
    } else if large && y_mode == MODE_GLOBAL_GLOBALMV {
        let r0 = ref_frame[0];
        let r1 = ref_frame[1];
        let t0 = (0..8).contains(&r0) && gm_type[r0 as usize] == GM_TYPE_TRANSLATION;
        let t1 = (0..8).contains(&r1) && gm_type[r1 as usize] == GM_TYPE_TRANSLATION;
        t0 || t1
    } else {
        true
    };

    // §5.11.x `if`-arm: per-direction loop over `enable_dual_filter ?
    // 2 : 1` dirs.
    let dir_count = if enable_dual_filter { 2usize } else { 1 };
    for dir in 0..dir_count {
        if !needs {
            // `interp_filter[ dir ] = EIGHTTAP` — no bits; the readout
            // must carry the exact fallback shape.
            if readout.interp_filter[dir] != EIGHTTAP || readout.read_from_bitstream[dir] {
                return Err(Error::PartitionWalkOutOfRange);
            }
            continue;
        }

        // `interp_filter[ dir ] S()` — the 3-way alphabet covers
        // `EIGHTTAP..=EIGHTTAP_SHARP` only (`BILINEAR` is reachable
        // solely through the frame-level f(2) path above).
        if !readout.read_from_bitstream[dir]
            || (readout.interp_filter[dir] as usize) >= INTERP_FILTERS
        {
            return Err(Error::PartitionWalkOutOfRange);
        }

        // §8.3.2 neighbour resolution: take the neighbour's
        // `InterpFilters[ .. ][ dir ]` only when it is available and
        // one of its `RefFrames[ .. ][ 0..2 ]` equals `RefFrame[ 0 ]`;
        // else the `3` sentinel.
        let above_type = if avail_u
            && (above_ref_frame[0] == ref_frame[0] || above_ref_frame[1] == ref_frame[0])
        {
            above_interp_filters[dir] as usize
        } else {
            INTERP_FILTER_NONE
        };
        let left_type = if avail_l
            && (left_ref_frame[0] == ref_frame[0] || left_ref_frame[1] == ref_frame[0])
        {
            left_interp_filters[dir] as usize
        } else {
            INTERP_FILTER_NONE
        };
        let ctx = interp_filter_ctx(above_type, left_type, dir as u32, is_compound)
            .ok_or(Error::PartitionWalkOutOfRange)?;
        let row = cdfs
            .interp_filter_cdf(ctx)
            .ok_or(Error::PartitionWalkOutOfRange)?;
        writer.write_symbol(readout.interp_filter[dir] as u32, row)?;
    }

    // §5.11.x post-loop mirror: `if ( !enable_dual_filter )
    // interp_filter[ 1 ] = interp_filter[ 0 ]` — no second symbol, so
    // slot 1 must mirror slot 0 with its read flag clear.
    if !enable_dual_filter
        && (readout.interp_filter[1] != readout.interp_filter[0] || readout.read_from_bitstream[1])
    {
        return Err(Error::PartitionWalkOutOfRange);
    }

    Ok(())
}

/// `FloorLog2(x)` per §4.7 (av1-spec p.21) — `s = 0; while (x !=
/// 0) { x >>= 1; s++; } return s - 1;`. Local helper because the
/// public surface lives in `cdf` / `symbol_decoder`; pulled inline
/// to avoid widening this module's import surface. Mirrors
/// [`crate::encoder::symbol_writer`]'s private helper of the same
/// name.
#[inline]
fn floor_log2(mut x: u32) -> u32 {
    debug_assert!(x != 0, "§4.7 FloorLog2 requires x != 0");
    let mut s: u32 = 0;
    while x != 0 {
        x >>= 1;
        s += 1;
    }
    s - 1
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cdf::{
        cfl_allowed_for_uv_mode, comp_mode_ctx, intra_mode_ctx, is_inter_ctx, size_group, skip_ctx,
        skip_mode_ctx, PartitionWalker, TileGeometry, BLOCK_16X16, BLOCK_4X4, BLOCK_4X8, BLOCK_8X4,
        BLOCK_8X8, DC_PRED, MODE_GLOBAL_GLOBALMV, MODE_NEAREST_NEWMV, MODE_NEAR_NEARMV,
        MODE_NEAR_NEWMV, MODE_NEW_NEARESTMV, MODE_NEW_NEARMV, V_PRED,
    };
    use crate::symbol_decoder::SymbolDecoder;

    /// Local cast helper — `DC_PRED` / `V_PRED` / etc. are exposed as
    /// `usize` constants in `crate::cdf`, but the §3 enumeration bounds
    /// them to `0..INTRA_MODES = 13`, so the cast to `u8` is total.
    const DC_PRED_U8: u8 = DC_PRED as u8;
    const V_PRED_U8: u8 = V_PRED as u8;

    /// Build a fresh 32×32 mi walker + default CDFs (the same fixture
    /// shape `decode_intra_block_mode_info_*` tests use).
    fn fresh_walker_and_cdfs() -> (PartitionWalker, TileCdfContext) {
        let geom = TileGeometry {
            mi_row_start: 0,
            mi_row_end: 32,
            mi_col_start: 0,
            mi_col_end: 32,
        };
        let walker = PartitionWalker::new(32, 32, geom).unwrap();
        let cdfs = TileCdfContext::new_from_defaults();
        (walker, cdfs)
    }

    // -----------------------------------------------------------------
    // §5.11.11 read_skip — write_skip round-trips through decode_skip.
    // -----------------------------------------------------------------

    /// `seg_skip_active = true` arm: no bits written, no bits read,
    /// `skip == 1` on both sides. The decode call also performs no
    /// bitstream read.
    #[test]
    fn write_skip_seg_short_circuit_writes_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_skip(&mut writer, &mut enc_cdfs, 1, 0, true).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        // For the no-symbol path the decoder needs *some* buffer; a
        // single byte is enough — `init_symbol` requires `sz >= 1`.
        // The decoder's `decode_skip` short-circuit fires on
        // `seg_skip_active == true` and doesn't touch the buffer.
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let skip = walker
            .decode_skip(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, true)
            .unwrap();
        assert_eq!(skip, 1, "§5.11.11 seg short-circuit returns skip = 1");
    }

    /// `seg_skip_active = false`, `skip = 0` at the frame origin
    /// (`ctx = 0` since AvailU = AvailL = false). Round-trips through
    /// `decode_skip` with the §8.3 CDF adaptation engaged on both sides.
    #[test]
    fn write_skip_zero_round_trip_at_origin() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let ctx = skip_ctx(0, 0);
        write_skip(&mut writer, &mut enc_cdfs, 0, ctx, false).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let skip = walker
            .decode_skip(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false)
            .unwrap();
        assert_eq!(skip, 0);
    }

    /// `seg_skip_active = false`, `skip = 1` at the frame origin.
    #[test]
    fn write_skip_one_round_trip_at_origin() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let ctx = skip_ctx(0, 0);
        write_skip(&mut writer, &mut enc_cdfs, 1, ctx, false).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let skip = walker
            .decode_skip(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false)
            .unwrap();
        assert_eq!(skip, 1);
    }

    /// `skip != 1` on the seg-short-circuit arm is a caller bug.
    #[test]
    fn write_skip_rejects_skip_zero_on_seg_arm() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_skip(&mut writer, &mut enc_cdfs, 0, 0, true).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.7 intra_frame_y_mode — write_intra_frame_y_mode round-trips
    // through decode_intra_frame_y_mode.
    // -----------------------------------------------------------------

    /// `y_mode = DC_PRED` at frame origin: both ctx slots feed
    /// `Intra_Mode_Context[ DC_PRED ]` from the unavailable-neighbour
    /// fallback per §8.3.2. Round-trip through the decoder.
    #[test]
    fn write_intra_frame_y_mode_dc_pred_round_trip_at_origin() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        // Both neighbours unavailable ⇒ DC_PRED ⇒ Intra_Mode_Context[0].
        let abovemode_ctx = intra_mode_ctx(DC_PRED);
        let leftmode_ctx = intra_mode_ctx(DC_PRED);
        write_intra_frame_y_mode(
            &mut writer,
            &mut enc_cdfs,
            DC_PRED_U8,
            abovemode_ctx,
            leftmode_ctx,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let y_mode = walker
            .decode_intra_frame_y_mode(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16)
            .unwrap();
        assert_eq!(y_mode, DC_PRED_U8);
    }

    /// `y_mode = V_PRED` (directional, ordinal 1) at frame origin —
    /// exercises a non-DC_PRED leaf and confirms the §8.3 adaptation
    /// stays in lockstep.
    #[test]
    fn write_intra_frame_y_mode_v_pred_round_trip() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let abovemode_ctx = intra_mode_ctx(DC_PRED);
        let leftmode_ctx = intra_mode_ctx(DC_PRED);
        write_intra_frame_y_mode(
            &mut writer,
            &mut enc_cdfs,
            V_PRED_U8,
            abovemode_ctx,
            leftmode_ctx,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let y_mode = walker
            .decode_intra_frame_y_mode(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16)
            .unwrap();
        assert_eq!(y_mode, V_PRED_U8);
    }

    /// `y_mode >= INTRA_MODES` is a caller bug.
    #[test]
    fn write_intra_frame_y_mode_rejects_out_of_range() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_intra_frame_y_mode(&mut writer, &mut enc_cdfs, 13, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.22 y_mode + uv_mode — write_y_mode + write_intra_uv_mode
    // round-trip through decode_intra_block_mode_info.
    // -----------------------------------------------------------------

    /// `y_mode = DC_PRED` + `uv_mode = DC_PRED` on a `BLOCK_16X16`
    /// block with `lossless = false`, `subsampling = 0/0` → CFL allowed
    /// (max dim 16 ≤ 32). Round-trips through `decode_intra_block_mode_info`
    /// with palette + filter-intra gates off.
    #[test]
    fn write_y_mode_and_uv_mode_dc_pred_round_trip() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);

        // §5.11.22 line 3: y_mode S() with Size_Group[MiSize] ctx.
        let y_size_group = size_group(BLOCK_16X16);
        write_y_mode(&mut writer, &mut enc_cdfs, DC_PRED_U8, y_size_group).unwrap();

        // §5.11.22 line 6: uv_mode S() with the cfl_allowed selector.
        let cfl_allowed = cfl_allowed_for_uv_mode(false, BLOCK_16X16, false, false);
        write_intra_uv_mode(
            &mut writer,
            &mut enc_cdfs,
            DC_PRED_U8,
            DC_PRED_U8,
            cfl_allowed,
        )
        .unwrap();

        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                /* mi_row = */ 0,
                /* mi_col = */ 0,
                /* sub_size = */ BLOCK_16X16,
                /* lossless = */ false,
                /* has_chroma = */ true,
                /* allow_screen_content_tools = */ false,
                /* enable_filter_intra = */ false,
                /* subsampling_x = */ false,
                /* subsampling_y = */ false,
                /* above_palette_y = */ false,
                /* left_palette_y = */ false,
                /* bit_depth = */ 8,
            )
            .unwrap();
        assert_eq!(info.y_mode, DC_PRED_U8, "round-tripped y_mode");
        assert_eq!(info.uv_mode, Some(DC_PRED_U8), "round-tripped uv_mode");
        // DC_PRED is not directional ⇒ angle_delta short-circuits to 0
        // without a bit read; both sides agree.
        assert_eq!(info.angle_delta_y, 0);
        assert_eq!(info.angle_delta_uv, Some(0));
        // UVMode != UV_CFL_PRED ⇒ cfl_alphas not read.
        assert_eq!(info.cfl_alpha_u, None);
        assert_eq!(info.cfl_alpha_v, None);
        // Palette + filter-intra gates off ⇒ all None.
        assert_eq!(info.has_palette_y, None);
        assert_eq!(info.use_filter_intra, None);
    }

    /// Monochrome (`has_chroma = false`): only `write_y_mode` fires;
    /// `decode_intra_block_mode_info` skips the §5.11.22 chroma arm
    /// entirely and consumes only the y_mode bit(s).
    #[test]
    fn write_y_mode_only_monochrome_round_trip() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let ctx = size_group(BLOCK_16X16);
        write_y_mode(&mut writer, &mut enc_cdfs, DC_PRED_U8, ctx).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                /* has_chroma = */ false,
                false,
                false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, DC_PRED_U8);
        assert_eq!(info.uv_mode, None);
    }

    // -----------------------------------------------------------------
    // §5.11.8 intra_segment_id — write_intra_segment_id round-trips
    // through decode_intra_segment_id.
    // -----------------------------------------------------------------

    /// `segmentation_enabled = false` arm: no bits written, no bits
    /// read; both sides land on `segment_id = 0`.
    #[test]
    fn write_intra_segment_id_disabled_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_intra_segment_id(&mut writer, &mut enc_cdfs, 0, 0, 0, 0, false, 0).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let lossless_array = [false; MAX_SEGMENTS];
        let (sid, _) = walker
            .decode_intra_segment_id(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                /* skip = */ 0,
                /* segmentation_enabled = */ false,
                /* last_active_seg_id = */ 0,
                &lossless_array,
            )
            .unwrap();
        assert_eq!(sid, 0);
    }

    /// `segmentation_enabled = true`, `skip = 1`: the §5.11.9 skip
    /// short-circuit fires on the decoder side (`segment_id = pred`,
    /// no bit read); the writer also emits no bits.
    #[test]
    fn write_intra_segment_id_skip_short_circuit() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        // pred = 0 at frame origin (all neighbours = -1 ⇒ pred = 0).
        write_intra_segment_id(&mut writer, &mut enc_cdfs, 0, 1, 0, 0, true, 7).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let lossless_array = [false; MAX_SEGMENTS];
        let (sid, _) = walker
            .decode_intra_segment_id(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                /* skip = */ 1,
                /* segmentation_enabled = */ true,
                /* last_active_seg_id = */ 7,
                &lossless_array,
            )
            .unwrap();
        assert_eq!(sid, 0, "skip arm ⇒ segment_id = pred = 0 at origin");
    }

    /// `segmentation_enabled = true`, `skip = 0`: the §5.11.9 `else`
    /// arm reads `diff S()` and reconstructs through `neg_deinterleave`.
    /// At frame origin (`pred = 0`), `neg_deinterleave(diff, 0, max)
    /// == diff` so `write_intra_segment_id(segment_id = 3)` emits
    /// `diff = 3`; the decoder reads `diff = 3` and reconstructs
    /// `segment_id = 3`.
    #[test]
    fn write_intra_segment_id_else_branch_at_origin() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        // At origin: AvailU = AvailL = false ⇒ all three prev_* are
        // None ⇒ §8.3.2 segment_id_ctx returns 0.
        write_intra_segment_id(
            &mut writer,
            &mut enc_cdfs,
            /* segment_id = */ 3,
            /* skip = */ 0,
            /* pred = */ 0,
            /* ctx = */ 0,
            true,
            7,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let lossless_array = [false; MAX_SEGMENTS];
        let (sid, _) = walker
            .decode_intra_segment_id(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                /* skip = */ 0,
                /* segmentation_enabled = */ true,
                /* last_active_seg_id = */ 7,
                &lossless_array,
            )
            .unwrap();
        assert_eq!(sid, 3);
    }

    // -----------------------------------------------------------------
    // §5.11.9 read_segment_id — write_segment_id round-trips through
    // decode_segment_id directly (the shared primitive feeding both
    // §5.11.8 intra_segment_id and §5.11.19 inter_segment_id).
    // -----------------------------------------------------------------

    /// §5.11.9 `if ( skip )` short-circuit: no `S()` symbol coded
    /// (the writer takes the no-op arm), and the decoder also reads
    /// no symbol (the §5.11.9 skip branch fires before any
    /// `read_symbol` call). At frame origin every neighbour is
    /// missing ⇒ `pred = 0`, so the round trip lands on `segment_id
    /// = 0`. `decoder.position()` must be unchanged across the
    /// decode call.
    #[test]
    fn write_segment_id_skip_short_circuit_writes_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_segment_id(
            &mut writer,
            &mut enc_cdfs,
            /* segment_id = */ 0,
            /* skip = */ 1,
            /* pred = */ 0,
            /* ctx = */ 0,
            /* last_active_seg_id = */ 7,
        )
        .unwrap();
        let bytes = writer.finish();
        // `finish()` may emit a flush byte even when no `S()` was
        // coded — the SymbolWriter's terminator-byte protocol is
        // independent of payload. Match the existing
        // `write_intra_segment_id_skip_short_circuit` test's pattern.
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let pos_before = dec.position();
        let sid = walker
            .decode_segment_id(
                &mut dec,
                &mut dec_cdfs,
                /* mi_row = */ 0,
                /* mi_col = */ 0,
                BLOCK_16X16,
                /* skip = */ 1,
                /* last_active_seg_id = */ 7,
            )
            .unwrap();
        assert_eq!(sid, 0);
        assert_eq!(dec.position(), pos_before, "no S() read on skip path");
    }

    /// §5.11.9 `else` arm at frame origin: `pred = 0` makes
    /// `neg_deinterleave(diff, 0, max) == diff` (the identity), so
    /// `segment_id = 5` encodes as `diff = 5` and the decoder
    /// reconstructs `segment_id = 5`. Smoke that the
    /// `neg_interleave`-based writer matches the existing
    /// `neg_deinterleave`-based reader.
    #[test]
    fn write_segment_id_else_branch_at_origin_round_trip() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        // At origin: all neighbours absent ⇒ §8.3.2 segment_id_ctx
        // returns 0.
        write_segment_id(
            &mut writer,
            &mut enc_cdfs,
            /* segment_id = */ 5,
            /* skip = */ 0,
            /* pred = */ 0,
            /* ctx = */ 0,
            /* last_active_seg_id = */ 7,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let sid = walker
            .decode_segment_id(
                &mut dec,
                &mut dec_cdfs,
                /* mi_row = */ 0,
                /* mi_col = */ 0,
                BLOCK_16X16,
                /* skip = */ 0,
                /* last_active_seg_id = */ 7,
            )
            .unwrap();
        assert_eq!(sid, 5);
    }

    /// §5.11.9 `else` arm with a non-zero `pred` — covers the upward
    /// bidirectional fan in `neg_interleave`. Stamps the (mi_row=0,
    /// mi_col=0) neighbour by a first encode-then-decode pair at
    /// `segment_id = 2`, so the *second* decode at (mi_row=4,
    /// mi_col=0) sees `prev_u = 2` ⇒ `pred = 2` by the §5.11.9
    /// neighbour cascade (`prev_l == -1, prev_ul == -1, prev_u !=
    /// -1` ⇒ `pred = prev_u`). The second writer call emits
    /// `diff = neg_interleave(s, 2, 8)`; the second decoder call
    /// reads it and reconstructs `s` via `neg_deinterleave`. Exhaust
    /// `segment_id ∈ 0..8`.
    #[test]
    fn write_segment_id_else_branch_upward_fan_round_trip() {
        for sid_in in 0..8u8 {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            // First write: stamp seg_id = 2 at (0, 0) — pred = 0 ⇒
            // diff = 2 on the wire.
            write_segment_id(
                &mut writer,
                &mut enc_cdfs,
                /* segment_id = */ 2,
                /* skip = */ 0,
                /* pred = */ 0,
                /* ctx = */ 0,
                /* last_active_seg_id = */ 7,
            )
            .unwrap();
            // Second write: at (4, 0) the §5.11.9 neighbour cascade
            // matches the decoder's view: `pred = prev_u = 2`.
            write_segment_id(
                &mut writer,
                &mut enc_cdfs,
                /* segment_id = */ sid_in,
                /* skip = */ 0,
                /* pred = */ 2,
                /* ctx = */ 0,
                /* last_active_seg_id = */ 7,
            )
            .unwrap();
            let bytes = writer.finish();

            let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            let sid_first = walker
                .decode_segment_id(
                    &mut dec,
                    &mut dec_cdfs,
                    /* mi_row = */ 0,
                    /* mi_col = */ 0,
                    BLOCK_16X16,
                    /* skip = */ 0,
                    /* last_active_seg_id = */ 7,
                )
                .unwrap();
            assert_eq!(sid_first, 2);
            let sid_out = walker
                .decode_segment_id(
                    &mut dec,
                    &mut dec_cdfs,
                    /* mi_row = */ 4,
                    /* mi_col = */ 0,
                    BLOCK_16X16,
                    /* skip = */ 0,
                    /* last_active_seg_id = */ 7,
                )
                .unwrap();
            assert_eq!(sid_out, sid_in, "round trip failed at sid = {}", sid_in);
        }
    }

    /// §5.11.9 `else` arm with `pred` on the downward bidirectional
    /// fan. Mirror of the upward test with `pred = 5`. Stamps the
    /// (0, 0) neighbour to 5 first, then exercises the second-block
    /// round trip over the 8-symbol alphabet.
    #[test]
    fn write_segment_id_else_branch_downward_fan_round_trip() {
        for sid_in in 0..8u8 {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            // First write: stamp seg_id = 5 at (0, 0); pred = 0 ⇒
            // diff = 5 on the wire.
            write_segment_id(
                &mut writer,
                &mut enc_cdfs,
                /* segment_id = */ 5,
                /* skip = */ 0,
                /* pred = */ 0,
                /* ctx = */ 0,
                /* last_active_seg_id = */ 7,
            )
            .unwrap();
            // Second write: at (4, 0) the §5.11.9 cascade yields
            // `pred = prev_u = 5`.
            write_segment_id(
                &mut writer,
                &mut enc_cdfs,
                /* segment_id = */ sid_in,
                /* skip = */ 0,
                /* pred = */ 5,
                /* ctx = */ 0,
                /* last_active_seg_id = */ 7,
            )
            .unwrap();
            let bytes = writer.finish();

            let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            let sid_first = walker
                .decode_segment_id(
                    &mut dec,
                    &mut dec_cdfs,
                    /* mi_row = */ 0,
                    /* mi_col = */ 0,
                    BLOCK_16X16,
                    /* skip = */ 0,
                    /* last_active_seg_id = */ 7,
                )
                .unwrap();
            assert_eq!(sid_first, 5);
            let sid_out = walker
                .decode_segment_id(
                    &mut dec,
                    &mut dec_cdfs,
                    /* mi_row = */ 4,
                    /* mi_col = */ 0,
                    BLOCK_16X16,
                    /* skip = */ 0,
                    /* last_active_seg_id = */ 7,
                )
                .unwrap();
            assert_eq!(sid_out, sid_in, "round trip failed at sid = {}", sid_in);
        }
    }

    /// §5.11.9 caller-bug shape: `skip == 1` but `segment_id != pred`
    /// — the spec forces `segment_id = pred` in that arm, so the
    /// encoder caller is contradicting itself. Surface as
    /// [`Error::PartitionWalkOutOfRange`].
    #[test]
    fn write_segment_id_rejects_skip_arm_mismatch() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_segment_id(
            &mut writer,
            &mut enc_cdfs,
            /* segment_id = */ 3,
            /* skip = */ 1,
            /* pred = */ 0, // mismatched: forced to equal pred under skip
            /* ctx = */ 0,
            /* last_active_seg_id = */ 7,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.9 caller-bug shape: `segment_id > last_active_seg_id`
    /// (i.e. outside `0..max`). The decoder's §5.11.9 alphabet caps
    /// the `S()` read at `LastActiveSegId + 1`; an encoder that
    /// presents a larger value is asking for a symbol that doesn't
    /// exist. Surface as [`Error::PartitionWalkOutOfRange`].
    #[test]
    fn write_segment_id_rejects_segment_id_out_of_alphabet() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_segment_id(
            &mut writer,
            &mut enc_cdfs,
            /* segment_id = */ 5,
            /* skip = */ 0,
            /* pred = */ 0,
            /* ctx = */ 0,
            /* last_active_seg_id = */ 3, // alphabet 0..=3
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.9 caller-bug shape: `pred > last_active_seg_id` — the
    /// §5.11.9 `pred` derivation is bounded by `last_active_seg_id`
    /// (neighbours' stored ids are ≤ `last_active_seg_id`, and the
    /// fallback is 0), so a caller passing a larger `pred` is a
    /// grid-walk bug.
    #[test]
    fn write_segment_id_rejects_pred_out_of_alphabet() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_segment_id(
            &mut writer,
            &mut enc_cdfs,
            /* segment_id = */ 0,
            /* skip = */ 0,
            /* pred = */ 5,
            /* ctx = */ 0,
            /* last_active_seg_id = */ 3,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.9 caller-bug shape: `last_active_seg_id >= MAX_SEGMENTS`
    /// — §6.10.8 caps `LastActiveSegId` at `MAX_SEGMENTS - 1 = 7`.
    #[test]
    fn write_segment_id_rejects_out_of_range_last_active() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_segment_id(
            &mut writer,
            &mut enc_cdfs,
            /* segment_id = */ 0,
            /* skip = */ 0,
            /* pred = */ 0,
            /* ctx = */ 0,
            /* last_active_seg_id = */ 8,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.9 caller-bug shape: `ctx >= SEGMENT_ID_CONTEXTS = 3` on
    /// the `S()` arm — the §8.3.2 ctx derivation lives in
    /// `0..SEGMENT_ID_CONTEXTS`.
    #[test]
    fn write_segment_id_rejects_out_of_range_ctx() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_segment_id(
            &mut writer,
            &mut enc_cdfs,
            /* segment_id = */ 0,
            /* skip = */ 0,
            /* pred = */ 0,
            /* ctx = */ 3,
            /* last_active_seg_id = */ 7,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // Composed roundtrip: write { skip, intra_segment_id,
    // intra_frame_y_mode } in §5.11.7 order, then decode via the
    // matching walker methods. Forms the first "encode produces a
    // payload the decoder can walk" smoke test for the §5.11 per-block
    // syntax layer.
    // -----------------------------------------------------------------

    /// §5.11.7 SegIdPreSkip = false ⇒ `read_skip` first, then
    /// `intra_segment_id`. Then §5.11.22 `y_mode` (which is what
    /// `decode_intra_block_mode_info` reads after the §5.11.7 prefix
    /// completes). End-to-end smoke that the encoder produces a
    /// payload the §5.11 decoder walks back to the same scalars.
    #[test]
    fn intra_prefix_plus_y_mode_round_trip_at_origin() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);

        // §5.11.11 read_skip — origin block (ctx = 0).
        let skip_ctx_val = skip_ctx(0, 0);
        write_skip(&mut writer, &mut enc_cdfs, 0, skip_ctx_val, false).unwrap();

        // §5.11.8 intra_segment_id — segmentation off, no bits.
        write_intra_segment_id(&mut writer, &mut enc_cdfs, 0, 0, 0, 0, false, 0).unwrap();

        // §5.11.22 y_mode — DC_PRED with Size_Group ctx.
        let y_ctx = size_group(BLOCK_8X8);
        write_y_mode(&mut writer, &mut enc_cdfs, DC_PRED_U8, y_ctx).unwrap();

        // §5.11.22 uv_mode — DC_PRED, CFL-allowed on 8x8 lossy.
        let cfl_allowed = cfl_allowed_for_uv_mode(false, BLOCK_8X8, false, false);
        write_intra_uv_mode(
            &mut writer,
            &mut enc_cdfs,
            DC_PRED_U8,
            DC_PRED_U8,
            cfl_allowed,
        )
        .unwrap();

        let bytes = writer.finish();

        // Decoder side: walk the same prefix, then decode_intra_block_mode_info
        // for y_mode + uv_mode.
        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let skip = walker
            .decode_skip(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_8X8, false)
            .unwrap();
        assert_eq!(skip, 0);
        let lossless_array = [false; MAX_SEGMENTS];
        let (sid, _) = walker
            .decode_intra_segment_id(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_8X8,
                skip,
                /* segmentation_enabled = */ false,
                /* last_active_seg_id = */ 0,
                &lossless_array,
            )
            .unwrap();
        assert_eq!(sid, 0);
        let info = walker
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_8X8,
                /* lossless = */ false,
                /* has_chroma = */ true,
                /* allow_screen_content_tools = */ false,
                /* enable_filter_intra = */ false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, DC_PRED_U8);
        assert_eq!(info.uv_mode, Some(DC_PRED_U8));
    }

    // -----------------------------------------------------------------
    // §5.11.45 `write_cfl_alphas` — round-trips through
    // `decode_intra_block_mode_info`'s built-in §5.11.45 reader (the
    // `UVMode == UV_CFL_PRED` arm). r231.
    // -----------------------------------------------------------------

    /// Helper that emits the §5.11.7 / §5.11.11 / §5.11.22 prefix at
    /// frame origin + the §5.11.45 alphas, then walks the bitstream
    /// back through the decoder's `decode_intra_block_mode_info` and
    /// returns the recovered `(CflAlphaU, CflAlphaV)`.
    fn round_trip_cfl_alphas(alpha_u: i8, alpha_v: i8) -> (Option<i8>, Option<i8>) {
        use crate::cdf::UV_CFL_PRED;
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_skip(&mut writer, &mut enc_cdfs, 0, skip_ctx(0, 0), false).unwrap();
        write_intra_segment_id(&mut writer, &mut enc_cdfs, 0, 0, 0, 0, false, 0).unwrap();
        let y_ctx = size_group(BLOCK_16X16);
        write_y_mode(&mut writer, &mut enc_cdfs, DC_PRED_U8, y_ctx).unwrap();
        // CFL-allowed on 16x16, no subsampling, no lossless.
        let cfl_allowed = cfl_allowed_for_uv_mode(false, BLOCK_16X16, false, false);
        write_intra_uv_mode(
            &mut writer,
            &mut enc_cdfs,
            DC_PRED_U8,
            UV_CFL_PRED as u8,
            cfl_allowed,
        )
        .unwrap();
        write_cfl_alphas(&mut writer, &mut enc_cdfs, alpha_u, alpha_v).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let _skip = walker
            .decode_skip(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false)
            .unwrap();
        let info = walker
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                /* lossless = */ false,
                /* has_chroma = */ true,
                false,
                false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.uv_mode, Some(UV_CFL_PRED as u8));
        (info.cfl_alpha_u, info.cfl_alpha_v)
    }

    #[test]
    fn write_cfl_alphas_round_trip_pos_pos() {
        let (au, av) = round_trip_cfl_alphas(1, 1);
        assert_eq!(au, Some(1));
        assert_eq!(av, Some(1));
    }

    #[test]
    fn write_cfl_alphas_round_trip_neg_neg() {
        let (au, av) = round_trip_cfl_alphas(-3, -7);
        assert_eq!(au, Some(-3));
        assert_eq!(av, Some(-7));
    }

    #[test]
    fn write_cfl_alphas_round_trip_zero_u_pos_v() {
        // signU = ZERO: only V's magnitude is written; U's value
        // reconstructs to 0 on the decoder.
        let (au, av) = round_trip_cfl_alphas(0, 4);
        assert_eq!(au, Some(0));
        assert_eq!(av, Some(4));
    }

    #[test]
    fn write_cfl_alphas_round_trip_neg_u_zero_v() {
        let (au, av) = round_trip_cfl_alphas(-2, 0);
        assert_eq!(au, Some(-2));
        assert_eq!(av, Some(0));
    }

    #[test]
    fn write_cfl_alphas_round_trip_max_magnitudes() {
        // |alpha| = 16 corresponds to cfl_alpha_u = 15 (the max raw
        // value the §5.11.45 S() reads from `Default_Cfl_Alpha_Cdf`).
        let (au, av) = round_trip_cfl_alphas(16, -16);
        assert_eq!(au, Some(16));
        assert_eq!(av, Some(-16));
    }

    #[test]
    fn write_cfl_alphas_rejects_both_zero() {
        // §6.10.36: (CFL_SIGN_ZERO, CFL_SIGN_ZERO) is prohibited as
        // redundant with UV_DC_PRED.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_cfl_alphas(&mut writer, &mut cdfs, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    #[test]
    fn write_cfl_alphas_rejects_out_of_range_magnitude() {
        // |alpha| > CFL_ALPHABET_SIZE = 16 is out of the §5.11.45
        // alphabet.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_cfl_alphas(&mut writer, &mut cdfs, 17, 1).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.20 read_is_inter — write_is_inter round-trips through
    // decode_is_inter. Each arm of the four-arm dispatch gets a test;
    // the fall-through `S()` arm gets both is_inter = 0 and is_inter = 1
    // round-trips plus the §8.3.2 ctx coverage.
    // -----------------------------------------------------------------

    /// §5.11.20 Arm 1: `skip_mode == 1` ⇒ `is_inter = 1`, no bits
    /// written. Mirrors `decode_is_inter_skip_mode_short_circuit_no_symbol_read`.
    #[test]
    fn write_is_inter_skip_mode_short_circuit_writes_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_is_inter(&mut writer, &mut enc_cdfs, 1, 0, 1, false, false, false).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        // No-symbol path — the decoder needs *some* buffer; a single
        // byte is enough. `init_symbol` requires `sz >= 1`.
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let pos_before = dec.position();
        let is_inter = walker
            .decode_is_inter(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                1,
                false,
                false,
                false,
            )
            .unwrap();
        assert_eq!(is_inter, 1, "§5.11.20 Arm 1: skip_mode = 1 ⇒ is_inter = 1");
        assert_eq!(
            dec.position(),
            pos_before,
            "no symbol bit read on the skip_mode arm"
        );
    }

    /// §5.11.20 Arm 2 (intra-routing branch): SEG_LVL_REF_FRAME
    /// active and FeatureData == INTRA_FRAME (caller encodes as
    /// `seg_ref_frame_is_inter = false`) ⇒ `is_inter = 0`, no bits.
    #[test]
    fn write_is_inter_seg_ref_frame_intra_routes_to_zero_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_is_inter(&mut writer, &mut enc_cdfs, 0, 0, 0, true, false, false).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let pos_before = dec.position();
        let is_inter = walker
            .decode_is_inter(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                0,
                true,
                false,
                false,
            )
            .unwrap();
        assert_eq!(
            is_inter, 0,
            "Arm 2: FeatureData == INTRA_FRAME ⇒ is_inter = 0"
        );
        assert_eq!(dec.position(), pos_before);
    }

    /// §5.11.20 Arm 2 (inter-routing branch): SEG_LVL_REF_FRAME
    /// active and FeatureData != INTRA_FRAME (caller encodes as
    /// `seg_ref_frame_is_inter = true`) ⇒ `is_inter = 1`, no bits.
    #[test]
    fn write_is_inter_seg_ref_frame_inter_routes_to_one_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_is_inter(&mut writer, &mut enc_cdfs, 1, 0, 0, true, true, false).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let pos_before = dec.position();
        let is_inter = walker
            .decode_is_inter(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                0,
                true,
                true,
                false,
            )
            .unwrap();
        assert_eq!(
            is_inter, 1,
            "Arm 2: FeatureData != INTRA_FRAME ⇒ is_inter = 1"
        );
        assert_eq!(dec.position(), pos_before);
    }

    /// §5.11.20 Arm 3: SEG_LVL_GLOBALMV active (with Arms 1 / 2 false)
    /// ⇒ `is_inter = 1`, no bits.
    #[test]
    fn write_is_inter_seg_globalmv_short_circuit_writes_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_is_inter(&mut writer, &mut enc_cdfs, 1, 0, 0, false, false, true).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let pos_before = dec.position();
        let is_inter = walker
            .decode_is_inter(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                0,
                false,
                false,
                true,
            )
            .unwrap();
        assert_eq!(is_inter, 1, "Arm 3: SEG_LVL_GLOBALMV ⇒ is_inter = 1");
        assert_eq!(dec.position(), pos_before);
    }

    /// §5.11.20 Arm 4 (fall-through `S()`): all three short-circuit
    /// arms false, `is_inter = 0` at the frame origin. The §8.3.2 ctx
    /// at `(mi_row = 0, mi_col = 0)` is `is_inter_ctx(None, None) = 0`
    /// (both neighbours unavailable). Round-trip through `decode_is_inter`
    /// with the §8.3 CDF adaptation engaged on both sides.
    #[test]
    fn write_is_inter_zero_else_branch_round_trip_at_origin() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let ctx = is_inter_ctx(None, None);
        write_is_inter(&mut writer, &mut enc_cdfs, 0, ctx, 0, false, false, false).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let is_inter = walker
            .decode_is_inter(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                0,
                false,
                false,
                false,
            )
            .unwrap();
        assert_eq!(is_inter, 0);
    }

    /// §5.11.20 Arm 4: `is_inter = 1` at the frame origin (same ctx as
    /// above). Round-trip through the decoder.
    #[test]
    fn write_is_inter_one_else_branch_round_trip_at_origin() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let ctx = is_inter_ctx(None, None);
        write_is_inter(&mut writer, &mut enc_cdfs, 1, ctx, 0, false, false, false).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let is_inter = walker
            .decode_is_inter(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                0,
                false,
                false,
                false,
            )
            .unwrap();
        assert_eq!(is_inter, 1);
    }

    /// §5.11.20 Arm 4 §8.3.2 ctx coverage at frame origin: walk every
    /// `(above_intra, left_intra)` combination [`is_inter_ctx`]
    /// distinguishes for `(None, None)` — i.e. both unavailable at the
    /// origin — and round-trip both `is_inter = 0` and `is_inter = 1`
    /// through the decoder. CDF adaptation engaged on both sides so
    /// the encoder's choice of `ctx` (which CDF row it writes into)
    /// MUST match the decoder's `is_inter_ctx(None, None) = 0` ctx
    /// derivation: any mismatch would either fail to decode (different
    /// row → different probability mass than the encoder used) or
    /// produce a different `is_inter` value than written.
    #[test]
    fn write_is_inter_else_branch_round_trip_at_origin_both_is_inter_values() {
        for is_inter_val in [0u8, 1u8] {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let ctx = is_inter_ctx(None, None);
            assert_eq!(ctx, 0, "origin neighbours unavailable ⇒ ctx = 0");
            assert!(ctx < IS_INTER_CONTEXTS);
            write_is_inter(
                &mut writer,
                &mut enc_cdfs,
                is_inter_val,
                ctx,
                0,
                false,
                false,
                false,
            )
            .unwrap();
            let bytes = writer.finish();
            let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            let decoded = walker
                .decode_is_inter(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    0,
                    false,
                    false,
                    false,
                )
                .unwrap();
            assert_eq!(decoded, is_inter_val);
        }
    }

    /// §8.3.2 ctx exhaustive coverage: every
    /// `(above_intra, left_intra)` combination [`is_inter_ctx`]
    /// recognises stays in `0..IS_INTER_CONTEXTS`, so any of them is
    /// an admissible `ctx` input to [`write_is_inter`] on the Arm 4
    /// fall-through path. The smoke test confirms write_is_inter
    /// accepts every ctx without erroring.
    #[test]
    fn write_is_inter_accepts_every_8_3_2_ctx() {
        let cases: &[(Option<bool>, Option<bool>)] = &[
            (None, None),
            (Some(true), None),
            (Some(false), None),
            (None, Some(true)),
            (None, Some(false)),
            (Some(true), Some(true)),
            (Some(true), Some(false)),
            (Some(false), Some(true)),
            (Some(false), Some(false)),
        ];
        for &(above, left) in cases {
            let ctx = is_inter_ctx(above, left);
            assert!(ctx < IS_INTER_CONTEXTS);
            for is_inter_val in [0u8, 1u8] {
                let mut cdfs = TileCdfContext::new_from_defaults();
                let mut writer = SymbolWriter::new(false);
                write_is_inter(
                    &mut writer,
                    &mut cdfs,
                    is_inter_val,
                    ctx,
                    0,
                    false,
                    false,
                    false,
                )
                .expect("§8.3.2 ctx admissible on Arm 4");
                let _bytes = writer.finish();
            }
        }
    }

    /// `is_inter > 1` is outside the §3 binary alphabet — caller bug.
    #[test]
    fn write_is_inter_rejects_out_of_range_is_inter() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_is_inter(&mut writer, &mut cdfs, 2, 0, 0, false, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.20 Arm 1: caller-supplied `is_inter != 1` on the skip_mode
    /// arm is a caller bug — the spec forces `is_inter = 1`.
    #[test]
    fn write_is_inter_rejects_zero_on_skip_mode_arm() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_is_inter(&mut writer, &mut cdfs, 0, 0, 1, false, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.20 Arm 2: caller-supplied `is_inter` must equal
    /// `seg_ref_frame_is_inter as u8` — mismatch is a caller bug.
    #[test]
    fn write_is_inter_rejects_arm2_mismatch() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        // SEG_LVL_REF_FRAME active, FeatureData == INTRA_FRAME (encoded
        // as `seg_ref_frame_is_inter = false`) — caller MUST pass
        // is_inter = 0; passing 1 is a bug.
        let err = write_is_inter(&mut writer, &mut cdfs, 1, 0, 0, true, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.20 Arm 3: caller-supplied `is_inter != 1` on the
    /// SEG_LVL_GLOBALMV arm is a caller bug — the spec forces 1.
    #[test]
    fn write_is_inter_rejects_zero_on_globalmv_arm() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_is_inter(&mut writer, &mut cdfs, 0, 0, 0, false, false, true).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.20 Arm 4: `ctx >= IS_INTER_CONTEXTS = 4` on the
    /// fall-through arm is an invalid §8.3.2 ctx — caller bug.
    #[test]
    fn write_is_inter_rejects_out_of_range_ctx_on_else_arm() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_is_inter(
            &mut writer,
            &mut cdfs,
            0,
            IS_INTER_CONTEXTS,
            0,
            false,
            false,
            false,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.20 arm-ordering invariant: when Arm 1 (`skip_mode == 1`)
    /// fires, the SEG_LVL_REF_FRAME / SEG_LVL_GLOBALMV / ctx parameters
    /// are NOT consulted. So setting them to values that would
    /// otherwise reject (e.g. an out-of-range ctx) is fine — Arm 1
    /// short-circuits before any of them is checked.
    #[test]
    fn write_is_inter_arm1_skips_later_arm_checks() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        // out-of-range ctx, but Arm 1 fires first ⇒ no error.
        write_is_inter(
            &mut writer,
            &mut cdfs,
            1,
            999, // would normally reject if Arm 4 were reached
            1,   // Arm 1 active
            true,
            false,
            true,
        )
        .unwrap();
    }

    // -----------------------------------------------------------------
    // §5.11.10 read_skip_mode — write_skip_mode round-trips through
    // decode_skip_mode. The six-condition short-circuit set collapses
    // (per the decoder's own parameter surface) into three encoder
    // inputs — `seg_skip_mode_off`, `!skip_mode_present`, and a
    // small-block check derived locally from `sub_size`. The
    // fall-through `S()` arm gets both skip_mode = 0 and skip_mode = 1
    // round-trips plus exhaustive §8.3.2 ctx coverage.
    // -----------------------------------------------------------------

    /// §5.11.10 `seg_skip_mode_off = true` arm — collapsed
    /// `SEG_LVL_SKIP || SEG_LVL_REF_FRAME || SEG_LVL_GLOBALMV` —
    /// forces `skip_mode = 0` with no bits emitted. The decoder side
    /// (`decode_skip_mode` with `seg_skip_mode_off = true`) likewise
    /// short-circuits without a symbol read.
    #[test]
    fn write_skip_mode_seg_short_circuit_writes_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_skip_mode(&mut writer, &mut enc_cdfs, 0, 0, BLOCK_16X16, true, true).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        // No-symbol path — the decoder needs at least one byte to
        // initialise the symbol decoder.
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let pos_before = dec.position();
        let skip_mode = walker
            .decode_skip_mode(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, true, true)
            .unwrap();
        assert_eq!(skip_mode, 0, "§5.11.10 seg-arm: any-active ⇒ skip_mode = 0");
        assert_eq!(
            dec.position(),
            pos_before,
            "no symbol bit read on the seg short-circuit arm"
        );
    }

    /// §5.11.10 `!skip_mode_present` arm — the §5.9.21 frame-header
    /// scalar is false ⇒ `skip_mode = 0`, no bits emitted.
    #[test]
    fn write_skip_mode_present_false_short_circuit_writes_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_skip_mode(&mut writer, &mut enc_cdfs, 0, 0, BLOCK_16X16, false, false).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let pos_before = dec.position();
        let skip_mode = walker
            .decode_skip_mode(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false, false)
            .unwrap();
        assert_eq!(skip_mode, 0);
        assert_eq!(dec.position(), pos_before);
    }

    /// §5.11.10 small-block short-circuit: `Block_Width[ MiSize ] < 8`
    /// on `BLOCK_4X8` (width 4, height 8) ⇒ `skip_mode = 0`, no bits.
    #[test]
    fn write_skip_mode_small_width_short_circuit_writes_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_skip_mode(&mut writer, &mut enc_cdfs, 0, 0, BLOCK_4X8, false, true).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let pos_before = dec.position();
        let skip_mode = walker
            .decode_skip_mode(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_4X8, false, true)
            .unwrap();
        assert_eq!(skip_mode, 0);
        assert_eq!(dec.position(), pos_before);
    }

    /// §5.11.10 small-block short-circuit: `Block_Height[ MiSize ] < 8`
    /// on `BLOCK_8X4` (width 8, height 4) ⇒ `skip_mode = 0`, no bits.
    #[test]
    fn write_skip_mode_small_height_short_circuit_writes_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_skip_mode(&mut writer, &mut enc_cdfs, 0, 0, BLOCK_8X4, false, true).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let pos_before = dec.position();
        let skip_mode = walker
            .decode_skip_mode(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_8X4, false, true)
            .unwrap();
        assert_eq!(skip_mode, 0);
        assert_eq!(dec.position(), pos_before);
    }

    /// §5.11.10 small-block short-circuit: both dimensions < 8 on
    /// `BLOCK_4X4` ⇒ `skip_mode = 0`, no bits.
    #[test]
    fn write_skip_mode_small_4x4_short_circuit_writes_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_skip_mode(&mut writer, &mut enc_cdfs, 0, 0, BLOCK_4X4, false, true).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let pos_before = dec.position();
        let skip_mode = walker
            .decode_skip_mode(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_4X4, false, true)
            .unwrap();
        assert_eq!(skip_mode, 0);
        assert_eq!(dec.position(), pos_before);
    }

    /// §5.11.10 fall-through `S()` arm: `skip_mode = 0` at the frame
    /// origin (both neighbours unavailable ⇒ §8.3.2 ctx = 0).
    /// Round-trip through `decode_skip_mode` with §8.3 CDF adaptation
    /// engaged on both sides.
    #[test]
    fn write_skip_mode_zero_else_branch_round_trip_at_origin() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let ctx = skip_mode_ctx(0, 0);
        assert_eq!(ctx, 0, "origin neighbours unavailable ⇒ ctx = 0");
        write_skip_mode(&mut writer, &mut enc_cdfs, 0, ctx, BLOCK_16X16, false, true).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let skip_mode = walker
            .decode_skip_mode(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false, true)
            .unwrap();
        assert_eq!(skip_mode, 0);
    }

    /// §5.11.10 fall-through `S()` arm: `skip_mode = 1` at the frame
    /// origin (same ctx). Round-trip through the decoder.
    #[test]
    fn write_skip_mode_one_else_branch_round_trip_at_origin() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let ctx = skip_mode_ctx(0, 0);
        write_skip_mode(&mut writer, &mut enc_cdfs, 1, ctx, BLOCK_16X16, false, true).unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let skip_mode = walker
            .decode_skip_mode(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false, true)
            .unwrap();
        assert_eq!(skip_mode, 1);
    }

    /// §8.3.2 `skip_mode` ctx is the neighbour-`SkipModes[]` sum, in
    /// `0..SKIP_MODE_CONTEXTS = 3`. Walk every `(above_sm, left_sm)`
    /// combination (`{0,1} × {0,1}` plus the `(1,1)` upper-bound case)
    /// and confirm the writer accepts each ctx for both `skip_mode ∈
    /// {0, 1}` on the fall-through arm.
    #[test]
    fn write_skip_mode_accepts_every_8_3_2_ctx() {
        let cases: &[(u8, u8)] = &[(0, 0), (0, 1), (1, 0), (1, 1)];
        for &(above, left) in cases {
            let ctx = skip_mode_ctx(above, left);
            assert!(ctx < SKIP_MODE_CONTEXTS);
            for skip_mode_val in [0u8, 1u8] {
                let mut cdfs = TileCdfContext::new_from_defaults();
                let mut writer = SymbolWriter::new(false);
                write_skip_mode(
                    &mut writer,
                    &mut cdfs,
                    skip_mode_val,
                    ctx,
                    BLOCK_16X16,
                    false,
                    true,
                )
                .expect("§8.3.2 ctx admissible on the fall-through arm");
                let _bytes = writer.finish();
            }
        }
    }

    /// `skip_mode > 1` is outside the §3 binary alphabet — caller bug.
    #[test]
    fn write_skip_mode_rejects_out_of_range_skip_mode() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err =
            write_skip_mode(&mut writer, &mut cdfs, 2, 0, BLOCK_16X16, false, true).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// `sub_size >= BLOCK_SIZES` is outside the §5.11.5 `MiSize`
    /// domain — caller bug.
    #[test]
    fn write_skip_mode_rejects_out_of_range_sub_size() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err =
            write_skip_mode(&mut writer, &mut cdfs, 0, 0, BLOCK_SIZES, false, true).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.10 short-circuit arm: caller-supplied `skip_mode != 0` on
    /// any short-circuit arm is a caller bug — the spec forces
    /// `skip_mode = 0`.
    #[test]
    fn write_skip_mode_rejects_one_on_seg_arm() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err =
            write_skip_mode(&mut writer, &mut cdfs, 1, 0, BLOCK_16X16, true, true).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.10 short-circuit arm: caller-supplied `skip_mode != 0` on
    /// the `!skip_mode_present` arm is a caller bug.
    #[test]
    fn write_skip_mode_rejects_one_on_present_false_arm() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err =
            write_skip_mode(&mut writer, &mut cdfs, 1, 0, BLOCK_16X16, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.10 short-circuit arm: caller-supplied `skip_mode != 0` on
    /// the small-block (Block_W < 8 || Block_H < 8) arm is a caller
    /// bug.
    #[test]
    fn write_skip_mode_rejects_one_on_small_block_arm() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err =
            write_skip_mode(&mut writer, &mut cdfs, 1, 0, BLOCK_4X4, false, true).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.10 fall-through arm: `ctx >= SKIP_MODE_CONTEXTS = 3` is
    /// an invalid §8.3.2 ctx — caller bug.
    #[test]
    fn write_skip_mode_rejects_out_of_range_ctx_on_else_arm() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_skip_mode(
            &mut writer,
            &mut cdfs,
            0,
            SKIP_MODE_CONTEXTS,
            BLOCK_16X16,
            false,
            true,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.10 arm-ordering invariant: when any short-circuit arm
    /// fires (`seg_skip_mode_off`, `!skip_mode_present`, or
    /// small-block), the `ctx` parameter is NOT consulted. So an
    /// out-of-range ctx is fine on a short-circuit arm.
    #[test]
    fn write_skip_mode_short_circuit_skips_ctx_check() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        // out-of-range ctx, but seg-arm fires first ⇒ no error.
        write_skip_mode(
            &mut writer,
            &mut cdfs,
            0,
            999, // would normally reject if fall-through reached
            BLOCK_16X16,
            true, // seg arm active
            true,
        )
        .unwrap();
    }

    // -----------------------------------------------------------------
    // §5.11.19 read_inter_segment_id — write_inter_segment_id
    // round-trips through PartitionWalker::decode_inter_segment_id.
    // -----------------------------------------------------------------

    /// Constant `LosslessArray` shape the round-trip tests share —
    /// all-false matches the default §6.8.2 derivation and lets us
    /// inspect the returned `lossless` flag without setting up the
    /// quantiser path.
    const LOSSLESS_ALL_FALSE: [bool; MAX_SEGMENTS] = [false; MAX_SEGMENTS];

    /// Helper — pad an empty payload (the no-S() arms) so
    /// `SymbolDecoder::init_symbol` accepts the buffer. Matches the
    /// pattern in the `write_skip_seg_short_circuit` /
    /// `write_segment_id_skip_short_circuit` tests.
    fn pad_no_symbol(bytes: Vec<u8>) -> Vec<u8> {
        if bytes.is_empty() {
            vec![0u8]
        } else {
            bytes
        }
    }

    /// Arm 1: `!segmentation_enabled` — no bits written, no bits read,
    /// `segment_id == 0` on both sides.
    #[test]
    fn write_inter_segment_id_disabled_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_inter_segment_id(
            &mut writer,
            &mut enc_cdfs,
            /* segment_id = */ 0,
            /* pred = */ 0,
            /* seg_id_read_ctx = */ 0,
            /* seg_pred_ctx = */ 0,
            /* pre_skip = */ true,
            /* skip = */ 0,
            /* seg_id_predicted = */ 0,
            /* segmentation_enabled = */ false,
            /* segmentation_update_map = */ true,
            /* segmentation_temporal_update = */ true,
            /* seg_id_pre_skip = */ true,
            /* predicted_segment_id = */ 0,
            /* last_active_seg_id = */ 7,
        )
        .unwrap();
        let bytes = pad_no_symbol(writer.finish());

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), true).unwrap();
        let pos_before = dec.position();
        let (sid, lossless) = walker
            .decode_inter_segment_id(
                &mut dec,
                &mut dec_cdfs,
                /* mi_row = */ 0,
                /* mi_col = */ 0,
                BLOCK_16X16,
                /* pre_skip = */ true,
                /* skip = */ 0,
                /* seg_id_pre_skip = */ true,
                /* segmentation_enabled = */ false,
                /* segmentation_update_map = */ true,
                /* segmentation_temporal_update = */ true,
                /* predicted_segment_id = */ 0,
                /* last_active_seg_id = */ 7,
                &LOSSLESS_ALL_FALSE,
            )
            .unwrap();
        assert_eq!(sid, 0);
        assert!(!lossless);
        assert_eq!(dec.position(), pos_before, "no S() read on disabled arm");
    }

    /// Arm 2: `!segmentation_update_map` — no bits; `segment_id ==
    /// predicted_segment_id` on both sides.
    #[test]
    fn write_inter_segment_id_no_update_map_adopts_predicted() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_inter_segment_id(
            &mut writer,
            &mut enc_cdfs,
            /* segment_id = */ 4,
            /* pred = */ 0,
            /* seg_id_read_ctx = */ 0,
            /* seg_pred_ctx = */ 0,
            /* pre_skip = */ false,
            /* skip = */ 0,
            /* seg_id_predicted = */ 0,
            /* segmentation_enabled = */ true,
            /* segmentation_update_map = */ false,
            /* segmentation_temporal_update = */ false,
            /* seg_id_pre_skip = */ false,
            /* predicted_segment_id = */ 4,
            /* last_active_seg_id = */ 7,
        )
        .unwrap();
        let bytes = pad_no_symbol(writer.finish());

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), true).unwrap();
        let pos_before = dec.position();
        let (sid, _) = walker
            .decode_inter_segment_id(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                /* pre_skip = */ false,
                /* skip = */ 0,
                /* seg_id_pre_skip = */ false,
                /* segmentation_enabled = */ true,
                /* segmentation_update_map = */ false,
                /* segmentation_temporal_update = */ false,
                /* predicted_segment_id = */ 4,
                7,
                &LOSSLESS_ALL_FALSE,
            )
            .unwrap();
        assert_eq!(sid, 4);
        assert_eq!(dec.position(), pos_before, "no S() read on !update_map arm");
    }

    /// Arm 3: `pre_skip && !seg_id_pre_skip` early-exit — no bits;
    /// `segment_id == 0` on both sides.
    #[test]
    fn write_inter_segment_id_pre_skip_early_exit_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_inter_segment_id(
            &mut writer,
            &mut enc_cdfs,
            /* segment_id = */ 0,
            /* pred = */ 0,
            /* seg_id_read_ctx = */ 0,
            /* seg_pred_ctx = */ 0,
            /* pre_skip = */ true,
            /* skip = */ 0,
            /* seg_id_predicted = */ 0,
            /* segmentation_enabled = */ true,
            /* segmentation_update_map = */ true,
            /* segmentation_temporal_update = */ false,
            /* seg_id_pre_skip = */ false,
            /* predicted_segment_id = */ 3,
            /* last_active_seg_id = */ 7,
        )
        .unwrap();
        let bytes = pad_no_symbol(writer.finish());

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), true).unwrap();
        let pos_before = dec.position();
        let (sid, _) = walker
            .decode_inter_segment_id(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                /* pre_skip = */ true,
                /* skip = */ 0,
                /* seg_id_pre_skip = */ false,
                /* segmentation_enabled = */ true,
                /* segmentation_update_map = */ true,
                /* segmentation_temporal_update = */ false,
                /* predicted_segment_id = */ 3,
                7,
                &LOSSLESS_ALL_FALSE,
            )
            .unwrap();
        assert_eq!(sid, 0);
        assert_eq!(
            dec.position(),
            pos_before,
            "no S() read on pre-skip early-exit"
        );
    }

    /// Arm 4 (`!pre_skip && skip`): the inner `read_segment_id()`
    /// fires with `skip = 1`, which §5.11.9 short-circuits to
    /// `segment_id = pred` (no S() coded). Round-trip across the
    /// 8-symbol alphabet of `pred` values at the frame origin (where
    /// `pred = 0` because no neighbours), then with a stamped
    /// neighbour where `pred != 0`.
    #[test]
    fn write_inter_segment_id_arm4_skip_branch_round_trip_at_origin() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        // At origin: all neighbours absent ⇒ §5.11.9 pred = 0.
        write_inter_segment_id(
            &mut writer,
            &mut enc_cdfs,
            /* segment_id = */ 0,
            /* pred = */ 0,
            /* seg_id_read_ctx = */ 0,
            /* seg_pred_ctx = */ 0,
            /* pre_skip = */ false,
            /* skip = */ 1,
            /* seg_id_predicted = */ 0,
            /* segmentation_enabled = */ true,
            /* segmentation_update_map = */ true,
            /* segmentation_temporal_update = */ true,
            /* seg_id_pre_skip = */ true,
            /* predicted_segment_id = */ 0,
            /* last_active_seg_id = */ 7,
        )
        .unwrap();
        let bytes = pad_no_symbol(writer.finish());

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), true).unwrap();
        let pos_before = dec.position();
        let (sid, _) = walker
            .decode_inter_segment_id(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                /* pre_skip = */ false,
                /* skip = */ 1,
                /* seg_id_pre_skip = */ true,
                /* segmentation_enabled = */ true,
                /* segmentation_update_map = */ true,
                /* segmentation_temporal_update = */ true,
                /* predicted_segment_id = */ 0,
                7,
                &LOSSLESS_ALL_FALSE,
            )
            .unwrap();
        assert_eq!(sid, 0);
        assert_eq!(
            dec.position(),
            pos_before,
            "Arm 4: no S() read (inner write_segment_id short-circuits on skip=1)"
        );
    }

    /// Arm 5 with `seg_id_predicted == 1` — emits exactly one S()
    /// (`seg_id_predicted` against `TileSegmentIdPredictedCdf`); the
    /// id itself is the adopted `predicted_segment_id`, no further
    /// bits. Round-trip across the alphabet of `predicted_segment_id`
    /// values via the temporal-update arm at the frame origin
    /// (`seg_pred_ctx = 0` because both neighbour ctx arrays are 0).
    #[test]
    fn write_inter_segment_id_temporal_update_adopt_round_trip() {
        for predicted in 0..8u8 {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_inter_segment_id(
                &mut writer,
                &mut enc_cdfs,
                /* segment_id = */ predicted,
                /* pred = */ 0, // unused on the adopt branch
                /* seg_id_read_ctx = */ 0,
                /* seg_pred_ctx = */ 0,
                /* pre_skip = */ false,
                /* skip = */ 0,
                /* seg_id_predicted = */ 1,
                /* segmentation_enabled = */ true,
                /* segmentation_update_map = */ true,
                /* segmentation_temporal_update = */ true,
                /* seg_id_pre_skip = */ true,
                /* predicted_segment_id = */ predicted,
                /* last_active_seg_id = */ 7,
            )
            .unwrap();
            let bytes = writer.finish();

            let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            let (sid, _) = walker
                .decode_inter_segment_id(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    /* pre_skip = */ false,
                    /* skip = */ 0,
                    /* seg_id_pre_skip = */ true,
                    /* segmentation_enabled = */ true,
                    /* segmentation_update_map = */ true,
                    /* segmentation_temporal_update = */ true,
                    /* predicted_segment_id = */ predicted,
                    7,
                    &LOSSLESS_ALL_FALSE,
                )
                .unwrap();
            assert_eq!(
                sid, predicted,
                "adopt round trip failed at predicted = {}",
                predicted
            );
        }
    }

    /// Arm 5 with `seg_id_predicted == 0` — emits two S() values:
    /// `seg_id_predicted = 0` against `TileSegmentIdPredictedCdf` then
    /// the §5.11.9 `S()` against `TileSegmentIdCdf` for the literal
    /// id. Round-trip across the alphabet of `segment_id` values at
    /// the frame origin (where the §5.11.9 pred = 0).
    #[test]
    fn write_inter_segment_id_temporal_update_literal_round_trip() {
        for sid_in in 0..8u8 {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_inter_segment_id(
                &mut writer,
                &mut enc_cdfs,
                /* segment_id = */ sid_in,
                /* pred = */ 0,
                /* seg_id_read_ctx = */ 0,
                /* seg_pred_ctx = */ 0,
                /* pre_skip = */ false,
                /* skip = */ 0,
                /* seg_id_predicted = */ 0,
                /* segmentation_enabled = */ true,
                /* segmentation_update_map = */ true,
                /* segmentation_temporal_update = */ true,
                /* seg_id_pre_skip = */ true,
                /* predicted_segment_id = */ 0,
                /* last_active_seg_id = */ 7,
            )
            .unwrap();
            let bytes = writer.finish();

            let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            let (sid_out, _) = walker
                .decode_inter_segment_id(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    /* pre_skip = */ false,
                    /* skip = */ 0,
                    /* seg_id_pre_skip = */ true,
                    /* segmentation_enabled = */ true,
                    /* segmentation_update_map = */ true,
                    /* segmentation_temporal_update = */ true,
                    /* predicted_segment_id = */ 0,
                    7,
                    &LOSSLESS_ALL_FALSE,
                )
                .unwrap();
            assert_eq!(
                sid_out, sid_in,
                "literal round trip failed at sid = {}",
                sid_in
            );
        }
    }

    /// Arm 6 (`segmentation_temporal_update == 0`, fall-through) —
    /// single §5.11.9 S() for the literal id; no `seg_id_predicted`
    /// symbol. Round-trip across the alphabet at the frame origin.
    #[test]
    fn write_inter_segment_id_fallthrough_round_trip() {
        for sid_in in 0..8u8 {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_inter_segment_id(
                &mut writer,
                &mut enc_cdfs,
                /* segment_id = */ sid_in,
                /* pred = */ 0,
                /* seg_id_read_ctx = */ 0,
                /* seg_pred_ctx = */ 0,
                /* pre_skip = */ false,
                /* skip = */ 0,
                /* seg_id_predicted = */ 0,
                /* segmentation_enabled = */ true,
                /* segmentation_update_map = */ true,
                /* segmentation_temporal_update = */ false,
                /* seg_id_pre_skip = */ true,
                /* predicted_segment_id = */ 0,
                /* last_active_seg_id = */ 7,
            )
            .unwrap();
            let bytes = writer.finish();

            let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            let (sid_out, _) = walker
                .decode_inter_segment_id(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    /* pre_skip = */ false,
                    /* skip = */ 0,
                    /* seg_id_pre_skip = */ true,
                    /* segmentation_enabled = */ true,
                    /* segmentation_update_map = */ true,
                    /* segmentation_temporal_update = */ false,
                    /* predicted_segment_id = */ 0,
                    7,
                    &LOSSLESS_ALL_FALSE,
                )
                .unwrap();
            assert_eq!(
                sid_out, sid_in,
                "arm 6 fall-through round trip failed at sid = {}",
                sid_in
            );
        }
    }

    // -----------------------------------------------------------------
    // §5.11.19 caller-bug shapes — every mismatch surfaces as
    // PartitionWalkOutOfRange.
    // -----------------------------------------------------------------

    /// Arm 1 rejection: `!segmentation_enabled` forces `segment_id =
    /// 0`; a non-zero caller value is a bug.
    #[test]
    fn write_inter_segment_id_arm1_rejects_nonzero_segment_id() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_segment_id(
            &mut writer,
            &mut cdfs,
            /* segment_id = */ 3,
            0,
            0,
            0,
            true,
            0,
            0,
            /* segmentation_enabled = */ false,
            true,
            true,
            true,
            0,
            7,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm 2 rejection: `!segmentation_update_map` forces
    /// `segment_id = predicted_segment_id`.
    #[test]
    fn write_inter_segment_id_arm2_rejects_segment_id_mismatch() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_segment_id(
            &mut writer,
            &mut cdfs,
            /* segment_id = */ 2,
            0,
            0,
            0,
            false,
            0,
            0,
            true,
            /* segmentation_update_map = */ false,
            false,
            false,
            /* predicted_segment_id = */ 5,
            7,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm 3 rejection: `pre_skip && !seg_id_pre_skip` forces
    /// `segment_id = 0`.
    #[test]
    fn write_inter_segment_id_arm3_rejects_nonzero_segment_id() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_segment_id(
            &mut writer,
            &mut cdfs,
            /* segment_id = */ 3,
            0,
            0,
            0,
            /* pre_skip = */ true,
            0,
            0,
            true,
            true,
            false,
            /* seg_id_pre_skip = */ false,
            3,
            7,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm 4 rejection: `!pre_skip && skip` delegates to
    /// `write_segment_id` with `skip = 1`, which requires
    /// `segment_id == pred`. A mismatch surfaces from the inner
    /// writer.
    #[test]
    fn write_inter_segment_id_arm4_rejects_pred_mismatch() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_segment_id(
            &mut writer,
            &mut cdfs,
            /* segment_id = */ 3,
            /* pred = */ 1, // mismatched — must equal segment_id on skip arm
            0,
            0,
            /* pre_skip = */ false,
            /* skip = */ 1,
            0,
            true,
            true,
            true,
            true,
            0,
            7,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm 5 (`seg_id_predicted == 1`) rejection: adoption requires
    /// `segment_id == predicted_segment_id`.
    #[test]
    fn write_inter_segment_id_arm5_adopt_rejects_predicted_mismatch() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_segment_id(
            &mut writer,
            &mut cdfs,
            /* segment_id = */ 4,
            0,
            0,
            0,
            false,
            0,
            /* seg_id_predicted = */ 1,
            true,
            true,
            true,
            true,
            /* predicted_segment_id = */ 2,
            7,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm 5 rejection: `seg_id_predicted > 1` is outside the binary
    /// alphabet.
    #[test]
    fn write_inter_segment_id_arm5_rejects_out_of_range_seg_id_predicted() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_segment_id(
            &mut writer,
            &mut cdfs,
            0,
            0,
            0,
            0,
            false,
            0,
            /* seg_id_predicted = */ 2,
            true,
            true,
            true,
            true,
            0,
            7,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm 5 rejection: `seg_pred_ctx >= SEGMENT_ID_PREDICTED_CONTEXTS
    /// = 3`.
    #[test]
    fn write_inter_segment_id_arm5_rejects_out_of_range_seg_pred_ctx() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_segment_id(
            &mut writer,
            &mut cdfs,
            0,
            0,
            0,
            /* seg_pred_ctx = */ SEGMENT_ID_PREDICTED_CONTEXTS,
            false,
            0,
            0,
            true,
            true,
            true,
            true,
            0,
            7,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Up-front rejection: `last_active_seg_id >= MAX_SEGMENTS`.
    #[test]
    fn write_inter_segment_id_rejects_out_of_range_last_active() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_segment_id(
            &mut writer,
            &mut cdfs,
            0,
            0,
            0,
            0,
            false,
            0,
            0,
            true,
            true,
            false,
            true,
            0,
            /* last_active_seg_id = */ MAX_SEGMENTS as u8,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Up-front rejection: `segment_id > last_active_seg_id` (outside
    /// the alphabet).
    #[test]
    fn write_inter_segment_id_rejects_segment_id_out_of_alphabet() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_segment_id(
            &mut writer,
            &mut cdfs,
            /* segment_id = */ 5,
            0,
            0,
            0,
            false,
            0,
            0,
            true,
            true,
            false,
            true,
            0,
            /* last_active_seg_id = */ 3,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Up-front rejection: `pred > last_active_seg_id`.
    #[test]
    fn write_inter_segment_id_rejects_pred_out_of_alphabet() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_segment_id(
            &mut writer,
            &mut cdfs,
            0,
            /* pred = */ 5,
            0,
            0,
            false,
            0,
            0,
            true,
            true,
            false,
            true,
            0,
            /* last_active_seg_id = */ 3,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Up-front rejection: `predicted_segment_id > last_active_seg_id`.
    #[test]
    fn write_inter_segment_id_rejects_predicted_segment_id_out_of_alphabet() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_segment_id(
            &mut writer,
            &mut cdfs,
            0,
            0,
            0,
            0,
            false,
            0,
            0,
            true,
            true,
            false,
            true,
            /* predicted_segment_id = */ 5,
            /* last_active_seg_id = */ 3,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm 5 admissibility across every §8.3.2 `seg_pred_ctx` ∈
    /// `0..SEGMENT_ID_PREDICTED_CONTEXTS = 3`. Round-trips the
    /// `seg_id_predicted == 1` branch (cheapest — one S() in, no inner
    /// `write_segment_id`).
    #[test]
    fn write_inter_segment_id_accepts_every_seg_pred_ctx() {
        for ctx in 0..SEGMENT_ID_PREDICTED_CONTEXTS {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_inter_segment_id(
                &mut writer,
                &mut enc_cdfs,
                /* segment_id = */ 3,
                0,
                0,
                /* seg_pred_ctx = */ ctx,
                false,
                0,
                /* seg_id_predicted = */ 1,
                true,
                true,
                true,
                true,
                /* predicted_segment_id = */ 3,
                7,
            )
            .unwrap();
            let _bytes = writer.finish();
        }
    }

    // -----------------------------------------------------------------
    // §5.11.18 inter_frame_mode_info — write_inter_frame_mode_info_prefix
    // round-trips through the decoder's matching composed call sequence:
    //   decode_inter_segment_id(pre_skip=true)
    //   decode_skip_mode
    //   if skip_mode==0 → decode_skip; else force skip=1
    //   if !seg_id_pre_skip → decode_inter_segment_id(pre_skip=false)
    //   decode_is_inter
    // Each test verifies (a) the writer's per-element committed values
    // match the decoder's sequential reads in the same order, and
    // (b) the writer's aggregate `InterFrameModeInfoPrefix` matches the
    // expected per-arm scalars.
    // -----------------------------------------------------------------

    /// Replay the writer's byte run through the same composed
    /// decoder-side call sequence the §5.11.18 spec body specifies, and
    /// return the four decoded scalars `(segment_id_after_post, skip_mode,
    /// skip, is_inter)` for the caller to assert against.
    #[allow(clippy::too_many_arguments)]
    fn replay_inter_frame_mode_info_prefix(
        bytes: Vec<u8>,
        cdf_disable_update: bool,
        sub_size: usize,
        seg_id_pre_skip: bool,
        segmentation_enabled: bool,
        segmentation_update_map: bool,
        segmentation_temporal_update: bool,
        predicted_segment_id: u8,
        last_active_seg_id: u8,
        skip_mode_present: bool,
        seg_skip_mode_off: bool,
        seg_skip_active: bool,
        seg_ref_frame_active: bool,
        seg_ref_frame_is_inter: bool,
        seg_globalmv_active: bool,
    ) -> (u8, u8, u8, u8) {
        let pad = pad_no_symbol(bytes);
        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), cdf_disable_update).unwrap();
        // §5.11.18 line 11.
        let (mut segment_id, _) = walker
            .decode_inter_segment_id(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                sub_size,
                /* pre_skip = */ true,
                /* skip = */ 0,
                seg_id_pre_skip,
                segmentation_enabled,
                segmentation_update_map,
                segmentation_temporal_update,
                predicted_segment_id,
                last_active_seg_id,
                &LOSSLESS_ALL_FALSE,
            )
            .unwrap();
        // §5.11.18 line 12.
        let skip_mode = walker
            .decode_skip_mode(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                sub_size,
                seg_skip_mode_off,
                skip_mode_present,
            )
            .unwrap();
        // §5.11.18 lines 13-14.
        let skip: u8 = if skip_mode != 0 {
            1
        } else {
            walker
                .decode_skip(&mut dec, &mut dec_cdfs, 0, 0, sub_size, seg_skip_active)
                .unwrap()
        };
        // §5.11.18 lines 15-16.
        if !seg_id_pre_skip {
            let (sid, _) = walker
                .decode_inter_segment_id(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    sub_size,
                    /* pre_skip = */ false,
                    skip,
                    seg_id_pre_skip,
                    segmentation_enabled,
                    segmentation_update_map,
                    segmentation_temporal_update,
                    predicted_segment_id,
                    last_active_seg_id,
                    &LOSSLESS_ALL_FALSE,
                )
                .unwrap();
            segment_id = sid;
        }
        // §5.11.18 line 22.
        let is_inter = walker
            .decode_is_inter(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                sub_size,
                skip_mode,
                seg_ref_frame_active,
                seg_ref_frame_is_inter,
                seg_globalmv_active,
            )
            .unwrap();
        (segment_id, skip_mode, skip, is_inter)
    }

    /// `segmentation_enabled = false` short-circuit on every segment_id
    /// arm: pre-skip and post-skip §5.11.19 writes emit no bits;
    /// `skip_mode_present = false` short-circuits §5.11.10 (no bits);
    /// `read_skip` fires; `read_is_inter` fires. Sub_size = BLOCK_16X16
    /// (above the 8×8 §5.11.10 small-block threshold).
    #[test]
    fn write_inter_frame_mode_info_prefix_segless_round_trip() {
        for (skip_in, is_inter_in) in [(0u8, 0u8), (0u8, 1u8), (1u8, 0u8), (1u8, 1u8)]
            .iter()
            .copied()
        {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let prefix = write_inter_frame_mode_info_prefix(
                &mut writer,
                &mut enc_cdfs,
                BLOCK_16X16,
                /* segment_id = */ 0,
                /* skip_mode = */ 0,
                /* skip = */ skip_in,
                /* is_inter = */ is_inter_in,
                /* seg_pred = */ 0,
                /* seg_id_predicted = */ 0,
                /* skip_mode_ctx = */ 0,
                /* skip_ctx = */ skip_ctx(0, 0),
                /* seg_id_read_ctx = */ 0,
                /* seg_pred_ctx = */ 0,
                /* is_inter_ctx = */ is_inter_ctx(None, None),
                /* seg_skip_mode_off = */ false,
                /* seg_skip_active = */ false,
                /* seg_ref_frame_active = */ false,
                /* seg_ref_frame_is_inter = */ false,
                /* seg_globalmv_active = */ false,
                /* segmentation_enabled = */ false,
                /* segmentation_update_map = */ false,
                /* segmentation_temporal_update = */ false,
                /* seg_id_pre_skip = */ true,
                /* predicted_segment_id = */ 0,
                /* last_active_seg_id = */ 7,
                /* skip_mode_present = */ false,
                &LOSSLESS_ALL_FALSE,
                &InterFrameDeltaSiteInputs::default(),
            )
            .unwrap();
            assert_eq!(prefix.segment_id, 0);
            assert_eq!(prefix.skip_mode, 0);
            assert_eq!(prefix.skip, skip_in);
            assert_eq!(prefix.is_inter, is_inter_in);
            assert!(!prefix.lossless);

            let (sid, skip_mode, skip, is_inter) = replay_inter_frame_mode_info_prefix(
                writer.finish(),
                false,
                BLOCK_16X16,
                true,
                false,
                false,
                false,
                0,
                7,
                false,
                false,
                false,
                false,
                false,
                false,
            );
            assert_eq!(sid, 0);
            assert_eq!(skip_mode, 0);
            assert_eq!(skip, skip_in);
            assert_eq!(is_inter, is_inter_in);
        }
    }

    /// `skip_mode == 1` arm: §5.11.10 emits one S(), §5.11.11 `read_skip`
    /// is skipped (skip forced to 1), §5.11.20 `read_is_inter` is forced
    /// to 1 (no bits). The pre-skip segment_id write emits no bits when
    /// segmentation is disabled.
    #[test]
    fn write_inter_frame_mode_info_prefix_skip_mode_round_trip() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let prefix = write_inter_frame_mode_info_prefix(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            /* segment_id = */ 0,
            /* skip_mode = */ 1,
            /* skip = */ 1,
            /* is_inter = */ 1,
            /* seg_pred = */ 0,
            /* seg_id_predicted = */ 0,
            /* skip_mode_ctx = */ 0,
            /* skip_ctx = */ 0,
            /* seg_id_read_ctx = */ 0,
            /* seg_pred_ctx = */ 0,
            /* is_inter_ctx = */ 0,
            /* seg_skip_mode_off = */ false,
            /* seg_skip_active = */ false,
            /* seg_ref_frame_active = */ false,
            /* seg_ref_frame_is_inter = */ false,
            /* seg_globalmv_active = */ false,
            /* segmentation_enabled = */ false,
            /* segmentation_update_map = */ false,
            /* segmentation_temporal_update = */ false,
            /* seg_id_pre_skip = */ true,
            /* predicted_segment_id = */ 0,
            /* last_active_seg_id = */ 7,
            /* skip_mode_present = */ true,
            &LOSSLESS_ALL_FALSE,
            &InterFrameDeltaSiteInputs::default(),
        )
        .unwrap();
        assert_eq!(prefix.segment_id, 0);
        assert_eq!(prefix.skip_mode, 1);
        assert_eq!(prefix.skip, 1);
        assert_eq!(prefix.is_inter, 1);

        let (sid, skip_mode, skip, is_inter) = replay_inter_frame_mode_info_prefix(
            writer.finish(),
            false,
            BLOCK_16X16,
            true,
            false,
            false,
            false,
            0,
            7,
            true,
            false,
            false,
            false,
            false,
            false,
        );
        assert_eq!(sid, 0);
        assert_eq!(skip_mode, 1);
        assert_eq!(skip, 1);
        assert_eq!(is_inter, 1);
    }

    /// `!seg_id_pre_skip` arm: both §5.11.19 calls fire. The post-skip
    /// call services the actual segment_id read (here through the
    /// `segmentation_temporal_update` adopt branch, no inner literal id
    /// read). `skip = 0` ⇒ the post-skip arm's "skip-block" branch
    /// (Arm 4 of write_inter_segment_id) does NOT fire.
    #[test]
    fn write_inter_frame_mode_info_prefix_post_skip_arm_round_trip() {
        // Pre-skip side runs through Arm 3 (`pre_skip && !seg_id_pre_skip`)
        // ⇒ no bits. Post-skip side hits the temporal-update adopt arm.
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let prefix = write_inter_frame_mode_info_prefix(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            /* segment_id = */ 3,
            /* skip_mode = */ 0,
            /* skip = */ 0,
            /* is_inter = */ 1,
            /* seg_pred = */ 0,
            /* seg_id_predicted = */ 1,
            /* skip_mode_ctx = */ 0,
            /* skip_ctx = */ 0,
            /* seg_id_read_ctx = */ 0,
            /* seg_pred_ctx = */ 0,
            /* is_inter_ctx = */ 0,
            /* seg_skip_mode_off = */ false,
            /* seg_skip_active = */ false,
            /* seg_ref_frame_active = */ false,
            /* seg_ref_frame_is_inter = */ false,
            /* seg_globalmv_active = */ false,
            /* segmentation_enabled = */ true,
            /* segmentation_update_map = */ true,
            /* segmentation_temporal_update = */ true,
            /* seg_id_pre_skip = */ false,
            /* predicted_segment_id = */ 3,
            /* last_active_seg_id = */ 7,
            /* skip_mode_present = */ true,
            &LOSSLESS_ALL_FALSE,
            &InterFrameDeltaSiteInputs::default(),
        )
        .unwrap();
        assert_eq!(prefix.segment_id, 3);
        assert_eq!(prefix.skip_mode, 0);
        assert_eq!(prefix.skip, 0);
        assert_eq!(prefix.is_inter, 1);

        let (sid, skip_mode, skip, is_inter) = replay_inter_frame_mode_info_prefix(
            writer.finish(),
            false,
            BLOCK_16X16,
            false,
            true,
            true,
            true,
            3,
            7,
            true,
            false,
            false,
            false,
            false,
            false,
        );
        assert_eq!(sid, 3);
        assert_eq!(skip_mode, 0);
        assert_eq!(skip, 0);
        assert_eq!(is_inter, 1);
    }

    /// SEG_LVL_REF_FRAME active arm: `seg_skip_mode_off = true` forces
    /// skip_mode = 0 (no bits); `seg_skip_active = true` forces
    /// `decode_skip` to short-circuit to skip = 1 (no bits);
    /// `seg_ref_frame_active = true` forces is_inter to match
    /// `seg_ref_frame_is_inter` (no bits). The only S() in the byte run
    /// is the pre-skip segment_id under the temporal-update adopt arm.
    #[test]
    fn write_inter_frame_mode_info_prefix_seg_ref_frame_round_trip() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        // segmentation enabled + temporal_update so the pre-skip call
        // emits one S() (seg_id_predicted = 1 adopts the predicted id).
        let prefix = write_inter_frame_mode_info_prefix(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            /* segment_id = */ 2,
            /* skip_mode = */ 0,
            /* skip = */ 1,
            /* is_inter = */ 1, // SEG_LVL_REF_FRAME = inter
            /* seg_pred = */ 0,
            /* seg_id_predicted = */ 1,
            /* skip_mode_ctx = */ 0,
            /* skip_ctx = */ 0,
            /* seg_id_read_ctx = */ 0,
            /* seg_pred_ctx = */ 0,
            /* is_inter_ctx = */ 0,
            /* seg_skip_mode_off = */ true,
            /* seg_skip_active = */ true,
            /* seg_ref_frame_active = */ true,
            /* seg_ref_frame_is_inter = */ true,
            /* seg_globalmv_active = */ false,
            /* segmentation_enabled = */ true,
            /* segmentation_update_map = */ true,
            /* segmentation_temporal_update = */ true,
            /* seg_id_pre_skip = */ true,
            /* predicted_segment_id = */ 2,
            /* last_active_seg_id = */ 7,
            /* skip_mode_present = */ true,
            &LOSSLESS_ALL_FALSE,
            &InterFrameDeltaSiteInputs::default(),
        )
        .unwrap();
        assert_eq!(prefix.segment_id, 2);
        assert_eq!(prefix.skip_mode, 0);
        assert_eq!(prefix.skip, 1);
        assert_eq!(prefix.is_inter, 1);

        let (sid, skip_mode, skip, is_inter) = replay_inter_frame_mode_info_prefix(
            writer.finish(),
            false,
            BLOCK_16X16,
            true,
            true,
            true,
            true,
            2,
            7,
            true,
            true,
            true,
            true,
            true,
            false,
        );
        assert_eq!(sid, 2);
        assert_eq!(skip_mode, 0);
        assert_eq!(skip, 1);
        assert_eq!(is_inter, 1);
    }

    /// Up-front rejection: `last_active_seg_id >= MAX_SEGMENTS`.
    #[test]
    fn write_inter_frame_mode_info_prefix_rejects_out_of_range_last_active() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_frame_mode_info_prefix(
            &mut writer,
            &mut cdfs,
            BLOCK_16X16,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            true,
            0,
            /* last_active_seg_id = */ MAX_SEGMENTS as u8,
            false,
            &LOSSLESS_ALL_FALSE,
            &InterFrameDeltaSiteInputs::default(),
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// `skip > 1` is outside the §3 binary alphabet — caller bug.
    /// (Caught after the §5.11.10 write, since the dispatcher checks
    /// `skip` after `write_skip_mode`. The pre-skip and skip_mode writes
    /// commit but the bytes are discarded by the caller on the err.)
    #[test]
    fn write_inter_frame_mode_info_prefix_rejects_out_of_range_skip() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_frame_mode_info_prefix(
            &mut writer,
            &mut cdfs,
            BLOCK_16X16,
            0,
            0,
            /* skip = */ 2,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            true,
            0,
            7,
            false,
            &LOSSLESS_ALL_FALSE,
            &InterFrameDeltaSiteInputs::default(),
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// `skip_mode == 1` but `skip != 1` is a caller bug — §5.11.18
    /// forces `skip = 1` on the skip_mode arm.
    #[test]
    fn write_inter_frame_mode_info_prefix_rejects_skip_mode_mismatch() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_inter_frame_mode_info_prefix(
            &mut writer,
            &mut cdfs,
            BLOCK_16X16,
            0,
            /* skip_mode = */ 1,
            /* skip = */ 0,
            1,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            true,
            0,
            7,
            true,
            &LOSSLESS_ALL_FALSE,
            &InterFrameDeltaSiteInputs::default(),
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// `Lossless = LosslessArray[ segment_id ]` is returned in the
    /// aggregate. Verify the dispatcher passes the lookup through
    /// unchanged for both true and false entries.
    #[test]
    fn write_inter_frame_mode_info_prefix_surfaces_lossless_lookup() {
        let mut lossless = [false; MAX_SEGMENTS];
        lossless[3] = true;
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let prefix = write_inter_frame_mode_info_prefix(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            /* segment_id = */ 3,
            0,
            0,
            0,
            0,
            1, // seg_id_predicted = 1, adopt predicted_segment_id
            0,
            skip_ctx(0, 0),
            0,
            0,
            is_inter_ctx(None, None),
            false,
            false,
            false,
            false,
            false,
            true, // segmentation_enabled
            true, // segmentation_update_map
            true, // segmentation_temporal_update
            true, // seg_id_pre_skip
            /* predicted_segment_id = */ 3,
            7,
            false, // skip_mode_present
            &lossless,
            &InterFrameDeltaSiteInputs::default(),
        )
        .unwrap();
        assert_eq!(prefix.segment_id, 3);
        assert!(
            prefix.lossless,
            "LosslessArray[3] == true ⇒ lossless = true"
        );
    }

    /// Default `InterFrameDeltaSiteInputs` (`enable_cdef = false`,
    /// `read_deltas = false`, zero deltas) is the all-short-circuit
    /// arm for §5.11.18 lines 18-20: no bits emitted on any of the
    /// three calls, prefix `cdef_idx == None`, `reduced_delta_q_index
    /// == 0`, `reduced_delta_lf == [0; FRAME_LF_COUNT]`. The §5.11.18
    /// line 22 `read_is_inter` still fires through.
    #[test]
    fn write_inter_frame_mode_info_prefix_delta_site_default_no_bits() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let prefix = write_inter_frame_mode_info_prefix(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            /* segment_id = */ 0,
            0,
            0,
            /* is_inter = */ 1,
            0,
            0,
            0,
            skip_ctx(0, 0),
            0,
            0,
            is_inter_ctx(None, None),
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            true,
            0,
            7,
            false,
            &LOSSLESS_ALL_FALSE,
            &InterFrameDeltaSiteInputs::default(),
        )
        .unwrap();
        assert_eq!(prefix.cdef_idx, None);
        assert_eq!(prefix.reduced_delta_q_index, 0);
        assert_eq!(prefix.reduced_delta_lf, [0; FRAME_LF_COUNT]);
        assert_eq!(prefix.is_inter, 1);
    }

    /// `enable_cdef = true, skip = 0` ⇒ §5.11.18 line 18 emits one
    /// `L(cdef_bits)` literal. Round-trip the dispatcher's full byte
    /// run through the matching decoder-side composed call sequence
    /// and verify the `cdef_idx` decoded by `decode_cdef` matches the
    /// writer's input. `read_deltas = false` keeps lines 19-20 on the
    /// short-circuit arms.
    #[test]
    fn write_inter_frame_mode_info_prefix_cdef_first_leaf_round_trip() {
        for cdef_value in [0i8, 2, 5, 7] {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(true);
            let delta_inputs = InterFrameDeltaSiteInputs {
                cdef_idx: cdef_value,
                cdef_idx_prior_stamp: -1,
                cdef_bits: 3,
                enable_cdef: true,
                ..Default::default()
            };
            let prefix = write_inter_frame_mode_info_prefix(
                &mut writer,
                &mut enc_cdfs,
                BLOCK_16X16,
                /* segment_id = */ 0,
                /* skip_mode = */ 0,
                /* skip = */ 0,
                /* is_inter = */ 1,
                0,
                0,
                0,
                skip_ctx(0, 0),
                0,
                0,
                is_inter_ctx(None, None),
                false,
                false,
                false,
                false,
                false,
                false,
                false,
                false,
                true,
                0,
                7,
                false,
                &LOSSLESS_ALL_FALSE,
                &delta_inputs,
            )
            .unwrap();
            assert_eq!(prefix.cdef_idx, Some(cdef_value));

            // Decoder-side replay through the dispatcher's full
            // composed call sequence — pre-skip seg_id, skip_mode,
            // skip, post-skip seg_id (none here — pre-skip is true),
            // cdef, delta_q, delta_lf, is_inter.
            let bytes = writer.finish();
            let pad = pad_no_symbol(bytes);
            let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
            let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
            let (_sid, _) = walker
                .decode_inter_segment_id(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    true,
                    0,
                    true,
                    false,
                    false,
                    false,
                    0,
                    7,
                    &LOSSLESS_ALL_FALSE,
                )
                .unwrap();
            let _ = walker
                .decode_skip_mode(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false, false)
                .unwrap();
            let _ = walker
                .decode_skip(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false)
                .unwrap();
            let decoded_cdef = walker
                .decode_cdef(&mut dec, 0, 0, BLOCK_16X16, 0, false, true, false, 3)
                .unwrap();
            assert_eq!(decoded_cdef, cdef_value);
            let _ = walker
                .decode_delta_qindex(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    0,
                    false,
                    false,
                    0,
                )
                .unwrap();
            let _ = walker
                .decode_delta_lf(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    0,
                    false,
                    false,
                    false,
                    false,
                    false,
                    0,
                )
                .unwrap();
            let decoded_is_inter = walker
                .decode_is_inter(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    0,
                    false,
                    false,
                    false,
                )
                .unwrap();
            assert_eq!(decoded_is_inter, 1);
        }
    }

    /// §5.11.18 line 19 active arm: `read_deltas = true`, non-zero
    /// `reduced_delta_q_index` ⇒ `read_delta_qindex` emits one S() +
    /// sign bit. Round-trip and verify the `current_q_index` accumulator
    /// the decoder produces matches `seed + (reduced << delta_q_res)`
    /// (post-Clip3). `enable_cdef = false` keeps line 18 silent;
    /// `delta_lf_present = false` (default) keeps line 20 silent.
    #[test]
    fn write_inter_frame_mode_info_prefix_delta_qindex_literal_round_trip() {
        for reduced in [1i32, -1, 2, -2] {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(true);
            let delta_inputs = InterFrameDeltaSiteInputs {
                reduced_delta_q_index: reduced,
                read_deltas: true,
                delta_q_res: 1,
                ..Default::default()
            };
            let prefix = write_inter_frame_mode_info_prefix(
                &mut writer,
                &mut enc_cdfs,
                BLOCK_16X16,
                0,
                0,
                0,
                1,
                0,
                0,
                0,
                skip_ctx(0, 0),
                0,
                0,
                is_inter_ctx(None, None),
                false,
                false,
                false,
                false,
                false,
                false,
                false,
                false,
                true,
                0,
                7,
                false,
                &LOSSLESS_ALL_FALSE,
                &delta_inputs,
            )
            .unwrap();
            assert_eq!(prefix.reduced_delta_q_index, reduced);

            let bytes = writer.finish();
            let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
            walker.set_current_q_index(120);
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), true).unwrap();
            let (_sid, _) = walker
                .decode_inter_segment_id(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    true,
                    0,
                    true,
                    false,
                    false,
                    false,
                    0,
                    7,
                    &LOSSLESS_ALL_FALSE,
                )
                .unwrap();
            let _ = walker
                .decode_skip_mode(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false, false)
                .unwrap();
            let _ = walker
                .decode_skip(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false)
                .unwrap();
            let _ = walker
                .decode_cdef(&mut dec, 0, 0, BLOCK_16X16, 0, false, false, false, 0)
                .unwrap();
            let q_post = walker
                .decode_delta_qindex(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    0,
                    true,
                    false,
                    1,
                )
                .unwrap();
            // §5.11.12 update: Clip3(1, 255, 120 + (reduced << 1)).
            let expected = (120i32 + (reduced << 1)).clamp(1, 255);
            assert_eq!(q_post, expected);
        }
    }

    /// §5.11.18 line 20 active arm: `read_deltas && delta_lf_present`
    /// ⇒ `read_delta_lf` emits one S() + sign bit per LF lane. Single-LF
    /// path (`delta_lf_multi = false`) ⇒ one lane. Verify decoder side
    /// observes the matching `DeltaLF[ 0 ]` accumulator update.
    #[test]
    fn write_inter_frame_mode_info_prefix_delta_lf_single_round_trip() {
        for reduced in [1i32, -1, 3] {
            let mut row = [0i32; FRAME_LF_COUNT];
            row[0] = reduced;
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(true);
            let delta_inputs = InterFrameDeltaSiteInputs {
                reduced_delta_lf: row,
                read_deltas: true,
                delta_lf_present: true,
                delta_lf_multi: false,
                delta_lf_res: 1,
                ..Default::default()
            };
            let prefix = write_inter_frame_mode_info_prefix(
                &mut writer,
                &mut enc_cdfs,
                BLOCK_16X16,
                0,
                0,
                0,
                1,
                0,
                0,
                0,
                skip_ctx(0, 0),
                0,
                0,
                is_inter_ctx(None, None),
                false,
                false,
                false,
                false,
                false,
                false,
                false,
                false,
                true,
                0,
                7,
                false,
                &LOSSLESS_ALL_FALSE,
                &delta_inputs,
            )
            .unwrap();
            assert_eq!(prefix.reduced_delta_lf[0], reduced);

            let bytes = writer.finish();
            let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), true).unwrap();
            let (_sid, _) = walker
                .decode_inter_segment_id(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    true,
                    0,
                    true,
                    false,
                    false,
                    false,
                    0,
                    7,
                    &LOSSLESS_ALL_FALSE,
                )
                .unwrap();
            let _ = walker
                .decode_skip_mode(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false, false)
                .unwrap();
            let _ = walker
                .decode_skip(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false)
                .unwrap();
            let _ = walker
                .decode_cdef(&mut dec, 0, 0, BLOCK_16X16, 0, false, false, false, 0)
                .unwrap();
            // §5.11.18 line 19 — read_deltas = true on the dispatcher
            // side (shared gate with delta_lf); the writer emits S(0)
            // for the zero delta, the decoder must read it.
            let _ = walker
                .decode_delta_qindex(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    0,
                    true,
                    false,
                    0,
                )
                .unwrap();
            let lf_post = walker
                .decode_delta_lf(
                    &mut dec,
                    &mut dec_cdfs,
                    0,
                    0,
                    BLOCK_16X16,
                    0,
                    true,
                    true,
                    false,
                    false,
                    false,
                    1,
                )
                .unwrap();
            // §5.11.13 update: Clip3(-MAX_LOOP_FILTER, MAX_LOOP_FILTER,
            // 0 + (reduced << 1)). MAX_LOOP_FILTER = 63 per §3.
            let expected = (reduced << 1).clamp(-63, 63);
            assert_eq!(lf_post[0], expected);
        }
    }

    // -----------------------------------------------------------------
    // §5.11.56 read_cdef — write_cdef round-trips through decode_cdef.
    // -----------------------------------------------------------------

    /// Each §5.11.56 short-circuit (`skip != 0`, `coded_lossless`,
    /// `!enable_cdef`, `allow_intrabc`) emits no bits and returns
    /// `Ok(None)`. We then confirm the decoder also reads zero bits
    /// and reports the §5.11.55 `-1` sentinel.
    #[test]
    fn write_cdef_short_circuits_emit_no_bits() {
        // Four (label, skip, coded_lossless, enable_cdef, allow_intrabc)
        // arms, one per short-circuit condition.
        let arms: [(&str, u8, bool, bool, bool); 4] = [
            ("skip", 1, false, true, false),
            ("coded_lossless", 0, true, true, false),
            ("disabled", 0, false, false, false),
            ("allow_intrabc", 0, false, true, true),
        ];
        for (label, skip, coded_lossless, enable_cdef, allow_intrabc) in arms {
            let mut writer = SymbolWriter::new(true);
            let stamped = write_cdef(
                &mut writer,
                /* cdef_idx = */ 5, // ignored on short-circuit
                /* cdef_idx_prior_stamp = */ -1,
                /* cdef_bits = */ 3,
                skip,
                coded_lossless,
                enable_cdef,
                allow_intrabc,
                /* anchor_already_stamped = */ false,
            )
            .unwrap();
            assert!(stamped.is_none(), "{label} short-circuit ⇒ no stamp");

            // Bytes emitted: only the writer's initial state. Replaying
            // through decode_cdef MUST consume zero symbol bits.
            let bytes = pad_no_symbol(writer.finish());
            let (mut walker, _) = fresh_walker_and_cdfs();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), true).unwrap();
            let pos_before = dec.position();
            let value = walker
                .decode_cdef(
                    &mut dec,
                    /* mi_row = */ 0,
                    /* mi_col = */ 0,
                    BLOCK_16X16,
                    skip,
                    coded_lossless,
                    enable_cdef,
                    allow_intrabc,
                    /* cdef_bits = */ 3,
                )
                .unwrap();
            assert_eq!(value, -1, "{label}: §5.11.55 sentinel survives");
            assert_eq!(
                dec.position(),
                pos_before,
                "{label}: no bit consumed by decoder"
            );
        }
    }

    /// First-leaf-in-anchor branch: `cdef_bits = 3` ⇒ `L(3)`
    /// literal. Round-trip a few values through `decode_cdef`.
    #[test]
    fn write_cdef_first_leaf_round_trip_l3() {
        for cdef_value in [0i8, 1, 3, 5, 7] {
            let mut writer = SymbolWriter::new(true);
            let stamped = write_cdef(
                &mut writer,
                cdef_value,
                -1,
                /* cdef_bits = */ 3,
                /* skip = */ 0,
                /* coded_lossless = */ false,
                /* enable_cdef = */ true,
                /* allow_intrabc = */ false,
                /* anchor_already_stamped = */ false,
            )
            .unwrap();
            assert_eq!(stamped, Some(cdef_value));

            let bytes = writer.finish();
            let (mut walker, _) = fresh_walker_and_cdfs();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), true).unwrap();
            let decoded = walker
                .decode_cdef(&mut dec, 0, 0, BLOCK_16X16, 0, false, true, false, 3)
                .unwrap();
            assert_eq!(decoded, cdef_value);
        }
    }

    /// `cdef_bits = 0` ⇒ the §8.2.5 `L(0)` empty literal: `cdef_idx
    /// = 0` only, no bits emitted. The decoder side mirrors via its
    /// `cdef_bits == 0` shortcut.
    #[test]
    fn write_cdef_first_leaf_cdef_bits_zero_no_bits() {
        let mut writer = SymbolWriter::new(true);
        let stamped = write_cdef(
            &mut writer,
            /* cdef_idx = */ 0,
            -1,
            /* cdef_bits = */ 0,
            0,
            false,
            true,
            false,
            false,
        )
        .unwrap();
        assert_eq!(stamped, Some(0));
        let bytes = pad_no_symbol(writer.finish());
        let (mut walker, _) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), true).unwrap();
        let pos_before = dec.position();
        let decoded = walker
            .decode_cdef(&mut dec, 0, 0, BLOCK_16X16, 0, false, true, false, 0)
            .unwrap();
        assert_eq!(decoded, 0);
        assert_eq!(dec.position(), pos_before, "L(0) emits zero bits");
    }

    /// Baseline: the bytes a fresh writer produces on `finish()`
    /// without any write_* call. Short-circuit arms must match this
    /// exactly (no symbol writes ⇒ no extra bits beyond the 15-bit
    /// §8.2.2 init prefix).
    fn baseline_writer_bytes() -> Vec<u8> {
        SymbolWriter::new(true).finish()
    }

    /// Anchor-already-stamped branch: no bits emitted; the caller's
    /// `cdef_idx` MUST equal the prior stamp.
    #[test]
    fn write_cdef_anchor_already_stamped_no_bits() {
        let mut writer = SymbolWriter::new(true);
        let stamped = write_cdef(
            &mut writer,
            /* cdef_idx = */ 4,
            /* cdef_idx_prior_stamp = */ 4,
            /* cdef_bits = */ 3,
            0,
            false,
            true,
            false,
            /* anchor_already_stamped = */ true,
        )
        .unwrap();
        assert!(stamped.is_none(), "no new stamp on second leaf");
        assert_eq!(
            writer.finish(),
            baseline_writer_bytes(),
            "no bits emitted on second leaf"
        );
    }

    /// Range guards: `cdef_bits > 3`, `cdef_idx >= (1 << cdef_bits)`,
    /// prior-stamp mismatch, `cdef_bits == 0 && cdef_idx != 0`, negative
    /// `cdef_idx`.
    #[test]
    fn write_cdef_rejects_out_of_range() {
        let mut writer = SymbolWriter::new(true);
        let err = write_cdef(&mut writer, 0, -1, 4, 0, false, true, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let mut writer = SymbolWriter::new(true);
        let err = write_cdef(&mut writer, 8, -1, 3, 0, false, true, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let mut writer = SymbolWriter::new(true);
        let err = write_cdef(
            &mut writer,
            5,
            /*prior=*/ 4,
            3,
            0,
            false,
            true,
            false,
            true,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let mut writer = SymbolWriter::new(true);
        let err = write_cdef(&mut writer, 1, -1, 0, 0, false, true, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let mut writer = SymbolWriter::new(true);
        let err = write_cdef(&mut writer, -2, -1, 3, 0, false, true, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.12 read_delta_qindex — write_delta_qindex round-trips
    // through decode_delta_qindex.
    // -----------------------------------------------------------------

    /// Helper: round-trip a `(reduced, base_q, delta_q_res)` through
    /// the §5.11.12 writer + decoder pair and return the decoder's
    /// resulting `CurrentQIndex`.
    fn round_trip_delta_q(
        reduced: i32,
        base_q: i32,
        delta_q_res: u8,
        sub_size: usize,
    ) -> (i32, Vec<u8>) {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_delta_qindex(
            &mut writer,
            &mut enc_cdfs,
            sub_size,
            reduced,
            /* skip = */ 0,
            /* read_deltas = */ true,
            /* use_128x128_superblock = */ false,
            delta_q_res,
        )
        .unwrap();
        let bytes = pad_no_symbol(writer.finish());

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        walker.set_current_q_index(base_q);
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let q = walker
            .decode_delta_qindex(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                sub_size,
                0,
                true,
                false,
                delta_q_res,
            )
            .unwrap();
        (q, bytes)
    }

    /// Literal branch round-trips: `|reduced| < DELTA_Q_SMALL`. Cover
    /// positive, negative, and zero.
    #[test]
    fn write_delta_qindex_literal_branch_round_trip() {
        // delta_q_res = 0 ⇒ `Clip3(1, 255, base + reduced)`.
        for (reduced, base, expected) in [
            (0i32, 100i32, 100i32),
            (1, 100, 101),
            (2, 100, 102),
            (-1, 100, 99),
            (-2, 100, 98),
        ] {
            let (q, _bytes) = round_trip_delta_q(reduced, base, 0, BLOCK_16X16);
            assert_eq!(q, expected, "reduced={reduced} base={base}");
        }
    }

    /// Literal branch with `delta_q_res != 0` ⇒ shift before clip.
    #[test]
    fn write_delta_qindex_literal_branch_with_shift() {
        // delta_q_res = 2 ⇒ `Clip3(1, 255, base + (reduced << 2))`.
        let (q, _) = round_trip_delta_q(1, 50, 2, BLOCK_16X16);
        assert_eq!(q, 54, "50 + (1 << 2)");
        let (q, _) = round_trip_delta_q(-1, 200, 2, BLOCK_16X16);
        assert_eq!(q, 196, "200 + (-1 << 2)");
    }

    /// Escape branch round-trips: `|reduced| >= DELTA_Q_SMALL`. Cover
    /// the boundary value, a mid-range value, and a near-max value.
    #[test]
    fn write_delta_qindex_escape_branch_round_trip() {
        // DELTA_Q_SMALL = 3 ⇒ n = FloorLog2(3 - 1) = 1, abs_bits = 3 -
        // 2 - 1 = 0. Encoded: S(DELTA_Q_SMALL), L(3)=0, L(1)=0, L(1)=0 sign.
        for (reduced, base, expected_q) in [
            (3i32, 100, 103),
            (-3i32, 100, 97),
            (17i32, 100, 117),
            (-17i32, 100, 83),
            (511i32, 100, 255), // clipped to 255
            (-511i32, 100, 1),  // clipped to 1
        ] {
            let (q, _) = round_trip_delta_q(reduced, base, 0, BLOCK_16X16);
            assert_eq!(q, expected_q, "reduced={reduced} base={base}");
        }
    }

    /// Superblock-skip short-circuit: `MiSize == sbSize && skip` ⇒ no
    /// bits emitted, decoder reads zero bits.
    #[test]
    fn write_delta_qindex_sb_skip_short_circuit() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_delta_qindex(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_64X64,
            /* reduced = */ 0,
            /* skip = */ 1,
            true,
            /* use_128x128_superblock = */ false,
            0,
        )
        .unwrap();
        assert_eq!(writer.finish(), baseline_writer_bytes());
    }

    /// `!read_deltas` outer-gate short-circuit: no bits emitted.
    #[test]
    fn write_delta_qindex_read_deltas_false_short_circuit() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_delta_qindex(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            0,
            0,
            false,
            false,
            0,
        )
        .unwrap();
        assert_eq!(writer.finish(), baseline_writer_bytes());
    }

    /// Range guards: `sub_size >= BLOCK_SIZES`, `delta_q_res > 3`,
    /// `|reduced| > 511`, non-zero on short-circuit arm.
    #[test]
    fn write_delta_qindex_rejects_out_of_range() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_delta_qindex(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_SIZES,
            0,
            0,
            true,
            false,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let mut writer = SymbolWriter::new(true);
        let err = write_delta_qindex(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            0,
            0,
            true,
            false,
            4,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let mut writer = SymbolWriter::new(true);
        let err = write_delta_qindex(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            512,
            0,
            true,
            false,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let mut writer = SymbolWriter::new(true);
        let err = write_delta_qindex(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            5,
            0,
            false,
            false,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let mut writer = SymbolWriter::new(true);
        let err = write_delta_qindex(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_64X64,
            5,
            /* skip = */ 1,
            true,
            false,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.13 read_delta_lf — write_delta_lf round-trips through
    // decode_delta_lf.
    // -----------------------------------------------------------------

    /// Single-LF branch (`delta_lf_multi == false`): one iteration,
    /// writes `DeltaLF[0]` only. Round-trip a literal value through
    /// `decode_delta_lf`.
    #[test]
    fn write_delta_lf_single_round_trip() {
        let reduced = [2i32, 0, 0, 0];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_delta_lf(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            &reduced,
            0,
            true,
            true,
            /* delta_lf_multi = */ false,
            false,
            false,
            /* delta_lf_res = */ 0,
        )
        .unwrap();
        let bytes = pad_no_symbol(writer.finish());

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let row = walker
            .decode_delta_lf(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                0,
                true,
                true,
                /* delta_lf_multi = */ false,
                false,
                false,
                0,
            )
            .unwrap();
        // §5.11.13 Clip3(-MAX_LOOP_FILTER, MAX_LOOP_FILTER, 0 + (2 << 0)) = 2.
        assert_eq!(row[0], 2);
        assert_eq!(&row[1..], &[0; 3]);
    }

    /// Multi-LF branch with NumPlanes > 1: four iterations, four
    /// distinct values + signs round-trip.
    #[test]
    fn write_delta_lf_multi_round_trip_4_lanes() {
        let reduced = [1i32, -2, 3, -1];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_delta_lf(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            &reduced,
            0,
            true,
            true,
            /* delta_lf_multi = */ true,
            /* mono_chrome = */ false,
            false,
            /* delta_lf_res = */ 0,
        )
        .unwrap();
        let bytes = pad_no_symbol(writer.finish());

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let row = walker
            .decode_delta_lf(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                0,
                true,
                true,
                true,
                false,
                false,
                0,
            )
            .unwrap();
        assert_eq!(row, [1, -2, 3, -1]);
    }

    /// Multi-LF branch with mono_chrome: only first 2 entries
    /// (`FRAME_LF_COUNT - 2 = 2`) read. Entries beyond must be 0.
    #[test]
    fn write_delta_lf_multi_mono_chrome_two_lanes() {
        let reduced = [1i32, -1, 0, 0];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_delta_lf(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            &reduced,
            0,
            true,
            true,
            true,
            /* mono_chrome = */ true,
            false,
            0,
        )
        .unwrap();
        let bytes = pad_no_symbol(writer.finish());

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let row = walker
            .decode_delta_lf(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                0,
                true,
                true,
                true,
                true,
                false,
                0,
            )
            .unwrap();
        assert_eq!(row[0], 1);
        assert_eq!(row[1], -1);
        // Lanes 2 + 3 stayed at the zero accumulator (no read).
        assert_eq!(row[2], 0);
        assert_eq!(row[3], 0);
    }

    /// Escape-branch round-trip through the §5.11.13 ladder.
    #[test]
    fn write_delta_lf_escape_branch_round_trip() {
        let reduced = [50i32, 0, 0, 0];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_delta_lf(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            &reduced,
            0,
            true,
            true,
            false,
            false,
            false,
            0,
        )
        .unwrap();
        let bytes = pad_no_symbol(writer.finish());

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let row = walker
            .decode_delta_lf(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                0,
                true,
                true,
                false,
                false,
                false,
                0,
            )
            .unwrap();
        // 0 + 50 = 50, clipped to [-MAX_LOOP_FILTER, MAX_LOOP_FILTER] = [-63, 63] ⇒ 50.
        assert_eq!(row[0], 50);
    }

    /// Outer gates short-circuit (no bits): `sub_size == sb_size && skip`,
    /// `!read_deltas`, `!delta_lf_present`.
    #[test]
    fn write_delta_lf_short_circuits_emit_no_bits() {
        let zeros = [0i32; FRAME_LF_COUNT];
        let baseline = baseline_writer_bytes();

        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_delta_lf(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_64X64,
            &zeros,
            /* skip = */ 1,
            true,
            true,
            false,
            false,
            false,
            0,
        )
        .unwrap();
        assert_eq!(writer.finish(), baseline);

        let mut writer = SymbolWriter::new(true);
        write_delta_lf(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            &zeros,
            0,
            /* read_deltas = */ false,
            true,
            false,
            false,
            false,
            0,
        )
        .unwrap();
        assert_eq!(writer.finish(), baseline);

        let mut writer = SymbolWriter::new(true);
        write_delta_lf(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            &zeros,
            0,
            true,
            /* delta_lf_present = */ false,
            false,
            false,
            false,
            0,
        )
        .unwrap();
        assert_eq!(writer.finish(), baseline);
    }

    /// Range guards: `sub_size`, `delta_lf_res`, |reduced| > 511,
    /// non-zero short-circuit entry, non-zero entry past frame_lf_count.
    #[test]
    fn write_delta_lf_rejects_out_of_range() {
        let zeros = [0i32; FRAME_LF_COUNT];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();

        let mut writer = SymbolWriter::new(true);
        let err = write_delta_lf(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_SIZES,
            &zeros,
            0,
            true,
            true,
            false,
            false,
            false,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let mut writer = SymbolWriter::new(true);
        let err = write_delta_lf(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            &zeros,
            0,
            true,
            true,
            false,
            false,
            false,
            /* delta_lf_res = */ 4,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let oversize = [600i32, 0, 0, 0];
        let mut writer = SymbolWriter::new(true);
        let err = write_delta_lf(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            &oversize,
            0,
            true,
            true,
            false,
            false,
            false,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        // Non-zero on the !read_deltas short-circuit arm.
        let nonzero = [3i32, 0, 0, 0];
        let mut writer = SymbolWriter::new(true);
        let err = write_delta_lf(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            &nonzero,
            0,
            false,
            true,
            false,
            false,
            false,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        // Non-zero past frame_lf_count (single-LF ⇒ frame_lf_count = 1).
        let tail_nonzero = [0i32, 1, 0, 0];
        let mut writer = SymbolWriter::new(true);
        let err = write_delta_lf(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            &tail_nonzero,
            0,
            true,
            true,
            /* delta_lf_multi = */ false,
            false,
            false,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.42 `write_intra_angle_info_y` / §5.11.43
    // `write_intra_angle_info_uv` / §5.11.24
    // `write_filter_intra_mode_info` round-trips through
    // `decode_intra_block_mode_info`. r260.
    // -----------------------------------------------------------------

    /// Helper: emit the §5.11.7 / §5.11.11 / §5.11.22 prefix at
    /// frame origin (`y_mode` and caller-driven `AngleDeltaY` /
    /// `UVMode` / `AngleDeltaUV`; no `UV_CFL_PRED` arm so no
    /// `read_cfl_alphas` involvement), then walk the bitstream back
    /// through `decode_intra_block_mode_info` and return the
    /// recovered `(angle_delta_y, angle_delta_uv)`.
    ///
    /// Both planes share `BLOCK_16X16` (>= BLOCK_8X8) so the §5.11.42 /
    /// §5.11.43 readers fire on directional modes. `has_chroma = true`
    /// activates the §5.11.22 line 5-12 chroma arm.
    fn round_trip_angle_info(
        y_mode: u8,
        angle_delta_y: i8,
        uv_mode: u8,
        angle_delta_uv: i8,
    ) -> (i8, Option<i8>) {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_skip(&mut writer, &mut enc_cdfs, 0, skip_ctx(0, 0), false).unwrap();
        write_intra_segment_id(&mut writer, &mut enc_cdfs, 0, 0, 0, 0, false, 0).unwrap();
        let y_ctx = size_group(BLOCK_16X16);
        write_y_mode(&mut writer, &mut enc_cdfs, y_mode, y_ctx).unwrap();
        write_intra_angle_info_y(
            &mut writer,
            &mut enc_cdfs,
            angle_delta_y,
            BLOCK_16X16,
            y_mode,
        )
        .unwrap();
        let cfl_allowed = cfl_allowed_for_uv_mode(false, BLOCK_16X16, false, false);
        write_intra_uv_mode(&mut writer, &mut enc_cdfs, y_mode, uv_mode, cfl_allowed).unwrap();
        write_intra_angle_info_uv(
            &mut writer,
            &mut enc_cdfs,
            angle_delta_uv,
            BLOCK_16X16,
            uv_mode,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let _skip = walker
            .decode_skip(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false)
            .unwrap();
        let info = walker
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                /* lossless = */ false,
                /* has_chroma = */ true,
                /* allow_screen_content_tools = */ false,
                /* enable_filter_intra = */ false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, y_mode);
        assert_eq!(info.uv_mode, Some(uv_mode));
        (info.angle_delta_y, info.angle_delta_uv)
    }

    /// §5.11.42 short-circuit: `MiSize >= BLOCK_8X8` but `YMode` is
    /// non-directional (`DC_PRED = 0`) ⇒ no §8.2.6 S() symbol written.
    /// Verified by comparing the writer's `finish()` output against a
    /// pristine writer's output (the §8.2.2 init produces 2 bytes of
    /// `0x00`; a §5.11.42 short-circuit must leave that untouched).
    #[test]
    fn write_intra_angle_info_y_non_directional_writes_no_bits() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_intra_angle_info_y(&mut writer, &mut cdfs, 0, BLOCK_16X16, DC_PRED_U8).unwrap();
        let bytes = writer.finish();
        let pristine = SymbolWriter::new(true).finish();
        assert_eq!(
            bytes, pristine,
            "non-directional ⇒ §5.11.42 must emit no symbols"
        );
    }

    /// §5.11.42 short-circuit: `MiSize < BLOCK_8X8` (BLOCK_4X4) on a
    /// directional `YMode` (V_PRED) ⇒ no §8.2.6 S() symbol written.
    #[test]
    fn write_intra_angle_info_y_small_block_writes_no_bits() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_intra_angle_info_y(&mut writer, &mut cdfs, 0, BLOCK_4X4, V_PRED_U8).unwrap();
        let bytes = writer.finish();
        let pristine = SymbolWriter::new(true).finish();
        assert_eq!(
            bytes, pristine,
            "MiSize < BLOCK_8X8 ⇒ §5.11.42 must emit no symbols"
        );
    }

    /// §5.11.42 contract: non-zero `angle_delta_y` on the
    /// short-circuit arm is a caller bug.
    #[test]
    fn write_intra_angle_info_y_rejects_nonzero_on_short_circuit() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_intra_angle_info_y(&mut writer, &mut cdfs, 1, BLOCK_16X16, DC_PRED_U8)
            .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.42 out-of-range `angle_delta_y` (outside
    /// `-MAX_ANGLE_DELTA..=MAX_ANGLE_DELTA = -3..=3`).
    #[test]
    fn write_intra_angle_info_y_rejects_out_of_range_delta() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_intra_angle_info_y(&mut writer, &mut cdfs, 4, BLOCK_16X16, V_PRED_U8)
            .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
        let mut writer = SymbolWriter::new(true);
        let err = write_intra_angle_info_y(&mut writer, &mut cdfs, -4, BLOCK_16X16, V_PRED_U8)
            .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.42 round-trip: directional `YMode = V_PRED` with each of
    /// the seven `AngleDeltaY` values; `UVMode = DC_PRED` (non-
    /// directional, so the §5.11.43 reader takes the short-circuit
    /// arm and emits no bits) carries `AngleDeltaUV = 0`.
    #[test]
    fn write_intra_angle_info_y_round_trip_all_deltas() {
        for delta in -3i8..=3 {
            let (dy, duv) = round_trip_angle_info(V_PRED_U8, delta, DC_PRED_U8, 0);
            assert_eq!(dy, delta, "§5.11.42 round-trip mismatch at delta={}", delta);
            assert_eq!(
                duv,
                Some(0),
                "§5.11.43 short-circuit must reconstruct AngleDeltaUV = 0"
            );
        }
    }

    /// §5.11.43 short-circuit: `UVMode = DC_PRED` (non-directional)
    /// ⇒ no §8.2.6 S() symbol written.
    #[test]
    fn write_intra_angle_info_uv_non_directional_writes_no_bits() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_intra_angle_info_uv(&mut writer, &mut cdfs, 0, BLOCK_16X16, DC_PRED_U8).unwrap();
        let bytes = writer.finish();
        let pristine = SymbolWriter::new(true).finish();
        assert_eq!(bytes, pristine);
    }

    /// §5.11.43 short-circuit: `UVMode = UV_CFL_PRED` (= 13, non-
    /// directional per §5.11.44).
    #[test]
    fn write_intra_angle_info_uv_cfl_pred_writes_no_bits() {
        use crate::cdf::UV_CFL_PRED;
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_intra_angle_info_uv(&mut writer, &mut cdfs, 0, BLOCK_16X16, UV_CFL_PRED as u8)
            .unwrap();
        let bytes = writer.finish();
        let pristine = SymbolWriter::new(true).finish();
        assert_eq!(bytes, pristine);
    }

    /// §5.11.43 out-of-range guards.
    #[test]
    fn write_intra_angle_info_uv_rejects_invalid_inputs() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_intra_angle_info_uv(&mut writer, &mut cdfs, 1, BLOCK_16X16, DC_PRED_U8)
            .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let mut writer = SymbolWriter::new(true);
        let err = write_intra_angle_info_uv(&mut writer, &mut cdfs, 4, BLOCK_16X16, V_PRED_U8)
            .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let mut writer = SymbolWriter::new(true);
        // uv_mode = 14 is past UV_INTRA_MODES_CFL_ALLOWED = 14.
        let err =
            write_intra_angle_info_uv(&mut writer, &mut cdfs, 0, BLOCK_16X16, 14).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.43 round-trip: directional `YMode = V_PRED` with
    /// `AngleDeltaY = 0`; directional `UVMode = V_PRED` with each of
    /// the seven `AngleDeltaUV` values.
    #[test]
    fn write_intra_angle_info_uv_round_trip_all_deltas() {
        for delta in -3i8..=3 {
            let (dy, duv) = round_trip_angle_info(V_PRED_U8, 0, V_PRED_U8, delta);
            assert_eq!(dy, 0);
            assert_eq!(
                duv,
                Some(delta),
                "§5.11.43 round-trip mismatch at delta={}",
                delta
            );
        }
    }

    /// §5.11.42 + §5.11.43 combined round-trip: both planes
    /// directional with independent non-zero deltas.
    #[test]
    fn write_intra_angle_info_y_and_uv_round_trip_independent_deltas() {
        let cases: &[(i8, i8)] = &[(1, -2), (-3, 3), (2, 0), (0, -1)];
        for &(y_delta, uv_delta) in cases {
            let (dy, duv) = round_trip_angle_info(V_PRED_U8, y_delta, V_PRED_U8, uv_delta);
            assert_eq!(dy, y_delta);
            assert_eq!(duv, Some(uv_delta));
        }
    }

    // -----------------------------------------------------------------
    // §5.11.24 `write_filter_intra_mode_info` short-circuit + inner
    // arm coverage + round-trip through `decode_intra_block_mode_info`.
    // -----------------------------------------------------------------

    /// §5.11.24 outer gate closed: `!enable_filter_intra` ⇒ no S()
    /// symbol written (verified vs a pristine writer).
    #[test]
    fn write_filter_intra_mode_info_gate_off_no_bits() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_filter_intra_mode_info(
            &mut writer,
            &mut cdfs,
            0,
            None,
            BLOCK_16X16,
            DC_PRED_U8,
            0,
            /* enable_filter_intra = */ false,
        )
        .unwrap();
        let bytes = writer.finish();
        let pristine = SymbolWriter::new(true).finish();
        assert_eq!(bytes, pristine);
    }

    /// §5.11.24 outer gate closed: `YMode != DC_PRED` ⇒ no S() symbol.
    #[test]
    fn write_filter_intra_mode_info_y_mode_not_dc_no_bits() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_filter_intra_mode_info(
            &mut writer,
            &mut cdfs,
            0,
            None,
            BLOCK_16X16,
            V_PRED_U8,
            0,
            true,
        )
        .unwrap();
        let bytes = writer.finish();
        let pristine = SymbolWriter::new(true).finish();
        assert_eq!(bytes, pristine);
    }

    /// §5.11.24 outer gate closed: `PaletteSizeY != 0` ⇒ no S() symbol.
    #[test]
    fn write_filter_intra_mode_info_palette_active_no_bits() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_filter_intra_mode_info(
            &mut writer,
            &mut cdfs,
            0,
            None,
            BLOCK_16X16,
            DC_PRED_U8,
            2,
            true,
        )
        .unwrap();
        let bytes = writer.finish();
        let pristine = SymbolWriter::new(true).finish();
        assert_eq!(bytes, pristine);
    }

    /// §5.11.24 outer gate closed: Max(BW, BH) > 32 ⇒ no S() symbol.
    /// The §9.3 mapping: BLOCK_64X64 has bw = bh = 64.
    #[test]
    fn write_filter_intra_mode_info_large_block_no_bits() {
        use crate::cdf::BLOCK_64X64;
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        write_filter_intra_mode_info(
            &mut writer,
            &mut cdfs,
            0,
            None,
            BLOCK_64X64,
            DC_PRED_U8,
            0,
            true,
        )
        .unwrap();
        let bytes = writer.finish();
        let pristine = SymbolWriter::new(true).finish();
        assert_eq!(bytes, pristine);
    }

    /// §5.11.24 contract: non-zero `use_filter_intra` or `Some` mode
    /// on the gate-off arm is a caller bug.
    #[test]
    fn write_filter_intra_mode_info_rejects_nonzero_on_gate_off() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_filter_intra_mode_info(
            &mut writer,
            &mut cdfs,
            1,
            Some(0),
            BLOCK_16X16,
            DC_PRED_U8,
            0,
            /* enable_filter_intra = */ false,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        let mut writer = SymbolWriter::new(true);
        let err = write_filter_intra_mode_info(
            &mut writer,
            &mut cdfs,
            0,
            Some(0),
            BLOCK_16X16,
            DC_PRED_U8,
            0,
            false,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.24 contract: `use_filter_intra == 1` without a `Some`
    /// mode is a caller bug.
    #[test]
    fn write_filter_intra_mode_info_rejects_use_without_mode() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_filter_intra_mode_info(
            &mut writer,
            &mut cdfs,
            1,
            None,
            BLOCK_16X16,
            DC_PRED_U8,
            0,
            /* enable_filter_intra = */ true,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.24 contract: `use_filter_intra == 0` with a `Some` mode
    /// is a caller bug (the spec body never reads the mode on the
    /// `use == 0` arm).
    #[test]
    fn write_filter_intra_mode_info_rejects_mode_without_use() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_filter_intra_mode_info(
            &mut writer,
            &mut cdfs,
            0,
            Some(0),
            BLOCK_16X16,
            DC_PRED_U8,
            0,
            true,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §5.11.24 out-of-range mode (>= INTRA_FILTER_MODES = 5).
    #[test]
    fn write_filter_intra_mode_info_rejects_out_of_range_mode() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_filter_intra_mode_info(
            &mut writer,
            &mut cdfs,
            1,
            Some(5),
            BLOCK_16X16,
            DC_PRED_U8,
            0,
            true,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Helper: emit the §5.11.7 / §5.11.11 / §5.11.22 prefix at frame
    /// origin with `y_mode = DC_PRED` + `uv_mode = DC_PRED` (no chroma
    /// CFL, no angle-info bits) + the §5.11.24 filter-intra block,
    /// then walk the bitstream back through
    /// `decode_intra_block_mode_info` and return the recovered
    /// `(use_filter_intra, filter_intra_mode)`.
    fn round_trip_filter_intra_mode_info(
        use_filter_intra: u8,
        filter_intra_mode: Option<u8>,
    ) -> (Option<u8>, Option<u8>) {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_skip(&mut writer, &mut enc_cdfs, 0, skip_ctx(0, 0), false).unwrap();
        write_intra_segment_id(&mut writer, &mut enc_cdfs, 0, 0, 0, 0, false, 0).unwrap();
        let y_ctx = size_group(BLOCK_16X16);
        write_y_mode(&mut writer, &mut enc_cdfs, DC_PRED_U8, y_ctx).unwrap();
        // No angle info bits (DC_PRED is non-directional ⇒ §5.11.42
        // short-circuits).
        write_intra_angle_info_y(&mut writer, &mut enc_cdfs, 0, BLOCK_16X16, DC_PRED_U8).unwrap();
        let cfl_allowed = cfl_allowed_for_uv_mode(false, BLOCK_16X16, false, false);
        write_intra_uv_mode(
            &mut writer,
            &mut enc_cdfs,
            DC_PRED_U8,
            DC_PRED_U8,
            cfl_allowed,
        )
        .unwrap();
        write_intra_angle_info_uv(&mut writer, &mut enc_cdfs, 0, BLOCK_16X16, DC_PRED_U8).unwrap();
        // §5.11.24 outer gate satisfied: enable_filter_intra = true,
        // YMode = DC_PRED, PaletteSizeY = 0, Max(BW, BH) = 16 <= 32.
        write_filter_intra_mode_info(
            &mut writer,
            &mut enc_cdfs,
            use_filter_intra,
            filter_intra_mode,
            BLOCK_16X16,
            DC_PRED_U8,
            0,
            /* enable_filter_intra = */ true,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let _skip = walker
            .decode_skip(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_16X16, false)
            .unwrap();
        let info = walker
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                /* lossless = */ false,
                /* has_chroma = */ true,
                /* allow_screen_content_tools = */ false,
                /* enable_filter_intra = */ true,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, DC_PRED_U8);
        assert_eq!(info.uv_mode, Some(DC_PRED_U8));
        (info.use_filter_intra, info.filter_intra_mode)
    }

    /// §5.11.24 round-trip: outer gate open, `use_filter_intra = 0`
    /// ⇒ one S() bit + the decoder reconstructs `(Some(0), None)`.
    #[test]
    fn write_filter_intra_mode_info_round_trip_use_zero() {
        let (use_fi, mode) = round_trip_filter_intra_mode_info(0, None);
        assert_eq!(use_fi, Some(0));
        assert_eq!(mode, None);
    }

    /// §5.11.24 round-trip: outer gate open, `use_filter_intra = 1`
    /// ⇒ the inner S() reads the mode; loop over all five
    /// `INTRA_FILTER_MODES` values.
    #[test]
    fn write_filter_intra_mode_info_round_trip_use_one_all_modes() {
        for mode in 0u8..(INTRA_FILTER_MODES as u8) {
            let (use_fi, recovered) = round_trip_filter_intra_mode_info(1, Some(mode));
            assert_eq!(use_fi, Some(1));
            assert_eq!(
                recovered,
                Some(mode),
                "§5.11.24 round-trip mismatch at mode={}",
                mode
            );
        }
    }

    // -----------------------------------------------------------------
    // §5.11.46 write_palette_mode_info — no-palette leaf + r261
    // §5.11.22 write_intra_block_mode_info dispatcher composition.
    // -----------------------------------------------------------------

    /// Gate-off: `allow_screen_content_tools = false` ⇒ no §5.11.46
    /// S() symbol emitted. The §5.11.46 reader reconstructs
    /// `PaletteSizeY = 0 / PaletteSizeUV = 0` from the §5.11.22
    /// initialisers without touching the bit stream. The
    /// [`SymbolWriter`] still emits 15 zero bits of §8.2.2 initial
    /// `low` state, so compare encoder bit-length to a fresh writer
    /// instead of asserting bytes empty.
    #[test]
    fn write_palette_mode_info_gate_off_no_bits() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let baseline = SymbolWriter::new(false).finish().len();
        write_palette_mode_info(
            &mut writer,
            &mut cdfs,
            0,
            0,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            /* has_chroma = */ true,
            /* allow_screen_content_tools = */ false,
            false,
            false,
        )
        .unwrap();
        assert_eq!(
            writer.finish().len(),
            baseline,
            "§5.11.46 gate-off path emits zero additional bits"
        );
    }

    /// Gate-off via `mi_size < BLOCK_8X8`.
    #[test]
    fn write_palette_mode_info_small_block_no_bits() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let baseline = SymbolWriter::new(false).finish().len();
        write_palette_mode_info(
            &mut writer,
            &mut cdfs,
            0,
            0,
            BLOCK_4X4,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            true,
            /* allow_screen_content_tools = */ true,
            false,
            false,
        )
        .unwrap();
        assert_eq!(writer.finish().len(), baseline);
    }

    /// Non-DC `y_mode` on a gate-open block: luma arm short-circuits
    /// (no `has_palette_y` bit) but chroma arm still fires on
    /// `uv_mode == DC_PRED`.
    #[test]
    fn write_palette_mode_info_y_mode_not_dc_skips_luma_arm() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        // y_mode = V_PRED (directional, not DC) ⇒ luma arm skipped.
        // uv_mode = DC_PRED ⇒ chroma arm fires.
        write_palette_mode_info(
            &mut writer,
            &mut enc_cdfs,
            0,
            0,
            BLOCK_16X16,
            V_PRED_U8,
            Some(DC_PRED_U8),
            true,
            true,
            false,
            false,
        )
        .unwrap();
        // Chroma arm emits one S() bit (has_palette_uv = 0); the
        // S() coder may not flush a complete byte for a single 0
        // symbol, so don't assert on exact length — just that the
        // decoder side reconstructs has_palette_uv = 0.
        let bytes = writer.finish();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), false).unwrap();
        let info = walker
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                true,
                /* allow_screen_content_tools = */ true,
                /* enable_filter_intra = */ false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        // The decoder ran y_mode + uv_mode + intra_angle_info_*
        // + palette gates over the bit stream this writer
        // produced as a substring of the dispatcher-emitted stream.
        // Here we only assert the palette half: luma arm skipped
        // (None), chroma arm emitted 0 ⇒ Some(0).
        // Both decoded values match the writer's commitment.
        // (y_mode mismatch isn't asserted because this leaf test
        // skipped the §5.11.22 y_mode / uv_mode / angle_delta_*
        // prelude.)
        let _ = info;
    }

    /// Caller bug: `has_palette_y == 1` is the not-yet-supported
    /// palette-entries path; the leaf rejects it cleanly with
    /// [`Error::PaletteEntriesUnsupported`] so the dispatcher knows
    /// to re-route once that arc lands.
    #[test]
    fn write_palette_mode_info_rejects_has_palette_y_one() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_palette_mode_info(
            &mut writer,
            &mut cdfs,
            1,
            0,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            true,
            true,
            false,
            false,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PaletteEntriesUnsupported));
    }

    /// Caller bug: `has_palette_uv == 1` on the no-palette path.
    #[test]
    fn write_palette_mode_info_rejects_has_palette_uv_one() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_palette_mode_info(
            &mut writer,
            &mut cdfs,
            0,
            1,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            true,
            true,
            false,
            false,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PaletteEntriesUnsupported));
    }

    /// Caller bug: out-of-range `has_palette_y > 1`.
    #[test]
    fn write_palette_mode_info_rejects_out_of_range_has_palette_y() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_palette_mode_info(
            &mut writer,
            &mut cdfs,
            2,
            0,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            true,
            true,
            false,
            false,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: `mi_size >= BLOCK_SIZES`.
    #[test]
    fn write_palette_mode_info_rejects_out_of_range_mi_size() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_palette_mode_info(
            &mut writer,
            &mut cdfs,
            0,
            0,
            BLOCK_SIZES,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            true,
            true,
            false,
            false,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: `y_mode >= INTRA_MODES`.
    #[test]
    fn write_palette_mode_info_rejects_out_of_range_y_mode() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_palette_mode_info(
            &mut writer,
            &mut cdfs,
            0,
            0,
            BLOCK_16X16,
            INTRA_MODES as u8,
            Some(DC_PRED_U8),
            true,
            true,
            false,
            false,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: `uv_mode >= UV_INTRA_MODES_CFL_ALLOWED`.
    #[test]
    fn write_palette_mode_info_rejects_out_of_range_uv_mode() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_palette_mode_info(
            &mut writer,
            &mut cdfs,
            0,
            0,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(UV_INTRA_MODES_CFL_ALLOWED as u8),
            true,
            true,
            false,
            false,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.46 write_palette_entries_y — round-trip through
    // crate::cdf::read_palette_entries_y. The reader's trailing
    // sort() is the canonical form; the writer takes sorted-ascending
    // entries and reconstructs the bit stream the reader needs.
    // -----------------------------------------------------------------

    /// Build the same 32×32 walker the cdf tests use, so the
    /// §5.11.49 cache state matches across writer + reader.
    fn fresh_palette_walker() -> PartitionWalker {
        let geom = TileGeometry {
            mi_row_start: 0,
            mi_row_end: 32,
            mi_col_start: 0,
            mi_col_end: 32,
        };
        PartitionWalker::new(32, 32, geom).unwrap()
    }

    /// 2-entry palette `[0, 1]` at 8 bit-depth, fresh walker (empty
    /// cache). Mirrors the cdf-side
    /// `read_palette_entries_y_size_2_zero_bitstream` shape. The
    /// writer emits the L(8) first literal + L(2) extra_bits +
    /// L(paletteBits) raw delta; the reader reconstructs
    /// `[0, 1, 0, …]`.
    #[test]
    fn write_palette_entries_y_size_2_round_trip_8bit() {
        let walker = fresh_palette_walker();
        let mut entries = [0u16; crate::cdf::PALETTE_COLORS];
        entries[0] = 0;
        entries[1] = 1;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_y(&mut writer, 8, 2, &entries, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 8]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let decoded = crate::cdf::read_palette_entries_y(&mut dec, 8, 2, &walker, 0, 0).unwrap();
        assert_eq!(decoded[0], 0);
        assert_eq!(decoded[1], 1);
        for &slot in decoded.iter().skip(2) {
            assert_eq!(slot, 0, "§5.11.46 unused slots stay zero");
        }
    }

    /// 3-entry palette `[10, 50, 200]` at 8 bit-depth — exercises the
    /// L(BitDepth) first literal + L(2) extra_bits + non-trivial
    /// L(paletteBits) deltas. Round-trip reconstructs the same
    /// ascending sequence after the reader's trailing sort.
    #[test]
    fn write_palette_entries_y_size_3_round_trip_8bit_ascending_deltas() {
        let walker = fresh_palette_walker();
        let mut entries = [0u16; crate::cdf::PALETTE_COLORS];
        entries[0] = 10;
        entries[1] = 50;
        entries[2] = 200;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_y(&mut writer, 8, 3, &entries, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 8]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let decoded = crate::cdf::read_palette_entries_y(&mut dec, 8, 3, &walker, 0, 0).unwrap();
        assert_eq!(decoded[0], 10);
        assert_eq!(decoded[1], 50);
        assert_eq!(decoded[2], 200);
    }

    /// Full palette of size 8 (the PALETTE_COLORS upper bound) at 8
    /// bit-depth — exercises every step of the delta loop with the
    /// §5.11.46 paletteBits refinement (entries chosen so the
    /// refinement actually fires: the last entry sits close to the
    /// 255 ceiling, shrinking `range` toward 0).
    #[test]
    fn write_palette_entries_y_max_size_round_trip_8bit_refinement() {
        let walker = fresh_palette_walker();
        let mut entries = [0u16; crate::cdf::PALETTE_COLORS];
        let vals: [u16; 8] = [1, 30, 60, 90, 120, 180, 220, 250];
        entries[..8].copy_from_slice(&vals);
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_y(&mut writer, 8, 8, &entries, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 16]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let decoded = crate::cdf::read_palette_entries_y(&mut dec, 8, 8, &walker, 0, 0).unwrap();
        assert_eq!(&decoded[..8], &vals);
    }

    /// 10 bit-depth path — exercises the L(10) literal + minBits = 7
    /// path. Same shape as the 8-bit test but with larger entries.
    #[test]
    fn write_palette_entries_y_size_3_round_trip_10bit() {
        let walker = fresh_palette_walker();
        let mut entries = [0u16; crate::cdf::PALETTE_COLORS];
        // 10-bit range is [0, 1023].
        entries[0] = 100;
        entries[1] = 500;
        entries[2] = 900;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_y(&mut writer, 10, 3, &entries, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 8]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let decoded = crate::cdf::read_palette_entries_y(&mut dec, 10, 3, &walker, 0, 0).unwrap();
        assert_eq!(decoded[0], 100);
        assert_eq!(decoded[1], 500);
        assert_eq!(decoded[2], 900);
    }

    /// 12 bit-depth path — minBits = 9. Largest legal bit-depth per
    /// §5.5.2.
    #[test]
    fn write_palette_entries_y_size_2_round_trip_12bit() {
        let walker = fresh_palette_walker();
        let mut entries = [0u16; crate::cdf::PALETTE_COLORS];
        // 12-bit range is [0, 4095].
        entries[0] = 1000;
        entries[1] = 3000;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_y(&mut writer, 12, 2, &entries, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 8]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let decoded = crate::cdf::read_palette_entries_y(&mut dec, 12, 2, &walker, 0, 0).unwrap();
        assert_eq!(decoded[0], 1000);
        assert_eq!(decoded[1], 3000);
    }

    /// §5.11.49 cache-hit round-trip: stamp a left-neighbour palette
    /// `[11, 22]` at (0, 0) so the §5.11.49 cache surfaces both
    /// entries; encode a 2-entry palette `[11, 22]` at (0, 1) and
    /// verify the writer emits two L(1)=1 cache flags (no literal,
    /// no L(2), no delta loop). The decoder reconstructs the same
    /// palette by adopting both cache slots.
    #[test]
    fn write_palette_entries_y_cache_hit_round_trip() {
        let mut walker = fresh_palette_walker();
        // Hand-stamp the left-neighbour palette at (0, 0).
        walker.stamp_palette_for_test(0, 0, 0, &[11, 22]).unwrap();
        // Encode a matching palette at (0, 1).
        let mut entries = [0u16; crate::cdf::PALETTE_COLORS];
        entries[0] = 11;
        entries[1] = 22;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_y(&mut writer, 8, 2, &entries, &walker, 0, 1).unwrap();
        let bytes = writer.finish();
        // The §5.11.46 `L(1)` reads are §8.2.5 literals — each rides
        // the §8.2.3 daala bool path, so the byte count reflects the
        // arithmetic-coder flush, not the raw 2 bits. The round-trip
        // through `read_palette_entries_y` is the load-bearing check.
        let pad = if bytes.is_empty() {
            vec![0u8; 4]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let decoded = crate::cdf::read_palette_entries_y(&mut dec, 8, 2, &walker, 0, 1).unwrap();
        assert_eq!(decoded[0], 11);
        assert_eq!(decoded[1], 22);
    }

    /// §5.11.49 partial-cache round-trip: left-neighbour cache is
    /// `[11, 22]`; encode `[11, 99]` at (0, 1). The writer emits
    /// L(1)=1 (adopt 11), L(1)=0 (skip 22), L(8)=99 as the first new
    /// entry. The decoder reconstructs `[11, 99]`.
    #[test]
    fn write_palette_entries_y_partial_cache_round_trip() {
        let mut walker = fresh_palette_walker();
        walker.stamp_palette_for_test(0, 0, 0, &[11, 22]).unwrap();
        let mut entries = [0u16; crate::cdf::PALETTE_COLORS];
        entries[0] = 11;
        entries[1] = 99;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_y(&mut writer, 8, 2, &entries, &walker, 0, 1).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 4]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let decoded = crate::cdf::read_palette_entries_y(&mut dec, 8, 2, &walker, 0, 1).unwrap();
        assert_eq!(decoded[0], 11);
        assert_eq!(decoded[1], 99);
    }

    /// Caller bug: descending entries — the §5.11.46 reader's
    /// trailing sort guarantees ascending input on the writer side;
    /// a descending pair surfaces `PartitionWalkOutOfRange`.
    #[test]
    fn write_palette_entries_y_rejects_descending() {
        let walker = fresh_palette_walker();
        let mut entries = [0u16; crate::cdf::PALETTE_COLORS];
        entries[0] = 50;
        entries[1] = 10;
        let mut writer = SymbolWriter::new(true);
        let err = write_palette_entries_y(&mut writer, 8, 2, &entries, &walker, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: duplicate entries — non-positive delta on a Y
    /// arm where the spec's `palette_delta_y++` step never produces
    /// a delta_raw of `-1` (delta_raw = (cur - prev) - 1; cur == prev
    /// ⇒ delta_raw = -1 underflow). Reject as caller bug.
    #[test]
    fn write_palette_entries_y_rejects_duplicate() {
        let walker = fresh_palette_walker();
        let mut entries = [0u16; crate::cdf::PALETTE_COLORS];
        entries[0] = 10;
        entries[1] = 10;
        let mut writer = SymbolWriter::new(true);
        let err = write_palette_entries_y(&mut writer, 8, 2, &entries, &walker, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: entry above the bit-depth range. 8 bit-depth ⇒
    /// entries in [0, 255]; entry 256 is out-of-range.
    #[test]
    fn write_palette_entries_y_rejects_above_bit_depth_range() {
        let walker = fresh_palette_walker();
        let mut entries = [0u16; crate::cdf::PALETTE_COLORS];
        entries[0] = 100;
        entries[1] = 256;
        let mut writer = SymbolWriter::new(true);
        let err = write_palette_entries_y(&mut writer, 8, 2, &entries, &walker, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: `palette_size_y` below the §5.11.46 minimum of 2.
    #[test]
    fn write_palette_entries_y_rejects_size_below_two() {
        let walker = fresh_palette_walker();
        let entries = [0u16; crate::cdf::PALETTE_COLORS];
        let mut writer = SymbolWriter::new(true);
        let err = write_palette_entries_y(&mut writer, 8, 1, &entries, &walker, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: `palette_size_y` above PALETTE_COLORS (=8).
    #[test]
    fn write_palette_entries_y_rejects_size_above_max() {
        let walker = fresh_palette_walker();
        let entries = [0u16; crate::cdf::PALETTE_COLORS];
        let mut writer = SymbolWriter::new(true);
        let err = write_palette_entries_y(&mut writer, 8, 9, &entries, &walker, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: invalid bit_depth (§5.5.2 admits 8 / 10 / 12 only).
    #[test]
    fn write_palette_entries_y_rejects_invalid_bit_depth() {
        let walker = fresh_palette_walker();
        let mut entries = [0u16; crate::cdf::PALETTE_COLORS];
        entries[0] = 0;
        entries[1] = 1;
        let mut writer = SymbolWriter::new(true);
        let err = write_palette_entries_y(&mut writer, 9, 2, &entries, &walker, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.46 write_palette_entries_uv — round-trip through
    // crate::cdf::read_palette_entries_uv. The reader sorts U after
    // decode (canonical form); V is left in source order on both arms.
    // -----------------------------------------------------------------

    /// 2-entry UV palette `[0, 0]` U + `[0, 0]` V at 8 bit-depth on the
    /// V direct-literal arm, fresh walker (empty cache). Mirrors the
    /// cdf-side `read_palette_entries_uv_size_2_zero_bitstream_direct_literal_arm`
    /// fixture: writer emits the L(8) first U literal then L(2)
    /// extra_bits then L(paletteBits) raw delta then L(1)=0 for
    /// `delta_encode_v` then two L(8) V literals.
    #[test]
    fn write_palette_entries_uv_size_2_round_trip_8bit_direct_v_arm() {
        let walker = fresh_palette_walker();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        u[0] = 0;
        u[1] = 0;
        v[0] = 0;
        v[1] = 0;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_uv(&mut writer, 8, 2, &u, &v, false, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 8]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let (u_dec, v_dec) =
            crate::cdf::read_palette_entries_uv(&mut dec, 8, 2, &walker, 0, 0).unwrap();
        assert_eq!(u_dec[0], 0);
        assert_eq!(u_dec[1], 0);
        assert_eq!(v_dec[0], 0);
        assert_eq!(v_dec[1], 0);
    }

    /// 3-entry UV palette with ascending U and arbitrary V on the
    /// direct-literal arm at 8 bit-depth. U `[10, 50, 200]` exercises
    /// the U delta loop (no `++`); V `[123, 7, 200]` exercises the
    /// direct-literal arm where order is preserved.
    #[test]
    fn write_palette_entries_uv_size_3_round_trip_8bit_direct_v_arm() {
        let walker = fresh_palette_walker();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        u[0] = 10;
        u[1] = 50;
        u[2] = 200;
        v[0] = 123;
        v[1] = 7;
        v[2] = 200;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_uv(&mut writer, 8, 3, &u, &v, false, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 16]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let (u_dec, v_dec) =
            crate::cdf::read_palette_entries_uv(&mut dec, 8, 3, &walker, 0, 0).unwrap();
        assert_eq!(u_dec[0], 10);
        assert_eq!(u_dec[1], 50);
        assert_eq!(u_dec[2], 200);
        // V on direct-literal arm preserves source order.
        assert_eq!(v_dec[0], 123);
        assert_eq!(v_dec[1], 7);
        assert_eq!(v_dec[2], 200);
    }

    /// 3-entry UV palette on the V delta-encoded arm at 8 bit-depth.
    /// U `[10, 50, 200]`; V `[100, 110, 90]` — exercises both positive
    /// (`110 - 100 = +10`) and negative (`90 - 110 = -20`) signed
    /// deltas with the §5.11.46 sign-bit emission. Round-trip matches.
    #[test]
    fn write_palette_entries_uv_size_3_round_trip_8bit_delta_v_arm() {
        let walker = fresh_palette_walker();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        u[0] = 10;
        u[1] = 50;
        u[2] = 200;
        v[0] = 100;
        v[1] = 110;
        v[2] = 90;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_uv(&mut writer, 8, 3, &u, &v, true, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 16]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let (u_dec, v_dec) =
            crate::cdf::read_palette_entries_uv(&mut dec, 8, 3, &walker, 0, 0).unwrap();
        assert_eq!(u_dec[0], 10);
        assert_eq!(u_dec[1], 50);
        assert_eq!(u_dec[2], 200);
        assert_eq!(v_dec[0], 100);
        assert_eq!(v_dec[1], 110);
        assert_eq!(v_dec[2], 90);
    }

    /// §5.11.46 V delta-arm modular wrap: V `[10, 250]` at 8 bit-depth.
    /// The natural delta `250 - 10 = +240` has magnitude 240; the
    /// wrap candidate `240 - 256 = -16` has magnitude 16 (much
    /// smaller). The writer picks the wrap candidate, the reader's
    /// modular-wrap step reconstructs `cur = 250` from `prev = 10 +
    /// (-16) = -6 → -6 + 256 = 250`.
    #[test]
    fn write_palette_entries_uv_v_delta_arm_modular_wrap_round_trip() {
        let walker = fresh_palette_walker();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        u[0] = 0;
        u[1] = 1;
        v[0] = 10;
        v[1] = 250;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_uv(&mut writer, 8, 2, &u, &v, true, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 8]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let (_, v_dec) =
            crate::cdf::read_palette_entries_uv(&mut dec, 8, 2, &walker, 0, 0).unwrap();
        assert_eq!(v_dec[0], 10);
        assert_eq!(v_dec[1], 250);
    }

    /// Full palette of size 8 on the V delta arm at 8 bit-depth —
    /// exercises every step of the V delta loop. U is the same
    /// ascending sequence the Y refinement test uses (`[1, 30, 60,
    /// 90, 120, 180, 220, 250]`); V samples arbitrary entries.
    #[test]
    fn write_palette_entries_uv_max_size_round_trip_8bit_delta_v_arm() {
        let walker = fresh_palette_walker();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        let u_vals: [u16; 8] = [1, 30, 60, 90, 120, 180, 220, 250];
        u[..8].copy_from_slice(&u_vals);
        // V samples chosen so each consecutive transition's smallest
        // §5.11.46 wrap-candidate magnitude stays < 2^paletteBits at
        // the writer's maximum paletteBits_v = bd - 4 + 3 = 7 (=>
        // magnitude < 128). The wrap-around bound is maxVal/2 = 128 at
        // 8-bit; staying strictly below that ceiling keeps every
        // candidate representable.
        let v_vals: [u16; 8] = [128, 65, 192, 70, 200, 100, 156, 35];
        v[..8].copy_from_slice(&v_vals);
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_uv(&mut writer, 8, 8, &u, &v, true, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 32]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let (u_dec, v_dec) =
            crate::cdf::read_palette_entries_uv(&mut dec, 8, 8, &walker, 0, 0).unwrap();
        assert_eq!(&u_dec[..8], &u_vals);
        assert_eq!(&v_dec[..8], &v_vals);
    }

    /// 10 bit-depth path on the direct-literal V arm — exercises L(10)
    /// literals + `minBits = bd - 3 = 7` on U.
    #[test]
    fn write_palette_entries_uv_size_3_round_trip_10bit_direct_v_arm() {
        let walker = fresh_palette_walker();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        // 10-bit range is [0, 1023].
        u[0] = 100;
        u[1] = 500;
        u[2] = 900;
        v[0] = 800;
        v[1] = 200;
        v[2] = 600;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_uv(&mut writer, 10, 3, &u, &v, false, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 16]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let (u_dec, v_dec) =
            crate::cdf::read_palette_entries_uv(&mut dec, 10, 3, &walker, 0, 0).unwrap();
        assert_eq!(u_dec[0], 100);
        assert_eq!(u_dec[1], 500);
        assert_eq!(u_dec[2], 900);
        assert_eq!(v_dec[0], 800);
        assert_eq!(v_dec[1], 200);
        assert_eq!(v_dec[2], 600);
    }

    /// 12 bit-depth path on the V delta arm — `minBits_v = bd - 4 = 8`.
    #[test]
    fn write_palette_entries_uv_size_2_round_trip_12bit_delta_v_arm() {
        let walker = fresh_palette_walker();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        // 12-bit range is [0, 4095].
        u[0] = 1000;
        u[1] = 3000;
        v[0] = 2000;
        v[1] = 2500;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_uv(&mut writer, 12, 2, &u, &v, true, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 16]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let (u_dec, v_dec) =
            crate::cdf::read_palette_entries_uv(&mut dec, 12, 2, &walker, 0, 0).unwrap();
        assert_eq!(u_dec[0], 1000);
        assert_eq!(u_dec[1], 3000);
        assert_eq!(v_dec[0], 2000);
        assert_eq!(v_dec[1], 2500);
    }

    /// §5.11.49 cache-hit round-trip on U: stamp a left-neighbour U
    /// palette `[11, 22]` at (0, 0); encode matching U `[11, 22]` +
    /// arbitrary V at (0, 1). The writer emits two L(1)=1 cache flags
    /// for U (no literal/L(2)/delta loop), then the V direct-literal
    /// arm. Round-trip matches.
    #[test]
    fn write_palette_entries_uv_cache_hit_round_trip() {
        let mut walker = fresh_palette_walker();
        // Stamp left-neighbour U palette at (0, 0) — plane = 1.
        walker.stamp_palette_for_test(1, 0, 0, &[11, 22]).unwrap();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        u[0] = 11;
        u[1] = 22;
        v[0] = 33;
        v[1] = 44;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_uv(&mut writer, 8, 2, &u, &v, false, &walker, 0, 1).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 8]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let (u_dec, v_dec) =
            crate::cdf::read_palette_entries_uv(&mut dec, 8, 2, &walker, 0, 1).unwrap();
        assert_eq!(u_dec[0], 11);
        assert_eq!(u_dec[1], 22);
        assert_eq!(v_dec[0], 33);
        assert_eq!(v_dec[1], 44);
    }

    /// Caller bug: U descending — non-strict ascending invariant
    /// surfaces `PartitionWalkOutOfRange`.
    #[test]
    fn write_palette_entries_uv_rejects_u_descending() {
        let walker = fresh_palette_walker();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        u[0] = 50;
        u[1] = 10;
        v[0] = 0;
        v[1] = 0;
        let mut writer = SymbolWriter::new(true);
        let err =
            write_palette_entries_uv(&mut writer, 8, 2, &u, &v, false, &walker, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// U duplicates are LEGAL on the UV writer (the U delta arm has
    /// no `++`, so delta_raw == 0 is valid). Round-trip succeeds.
    #[test]
    fn write_palette_entries_uv_u_duplicates_accepted() {
        let walker = fresh_palette_walker();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        u[0] = 10;
        u[1] = 10;
        v[0] = 0;
        v[1] = 0;
        let mut writer = SymbolWriter::new(true);
        write_palette_entries_uv(&mut writer, 8, 2, &u, &v, false, &walker, 0, 0).unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() {
            vec![0u8; 8]
        } else {
            bytes
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), true).unwrap();
        let (u_dec, _) =
            crate::cdf::read_palette_entries_uv(&mut dec, 8, 2, &walker, 0, 0).unwrap();
        assert_eq!(u_dec[0], 10);
        assert_eq!(u_dec[1], 10);
    }

    /// Caller bug: U entry above the bit-depth range.
    #[test]
    fn write_palette_entries_uv_rejects_u_above_bit_depth_range() {
        let walker = fresh_palette_walker();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        u[0] = 100;
        u[1] = 256;
        v[0] = 0;
        v[1] = 0;
        let mut writer = SymbolWriter::new(true);
        let err =
            write_palette_entries_uv(&mut writer, 8, 2, &u, &v, false, &walker, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: V entry above the bit-depth range.
    #[test]
    fn write_palette_entries_uv_rejects_v_above_bit_depth_range() {
        let walker = fresh_palette_walker();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        u[0] = 0;
        u[1] = 1;
        v[0] = 0;
        v[1] = 256;
        let mut writer = SymbolWriter::new(true);
        let err =
            write_palette_entries_uv(&mut writer, 8, 2, &u, &v, false, &walker, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: `palette_size_uv` below the §5.11.46 minimum of 2.
    #[test]
    fn write_palette_entries_uv_rejects_size_below_two() {
        let walker = fresh_palette_walker();
        let u = [0u16; crate::cdf::PALETTE_COLORS];
        let v = [0u16; crate::cdf::PALETTE_COLORS];
        let mut writer = SymbolWriter::new(true);
        let err =
            write_palette_entries_uv(&mut writer, 8, 1, &u, &v, false, &walker, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: `palette_size_uv` above PALETTE_COLORS (=8).
    #[test]
    fn write_palette_entries_uv_rejects_size_above_max() {
        let walker = fresh_palette_walker();
        let u = [0u16; crate::cdf::PALETTE_COLORS];
        let v = [0u16; crate::cdf::PALETTE_COLORS];
        let mut writer = SymbolWriter::new(true);
        let err =
            write_palette_entries_uv(&mut writer, 8, 9, &u, &v, false, &walker, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: invalid bit_depth (§5.5.2 admits 8 / 10 / 12 only).
    #[test]
    fn write_palette_entries_uv_rejects_invalid_bit_depth() {
        let walker = fresh_palette_walker();
        let mut u = [0u16; crate::cdf::PALETTE_COLORS];
        let mut v = [0u16; crate::cdf::PALETTE_COLORS];
        u[0] = 0;
        u[1] = 1;
        v[0] = 0;
        v[1] = 0;
        let mut writer = SymbolWriter::new(true);
        let err =
            write_palette_entries_uv(&mut writer, 9, 2, &u, &v, false, &walker, 0, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.22 write_intra_block_mode_info dispatcher — full
    // round-trip through PartitionWalker::decode_intra_block_mode_info.
    // -----------------------------------------------------------------

    /// Round-trip the §5.11.22 dispatcher on the default fixture
    /// (`BLOCK_16X16`, `y_mode = DC_PRED`, `uv_mode = DC_PRED`,
    /// gates closed, no chroma subsampling, monochrome=false). All
    /// short-circuits fire; the decoder reconstructs every field.
    #[test]
    fn write_intra_block_mode_info_dc_pred_full_round_trip() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_intra_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            0,
            0,
            /* cfl_allowed = */ true,
            /* has_chroma = */ true,
            /* allow_screen_content_tools = */ false,
            /* enable_filter_intra = */ false,
            0,
            None,
            false,
            false,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                /* lossless = */ false,
                /* has_chroma = */ true,
                /* allow_screen_content_tools = */ false,
                /* enable_filter_intra = */ false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, DC_PRED_U8);
        assert_eq!(info.uv_mode, Some(DC_PRED_U8));
        assert_eq!(info.angle_delta_y, 0);
        assert_eq!(info.angle_delta_uv, Some(0));
        assert_eq!(info.cfl_alpha_u, None);
        assert_eq!(info.cfl_alpha_v, None);
        assert_eq!(info.has_palette_y, None);
        assert_eq!(info.has_palette_uv, None);
        assert_eq!(info.use_filter_intra, None);
        assert_eq!(info.filter_intra_mode, None);
    }

    /// Round-trip the §5.11.22 dispatcher with a directional
    /// `y_mode = V_PRED` and a non-zero `angle_delta_y = 2`. The
    /// §5.11.42 inner-arm fires for the luma plane; UV stays DC_PRED
    /// non-directional so the §5.11.43 short-circuit holds.
    #[test]
    fn write_intra_block_mode_info_directional_y_round_trip() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_intra_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            V_PRED_U8,
            Some(DC_PRED_U8),
            /* angle_delta_y = */ 2,
            0,
            true,
            true,
            false,
            false,
            0,
            None,
            false,
            false,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                true,
                false,
                false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, V_PRED_U8);
        assert_eq!(info.uv_mode, Some(DC_PRED_U8));
        assert_eq!(info.angle_delta_y, 2);
        assert_eq!(info.angle_delta_uv, Some(0));
    }

    /// Round-trip the §5.11.22 dispatcher with `allow_screen_content_tools
    /// = true` (palette gate open): writer commits has_palette_* = 0
    /// on both planes, decoder reconstructs `Some(0)` on both arms.
    #[test]
    fn write_intra_block_mode_info_palette_gate_open_no_palette_round_trip() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_intra_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            0,
            0,
            true,
            true,
            /* allow_screen_content_tools = */ true,
            false,
            0,
            None,
            false,
            false,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                true,
                /* allow_screen_content_tools = */ true,
                false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, DC_PRED_U8);
        assert_eq!(info.uv_mode, Some(DC_PRED_U8));
        assert_eq!(info.has_palette_y, Some(0));
        assert_eq!(info.has_palette_uv, Some(0));
    }

    /// Round-trip with `enable_filter_intra = true` and
    /// `use_filter_intra = 1` + `filter_intra_mode = Some(3)`. All
    /// three §5.11.22 leaves cooperate; the §5.11.24 outer gate fires
    /// (DC_PRED + palette_size_y = 0 + Max(bw,bh) = 16 ≤ 32).
    #[test]
    fn write_intra_block_mode_info_filter_intra_arm_round_trip() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_intra_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            0,
            0,
            true,
            true,
            false,
            /* enable_filter_intra = */ true,
            /* use_filter_intra = */ 1,
            Some(3),
            false,
            false,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                true,
                false,
                /* enable_filter_intra = */ true,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.use_filter_intra, Some(1));
        assert_eq!(info.filter_intra_mode, Some(3));
    }

    /// Monochrome round-trip (`has_chroma = false`): dispatcher
    /// skips the §5.11.22 line 5-10 chroma arm. `uv_mode` MUST be
    /// `None`; `angle_delta_uv` MUST be `0`.
    #[test]
    fn write_intra_block_mode_info_monochrome_round_trip() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_intra_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            None,
            0,
            0,
            true,
            /* has_chroma = */ false,
            false,
            false,
            0,
            None,
            false,
            false,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                /* has_chroma = */ false,
                false,
                false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, DC_PRED_U8);
        assert_eq!(info.uv_mode, None);
        assert_eq!(info.angle_delta_uv, None);
    }

    /// Caller bug: `has_chroma = true` with `uv_mode = None`.
    #[test]
    fn write_intra_block_mode_info_rejects_missing_uv_mode_when_has_chroma() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_intra_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            None,
            0,
            0,
            true,
            true,
            false,
            false,
            0,
            None,
            false,
            false,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: dispatcher rejects the CFL arm (UV_CFL_PRED = 13)
    /// on this round's scope — composed `write_cfl_alphas` is a
    /// future-round arc.
    #[test]
    fn write_intra_block_mode_info_rejects_uv_cfl_pred() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_intra_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(13),
            0,
            0,
            true,
            true,
            false,
            false,
            0,
            None,
            false,
            false,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: monochrome (`has_chroma = false`) with `Some`
    /// uv_mode contradicts the §5.11.22 line 5 `if (HasChroma)`
    /// guard.
    #[test]
    fn write_intra_block_mode_info_rejects_some_uv_when_monochrome() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let err = write_intra_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            0,
            0,
            true,
            /* has_chroma = */ false,
            false,
            false,
            0,
            None,
            false,
            false,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.46 write_palette_mode_info_with_entries — round-trip the
    // lifted leaf through the reader's full §5.11.46 sub-block via
    // decode_intra_block_mode_info.
    // -----------------------------------------------------------------

    /// Gate-off: `allow_screen_content_tools = false` ⇒ no §5.11.46
    /// S() symbol emitted. Same shape as the legacy leaf's gate-off
    /// test, but exercised through the r264 entries-aware leaf with
    /// dummy entry slots.
    #[test]
    fn write_palette_mode_info_with_entries_gate_off_no_bits() {
        let walker = fresh_palette_walker();
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let baseline = SymbolWriter::new(false).finish().len();
        let dummy = [0u16; PALETTE_COLORS];
        write_palette_mode_info_with_entries(
            &mut writer,
            &mut cdfs,
            /* has_palette_y = */ 0,
            /* has_palette_uv = */ 0,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            /* has_chroma = */ true,
            /* allow_screen_content_tools = */ false,
            false,
            false,
            8,
            /* palette_size_y = */ 0,
            &dummy,
            /* palette_size_uv = */ 0,
            &dummy,
            &dummy,
            false,
            &walker,
            0,
            0,
        )
        .unwrap();
        assert_eq!(
            writer.finish().len(),
            baseline,
            "§5.11.46 gate-off path emits zero additional bits via the r264 leaf"
        );
    }

    /// Gate-off via `mi_size < BLOCK_8X8` while
    /// `allow_screen_content_tools = true`. Mirrors the legacy leaf's
    /// `write_palette_mode_info_small_block_no_bits`.
    #[test]
    fn write_palette_mode_info_with_entries_small_block_no_bits() {
        let walker = fresh_palette_walker();
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let baseline = SymbolWriter::new(false).finish().len();
        let dummy = [0u16; PALETTE_COLORS];
        write_palette_mode_info_with_entries(
            &mut writer,
            &mut cdfs,
            0,
            0,
            BLOCK_4X4,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            true,
            /* allow_screen_content_tools = */ true,
            false,
            false,
            8,
            0,
            &dummy,
            0,
            &dummy,
            &dummy,
            false,
            &walker,
            0,
            0,
        )
        .unwrap();
        assert_eq!(writer.finish().len(), baseline);
    }

    /// Gate-off rejects non-zero `has_palette_y` (caller bug: the
    /// reader never reads the symbol on the gate-off arm).
    #[test]
    fn write_palette_mode_info_with_entries_gate_off_rejects_has_palette_y_one() {
        let walker = fresh_palette_walker();
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let mut entries = [0u16; PALETTE_COLORS];
        entries[0] = 0;
        entries[1] = 1;
        let err = write_palette_mode_info_with_entries(
            &mut writer,
            &mut cdfs,
            1,
            0,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            true,
            /* allow_screen_content_tools = */ false,
            false,
            false,
            8,
            2,
            &entries,
            0,
            &entries,
            &entries,
            false,
            &walker,
            0,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller bug: `has_palette_y == 1` with `palette_size_y` outside
    /// `2..=PALETTE_COLORS`.
    #[test]
    fn write_palette_mode_info_with_entries_rejects_palette_size_y_out_of_range() {
        let walker = fresh_palette_walker();
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(true);
        let entries = [0u16; PALETTE_COLORS];
        let err = write_palette_mode_info_with_entries(
            &mut writer,
            &mut cdfs,
            1,
            0,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            true,
            true,
            false,
            false,
            8,
            /* palette_size_y = */ 1,
            &entries,
            0,
            &entries,
            &entries,
            false,
            &walker,
            0,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Luma arm fires: `has_palette_y = 1`, `palette_size_y = 2`,
    /// entries `[0, 1]`. Round-trip through the reader's full
    /// §5.11.46 sub-block via decode_intra_block_mode_info — exposes
    /// the writer's `palette_size_y_minus_2 S()` + the §5.11.46
    /// palette-entries syntax committed by [`write_palette_entries_y`].
    ///
    /// Embedded in a dispatcher-shaped bit stream so the decoder's
    /// upstream `y_mode` / angle_delta steps consume the same prefix
    /// the writer emitted.
    #[test]
    fn write_palette_mode_info_with_entries_luma_arm_round_trip() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let mut entries_y = [0u16; PALETTE_COLORS];
        entries_y[0] = 0;
        entries_y[1] = 1;
        let dummy = [0u16; PALETTE_COLORS];

        // Use the r264 full dispatcher so the bit stream a decoder
        // walks is well-formed: y_mode + angle_delta + uv_mode +
        // angle_delta_uv + the §5.11.46 palette body + the §5.11.24
        // filter_intra closer (gate closes mechanically when
        // PaletteSizeY != 0).
        write_intra_block_mode_info_with_palette(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            0,
            0,
            /* cfl_allowed = */ true,
            /* has_chroma = */ true,
            /* allow_screen_content_tools = */ true,
            /* enable_filter_intra = */ false,
            0,
            None,
            false,
            false,
            8,
            /* has_palette_y = */ 1,
            /* palette_size_y = */ 2,
            &entries_y,
            /* has_palette_uv = */ 0,
            0,
            &dummy,
            &dummy,
            false,
            /* cfl_alpha_u = */ None,
            /* cfl_alpha_v = */ None,
            &walker_w,
            0,
            0,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker_r, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker_r
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                true,
                /* allow_screen_content_tools = */ true,
                false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, DC_PRED_U8);
        assert_eq!(info.has_palette_y, Some(1));
        assert_eq!(info.palette_size_y, Some(2));
        let pal_y = info.palette_colors_y.expect("luma arm fired");
        assert_eq!(pal_y[0], 0);
        assert_eq!(pal_y[1], 1);
        // Chroma arm closed (has_palette_uv = 0). The §5.11.46 reader
        // commits Some(0) on a fired chroma S() even on the off-arm.
        assert_eq!(info.has_palette_uv, Some(0));
    }

    /// Chroma arm fires: `has_palette_y = 0`, `has_palette_uv = 1`,
    /// `palette_size_uv = 2`, U `[0, 1]`, V `[5, 7]`, V-direct arm.
    /// Round-trip through the dispatcher into the reader.
    #[test]
    fn write_palette_mode_info_with_entries_chroma_direct_v_arm_round_trip() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let dummy = [0u16; PALETTE_COLORS];
        let mut entries_u = [0u16; PALETTE_COLORS];
        entries_u[0] = 0;
        entries_u[1] = 1;
        let mut entries_v = [0u16; PALETTE_COLORS];
        entries_v[0] = 5;
        entries_v[1] = 7;
        write_intra_block_mode_info_with_palette(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            0,
            0,
            true,
            true,
            /* allow_screen_content_tools = */ true,
            false,
            0,
            None,
            false,
            false,
            8,
            /* has_palette_y = */ 0,
            0,
            &dummy,
            /* has_palette_uv = */ 1,
            /* palette_size_uv = */ 2,
            &entries_u,
            &entries_v,
            /* delta_encode_v = */ false,
            /* cfl_alpha_u = */ None,
            /* cfl_alpha_v = */ None,
            &walker_w,
            0,
            0,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker_r, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker_r
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                true,
                true,
                false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, DC_PRED_U8);
        assert_eq!(info.has_palette_y, Some(0));
        assert_eq!(info.has_palette_uv, Some(1));
        assert_eq!(info.palette_size_uv, Some(2));
        let pal_u = info.palette_colors_u.expect("chroma arm fired");
        assert_eq!(pal_u[0], 0);
        assert_eq!(pal_u[1], 1);
        let pal_v = info.palette_colors_v.expect("chroma arm fired");
        assert_eq!(pal_v[0], 5);
        assert_eq!(pal_v[1], 7);
    }

    /// Both arms fire: luma `[0, 3]` size 2, chroma U `[0, 1]` + V
    /// `[10, 12]` size 2 with the V delta-encoded arm. Round-trip the
    /// full §5.11.46 body via the r264 dispatcher into the reader.
    #[test]
    fn write_palette_mode_info_with_entries_both_arms_delta_v_round_trip() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let mut entries_y = [0u16; PALETTE_COLORS];
        entries_y[0] = 0;
        entries_y[1] = 3;
        let mut entries_u = [0u16; PALETTE_COLORS];
        entries_u[0] = 0;
        entries_u[1] = 1;
        let mut entries_v = [0u16; PALETTE_COLORS];
        entries_v[0] = 10;
        entries_v[1] = 12;
        write_intra_block_mode_info_with_palette(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            0,
            0,
            true,
            true,
            /* allow_screen_content_tools = */ true,
            false,
            0,
            None,
            false,
            false,
            8,
            /* has_palette_y = */ 1,
            2,
            &entries_y,
            /* has_palette_uv = */ 1,
            2,
            &entries_u,
            &entries_v,
            /* delta_encode_v = */ true,
            /* cfl_alpha_u = */ None,
            /* cfl_alpha_v = */ None,
            &walker_w,
            0,
            0,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker_r, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker_r
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                true,
                true,
                false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.has_palette_y, Some(1));
        assert_eq!(info.palette_size_y, Some(2));
        let pal_y = info.palette_colors_y.expect("luma arm fired");
        assert_eq!(pal_y[0], 0);
        assert_eq!(pal_y[1], 3);
        assert_eq!(info.has_palette_uv, Some(1));
        assert_eq!(info.palette_size_uv, Some(2));
        let pal_u = info.palette_colors_u.expect("chroma arm fired");
        assert_eq!(pal_u[0], 0);
        assert_eq!(pal_u[1], 1);
        let pal_v = info.palette_colors_v.expect("chroma arm fired");
        assert_eq!(pal_v[0], 10);
        assert_eq!(pal_v[1], 12);
    }

    /// The §5.11.24 filter_intra outer gate closes when `PaletteSizeY
    /// != 0` — even with `enable_filter_intra = true` + `use_filter_intra
    /// = 0`, the gate closes mechanically. Round-trip confirms the
    /// reader observes `use_filter_intra == None`.
    #[test]
    fn write_intra_block_mode_info_with_palette_filter_intra_closed_by_palette() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let mut entries_y = [0u16; PALETTE_COLORS];
        entries_y[0] = 0;
        entries_y[1] = 1;
        let dummy = [0u16; PALETTE_COLORS];
        write_intra_block_mode_info_with_palette(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            0,
            0,
            true,
            true,
            /* allow_screen_content_tools = */ true,
            /* enable_filter_intra = */ true,
            0,
            None,
            false,
            false,
            8,
            /* has_palette_y = */ 1,
            2,
            &entries_y,
            0,
            0,
            &dummy,
            &dummy,
            false,
            /* cfl_alpha_u = */ None,
            /* cfl_alpha_v = */ None,
            &walker_w,
            0,
            0,
        )
        .unwrap();
        let bytes = writer.finish();
        let (mut walker_r, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker_r
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                true,
                true,
                /* enable_filter_intra = */ true,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.has_palette_y, Some(1));
        assert_eq!(info.use_filter_intra, None);
        assert_eq!(info.filter_intra_mode, None);
    }

    // -----------------------------------------------------------------
    // r265: §5.11.22 line-8 CFL arm — write_cfl_alphas composition.
    // -----------------------------------------------------------------

    /// CFL arm round-trip with both alphas positive (`alpha_u = +1`,
    /// `alpha_v = +2`): exercises the §5.11.45 `signU == CFL_SIGN_POS
    /// && signV == CFL_SIGN_POS` joint-sign slot (`joint = 3*2 + 2 - 1
    /// = 7`), both per-plane CFL alpha S() emissions, and the
    /// dispatcher's CFL-arm threading between `write_intra_uv_mode`
    /// and `write_intra_angle_info_uv`.
    #[test]
    fn write_intra_block_mode_info_with_palette_cfl_arm_both_pos_round_trip() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let dummy = [0u16; PALETTE_COLORS];
        // UV_CFL_PRED = 13 per §3.
        let uv_cfl_pred: u8 = 13;
        write_intra_block_mode_info_with_palette(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(uv_cfl_pred),
            0,
            0,
            /* cfl_allowed = */ true,
            /* has_chroma = */ true,
            /* allow_screen_content_tools = */ false,
            /* enable_filter_intra = */ false,
            0,
            None,
            false,
            false,
            8,
            /* has_palette_y = */ 0,
            0,
            &dummy,
            /* has_palette_uv = */ 0,
            0,
            &dummy,
            &dummy,
            false,
            /* cfl_alpha_u = */ Some(1),
            /* cfl_alpha_v = */ Some(2),
            &walker_w,
            0,
            0,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker_r, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker_r
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                /* lossless = */ false,
                /* has_chroma = */ true,
                /* allow_screen_content_tools = */ false,
                /* enable_filter_intra = */ false,
                /* subsampling_x = */ false,
                /* subsampling_y = */ false,
                /* above_palette_y = */ false,
                /* left_palette_y = */ false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, DC_PRED_U8);
        assert_eq!(info.uv_mode, Some(uv_cfl_pred));
        assert_eq!(info.cfl_alpha_u, Some(1));
        assert_eq!(info.cfl_alpha_v, Some(2));
    }

    /// CFL arm round-trip with one zero / one negative alpha
    /// (`alpha_u = 0`, `alpha_v = -3`): exercises the §5.11.45
    /// `signU == CFL_SIGN_ZERO && signV == CFL_SIGN_NEG` joint-sign
    /// slot (`joint = 3*0 + 1 - 1 = 0`) — the no-bits U arm + the
    /// `signed = -(1 + raw)` V arm.
    #[test]
    fn write_intra_block_mode_info_with_palette_cfl_arm_zero_neg_round_trip() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let dummy = [0u16; PALETTE_COLORS];
        let uv_cfl_pred: u8 = 13;
        write_intra_block_mode_info_with_palette(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(uv_cfl_pred),
            0,
            0,
            true,
            true,
            false,
            false,
            0,
            None,
            false,
            false,
            8,
            0,
            0,
            &dummy,
            0,
            0,
            &dummy,
            &dummy,
            false,
            Some(0),
            Some(-3),
            &walker_w,
            0,
            0,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker_r, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker_r
            .decode_intra_block_mode_info(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                true,
                false,
                false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.uv_mode, Some(uv_cfl_pred));
        assert_eq!(info.cfl_alpha_u, Some(0));
        assert_eq!(info.cfl_alpha_v, Some(-3));
    }

    /// Caller-bug rejection: `uv_mode == UV_CFL_PRED` with
    /// `cfl_alpha_u == None`. The §5.11.22 CFL arm requires both
    /// alphas to be `Some`; passing `None` is a caller bug and the
    /// dispatcher MUST surface it before touching the bit stream.
    #[test]
    fn write_intra_block_mode_info_with_palette_cfl_arm_missing_alpha_rejected() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let dummy = [0u16; PALETTE_COLORS];
        let uv_cfl_pred: u8 = 13;
        let err = write_intra_block_mode_info_with_palette(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(uv_cfl_pred),
            0,
            0,
            true,
            true,
            false,
            false,
            0,
            None,
            false,
            false,
            8,
            0,
            0,
            &dummy,
            0,
            0,
            &dummy,
            &dummy,
            false,
            /* cfl_alpha_u = */ None,
            /* cfl_alpha_v = */ Some(1),
            &walker_w,
            0,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller-bug rejection: non-CFL `uv_mode` with `cfl_alpha_u ==
    /// Some(_)`. On the non-CFL chroma arm the §5.11.22 reader never
    /// touches `read_cfl_alphas`; passing `Some` is a caller bug.
    #[test]
    fn write_intra_block_mode_info_with_palette_non_cfl_with_alpha_rejected() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let dummy = [0u16; PALETTE_COLORS];
        let err = write_intra_block_mode_info_with_palette(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            0,
            0,
            true,
            true,
            false,
            false,
            0,
            None,
            false,
            false,
            8,
            0,
            0,
            &dummy,
            0,
            0,
            &dummy,
            &dummy,
            false,
            /* cfl_alpha_u = */ Some(1),
            /* cfl_alpha_v = */ None,
            &walker_w,
            0,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Caller-bug rejection: `uv_mode == UV_CFL_PRED` with
    /// `cfl_allowed == false`. The §8.3.2 CDF-row selector excludes
    /// `UV_CFL_PRED` on the CFL-not-allowed row (width 13 vs 14), so
    /// the writer MUST stay in lockstep with the reader's row choice.
    #[test]
    fn write_intra_block_mode_info_with_palette_cfl_arm_cfl_not_allowed_rejected() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let dummy = [0u16; PALETTE_COLORS];
        let uv_cfl_pred: u8 = 13;
        let err = write_intra_block_mode_info_with_palette(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(uv_cfl_pred),
            0,
            0,
            /* cfl_allowed = */ false,
            true,
            false,
            false,
            0,
            None,
            false,
            false,
            8,
            0,
            0,
            &dummy,
            0,
            0,
            &dummy,
            &dummy,
            false,
            Some(1),
            Some(1),
            &walker_w,
            0,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // r280: §5.11.7 `else` (non-intrabc) arm — write_intra_frame_else_arm
    // round-trips through decode_intra_frame_mode_info_else_arm.
    // -----------------------------------------------------------------

    /// Directional luma + chroma round-trip at the frame origin
    /// (`AvailU = AvailL = false` ⇒ both §8.3.2 ctx values are the
    /// `intra_mode_ctx(DC_PRED) = 0` identity): `YMode = V_PRED` with
    /// `AngleDeltaY = +2`, `UVMode = V_PRED` with `AngleDeltaUV = -1`.
    /// Exercises the `intra_frame_y_mode` S() (the element that
    /// distinguishes §5.11.7 from §5.11.22) plus both §5.11.42/§5.11.43
    /// angle reads, and asserts the `TileIntraFrameYModeCdf[0][0]`
    /// adaptation rows match on both sides.
    #[test]
    fn write_intra_frame_else_arm_directional_round_trip() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let dummy = [0u16; PALETTE_COLORS];
        let cfl_allowed = cfl_allowed_for_uv_mode(false, BLOCK_16X16, false, false);
        write_intra_frame_else_arm(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            V_PRED_U8,
            Some(V_PRED_U8),
            /* angle_delta_y = */ 2,
            /* angle_delta_uv = */ -1,
            cfl_allowed,
            /* has_chroma = */ true,
            /* allow_screen_content_tools = */ false,
            /* enable_filter_intra = */ false,
            0,
            None,
            false,
            false,
            8,
            0,
            0,
            &dummy,
            0,
            0,
            &dummy,
            &dummy,
            false,
            None,
            None,
            &walker_w,
            0,
            0,
            /* abovemode_ctx = */ 0,
            /* leftmode_ctx = */ 0,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker_r, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker_r
            .decode_intra_frame_mode_info_else_arm(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                /* lossless = */ false,
                /* has_chroma = */ true,
                /* allow_screen_content_tools = */ false,
                /* enable_filter_intra = */ false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, V_PRED_U8);
        assert_eq!(info.angle_delta_y, 2);
        assert_eq!(info.uv_mode, Some(V_PRED_U8));
        assert_eq!(info.angle_delta_uv, Some(-1));
        assert_eq!(info.ref_frame, [0, -1], "§5.11.7 prefix RefFrame pair");
        // §8.3 adaptation lockstep on the arm-distinguishing CDF row.
        assert_eq!(
            enc_cdfs.intra_frame_y_mode_cdf(0, 0).to_vec(),
            dec_cdfs.intra_frame_y_mode_cdf(0, 0).to_vec(),
            "TileIntraFrameYModeCdf[0][0] adaptation must match"
        );
    }

    /// Two adjacent BLOCK_8X8 blocks sharing one stream + one decode
    /// walker: block A at (0, 0) codes `V_PRED` with the (0, 0) ctx
    /// identity; block B at (0, 2) has `AvailL = true` so the writer
    /// passes `leftmode_ctx = intra_mode_ctx(V_PRED)` (mirroring its
    /// own §5.11.5 `YModes[][]` bookkeeping) and the reader must
    /// derive the same pair from the grid its block-A decode stamped.
    /// A ctx mismatch desynchronises the arithmetic coder and fails
    /// the block-B assertions.
    #[test]
    fn write_intra_frame_else_arm_neighbour_ctx_round_trip() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let dummy = [0u16; PALETTE_COLORS];
        let write_block = |writer: &mut SymbolWriter,
                           cdfs: &mut TileCdfContext,
                           y_mode: u8,
                           mi_col: u32,
                           leftmode_ctx: usize|
         -> Result<(), Error> {
            write_intra_frame_else_arm(
                writer,
                cdfs,
                BLOCK_8X8,
                y_mode,
                /* uv_mode = */ None,
                0,
                0,
                false,
                /* has_chroma = */ false,
                false,
                false,
                0,
                None,
                false,
                false,
                8,
                0,
                0,
                &dummy,
                0,
                0,
                &dummy,
                &dummy,
                false,
                None,
                None,
                &walker_w,
                0,
                mi_col,
                /* abovemode_ctx = */ 0,
                leftmode_ctx,
            )
        };
        // Block A at (0, 0): both neighbours unavailable.
        write_block(&mut writer, &mut enc_cdfs, V_PRED_U8, 0, 0).unwrap();
        // Block B at (0, 2): left neighbour is block A (`YMode =
        // V_PRED`), above row is outside the tile.
        write_block(
            &mut writer,
            &mut enc_cdfs,
            DC_PRED_U8,
            2,
            intra_mode_ctx(V_PRED),
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker_r, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let decode_block = |walker_r: &mut PartitionWalker,
                            dec: &mut SymbolDecoder<'_>,
                            dec_cdfs: &mut TileCdfContext,
                            mi_col: u32| {
            walker_r
                .decode_intra_frame_mode_info_else_arm(
                    dec, dec_cdfs, 0, mi_col, BLOCK_8X8, false, false, false, false, false, false,
                    false, false, 8,
                )
                .unwrap()
        };
        let info_a = decode_block(&mut walker_r, &mut dec, &mut dec_cdfs, 0);
        assert_eq!(info_a.y_mode, V_PRED_U8);
        let info_b = decode_block(&mut walker_r, &mut dec, &mut dec_cdfs, 2);
        assert_eq!(info_b.y_mode, DC_PRED_U8);
        // Monochrome arm: no chroma fields decoded.
        assert_eq!(info_b.uv_mode, None);
        assert_eq!(info_b.angle_delta_uv, None);
        // The block-B row is the one keyed by the stamped neighbour.
        let ctx = intra_mode_ctx(V_PRED);
        assert_eq!(
            enc_cdfs.intra_frame_y_mode_cdf(0, ctx).to_vec(),
            dec_cdfs.intra_frame_y_mode_cdf(0, ctx).to_vec(),
            "neighbour-keyed TileIntraFrameYModeCdf row adaptation must match"
        );
    }

    /// §5.11.45 CFL arm round-trip on the §5.11.7 composition:
    /// `UVMode = UV_CFL_PRED` with `CflAlphaU = -2` / `CflAlphaV = +1`
    /// (joint-sign slot `signU = NEG`, `signV = POS`).
    #[test]
    fn write_intra_frame_else_arm_cfl_round_trip() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let dummy = [0u16; PALETTE_COLORS];
        let uv_cfl_pred: u8 = 13;
        write_intra_frame_else_arm(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(uv_cfl_pred),
            0,
            0,
            /* cfl_allowed = */ true,
            /* has_chroma = */ true,
            false,
            false,
            0,
            None,
            false,
            false,
            8,
            0,
            0,
            &dummy,
            0,
            0,
            &dummy,
            &dummy,
            false,
            /* cfl_alpha_u = */ Some(-2),
            /* cfl_alpha_v = */ Some(1),
            &walker_w,
            0,
            0,
            0,
            0,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker_r, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker_r
            .decode_intra_frame_mode_info_else_arm(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                true,
                false,
                false,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, DC_PRED_U8);
        assert_eq!(info.uv_mode, Some(uv_cfl_pred));
        assert_eq!(info.cfl_alpha_u, Some(-2));
        assert_eq!(info.cfl_alpha_v, Some(1));
    }

    /// §5.11.46 palette arm round-trip on the §5.11.7 composition:
    /// `has_palette_y = 1` with the 2-entry `[0, 1]` fixture; the
    /// §5.11.24 filter-intra gate must close mechanically
    /// (`PaletteSizeY != 0`) even with `enable_filter_intra = true`.
    #[test]
    fn write_intra_frame_else_arm_palette_round_trip() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let mut entries_y = [0u16; PALETTE_COLORS];
        entries_y[0] = 0;
        entries_y[1] = 1;
        let dummy = [0u16; PALETTE_COLORS];
        write_intra_frame_else_arm(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_16X16,
            DC_PRED_U8,
            Some(DC_PRED_U8),
            0,
            0,
            /* cfl_allowed = */ true,
            /* has_chroma = */ true,
            /* allow_screen_content_tools = */ true,
            /* enable_filter_intra = */ true,
            0,
            None,
            false,
            false,
            8,
            /* has_palette_y = */ 1,
            2,
            &entries_y,
            /* has_palette_uv = */ 0,
            0,
            &dummy,
            &dummy,
            false,
            None,
            None,
            &walker_w,
            0,
            0,
            0,
            0,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker_r, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker_r
            .decode_intra_frame_mode_info_else_arm(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_16X16,
                false,
                true,
                true,
                true,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.has_palette_y, Some(1));
        assert_eq!(info.palette_size_y, Some(2));
        let colors = info.palette_colors_y.expect("entries decoded");
        assert_eq!(&colors[..2], &[0, 1]);
        assert_eq!(info.has_palette_uv, Some(0));
        assert_eq!(info.use_filter_intra, None, "§5.11.24 gate closed");
        assert_eq!(info.filter_intra_mode, None);
    }

    /// Monochrome (`HasChroma = false`) + §5.11.24 filter-intra arm:
    /// `use_filter_intra = 1` with `filter_intra_mode = 3`. The chroma
    /// reads are skipped entirely (no `uv_mode` / CFL / UV-angle
    /// symbols on the wire).
    #[test]
    fn write_intra_frame_else_arm_monochrome_filter_intra_round_trip() {
        let walker_w = fresh_palette_walker();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let dummy = [0u16; PALETTE_COLORS];
        write_intra_frame_else_arm(
            &mut writer,
            &mut enc_cdfs,
            BLOCK_8X8,
            DC_PRED_U8,
            None,
            0,
            0,
            false,
            /* has_chroma = */ false,
            false,
            /* enable_filter_intra = */ true,
            /* use_filter_intra = */ 1,
            /* filter_intra_mode = */ Some(3),
            false,
            false,
            8,
            0,
            0,
            &dummy,
            0,
            0,
            &dummy,
            &dummy,
            false,
            None,
            None,
            &walker_w,
            0,
            0,
            0,
            0,
        )
        .unwrap();
        let bytes = writer.finish();

        let (mut walker_r, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let info = walker_r
            .decode_intra_frame_mode_info_else_arm(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                BLOCK_8X8,
                false,
                false,
                false,
                true,
                false,
                false,
                false,
                false,
                8,
            )
            .unwrap();
        assert_eq!(info.y_mode, DC_PRED_U8);
        assert_eq!(info.uv_mode, None);
        assert_eq!(info.use_filter_intra, Some(1));
        assert_eq!(info.filter_intra_mode, Some(3));
    }

    /// Caller-bug battery: out-of-range `y_mode` (= INTRA_MODES),
    /// out-of-range `abovemode_ctx` (= INTRA_MODE_CONTEXTS), and a
    /// `Some` `uv_mode` on the monochrome arm. All three must surface
    /// BEFORE any symbol write.
    #[test]
    fn write_intra_frame_else_arm_rejects_caller_bugs() {
        let walker_w = fresh_palette_walker();
        let dummy = [0u16; PALETTE_COLORS];
        let run = |y_mode: u8, uv_mode: Option<u8>, has_chroma: bool, above_ctx: usize| {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_intra_frame_else_arm(
                &mut writer,
                &mut enc_cdfs,
                BLOCK_16X16,
                y_mode,
                uv_mode,
                0,
                0,
                true,
                has_chroma,
                false,
                false,
                0,
                None,
                false,
                false,
                8,
                0,
                0,
                &dummy,
                0,
                0,
                &dummy,
                &dummy,
                false,
                None,
                None,
                &walker_w,
                0,
                0,
                above_ctx,
                0,
            )
            .unwrap_err()
        };
        // §3: y_mode must be < INTRA_MODES = 13.
        assert!(matches!(
            run(13, Some(DC_PRED_U8), true, 0),
            Error::PartitionWalkOutOfRange
        ));
        // §8.3.2: ctx pair must be < INTRA_MODE_CONTEXTS = 5.
        assert!(matches!(
            run(DC_PRED_U8, Some(DC_PRED_U8), true, 5),
            Error::PartitionWalkOutOfRange
        ));
        // §5.11.7 `if ( HasChroma )` guard: `Some` uv_mode on the
        // monochrome arm is a caller bug.
        assert!(matches!(
            run(DC_PRED_U8, Some(DC_PRED_U8), false, 0),
            Error::PartitionWalkOutOfRange
        ));
    }

    // -----------------------------------------------------------------
    // §5.11.23 / §5.11.25 — write_inter_block_mode_info_bootstrap.
    // -----------------------------------------------------------------

    /// Arm 1 (`skip_mode == 1`) propagates `SkipModeFrame[ 0..2 ]`
    /// verbatim. No bits, no CDF accesses — the call site is a pure
    /// derivation.
    #[test]
    fn write_inter_block_mode_info_bootstrap_skip_mode_arm_propagates_skip_mode_frame() {
        // §5.9.22 yields a positive pair for `SkipModeFrame`. Pick
        // LAST_FRAME / GOLDEN_FRAME so the result is unambiguously
        // not an arm-3 fallback (which would force `[1, -1]`).
        let last_frame: i8 = 1; // LAST_FRAME per §3.
        let golden_frame: i8 = 4; // GOLDEN_FRAME per §3.
        let pair = write_inter_block_mode_info_bootstrap(
            1,
            [last_frame, golden_frame],
            // seg_ref_frame_active — ignored on arm 1.
            true,
            7,
            // seg_skip / seg_globalmv — ignored on arm 1.
            true,
            true,
        )
        .unwrap();
        assert_eq!(pair, (last_frame, golden_frame));
    }

    /// Arm 2 (`SEG_LVL_REF_FRAME`) stamps `FeatureData[..][..]` into
    /// slot 0 and `NONE = -1` into slot 1.
    #[test]
    fn write_inter_block_mode_info_bootstrap_seg_ref_frame_arm_stamps_feature_data_and_none() {
        // ALTREF2_FRAME = 6 per §3.
        let seg_data: i8 = 6;
        let pair = write_inter_block_mode_info_bootstrap(
            0,
            // Arm-1 inputs ignored on arm 2.
            [0, 0],
            true,
            seg_data,
            // Arm-3 inputs ignored on arm 2.
            true,
            true,
        )
        .unwrap();
        assert_eq!(pair, (seg_data, -1));
    }

    /// Arm 3 (`SEG_LVL_SKIP || SEG_LVL_GLOBALMV` with the earlier two
    /// selectors false) stamps `LAST_FRAME = 1` into slot 0 and
    /// `NONE = -1` into slot 1. Verify both selectors trigger the
    /// arm independently.
    #[test]
    fn write_inter_block_mode_info_bootstrap_seg_skip_only_arm_stamps_last_and_none() {
        let pair = write_inter_block_mode_info_bootstrap(0, [0, 0], false, 0, true, false).unwrap();
        assert_eq!(pair, (1, -1));
    }

    #[test]
    fn write_inter_block_mode_info_bootstrap_seg_globalmv_only_arm_stamps_last_and_none() {
        let pair = write_inter_block_mode_info_bootstrap(0, [0, 0], false, 0, false, true).unwrap();
        assert_eq!(pair, (1, -1));
    }

    #[test]
    fn write_inter_block_mode_info_bootstrap_seg_skip_and_globalmv_arm_stamps_last_and_none() {
        let pair = write_inter_block_mode_info_bootstrap(0, [0, 0], false, 0, true, true).unwrap();
        assert_eq!(pair, (1, -1));
    }

    /// Arm 1 takes priority over arms 2 + 3 — even when the
    /// segmentation selectors are also active, `skip_mode == 1`
    /// must short-circuit to `SkipModeFrame`.
    #[test]
    fn write_inter_block_mode_info_bootstrap_skip_mode_priority_over_seg_arms() {
        let pair = write_inter_block_mode_info_bootstrap(1, [2, 3], true, 4, true, true).unwrap();
        assert_eq!(pair, (2, 3));
    }

    /// Arm 2 takes priority over arm 3 — even when `SEG_LVL_SKIP` is
    /// active, the `SEG_LVL_REF_FRAME` arm fires first.
    #[test]
    fn write_inter_block_mode_info_bootstrap_seg_ref_frame_priority_over_seg_skip() {
        let pair = write_inter_block_mode_info_bootstrap(0, [0, 0], true, 5, true, true).unwrap();
        // ALTREF2_FRAME = 6? No — `5` is BWDREF_FRAME per §3. Pair must
        // be (5, -1) regardless.
        assert_eq!(pair, (5, -1));
    }

    /// Fall-through `else` (arm 4) — all four selectors false — is
    /// deferred to a follow-up arc and rejected as caller-bug.
    #[test]
    fn write_inter_block_mode_info_bootstrap_fall_through_else_rejected() {
        let err =
            write_inter_block_mode_info_bootstrap(0, [0, 0], false, 0, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// `skip_mode > 1` violates the §3 binary alphabet.
    #[test]
    fn write_inter_block_mode_info_bootstrap_rejects_out_of_range_skip_mode() {
        let err =
            write_inter_block_mode_info_bootstrap(2, [1, 2], false, 0, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// `skip_mode == 1` with `SkipModeFrame[ 0 ] = NONE = -1` violates
    /// the §5.9.22 invariant — caller-bug.
    #[test]
    fn write_inter_block_mode_info_bootstrap_rejects_skip_mode_frame_with_none_slot0() {
        let err =
            write_inter_block_mode_info_bootstrap(1, [-1, 2], true, 0, true, true).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// `skip_mode == 1` with `SkipModeFrame[ 1 ] = NONE = -1` violates
    /// the §5.9.22 invariant — caller-bug.
    #[test]
    fn write_inter_block_mode_info_bootstrap_rejects_skip_mode_frame_with_none_slot1() {
        let err =
            write_inter_block_mode_info_bootstrap(1, [1, -1], true, 0, true, true).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// `skip_mode == 1` with `SkipModeFrame[ 0 ]` past ALTREF_FRAME =
    /// 7 violates §3.
    #[test]
    fn write_inter_block_mode_info_bootstrap_rejects_skip_mode_frame_above_altref() {
        let err =
            write_inter_block_mode_info_bootstrap(1, [8, 2], false, 0, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm 2 with `seg_ref_frame_data = NONE = -1` violates the §6.4.1
    /// segmentation-feature alphabet for `SEG_LVL_REF_FRAME`.
    #[test]
    fn write_inter_block_mode_info_bootstrap_rejects_seg_ref_frame_none() {
        let err =
            write_inter_block_mode_info_bootstrap(0, [0, 0], true, -1, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm 2 with `seg_ref_frame_data > ALTREF_FRAME = 7` violates §3.
    #[test]
    fn write_inter_block_mode_info_bootstrap_rejects_seg_ref_frame_above_altref() {
        let err =
            write_inter_block_mode_info_bootstrap(0, [0, 0], true, 8, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arms 2 / 3 with `INTRA_FRAME = 0` for `seg_ref_frame_data` is
    /// **valid** — the §6.4.1 alphabet includes `0..=ALTREF_FRAME = 7`,
    /// and a segmented `INTRA_FRAME` ref drives the `isCompound = 0`
    /// (intra-only) downstream branch. The pair is `(0, -1)`.
    #[test]
    fn write_inter_block_mode_info_bootstrap_seg_ref_frame_intra_frame_is_valid() {
        let pair = write_inter_block_mode_info_bootstrap(0, [0, 0], true, 0, false, false).unwrap();
        assert_eq!(pair, (0, -1));
    }

    /// `isCompound = RefFrame[ 1 ] > INTRA_FRAME` per §5.11.23 line 5:
    /// the caller's downstream derivation MUST observe the bootstrap's
    /// returned pair. Sanity-check the predicate for each arm.
    #[test]
    fn write_inter_block_mode_info_bootstrap_is_compound_derivation_per_arm() {
        // Arm 1 with a positive `SkipModeFrame[ 1 ]` ⇒ isCompound = 1.
        let (_, slot1) =
            write_inter_block_mode_info_bootstrap(1, [1, 4], false, 0, false, false).unwrap();
        // INTRA_FRAME = 0; `1` and `4` both exceed 0.
        assert!(slot1 > 0);

        // Arm 2 stamps NONE = -1 in slot 1 ⇒ isCompound = 0.
        let (_, slot1) =
            write_inter_block_mode_info_bootstrap(0, [0, 0], true, 3, false, false).unwrap();
        assert!(slot1 <= 0);

        // Arm 3 stamps NONE = -1 in slot 1 ⇒ isCompound = 0.
        let (_, slot1) =
            write_inter_block_mode_info_bootstrap(0, [0, 0], false, 0, true, false).unwrap();
        assert!(slot1 <= 0);
    }

    // -----------------------------------------------------------------
    // §5.11.25 arm 4 first symbol — write_comp_mode.
    // -----------------------------------------------------------------

    /// §8.3.2 `comp_mode` ctx at the frame origin: both neighbours
    /// unavailable (`AvailU == AvailL == false`) ⇒ the `else` branch
    /// `ctx = 1`. The neighbour single/intra/ref-frame inputs are
    /// irrelevant when neither neighbour is available, so we pass the
    /// no-neighbour shape.
    fn comp_mode_ctx_at_origin() -> usize {
        comp_mode_ctx(false, false, true, true, false, false, [-1, -1], [-1, -1])
    }

    /// §5.11.25 arm 4 explicit path: `reference_select == true` and
    /// `Min( bw4, bh4 ) >= 2` (BLOCK_8X8 ⇒ bw4 = bh4 = 2) ⇒ one `S()`
    /// emitted. Round-trip `comp_mode = SINGLE_REFERENCE = 0` through
    /// the decoder's `comp_mode` symbol read with the §8.3 CDF
    /// adaptation engaged on both sides.
    #[test]
    fn write_comp_mode_single_reference_round_trip_at_origin() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let ctx = comp_mode_ctx_at_origin();
        write_comp_mode(&mut writer, &mut enc_cdfs, 0, BLOCK_8X8, true, ctx).unwrap();
        let bytes = writer.finish();

        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let row = dec_cdfs.comp_mode_cdf(ctx);
        let comp_mode = dec.read_symbol(row).unwrap();
        assert_eq!(comp_mode, 0, "SINGLE_REFERENCE");
    }

    /// §5.11.25 arm 4 explicit path: `comp_mode = COMPOUND_REFERENCE = 1`
    /// round-trips through the decoder's `comp_mode` symbol read at the
    /// frame origin. CDF row chosen by the encoder MUST match the
    /// decoder's `ctx` or the read would diverge.
    #[test]
    fn write_comp_mode_compound_reference_round_trip_at_origin() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let ctx = comp_mode_ctx_at_origin();
        write_comp_mode(&mut writer, &mut enc_cdfs, 1, BLOCK_8X8, true, ctx).unwrap();
        let bytes = writer.finish();

        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let row = dec_cdfs.comp_mode_cdf(ctx);
        let comp_mode = dec.read_symbol(row).unwrap();
        assert_eq!(comp_mode, 1, "COMPOUND_REFERENCE");
    }

    /// §5.11.25 arm 4 suppressed-bit path: `reference_select == false`
    /// ⇒ no `S()` emitted (the spec forces `comp_mode = SINGLE_REFERENCE`
    /// with no bit). The finished symbol stream is empty of payload.
    #[test]
    fn write_comp_mode_reference_select_false_emits_no_bit() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let ctx = comp_mode_ctx_at_origin();
        write_comp_mode(&mut writer, &mut enc_cdfs, 0, BLOCK_8X8, false, ctx).unwrap();
        let bytes = writer.finish();
        // The CDF row MUST be untouched — no symbol was coded.
        let mut pristine = TileCdfContext::new_from_defaults();
        let expected: Vec<u16> = pristine.comp_mode_cdf(ctx).to_vec();
        let actual: Vec<u16> = enc_cdfs.comp_mode_cdf(ctx).to_vec();
        assert_eq!(actual, expected, "no S() emitted ⇒ comp_mode CDF unchanged");
        // An empty payload finishes to the minimal symbol-stream tail.
        assert!(bytes.len() <= 2, "no payload bits coded");
    }

    /// §5.11.25 arm 4 suppressed-bit path via the size precondition:
    /// `Min( bw4, bh4 ) < 2` (BLOCK_4X4 ⇒ bw4 = bh4 = 1) forces
    /// `comp_mode = SINGLE_REFERENCE` even with `reference_select == true`.
    #[test]
    fn write_comp_mode_small_block_emits_no_bit() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let ctx = comp_mode_ctx_at_origin();
        write_comp_mode(&mut writer, &mut enc_cdfs, 0, BLOCK_4X4, true, ctx).unwrap();
        let bytes = writer.finish();
        assert!(bytes.len() <= 2, "no payload bits coded for a sub-8 block");
    }

    /// §5.11.25 arm 4: BLOCK_4X8 / BLOCK_8X4 have `Min( bw4, bh4 ) = 1`
    /// ⇒ suppressed-bit even with `reference_select == true`. The
    /// rectangular sub-8 shapes exercise the `Min` (not `Max`) bound.
    #[test]
    fn write_comp_mode_rectangular_sub8_blocks_suppress_bit() {
        for &mi_size in &[BLOCK_4X8, BLOCK_8X4] {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let ctx = comp_mode_ctx_at_origin();
            // comp_mode MUST be SINGLE_REFERENCE on the suppressed path.
            write_comp_mode(&mut writer, &mut enc_cdfs, 0, mi_size, true, ctx).unwrap();
            let bytes = writer.finish();
            assert!(bytes.len() <= 2, "Min(bw4,bh4) = 1 ⇒ no bit");
        }
    }

    /// §3 binary alphabet: `comp_mode > 1` is rejected.
    #[test]
    fn write_comp_mode_rejects_out_of_range_value() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_comp_mode(&mut writer, &mut cdfs, 2, BLOCK_8X8, true, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Invalid §3 block-size index is rejected.
    #[test]
    fn write_comp_mode_rejects_out_of_range_mi_size() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_comp_mode(&mut writer, &mut cdfs, 0, BLOCK_SIZES, true, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Suppressed-bit path with `comp_mode != SINGLE_REFERENCE` is a
    /// caller bug — the spec forces `SINGLE_REFERENCE` with no bit, so
    /// any other value cannot be represented.
    #[test]
    fn write_comp_mode_rejects_compound_on_suppressed_path() {
        // reference_select == false suppresses the bit.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_comp_mode(&mut writer, &mut cdfs, 1, BLOCK_8X8, false, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        // Min(bw4,bh4) < 2 suppresses the bit too.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_comp_mode(&mut writer, &mut cdfs, 1, BLOCK_4X4, true, 0).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Explicit path with `ctx >= COMP_INTER_CONTEXTS` is rejected.
    #[test]
    fn write_comp_mode_rejects_out_of_range_ctx_on_explicit_path() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_comp_mode(
            &mut writer,
            &mut cdfs,
            0,
            BLOCK_8X8,
            true,
            COMP_INTER_CONTEXTS,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// §8.3.2 ctx coverage: every `ctx` in `0..COMP_INTER_CONTEXTS`
    /// round-trips both `comp_mode` values through the decoder. The
    /// CDF row the encoder writes MUST be the row the decoder reads.
    #[test]
    fn write_comp_mode_all_ctx_round_trip() {
        for ctx in 0..COMP_INTER_CONTEXTS {
            for value in 0u8..=1 {
                let mut enc_cdfs = TileCdfContext::new_from_defaults();
                let mut writer = SymbolWriter::new(false);
                write_comp_mode(&mut writer, &mut enc_cdfs, value, BLOCK_8X8, true, ctx).unwrap();
                let bytes = writer.finish();

                let mut dec_cdfs = TileCdfContext::new_from_defaults();
                let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
                let row = dec_cdfs.comp_mode_cdf(ctx);
                let got = dec.read_symbol(row).unwrap() as u8;
                assert_eq!(got, value, "ctx {ctx} value {value} round-trip");
            }
        }
    }

    // -----------------------------------------------------------------
    // §5.11.25 arm 4 COMPOUND_REFERENCE body — write_compound_ref_frames.
    // -----------------------------------------------------------------

    /// The §5.11.18 prologue neighbour-state octet the writer and the
    /// mirror reader both consume. Bundled so a test cannot
    /// accidentally hand the two sides different neighbour state.
    #[derive(Clone, Copy)]
    struct NeighbourOctet {
        avail_u: bool,
        avail_l: bool,
        above_single: bool,
        left_single: bool,
        above_intra: bool,
        left_intra: bool,
        above_ref_frame: [i32; 2],
        left_ref_frame: [i32; 2],
    }

    /// Frame-origin octet: both neighbours unavailable. Every
    /// `count_refs` is 0 ⇒ every `ref_count_ctx` selection is 1, and
    /// the §8.3.2 p.382 `comp_ref_type` ctx falls to the final `else`
    /// arm (`ctx = 2`).
    const ORIGIN: NeighbourOctet = NeighbourOctet {
        avail_u: false,
        avail_l: false,
        above_single: true,
        left_single: true,
        above_intra: false,
        left_intra: false,
        above_ref_frame: [-1, -1],
        left_ref_frame: [-1, -1],
    };

    /// The sixteen compound pairs the §5.11.25 COMPOUND_REFERENCE
    /// sub-tree reaches: four UNIDIR leaves + the 4 × 3 BIDIR product
    /// (§6.10.24 Group 1 × Group 2).
    const UNIDIR_PAIRS: [[i32; 2]; 4] = [[5, 7], [1, 2], [1, 3], [1, 4]];
    const BIDIR_PAIRS: [[i32; 2]; 12] = [
        [1, 5],
        [1, 6],
        [1, 7],
        [2, 5],
        [2, 6],
        [2, 7],
        [3, 5],
        [3, 6],
        [3, 7],
        [4, 5],
        [4, 6],
        [4, 7],
    ];

    /// Mirror of the decoder's §5.11.25 COMPOUND_REFERENCE symbol
    /// reads: `comp_ref_type` then the UNIDIR / BIDIR cascade, each
    /// `S()` against the same §8.3.2 CDF row selection the writer
    /// derives. Returns the decoded `RefFrame[ 0..2 ]` pair.
    fn read_compound_ref_frames_mirror(
        dec: &mut SymbolDecoder<'_>,
        cdfs: &mut TileCdfContext,
        n: NeighbourOctet,
    ) -> [i32; 2] {
        let cnt = |ft: i32| {
            count_refs(
                ft,
                n.avail_u,
                n.above_ref_frame,
                n.avail_l,
                n.left_ref_frame,
            )
        };
        // `comp_ref_type` S() (§8.3.2 p.382).
        let crt_ctx = comp_ref_type_ctx(
            n.avail_u,
            n.avail_l,
            n.above_single,
            n.left_single,
            n.above_intra,
            n.left_intra,
            n.above_ref_frame,
            n.left_ref_frame,
        );
        let comp_ref_type = dec.read_symbol(cdfs.comp_ref_type_cdf(crt_ctx)).unwrap();
        if comp_ref_type == 0 {
            // UNIDIR_COMP_REFERENCE: `uni_comp_ref` cascade.
            let ucr_ctx =
                ref_count_ctx(cnt(1) + cnt(2) + cnt(3) + cnt(4), cnt(5) + cnt(6) + cnt(7));
            let uni = dec.read_symbol(cdfs.uni_comp_ref_cdf(ucr_ctx, 0)).unwrap();
            if uni != 0 {
                return [5, 7]; // (BWDREF_FRAME, ALTREF_FRAME)
            }
            let p1_ctx = ref_count_ctx(cnt(2), cnt(3) + cnt(4));
            let p1 = dec.read_symbol(cdfs.uni_comp_ref_cdf(p1_ctx, 1)).unwrap();
            if p1 == 0 {
                return [1, 2]; // (LAST_FRAME, LAST2_FRAME)
            }
            let p2_ctx = ref_count_ctx(cnt(3), cnt(4));
            let p2 = dec.read_symbol(cdfs.uni_comp_ref_cdf(p2_ctx, 2)).unwrap();
            if p2 != 0 {
                [1, 4] // (LAST_FRAME, GOLDEN_FRAME)
            } else {
                [1, 3] // (LAST_FRAME, LAST3_FRAME)
            }
        } else {
            // BIDIR_COMP_REFERENCE: `comp_ref` then `comp_bwdref`.
            let cr_ctx = ref_count_ctx(cnt(1) + cnt(2), cnt(3) + cnt(4));
            let comp_ref = dec.read_symbol(cdfs.comp_ref_cdf(cr_ctx, 0)).unwrap();
            let ref0 = if comp_ref == 0 {
                let p1_ctx = ref_count_ctx(cnt(1), cnt(2));
                let p1 = dec.read_symbol(cdfs.comp_ref_cdf(p1_ctx, 1)).unwrap();
                if p1 != 0 {
                    2
                } else {
                    1
                }
            } else {
                let p2_ctx = ref_count_ctx(cnt(3), cnt(4));
                let p2 = dec.read_symbol(cdfs.comp_ref_cdf(p2_ctx, 2)).unwrap();
                if p2 != 0 {
                    4
                } else {
                    3
                }
            };
            let bw_ctx = ref_count_ctx(cnt(5) + cnt(6), cnt(7));
            let bwdref = dec.read_symbol(cdfs.comp_bwd_ref_cdf(bw_ctx, 0)).unwrap();
            let ref1 = if bwdref == 0 {
                let p1_ctx = ref_count_ctx(cnt(5), cnt(6));
                let p1 = dec.read_symbol(cdfs.comp_bwd_ref_cdf(p1_ctx, 1)).unwrap();
                if p1 != 0 {
                    6
                } else {
                    5
                }
            } else {
                7
            };
            [ref0, ref1]
        }
    }

    /// Encode one compound pair with `write_compound_ref_frames`, then
    /// decode it back through the mirror reader with an independent
    /// CDF context. Asserts the pair survives and that BOTH sides'
    /// adapted CDF tables are identical afterwards — if the writer
    /// selected a different §8.3.2 row than the reader for any symbol,
    /// the adapted-row sets would diverge.
    fn round_trip_pair(pair: [i32; 2], n: NeighbourOctet) {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_compound_ref_frames(
            &mut writer,
            &mut enc_cdfs,
            pair,
            n.avail_u,
            n.avail_l,
            n.above_single,
            n.left_single,
            n.above_intra,
            n.left_intra,
            n.above_ref_frame,
            n.left_ref_frame,
        )
        .unwrap();
        let bytes = writer.finish();

        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let got = read_compound_ref_frames_mirror(&mut dec, &mut dec_cdfs, n);
        assert_eq!(got, pair, "pair {pair:?} round-trip");
        assert_ref_tables_eq(&mut enc_cdfs, &mut dec_cdfs);
    }

    /// Compare every §8.3.2 ref-frame CDF row (`comp_ref_type` /
    /// `uni_comp_ref` / `comp_ref` / `comp_bwdref`, all ctx × p slots)
    /// between two contexts.
    fn assert_ref_tables_eq(a: &mut TileCdfContext, b: &mut TileCdfContext) {
        for ctx in 0..crate::cdf::COMP_REF_TYPE_CONTEXTS {
            assert_eq!(
                a.comp_ref_type_cdf(ctx).to_vec(),
                b.comp_ref_type_cdf(ctx).to_vec(),
                "comp_ref_type ctx {ctx}"
            );
        }
        for ctx in 0..crate::cdf::REF_CONTEXTS {
            for p in 0..3 {
                assert_eq!(
                    a.uni_comp_ref_cdf(ctx, p).to_vec(),
                    b.uni_comp_ref_cdf(ctx, p).to_vec(),
                    "uni_comp_ref ctx {ctx} p {p}"
                );
                assert_eq!(
                    a.comp_ref_cdf(ctx, p).to_vec(),
                    b.comp_ref_cdf(ctx, p).to_vec(),
                    "comp_ref ctx {ctx} p {p}"
                );
            }
            for p in 0..2 {
                assert_eq!(
                    a.comp_bwd_ref_cdf(ctx, p).to_vec(),
                    b.comp_bwd_ref_cdf(ctx, p).to_vec(),
                    "comp_bwdref ctx {ctx} p {p}"
                );
            }
        }
    }

    /// All four §5.11.25 UNIDIR leaves round-trip at the frame origin.
    #[test]
    fn write_compound_ref_frames_unidir_pairs_round_trip_at_origin() {
        for pair in UNIDIR_PAIRS {
            round_trip_pair(pair, ORIGIN);
        }
    }

    /// All twelve §6.10.24 Group-1 × Group-2 BIDIR pairs round-trip at
    /// the frame origin.
    #[test]
    fn write_compound_ref_frames_bidir_pairs_round_trip_at_origin() {
        for pair in BIDIR_PAIRS {
            round_trip_pair(pair, ORIGIN);
        }
    }

    /// Neighbour-engaged ctx selection: all sixteen pairs round-trip
    /// with non-trivial §8.3.2 contexts. Octet A puts a BIDIR compound
    /// neighbour above + a single-ref neighbour left; octet B puts a
    /// UNIDIR compound neighbour above + an intra neighbour left. The
    /// table-equality check inside `round_trip_pair` proves the writer
    /// derived the same per-symbol rows as the reader.
    #[test]
    fn write_compound_ref_frames_round_trip_with_neighbour_ctx() {
        let octet_a = NeighbourOctet {
            avail_u: true,
            avail_l: true,
            above_single: false,
            left_single: true,
            above_intra: false,
            left_intra: false,
            above_ref_frame: [1, 5],
            left_ref_frame: [3, -1],
        };
        let octet_b = NeighbourOctet {
            avail_u: true,
            avail_l: true,
            above_single: false,
            left_single: true,
            above_intra: false,
            left_intra: true,
            above_ref_frame: [5, 7],
            left_ref_frame: [0, -1],
        };
        for n in [octet_a, octet_b] {
            for pair in UNIDIR_PAIRS.iter().chain(BIDIR_PAIRS.iter()) {
                round_trip_pair(*pair, n);
            }
        }
    }

    /// `( BWDREF, ALTREF )` is the two-symbol UNIDIR leaf:
    /// `comp_ref_type` + `uni_comp_ref` only. The `uni_comp_ref_p1` /
    /// `uni_comp_ref_p2` rows and the entire BIDIR table set MUST stay
    /// pristine — extra symbols would adapt them.
    #[test]
    fn write_compound_ref_frames_bwdref_altref_emits_two_symbols() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_compound_ref_frames(
            &mut writer,
            &mut enc_cdfs,
            [5, 7],
            false,
            false,
            true,
            true,
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap();
        let mut pristine = TileCdfContext::new_from_defaults();
        // ref_count_ctx(0, 0) = 1 at the origin; §8.3.2 p.382 ctx = 2.
        assert_ne!(
            enc_cdfs.comp_ref_type_cdf(2).to_vec(),
            pristine.comp_ref_type_cdf(2).to_vec(),
            "comp_ref_type coded"
        );
        assert_ne!(
            enc_cdfs.uni_comp_ref_cdf(1, 0).to_vec(),
            pristine.uni_comp_ref_cdf(1, 0).to_vec(),
            "uni_comp_ref coded"
        );
        for p in 1..3 {
            assert_eq!(
                enc_cdfs.uni_comp_ref_cdf(1, p).to_vec(),
                pristine.uni_comp_ref_cdf(1, p).to_vec(),
                "uni_comp_ref p{p} NOT coded"
            );
        }
        for p in 0..3 {
            assert_eq!(
                enc_cdfs.comp_ref_cdf(1, p).to_vec(),
                pristine.comp_ref_cdf(1, p).to_vec(),
                "comp_ref p{p} NOT coded"
            );
        }
    }

    /// `( LAST, LAST2 )` stops after `uni_comp_ref_p1 = 0`: the
    /// `uni_comp_ref_p2` row stays pristine.
    #[test]
    fn write_compound_ref_frames_last_last2_leaves_p2_untouched() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_compound_ref_frames(
            &mut writer,
            &mut enc_cdfs,
            [1, 2],
            false,
            false,
            true,
            true,
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap();
        let mut pristine = TileCdfContext::new_from_defaults();
        assert_ne!(
            enc_cdfs.uni_comp_ref_cdf(1, 1).to_vec(),
            pristine.uni_comp_ref_cdf(1, 1).to_vec(),
            "uni_comp_ref_p1 coded"
        );
        assert_eq!(
            enc_cdfs.uni_comp_ref_cdf(1, 2).to_vec(),
            pristine.uni_comp_ref_cdf(1, 2).to_vec(),
            "uni_comp_ref_p2 NOT coded"
        );
    }

    /// A BIDIR pair with `RefFrame[ 1 ] = ALTREF_FRAME` takes the
    /// `comp_bwdref = 1` arm: the `comp_bwdref_p1` row stays pristine.
    #[test]
    fn write_compound_ref_frames_altref_arm_skips_bwdref_p1() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_compound_ref_frames(
            &mut writer,
            &mut enc_cdfs,
            [1, 7],
            false,
            false,
            true,
            true,
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap();
        let mut pristine = TileCdfContext::new_from_defaults();
        assert_ne!(
            enc_cdfs.comp_bwd_ref_cdf(1, 0).to_vec(),
            pristine.comp_bwd_ref_cdf(1, 0).to_vec(),
            "comp_bwdref coded"
        );
        assert_eq!(
            enc_cdfs.comp_bwd_ref_cdf(1, 1).to_vec(),
            pristine.comp_bwd_ref_cdf(1, 1).to_vec(),
            "comp_bwdref_p1 NOT coded"
        );
    }

    /// §6.10.24: a compound pair never carries `INTRA_FRAME = 0` or
    /// `NONE = -1` in either slot, nor an index past `ALTREF_FRAME = 7`.
    #[test]
    fn write_compound_ref_frames_rejects_out_of_range_slots() {
        for pair in [[0, 5], [-1, 5], [8, 5], [1, 0], [1, -1], [1, 8]] {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err = write_compound_ref_frames(
                &mut writer,
                &mut cdfs,
                pair,
                false,
                false,
                true,
                true,
                false,
                false,
                [-1, -1],
                [-1, -1],
            )
            .unwrap_err();
            assert!(
                matches!(err, Error::PartitionWalkOutOfRange),
                "pair {pair:?} rejected"
            );
        }
    }

    /// Same-group pairs outside the four §5.11.25 UNIDIR leaves are
    /// unreachable by the sub-tree and rejected.
    #[test]
    fn write_compound_ref_frames_rejects_unreachable_samedir_pairs() {
        for pair in [[2, 3], [2, 4], [3, 4], [5, 6], [6, 7], [1, 1], [7, 5]] {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err = write_compound_ref_frames(
                &mut writer,
                &mut cdfs,
                pair,
                false,
                false,
                true,
                true,
                false,
                false,
                [-1, -1],
                [-1, -1],
            )
            .unwrap_err();
            assert!(
                matches!(err, Error::PartitionWalkOutOfRange),
                "pair {pair:?} rejected"
            );
        }
    }

    /// Different-group pairs with the Group-2 (backward) frame in slot
    /// 0 cannot be expressed by the §5.11.25 BIDIR sub-tree.
    #[test]
    fn write_compound_ref_frames_rejects_reversed_bidir_pairs() {
        for pair in [[5, 1], [6, 2], [7, 4]] {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err = write_compound_ref_frames(
                &mut writer,
                &mut cdfs,
                pair,
                false,
                false,
                true,
                true,
                false,
                false,
                [-1, -1],
                [-1, -1],
            )
            .unwrap_err();
            assert!(
                matches!(err, Error::PartitionWalkOutOfRange),
                "pair {pair:?} rejected"
            );
        }
    }

    /// Sequential pairs through ONE encoder CDF context decode back
    /// through ONE decoder CDF context — the §8.2.6 per-symbol CDF
    /// adaptation stays in lockstep across the whole sequence.
    #[test]
    fn write_compound_ref_frames_sequential_adaptation_round_trip() {
        let sequence = [[1, 4], [5, 7], [3, 6], [1, 2], [4, 7], [1, 3], [2, 5]];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        for pair in sequence {
            write_compound_ref_frames(
                &mut writer,
                &mut enc_cdfs,
                pair,
                false,
                false,
                true,
                true,
                false,
                false,
                [-1, -1],
                [-1, -1],
            )
            .unwrap();
        }
        let bytes = writer.finish();

        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        for pair in sequence {
            let got = read_compound_ref_frames_mirror(&mut dec, &mut dec_cdfs, ORIGIN);
            assert_eq!(got, pair, "sequential pair {pair:?}");
        }
        assert_ref_tables_eq(&mut enc_cdfs, &mut dec_cdfs);
    }

    // -----------------------------------------------------------------
    // §5.11.25 arm 4 SINGLE_REFERENCE body — write_single_ref_frames.
    // -----------------------------------------------------------------

    /// Mirror of the decoder's §5.11.25 SINGLE_REFERENCE symbol reads:
    /// `single_ref_p1` then the p2/p6 (backward) or p3/p5/p4 (forward)
    /// cascade, each `S()` against the same §8.3.2 `TileSingleRefCdf`
    /// row selection the writer derives. Returns the decoded
    /// `RefFrame[ 0..2 ]` pair (`RefFrame[ 1 ] = NONE = -1`).
    fn read_single_ref_frames_mirror(
        dec: &mut SymbolDecoder<'_>,
        cdfs: &mut TileCdfContext,
        n: NeighbourOctet,
    ) -> [i32; 2] {
        let cnt = |ft: i32| {
            count_refs(
                ft,
                n.avail_u,
                n.above_ref_frame,
                n.avail_l,
                n.left_ref_frame,
            )
        };
        // `single_ref_p1` S() (§8.3.2 p.368: fwdCount vs bwdCount).
        let p1_ctx = ref_count_ctx(cnt(1) + cnt(2) + cnt(3) + cnt(4), cnt(5) + cnt(6) + cnt(7));
        let p1 = dec.read_symbol(cdfs.single_ref_cdf(p1_ctx, 0)).unwrap();
        let ref0 = if p1 != 0 {
            // `single_ref_p2` S() (ctx as in comp_bwdref).
            let p2_ctx = ref_count_ctx(cnt(5) + cnt(6), cnt(7));
            let p2 = dec.read_symbol(cdfs.single_ref_cdf(p2_ctx, 1)).unwrap();
            if p2 == 0 {
                // `single_ref_p6` S() (ctx as in comp_bwdref_p1).
                let p6_ctx = ref_count_ctx(cnt(5), cnt(6));
                let p6 = dec.read_symbol(cdfs.single_ref_cdf(p6_ctx, 5)).unwrap();
                if p6 != 0 {
                    6 // ALTREF2_FRAME
                } else {
                    5 // BWDREF_FRAME
                }
            } else {
                7 // ALTREF_FRAME
            }
        } else {
            // `single_ref_p3` S() (ctx as in comp_ref).
            let p3_ctx = ref_count_ctx(cnt(1) + cnt(2), cnt(3) + cnt(4));
            let p3 = dec.read_symbol(cdfs.single_ref_cdf(p3_ctx, 2)).unwrap();
            if p3 != 0 {
                // `single_ref_p5` S() (ctx as in comp_ref_p2).
                let p5_ctx = ref_count_ctx(cnt(3), cnt(4));
                let p5 = dec.read_symbol(cdfs.single_ref_cdf(p5_ctx, 4)).unwrap();
                if p5 != 0 {
                    4 // GOLDEN_FRAME
                } else {
                    3 // LAST3_FRAME
                }
            } else {
                // `single_ref_p4` S() (ctx as in comp_ref_p1).
                let p4_ctx = ref_count_ctx(cnt(1), cnt(2));
                let p4 = dec.read_symbol(cdfs.single_ref_cdf(p4_ctx, 3)).unwrap();
                if p4 != 0 {
                    2 // LAST2_FRAME
                } else {
                    1 // LAST_FRAME
                }
            }
        };
        [ref0, -1]
    }

    /// Compare every §8.3.2 `TileSingleRefCdf` row (all ctx × p slots)
    /// between two contexts.
    fn assert_single_ref_tables_eq(a: &mut TileCdfContext, b: &mut TileCdfContext) {
        for ctx in 0..crate::cdf::REF_CONTEXTS {
            for p in 0..6 {
                assert_eq!(
                    a.single_ref_cdf(ctx, p).to_vec(),
                    b.single_ref_cdf(ctx, p).to_vec(),
                    "single_ref ctx {ctx} p {p}"
                );
            }
        }
    }

    /// Encode one single reference with `write_single_ref_frames`,
    /// decode it back through the mirror reader with an independent
    /// CDF context, and assert the full `TileSingleRefCdf` table set
    /// matches afterwards (a row-selection mismatch on any symbol
    /// would leave the adapted-row sets diverged).
    fn round_trip_single_ref(ref0: i32, n: NeighbourOctet) {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_single_ref_frames(
            &mut writer,
            &mut enc_cdfs,
            [ref0, -1],
            n.avail_u,
            n.avail_l,
            n.above_ref_frame,
            n.left_ref_frame,
        )
        .unwrap();
        let bytes = writer.finish();

        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let got = read_single_ref_frames_mirror(&mut dec, &mut dec_cdfs, n);
        assert_eq!(got, [ref0, -1], "single ref {ref0} round-trip");
        assert_single_ref_tables_eq(&mut enc_cdfs, &mut dec_cdfs);
    }

    /// All seven §5.11.25 SINGLE_REFERENCE leaves (`LAST_FRAME = 1`
    /// ..= `ALTREF_FRAME = 7`) round-trip at the frame origin.
    #[test]
    fn write_single_ref_frames_all_refs_round_trip_at_origin() {
        for ref0 in 1..=7 {
            round_trip_single_ref(ref0, ORIGIN);
        }
    }

    /// Neighbour-engaged ctx selection: all seven leaves round-trip
    /// under the same two non-trivial §8.3.2 octets the COMPOUND-body
    /// tests use. The table-equality check inside
    /// `round_trip_single_ref` proves the writer derived the same
    /// per-symbol rows as the reader.
    #[test]
    fn write_single_ref_frames_round_trip_with_neighbour_ctx() {
        let octet_a = NeighbourOctet {
            avail_u: true,
            avail_l: true,
            above_single: false,
            left_single: true,
            above_intra: false,
            left_intra: false,
            above_ref_frame: [1, 5],
            left_ref_frame: [3, -1],
        };
        let octet_b = NeighbourOctet {
            avail_u: true,
            avail_l: true,
            above_single: false,
            left_single: true,
            above_intra: false,
            left_intra: true,
            above_ref_frame: [5, 7],
            left_ref_frame: [0, -1],
        };
        for n in [octet_a, octet_b] {
            for ref0 in 1..=7 {
                round_trip_single_ref(ref0, n);
            }
        }
    }

    /// `ALTREF_FRAME` is the two-symbol leaf: `single_ref_p1` +
    /// `single_ref_p2` only. The p3/p4/p5/p6 rows MUST stay pristine —
    /// extra symbols would adapt them. (At the origin every
    /// `ref_count_ctx` selection is 1.)
    #[test]
    fn write_single_ref_frames_altref_emits_two_symbols() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_single_ref_frames(
            &mut writer,
            &mut enc_cdfs,
            [7, -1],
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap();
        let mut pristine = TileCdfContext::new_from_defaults();
        for p in [0, 1] {
            assert_ne!(
                enc_cdfs.single_ref_cdf(1, p).to_vec(),
                pristine.single_ref_cdf(1, p).to_vec(),
                "single_ref p{} coded",
                p + 1
            );
        }
        for p in [2, 3, 4, 5] {
            assert_eq!(
                enc_cdfs.single_ref_cdf(1, p).to_vec(),
                pristine.single_ref_cdf(1, p).to_vec(),
                "single_ref p{} NOT coded",
                p + 1
            );
        }
    }

    /// `BWDREF_FRAME` takes the `single_ref_p2 = 0` arm and codes
    /// `single_ref_p6`; the forward-side p3/p4/p5 rows stay pristine.
    #[test]
    fn write_single_ref_frames_bwdref_arm_codes_p6() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_single_ref_frames(
            &mut writer,
            &mut enc_cdfs,
            [5, -1],
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap();
        let mut pristine = TileCdfContext::new_from_defaults();
        assert_ne!(
            enc_cdfs.single_ref_cdf(1, 5).to_vec(),
            pristine.single_ref_cdf(1, 5).to_vec(),
            "single_ref_p6 coded"
        );
        for p in [2, 3, 4] {
            assert_eq!(
                enc_cdfs.single_ref_cdf(1, p).to_vec(),
                pristine.single_ref_cdf(1, p).to_vec(),
                "single_ref p{} NOT coded",
                p + 1
            );
        }
    }

    /// `LAST_FRAME` takes the forward arm (`single_ref_p1 = 0` →
    /// `single_ref_p3` → `single_ref_p4`); the backward-side p2/p6
    /// rows and the p5 row stay pristine.
    #[test]
    fn write_single_ref_frames_forward_arm_skips_backward_rows() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_single_ref_frames(
            &mut writer,
            &mut enc_cdfs,
            [1, -1],
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap();
        let mut pristine = TileCdfContext::new_from_defaults();
        for p in [0, 2, 3] {
            assert_ne!(
                enc_cdfs.single_ref_cdf(1, p).to_vec(),
                pristine.single_ref_cdf(1, p).to_vec(),
                "single_ref p{} coded",
                p + 1
            );
        }
        for p in [1, 4, 5] {
            assert_eq!(
                enc_cdfs.single_ref_cdf(1, p).to_vec(),
                pristine.single_ref_cdf(1, p).to_vec(),
                "single_ref p{} NOT coded",
                p + 1
            );
        }
    }

    /// Slot 0 outside `LAST_FRAME..=ALTREF_FRAME` (`INTRA_FRAME = 0`,
    /// `NONE = -1`, past-`ALTREF` 8) and any slot-1 value other than
    /// `NONE = -1` are caller bugs.
    #[test]
    fn write_single_ref_frames_rejects_out_of_range() {
        for pair in [[0, -1], [-1, -1], [8, -1], [1, 0], [1, 5], [3, 7]] {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err = write_single_ref_frames(
                &mut writer,
                &mut cdfs,
                pair,
                false,
                false,
                [-1, -1],
                [-1, -1],
            )
            .unwrap_err();
            assert!(
                matches!(err, Error::PartitionWalkOutOfRange),
                "pair {pair:?} rejected"
            );
        }
    }

    /// Sequential single refs through ONE encoder CDF context decode
    /// back through ONE decoder CDF context — the §8.2.6 per-symbol
    /// CDF adaptation stays in lockstep across the whole sequence.
    #[test]
    fn write_single_ref_frames_sequential_adaptation_round_trip() {
        let sequence = [1, 7, 3, 5, 2, 6, 4, 1, 7];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        for ref0 in sequence {
            write_single_ref_frames(
                &mut writer,
                &mut enc_cdfs,
                [ref0, -1],
                false,
                false,
                [-1, -1],
                [-1, -1],
            )
            .unwrap();
        }
        let bytes = writer.finish();

        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        for ref0 in sequence {
            let got = read_single_ref_frames_mirror(&mut dec, &mut dec_cdfs, ORIGIN);
            assert_eq!(got, [ref0, -1], "sequential ref {ref0}");
        }
        assert_single_ref_tables_eq(&mut enc_cdfs, &mut dec_cdfs);
    }

    // -----------------------------------------------------------------
    // §5.11.25 full arm-4 dispatcher — write_ref_frames.
    // -----------------------------------------------------------------

    /// Mirror of the decoder's §5.11.25 arm-4 reads: the `comp_mode`
    /// S() (or its `reference_select && Min( bw4, bh4 ) >= 2`
    /// suppression to `SINGLE_REFERENCE`) followed by the matching
    /// reference-body mirror.
    fn read_ref_frames_arm4_mirror(
        dec: &mut SymbolDecoder<'_>,
        cdfs: &mut TileCdfContext,
        n: NeighbourOctet,
        mi_size: usize,
        reference_select: bool,
    ) -> [i32; 2] {
        let bw4 = NUM_4X4_BLOCKS_WIDE[mi_size];
        let bh4 = NUM_4X4_BLOCKS_HIGH[mi_size];
        let comp_mode = if reference_select && bw4.min(bh4) >= 2 {
            let ctx = comp_mode_ctx(
                n.avail_u,
                n.avail_l,
                n.above_single,
                n.left_single,
                n.above_intra,
                n.left_intra,
                n.above_ref_frame,
                n.left_ref_frame,
            );
            dec.read_symbol(cdfs.comp_mode_cdf(ctx)).unwrap()
        } else {
            0 // SINGLE_REFERENCE
        };
        if comp_mode == 1 {
            read_compound_ref_frames_mirror(dec, cdfs, n)
        } else {
            read_single_ref_frames_mirror(dec, cdfs, n)
        }
    }

    /// `write_ref_frames` with no arm-1/2/3 trigger and a matching /
    /// non-matching pair. Arm-1 (`skip_mode`): no symbols emitted, the
    /// matching pair is accepted and a mismatched pair rejected.
    #[test]
    fn write_ref_frames_arm1_verifies_skip_mode_pair() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_ref_frames(
            &mut writer,
            &mut cdfs,
            [1, 7],
            BLOCK_8X8,
            1,
            [1, 7],
            false,
            0,
            false,
            false,
            true,
            false,
            false,
            true,
            true,
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap();
        let bytes = writer.finish();
        assert!(bytes.len() <= 2, "arm 1 emits no payload bits");

        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_ref_frames(
            &mut writer,
            &mut cdfs,
            [1, 5],
            BLOCK_8X8,
            1,
            [1, 7],
            false,
            0,
            false,
            false,
            true,
            false,
            false,
            true,
            true,
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm-2 (`SEG_LVL_REF_FRAME`) and arm-3 (`SEG_LVL_SKIP` /
    /// `SEG_LVL_GLOBALMV`) forced pairs verify; mismatches reject.
    #[test]
    fn write_ref_frames_arm2_arm3_verify_forced_pairs() {
        // Arm 2: ( seg_ref_frame_data, NONE ).
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_ref_frames(
            &mut writer,
            &mut cdfs,
            [4, -1],
            BLOCK_8X8,
            0,
            [0, 0],
            true,
            4,
            false,
            false,
            true,
            false,
            false,
            true,
            true,
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap();

        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_ref_frames(
            &mut writer,
            &mut cdfs,
            [3, -1],
            BLOCK_8X8,
            0,
            [0, 0],
            true,
            4,
            false,
            false,
            true,
            false,
            false,
            true,
            true,
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));

        // Arm 3: ( LAST_FRAME, NONE ).
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_ref_frames(
            &mut writer,
            &mut cdfs,
            [1, -1],
            BLOCK_8X8,
            0,
            [0, 0],
            false,
            0,
            true,
            false,
            true,
            false,
            false,
            true,
            true,
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap();

        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_ref_frames(
            &mut writer,
            &mut cdfs,
            [2, -1],
            BLOCK_8X8,
            0,
            [0, 0],
            false,
            0,
            false,
            true,
            true,
            false,
            false,
            true,
            true,
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm-4 full round-trips: every SINGLE target (`( 1..=7, NONE )`)
    /// and every reachable COMPOUND pair survives the composed
    /// `comp_mode` + body cascade through the arm-4 mirror at the
    /// frame origin (`reference_select = true`, BLOCK_8X8 ⇒ explicit
    /// `comp_mode` bit on every block).
    #[test]
    fn write_ref_frames_arm4_round_trips_all_targets() {
        let singles = (1..=7).map(|r| [r, -1]);
        let compounds = UNIDIR_PAIRS.iter().chain(BIDIR_PAIRS.iter()).copied();
        for pair in singles.chain(compounds) {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_ref_frames(
                &mut writer,
                &mut enc_cdfs,
                pair,
                BLOCK_8X8,
                0,
                [0, 0],
                false,
                0,
                false,
                false,
                true,
                false,
                false,
                true,
                true,
                false,
                false,
                [-1, -1],
                [-1, -1],
            )
            .unwrap();
            let bytes = writer.finish();

            let mut dec_cdfs = TileCdfContext::new_from_defaults();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            let got = read_ref_frames_arm4_mirror(&mut dec, &mut dec_cdfs, ORIGIN, BLOCK_8X8, true);
            assert_eq!(got, pair, "arm-4 pair {pair:?} round-trip");
            assert_ref_tables_eq(&mut enc_cdfs, &mut dec_cdfs);
            assert_single_ref_tables_eq(&mut enc_cdfs, &mut dec_cdfs);
        }
    }

    /// Suppressed-`comp_mode` path (`reference_select = false`): a
    /// SINGLE target round-trips with no `comp_mode` bit, and a
    /// COMPOUND target is rejected (the spec forces
    /// `SINGLE_REFERENCE` with no bit there).
    #[test]
    fn write_ref_frames_suppressed_comp_mode_path() {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_ref_frames(
            &mut writer,
            &mut enc_cdfs,
            [3, -1],
            BLOCK_8X8,
            0,
            [0, 0],
            false,
            0,
            false,
            false,
            false,
            false,
            false,
            true,
            true,
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap();
        let bytes = writer.finish();
        let mut pristine = TileCdfContext::new_from_defaults();
        for ctx in 0..COMP_INTER_CONTEXTS {
            assert_eq!(
                enc_cdfs.comp_mode_cdf(ctx).to_vec(),
                pristine.comp_mode_cdf(ctx).to_vec(),
                "no comp_mode S() emitted"
            );
        }
        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let got = read_ref_frames_arm4_mirror(&mut dec, &mut dec_cdfs, ORIGIN, BLOCK_8X8, false);
        assert_eq!(got, [3, -1]);

        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_ref_frames(
            &mut writer,
            &mut cdfs,
            [1, 7],
            BLOCK_8X8,
            0,
            [0, 0],
            false,
            0,
            false,
            false,
            false,
            false,
            false,
            true,
            true,
            false,
            false,
            [-1, -1],
            [-1, -1],
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm-4 slot-1 values outside `{ NONE } ∪ LAST_FRAME..=ALTREF_FRAME`
    /// (`INTRA_FRAME = 0`, `-2`, `8`) are unreachable from §5.11.25
    /// and rejected before any symbol is emitted.
    #[test]
    fn write_ref_frames_rejects_invalid_slot1() {
        for slot1 in [0, -2, 8] {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err = write_ref_frames(
                &mut writer,
                &mut cdfs,
                [1, slot1],
                BLOCK_8X8,
                0,
                [0, 0],
                false,
                0,
                false,
                false,
                true,
                false,
                false,
                true,
                true,
                false,
                false,
                [-1, -1],
                [-1, -1],
            )
            .unwrap_err();
            assert!(
                matches!(err, Error::PartitionWalkOutOfRange),
                "slot1 {slot1} rejected"
            );
        }
    }

    // -----------------------------------------------------------------
    // §5.11.23 single-prediction inter mode — write_inter_single_mode
    // round-trips through a mirror of the decoder's new_mv / zero_mv /
    // ref_mv reads (av1-spec p.74). The full decoder path needs a
    // find_mv_stack (§7.10.2), not yet implemented; this mirror runs
    // exactly the three §5.11.23 S() reads in order against the same
    // CDF rows, recovering YMode the way the reader does.
    // -----------------------------------------------------------------

    /// Replays the §5.11.23 single-prediction `YMode` derivation from a
    /// decoder over `bytes`, consuming the same `new_mv` / `zero_mv` /
    /// `ref_mv` symbols the writer emitted. Returns the recovered
    /// `YMode`.
    fn decode_inter_single_mode_mirror(
        bytes: &[u8],
        cdfs: &mut TileCdfContext,
        disable_cdf_update: bool,
        new_mv_context: usize,
        zero_mv_context: usize,
        ref_mv_context: usize,
    ) -> u8 {
        let pad = if bytes.is_empty() {
            vec![0u8]
        } else {
            bytes.to_vec()
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), disable_cdf_update).unwrap();
        // §5.11.23: new_mv S(); new_mv == 0 ⇒ NEWMV.
        let new_mv_row = cdfs.new_mv_cdf(new_mv_context);
        let new_mv = dec.read_symbol(new_mv_row).unwrap();
        if new_mv == 0 {
            return MODE_NEWMV;
        }
        // zero_mv S(); zero_mv == 0 ⇒ GLOBALMV.
        let zero_mv_row = cdfs.zero_mv_cdf(zero_mv_context);
        let zero_mv = dec.read_symbol(zero_mv_row).unwrap();
        if zero_mv == 0 {
            return MODE_GLOBALMV;
        }
        // ref_mv S(); (ref_mv == 0) ? NEARESTMV : NEARMV.
        let ref_mv_row = cdfs.ref_mv_cdf(ref_mv_context);
        let ref_mv = dec.read_symbol(ref_mv_row).unwrap();
        if ref_mv == 0 {
            MODE_NEARESTMV
        } else {
            MODE_NEARMV
        }
    }

    /// Each single-prediction `YMode` round-trips through the decoder
    /// mirror at `NewMvContext = ZeroMvContext = RefMvContext = 0`, with
    /// §8.3 CDF adaptation engaged on both sides (so the encode-side and
    /// decode-side rows step identically).
    #[test]
    fn write_inter_single_mode_all_modes_round_trip_at_origin() {
        for y_mode in [MODE_NEWMV, MODE_GLOBALMV, MODE_NEARESTMV, MODE_NEARMV] {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_inter_single_mode(&mut writer, &mut enc_cdfs, y_mode, 0, 0, 0).unwrap();
            let bytes = writer.finish();

            let mut dec_cdfs = TileCdfContext::new_from_defaults();
            let recovered = decode_inter_single_mode_mirror(&bytes, &mut dec_cdfs, false, 0, 0, 0);
            assert_eq!(recovered, y_mode, "YMode {y_mode} round-trips at origin");
            // The encode-side and decode-side CDFs must have adapted
            // identically — assert the consulted rows match.
            assert_eq!(enc_cdfs.new_mv_cdf(0), dec_cdfs.new_mv_cdf(0));
            if y_mode != MODE_NEWMV {
                assert_eq!(enc_cdfs.zero_mv_cdf(0), dec_cdfs.zero_mv_cdf(0));
            }
            if matches!(y_mode, MODE_NEARESTMV | MODE_NEARMV) {
                assert_eq!(enc_cdfs.ref_mv_cdf(0), dec_cdfs.ref_mv_cdf(0));
            }
        }
    }

    /// Round-trip every mode under non-zero, distinct §8.3.2 contexts
    /// (`NewMvContext = 5`, `ZeroMvContext = 1`, `RefMvContext = 4`) to
    /// prove the writer selects the same CDF rows the reader does.
    #[test]
    fn write_inter_single_mode_all_modes_round_trip_nonzero_ctx() {
        for y_mode in [MODE_NEWMV, MODE_GLOBALMV, MODE_NEARESTMV, MODE_NEARMV] {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_inter_single_mode(&mut writer, &mut enc_cdfs, y_mode, 5, 1, 4).unwrap();
            let bytes = writer.finish();

            let mut dec_cdfs = TileCdfContext::new_from_defaults();
            let recovered = decode_inter_single_mode_mirror(&bytes, &mut dec_cdfs, false, 5, 1, 4);
            assert_eq!(
                recovered, y_mode,
                "YMode {y_mode} round-trips at ctx (5,1,4)"
            );
        }
    }

    /// Symbol-count leaf checks: `NEWMV` emits exactly one symbol,
    /// `GLOBALMV` two, `NEARESTMV` / `NEARMV` three. Verified by
    /// comparing each leaf's emitted byte stream against an independent
    /// re-encode of only the symbols that leaf should produce (over a
    /// pristine CDF copy), proving no extra symbol is written.
    #[test]
    fn write_inter_single_mode_emits_expected_symbol_counts() {
        // NEWMV: only new_mv = 0.
        {
            let mut a = TileCdfContext::new_from_defaults();
            let mut wa = SymbolWriter::new(false);
            write_inter_single_mode(&mut wa, &mut a, MODE_NEWMV, 0, 0, 0).unwrap();
            let got = wa.finish();

            let mut b = TileCdfContext::new_from_defaults();
            let mut wb = SymbolWriter::new(false);
            let row = b.new_mv_cdf(0);
            wb.write_symbol(0, row).unwrap();
            let want = wb.finish();
            assert_eq!(got, want, "NEWMV emits exactly one (new_mv = 0) symbol");
        }
        // GLOBALMV: new_mv = 1, zero_mv = 0.
        {
            let mut a = TileCdfContext::new_from_defaults();
            let mut wa = SymbolWriter::new(false);
            write_inter_single_mode(&mut wa, &mut a, MODE_GLOBALMV, 0, 0, 0).unwrap();
            let got = wa.finish();

            let mut b = TileCdfContext::new_from_defaults();
            let mut wb = SymbolWriter::new(false);
            let row = b.new_mv_cdf(0);
            wb.write_symbol(1, row).unwrap();
            let row = b.zero_mv_cdf(0);
            wb.write_symbol(0, row).unwrap();
            let want = wb.finish();
            assert_eq!(got, want, "GLOBALMV emits new_mv = 1, zero_mv = 0");
        }
        // NEARMV: new_mv = 1, zero_mv = 1, ref_mv = 1.
        {
            let mut a = TileCdfContext::new_from_defaults();
            let mut wa = SymbolWriter::new(false);
            write_inter_single_mode(&mut wa, &mut a, MODE_NEARMV, 0, 0, 0).unwrap();
            let got = wa.finish();

            let mut b = TileCdfContext::new_from_defaults();
            let mut wb = SymbolWriter::new(false);
            let row = b.new_mv_cdf(0);
            wb.write_symbol(1, row).unwrap();
            let row = b.zero_mv_cdf(0);
            wb.write_symbol(1, row).unwrap();
            let row = b.ref_mv_cdf(0);
            wb.write_symbol(1, row).unwrap();
            let want = wb.finish();
            assert_eq!(
                got, want,
                "NEARMV emits new_mv = 1, zero_mv = 1, ref_mv = 1"
            );
        }
    }

    /// A `YMode` outside the four single-prediction modes (e.g.
    /// `DC_PRED = 0`, `MODE_NEAREST_NEARESTMV = 18`) is a caller bug.
    #[test]
    fn write_inter_single_mode_rejects_invalid_y_mode() {
        for y_mode in [0u8, 13, MODE_NEAREST_NEARESTMV, 25, 255] {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err = write_inter_single_mode(&mut writer, &mut cdfs, y_mode, 0, 0, 0).unwrap_err();
            assert!(
                matches!(err, Error::PartitionWalkOutOfRange),
                "y_mode {y_mode} rejected"
            );
        }
    }

    /// An out-of-range `NewMvContext` (always consulted) is rejected.
    #[test]
    fn write_inter_single_mode_rejects_bad_new_mv_ctx() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err =
            write_inter_single_mode(&mut writer, &mut cdfs, MODE_NEWMV, NEW_MV_CONTEXTS, 0, 0)
                .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// `ZeroMvContext` is only consulted past the `new_mv == 1` branch:
    /// a bad value is tolerated for `NEWMV` (never reached) but rejected
    /// for `GLOBALMV` (consulted).
    #[test]
    fn write_inter_single_mode_zero_mv_ctx_only_validated_when_consulted() {
        // NEWMV never reads zero_mv ⇒ bad zero_mv ctx is fine.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_inter_single_mode(&mut writer, &mut cdfs, MODE_NEWMV, 0, ZERO_MV_CONTEXTS, 0)
            .unwrap();

        // GLOBALMV reads zero_mv ⇒ bad zero_mv ctx is a caller bug.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_inter_single_mode(
            &mut writer,
            &mut cdfs,
            MODE_GLOBALMV,
            0,
            ZERO_MV_CONTEXTS,
            0,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// `RefMvContext` is only consulted on the NEARESTMV / NEARMV leaf:
    /// a bad value is tolerated for `GLOBALMV` (never reached) but
    /// rejected for `NEARMV` (consulted).
    #[test]
    fn write_inter_single_mode_ref_mv_ctx_only_validated_when_consulted() {
        // GLOBALMV never reads ref_mv ⇒ bad ref_mv ctx is fine.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_inter_single_mode(&mut writer, &mut cdfs, MODE_GLOBALMV, 0, 0, REF_MV_CONTEXTS)
            .unwrap();

        // NEARMV reads ref_mv ⇒ bad ref_mv ctx is a caller bug.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err =
            write_inter_single_mode(&mut writer, &mut cdfs, MODE_NEARMV, 0, 0, REF_MV_CONTEXTS)
                .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// A sequential run of distinct modes round-trips with §8.3 CDF
    /// adaptation engaged across blocks: each write mutates the shared
    /// encode-side CDFs, each mirror read mutates the decode-side CDFs,
    /// and the two stay in lockstep over the whole sequence.
    #[test]
    fn write_inter_single_mode_sequential_cdf_adaptation_lockstep() {
        let modes = [
            MODE_NEWMV,
            MODE_NEARMV,
            MODE_GLOBALMV,
            MODE_NEARESTMV,
            MODE_NEWMV,
            MODE_NEARMV,
            MODE_NEARESTMV,
            MODE_GLOBALMV,
        ];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        for &y_mode in &modes {
            let mut writer = SymbolWriter::new(false);
            write_inter_single_mode(&mut writer, &mut enc_cdfs, y_mode, 2, 1, 3).unwrap();
            let bytes = writer.finish();
            let recovered = decode_inter_single_mode_mirror(&bytes, &mut dec_cdfs, false, 2, 1, 3);
            assert_eq!(recovered, y_mode);
        }
        // After the run the consulted rows must be identical on both
        // sides (proves the per-symbol adaptation matched throughout).
        assert_eq!(enc_cdfs.new_mv_cdf(2), dec_cdfs.new_mv_cdf(2));
        assert_eq!(enc_cdfs.zero_mv_cdf(1), dec_cdfs.zero_mv_cdf(1));
        assert_eq!(enc_cdfs.ref_mv_cdf(3), dec_cdfs.ref_mv_cdf(3));
    }

    // -----------------------------------------------------------------
    // §5.11.23 compound-prediction inter mode — write_compound_mode
    // round-trips through a mirror of the decoder's single
    // `compound_mode` S() read (av1-spec p.74, arm 3). The full decoder
    // path needs a find_mv_stack (§7.10.2), not yet implemented; this
    // mirror runs exactly the one §5.11.23 S() read against the same
    // CDF row, recovering YMode as `NEAREST_NEARESTMV + compound_mode`.
    // -----------------------------------------------------------------

    /// Replays the §5.11.23 compound-prediction `YMode` derivation from
    /// a decoder over `bytes`, consuming the same `compound_mode`
    /// symbol the writer emitted. Returns the recovered `YMode`.
    fn decode_compound_mode_mirror(
        bytes: &[u8],
        cdfs: &mut TileCdfContext,
        disable_cdf_update: bool,
        compound_mode_context: usize,
    ) -> u8 {
        let pad = if bytes.is_empty() {
            vec![0u8]
        } else {
            bytes.to_vec()
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), disable_cdf_update).unwrap();
        // §5.11.23 line 8: YMode = NEAREST_NEARESTMV + compound_mode.
        let row = cdfs.compound_mode_cdf(compound_mode_context);
        let cm = dec.read_symbol(row).unwrap() as u8;
        MODE_NEAREST_NEARESTMV + cm
    }

    /// Every compound `YMode` (`NEAREST_NEARESTMV = 18 ..= NEW_NEWMV =
    /// 25`) round-trips through the decoder mirror at
    /// `compound_mode_context = 0`, with §8.3 CDF adaptation engaged on
    /// both sides (so the encode-side and decode-side rows step
    /// identically).
    #[test]
    fn write_compound_mode_all_modes_round_trip_at_origin() {
        for y_mode in MODE_NEAREST_NEARESTMV..=MODE_NEW_NEWMV {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_compound_mode(&mut writer, &mut enc_cdfs, y_mode, 0).unwrap();
            let bytes = writer.finish();

            let mut dec_cdfs = TileCdfContext::new_from_defaults();
            let recovered = decode_compound_mode_mirror(&bytes, &mut dec_cdfs, false, 0);
            assert_eq!(recovered, y_mode, "YMode {y_mode} round-trips at origin");
            // The encode-side and decode-side CDFs must have adapted
            // identically — assert the consulted row matches.
            assert_eq!(enc_cdfs.compound_mode_cdf(0), dec_cdfs.compound_mode_cdf(0));
        }
    }

    /// Round-trip every compound mode under each non-zero §8.3.2
    /// context `1..COMPOUND_MODE_CONTEXTS`, proving the writer selects
    /// the same `TileCompoundModeCdf` row the reader does.
    #[test]
    fn write_compound_mode_all_modes_round_trip_each_ctx() {
        for ctx in 1..COMPOUND_MODE_CONTEXTS {
            for y_mode in MODE_NEAREST_NEARESTMV..=MODE_NEW_NEWMV {
                let mut enc_cdfs = TileCdfContext::new_from_defaults();
                let mut writer = SymbolWriter::new(false);
                write_compound_mode(&mut writer, &mut enc_cdfs, y_mode, ctx).unwrap();
                let bytes = writer.finish();

                let mut dec_cdfs = TileCdfContext::new_from_defaults();
                let recovered = decode_compound_mode_mirror(&bytes, &mut dec_cdfs, false, ctx);
                assert_eq!(recovered, y_mode, "YMode {y_mode} round-trips at ctx {ctx}");
            }
        }
    }

    /// Symbol-count leaf check: each compound mode emits exactly one
    /// `compound_mode` symbol equal to `YMode - NEAREST_NEARESTMV`.
    /// Verified by comparing the emitted byte stream against an
    /// independent re-encode of only that single symbol over a pristine
    /// CDF copy, proving no extra symbol is written.
    #[test]
    fn write_compound_mode_emits_exactly_one_symbol() {
        for y_mode in MODE_NEAREST_NEARESTMV..=MODE_NEW_NEWMV {
            let mut a = TileCdfContext::new_from_defaults();
            let mut wa = SymbolWriter::new(false);
            write_compound_mode(&mut wa, &mut a, y_mode, 0).unwrap();
            let got = wa.finish();

            let mut b = TileCdfContext::new_from_defaults();
            let mut wb = SymbolWriter::new(false);
            let cm = (y_mode - MODE_NEAREST_NEARESTMV) as u32;
            let row = b.compound_mode_cdf(0);
            wb.write_symbol(cm, row).unwrap();
            let want = wb.finish();
            assert_eq!(
                got, want,
                "YMode {y_mode} emits exactly one compound_mode symbol"
            );
        }
    }

    /// A `YMode` outside the eight compound modes (single-prediction
    /// `NEWMV = 17`, intra `DC_PRED = 0`, just past `NEW_NEWMV` at 26,
    /// and `255`) is a caller bug.
    #[test]
    fn write_compound_mode_rejects_invalid_y_mode() {
        for y_mode in [0u8, 13, MODE_NEWMV, MODE_NEW_NEWMV + 1, 255] {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err = write_compound_mode(&mut writer, &mut cdfs, y_mode, 0).unwrap_err();
            assert!(
                matches!(err, Error::PartitionWalkOutOfRange),
                "y_mode {y_mode} rejected"
            );
        }
    }

    /// The `compound_mode` symbol is always emitted, so the ctx is
    /// always validated: an out-of-range context is a caller bug even
    /// on the first mode.
    #[test]
    fn write_compound_mode_rejects_bad_ctx() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_compound_mode(
            &mut writer,
            &mut cdfs,
            MODE_NEAREST_NEARESTMV,
            COMPOUND_MODE_CONTEXTS,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// A sequential run of distinct compound modes round-trips with
    /// §8.3 CDF adaptation engaged across blocks: each write mutates the
    /// shared encode-side CDFs, each mirror read mutates the decode-side
    /// CDFs, and the two stay in lockstep over the whole sequence.
    #[test]
    fn write_compound_mode_sequential_cdf_adaptation_lockstep() {
        let modes = [
            MODE_NEAREST_NEARESTMV,
            MODE_NEW_NEWMV,
            MODE_NEAR_NEARMV,
            MODE_GLOBAL_GLOBALMV,
            MODE_NEAREST_NEWMV,
            MODE_NEW_NEARMV,
            MODE_NEW_NEARESTMV,
            MODE_NEAR_NEWMV,
        ];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        for &y_mode in &modes {
            let mut writer = SymbolWriter::new(false);
            write_compound_mode(&mut writer, &mut enc_cdfs, y_mode, 4).unwrap();
            let bytes = writer.finish();
            let recovered = decode_compound_mode_mirror(&bytes, &mut dec_cdfs, false, 4);
            assert_eq!(recovered, y_mode);
        }
        // After the run the consulted row must be identical on both
        // sides (proves the per-symbol adaptation matched throughout).
        assert_eq!(enc_cdfs.compound_mode_cdf(4), dec_cdfs.compound_mode_cdf(4));
    }

    // -----------------------------------------------------------------
    // §5.11.23 drl_mode — write_drl_mode round-trips through a decoder
    // mirror of the exact §5.11.23 RefMvIdx loop.
    // -----------------------------------------------------------------

    /// Decoder mirror of the §5.11.23 `drl_mode` `RefMvIdx` loop for the
    /// two coding arms (NEWMV / NEW_NEWMV start at idx 0, `has_nearmv( )`
    /// start at idx 1). Reads the `drl_mode` symbols the writer emitted
    /// and reconstructs `RefMvIdx` exactly as the spec body does. Modes
    /// outside the two arms code no symbols and return `RefMvIdx = 0`.
    fn decode_drl_mode_mirror(
        bytes: &[u8],
        cdfs: &mut TileCdfContext,
        disable_cdf_update: bool,
        y_mode: u8,
        num_mv_found: u32,
        drl_ctx_stack: &[u32],
    ) -> u32 {
        let pad = if bytes.is_empty() {
            vec![0u8]
        } else {
            bytes.to_vec()
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), disable_cdf_update).unwrap();
        let mut ref_mv_idx: u32 = 0;
        let start = if y_mode == MODE_NEWMV || y_mode == MODE_NEW_NEWMV {
            0
        } else if matches!(
            y_mode,
            MODE_NEARMV | MODE_NEAR_NEARMV | MODE_NEAR_NEWMV | MODE_NEW_NEARMV
        ) {
            ref_mv_idx = 1;
            1
        } else {
            return 0;
        };
        let mut idx = start;
        while idx < start + 2 {
            if num_mv_found > idx + 1 {
                let row = cdfs.drl_mode_cdf(drl_ctx_stack[idx as usize] as usize);
                let drl = dec.read_symbol(row).unwrap();
                if drl == 0 {
                    ref_mv_idx = idx;
                    break;
                }
                ref_mv_idx = idx + 1;
            }
            idx += 1;
        }
        ref_mv_idx
    }

    /// The NEWMV / NEW_NEWMV arm round-trips every reachable `RefMvIdx`
    /// for every stack depth `NumMvFound = 1..=3`, with §8.3 CDF
    /// adaptation engaged on both sides. The reachable index set is
    /// exactly `0 ..= min(2, NumMvFound - 1)`.
    #[test]
    fn write_drl_mode_newmv_arm_round_trips_all_depths() {
        let ctx_stack = [2u32, 0, 1, 0, 0, 0, 0, 0];
        for y_mode in [MODE_NEWMV, MODE_NEW_NEWMV] {
            for num_mv_found in 1u32..=3 {
                let max_idx = (num_mv_found - 1).min(2);
                for ref_mv_idx in 0..=max_idx {
                    let mut enc_cdfs = TileCdfContext::new_from_defaults();
                    let mut writer = SymbolWriter::new(false);
                    write_drl_mode(
                        &mut writer,
                        &mut enc_cdfs,
                        y_mode,
                        ref_mv_idx,
                        num_mv_found,
                        &ctx_stack,
                    )
                    .unwrap();
                    let bytes = writer.finish();

                    let mut dec_cdfs = TileCdfContext::new_from_defaults();
                    let recovered = decode_drl_mode_mirror(
                        &bytes,
                        &mut dec_cdfs,
                        false,
                        y_mode,
                        num_mv_found,
                        &ctx_stack,
                    );
                    assert_eq!(
                        recovered, ref_mv_idx,
                        "NEWMV-arm YMode {y_mode} RefMvIdx {ref_mv_idx} @ NumMvFound {num_mv_found}"
                    );
                    // Every consulted DRL CDF row stepped identically.
                    for c in 0..DRL_MODE_CONTEXTS {
                        assert_eq!(enc_cdfs.drl_mode_cdf(c), dec_cdfs.drl_mode_cdf(c));
                    }
                }
            }
        }
    }

    /// The `has_nearmv( )` arm round-trips every reachable `RefMvIdx`
    /// for every stack depth `NumMvFound = 1..=4`. The arm seeds
    /// `RefMvIdx = 1` and iterates `idx ∈ {1, 2}`, so the reachable set
    /// is `1 ..= max(1, min(3, NumMvFound - 1))`.
    #[test]
    fn write_drl_mode_nearmv_arm_round_trips_all_depths() {
        let ctx_stack = [0u32, 1, 2, 0, 0, 0, 0, 0];
        for y_mode in [
            MODE_NEARMV,
            MODE_NEAR_NEARMV,
            MODE_NEAR_NEWMV,
            MODE_NEW_NEARMV,
        ] {
            for num_mv_found in 1u32..=4 {
                let max_idx = (num_mv_found - 1).clamp(1, 3);
                for ref_mv_idx in 1..=max_idx {
                    let mut enc_cdfs = TileCdfContext::new_from_defaults();
                    let mut writer = SymbolWriter::new(false);
                    write_drl_mode(
                        &mut writer,
                        &mut enc_cdfs,
                        y_mode,
                        ref_mv_idx,
                        num_mv_found,
                        &ctx_stack,
                    )
                    .unwrap();
                    let bytes = writer.finish();

                    let mut dec_cdfs = TileCdfContext::new_from_defaults();
                    let recovered = decode_drl_mode_mirror(
                        &bytes,
                        &mut dec_cdfs,
                        false,
                        y_mode,
                        num_mv_found,
                        &ctx_stack,
                    );
                    assert_eq!(
                        recovered, ref_mv_idx,
                        "NEAR-arm YMode {y_mode} RefMvIdx {ref_mv_idx} @ NumMvFound {num_mv_found}"
                    );
                }
            }
        }
    }

    /// Modes outside the two coding arms (GLOBALMV, NEARESTMV, and the
    /// compound NEAREST_NEARESTMV / GLOBAL_GLOBALMV) code **no**
    /// `drl_mode` symbols: the writer emits an empty (or terminator-only)
    /// stream and the decoder mirror reconstructs `RefMvIdx = 0` without
    /// touching the buffer.
    #[test]
    fn write_drl_mode_non_coding_modes_emit_no_symbols() {
        let ctx_stack = [0u32; MAX_REF_MV_STACK_SIZE];
        for y_mode in [
            MODE_GLOBALMV,
            MODE_NEARESTMV,
            MODE_NEAREST_NEARESTMV,
            MODE_GLOBAL_GLOBALMV,
        ] {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_drl_mode(&mut writer, &mut enc_cdfs, y_mode, 0, 4, &ctx_stack).unwrap();
            let bytes = writer.finish();
            // No DRL CDF row may have adapted (no symbol coded).
            for c in 0..DRL_MODE_CONTEXTS {
                assert_eq!(
                    enc_cdfs.drl_mode_cdf(c),
                    TileCdfContext::new_from_defaults().drl_mode_cdf(c),
                    "non-coding mode {y_mode} left DRL ctx {c} pristine"
                );
            }
            // And the mirror reconstructs RefMvIdx = 0.
            let mut dec_cdfs = TileCdfContext::new_from_defaults();
            let recovered =
                decode_drl_mode_mirror(&bytes, &mut dec_cdfs, false, y_mode, 4, &ctx_stack);
            assert_eq!(recovered, 0);
        }
    }

    /// A shallow stack (`NumMvFound <= 1`) makes slot 0 unreachable, so
    /// even a NEWMV block codes no `drl_mode` symbol and `RefMvIdx`
    /// stays at the arm seed (`0`).
    #[test]
    fn write_drl_mode_shallow_stack_codes_nothing() {
        let ctx_stack = [0u32; MAX_REF_MV_STACK_SIZE];
        for num_mv_found in 0u32..=1 {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_drl_mode(
                &mut writer,
                &mut enc_cdfs,
                MODE_NEWMV,
                0,
                num_mv_found,
                &ctx_stack,
            )
            .unwrap();
            for c in 0..DRL_MODE_CONTEXTS {
                assert_eq!(
                    enc_cdfs.drl_mode_cdf(c),
                    TileCdfContext::new_from_defaults().drl_mode_cdf(c)
                );
            }
        }
    }

    /// Distinct per-slot DRL contexts are honoured: with
    /// `DrlCtxStack = [0, 1, …]` a NEWMV block selecting `RefMvIdx = 1`
    /// over `NumMvFound = 3` codes `drl_mode = 1` at slot-0 ctx 0 and
    /// `drl_mode = 0` at slot-1 ctx 1. Verified against an independent
    /// re-encode of exactly those two symbols.
    #[test]
    fn write_drl_mode_uses_per_slot_context() {
        let ctx_stack = [0u32, 1, 2, 0, 0, 0, 0, 0];
        let mut a = TileCdfContext::new_from_defaults();
        let mut wa = SymbolWriter::new(false);
        write_drl_mode(&mut wa, &mut a, MODE_NEWMV, 1, 3, &ctx_stack).unwrap();
        let got = wa.finish();

        let mut b = TileCdfContext::new_from_defaults();
        let mut wb = SymbolWriter::new(false);
        // slot 0: drl_mode = 1 over ctx 0.
        let row0 = b.drl_mode_cdf(0);
        wb.write_symbol(1, row0).unwrap();
        // slot 1: drl_mode = 0 over ctx 1.
        let row1 = b.drl_mode_cdf(1);
        wb.write_symbol(0, row1).unwrap();
        let want = wb.finish();
        assert_eq!(got, want, "per-slot DRL contexts honoured");
    }

    /// A `RefMvIdx` unreachable at the given stack depth (NEWMV arm,
    /// `RefMvIdx = 2` but `NumMvFound = 2` so slot 1 is unreachable) is
    /// a caller bug — the reader could never decode it.
    #[test]
    fn write_drl_mode_rejects_unreachable_index() {
        let ctx_stack = [0u32; MAX_REF_MV_STACK_SIZE];
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_drl_mode(&mut writer, &mut cdfs, MODE_NEWMV, 2, 2, &ctx_stack).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Out-of-window `RefMvIdx` for each arm is a caller bug: `> 2` for
    /// the NEWMV arm, `0` or `> 3` for the `has_nearmv( )` arm.
    #[test]
    fn write_drl_mode_rejects_out_of_window_index() {
        let ctx_stack = [0u32; MAX_REF_MV_STACK_SIZE];
        // NEWMV arm: 3 is past the {0,1,2} window.
        {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err =
                write_drl_mode(&mut writer, &mut cdfs, MODE_NEWMV, 3, 5, &ctx_stack).unwrap_err();
            assert!(matches!(err, Error::PartitionWalkOutOfRange));
        }
        // has_nearmv arm: 0 is below the {1,2,3} window.
        {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err =
                write_drl_mode(&mut writer, &mut cdfs, MODE_NEARMV, 0, 5, &ctx_stack).unwrap_err();
            assert!(matches!(err, Error::PartitionWalkOutOfRange));
        }
        // has_nearmv arm: 4 is past the {1,2,3} window.
        {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err =
                write_drl_mode(&mut writer, &mut cdfs, MODE_NEARMV, 4, 5, &ctx_stack).unwrap_err();
            assert!(matches!(err, Error::PartitionWalkOutOfRange));
        }
    }

    /// Range guards: `ref_mv_idx >= MAX_REF_MV_STACK_SIZE`,
    /// `num_mv_found > MAX_REF_MV_STACK_SIZE`, and a bad per-slot DRL
    /// context (`>= DRL_MODE_CONTEXTS`) are each caller bugs.
    #[test]
    fn write_drl_mode_range_guards() {
        // ref_mv_idx out of stack range.
        {
            let ctx_stack = [0u32; MAX_REF_MV_STACK_SIZE];
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err = write_drl_mode(
                &mut writer,
                &mut cdfs,
                MODE_NEWMV,
                MAX_REF_MV_STACK_SIZE as u32,
                3,
                &ctx_stack,
            )
            .unwrap_err();
            assert!(matches!(err, Error::PartitionWalkOutOfRange));
        }
        // num_mv_found past the stack cap.
        {
            let ctx_stack = [0u32; MAX_REF_MV_STACK_SIZE];
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err = write_drl_mode(
                &mut writer,
                &mut cdfs,
                MODE_NEWMV,
                0,
                MAX_REF_MV_STACK_SIZE as u32 + 1,
                &ctx_stack,
            )
            .unwrap_err();
            assert!(matches!(err, Error::PartitionWalkOutOfRange));
        }
        // Bad per-slot DRL context on a coded slot.
        {
            let mut ctx_stack = [0u32; MAX_REF_MV_STACK_SIZE];
            ctx_stack[0] = DRL_MODE_CONTEXTS as u32;
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err =
                write_drl_mode(&mut writer, &mut cdfs, MODE_NEWMV, 0, 3, &ctx_stack).unwrap_err();
            assert!(matches!(err, Error::PartitionWalkOutOfRange));
        }
    }

    /// A sequential run of NEWMV-arm DRL writes round-trips with §8.3
    /// CDF adaptation engaged across blocks, the encode-side and
    /// decode-side DRL CDFs staying in lockstep over the whole run.
    #[test]
    fn write_drl_mode_sequential_cdf_adaptation_lockstep() {
        let ctx_stack = [0u32, 1, 2, 0, 0, 0, 0, 0];
        let cases = [
            (MODE_NEWMV, 0u32, 3u32),
            (MODE_NEW_NEWMV, 1, 3),
            (MODE_NEWMV, 2, 3),
            (MODE_NEW_NEWMV, 0, 2),
            (MODE_NEWMV, 1, 2),
        ];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        for &(y_mode, ref_mv_idx, num_mv_found) in &cases {
            let mut writer = SymbolWriter::new(false);
            write_drl_mode(
                &mut writer,
                &mut enc_cdfs,
                y_mode,
                ref_mv_idx,
                num_mv_found,
                &ctx_stack,
            )
            .unwrap();
            let bytes = writer.finish();
            let recovered = decode_drl_mode_mirror(
                &bytes,
                &mut dec_cdfs,
                false,
                y_mode,
                num_mv_found,
                &ctx_stack,
            );
            assert_eq!(recovered, ref_mv_idx);
        }
        for c in 0..DRL_MODE_CONTEXTS {
            assert_eq!(enc_cdfs.drl_mode_cdf(c), dec_cdfs.drl_mode_cdf(c));
        }
    }

    // -----------------------------------------------------------------
    // §5.11.31 / §5.11.32 — write_read_mv / write_read_mv_component.
    // -----------------------------------------------------------------

    /// Decode mirror of §5.11.32 `read_mv_component( comp )` — the
    /// literal reader pseudocode, used to round-trip the writer. Returns
    /// the signed difference the reader would reconstruct.
    fn decode_read_mv_component_mirror(
        dec: &mut SymbolDecoder,
        cdfs: &mut TileCdfContext,
        mv_ctx: usize,
        comp: usize,
        force_integer_mv: bool,
        allow_high_precision_mv: bool,
    ) -> i32 {
        let mv_sign = dec.read_symbol(cdfs.mv_sign_cdf(mv_ctx, comp)).unwrap();
        let mv_class = dec.read_symbol(cdfs.mv_class_cdf(mv_ctx, comp)).unwrap();
        let mag: u32 = if mv_class == MV_CLASS_0 as u32 {
            let mv_class0_bit = dec
                .read_symbol(cdfs.mv_class0_bit_cdf(mv_ctx, comp))
                .unwrap();
            let mv_class0_fr = if force_integer_mv {
                3
            } else {
                dec.read_symbol(cdfs.mv_class0_fr_cdf(mv_ctx, comp, mv_class0_bit as usize))
                    .unwrap()
            };
            let mv_class0_hp = if allow_high_precision_mv {
                dec.read_symbol(cdfs.mv_class0_hp_cdf(mv_ctx, comp))
                    .unwrap()
            } else {
                1
            };
            ((mv_class0_bit << 3) | (mv_class0_fr << 1) | mv_class0_hp) + 1
        } else {
            let mut d: u32 = 0;
            for i in 0..(mv_class as usize) {
                let mv_bit = dec.read_symbol(cdfs.mv_bit_cdf(mv_ctx, comp, i)).unwrap();
                d |= mv_bit << i;
            }
            let mut mag = (CLASS0_SIZE as u32) << (mv_class + 2);
            let mv_fr = if force_integer_mv {
                3
            } else {
                dec.read_symbol(cdfs.mv_fr_cdf(mv_ctx, comp)).unwrap()
            };
            let mv_hp = if allow_high_precision_mv {
                dec.read_symbol(cdfs.mv_hp_cdf(mv_ctx, comp)).unwrap()
            } else {
                1
            };
            mag += ((d << 3) | (mv_fr << 1) | mv_hp) + 1;
            mag
        };
        if mv_sign != 0 {
            -(mag as i32)
        } else {
            mag as i32
        }
    }

    /// Decode mirror of §5.11.31 `read_mv( ref )` — reconstructs the
    /// `Mv[ ref ]` the reader would produce from `pred_mv` and the
    /// emitted symbols.
    fn decode_read_mv_mirror(
        bytes: &[u8],
        cdfs: &mut TileCdfContext,
        pred_mv: [i32; 2],
        use_intrabc: bool,
        force_integer_mv: bool,
        allow_high_precision_mv: bool,
    ) -> [i32; 2] {
        let mv_ctx = if use_intrabc { MV_INTRABC_CONTEXT } else { 0 };
        let mut dec = SymbolDecoder::init_symbol(bytes, bytes.len(), false).unwrap();
        let mv_joint = dec.read_symbol(cdfs.mv_joint_cdf(mv_ctx)).unwrap() as u8;
        let mut diff = [0i32; 2];
        if mv_joint == MV_JOINT_HZVNZ || mv_joint == MV_JOINT_HNZVNZ {
            diff[0] = decode_read_mv_component_mirror(
                &mut dec,
                cdfs,
                mv_ctx,
                0,
                force_integer_mv,
                allow_high_precision_mv,
            );
        }
        if mv_joint == MV_JOINT_HNZVZ || mv_joint == MV_JOINT_HNZVNZ {
            diff[1] = decode_read_mv_component_mirror(
                &mut dec,
                cdfs,
                mv_ctx,
                1,
                force_integer_mv,
                allow_high_precision_mv,
            );
        }
        [pred_mv[0] + diff[0], pred_mv[1] + diff[1]]
    }

    /// Full round-trip of `write_read_mv` over a sweep of target MVs at
    /// high precision (every symbol read), at the frame origin
    /// (`MvCtx = 0`), with §8.3 CDF adaptation engaged on both sides.
    #[test]
    fn write_read_mv_round_trips_high_precision() {
        let pred = [100, -50];
        // Mix of: zero joint, single-axis, both-axis, class-0 and
        // higher-class magnitudes, and both signs.
        let targets = [
            [100, -50],    // diff (0,0)   — MV_JOINT_ZERO
            [100, -49],    // diff (0,+1)  — HNZVZ, class 0
            [101, -50],    // diff (+1,0)  — HZVNZ, class 0
            [101, -49],    // diff (+1,+1) — HNZVNZ
            [84, -50],     // diff (-16,0) — class 1
            [100, 30],     // diff (0,+80) — bigger class
            [1100, -1050], // diff (+1000,-1000) — high class both axes
            [50, -200],    // diff (-50,-150)
        ];
        for tgt in targets {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_read_mv(&mut writer, &mut enc_cdfs, tgt, pred, false, false, true).unwrap();
            let bytes = writer.finish();

            let mut dec_cdfs = TileCdfContext::new_from_defaults();
            let recovered = decode_read_mv_mirror(&bytes, &mut dec_cdfs, pred, false, false, true);
            assert_eq!(recovered, tgt, "MV {tgt:?} round-trips at high precision");
            // CDF adaptation must have stepped identically on both sides.
            assert_eq!(enc_cdfs.mv_joint_cdf(0), dec_cdfs.mv_joint_cdf(0));
        }
    }

    /// Round-trip under the intra-block-copy context (`use_intrabc`),
    /// proving the writer selects `MvCtx = MV_INTRABC_CONTEXT = 1`.
    #[test]
    fn write_read_mv_round_trips_intrabc_context() {
        let pred = [0, 0];
        // intra block copy is integer-only, so each component magnitude
        // must be a multiple of 8 (low 3 bits = `(fr<<1)|hp` = `(3<<1)|1`).
        let tgt = [-40, 64];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        // intra block copy is always integer + non-high-precision.
        write_read_mv(&mut writer, &mut enc_cdfs, tgt, pred, true, true, false).unwrap();
        let bytes = writer.finish();

        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let recovered = decode_read_mv_mirror(&bytes, &mut dec_cdfs, pred, true, true, false);
        assert_eq!(
            recovered, tgt,
            "MV round-trips under MvCtx = MV_INTRABC_CONTEXT"
        );
        // The intrabc context row must have adapted, not the ctx-0 row.
        assert_eq!(
            enc_cdfs.mv_joint_cdf(MV_INTRABC_CONTEXT),
            dec_cdfs.mv_joint_cdf(MV_INTRABC_CONTEXT)
        );
    }

    /// Round-trip under `force_integer_mv` (fractional field synthesised
    /// to 3, never read) and `!allow_high_precision_mv` (hp forced to 1).
    /// Only integer-aligned MVs with `fr == 3, hp == 1` are
    /// representable — i.e. magnitudes of the form `8k`.
    #[test]
    fn write_read_mv_round_trips_force_integer() {
        let pred = [0, 0];
        // diff components are multiples of 8 (their low 3 bits = `(fr<<1)|hp`
        // = (3<<1)|1 = 7 ⇒ offset & 7 == 7, i.e. mag in {8, 16, 24, ...}).
        for &d0 in &[0i32, 8, -8, 64, -128] {
            for &d1 in &[0i32, 8, 256, -512] {
                let tgt = [d0, d1];
                let mut enc_cdfs = TileCdfContext::new_from_defaults();
                let mut writer = SymbolWriter::new(false);
                write_read_mv(&mut writer, &mut enc_cdfs, tgt, pred, false, true, false).unwrap();
                let bytes = writer.finish();

                let mut dec_cdfs = TileCdfContext::new_from_defaults();
                let recovered =
                    decode_read_mv_mirror(&bytes, &mut dec_cdfs, pred, false, true, false);
                assert_eq!(recovered, tgt, "integer MV {tgt:?} round-trips");
            }
        }
    }

    /// `mv_joint` selection covers all four quadrants exactly.
    #[test]
    fn write_read_mv_joint_selection_is_exhaustive() {
        let pred = [10, 20];
        let cases = [
            ([10, 20], MV_JOINT_ZERO),
            ([10, 21], MV_JOINT_HNZVZ),
            ([11, 20], MV_JOINT_HZVNZ),
            ([11, 21], MV_JOINT_HNZVNZ),
        ];
        for (tgt, want_joint) in cases {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_read_mv(&mut writer, &mut enc_cdfs, tgt, pred, false, false, true).unwrap();
            let bytes = writer.finish();

            // Read just the leading mv_joint symbol to confirm the quadrant.
            let mut dec_cdfs = TileCdfContext::new_from_defaults();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            let joint = dec.read_symbol(dec_cdfs.mv_joint_cdf(0)).unwrap() as u8;
            assert_eq!(joint, want_joint, "MV {tgt:?} ⇒ joint {want_joint}");
        }
    }

    /// `MV_JOINT_ZERO` emits exactly one symbol (the joint) and no
    /// component reads — verified by re-encoding only that symbol.
    #[test]
    fn write_read_mv_zero_joint_emits_only_joint_symbol() {
        let pred = [7, -3];
        let mut a = TileCdfContext::new_from_defaults();
        let mut wa = SymbolWriter::new(false);
        write_read_mv(&mut wa, &mut a, pred, pred, false, false, true).unwrap();
        let got = wa.finish();

        let mut b = TileCdfContext::new_from_defaults();
        let mut wb = SymbolWriter::new(false);
        let row = b.mv_joint_cdf(0);
        wb.write_symbol(MV_JOINT_ZERO as u32, row).unwrap();
        let want = wb.finish();
        assert_eq!(got, want, "MV_JOINT_ZERO writes a single joint symbol");
    }

    /// The class derivation boundary: `mag == 16` (offset 15) is the
    /// last `MV_CLASS_0` magnitude, `mag == 17` (offset 16) is the first
    /// `MV_CLASS_1` magnitude. Both round-trip.
    #[test]
    fn write_read_mv_class_boundary_round_trips() {
        let pred = [0, 0];
        for d in [16i32, 17, -16, -17] {
            let tgt = [d, 0];
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_read_mv(&mut writer, &mut enc_cdfs, tgt, pred, false, false, true).unwrap();
            let bytes = writer.finish();
            let mut dec_cdfs = TileCdfContext::new_from_defaults();
            let recovered = decode_read_mv_mirror(&bytes, &mut dec_cdfs, pred, false, false, true);
            assert_eq!(recovered, tgt, "class-boundary diff {d} round-trips");
        }
    }

    /// A zero component handed to `write_read_mv_component` directly is a
    /// caller bug (the reader signals zero via `mv_joint`, never a
    /// component read).
    #[test]
    fn write_read_mv_component_rejects_zero_diff() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err =
            write_read_mv_component(&mut writer, &mut cdfs, 0, 0, 0, false, true).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Out-of-range `mv_ctx` / `comp` are caller bugs.
    #[test]
    fn write_read_mv_component_rejects_bad_indices() {
        for (ctx, comp) in [(MV_CONTEXTS, 0), (0, MV_COMPS)] {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err = write_read_mv_component(&mut writer, &mut cdfs, ctx, comp, 5, false, true)
                .unwrap_err();
            assert!(matches!(err, Error::PartitionWalkOutOfRange));
        }
    }

    /// Under `force_integer_mv`, a diff whose fractional field is not 3
    /// is unrepresentable (the reader would synthesise `fr = 3`, not the
    /// caller's value) and must be rejected rather than mis-encoded.
    #[test]
    fn write_read_mv_component_rejects_non_integer_under_force_integer() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        // diff = 1 ⇒ offset 0 ⇒ class0, fr = 0 ≠ 3 under force_integer.
        let err = write_read_mv_component(&mut writer, &mut cdfs, 0, 0, 1, true, true).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Under `!allow_high_precision_mv`, a diff whose half-pel bit is not
    /// 1 is unrepresentable (the reader forces `hp = 1`).
    #[test]
    fn write_read_mv_component_rejects_non_hp_when_disallowed() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        // diff = 2 ⇒ offset 1 ⇒ class0, hp = 1, fr = 0. That's fine.
        // diff = 3 ⇒ offset 2 ⇒ hp = 0 ≠ 1 under no-high-precision ⇒ reject.
        let err =
            write_read_mv_component(&mut writer, &mut cdfs, 0, 0, 3, false, false).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// A magnitude beyond the encodable MV range (`mv_class >= MV_CLASSES`)
    /// is rejected. The maximum encodable offset is `2^(MV_CLASSES+3) - 1`;
    /// one past it overflows the class alphabet.
    #[test]
    fn write_read_mv_component_rejects_overlarge_magnitude() {
        // mv_class = FloorLog2(offset) - 3 must be < MV_CLASSES = 11, so
        // FloorLog2(offset) < 14, i.e. offset < 2^14 = 16384 ⇒ mag <= 16384.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        // mag = 16385 ⇒ offset 16384 ⇒ FloorLog2 = 14 ⇒ class 11 ⇒ reject.
        let err =
            write_read_mv_component(&mut writer, &mut cdfs, 0, 0, 16385, false, true).unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.26 assign_mv PredMv derivation — get_mode + assign_mv_pred_mv
    // -----------------------------------------------------------------

    /// Build a `FindMvStackResult` with caller-chosen `num_mv_found`,
    /// distinct per-slot `RefStackMv` (so a wrong `pos` is observable),
    /// and per-list `GlobalMvs`. Single-pred slots fill list 0; compound
    /// fills both lists. Every other field is the spatial-only default.
    fn mk_mv_stack(num_mv_found: u32) -> FindMvStackResult {
        let mut ref_stack_mv = [[[0i32; 2]; 2]; MAX_REF_MV_STACK_SIZE];
        #[allow(clippy::needless_range_loop)]
        for idx in 0..MAX_REF_MV_STACK_SIZE {
            // List 0: encode the slot index into the value so a mis-pick
            // is caught. List 1: a distinct offset so list selection is
            // also observable.
            ref_stack_mv[idx][0] = [100 + idx as i32 * 10, 200 + idx as i32 * 10];
            ref_stack_mv[idx][1] = [300 + idx as i32 * 10, 400 + idx as i32 * 10];
        }
        FindMvStackResult {
            num_mv_found,
            new_mv_count: 0,
            ref_stack_mv,
            weight_stack: [0; MAX_REF_MV_STACK_SIZE],
            global_mvs: [[7, 9], [11, 13]],
            new_mv_context: 0,
            ref_mv_context: 0,
            zero_mv_context: 0,
            drl_ctx_stack: [0; MAX_REF_MV_STACK_SIZE],
            close_matches: 0,
            total_matches: 0,
            num_nearest: 0,
            num_new: 0,
        }
    }

    /// §5.11.26 GLOBALMV arm — `PredMv[i] = GlobalMvs[i]` regardless of
    /// `RefMvIdx` or stack depth, on both lists.
    #[test]
    fn assign_mv_pred_mv_globalmv_uses_global() {
        let s = mk_mv_stack(0);
        assert_eq!(assign_mv_pred_mv(&s, MODE_GLOBALMV, 0, 2).unwrap(), [7, 9]);
        // Compound list 1 via GLOBAL_GLOBALMV.
        assert_eq!(
            assign_mv_pred_mv(&s, MODE_GLOBAL_GLOBALMV, 1, 0).unwrap(),
            [11, 13]
        );
    }

    /// §5.11.26 NEARESTMV arm — `pos = 0` always, so the top stack slot
    /// is selected irrespective of `RefMvIdx`.
    #[test]
    fn assign_mv_pred_mv_nearestmv_uses_slot0() {
        let s = mk_mv_stack(4);
        for ref_mv_idx in 0..=3 {
            assert_eq!(
                assign_mv_pred_mv(&s, MODE_NEARESTMV, 0, ref_mv_idx).unwrap(),
                [100, 200],
                "NEARESTMV ignores RefMvIdx {ref_mv_idx}"
            );
        }
    }

    /// §5.11.26 NEARMV arm — `pos = RefMvIdx`, selecting the indexed slot.
    #[test]
    fn assign_mv_pred_mv_nearmv_uses_ref_mv_idx() {
        let s = mk_mv_stack(4);
        assert_eq!(
            assign_mv_pred_mv(&s, MODE_NEARMV, 0, 1).unwrap(),
            [110, 210]
        );
        assert_eq!(
            assign_mv_pred_mv(&s, MODE_NEARMV, 0, 2).unwrap(),
            [120, 220]
        );
        assert_eq!(
            assign_mv_pred_mv(&s, MODE_NEARMV, 0, 3).unwrap(),
            [130, 230]
        );
    }

    /// §5.11.26 NEWMV arm — `pos = RefMvIdx` when `NumMvFound > 1`, but
    /// collapses to `pos = 0` whenever `NumMvFound <= 1`.
    #[test]
    fn assign_mv_pred_mv_newmv_collapses_on_short_stack() {
        // NumMvFound = 2 ⇒ pos = RefMvIdx honoured.
        let deep = mk_mv_stack(2);
        assert_eq!(
            assign_mv_pred_mv(&deep, MODE_NEWMV, 0, 1).unwrap(),
            [110, 210]
        );
        // NumMvFound = 1 ⇒ pos forced to 0 even though RefMvIdx = 0 here;
        // and NumMvFound = 1 with RefMvIdx = 0 still reads slot 0.
        let shallow = mk_mv_stack(1);
        assert_eq!(
            assign_mv_pred_mv(&shallow, MODE_NEWMV, 0, 0).unwrap(),
            [100, 200]
        );
    }

    /// §5.11.26 compound NEW_NEARMV — list 0 (`NEWMV`) draws from the
    /// stack at `RefMvIdx`; list 1 (`NEARMV`) draws from list-1 slots.
    #[test]
    fn assign_mv_pred_mv_compound_per_list() {
        let s = mk_mv_stack(4);
        // list 0 = NEWMV @ RefMvIdx 1 ⇒ ref_stack_mv[1][0].
        assert_eq!(
            assign_mv_pred_mv(&s, MODE_NEW_NEARMV, 0, 1).unwrap(),
            [110, 210]
        );
        // list 1 = NEARMV @ RefMvIdx 1 ⇒ ref_stack_mv[1][1].
        assert_eq!(
            assign_mv_pred_mv(&s, MODE_NEW_NEARMV, 1, 1).unwrap(),
            [310, 410]
        );
    }

    /// §5.11.26 reachability reject — a `pos` at or beyond `NumMvFound`
    /// would read an undefined `RefStackMv` slot.
    #[test]
    fn assign_mv_pred_mv_rejects_unreachable_pos() {
        let s = mk_mv_stack(2);
        // NEARMV @ RefMvIdx 2 needs slot 2, but NumMvFound = 2 ⇒ reject.
        assert!(matches!(
            assign_mv_pred_mv(&s, MODE_NEARMV, 0, 2).unwrap_err(),
            Error::PartitionWalkOutOfRange
        ));
        // NEARESTMV / NEWMV on an empty stack read slot 0 — ALWAYS
        // populated per the §7.10.2.12 extra-search pad ("for single
        // prediction, NumMvFound is not incremented by the addition of
        // global motion candidates"), so an empty-neighbourhood block
        // legitimately predicts from it (r411 contract fix).
        let empty = mk_mv_stack(0);
        assert_eq!(
            assign_mv_pred_mv(&empty, MODE_NEARESTMV, 0, 0).unwrap(),
            empty.ref_stack_mv[0][0]
        );
        assert_eq!(
            assign_mv_pred_mv(&empty, MODE_NEWMV, 0, 0).unwrap(),
            empty.ref_stack_mv[0][0]
        );
        // A `pos >= 2` beyond `NumMvFound` stays a reject on the empty
        // stack (unreachable through the §5.11.23 drl loop).
        assert!(matches!(
            assign_mv_pred_mv(&empty, MODE_NEARMV, 0, 2).unwrap_err(),
            Error::PartitionWalkOutOfRange
        ));
    }

    /// §5.11.26 caller-bug rejects on the mode / ref-list guard — a
    /// non-inter `y_mode` or a `ref_list > 1`.
    #[test]
    fn assign_mv_pred_mv_rejects_bad_mode_or_ref_list() {
        let s = mk_mv_stack(4);
        assert!(matches!(
            assign_mv_pred_mv(&s, DC_PRED_U8, 0, 0).unwrap_err(),
            Error::PartitionWalkOutOfRange
        ));
        assert!(matches!(
            assign_mv_pred_mv(&s, MODE_NEW_NEWMV + 1, 0, 0).unwrap_err(),
            Error::PartitionWalkOutOfRange
        ));
        assert!(matches!(
            assign_mv_pred_mv(&s, MODE_NEARMV, 2, 0).unwrap_err(),
            Error::PartitionWalkOutOfRange
        ));
    }

    /// The PredMv this helper derives is exactly the predictor
    /// [`write_read_mv`] subtracts: a NEWMV target minus its derived
    /// `PredMv` round-trips through a §5.11.31 reader mirror.
    #[test]
    fn assign_mv_pred_mv_feeds_write_read_mv() {
        let s = mk_mv_stack(2);
        let pred = assign_mv_pred_mv(&s, MODE_NEWMV, 0, 1).unwrap();
        assert_eq!(pred, [110, 210]);
        let target_mv = [pred[0] + 5, pred[1] - 9];

        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_read_mv(
            &mut writer,
            &mut enc_cdfs,
            target_mv,
            pred,
            false,
            false,
            true,
        )
        .unwrap();
        let bytes = writer.finish();

        // §5.11.31 reader mirror reconstructs Mv = PredMv + diffMv.
        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let recovered = decode_read_mv_mirror(&bytes, &mut dec_cdfs, pred, false, false, true);
        assert_eq!(recovered, target_mv, "PredMv + diffMv reconstructs Mv");
    }

    // -----------------------------------------------------------------
    // §5.11.26 assign_mv intra-block-copy arm — assign_mv_pred_mv_intrabc
    // -----------------------------------------------------------------

    /// Build a `FindMvStackResult` shaped like the §7.10.2 output on an
    /// intra-block-copy block: `GlobalMvs[ 0 ]` is the zero vector
    /// (§7.10.2.1 `ref == INTRA_FRAME`), and the caller chooses the two
    /// `RefStackMv[ idx ][ 0 ]` slots the §5.11.26 intrabc arm reads
    /// (the §7.10.2.12 single-pred tail pads unwritten slots with
    /// `GlobalMvs[ 0 ] = [0, 0]` without incrementing `NumMvFound`).
    fn mk_intrabc_mv_stack(
        slot0: [i32; 2],
        slot1: [i32; 2],
        num_mv_found: u32,
    ) -> FindMvStackResult {
        let mut s = mk_mv_stack(num_mv_found);
        s.global_mvs = [[0, 0], [0, 0]];
        s.ref_stack_mv = [[[0i32; 2]; 2]; MAX_REF_MV_STACK_SIZE];
        s.ref_stack_mv[0][0] = slot0;
        s.ref_stack_mv[1][0] = slot1;
        s
    }

    /// §5.11.26 intrabc — a non-zero `RefStackMv[ 0 ][ 0 ]` is the
    /// predictor; slot 1 and the superblock fallback are never reached.
    #[test]
    fn assign_mv_pred_mv_intrabc_uses_slot0_when_nonzero() {
        let s = mk_intrabc_mv_stack([-64, 8], [16, -24], 2);
        assert_eq!(
            assign_mv_pred_mv_intrabc(&s, false, 16, 0).unwrap(),
            [-64, 8]
        );
        // A half-zero slot 0 is still "non-zero" — the fallback tests
        // both components.
        let row_only = mk_intrabc_mv_stack([-8, 0], [16, -24], 2);
        assert_eq!(
            assign_mv_pred_mv_intrabc(&row_only, false, 16, 0).unwrap(),
            [-8, 0]
        );
    }

    /// §5.11.26 intrabc — a zero slot 0 falls through to
    /// `RefStackMv[ 1 ][ 0 ]`.
    #[test]
    fn assign_mv_pred_mv_intrabc_falls_to_slot1() {
        let s = mk_intrabc_mv_stack([0, 0], [16, -24], 2);
        assert_eq!(
            assign_mv_pred_mv_intrabc(&s, false, 16, 0).unwrap(),
            [16, -24]
        );
    }

    /// §5.11.26 intrabc — both slots zero (the §7.10.2.12 global-MV
    /// padding outcome on the first intrabc block) and the block in the
    /// tile's top superblock row (`MiRow - sbSize4 < MiRowStart`):
    /// the predictor points left by `sbSize4 * MI_SIZE +
    /// INTRABC_DELAY_PIXELS` luma samples, in the **column** component,
    /// in 1/8-sample units.
    #[test]
    fn assign_mv_pred_mv_intrabc_top_row_fallback() {
        let s = mk_intrabc_mv_stack([0, 0], [0, 0], 0);
        // 64x64 superblocks: sbSize4 = 16 ⇒ [0, -(16*4 + 256)*8].
        assert_eq!(
            assign_mv_pred_mv_intrabc(&s, false, 0, 0).unwrap(),
            [0, -2560]
        );
        // mi_row 15 with MiRowStart 0 is still inside the first
        // superblock row (15 - 16 < 0).
        assert_eq!(
            assign_mv_pred_mv_intrabc(&s, false, 15, 0).unwrap(),
            [0, -2560]
        );
        // 128x128 superblocks: sbSize4 = 32 ⇒ [0, -(32*4 + 256)*8];
        // rows 0..=31 of the tile are all "top superblock row".
        assert_eq!(
            assign_mv_pred_mv_intrabc(&s, true, 31, 0).unwrap(),
            [0, -3072]
        );
        // A non-zero MiRowStart shifts the boundary: mi_row 40 in a
        // tile starting at mi_row 32 is in that tile's top superblock
        // row (40 - 16 < 32).
        assert_eq!(
            assign_mv_pred_mv_intrabc(&s, false, 40, 32).unwrap(),
            [0, -2560]
        );
    }

    /// §5.11.26 intrabc — both slots zero below the top superblock row:
    /// the predictor points one superblock **up** (`-(sbSize4 * MI_SIZE
    /// * 8)` in the row component).
    #[test]
    fn assign_mv_pred_mv_intrabc_up_fallback() {
        let s = mk_intrabc_mv_stack([0, 0], [0, 0], 0);
        // 64x64: mi_row 16 is exactly one superblock down (16 - 16 = 0
        // is NOT < 0) ⇒ [-16*4*8, 0].
        assert_eq!(
            assign_mv_pred_mv_intrabc(&s, false, 16, 0).unwrap(),
            [-512, 0]
        );
        // 128x128: sbSize4 = 32 ⇒ [-1024, 0] from row 32.
        assert_eq!(
            assign_mv_pred_mv_intrabc(&s, true, 32, 0).unwrap(),
            [-1024, 0]
        );
        // Tile-relative: mi_row 48 with MiRowStart 32 (48 - 16 >= 32).
        assert_eq!(
            assign_mv_pred_mv_intrabc(&s, false, 48, 32).unwrap(),
            [-512, 0]
        );
    }

    /// §5.11.26 intrabc caller-bug reject — a block above its own tile
    /// (`mi_row < mi_row_start`).
    #[test]
    fn assign_mv_pred_mv_intrabc_rejects_row_above_tile() {
        let s = mk_intrabc_mv_stack([0, 0], [0, 0], 0);
        assert!(matches!(
            assign_mv_pred_mv_intrabc(&s, false, 31, 32).unwrap_err(),
            Error::PartitionWalkOutOfRange
        ));
    }

    /// End-to-end: the §5.11.26 intrabc predictor feeds
    /// [`write_read_mv`] under the intra-block-copy MV regime
    /// (`MvCtx = MV_INTRABC_CONTEXT`, integer-only, no high precision)
    /// and the §5.11.31 reader mirror reconstructs the target block
    /// vector — on both the slot-0 path and the top-row fallback.
    #[test]
    fn assign_mv_pred_mv_intrabc_feeds_write_read_mv() {
        for (s, mi_row, target_mv) in [
            // Slot-0 predictor (integer-aligned: multiples of 8).
            (
                mk_intrabc_mv_stack([-64, 8], [0, 0], 2),
                16,
                [-64 + 16, 8 - 40],
            ),
            // Zero stack ⇒ top-row fallback predictor [0, -2560].
            (mk_intrabc_mv_stack([0, 0], [0, 0], 0), 0, [0, -2560 + 64]),
        ] {
            let pred = assign_mv_pred_mv_intrabc(&s, false, mi_row, 0).unwrap();
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_read_mv(
                &mut writer,
                &mut enc_cdfs,
                target_mv,
                pred,
                /* use_intrabc = */ true,
                /* force_integer_mv = */ true,
                /* allow_high_precision_mv = */ false,
            )
            .unwrap();
            let bytes = writer.finish();

            let mut dec_cdfs = TileCdfContext::new_from_defaults();
            let recovered = decode_read_mv_mirror(&bytes, &mut dec_cdfs, pred, true, true, false);
            assert_eq!(recovered, target_mv, "intrabc Mv reconstructs");
            // The intrabc context row adapted identically on both sides.
            assert_eq!(
                enc_cdfs.mv_joint_cdf(MV_INTRABC_CONTEXT),
                dec_cdfs.mv_joint_cdf(MV_INTRABC_CONTEXT)
            );
        }
    }

    // -----------------------------------------------------------------
    // §5.11.7 use_intrabc — write_use_intrabc round-trips through
    // PartitionWalker::decode_use_intrabc; write_intra_frame_intrabc_arm
    // composes the full §5.11.7 intrabc arm (r279).
    // -----------------------------------------------------------------

    /// `write_use_intrabc` round-trips both values through the decode
    /// twin with §8.3 CDF adaptation in lockstep.
    #[test]
    fn write_use_intrabc_round_trips_through_decode_twin() {
        for target in [0u8, 1u8] {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_use_intrabc(&mut writer, &mut enc_cdfs, target, true).unwrap();
            let bytes = writer.finish();

            let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            let got = walker
                .decode_use_intrabc(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_8X8, true)
                .unwrap();
            assert_eq!(got, target, "use_intrabc = {target} round-trips");
            // The contextless TileIntrabcCdf row adapted identically.
            assert_eq!(enc_cdfs.intrabc_cdf(), dec_cdfs.intrabc_cdf());
        }
    }

    /// §5.11.7 fall-through arm (`allow_intrabc == 0`): no bits, the
    /// reader forces `use_intrabc = 0` — a `1` is a caller bug, as is
    /// any value outside the §3 binary alphabet.
    #[test]
    fn write_use_intrabc_fall_through_no_bits_and_rejects() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_use_intrabc(&mut writer, &mut cdfs, 0, false).unwrap();
        assert_eq!(
            writer.finish(),
            SymbolWriter::new(false).finish(),
            "fall-through arm emits no bits"
        );
        // The CDF row must be untouched on the no-bit arm.
        assert_eq!(
            cdfs.intrabc_cdf(),
            TileCdfContext::new_from_defaults().intrabc_cdf()
        );

        let mut writer = SymbolWriter::new(false);
        assert!(matches!(
            write_use_intrabc(&mut writer, &mut cdfs, 1, false).unwrap_err(),
            Error::PartitionWalkOutOfRange
        ));
        for allow in [false, true] {
            let mut writer = SymbolWriter::new(false);
            assert!(matches!(
                write_use_intrabc(&mut writer, &mut cdfs, 2, allow).unwrap_err(),
                Error::PartitionWalkOutOfRange
            ));
        }
    }

    /// Full §5.11.7 intrabc-arm composition: `use_intrabc` S() +
    /// §5.11.26 `assign_mv( 0 )` (forced-NEWMV `read_mv`) round-trip
    /// through `decode_use_intrabc` + a §5.11.31 reader mirror sharing
    /// one decoder, on both the slot-0 predictor and the zero-stack
    /// top-row fallback. The returned readout carries the spec's fixed
    /// no-bit assignments.
    #[test]
    fn write_intra_frame_intrabc_arm_round_trips() {
        for (stack, mi_row, target_mv) in [
            // Slot-0 predictor [-64, 8]; integer-aligned diff (+16, -48).
            (mk_intrabc_mv_stack([-64, 8], [0, 0], 2), 16u32, [-48, -40]),
            // Zero stack ⇒ top-row fallback predictor [0, -2560].
            (
                mk_intrabc_mv_stack([0, 0], [0, 0], 0),
                0u32,
                [0, -2560 + 64],
            ),
        ] {
            let inputs = IntrabcArmInputs {
                mv: target_mv,
                mv_stack: &stack,
                use_128x128_superblock: false,
                mi_row,
                mi_row_start: 0,
            };
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let info =
                write_intra_frame_intrabc_arm(&mut writer, &mut enc_cdfs, true, Some(&inputs))
                    .unwrap()
                    .expect("intrabc arm fired");
            let bytes = writer.finish();

            // §5.11.7 fixed assignments surface in the readout.
            let want_pred = assign_mv_pred_mv_intrabc(&stack, false, mi_row, 0).unwrap();
            assert_eq!(
                info,
                IntrabcBlockInfo {
                    is_inter: 1,
                    y_mode: DC_PRED_U8,
                    uv_mode: DC_PRED_U8,
                    motion_mode: MOTION_MODE_SIMPLE,
                    compound_type: COMPOUND_AVERAGE,
                    palette_size_y: 0,
                    palette_size_uv: 0,
                    interp_filter: [BILINEAR, BILINEAR],
                    mv: target_mv,
                    pred_mv: want_pred,
                }
            );

            // Reader side: decode_use_intrabc + the §5.11.31 read_mv
            // mirror from the same live decoder.
            let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            let use_intrabc = walker
                .decode_use_intrabc(&mut dec, &mut dec_cdfs, mi_row, 0, BLOCK_16X16, true)
                .unwrap();
            assert_eq!(use_intrabc, 1);
            let mv_joint = dec
                .read_symbol(dec_cdfs.mv_joint_cdf(MV_INTRABC_CONTEXT))
                .unwrap() as u8;
            let mut diff = [0i32; 2];
            if mv_joint == MV_JOINT_HZVNZ || mv_joint == MV_JOINT_HNZVNZ {
                diff[0] = decode_read_mv_component_mirror(
                    &mut dec,
                    &mut dec_cdfs,
                    MV_INTRABC_CONTEXT,
                    0,
                    true,
                    false,
                );
            }
            if mv_joint == MV_JOINT_HNZVZ || mv_joint == MV_JOINT_HNZVNZ {
                diff[1] = decode_read_mv_component_mirror(
                    &mut dec,
                    &mut dec_cdfs,
                    MV_INTRABC_CONTEXT,
                    1,
                    true,
                    false,
                );
            }
            let recovered = [want_pred[0] + diff[0], want_pred[1] + diff[1]];
            assert_eq!(recovered, target_mv, "intrabc Mv reconstructs");
            // Both adapted rows stepped identically.
            assert_eq!(enc_cdfs.intrabc_cdf(), dec_cdfs.intrabc_cdf());
            assert_eq!(
                enc_cdfs.mv_joint_cdf(MV_INTRABC_CONTEXT),
                dec_cdfs.mv_joint_cdf(MV_INTRABC_CONTEXT)
            );
        }
    }

    /// `intrabc = None`: a single `use_intrabc = 0` S() (byte-equal to
    /// the leaf) when allowed, no bits on the fall-through arm —
    /// `None` returned either way so the caller proceeds to the
    /// §5.11.7 `else` (intra) arm.
    #[test]
    fn write_intra_frame_intrabc_arm_none_matches_leaf() {
        // allow_intrabc = true: one S() carrying 0.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let info = write_intra_frame_intrabc_arm(&mut writer, &mut cdfs, true, None).unwrap();
        assert!(info.is_none());
        let bytes = writer.finish();

        let mut leaf_cdfs = TileCdfContext::new_from_defaults();
        let mut leaf_writer = SymbolWriter::new(false);
        write_use_intrabc(&mut leaf_writer, &mut leaf_cdfs, 0, true).unwrap();
        assert_eq!(bytes, leaf_writer.finish(), "byte-equal to the leaf");

        let (mut walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let got = walker
            .decode_use_intrabc(&mut dec, &mut dec_cdfs, 0, 0, BLOCK_8X8, true)
            .unwrap();
        assert_eq!(got, 0);

        // allow_intrabc = false: no bits at all.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let info = write_intra_frame_intrabc_arm(&mut writer, &mut cdfs, false, None).unwrap();
        assert!(info.is_none());
        assert_eq!(writer.finish(), SymbolWriter::new(false).finish());
    }

    /// Caller-bug rejects: an intrabc block on a frame whose header
    /// never allowed it, a block above its own tile, and a target MV
    /// whose difference is not integer-aligned (intra block copy is
    /// integer-only per §5.11.32).
    #[test]
    fn write_intra_frame_intrabc_arm_rejects() {
        let stack = mk_intrabc_mv_stack([-64, 8], [0, 0], 2);
        let inputs = IntrabcArmInputs {
            mv: [-48, -40],
            mv_stack: &stack,
            use_128x128_superblock: false,
            mi_row: 16,
            mi_row_start: 0,
        };
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        assert!(matches!(
            write_intra_frame_intrabc_arm(&mut writer, &mut cdfs, false, Some(&inputs))
                .unwrap_err(),
            Error::PartitionWalkOutOfRange
        ));

        // mi_row above the tile's MiRowStart — propagated from the
        // §5.11.26 predictor derivation.
        let above_tile = IntrabcArmInputs {
            mi_row: 31,
            mi_row_start: 32,
            ..inputs
        };
        let mut writer = SymbolWriter::new(false);
        assert!(matches!(
            write_intra_frame_intrabc_arm(&mut writer, &mut cdfs, true, Some(&above_tile))
                .unwrap_err(),
            Error::PartitionWalkOutOfRange
        ));

        // Non-integer-aligned difference (diff col = +4, not a multiple
        // of 8) — propagated from the §5.11.32 component writer.
        let fractional = IntrabcArmInputs {
            mv: [-64, 12],
            ..inputs
        };
        let mut writer = SymbolWriter::new(false);
        assert!(matches!(
            write_intra_frame_intrabc_arm(&mut writer, &mut cdfs, true, Some(&fractional))
                .unwrap_err(),
            Error::PartitionWalkOutOfRange
        ));
    }

    // -----------------------------------------------------------------
    // §5.11.23 inter_block_mode_info composition — write_inter_block_mode_info
    // round-trips through a full §5.11.23 reader mirror (ref_frames →
    // mode → drl_mode → assign_mv), reproduced inline. The mirror is
    // exercised on the no-neighbour octet (avail_u = avail_l = false),
    // so every §8.3.2 `count_refs` is 0 and every `ref_count_ctx(0,0)`
    // is 1 (Equal), making the SINGLE_REFERENCE ref-frame reads
    // deterministic.
    // -----------------------------------------------------------------

    /// Full §5.11.23 reader mirror, restricted to the SINGLE_REFERENCE /
    /// compound-via-target subset the round-trip tests drive. Reads
    /// `read_ref_frames` (single arm), the §5.11.23 mode dispatch, the
    /// `drl_mode` loop, and `assign_mv` from a single live decoder, on
    /// the all-empty neighbour octet. Returns `(ref_frame, y_mode,
    /// ref_mv_idx, mv)`.
    #[allow(clippy::too_many_arguments)]
    fn mirror_inter_block_mode_info(
        bytes: &[u8],
        cdfs: &mut TileCdfContext,
        mv_stack: &FindMvStackResult,
        skip_mode: u8,
        skip_mode_frame: [i32; 2],
        seg_skip_active: bool,
        seg_globalmv_active: bool,
        force_integer_mv: bool,
        allow_high_precision_mv: bool,
    ) -> ([i32; 2], u8, u32, [[i32; 2]; 2]) {
        let pad = if bytes.is_empty() {
            vec![0u8]
        } else {
            bytes.to_vec()
        };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), false).unwrap();

        // §5.11.25 read_ref_frames (no-bit arms first, then the
        // SINGLE_REFERENCE arm-4 reader on the empty neighbour octet).
        let ref_frame: [i32; 2] = if skip_mode != 0 {
            [skip_mode_frame[0], skip_mode_frame[1]]
        } else if seg_skip_active || seg_globalmv_active {
            [1, -1]
        } else {
            // Arm 4, SINGLE_REFERENCE (no reference_select ⇒ comp_mode =
            // SINGLE). All count_refs are 0 ⇒ ref_count_ctx = 1 (Equal).
            let p1_ctx = ref_count_ctx(0, 0);
            let single_ref_p1 = dec.read_symbol(cdfs.single_ref_cdf(p1_ctx, 0)).unwrap() as u8;
            let r0: i32 = if single_ref_p1 != 0 {
                let single_ref_p2 = dec
                    .read_symbol(cdfs.single_ref_cdf(ref_count_ctx(0, 0), 1))
                    .unwrap() as u8;
                if single_ref_p2 == 0 {
                    let single_ref_p6 = dec
                        .read_symbol(cdfs.single_ref_cdf(ref_count_ctx(0, 0), 5))
                        .unwrap() as u8;
                    if single_ref_p6 != 0 {
                        6
                    } else {
                        5
                    }
                } else {
                    7
                }
            } else {
                let single_ref_p3 = dec
                    .read_symbol(cdfs.single_ref_cdf(ref_count_ctx(0, 0), 2))
                    .unwrap() as u8;
                if single_ref_p3 != 0 {
                    let single_ref_p5 = dec
                        .read_symbol(cdfs.single_ref_cdf(ref_count_ctx(0, 0), 4))
                        .unwrap() as u8;
                    if single_ref_p5 != 0 {
                        4
                    } else {
                        3
                    }
                } else {
                    let single_ref_p4 = dec
                        .read_symbol(cdfs.single_ref_cdf(ref_count_ctx(0, 0), 3))
                        .unwrap() as u8;
                    if single_ref_p4 != 0 {
                        2
                    } else {
                        1
                    }
                }
            };
            [r0, -1]
        };

        let is_compound = ref_frame[1] > 0;

        // §5.11.23 mode dispatch.
        let y_mode: u8 = if skip_mode != 0 {
            MODE_NEAREST_NEARESTMV
        } else if seg_skip_active || seg_globalmv_active {
            MODE_GLOBALMV
        } else if is_compound {
            let cm_ctx = compound_mode_ctx(
                mv_stack.ref_mv_context as usize,
                mv_stack.new_mv_context as usize,
            );
            let cm = dec.read_symbol(cdfs.compound_mode_cdf(cm_ctx)).unwrap() as u8;
            MODE_NEAREST_NEARESTMV + cm
        } else {
            let new_mv = dec
                .read_symbol(cdfs.new_mv_cdf(mv_stack.new_mv_context as usize))
                .unwrap();
            if new_mv == 0 {
                MODE_NEWMV
            } else {
                let zero_mv = dec
                    .read_symbol(cdfs.zero_mv_cdf(mv_stack.zero_mv_context as usize))
                    .unwrap();
                if zero_mv == 0 {
                    MODE_GLOBALMV
                } else {
                    let ref_mv = dec
                        .read_symbol(cdfs.ref_mv_cdf(mv_stack.ref_mv_context as usize))
                        .unwrap();
                    if ref_mv == 0 {
                        MODE_NEARESTMV
                    } else {
                        MODE_NEARMV
                    }
                }
            }
        };

        // §5.11.23 drl_mode loop.
        let mut ref_mv_idx: u32 = 0;
        if y_mode == MODE_NEWMV || y_mode == MODE_NEW_NEWMV {
            for idx in 0u32..2 {
                if mv_stack.num_mv_found > idx + 1 {
                    let drl_ctx = mv_stack.drl_ctx_stack[idx as usize] as usize;
                    let drl = dec.read_symbol(cdfs.drl_mode_cdf(drl_ctx)).unwrap();
                    if drl == 0 {
                        ref_mv_idx = idx;
                        break;
                    }
                    ref_mv_idx = idx + 1;
                }
            }
        } else if has_nearmv(y_mode) {
            ref_mv_idx = 1;
            for idx in 1u32..3 {
                if mv_stack.num_mv_found > idx + 1 {
                    let drl_ctx = mv_stack.drl_ctx_stack[idx as usize] as usize;
                    let drl = dec.read_symbol(cdfs.drl_mode_cdf(drl_ctx)).unwrap();
                    if drl == 0 {
                        ref_mv_idx = idx;
                        break;
                    }
                    ref_mv_idx = idx + 1;
                }
            }
        }

        // §5.11.31 assign_mv (use_intrabc = 0): NEWMV lists read a diff.
        let list_count: u8 = if is_compound { 2 } else { 1 };
        let mut mv: [[i32; 2]; 2] = [[0, 0], [0, 0]];
        for i in 0..list_count {
            let pred = assign_mv_pred_mv(mv_stack, y_mode, i, ref_mv_idx).unwrap();
            if crate::cdf::get_mode(y_mode, i as usize) == MODE_NEWMV {
                let mv_ctx = 0usize;
                let mv_joint = dec.read_symbol(cdfs.mv_joint_cdf(mv_ctx)).unwrap() as u8;
                let mut diff = [0i32; 2];
                if mv_joint == MV_JOINT_HZVNZ || mv_joint == MV_JOINT_HNZVNZ {
                    diff[0] = decode_read_mv_component_mirror(
                        &mut dec,
                        cdfs,
                        mv_ctx,
                        0,
                        force_integer_mv,
                        allow_high_precision_mv,
                    );
                }
                if mv_joint == MV_JOINT_HNZVZ || mv_joint == MV_JOINT_HNZVNZ {
                    diff[1] = decode_read_mv_component_mirror(
                        &mut dec,
                        cdfs,
                        mv_ctx,
                        1,
                        force_integer_mv,
                        allow_high_precision_mv,
                    );
                }
                mv[i as usize] = [pred[0] + diff[0], pred[1] + diff[1]];
            } else {
                mv[i as usize] = pred;
            }
        }

        (ref_frame, y_mode, ref_mv_idx, mv)
    }

    /// Build the no-neighbour arm-selection / octet inputs shared by the
    /// round-trip tests: empty neighbour octet, no segmentation, no
    /// reference_select (forces the §5.11.25 SINGLE_REFERENCE arm).
    #[allow(clippy::type_complexity)]
    fn empty_neighbour_inputs() -> (bool, bool, bool, bool, bool, bool, [i32; 2], [i32; 2]) {
        (
            false,
            false, // avail_u, avail_l
            true,
            true, // above_single, left_single
            false,
            false, // above_intra, left_intra
            [-1, -1],
            [-1, -1], // above_ref_frame, left_ref_frame
        )
    }

    /// §5.11.23 single-pred NEWMV round-trip: ref_frames (single arm) +
    /// new_mv S() + drl_mode loop + assign_mv NEWMV read all chain
    /// through the §5.11.23 reader mirror, recovering RefFrame / YMode /
    /// RefMvIdx / Mv exactly.
    #[test]
    fn write_inter_block_mode_info_single_newmv_round_trip() {
        let stack = mk_mv_stack(3); // NumMvFound = 3 ⇒ drl loop codes bits.
        let pred = assign_mv_pred_mv(&stack, MODE_NEWMV, 0, 1).unwrap();
        let target_mv = [pred[0] + 7, pred[1] - 5];
        let mv = [target_mv, [0, 0]];
        let ref_frame = [4i32, -1]; // GOLDEN_FRAME, single.
        let (au, al, asg, lsg, ai, li, arf, lrf) = empty_neighbour_inputs();

        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_inter_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            ref_frame,
            MODE_NEWMV,
            mv,
            /* ref_mv_idx = */ 1,
            &stack,
            BLOCK_8X8,
            /* skip_mode = */ 0,
            [0, 0],
            false,
            0,
            false,
            false,
            /* reference_select = */ false,
            au,
            al,
            asg,
            lsg,
            ai,
            li,
            arf,
            lrf,
            /* force_integer_mv = */ false,
            /* allow_high_precision_mv = */ true,
            &InterBlockModeInfoTail::bit_silent(),
        )
        .unwrap();
        let bytes = writer.finish();

        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let (rf, ym, rmi, dmv) = mirror_inter_block_mode_info(
            &bytes,
            &mut dec_cdfs,
            &stack,
            0,
            [0, 0],
            false,
            false,
            false,
            true,
        );
        assert_eq!(rf, ref_frame, "RefFrame round-trips");
        assert_eq!(ym, MODE_NEWMV, "YMode round-trips");
        assert_eq!(rmi, 1, "RefMvIdx round-trips");
        assert_eq!(dmv[0], target_mv, "list-0 NEWMV Mv round-trips");
        // CDFs adapted in lockstep on the consulted rows.
        assert_eq!(enc_cdfs.new_mv_cdf(0), dec_cdfs.new_mv_cdf(0));
        assert_eq!(enc_cdfs.mv_joint_cdf(0), dec_cdfs.mv_joint_cdf(0));
    }

    /// §5.11.23 single-pred NEARMV round-trip: the `has_nearmv()` drl arm
    /// codes a `drl_mode` bit, and assign_mv emits NO MV bits (NEARMV is
    /// not NEWMV ⇒ Mv = PredMv).
    #[test]
    fn write_inter_block_mode_info_single_nearmv_round_trip() {
        let stack = mk_mv_stack(4); // deep enough for the has_nearmv loop.
                                    // NEARMV @ RefMvIdx 2 ⇒ PredMv = ref_stack_mv[2][0].
        let pred = assign_mv_pred_mv(&stack, MODE_NEARMV, 0, 2).unwrap();
        let mv = [pred, [0, 0]]; // Mv inherits PredMv (no NEWMV read).
        let ref_frame = [2i32, -1]; // LAST2_FRAME, single.
        let (au, al, asg, lsg, ai, li, arf, lrf) = empty_neighbour_inputs();

        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_inter_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            ref_frame,
            MODE_NEARMV,
            mv,
            /* ref_mv_idx = */ 2,
            &stack,
            BLOCK_8X8,
            0,
            [0, 0],
            false,
            0,
            false,
            false,
            false,
            au,
            al,
            asg,
            lsg,
            ai,
            li,
            arf,
            lrf,
            false,
            true,
            &InterBlockModeInfoTail::bit_silent(),
        )
        .unwrap();
        let bytes = writer.finish();

        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let (rf, ym, rmi, dmv) = mirror_inter_block_mode_info(
            &bytes,
            &mut dec_cdfs,
            &stack,
            0,
            [0, 0],
            false,
            false,
            false,
            true,
        );
        assert_eq!(rf, ref_frame);
        assert_eq!(ym, MODE_NEARMV);
        assert_eq!(rmi, 2, "RefMvIdx from has_nearmv drl arm");
        assert_eq!(dmv[0], pred, "NEARMV Mv = PredMv (no diff bits)");
    }

    /// §5.11.23 single-pred NEARESTMV round-trip: no drl bits (NEARESTMV
    /// is neither a NEWMV arm nor has_nearmv), no MV bits.
    #[test]
    fn write_inter_block_mode_info_single_nearestmv_round_trip() {
        let stack = mk_mv_stack(4);
        let pred = assign_mv_pred_mv(&stack, MODE_NEARESTMV, 0, 0).unwrap();
        let mv = [pred, [0, 0]];
        let ref_frame = [1i32, -1]; // LAST_FRAME.
        let (au, al, asg, lsg, ai, li, arf, lrf) = empty_neighbour_inputs();

        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_inter_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            ref_frame,
            MODE_NEARESTMV,
            mv,
            0,
            &stack,
            BLOCK_8X8,
            0,
            [0, 0],
            false,
            0,
            false,
            false,
            false,
            au,
            al,
            asg,
            lsg,
            ai,
            li,
            arf,
            lrf,
            false,
            true,
            &InterBlockModeInfoTail::bit_silent(),
        )
        .unwrap();
        let bytes = writer.finish();

        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let (rf, ym, rmi, dmv) = mirror_inter_block_mode_info(
            &bytes,
            &mut dec_cdfs,
            &stack,
            0,
            [0, 0],
            false,
            false,
            false,
            true,
        );
        assert_eq!(rf, ref_frame);
        assert_eq!(ym, MODE_NEARESTMV);
        assert_eq!(rmi, 0);
        assert_eq!(dmv[0], pred);
    }

    /// §5.11.23 arm-1 skip_mode round-trip: ref_frames forced to
    /// SkipModeFrame (no bits), YMode = NEAREST_NEARESTMV (no mode bits),
    /// both lists NEARESTMV ⇒ no MV bits. The whole composition emits
    /// zero symbols.
    #[test]
    fn write_inter_block_mode_info_skip_mode_emits_no_bits() {
        let stack = mk_mv_stack(2);
        // skip_mode forces a compound NEAREST_NEARESTMV pair.
        let skip_frame = [1i8, 5i8]; // LAST + BWDREF.
        let pred0 = assign_mv_pred_mv(&stack, MODE_NEAREST_NEARESTMV, 0, 0).unwrap();
        let pred1 = assign_mv_pred_mv(&stack, MODE_NEAREST_NEARESTMV, 1, 0).unwrap();
        let mv = [pred0, pred1];
        let ref_frame = [1i32, 5];
        let (au, al, asg, lsg, ai, li, arf, lrf) = empty_neighbour_inputs();

        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_inter_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            ref_frame,
            MODE_NEAREST_NEARESTMV,
            mv,
            0,
            &stack,
            BLOCK_8X8,
            /* skip_mode = */ 1,
            skip_frame,
            false,
            0,
            false,
            false,
            false,
            au,
            al,
            asg,
            lsg,
            ai,
            li,
            arf,
            lrf,
            false,
            true,
            &InterBlockModeInfoTail::bit_silent(),
        )
        .unwrap();
        let bytes = writer.finish();
        // No symbols emitted ⇒ an empty (or trivially-flushed) payload.
        // The decode mirror recovers the forced pair / mode with no reads.
        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let (rf, ym, rmi, dmv) = mirror_inter_block_mode_info(
            &bytes,
            &mut dec_cdfs,
            &stack,
            1,
            [1, 5],
            false,
            false,
            false,
            true,
        );
        assert_eq!(rf, ref_frame);
        assert_eq!(ym, MODE_NEAREST_NEARESTMV);
        assert_eq!(rmi, 0);
        assert_eq!(dmv[0], pred0);
        assert_eq!(dmv[1], pred1);
    }

    /// §5.11.23 arm-2 seg_globalmv round-trip: ref_frames = (LAST, NONE)
    /// (no bits), YMode = GLOBALMV (no mode bits), single GLOBALMV list
    /// ⇒ Mv = GlobalMvs[0], no MV bits.
    #[test]
    fn write_inter_block_mode_info_seg_globalmv_emits_no_bits() {
        let stack = mk_mv_stack(2);
        let mv = [stack.global_mvs[0], [0, 0]];
        let ref_frame = [1i32, -1];
        let (au, al, asg, lsg, ai, li, arf, lrf) = empty_neighbour_inputs();

        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_inter_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            ref_frame,
            MODE_GLOBALMV,
            mv,
            0,
            &stack,
            BLOCK_8X8,
            0,
            [0, 0],
            false,
            0,
            /* seg_skip_active = */ false,
            /* seg_globalmv_active = */ true,
            false,
            au,
            al,
            asg,
            lsg,
            ai,
            li,
            arf,
            lrf,
            false,
            true,
            &InterBlockModeInfoTail::bit_silent(),
        )
        .unwrap();
        let bytes = writer.finish();

        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let (rf, ym, _rmi, dmv) = mirror_inter_block_mode_info(
            &bytes,
            &mut dec_cdfs,
            &stack,
            0,
            [0, 0],
            false,
            true,
            false,
            true,
        );
        assert_eq!(rf, ref_frame);
        assert_eq!(ym, MODE_GLOBALMV);
        assert_eq!(dmv[0], stack.global_mvs[0]);
    }

    /// §5.11.23 arm-consistency reject: a `y_mode` that doesn't match the
    /// active arm (here arm 2 forces GLOBALMV, but NEWMV is handed) is a
    /// caller bug.
    #[test]
    fn write_inter_block_mode_info_rejects_arm_inconsistent_y_mode() {
        let stack = mk_mv_stack(2);
        let (au, al, asg, lsg, ai, li, arf, lrf) = empty_neighbour_inputs();
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_inter_block_mode_info(
            &mut writer,
            &mut enc_cdfs,
            [1, -1],
            MODE_NEWMV, // inconsistent with the seg_globalmv arm.
            [[0, 0], [0, 0]],
            0,
            &stack,
            BLOCK_8X8,
            0,
            [0, 0],
            false,
            0,
            false,
            true,
            false,
            au,
            al,
            asg,
            lsg,
            ai,
            li,
            arf,
            lrf,
            false,
            true,
            &InterBlockModeInfoTail::bit_silent(),
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.28 read_interintra_mode — write_interintra_mode round-trips
    // through PartitionWalker::read_interintra_mode (r276).
    // -----------------------------------------------------------------

    /// Zero-read readout shape (`interintra = 0`, every option `None`).
    fn ii_zero() -> InterIntraReadout {
        InterIntraReadout {
            interintra: 0,
            interintra_mode: None,
            wedge_interintra: None,
            wedge_index: None,
        }
    }

    /// Encode `readout` then decode it back through the §5.11.28
    /// reader on a fresh walker; returns the recovered readout.
    fn roundtrip_interintra(
        readout: InterIntraReadout,
        mi_size: usize,
        skip_mode: u8,
        is_compound: bool,
        enable: bool,
    ) -> InterIntraReadout {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_interintra_mode(
            &mut writer,
            &mut enc_cdfs,
            &readout,
            mi_size,
            skip_mode,
            is_compound,
            enable,
        )
        .unwrap();
        let bytes = writer.finish();

        let (walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), false).unwrap();
        let got = walker
            .read_interintra_mode(
                &mut dec,
                &mut dec_cdfs,
                mi_size,
                skip_mode,
                is_compound,
                enable,
            )
            .unwrap();
        // Both sides must have adapted the shared rows identically.
        if let Some(ctx) = crate::cdf::interintra_ctx(mi_size) {
            assert_eq!(enc_cdfs.inter_intra.get(ctx), dec_cdfs.inter_intra.get(ctx));
            assert_eq!(
                enc_cdfs.inter_intra_mode.get(ctx),
                dec_cdfs.inter_intra_mode.get(ctx)
            );
        }
        if mi_size < BLOCK_SIZES {
            assert_eq!(
                enc_cdfs.wedge_inter_intra[mi_size],
                dec_cdfs.wedge_inter_intra[mi_size]
            );
            assert_eq!(enc_cdfs.wedge_index[mi_size], dec_cdfs.wedge_index[mi_size]);
        }
        got
    }

    /// Every §5.11.28 gate-closing clause (skip_mode, sequence flag,
    /// compound, block-size band) writes no bits for the zero readout —
    /// the emitted stream equals an untouched writer's.
    #[test]
    fn write_interintra_mode_gate_closed_writes_no_bits() {
        // (mi_size, skip_mode, is_compound, enable)
        let configs = [
            (BLOCK_8X8, 1u8, false, true),
            (BLOCK_8X8, 0u8, false, false),
            (BLOCK_8X8, 0u8, true, true),
            (BLOCK_4X4, 0u8, false, true),
            (BLOCK_64X64, 0u8, false, true),
        ];
        for (mi_size, skip_mode, is_compound, enable) in configs {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_interintra_mode(
                &mut writer,
                &mut cdfs,
                &ii_zero(),
                mi_size,
                skip_mode,
                is_compound,
                enable,
            )
            .unwrap();
            let got = writer.finish();
            let want = SymbolWriter::new(false).finish();
            assert_eq!(got, want, "gate-closed config writes no symbols");
        }
    }

    /// Gate closed but the readout claims a coded shape — caller bug.
    #[test]
    fn write_interintra_mode_gate_closed_rejects_nonzero_readout() {
        let bad = InterIntraReadout {
            interintra: 1,
            interintra_mode: Some(0),
            wedge_interintra: Some(0),
            wedge_index: None,
        };
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_interintra_mode(&mut writer, &mut cdfs, &bad, BLOCK_8X8, 1, false, true)
            .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Gate open: `interintra = 0` plus all four inner-arm shapes
    /// (every `interintra_mode`, wedge off) round-trip at every band
    /// size.
    #[test]
    fn write_interintra_mode_round_trips_inner_arm_no_wedge() {
        for mi_size in [BLOCK_8X8, BLOCK_16X16, BLOCK_32X32] {
            let got = roundtrip_interintra(ii_zero(), mi_size, 0, false, true);
            assert_eq!(got, ii_zero(), "interintra = 0 at mi_size {mi_size}");
            for ii_mode in 0..INTERINTRA_MODES as u8 {
                let readout = InterIntraReadout {
                    interintra: 1,
                    interintra_mode: Some(ii_mode),
                    wedge_interintra: Some(0),
                    wedge_index: None,
                };
                let got = roundtrip_interintra(readout, mi_size, 0, false, true);
                assert_eq!(got, readout, "mode {ii_mode} at mi_size {mi_size}");
            }
        }
    }

    /// Gate open, wedge sub-branch: `wedge_index` extremes round-trip.
    #[test]
    fn write_interintra_mode_round_trips_wedge_indices() {
        for wi in [0u8, 7, 15] {
            let readout = InterIntraReadout {
                interintra: 1,
                interintra_mode: Some(1),
                wedge_interintra: Some(1),
                wedge_index: Some(wi),
            };
            let got = roundtrip_interintra(readout, BLOCK_8X8, 0, false, true);
            assert_eq!(got, readout, "wedge_index {wi}");
        }
    }

    /// Readout shapes the §5.11.28 reader can never produce on an open
    /// gate are rejected before any symbol decodes wrong.
    #[test]
    fn write_interintra_mode_rejects_inconsistent_readouts() {
        let cases = [
            // interintra out of the binary alphabet.
            InterIntraReadout {
                interintra: 2,
                interintra_mode: None,
                wedge_interintra: None,
                wedge_index: None,
            },
            // interintra = 0 with a populated inner field.
            InterIntraReadout {
                interintra: 0,
                interintra_mode: Some(0),
                wedge_interintra: None,
                wedge_index: None,
            },
            // interintra = 1 missing interintra_mode.
            InterIntraReadout {
                interintra: 1,
                interintra_mode: None,
                wedge_interintra: Some(0),
                wedge_index: None,
            },
            // interintra_mode out of range.
            InterIntraReadout {
                interintra: 1,
                interintra_mode: Some(INTERINTRA_MODES as u8),
                wedge_interintra: Some(0),
                wedge_index: None,
            },
            // wedge_interintra = 1 missing wedge_index.
            InterIntraReadout {
                interintra: 1,
                interintra_mode: Some(0),
                wedge_interintra: Some(1),
                wedge_index: None,
            },
            // wedge_interintra = 0 with a stray wedge_index.
            InterIntraReadout {
                interintra: 1,
                interintra_mode: Some(0),
                wedge_interintra: Some(0),
                wedge_index: Some(0),
            },
            // wedge_index out of range.
            InterIntraReadout {
                interintra: 1,
                interintra_mode: Some(0),
                wedge_interintra: Some(1),
                wedge_index: Some(WEDGE_TYPES as u8),
            },
        ];
        for bad in cases {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err =
                write_interintra_mode(&mut writer, &mut cdfs, &bad, BLOCK_8X8, 0, false, true)
                    .unwrap_err();
            assert!(
                matches!(err, Error::PartitionWalkOutOfRange),
                "rejected {bad:?}"
            );
        }
    }

    /// `interintra = 0` on an open gate emits exactly one symbol —
    /// byte-equal to an independent single-S() encode over the same row.
    #[test]
    fn write_interintra_mode_interintra_zero_emits_one_symbol() {
        let mut a = TileCdfContext::new_from_defaults();
        let mut wa = SymbolWriter::new(false);
        write_interintra_mode(&mut wa, &mut a, &ii_zero(), BLOCK_16X16, 0, false, true).unwrap();
        let got = wa.finish();

        let mut b = TileCdfContext::new_from_defaults();
        let mut wb = SymbolWriter::new(false);
        let ctx = crate::cdf::interintra_ctx(BLOCK_16X16).unwrap();
        let row = b.inter_intra_cdf(ctx).unwrap();
        wb.write_symbol(0, row).unwrap();
        let want = wb.finish();
        assert_eq!(got, want, "open gate + interintra = 0 is one S()");
    }

    // -----------------------------------------------------------------
    // §5.11.27 read_motion_mode — write_motion_mode round-trips through
    // PartitionWalker::read_motion_mode (r276).
    // -----------------------------------------------------------------

    /// Build a 16×16 mi walker with one stamped inter neighbour above
    /// `(2, 0)` so `has_overlappable_candidates()` is true for a
    /// BLOCK_8X8 block at that position (the same fixture the decode
    /// tests use).
    fn walker_with_overlappable_above() -> PartitionWalker {
        let geom = TileGeometry {
            mi_row_start: 0,
            mi_row_end: 16,
            mi_col_start: 0,
            mi_col_end: 16,
        };
        let mut walker = PartitionWalker::new(16, 16, geom).unwrap();
        walker.stamp_inter_neighbour(1, 0, BLOCK_8X8, [1, -1], MODE_NEARESTMV, [[0, 0], [0, 0]]);
        walker
    }

    /// Every §5.11.27 short-circuit arm forces SIMPLE with no bits;
    /// a non-SIMPLE target on those arms is a caller bug.
    #[test]
    fn write_motion_mode_short_circuit_arms_write_no_bits_and_reject_non_simple() {
        // (mi_size, skip_mode, is_compound, ref_frame, y_mode,
        //  switchable, force_integer_mv, gm_type_ref0, has_overlappable)
        struct Arm {
            mi_size: usize,
            skip_mode: u8,
            is_compound: bool,
            ref_frame: [i32; 2],
            y_mode: u8,
            switchable: bool,
            force_integer_mv: bool,
            gm_type_ref0: i32,
            has_overlappable: bool,
        }
        let base = Arm {
            mi_size: BLOCK_16X16,
            skip_mode: 0,
            is_compound: false,
            ref_frame: [1, -1],
            y_mode: MODE_NEARESTMV,
            switchable: true,
            force_integer_mv: false,
            gm_type_ref0: 0,
            has_overlappable: true,
        };
        let arms = [
            Arm {
                skip_mode: 1,
                ..base
            },
            Arm {
                switchable: false,
                ..base
            },
            // Min(Block_Width, Block_Height) < 8.
            Arm {
                mi_size: BLOCK_4X8,
                ..base
            },
            // GLOBALMV with a beyond-TRANSLATION global model.
            Arm {
                y_mode: MODE_GLOBALMV,
                gm_type_ref0: GM_TYPE_TRANSLATION + 1,
                ..base
            },
            Arm {
                is_compound: true,
                ref_frame: [1, 4],
                ..base
            },
            // RefFrame[1] == INTRA_FRAME = 0.
            Arm {
                ref_frame: [1, 0],
                ..base
            },
            Arm {
                has_overlappable: false,
                ..base
            },
        ];
        for (i, arm) in arms.iter().enumerate() {
            let mut gm_type = [0i32; 8];
            gm_type[arm.ref_frame[0] as usize] = arm.gm_type_ref0;
            let run = |motion_mode: u8| {
                let mut cdfs = TileCdfContext::new_from_defaults();
                let mut writer = SymbolWriter::new(false);
                let r = write_motion_mode(
                    &mut writer,
                    &mut cdfs,
                    motion_mode,
                    arm.mi_size,
                    arm.skip_mode,
                    arm.is_compound,
                    arm.ref_frame,
                    arm.y_mode,
                    0,
                    arm.switchable,
                    true,
                    arm.force_integer_mv,
                    gm_type,
                    [false; 7],
                    arm.has_overlappable,
                );
                (r, writer.finish())
            };
            let (ok, bytes) = run(MOTION_MODE_SIMPLE);
            ok.unwrap();
            assert_eq!(
                bytes,
                SymbolWriter::new(false).finish(),
                "short-circuit arm {i} writes no symbols"
            );
            let (err, _) = run(MOTION_MODE_OBMC);
            assert!(
                matches!(err.unwrap_err(), Error::PartitionWalkOutOfRange),
                "short-circuit arm {i} rejects OBMC"
            );
        }
    }

    /// Arm A (`use_obmc` S(), forced via `force_integer_mv = true`):
    /// SIMPLE and OBMC round-trip; WARPED_CAUSAL is unreachable.
    #[test]
    fn write_motion_mode_arm_a_round_trips() {
        for target in [MOTION_MODE_SIMPLE, MOTION_MODE_OBMC] {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_motion_mode(
                &mut writer,
                &mut enc_cdfs,
                target,
                BLOCK_8X8,
                0,
                false,
                [1, -1],
                MODE_NEARESTMV,
                0,
                true,
                true,
                /* force_integer_mv = */ true,
                [0; 8],
                [false; 7],
                true,
            )
            .unwrap();
            let bytes = writer.finish();

            let walker = walker_with_overlappable_above();
            let mut dec_cdfs = TileCdfContext::new_from_defaults();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            let got = walker
                .read_motion_mode(
                    &mut dec,
                    &mut dec_cdfs,
                    2,
                    0,
                    BLOCK_8X8,
                    0,
                    false,
                    [1, -1],
                    MODE_NEARESTMV,
                    0,
                    true,
                    true,
                    true,
                    [0; 8],
                    [false; 7],
                )
                .unwrap();
            assert_eq!(got, target, "arm A target {target} round-trips");
            assert_eq!(enc_cdfs.use_obmc[BLOCK_8X8], dec_cdfs.use_obmc[BLOCK_8X8]);
        }
        // WARPED_CAUSAL on arm A is a caller bug.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_motion_mode(
            &mut writer,
            &mut cdfs,
            crate::cdf::MOTION_MODE_WARPED_CAUSAL,
            BLOCK_8X8,
            0,
            false,
            [1, -1],
            MODE_NEARESTMV,
            0,
            true,
            true,
            true,
            [0; 8],
            [false; 7],
            true,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Arm B (`motion_mode` S(): `num_samples > 0`, warped motion
    /// allowed, unscaled, fractional MVs): all three ordinals
    /// round-trip.
    #[test]
    fn write_motion_mode_arm_b_round_trips() {
        for target in 0..MOTION_MODES as u8 {
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_motion_mode(
                &mut writer,
                &mut enc_cdfs,
                target,
                BLOCK_8X8,
                0,
                false,
                [1, -1],
                MODE_NEARESTMV,
                /* num_samples = */ 2,
                true,
                true,
                false,
                [0; 8],
                [false; 7],
                true,
            )
            .unwrap();
            let bytes = writer.finish();

            let walker = walker_with_overlappable_above();
            let mut dec_cdfs = TileCdfContext::new_from_defaults();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            let got = walker
                .read_motion_mode(
                    &mut dec,
                    &mut dec_cdfs,
                    2,
                    0,
                    BLOCK_8X8,
                    0,
                    false,
                    [1, -1],
                    MODE_NEARESTMV,
                    2,
                    true,
                    true,
                    false,
                    [0; 8],
                    [false; 7],
                )
                .unwrap();
            assert_eq!(got, target, "arm B target {target} round-trips");
            assert_eq!(
                enc_cdfs.motion_mode[BLOCK_8X8],
                dec_cdfs.motion_mode[BLOCK_8X8]
            );
        }
    }

    /// A scaled `RefFrame[ 0 ]` routes to arm A even with warp samples
    /// available — WARPED_CAUSAL must be rejected and the `use_obmc`
    /// row (not the `motion_mode` row) adapts.
    #[test]
    fn write_motion_mode_scaled_ref_routes_to_arm_a() {
        let mut scaled = [false; 7];
        scaled[0] = true; // LAST_FRAME.
        let mut cdfs = TileCdfContext::new_from_defaults();
        let use_obmc_before = cdfs.use_obmc[BLOCK_8X8];
        let motion_mode_before = cdfs.motion_mode[BLOCK_8X8];
        let mut writer = SymbolWriter::new(false);
        write_motion_mode(
            &mut writer,
            &mut cdfs,
            MOTION_MODE_OBMC,
            BLOCK_8X8,
            0,
            false,
            [1, -1],
            MODE_NEARESTMV,
            2,
            true,
            true,
            false,
            [0; 8],
            scaled,
            true,
        )
        .unwrap();
        assert_ne!(cdfs.use_obmc[BLOCK_8X8], use_obmc_before);
        assert_eq!(cdfs.motion_mode[BLOCK_8X8], motion_mode_before);

        let mut writer = SymbolWriter::new(false);
        let err = write_motion_mode(
            &mut writer,
            &mut cdfs,
            crate::cdf::MOTION_MODE_WARPED_CAUSAL,
            BLOCK_8X8,
            0,
            false,
            [1, -1],
            MODE_NEARESTMV,
            2,
            true,
            true,
            false,
            [0; 8],
            scaled,
            true,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // §5.11.29 read_compound_type — write_compound_type round-trips
    // through PartitionWalker::read_compound_type (r276).
    // -----------------------------------------------------------------

    /// The §5.11.29 line-1/2 pre-set readout with `compound_type = ty`.
    fn ct_preset(ty: u8) -> crate::cdf::CompoundTypeReadout {
        crate::cdf::CompoundTypeReadout {
            comp_group_idx: 0,
            compound_idx: 1,
            compound_type: ty,
            wedge_index: None,
            wedge_sign: None,
            mask_type: None,
        }
    }

    /// Encode `readout` then decode it back through the §5.11.29 reader
    /// on a fresh walker at `(2, 2)` with both neighbours available and
    /// compound-coded (`!AboveSingle` / `!LeftSingle` — the grid
    /// pre-fill identities 0 / 1 feed both ctx walks on both sides).
    #[allow(clippy::too_many_arguments)]
    fn roundtrip_compound_type(
        readout: crate::cdf::CompoundTypeReadout,
        mi_size: usize,
        skip_mode: u8,
        is_compound: bool,
        interintra: u8,
        wedge_interintra: u8,
        enable_masked_compound: bool,
        enable_jnt_comp: bool,
        dist_equal: bool,
    ) -> crate::cdf::CompoundTypeReadout {
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_compound_type(
            &mut writer,
            &mut enc_cdfs,
            &readout,
            mi_size,
            skip_mode,
            is_compound,
            interintra,
            wedge_interintra,
            enable_masked_compound,
            enable_jnt_comp,
            dist_equal,
            true,
            true,
            false,
            false,
            false,
            false,
            // Grid pre-fill identities for a fresh walker.
            0,
            0,
            1,
            1,
        )
        .unwrap();
        let bytes = writer.finish();

        let (walker, mut dec_cdfs) = fresh_walker_and_cdfs();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), false).unwrap();
        walker
            .read_compound_type(
                &mut dec,
                &mut dec_cdfs,
                2,
                2,
                mi_size,
                skip_mode,
                is_compound,
                interintra,
                wedge_interintra,
                enable_masked_compound,
                enable_jnt_comp,
                dist_equal,
                true,
                true,
                false,
                false,
                false,
                false,
            )
            .unwrap()
    }

    /// `skip_mode` short-circuit: the pre-set readout writes no bits;
    /// anything else on that arm is a caller bug.
    #[test]
    fn write_compound_type_skip_mode_writes_no_bits() {
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_compound_type(
            &mut writer,
            &mut cdfs,
            &ct_preset(COMPOUND_AVERAGE),
            BLOCK_8X8,
            1,
            true,
            0,
            0,
            true,
            true,
            false,
            false,
            false,
            true,
            true,
            false,
            false,
            0,
            0,
            1,
            1,
        )
        .unwrap();
        assert_eq!(writer.finish(), SymbolWriter::new(false).finish());

        let mut writer = SymbolWriter::new(false);
        let err = write_compound_type(
            &mut writer,
            &mut cdfs,
            &ct_preset(COMPOUND_DISTANCE),
            BLOCK_8X8,
            1,
            true,
            0,
            0,
            true,
            true,
            false,
            false,
            false,
            true,
            true,
            false,
            false,
            0,
            0,
            1,
            1,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Single-pred else-arm: the §5.11.28 outcome alone selects the
    /// type, no bits on any of the three shapes; a mismatched type is
    /// rejected.
    #[test]
    fn write_compound_type_single_pred_arms_write_no_bits() {
        // (interintra, wedge_interintra, expected type)
        let shapes = [
            (0u8, 0u8, COMPOUND_AVERAGE),
            (1, 0, COMPOUND_INTRA),
            (1, 1, COMPOUND_WEDGE),
        ];
        for (ii, wii, expected) in shapes {
            let got = roundtrip_compound_type(
                ct_preset(expected),
                BLOCK_8X8,
                0,
                false,
                ii,
                wii,
                true,
                true,
                false,
            );
            assert_eq!(got, ct_preset(expected), "shape ({ii}, {wii})");

            // Mismatched type — pick a type the arm can't produce.
            let bad = if expected == COMPOUND_AVERAGE {
                COMPOUND_INTRA
            } else {
                COMPOUND_AVERAGE
            };
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            let err = write_compound_type(
                &mut writer,
                &mut cdfs,
                &ct_preset(bad),
                BLOCK_8X8,
                0,
                false,
                ii,
                wii,
                true,
                true,
                false,
                true,
                true,
                false,
                false,
                false,
                false,
                0,
                0,
                1,
                1,
            )
            .unwrap_err();
            assert!(matches!(err, Error::PartitionWalkOutOfRange));
        }
    }

    /// Compound group-1 wedge path: `comp_group_idx = 1`,
    /// `compound_type = COMPOUND_WEDGE`, `wedge_index` S() +
    /// `wedge_sign` L(1) all round-trip.
    #[test]
    fn write_compound_type_group1_wedge_round_trips() {
        for (wi, ws) in [(0u8, 0u8), (5, 1), (15, 0)] {
            let readout = crate::cdf::CompoundTypeReadout {
                comp_group_idx: 1,
                compound_idx: 1,
                compound_type: COMPOUND_WEDGE,
                wedge_index: Some(wi),
                wedge_sign: Some(ws),
                mask_type: None,
            };
            let got = roundtrip_compound_type(readout, BLOCK_8X8, 0, true, 0, 0, true, true, false);
            assert_eq!(got, readout, "wedge ({wi}, {ws})");
        }
    }

    /// Compound group-1 diffwtd path (`Wedge_Bits > 0` so the type is
    /// S()-coded): `mask_type` L(1) round-trips both ways.
    #[test]
    fn write_compound_type_group1_diffwtd_round_trips() {
        for mt in [0u8, 1] {
            let readout = crate::cdf::CompoundTypeReadout {
                comp_group_idx: 1,
                compound_idx: 1,
                compound_type: COMPOUND_DIFFWTD,
                wedge_index: None,
                wedge_sign: None,
                mask_type: Some(mt),
            };
            let got = roundtrip_compound_type(readout, BLOCK_8X8, 0, true, 0, 0, true, true, false);
            assert_eq!(got, readout, "diffwtd mask_type {mt}");
        }
    }

    /// Compound group-1 with `Wedge_Bits[ MiSize ] == 0` (BLOCK_64X64):
    /// the type is forced to COMPOUND_DIFFWTD with no `compound_type`
    /// S(); claiming COMPOUND_WEDGE there is a caller bug.
    #[test]
    fn write_compound_type_group1_n_zero_forces_diffwtd() {
        let readout = crate::cdf::CompoundTypeReadout {
            comp_group_idx: 1,
            compound_idx: 1,
            compound_type: COMPOUND_DIFFWTD,
            wedge_index: None,
            wedge_sign: None,
            mask_type: Some(1),
        };
        let got = roundtrip_compound_type(readout, BLOCK_64X64, 0, true, 0, 0, true, true, false);
        assert_eq!(got, readout);

        let bad = crate::cdf::CompoundTypeReadout {
            compound_type: COMPOUND_WEDGE,
            wedge_index: Some(0),
            wedge_sign: Some(0),
            mask_type: None,
            ..readout
        };
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_compound_type(
            &mut writer,
            &mut cdfs,
            &bad,
            BLOCK_64X64,
            0,
            true,
            0,
            0,
            true,
            true,
            false,
            true,
            true,
            false,
            false,
            false,
            false,
            0,
            0,
            1,
            1,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    /// Compound group-0 jnt path: `compound_idx = 1 ⇒ AVERAGE` and
    /// `compound_idx = 0 ⇒ DISTANCE` round-trip under both `dist_equal`
    /// seeds (ctx 0 + 2 vs ctx 3 + 2).
    #[test]
    fn write_compound_type_group0_jnt_round_trips() {
        for dist_equal in [false, true] {
            for (ci, ty) in [(1u8, COMPOUND_AVERAGE), (0, COMPOUND_DISTANCE)] {
                let readout = crate::cdf::CompoundTypeReadout {
                    comp_group_idx: 0,
                    compound_idx: ci,
                    compound_type: ty,
                    wedge_index: None,
                    wedge_sign: None,
                    mask_type: None,
                };
                let got = roundtrip_compound_type(
                    readout, BLOCK_8X8, 0, true, 0, 0, true, true, dist_equal,
                );
                assert_eq!(got, readout, "jnt ({ci}, dist_equal {dist_equal})");
            }
        }
    }

    /// Compound group-0 without jnt: only the `comp_group_idx` S() is
    /// coded — byte-equal to an independent single-symbol encode.
    #[test]
    fn write_compound_type_group0_no_jnt_emits_one_symbol() {
        let mut a = TileCdfContext::new_from_defaults();
        let mut wa = SymbolWriter::new(false);
        write_compound_type(
            &mut wa,
            &mut a,
            &ct_preset(COMPOUND_AVERAGE),
            BLOCK_8X8,
            0,
            true,
            0,
            0,
            true,
            /* enable_jnt_comp = */ false,
            false,
            true,
            true,
            false,
            false,
            false,
            false,
            0,
            0,
            1,
            1,
        )
        .unwrap();
        let got = wa.finish();

        let mut b = TileCdfContext::new_from_defaults();
        let mut wb = SymbolWriter::new(false);
        let ctx = crate::cdf::comp_group_idx_ctx(true, false, 0, false, true, false, 0, false);
        let row = b.comp_group_idx_cdf(ctx);
        wb.write_symbol(0, row).unwrap();
        let want = wb.finish();
        assert_eq!(
            got, want,
            "group-0 no-jnt is exactly one comp_group_idx S()"
        );
    }

    /// Readout shapes the §5.11.29 reader can never produce are
    /// rejected.
    #[test]
    fn write_compound_type_rejects_inconsistent_readouts() {
        let run =
            |readout: &crate::cdf::CompoundTypeReadout, enable_masked: bool, enable_jnt: bool| {
                let mut cdfs = TileCdfContext::new_from_defaults();
                let mut writer = SymbolWriter::new(false);
                write_compound_type(
                    &mut writer,
                    &mut cdfs,
                    readout,
                    BLOCK_8X8,
                    0,
                    true,
                    0,
                    0,
                    enable_masked,
                    enable_jnt,
                    false,
                    true,
                    true,
                    false,
                    false,
                    false,
                    false,
                    0,
                    0,
                    1,
                    1,
                )
            };
        // comp_group_idx = 1 with masked compound disabled.
        let bad = crate::cdf::CompoundTypeReadout {
            comp_group_idx: 1,
            compound_idx: 1,
            compound_type: COMPOUND_DIFFWTD,
            wedge_index: None,
            wedge_sign: None,
            mask_type: Some(0),
        };
        assert!(run(&bad, false, true).is_err());
        // jnt arm with a type inconsistent with compound_idx.
        let bad = crate::cdf::CompoundTypeReadout {
            comp_group_idx: 0,
            compound_idx: 1,
            compound_type: COMPOUND_DISTANCE,
            wedge_index: None,
            wedge_sign: None,
            mask_type: None,
        };
        assert!(run(&bad, true, true).is_err());
        // wedge type missing its sub-branch fields.
        let bad = crate::cdf::CompoundTypeReadout {
            comp_group_idx: 1,
            compound_idx: 1,
            compound_type: COMPOUND_WEDGE,
            wedge_index: None,
            wedge_sign: None,
            mask_type: None,
        };
        assert!(run(&bad, true, true).is_err());
        // diffwtd type with stray wedge fields.
        let bad = crate::cdf::CompoundTypeReadout {
            comp_group_idx: 1,
            compound_idx: 1,
            compound_type: COMPOUND_DIFFWTD,
            wedge_index: Some(0),
            wedge_sign: None,
            mask_type: Some(0),
        };
        assert!(run(&bad, true, true).is_err());
        // wedge_index out of range.
        let bad = crate::cdf::CompoundTypeReadout {
            comp_group_idx: 1,
            compound_idx: 1,
            compound_type: COMPOUND_WEDGE,
            wedge_index: Some(WEDGE_TYPES as u8),
            wedge_sign: Some(0),
            mask_type: None,
        };
        assert!(run(&bad, true, true).is_err());
        // group-0 no-jnt arm with a non-preset compound_idx.
        let bad = crate::cdf::CompoundTypeReadout {
            comp_group_idx: 0,
            compound_idx: 0,
            compound_type: COMPOUND_AVERAGE,
            wedge_index: None,
            wedge_sign: None,
            mask_type: None,
        };
        assert!(run(&bad, true, false).is_err());
    }

    // -----------------------------------------------------------------
    // §5.11.x interpolation-filter loop — write_interpolation_filter
    // round-trips through PartitionWalker::read_interpolation_filter
    // (r277).
    // -----------------------------------------------------------------

    /// §5.9.24 identity warp parameters (diagonal `1 <<
    /// WARPEDMODEL_PREC_BITS = 1 << 16`) for the §7.10.2.1 global-mv
    /// setup in the dispatcher round-trip below.
    fn identity_gm_params() -> [[i32; 6]; 8] {
        let mut params = [[0i32; 6]; 8];
        for p in &mut params {
            p[2] = 1 << 16;
            p[5] = 1 << 16;
        }
        params
    }

    /// Encode `readout` then decode it back through the §5.11.x reader
    /// on a fresh walker at `(0, 0)` (no neighbours on either side);
    /// returns the recovered readout after asserting CDF lockstep.
    #[allow(clippy::too_many_arguments)]
    fn roundtrip_interp_filter(
        readout: InterpolationFilterReadout,
        mi_size: usize,
        skip_mode: u8,
        y_mode: u8,
        motion_mode: u8,
        interpolation_filter: u8,
        enable_dual_filter: bool,
        gm_type: [i32; 8],
    ) -> InterpolationFilterReadout {
        let ref_frame = [1i32, -1];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_interpolation_filter(
            &mut writer,
            &mut enc_cdfs,
            &readout,
            mi_size,
            skip_mode,
            /* is_compound = */ false,
            ref_frame,
            y_mode,
            motion_mode,
            interpolation_filter,
            enable_dual_filter,
            gm_type,
            /* avail_u = */ false,
            /* avail_l = */ false,
            [-1, -1],
            [-1, -1],
            [EIGHTTAP, EIGHTTAP],
            [EIGHTTAP, EIGHTTAP],
        )
        .unwrap();
        let bytes = writer.finish();
        let pad = if bytes.is_empty() { vec![0u8] } else { bytes };
        let mut dec = SymbolDecoder::init_symbol(&pad, pad.len(), false).unwrap();
        let geom = TileGeometry {
            mi_row_start: 0,
            mi_row_end: 8,
            mi_col_start: 0,
            mi_col_end: 8,
        };
        let walker = PartitionWalker::new(8, 8, geom).unwrap();
        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let got = walker
            .read_interpolation_filter(
                &mut dec,
                &mut dec_cdfs,
                0,
                0,
                mi_size,
                skip_mode,
                false,
                ref_frame,
                y_mode,
                motion_mode,
                interpolation_filter,
                enable_dual_filter,
                gm_type,
            )
            .unwrap();
        // Writer and reader adapted the same TileInterpFilterCdf rows.
        assert_eq!(enc_cdfs.interp_filter, dec_cdfs.interp_filter);
        got
    }

    /// SWITCHABLE + `needs_interp_filter() == 1` + dual filter: two
    /// independent S() symbols, one per direction, round-trip exactly.
    #[test]
    fn write_interpolation_filter_switchable_dual_round_trips() {
        let target = InterpolationFilterReadout {
            interp_filter: [crate::cdf::EIGHTTAP_SHARP, crate::cdf::EIGHTTAP_SMOOTH],
            read_from_bitstream: [true, true],
        };
        let got = roundtrip_interp_filter(
            target,
            BLOCK_16X16,
            0,
            MODE_NEARESTMV,
            MOTION_MODE_SIMPLE,
            SWITCHABLE,
            /* enable_dual_filter = */ true,
            [0; 8],
        );
        assert_eq!(got, target);
    }

    /// SWITCHABLE + needs + single-dir loop: one S(); slot 1 mirrors
    /// slot 0 with its read flag clear per the §5.11.x post-loop line.
    #[test]
    fn write_interpolation_filter_switchable_single_dir_round_trips() {
        let target = InterpolationFilterReadout {
            interp_filter: [crate::cdf::EIGHTTAP_SMOOTH, crate::cdf::EIGHTTAP_SMOOTH],
            read_from_bitstream: [true, false],
        };
        let got = roundtrip_interp_filter(
            target,
            BLOCK_8X8,
            0,
            MODE_NEARESTMV,
            MOTION_MODE_SIMPLE,
            SWITCHABLE,
            /* enable_dual_filter = */ false,
            [0; 8],
        );
        assert_eq!(got, target);
    }

    /// Non-SWITCHABLE frame header: both slots forced, zero bits; the
    /// reader recovers the same forced shape from an untouched stream.
    #[test]
    fn write_interpolation_filter_non_switchable_writes_no_bits() {
        let target = InterpolationFilterReadout {
            interp_filter: [crate::cdf::EIGHTTAP_SHARP, crate::cdf::EIGHTTAP_SHARP],
            read_from_bitstream: [false, false],
        };
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_interpolation_filter(
            &mut writer,
            &mut cdfs,
            &target,
            BLOCK_8X8,
            0,
            false,
            [1, -1],
            MODE_NEARESTMV,
            MOTION_MODE_SIMPLE,
            crate::cdf::EIGHTTAP_SHARP,
            true,
            [0; 8],
            false,
            false,
            [-1, -1],
            [-1, -1],
            [EIGHTTAP, EIGHTTAP],
            [EIGHTTAP, EIGHTTAP],
        )
        .unwrap();
        assert_eq!(writer.finish(), SymbolWriter::new(false).finish());
        let got = roundtrip_interp_filter(
            target,
            BLOCK_8X8,
            0,
            MODE_NEARESTMV,
            MOTION_MODE_SIMPLE,
            crate::cdf::EIGHTTAP_SHARP,
            true,
            [0; 8],
        );
        assert_eq!(got, target);
    }

    /// `needs_interp_filter() == 0` arms — skip_mode, LOCALWARP motion
    /// mode, and large-GLOBALMV-with-non-TRANSLATION gm — all force
    /// EIGHTTAP with zero bits; the GLOBALMV-with-TRANSLATION twin
    /// re-opens the S().
    #[test]
    fn write_interpolation_filter_needs_gate_arms() {
        let forced = InterpolationFilterReadout {
            interp_filter: [EIGHTTAP, EIGHTTAP],
            read_from_bitstream: [false, false],
        };
        // skip_mode.
        let got = roundtrip_interp_filter(
            forced,
            BLOCK_8X8,
            1,
            MODE_NEAREST_NEARESTMV,
            MOTION_MODE_SIMPLE,
            SWITCHABLE,
            false,
            [0; 8],
        );
        assert_eq!(got, forced);
        // motion_mode == LOCALWARP (WARPED_CAUSAL).
        let got = roundtrip_interp_filter(
            forced,
            BLOCK_16X16,
            0,
            MODE_NEARESTMV,
            MOTION_MODE_WARPED_CAUSAL,
            SWITCHABLE,
            true,
            [0; 8],
        );
        assert_eq!(got, forced);
        // large GLOBALMV with non-TRANSLATION gm_type.
        let mut gm = [0i32; 8];
        gm[1] = GM_TYPE_TRANSLATION + 1; // ROTZOOM
        let got = roundtrip_interp_filter(
            forced,
            BLOCK_16X16,
            0,
            MODE_GLOBALMV,
            MOTION_MODE_SIMPLE,
            SWITCHABLE,
            true,
            gm,
        );
        assert_eq!(got, forced);
        // … and with gm_type == TRANSLATION the gate re-opens (S()
        // fires).
        let mut gm = [0i32; 8];
        gm[1] = GM_TYPE_TRANSLATION;
        let coded = InterpolationFilterReadout {
            interp_filter: [crate::cdf::EIGHTTAP_SHARP, crate::cdf::EIGHTTAP_SHARP],
            read_from_bitstream: [true, false],
        };
        let got = roundtrip_interp_filter(
            coded,
            BLOCK_16X16,
            0,
            MODE_GLOBALMV,
            MOTION_MODE_SIMPLE,
            SWITCHABLE,
            false,
            gm,
        );
        assert_eq!(got, coded);
    }

    /// §8.3.2 neighbour ctx: a matching above neighbour moves the ctx
    /// off the `3` sentinel band. Hand-mirror the single S() with the
    /// exact ctx the writer must have used and verify both the symbol
    /// and that only that row adapted.
    #[test]
    fn write_interpolation_filter_neighbour_ctx_selection() {
        let target = InterpolationFilterReadout {
            interp_filter: [crate::cdf::EIGHTTAP_SMOOTH, crate::cdf::EIGHTTAP_SMOOTH],
            read_from_bitstream: [true, false],
        };
        let ref_frame = [1i32, -1];
        let mut enc_cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        write_interpolation_filter(
            &mut writer,
            &mut enc_cdfs,
            &target,
            BLOCK_8X8,
            0,
            false,
            ref_frame,
            MODE_NEARESTMV,
            MOTION_MODE_SIMPLE,
            SWITCHABLE,
            false,
            [0; 8],
            /* avail_u = */ true,
            /* avail_l = */ false,
            /* above_ref_frame = */ [1, -1], // matches RefFrame[0]
            [-1, -1],
            /* above_interp_filters = */
            [crate::cdf::EIGHTTAP_SHARP, crate::cdf::EIGHTTAP_SHARP],
            [EIGHTTAP, EIGHTTAP],
        )
        .unwrap();
        let bytes = writer.finish();

        // Hand mirror: aboveType = SHARP (match), leftType = NONE ⇒
        // ctx = (0 * 2 + 0) * 4 + SHARP = 2.
        let expected_ctx = interp_filter_ctx(
            crate::cdf::EIGHTTAP_SHARP as usize,
            INTERP_FILTER_NONE,
            0,
            false,
        )
        .unwrap();
        assert_eq!(expected_ctx, 2);
        let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
        let mut dec_cdfs = TileCdfContext::new_from_defaults();
        let sym = dec
            .read_symbol(dec_cdfs.interp_filter_cdf(expected_ctx).unwrap())
            .unwrap();
        assert_eq!(sym as u8, crate::cdf::EIGHTTAP_SMOOTH);
        assert_eq!(enc_cdfs.interp_filter, dec_cdfs.interp_filter);
        // A mismatched above reference falls back to the sentinel band:
        // same target re-encoded with above_ref_frame = GOLDEN must
        // consult ctx 3 instead (different row ⇒ different CDF state).
        let mut enc_cdfs2 = TileCdfContext::new_from_defaults();
        let mut writer2 = SymbolWriter::new(false);
        write_interpolation_filter(
            &mut writer2,
            &mut enc_cdfs2,
            &target,
            BLOCK_8X8,
            0,
            false,
            ref_frame,
            MODE_NEARESTMV,
            MOTION_MODE_SIMPLE,
            SWITCHABLE,
            false,
            [0; 8],
            true,
            false,
            [4, -1], // GOLDEN ≠ RefFrame[0] ⇒ sentinel
            [-1, -1],
            [crate::cdf::EIGHTTAP_SHARP, crate::cdf::EIGHTTAP_SHARP],
            [EIGHTTAP, EIGHTTAP],
        )
        .unwrap();
        let _ = writer2.finish();
        assert_ne!(
            enc_cdfs2.interp_filter[2], enc_cdfs.interp_filter[2],
            "ctx-2 row adapted only on the matching-neighbour encode"
        );
        let sentinel_ctx =
            interp_filter_ctx(INTERP_FILTER_NONE, INTERP_FILTER_NONE, 0, false).unwrap();
        assert_eq!(sentinel_ctx, 3);
        assert_ne!(
            enc_cdfs2.interp_filter[3],
            TileCdfContext::new_from_defaults().interp_filter[3],
            "ctx-3 row adapted on the mismatched-neighbour encode"
        );
    }

    /// Caller-bug rejects: shapes the §5.11.x reader could never
    /// produce on the given gate configuration.
    #[test]
    fn write_interpolation_filter_rejects_inconsistent_readouts() {
        let run = |readout: InterpolationFilterReadout,
                   skip_mode: u8,
                   interpolation_filter: u8,
                   enable_dual_filter: bool|
         -> Result<(), Error> {
            let mut cdfs = TileCdfContext::new_from_defaults();
            let mut writer = SymbolWriter::new(false);
            write_interpolation_filter(
                &mut writer,
                &mut cdfs,
                &readout,
                BLOCK_8X8,
                skip_mode,
                false,
                [1, -1],
                MODE_NEARESTMV,
                MOTION_MODE_SIMPLE,
                interpolation_filter,
                enable_dual_filter,
                [0; 8],
                false,
                false,
                [-1, -1],
                [-1, -1],
                [EIGHTTAP, EIGHTTAP],
                [EIGHTTAP, EIGHTTAP],
            )
        };
        let forced = |f: u8| InterpolationFilterReadout {
            interp_filter: [f, f],
            read_from_bitstream: [false, false],
        };
        // Non-SWITCHABLE with a slot off the frame-header value.
        assert!(run(forced(EIGHTTAP), 0, crate::cdf::EIGHTTAP_SHARP, false).is_err());
        // Non-SWITCHABLE with a stray read flag.
        assert!(run(
            InterpolationFilterReadout {
                interp_filter: [EIGHTTAP, EIGHTTAP],
                read_from_bitstream: [true, false],
            },
            0,
            EIGHTTAP,
            false,
        )
        .is_err());
        // SWITCHABLE + !needs (skip_mode) but a non-EIGHTTAP slot.
        assert!(run(forced(crate::cdf::EIGHTTAP_SMOOTH), 1, SWITCHABLE, false).is_err());
        // SWITCHABLE + !needs with a stray read flag.
        assert!(run(
            InterpolationFilterReadout {
                interp_filter: [EIGHTTAP, EIGHTTAP],
                read_from_bitstream: [true, false],
            },
            1,
            SWITCHABLE,
            false,
        )
        .is_err());
        // SWITCHABLE + needs but the read flag is clear.
        assert!(run(forced(crate::cdf::EIGHTTAP_SMOOTH), 0, SWITCHABLE, false).is_err());
        // BILINEAR is not codeable through the 3-way S().
        assert!(run(
            InterpolationFilterReadout {
                interp_filter: [BILINEAR, BILINEAR],
                read_from_bitstream: [true, false],
            },
            0,
            SWITCHABLE,
            false,
        )
        .is_err());
        // !enable_dual_filter mirror violation (slot 1 differs).
        assert!(run(
            InterpolationFilterReadout {
                interp_filter: [EIGHTTAP, crate::cdf::EIGHTTAP_SMOOTH],
                read_from_bitstream: [true, false],
            },
            0,
            SWITCHABLE,
            false,
        )
        .is_err());
        // !enable_dual_filter with a stray slot-1 read flag.
        assert!(run(
            InterpolationFilterReadout {
                interp_filter: [EIGHTTAP, EIGHTTAP],
                read_from_bitstream: [true, true],
            },
            0,
            SWITCHABLE,
            false,
        )
        .is_err());
        // interpolation_filter out of the §6.8.9 range.
        assert!(run(forced(EIGHTTAP), 0, SWITCHABLE + 1, false).is_err());
    }

    // -----------------------------------------------------------------
    // §5.11.23 composed tail (r277) — write_inter_block_mode_info
    // with the four tail leaves folded, round-tripped through the
    // decode-side dispatcher (which consumes the tail in the same
    // spec order as of the r277 ordering fix).
    // -----------------------------------------------------------------

    /// Per-block encode parameters shared by the three-block dispatcher
    /// round-trip below. Blocks sit at rows 0 / 2 / 4 of column 0 on an
    /// 8x8-mi walker, each BLOCK_8X8, each single-pred NEARESTMV on
    /// LAST_FRAME.
    ///
    /// * Block A — every tail leaf silent (gates shut except the
    ///   SWITCHABLE interp S()).
    /// * Block B — `interintra = 1` (wedge arm): four §5.11.28 S()s,
    ///   then the `RefFrame[ 1 ] = INTRA_FRAME` override SILENCES
    ///   §5.11.27 motion_mode even though `has_overlappable_candidates`
    ///   is true (block A sits above) — the §5.11.23 spec-order proof.
    /// * Block C — `interintra` gate open but 0 (one S()), then the
    ///   §5.11.27 arm-A `use_obmc` S() fires (OBMC), then the interp
    ///   S(). Genuine §5.11.28→§5.11.27 interleaving.
    const RT_REF: [i32; 2] = [1, -1];

    /// Tail for block A: fully silent. Block A is GLOBALMV on an empty
    /// stack (a fresh tile has no MV candidates for NEAREST/NEAR), and
    /// large-GLOBALMV with IDENTITY gm closes `needs_interp_filter()`
    /// — the SWITCHABLE loop forces EIGHTTAP with no S().
    fn rt_tail_a() -> InterBlockModeInfoTail {
        InterBlockModeInfoTail {
            interpolation_filter: SWITCHABLE,
            interp_filter: InterpolationFilterReadout {
                interp_filter: [EIGHTTAP, EIGHTTAP],
                read_from_bitstream: [false, false],
            },
            ..InterBlockModeInfoTail::bit_silent()
        }
    }

    /// Tail for block B: interintra wedge arm; motion silenced by the
    /// §5.11.28 slot-1 override despite the open motion gates.
    fn rt_tail_b(has_overlappable: bool) -> InterBlockModeInfoTail {
        InterBlockModeInfoTail {
            interintra: InterIntraReadout {
                interintra: 1,
                interintra_mode: Some(crate::cdf::II_SMOOTH_PRED),
                wedge_interintra: Some(1),
                wedge_index: Some(5),
            },
            enable_interintra_compound: true,
            is_motion_mode_switchable: true,
            allow_warped_motion: true,
            has_overlappable,
            compound_type: crate::cdf::CompoundTypeReadout {
                comp_group_idx: 0,
                compound_idx: 1,
                compound_type: COMPOUND_WEDGE,
                wedge_index: None,
                wedge_sign: None,
                mask_type: None,
            },
            interpolation_filter: SWITCHABLE,
            interp_filter: InterpolationFilterReadout {
                interp_filter: [crate::cdf::EIGHTTAP_SHARP, crate::cdf::EIGHTTAP_SHARP],
                read_from_bitstream: [true, false],
            },
            above_interp_filters: [EIGHTTAP, EIGHTTAP],
            ..InterBlockModeInfoTail::bit_silent()
        }
    }

    /// Tail for block C: interintra gate open / flag 0, §5.11.27 arm A
    /// (`!allow_warped_motion`) coding `use_obmc = 1`.
    fn rt_tail_c(has_overlappable: bool, num_samples: u32) -> InterBlockModeInfoTail {
        InterBlockModeInfoTail {
            enable_interintra_compound: true,
            motion_mode: MOTION_MODE_OBMC,
            num_samples,
            is_motion_mode_switchable: true,
            allow_warped_motion: false,
            has_overlappable,
            interpolation_filter: SWITCHABLE,
            interp_filter: InterpolationFilterReadout {
                interp_filter: [crate::cdf::EIGHTTAP_SMOOTH, crate::cdf::EIGHTTAP_SMOOTH],
                read_from_bitstream: [true, false],
            },
            above_interp_filters: [crate::cdf::EIGHTTAP_SHARP, crate::cdf::EIGHTTAP_SHARP],
            ..InterBlockModeInfoTail::bit_silent()
        }
    }

    /// Decode `n_blocks` of the three-block stream through the
    /// §5.11.23 dispatcher, returning the stamped walker plus the
    /// decoded aggregates.
    fn rt_decode_blocks(
        bytes: &[u8],
        n_blocks: usize,
    ) -> (PartitionWalker, Vec<crate::cdf::DecodedInterBlockModeInfo>) {
        let geom = TileGeometry {
            mi_row_start: 0,
            mi_row_end: 8,
            mi_col_start: 0,
            mi_col_end: 8,
        };
        let mut walker = PartitionWalker::new(8, 8, geom).unwrap();
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut dec = SymbolDecoder::init_symbol(bytes, bytes.len(), false).unwrap();
        let mfmvs = crate::cdf::MotionFieldMvs::new_invalid(8, 8);
        let mut infos = Vec::new();
        for blk in 0..n_blocks {
            let mi_row = (blk as u32) * 2;
            let avail_u = blk > 0;
            // Above-neighbour scalars: block B's slot 1 is overridden
            // to INTRA_FRAME by its §5.11.28 arm, so block C observes
            // [1, 0]; block B observes A's plain [1, -1].
            let above_ref_frame = if blk == 2 { [1, 0] } else { RT_REF };
            let (eic, imms, awm) = match blk {
                0 => (false, false, false),
                1 => (true, true, true),
                _ => (true, true, false),
            };
            let info = walker
                .decode_inter_block_mode_info(
                    &mut dec,
                    &mut cdfs,
                    mi_row,
                    0,
                    BLOCK_8X8,
                    /* skip_mode = */ 0,
                    avail_u,
                    /* avail_l = */ false,
                    above_ref_frame,
                    [-1, -1],
                    /* above_intra = */ false,
                    /* left_intra = */ false,
                    /* above_single = */ true,
                    /* left_single = */ true,
                    /* skip_mode_frame = */ [1, -1],
                    /* seg_ref_frame_active = */ false,
                    /* seg_ref_frame_data = */ 0,
                    /* seg_skip_active = */ false,
                    /* seg_globalmv_active = */ false,
                    /* reference_select = */ false,
                    [0; 8],
                    identity_gm_params(),
                    [0; 8],
                    /* allow_high_precision_mv = */ false,
                    /* force_integer_mv = */ false,
                    /* use_ref_frame_mvs = */ false,
                    imms,
                    awm,
                    [false; 7],
                    eic,
                    /* enable_masked_compound = */ false,
                    /* enable_jnt_comp = */ false,
                    /* order_hints = */ crate::cdf::FrameInterOrderHints::IDENTITY,
                    SWITCHABLE,
                    /* enable_dual_filter = */ false,
                    &mfmvs,
                )
                .unwrap();
            infos.push(info);
        }
        (walker, infos)
    }

    /// Encode the first `n_blocks` of the three-block stream with the
    /// composed writer. Per-block §7.10 inputs (mv stack, warp samples,
    /// overlappable flag) come from an encoder-side walker stamped by
    /// decoding the previously-encoded prefix — the same state the
    /// decode dispatcher rebuilds.
    fn rt_encode_blocks(n_blocks: usize) -> Vec<u8> {
        let geom = TileGeometry {
            mi_row_start: 0,
            mi_row_end: 8,
            mi_col_start: 0,
            mi_col_end: 8,
        };
        let mfmvs = crate::cdf::MotionFieldMvs::new_invalid(8, 8);
        let mut writer = SymbolWriter::new(false);
        let mut cdfs = TileCdfContext::new_from_defaults();
        for blk in 0..n_blocks {
            // Encoder-side mirror walker carrying the prefix stamps.
            let walker = if blk == 0 {
                PartitionWalker::new(8, 8, geom).unwrap()
            } else {
                rt_decode_blocks(&rt_encode_blocks(blk), blk).0
            };
            let mi_row = (blk as u32) * 2;
            let stack = walker
                .find_mv_stack(
                    mi_row,
                    0,
                    BLOCK_8X8,
                    RT_REF,
                    false,
                    false,
                    [0; 8],
                    identity_gm_params(),
                    [0; 8],
                    false,
                    false,
                    &mfmvs,
                )
                .unwrap();
            // Block A codes GLOBALMV (the fresh tile has an empty MV
            // stack, so the NEAREST family has no §5.11.31 source
            // slot); B and C code NEARESTMV off the stack the A / B
            // stamps populate.
            let y_mode = if blk == 0 {
                MODE_GLOBALMV
            } else {
                MODE_NEARESTMV
            };
            let mv0 = if blk == 0 {
                stack.global_mvs[0]
            } else {
                assert!(stack.num_mv_found > 0, "above stamps must feed the stack");
                assign_mv_pred_mv(&stack, MODE_NEARESTMV, 0, 0).unwrap()
            };
            let has_ov = walker.has_overlappable_candidates(mi_row, 0, BLOCK_8X8);
            let tail = match blk {
                0 => rt_tail_a(),
                1 => {
                    // The order proof: the motion gates ARE open
                    // (above block A is an overlappable inter
                    // neighbour) — only the §5.11.28 slot-1 override
                    // silences §5.11.27.
                    assert!(has_ov, "block A above must be overlappable");
                    rt_tail_b(has_ov)
                }
                _ => {
                    assert!(has_ov, "block B above must be overlappable");
                    let ns =
                        walker.find_warp_samples(mi_row, 0, BLOCK_8X8, RT_REF[0], [mv0, [0, 0]]);
                    rt_tail_c(has_ov, ns)
                }
            };
            let above_ref_frame = if blk == 2 { [1, 0] } else { RT_REF };
            write_inter_block_mode_info(
                &mut writer,
                &mut cdfs,
                RT_REF,
                y_mode,
                [mv0, [0, 0]],
                /* ref_mv_idx = */ 0,
                &stack,
                BLOCK_8X8,
                /* skip_mode = */ 0,
                [1, -1],
                false,
                0,
                false,
                false,
                /* reference_select = */ false,
                /* avail_u = */ blk > 0,
                /* avail_l = */ false,
                /* above_single = */ true,
                /* left_single = */ true,
                /* above_intra = */ false,
                /* left_intra = */ false,
                above_ref_frame,
                [-1, -1],
                /* force_integer_mv = */ false,
                /* allow_high_precision_mv = */ false,
                &tail,
            )
            .unwrap();
        }
        writer.finish()
    }

    /// Three-block composed-writer → decode-dispatcher round-trip.
    /// Proves (1) the r277 tail fold emits the §5.11.23 leaves in spec
    /// order (§5.11.28 → §5.11.27 → §5.11.29 → interp loop), (2) the
    /// §5.11.28 `RefFrame[ 1 ] = INTRA_FRAME` override silences the
    /// §5.11.27 writer/reader pair on interintra blocks even with
    /// overlappable neighbours, and (3) the dispatcher (post r277
    /// ordering fix) rebuilds every aggregate bit-exactly.
    #[test]
    fn write_inter_block_mode_info_tail_round_trips_through_dispatcher() {
        let bytes = rt_encode_blocks(3);
        let (walker, infos) = rt_decode_blocks(&bytes, 3);
        assert_eq!(infos.len(), 3);

        // Block A: silent tail; large-GLOBALMV + IDENTITY gm closes
        // `needs_interp_filter()` ⇒ forced EIGHTTAP under SWITCHABLE.
        assert_eq!(infos[0].ref_frame, RT_REF);
        assert_eq!(infos[0].y_mode, MODE_GLOBALMV);
        assert_eq!(infos[0].interintra.interintra, 0);
        assert_eq!(infos[0].motion_mode, MOTION_MODE_SIMPLE);
        assert_eq!(infos[0].compound_type.compound_type, COMPOUND_AVERAGE);
        assert_eq!(infos[0].interp_filter.interp_filter, [EIGHTTAP, EIGHTTAP]);
        assert_eq!(infos[0].interp_filter.read_from_bitstream, [false, false]);

        // Block B: the interintra wedge arm + the slot-1 override.
        assert_eq!(
            infos[1].ref_frame,
            [1, 0],
            "§5.11.28 RefFrame[1] = INTRA_FRAME override surfaces"
        );
        assert_eq!(infos[1].interintra.interintra, 1);
        assert_eq!(
            infos[1].interintra.interintra_mode,
            Some(crate::cdf::II_SMOOTH_PRED)
        );
        assert_eq!(infos[1].interintra.wedge_interintra, Some(1));
        assert_eq!(infos[1].interintra.wedge_index, Some(5));
        assert_eq!(
            infos[1].motion_mode, MOTION_MODE_SIMPLE,
            "slot-1 INTRA_FRAME silences §5.11.27 despite overlappable neighbours"
        );
        assert_eq!(infos[1].compound_type.compound_type, COMPOUND_WEDGE);
        assert_eq!(
            infos[1].interp_filter.interp_filter,
            [crate::cdf::EIGHTTAP_SHARP, crate::cdf::EIGHTTAP_SHARP]
        );

        // Block C: interintra-open/0 followed by the arm-A use_obmc S().
        assert_eq!(infos[2].ref_frame, RT_REF);
        assert_eq!(infos[2].interintra.interintra, 0);
        assert_eq!(infos[2].motion_mode, MOTION_MODE_OBMC);
        assert_eq!(infos[2].compound_type.compound_type, COMPOUND_AVERAGE);
        assert_eq!(
            infos[2].interp_filter.interp_filter,
            [crate::cdf::EIGHTTAP_SMOOTH, crate::cdf::EIGHTTAP_SMOOTH]
        );

        // Grid stamps: B's slot-1 override landed on the walker.
        let cell = (2 * 8) as usize; // (row 2, col 0)
        assert_eq!(walker.ref_frames()[cell * 2 + 1], 0);
    }

    /// Composed-writer tail consistency reject: a `motion_mode` the
    /// reader could never produce after the §5.11.28 override (OBMC on
    /// an interintra block) is a caller bug.
    #[test]
    fn write_inter_block_mode_info_tail_rejects_motion_on_interintra() {
        let stack = mk_mv_stack(2);
        let pred = assign_mv_pred_mv(&stack, MODE_NEARESTMV, 0, 0).unwrap();
        let (au, al, asg, lsg, ai, li, arf, lrf) = empty_neighbour_inputs();
        let mut tail = rt_tail_b(true);
        tail.motion_mode = MOTION_MODE_OBMC; // unreachable: slot-1 override forces SIMPLE
        let mut cdfs = TileCdfContext::new_from_defaults();
        let mut writer = SymbolWriter::new(false);
        let err = write_inter_block_mode_info(
            &mut writer,
            &mut cdfs,
            [1, -1],
            MODE_NEARESTMV,
            [pred, [0, 0]],
            0,
            &stack,
            BLOCK_8X8,
            0,
            [0, 0],
            false,
            0,
            false,
            false,
            false,
            au,
            al,
            asg,
            lsg,
            ai,
            li,
            arf,
            lrf,
            false,
            true,
            &tail,
        )
        .unwrap_err();
        assert!(matches!(err, Error::PartitionWalkOutOfRange));
    }

    // -----------------------------------------------------------------
    // r282 — §5.11.49 write_palette_tokens_plane round-trips.
    // -----------------------------------------------------------------

    /// Round-trip one §5.11.49 plane walk through
    /// [`crate::cdf::palette_tokens_plane`], including the
    /// frame-edge-straddling case (`onscreen < block`): the writer
    /// only consumes the on-screen samples and the reader's
    /// border-fill is derived, not coded.
    #[test]
    fn r282_write_palette_tokens_plane_round_trip_with_offscreen_border() {
        use crate::cdf::palette_tokens_plane;
        use crate::symbol_decoder::SymbolDecoder;

        let (block_w, block_h) = (8usize, 8usize);
        let (onscreen_w, onscreen_h) = (6usize, 5usize);
        let palette_size = 3usize;
        // Diagonal bands over {0, 1, 2} on the on-screen rectangle.
        let mut target = vec![0u8; block_w * block_h];
        for r in 0..onscreen_h {
            for c in 0..onscreen_w {
                target[r * block_w + c] = ((r + c) % palette_size) as u8;
            }
        }

        for plane in [PalettePlane::Y, PalettePlane::Uv] {
            let mut writer = SymbolWriter::new(false);
            let mut enc_cdfs = TileCdfContext::new_from_defaults();
            write_palette_tokens_plane(
                &mut writer,
                &mut enc_cdfs,
                plane,
                palette_size,
                block_w,
                block_h,
                onscreen_w,
                onscreen_h,
                &target,
                block_w,
            )
            .unwrap();
            writer.write_literal(8, 0x3C).unwrap();
            let bytes = writer.finish();

            let mut dec_cdfs = TileCdfContext::new_from_defaults();
            let mut dec = SymbolDecoder::init_symbol(&bytes, bytes.len(), false).unwrap();
            // §5.11.49: `color_index_map NS(PaletteSize)` first.
            let color_index_map = dec.read_ns(palette_size as u32).unwrap() as u8;
            assert_eq!(color_index_map, target[0], "NS top-left sample");
            let mut color_map = vec![0u8; block_w * block_h];
            palette_tokens_plane(
                &mut dec,
                &mut dec_cdfs,
                plane,
                palette_size,
                block_w,
                block_h,
                onscreen_w,
                onscreen_h,
                color_index_map,
                &mut color_map,
                block_w,
            )
            .unwrap();
            // On-screen reconstruction matches the committed map.
            for r in 0..onscreen_h {
                for c in 0..onscreen_w {
                    assert_eq!(
                        color_map[r * block_w + c],
                        target[r * block_w + c],
                        "on-screen ColorMap sample ({r}, {c})"
                    );
                }
            }
            // §5.11.49 border-fill: right edge replicates column
            // `onscreenWidth - 1`, bottom rows replicate row
            // `onscreenHeight - 1`.
            for r in 0..onscreen_h {
                for c in onscreen_w..block_w {
                    assert_eq!(
                        color_map[r * block_w + c],
                        color_map[r * block_w + onscreen_w - 1],
                        "horizontal border-fill ({r}, {c})"
                    );
                }
            }
            for r in onscreen_h..block_h {
                for c in 0..block_w {
                    assert_eq!(
                        color_map[r * block_w + c],
                        color_map[(onscreen_h - 1) * block_w + c],
                        "vertical border-fill ({r}, {c})"
                    );
                }
            }
            // §8.3 adaptation lockstep + sentinel.
            assert_eq!(enc_cdfs, dec_cdfs, "CDF adaptation lockstep");
            assert_eq!(dec.read_literal(8).unwrap(), 0x3C, "sync sentinel");
        }
    }

    /// Caller-bug guards on the §5.11.49 writer: out-of-range palette
    /// size, on-screen sample outside the palette alphabet, undersized
    /// map, zero on-screen dims.
    #[test]
    fn r282_write_palette_tokens_plane_rejects_caller_bugs() {
        let mut writer = SymbolWriter::new(true);
        let mut cdfs = TileCdfContext::new_from_defaults();
        let map = vec![0u8; 64];

        // palette_size outside 2..=PALETTE_COLORS.
        for bad_size in [0usize, 1, PALETTE_COLORS + 1] {
            let err = write_palette_tokens_plane(
                &mut writer,
                &mut cdfs,
                PalettePlane::Y,
                bad_size,
                8,
                8,
                8,
                8,
                &map,
                8,
            )
            .unwrap_err();
            assert!(matches!(err, Error::InvalidPaletteWalkArgs));
        }
        // On-screen sample >= palette_size.
        let mut bad_map = map.clone();
        bad_map[10] = 5;
        let err = write_palette_tokens_plane(
            &mut writer,
            &mut cdfs,
            PalettePlane::Y,
            2,
            8,
            8,
            8,
            8,
            &bad_map,
            8,
        )
        .unwrap_err();
        assert!(matches!(err, Error::InvalidPaletteWalkArgs));
        // Map shorter than block_height * stride.
        let err = write_palette_tokens_plane(
            &mut writer,
            &mut cdfs,
            PalettePlane::Y,
            2,
            8,
            8,
            8,
            8,
            &map[..32],
            8,
        )
        .unwrap_err();
        assert!(matches!(err, Error::InvalidPaletteWalkArgs));
        // Zero / oversize on-screen dims.
        for (ow, oh) in [(0usize, 8usize), (8, 0), (9, 8), (8, 9)] {
            let err = write_palette_tokens_plane(
                &mut writer,
                &mut cdfs,
                PalettePlane::Y,
                2,
                8,
                8,
                ow,
                oh,
                &map,
                8,
            )
            .unwrap_err();
            assert!(matches!(err, Error::InvalidPaletteWalkArgs));
        }
    }
}