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
//! Tests for `crate::row::scalar::grayf16`.

use super::*;
use half::f16;

/// Re-encode a host-native f16 slice as LE-encoded byte storage (each element
/// stored with LE u16-bit byte layout). Kernels called with `BE = false`
/// recover the intended host-native value via `u16::from_le` on both LE
/// (no-op) and BE (byte-swap) hosts.
fn as_le_f16(host: &[f16]) -> std::vec::Vec<f16> {
  host
    .iter()
    .map(|v| f16::from_bits(u16::from_ne_bytes(v.to_bits().to_le_bytes())))
    .collect()
}

/// Mirror of `as_le_f16` for kernels invoked with `BE = true`.
fn as_be_f16(host: &[f16]) -> std::vec::Vec<f16> {
  host
    .iter()
    .map(|v| f16::from_bits(u16::from_ne_bytes(v.to_bits().to_be_bytes())))
    .collect()
}

fn h(v: f32) -> f16 {
  f16::from_f32(v)
}

// ---- grayf16_to_rgb_row --------------------------------------------------

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgb_zero() {
  let plane = as_le_f16(&[h(0.0)]);
  let mut out = [0xFFu8; 3];
  grayf16_to_rgb_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [0, 0, 0]);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgb_max() {
  let plane = as_le_f16(&[h(1.0)]);
  let mut out = [0u8; 3];
  grayf16_to_rgb_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [255, 255, 255]);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgb_mid() {
  // 0.5 is exactly representable in f16. 0.5 * 255 + 0.5 = 128.0 → 128.
  let plane = as_le_f16(&[h(0.5)]);
  let mut out = [0u8; 3];
  grayf16_to_rgb_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [128, 128, 128]);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgb_saturates_high() {
  let plane = as_le_f16(&[h(1.5)]);
  let mut out = [0u8; 3];
  grayf16_to_rgb_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [255, 255, 255]);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgb_saturates_low() {
  let plane = as_le_f16(&[h(-0.1)]);
  let mut out = [0xFFu8; 3];
  grayf16_to_rgb_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [0, 0, 0]);
}

// ---- grayf16_to_rgba_row -------------------------------------------------

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgba_zero_alpha_opaque() {
  let plane = as_le_f16(&[h(0.0)]);
  let mut out = [0u8; 4];
  grayf16_to_rgba_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [0, 0, 0, 0xFF]);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgba_max_alpha_opaque() {
  let plane = as_le_f16(&[h(1.0)]);
  let mut out = [0u8; 4];
  grayf16_to_rgba_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [255, 255, 255, 0xFF]);
}

// ---- grayf16_to_rgb_u16_row ----------------------------------------------

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgb_u16_zero() {
  let plane = as_le_f16(&[h(0.0)]);
  let mut out = [0xFFFFu16; 3];
  grayf16_to_rgb_u16_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [0, 0, 0]);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgb_u16_max() {
  let plane = as_le_f16(&[h(1.0)]);
  let mut out = [0u16; 3];
  grayf16_to_rgb_u16_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [65535, 65535, 65535]);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgb_u16_saturates_high() {
  let plane = as_le_f16(&[h(2.0)]);
  let mut out = [0u16; 3];
  grayf16_to_rgb_u16_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [65535, 65535, 65535]);
}

// ---- grayf16_to_rgba_u16_row ---------------------------------------------

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgba_u16_opaque() {
  let plane = as_le_f16(&[h(1.0)]);
  let mut out = [0u16; 4];
  grayf16_to_rgba_u16_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [65535, 65535, 65535, 0xFFFF]);
}

// ---- grayf16_to_rgb_f32_row ----------------------------------------------

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgb_f32_lossless_replicate() {
  // 1.5 is exactly representable in f16; widening to f32 is exact.
  let plane = as_le_f16(&[h(1.5)]);
  let mut out = [0.0f32; 3];
  grayf16_to_rgb_f32_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [1.5, 1.5, 1.5]);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgb_f32_negative_preserved() {
  let plane = as_le_f16(&[h(-0.5)]);
  let mut out = [0.0f32; 3];
  grayf16_to_rgb_f32_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [-0.5, -0.5, -0.5]);
}

