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
/*
 * Copyright (c) Radzivon Bartoshyk 6/2026. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1.  Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2.  Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3.  Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
use crate::cdf::{CdfModeContext, CdfMvContext};
use crate::env::warp_type;
use crate::headers::{NSWienerPlane, RestorationType, WarpedMotionParams, WarpedMotionType};
use crate::internal::NsWienerBank;
use crate::intops::{iclip, imax, imin, inv_recenter};
use crate::levels::{BlockSize, INVALID_MV, Mv, MvXY, N_BS_SIZES, TxPartition};
use crate::lf_mask::Av2RestorationUnit;
use crate::msac::MsacReader;
use crate::pal::pal_idx_finish;

use crate::refmvs;
use crate::tables::{
    BLOCK_DIMENSIONS, DEFAULT_WM_PARAMS, NS_WIENER_COEF_RANGE_UV, NS_WIENER_COEF_RANGE_Y,
    SUBSET_MASKS_UV, SUBSET_MASKS_Y,
};
use crate::warpmv::{find_affine_int, get_shear_params, set_affine_mv2d};

pub(crate) static SIZE_GROUP: [u8; N_BS_SIZES] = {
    let mut t = [0u8; N_BS_SIZES];
    // group 0: 4x4, 4x8, 8x4, 4x16, 16x4
    t[BlockSize::Bs4x4 as usize] = 0;
    t[BlockSize::Bs4x8 as usize] = 0;
    t[BlockSize::Bs8x4 as usize] = 0;
    t[BlockSize::Bs4x16 as usize] = 0;
    t[BlockSize::Bs16x4 as usize] = 0;
    // group 1: 8x8, 8x16, 16x8, 8x32, 32x8, 4x32, 32x4
    t[BlockSize::Bs8x8 as usize] = 1;
    t[BlockSize::Bs8x16 as usize] = 1;
    t[BlockSize::Bs16x8 as usize] = 1;
    t[BlockSize::Bs8x32 as usize] = 1;
    t[BlockSize::Bs32x8 as usize] = 1;
    t[BlockSize::Bs4x32 as usize] = 1;
    t[BlockSize::Bs32x4 as usize] = 1;
    // group 2: 16x16, 16x32, 32x16, 16x64, 64x16, 8x64, 64x8, 4x64, 64x4
    t[BlockSize::Bs16x16 as usize] = 2;
    t[BlockSize::Bs16x32 as usize] = 2;
    t[BlockSize::Bs32x16 as usize] = 2;
    t[BlockSize::Bs16x64 as usize] = 2;
    t[BlockSize::Bs64x16 as usize] = 2;
    t[BlockSize::Bs8x64 as usize] = 2;
    t[BlockSize::Bs64x8 as usize] = 2;
    t[BlockSize::Bs4x64 as usize] = 2;
    t[BlockSize::Bs64x4 as usize] = 2;
    // group 3: 32x32+
    t[BlockSize::Bs32x32 as usize] = 3;
    t[BlockSize::Bs32x64 as usize] = 3;
    t[BlockSize::Bs64x32 as usize] = 3;
    t[BlockSize::Bs64x64 as usize] = 3;
    t[BlockSize::Bs64x128 as usize] = 3;
    t[BlockSize::Bs128x64 as usize] = 3;
    t[BlockSize::Bs128x128 as usize] = 3;
    t[BlockSize::Bs128x256 as usize] = 3;
    t[BlockSize::Bs256x128 as usize] = 3;
    t[BlockSize::Bs256x256 as usize] = 3;
    t
};

// TX partition size group per block size
pub(crate) static TX_PART_GROUP: [u8; N_BS_SIZES] = {
    let mut t = [0u8; N_BS_SIZES];
    t[BlockSize::Bs8x4 as usize] = 0;
    t[BlockSize::Bs4x8 as usize] = 0;
    t[BlockSize::Bs4x4 as usize] = 0;
    t[BlockSize::Bs8x8 as usize] = 1;
    t[BlockSize::Bs16x8 as usize] = 2;
    t[BlockSize::Bs8x16 as usize] = 2;
    t[BlockSize::Bs16x16 as usize] = 3;
    t[BlockSize::Bs32x16 as usize] = 4;
    t[BlockSize::Bs16x32 as usize] = 4;
    t[BlockSize::Bs32x32 as usize] = 5;
    t[BlockSize::Bs64x32 as usize] = 6;
    t[BlockSize::Bs32x64 as usize] = 6;
    t[BlockSize::Bs64x64 as usize] = 7;
    // extended sizes map to 8
    t[BlockSize::Bs64x16 as usize] = 8;
    t[BlockSize::Bs64x8 as usize] = 8;
    t[BlockSize::Bs64x4 as usize] = 8;
    t[BlockSize::Bs32x8 as usize] = 8;
    t[BlockSize::Bs32x4 as usize] = 8;
    t[BlockSize::Bs16x64 as usize] = 8;
    t[BlockSize::Bs16x4 as usize] = 8;
    t[BlockSize::Bs8x64 as usize] = 8;
    t[BlockSize::Bs8x32 as usize] = 8;
    t[BlockSize::Bs4x64 as usize] = 8;
    t[BlockSize::Bs4x32 as usize] = 8;
    t[BlockSize::Bs4x16 as usize] = 8;
    t
};

// TX type group for 2D V/H partition per block size
pub(crate) static TX_TYPE_GROUP_VH: [u8; N_BS_SIZES] = {
    let mut t = [0u8; N_BS_SIZES];
    t[BlockSize::Bs8x8 as usize] = 0;
    t[BlockSize::Bs8x16 as usize] = 1;
    t[BlockSize::Bs16x8 as usize] = 2;
    t[BlockSize::Bs16x16 as usize] = 3;
    t[BlockSize::Bs16x32 as usize] = 4;
    t[BlockSize::Bs32x16 as usize] = 5;
    t[BlockSize::Bs32x32 as usize] = 6;
    t[BlockSize::Bs32x64 as usize] = 7;
    t[BlockSize::Bs64x32 as usize] = 8;
    t[BlockSize::Bs64x64 as usize] = 9;
    t[BlockSize::Bs8x32 as usize] = 10;
    t[BlockSize::Bs8x64 as usize] = 10;
    t[BlockSize::Bs64x8 as usize] = 11;
    t[BlockSize::Bs32x8 as usize] = 11;
    t[BlockSize::Bs16x64 as usize] = 12;
    t[BlockSize::Bs64x16 as usize] = 13;
    t
};

pub(crate) fn jmvd_scale(mv: &mut MvXY, amvd: bool, jmvd_scale_mode: i32) {
    if amvd {
        match jmvd_scale_mode {
            0 => {}
            1 => {
                mv.y *= 2;
                mv.x *= 2;
            }
            2 => {
                mv.y /= 2;
                mv.x /= 2;
            }
            _ => unreachable!(),
        }
    } else {
        match jmvd_scale_mode {
            0 => {}
            1 => mv.y *= 2,
            2 => mv.x *= 2,
            3 => mv.y /= 2,
            4 => mv.x /= 2,
            _ => unreachable!(),
        }
    }
}

pub(crate) fn get_prev_frame_segid(
    by: i32,
    bx: i32,
    w4: i32,
    h4: i32,
    ref_seg_map: &[u8],
    stride: isize,
) -> u32 {
    let mut seg_id = 8u32;
    let mut off = (by as isize * stride + bx as isize) as usize;
    for _ in 0..h4 {
        for x in 0..w4 as usize {
            seg_id = imin(seg_id as i32, ref_seg_map[off + x] as i32) as u32;
        }
        if seg_id == 0 {
            break;
        }
        off = (off as isize + stride) as usize;
    }
    seg_id
}

/// Spatial-prediction of the current-frame segment id (`get_cur_frame_segid`,
/// class into `seg_ctx`.
pub(crate) fn get_cur_frame_segid(
    by: i32,
    bx: i32,
    have_top: bool,
    have_left: bool,
    seg_ctx: &mut i32,
    cur_seg_map: &[u8],
    stride: isize,
) -> u32 {
    let base = (bx as isize + by as isize * stride) as usize;
    if have_left && have_top {
        let l = cur_seg_map[base - 1] as i32;
        let a = cur_seg_map[(base as isize - stride) as usize] as i32;
        let al = cur_seg_map[(base as isize - (stride + 1)) as usize] as i32;
        if l == a && al == l {
            *seg_ctx = 2;
        } else if l == a || al == l || a == al {
            *seg_ctx = 1;
        } else {
            *seg_ctx = 0;
        }
        (if a == al { a } else { l }) as u32
    } else {
        *seg_ctx = 0;
        if have_left {
            cur_seg_map[base - 1] as u32
        } else if have_top {
            cur_seg_map[(base as isize - stride) as usize] as u32
        } else {
            0
        }
    }
}

pub(crate) static MV_PREC_TBL: [[u8; 3]; 2] = [[3, 1, 0], [4, 3, 1]];

use crate::levels::N_PARTITIONS;

// child partition split limits: [w_limit, h_limit]
pub(crate) static PARTITION_LIM: [[u8; 2]; N_PARTITIONS] = [
    [1, 1], // NONE
    [1, 2], // H
    [2, 1], // V
    [2, 4], // H3
    [4, 2], // V3
    [1, 8], // H4A
    [1, 8], // H4B
    [8, 1], // V4A
    [8, 1], // V4B
    [2, 2], // SPLIT
];

pub(crate) static WEDGE_ANGLE_DIST2IDX: [[i8; 4]; 20] = [
    [-1, 0, 1, 2],    // WEDGE_0
    [3, 4, 5, 6],     // WEDGE_14
    [7, 8, 9, 10],    // WEDGE_27
    [11, 12, 13, 14], // WEDGE_45
    [15, 16, 17, 18], // WEDGE_63
    [-1, 19, 20, 21], // WEDGE_90
    [22, 23, 24, 25], // WEDGE_117
    [26, 27, 28, 29], // WEDGE_135
    [30, 31, 32, 33], // WEDGE_153
    [34, 35, 36, 37], // WEDGE_166
    [-1, 38, 39, 40], // WEDGE_180
    [-1, 41, 42, 43], // WEDGE_194
    [-1, 44, 45, 46], // WEDGE_207
    [-1, 47, 48, 49], // WEDGE_225
    [-1, 50, 51, 52], // WEDGE_243
    [-1, 53, 54, 55], // WEDGE_270
    [-1, 56, 57, 58], // WEDGE_297
    [-1, 59, 60, 61], // WEDGE_315
    [-1, 62, 63, 64], // WEDGE_333
    [-1, 65, 66, 67], // WEDGE_346
];

#[derive(Clone, Copy)]
pub(crate) struct PartitionConstants {
    pub(crate) part: [[i8; 4]; 2],
    pub(crate) ctx: [i8; 2],
}

const I: i8 = -1; // BS_INVALID shorthand
use BlockSize::*;

pub(crate) static PARTITION_SUBB: [PartitionConstants; N_BS_SIZES] = {
    let mut t = [PartitionConstants {
        part: [[I; 4]; 2],
        ctx: [I; 2],
    }; N_BS_SIZES];

    t[Bs256x256 as usize] = PartitionConstants {
        part: [
            [Bs256x128 as i8, I, I, Bs128x128 as i8],
            [Bs128x256 as i8, I, I, Bs128x128 as i8],
        ],
        ctx: [9, 12],
    };
    t[Bs256x128 as usize] = PartitionConstants {
        part: [[I, I, I, I], [Bs128x128 as i8, I, I, I]],
        ctx: [8, I],
    };
    t[Bs128x256 as usize] = PartitionConstants {
        part: [[Bs128x128 as i8, I, I, I], [I, I, I, I]],
        ctx: [7, I],
    };
    t[Bs128x128 as usize] = PartitionConstants {
        part: [
            [Bs128x64 as i8, I, I, Bs64x64 as i8],
            [Bs64x128 as i8, I, I, Bs64x64 as i8],
        ],
        ctx: [6, 9],
    };
    t[Bs128x64 as usize] = PartitionConstants {
        part: [[I, I, I, I], [Bs64x64 as i8, I, I, I]],
        ctx: [5, I],
    };
    t[Bs64x128 as usize] = PartitionConstants {
        part: [[Bs64x64 as i8, I, I, I], [I, I, I, I]],
        ctx: [4, I],
    };
    t[Bs64x64 as usize] = PartitionConstants {
        part: [
            [Bs64x32 as i8, Bs64x16 as i8, Bs64x8 as i8, Bs32x32 as i8],
            [Bs32x64 as i8, Bs16x64 as i8, Bs8x64 as i8, Bs32x32 as i8],
        ],
        ctx: [3, 6],
    };
    t[Bs64x32 as usize] = PartitionConstants {
        part: [
            [Bs64x16 as i8, Bs64x8 as i8, Bs64x4 as i8, Bs32x16 as i8],
            [Bs32x32 as i8, Bs16x32 as i8, Bs8x32 as i8, Bs32x16 as i8],
        ],
        ctx: [3, 5],
    };
    t[Bs64x16 as usize] = PartitionConstants {
        part: [
            [Bs64x8 as i8, Bs64x4 as i8, I, Bs32x8 as i8],
            [Bs32x16 as i8, Bs16x16 as i8, Bs8x16 as i8, Bs32x8 as i8],
        ],
        ctx: [15, 14],
    };
    t[Bs64x8 as usize] = PartitionConstants {
        part: [[I, I, I, I], [I, I, I, I]],
        ctx: [0, 0],
    };
    t[Bs64x4 as usize] = PartitionConstants {
        part: [[I, I, I, I], [I, I, I, I]],
        ctx: [0, I],
    };
    t[Bs32x64 as usize] = PartitionConstants {
        part: [
            [Bs32x32 as i8, Bs32x16 as i8, Bs32x8 as i8, Bs16x32 as i8],
            [Bs16x64 as i8, Bs8x64 as i8, Bs4x64 as i8, Bs16x32 as i8],
        ],
        ctx: [3, 4],
    };
    t[Bs32x32 as usize] = PartitionConstants {
        part: [
            [Bs32x16 as i8, Bs32x8 as i8, Bs32x4 as i8, Bs16x16 as i8],
            [Bs16x32 as i8, Bs8x32 as i8, Bs4x32 as i8, Bs16x16 as i8],
        ],
        ctx: [2, 3],
    };
    t[Bs32x16 as usize] = PartitionConstants {
        part: [
            [Bs32x8 as i8, Bs32x4 as i8, I, Bs16x8 as i8],
            [Bs16x16 as i8, Bs8x16 as i8, Bs4x16 as i8, Bs16x8 as i8],
        ],
        ctx: [2, 2],
    };
    t[Bs32x8 as usize] = PartitionConstants {
        part: [
            [Bs32x4 as i8, I, I, I],
            [Bs16x8 as i8, Bs8x8 as i8, Bs4x8 as i8, Bs16x4 as i8],
        ],
        ctx: [13, 14],
    };
    t[Bs32x4 as usize] = PartitionConstants {
        part: [[I, I, I, I], [I, I, I, I]],
        ctx: [0, I],
    };
    t[Bs16x64 as usize] = PartitionConstants {
        part: [
            [Bs16x32 as i8, Bs16x16 as i8, Bs16x8 as i8, Bs8x32 as i8],
            [Bs8x64 as i8, Bs4x64 as i8, I, Bs8x32 as i8],
        ],
        ctx: [14, 13],
    };
    t[Bs16x32 as usize] = PartitionConstants {
        part: [
            [Bs16x16 as i8, Bs16x8 as i8, Bs16x4 as i8, Bs8x16 as i8],
            [Bs8x32 as i8, Bs4x32 as i8, I, Bs8x16 as i8],
        ],
        ctx: [2, 1],
    };
    t[Bs16x16 as usize] = PartitionConstants {
        part: [
            [Bs16x8 as i8, Bs16x4 as i8, I, Bs8x8 as i8],
            [Bs8x16 as i8, Bs4x16 as i8, I, Bs8x8 as i8],
        ],
        ctx: [1, 0],
    };
    t[Bs16x8 as usize] = PartitionConstants {
        part: [
            [Bs16x4 as i8, I, I, I],
            [Bs8x8 as i8, Bs4x8 as i8, I, Bs8x4 as i8],
        ],
        ctx: [1, 2],
    };
    t[Bs16x4 as usize] = PartitionConstants {
        part: [[I, I, I, I], [Bs8x4 as i8, I, I, I]],
        ctx: [11, I],
    };
    t[Bs8x64 as usize] = PartitionConstants {
        part: [[I, I, I, I], [I, I, I, I]],
        ctx: [0, 0],
    };
    t[Bs8x32 as usize] = PartitionConstants {
        part: [
            [Bs8x16 as i8, Bs8x8 as i8, Bs8x4 as i8, Bs4x16 as i8],
            [Bs4x32 as i8, I, I, I],
        ],
        ctx: [12, 13],
    };
    t[Bs8x16 as usize] = PartitionConstants {
        part: [
            [Bs8x8 as i8, Bs8x4 as i8, I, Bs4x8 as i8],
            [Bs4x16 as i8, I, I, I],
        ],
        ctx: [1, 1],
    };
    t[Bs8x8 as usize] = PartitionConstants {
        part: [[Bs8x4 as i8, I, I, I], [Bs4x8 as i8, I, I, I]],
        ctx: [0, 0],
    };
    t[Bs8x4 as usize] = PartitionConstants {
        part: [[I, I, I, I], [Bs4x4 as i8, I, I, I]],
        ctx: [0, I],
    };
    t[Bs4x64 as usize] = PartitionConstants {
        part: [[I, I, I, I], [I, I, I, I]],
        ctx: [0, I],
    };
    t[Bs4x32 as usize] = PartitionConstants {
        part: [[I, I, I, I], [I, I, I, I]],
        ctx: [0, I],
    };
    t[Bs4x16 as usize] = PartitionConstants {
        part: [[Bs4x8 as i8, I, I, I], [I, I, I, I]],
        ctx: [10, I],
    };
    t[Bs4x8 as usize] = PartitionConstants {
        part: [[Bs4x4 as i8, I, I, I], [I, I, I, I]],
        ctx: [0, I],
    };
    t[Bs4x4 as usize] = PartitionConstants {
        part: [[I, I, I, I], [I, I, I, I]],
        ctx: [I, I],
    };
    t
};

// indexed by inter_mode - CompInterPredMode::NearMvNewMv

pub(crate) fn read_wedge_idx<const UPDATE_CDF: bool, M: MsacReader<UPDATE_CDF>>(
    msac: &mut M,
    cdf_m: &mut CdfModeContext,
) -> i8 {
    let quad = msac.decode_symbol_adapt_n_padded::<3, 4>(cdf_m.wedge_quad()) as usize;
    let angle =
        5 * quad + msac.decode_symbol_adapt_n_padded::<4, 8>(cdf_m.wedge_angle(quad)) as usize;
    let dist = if (angle.wrapping_sub(1)) >= 9 || angle == 5 {
        1 + msac.decode_symbol_adapt_n_padded::<2, 4>(cdf_m.wedge_dist2()) as usize
    } else {
        msac.decode_symbol_adapt_n_padded::<3, 4>(cdf_m.wedge_dist()) as usize
    };
    WEDGE_ANGLE_DIST2IDX[angle][dist]
}

pub(crate) fn decode_4way<const UPDATE_CDF: bool, M: MsacReader<UPDATE_CDF>>(
    msac: &mut M,
    r: i32,
    cdf: &mut [u16],
    n_bits: i32,
) -> i32 {
    debug_assert!(n_bits >= 4);
    let bin = msac.decode_symbol_adapt(cdf, 3) as i32;
    let rem =
        msac.decode_bools_bypass((n_bits + bin + if bin == 0 { 1 } else { 0 } - 4) as u32) as i32;
    let v = (if bin != 0 { 1 << (n_bits + bin - 4) } else { 0 }) + rem;
    let n = 1 << n_bits;
    if r * 2 <= n {
        inv_recenter(r as u32, v as u32) as i32
    } else {
        n - 1 - inv_recenter((n - 1 - r) as u32, v as u32) as i32
    }
}

pub(crate) fn read_mv_residual<const UPDATE_CDF: bool, M: MsacReader<UPDATE_CDF>>(
    msac: &mut M,
    cdf_mv: &mut CdfMvContext,
    shell_tip: &mut [u16],
    mv_prec: i32,
) -> Mv {
    let n_syms = 9 + mv_prec;
    let h_syms = n_syms >> 1;

    let mut sh_class;
    if msac.decode_bool_adapt(cdf_mv.shell_set()) != 0 {
        let h_syms2 = n_syms - h_syms;
        sh_class = h_syms
            + 1
            + msac.decode_symbol_adapt_padded::<8>(
                cdf_mv.shell_upper(mv_prec as usize),
                imin(h_syms2, 7) as usize,
            ) as i32;
        if mv_prec + sh_class == 21 {
            sh_class += msac.decode_bool_adapt(shell_tip) as i32;
        }
    } else {
        sh_class = msac
            .decode_symbol_adapt_padded::<8>(cdf_mv.shell_lower(mv_prec as usize), h_syms as usize)
            as i32;
    }

    let mut sh_index;
    if sh_class < 2 {
        sh_index = msac.decode_bool_adapt(cdf_mv.shell_offset_low(sh_class as usize)) as i32;
    } else if sh_class == 2 {
        sh_index = msac.decode_bool_adapt(cdf_mv.shell_offset_cl2()) as i32;
        if sh_index != 0 {
            sh_index += msac.decode_bool_bypass() as i32;
            if sh_index == 2 {
                sh_index += msac.decode_bool_bypass() as i32;
            }
        }
    } else {
        sh_index = 0;
        let mut m = 1i32;
        for i in 0..sh_class {
            sh_index |= m * msac.decode_bool_adapt(cdf_mv.shell_offset_hi(i as usize)) as i32;
            m <<= 1;
        }
    }

    if sh_class != 0 {
        sh_index += 1 << sh_class;
    }
    if sh_index == 0 {
        return Mv::from_bits(0);
    }

    let mut pair_index = 0i32;
    if sh_index >= 2 {
        pair_index = msac.decode_bool_adapt(cdf_mv.col_component(0)) as i32;
        if pair_index != 0 && sh_index >= 4 {
            pair_index += msac.decode_bool_adapt(cdf_mv.col_component(1)) as i32;
            if pair_index == 2 && sh_index >= 6 {
                pair_index += msac.decode_uniform((sh_index as u32 >> 1) - 1) as i32;
            }
        }
    }

    let sh = 6 - mv_prec;
    if pair_index * 2 == sh_index {
        let v = (sh_index >> 1) << sh;
        Mv {
            c: MvXY { y: v, x: v },
        }
    } else {
        let b = msac.decode_bool_adapt(cdf_mv.col_index(imin(sh_class, 3) as usize));
        if b != 0 {
            Mv {
                c: MvXY {
                    y: pair_index << sh,
                    x: (sh_index - pair_index) << sh,
                },
            }
        } else {
            Mv {
                c: MvXY {
                    x: pair_index << sh,
                    y: (sh_index - pair_index) << sh,
                },
            }
        }
    }
}

pub(crate) fn read_mv_full<const UPDATE_CDF: bool, M: MsacReader<UPDATE_CDF>>(
    msac: &mut M,
    cdf_mv: &mut CdfMvContext,
    mv_prec: i32,
) -> Mv {
    let mut shell_tip = [cdf_mv.data[114], cdf_mv.data[115]];
    let mv = read_mv_residual(msac, cdf_mv, &mut shell_tip, mv_prec);
    cdf_mv.data[114] = shell_tip[0];
    cdf_mv.data[115] = shell_tip[1];
    mv
}

pub(crate) fn read_amvd<const UPDATE_CDF: bool, M: MsacReader<UPDATE_CDF>>(
    msac: &mut M,
    cdf_m: &mut CdfModeContext,
) -> Mv {
    let joint = msac.decode_symbol_adapt_n_padded::<3, 4>(cdf_m.amvd_joint()) as i32;
    if joint == 0 {
        return Mv::default();
    }
    let y = if joint & 2 != 0 {
        let s = msac.decode_symbol_adapt_n_padded::<7, 8>(cdf_m.amvd_index(0)) as i32;
        if s < 3 { 2 + s * 2 } else { 1 << s }
    } else {
        0
    };
    let x = if joint & 1 != 0 {
        let s = msac.decode_symbol_adapt_n_padded::<7, 8>(cdf_m.amvd_index(1)) as i32;
        if s < 3 { 2 + s * 2 } else { 1 << s }
    } else {
        0
    };
    Mv::from_xy(y, x)
}

#[inline]
fn read_pal_direction(sz: &[i32; 4], read_bit: impl FnOnce() -> u32) -> u32 {
    if imax(sz[2], sz[3]) < 64 {
        read_bit()
    } else {
        0
    }
}

pub(crate) fn read_pal_indices<const UPDATE_CDF: bool, M: MsacReader<UPDATE_CDF>>(
    msac: &mut M,
    cdf_m: &mut CdfModeContext,
    pal_out: &mut [u8],
    scratch: &mut [u8],
    pal_sz: i32,
    sz: &[i32; 4],
) -> i32 {
    let dir = read_pal_direction(sz, || msac.decode_bool_bypass());
    let strides: [isize; 2] = if dir != 0 {
        [1, sz[2] as isize]
    } else {
        [sz[2] as isize, 1]
    };

    let lim1 = sz[dir as usize ^ 1] as usize;
    let lim2 = sz[dir as usize] as usize;
    let pal_cdf_base = (pal_sz - 2) as usize;
    let nsym = (pal_sz - 1) as usize;

    let mut copy = msac.decode_symbol_adapt_n_padded::<2, 4>(cdf_m.pal_idx_identity(3)) as i32;
    if copy == 2 {
        return -1;
    }
    let mut prev_v = msac.decode_uniform(pal_sz as u32) as i32;
    scratch[0] = prev_v as u8;
    if copy == 1 {
        for m in 1..lim2 {
            scratch[(m as isize * strides[1]) as usize] = prev_v as u8;
        }
    } else {
        let mut prev_h = prev_v;
        for m in 1..lim2 {
            let v =
                msac.decode_symbol_adapt_padded::<8>(cdf_m.pal_idx(pal_cdf_base, 0), nsym) as i32;
            prev_h = if v == 0 {
                prev_h
            } else {
                v - (v <= prev_h) as i32
            };
            scratch[(m as isize * strides[1]) as usize] = prev_h as u8;
        }
    }

    let mut off: isize = strides[0];
    for _n in 1..lim1 {
        copy =
            msac.decode_symbol_adapt_n_padded::<2, 4>(cdf_m.pal_idx_identity(copy as usize)) as i32;
        if copy == 2 {
            for m in 0..lim2 {
                let dst = (off + m as isize * strides[1]) as usize;
                let src = (off - strides[0] + m as isize * strides[1]) as usize;
                scratch[dst] = scratch[src];
            }
        } else {
            let v =
                msac.decode_symbol_adapt_padded::<8>(cdf_m.pal_idx(pal_cdf_base, 0), nsym) as i32;
            let next_v = if v == 0 {
                prev_v
            } else {
                v - (v <= prev_v) as i32
            };
            scratch[off as usize] = next_v as u8;

            if copy == 1 {
                for m in 1..lim2 {
                    scratch[(off + m as isize * strides[1]) as usize] = next_v as u8;
                }
            } else {
                let mut prev_tl = prev_v;
                let mut prev_l = next_v;
                for m in 1..lim2 {
                    let prev_t =
                        scratch[(off - strides[0] + m as isize * strides[1]) as usize] as i32;
                    let ctx = if prev_t == prev_l {
                        3 + (prev_tl == prev_l) as usize
                    } else {
                        1 + (prev_t == prev_tl || prev_l == prev_tl) as usize
                    };
                    let v = msac
                        .decode_symbol_adapt_padded::<8>(cdf_m.pal_idx(pal_cdf_base, ctx), nsym)
                        as i32;
                    let p = match ctx {
                        1 => match v {
                            0 | 1 => {
                                if v == dir as i32 {
                                    prev_l
                                } else {
                                    prev_t
                                }
                            }
                            2 => prev_tl,
                            _ => {
                                let s1 = (prev_l < prev_t) as i32;
                                let s2 = (prev_l < prev_tl) as i32;
                                let s3 = (prev_t < prev_tl) as i32;
                                v - (v <= prev_l + s1 + s2) as i32
                                    - (v <= prev_t + s3 + 1 - s1) as i32
                                    - (v <= prev_tl + 1 - s2 + 1 - s3) as i32
                            }
                        },
                        2 => {
                            let prev_l_or_t = prev_l + prev_t - prev_tl;
                            match v {
                                0 => prev_tl,
                                1 => prev_l_or_t,
                                _ => {
                                    let s = (prev_l_or_t < prev_tl) as i32;
                                    v - (v <= prev_l_or_t + s) as i32
                                        - (v <= prev_tl + 1 - s) as i32
                                }
                            }
                        }
                        3 => match v {
                            0 => prev_l,
                            1 => prev_tl,
                            _ => {
                                let s = (prev_l < prev_tl) as i32;
                                v - (v <= prev_l + s) as i32 - (v <= prev_tl + 1 - s) as i32
                            }
                        },
                        4 => {
                            if v == 0 {
                                prev_l
                            } else {
                                v - (v <= prev_l) as i32
                            }
                        }
                        _ => unreachable!(),
                    };
                    scratch[(off + m as isize * strides[1]) as usize] = p as u8;
                    prev_l = p;
                    prev_tl = prev_t;
                }
            }
            prev_v = next_v;
        }
        off += strides[0];
    }

    pal_idx_finish(
        pal_out,
        scratch,
        sz[2] as usize,
        sz[3] as usize,
        sz[0] as usize,
        sz[1] as usize,
    );
    0
}

/// Read a luma palette color list from the bitstream.
///
/// reused-cache mask over the above/left neighbour palettes, then any new entries,
/// and merges them into a sorted palette in `pal` (8 entries). Returns `pal_sz`.
///
/// - `a_pal`/`l_pal`: the above / left palette caches (`t->al_pal[0][bx4]` /
///   `[1][by4]`), 8 entries each.
/// - `a_cache`/`l_cache`: number of valid cache entries above / left
///   (`t->a->pal_sz[bx4]` gated by `by4 & 15`, and `t->l.pal_sz[by4]`).
#[allow(clippy::too_many_arguments)]
pub(crate) fn read_pal_plane<const UPDATE_CDF: bool, M: MsacReader<UPDATE_CDF>>(
    msac: &mut M,
    cdf_m: &mut CdfModeContext,
    pal: &mut [u16; 8],
    a_pal: &[u16; 8],
    l_pal: &[u16; 8],
    a_cache: i32,
    l_cache: i32,
    bpc: u32,
) -> u8 {
    let pal_sz = msac.decode_symbol_adapt_n_padded::<6, 8>(cdf_m.pal_sz()) as i32 + 2;

    // find cached entries (but don't load them yet)
    let n_cache = l_cache + a_cache;
    let mut n_used_cache = 0i32;
    let mut cache_reuse_mask: u32 = 0;
    let mut off = 0i32;
    {
        let mut n = imin(n_cache, pal_sz);
        while n != 0 {
            let m = msac.decode_bools_bypass(n as u32);
            cache_reuse_mask <<= n;
            cache_reuse_mask |= m;
            n_used_cache += m.count_ones() as i32;
            off += n;
            n = imin(n_cache - off, pal_sz - n_used_cache);
        }
    }

    let mut cache = [0u16; 8];
    if n_used_cache != 0 {
        // `select`: directly copy the selected cache entries from `dir` into cache[].
        let select = |dir: &[u16; 8], cache: &mut [u16; 8]| {
            let mut mask = cache_reuse_mask << (32 - off);
            let mut i = 0usize;
            let mut n = 0u32;
            loop {
                let n_zero = mask.leading_zeros();
                cache[i] = dir[(n + n_zero) as usize];
                i += 1;
                n += n_zero + 1;
                mask <<= n_zero + 1;
                if mask == 0 {
                    break;
                }
            }
        };
        if l_cache == 0 {
            select(a_pal, &mut cache);
        } else if a_cache == 0 {
            select(l_pal, &mut cache);
        } else {
            // sort selected cache entries from a & l into cache[]
            let min_n = imin(a_cache, l_cache);
            let mask0 = cache_reuse_mask << (32 - off);
            let rem_mask = (mask0 << (min_n * 2)) >> min_n;
            let mut shared_mask = mask0.wrapping_sub(rem_mask >> min_n);
            shared_mask = (shared_mask & 0xaaaa_0000) | ((shared_mask & 0x5555_0000) >> 15);
            shared_mask |= shared_mask << 1;
            shared_mask &= 0xcccc_cccc;
            shared_mask |= shared_mask << 2;
            shared_mask &= 0xf0f0_f0f0;
            shared_mask |= shared_mask << 4;
            let a_gt_l = (a_cache > l_cache) as u32;
            let mut a_mask =
                (shared_mask & 0xff00_0000).wrapping_add(a_gt_l.wrapping_mul(rem_mask));
            let mut l_mask =
                ((shared_mask & 0xff00) << 16).wrapping_add((1 - a_gt_l).wrapping_mul(rem_mask));

            let mut i = 0usize;
            let mut a_n = 0u32;
            let mut l_n = 0u32;
            if a_mask != 0 && l_mask != 0 {
                a_n += a_mask.leading_zeros();
                a_mask <<= a_mask.leading_zeros();
                l_n += l_mask.leading_zeros();
                l_mask <<= l_mask.leading_zeros();
                loop {
                    if a_pal[a_n as usize] < l_pal[l_n as usize] {
                        cache[i] = a_pal[a_n as usize];
                        i += 1;
                        a_mask <<= 1;
                        if a_mask == 0 {
                            break;
                        }
                        let nz = a_mask.leading_zeros();
                        a_n += 1 + nz;
                        a_mask <<= nz;
                    } else {
                        cache[i] = l_pal[l_n as usize];
                        i += 1;
                        l_mask <<= 1;
                        if l_mask == 0 {
                            break;
                        }
                        let nz = l_mask.leading_zeros();
                        l_n += 1 + nz;
                        l_mask <<= nz;
                    }
                }
            }
            if a_mask != 0 {
                a_n += a_mask.leading_zeros();
                a_mask <<= a_mask.leading_zeros();
                loop {
                    cache[i] = a_pal[a_n as usize];
                    i += 1;
                    a_mask <<= 1;
                    if a_mask == 0 {
                        break;
                    }
                    let nz = a_mask.leading_zeros();
                    a_n += 1 + nz;
                    a_mask <<= nz;
                }
            } else {
                l_n += l_mask.leading_zeros();
                l_mask <<= l_mask.leading_zeros();
                loop {
                    cache[i] = l_pal[l_n as usize];
                    i += 1;
                    l_mask <<= 1;
                    if l_mask == 0 {
                        break;
                    }
                    let nz = l_mask.leading_zeros();
                    l_n += 1 + nz;
                    l_mask <<= nz;
                }
            }
        }
    }

    // parse new entries
    if n_used_cache < pal_sz {
        let mut i = n_used_cache as usize;
        let mut prev = msac.decode_bools_bypass(bpc) as i32;
        pal[i] = prev as u16;
        i += 1;

        if (i as i32) < pal_sz {
            let mut bits = bpc as i32 - 3 + msac.decode_bools_bypass(2) as i32;
            let max = (1i32 << bpc) - 1;
            loop {
                let delta = msac.decode_bools_bypass(bits as u32) as i32;
                prev = imin(prev + delta + 1, max);
                pal[i] = prev as u16;
                i += 1;
                if prev + 1 >= max {
                    while (i as i32) < pal_sz {
                        pal[i] = max as u16;
                        i += 1;
                    }
                    break;
                }
                bits = imin(bits, 1 + crate::intops::ulog2((max - prev - 1) as u32));
                if (i as i32) >= pal_sz {
                    break;
                }
            }
        }

        // merge selected cache & new entries into pal while sorting cache
        if n_used_cache != 0 {
            let mut n = 0i32;
            let mut m = n_used_cache;
            for k in 0..pal_sz as usize {
                if n < n_used_cache && (m >= pal_sz || cache[n as usize] <= pal[m as usize]) {
                    pal[k] = cache[n as usize];
                    n += 1;
                } else {
                    pal[k] = pal[m as usize];
                    m += 1;
                }
            }
        }
    } else {
        pal[..pal_sz as usize].copy_from_slice(&cache[..pal_sz as usize]);
    }

    pal_sz as u8
}

pub(crate) fn read_tx_part<const UPDATE_CDF: bool, M: MsacReader<UPDATE_CDF>>(
    msac: &mut M,
    cdf_m: &mut CdfModeContext,
    b: &mut crate::levels::Av2Block,
    bs: BlockSize,
    lossless: bool,
    txfm_switchable: bool,
) {
    let bs_idx = bs as usize;
    let b_dim = &BLOCK_DIMENSIONS[bs_idx];
    let bw4 = b_dim[0] as i32;
    let bh4 = b_dim[1] as i32;

    b.tx_part = TxPartition::None as u8;
    if lossless {
        b.tx_size_ll = 0;
        if bs != BlockSize::Bs4x4
            && (if b.is_intra != 0 && b.intrabc == 0 {
                b.fsc != 0
            } else {
                b.skip_txfm == 0
            })
        {
            let szctx = SIZE_GROUP[bs_idx] as usize;
            let inter = (b.is_intra == 0 || b.intrabc != 0) as usize;
            b.tx_size_ll = msac.decode_bool_adapt(cdf_m.txsz_lossless(szctx, inter)) as u8;
        }
    } else if b.skip_txfm == 0 && txfm_switchable && bs != BlockSize::Bs4x4 && imax(bw4, bh4) <= 16
    {
        let inter = (b.is_intra == 0 || b.intrabc != 0) as usize;
        let szctx = TX_PART_GROUP[bs_idx] as usize;
        let is_split = msac.decode_bool_adapt(cdf_m.tx_split(b.fsc as usize, inter, szctx));
        if is_split != 0 {
            if imin(bw4, bh4) >= 2 {
                let ctx = TX_TYPE_GROUP_VH[bs_idx] as usize;
                b.tx_part = 1 + msac.decode_symbol_adapt_n_padded::<6, 8>(cdf_m.tx_part_2d(
                    b.fsc as usize,
                    inter,
                    ctx,
                )) as u8;
            } else if imax(bw4, bh4) >= 4 {
                let ctx = (bw4 >= 4) as usize;
                let tx_part_4way =
                    msac.decode_bool_adapt(cdf_m.tx_part_1d(b.fsc as usize, inter, ctx));
                b.tx_part = TxPartition::H as u8 + ctx as u8 + tx_part_4way as u8 * 2;
            } else {
                debug_assert!(bs == BlockSize::Bs4x8 || bs == BlockSize::Bs8x4);
                b.tx_part = if bs == BlockSize::Bs4x8 {
                    TxPartition::H as u8
                } else {
                    TxPartition::V as u8
                };
            }
        }
    }

    // For valid streams the CDF only ever selects a tx partition that is legal
    // invalid combinations a corrupted stream can produce. Guard here so every
    // downstream `TX_PART_TBL[bs][tx_part]` (an i8 with a -1 sentinel) lookup is
    // safe: fall back to the always-valid TX_PARTITION_NONE. No-op for valid
    // input, where the selected partition is never -1.
    if crate::tables::TX_PART_TBL[bs_idx][b.tx_part as usize] < 0 {
        b.tx_part = TxPartition::None as u8;
    }
}

pub(crate) fn read_restoration_info<const UPDATE_CDF: bool, M: MsacReader<UPDATE_CDF>>(
    msac: &mut M,
    cdf_m: &mut CdfModeContext,
    bank: &mut NsWienerBank,
    lr: &mut Av2RestorationUnit,
    p: usize,
    frame_type: RestorationType,
    ns_plane: &NSWienerPlane,
) {
    let is_uv = (p != 0) as usize;

    if frame_type == RestorationType::Switchable {
        debug_assert!(p == 0);
        if msac.decode_bool_adapt(cdf_m.rst_switchable(0)) != 0 {
            lr.restoration_type = RestorationType::None as u8;
        } else {
            let t = msac.decode_bool_adapt(cdf_m.rst_switchable(1));
            lr.restoration_type = if t != 0 {
                RestorationType::PcWiener as u8
            } else {
                RestorationType::NsWiener as u8
            };
        }
    } else {
        // AVM disables PC-Wiener for chroma, so non-switchable chroma LR can
        // only signal NS-Wiener or None.
        debug_assert!(p == 0 || frame_type == RestorationType::NsWiener);
        let cdf = if frame_type == RestorationType::NsWiener {
            cdf_m.rst_ns_wiener()
        } else {
            cdf_m.rst_pc_wiener()
        };
        let t = msac.decode_bool_adapt(cdf);
        lr.restoration_type = if t != 0 {
            frame_type as u8
        } else {
            RestorationType::None as u8
        };
    }

    if lr.restoration_type == RestorationType::NsWiener as u8 && ns_plane.frame_filters_on == 0 {
        let n_classes = ns_plane.num_classes as usize;
        let mut exact_match_mask = 0u32;
        let mut bank_refs = [0u8; 16];

        for n in 0..n_classes {
            let exact_match = msac.decode_bool_bypass();
            let bank_size = bank.bank_size[n] as i32;
            let mut r = 0i32;
            while r < bank_size - 1 {
                if msac.decode_bool_bypass() != 0 {
                    break;
                }
                r += 1;
            }
            let r_idx = ((bank.bank_idx[n] as i32 - r) & 3) as u8;
            exact_match_mask |= (1 << n) * exact_match;
            bank_refs[n] = r_idx;
        }

        let masks: &[u32] = if is_uv != 0 {
            &SUBSET_MASKS_UV
        } else {
            &SUBSET_MASKS_Y
        };
        let cf_range: &[[i8; 2]] = if is_uv != 0 {
            &NS_WIENER_COEF_RANGE_UV
        } else {
            &NS_WIENER_COEF_RANGE_Y
        };
        let n_coefs = 16 + is_uv * 2;

        for n in 0..n_classes {
            let r = bank_refs[n] as usize;
            let exact = (exact_match_mask >> n) & 1 != 0;

            if exact {
                lr.ns_filter[n][..n_coefs].copy_from_slice(&bank.filter[r][n][..n_coefs]);
                if bank.bank_size[n] == 0 {
                    bank.bank_size[n] = 1;
                }
                continue;
            }

            lr.ns_filter[n][..n_coefs].fill(0);
            let mut s = 0usize;
            while s < 3 - is_uv {
                if msac.decode_bool_adapt(cdf_m.wiener_ns_len(is_uv)) == 0 {
                    break;
                }
                s += 1;
            }
            let mask = masks[s];
            let asym = is_uv != 0 && s != 0 && msac.decode_bool_adapt(cdf_m.wiener_ns_sym()) != 0;

            let ref_filter = &bank.filter[r][n];
            let mut i = 0usize;
            let mut m = mask;
            while i < n_coefs {
                if m & 1 == 0 {
                    i += 1;
                    m >>= 1;
                    continue;
                }
                lr.ns_filter[n][i] = (decode_4way(
                    msac,
                    ref_filter[i] as i32 - cf_range[i][1] as i32,
                    cdf_m.wiener_ns_cf(),
                    cf_range[i][0] as i32,
                ) + cf_range[i][1] as i32) as i8;
                if asym && i >= 6 {
                    lr.ns_filter[n][i + 1] = lr.ns_filter[n][i];
                    i += 1;
                    m >>= 1;
                }
                i += 1;
                m >>= 1;
            }

            let bidx = ((1 + bank.bank_idx[n]) & 3) as usize;
            bank.bank_idx[n] = bidx as u8;
            bank.filter[bidx][n][..n_coefs].copy_from_slice(&lr.ns_filter[n][..n_coefs]);
            if bank.bank_size[n] < 4 {
                bank.bank_size[n] += 1;
            }
        }
    }
}

pub(crate) fn derive_warpmv(
    rt: &refmvs::Tile,
    bx: i32,
    by: i32,
    have_top: bool,
    have_left: bool,
    bw4: i32,
    bh4: i32,
    w4: i32,
    h4: i32,
    ref_idx: i8,
    mv: Mv,
    wmp: &mut WarpedMotionParams,
    sb_step: i32,
    col_end: i32,
) {
    let mut pts = [[[0i32; 2]; 2]; 8];
    let mut np = 0usize;

    macro_rules! add_sample {
        ($dx:expr, $dy:expr, $sx:expr, $sy:expr, $rp:expr) => {{
            let rp: &refmvs::Block = $rp;
            let bd = &BLOCK_DIMENSIONS[rp.bs as usize];
            let rmv = if rp.mf & 2 != 0 { &rp.lmv } else { &rp.mv };
            for n in 0..2usize {
                if rp.reference.ref_at(n) != ref_idx {
                    continue;
                }
                pts[np][0][0] = 16 * (2 * ($dx as i32) + ($sx as i32) * bd[0] as i32) - 8;
                pts[np][0][1] = 16 * (2 * ($dy as i32) + ($sy as i32) * bd[1] as i32) - 8;
                pts[np][1][0] = pts[np][0][0] + rmv[n].x();
                pts[np][1][1] = pts[np][0][1] + rmv[n].y();
                np += 1;
                if np == 8 {
                    break;
                }
            }
        }};
    }

    debug_assert!(bw4 > 1);
    let mut have_topleft = false;
    let mut have_topright = false;
    let is_not_sb_boundary = (by & (sb_step - 1)) != 0;
    let mut init_odd = 0i32;

    if have_top {
        if is_not_sb_boundary {
            let ra_base = ((by - 1) & 63) as usize * 128;
            let r2_x = (bx & 127) as usize;
            let mut off = -(rt.r[ra_base + r2_x].ox4 as i32);
            have_topleft = off == 0;
            while off < w4 && np < 8 {
                let idx = ra_base + ((bx + off) & 127) as usize;
                add_sample!(off, 0, 1, -1, &rt.r[idx]);
                off += BLOCK_DIMENSIONS[rt.r[idx].bs as usize][0] as i32;
            }
            have_topright = off <= bw4;
        } else {
            let ra_off = rt.ra_off;
            let r2_idx = ra_off + (bx >> 1) as usize;
            init_odd = bx & 1;
            have_topleft = true;
            let mut off = if BLOCK_DIMENSIONS[rt.ra[r2_idx].bs as usize][0] as i32
                <= rt.ra[r2_idx].ox4 as i32 + init_odd
            {
                1
            } else {
                0
            };
            let tr_ext = (bx + bw4) & (sb_step - 1) != 0
                && (rt.ra[ra_off + ((bx + bw4) >> 1) as usize].ox4 != 0 || init_odd != 0);
            let tr_ext_i = tr_ext as i32;
            while off < w4 + tr_ext_i && np < 8 {
                let off8 = ra_off + ((bx + off) >> 1) as usize;
                let odd = (bx + off) & 1;
                let ioff = off - rt.ra[off8].ox4 as i32 - odd;
                add_sample!(ioff, 0, 1, -1, &rt.ra[off8]);
                off = ioff + BLOCK_DIMENSIONS[rt.ra[off8].bs as usize][0] as i32 + 1;
            }
            have_topright = true;
        }

        have_topright = have_topright
            && bw4 <= 16
            && bx + bw4 + ((!is_not_sb_boundary) as i32) < col_end
            && (!is_not_sb_boundary
                || ((bx + bw4) & (sb_step - 1) != 0
                    && rt.r[((by - 1) & 63) as usize * 128 + ((bx + bw4) & 127) as usize].mv[0]
                        .y()
                        != INVALID_MV));
    }

    if np < 8 && have_left {
        let left_x = ((bx - 1) & 127) as usize;
        let r_base = (by & 63) as usize * 128;
        let mut off = -(rt.r[r_base + left_x].oy4 as i32);
        have_topleft = have_topleft && off == 0;
        loop {
            let row = ((by & 63) as isize + off as isize) as usize;
            let idx = row * 128 + left_x;
            add_sample!(0, off, -1, 1, &rt.r[idx]);
            off += BLOCK_DIMENSIONS[rt.r[idx].bs as usize][1] as i32;
            if off >= h4 || np >= 8 {
                break;
            }
        }
    } else {
        have_topleft = false;
    }

    if is_not_sb_boundary {
        let ra_base = ((by - 1) & 63) as usize * 128;
        if np < 8 && have_topleft {
            add_sample!(0, 0, -1, -1, &rt.r[ra_base + ((bx - 1) & 127) as usize]);
        }
        if np < 8 && have_topright {
            add_sample!(bw4, 0, 1, -1, &rt.r[ra_base + ((bx + bw4) & 127) as usize]);
        }
    } else {
        if np < 8 && have_topleft {
            let r2 = if bx & (sb_step - 1) != 0 {
                &rt.ra[rt.ra_off + ((bx - 1) >> 1) as usize]
            } else {
                &rt.ra_tl
            };
            if BLOCK_DIMENSIONS[r2.bs as usize][0] as i32 + init_odd == r2.ox4 as i32 + 2 {
                add_sample!(0, 0, -1, -1, r2);
            }
        }
        if np < 8 && have_topright {
            let r2 = &rt.ra[rt.ra_off + ((bx + bw4 + 1) >> 1) as usize];
            if r2.ox4 as i32 == init_odd {
                add_sample!(bw4, 0, 1, -1, r2);
            }
        }
    }

    debug_assert!(np > 0 && np <= 8);

    if find_affine_int(&pts[..np], np, bw4, bh4, mv.xy(), wmp, bx, by) == 0
        && get_shear_params(wmp) == 0
    {
        wmp.wm_type = warp_type(&wmp.matrix);
    } else {
        wmp.wm_type = WarpedMotionType::Invalid;
    }
}

pub(crate) fn extend_warpmv(
    rt: &refmvs::Tile,
    bx: i32,
    by: i32,
    x_off: i32,
    y_off: i32,
    b_dim: &[u8],
    ref0: i8,
    mv0: Mv,
    wmp: &mut WarpedMotionParams,
    sb_step: i32,
    gmv_matrix: &[i32; 6],
) {
    let r = if y_off == -1 && (by & (sb_step - 1)) == 0 {
        if x_off < 0 && (bx & (sb_step - 1)) == 0 {
            &rt.ra_tl
        } else {
            &rt.ra[rt.ra_off + ((bx + x_off) >> 1) as usize]
        }
    } else {
        &rt.r[((by + y_off) & 63) as usize * 128 + ((bx + x_off) & 127) as usize]
    };
    let m = &mut wmp.matrix;

    if r.mf & 2 != 0 {
        if r.warp_type == WarpedMotionType::Invalid as i8 {
            m.copy_from_slice(&DEFAULT_WM_PARAMS.matrix);
        } else {
            m.copy_from_slice(&r.m);
        }
    } else if r.mf & 1 != 0 {
        m.copy_from_slice(gmv_matrix);
    } else {
        m[2..6].copy_from_slice(&DEFAULT_WM_PARAMS.matrix[2..6]);
        let ref_n = (r.reference.ref_at(0) != ref0) as usize;
        m[0] = r.mv[ref_n].x() * (1 << 13);
        m[1] = r.mv[ref_n].y() * (1 << 13);
    }

    let bw4 = b_dim[0] as i32;
    let bh4 = b_dim[1] as i32;
    let sx = bx * 4 + 2 * bw4 - 1;
    let sy = by * 4 + 2 * bh4 - 1;
    let mv0c = mv0.xy();
    let px = ((sx as i64) << 16) + mv0c.x as i64 * (1 << 13);
    let py = ((sy as i64) << 16) + mv0c.y as i64 * (1 << 13);

    if x_off >= 0 {
        debug_assert!(y_off == -1);
        let ay = by * 4 - 1;
        let sh = 1 + b_dim[3] as i32;
        let apx = m[2] as i64 * sx as i64 + m[3] as i64 * ay as i64 + m[0] as i64;
        let apy = m[4] as i64 * sx as i64 + m[5] as i64 * ay as i64 + m[1] as i64;
        let m3 = ((px - apx + bh4 as i64 - (px < apx) as i64) >> sh) as i32;
        let m5 = ((py - apy + bh4 as i64 - (py < apy) as i64) >> sh) as i32;
        m[3] = iclip((m3 + 0x20 - (m3 < 0) as i32) & !0x3f, -0x7fc0, 0x7fc0);
        m[5] = iclip((m5 + 0x20 - (m5 < 0x10000) as i32) & !0x3f, 0x8040, 0x17fc0);
    } else {
        debug_assert!(x_off == -1 || (by & (sb_step - 1)) == 0);
        let ax = bx * 4 - 1;
        let sh = 1 + b_dim[2] as i32;
        let lpx = m[2] as i64 * ax as i64 + m[3] as i64 * sy as i64 + m[0] as i64;
        let lpy = m[4] as i64 * ax as i64 + m[5] as i64 * sy as i64 + m[1] as i64;
        let m2 = ((px - lpx + bw4 as i64 - (px < lpx) as i64) >> sh) as i32;
        let m4 = ((py - lpy + bw4 as i64 - (py < lpy) as i64) >> sh) as i32;
        m[2] = iclip((m2 + 0x20 - (m2 < 0x10000) as i32) & !0x3f, 0x8040, 0x17fc0);
        m[4] = iclip((m4 + 0x20 - (m4 < 0) as i32) & !0x3f, -0x7fc0, 0x7fc0);
    }

    set_affine_mv2d(bw4, bh4, mv0c, wmp, bx, by);
    wmp.wm_type = if get_shear_params(wmp) != 0 {
        WarpedMotionType::Invalid
    } else {
        warp_type(&wmp.matrix)
    };
}

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

    #[test]
    fn palette_direction_is_not_read_for_64px_blocks() {
        let sz = [64, 64, 64, 64];
        assert_eq!(read_pal_direction(&sz, || panic!("direction bit read")), 0);

        let sz = [64, 32, 64, 32];
        assert_eq!(read_pal_direction(&sz, || panic!("direction bit read")), 0);
    }

    #[test]
    fn palette_direction_is_read_for_smaller_blocks() {
        let sz = [32, 32, 32, 32];
        assert_eq!(read_pal_direction(&sz, || 1), 1);
    }
}