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
//! VUYA (FFmpeg `AV_PIX_FMT_VUYA`) row-level dispatchers (Ship 12c).
//!
//! Three entries: `vuya_to_rgb_row`, `vuya_to_rgba_row`,
//! `vuya_to_luma_row`. Routes through the standard `cfg_select!`
//! per-arch block; `use_simd = false` forces scalar.
//!
//! VUYA is 4:4:4 (no chroma subsampling): each pixel is a 4-byte
//! quadruple `[V(8), U(8), Y(8), A(8)]`. Buffer length is `width x 4`
//! bytes — no even-width restriction.
//!
//! The RGB dispatcher (`vuya_to_rgb_row`) is shared with VUYX because
//! the byte stream is bit-identical for the RGB path (alpha is
//! discarded). `vuyx_to_rgb_row` in `dispatch::vuyx` is a re-export.
//! Likewise `vuya_to_luma_row` is re-exported as `vuyx_to_luma_row`.

#[cfg(any(
  target_arch = "aarch64",
  target_arch = "x86_64",
  target_arch = "wasm32"
))]
use crate::row::arch;
#[cfg(target_arch = "aarch64")]
use crate::row::neon_available;
#[cfg(target_arch = "wasm32")]
use crate::row::simd128_available;
#[cfg(target_arch = "x86_64")]
use crate::row::{avx2_available, avx512_available, sse41_available};
use crate::{
  KernelMatrix,
  row::{rgb_row_bytes, rgba_row_bytes, scalar},
};

/// Returns the minimum byte count of one packed VUYA / VUYX row
/// (`width x 4`) with overflow checking. Panics if `width x 4` cannot
/// be represented as `usize` (only reachable on 32-bit targets with
/// extreme widths).
#[cfg_attr(not(tarpaulin), inline(always))]
fn vuya_packed_bytes(width: usize) -> usize {
  match width.checked_mul(4) {
    Some(n) => n,
    None => panic!("width ({width}) x 4 overflows usize (VUYA packed row)"),
  }
}

/// Converts one row of VUYA or VUYX to packed RGB (u8). The alpha byte in
/// the source is discarded — RGB output has no alpha channel. This kernel
/// is bit-identical for both VUYA and VUYX (the A byte difference is
/// irrelevant when there is no α store), so `vuyx_to_rgb_row` in
/// `dispatch::vuyx` is a re-export of this function. `use_simd = false`
/// forces scalar.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn vuya_to_rgb_row(
  packed: &[u8],
  rgb_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
  use_simd: bool,
) {
  assert!(
    packed.len() >= vuya_packed_bytes(width),
    "packed row too short"
  );
  assert!(
    rgb_out.len() >= rgb_row_bytes(width),
    "rgb_out row too short"
  );

  if use_simd {
    cfg_select! {
      target_arch = "aarch64" => {
        if neon_available() {
          // SAFETY: NEON verified at runtime.
          unsafe {
            arch::neon::vuya_to_rgb_row(packed, rgb_out, width, matrix, full_range);
          }
          return;
        }
      }
      target_arch = "x86_64" => {
        if avx512_available() {
          // SAFETY: AVX-512BW verified.
          unsafe {
            arch::x86_avx512::vuya_to_rgb_row(packed, rgb_out, width, matrix, full_range);
          }
          return;
        }
        if avx2_available() {
          // SAFETY: AVX2 verified.
          unsafe {
            arch::x86_avx2::vuya_to_rgb_row(packed, rgb_out, width, matrix, full_range);
          }
          return;
        }
        if sse41_available() {
          // SAFETY: SSE4.1 verified.
          unsafe {
            arch::x86_sse41::vuya_to_rgb_row(packed, rgb_out, width, matrix, full_range);
          }
          return;
        }
      }
      target_arch = "wasm32" => {
        if simd128_available() {
          // SAFETY: simd128 compile-time verified.
          unsafe {
            arch::wasm_simd128::vuya_to_rgb_row(packed, rgb_out, width, matrix, full_range);
          }
          return;
        }
      }
      _ => {}
    }
  }

  scalar::vuya_to_rgb_row(packed, rgb_out, width, matrix, full_range);
}

