oxideav-vp8 0.2.2

Pure-Rust VP8 video 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
//! Encoder-side integer-pixel motion-search primitives.
//!
//! VP8's bitstream (RFC 6386) is silent on how an encoder picks its
//! motion vectors — §2 explicitly notes that the standard "does not
//! specify a particular encoding algorithm". The only on-wire constraint
//! is the §17.1 range: each motion-vector component is a signed integer
//! in `[-1023, +1023]` quarter-pixels of luma displacement, with whole
//! pixels at multiples of 4 (§17.1 page 108).
//!
//! This module is the smallest piece of infrastructure that a non-zero
//! MV codepath needs: given a source 16×16 luma block and a reference
//! plane, return a whole-pixel candidate MV that minimises the
//! sum-of-absolute-differences (SAD) between the source and the
//! reference patch the MV would select.
//!
//! ## Scope
//!
//! * **Whole-pixel descent followed by §18.3 half-pixel and quarter-
//!   pixel refinement.** [`small_diamond_search_luma`] finds the best
//!   integer-pixel MV via a 4-neighbour descent against the §20.14
//!   edge-replicating fetch primitive; [`half_pixel_refine_luma`] then
//!   probes the 8 half-pixel offsets around that whole-pixel result;
//!   [`quarter_pixel_refine_luma`] then probes the 8 quarter-pixel
//!   offsets around the half-pixel result. Each sub-pixel candidate is
//!   evaluated through the §18.3 six-tap synthesis the decoder runs
//!   (`version == 0` bicubic tap set), keyed by `(stored_luma_mv(mv) &
//!   7)` — so a §17 quarter-pixel MV of magnitude 1 selects the
//!   `1/4`-position row of the §18.3 filter table (`{ 2, -11, 108, 36,
//!   -8, 1 }`), magnitude 3 the `3/4`-position row, and so on. The
//!   returned MV is always on the §17 quarter-pixel grid (the on-wire
//!   resolution of `mv` per §17.1) and inside `[MV_MIN, MV_MAX]`.
//! * **Luma only.** The chroma plane is not sampled — the §18.1
//!   `chroma_mv = avg(v, v, v, v)` derivation means a half-pixel luma
//!   MV maps to a sub-pixel chroma MV by construction; sampling chroma
//!   SAD per candidate would noticeably increase per-iteration cost
//!   without typically changing the picked MV.
//! * **Small-diamond integer descent.** A 4-neighbour (N / S / E / W at
//!   ±1 whole pixel) descent from a caller-supplied center, terminating
//!   when no neighbour improves the SAD or after `max_iters` iterations.
//!   Larger integer patterns (hex, square, diamond-with-radius-N) are
//!   deferred.
//!
//! ## §17.1 range clamp
//!
//! All candidate MVs are clamped into `[MV_MIN, MV_MAX]` =
//! `[-1023, +1023]` quarter-pixels before the reference fetch. The
//! underlying §20.14 `build_mc_border` edge replication inside
//! [`crate::motion_comp::fetch_block_whole_pixel`] /
//! [`crate::motion_comp::fetch_block_halo`] keeps a candidate that
//! walks the patch (or the sixtap halo) off the picture safe: the fetch
//! just reads the nearest in-bounds row / column. The §17.1 clamp is
//! applied because anything outside that range cannot be coded in the
//! bitstream regardless of whether the reference fetch would be valid.
//!
//! ## Reference
//!
//! * RFC 6386 §2 — encoder algorithm not specified.
//! * RFC 6386 §17.1 — MV component range `[-1023, +1023]`, whole pixels
//!   at multiples of 4 (i.e. half-pixel = multiples of 2 = the
//!   [`HALF_PIXEL_STEP`] grid).
//! * RFC 6386 §18.1 — `stored_luma_mv` doubling that turns a §17
//!   quarter-pixel MV into the §18 eighth-pixel resolution the §18.3
//!   filter table indexes.
//! * RFC 6386 §18.3 — six-tap sub-pixel interpolation (`version == 0`
//!   bicubic / `version != 0` bilinear); the half-pixel refinement runs
//!   the same `filter_block_4x4` synthesis the decoder will reproduce.
//! * RFC 6386 §20.14 — `build_mc_border` edge replication for fetches
//!   that walk off the picture.

use crate::motion_comp::{
    fetch_block_whole_pixel, filter_block_4x4, filter_set_for_version, stored_luma_mv,
};
use crate::motion_vector::Mv;

/// Minimum value of a single §17.1 MV component (quarter-pixel units).
pub const MV_MIN: i16 = -1023;

/// Maximum value of a single §17.1 MV component (quarter-pixel units).
pub const MV_MAX: i16 = 1023;

/// One whole-pixel step in §17 quarter-pixel units (`4 quarter-pixels`
/// per whole pixel).
pub const WHOLE_PIXEL_STEP: i16 = 4;

/// One half-pixel step in §17 quarter-pixel units (`2 quarter-pixels`
/// per half pixel). After §18.1 doubling this becomes the §18.3
/// eighth-pixel fraction `4` — i.e. the symmetric half-pixel tap row of
/// the §18.3 `filters` table (row 4: `{ 3, -16, 77, 77, -16, 3 }`).
pub const HALF_PIXEL_STEP: i16 = 2;

/// One quarter-pixel step in §17 quarter-pixel units (`1` per quarter
/// pixel — §17.1 codes `V` in quarter-pixels directly). After §18.1
/// doubling this becomes the §18.3 eighth-pixel fraction `2` — i.e. the
/// `1/4`-position tap row of the §18.3 `filters` table (row 2:
/// `{ 2, -11, 108, 36, -8, 1 }`). A `QUARTER_PIXEL_STEP` of magnitude 3
/// likewise selects row 6 (`3/4`, the reverse of row 2).
pub const QUARTER_PIXEL_STEP: i16 = 1;

/// Pixel-wise sum of absolute differences between two 16×16 blocks.
///
/// SAD is the cheapest distortion metric a motion search can use: an
/// `O(N)` integer subtract-abs-accumulate that does not require any
/// reconstruction round-trip. Returned as `u32` because a fully-
/// saturated 16×16 block sums to `256 * 255 = 65_280`, comfortably
/// inside `u32` and inside `i32` for downstream arithmetic.
#[inline]
pub fn block_sad_16x16(src: &[u8; 256], pred: &[u8; 256]) -> u32 {
    let mut acc: u32 = 0;
    for i in 0..256 {
        let s = src[i] as i32;
        let p = pred[i] as i32;
        acc += (s - p).unsigned_abs();
    }
    acc
}

/// A borrow of one reference frame's **luma plane** sized for motion
/// search at whole-pixel granularity.
///
/// The search only ever samples luma (the §18.1 `chroma_mv = avg(v, v,
/// v, v)` derivation means a whole-pixel luma MV maps directly to a
/// whole-pixel chroma MV, and chroma SAD adds noticeably more cost per
/// candidate without changing which MV the descent picks). This struct
/// bundles the four parameters every per-candidate fetch needs into a
/// single argument, keeping the function signatures inside this module
/// readable.
#[derive(Debug, Clone, Copy)]
pub struct LumaRef<'a> {
    /// The reference frame's luma plane, row-major.
    pub plane: &'a [u8],
    /// Stride of `plane` in bytes (= the row pitch).
    pub stride: usize,
    /// Plane width in pixels (= the visible / coded luma width).
    pub width: usize,
    /// Plane height in pixels (= the visible / coded luma height).
    pub height: usize,
}

