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
//! #263 PR 6 — direct **4:4:4-packed** YUV→HSV kernels + RGB-free
//! routing (ayuv64 / vuya / vuyx / xv36).
//!
//! Two concerns, mirroring the planar / semi-planar HSV suites:
//!
//! 1. **Row-kernel parity** — each `{fmt}_to_hsv_row` dispatcher must be
//!    byte-identical to `rgb_to_hsv_row({fmt}_to_rgb_row(...))` within a
//!    tier (they share the same **8-bit** RGB intermediate the existing
//!    packed→RGB→HSV path uses, and the same per-tier HSV), across the
//!    three colour matrices, full/limited range, both endiannesses where
//!    the format carries a `BE` wire variant (ayuv64 / xv36), and a
//!    spread of widths — for both the scalar path (`use_simd = false`)
//!    and the host SIMD path (`use_simd = true`). The α / padding slot is
//!    independent of HSV (HSV derives from the Y/U/V colour only), so the
//!    reference RGB kernel and the HSV kernel both drop it identically.
//! 2. **Structural** — a 4:4:4-packed-YUV sink with ONLY `with_hsv()`
//!    (no RGB / RGBA) must not grow the source-width RGB scratch.

use super::*;
use crate::row::{
  ayuv64_to_hsv_row, ayuv64_to_rgb_row, rgb_to_hsv_row, vuya_to_hsv_row, vuya_to_rgb_row,
  vuyx_to_hsv_row, vuyx_to_rgb_row, xv36_to_hsv_row, xv36_to_rgb_row,
};

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

/// A non-trivial, non-gray pseudo-random `u8` sample at a given position,
/// so the HSV hue / saturation branches are all exercised rather than the
/// degenerate gray fast-path.
fn samp8(i: usize, salt: usize) -> u8 {
  (i.wrapping_mul(2_654_435_761)
    .wrapping_add(salt.wrapping_mul(40_503))
    .wrapping_add(0x9E37) as u32
    & 0xFF) as u8
}

/// A non-trivial pseudo-random `u16` sample (full 16-bit range) at a
/// given position — used for AYUV64 (16-bit native) and, MSB-aligned, for
/// XV36 (12-bit).
fn samp16(i: usize, salt: usize) -> u16 {
  (i.wrapping_mul(2_654_435_761)
    .wrapping_add(salt.wrapping_mul(40_503))
    .wrapping_add(0x9E37) as u32
    & 0xFFFF) as u16
}

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);
  }};
}

// ---- AYUV64 (16-bit, BE-threaded) --------------------------------------

/// Build host-native LE/BE packed AYUV64 buffers (slot order `[A, Y, U,
/// V]`) for `width` pixels. Returns `(le, be)` where each `u16`, when
/// serialized via `to_ne_bytes`, reproduces the LE/BE wire bytes — so the
/// `be_input = false` / `be_input = true` kernel paths are exercised
/// regardless of host endianness.
fn ayuv64_le_be(width: usize) -> (Vec<u16>, Vec<u16>) {
  let mut intended = Vec::with_capacity(width * 4);
  for x in 0..width {
    intended.push(samp16(x, 0)); // A (dropped by HSV)
    intended.push(samp16(x, 1)); // Y
    intended.push(samp16(x, 2)); // U
    intended.push(samp16(x, 3)); // V
  }
  let le = intended
    .iter()
    .map(|v| u16::from_ne_bytes(v.to_le_bytes()))
    .collect();
  let be = intended
    .iter()
    .map(|v| u16::from_ne_bytes(v.to_be_bytes()))
    .collect();
  (le, be)
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_hsv_row_matches_rgb_then_hsv() {
  for &w in &[1usize, 3, 5, 15, 16, 31, 64, 65] {
    let (le, be) = ayuv64_le_be(w);
    for (be_input, buf) in [(false, &le), (true, &be)] {
      for &matrix in &MATRICES {
        for &full_range in &[true, false] {
          for &use_simd in &[false, true] {
            let mut rgb = std::vec![0u8; w * 3];
            ayuv64_to_rgb_row(buf, &mut rgb, w, matrix, full_range, use_simd, be_input);
            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];
            ayuv64_to_hsv_row(
              buf, &mut h, &mut s, &mut v, w, matrix, full_range, use_simd, be_input,
            );
            assert_planes_eq!(
              (h, s, v),
              want,
              std::format!(
                "ayuv64 be={be_input} w={w} {matrix:?} full={full_range} simd={use_simd}"
              )
            );
          }
        }
      }
    }
  }
}

