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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Fused-downscale coverage for `Yuv410p` / `Yuv440p` — routed through
//! the planar dual-stream resample: **luma / luma_u16 area-resample
//! the Y plane directly** (the YUV luma contract), while RGB / RGBA /
//! HSV bin a converted source-width RGB row. So RGB equals an `Rgb24`
//! resample of the identity-converted frame, and luma equals the
//! area-downscaled Y plane — *not* RGB-derived luma, pinned under
//! saturated chroma where the two diverge.
//!
//! These are the remaining 8-bit planar subsampling variants: 4:1:0
//! (quarter-width *and* quarter-height chroma) and 4:4:0 (full-width,
//! half-height chroma). They route identically to Yuv411p / Yuv444p;
//! only the chroma-subsampling convert kernel differs.

use crate::{
  KernelMatrix, PixelSink,
  resample::{AreaResampler, ResampleError},
  sinker::{MixedSinker, MixedSinkerError},
  source::{Rgb24, Yuv410p, Yuv410pRow, Yuv440p, Yuv440pRow, rgb24_to, yuv410p_to, yuv440p_to},
};
use mediaframe::frame::{Rgb24Frame, Yuv410pFrame, Yuv440pFrame};

const SRC: usize = 8;
const OUT: usize = 4;

/// Exact 2x2-block area mean (round-half-up) of an `SRC`-grid plane to
/// the `OUT` grid — the integer-ratio (2:1) area-downscale reference.
fn block_mean_2x2(plane: &[u8]) -> Vec<u8> {
  let mut out = vec![0u8; OUT * OUT];
  for oy in 0..OUT {
    for ox in 0..OUT {
      let mut s = 0u32;
      for dy in 0..2 {
        for dx in 0..2 {
          s += plane[(oy * 2 + dy) * SRC + ox * 2 + dx] as u32;
        }
      }
      out[oy * OUT + ox] = ((s + 2) / 4) as u8;
    }
  }
  out
}

/// `Rgb24` resample of a full-res converted RGB frame — the colour
/// reference both formats must match.
fn rgb24_rgb_reference(converted: &[u8]) -> Vec<u8> {
  let src = Rgb24Frame::new(converted, SRC as u32, SRC as u32, (SRC * 3) as u32);
  let mut rgb = vec![0u8; OUT * OUT * 3];
  {
    let mut sink =
      MixedSinker::<Rgb24, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_rgb(&mut rgb)
        .unwrap();
    rgb24_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
  }
  rgb
}

// ---- Yuv410p (quarter-width, quarter-height chroma) ----

fn yuv410p_ramp() -> (Vec<u8>, Vec<u8>, Vec<u8>) {
  let cw = SRC / 4;
  let ch = SRC / 4;
  let mut y = vec![0u8; SRC * SRC];
  let mut u = vec![0u8; cw * ch];
  let mut v = vec![0u8; cw * ch];
  for (i, p) in y.iter_mut().enumerate() {
    *p = 40 + (i as u8) * 2;
  }
  for (i, p) in u.iter_mut().enumerate() {
    *p = 70 + ((i % cw) as u8) * 5;
  }
  for (i, p) in v.iter_mut().enumerate() {
    *p = 200 - ((i % cw) as u8) * 4;
  }
  (y, u, v)
}

