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
//! §7.14 Loop filter process — the deblocking filter applied to
//! reconstructed `CurrFrame[]` samples per av1-spec p.307-318.
//!
//! ## Coverage (round 195 — close-out push)
//!
//! This module covers the §7.14 deblocking layer end-to-end at the
//! sample-filtering level, plus the §7.14.1 top-level per-edge driver
//! and the §7.14.4 / §7.14.5 adaptive strength derivation:
//!
//! * §7.14.1 — [`loop_filter_frame`]: per-plane × per-pass (vertical
//!   then horizontal) × per-row × per-col walk that calls
//!   [`loop_filter_edge`] on every 4×4 boundary the spec visits.
//! * §7.14.2 — [`loop_filter_edge`]: per-edge driver that derives
//!   `onScreen` / `xP` / `yP` / `prevRow` / `prevCol` / `isBlockEdge` /
//!   `isTxEdge` / `applyFilter` per av1-spec p.308-309, then dispatches
//!   to §7.14.3 + §7.14.4 + §7.14.6 for the chosen edge.
//! * §7.14.3 — [`filter_size`]: `Min(16, baseSize)` for luma, `Min(8,
//!   baseSize)` for chroma, with `baseSize = Min(Tx_*[txSz],
//!   Tx_*[prevTxSz])` per av1-spec p.310.
//! * §7.14.4 — [`adaptive_filter_strength`]: derives `(lvl, limit,
//!   blimit, thresh)` per av1-spec p.311 (driving §7.14.5 for the
//!   `lvl` core).
//! * §7.14.5 — [`adaptive_filter_strength_selection`]: the §5.9.14
//!   per-segment `SEG_LVL_ALT_LF_*` adjustment + the §5.9.11
//!   `loop_filter_ref_deltas[]` / `loop_filter_mode_deltas[]`
//!   adjustment, both clipped to `[0, MAX_LOOP_FILTER]` per av1-spec
//!   p.312.
//! * §7.14.6.1 — [`sample_filtering`]: per-pixel dispatch over
//!   [`filter_mask`] → [`narrow_filter`] / [`wide_filter`] based on
//!   `filterSize` and the flat-region masks.
//! * §7.14.6.2 — [`filter_mask`]: returns `(hevMask, filterMask,
//!   flatMask, flatMask2)` per av1-spec p.314-315.
//! * §7.14.6.3 — [`narrow_filter`]: the 4-tap edge filter that
//!   modifies up to two samples per side using the §3 `filter4_clamp`
//!   bit-depth-aware clamp.
//! * §7.14.6.4 — [`wide_filter`]: the 6/8/14-tap low-pass filter for
//!   detected flat regions (`log2Size ∈ {3, 4}`).
//!
//! ## Standalone-friendly surface
//!
//! The top-level driver takes a small [`LoopFilterFrameContext`]
//! bundling everything needed to walk the frame:
//!
//! * `loop_filter_level[0..=3]`, `loop_filter_sharpness`,
//!   `loop_filter_delta_enabled`, `loop_filter_ref_deltas`,
//!   `loop_filter_mode_deltas` — straight from
//!   [`crate::LoopFilterParams`].
//! * `delta_lf_multi`, `mi_rows`, `mi_cols`, `num_planes`,
//!   `bit_depth`, `subsampling_x`, `subsampling_y`, `frame_width`,
//!   `frame_height` — frame geometry.
//! * Per-block predicates (`is_intra(row, col)`, `skip(row, col)`,
//!   `ref_frame(row, col)`, `mode(row, col)`, `segment_id(row, col)`,
//!   `delta_lf(row, col, idx)`, `lf_tx_size(plane, row, col)`) — fed
//!   as closures so the driver is testable in isolation without
//!   needing the §5.11.x decode walker wired up. The §7.11.x intra
//!   driver consumes these from
//!   [`crate::PartitionWalker::y_modes`] / [`segment_ids`] /
//!   [`is_inters`] / [`skips`].
//! * Per-plane `CurrFrame[plane]` mutable buffers as
//!   [`PlaneBuffer<'_>`] — `samples` row-major slice + `(rows, cols)`
//!   extent.
//!
//! ## Bitstream-conformance gates not enforced here
//!
//! The §7.14 driver assumes the caller has already enforced:
//!
//! * §5.9.11 `loop_filter_level[i] <= MAX_LOOP_FILTER = 63` (the
//!   `f(6)` reader guarantees this).
//! * §5.9.14 `SEG_LVL_ALT_LF_*` per-segment feature values are signed
//!   `[-63, 63]` (the `su(1 + 6)` reader guarantees this).
//! * Plane buffers are sized at `(mi_rows * MI_SIZE) >> subY` ×
//!   `(mi_cols * MI_SIZE) >> subX` per av1-spec §5.11.34.

use crate::{
    cdf::MI_SIZE,
    uncompressed_header_tail::{INTRA_FRAME, MAX_LOOP_FILTER, TOTAL_REFS_PER_FRAME},
};

// =====================================================================
// §3 mode-type ordinals consumed by §7.14.4 — av1-spec p.13-14.
// =====================================================================

/// `NEARESTMV` per §3 — the lowest §7.14.4-tracked inter mode ordinal.
/// `mode >= NEARESTMV && mode != GLOBALMV && mode != GLOBAL_GLOBALMV`
/// drives `modeType = 1` per av1-spec p.311 line 17188.
///
/// Mirrors [`crate::cdf::MODE_NEARESTMV`] for the §7.14.4 mode-type
/// comparison.
pub const NEARESTMV: u8 = crate::cdf::MODE_NEARESTMV;
/// `GLOBALMV` per §3 — single-ref global-MV mode ordinal.
/// Excluded from the `modeType = 1` set by §7.14.4 per av1-spec p.311
/// line 17188.
pub const GLOBALMV: u8 = crate::cdf::MODE_GLOBALMV;
/// `GLOBAL_GLOBALMV` per §3 — compound-ref global-global-MV mode
/// ordinal. Excluded from the `modeType = 1` set by §7.14.4 per
/// av1-spec p.311 line 17188.
pub const GLOBAL_GLOBALMV: u8 = crate::cdf::MODE_GLOBAL_GLOBALMV;

// =====================================================================
// §3 segmentation feature ordinals consumed by §7.14.5 — av1-spec p.14.
// =====================================================================

/// `SEG_LVL_ALT_LF_Y_V` per §3 — feature 1 (per-segment alt LF for
/// vertical-pass luma). The §7.14.5 `feature = SEG_LVL_ALT_LF_Y_V +
/// i` selector lands on this for `i == 0` (i.e. `plane == 0 && pass ==
/// 0`). av1-spec p.312 line 17257.
pub const SEG_LVL_ALT_LF_Y_V: usize = 1;

// =====================================================================
// §7.14.1 / §7.14.2 — public top-level surface.
// =====================================================================

/// Mutable view of a single plane's `CurrFrame[plane]` sample buffer.
///
/// The `samples` slice is row-major `rows * cols` `i32` post-`Clip1`
/// per av1-spec §7.12.3 step-3 — same shape produced by
/// [`crate::PartitionWalker`] for the lazy per-plane buffers.
#[derive(Debug)]
pub struct PlaneBuffer<'a> {
    /// Per-plane row count: `(mi_rows * MI_SIZE) >> sub_y`.
    pub rows: u32,
    /// Per-plane column count: `(mi_cols * MI_SIZE) >> sub_x`.
    pub cols: u32,
    /// `rows * cols` row-major sample buffer.
    pub samples: &'a mut [i32],
}