/// Converts one row of VUYA / VUYX **directly** to planar HSV bytes
/// (OpenCV `cv2.COLOR_RGB2HSV` encoding: `H ∈ [0, 179]`,
/// `S, V ∈ [0, 255]`), without materializing a source-width RGB row.
/// Byte-identical to `rgb_to_hsv_row(vuya_to_rgb_row(...))` within the
/// selected tier — the SIMD path stages a fixed 64-pixel 8-bit RGB chunk
/// internally. The α byte is dropped (HSV is colour-only), so this kernel
/// is bit-identical for both VUYA and VUYX; `vuyx_to_hsv_row` in
/// `dispatch::vuyx` is a re-export. See `scalar::vuya_to_hsv_row` for the
/// reference. `use_simd = false` forces the scalar reference path.
#[cfg_attr(not(tarpaulin), inline(always))]
#[allow(clippy::too_many_arguments)]
pub fn vuya_to_hsv_row(
  packed: &[u8],
  h_out: &mut [u8],
  s_out: &mut [u8],
  v_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
  use_simd: bool,
) {
  assert!(
    packed.len() >= vuya_packed_bytes(width),
    "packed row too short"
  );
  assert!(h_out.len() >= width, "h_out row too short");
  assert!(s_out.len() >= width, "s_out row too short");
  assert!(v_out.len() >= width, "v_out row too short");

  if use_simd {
    cfg_select! {
      target_arch = "aarch64" => {
        if neon_available() {
          // SAFETY: NEON verified at runtime.
          unsafe {
            arch::neon::vuya_to_hsv_row(packed, h_out, s_out, v_out, width, matrix, full_range);
          }
          return;
        }
      }
      target_arch = "x86_64" => {
        if avx512_available() {
          // SAFETY: AVX-512BW verified.
          unsafe {
            arch::x86_avx512::vuya_to_hsv_row(
              packed, h_out, s_out, v_out, width, matrix, full_range,
            );
          }
          return;
        }
        if avx2_available() {
          // SAFETY: AVX2 verified.
          unsafe {
            arch::x86_avx2::vuya_to_hsv_row(packed, h_out, s_out, v_out, width, matrix, full_range);
          }
          return;
        }
        if sse41_available() {
          // SAFETY: SSE4.1 verified.
          unsafe {
            arch::x86_sse41::vuya_to_hsv_row(
              packed, h_out, s_out, v_out, width, matrix, full_range,
            );
          }
          return;
        }
      }
      target_arch = "wasm32" => {
        if simd128_available() {
          // SAFETY: simd128 compile-time verified.
          unsafe {
            arch::wasm_simd128::vuya_to_hsv_row(
              packed, h_out, s_out, v_out, width, matrix, full_range,
            );
          }
          return;
        }
      }
      _ => {}
    }
  }

  scalar::vuya_to_hsv_row(packed, h_out, s_out, v_out, width, matrix, full_range);
}

/// Converts one row of VUYA to packed RGBA (u8). The source alpha byte at
/// offset 3 of each pixel quadruple is passed through verbatim
/// (`ALPHA_SRC = true`). For VUYX (where the A byte is padding and output
/// α should be `0xFF`), use `vuyx_to_rgba_row` instead. `use_simd = false`
/// forces scalar.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn vuya_to_rgba_row(
  packed: &[u8],
  rgba_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
  use_simd: bool,
) {
  assert!(
    packed.len() >= vuya_packed_bytes(width),
    "packed row too short"
  );
  assert!(
    rgba_out.len() >= rgba_row_bytes(width),
    "rgba_out row too short"
  );

  if use_simd {
    cfg_select! {
      target_arch = "aarch64" => {
        if neon_available() {
          // SAFETY: NEON verified.
          unsafe {
            arch::neon::vuya_to_rgba_row(packed, rgba_out, width, matrix, full_range);
          }
          return;
        }
      }
      target_arch = "x86_64" => {
        if avx512_available() {
          // SAFETY: AVX-512BW verified.
          unsafe {
            arch::x86_avx512::vuya_to_rgba_row(packed, rgba_out, width, matrix, full_range);
          }
          return;
        }
        if avx2_available() {
          // SAFETY: AVX2 verified.
          unsafe {
            arch::x86_avx2::vuya_to_rgba_row(packed, rgba_out, width, matrix, full_range);
          }
          return;
        }
        if sse41_available() {
          // SAFETY: SSE4.1 verified.
          unsafe {
            arch::x86_sse41::vuya_to_rgba_row(packed, rgba_out, width, matrix, full_range);
          }
          return;
        }
      }
      target_arch = "wasm32" => {
        if simd128_available() {
          // SAFETY: simd128 compile-time verified.
          unsafe {
            arch::wasm_simd128::vuya_to_rgba_row(packed, rgba_out, width, matrix, full_range);
          }
          return;
        }
      }
      _ => {}
    }
  }

  scalar::vuya_to_rgba_row(packed, rgba_out, width, matrix, full_range);
}

