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
//! Fused-downscale coverage for the 8-bit **packed 4:4:4** YUV NATIVE fast tier
//! (issue #123) — [`Vuyx`](crate::source::Vuyx) (bytes `V U Y X` per pixel,
//! `X` padding). The 4:4:4 analog of the packed 4:2:2 native tier
//! (`Yuyv422` / `Uyvy422` / `Yvyu422`).
//!
//! The wrapper de-PACKS each fully-interleaved source row into separate
//! Y (`w`) / U (`w`) / V (`w`) scratch planes — all FULL width, since 4:4:4 has
//! no chroma subsampling — at the per-format byte offsets, then reuses the
//! planar twin's non-4:2:0 join
//! ([`yuv_planar_process_native`](crate::sinker::mixed::planar_8bit::yuv_planar_process_native))
//! at [`Yuv444p`](crate::source::Yuv444p) geometry (`chroma_vsub = 1`,
//! `chroma_w = w`). The native tier bins the planes to the output grid and
//! converts ONCE per output row at output width, vs the packed row-stage tier
//! ([`packed_yuv444_triple_resample`](crate::sinker::mixed::packed_yuv444_triple_resample)),
//! which converts each source row at source width then bins in RGB. The tiers
//! differ in colour SEMANTICS (native averages in YUV then converts; row-stage
//! converts then averages in RGB), so native is NOT byte-identical to row-stage
//! — only within a small tolerance in-gamut. Luma is bit-identical (both bin the
//! same native Y).
//!
//! The suite asserts (the #227 bar, re-pointed at the packed 4:4:4 de-pack):
//! - (a) the strongest check — packed native is BYTE-IDENTICAL to a `Yuv444p`
//!   NATIVE conversion of the de-packed planes (the de-pack-then-reuse claim; a
//!   wrong byte offset would diverge). This is the twin-parity oracle.
//! - (b) native vs the packed ROW-STAGE tier (the cv2 INTER_AREA oracle): luma
//!   bit-identical, in-gamut colour within `TOL_U8`, swept over matrices and the
//!   range flag.
//! - (c) constant planes bin exactly on both grids, so native reproduces the
//!   full-resolution direct conversion EXACTLY (the true 0-LSB case).
//! - the luma-only lazy-chroma carry-through (no chroma planning / alloc), the
//!   default-on flag, and the native/row-stage route-freeze (#186) contracts.

use crate::{
  KernelMatrix, PixelSink,
  resample::AreaResampler,
  sinker::{MixedSinker, MixedSinkerError},
  source::{Vuyx, VuyxRow, Yuv444p, vuyx_to, yuv444p_to},
};
use mediaframe::frame::{VuyxFrame, Yuv444pFrame};

/// In-gamut per-channel tolerance between the native and packed row-stage tiers.
/// The two average in different domains (YUV vs RGB) and round independently per
/// output pixel; matches the packed 4:2:2 twin's bound. Native correctness itself
/// is pinned EXACTLY by the byte-identical `Yuv444p`-native twin-parity oracle
/// below; this bound only documents the row-stage semantic gap.
const TOL_U8: u8 = 5;

/// Per-pixel `(Y, U, V)` ramp (`w x h` each — 4:4:4, every pixel its own
/// chroma): a wide-swinging Y over chroma ramps. Used by the byte-exact
/// twin-parity oracle (a pure de-pack-then-reuse identity, valid at any code)
/// and the constant-plane / route tests — NOT by the row-stage tolerance
/// comparison, which needs a gamut-interior fixture (see [`textured`]).
fn yuv_ramp(w: usize, h: usize) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
  let y: Vec<u8> = (0..w * h)
    .map(|i| 40u8.wrapping_add((i as u8).wrapping_mul(2)))
    .collect();
  let u: Vec<u8> = (0..w * h).map(|i| 110 + (i % 24) as u8).collect();
  let v: Vec<u8> = (0..w * h).map(|i| 120 + (i % 24) as u8).collect();
  (y, u, v)
}

/// A gamut-INTERIOR `(Y, U, V)` ramp (`w x h` each): every code stays well inside
/// the limited-range gamut, so the native (average-in-YUV) and row-stage
/// (convert-then-average) tiers diverge only by per-pixel rounding, not by an RGB
/// clamp.
fn textured(w: usize, h: usize) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
  let y: Vec<u8> = (0..w * h).map(|i| 60 + (i % 64) as u8).collect();
  let u: Vec<u8> = (0..w * h).map(|i| 110 + (i % 24) as u8).collect();
  let v: Vec<u8> = (0..w * h).map(|i| 120 + (i % 24) as u8).collect();
  (y, u, v)
}