impl PlaneBuffer<'_> {
    /// Bounds-checked load — returns `0` for out-of-bounds reads. The
    /// §7.14.6 filter mask process never indexes outside the on-screen
    /// region (the §7.14.2 `onScreen` gate prevents it), but a few
    /// `flatMask2`/wide-filter taps can reach the buffer's first /
    /// last column. The spec replicates edge samples; we mirror that
    /// by clipping the index to the buffer's `[0, rows-1]` /
    /// `[0, cols-1]` range.
    #[inline]
    fn get(&self, y: i32, x: i32) -> i32 {
        let yc = y.clamp(0, self.rows as i32 - 1) as usize;
        let xc = x.clamp(0, self.cols as i32 - 1) as usize;
        self.samples[yc * (self.cols as usize) + xc]
    }

    /// Bounds-checked store — silently drops out-of-bounds writes.
    /// The on-screen gate ensures the central edge samples land
    /// inside the buffer; this is a safety net for the wide filter's
    /// outermost taps.
    /// r475 — the flat index of `(y, x)` when it lies inside the
    /// buffer, `None` otherwise (the callers' in-range fast paths).
    #[inline]
    fn index_or_none(&self, y: i32, x: i32) -> Option<usize> {
        if y < 0 || x < 0 || y >= self.rows as i32 || x >= self.cols as i32 {
            return None;
        }
        Some((y as usize) * (self.cols as usize) + (x as usize))
    }

    #[inline]
    fn set(&mut self, y: i32, x: i32, v: i32) {
        if y < 0 || x < 0 {
            return;
        }
        let (y, x) = (y as u32, x as u32);
        if y >= self.rows || x >= self.cols {
            return;
        }
        let idx = (y as usize) * (self.cols as usize) + (x as usize);
        self.samples[idx] = v;
    }
}

/// Caller-supplied frame-level inputs the §7.14 driver consults.
///
/// All §5.9.11 / §5.9.18 / §5.5 / §5.9.5 fields come straight from
/// the parsed frame header. Per-block predicates are closures so the
/// driver can be exercised in isolation without wiring the full
/// §5.11 decode state.
pub struct LoopFilterFrameContext<'a> {
    /// `loop_filter_level[0..=3]` per §5.9.11. Indices `0, 1` are
    /// luma vertical / horizontal; indices `2, 3` are U / V (set to
    /// `0` if monochrome).
    pub loop_filter_level: [u8; 4],
    /// `loop_filter_sharpness` per §5.9.11 — `f(3)`, range `0..=7`.
    pub loop_filter_sharpness: u8,
    /// `loop_filter_delta_enabled` per §5.9.11.
    pub loop_filter_delta_enabled: bool,
    /// `loop_filter_ref_deltas[0..=7]` per §5.9.11 after the update
    /// walk.
    pub loop_filter_ref_deltas: [i8; TOTAL_REFS_PER_FRAME],
    /// `loop_filter_mode_deltas[0..=1]` per §5.9.11 after the update
    /// walk.
    pub loop_filter_mode_deltas: [i8; 2],
    /// `delta_lf_multi` per §5.9.18. When `false`, per-block `DeltaLF`
    /// uses index `0`; when `true`, it uses `(plane == 0) ? pass :
    /// (plane + 1)`.
    pub delta_lf_multi: bool,
    /// `MiRows` per §5.9.5.
    pub mi_rows: u32,
    /// `MiCols` per §5.9.5.
    pub mi_cols: u32,
    /// `NumPlanes` per §5.5.2 — `1` for monochrome, `3` otherwise.
    pub num_planes: u8,
    /// `BitDepth` per §5.5.2 (`8`, `10`, or `12`).
    pub bit_depth: u8,
    /// `subsampling_x` per §5.5.2.
    pub subsampling_x: u8,
    /// `subsampling_y` per §5.5.2.
    pub subsampling_y: u8,
    /// `FrameWidth` per §5.9.5 (post-superres).
    pub frame_width: u32,
    /// `FrameHeight` per §5.9.5.
    pub frame_height: u32,
    /// `RefFrames[row][col][0] <= INTRA_FRAME` per §7.14.2 line 17073.
    pub is_intra: &'a dyn Fn(u32, u32) -> bool,
    /// `Skips[row][col]` per §7.14.2 line 17071.
    pub skip: &'a dyn Fn(u32, u32) -> bool,
    /// `RefFrames[row][col][0]` per §7.14.4 line 17181 — the
    /// reference-frame ordinal in `0..=TOTAL_REFS_PER_FRAME`.
    pub ref_frame: &'a dyn Fn(u32, u32) -> u8,
    /// `YModes[row][col]` per §7.14.4 line 17183 — the per-block
    /// mode ordinal used to classify §7.14.4's `modeType`.
    pub mode: &'a dyn Fn(u32, u32) -> u8,
    /// `SegmentIds[row][col]` per §7.14.4 line 17179 — `-1` (signalled
    /// as `u8::MAX`) for cells without a segment id.
    pub segment_id: &'a dyn Fn(u32, u32) -> u8,
    /// `DeltaLFs[row][col][idx]` per §7.14.4 line 17195-17198. `idx`
    /// is `0` for `delta_lf_multi == 0`; otherwise `(plane == 0) ?
    /// pass : (plane + 1)` (so `0..=3`).
    pub delta_lf: &'a dyn Fn(u32, u32, usize) -> i8,
    /// `seg_feature_active_idx(segment, feature)` per §7.14.5 line
    /// 17259. `feature ∈ {SEG_LVL_ALT_LF_Y_V, +Y_H, +U, +V}` (i.e.
    /// `1..=4`).
    pub seg_feature_active: &'a dyn Fn(u32, usize) -> bool,
    /// `FeatureData[segment][feature]` per §7.14.5 line 17266 — the
    /// signed alt-LF offset that gets added to `lvlSeg` when
    /// `seg_feature_active(segment, feature) == 1`.
    pub seg_feature_data: &'a dyn Fn(u32, usize) -> i16,
    /// `LoopfilterTxSizes[plane][row >> subY][col >> subX]` per
    /// §7.14.2 line 17067 — the per-plane transform-size index in
    /// `0..=TX_SIZES_ALL`.
    pub lf_tx_size: &'a dyn Fn(usize, u32, u32) -> usize,
    /// `MiSizes[row][col]` per §7.14.2 line 17065 — the per-mi block
    /// size ordinal. Combined with `plane` via
    /// [`crate::cdf::get_plane_residual_size`]-equivalent: for the
    /// driver, this is consumed by [`block_width`] / [`block_height`]
    /// for the §7.14.2 `Block_Width[planeSize]` /
    /// `Block_Height[planeSize]` compare.
    pub mi_size: &'a dyn Fn(u32, u32) -> usize,
}

impl std::fmt::Debug for LoopFilterFrameContext<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LoopFilterFrameContext")
            .field("loop_filter_level", &self.loop_filter_level)
            .field("loop_filter_sharpness", &self.loop_filter_sharpness)
            .field("loop_filter_delta_enabled", &self.loop_filter_delta_enabled)
            .field("loop_filter_ref_deltas", &self.loop_filter_ref_deltas)
            .field("loop_filter_mode_deltas", &self.loop_filter_mode_deltas)
            .field("delta_lf_multi", &self.delta_lf_multi)
            .field("mi_rows", &self.mi_rows)
            .field("mi_cols", &self.mi_cols)
            .field("num_planes", &self.num_planes)
            .field("bit_depth", &self.bit_depth)
            .field("subsampling_x", &self.subsampling_x)
            .field("subsampling_y", &self.subsampling_y)
            .field("frame_width", &self.frame_width)
            .field("frame_height", &self.frame_height)
            .finish_non_exhaustive()
    }
}