/// Extracts one row of 8-bit luma from a packed VUYA or VUYX buffer. Y is
/// at byte offset 2 of each pixel quadruple; the V, U, and A bytes are
/// ignored. Avoids the full YUV→RGB pipeline when only luma is needed.
/// This function is shared with VUYX (`vuyx_to_luma_row` in
/// `dispatch::vuyx` re-exports it). `use_simd = false` forces scalar.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn vuya_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize, use_simd: bool) {
  assert!(
    packed.len() >= vuya_packed_bytes(width),
    "packed row too short"
  );
  assert!(luma_out.len() >= width, "luma_out row too short");

  if use_simd {
    cfg_select! {
      target_arch = "aarch64" => {
        if neon_available() {
          // SAFETY: NEON verified.
          unsafe {
            arch::neon::vuya_to_luma_row(packed, luma_out, width);
          }
          return;
        }
      }
      target_arch = "x86_64" => {
        if avx512_available() {
          // SAFETY: AVX-512BW verified.
          unsafe {
            arch::x86_avx512::vuya_to_luma_row(packed, luma_out, width);
          }
          return;
        }
        if avx2_available() {
          // SAFETY: AVX2 verified.
          unsafe {
            arch::x86_avx2::vuya_to_luma_row(packed, luma_out, width);
          }
          return;
        }
        if sse41_available() {
          // SAFETY: SSE4.1 verified.
          unsafe {
            arch::x86_sse41::vuya_to_luma_row(packed, luma_out, width);
          }
          return;
        }
      }
      target_arch = "wasm32" => {
        if simd128_available() {
          // SAFETY: simd128 compile-time verified.
          unsafe {
            arch::wasm_simd128::vuya_to_luma_row(packed, luma_out, width);
          }
          return;
        }
      }
      _ => {}
    }
  }

  scalar::vuya_to_luma_row(packed, luma_out, width);
}

/// Extracts one row of u16 luma (zero-extended Y bytes) from a packed VUYA
/// or VUYX buffer. Y is at byte offset 2 of each pixel quadruple; the V, U,
/// and A/X bytes are ignored. `use_simd = false` forces scalar.
///
/// This dispatcher is shared between VUYA and VUYX (Y position is identical).
/// `vuyx_to_luma_u16_row` in `dispatch::vuyx` re-exports this function.
#[cfg_attr(not(any(feature = "std", feature = "alloc")), allow(dead_code))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn vuya_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize, use_simd: bool) {
  assert!(
    packed.len() >= vuya_packed_bytes(width),
    "packed row too short"
  );
  assert!(out.len() >= width, "out too short");

  if use_simd {
    cfg_select! {
      target_arch = "aarch64" => {
        if neon_available() {
          // SAFETY: NEON verified at runtime.
          unsafe {
            arch::neon::vuya_to_luma_u16_row(packed, out, width);
          }
          return;
        }
      }
      target_arch = "x86_64" => {
        if avx512_available() {
          // SAFETY: AVX-512BW verified.
          unsafe {
            arch::x86_avx512::vuya_to_luma_u16_row(packed, out, width);
          }
          return;
        }
        if avx2_available() {
          // SAFETY: AVX2 verified.
          unsafe {
            arch::x86_avx2::vuya_to_luma_u16_row(packed, out, width);
          }
          return;
        }
        if sse41_available() {
          // SAFETY: SSE4.1 verified.
          unsafe {
            arch::x86_sse41::vuya_to_luma_u16_row(packed, out, width);
          }
          return;
        }
      }
      target_arch = "wasm32" => {
        if simd128_available() {
          // SAFETY: simd128 compile-time verified.
          unsafe {
            arch::wasm_simd128::vuya_to_luma_u16_row(packed, out, width);
          }
          return;
        }
      }
      _ => {}
    }
  }

  scalar::vuya_to_luma_u16_row(packed, out, width);
}

#[cfg(all(test, feature = "std"))]
mod tests {
  //! Smoke tests for the public VUYA dispatchers. Walker / kernel
  //! correctness lives in the per-arch tests and the scalar reference's
  //! own inline tests; this block verifies the dispatcher correctly
  //! reaches its scalar fallback when SIMD is disabled and panics on
  //! invalid inputs.
  use super::*;

