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
//! #263 PR 2 — direct semi-planar NV YUV→HSV kernels + native-Y luma
//! kernel + RGB-free routing.
//!
//! Three concerns:
//!
//! 1. **Row-kernel parity** — each `nv*_to_hsv_row` dispatcher must be
//!    byte-identical to `rgb_to_hsv_row(nv*_to_rgb_row(...))` within a
//!    tier (they share the RGB intermediate and the same per-tier HSV),
//!    for both the scalar path (`use_simd = false`) and the host SIMD
//!    path (`use_simd = true`).
//! 2. **Native luma** — `nv_to_luma_row` reproduces the Y plane verbatim
//!    (the same bytes the sink's former inline `copy_from_slice`
//!    produced).
//! 3. **Structural** — an NV sink with ONLY `with_hsv()` (no RGB / RGBA)
//!    must not grow the source-width RGB scratch, `with_luma()` +
//!    `with_hsv()` stays RGB-free, and the luma output equals the native
//!    Y plane.

use super::*;
use crate::row::{
  nv_to_luma_row, nv12_to_hsv_row, nv12_to_rgb_row, nv21_to_hsv_row, nv21_to_rgb_row,
  nv24_to_hsv_row, nv24_to_rgb_row, nv42_to_hsv_row, nv42_to_rgb_row, rgb_to_hsv_row,
};

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

/// A non-trivial, non-gray pseudo-random byte at a given position 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
}

/// Reference HSV: the explicit `NV → RGB → HSV` via the same dispatcher
/// tier, into a freshly-staged RGB row. The direct kernel must reproduce
/// this bit-for-bit.
fn ref_hsv_from_rgb(rgb: &[u8], w: usize, use_simd: bool) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
  let mut h = std::vec![0u8; w];
  let mut s = std::vec![0u8; w];
  let mut v = std::vec![0u8; w];
  rgb_to_hsv_row(rgb, &mut h, &mut s, &mut v, w, use_simd);
  (h, s, v)
}

macro_rules! assert_planes_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);
  }};
}

