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
//! wasm-simd128 kernels for 32-bit packed RGB / RGBA sources (Rgb96 / Rgba128).
//!
//! ## Format layouts
//!
//! | Format  | Elements per pixel | Channel order in memory |
//! |---------|--------------------|------------------------|
//! | Rgb96   | 3 u32              | R, G, B                |
//! | Rgba128 | 4 u32              | R, G, B, A             |
//!
//! ## Per-format SIMD strategy (8 pixels per SIMD iteration via v128)
//!
//! Each 4-pixel sub-group is loaded as three (Rgb96) / four (Rgba128) `v128`
//! registers (4 u32 lanes each), byte-swapped per u32 lane when `BE = true`,
//! and deinterleaved into per-channel `u32x4` lane vectors with `u8x16_swizzle`
//! (the same byte-gather masks as the SSE4.1 sibling). Two sub-groups feed the
//! shared [`super::write_rgb_16`] / [`super::write_rgba_16`] /
//! [`super::write_rgb_u16_8`] / [`super::write_rgba_u16_8`] writers.
//!
//! ## Depth conversion
//!
//! - **u32 → u8:**  `u32x4_shr(v, 24)` + `u16x8_narrow_i32x4` + `u8x16_narrow_i16x8`
//!   (net `>> 24`, matching scalar `(v >> 24) as u8`).
//! - **u32 → u16:** `u32x4_shr(v, 16)` + `u16x8_narrow_i32x4` (`>> 16`, matching
//!   scalar `(v >> 16) as u16`). Values fit their target width after the shift,
//!   so the saturating narrows never clamp.
//!
//! ## Scalar tail
//!
//! All kernels handle `width % 8` remaining pixels via the scalar reference.
// Kernels are wired into the dispatcher in the dispatch-wiring step; suppress
// dead_code until then.
#![allow(dead_code)]

use core::arch::wasm32::*;

use super::*;

// ---- endian byte-swap helper ------------------------------------------------

/// Compile-time host endianness. `true` on BE targets, `false` on LE.
const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");

/// Conditionally byte-swap every u32 lane in `v` into host-native order. The
/// gate is `BE != HOST_NATIVE_BE`; wasm32 is always little-endian, so the swap
/// fires exactly when `BE = true`.
#[inline(always)]
unsafe fn byteswap32_if_be<const BE: bool>(v: v128) -> v128 {
  if BE != HOST_NATIVE_BE {
    u8x16_swizzle(
      v,
      i8x16(3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12),
    )
  } else {
    v
  }
}

