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
//! #263 PR 8 — route the YUVA sinks' `with_hsv()` RGB-free, reusing the
//! existing planar YUV→HSV kernels.
//!
//! YUVA is planar Y/U/V + an alpha plane; HSV is colour-only (alpha is
//! dropped), so a YUVA→HSV is byte-identical to the no-alpha planar
//! YUV→HSV on the same Y/U/V. The YUVA sinks' alpha-drop RGB path already
//! reuses the planar `yuv_*_to_rgb_row` / `yuv*p*_to_rgb_row_endian`
//! dispatchers, so HSV reuses the MATCHING planar HSV kernel — no new
//! kernels. This PR wires that routing in the six sink direct paths
//! (8-bit + high-bit, for 4:2:0 / 4:2:2 / 4:4:4).
//!
//! Two concerns:
//!
//! 1. **Sink parity** — a YUVA sink with ONLY `with_hsv()` must produce
//!    output byte-identical to the SAME YUVA sink with `with_rgb()`
//!    followed by `rgb_to_hsv_row` on that RGB, for both tiers
//!    (`use_simd` on/off), every matrix, range, and width.
//! 2. **Structural** — a YUVA sink with ONLY `with_hsv()` (and
//!    `with_luma()` + `with_hsv()`) must not grow the source-width RGB
//!    scratch.

use super::*;
use crate::row::{rgb_to_hsv_row, yuv_420_to_rgb_row, yuv_444_to_rgb_row};

const MATRICES: [KernelMatrix; 3] = [
  KernelMatrix::Bt601,
  KernelMatrix::Bt709,
  KernelMatrix::Bt2020Ncl,
];

/// A non-trivial, non-gray pseudo-random byte so the HSV hue /
/// saturation branches are all exercised (delta != 0, every `v == r /
/// g / b` arm) rather than the degenerate gray fast-path.
fn pat(i: usize, salt: usize) -> u8 {
  ((i
    .wrapping_mul(37)
    .wrapping_add(salt.wrapping_mul(101))
    .wrapping_add(11))
    & 0xFF) as u8
}

/// HSV reference for one frame: derive it from the YUVA sink's *RGB*
/// output by running `rgb_to_hsv_row` per row at the SAME tier. The
/// HSV-direct sink path must reproduce this bit-for-bit.
fn ref_hsv_from_rgb_frame(
  rgb: &[u8],
  w: usize,
  h: usize,
  use_simd: bool,
) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
  let mut hh = std::vec![0u8; w * h];
  let mut ss = std::vec![0u8; w * h];
  let mut vv = std::vec![0u8; w * h];
  for y in 0..h {
    rgb_to_hsv_row(
      &rgb[y * w * 3..(y + 1) * w * 3],
      &mut hh[y * w..(y + 1) * w],
      &mut ss[y * w..(y + 1) * w],
      &mut vv[y * w..(y + 1) * w],
      w,
      use_simd,
    );
  }
  (hh, ss, vv)
}

macro_rules! assert_hsv_eq {
  ($got:expr, $want:expr, $ctx:expr) => {{
    let (gh, gs, gv) = &$got;
    let (wh, ws, wv) = &$want;
    assert_eq!(gh, wh, "H mismatch ({})", $ctx);
    assert_eq!(gs, ws, "S mismatch ({})", $ctx);
    assert_eq!(gv, wv, "V mismatch ({})", $ctx);
  }};
}