// ---- XV36 (12-bit MSB-aligned, BE-threaded) ----------------------------

/// Build host-native LE/BE packed XV36 buffers (slot order `[U, Y, V, A]`,
/// each MSB-aligned at 12 bits — low 4 bits zero) for `width` pixels.
fn xv36_le_be(width: usize) -> (Vec<u16>, Vec<u16>) {
  let mut intended = Vec::with_capacity(width * 4);
  for x in 0..width {
    // Mask to 12 bits then shift up 4 to MSB-align (matches the XV36 wire
    // layout; the kernels `>> 4` to recover the 12-bit value).
    intended.push((samp16(x, 5) & 0x0FFF) << 4); // U
    intended.push((samp16(x, 6) & 0x0FFF) << 4); // Y
    intended.push((samp16(x, 7) & 0x0FFF) << 4); // V
    intended.push((samp16(x, 8) & 0x0FFF) << 4); // A (padding)
  }
  let le = intended
    .iter()
    .map(|v| u16::from_ne_bytes(v.to_le_bytes()))
    .collect();
  let be = intended
    .iter()
    .map(|v| u16::from_ne_bytes(v.to_be_bytes()))
    .collect();
  (le, be)
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn xv36_hsv_row_matches_rgb_then_hsv() {
  for &w in &[1usize, 3, 5, 15, 16, 31, 64, 65] {
    let (le, be) = xv36_le_be(w);
    for (be_input, buf) in [(false, &le), (true, &be)] {
      for &matrix in &MATRICES {
        for &full_range in &[true, false] {
          for &use_simd in &[false, true] {
            let mut rgb = std::vec![0u8; w * 3];
            xv36_to_rgb_row(buf, &mut rgb, w, matrix, full_range, use_simd, be_input);
            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];
            xv36_to_hsv_row(
              buf, &mut h, &mut s, &mut v, w, matrix, full_range, use_simd, be_input,
            );
            assert_planes_eq!(
              (h, s, v),
              want,
              std::format!("xv36 be={be_input} w={w} {matrix:?} full={full_range} simd={use_simd}")
            );
          }
        }
      }
    }
  }
}

// ---- VUYA / VUYX (8-bit, no endian variant) ----------------------------

