pixon 0.8.0

SIMD-dispatched color conversion for the FFmpeg AVPixelFormat space: one-call decode of validated source frames to RGB / RGBA / Luma / HSV, fused resampling while converting, colorimetry-honest matrices and chroma siting, no_std-friendly.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//! Fused-downscale coverage for the high-bit **planar** 4:4:0 YUV family —
//! `Yuv440p10` / `Yuv440p12` (LE + BE wire). Low-packed `u16` Y plane +
//! **full-width**, half-height U / V planes (vertical-only subsampling — no
//! horizontal chroma upsampling; the vertical chroma sharing is resolved by
//! the walker, which hands each luma row its vertically-shared full-width
//! `u` / `v`).
//!
//! These route through [`packed_yuv444_triple_resample`] (the same tail the
//! high-bit planar 4:4:4 family uses — 4:4:0 reuses the `yuv444pN_to_rgb_*`
//! kernels at full-width chroma), with **three** independent native-precision
//! binnings:
//! - **u8 colour (rgb / rgba / hsv)** bins a converted source-width u8 RGB
//!   row (`yuv444pN_to_rgb_row_endian`).
//! - **u16 colour (rgb_u16 / rgba_u16)** bins an *independent* converted
//!   source-width native u16 RGB row — never a narrowing of the u8 bin.
//! - **luma** bins the de-interleaved native Y; `luma = binned_Y >>
//!   (BITS - 8)`. (Yuv440p exposes no `luma_u16` output.)
//!
//! Colour oracles drive a direct identity sink at source resolution (the
//! walker upsamples chroma vertically to full-res before the sink converts)
//! and 2x2-block-mean its output; the luma oracle area-bins the native Y
//! plane then narrows. Uniform-gray + saturated-chroma counterexamples pin
//! the independent-u8/u16 and native-Y-luma contracts.

use crate::{
  KernelMatrix, PixelSink,
  frame::*,
  resample::{AreaResampler, ResampleError},
  sinker::{MixedSinker, MixedSinkerError},
};

const SRC: usize = 8;
const CW: usize = SRC;
const CH: usize = SRC / 2;
const OUT: usize = 4;
const M: KernelMatrix = KernelMatrix::Bt601;
const FR: bool = true;

/// Re-encode a host-native u16 slice as host-independent LE-wire byte
/// storage (the `*LeFrame` plane contract), recovered via `from_le`.
fn as_le_u16(host: &[u16]) -> Vec<u16> {
  host
    .iter()
    .map(|v| u16::from_ne_bytes(v.to_le_bytes()))
    .collect()
}

/// Re-encode a host-native u16 slice as host-independent BE-wire byte
/// storage (the `*BeFrame` plane contract), recovered via `from_be`.
fn as_be_u16(host: &[u16]) -> Vec<u16> {
  host
    .iter()
    .map(|v| u16::from_ne_bytes(v.to_be_bytes()))
    .collect()
}

/// Exact 2x2-block area mean (round-half-up) of an `SRC`-grid 3-channel
/// u8 RGB plane.
fn block_mean_2x2_rgb_u8(rgb: &[u8]) -> Vec<u8> {
  let mut out = vec![0u8; OUT * OUT * 3];
  for oy in 0..OUT {
    for ox in 0..OUT {
      for c in 0..3 {
        let mut s = 0u32;
        for dy in 0..2 {
          for dx in 0..2 {
            s += rgb[((oy * 2 + dy) * SRC + ox * 2 + dx) * 3 + c] as u32;
          }
        }
        out[(oy * OUT + ox) * 3 + c] = ((s + 2) / 4) as u8;
      }
    }
  }
  out
}

/// Exact 2x2-block area mean (round-half-up) of an `SRC`-grid u16 plane.
fn block_mean_2x2_u16(plane: &[u16]) -> Vec<u16> {
  let mut out = vec![0u16; OUT * OUT];
  for oy in 0..OUT {
    for ox in 0..OUT {
      let mut s = 0u32;
      for dy in 0..2 {
        for dx in 0..2 {
          s += plane[(oy * 2 + dy) * SRC + ox * 2 + dx] as u32;
        }
      }
      out[oy * OUT + ox] = ((s + 2) / 4) as u16;
    }
  }
  out
}