/// A motion-search result: the best whole-pixel MV the search found
/// and the SAD it achieved against the source.
///
/// `mv` is in §17 quarter-pixel units (whole-pixel ⇒ multiples of
/// `WHOLE_PIXEL_STEP = 4`) and is clamped into `[MV_MIN, MV_MAX]`
/// per §17.1. `sad` is the [`block_sad_16x16`] value at that MV.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SearchResult {
    /// The selected motion vector (§17.1 quarter-pixel units,
    /// whole-pixel = multiples of `WHOLE_PIXEL_STEP`).
    pub mv: Mv,
    /// The 16×16 luma SAD between the source block and the reference
    /// patch [`mv`](Self::mv) selects.
    pub sad: u32,
}

/// Compute the 16×16 luma SAD for one whole-pixel MV candidate.
///
/// The reference plane is sampled at `(mb_col * 16, mb_row * 16) +
/// (mv >> 2)` whole pixels (with §20.14 edge replication for any rows
/// or columns past the picture boundary). The MV must be a whole-pixel
/// vector (both components multiples of [`WHOLE_PIXEL_STEP`]) and must
/// already be clamped into `[MV_MIN, MV_MAX]` — debug builds assert
/// both; release builds silently round/clamp via the underlying
/// `fetch_block_whole_pixel` call.
///
/// This is the per-candidate SAD evaluator used by
/// [`small_diamond_search_luma`]; it is exposed publicly so a future
/// alternative search shape (hex, full-search, …) can be built against
/// the same evaluator.
pub fn mb_luma_sad_at_whole_mv(
    reference: LumaRef<'_>,
    mb_col: usize,
    mb_row: usize,
    src_y: &[u8; 256],
    mv: Mv,
) -> u32 {
    debug_assert_eq!(mv.row % WHOLE_PIXEL_STEP, 0, "MV row must be whole-pixel");
    debug_assert_eq!(mv.col % WHOLE_PIXEL_STEP, 0, "MV col must be whole-pixel");
    debug_assert!((MV_MIN..=MV_MAX).contains(&mv.row));
    debug_assert!((MV_MIN..=MV_MAX).contains(&mv.col));

    let blk_x0 = mb_col * 16;
    let blk_y0 = mb_row * 16;
    // §18.1 stored_luma_mv doubles the §17 quarter-pixel vector into
    // the §18 eighth-pixel resolution `fetch_block_whole_pixel`
    // consumes. For a whole-pixel input the fractional bits stay zero
    // after doubling so the fetch collapses to the §18.3 page 115
    // "subblock is simply copied" path.
    let mv_eighth = stored_luma_mv(mv);

    let mut pred = [0u8; 256];
    for sub_r in 0..4 {
        for sub_c in 0..4 {
            let blk_x = blk_x0 + sub_c * 4;
            let blk_y = blk_y0 + sub_r * 4;
            let patch = fetch_block_whole_pixel(
                reference.plane,
                reference.stride,
                reference.width,
                reference.height,
                blk_x,
                blk_y,
                mv_eighth,
            );
            // Place the 4×4 patch at the (sub_r, sub_c) slot of the
            // 16×16 prediction.
            for r in 0..4 {
                let dst_row = sub_r * 4 + r;
                let dst_col = sub_c * 4;
                pred[dst_row * 16 + dst_col..dst_row * 16 + dst_col + 4]
                    .copy_from_slice(&patch[r * 4..r * 4 + 4]);
            }
        }
    }

    block_sad_16x16(src_y, &pred)
}

/// Clamp a §17.1 candidate component into `[MV_MIN, MV_MAX]`.
#[inline]
fn clamp_component(v: i32) -> i16 {
    v.clamp(MV_MIN as i32, MV_MAX as i32) as i16
}

/// Round `v` toward zero to the nearest whole-pixel multiple of
/// [`WHOLE_PIXEL_STEP`]. Used to coerce a caller-supplied center MV
/// onto the whole-pixel grid the search visits.
#[inline]
fn snap_to_whole_pixel(v: i16) -> i16 {
    (v / WHOLE_PIXEL_STEP) * WHOLE_PIXEL_STEP
}

/// Small-diamond integer-pixel motion search for a 16×16 luma block.
///
/// Iterates a 4-neighbour (N, S, W, E at ±1 whole pixel each) descent
/// from `center`, replacing the current best with whichever neighbour
/// produces a strictly smaller SAD. Terminates when no neighbour
/// improves the SAD or after `max_iters` iterations (whichever comes
/// first). The returned [`SearchResult::mv`] is always on the
/// whole-pixel grid and inside `[MV_MIN, MV_MAX]`; the returned `sad`
/// is its [`block_sad_16x16`] value.
///
/// `center` is taken as the §17.1 starting MV; its components are
/// snapped to the whole-pixel grid (toward zero) and clamped into
/// `[MV_MIN, MV_MAX]` before the search begins.
///
/// Pass `max_iters = 0` for a pure no-op probe (returns the SAD at the
/// snapped/clamped `center` with no neighbour exploration).
pub fn small_diamond_search_luma(
    reference: LumaRef<'_>,
    mb_col: usize,
    mb_row: usize,
    src_y: &[u8; 256],
    center: Mv,
    max_iters: u32,
) -> SearchResult {
    // Clamp first, *then* snap toward zero — snapping first can leave
    // the clamped result off the whole-pixel grid because §17.1's
    // boundary (±1023) is not a multiple of `WHOLE_PIXEL_STEP` (4).
    let mut best_mv = Mv {
        row: snap_to_whole_pixel(clamp_component(center.row as i32)),
        col: snap_to_whole_pixel(clamp_component(center.col as i32)),
    };
    let mut best_sad = mb_luma_sad_at_whole_mv(reference, mb_col, mb_row, src_y, best_mv);

    // 4-neighbour offsets (N, S, W, E) in §17 quarter-pixel units.
    let neighbours: [(i32, i32); 4] = [
        (-(WHOLE_PIXEL_STEP as i32), 0),
        (WHOLE_PIXEL_STEP as i32, 0),
        (0, -(WHOLE_PIXEL_STEP as i32)),
        (0, WHOLE_PIXEL_STEP as i32),
    ];

    for _ in 0..max_iters {
        let mut improved = false;
        for (drow, dcol) in neighbours {
            // Clamp first, then snap toward zero so the candidate
            // never drifts off the whole-pixel grid (§17.1's ±1023
            // boundary is not a multiple of WHOLE_PIXEL_STEP).
            let cand_row = snap_to_whole_pixel(clamp_component(best_mv.row as i32 + drow));
            let cand_col = snap_to_whole_pixel(clamp_component(best_mv.col as i32 + dcol));
            // Clamping can collapse a candidate back onto best_mv at
            // the §17.1 boundary; skip those to avoid recomputing the
            // identical SAD.
            if cand_row == best_mv.row && cand_col == best_mv.col {
                continue;
            }
            let cand_mv = Mv {
                row: cand_row,
                col: cand_col,
            };
            let cand_sad = mb_luma_sad_at_whole_mv(reference, mb_col, mb_row, src_y, cand_mv);
            if cand_sad < best_sad {
                best_sad = cand_sad;
                best_mv = cand_mv;
                improved = true;
            }
        }
        if !improved {
            // Local minimum on the small-diamond pattern; nothing
            // larger would dislodge it without a bigger neighbourhood.
            break;
        }
    }

    SearchResult {
        mv: best_mv,
        sad: best_sad,
    }
}