fn yuv410p_frame<'a>(y: &'a [u8], u: &'a [u8], v: &'a [u8]) -> Yuv410pFrame<'a> {
  let cw = (SRC / 4) as u32;
  Yuv410pFrame::new(y, u, v, SRC as u32, SRC as u32, SRC as u32, cw, cw)
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv410p_resample_rgb_matches_rgb24_of_converted_frame() {
  let (y, u, v) = yuv410p_ramp();
  let mut full_rgb = vec![0u8; SRC * SRC * 3];
  {
    let mut sink = MixedSinker::<Yuv410p>::new(SRC, SRC)
      .with_rgb(&mut full_rgb)
      .unwrap();
    yuv410p_to(
      &yuv410p_frame(&y, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  let mut rgb = vec![0u8; OUT * OUT * 3];
  {
    let mut sink =
      MixedSinker::<Yuv410p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_rgb(&mut rgb)
        .unwrap();
    yuv410p_to(
      &yuv410p_frame(&y, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  assert_eq!(
    rgb,
    rgb24_rgb_reference(&full_rgb),
    "rgb: row-stage == convert-then-bin"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv410p_resample_luma_is_area_downscaled_y_plane() {
  let (y, u, v) = yuv410p_ramp();
  let (mut luma, mut luma_u16) = (vec![0u8; OUT * OUT], vec![0u16; OUT * OUT]);
  {
    let mut sink =
      MixedSinker::<Yuv410p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_luma(&mut luma)
        .unwrap()
        .with_luma_u16(&mut luma_u16)
        .unwrap();
    yuv410p_to(
      &yuv410p_frame(&y, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  let y_ref = block_mean_2x2(&y);
  assert_eq!(luma, y_ref, "luma must be the area-downscaled Y plane");
  let y_ref_u16: Vec<u16> = y_ref.iter().map(|&b| b as u16).collect();
  assert_eq!(
    luma_u16, y_ref_u16,
    "luma_u16 must be the area-downscaled Y, zero-extended"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv410p_resample_luma_from_y_not_rgb_under_saturated_chroma() {
  let cw = SRC / 4;
  let ch = SRC / 4;
  let y = vec![16u8; SRC * SRC];
  let u = vec![240u8; cw * ch];
  let v = vec![16u8; cw * ch];
  let mut luma = vec![0u8; OUT * OUT];
  {
    let mut sink =
      MixedSinker::<Yuv410p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_luma(&mut luma)
        .unwrap();
    yuv410p_to(
      &yuv410p_frame(&y, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  assert!(
    luma.iter().all(|&b| b == 16),
    "luma must be the Y plane (16), not RGB-derived; got {luma:?}"
  );
}

// ---- Yuv440p (full-width, half-height chroma) ----

fn yuv440p_ramp() -> (Vec<u8>, Vec<u8>, Vec<u8>) {
  let ch = SRC / 2;
  let mut y = vec![0u8; SRC * SRC];
  let mut u = vec![0u8; SRC * ch];
  let mut v = vec![0u8; SRC * ch];
  for (i, p) in y.iter_mut().enumerate() {
    *p = 40 + (i as u8) * 2;
  }
  for (i, p) in u.iter_mut().enumerate() {
    *p = 70 + ((i % SRC) as u8) * 5;
  }
  for (i, p) in v.iter_mut().enumerate() {
    *p = 200 - ((i % SRC) as u8) * 3;
  }
  (y, u, v)
}

fn yuv440p_frame<'a>(y: &'a [u8], u: &'a [u8], v: &'a [u8]) -> Yuv440pFrame<'a> {
  Yuv440pFrame::new(
    y, u, v, SRC as u32, SRC as u32, SRC as u32, SRC as u32, SRC as u32,
  )
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv440p_resample_rgb_matches_rgb24_of_converted_frame() {
  let (y, u, v) = yuv440p_ramp();
  let mut full_rgb = vec![0u8; SRC * SRC * 3];
  {
    let mut sink = MixedSinker::<Yuv440p>::new(SRC, SRC)
      .with_rgb(&mut full_rgb)
      .unwrap();
    yuv440p_to(
      &yuv440p_frame(&y, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  let mut rgb = vec![0u8; OUT * OUT * 3];
  {
    // Pin the row-stage tier: this oracle is convert-then-bin (the default
    // tier is now the bin-then-convert native fast tier, covered for parity
    // in `resample_yuv_planar_8bit_native`).
    let mut sink =
      MixedSinker::<Yuv440p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_native(false)
        .with_rgb(&mut rgb)
        .unwrap();
    yuv440p_to(
      &yuv440p_frame(&y, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  assert_eq!(
    rgb,
    rgb24_rgb_reference(&full_rgb),
    "rgb: row-stage == convert-then-bin"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv440p_resample_luma_is_area_downscaled_y_plane() {
  let (y, u, v) = yuv440p_ramp();
  let (mut luma, mut luma_u16) = (vec![0u8; OUT * OUT], vec![0u16; OUT * OUT]);
  {
    let mut sink =
      MixedSinker::<Yuv440p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_luma(&mut luma)
        .unwrap()
        .with_luma_u16(&mut luma_u16)
        .unwrap();
    yuv440p_to(
      &yuv440p_frame(&y, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  let y_ref = block_mean_2x2(&y);
  assert_eq!(luma, y_ref, "luma must be the area-downscaled Y plane");
  let y_ref_u16: Vec<u16> = y_ref.iter().map(|&b| b as u16).collect();
  assert_eq!(
    luma_u16, y_ref_u16,
    "luma_u16 must be the area-downscaled Y, zero-extended"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv440p_resample_luma_from_y_not_rgb_under_saturated_chroma() {
  let ch = SRC / 2;
  let y = vec![16u8; SRC * SRC];
  let u = vec![240u8; SRC * ch];
  let v = vec![16u8; SRC * ch];
  let mut luma = vec![0u8; OUT * OUT];
  {
    let mut sink =
      MixedSinker::<Yuv440p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_luma(&mut luma)
        .unwrap();
    yuv440p_to(
      &yuv440p_frame(&y, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  assert!(
    luma.iter().all(|&b| b == 16),
    "luma must be the Y plane (16), not RGB-derived; got {luma:?}"
  );
}

// ---- Cross-frame stream reset + identity-plan + sequencing ----

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv410p_resample_reuses_luma_stream_across_frames() {
  // Reset coverage: a reused sink must reset the Y-plane luma stream
  // each frame, else frame 2 row 0 is rejected as out-of-sequence.
  let (y1, u, v) = yuv410p_ramp();
  let mut y2 = y1.clone();
  for p in y2.iter_mut() {
    *p = 255 - *p;
  }
  let mut luma = vec![0u8; OUT * OUT];
  {
    let mut sink =
      MixedSinker::<Yuv410p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_luma(&mut luma)
        .unwrap();
    yuv410p_to(
      &yuv410p_frame(&y1, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
    yuv410p_to(
      &yuv410p_frame(&y2, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  assert_eq!(
    luma,
    block_mean_2x2(&y2),
    "frame 2 luma must area-downscale frame 2's Y"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv440p_resample_reuses_luma_stream_across_frames() {
  let (y1, u, v) = yuv440p_ramp();
  let mut y2 = y1.clone();
  for p in y2.iter_mut() {
    *p = 255 - *p;
  }
  let mut luma = vec![0u8; OUT * OUT];
  {
    let mut sink =
      MixedSinker::<Yuv440p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_luma(&mut luma)
        .unwrap();
    yuv440p_to(
      &yuv440p_frame(&y1, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
    yuv440p_to(
      &yuv440p_frame(&y2, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  assert_eq!(
    luma,
    block_mean_2x2(&y2),
    "frame 2 luma must area-downscale frame 2's Y"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv410p_identity_plan_matches_new_sink() {
  let (y, u, v) = yuv410p_ramp();
  let mut direct = vec![0u8; SRC * SRC * 3];
  {
    let mut sink = MixedSinker::<Yuv410p>::new(SRC, SRC)
      .with_rgb(&mut direct)
      .unwrap();
    yuv410p_to(
      &yuv410p_frame(&y, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  let mut via_area = vec![0u8; SRC * SRC * 3];
  {
    let mut sink =
      MixedSinker::<Yuv410p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(SRC, SRC))
        .unwrap()
        .with_rgb(&mut via_area)
        .unwrap();
    yuv410p_to(
      &yuv410p_frame(&y, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  assert_eq!(direct, via_area);
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv440p_identity_plan_matches_new_sink() {
  let (y, u, v) = yuv440p_ramp();
  let mut direct = vec![0u8; SRC * SRC * 3];
  {
    let mut sink = MixedSinker::<Yuv440p>::new(SRC, SRC)
      .with_rgb(&mut direct)
      .unwrap();
    yuv440p_to(
      &yuv440p_frame(&y, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  let mut via_area = vec![0u8; SRC * SRC * 3];
  {
    let mut sink =
      MixedSinker::<Yuv440p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(SRC, SRC))
        .unwrap()
        .with_rgb(&mut via_area)
        .unwrap();
    yuv440p_to(
      &yuv440p_frame(&y, &u, &v),
      true,
      sink.set_kernel_matrix(KernelMatrix::Bt601),
    )
    .unwrap();
  }
  assert_eq!(direct, via_area);
}

#[test]
fn yuv410p_resample_no_outputs_is_a_no_op() {
  let (y, u, v) = yuv410p_ramp();
  let mut sink =
    MixedSinker::<Yuv410p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
      .unwrap();
  // No outputs attached: a legal no-op, accepted without error.
  yuv410p_to(
    &yuv410p_frame(&y, &u, &v),
    true,
    sink.set_kernel_matrix(KernelMatrix::Bt601),
  )
  .unwrap();
}

#[test]
fn yuv440p_resample_no_outputs_is_a_no_op() {
  let (y, u, v) = yuv440p_ramp();
  let mut sink =
    MixedSinker::<Yuv440p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
      .unwrap();
  yuv440p_to(
    &yuv440p_frame(&y, &u, &v),
    true,
    sink.set_kernel_matrix(KernelMatrix::Bt601),
  )
  .unwrap();
}

#[test]
fn yuv410p_resample_rejects_out_of_sequence_rows() {
  let (y, u, v) = yuv410p_ramp();
  let cw = SRC / 4;
  let mut rgb = vec![0u8; OUT * OUT * 3];
  let mut sink =
    MixedSinker::<Yuv410p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
      .unwrap()
      .with_rgb(&mut rgb)
      .unwrap();
  sink.begin_frame(SRC as u32, SRC as u32).unwrap();
  // Feed row 2 first — the stream expects strict sequencing from 0. In
  // 4:1:0 the chroma row index is `row / 4`, so row 2 reads chroma 0.
  let row2 = Yuv410pRow::for_tests(
    &y[SRC * 2..SRC * 3],
    &u[0..cw],
    &v[0..cw],
    2,
    KernelMatrix::Bt601,
    true,
  );
  let err = sink.process(row2).unwrap_err();
  assert!(
    matches!(
      err,
      MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
    ),
    "got {err:?}"
  );
}

#[test]
fn yuv440p_resample_rejects_out_of_sequence_rows() {
  let (y, u, v) = yuv440p_ramp();
  let mut rgb = vec![0u8; OUT * OUT * 3];
  let mut sink =
    MixedSinker::<Yuv440p, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
      .unwrap()
      .with_rgb(&mut rgb)
      .unwrap();
  sink.begin_frame(SRC as u32, SRC as u32).unwrap();
  // Feed row 2 first — the stream expects strict sequencing from 0. In
  // 4:4:0 the chroma row index is `row / 2`, so row 2 reads chroma 1.
  let row2 = Yuv440pRow::for_tests(
    &y[SRC * 2..SRC * 3],
    &u[SRC..SRC * 2],
    &v[SRC..SRC * 2],
    2,
    KernelMatrix::Bt601,
    true,
  );
  let err = sink.process(row2).unwrap_err();
  assert!(
    matches!(
      err,
      MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
    ),
    "got {err:?}"
  );
}