  /// Pack one VUYA pixel from explicit V / U / Y / A samples.
  fn pack_vuya(v: u8, u: u8, y: u8, a: u8) -> [u8; 4] {
    [v, u, y, a]
  }

  /// Build a `Vec<u8>` VUYA row of `width` neutral-gray pixels
  /// (V=128, U=128, Y=y_val, A=a_val). Any positive width is valid.
  fn solid_vuya(width: usize, y_val: u8, a_val: u8) -> std::vec::Vec<u8> {
    let quad = pack_vuya(128, 128, y_val, a_val);
    (0..width).flat_map(|_| quad).collect()
  }

  #[test]
  #[should_panic(expected = "packed row too short")]
  fn vuya_dispatcher_rejects_short_packed() {
    // packed buffer has only 2 x 4 = 8 bytes for width = 4 (needs 16).
    let packed = [0u8; 8];
    let mut rgb = [0u8; 4 * 3];
    vuya_to_rgb_row(&packed, &mut rgb, 4, KernelMatrix::Bt709, true, false);
  }

  #[test]
  #[should_panic(expected = "rgb_out row too short")]
  fn vuya_dispatcher_rejects_short_rgb_output() {
    let packed = [0u8; 4 * 4];
    let mut rgb = [0u8; 2];
    vuya_to_rgb_row(&packed, &mut rgb, 4, KernelMatrix::Bt709, true, false);
  }

  #[test]
  #[should_panic(expected = "rgba_out row too short")]
  fn vuya_dispatcher_rejects_short_rgba_output() {
    let packed = [0u8; 4 * 4];
    let mut rgba = [0u8; 2];
    vuya_to_rgba_row(&packed, &mut rgba, 4, KernelMatrix::Bt709, true, false);
  }

  #[test]
  #[should_panic(expected = "luma_out row too short")]
  fn vuya_dispatcher_rejects_short_luma_output() {
    let packed = [0u8; 4 * 4];
    let mut luma = [0u8; 2];
    vuya_to_luma_row(&packed, &mut luma, 4, false);
  }

  #[test]
  fn vuya_dispatchers_route_with_simd_false() {
    // Full-range gray: Y=128, U=V=128. With full_range=true and neutral
    // chroma the output should be close to 128 on every RGB channel.
    let buf = solid_vuya(8, 128, 0xAB);

    // RGB — full-range gray ≈ 128
    let mut rgb = [0u8; 8 * 3];
    vuya_to_rgb_row(&buf, &mut rgb, 8, KernelMatrix::Bt709, true, false);
    for px in rgb.chunks(3) {
      assert!(px[0].abs_diff(128) <= 2, "R channel off: {}", px[0]);
      assert_eq!(px[0], px[1], "R ≠ G");
      assert_eq!(px[1], px[2], "G ≠ B");
    }

    // RGBA — source alpha byte 0xAB must pass through verbatim (VUYA).
    let mut rgba = [0u8; 8 * 4];
    vuya_to_rgba_row(&buf, &mut rgba, 8, KernelMatrix::Bt709, true, false);
    for px in rgba.chunks(4) {
      assert!(px[0].abs_diff(128) <= 2, "R channel off: {}", px[0]);
      assert_eq!(px[3], 0xAB, "alpha must be source value for VUYA");
    }

    // Luma — Y=128 must appear directly.
    let mut luma = [0u8; 8];
    vuya_to_luma_row(&buf, &mut luma, 8, false);
    for &y in &luma {
      assert_eq!(y, 128u8, "luma must equal source Y byte");
    }
  }

  // ---- 32-bit width x 4 overflow guard ------------------------------------

  #[cfg(target_pointer_width = "32")]
  const OVERFLOW_WIDTH_TIMES_4: usize = (usize::MAX / 4) + 1;

  #[cfg(target_pointer_width = "32")]
  #[test]
  #[should_panic(expected = "overflows usize")]
  fn vuya_dispatcher_rejects_width_times_4_overflow() {
    let p: [u8; 0] = [];
    let mut rgb: [u8; 0] = [];
    vuya_to_rgb_row(
      &p,
      &mut rgb,
      OVERFLOW_WIDTH_TIMES_4,
      KernelMatrix::Bt709,
      true,
      false,
    );
  }
}