/// Compute the 16×16 luma SAD for one §17 quarter-pixel MV candidate
/// (potentially at half-pixel resolution).
///
/// Unlike [`mb_luma_sad_at_whole_mv`] this routine accepts any §17.1
/// MV whose components are multiples of [`HALF_PIXEL_STEP`] (i.e.
/// whole-pixel ∪ half-pixel). The §18.3 prediction synthesis goes
/// through the same [`filter_block_4x4`] / [`crate::motion_comp::sixtap_2d`]
/// path the decoder runs at decode time, with the version=0 bicubic
/// six-tap tap-set — the encoder commits to `version == 0` in every
/// emitted frame tag, so this is the tap-set the decoder will reproduce
/// on its half-pixel MV path. Sub-blocks whose §18.1-doubled fraction
/// is zero (whole-pixel) collapse to the §18.3 "subblock is simply
/// copied" path inside [`filter_block_4x4`].
///
/// The MV must be inside `[MV_MIN, MV_MAX]` per §17.1; debug builds
/// assert. Quarter-pixel positions (`mv.row & 1 != 0`, etc.) are not
/// rejected — the underlying §18.3 filter table is indexed by
/// `(stored_luma_mv(mv) & 7)` and supports all eight fractions — but
/// the [`half_pixel_refine_luma`] caller only ever produces half-pixel
/// candidates.
pub fn mb_luma_sad_at_mv(
    reference: LumaRef<'_>,
    mb_col: usize,
    mb_row: usize,
    src_y: &[u8; 256],
    mv: Mv,
) -> u32 {
    debug_assert!((MV_MIN..=MV_MAX).contains(&mv.row));
    debug_assert!((MV_MIN..=MV_MAX).contains(&mv.col));

    let blk_x0 = mb_col * 16;
    let blk_y0 = mb_row * 16;
    // §18.1 stored_luma_mv doubles the §17 quarter-pixel vector into
    // the §18 eighth-pixel resolution the §18.3 filter table indexes.
    let mv_eighth = stored_luma_mv(mv);
    // §20.14 setup_subpixel_filters: `version == 0` selects the bicubic
    // six-tap set. The encoder commits to `version == 0` in its emitted
    // frame tag, so the decoder will run the same tap-set when re-decoding
    // this candidate.
    let filters = filter_set_for_version(0).taps();

    let mut pred = [0u8; 256];
    for sub_r in 0..4 {
        for sub_c in 0..4 {
            let blk_x = blk_x0 + sub_c * 4;
            let blk_y = blk_y0 + sub_r * 4;
            let patch = filter_block_4x4(
                reference.plane,
                reference.stride,
                reference.width,
                reference.height,
                blk_x,
                blk_y,
                mv_eighth,
                filters,
            );
            for r in 0..4 {
                let dst_row = sub_r * 4 + r;
                let dst_col = sub_c * 4;
                pred[dst_row * 16 + dst_col..dst_row * 16 + dst_col + 4]
                    .copy_from_slice(&patch[r * 4..r * 4 + 4]);
            }
        }
    }

    block_sad_16x16(src_y, &pred)
}

/// Half-pixel motion-search refinement around a whole-pixel center —
/// RFC 6386 §18.3 / §17.1.
///
/// Given a `whole_pixel_center` MV that came out of an integer-pixel
/// search such as [`small_diamond_search_luma`], probe the eight
/// half-pixel offsets ±[`HALF_PIXEL_STEP`] in each of the (row, col)
/// axes — i.e. the 3×3 grid `{(-1, -1), (-1, 0), (-1, +1), (0, -1),
/// (0, +1), (+1, -1), (+1, 0), (+1, +1)} * HALF_PIXEL_STEP` around the
/// center, excluding the center itself — and return the MV with the
/// smallest 16×16 luma SAD across {center, all 8 neighbours}.
///
/// Each half-pixel candidate is evaluated through [`mb_luma_sad_at_mv`],
/// which runs the §18.3 six-tap synthesis (the bicubic `version == 0`
/// tap-set the encoder always commits to in its frame tag). The center's
/// SAD is recomputed (whole-pixel ⇒ the §18.3 copy path) rather than
/// passed in as an argument so the function is independent of how the
/// caller derived the integer-pixel candidate.
///
/// `whole_pixel_center` is asserted to be on the whole-pixel grid
/// (components multiples of [`WHOLE_PIXEL_STEP`]) and inside §17.1's
/// `[MV_MIN, MV_MAX]`. Candidates outside §17.1's range are clamped
/// (snapped onto the nearest in-range value); a candidate that collapses
/// onto an already-evaluated MV after clamping is skipped to avoid
/// recomputing the same SAD. Ties between the center and a neighbour go
/// to the **center** — fewer §17.2 component bits to code (the magnitude
/// 2 differential adds at least one extra bit per component vs. the
/// magnitude-0 / multiple-of-4 differential the whole-pixel center
/// carries).
///
/// This is the smallest §18.3 refinement that gives a sub-pixel
/// translation a chance of self-decoding bit-exactly: pure-translation
/// content that lies on the half-pixel grid (e.g. a `+0.5` luma pixel
/// shift) is fundamentally unreachable from a whole-pixel-only descent.
pub fn half_pixel_refine_luma(
    reference: LumaRef<'_>,
    mb_col: usize,
    mb_row: usize,
    src_y: &[u8; 256],
    whole_pixel_center: Mv,
) -> SearchResult {
    debug_assert_eq!(
        whole_pixel_center.row % WHOLE_PIXEL_STEP,
        0,
        "whole_pixel_center.row must be on the whole-pixel grid"
    );
    debug_assert_eq!(
        whole_pixel_center.col % WHOLE_PIXEL_STEP,
        0,
        "whole_pixel_center.col must be on the whole-pixel grid"
    );
    debug_assert!((MV_MIN..=MV_MAX).contains(&whole_pixel_center.row));
    debug_assert!((MV_MIN..=MV_MAX).contains(&whole_pixel_center.col));

    let mut best_mv = whole_pixel_center;
    let mut best_sad = mb_luma_sad_at_mv(reference, mb_col, mb_row, src_y, best_mv);

    // 8 half-pixel offsets around the center, in §17 quarter-pixel units.
    let step = HALF_PIXEL_STEP as i32;
    let offsets: [(i32, i32); 8] = [
        (-step, -step),
        (-step, 0),
        (-step, step),
        (0, -step),
        (0, step),
        (step, -step),
        (step, 0),
        (step, step),
    ];

    for (drow, dcol) in offsets {
        let cand_row = clamp_component(whole_pixel_center.row as i32 + drow);
        let cand_col = clamp_component(whole_pixel_center.col as i32 + dcol);
        // After §17.1 clamping a half-pixel candidate at the boundary
        // can collapse back onto the center (e.g. row=1023 + 2 ⇒ 1023);
        // skip those to avoid recomputing the identical SAD.
        if cand_row == best_mv.row && cand_col == best_mv.col {
            continue;
        }
        let cand_mv = Mv {
            row: cand_row,
            col: cand_col,
        };
        let cand_sad = mb_luma_sad_at_mv(reference, mb_col, mb_row, src_y, cand_mv);
        // Tie goes to the previous best (which starts as the center,
        // so on equality the whole-pixel center wins — fewer §17.2
        // component bits to code).
        if cand_sad < best_sad {
            best_sad = cand_sad;
            best_mv = cand_mv;
        }
    }

    SearchResult {
        mv: best_mv,
        sad: best_sad,
    }
}