/// §7.14.1 top-level loop-filter driver — av1-spec p.307 lines
/// 16959-16970.
///
/// Iterates over `plane × pass × row × col` with the spec's
/// `rowStep`/`colStep` chroma stride. Calls [`loop_filter_edge`] on
/// every edge the spec visits; the inner gates (`onScreen`,
/// `isBlockEdge`/`isTxEdge`, `lvl == 0`) decide whether any samples
/// actually move.
///
/// Modifies the per-plane buffers in place.
pub fn loop_filter_frame(ctx: &LoopFilterFrameContext<'_>, planes: &mut [PlaneBuffer<'_>]) {
    loop_filter_rows(ctx, planes, 0, ctx.mi_rows);
}

/// r464 — [`loop_filter_frame`] restricted to the edges whose `row`
/// lies in `mi_row_start..mi_row_end` (both passes, every plane). On
/// the whole range this IS the frame process; on a band it is the
/// encoder's level-election probe (the band's first horizontal edge
/// reads samples above the band as the band left them).
pub fn loop_filter_rows(
    ctx: &LoopFilterFrameContext<'_>,
    planes: &mut [PlaneBuffer<'_>],
    mi_row_start: u32,
    mi_row_end: u32,
) {
    let num_planes = ctx.num_planes.min(planes.len() as u8);
    let mi_row_end = mi_row_end.min(ctx.mi_rows);
    for plane in 0..num_planes {
        // av1-spec p.307 line 16961: skip chroma planes whose
        // `loop_filter_level[1 + plane]` slot is zero.
        if plane > 0 && ctx.loop_filter_level[(1 + plane) as usize] == 0 {
            continue;
        }
        let (sub_x, sub_y) = subsampling_for_plane(plane, ctx.subsampling_x, ctx.subsampling_y);
        let row_step: u32 = if plane == 0 { 1 } else { 1 << sub_y };
        let col_step: u32 = if plane == 0 { 1 } else { 1 << sub_x };
        for pass in 0..2u8 {
            let mut row = mi_row_start;
            while row < mi_row_end {
                let mut col = 0u32;
                while col < ctx.mi_cols {
                    loop_filter_edge(ctx, planes, plane, pass, row, col);
                    col += col_step;
                }
                row += row_step;
            }
        }
    }
}

/// r475 — one plane, one pass of [`loop_filter_rows`] over the edge
/// rows `mi_row_start .. mi_row_end`, with the samples addressed
/// through a row window of the plane (`window_row0` = the plane row
/// at `window.samples[ 0 ]`). The band-parallel driver's unit of
/// work: within a pass no two edges of a plane interact — §7.14.3
/// takes the filter size as the smaller transform size across the
/// edge, so an edge at `e` of size `S` writes at most `w( S )` rows
/// each side (`w = 2 / 3 / 6` for `S = 4..6 / 8 / 16`, §7.14.6.3 /
/// §7.14.6.4) while the next edge lies `T >= S` rows away and reads
/// `r( S' )` rows back (`r = 2 / 4 / 7`, §7.14.6.2) with `S' <= T`,
/// and `T > w + r - 1` for every admissible pair — so any order of
/// the edges of one pass, hence any split into bands, yields the
/// spec's samples. A pass-1 band needs its 7-row skirts in the
/// window; a pass-0 band needs none.
#[allow(clippy::too_many_arguments)]
pub(crate) fn loop_filter_rows_window(
    ctx: &LoopFilterFrameContext<'_>,
    plane: u8,
    pass: u8,
    window: &mut PlaneBuffer<'_>,
    window_row0: u32,
    mi_row_start: u32,
    mi_row_end: u32,
) {
    if plane > 0 && ctx.loop_filter_level[(1 + plane) as usize] == 0 {
        return;
    }
    let (sub_x, sub_y) = subsampling_for_plane(plane, ctx.subsampling_x, ctx.subsampling_y);
    let row_step: u32 = if plane == 0 { 1 } else { 1 << sub_y };
    let col_step: u32 = if plane == 0 { 1 } else { 1 << sub_x };
    let mi_row_end = mi_row_end.min(ctx.mi_rows);
    let mut row = mi_row_start;
    while row < mi_row_end {
        let mut col = 0u32;
        while col < ctx.mi_cols {
            loop_filter_edge_in(ctx, window, plane, pass, row, col, window_row0);
            col += col_step;
        }
        row += row_step;
    }
}

/// r475 — split a row-major plane into disjoint mutable row windows
/// `[ start, end )` (sorted, non-overlapping, in plane rows).
pub(crate) fn row_windows_mut<'a>(
    samples: &'a mut [i32],
    cols: usize,
    ranges: &[(usize, usize)],
) -> Vec<&'a mut [i32]> {
    let mut out = Vec::with_capacity(ranges.len());
    let mut rest = samples;
    let mut consumed = 0usize;
    for &(start, end) in ranges {
        debug_assert!(start >= consumed && end >= start);
        let (_, tail) = std::mem::take(&mut rest).split_at_mut((start - consumed) * cols);
        let (win, tail) = tail.split_at_mut((end - start) * cols);
        out.push(win);
        rest = tail;
        consumed = end;
    }
    out
}

/// r475 — run `jobs` on up to `threads` scoped workers (a shared
/// queue; serial when `threads <= 1`). Each job owns its disjoint
/// slice of the picture, so the result is the serial result.
pub(crate) fn run_jobs<'a>(threads: usize, jobs: Vec<Box<dyn FnOnce() + Send + 'a>>) {
    if threads <= 1 || jobs.len() <= 1 {
        for job in jobs {
            job();
        }
        return;
    }
    let queue = std::sync::Mutex::new(jobs);
    std::thread::scope(|scope| {
        for _ in 0..threads.min(queue.lock().map(|q| q.len()).unwrap_or(1)) {
            let queue = &queue;
            scope.spawn(move || loop {
                let job = match queue.lock() {
                    Ok(mut q) => q.pop(),
                    Err(_) => None,
                };
                match job {
                    Some(job) => job(),
                    None => break,
                }
            });
        }
    });
}