// ---- 8-bit sink parity (HSV-only == with_rgb then rgb_to_hsv) ----------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuva420p_sink_hsv_only_matches_rgb_then_hsv() {
  // 4:2:0 — half-width, half-height chroma; even widths incl. an odd half.
  for &(w, h) in &[(2usize, 2usize), (6, 4), (16, 8), (30, 6)] {
    let yp: Vec<u8> = (0..w * h).map(|i| pat(i, 1)).collect();
    let up: Vec<u8> = (0..(w / 2) * (h / 2)).map(|i| pat(i, 2)).collect();
    let vp: Vec<u8> = (0..(w / 2) * (h / 2)).map(|i| pat(i, 3)).collect();
    let ap: Vec<u8> = (0..w * h).map(|i| pat(i, 4)).collect();
    let src = Yuva420pFrame::try_new(
      &yp,
      &up,
      &vp,
      &ap,
      w as u32,
      h as u32,
      w as u32,
      (w / 2) as u32,
      (w / 2) as u32,
      w as u32,
    )
    .unwrap();
    for &matrix in &MATRICES {
      for &full_range in &[true, false] {
        for &use_simd in &[false, true] {
          // Reference: with_rgb on the YUVA sink, then rgb_to_hsv_row.
          let mut rgb = std::vec![0u8; w * h * 3];
          {
            let mut s = MixedSinker::<Yuva420p>::new(w, h)
              .with_simd(use_simd)
              .with_rgb(&mut rgb)
              .unwrap();
            yuva420p_to(&src, full_range, s.set_kernel_matrix(matrix)).unwrap();
          }
          let want = ref_hsv_from_rgb_frame(&rgb, w, h, use_simd);

          // HSV-direct: with_hsv only on the YUVA sink.
          let mut hh = std::vec![0u8; w * h];
          let mut ss = std::vec![0u8; w * h];
          let mut vv = std::vec![0u8; w * h];
          {
            let mut s = MixedSinker::<Yuva420p>::new(w, h)
              .with_simd(use_simd)
              .with_hsv(&mut hh, &mut ss, &mut vv)
              .unwrap();
            yuva420p_to(&src, full_range, s.set_kernel_matrix(matrix)).unwrap();
          }
          assert_hsv_eq!(
            (hh, ss, vv),
            want,
            std::format!("yuva420p w={w} h={h} {matrix:?} full={full_range} simd={use_simd}")
          );
        }
      }
    }
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuva422p_sink_hsv_only_matches_rgb_then_hsv() {
  // 4:2:2 — half-width chroma, full height.
  for &(w, h) in &[(2usize, 3usize), (6, 4), (16, 8), (30, 5)] {
    let yp: Vec<u8> = (0..w * h).map(|i| pat(i, 1)).collect();
    let up: Vec<u8> = (0..(w / 2) * h).map(|i| pat(i, 2)).collect();
    let vp: Vec<u8> = (0..(w / 2) * h).map(|i| pat(i, 3)).collect();
    let ap: Vec<u8> = (0..w * h).map(|i| pat(i, 4)).collect();
    let src = Yuva422pFrame::try_new(
      &yp,
      &up,
      &vp,
      &ap,
      w as u32,
      h as u32,
      w as u32,
      (w / 2) as u32,
      (w / 2) as u32,
      w as u32,
    )
    .unwrap();
    for &matrix in &MATRICES {
      for &full_range in &[true, false] {
        for &use_simd in &[false, true] {
          let mut rgb = std::vec![0u8; w * h * 3];
          {
            let mut s = MixedSinker::<Yuva422p>::new(w, h)
              .with_simd(use_simd)
              .with_rgb(&mut rgb)
              .unwrap();
            yuva422p_to(&src, full_range, s.set_kernel_matrix(matrix)).unwrap();
          }
          let want = ref_hsv_from_rgb_frame(&rgb, w, h, use_simd);

          let mut hh = std::vec![0u8; w * h];
          let mut ss = std::vec![0u8; w * h];
          let mut vv = std::vec![0u8; w * h];
          {
            let mut s = MixedSinker::<Yuva422p>::new(w, h)
              .with_simd(use_simd)
              .with_hsv(&mut hh, &mut ss, &mut vv)
              .unwrap();
            yuva422p_to(&src, full_range, s.set_kernel_matrix(matrix)).unwrap();
          }
          assert_hsv_eq!(
            (hh, ss, vv),
            want,
            std::format!("yuva422p w={w} h={h} {matrix:?} full={full_range} simd={use_simd}")
          );
        }
      }
    }
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuva444p_sink_hsv_only_matches_rgb_then_hsv() {
  // 4:4:4 — full-width chroma; arbitrary widths incl. odd.
  for &(w, h) in &[(1usize, 1usize), (5, 4), (16, 8), (31, 5)] {
    let yp: Vec<u8> = (0..w * h).map(|i| pat(i, 1)).collect();
    let up: Vec<u8> = (0..w * h).map(|i| pat(i, 2)).collect();
    let vp: Vec<u8> = (0..w * h).map(|i| pat(i, 3)).collect();
    let ap: Vec<u8> = (0..w * h).map(|i| pat(i, 4)).collect();
    let src = Yuva444pFrame::try_new(
      &yp, &up, &vp, &ap, w as u32, h as u32, w as u32, w as u32, w as u32, w as u32,
    )
    .unwrap();
    for &matrix in &MATRICES {
      for &full_range in &[true, false] {
        for &use_simd in &[false, true] {
          let mut rgb = std::vec![0u8; w * h * 3];
          {
            let mut s = MixedSinker::<Yuva444p>::new(w, h)
              .with_simd(use_simd)
              .with_rgb(&mut rgb)
              .unwrap();
            yuva444p_to(&src, full_range, s.set_kernel_matrix(matrix)).unwrap();
          }
          let want = ref_hsv_from_rgb_frame(&rgb, w, h, use_simd);

          let mut hh = std::vec![0u8; w * h];
          let mut ss = std::vec![0u8; w * h];
          let mut vv = std::vec![0u8; w * h];
          {
            let mut s = MixedSinker::<Yuva444p>::new(w, h)
              .with_simd(use_simd)
              .with_hsv(&mut hh, &mut ss, &mut vv)
              .unwrap();
            yuva444p_to(&src, full_range, s.set_kernel_matrix(matrix)).unwrap();
          }
          assert_hsv_eq!(
            (hh, ss, vv),
            want,
            std::format!("yuva444p w={w} h={h} {matrix:?} full={full_range} simd={use_simd}")
          );
        }
      }
    }
  }
}

// ---- High-bit sink parity (one depth per family) -----------------------
//
// High-bit YUVA stores LOGICAL u16 values directly (the default LE
// `BE = false`). Each family routes the HSV-direct path through the
// matching `yuv*p*_to_hsv_row_endian` kernel; the reference is the same
// sink's `with_rgb` output run through `rgb_to_hsv_row`.

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuva420p10_sink_hsv_only_matches_rgb_then_hsv() {
  // 4:2:0 high-bit (10-bit, max logical 1023).
  for &(w, h) in &[(2usize, 2usize), (16, 8), (30, 6)] {
    let yp: Vec<u16> = (0..w * h).map(|i| (pat(i, 1) as u16) << 2).collect();
    let up: Vec<u16> = (0..(w / 2) * (h / 2))
      .map(|i| (pat(i, 2) as u16) << 2)
      .collect();
    let vp: Vec<u16> = (0..(w / 2) * (h / 2))
      .map(|i| (pat(i, 3) as u16) << 2)
      .collect();
    let ap: Vec<u16> = (0..w * h).map(|i| (pat(i, 4) as u16) << 2).collect();
    let src = Yuva420p10Frame::try_new(
      &yp,
      &up,
      &vp,
      &ap,
      w as u32,
      h as u32,
      w as u32,
      (w / 2) as u32,
      (w / 2) as u32,
      w as u32,
    )
    .unwrap();
    for &matrix in &MATRICES {
      for &full_range in &[true, false] {
        for &use_simd in &[false, true] {
          let mut rgb = std::vec![0u8; w * h * 3];
          {
            let mut s = MixedSinker::<Yuva420p10>::new(w, h)
              .with_simd(use_simd)
              .with_rgb(&mut rgb)
              .unwrap();
            yuva420p10_to(&src, full_range, s.set_kernel_matrix(matrix)).unwrap();
          }
          let want = ref_hsv_from_rgb_frame(&rgb, w, h, use_simd);

          let mut hh = std::vec![0u8; w * h];
          let mut ss = std::vec![0u8; w * h];
          let mut vv = std::vec![0u8; w * h];
          {
            let mut s = MixedSinker::<Yuva420p10>::new(w, h)
              .with_simd(use_simd)
              .with_hsv(&mut hh, &mut ss, &mut vv)
              .unwrap();
            yuva420p10_to(&src, full_range, s.set_kernel_matrix(matrix)).unwrap();
          }
          assert_hsv_eq!(
            (hh, ss, vv),
            want,
            std::format!("yuva420p10 w={w} h={h} {matrix:?} full={full_range} simd={use_simd}")
          );
        }
      }
    }
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuva422p12_sink_hsv_only_matches_rgb_then_hsv() {
  // 4:2:2 high-bit (12-bit, max logical 4095).
  for &(w, h) in &[(2usize, 3usize), (16, 8), (30, 5)] {
    let yp: Vec<u16> = (0..w * h).map(|i| (pat(i, 1) as u16) << 4).collect();
    let up: Vec<u16> = (0..(w / 2) * h).map(|i| (pat(i, 2) as u16) << 4).collect();
    let vp: Vec<u16> = (0..(w / 2) * h).map(|i| (pat(i, 3) as u16) << 4).collect();
    let ap: Vec<u16> = (0..w * h).map(|i| (pat(i, 4) as u16) << 4).collect();
    let src = Yuva422p12Frame::try_new(
      &yp,
      &up,
      &vp,
      &ap,
      w as u32,
      h as u32,
      w as u32,
      (w / 2) as u32,
      (w / 2) as u32,
      w as u32,
    )
    .unwrap();
    for &matrix in &MATRICES {
      for &full_range in &[true, false] {
        for &use_simd in &[false, true] {
          let mut rgb = std::vec![0u8; w * h * 3];
          {
            let mut s = MixedSinker::<Yuva422p12>::new(w, h)
              .with_simd(use_simd)
              .with_rgb(&mut rgb)
              .unwrap();
            yuva422p12_to(&src, full_range, s.set_kernel_matrix(matrix)).unwrap();
          }
          let want = ref_hsv_from_rgb_frame(&rgb, w, h, use_simd);

          let mut hh = std::vec![0u8; w * h];
          let mut ss = std::vec![0u8; w * h];
          let mut vv = std::vec![0u8; w * h];
          {
            let mut s = MixedSinker::<Yuva422p12>::new(w, h)
              .with_simd(use_simd)
              .with_hsv(&mut hh, &mut ss, &mut vv)
              .unwrap();
            yuva422p12_to(&src, full_range, s.set_kernel_matrix(matrix)).unwrap();
          }
          assert_hsv_eq!(
            (hh, ss, vv),
            want,
            std::format!("yuva422p12 w={w} h={h} {matrix:?} full={full_range} simd={use_simd}")
          );
        }
      }
    }
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuva444p14_sink_hsv_only_matches_rgb_then_hsv() {
  // 4:4:4 high-bit (14-bit, max logical 16383).
  for &(w, h) in &[(1usize, 1usize), (16, 8), (31, 5)] {
    let yp: Vec<u16> = (0..w * h).map(|i| (pat(i, 1) as u16) << 6).collect();
    let up: Vec<u16> = (0..w * h).map(|i| (pat(i, 2) as u16) << 6).collect();
    let vp: Vec<u16> = (0..w * h).map(|i| (pat(i, 3) as u16) << 6).collect();
    let ap: Vec<u16> = (0..w * h).map(|i| (pat(i, 4) as u16) << 6).collect();
    let src = Yuva444p14Frame::try_new(
      &yp, &up, &vp, &ap, w as u32, h as u32, w as u32, w as u32, w as u32, w as u32,
    )
    .unwrap();
    for &matrix in &MATRICES {
      for &full_range in &[true, false] {
        for &use_simd in &[false, true] {
          let mut rgb = std::vec![0u8; w * h * 3];
          {
            let mut s = MixedSinker::<Yuva444p14>::new(w, h)
              .with_simd(use_simd)
              .with_rgb(&mut rgb)
              .unwrap();
            yuva444p14_to(&src, full_range, s.set_kernel_matrix(matrix)).unwrap();
          }
          let want = ref_hsv_from_rgb_frame(&rgb, w, h, use_simd);

          let mut hh = std::vec![0u8; w * h];
          let mut ss = std::vec![0u8; w * h];
          let mut vv = std::vec![0u8; w * h];
          {
            let mut s = MixedSinker::<Yuva444p14>::new(w, h)
              .with_simd(use_simd)
              .with_hsv(&mut hh, &mut ss, &mut vv)
              .unwrap();
            yuva444p14_to(&src, full_range, s.set_kernel_matrix(matrix)).unwrap();
          }
          assert_hsv_eq!(
            (hh, ss, vv),
            want,
            std::format!("yuva444p14 w={w} h={h} {matrix:?} full={full_range} simd={use_simd}")
          );
        }
      }
    }
  }
}

// ---- HSV is colour-only: drops alpha (independent of the A plane) ------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuva420p_hsv_only_is_independent_of_alpha() {
  // Two frames with identical Y/U/V but different alpha planes must yield
  // byte-identical HSV — confirming the HSV-direct path drops alpha.
  let (w, h) = (16usize, 8usize);
  let yp: Vec<u8> = (0..w * h).map(|i| pat(i, 1)).collect();
  let up: Vec<u8> = (0..(w / 2) * (h / 2)).map(|i| pat(i, 2)).collect();
  let vp: Vec<u8> = (0..(w / 2) * (h / 2)).map(|i| pat(i, 3)).collect();
  let a_zero = std::vec![0u8; w * h];
  let a_full = std::vec![0xFFu8; w * h];

  let mut got = [(); 2].map(|_| {
    (
      std::vec![0u8; w * h],
      std::vec![0u8; w * h],
      std::vec![0u8; w * h],
    )
  });
  for (slot, ap) in [&a_zero, &a_full].into_iter().enumerate() {
    let src = Yuva420pFrame::try_new(
      &yp,
      &up,
      &vp,
      ap,
      w as u32,
      h as u32,
      w as u32,
      (w / 2) as u32,
      (w / 2) as u32,
      w as u32,
    )
    .unwrap();
    let (hh, ss, vv) = &mut got[slot];
    let mut s = MixedSinker::<Yuva420p>::new(w, h)
      .with_hsv(hh, ss, vv)
      .unwrap();
    yuva420p_to(&src, true, s.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  }
  assert_eq!(got[0].0, got[1].0, "H must ignore alpha");
  assert_eq!(got[0].1, got[1].1, "S must ignore alpha");
  assert_eq!(got[0].2, got[1].2, "V must ignore alpha");
}

// ---- Structural: HSV-only attaches no source-width RGB scratch ---------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuva_sink_hsv_only_grows_no_rgb_scratch_all_families() {
  let (w, h) = (16usize, 8usize);
  let yp: Vec<u8> = (0..w * h).map(|i| pat(i, 1)).collect();
  let ap: Vec<u8> = (0..w * h).map(|i| pat(i, 4)).collect();

  // 4:2:0 — half-width, half-height chroma.
  {
    let up: Vec<u8> = (0..(w / 2) * (h / 2)).map(|i| pat(i, 2)).collect();
    let vp: Vec<u8> = (0..(w / 2) * (h / 2)).map(|i| pat(i, 3)).collect();
    let src = Yuva420pFrame::try_new(
      &yp,
      &up,
      &vp,
      &ap,
      w as u32,
      h as u32,
      w as u32,
      (w / 2) as u32,
      (w / 2) as u32,
      w as u32,
    )
    .unwrap();
    let mut hh = std::vec![0u8; w * h];
    let mut ss = std::vec![0u8; w * h];
    let mut vv = std::vec![0u8; w * h];
    let scratch_len = {
      let mut s = MixedSinker::<Yuva420p>::new(w, h)
        .with_hsv(&mut hh, &mut ss, &mut vv)
        .unwrap();
      yuva420p_to(&src, true, s.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
      s.rgb_scratch.len()
    };
    assert_eq!(scratch_len, 0, "Yuva420p HSV-only RGB-free");
  }

  // 4:2:2 — half-width chroma, full height.
  {
    let up: Vec<u8> = (0..(w / 2) * h).map(|i| pat(i, 2)).collect();
    let vp: Vec<u8> = (0..(w / 2) * h).map(|i| pat(i, 3)).collect();
    let src = Yuva422pFrame::try_new(
      &yp,
      &up,
      &vp,
      &ap,
      w as u32,
      h as u32,
      w as u32,
      (w / 2) as u32,
      (w / 2) as u32,
      w as u32,
    )
    .unwrap();
    let mut hh = std::vec![0u8; w * h];
    let mut ss = std::vec![0u8; w * h];
    let mut vv = std::vec![0u8; w * h];
    let scratch_len = {
      let mut s = MixedSinker::<Yuva422p>::new(w, h)
        .with_hsv(&mut hh, &mut ss, &mut vv)
        .unwrap();
      yuva422p_to(&src, true, s.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
      s.rgb_scratch.len()
    };
    assert_eq!(scratch_len, 0, "Yuva422p HSV-only RGB-free");
  }

  // 4:4:4 — full-width chroma.
  {
    let up: Vec<u8> = (0..w * h).map(|i| pat(i, 2)).collect();
    let vp: Vec<u8> = (0..w * h).map(|i| pat(i, 3)).collect();
    let src = Yuva444pFrame::try_new(
      &yp, &up, &vp, &ap, w as u32, h as u32, w as u32, w as u32, w as u32, w as u32,
    )
    .unwrap();
    let mut hh = std::vec![0u8; w * h];
    let mut ss = std::vec![0u8; w * h];
    let mut vv = std::vec![0u8; w * h];
    let scratch_len = {
      let mut s = MixedSinker::<Yuva444p>::new(w, h)
        .with_hsv(&mut hh, &mut ss, &mut vv)
        .unwrap();
      yuva444p_to(&src, true, s.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
      s.rgb_scratch.len()
    };
    assert_eq!(scratch_len, 0, "Yuva444p HSV-only RGB-free");
  }

  // High-bit representative (4:4:4 14-bit).
  {
    let yp16: Vec<u16> = (0..w * h).map(|i| (pat(i, 1) as u16) << 6).collect();
    let up16: Vec<u16> = (0..w * h).map(|i| (pat(i, 2) as u16) << 6).collect();
    let vp16: Vec<u16> = (0..w * h).map(|i| (pat(i, 3) as u16) << 6).collect();
    let ap16: Vec<u16> = (0..w * h).map(|i| (pat(i, 4) as u16) << 6).collect();
    let src = Yuva444p14Frame::try_new(
      &yp16, &up16, &vp16, &ap16, w as u32, h as u32, w as u32, w as u32, w as u32, w as u32,
    )
    .unwrap();
    let mut hh = std::vec![0u8; w * h];
    let mut ss = std::vec![0u8; w * h];
    let mut vv = std::vec![0u8; w * h];
    let scratch_len = {
      let mut s = MixedSinker::<Yuva444p14>::new(w, h)
        .with_hsv(&mut hh, &mut ss, &mut vv)
        .unwrap();
      yuva444p14_to(&src, true, s.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
      s.rgb_scratch.len()
    };
    assert_eq!(scratch_len, 0, "Yuva444p14 HSV-only RGB-free");
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuva420p_luma_plus_hsv_only_is_correct_and_rgb_free() {
  // with_luma() + with_hsv(), no RGB: native Y luma AND direct HSV, with
  // no source-width RGB scratch.
  let (w, h) = (16usize, 8usize);
  let yp: Vec<u8> = (0..w * h).map(|i| pat(i, 7)).collect();
  let up: Vec<u8> = (0..(w / 2) * (h / 2)).map(|i| pat(i, 9)).collect();
  let vp: Vec<u8> = (0..(w / 2) * (h / 2)).map(|i| pat(i, 11)).collect();
  let ap: Vec<u8> = (0..w * h).map(|i| pat(i, 13)).collect();
  let src = Yuva420pFrame::try_new(
    &yp,
    &up,
    &vp,
    &ap,
    w as u32,
    h as u32,
    w as u32,
    (w / 2) as u32,
    (w / 2) as u32,
    w as u32,
  )
  .unwrap();

  let mut luma = std::vec![0u8; w * h];
  let mut hh = std::vec![0u8; w * h];
  let mut ss = std::vec![0u8; w * h];
  let mut vv = std::vec![0u8; w * h];
  let scratch_len = {
    let mut s = MixedSinker::<Yuva420p>::new(w, h)
      .with_luma(&mut luma)
      .unwrap()
      .with_hsv(&mut hh, &mut ss, &mut vv)
      .unwrap();
    yuva420p_to(&src, true, s.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
    s.rgb_scratch.len()
  };

  assert_eq!(
    scratch_len, 0,
    "luma + HSV (no RGB) must not grow the RGB scratch"
  );
  // Luma is the Y plane verbatim (8-bit YUVA Y).
  assert_eq!(luma, yp, "luma must equal the native Y plane");
  // HSV row 0 matches the two-step reference (alpha dropped).
  let mut rgb0 = std::vec![0u8; w * 3];
  yuv_420_to_rgb_row(
    &yp[..w],
    &up[..w / 2],
    &vp[..w / 2],
    &mut rgb0,
    w,
    KernelMatrix::Bt709,
    true,
    true,
  );
  let (rh, rs, rv) = {
    let mut h0 = std::vec![0u8; w];
    let mut s0 = std::vec![0u8; w];
    let mut v0 = std::vec![0u8; w];
    rgb_to_hsv_row(&rgb0, &mut h0, &mut s0, &mut v0, w, true);
    (h0, s0, v0)
  };
  assert_eq!(&hh[..w], &rh[..], "row 0 H");
  assert_eq!(&ss[..w], &rs[..], "row 0 S");
  assert_eq!(&vv[..w], &rv[..], "row 0 V");

  // Cross-check 4:4:4 row 0 too (full-width chroma kernel).
  let up4: Vec<u8> = (0..w * h).map(|i| pat(i, 9)).collect();
  let vp4: Vec<u8> = (0..w * h).map(|i| pat(i, 11)).collect();
  let src4 = Yuva444pFrame::try_new(
    &yp, &up4, &vp4, &ap, w as u32, h as u32, w as u32, w as u32, w as u32, w as u32,
  )
  .unwrap();
  let mut hh4 = std::vec![0u8; w * h];
  let mut ss4 = std::vec![0u8; w * h];
  let mut vv4 = std::vec![0u8; w * h];
  {
    let mut s = MixedSinker::<Yuva444p>::new(w, h)
      .with_hsv(&mut hh4, &mut ss4, &mut vv4)
      .unwrap();
    yuva444p_to(&src4, true, s.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  }
  let mut rgb04 = std::vec![0u8; w * 3];
  yuv_444_to_rgb_row(
    &yp[..w],
    &up4[..w],
    &vp4[..w],
    &mut rgb04,
    w,
    KernelMatrix::Bt709,
    true,
    true,
  );
  let mut h04 = std::vec![0u8; w];
  let mut s04 = std::vec![0u8; w];
  let mut v04 = std::vec![0u8; w];
  rgb_to_hsv_row(&rgb04, &mut h04, &mut s04, &mut v04, w, true);
  assert_eq!(&hh4[..w], &h04[..], "444 row 0 H");
  assert_eq!(&ss4[..w], &s04[..], "444 row 0 S");
  assert_eq!(&vv4[..w], &v04[..], "444 row 0 V");
}