/// Build a packed VUYA / VUYX buffer (byte order `[V, U, Y, A]`) for
/// `width` pixels.
fn vuya_packed(width: usize) -> Vec<u8> {
  let mut out = Vec::with_capacity(width * 4);
  for x in 0..width {
    out.push(samp8(x, 11)); // V
    out.push(samp8(x, 12)); // U
    out.push(samp8(x, 13)); // Y
    out.push(samp8(x, 14)); // A / X (dropped by HSV)
  }
  out
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn vuya_hsv_row_matches_rgb_then_hsv() {
  for &w in &[1usize, 3, 5, 15, 16, 31, 64, 65] {
    let buf = vuya_packed(w);
    for &matrix in &MATRICES {
      for &full_range in &[true, false] {
        for &use_simd in &[false, true] {
          let mut rgb = std::vec![0u8; w * 3];
          vuya_to_rgb_row(&buf, &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];
          vuya_to_hsv_row(
            &buf, &mut h, &mut s, &mut v, w, matrix, full_range, use_simd,
          );
          assert_planes_eq!(
            (h, s, v),
            want,
            std::format!("vuya w={w} {matrix:?} full={full_range} simd={use_simd}")
          );
        }
      }
    }
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn vuyx_hsv_row_matches_rgb_then_hsv() {
  // VUYX shares VUYA's byte stream; the padding byte is dropped by both
  // the RGB and HSV kernels, so the parity oracle is the VUYX RGB kernel.
  for &w in &[1usize, 3, 5, 15, 16, 31, 64, 65] {
    let buf = vuya_packed(w);
    for &matrix in &MATRICES {
      for &full_range in &[true, false] {
        for &use_simd in &[false, true] {
          let mut rgb = std::vec![0u8; w * 3];
          vuyx_to_rgb_row(&buf, &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];
          vuyx_to_hsv_row(
            &buf, &mut h, &mut s, &mut v, w, matrix, full_range, use_simd,
          );
          assert_planes_eq!(
            (h, s, v),
            want,
            std::format!("vuyx w={w} {matrix:?} full={full_range} simd={use_simd}")
          );
        }
      }
    }
  }
}

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

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

  // AYUV64 — 16-bit packed, slot order [A, Y, U, V].
  {
    let mut buf = Vec::with_capacity(w * h * 4);
    for i in 0..w * h {
      buf.push(samp16(i, 0));
      buf.push(samp16(i, 1));
      buf.push(samp16(i, 2));
      buf.push(samp16(i, 3));
    }
    let src = Ayuv64Frame::try_new(&buf, w as u32, h as u32, (w * 4) 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 sink = MixedSinker::<Ayuv64>::new(w, h)
        .with_hsv(&mut hh, &mut ss, &mut vv)
        .unwrap();
      ayuv64_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
      sink.rgb_scratch.len()
    };
    assert_eq!(
      scratch_len, 0,
      "Ayuv64 HSV-only must not grow the RGB scratch"
    );

    // Cross-check row 0 against the explicit YUV→RGB→HSV reference (LE).
    let mut rgb0 = std::vec![0u8; w * 3];
    ayuv64_to_rgb_row(
      &buf[..w * 4],
      &mut rgb0,
      w,
      KernelMatrix::Bt709,
      true,
      true,
      false,
    );
    let (rh, rs, rv) = ref_hsv_from_rgb(&rgb0, w, true);
    assert_eq!(&hh[..w], &rh[..], "ayuv64 row 0 H");
    assert_eq!(&ss[..w], &rs[..], "ayuv64 row 0 S");
    assert_eq!(&vv[..w], &rv[..], "ayuv64 row 0 V");
  }

  // XV36 — 12-bit MSB-aligned packed, slot order [U, Y, V, A].
  {
    let mut buf = Vec::with_capacity(w * h * 4);
    for i in 0..w * h {
      buf.push((samp16(i, 5) & 0x0FFF) << 4);
      buf.push((samp16(i, 6) & 0x0FFF) << 4);
      buf.push((samp16(i, 7) & 0x0FFF) << 4);
      buf.push((samp16(i, 8) & 0x0FFF) << 4);
    }
    let src = Xv36Frame::new(&buf, w as u32, h as u32, (w * 4) 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::<Xv36>::new(w, h)
        .with_hsv(&mut hh, &mut ss, &mut vv)
        .unwrap();
      xv36_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
      sink.rgb_scratch.len()
    };
    assert_eq!(
      scratch_len, 0,
      "Xv36 HSV-only must not grow the RGB scratch"
    );
  }

  // VUYA — 8-bit packed, byte order [V, U, Y, A].
  {
    let buf = vuya_packed(w * h);
    let src = VuyaFrame::try_new(&buf, w as u32, h as u32, (w * 4) 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 sink = MixedSinker::<Vuya>::new(w, h)
        .with_hsv(&mut hh, &mut ss, &mut vv)
        .unwrap();
      vuya_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
      sink.rgb_scratch.len()
    };
    assert_eq!(
      scratch_len, 0,
      "Vuya HSV-only must not grow the RGB scratch"
    );
  }

  // VUYX — 8-bit packed, byte order [V, U, Y, X] (X padding).
  {
    let buf = vuya_packed(w * h);
    let src = VuyxFrame::try_new(&buf, w as u32, h as u32, (w * 4) 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 sink = MixedSinker::<Vuyx>::new(w, h)
        .with_hsv(&mut hh, &mut ss, &mut vv)
        .unwrap();
      vuyx_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
      sink.rgb_scratch.len()
    };
    assert_eq!(
      scratch_len, 0,
      "Vuyx HSV-only must not grow the RGB scratch"
    );
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn vuya_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 buf = vuya_packed(w * h);
  let src = VuyaFrame::try_new(&buf, w as u32, h as u32, (w * 4) 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 sink = MixedSinker::<Vuya>::new(w, h)
      .with_luma(&mut luma)
      .unwrap()
      .with_hsv(&mut hh, &mut ss, &mut vv)
      .unwrap();
    vuya_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"
  );
  // Native Y luma: Y is at byte offset 2 of each quadruple, copied verbatim.
  for (i, &got) in luma.iter().enumerate() {
    assert_eq!(got, buf[i * 4 + 2], "vuya luma row pixel {i}");
  }
  // HSV row 0 matches the two-step reference.
  let mut rgb0 = std::vec![0u8; w * 3];
  vuya_to_rgb_row(
    &buf[..w * 4],
    &mut rgb0,
    w,
    KernelMatrix::Bt709,
    true,
    false,
  );
  let (rh, rs, rv) = ref_hsv_from_rgb(&rgb0, w, true);
  assert_eq!(&hh[..w], &rh[..], "vuya row 0 H");
  assert_eq!(&ss[..w], &rs[..], "vuya row 0 S");
  assert_eq!(&vv[..w], &rv[..], "vuya row 0 V");
}