/// §7.14.2 per-edge driver — av1-spec p.307-310 lines 16994-17126.
///
/// `plane` is `0` (Y), `1` (U), `2` (V). `pass` is `0` for vertical
/// boundaries (filtering horizontally), `1` for horizontal boundaries
/// (filtering vertically). `row` and `col` are in 4×4-block luma
/// coordinates.
pub fn loop_filter_edge(
    ctx: &LoopFilterFrameContext<'_>,
    planes: &mut [PlaneBuffer<'_>],
    plane: u8,
    pass: u8,
    row: u32,
    col: u32,
) {
    let Some(plane_buf) = planes.get_mut(plane as usize) else {
        return;
    };
    loop_filter_edge_in(ctx, plane_buf, plane, pass, row, col, 0);
}

/// [`loop_filter_edge`] over one plane buffer that may be a row
/// WINDOW of the plane: `window_row0` is the plane row its row 0
/// holds (`0` for the whole plane). The §7.14.2 gates run in frame
/// coordinates; only the sample addressing is shifted.
#[allow(clippy::too_many_arguments)]
pub(crate) fn loop_filter_edge_in(
    ctx: &LoopFilterFrameContext<'_>,
    plane_buf: &mut PlaneBuffer<'_>,
    plane: u8,
    pass: u8,
    row: u32,
    col: u32,
    window_row0: u32,
) {
    let (sub_x, sub_y) = subsampling_for_plane(plane, ctx.subsampling_x, ctx.subsampling_y);
    let (dx, dy) = if pass == 0 {
        (1i32, 0i32)
    } else {
        (0i32, 1i32)
    };
    let x = col * (MI_SIZE as u32);
    let y = row * (MI_SIZE as u32);
    // av1-spec p.308 line 17028-17030: row |= subY; col |= subX. This
    // snaps a chroma walk that landed on an odd 4×4 cell down to the
    // chroma-aligned cell.
    let row = row | (sub_y as u32);
    let col = col | (sub_x as u32);
    // av1-spec p.308 line 17040-17048: onScreen gate.
    if x >= ctx.frame_width {
        return;
    }
    if y >= ctx.frame_height {
        return;
    }
    if pass == 0 && x == 0 {
        return;
    }
    if pass == 1 && y == 0 {
        return;
    }
    // av1-spec p.308 line 17054-17056.
    let x_p = (x >> sub_x) as i32;
    let y_p = (y >> sub_y) as i32;
    // av1-spec p.308 line 17061-17063.
    let prev_row = (row as i32) - (dy << sub_y);
    let prev_col = (col as i32) - (dx << sub_x);
    if prev_row < 0 || prev_col < 0 {
        // The onScreen gate already drops `pass == 0 && x == 0` /
        // `pass == 1 && y == 0`, so a negative prev_row / prev_col
        // here would only come from a chroma-aligned cell whose
        // neighbour-via-`subX/Y` lands off-grid — defensively skip.
        return;
    }
    let prev_row = prev_row as u32;
    let prev_col = prev_col as u32;
    if row >= ctx.mi_rows || col >= ctx.mi_cols {
        return;
    }
    // av1-spec p.308 line 17065-17075.
    let mi_size = (ctx.mi_size)(row, col);
    let tx_sz = (ctx.lf_tx_size)(plane as usize, row >> sub_y, col >> sub_x);
    // §7.14.2 line 17069: `planeSize = get_plane_residual_size( MiSize,
    // plane )`. On chroma planes the subsampled block size drives the
    // `isBlockEdge` modulus — e.g. a BLOCK_32X8 luma leaf's 4:2:0
    // chroma prediction block is 16×4, so its chroma `isBlockEdge`
    // fires every 4 plane rows, NOT every 8 (r387 fix: `planeSize =
    // MiSize` missed every chroma block edge whose subsampled extent no
    // longer divided the luma extent — on a both-sides-skip inter edge,
    // `applyFilter` then never fired).
    let plane_size =
        crate::cdf::get_plane_residual_size(mi_size, plane, sub_x, sub_y).unwrap_or(mi_size);
    let skip = (ctx.skip)(row, col);
    let is_intra = (ctx.is_intra)(row, col);
    let prev_tx_sz = (ctx.lf_tx_size)(plane as usize, prev_row >> sub_y, prev_col >> sub_x);
    // av1-spec p.308 line 17079-17084: isBlockEdge.
    let is_block_edge = if pass == 0 {
        (x_p as u32) % block_width(plane_size).max(1) == 0
    } else {
        (y_p as u32) % block_height(plane_size).max(1) == 0
    };
    // av1-spec p.308 line 17088-17099: isTxEdge.
    let is_tx_edge = if pass == 0 {
        (x_p as u32) % crate::cdf::TX_WIDTH[tx_sz].max(1) as u32 == 0
    } else {
        (y_p as u32) % crate::cdf::TX_HEIGHT[tx_sz].max(1) as u32 == 0
    };
    // av1-spec p.309 line 17101-17107: applyFilter.
    let apply_filter = if !is_tx_edge {
        false
    } else {
        is_block_edge || !skip || is_intra
    };
    // §7.14.3 filter size derivation.
    let filter_size = filter_size(tx_sz, prev_tx_sz, pass, plane);
    // §7.14.4 adaptive filter strength on the current side.
    let mut strength = adaptive_filter_strength(ctx, row, col, plane, pass);
    if strength.lvl == 0 {
        // av1-spec p.309 line 17115-17116: fall back to prev side if
        // current-side lvl == 0.
        strength = adaptive_filter_strength(ctx, prev_row, prev_col, plane, pass);
    }
    if !apply_filter || strength.lvl == 0 {
        return;
    }
    let y_p = y_p - window_row0 as i32;
    // av1-spec p.309 line 17118-17122: walk i = 0..MI_SIZE-1 along
    // the edge (the boundary spans `MI_SIZE` samples for the 4×4
    // grid).
    for i in 0..(MI_SIZE as i32) {
        let sx = x_p + dy * i;
        let sy = y_p + dx * i;
        sample_filtering(
            plane_buf,
            sx,
            sy,
            plane,
            strength.limit,
            strength.blimit,
            strength.thresh,
            dx,
            dy,
            filter_size,
            ctx.bit_depth,
        );
    }
}

// =====================================================================
// §7.14.3 — Filter size derivation. av1-spec p.310.
// =====================================================================

/// §7.14.3 — `Min(plane==0 ? 16 : 8, Min(Tx_*[txSz], Tx_*[prevTxSz]))`.
///
/// `pass == 0` consults `Tx_Width[]`; `pass == 1` consults
/// `Tx_Height[]`. `plane == 0` caps at 16, else at 8.
#[must_use]
pub fn filter_size(tx_sz: usize, prev_tx_sz: usize, pass: u8, plane: u8) -> u32 {
    let base_size = if pass == 0 {
        (crate::cdf::TX_WIDTH[tx_sz].min(crate::cdf::TX_WIDTH[prev_tx_sz])) as u32
    } else {
        (crate::cdf::TX_HEIGHT[tx_sz].min(crate::cdf::TX_HEIGHT[prev_tx_sz])) as u32
    };
    let cap = if plane == 0 { 16 } else { 8 };
    base_size.min(cap)
}

// =====================================================================
// §7.14.4 — Adaptive filter strength process. av1-spec p.311.
// =====================================================================

/// §7.14.4 output bundle.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FilterStrength {
    /// `lvl` per av1-spec p.311 line 17177 — the §7.14.5 selected
    /// filter level, in `[0, MAX_LOOP_FILTER]`.
    pub lvl: u8,
    /// `limit` per av1-spec p.311 line 17218-17222.
    pub limit: u8,
    /// `blimit` per av1-spec p.311 line 17224 — `2 * (lvl + 2) + limit`.
    pub blimit: u8,
    /// `thresh` per av1-spec p.311 line 17226 — `lvl >> 4`.
    pub thresh: u8,
}