// ---- grayf16_to_luma_row -------------------------------------------------

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_luma_zero() {
  let plane = as_le_f16(&[h(0.0)]);
  let mut out = [0xFFu8; 1];
  grayf16_to_luma_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [0]);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_luma_max() {
  let plane = as_le_f16(&[h(1.0)]);
  let mut out = [0u8; 1];
  grayf16_to_luma_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [255]);
}

// ---- grayf16_to_luma_u16_row ---------------------------------------------

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_luma_u16_max() {
  let plane = as_le_f16(&[h(1.0)]);
  let mut out = [0u16; 1];
  grayf16_to_luma_u16_row::<false>(&plane, &mut out, 1);
  assert_eq!(out, [65535]);
}

// ---- grayf16_to_luma_f32_row ---------------------------------------------

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_luma_f32_identity() {
  let intended = [h(0.0), h(0.5), h(1.0), h(1.5), h(-0.1)];
  let plane = as_le_f16(&intended);
  let mut out = [99.0f32; 5];
  grayf16_to_luma_f32_row::<false>(&plane, &mut out, 5);
  // Lossless widen — each output equals the f16's f32 widening.
  let expected: std::vec::Vec<f32> = intended.iter().map(|v| v.to_f32()).collect();
  assert_eq!(out.as_slice(), expected.as_slice());
}