/// Deinterleave 4 pixels of stride-3 u32 (Rgb96) into `(R, G, B)` `u32x4`
/// channel lane vectors. See the SSE4.1 sibling for the mask derivation.
///
/// # Safety
///
/// Caller must hold the `simd128` target_feature.
#[inline(always)]
unsafe fn deinterleave_rgb96_4px(v0: v128, v1: v128, v2: v128) -> (v128, v128, v128) {
  let r_v0 = i8x16(0, 1, 2, 3, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1);
  let r_v1 = i8x16(-1, -1, -1, -1, -1, -1, -1, -1, 8, 9, 10, 11, -1, -1, -1, -1);
  let r_v2 = i8x16(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 4, 5, 6, 7);
  let r = v128_or(
    v128_or(u8x16_swizzle(v0, r_v0), u8x16_swizzle(v1, r_v1)),
    u8x16_swizzle(v2, r_v2),
  );

  let g_v0 = i8x16(4, 5, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  let g_v1 = i8x16(-1, -1, -1, -1, 0, 1, 2, 3, 12, 13, 14, 15, -1, -1, -1, -1);
  let g_v2 = i8x16(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 8, 9, 10, 11);
  let g = v128_or(
    v128_or(u8x16_swizzle(v0, g_v0), u8x16_swizzle(v1, g_v1)),
    u8x16_swizzle(v2, g_v2),
  );

  let b_v0 = i8x16(8, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  let b_v1 = i8x16(-1, -1, -1, -1, 4, 5, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1);
  let b_v2 = i8x16(-1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 12, 13, 14, 15);
  let b = v128_or(
    v128_or(u8x16_swizzle(v0, b_v0), u8x16_swizzle(v1, b_v1)),
    u8x16_swizzle(v2, b_v2),
  );

  (r, g, b)
}

/// Loads, byte-swaps, and deinterleaves 4 pixels of Rgb96 into `u32x4` planes.
///
/// # Safety
///
/// `ptr` must point to at least 12 readable u32; `simd128` must be held.
#[inline(always)]
unsafe fn load_deint_rgb96_4px<const BE: bool>(ptr: *const u32) -> (v128, v128, v128) {
  unsafe {
    let v0 = byteswap32_if_be::<BE>(v128_load(ptr.cast()));
    let v1 = byteswap32_if_be::<BE>(v128_load(ptr.add(4).cast()));
    let v2 = byteswap32_if_be::<BE>(v128_load(ptr.add(8).cast()));
    deinterleave_rgb96_4px(v0, v1, v2)
  }
}

/// Narrows two `u32x4` planes (`>> 16` applied) into a `u16x8` channel vector.
#[inline(always)]
unsafe fn narrow_pair_u32_to_u16x8(lo: v128, hi: v128) -> v128 {
  u16x8_narrow_i32x4(u32x4_shr(lo, 16), u32x4_shr(hi, 16))
}

/// Narrows two `u32x4` planes (`>> 24` applied) into a `u8x16` channel vector
/// whose low 8 bytes carry the 8 valid samples.
#[inline(always)]
unsafe fn narrow_pair_u32_to_u8x8(lo: v128, hi: v128) -> v128 {
  let u16 = u16x8_narrow_i32x4(u32x4_shr(lo, 24), u32x4_shr(hi, 24));
  u8x16_narrow_i16x8(u16, u16x8_splat(0))
}

// Rgb96 (R, G, B — 3 u32 elements per pixel).

/// wasm Rgb96 → packed u8 RGB. 8 pixels per SIMD iteration.
///
/// # Safety
///
/// 1. The `simd128` target_feature must be available.
/// 2. `rgb96.len() >= width * 3`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_rgb96_to_rgb_row<const BE: bool>(
  rgb96: &[u32],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgb96.len() >= width * 3, "rgb96 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let p = rgb96.as_ptr().add(x * 3);
      let (r0, g0, b0) = load_deint_rgb96_4px::<BE>(p);
      let (r1, g1, b1) = load_deint_rgb96_4px::<BE>(p.add(12));
      let r = narrow_pair_u32_to_u8x8(r0, r1);
      let g = narrow_pair_u32_to_u8x8(g0, g1);
      let b = narrow_pair_u32_to_u8x8(b0, b1);
      // write_rgb_16 writes 16 px (48 bytes); only first 8 px (24 bytes) valid.
      let mut tmp = [0u8; 48];
      write_rgb_16(r, g, b, tmp.as_mut_ptr());
      core::ptr::copy_nonoverlapping(tmp.as_ptr(), rgb_out.as_mut_ptr().add(x * 3), 24);
      x += 8;
    }
    if x < width {
      scalar::rgb96_to_rgb_row::<BE>(&rgb96[x * 3..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// wasm Rgb96 → packed u8 RGBA. 8 pixels per SIMD iteration. Alpha forced to 0xFF.
///
/// # Safety
///
/// 1. The `simd128` target_feature must be available.
/// 2. `rgb96.len() >= width * 3`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_rgb96_to_rgba_row<const BE: bool>(
  rgb96: &[u32],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgb96.len() >= width * 3, "rgb96 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let opaque = u8x16_splat(0xFF);
    let mut x = 0usize;
    while x + 8 <= width {
      let p = rgb96.as_ptr().add(x * 3);
      let (r0, g0, b0) = load_deint_rgb96_4px::<BE>(p);
      let (r1, g1, b1) = load_deint_rgb96_4px::<BE>(p.add(12));
      let r = narrow_pair_u32_to_u8x8(r0, r1);
      let g = narrow_pair_u32_to_u8x8(g0, g1);
      let b = narrow_pair_u32_to_u8x8(b0, b1);
      let mut tmp = [0u8; 64];
      write_rgba_16(r, g, b, opaque, tmp.as_mut_ptr());
      core::ptr::copy_nonoverlapping(tmp.as_ptr(), rgba_out.as_mut_ptr().add(x * 4), 32);
      x += 8;
    }
    if x < width {
      scalar::rgb96_to_rgba_row::<BE>(&rgb96[x * 3..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

/// wasm Rgb96 → native-depth u16 RGB. 8 pixels per SIMD iteration.
///
/// # Safety
///
/// 1. The `simd128` target_feature must be available.
/// 2. `rgb96.len() >= width * 3`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_rgb96_to_rgb_u16_row<const BE: bool>(
  rgb96: &[u32],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgb96.len() >= width * 3, "rgb96 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let p = rgb96.as_ptr().add(x * 3);
      let (r0, g0, b0) = load_deint_rgb96_4px::<BE>(p);
      let (r1, g1, b1) = load_deint_rgb96_4px::<BE>(p.add(12));
      let r = narrow_pair_u32_to_u16x8(r0, r1);
      let g = narrow_pair_u32_to_u16x8(g0, g1);
      let b = narrow_pair_u32_to_u16x8(b0, b1);
      write_rgb_u16_8(r, g, b, rgb_out.as_mut_ptr().add(x * 3));
      x += 8;
    }
    if x < width {
      scalar::rgb96_to_rgb_u16_row::<BE>(&rgb96[x * 3..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// wasm Rgb96 → native-depth u16 RGBA. 8 pixels per SIMD iteration. Alpha 0xFFFF.
///
/// # Safety
///
/// 1. The `simd128` target_feature must be available.
/// 2. `rgb96.len() >= width * 3`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_rgb96_to_rgba_u16_row<const BE: bool>(
  rgb96: &[u32],
  rgba_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgb96.len() >= width * 3, "rgb96 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let opaque = u16x8_splat(0xFFFF);
    let mut x = 0usize;
    while x + 8 <= width {
      let p = rgb96.as_ptr().add(x * 3);
      let (r0, g0, b0) = load_deint_rgb96_4px::<BE>(p);
      let (r1, g1, b1) = load_deint_rgb96_4px::<BE>(p.add(12));
      let r = narrow_pair_u32_to_u16x8(r0, r1);
      let g = narrow_pair_u32_to_u16x8(g0, g1);
      let b = narrow_pair_u32_to_u16x8(b0, b1);
      write_rgba_u16_8(r, g, b, opaque, rgba_out.as_mut_ptr().add(x * 4));
      x += 8;
    }
    if x < width {
      scalar::rgb96_to_rgba_u16_row::<BE>(&rgb96[x * 3..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

// ---- Rgba128 deinterleave (4 u32 per pixel, 4 loads per 4 px) ---------------

/// Deinterleave 4 pixels of stride-4 u32 (Rgba128) into `(R, G, B, A)` `u32x4`
/// lane vectors via the compile-time `i32x4_shuffle` 4x4 transpose ladder
/// (the wasm analogue of the SSE `unpacklo/hi` ladder).
///
/// # Safety
///
/// Caller must hold the `simd128` target_feature.
#[inline(always)]
unsafe fn deinterleave_rgba128_4px(
  v0: v128,
  v1: v128,
  v2: v128,
  v3: v128,
) -> (v128, v128, v128, v128) {
  // v_n = [Rn, Gn, Bn, An].
  let t0 = i32x4_shuffle::<0, 4, 1, 5>(v0, v1); // [R0, R1, G0, G1]
  let t1 = i32x4_shuffle::<2, 6, 3, 7>(v0, v1); // [B0, B1, A0, A1]
  let t2 = i32x4_shuffle::<0, 4, 1, 5>(v2, v3); // [R2, R3, G2, G3]
  let t3 = i32x4_shuffle::<2, 6, 3, 7>(v2, v3); // [B2, B3, A2, A3]
  let r = i32x4_shuffle::<0, 1, 4, 5>(t0, t2); // [R0, R1, R2, R3]
  let g = i32x4_shuffle::<2, 3, 6, 7>(t0, t2); // [G0, G1, G2, G3]
  let b = i32x4_shuffle::<0, 1, 4, 5>(t1, t3); // [B0, B1, B2, B3]
  let a = i32x4_shuffle::<2, 3, 6, 7>(t1, t3); // [A0, A1, A2, A3]
  (r, g, b, a)
}

/// Loads, byte-swaps, and deinterleaves 4 pixels of Rgba128 into `u32x4` planes.
///
/// # Safety
///
/// `ptr` must point to at least 16 readable u32; `simd128` must be held.
#[inline(always)]
unsafe fn load_deint_rgba128_4px<const BE: bool>(ptr: *const u32) -> (v128, v128, v128, v128) {
  unsafe {
    let v0 = byteswap32_if_be::<BE>(v128_load(ptr.cast()));
    let v1 = byteswap32_if_be::<BE>(v128_load(ptr.add(4).cast()));
    let v2 = byteswap32_if_be::<BE>(v128_load(ptr.add(8).cast()));
    let v3 = byteswap32_if_be::<BE>(v128_load(ptr.add(12).cast()));
    deinterleave_rgba128_4px(v0, v1, v2, v3)
  }
}

// Rgba128 (R, G, B, A — 4 u32 elements per pixel).

/// wasm Rgba128 → packed u8 RGB. 8 pixels per SIMD iteration. Alpha discarded.
///
/// # Safety
///
/// 1. The `simd128` target_feature must be available.
/// 2. `rgba128.len() >= width * 4`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_rgba128_to_rgb_row<const BE: bool>(
  rgba128: &[u32],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgba128.len() >= width * 4, "rgba128 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let p = rgba128.as_ptr().add(x * 4);
      let (r0, g0, b0, _) = load_deint_rgba128_4px::<BE>(p);
      let (r1, g1, b1, _) = load_deint_rgba128_4px::<BE>(p.add(16));
      let r = narrow_pair_u32_to_u8x8(r0, r1);
      let g = narrow_pair_u32_to_u8x8(g0, g1);
      let b = narrow_pair_u32_to_u8x8(b0, b1);
      let mut tmp = [0u8; 48];
      write_rgb_16(r, g, b, tmp.as_mut_ptr());
      core::ptr::copy_nonoverlapping(tmp.as_ptr(), rgb_out.as_mut_ptr().add(x * 3), 24);
      x += 8;
    }
    if x < width {
      scalar::rgba128_to_rgb_row::<BE>(&rgba128[x * 4..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// wasm Rgba128 → packed u8 RGBA. 8 pixels per SIMD iteration. Alpha passed through.
///
/// # Safety
///
/// 1. The `simd128` target_feature must be available.
/// 2. `rgba128.len() >= width * 4`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_rgba128_to_rgba_row<const BE: bool>(
  rgba128: &[u32],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgba128.len() >= width * 4, "rgba128 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let p = rgba128.as_ptr().add(x * 4);
      let (r0, g0, b0, a0) = load_deint_rgba128_4px::<BE>(p);
      let (r1, g1, b1, a1) = load_deint_rgba128_4px::<BE>(p.add(16));
      let r = narrow_pair_u32_to_u8x8(r0, r1);
      let g = narrow_pair_u32_to_u8x8(g0, g1);
      let b = narrow_pair_u32_to_u8x8(b0, b1);
      let a = narrow_pair_u32_to_u8x8(a0, a1);
      let mut tmp = [0u8; 64];
      write_rgba_16(r, g, b, a, tmp.as_mut_ptr());
      core::ptr::copy_nonoverlapping(tmp.as_ptr(), rgba_out.as_mut_ptr().add(x * 4), 32);
      x += 8;
    }
    if x < width {
      scalar::rgba128_to_rgba_row::<BE>(&rgba128[x * 4..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

/// wasm Rgba128 → native-depth u16 RGB. 8 pixels per SIMD iteration. Alpha discarded.
///
/// # Safety
///
/// 1. The `simd128` target_feature must be available.
/// 2. `rgba128.len() >= width * 4`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_rgba128_to_rgb_u16_row<const BE: bool>(
  rgba128: &[u32],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgba128.len() >= width * 4, "rgba128 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let p = rgba128.as_ptr().add(x * 4);
      let (r0, g0, b0, _) = load_deint_rgba128_4px::<BE>(p);
      let (r1, g1, b1, _) = load_deint_rgba128_4px::<BE>(p.add(16));
      let r = narrow_pair_u32_to_u16x8(r0, r1);
      let g = narrow_pair_u32_to_u16x8(g0, g1);
      let b = narrow_pair_u32_to_u16x8(b0, b1);
      write_rgb_u16_8(r, g, b, rgb_out.as_mut_ptr().add(x * 3));
      x += 8;
    }
    if x < width {
      scalar::rgba128_to_rgb_u16_row::<BE>(&rgba128[x * 4..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// wasm Rgba128 → native-depth u16 RGBA. 8 pixels per SIMD iteration. Alpha passed through.
///
/// # Safety
///
/// 1. The `simd128` target_feature must be available.
/// 2. `rgba128.len() >= width * 4`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_rgba128_to_rgba_u16_row<const BE: bool>(
  rgba128: &[u32],
  rgba_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgba128.len() >= width * 4, "rgba128 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let p = rgba128.as_ptr().add(x * 4);
      let (r0, g0, b0, a0) = load_deint_rgba128_4px::<BE>(p);
      let (r1, g1, b1, a1) = load_deint_rgba128_4px::<BE>(p.add(16));
      let r = narrow_pair_u32_to_u16x8(r0, r1);
      let g = narrow_pair_u32_to_u16x8(g0, g1);
      let b = narrow_pair_u32_to_u16x8(b0, b1);
      let a = narrow_pair_u32_to_u16x8(a0, a1);
      write_rgba_u16_8(r, g, b, a, rgba_out.as_mut_ptr().add(x * 4));
      x += 8;
    }
    if x < width {
      scalar::rgba128_to_rgba_u16_row::<BE>(&rgba128[x * 4..], &mut rgba_out[x * 4..], width - x);
    }
  }
}