/// §7.14.4 — Derive `(lvl, limit, blimit, thresh)` for the per-edge
/// cell at `(row, col, plane, pass)`.
#[must_use]
pub fn adaptive_filter_strength(
    ctx: &LoopFilterFrameContext<'_>,
    row: u32,
    col: u32,
    plane: u8,
    pass: u8,
) -> FilterStrength {
    // av1-spec p.311 line 17179-17198.
    let segment = (ctx.segment_id)(row, col);
    let r = (ctx.ref_frame)(row, col);
    let m = (ctx.mode)(row, col);
    let mode_type: u8 = if m >= NEARESTMV && m != GLOBALMV && m != GLOBAL_GLOBALMV {
        1
    } else {
        0
    };
    let delta_lf_idx = if ctx.delta_lf_multi {
        if plane == 0 {
            pass as usize
        } else {
            (plane + 1) as usize
        }
    } else {
        0
    };
    let delta_lf = (ctx.delta_lf)(row, col, delta_lf_idx);
    let lvl = adaptive_filter_strength_selection(
        ctx,
        segment as u32,
        r,
        mode_type,
        delta_lf,
        plane,
        pass,
    );
    // av1-spec p.311 line 17210-17226.
    let shift = if ctx.loop_filter_sharpness > 4 {
        2
    } else if ctx.loop_filter_sharpness > 0 {
        1
    } else {
        0
    };
    let lvl_i = lvl as i32;
    let limit_i = if ctx.loop_filter_sharpness > 0 {
        (lvl_i >> shift).clamp(1, 9 - ctx.loop_filter_sharpness as i32)
    } else {
        (lvl_i >> shift).max(1)
    };
    let limit = limit_i as u8;
    let blimit = (2 * (lvl as u32 + 2) + limit as u32) as u8;
    let thresh = lvl >> 4;
    FilterStrength {
        lvl,
        limit,
        blimit,
        thresh,
    }
}

/// §7.14.5 — Adaptive filter strength selection. av1-spec p.311-312.
#[must_use]
pub fn adaptive_filter_strength_selection(
    ctx: &LoopFilterFrameContext<'_>,
    segment: u32,
    ref_frame: u8,
    mode_type: u8,
    delta_lf: i8,
    plane: u8,
    pass: u8,
) -> u8 {
    // av1-spec p.312 line 17249.
    let i = if plane == 0 {
        pass as usize
    } else {
        (plane + 1) as usize
    };
    // av1-spec p.312 line 17251.
    let base_filter_level = clip3_i32(
        0,
        MAX_LOOP_FILTER as i32,
        (delta_lf as i32) + (ctx.loop_filter_level[i] as i32),
    );
    let mut lvl_seg = base_filter_level;
    // av1-spec p.312 line 17257-17268.
    let feature = SEG_LVL_ALT_LF_Y_V + i;
    if (ctx.seg_feature_active)(segment, feature) {
        lvl_seg = clip3_i32(
            0,
            MAX_LOOP_FILTER as i32,
            (ctx.seg_feature_data)(segment, feature) as i32 + lvl_seg,
        );
    }
    // av1-spec p.312 line 17270-17280.
    if ctx.loop_filter_delta_enabled {
        let n_shift = lvl_seg >> 5;
        if ref_frame as usize == INTRA_FRAME {
            lvl_seg += (ctx.loop_filter_ref_deltas[INTRA_FRAME] as i32) << n_shift;
        } else {
            let ref_idx = (ref_frame as usize).min(TOTAL_REFS_PER_FRAME - 1);
            let mode_idx = (mode_type as usize).min(ctx.loop_filter_mode_deltas.len() - 1);
            lvl_seg += (ctx.loop_filter_ref_deltas[ref_idx] as i32) << n_shift;
            lvl_seg += (ctx.loop_filter_mode_deltas[mode_idx] as i32) << n_shift;
        }
        lvl_seg = clip3_i32(0, MAX_LOOP_FILTER as i32, lvl_seg);
    }
    lvl_seg.clamp(0, 255) as u8
}

// =====================================================================
// §7.14.6.1 — Sample filtering dispatch. av1-spec p.313.
// =====================================================================

/// §7.14.6.1 — Run [`filter_mask`] at `(x, y)` then dispatch to
/// [`narrow_filter`] or [`wide_filter`] per the resulting masks and
/// `filter_size`.
#[allow(clippy::too_many_arguments)]
pub fn sample_filtering(
    plane_buf: &mut PlaneBuffer<'_>,
    x: i32,
    y: i32,
    plane: u8,
    limit: u8,
    blimit: u8,
    thresh: u8,
    dx: i32,
    dy: i32,
    filter_size: u32,
    bit_depth: u8,
) {
    let masks = filter_mask(
        plane_buf,
        x,
        y,
        plane,
        limit,
        blimit,
        thresh,
        dx,
        dy,
        filter_size,
        bit_depth,
    );
    if !masks.filter_mask {
        return;
    }
    if filter_size == 4 || !masks.flat_mask {
        narrow_filter(plane_buf, x, y, dx, dy, masks.hev_mask, bit_depth);
    } else if filter_size == 8 || !masks.flat_mask2 {
        wide_filter(plane_buf, x, y, plane, dx, dy, 3, bit_depth);
    } else {
        wide_filter(plane_buf, x, y, plane, dx, dy, 4, bit_depth);
    }
}

// =====================================================================
// §7.14.6.2 — Filter mask process. av1-spec p.314-315.
// =====================================================================

/// §7.14.6.2 output: the four edge-classification masks.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FilterMaskOutput {
    /// `hevMask` — high-edge-variance indicator.
    pub hev_mask: bool,
    /// `filterMask` — `true` ⇔ filtering should occur.
    pub filter_mask: bool,
    /// `flatMask` — `true` ⇔ samples within 4 either side of the
    /// boundary are in a flat region (`filterSize >= 8`).
    pub flat_mask: bool,
    /// `flatMask2` — `true` ⇔ samples within 6 either side are in a
    /// flat region (`filterSize >= 16`).
    pub flat_mask2: bool,
}