/// Interleaves half-width `u` / `v` chroma planes into a single NV-style
/// row. `swap = false` writes `U0 V0 U1 V1 …` (NV12 / NV16 / NV24);
/// `swap = true` writes `V0 U0 …` (NV21 / NV42).
fn interleave(u: &[u8], v: &[u8], swap: bool) -> Vec<u8> {
  let mut out = std::vec![0u8; u.len() * 2];
  for i in 0..u.len() {
    if swap {
      out[i * 2] = v[i];
      out[i * 2 + 1] = u[i];
    } else {
      out[i * 2] = u[i];
      out[i * 2 + 1] = v[i];
    }
  }
  out
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn nv12_hsv_row_matches_rgb_then_hsv() {
  // NV12 (4:2:0, UV-ordered) — even widths incl. an odd-half tail. The
  // chroma row is `width` interleaved bytes (one UV pair per two pixels).
  for &w in &[2usize, 4, 6, 14, 16, 30, 64, 66] {
    let y: Vec<u8> = (0..w).map(|i| pat(i, 1)).collect();
    let u: Vec<u8> = (0..w / 2).map(|i| pat(i, 2)).collect();
    let v_in: Vec<u8> = (0..w / 2).map(|i| pat(i, 3)).collect();
    let uv = interleave(&u, &v_in, false);
    for &matrix in &MATRICES {
      for &full_range in &[true, false] {
        for &use_simd in &[false, true] {
          let mut rgb = std::vec![0u8; w * 3];
          nv12_to_rgb_row(&y, &uv, &mut rgb, w, matrix, full_range, use_simd);
          let want = ref_hsv_from_rgb(&rgb, w, use_simd);

          let mut h = std::vec![0u8; w];
          let mut s = std::vec![0u8; w];
          let mut v = std::vec![0u8; w];
          nv12_to_hsv_row(
            &y, &uv, &mut h, &mut s, &mut v, w, matrix, full_range, use_simd,
          );
          assert_planes_eq!(
            (h, s, v),
            want,
            std::format!("nv12 w={w} {matrix:?} full={full_range} simd={use_simd}")
          );
        }
      }
    }
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn nv21_hsv_row_matches_rgb_then_hsv() {
  // NV21 (4:2:0, VU-ordered) — the swapped-chroma twin of NV12.
  for &w in &[2usize, 4, 6, 14, 16, 30, 64, 66] {
    let y: Vec<u8> = (0..w).map(|i| pat(i, 1)).collect();
    let u: Vec<u8> = (0..w / 2).map(|i| pat(i, 2)).collect();
    let v_in: Vec<u8> = (0..w / 2).map(|i| pat(i, 3)).collect();
    let vu = interleave(&u, &v_in, true);
    for &matrix in &MATRICES {
      for &full_range in &[true, false] {
        for &use_simd in &[false, true] {
          let mut rgb = std::vec![0u8; w * 3];
          nv21_to_rgb_row(&y, &vu, &mut rgb, w, matrix, full_range, use_simd);
          let want = ref_hsv_from_rgb(&rgb, w, use_simd);

          let mut h = std::vec![0u8; w];
          let mut s = std::vec![0u8; w];
          let mut v = std::vec![0u8; w];
          nv21_to_hsv_row(
            &y, &vu, &mut h, &mut s, &mut v, w, matrix, full_range, use_simd,
          );
          assert_planes_eq!(
            (h, s, v),
            want,
            std::format!("nv21 w={w} {matrix:?} full={full_range} simd={use_simd}")
          );
        }
      }
    }
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn nv24_hsv_row_matches_rgb_then_hsv() {
  // NV24 (4:4:4, UV-ordered) — full-width chroma (`2 * width` interleaved
  // bytes); arbitrary widths incl. odd.
  for &w in &[1usize, 3, 5, 15, 16, 31, 64, 65] {
    let y: Vec<u8> = (0..w).map(|i| pat(i, 1)).collect();
    let u: Vec<u8> = (0..w).map(|i| pat(i, 2)).collect();
    let v_in: Vec<u8> = (0..w).map(|i| pat(i, 3)).collect();
    let uv = interleave(&u, &v_in, false);
    for &matrix in &MATRICES {
      for &full_range in &[true, false] {
        for &use_simd in &[false, true] {
          let mut rgb = std::vec![0u8; w * 3];
          nv24_to_rgb_row(&y, &uv, &mut rgb, w, matrix, full_range, use_simd);
          let want = ref_hsv_from_rgb(&rgb, w, use_simd);

          let mut h = std::vec![0u8; w];
          let mut s = std::vec![0u8; w];
          let mut v = std::vec![0u8; w];
          nv24_to_hsv_row(
            &y, &uv, &mut h, &mut s, &mut v, w, matrix, full_range, use_simd,
          );
          assert_planes_eq!(
            (h, s, v),
            want,
            std::format!("nv24 w={w} {matrix:?} full={full_range} simd={use_simd}")
          );
        }
      }
    }
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn nv42_hsv_row_matches_rgb_then_hsv() {
  // NV42 (4:4:4, VU-ordered) — the swapped-chroma twin of NV24.
  for &w in &[1usize, 3, 5, 15, 16, 31, 64, 65] {
    let y: Vec<u8> = (0..w).map(|i| pat(i, 1)).collect();
    let u: Vec<u8> = (0..w).map(|i| pat(i, 2)).collect();
    let v_in: Vec<u8> = (0..w).map(|i| pat(i, 3)).collect();
    let vu = interleave(&u, &v_in, true);
    for &matrix in &MATRICES {
      for &full_range in &[true, false] {
        for &use_simd in &[false, true] {
          let mut rgb = std::vec![0u8; w * 3];
          nv42_to_rgb_row(&y, &vu, &mut rgb, w, matrix, full_range, use_simd);
          let want = ref_hsv_from_rgb(&rgb, w, use_simd);

          let mut h = std::vec![0u8; w];
          let mut s = std::vec![0u8; w];
          let mut v = std::vec![0u8; w];
          nv42_to_hsv_row(
            &y, &vu, &mut h, &mut s, &mut v, w, matrix, full_range, use_simd,
          );
          assert_planes_eq!(
            (h, s, v),
            want,
            std::format!("nv42 w={w} {matrix:?} full={full_range} simd={use_simd}")
          );
        }
      }
    }
  }
}

// ---- Native luma: the kernel equals the verbatim Y plane copy ----------

#[test]
fn nv_luma_row_equals_verbatim_y_copy() {
  // The native-Y contract: `nv_to_luma_row` is a straight copy of the
  // first `width` Y samples — bit-identical to the sink's former inline
  // `luma.copy_from_slice(&row.y()[..w])`.
  for &w in &[1usize, 2, 7, 16, 31, 64] {
    let y: Vec<u8> = (0..w + 5).map(|i| pat(i, 4)).collect();
    let mut got = std::vec![0u8; w];
    nv_to_luma_row(&y, &mut got, w);
    let want = &y[..w];
    assert_eq!(&got[..], want, "nv_to_luma_row w={w}");
  }
}

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

/// Plane bytes (a varied, non-gray pattern).
fn plane(width: usize, height: usize, salt: usize) -> Vec<u8> {
  (0..width * height).map(|i| pat(i, salt)).collect()
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn nv12_hsv_only_grows_no_rgb_scratch() {
  let (w, h) = (16usize, 8usize);
  let yp = plane(w, h, 1);
  let up = plane(w / 2, h / 2, 2);
  let vp = plane(w / 2, h / 2, 3);
  // NV12 chroma is `width`-byte interleaved UV per row, half height.
  let mut uv = std::vec![0u8; w * (h / 2)];
  for row in 0..h / 2 {
    let u_row = &up[row * (w / 2)..(row + 1) * (w / 2)];
    let v_row = &vp[row * (w / 2)..(row + 1) * (w / 2)];
    uv[row * w..(row + 1) * w].copy_from_slice(&interleave(u_row, v_row, false));
  }
  let src = Nv12Frame::new(&yp, &uv, w as u32, h as u32, w as u32, w as u32);

  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 sink = MixedSinker::<Nv12>::new(w, h)
      .with_hsv(&mut hh, &mut ss, &mut vv)
      .unwrap();
    nv12_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
    sink.rgb_scratch.len()
  };
  assert_eq!(
    scratch_len, 0,
    "Nv12 HSV-only must not grow the source-width RGB scratch"
  );

  // Cross-check row 0 against the explicit NV→RGB→HSV reference.
  let mut rgb0 = std::vec![0u8; w * 3];
  nv12_to_rgb_row(
    &yp[..w],
    &uv[..w],
    &mut rgb0,
    w,
    KernelMatrix::Bt601,
    true,
    true,
  );
  let (rh, rs, rv) = ref_hsv_from_rgb(&rgb0, w, true);
  assert_eq!(&hh[..w], &rh[..], "row 0 H");
  assert_eq!(&ss[..w], &rs[..], "row 0 S");
  assert_eq!(&vv[..w], &rv[..], "row 0 V");
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn nv12_luma_plus_hsv_only_is_correct_and_rgb_free() {
  // with_luma() + with_hsv(), no RGB: native-Y luma (via `nv_to_luma_row`)
  // AND direct HSV, with no source-width RGB scratch.
  let (w, h) = (16usize, 8usize);
  let yp = plane(w, h, 7);
  let up = plane(w / 2, h / 2, 9);
  let vp = plane(w / 2, h / 2, 11);
  let mut uv = std::vec![0u8; w * (h / 2)];
  for row in 0..h / 2 {
    let u_row = &up[row * (w / 2)..(row + 1) * (w / 2)];
    let v_row = &vp[row * (w / 2)..(row + 1) * (w / 2)];
    uv[row * w..(row + 1) * w].copy_from_slice(&interleave(u_row, v_row, false));
  }
  let src = Nv12Frame::new(&yp, &uv, w as u32, h as u32, w as u32, w as u32);

  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 sink = MixedSinker::<Nv12>::new(w, h)
      .with_luma(&mut luma)
      .unwrap()
      .with_hsv(&mut hh, &mut ss, &mut vv)
      .unwrap();
    nv12_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
    sink.rgb_scratch.len()
  };

  assert_eq!(
    scratch_len, 0,
    "luma + HSV (no RGB) must not grow the RGB scratch"
  );
  // Luma is the Y plane verbatim (native-Y contract).
  assert_eq!(luma, yp, "luma must equal the native Y plane");
  // HSV row 0 matches the two-step reference.
  let mut rgb0 = std::vec![0u8; w * 3];
  nv12_to_rgb_row(
    &yp[..w],
    &uv[..w],
    &mut rgb0,
    w,
    KernelMatrix::Bt709,
    true,
    true,
  );
  let (rh, rs, rv) = ref_hsv_from_rgb(&rgb0, w, true);
  assert_eq!(&hh[..w], &rh[..], "row 0 H");
  assert_eq!(&ss[..w], &rs[..], "row 0 S");
  assert_eq!(&vv[..w], &rv[..], "row 0 V");
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn nv_hsv_only_grows_no_rgb_scratch_all_formats() {
  let (w, h) = (16usize, 8usize);

  // NV16 (4:2:2, UV) — half-width chroma, full height (`width` bytes/row).
  {
    let yp = plane(w, h, 1);
    let up = plane(w / 2, h, 2);
    let vp = plane(w / 2, h, 3);
    let mut uv = std::vec![0u8; w * h];
    for row in 0..h {
      let u_row = &up[row * (w / 2)..(row + 1) * (w / 2)];
      let v_row = &vp[row * (w / 2)..(row + 1) * (w / 2)];
      uv[row * w..(row + 1) * w].copy_from_slice(&interleave(u_row, v_row, false));
    }
    let src = Nv16Frame::new(&yp, &uv, w as u32, h as u32, w as u32, w as u32);
    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 sink = MixedSinker::<Nv16>::new(w, h)
        .with_hsv(&mut hh, &mut ss, &mut vv)
        .unwrap();
      nv16_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
      sink.rgb_scratch.len()
    };
    assert_eq!(scratch_len, 0, "Nv16 HSV-only RGB-free");
    // Row 0 matches the NV12-kernel reference (4:2:2 reuses it).
    let mut rgb0 = std::vec![0u8; w * 3];
    nv12_to_rgb_row(
      &yp[..w],
      &uv[..w],
      &mut rgb0,
      w,
      KernelMatrix::Bt601,
      true,
      true,
    );
    let (rh, rs, rv) = ref_hsv_from_rgb(&rgb0, w, true);
    assert_eq!(&hh[..w], &rh[..], "nv16 row 0 H");
    assert_eq!(&ss[..w], &rs[..], "nv16 row 0 S");
    assert_eq!(&vv[..w], &rv[..], "nv16 row 0 V");
  }

  // NV21 (4:2:0, VU) — swapped-chroma twin of NV12.
  {
    let yp = plane(w, h, 1);
    let up = plane(w / 2, h / 2, 2);
    let vp = plane(w / 2, h / 2, 3);
    let mut vu = std::vec![0u8; w * (h / 2)];
    for row in 0..h / 2 {
      let u_row = &up[row * (w / 2)..(row + 1) * (w / 2)];
      let v_row = &vp[row * (w / 2)..(row + 1) * (w / 2)];
      vu[row * w..(row + 1) * w].copy_from_slice(&interleave(u_row, v_row, true));
    }
    let src = Nv21Frame::new(&yp, &vu, w as u32, h as u32, w as u32, w as u32);
    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 sink = MixedSinker::<Nv21>::new(w, h)
        .with_hsv(&mut hh, &mut ss, &mut vv)
        .unwrap();
      nv21_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
      sink.rgb_scratch.len()
    };
    assert_eq!(scratch_len, 0, "Nv21 HSV-only RGB-free");
  }

  // NV24 (4:4:4, UV) — full-width chroma (`2 * width` bytes/row).
  {
    let yp = plane(w, h, 1);
    let up = plane(w, h, 2);
    let vp = plane(w, h, 3);
    let mut uv = std::vec![0u8; 2 * w * h];
    for row in 0..h {
      let u_row = &up[row * w..(row + 1) * w];
      let v_row = &vp[row * w..(row + 1) * w];
      uv[row * 2 * w..(row + 1) * 2 * w].copy_from_slice(&interleave(u_row, v_row, false));
    }
    let src = Nv24Frame::new(&yp, &uv, w as u32, h as u32, w as u32, (2 * w) as u32);
    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 sink = MixedSinker::<Nv24>::new(w, h)
        .with_hsv(&mut hh, &mut ss, &mut vv)
        .unwrap();
      nv24_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
      sink.rgb_scratch.len()
    };
    assert_eq!(scratch_len, 0, "Nv24 HSV-only RGB-free");
    let mut rgb0 = std::vec![0u8; w * 3];
    nv24_to_rgb_row(
      &yp[..w],
      &uv[..2 * w],
      &mut rgb0,
      w,
      KernelMatrix::Bt601,
      true,
      true,
    );
    let (rh, rs, rv) = ref_hsv_from_rgb(&rgb0, w, true);
    assert_eq!(&hh[..w], &rh[..], "nv24 row 0 H");
    assert_eq!(&ss[..w], &rs[..], "nv24 row 0 S");
    assert_eq!(&vv[..w], &rv[..], "nv24 row 0 V");
  }

  // NV42 (4:4:4, VU) — swapped-chroma twin of NV24.
  {
    let yp = plane(w, h, 1);
    let up = plane(w, h, 2);
    let vp = plane(w, h, 3);
    let mut vu = std::vec![0u8; 2 * w * h];
    for row in 0..h {
      let u_row = &up[row * w..(row + 1) * w];
      let v_row = &vp[row * w..(row + 1) * w];
      vu[row * 2 * w..(row + 1) * 2 * w].copy_from_slice(&interleave(u_row, v_row, true));
    }
    let src = Nv42Frame::new(&yp, &vu, w as u32, h as u32, w as u32, (2 * w) as u32);
    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 sink = MixedSinker::<Nv42>::new(w, h)
        .with_hsv(&mut hh, &mut ss, &mut vv)
        .unwrap();
      nv42_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
      sink.rgb_scratch.len()
    };
    assert_eq!(scratch_len, 0, "Nv42 HSV-only RGB-free");
  }
}