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
use super::*;

fn solid_yuv410p_frame(
  width: u32,
  height: u32,
  y: u8,
  u: u8,
  v: u8,
) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
  let w = width as usize;
  let h = height as usize;
  let cw = w / 4;
  let ch = h / 4;
  (
    std::vec![y; w * h],
    std::vec![u; cw * ch],
    std::vec![v; cw * ch],
  )
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn luma_only_copies_y_plane() {
  let (yp, up, vp) = solid_yuv410p_frame(16, 8, 42, 128, 128);
  let src = Yuv410pFrame::new(&yp, &up, &vp, 16, 8, 16, 4, 4);

  let mut luma = std::vec![0u8; 16 * 8];
  let mut sink = MixedSinker::<Yuv410p>::new(16, 8)
    .with_luma(&mut luma)
    .unwrap();
  yuv410p_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

  assert!(luma.iter().all(|&y| y == 42), "luma should be solid 42");
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn luma_u16_zero_extends_y_plane() {
  let (yp, up, vp) = solid_yuv410p_frame(16, 8, 200, 128, 128);
  let src = Yuv410pFrame::new(&yp, &up, &vp, 16, 8, 16, 4, 4);

  let mut luma = std::vec![0u16; 16 * 8];
  let mut sink = MixedSinker::<Yuv410p>::new(16, 8)
    .with_luma_u16(&mut luma)
    .unwrap();
  yuv410p_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

  assert!(luma.iter().all(|&y| y == 200u16));
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgb_only_converts_gray_to_gray() {
  // Neutral chroma → gray RGB; solid Y=128 → ~128 in every RGB byte.
  let (yp, up, vp) = solid_yuv410p_frame(16, 8, 128, 128, 128);
  let src = Yuv410pFrame::new(&yp, &up, &vp, 16, 8, 16, 4, 4);

  let mut rgb = std::vec![0u8; 16 * 8 * 3];
  let mut sink = MixedSinker::<Yuv410p>::new(16, 8)
    .with_rgb(&mut rgb)
    .unwrap();
  yuv410p_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

  for px in rgb.chunks(3) {
    assert!(px[0].abs_diff(128) <= 1);
    assert_eq!(px[0], px[1]);
    assert_eq!(px[1], px[2]);
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgba_only_converts_gray_to_gray_with_opaque_alpha() {
  let (yp, up, vp) = solid_yuv410p_frame(16, 8, 128, 128, 128);
  let src = Yuv410pFrame::new(&yp, &up, &vp, 16, 8, 16, 4, 4);

  let mut rgba = std::vec![0u8; 16 * 8 * 4];
  let mut sink = MixedSinker::<Yuv410p>::new(16, 8)
    .with_rgba(&mut rgba)
    .unwrap();
  yuv410p_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

  for px in rgba.chunks(4) {
    assert!(px[0].abs_diff(128) <= 1, "R");
    assert_eq!(px[0], px[1], "RGB monochromatic");
    assert_eq!(px[1], px[2], "RGB monochromatic");
    assert_eq!(px[3], 0xFF, "alpha must default to opaque");
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn hsv_only_produces_gray_hsv() {
  let (yp, up, vp) = solid_yuv410p_frame(16, 8, 128, 128, 128);
  let src = Yuv410pFrame::new(&yp, &up, &vp, 16, 8, 16, 4, 4);

  let mut h = std::vec![0xFFu8; 16 * 8];
  let mut s = std::vec![0xFFu8; 16 * 8];
  let mut v = std::vec![0xFFu8; 16 * 8];
  let mut sink = MixedSinker::<Yuv410p>::new(16, 8)
    .with_hsv(&mut h, &mut s, &mut v)
    .unwrap();
  yuv410p_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

  assert!(h.iter().all(|&b| b == 0));
  assert!(s.iter().all(|&b| b == 0));
  assert!(v.iter().all(|&b| b.abs_diff(128) <= 1));
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn mixed_all_outputs_populated() {
  let (yp, up, vp) = solid_yuv410p_frame(16, 8, 200, 128, 128);
  let src = Yuv410pFrame::new(&yp, &up, &vp, 16, 8, 16, 4, 4);

  let mut rgb = std::vec![0u8; 16 * 8 * 3];
  let mut rgba = std::vec![0u8; 16 * 8 * 4];
  let mut luma = std::vec![0u8; 16 * 8];
  let mut luma_u16 = std::vec![0u16; 16 * 8];
  let mut h = std::vec![0u8; 16 * 8];
  let mut s = std::vec![0u8; 16 * 8];
  let mut v = std::vec![0u8; 16 * 8];
  let mut sink = MixedSinker::<Yuv410p>::new(16, 8)
    .with_rgb(&mut rgb)
    .unwrap()
    .with_rgba(&mut rgba)
    .unwrap()
    .with_luma(&mut luma)
    .unwrap()
    .with_luma_u16(&mut luma_u16)
    .unwrap()
    .with_hsv(&mut h, &mut s, &mut v)
    .unwrap();
  yuv410p_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

  // Luma = Y plane verbatim.
  assert!(luma.iter().all(|&y| y == 200));
  assert!(luma_u16.iter().all(|&y| y == 200u16));
  // RGB monochrome at ~200.
  for px in rgb.chunks(3) {
    assert!(px[0].abs_diff(200) <= 1);
  }
  // Strategy A fan-out: RGBA's RGB bytes should match RGB output, alpha = 0xFF.
  for (rgb_px, rgba_px) in rgb.chunks(3).zip(rgba.chunks(4)) {
    assert_eq!(rgb_px[0], rgba_px[0]);
    assert_eq!(rgb_px[1], rgba_px[1]);
    assert_eq!(rgb_px[2], rgba_px[2]);
    assert_eq!(rgba_px[3], 0xFF);
  }
  // HSV of gray.
  assert!(h.iter().all(|&b| b == 0));
  assert!(s.iter().all(|&b| b == 0));
  assert!(v.iter().all(|&b| b.abs_diff(200) <= 1));
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn vertical_chroma_subsampling_4x_each_chroma_row_covers_4_y_rows() {
  // Build a 16x8 frame with two distinct chroma blocks vertically:
  // chroma row 0 covers Y rows 0..3, chroma row 1 covers Y rows 4..7.
  // Set chroma block 0 to (U=80, V=180) and block 1 to (U=160, V=80) —
  // distinct enough to produce visibly different RGB across the
  // horizontal divide.
  let w = 16usize;
  let h = 8usize;
  let cw = w / 4;
  // Two chroma rows: row 0 (covers Y rows 0..3) and row 1 (covers Y rows 4..7).
  let mut up = std::vec![0u8; cw * 2];
  let mut vp = std::vec![0u8; cw * 2];
  for x in 0..cw {
    up[x] = 80;
    up[cw + x] = 160;
    vp[x] = 180;
    vp[cw + x] = 80;
  }
  let yp = std::vec![128u8; w * h];

  let src = Yuv410pFrame::new(
    &yp, &up, &vp, w as u32, h as u32, w as u32, cw as u32, cw as u32,
  );

  let mut rgb = std::vec![0u8; w * h * 3];
  let mut sink = MixedSinker::<Yuv410p>::new(w, h)
    .with_rgb(&mut rgb)
    .unwrap();
  yuv410p_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

  // Y rows 0..=3 should all share the same RGB color (chroma block 0).
  let row_bytes = w * 3;
  let r0 = &rgb[..row_bytes];
  for y_idx in 1..4 {
    let row = &rgb[y_idx * row_bytes..(y_idx + 1) * row_bytes];
    assert_eq!(
      r0, row,
      "Y row 0 and row {y_idx} should share chroma — same RGB"
    );
  }
  // Y rows 4..=7 should all share the same (different) RGB color.
  let r4 = &rgb[4 * row_bytes..5 * row_bytes];
  for y_idx in 5..8 {
    let row = &rgb[y_idx * row_bytes..(y_idx + 1) * row_bytes];
    assert_eq!(
      r4, row,
      "Y row 4 and row {y_idx} should share chroma — same RGB"
    );
  }
  // The two chroma blocks produce DIFFERENT RGB.
  assert_ne!(
    r0, r4,
    "distinct chroma blocks should produce distinct RGB output"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn horizontal_chroma_subsampling_4x_each_chroma_sample_covers_4_y_columns() {
  // Build a 16x4 frame with two distinct chroma samples per row:
  // sample 0 covers Y columns 0..3, sample 1 covers Y columns 4..7,
  // sample 2 covers Y columns 8..11, sample 3 covers Y columns 12..15.
  let w = 16usize;
  let h = 4usize;
  let cw = w / 4;
  let mut up = std::vec![0u8; cw];
  let mut vp = std::vec![0u8; cw];
  // Different chroma in each of the 4 horizontal blocks.
  for x in 0..cw {
    up[x] = (60 + x * 30) as u8; // 60, 90, 120, 150
    vp[x] = (200 - x * 30) as u8; // 200, 170, 140, 110
  }
  let yp = std::vec![128u8; w * h];

  let src = Yuv410pFrame::new(
    &yp, &up, &vp, w as u32, h as u32, w as u32, cw as u32, cw as u32,
  );

  let mut rgb = std::vec![0u8; w * h * 3];
  let mut sink = MixedSinker::<Yuv410p>::new(w, h)
    .with_rgb(&mut rgb)
    .unwrap();
  yuv410p_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();

  // Within row 0, columns 0..=3 should all share RGB; columns 4..=7
  // should share a different RGB; etc.
  let row0 = &rgb[..w * 3];
  for block in 0..cw {
    let block_start = block * 4;
    let p0 = &row0[block_start * 3..(block_start + 1) * 3];
    for x in 1..4 {
      let p = &row0[(block_start + x) * 3..(block_start + x + 1) * 3];
      assert_eq!(
        p0, p,
        "block {block}, col {x} should share chroma with col 0"
      );
    }
  }
  // Adjacent blocks must produce different RGB (chroma differs).
  for block in 0..cw - 1 {
    let p_a = &row0[block * 4 * 3..(block * 4 + 1) * 3];
    let p_b = &row0[(block + 1) * 4 * 3..((block + 1) * 4 + 1) * 3];
    assert_ne!(
      p_a,
      p_b,
      "block {block} and {} have distinct chroma → distinct RGB",
      block + 1
    );
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgb_simd_matches_scalar_pseudo_random() {
  // SIMD ↔ scalar parity at the sinker level. Width 64 exercises both
  // the NEON 16-lane main loop and the trailing scalar-fallback path
  // (no tail at 64 since 64 is a multiple of 16, but tests that the
  // routing produces the same result either way).
  let w = 64u32;
  let h = 8u32;
  let cw = (w / 4) as usize;
  let ch = (h / 4) as usize;
  let mut yp = std::vec![0u8; (w * h) as usize];
  let mut up = std::vec![0u8; cw * ch];
  let mut vp = std::vec![0u8; cw * ch];
  pseudo_random_u8(&mut yp, 0xC0FFEE);
  pseudo_random_u8(&mut up, 0xDECAFBAD);
  pseudo_random_u8(&mut vp, 0xBADF00D);
  let src = Yuv410pFrame::new(&yp, &up, &vp, w, h, w, cw as u32, cw as u32);

  let mut rgb_simd = std::vec![0u8; (w * h) as usize * 3];
  let mut rgb_scalar = std::vec![0u8; (w * h) as usize * 3];

  let mut sink_simd = MixedSinker::<Yuv410p>::new(w as usize, h as usize)
    .with_rgb(&mut rgb_simd)
    .unwrap();
  yuv410p_to(&src, true, sink_simd.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();

  let mut sink_scalar = MixedSinker::<Yuv410p>::new(w as usize, h as usize)
    .with_rgb(&mut rgb_scalar)
    .unwrap();
  sink_scalar.set_simd(false);
  yuv410p_to(
    &src,
    true,
    sink_scalar.set_kernel_matrix(KernelMatrix::Bt709),
  )
  .unwrap();

  assert_eq!(
    rgb_simd, rgb_scalar,
    "SIMD and scalar must produce byte-identical RGB"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn non_4_aligned_height_reuses_trailing_chroma_row() {
  // Height = 6 → chroma height = ceil(6/4) = 2. Y rows 0..=3 read
  // chroma row 0; Y rows 4..=5 read chroma row 1. Distinct chroma
  // values per chroma row let us assert the walker mapped the
  // trailing partial group correctly.
  let w: u32 = 16;
  let h: u32 = 6;
  let cw = (w / 4) as usize; // 4
  let ch = h.div_ceil(4) as usize; // 2

  // Distinct chroma per row: row 0 = (40, 200), row 1 = (200, 40).
  let mut up = std::vec![0u8; cw * ch];
  let mut vp = std::vec![0u8; cw * ch];
  for x in 0..cw {
    up[x] = 40;
    vp[x] = 200;
    up[cw + x] = 200;
    vp[cw + x] = 40;
  }
  // Mid-gray Y so the chroma swing is what dominates color output.
  let yp = std::vec![128u8; (w * h) as usize];

  let src = Yuv410pFrame::try_new(&yp, &up, &vp, w, h, w, cw as u32, cw as u32).expect("valid");

  let mut rgb_simd = std::vec![0u8; (w * h) as usize * 3];
  let mut rgb_scalar = std::vec![0u8; (w * h) as usize * 3];

  let mut sink_simd = MixedSinker::<Yuv410p>::new(w as usize, h as usize)
    .with_rgb(&mut rgb_simd)
    .unwrap();
  yuv410p_to(&src, true, sink_simd.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();

  let mut sink_scalar = MixedSinker::<Yuv410p>::new(w as usize, h as usize)
    .with_rgb(&mut rgb_scalar)
    .unwrap();
  sink_scalar.set_simd(false);
  yuv410p_to(
    &src,
    true,
    sink_scalar.set_kernel_matrix(KernelMatrix::Bt709),
  )
  .unwrap();

  assert_eq!(
    rgb_simd, rgb_scalar,
    "SIMD and scalar must agree at non-4-aligned heights"
  );

  // Sanity: the top half (chroma row 0, U=40 V=200) and bottom strip
  // (chroma row 1, U=200 V=40) must produce visibly different colors.
  let row_stride = (w * 3) as usize;
  let top_red = rgb_scalar[0];
  let bot_red = rgb_scalar[5 * row_stride];
  assert_ne!(
    top_red, bot_red,
    "row 0 and row 5 should derive from different chroma rows"
  );
}

#[test]
fn non_multiple_of_4_width_surfaces_width_alignment_multiple_of_four_error() {
  // Yuv410p subsamples chroma 4:1:0, so widths not divisible by 4
  // can't form a complete chroma group. The sinker must return
  // `WidthAlignment` with `WidthAlignmentRequirement::MultipleOfFour`
  // — matching the format-specific `Yuv410pFrameError::WidthNotMultipleOf4`
  // returned by `Yuv410pFrame::try_new` and the Uyyvyy411 sinker
  // convention. The pre-refactor variant `OddWidth` (whose message was
  // scoped to 4:2:0) is no longer applicable here.
  let mut rgb = std::vec![0u8; 18 * 8 * 3];
  let mut sink = MixedSinker::<Yuv410p>::new(18, 8)
    .with_rgb(&mut rgb)
    .unwrap();
  let err = sink.begin_frame(18, 8).unwrap_err();
  assert_eq!(
    err,
    MixedSinkerError::WidthAlignment(WidthAlignment::multiple_of_four(18,)),
    "expected WidthAlignment {{ width: 18, required: MultipleOfFour }}, got {err:?}"
  );
}

// Atomicity (#308): the up-front RGB-scratch preflight must return
// `AllocationFailed` BEFORE any output row is written, so an allocator refusal
// leaves the output frame untouched. Mirrors the Yuv420p sibling. Reuses the
// crate's RGB-scratch failpoint (`yuva`-gated, so this test is too; under
// `--all-features` both `yuv-planar` and `yuva` are on).
#[cfg(feature = "yuva")]
#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv410p_rgb_scratch_alloc_failure_leaves_outputs_untouched() {
  use crate::resample::ResampleError;

  // luma + RGBA + HSV with NO rgb output: `want_hsv && want_rgba && !want_rgb`
  // needs an RGB row but has no caller RGB buffer, so the decode grows the RGB
  // row scratch (`rgb_row_buf_or_scratch`'s scratch arm). With that allocation
  // armed to fail, the preflight returns AllocationFailed BEFORE any output
  // row — luma included — is written.
  let (yp, up, vp) = solid_yuv410p_frame(16, 8, 42, 128, 128);
  let src = Yuv410pFrame::new(&yp, &up, &vp, 16, 8, 16, 4, 4);
  let mut luma = std::vec![0xABu8; 16 * 8];
  let mut rgba = std::vec![0xCDu8; 16 * 8 * 4];
  let (mut hh, mut ss, mut vv) = (
    std::vec![0xCDu8; 16 * 8],
    std::vec![0xCDu8; 16 * 8],
    std::vec![0xCDu8; 16 * 8],
  );
  let mut sink = MixedSinker::<Yuv410p>::new(16, 8)
    .with_luma(&mut luma)
    .unwrap()
    .with_rgba(&mut rgba)
    .unwrap()
    .with_hsv(&mut hh, &mut ss, &mut vv)
    .unwrap();

  super::super::arm_rgb_scratch_alloc_failure();
  let err = yuv410p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap_err();
  drop(sink);

  assert!(
    matches!(
      err,
      MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
    ),
    "RGB-scratch refusal must surface as a recoverable AllocationFailed, got {err:?}"
  );
  assert!(
    luma.iter().all(|&b| b == 0xAB),
    "luma must be untouched on the rgb-scratch alloc-failure path"
  );
}