/// §7.14.6.2 — Compute `(hevMask, filterMask, flatMask, flatMask2)`
/// from samples around `(x, y)` on `plane`. av1-spec p.314-315.
#[allow(clippy::too_many_arguments)]
#[must_use]
pub fn filter_mask(
    plane_buf: &PlaneBuffer<'_>,
    x: i32,
    y: i32,
    plane: u8,
    limit: u8,
    blimit: u8,
    thresh: u8,
    dx: i32,
    dy: i32,
    filter_size: u32,
    bit_depth: u8,
) -> FilterMaskOutput {
    // Sample reads — av1-spec p.314 lines 17352-17365. Up to 7 either
    // side; only the wide path reads `p3..p6` / `q3..q6`. r475 — the
    // reach is gathered once per `filter_size` (a 4-tap edge touches
    // `p1..q1` only), directly when the whole reach is inside the
    // plane and through the clamped `get` otherwise.
    let reach: i32 = if filter_size >= 16 {
        7
    } else if filter_size >= 8 {
        4
    } else {
        2
    };
    let mut taps = [0i32; 14];
    let base = plane_buf.index_or_none(y - reach * dy, x - reach * dx);
    let end = plane_buf.index_or_none(y + (reach - 1) * dy, x + (reach - 1) * dx);
    match (base, end) {
        (Some(base), Some(_)) => {
            let stride = (dx + dy * plane_buf.cols as i32) as usize;
            let first = 7 - reach as usize;
            for k in 0..(2 * reach) as usize {
                taps[first + k] = plane_buf.samples[base + k * stride];
            }
        }
        _ => {
            for p in -reach..reach {
                taps[(p + 7) as usize] = plane_buf.get(y + p * dy, x + p * dx);
            }
        }
    }
    let [p6, p5, p4, p3, p2, p1, p0, q0, q1, q2, q3, q4, q5, q6] = taps;
    let bd_shift = bit_depth - 8;
    // hevMask — av1-spec p.314 lines 17381-17385.
    let thresh_bd = (thresh as i32) << bd_shift;
    let hev_mask = (p1 - p0).abs() > thresh_bd || (q1 - q0).abs() > thresh_bd;
    // filterLen — av1-spec p.315 lines 17389-17395.
    let filter_len: u32 = if filter_size == 4 {
        4
    } else if plane != 0 {
        6
    } else if filter_size == 8 {
        8
    } else {
        16
    };
    // filterMask — av1-spec p.315 lines 17402-17416.
    let limit_bd = (limit as i32) << bd_shift;
    let blimit_bd = (blimit as i32) << bd_shift;
    let mut mask = (p1 - p0).abs() > limit_bd;
    mask |= (q1 - q0).abs() > limit_bd;
    mask |= (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 > blimit_bd;
    if filter_len >= 6 {
        mask |= (p2 - p1).abs() > limit_bd;
        mask |= (q2 - q1).abs() > limit_bd;
    }
    if filter_len >= 8 {
        mask |= (p3 - p2).abs() > limit_bd;
        mask |= (q3 - q2).abs() > limit_bd;
    }
    let filter_mask = !mask;
    // flatMask — av1-spec p.315 lines 17431-17443.
    let threshold_bd = 1i32 << bd_shift;
    let flat_mask = if filter_size >= 8 {
        let mut m = (p1 - p0).abs() > threshold_bd;
        m |= (q1 - q0).abs() > threshold_bd;
        m |= (p2 - p0).abs() > threshold_bd;
        m |= (q2 - q0).abs() > threshold_bd;
        if filter_len >= 8 {
            m |= (p3 - p0).abs() > threshold_bd;
            m |= (q3 - q0).abs() > threshold_bd;
        }
        !m
    } else {
        false
    };
    // flatMask2 — av1-spec p.315 lines 17451-17461.
    let flat_mask2 = if filter_size >= 16 {
        let mut m = (p6 - p0).abs() > threshold_bd;
        m |= (q6 - q0).abs() > threshold_bd;
        m |= (p5 - p0).abs() > threshold_bd;
        m |= (q5 - q0).abs() > threshold_bd;
        m |= (p4 - p0).abs() > threshold_bd;
        m |= (q4 - q0).abs() > threshold_bd;
        !m
    } else {
        false
    };
    FilterMaskOutput {
        hev_mask,
        filter_mask,
        flat_mask,
        flat_mask2,
    }
}

// =====================================================================
// §7.14.6.3 — Narrow filter. av1-spec p.316.
// =====================================================================

/// §7.14.6.3 — Modify up to two samples each side of the boundary at
/// `(x, y)`. `hev_mask == false` widens the modify window from one
/// to two on each side.
pub fn narrow_filter(
    plane_buf: &mut PlaneBuffer<'_>,
    x: i32,
    y: i32,
    dx: i32,
    dy: i32,
    hev_mask: bool,
    bit_depth: u8,
) {
    let half = 0x80i32 << (bit_depth - 8);
    let q0 = plane_buf.get(y, x);
    let q1 = plane_buf.get(y + dy, x + dx);
    let p0 = plane_buf.get(y - dy, x - dx);
    let p1 = plane_buf.get(y - 2 * dy, x - 2 * dx);
    let ps1 = p1 - half;
    let ps0 = p0 - half;
    let qs0 = q0 - half;
    let qs1 = q1 - half;
    let mut filter = if hev_mask {
        filter4_clamp(ps1 - qs1, bit_depth)
    } else {
        0
    };
    filter = filter4_clamp(filter + 3 * (qs0 - ps0), bit_depth);
    let filter1 = filter4_clamp(filter + 4, bit_depth) >> 3;
    let filter2 = filter4_clamp(filter + 3, bit_depth) >> 3;
    let oq0 = filter4_clamp(qs0 - filter1, bit_depth) + half;
    let op0 = filter4_clamp(ps0 + filter2, bit_depth) + half;
    plane_buf.set(y, x, oq0);
    plane_buf.set(y - dy, x - dx, op0);
    if !hev_mask {
        let f = round2_i32(filter1, 1);
        let oq1 = filter4_clamp(qs1 - f, bit_depth) + half;
        let op1 = filter4_clamp(ps1 + f, bit_depth) + half;
        plane_buf.set(y + dy, x + dx, oq1);
        plane_buf.set(y - 2 * dy, x - 2 * dx, op1);
    }
}

// =====================================================================
// §7.14.6.4 — Wide filter. av1-spec p.317.
// =====================================================================

/// §7.14.6.4 — Apply the 6/8/14-tap low-pass filter centred at the
/// boundary. `log2_size ∈ {3, 4}`.
#[allow(clippy::too_many_arguments)]
pub fn wide_filter(
    plane_buf: &mut PlaneBuffer<'_>,
    x: i32,
    y: i32,
    plane: u8,
    dx: i32,
    dy: i32,
    log2_size: u32,
    _bit_depth: u8,
) {
    // av1-spec p.317 lines 17552-17565.
    let n: i32 = if log2_size == 4 {
        6
    } else if plane == 0 {
        3
    } else {
        2
    };
    let n2: i32 = if log2_size == 3 && plane == 0 { 0 } else { 1 };
    // r475 — gather the `p[-(n+1)..=n]` taps once (clamped reads at
    // the plane edge exactly as the per-tap `get` did), run the
    // §7.14.6.4 sums in `i32` (at most 14 taps of weight ≤ 2 on
    // 12-bit samples) and write the `2n` outputs back. The per-call
    // heap buffer and the 13-fold re-reads were 15 % of a 12 MP
    // still decode.
    let mut taps = [0i32; 14];
    let base = plane_buf.index_or_none(y - (n + 1) * dy, x - (n + 1) * dx);
    let end = plane_buf.index_or_none(y + n * dy, x + n * dx);
    let stride = (dx + dy * plane_buf.cols as i32) as usize;
    match (base, end) {
        (Some(base), Some(_)) => {
            for (k, slot) in taps.iter_mut().take((2 * n + 2) as usize).enumerate() {
                *slot = plane_buf.samples[base + k * stride];
            }
        }
        _ => {
            for (k, slot) in taps.iter_mut().take((2 * n + 2) as usize).enumerate() {
                let p = k as i32 - (n + 1);
                *slot = plane_buf.get(y + p * dy, x + p * dx);
            }
        }
    }
    // av1-spec p.317 lines 17570-17578: `F[ i ] = Round2( Σ_{j=-n..n}
    // tap( j ) · p[ Clip3( -(n+1), n, i + j ) ], log2Size )` with
    // `tap( j ) = 2` for `|j| <= n2`, else `1`. r475 — the same
    // integer sum as a sliding window: the unit-weight window
    // `Σ_{j=-n..n} p[ Clip3( .., i + j ) ]` advances by one clamped
    // sample per output, and the `|j| <= n2` taps add their second
    // copy. Integer addition is associative, so the sums — and the
    // rounded outputs — are the spec's, at a fraction of the 13 × 12
    // multiplies.
    let tap_at = |k: i32| taps[(clip3_i32(-(n + 1), n, k) + n + 1) as usize];
    let mut window: i32 = 0;
    for j in -n..=n {
        window += tap_at(-n + j);
    }
    let mut f = [0i32; 12];
    for (idx, i) in (-n..n).enumerate() {
        if i > -n {
            window += tap_at(i + n) - tap_at(i - 1 - n);
        }
        let mut t = window;
        for j in -n2..=n2 {
            t += tap_at(i + j);
        }
        f[idx] = round2_i32(t, log2_size);
    }
    if let Some(base) = base {
        let first = base + stride;
        for (idx, _) in (-n..n).enumerate() {
            plane_buf.samples[first + idx * stride] = f[idx];
        }
    } else {
        for (idx, i) in (-n..n).enumerate() {
            plane_buf.set(y + i * dy, x + i * dx, f[idx]);
        }
    }
}

// =====================================================================
// Internal helpers.
// =====================================================================

/// §7.14.2 line 17008-17010: per-plane (`subX`, `subY`).
#[inline]
fn subsampling_for_plane(plane: u8, subsampling_x: u8, subsampling_y: u8) -> (u8, u8) {
    if plane == 0 {
        (0, 0)
    } else {
        (subsampling_x, subsampling_y)
    }
}

/// `Block_Width[planeSize]` per §3 — defers to the `cdf::BLOCK_SIZES`
/// table by way of [`crate::cdf::num_4x4_blocks_wide`].
#[inline]
fn block_width(plane_size: usize) -> u32 {
    if plane_size >= crate::cdf::BLOCK_SIZES {
        // Defensive: invalid block size — treat as 4 (smallest §3
        // block) to keep `isBlockEdge` from triggering on every
        // pixel.
        return 4;
    }
    (crate::cdf::num_4x4_blocks_wide(plane_size) * 4) as u32
}

/// `Block_Height[planeSize]` per §3 — mirror of [`block_width`].
#[inline]
fn block_height(plane_size: usize) -> u32 {
    if plane_size >= crate::cdf::BLOCK_SIZES {
        return 4;
    }
    (crate::cdf::num_4x4_blocks_high(plane_size) * 4) as u32
}

/// §3 `Clip3(a, b, x)` — clamp `x` to `[a, b]`.
#[inline]
fn clip3_i32(a: i32, b: i32, x: i32) -> i32 {
    if x < a {
        a
    } else if x > b {
        b
    } else {
        x
    }
}

/// §3 `Round2(x, n) = (x + (1 << (n - 1))) >> n` — unsigned/i32
/// rounding right-shift used by [`narrow_filter`] for the `n = 1`
/// case (and as a primitive for the wide filter's `Round2(t,
/// log2_size)` post-shift).
#[inline]
fn round2_i32(x: i32, n: u32) -> i32 {
    if n == 0 {
        x
    } else {
        (x + (1 << (n - 1))) >> n
    }
}

/// `filter4_clamp(v) = Clip3(-(1 << (BitDepth - 1)), (1 << (BitDepth -
/// 1)) - 1, v)` per av1-spec p.316 line 17498.
#[inline]
fn filter4_clamp(value: i32, bit_depth: u8) -> i32 {
    let lo = -(1i32 << (bit_depth - 1));
    let hi = (1i32 << (bit_depth - 1)) - 1;
    clip3_i32(lo, hi, value)
}

// =====================================================================
// Tests.
// =====================================================================

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

    #[allow(clippy::too_many_arguments)]
    fn make_ctx<'a>(
        mi_rows: u32,
        mi_cols: u32,
        loop_filter_level: [u8; 4],
        is_intra: &'a dyn Fn(u32, u32) -> bool,
        skip: &'a dyn Fn(u32, u32) -> bool,
        ref_frame: &'a dyn Fn(u32, u32) -> u8,
        mode: &'a dyn Fn(u32, u32) -> u8,
        segment_id: &'a dyn Fn(u32, u32) -> u8,
        delta_lf: &'a dyn Fn(u32, u32, usize) -> i8,
        seg_feature_active: &'a dyn Fn(u32, usize) -> bool,
        seg_feature_data: &'a dyn Fn(u32, usize) -> i16,
        lf_tx_size: &'a dyn Fn(usize, u32, u32) -> usize,
        mi_size: &'a dyn Fn(u32, u32) -> usize,
    ) -> LoopFilterFrameContext<'a> {
        LoopFilterFrameContext {
            loop_filter_level,
            loop_filter_sharpness: 0,
            loop_filter_delta_enabled: false,
            loop_filter_ref_deltas: [0; TOTAL_REFS_PER_FRAME],
            loop_filter_mode_deltas: [0; 2],
            delta_lf_multi: false,
            mi_rows,
            mi_cols,
            num_planes: 3,
            bit_depth: 8,
            subsampling_x: 1,
            subsampling_y: 1,
            frame_width: mi_cols * MI_SIZE as u32,
            frame_height: mi_rows * MI_SIZE as u32,
            is_intra,
            skip,
            ref_frame,
            mode,
            segment_id,
            delta_lf,
            seg_feature_active,
            seg_feature_data,
            lf_tx_size,
            mi_size,
        }
    }

    // --- §7.14.4 / §7.14.5 strength derivation --------------------

    #[test]
    fn strength_returns_zero_when_loop_filter_level_zero() {
        let ctx = make_ctx(
            4,
            4,
            [0, 0, 0, 0],
            &|_, _| true,
            &|_, _| false,
            &|_, _| INTRA_FRAME as u8,
            &|_, _| 0u8,
            &|_, _| 0u8,
            &|_, _, _| 0i8,
            &|_, _| false,
            &|_, _| 0i16,
            &|_, _, _| crate::cdf::TX_4X4,
            &|_, _| crate::cdf::BLOCK_4X4,
        );
        let s = adaptive_filter_strength(&ctx, 0, 0, 0, 0);
        assert_eq!(s.lvl, 0);
    }

    #[test]
    fn strength_basic_table_lvl_32_sharp_0() {
        let ctx = make_ctx(
            4,
            4,
            [32, 32, 32, 32],
            &|_, _| true,
            &|_, _| false,
            &|_, _| INTRA_FRAME as u8,
            &|_, _| 0u8,
            &|_, _| 0u8,
            &|_, _, _| 0i8,
            &|_, _| false,
            &|_, _| 0i16,
            &|_, _, _| crate::cdf::TX_4X4,
            &|_, _| crate::cdf::BLOCK_4X4,
        );
        let s = adaptive_filter_strength(&ctx, 0, 0, 0, 0);
        // sharpness 0 ⇒ shift 0 ⇒ limit = max(1, 32) = 32.
        // blimit = 2 * (32 + 2) + 32 = 100.
        // thresh = 32 >> 4 = 2.
        assert_eq!(s.lvl, 32);
        assert_eq!(s.limit, 32);
        assert_eq!(s.blimit, 100);
        assert_eq!(s.thresh, 2);
    }

    #[test]
    fn strength_intra_ref_delta_lifts_lvl() {
        let mut ctx = make_ctx(
            4,
            4,
            [10, 10, 10, 10],
            &|_, _| true,
            &|_, _| false,
            &|_, _| INTRA_FRAME as u8,
            &|_, _| 0u8,
            &|_, _| 0u8,
            &|_, _, _| 0i8,
            &|_, _| false,
            &|_, _| 0i16,
            &|_, _, _| crate::cdf::TX_4X4,
            &|_, _| crate::cdf::BLOCK_4X4,
        );
        ctx.loop_filter_delta_enabled = true;
        ctx.loop_filter_ref_deltas[INTRA_FRAME] = 5;
        let s = adaptive_filter_strength(&ctx, 0, 0, 0, 0);
        // base = lvl[0] = 10. n_shift = 10 >> 5 = 0. lvl_seg += 5
        // ⇒ 15. Clip3(0, 63) = 15.
        assert_eq!(s.lvl, 15);
    }

    #[test]
    fn strength_clipped_at_max_loop_filter() {
        let mut ctx = make_ctx(
            4,
            4,
            [60, 60, 60, 60],
            &|_, _| true,
            &|_, _| false,
            &|_, _| INTRA_FRAME as u8,
            &|_, _| 0u8,
            &|_, _| 0u8,
            &|_, _, _| 0i8,
            &|_, _| false,
            &|_, _| 0i16,
            &|_, _, _| crate::cdf::TX_4X4,
            &|_, _| crate::cdf::BLOCK_4X4,
        );
        ctx.loop_filter_delta_enabled = true;
        ctx.loop_filter_ref_deltas[INTRA_FRAME] = 10; // n_shift = 60>>5 = 1 ⇒ +20 ⇒ 80 ⇒ clip 63.
        let s = adaptive_filter_strength(&ctx, 0, 0, 0, 0);
        assert_eq!(s.lvl, MAX_LOOP_FILTER as u8);
    }

    // --- §7.14.3 filter size --------------------------------------

    #[test]
    fn filter_size_luma_caps_at_16_chroma_at_8() {
        // TX_64X64 width 64; cap at 16 for luma, 8 for chroma.
        assert_eq!(
            filter_size(crate::cdf::TX_64X64, crate::cdf::TX_64X64, 0, 0),
            16
        );
        assert_eq!(
            filter_size(crate::cdf::TX_64X64, crate::cdf::TX_64X64, 0, 1),
            8
        );
        // TX_4X4 width 4; min(4, cap) = 4.
        assert_eq!(filter_size(crate::cdf::TX_4X4, crate::cdf::TX_4X4, 0, 0), 4);
    }

    // --- §7.14.6.3 narrow filter ----------------------------------

    #[test]
    fn narrow_filter_idempotent_on_uniform_input() {
        // Uniform 128 input ⇒ p0 == q0 == p1 == q1; filter4_clamp(ps1 - qs1) = 0;
        // filter4_clamp(0 + 3*(qs0 - ps0)) = 0; oq0 = qs0 - (4>>3) = qs0;
        // op0 = ps0 + (3>>3) = ps0. Samples unchanged.
        let mut samples = vec![128i32; 8 * 8];
        let mut buf = PlaneBuffer {
            rows: 8,
            cols: 8,
            samples: &mut samples,
        };
        let before = buf.samples.to_vec();
        narrow_filter(&mut buf, 4, 4, 1, 0, false, 8);
        assert_eq!(buf.samples, &before[..]);
    }

    #[test]
    fn narrow_filter_softens_sharp_step_edge() {
        // Vertical edge between cols 3 and 4: left half 100, right 156.
        // p1 = (4,2) = 100; p0 = (4,3) = 100; q0 = (4,4) = 156; q1 = (4,5) = 156.
        // Expect q0 to come down and p0 to come up after the filter, by a small
        // delta governed by the narrow-filter's 4 + 3 rounding.
        let mut samples = vec![0i32; 8 * 8];
        for y in 0..8 {
            for x in 0..8 {
                samples[y * 8 + x] = if x < 4 { 100 } else { 156 };
            }
        }
        let mut buf = PlaneBuffer {
            rows: 8,
            cols: 8,
            samples: &mut samples,
        };
        narrow_filter(&mut buf, 4, 4, 1, 0, true, 8);
        let p0 = buf.samples[4 * 8 + 3];
        let q0 = buf.samples[4 * 8 + 4];
        assert!(p0 > 100, "p0={p0} should rise above 100");
        assert!(q0 < 156, "q0={q0} should fall below 156");
        // Symmetry: the step delta moves equally in both directions.
        assert_eq!(q0 - 128, 128 - p0);
    }

    // --- §7.14.6.4 wide filter ------------------------------------

    #[test]
    fn wide_filter_log2_3_luma_preserves_uniform_input() {
        let mut samples = vec![200i32; 16 * 16];
        let mut buf = PlaneBuffer {
            rows: 16,
            cols: 16,
            samples: &mut samples,
        };
        let before = buf.samples.to_vec();
        wide_filter(&mut buf, 8, 8, 0, 1, 0, 3, 8);
        assert_eq!(buf.samples, &before[..]);
    }

    #[test]
    fn wide_filter_log2_4_luma_preserves_uniform_input() {
        let mut samples = vec![64i32; 32 * 32];
        let mut buf = PlaneBuffer {
            rows: 32,
            cols: 32,
            samples: &mut samples,
        };
        let before = buf.samples.to_vec();
        wide_filter(&mut buf, 16, 16, 0, 1, 0, 4, 8);
        assert_eq!(buf.samples, &before[..]);
    }

    // --- §7.14.6.2 mask -------------------------------------------

    #[test]
    fn filter_mask_flat_region_allows_filter() {
        let mut samples = vec![100i32; 16 * 16];
        let buf = PlaneBuffer {
            rows: 16,
            cols: 16,
            samples: &mut samples,
        };
        let m = filter_mask(&buf, 8, 8, 0, 16, 32, 8, 1, 0, 8, 8);
        assert!(m.filter_mask, "flat region must permit filtering");
        assert!(m.flat_mask, "flat region must be flat");
        assert!(!m.hev_mask, "uniform region cannot be hev");
    }

    #[test]
    fn filter_mask_steep_edge_blocks_filter() {
        // Sharp step beyond `blimit` — filter must be vetoed.
        let mut samples = vec![0i32; 16 * 16];
        for y in 0..16 {
            for x in 0..16 {
                samples[y * 16 + x] = if x < 8 { 50 } else { 200 };
            }
        }
        let buf = PlaneBuffer {
            rows: 16,
            cols: 16,
            samples: &mut samples,
        };
        // limit = 4, blimit = 12, thresh = 6 → all small ⇒ vetoes.
        let m = filter_mask(&buf, 8, 8, 0, 4, 12, 6, 1, 0, 8, 8);
        assert!(!m.filter_mask, "steep step must veto filter");
    }

    // --- §7.14.1 driver iteration ---------------------------------

    #[test]
    fn driver_iterates_correct_edge_count_for_2x2_mi_grid() {
        use std::cell::Cell;
        let edges: Cell<u32> = Cell::new(0);
        let ctr = |_r: u32, _c: u32| {
            edges.set(edges.get() + 1);
            false
        };
        let ctx = LoopFilterFrameContext {
            loop_filter_level: [10, 10, 0, 0],
            loop_filter_sharpness: 0,
            loop_filter_delta_enabled: false,
            loop_filter_ref_deltas: [0; TOTAL_REFS_PER_FRAME],
            loop_filter_mode_deltas: [0; 2],
            delta_lf_multi: false,
            mi_rows: 2,
            mi_cols: 2,
            num_planes: 1,
            bit_depth: 8,
            subsampling_x: 1,
            subsampling_y: 1,
            frame_width: 8,
            frame_height: 8,
            is_intra: &ctr,
            skip: &|_, _| false,
            ref_frame: &|_, _| INTRA_FRAME as u8,
            mode: &|_, _| 0u8,
            segment_id: &|_, _| 0u8,
            delta_lf: &|_, _, _| 0i8,
            seg_feature_active: &|_, _| false,
            seg_feature_data: &|_, _| 0i16,
            lf_tx_size: &|_, _, _| crate::cdf::TX_4X4,
            mi_size: &|_, _| crate::cdf::BLOCK_8X8,
        };
        let mut samples = vec![100i32; 8 * 8];
        let mut planes = vec![PlaneBuffer {
            rows: 8,
            cols: 8,
            samples: &mut samples,
        }];
        loop_filter_frame(&ctx, &mut planes);
        // For 2x2 mi-grid, 1 luma plane, 2 passes: each pass walks
        // 2 rows × 2 cols = 4 cells. Of these, the on-screen gate
        // drops `pass==0 && x==0` (2 cells) and `pass==1 && y==0`
        // (2 cells), so the `is_intra` predicate fires on the
        // remaining 2 + 2 = 4 cells.
        assert_eq!(
            edges.get(),
            4,
            "expected 4 on-screen edges across both passes"
        );
    }
}