/// Builds a Vuyx packed plane (`V U Y X` per pixel; `X` padding = 0). 4:4:4 —
/// every pixel carries its own V / U / Y. `w x 4` bytes per row.
fn vuyx_from(y: &[u8], u: &[u8], v: &[u8], w: usize, h: usize) -> Vec<u8> {
  let mut buf = vec![0u8; 4 * w * h];
  for i in 0..w * h {
    let base = i * 4;
    buf[base] = v[i]; // V
    buf[base + 1] = u[i]; // U
    buf[base + 2] = y[i]; // Y
    buf[base + 3] = 0; // X padding
  }
  buf
}

/// `(rgb, rgba, luma, luma_u16, hsv_h, hsv_s, hsv_v)` of one downscale.
type Outs = (
  Vec<u8>,
  Vec<u8>,
  Vec<u8>,
  Vec<u16>,
  Vec<u8>,
  Vec<u8>,
  Vec<u8>,
);

/// All-outputs `Yuv444p` downscale of the de-packed planes (the byte-exact twin
/// reference). Only RGB + luma are oracled against, but every output is produced
/// so the helper doubles as the colour/luma source.
#[allow(clippy::too_many_arguments)]
fn run_yuv444p(
  y: &[u8],
  u: &[u8],
  v: &[u8],
  w: usize,
  h: usize,
  ow: usize,
  oh: usize,
  full_range: bool,
  matrix: KernelMatrix,
  native: bool,
) -> (Vec<u8>, Vec<u8>) {
  let n = ow * oh;
  let mut rgb = vec![0u8; n * 3];
  let mut luma = vec![0u8; n];
  {
    let frame = Yuv444pFrame::new(y, u, v, w as u32, h as u32, w as u32, w as u32, w as u32);
    let mut sink =
      MixedSinker::<Yuv444p, AreaResampler>::with_resampler(w, h, AreaResampler::to(ow, oh))
        .unwrap()
        .with_native(native)
        .with_rgb(&mut rgb)
        .unwrap()
        .with_luma(&mut luma)
        .unwrap();
    yuv444p_to(&frame, full_range, sink.set_kernel_matrix(matrix)).unwrap();
  }
  (rgb, luma)
}

/// One all-outputs native-or-row-stage downscale of the packed Vuyx frame.
#[allow(clippy::too_many_arguments)]
fn run(
  packed: &[u8],
  w: usize,
  h: usize,
  ow: usize,
  oh: usize,
  full_range: bool,
  matrix: KernelMatrix,
  native: bool,
) -> Outs {
  let n = ow * oh;
  let mut rgb = vec![0u8; n * 3];
  let mut rgba = vec![0u8; n * 4];
  let mut luma = vec![0u8; n];
  let mut luma_u16 = vec![0u16; n];
  let (mut hh, mut ss, mut vv) = (vec![0u8; n], vec![0u8; n], vec![0u8; n]);
  {
    let frame = VuyxFrame::try_new(packed, w as u32, h as u32, (4 * w) as u32).unwrap();
    let mut sink =
      MixedSinker::<Vuyx, AreaResampler>::with_resampler(w, h, AreaResampler::to(ow, oh))
        .unwrap()
        .with_native(native)
        .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 hh, &mut ss, &mut vv)
        .unwrap();
    vuyx_to(&frame, full_range, sink.set_kernel_matrix(matrix)).unwrap();
  }
  (rgb, rgba, luma, luma_u16, hh, ss, vv)
}

/// (a) The strongest check: packed native is byte-identical to a `Yuv444p`
/// NATIVE conversion of the de-packed planes, for RGB and luma, at several
/// geometries. A wrong de-pack byte offset diverges.
#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_equals_yuv444p_native_on_depacked_planes() {
  for (w, h, ow, oh) in [
    (8usize, 8usize, 4usize, 4usize),
    (12, 10, 5, 4),
    (12, 10, 7, 6),
    (8, 10, 3, 4),
  ] {
    let (y, u, v) = yuv_ramp(w, h);
    let packed = vuyx_from(&y, &u, &v, w, h);

    let nv = run(&packed, w, h, ow, oh, true, KernelMatrix::Bt601, true);
    let (p_rgb, p_luma) = run_yuv444p(&y, &u, &v, w, h, ow, oh, true, KernelMatrix::Bt601, true);

    assert_eq!(
      nv.0, p_rgb,
      "packed native rgb == yuv444p native rgb ({w}x{h}->{ow}x{oh})"
    );
    assert_eq!(
      nv.2, p_luma,
      "packed native luma == yuv444p native luma ({w}x{h}->{ow}x{oh})"
    );
    // RGBA colour mirrors RGB with an opaque-alpha pad.
    for (px, rgb_px) in nv.1.chunks_exact(4).zip(p_rgb.chunks_exact(3)) {
      assert_eq!(&px[..3], rgb_px, "native rgba colour == rgb");
      assert_eq!(px[3], 0xFF, "native rgba alpha opaque");
    }
    // luma_u16 is the binned Y zero-extended.
    let lu16: Vec<u16> = p_luma.iter().map(|&b| b as u16).collect();
    assert_eq!(nv.3, lu16, "native luma_u16 == binned Y zero-extended");
  }
}