/// Quarter-pixel motion-search refinement around a half-pixel center —
/// RFC 6386 §18.3 / §17.1.
///
/// Given a `half_pixel_center` MV that came out of
/// [`half_pixel_refine_luma`] (components multiples of
/// [`HALF_PIXEL_STEP`]), probe the eight quarter-pixel offsets
/// ±[`QUARTER_PIXEL_STEP`] in each of the (row, col) axes — i.e. the
/// 3×3 grid around the center excluding the center itself — and return
/// the MV with the smallest 16×16 luma SAD across {center, all 8
/// neighbours}.
///
/// Each quarter-pixel candidate is evaluated through [`mb_luma_sad_at_mv`],
/// which runs the §18.3 six-tap synthesis (the bicubic `version == 0`
/// tap-set the encoder always commits to in its frame tag). After §18.1
/// doubling, a §17 quarter-pixel offset becomes the §18.3 eighth-pixel
/// fraction `2` (`1/4` tap row, `{ 2, -11, 108, 36, -8, 1 }`) or `6`
/// (`3/4` tap row, the reverse) depending on whether the center already
/// carried a half-pixel offset on that axis. The center's SAD is
/// recomputed (via the §18.3 sixtap, which collapses to the copy path
/// only when both fractions are zero) so the function is independent of
/// how the caller derived the half-pixel candidate.
///
/// `half_pixel_center` is asserted to be on the half-pixel grid
/// (components multiples of [`HALF_PIXEL_STEP`]) and inside §17.1's
/// `[MV_MIN, MV_MAX]`. Candidates outside §17.1's range are clamped
/// (snapped onto the nearest in-range value); a candidate that collapses
/// onto an already-evaluated MV after clamping is skipped to avoid
/// recomputing the same SAD. Ties between the center and a neighbour go
/// to the **center** — fewer §17.2 component bits to code (one extra
/// quarter-pixel offset on a component adds at least one §17.2 long-form
/// bit per component vs. the half- or whole-pixel center).
///
/// This is the smallest §18.3 refinement on top of [`half_pixel_refine_luma`]
/// that gives a pure-translation source landing on the quarter-pixel
/// grid (e.g. a `+0.25` luma-pixel shift) a chance of self-decoding
/// bit-exactly — content at that fractional offset is fundamentally
/// unreachable from a half-pixel-only descent.
pub fn quarter_pixel_refine_luma(
    reference: LumaRef<'_>,
    mb_col: usize,
    mb_row: usize,
    src_y: &[u8; 256],
    half_pixel_center: Mv,
) -> SearchResult {
    debug_assert_eq!(
        half_pixel_center.row % HALF_PIXEL_STEP,
        0,
        "half_pixel_center.row must be on the half-pixel grid"
    );
    debug_assert_eq!(
        half_pixel_center.col % HALF_PIXEL_STEP,
        0,
        "half_pixel_center.col must be on the half-pixel grid"
    );
    debug_assert!((MV_MIN..=MV_MAX).contains(&half_pixel_center.row));
    debug_assert!((MV_MIN..=MV_MAX).contains(&half_pixel_center.col));

    let mut best_mv = half_pixel_center;
    let mut best_sad = mb_luma_sad_at_mv(reference, mb_col, mb_row, src_y, best_mv);

    // 8 quarter-pixel offsets around the center, in §17 quarter-pixel units.
    let step = QUARTER_PIXEL_STEP as i32;
    let offsets: [(i32, i32); 8] = [
        (-step, -step),
        (-step, 0),
        (-step, step),
        (0, -step),
        (0, step),
        (step, -step),
        (step, 0),
        (step, step),
    ];

    for (drow, dcol) in offsets {
        let cand_row = clamp_component(half_pixel_center.row as i32 + drow);
        let cand_col = clamp_component(half_pixel_center.col as i32 + dcol);
        // After §17.1 clamping a quarter-pixel candidate at the boundary
        // can collapse back onto the center (e.g. row=1023 + 1 ⇒ 1023);
        // skip those to avoid recomputing the identical SAD.
        if cand_row == best_mv.row && cand_col == best_mv.col {
            continue;
        }
        let cand_mv = Mv {
            row: cand_row,
            col: cand_col,
        };
        let cand_sad = mb_luma_sad_at_mv(reference, mb_col, mb_row, src_y, cand_mv);
        // Tie goes to the previous best (which starts as the half-pixel
        // center, so on equality the lower-§17.2-bit candidate wins).
        if cand_sad < best_sad {
            best_sad = cand_sad;
            best_mv = cand_mv;
        }
    }

    SearchResult {
        mv: best_mv,
        sad: best_sad,
    }
}