/// Exact 2x2-block area mean (round-half-up) of an `SRC`-grid 3-channel
/// u16 RGB plane.
fn block_mean_2x2_rgb_u16(rgb: &[u16]) -> Vec<u16> {
  let mut out = vec![0u16; OUT * OUT * 3];
  for oy in 0..OUT {
    for ox in 0..OUT {
      for c in 0..3 {
        let mut s = 0u32;
        for dy in 0..2 {
          for dx in 0..2 {
            s += rgb[((oy * 2 + dy) * SRC + ox * 2 + dx) * 3 + c] as u32;
          }
        }
        out[(oy * OUT + ox) * 3 + c] = ((s + 2) / 4) as u16;
      }
    }
  }
  out
}

macro_rules! yuv440p_high_bit_resample_suite {
  (
    $mod:ident, $frame_le:ident, $frame_be:ident, $marker:ident, $row:ident,
    $walker:ident, $walker_be:ident, $bits:literal,
  ) => {
    mod $mod {
      use super::*;
      use crate::source::{$marker, $row, $walker, $walker_be};

      const MASK: u16 = ((1u32 << $bits) - 1) as u16;
      const MID: u16 = (1u16 << ($bits - 1));

      /// Per-pixel `(Y)` ramp + per-chroma-sample `(U, V)` ramp into an
      /// `SRC`-grid Y plane and full-width / half-height U / V planes
      /// (4:4:0, low-packed native codes so every kernel sees real math).
      fn ramp() -> (Vec<u16>, Vec<u16>, Vec<u16>) {
        let mut y = vec![0u16; SRC * SRC];
        let mut u = vec![0u16; CW * CH];
        let mut v = vec![0u16; CW * CH];
        for i in 0..SRC * SRC {
          y[i] = ((40u32 + i as u32 * 37) & MASK as u32) as u16;
        }
        for i in 0..CW * CH {
          u[i] = ((300u32 + i as u32 * 53) & MASK as u32) as u16;
          v[i] = (MASK as u32).wrapping_sub(i as u32 * 41) as u16 & MASK;
        }
        (y, u, v)
      }

      /// Uniform-gray planes: constant Y, neutral chroma (U = V = mid).
      fn uniform_gray(y: u16) -> (Vec<u16>, Vec<u16>, Vec<u16>) {
        (
          vec![y & MASK; SRC * SRC],
          vec![MID & MASK; CW * CH],
          vec![MID & MASK; CW * CH],
        )
      }

      /// Saturated-chroma planes: constant Y, extreme U/V.
      fn saturated(y: u16) -> (Vec<u16>, Vec<u16>, Vec<u16>) {
        (
          vec![y & MASK; SRC * SRC],
          vec![MASK; CW * CH],
          vec![0u16; CW * CH],
        )
      }

      fn frame<'a>(y: &'a [u16], u: &'a [u16], v: &'a [u16]) -> $frame_le<'a> {
        $frame_le::try_new(
          y, u, v, SRC as u32, SRC as u32, SRC as u32, CW as u32, CW as u32,
        )
        .unwrap()
      }
      fn frame_be<'a>(y: &'a [u16], u: &'a [u16], v: &'a [u16]) -> $frame_be<'a> {
        $frame_be::try_new(
          y, u, v, SRC as u32, SRC as u32, SRC as u32, CW as u32, CW as u32,
        )
        .unwrap()
      }

      fn direct_rgb_u8(y: &[u16], u: &[u16], v: &[u16]) -> Vec<u8> {
        let mut rgb = vec![0u8; SRC * SRC * 3];
        let mut sink = MixedSinker::<$marker>::new(SRC, SRC)
          .with_rgb(&mut rgb)
          .unwrap();
        $walker(&frame(y, u, v), FR, sink.set_kernel_matrix(M)).unwrap();
        rgb
      }
      fn direct_rgb_u16(y: &[u16], u: &[u16], v: &[u16]) -> Vec<u16> {
        let mut rgb = vec![0u16; SRC * SRC * 3];
        let mut sink = MixedSinker::<$marker>::new(SRC, SRC)
          .with_rgb_u16(&mut rgb)
          .unwrap();
        $walker(&frame(y, u, v), FR, sink.set_kernel_matrix(M)).unwrap();
        rgb
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn rgb_u8_matches_area_bin_of_direct() {
        let (y, u, v) = ramp();
        let mut rgb = vec![0u8; OUT * OUT * 3];
        {
          let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
            SRC,
            SRC,
            AreaResampler::to(OUT, OUT),
          )
          .unwrap()
          .with_native(false)
          .with_rgb(&mut rgb)
          .unwrap();
          $walker(&frame(&y, &u, &v), FR, sink.set_kernel_matrix(M)).unwrap();
        }
        assert_eq!(rgb, block_mean_2x2_rgb_u8(&direct_rgb_u8(&y, &u, &v)));
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn rgb_u16_is_exact_native_area_mean() {
        let (y, u, v) = ramp();
        let mut rgb = vec![0u16; OUT * OUT * 3];
        {
          let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
            SRC,
            SRC,
            AreaResampler::to(OUT, OUT),
          )
          .unwrap()
          .with_native(false)
          .with_rgb_u16(&mut rgb)
          .unwrap();
          $walker(&frame(&y, &u, &v), FR, sink.set_kernel_matrix(M)).unwrap();
        }
        assert_eq!(rgb, block_mean_2x2_rgb_u16(&direct_rgb_u16(&y, &u, &v)));
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn u8_color_is_not_a_narrowing_of_u16() {
        // Independence proof: over a varying ramp the u8 and u16 YUV→RGB
        // kernels round and scale differently, so narrowing the binned u16
        // colour to u8 (`>> (BITS - 8)`) diverges from the genuine u8 bin —
        // each binning must match its OWN native-depth oracle.
        let (y, u, v) = ramp();
        let mut rgb_u8 = vec![0u8; OUT * OUT * 3];
        let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
        {
          let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
            SRC,
            SRC,
            AreaResampler::to(OUT, OUT),
          )
          .unwrap()
          .with_native(false)
          .with_rgb(&mut rgb_u8)
          .unwrap()
          .with_rgb_u16(&mut rgb_u16)
          .unwrap();
          $walker(&frame(&y, &u, &v), FR, sink.set_kernel_matrix(M)).unwrap();
        }
        assert_eq!(rgb_u8, block_mean_2x2_rgb_u8(&direct_rgb_u8(&y, &u, &v)));
        assert_eq!(rgb_u16, block_mean_2x2_rgb_u16(&direct_rgb_u16(&y, &u, &v)));
        let narrowed: Vec<u8> = rgb_u16.iter().map(|&c| (c >> ($bits - 8)) as u8).collect();
        assert_ne!(
          rgb_u8, narrowed,
          "u8 colour must be an independent bin, not a narrowed u16 bin"
        );
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn luma_is_native_y_area_mean() {
        let (y, u, v) = ramp();
        // Native Y is range-INDEPENDENT (luma is the Y plane area-mean, no
        // matrix / range applied), so full-range and limited-range must
        // both produce the area-binned Y plane narrowed `>> (BITS - 8)`.
        let y_ref = block_mean_2x2_u16(&y);
        let luma_ref: Vec<u8> = y_ref.iter().map(|&v| (v >> ($bits - 8)) as u8).collect();
        for full_range in [true, false] {
          let mut luma = vec![0u8; OUT * OUT];
          {
            let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
              SRC,
              SRC,
              AreaResampler::to(OUT, OUT),
            )
            .unwrap()
            .with_native(false)
            .with_luma(&mut luma)
            .unwrap();
            $walker(&frame(&y, &u, &v), full_range, sink.set_kernel_matrix(M)).unwrap();
          }
          assert_eq!(
            luma, luma_ref,
            "luma = binned native Y >> (BITS - 8) (full_range={full_range})"
          );
        }
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn luma_from_native_y_under_saturated_chroma() {
        let yc: u16 = (MASK / 4) & MASK;
        let (y, u, v) = saturated(yc);
        let mut luma = vec![0u8; OUT * OUT];
        {
          let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
            SRC,
            SRC,
            AreaResampler::to(OUT, OUT),
          )
          .unwrap()
          .with_native(false)
          .with_luma(&mut luma)
          .unwrap();
          $walker(&frame(&y, &u, &v), FR, sink.set_kernel_matrix(M)).unwrap();
        }
        let expect = (yc >> ($bits - 8)) as u8;
        assert!(
          luma.iter().all(|&l| l == expect),
          "luma must be native Y >> shift ({expect}), not RGB-derived; got {luma:?}"
        );
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn uniform_gray_color_unchanged_counterexample() {
        let (y, u, v) = uniform_gray(MID);
        let direct_u8 = direct_rgb_u8(&y, &u, &v);
        let direct_u16 = direct_rgb_u16(&y, &u, &v);

        let mut rgb = vec![0u8; OUT * OUT * 3];
        let mut rgba = vec![0u8; OUT * OUT * 4];
        let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
        let mut hh = vec![0u8; OUT * OUT];
        let mut ss = vec![0u8; OUT * OUT];
        let mut vv = vec![0u8; OUT * OUT];
        {
          let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
            SRC,
            SRC,
            AreaResampler::to(OUT, OUT),
          )
          .unwrap()
          .with_native(false)
          .with_rgb(&mut rgb)
          .unwrap()
          .with_rgba(&mut rgba)
          .unwrap()
          .with_rgb_u16(&mut rgb_u16)
          .unwrap()
          .with_hsv(&mut hh, &mut ss, &mut vv)
          .unwrap();
          $walker(&frame(&y, &u, &v), FR, sink.set_kernel_matrix(M)).unwrap();
        }
        let g_u8 = &direct_u8[..3];
        for px in rgb.chunks_exact(3) {
          assert_eq!(px, g_u8, "uniform-gray rgb must equal the direct gray");
        }
        for px in rgba.chunks_exact(4) {
          assert_eq!(&px[..3], g_u8, "uniform-gray rgba colour");
          assert_eq!(px[3], 0xFF, "uniform-gray rgba alpha");
        }
        let g_u16 = &direct_u16[..3];
        for px in rgb_u16.chunks_exact(3) {
          assert_eq!(px, g_u16, "uniform-gray rgb_u16 must equal the direct gray");
        }
        assert!(hh.iter().all(|&h| h == 0), "uniform-gray hsv H");
        assert!(ss.iter().all(|&s| s == 0), "uniform-gray hsv S");
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn all_outputs_combo() {
        let (y, u, v) = ramp();
        let rgb_u8_ref = block_mean_2x2_rgb_u8(&direct_rgb_u8(&y, &u, &v));
        let rgb_u16_ref = block_mean_2x2_rgb_u16(&direct_rgb_u16(&y, &u, &v));
        let y_ref = block_mean_2x2_u16(&y);

        let mut rgb = vec![0u8; OUT * OUT * 3];
        let mut rgba = vec![0u8; OUT * OUT * 4];
        let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
        let mut rgba_u16 = vec![0u16; OUT * OUT * 4];
        let mut luma = vec![0u8; OUT * OUT];
        let mut hh = vec![0u8; OUT * OUT];
        let mut ss = vec![0u8; OUT * OUT];
        let mut vv = vec![0u8; OUT * OUT];
        {
          let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
            SRC,
            SRC,
            AreaResampler::to(OUT, OUT),
          )
          .unwrap()
          .with_native(false)
          .with_rgb(&mut rgb)
          .unwrap()
          .with_rgba(&mut rgba)
          .unwrap()
          .with_rgb_u16(&mut rgb_u16)
          .unwrap()
          .with_rgba_u16(&mut rgba_u16)
          .unwrap()
          .with_luma(&mut luma)
          .unwrap()
          .with_hsv(&mut hh, &mut ss, &mut vv)
          .unwrap();
          $walker(&frame(&y, &u, &v), FR, sink.set_kernel_matrix(M)).unwrap();
        }
        assert_eq!(rgb, rgb_u8_ref, "all-outputs rgb");
        for (px, rgb_px) in rgba.chunks_exact(4).zip(rgb_u8_ref.chunks_exact(3)) {
          assert_eq!(&px[..3], rgb_px, "all-outputs rgba colour");
          assert_eq!(px[3], 0xFF, "all-outputs rgba alpha");
        }
        assert_eq!(rgb_u16, rgb_u16_ref, "all-outputs rgb_u16");
        for (px, rgb_px) in rgba_u16.chunks_exact(4).zip(rgb_u16_ref.chunks_exact(3)) {
          assert_eq!(&px[..3], rgb_px, "all-outputs rgba_u16 colour");
          assert_eq!(px[3], MASK, "all-outputs rgba_u16 alpha");
        }
        let luma_ref: Vec<u8> = y_ref.iter().map(|&v| (v >> ($bits - 8)) as u8).collect();
        assert_eq!(luma, luma_ref, "all-outputs luma");
        let mut hh_ref = vec![0u8; OUT * OUT];
        let mut ss_ref = vec![0u8; OUT * OUT];
        let mut vv_ref = vec![0u8; OUT * OUT];
        crate::row::rgb_to_hsv_row(
          &rgb_u8_ref,
          &mut hh_ref,
          &mut ss_ref,
          &mut vv_ref,
          OUT * OUT,
          false,
        );
        assert_eq!(hh, hh_ref, "all-outputs hsv H");
        assert_eq!(ss, ss_ref, "all-outputs hsv S");
        assert_eq!(vv, vv_ref, "all-outputs hsv V");
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn le_be_outputs_identical() {
        let (y, u, v) = ramp();
        let (y_le, u_le, v_le) = (as_le_u16(&y), as_le_u16(&u), as_le_u16(&v));
        let (y_be, u_be, v_be) = (as_be_u16(&y), as_be_u16(&u), as_be_u16(&v));

        let mut le_rgb = vec![0u8; OUT * OUT * 3];
        let mut le_rgb_u16 = vec![0u16; OUT * OUT * 3];
        let mut le_luma = vec![0u8; OUT * OUT];
        {
          let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
            SRC,
            SRC,
            AreaResampler::to(OUT, OUT),
          )
          .unwrap()
          .with_native(false)
          .with_rgb(&mut le_rgb)
          .unwrap()
          .with_rgb_u16(&mut le_rgb_u16)
          .unwrap()
          .with_luma(&mut le_luma)
          .unwrap();
          $walker(&frame(&y_le, &u_le, &v_le), FR, sink.set_kernel_matrix(M)).unwrap();
        }

        let mut be_rgb = vec![0u8; OUT * OUT * 3];
        let mut be_rgb_u16 = vec![0u16; OUT * OUT * 3];
        let mut be_luma = vec![0u8; OUT * OUT];
        {
          let mut sink = MixedSinker::<$marker<true>, AreaResampler>::with_resampler(
            SRC,
            SRC,
            AreaResampler::to(OUT, OUT),
          )
          .unwrap()
          .with_native(false)
          .with_rgb(&mut be_rgb)
          .unwrap()
          .with_rgb_u16(&mut be_rgb_u16)
          .unwrap()
          .with_luma(&mut be_luma)
          .unwrap();
          $walker_be::<_, true>(
            &frame_be(&y_be, &u_be, &v_be),
            FR,
            sink.set_kernel_matrix(M),
          )
          .unwrap();
        }

        assert_eq!(le_rgb, be_rgb, "rgb LE/BE diverge");
        assert_eq!(le_rgb_u16, be_rgb_u16, "rgb_u16 LE/BE diverge");
        assert_eq!(le_luma, be_luma, "luma LE/BE diverge");
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn simd_matches_scalar() {
        let (y, u, v) = ramp();
        let run = |simd: bool| {
          let mut rgb = vec![0u8; OUT * OUT * 3];
          let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
          let mut luma = vec![0u8; OUT * OUT];
          {
            let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
              SRC,
              SRC,
              AreaResampler::to(OUT, OUT),
            )
            .unwrap()
            .with_native(false)
            .with_simd(simd)
            .with_rgb(&mut rgb)
            .unwrap()
            .with_rgb_u16(&mut rgb_u16)
            .unwrap()
            .with_luma(&mut luma)
            .unwrap();
            $walker(&frame(&y, &u, &v), FR, sink.set_kernel_matrix(M)).unwrap();
          }
          (rgb, rgb_u16, luma)
        };
        assert_eq!(run(true), run(false), "SIMD vs scalar diverge");
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn chroma_upsample_matches_direct() {
        // 4:4:0 is vertical-only chroma subsampling: the walker shares each
        // chroma row across the two luma rows above/below it (no horizontal
        // upsampling — chroma is full-width). A per-pixel-varying chroma
        // frame must still match the direct convert-then-bin (which shares
        // the same chroma row vertically at full resolution).
        let (y, u, v) = ramp();
        let mut rgb = vec![0u8; OUT * OUT * 3];
        let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
        {
          let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
            SRC,
            SRC,
            AreaResampler::to(OUT, OUT),
          )
          .unwrap()
          .with_native(false)
          .with_rgb(&mut rgb)
          .unwrap()
          .with_rgb_u16(&mut rgb_u16)
          .unwrap();
          $walker(&frame(&y, &u, &v), FR, sink.set_kernel_matrix(M)).unwrap();
        }
        assert_eq!(
          rgb,
          block_mean_2x2_rgb_u8(&direct_rgb_u8(&y, &u, &v)),
          "u8 chroma-upsampled"
        );
        assert_eq!(
          rgb_u16,
          block_mean_2x2_rgb_u16(&direct_rgb_u16(&y, &u, &v)),
          "u16 chroma-upsampled"
        );
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn identity_plan_matches_new_sink() {
        let (y, u, v) = ramp();
        let mut direct = vec![0u8; SRC * SRC * 3];
        {
          let mut sink = MixedSinker::<$marker>::new(SRC, SRC)
            .with_rgb(&mut direct)
            .unwrap();
          $walker(&frame(&y, &u, &v), FR, sink.set_kernel_matrix(M)).unwrap();
        }
        let mut via_area = vec![0u8; SRC * SRC * 3];
        {
          let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
            SRC,
            SRC,
            AreaResampler::to(SRC, SRC),
          )
          .unwrap()
          .with_rgb(&mut via_area)
          .unwrap();
          $walker(&frame(&y, &u, &v), FR, sink.set_kernel_matrix(M)).unwrap();
        }
        assert_eq!(direct, via_area, "identity plan must match the direct sink");
      }

      #[test]
      fn no_outputs_is_a_no_op() {
        let (y, u, v) = ramp();
        let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
          SRC,
          SRC,
          AreaResampler::to(OUT, OUT),
        )
        .unwrap();
        $walker(&frame(&y, &u, &v), FR, sink.set_kernel_matrix(M)).unwrap();
        assert!(
          !sink.luma_stream_u16_allocated(),
          "no-output sink allocated a luma stream"
        );
        assert!(
          !sink.rgb_stream_allocated(),
          "no-output sink allocated an rgb stream"
        );
        assert!(
          !sink.rgb_stream_u16_allocated(),
          "no-output sink allocated a u16 rgb stream"
        );
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn resets_streams_across_frames() {
        let (y1, u1, v1) = ramp();
        let invert =
          |p: &[u16]| -> Vec<u16> { p.iter().map(|&x| MASK.wrapping_sub(x) & MASK).collect() };
        let (y2, u2, v2) = (invert(&y1), invert(&u1), invert(&v1));
        let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
        let mut luma = vec![0u8; OUT * OUT];
        {
          let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
            SRC,
            SRC,
            AreaResampler::to(OUT, OUT),
          )
          .unwrap()
          .with_native(false)
          .with_rgb_u16(&mut rgb_u16)
          .unwrap()
          .with_luma(&mut luma)
          .unwrap();
          $walker(&frame(&y1, &u1, &v1), FR, sink.set_kernel_matrix(M)).unwrap();
          $walker(&frame(&y2, &u2, &v2), FR, sink.set_kernel_matrix(M)).unwrap();
        }
        assert_eq!(
          rgb_u16,
          block_mean_2x2_rgb_u16(&direct_rgb_u16(&y2, &u2, &v2))
        );
        let y_ref = block_mean_2x2_u16(&y2);
        let luma_ref: Vec<u8> = y_ref.iter().map(|&v| (v >> ($bits - 8)) as u8).collect();
        assert_eq!(luma, luma_ref);
      }

      #[test]
      fn out_of_sequence_first_row_rejected_before_allocation() {
        let (y, u, v) = ramp();
        let mut rgb = vec![0u8; OUT * OUT * 3];
        let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
        let mut luma = vec![0u8; OUT * OUT];
        let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
          SRC,
          SRC,
          AreaResampler::to(OUT, OUT),
        )
        .unwrap()
        .with_native(false)
        .with_rgb(&mut rgb)
        .unwrap()
        .with_rgb_u16(&mut rgb_u16)
        .unwrap()
        .with_luma(&mut luma)
        .unwrap();
        sink.begin_frame(SRC as u32, SRC as u32).unwrap();
        // Row 3's vertically-shared chroma row is `3 / 2 == 1` (full-width).
        let (yr, cr) = (3 * SRC, 1 * CW);
        let err = sink
          .process($row::for_tests(
            &y[yr..yr + SRC],
            &u[cr..cr + CW],
            &v[cr..cr + CW],
            3,
            M,
            FR,
          ))
          .unwrap_err();
        assert!(
          matches!(
            err,
            MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
          ),
          "expected OutOfSequenceRow, got {err:?}"
        );
        assert!(
          !sink.luma_stream_u16_allocated()
            && !sink.rgb_stream_allocated()
            && !sink.rgb_stream_u16_allocated(),
          "stream allocated for a rejected row"
        );
        assert!(
          rgb.iter().all(|&b| b == 0)
            && rgb_u16.iter().all(|&b| b == 0)
            && luma.iter().all(|&b| b == 0),
          "rejected row mutated output"
        );
      }

      #[test]
      #[cfg_attr(
        miri,
        ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
      )]
      fn rejects_mid_frame_output_change() {
        let (y, u, v) = ramp();
        let mut rgb = vec![0u8; OUT * OUT * 3];
        let mut luma = vec![0u8; OUT * OUT];
        let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
          SRC,
          SRC,
          AreaResampler::to(OUT, OUT),
        )
        .unwrap()
        .with_native(false)
        .with_rgb(&mut rgb)
        .unwrap();
        sink.begin_frame(SRC as u32, SRC as u32).unwrap();
        // Rows 0 and 1 both read chroma row 0 (`r / 2`), full-width.
        sink
          .process($row::for_tests(&y[..SRC], &u[..CW], &v[..CW], 0, M, FR))
          .unwrap();
        sink.set_luma(&mut luma).unwrap();
        let err = sink
          .process($row::for_tests(
            &y[SRC..2 * SRC],
            &u[..CW],
            &v[..CW],
            1,
            M,
            FR,
          ))
          .unwrap_err();
        assert!(
          matches!(err, MixedSinkerError::ResampleOutputsChanged(_)),
          "expected ResampleOutputsChanged, got {err:?}"
        );
        assert!(
          luma.iter().all(|&b| b == 0),
          "rejected row mutated the new output"
        );
      }
    }
  };
}

yuv440p_high_bit_resample_suite!(
  yuv440p10,
  Yuv440p10LeFrame,
  Yuv440p10BeFrame,
  Yuv440p10,
  Yuv440p10Row,
  yuv440p10_to,
  yuv440p10_to_endian,
  10,
);
yuv440p_high_bit_resample_suite!(
  yuv440p12,
  Yuv440p12LeFrame,
  Yuv440p12BeFrame,
  Yuv440p12,
  Yuv440p12Row,
  yuv440p12_to,
  yuv440p12_to_endian,
  12,
);