/// (b) Native vs the packed ROW-STAGE tier (the cv2 INTER_AREA oracle): luma
/// bit-identical (both bin the same native Y), in-gamut colour within `TOL_U8`.
/// The `with_native(true)` vs `with_native(false)` differential, swept over
/// matrices and the range flag.
#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_within_tolerance_of_row_stage() {
  let (w, h) = (12, 10);
  let (y, u, v) = textured(w, h);
  let packed = vuyx_from(&y, &u, &v, w, h);
  for (ow, oh) in [(6, 5), (4, 4), (7, 6), (5, 3)] {
    for full_range in [false, true] {
      for matrix in [
        KernelMatrix::Bt601,
        KernelMatrix::Bt709,
        KernelMatrix::Bt2020Ncl,
      ] {
        let native = run(&packed, w, h, ow, oh, full_range, matrix, true);
        let row = run(&packed, w, h, ow, oh, full_range, matrix, false);
        assert_eq!(
          native.2, row.2,
          "luma bit-identical {ow}x{oh} fr={full_range} {matrix:?}"
        );
        assert_eq!(
          native.3, row.3,
          "luma_u16 bit-identical {ow}x{oh} fr={full_range} {matrix:?}"
        );
        for (name, a, b) in [("rgb", &native.0, &row.0), ("rgba", &native.1, &row.1)] {
          for (i, (x, y)) in a.iter().zip(b.iter()).enumerate() {
            assert!(
              x.abs_diff(*y) <= TOL_U8,
              "{name} {ow}x{oh} fr={full_range} {matrix:?} idx {i}: native {x} vs row {y}"
            );
          }
        }
      }
    }
  }
}

/// (c) Constant planes bin exactly on both grids, so native reproduces the
/// full-resolution direct conversion EXACTLY — the true 0-LSB case.
#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_solid_frame_exact() {
  let (w, h) = (8, 8);
  let y = vec![120u8; w * h];
  let u = vec![90u8; w * h];
  let v = vec![170u8; w * h];
  let packed = vuyx_from(&y, &u, &v, w, h);

  // Full-resolution direct conversion (identity sink).
  let mut full_rgb = vec![0u8; w * h * 3];
  {
    let frame = VuyxFrame::try_new(&packed, w as u32, h as u32, (4 * w) as u32).unwrap();
    let mut sink = MixedSinker::<Vuyx>::new(w, h)
      .with_rgb(&mut full_rgb)
      .unwrap();
    vuyx_to(&frame, false, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  }
  let out = run(&packed, w, h, 4, 4, false, KernelMatrix::Bt709, true);
  for px in out.0.chunks_exact(3) {
    assert_eq!(
      (px[0], px[1], px[2]),
      (full_rgb[0], full_rgb[1], full_rgb[2]),
      "native solid rgb == full-res conversion"
    );
  }
  assert!(out.2.iter().all(|&l| l == 120), "native solid luma == Y");
}

/// `with_native(true)` is the builder default for this format.
#[test]
fn native_is_default_on() {
  let sink =
    MixedSinker::<Vuyx, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4)).unwrap();
  assert!(sink.native(), "with_native must default to true");
  assert!(
    !sink.with_native(false).native(),
    "with_native(false) must disable the tier"
  );
}