// ─────────────────────────────────── tests ────────────────────────────────────

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

    /// Render a luma plane sized for at least one 16×16 macroblock at
    /// `(mb_col, mb_row) = (0, 0)`, filled with a horizontal ramp
    /// `(x % 251)` so a horizontal MV shift produces a non-trivial SAD
    /// delta.
    fn ramp_plane(width: usize, height: usize) -> Vec<u8> {
        let mut p = vec![0u8; width * height];
        for y in 0..height {
            for x in 0..width {
                p[y * width + x] = ((x + y * 3) % 251) as u8;
            }
        }
        p
    }

    /// Render a luma plane that is constant background `bg` with a
    /// single bright `fg`-valued square of size `feat` placed with its
    /// top-left corner at `(fx, fy)`. The square is the only feature,
    /// so the SAD as a function of MV has a unique global minimum at
    /// the MV that aligns the candidate patch's feature with the
    /// source patch's feature — no axis-asymmetric ramp gradient to
    /// distract a greedy descent into a local minimum.
    fn feature_plane(
        width: usize,
        height: usize,
        bg: u8,
        fg: u8,
        fx: usize,
        fy: usize,
        feat: usize,
    ) -> Vec<u8> {
        let mut p = vec![bg; width * height];
        for r in 0..feat {
            for c in 0..feat {
                let y = fy + r;
                let x = fx + c;
                if x < width && y < height {
                    p[y * width + x] = fg;
                }
            }
        }
        p
    }

    /// Extract the 16×16 block at `(mb_col, mb_row)` from a plane laid
    /// out with stride `stride`.
    fn extract_mb_block(plane: &[u8], stride: usize, mb_col: usize, mb_row: usize) -> [u8; 256] {
        let mut blk = [0u8; 256];
        let x0 = mb_col * 16;
        let y0 = mb_row * 16;
        for r in 0..16 {
            let src = (y0 + r) * stride + x0;
            blk[r * 16..r * 16 + 16].copy_from_slice(&plane[src..src + 16]);
        }
        blk
    }

    #[test]
    fn block_sad_identical_inputs_is_zero() {
        let mut a = [0u8; 256];
        for (i, slot) in a.iter_mut().enumerate() {
            *slot = (i % 251) as u8;
        }
        assert_eq!(block_sad_16x16(&a, &a), 0);
    }

    #[test]
    fn block_sad_one_pixel_delta_is_one() {
        let a = [10u8; 256];
        let mut b = a;
        b[42] = 11;
        assert_eq!(block_sad_16x16(&a, &b), 1);
    }

    #[test]
    fn block_sad_saturated_difference() {
        let a = [0u8; 256];
        let b = [255u8; 256];
        assert_eq!(block_sad_16x16(&a, &b), 256 * 255);
    }

    #[test]
    fn block_sad_known_manual_sum() {
        let mut a = [0u8; 256];
        let mut b = [0u8; 256];
        // First three pixels: |10-3| + |20-5| + |30-2| = 7 + 15 + 28 = 50.
        a[0] = 10;
        b[0] = 3;
        a[1] = 20;
        b[1] = 5;
        a[2] = 30;
        b[2] = 2;
        assert_eq!(block_sad_16x16(&a, &b), 50);
    }

    /// Convenience constructor: bundle a plane + its dimensions into a
    /// [`LumaRef`] for the test's `(stride == width)` packed layout.
    fn packed_luma_ref<'a>(plane: &'a [u8], width: usize, height: usize) -> LumaRef<'a> {
        LumaRef {
            plane,
            stride: width,
            width,
            height,
        }
    }

    #[test]
    fn sad_at_zero_mv_equals_direct_block_sad() {
        // Two distinct planes: ref is the ramp, src is the same ramp
        // with one pixel offset. SAD at (0,0) MV should equal
        // block_sad_16x16(ref_block_at_origin, src_block_at_origin).
        let width = 32;
        let height = 32;
        let reference = ramp_plane(width, height);
        let mut src_plane = reference.clone();
        // Bump every fourth pixel of the MB(0,0) by 5.
        for y in 0..16 {
            for x in (0..16).step_by(4) {
                src_plane[y * width + x] = src_plane[y * width + x].wrapping_add(5);
            }
        }
        let src_blk = extract_mb_block(&src_plane, width, 0, 0);
        let ref_blk = extract_mb_block(&reference, width, 0, 0);
        let expected = block_sad_16x16(&src_blk, &ref_blk);
        let got = mb_luma_sad_at_whole_mv(
            packed_luma_ref(&reference, width, height),
            0,
            0,
            &src_blk,
            Mv { row: 0, col: 0 },
        );
        assert_eq!(got, expected);
    }

    #[test]
    fn diamond_search_finds_exact_translation_horizontal() {
        // Source MB(0,0) covers pixels (0..16, 0..16). Place the
        // source's feature inside the MB at (sx=4, sy=4) and the
        // reference's feature shifted right by 2 whole pixels — at
        // (sx=6, sy=4). Aligning the candidate patch's feature with
        // the source's feature requires MV (row=0, col=+8 quarter-
        // pixels = +2 whole pixels). The small-diamond descent picks
        // it up in two iterations.
        let width = 64;
        let height = 32;
        let reference = feature_plane(width, height, 128, 240, 6, 4, 4);
        let source_plane = feature_plane(width, height, 128, 240, 4, 4, 4);
        let src_blk = extract_mb_block(&source_plane, width, 0, 0);
        let result = small_diamond_search_luma(
            packed_luma_ref(&reference, width, height),
            0,
            0,
            &src_blk,
            Mv { row: 0, col: 0 },
            32,
        );
        assert_eq!(
            result.mv,
            Mv {
                row: 0,
                col: 2 * WHOLE_PIXEL_STEP,
            },
            "search must find the 2-whole-pixel horizontal offset"
        );
        assert_eq!(result.sad, 0, "exact translation ⇒ zero SAD");
    }

    #[test]
    fn diamond_search_finds_exact_translation_diagonal() {
        // Source feature inside MB(0,0) at (sx=5, sy=6); reference
        // feature shifted (+3 col, +2 row) at (sx=8, sy=8). Aligning
        // needs MV (row=+8 quarter-pixels = +2 whole-pixel down,
        //          col=+12 quarter-pixels = +3 whole-pixel right).
        let width = 64;
        let height = 64;
        let reference = feature_plane(width, height, 100, 220, 8, 8, 5);
        let source_plane = feature_plane(width, height, 100, 220, 5, 6, 5);
        let src_blk = extract_mb_block(&source_plane, width, 0, 0);
        let result = small_diamond_search_luma(
            packed_luma_ref(&reference, width, height),
            0,
            0,
            &src_blk,
            Mv { row: 0, col: 0 },
            64,
        );
        assert_eq!(
            result.mv,
            Mv {
                row: 2 * WHOLE_PIXEL_STEP,
                col: 3 * WHOLE_PIXEL_STEP,
            }
        );
        assert_eq!(result.sad, 0);
    }

    #[test]
    fn diamond_search_zero_iters_returns_center_sad() {
        // max_iters = 0 ⇒ no neighbour exploration, just the SAD at
        // the snapped/clamped center.
        let width = 32;
        let height = 32;
        let reference = ramp_plane(width, height);
        let src_blk = extract_mb_block(&reference, width, 0, 0);
        let result = small_diamond_search_luma(
            packed_luma_ref(&reference, width, height),
            0,
            0,
            &src_blk,
            Mv { row: 0, col: 0 },
            0,
        );
        assert_eq!(result.mv, Mv { row: 0, col: 0 });
        assert_eq!(result.sad, 0);
    }

    #[test]
    fn diamond_search_identical_source_returns_zero_mv() {
        // Source = the same MB the encoder is positioned at, with the
        // reference being the same plane: the (0, 0) MV is already the
        // global optimum; no neighbour can improve it.
        let width = 32;
        let height = 32;
        let reference = ramp_plane(width, height);
        let src_blk = extract_mb_block(&reference, width, 0, 0);
        let result = small_diamond_search_luma(
            packed_luma_ref(&reference, width, height),
            0,
            0,
            &src_blk,
            Mv { row: 0, col: 0 },
            8,
        );
        assert_eq!(result.mv, Mv { row: 0, col: 0 });
        assert_eq!(result.sad, 0);
    }

    #[test]
    fn diamond_search_snaps_center_to_whole_pixel() {
        // Caller hands in a sub-pixel center (col = 5 quarter-pixels =
        // 1.25 whole pixels). The search snaps to col = 4 toward zero
        // before starting; on a flat source/reference the result is
        // still SAD 0 (flat ⇒ all MVs equivalent).
        let width = 32;
        let height = 32;
        let reference = vec![200u8; width * height];
        let src_blk = [200u8; 256];
        let result = small_diamond_search_luma(
            packed_luma_ref(&reference, width, height),
            0,
            0,
            &src_blk,
            Mv { row: 1, col: 5 },
            0,
        );
        // Snap toward zero ⇒ row=0, col=4 — both whole-pixel grid.
        assert_eq!(result.mv.row % WHOLE_PIXEL_STEP, 0);
        assert_eq!(result.mv.col % WHOLE_PIXEL_STEP, 0);
        assert_eq!(result.mv, Mv { row: 0, col: 4 });
        assert_eq!(result.sad, 0);
    }

    #[test]
    fn diamond_search_clamps_extreme_center_to_section_17_1_range() {
        // §17.1: component must be in [-1023, +1023]. A center at i16::MAX
        // must be clamped down to 1020 (largest multiple of 4 inside +1023),
        // and the search must not panic from an out-of-range fetch.
        let width = 32;
        let height = 32;
        let reference = vec![128u8; width * height];
        let src_blk = [128u8; 256];
        let result = small_diamond_search_luma(
            packed_luma_ref(&reference, width, height),
            0,
            0,
            &src_blk,
            Mv {
                row: i16::MAX,
                col: i16::MIN,
            },
            4,
        );
        assert!((MV_MIN..=MV_MAX).contains(&result.mv.row));
        assert!((MV_MIN..=MV_MAX).contains(&result.mv.col));
        assert_eq!(result.mv.row % WHOLE_PIXEL_STEP, 0);
        assert_eq!(result.mv.col % WHOLE_PIXEL_STEP, 0);
        // Flat source ⇒ zero SAD at every legal MV.
        assert_eq!(result.sad, 0);
    }

    #[test]
    fn diamond_search_off_picture_edge_replicate_does_not_panic() {
        // Anchor the search at MB(0, 0) (top-left), with a candidate
        // MV that would walk past the top-left edge. The §20.14
        // build_mc_border edge-replicate inside fetch_block_whole_pixel
        // must absorb the out-of-plane fetch; the search itself
        // remains well-defined and returns a non-panicking result.
        let width = 32;
        let height = 32;
        let reference = ramp_plane(width, height);
        let src_blk = extract_mb_block(&reference, width, 0, 0);
        let result = small_diamond_search_luma(
            packed_luma_ref(&reference, width, height),
            0,
            0,
            &src_blk,
            Mv { row: -64, col: -64 },
            8,
        );
        // Just confirming no panic and the result is on the grid + in range.
        assert_eq!(result.mv.row % WHOLE_PIXEL_STEP, 0);
        assert_eq!(result.mv.col % WHOLE_PIXEL_STEP, 0);
        assert!((MV_MIN..=MV_MAX).contains(&result.mv.row));
        assert!((MV_MIN..=MV_MAX).contains(&result.mv.col));
    }

    #[test]
    fn diamond_search_never_increases_sad_from_center() {
        // The search must never *worsen* the candidate it returned vs
        // the SAD at the snapped center; descent semantics require
        // result.sad <= sad(snapped_center). Use a single-feature
        // landscape so the SAD topology has one global minimum and the
        // greedy small-diamond reaches it from a nearby center.
        let width = 48;
        let height = 48;
        // Source feature inside MB(0,0) at (sx=4, sy=6), reference
        // feature shifted to (sx=6, sy=6) — convergence MV
        // (row=0, col=+2 whole-pixels = +8 quarter-pixels).
        let reference = feature_plane(width, height, 90, 230, 6, 6, 5);
        let source_plane = feature_plane(width, height, 90, 230, 4, 6, 5);
        let src_blk = extract_mb_block(&source_plane, width, 0, 0);
        let ref_luma = packed_luma_ref(&reference, width, height);
        let sad_at_center =
            mb_luma_sad_at_whole_mv(ref_luma, 0, 0, &src_blk, Mv { row: 0, col: 0 });
        let result = small_diamond_search_luma(ref_luma, 0, 0, &src_blk, Mv { row: 0, col: 0 }, 8);
        assert!(
            result.sad <= sad_at_center,
            "descent must not increase SAD (center={sad_at_center}, result={})",
            result.sad
        );
        // The exact optimum lives at col = 2 whole pixels = 8 in §17 units.
        assert_eq!(result.mv, Mv { row: 0, col: 8 });
        assert_eq!(result.sad, 0);
    }

    #[test]
    fn half_pixel_refine_keeps_center_on_flat_source() {
        // Flat source ⇒ SAD is zero at every MV ⇒ tie-break keeps the
        // whole-pixel center (fewer §17.2 component bits).
        let width = 32;
        let height = 32;
        let reference = vec![200u8; width * height];
        let src_blk = [200u8; 256];
        let result = half_pixel_refine_luma(
            packed_luma_ref(&reference, width, height),
            0,
            0,
            &src_blk,
            Mv { row: 0, col: 0 },
        );
        assert_eq!(result.mv, Mv { row: 0, col: 0 });
        assert_eq!(result.sad, 0);
    }

    /// Render a luma plane with a non-degenerate 2D ramp: distinct row
    /// and column gradients with different slopes. Used by the
    /// half-pixel refinement test so the §18.3 filter at every half-
    /// pixel position produces a distinct prediction (no row/column
    /// invariance lets a diagonal half-pixel candidate tie a cardinal).
    fn two_d_ramp_plane(width: usize, height: usize) -> Vec<u8> {
        let mut p = vec![0u8; width * height];
        for y in 0..height {
            for x in 0..width {
                // Different per-axis slopes so the sixtap response at
                // each (dx, dy) half-pixel offset is distinct.
                let v = ((x as i32) + 3 * (y as i32)).clamp(0, 240);
                p[y * width + x] = v as u8;
            }
        }
        p
    }

    #[test]
    fn half_pixel_refine_finds_half_pixel_horizontal_shift() {
        // Build a synthetic source whose MB(1, 1) block is EXACTLY the
        // §18.3 sixtap synthesis of the reference at MV (0, +1/2 px) so
        // there is one and only one half-pixel candidate that drives the
        // SAD to zero (the two_d_ramp plane has a distinct response at
        // every half-pixel position, breaking row-invariance ties).
        let width = 64;
        let height = 64;
        let reference = two_d_ramp_plane(width, height);
        // Construct src_blk = sixtap_2d(reference at MB(1,0), col-half).
        // That is: take the reference and apply mb_luma_sad_at_mv's own
        // prediction path with the ground-truth MV — the resulting block
        // is by construction the SAD-zero source for MV (0, HALF_PIXEL_STEP).
        let ref_luma = packed_luma_ref(&reference, width, height);
        // Synthesize the source MB by re-using the §18.3 prediction at
        // the ground-truth half-pixel MV, then verify the descent finds
        // it.
        let truth_mv = Mv {
            row: 0,
            col: HALF_PIXEL_STEP,
        };
        // Build the source block by running the same per-sub-block
        // filter the SAD evaluator uses internally — sample-exact mirror
        // of mb_luma_sad_at_mv's prediction (so SAD == 0 at truth_mv).
        let mut src_blk = [0u8; 256];
        let mv_eighth = stored_luma_mv(truth_mv);
        let filters = filter_set_for_version(0).taps();
        let blk_x0 = 16;
        let blk_y0 = 16;
        for sub_r in 0..4 {
            for sub_c in 0..4 {
                let blk_x = blk_x0 + sub_c * 4;
                let blk_y = blk_y0 + sub_r * 4;
                let patch = filter_block_4x4(
                    &reference, width, width, height, blk_x, blk_y, mv_eighth, filters,
                );
                for r in 0..4 {
                    let dst_row = sub_r * 4 + r;
                    let dst_col = sub_c * 4;
                    src_blk[dst_row * 16 + dst_col..dst_row * 16 + dst_col + 4]
                        .copy_from_slice(&patch[r * 4..r * 4 + 4]);
                }
            }
        }
        let result = half_pixel_refine_luma(ref_luma, 1, 1, &src_blk, Mv { row: 0, col: 0 });
        assert_eq!(
            result.mv, truth_mv,
            "half-pixel +0.5 px horizontal shift expected, got {:?}",
            result.mv
        );
        // The source equals the §18.3 prediction at truth_mv by
        // construction ⇒ SAD must be exactly zero at the picked MV.
        assert_eq!(result.sad, 0);
    }

    #[test]
    fn half_pixel_refine_never_worsens_sad() {
        // The refinement is descent semantics: the returned SAD must be
        // ≤ the SAD at the whole-pixel center. Use a randomish-but-
        // deterministic plane so any half-pixel candidate has a chance
        // of beating the center.
        let width = 48;
        let height = 48;
        let reference = ramp_plane(width, height);
        // Source = ramp + low-amplitude horizontal phase shift built by
        // averaging neighbours (a manual half-pixel filter is overkill;
        // any non-trivial source vs. ref will exercise the descent).
        let mut src_plane = reference.clone();
        for y in 0..height {
            for x in 1..(width - 1) {
                let a = reference[y * width + x - 1] as u32;
                let b = reference[y * width + x] as u32;
                src_plane[y * width + x] = ((a + b) / 2) as u8;
            }
        }
        let src_blk = extract_mb_block(&src_plane, width, 1, 1);
        let ref_luma = packed_luma_ref(&reference, width, height);
        let sad_at_center = mb_luma_sad_at_mv(ref_luma, 1, 1, &src_blk, Mv { row: 0, col: 0 });
        let result = half_pixel_refine_luma(ref_luma, 1, 1, &src_blk, Mv { row: 0, col: 0 });
        assert!(
            result.sad <= sad_at_center,
            "half-pixel refinement must not worsen SAD (center={sad_at_center}, result={})",
            result.sad
        );
        // The chosen MV must be a half-pixel multiple of HALF_PIXEL_STEP
        // (i.e. ±0 or ±HALF_PIXEL_STEP on each component).
        assert!(result.mv.row.unsigned_abs() <= HALF_PIXEL_STEP as u16);
        assert!(result.mv.col.unsigned_abs() <= HALF_PIXEL_STEP as u16);
        assert_eq!(result.mv.row % HALF_PIXEL_STEP, 0);
        assert_eq!(result.mv.col % HALF_PIXEL_STEP, 0);
    }

    #[test]
    fn half_pixel_refine_clamps_at_section_17_1_boundary() {
        // Center at the §17.1 boundary (row=+1020 = 255 whole pixels);
        // half-pixel candidates that would step past the boundary
        // (col = MV_MAX = 1023 plus HALF_PIXEL_STEP) get clamped back
        // onto an already-evaluated MV and skipped. The result must
        // still be on the half-pixel grid and inside §17.1's range.
        let width = 32;
        let height = 32;
        let reference = vec![100u8; width * height];
        let src_blk = [100u8; 256];
        let result = half_pixel_refine_luma(
            packed_luma_ref(&reference, width, height),
            0,
            0,
            &src_blk,
            Mv {
                row: 1020,
                col: 1020,
            },
        );
        assert!((MV_MIN..=MV_MAX).contains(&result.mv.row));
        assert!((MV_MIN..=MV_MAX).contains(&result.mv.col));
        // Flat ⇒ tie-break keeps the center.
        assert_eq!(
            result.mv,
            Mv {
                row: 1020,
                col: 1020
            }
        );
        assert_eq!(result.sad, 0);
    }

    #[test]
    fn mb_luma_sad_at_mv_whole_pixel_matches_whole_pixel_helper() {
        // For a whole-pixel MV, mb_luma_sad_at_mv must equal
        // mb_luma_sad_at_whole_mv (the §18.3 sixtap collapses to the
        // copy path when the fraction is zero, so the two evaluators
        // produce the same prediction).
        let width = 32;
        let height = 32;
        let reference = ramp_plane(width, height);
        let mut src_plane = reference.clone();
        // Perturb the source so the SAD isn't trivially zero.
        for y in 0..height {
            src_plane[y * width + (y % width)] = src_plane[y * width + (y % width)].wrapping_add(7);
        }
        let src_blk = extract_mb_block(&src_plane, width, 0, 0);
        let ref_luma = packed_luma_ref(&reference, width, height);
        let mv = Mv { row: 0, col: 0 };
        let sad_general = mb_luma_sad_at_mv(ref_luma, 0, 0, &src_blk, mv);
        let sad_whole = mb_luma_sad_at_whole_mv(ref_luma, 0, 0, &src_blk, mv);
        assert_eq!(sad_general, sad_whole);
    }

    #[test]
    fn quarter_pixel_refine_keeps_center_on_flat_source() {
        // Flat source ⇒ SAD is zero at every MV ⇒ tie-break keeps the
        // half-pixel center (fewer §17.2 component bits than a quarter-
        // pixel offset on the same axis).
        let width = 32;
        let height = 32;
        let reference = vec![200u8; width * height];
        let src_blk = [200u8; 256];
        let result = quarter_pixel_refine_luma(
            packed_luma_ref(&reference, width, height),
            0,
            0,
            &src_blk,
            Mv {
                row: 0,
                col: HALF_PIXEL_STEP,
            },
        );
        assert_eq!(
            result.mv,
            Mv {
                row: 0,
                col: HALF_PIXEL_STEP,
            }
        );
        assert_eq!(result.sad, 0);
    }

    /// Render a luma plane with sharp luminance steps superimposed on a
    /// gentle ramp — designed so the §18.3 sixtap response is distinct
    /// at every fractional offset (the ramp alone is linear and the
    /// sixtap collapses to identity rounding on it, so every fractional
    /// MV yields the same byte pattern; the steps break that
    /// degeneracy).
    fn stepped_plane(width: usize, height: usize) -> Vec<u8> {
        let mut p = vec![0u8; width * height];
        for y in 0..height {
            for x in 0..width {
                // Base ramp + per-column step pattern (every 3rd column
                // takes a +40 luma bump) + per-row stripe (every 5th
                // row takes a -20 luma dip). High-frequency content
                // makes the §18.3 sixtap distinguish quarter-pixel
                // offsets from half-pixel and whole-pixel offsets.
                let base = ((x as i32) + 2 * (y as i32)).clamp(0, 180);
                let step = if x % 3 == 0 { 40 } else { 0 };
                let stripe = if y % 5 == 0 { -20 } else { 0 };
                let v = (base + step + stripe).clamp(0, 240);
                p[y * width + x] = v as u8;
            }
        }
        p
    }

    #[test]
    fn quarter_pixel_refine_finds_quarter_pixel_horizontal_shift() {
        // Build a synthetic source whose MB(1, 1) block is EXACTLY the
        // §18.3 sixtap synthesis of the reference at MV (0, +1/4 px) so
        // there is one and only one quarter-pixel candidate that drives
        // the SAD to zero. The stepped_plane carries high-frequency
        // content the sixtap distinguishes at every fractional offset
        // (a purely linear plane like two_d_ramp gives identical bytes
        // for every quarter/half-pixel MV in `u8` arithmetic).
        let width = 64;
        let height = 64;
        let reference = stepped_plane(width, height);
        let ref_luma = packed_luma_ref(&reference, width, height);
        let truth_mv = Mv {
            row: 0,
            col: QUARTER_PIXEL_STEP,
        };
        // Source MB = sample-exact mirror of mb_luma_sad_at_mv's
        // prediction at truth_mv (so SAD == 0 at truth_mv).
        let mut src_blk = [0u8; 256];
        let mv_eighth = stored_luma_mv(truth_mv);
        let filters = filter_set_for_version(0).taps();
        let blk_x0 = 16;
        let blk_y0 = 16;
        for sub_r in 0..4 {
            for sub_c in 0..4 {
                let blk_x = blk_x0 + sub_c * 4;
                let blk_y = blk_y0 + sub_r * 4;
                let patch = filter_block_4x4(
                    &reference, width, width, height, blk_x, blk_y, mv_eighth, filters,
                );
                for r in 0..4 {
                    let dst_row = sub_r * 4 + r;
                    let dst_col = sub_c * 4;
                    src_blk[dst_row * 16 + dst_col..dst_row * 16 + dst_col + 4]
                        .copy_from_slice(&patch[r * 4..r * 4 + 4]);
                }
            }
        }
        // The driver starts at the whole-pixel center (0, 0), runs the
        // half-pixel refinement first, then the quarter-pixel refinement.
        // From the whole-pixel center the half-pixel refinement cannot
        // reach the +1/4 px source (the half-pixel grid is a strict
        // subset of the quarter-pixel grid), so the half-pixel result
        // lands at one of {(0, 0), (0, +1/2)}; the quarter-pixel
        // refinement around that result reaches (0, +1/4) via either a
        // (0, +1) step from (0, 0) or a (0, -1) step from (0, +1/2).
        let half = half_pixel_refine_luma(ref_luma, 1, 1, &src_blk, Mv { row: 0, col: 0 });
        let result = quarter_pixel_refine_luma(ref_luma, 1, 1, &src_blk, half.mv);
        assert_eq!(
            result.mv, truth_mv,
            "quarter-pixel +1/4 px horizontal shift expected, got {:?}",
            result.mv
        );
        assert_eq!(result.sad, 0);
    }

    #[test]
    fn quarter_pixel_refine_never_worsens_sad() {
        // The refinement is descent semantics: the returned SAD must be
        // ≤ the SAD at the half-pixel center.
        let width = 48;
        let height = 48;
        let reference = ramp_plane(width, height);
        // Synthesize a source with a slight fractional phase shift built
        // by a manual triangle-tap (a/4 + b/2 + c/4) so the SAD topology
        // has non-trivial quarter-pixel response.
        let mut src_plane = reference.clone();
        for y in 0..height {
            for x in 1..(width - 1) {
                let a = reference[y * width + x - 1] as u32;
                let b = reference[y * width + x] as u32;
                let c = reference[y * width + x + 1] as u32;
                src_plane[y * width + x] = ((a + 2 * b + c) / 4) as u8;
            }
        }
        let src_blk = extract_mb_block(&src_plane, width, 1, 1);
        let ref_luma = packed_luma_ref(&reference, width, height);
        let half_center = Mv {
            row: 0,
            col: HALF_PIXEL_STEP,
        };
        let sad_at_center = mb_luma_sad_at_mv(ref_luma, 1, 1, &src_blk, half_center);
        let result = quarter_pixel_refine_luma(ref_luma, 1, 1, &src_blk, half_center);
        assert!(
            result.sad <= sad_at_center,
            "quarter-pixel refinement must not worsen SAD (center={sad_at_center}, result={})",
            result.sad
        );
        // The chosen MV must stay within ±QUARTER_PIXEL_STEP of the
        // half-pixel center on each component — the refinement's
        // neighbourhood is the 3×3 quarter-pixel grid. (The §17 quarter-
        // pixel grid invariant is upheld by `Mv`'s i16 components by
        // construction, since QUARTER_PIXEL_STEP == 1.)
        let drow = result.mv.row - half_center.row;
        let dcol = result.mv.col - half_center.col;
        assert!(drow.abs() <= QUARTER_PIXEL_STEP);
        assert!(dcol.abs() <= QUARTER_PIXEL_STEP);
    }

    #[test]
    fn quarter_pixel_refine_clamps_at_section_17_1_boundary() {
        // Center at the §17.1 boundary (row=col=+1022 = a half-pixel-grid
        // MV near MV_MAX); quarter-pixel candidates that would step past
        // +1023 get clamped back onto an already-evaluated MV and
        // skipped. The result must stay on the quarter-pixel grid and
        // inside §17.1's range.
        let width = 32;
        let height = 32;
        let reference = vec![100u8; width * height];
        let src_blk = [100u8; 256];
        let result = quarter_pixel_refine_luma(
            packed_luma_ref(&reference, width, height),
            0,
            0,
            &src_blk,
            Mv {
                row: 1022,
                col: 1022,
            },
        );
        assert!((MV_MIN..=MV_MAX).contains(&result.mv.row));
        assert!((MV_MIN..=MV_MAX).contains(&result.mv.col));
        // The §17 quarter-pixel grid invariant is upheld by `Mv`'s
        // i16 components by construction (QUARTER_PIXEL_STEP == 1).
        // Flat ⇒ tie-break keeps the center.
        assert_eq!(
            result.mv,
            Mv {
                row: 1022,
                col: 1022,
            }
        );
        assert_eq!(result.sad, 0);
    }

    #[test]
    fn quarter_pixel_refine_at_whole_pixel_center_equals_half_pixel_refine() {
        // When the half-pixel center is a whole-pixel MV (both fractions
        // zero), the quarter-pixel refinement explores the 8 ±1-§17-unit
        // offsets around it. The result must still be a non-worsening
        // refinement and live on the quarter-pixel grid.
        let width = 48;
        let height = 48;
        let reference = ramp_plane(width, height);
        let src_blk = extract_mb_block(&reference, width, 1, 1);
        let ref_luma = packed_luma_ref(&reference, width, height);
        let whole_center = Mv { row: 0, col: 0 };
        let sad_at_center = mb_luma_sad_at_mv(ref_luma, 1, 1, &src_blk, whole_center);
        let result = quarter_pixel_refine_luma(ref_luma, 1, 1, &src_blk, whole_center);
        assert!(result.sad <= sad_at_center);
        // Identical source/ref at MV (0,0) ⇒ tie-break keeps the center.
        assert_eq!(result.mv, whole_center);
        assert_eq!(result.sad, 0);
    }

    #[test]
    fn search_result_is_copy_eq() {
        // The struct is small enough to be Copy + Eq + Debug — pin the
        // contract so a future field addition has to think about it.
        let a = SearchResult {
            mv: Mv { row: 4, col: -8 },
            sad: 1234,
        };
        let b = a;
        assert_eq!(a, b);
        assert_eq!(format!("{a:?}"), format!("{b:?}"));
    }
}