// ---- grayf16_to_hsv_row --------------------------------------------------

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_hsv_zero() {
  let plane = as_le_f16(&[h(0.0)]);
  let mut hue = [0xFFu8; 1];
  let mut s = [0xFFu8; 1];
  let mut v = [0u8; 1];
  grayf16_to_hsv_row::<false>(&plane, &mut hue, &mut s, &mut v, 1);
  assert_eq!(hue[0], 0, "H must be 0 for achromatic source");
  assert_eq!(s[0], 0, "S must be 0 for achromatic source");
  assert_eq!(v[0], 0);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_hsv_max() {
  let plane = as_le_f16(&[h(1.0)]);
  let mut hue = [0u8; 1];
  let mut s = [0u8; 1];
  let mut v = [0u8; 1];
  grayf16_to_hsv_row::<false>(&plane, &mut hue, &mut s, &mut v, 1);
  assert_eq!(hue[0], 0);
  assert_eq!(s[0], 0);
  assert_eq!(v[0], 255);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_hsv_mid() {
  let plane = as_le_f16(&[h(0.5)]);
  let mut hue = [0u8; 1];
  let mut s = [0u8; 1];
  let mut v = [0u8; 1];
  grayf16_to_hsv_row::<false>(&plane, &mut hue, &mut s, &mut v, 1);
  assert_eq!(hue[0], 0);
  assert_eq!(s[0], 0);
  assert_eq!(v[0], 128);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_hsv_clamps_hdr() {
  let plane = as_le_f16(&[h(2.0)]);
  let mut hue = [0u8; 1];
  let mut s = [0u8; 1];
  let mut v = [0u8; 1];
  grayf16_to_hsv_row::<false>(&plane, &mut hue, &mut s, &mut v, 1);
  assert_eq!(v[0], 255);
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_rgb_multi_pixel() {
  let plane = as_le_f16(&[h(0.0), h(1.0), h(0.5)]);
  let mut out = [0u8; 9];
  grayf16_to_rgb_row::<false>(&plane, &mut out, 3);
  assert_eq!(&out[0..3], &[0, 0, 0]);
  assert_eq!(&out[3..6], &[255, 255, 255]);
  assert_eq!(&out[6..9], &[128, 128, 128]);
}

// ---- BE parity tests: grayf16 ---------------------------------------------
// Build a single host-native `intended` fixture, materialise it as LE-encoded
// and BE-encoded byte storage, run both `<false>` and `<true>` kernels, and
// pin each output against an absolute scalar reference so the parity assertion
// cannot pass on two equally corrupt decodes.

fn ref_grayf16_to_rgb(intended: &[f16], width: usize) -> std::vec::Vec<u8> {
  let mut out = std::vec![0u8; width * 3];
  for (x, &y) in intended[..width].iter().enumerate() {
    let v = f32_to_u8(y.to_f32());
    out[x * 3] = v;
    out[x * 3 + 1] = v;
    out[x * 3 + 2] = v;
  }
  out
}

fn ref_grayf16_to_luma(intended: &[f16], width: usize) -> std::vec::Vec<u8> {
  let mut out = std::vec![0u8; width];
  for (x, &y) in intended[..width].iter().enumerate() {
    out[x] = f32_to_u8(y.to_f32());
  }
  out
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_be_parity_rgb() {
  let intended = [h(0.5)];
  let le = as_le_f16(&intended);
  let be = as_be_f16(&intended);
  let mut out_le = [0u8; 3];
  let mut out_be = [0u8; 3];
  grayf16_to_rgb_row::<false>(&le, &mut out_le, 1);
  grayf16_to_rgb_row::<true>(&be, &mut out_be, 1);
  let expected = ref_grayf16_to_rgb(&intended, 1);
  assert_eq!(out_le.as_slice(), expected, "LE path must match reference");
  assert_eq!(out_be.as_slice(), expected, "BE path must match reference");
  assert_eq!(out_le, out_be, "BE and LE grayf16 rgb outputs must agree");
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_be_parity_luma() {
  let intended = [h(0.25)];
  let le = as_le_f16(&intended);
  let be = as_be_f16(&intended);
  let mut out_le = [0u8; 1];
  let mut out_be = [0u8; 1];
  grayf16_to_luma_row::<false>(&le, &mut out_le, 1);
  grayf16_to_luma_row::<true>(&be, &mut out_be, 1);
  let expected = ref_grayf16_to_luma(&intended, 1);
  assert_eq!(out_le.as_slice(), expected, "LE path must match reference");
  assert_eq!(out_be.as_slice(), expected, "BE path must match reference");
  assert_eq!(out_le, out_be, "BE and LE grayf16 luma outputs must agree");
}

#[cfg_attr(
  miri,
  ignore = "half::f16 uses inline assembly on aarch64 unsupported by Miri"
)]
#[test]
fn grayf16_to_luma_f32_row_be_le_parity_lossless() {
  // Mix of normal, HDR, negative, subnormal, and exact-zero f16 values.
  let intended: std::vec::Vec<f16> = std::vec![
    h(0.25),
    h(1.5),
    h(-0.5),
    f16::from_bits(0x0001),
    h(0.0),
    h(2048.0)
  ];
  let width = intended.len();
  let le = as_le_f16(&intended);
  let be = as_be_f16(&intended);
  let mut out_le = std::vec![0.0f32; width];
  let mut out_be = std::vec![0.0f32; width];
  grayf16_to_luma_f32_row::<false>(&le, &mut out_le, width);
  grayf16_to_luma_f32_row::<true>(&be, &mut out_be, width);
  let expected: std::vec::Vec<f32> = intended.iter().map(|v| v.to_f32()).collect();
  let bits_le: std::vec::Vec<u32> = out_le.iter().map(|v| v.to_bits()).collect();
  let bits_be: std::vec::Vec<u32> = out_be.iter().map(|v| v.to_bits()).collect();
  let bits_expected: std::vec::Vec<u32> = expected.iter().map(|v| v.to_bits()).collect();
  assert_eq!(bits_le, bits_expected, "LE path must match reference");
  assert_eq!(bits_be, bits_expected, "BE path must match reference");
  assert_eq!(bits_le, bits_be, "BE and LE grayf16 luma_f32 must match");
}