/// A mid-frame `set_native` flip splits one frame across two independent stream
/// machines and must reject as the deterministic `NativeRouteChanged` (the #186
/// CHECK-before / SET-after guard).
#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_to_rowstage_route_flip_mid_frame_rejected() {
  let (w, h) = (8, 8);
  let (y, u, v) = yuv_ramp(w, h);
  let packed = vuyx_from(&y, &u, &v, w, h);
  let mut luma = vec![0u8; 4 * 4];
  let mut sink = MixedSinker::<Vuyx, AreaResampler>::with_resampler(w, h, AreaResampler::to(4, 4))
    .unwrap()
    .with_native(true)
    .with_luma(&mut luma)
    .unwrap();
  sink.begin_frame(w as u32, h as u32).unwrap();
  // Row 0 freezes the route = native.
  sink
    .process(VuyxRow::for_tests(
      &packed[0..4 * w],
      0,
      KernelMatrix::Bt601,
      true,
    ))
    .expect("native row 0 freezes the route and succeeds");
  sink.set_native(false);
  let err = sink
    .process(VuyxRow::for_tests(
      &packed[4 * w..8 * w],
      1,
      KernelMatrix::Bt601,
      true,
    ))
    .unwrap_err();
  assert!(
    matches!(err, MixedSinkerError::NativeRouteChanged(_)),
    "a native -> row-stage mid-frame route flip must reject as \
     NativeRouteChanged, got {err:?}"
  );
}

/// Native survives a frame restart on a reused sink: `begin_frame` resets the
/// join + the frozen route, so a second frame (the OTHER tier) downscales its
/// own planes correctly.
#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_reuses_join_and_resets_route_across_frames() {
  let (w, h) = (8, 8);
  let (y, u, v) = yuv_ramp(w, h);
  let packed = vuyx_from(&y, &u, &v, w, h);
  let mut luma = vec![0u8; 4 * 4];
  let mut sink = MixedSinker::<Vuyx, AreaResampler>::with_resampler(w, h, AreaResampler::to(4, 4))
    .unwrap()
    .with_native(true)
    .with_luma(&mut luma)
    .unwrap();
  let frame = VuyxFrame::try_new(&packed, w as u32, h as u32, (4 * w) as u32).unwrap();
  // Frame 1: native, route constant across every row — no false reject.
  vuyx_to(&frame, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
  // Frame 2: flip to row-stage for the WHOLE frame; the per-frame reset (in
  // `begin_frame`) cleared the frozen route, so this is allowed.
  sink.set_native(false);
  vuyx_to(&frame, true, sink.set_kernel_matrix(KernelMatrix::Bt601))
    .expect("a new frame may pick the other tier; the route reset per frame");
}

/// A luma-only packed 4:4:4 native sink must NOT plan or allocate any chroma
/// state — else luma-only Vuyx resampling depends on an unused chroma allocation
/// and can fail under memory pressure before producing luma. Armed with the
/// planar-native chroma-planning failpoint (the join is shared with the planar
/// twin): a luma-only row leaves it unconsumed (the run succeeds), while a colour
/// row reaches chroma planning and fires.
#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn luma_only_native_skips_chroma_planning() {
  let (w, h) = (8, 8);
  let (y, u, v) = yuv_ramp(w, h);
  let packed = vuyx_from(&y, &u, &v, w, h);
  let frame = VuyxFrame::try_new(&packed, w as u32, h as u32, (4 * w) as u32).unwrap();

  // 2x2-block area mean of the Y plane — the luma reference.
  let mut y_ref = vec![0u8; 4 * 4];
  for oy in 0..4 {
    for ox in 0..4 {
      let mut s = 0u32;
      for dy in 0..2 {
        for dx in 0..2 {
          s += y[(oy * 2 + dy) * w + ox * 2 + dx] as u32;
        }
      }
      y_ref[oy * 4 + ox] = ((s + 2) / 4) as u8;
    }
  }

  crate::sinker::mixed::arm_planar_native_chroma_failure();

  // Luma-only: the armed chroma failpoint is never reached -> Ok.
  let mut luma = vec![0u8; 4 * 4];
  {
    let mut sink =
      MixedSinker::<Vuyx, AreaResampler>::with_resampler(w, h, AreaResampler::to(4, 4))
        .unwrap()
        .with_native(true)
        .with_luma(&mut luma)
        .unwrap();
    vuyx_to(&frame, true, sink.set_kernel_matrix(KernelMatrix::Bt601))
      .expect("luma-only native must not plan chroma");
  }
  assert_eq!(luma, y_ref, "luma-only native == area-downscaled Y");

  // Colour: the still-armed failpoint fires at chroma planning -> Err. This both
  // proves the failpoint is wired to chroma planning and consumes the arm.
  let mut rgb = vec![0u8; 4 * 4 * 3];
  let mut sink = MixedSinker::<Vuyx, AreaResampler>::with_resampler(w, h, AreaResampler::to(4, 4))
    .unwrap()
    .with_native(true)
    .with_rgb(&mut rgb)
    .unwrap();
  assert!(
    vuyx_to(&frame, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).is_err(),
    "colour native must reach chroma planning (the armed failpoint fires)"
  );
}