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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//! Fused-downscale coverage for the packed-float-RGB family
//! (`Rgbf32`): the wire row converts to source-width host f32 RGB,
//! binning runs in float, the `rgb_f32` output is the exact area mean,
//! and every integer output (`rgb` / `rgba` / `rgb_u16` / `rgba_u16` /
//! `luma` / `luma_u16` / `hsv`) mirrors the direct Rgbf32 path's
//! clamp+scale kernels run over the binned row — the canonical oracle
//! being the direct full-res conversion over a pre-binned frame.

use crate::{
  KernelMatrix, PixelSink,
  frame::Rgbf32LeFrame,
  resample::{AreaResampler, ResampleError},
  sinker::{MixedSinker, MixedSinkerError},
  source::{Rgbf32, Rgbf32Row, rgbf32_to},
};

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

/// Re-encode a host-native f32 slice as **LE-encoded** byte storage,
/// so a fixture reads back identically on LE (no-op) and BE (byte-swap)
/// hosts. Mirrors `as_le_rgbf32` in the direct Rgbf32 tests.
fn as_le_rgbf32(host: &[f32]) -> Vec<f32> {
  host
    .iter()
    .map(|&v| f32::from_bits(u32::from_ne_bytes(v.to_bits().to_le_bytes())))
    .collect()
}

/// Per-channel f32 ramp with **integer-valued** samples (so the 2x2
/// area mean is exact in f32) that deliberately spans HDR (> 1.0) and
/// negative values — the float path must carry both losslessly into
/// `rgb_f32`, while the integer outputs saturate them per the direct
/// path's clamp.
fn packed_frame_f32() -> Vec<f32> {
  let mut buf = vec![0.0f32; SRC * SRC * 3];
  for (i, px) in buf.chunks_exact_mut(3).enumerate() {
    let i = i as i32;
    // R: small in-range integers and HDR (e.g. 2..) — saturates to the
    // integer max on the u8/u16 paths, preserved exactly in rgb_f32.
    px[0] = (i % 5) as f32; // 0..4, includes HDR (>1) values
    // G: large HDR values to exercise saturation.
    px[1] = (100 - i) as f32;
    // B: negative samples — clamp to 0 on integer outputs, preserved
    // exactly (with sign) in rgb_f32.
    px[2] = -((i % 7) as f32);
  }
  buf
}

/// Exact 2x2 block mean over host f32 values — integer-valued samples
/// divided by 4 (a power of two) are exactly representable, so this is
/// the bit-exact contract for the float area downscale.
fn expected_block_mean_f32(src: &[f32], ox: usize, oy: usize, c: usize) -> f32 {
  let mut acc = 0.0f64;
  for dy in 0..2 {
    for dx in 0..2 {
      acc += src[((oy * 2 + dy) * SRC + ox * 2 + dx) * 3 + c] as f64;
    }
  }
  (acc / 4.0) as f32
}

#[test]
fn rgbf32_downscale_rgb_f32_is_exact_area_mean() {
  let host = packed_frame_f32();
  let wire = as_le_rgbf32(&host);
  let src = Rgbf32LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();

  let mut rgb_f32 = vec![0.0f32; OUT * OUT * 3];
  {
    let mut sink =
      MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_rgb_f32(&mut rgb_f32)
        .unwrap();
    rgbf32_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  }
  for oy in 0..OUT {
    for ox in 0..OUT {
      for c in 0..3 {
        let got = rgb_f32[(oy * OUT + ox) * 3 + c];
        let want = expected_block_mean_f32(&host, ox, oy, c);
        assert_eq!(got, want, "({ox},{oy}) c{c}: {got} != {want}");
      }
    }
  }
}

#[test]
fn rgbf32_downscale_rgb_f32_preserves_hdr_and_negative() {
  // The binned mean of an HDR-or-negative 2x2 block must itself be HDR
  // / negative — proving the float path never clamps `rgb_f32`. The
  // fixture's G channel is `100 - i` (always > 1) and B is `-(i % 7)`
  // (<= 0), so at least one binned cell is > 1 and at least one < 0.
  let host = packed_frame_f32();
  let wire = as_le_rgbf32(&host);
  let src = Rgbf32LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();

  let mut rgb_f32 = vec![0.0f32; OUT * OUT * 3];
  {
    let mut sink =
      MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_rgb_f32(&mut rgb_f32)
        .unwrap();
    rgbf32_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  }
  let any_hdr = rgb_f32.iter().any(|&v| v > 1.0);
  let any_negative = rgb_f32.iter().any(|&v| v < 0.0);
  assert!(any_hdr, "binned rgb_f32 lost all HDR (> 1.0) values");
  assert!(any_negative, "binned rgb_f32 clamped away negative values");
}

#[test]
fn rgbf32_derived_outputs_come_from_binned_rgb() {
  // Every attached output — lossless f32, native-depth u16, and u8 —
  // must be exactly what the direct full-res Rgbf32 sink produces over
  // a frame that already holds the binned f32 RGB.
  let host = packed_frame_f32();
  let wire = as_le_rgbf32(&host);
  let src = Rgbf32LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();

  let mut rgb = vec![0u8; OUT * OUT * 3];
  let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
  let mut rgb_f32 = vec![0.0f32; OUT * OUT * 3];
  let mut rgba = vec![0u8; OUT * OUT * 4];
  let mut rgba_u16 = vec![0u16; OUT * OUT * 4];
  let mut luma = vec![0u8; OUT * OUT];
  let mut luma_u16 = vec![0u16; OUT * OUT];
  let mut h = vec![0u8; OUT * OUT];
  let mut s_ = vec![0u8; OUT * OUT];
  let mut v_ = vec![0u8; OUT * OUT];
  {
    let mut sink =
      MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_rgb(&mut rgb)
        .unwrap()
        .with_rgb_u16(&mut rgb_u16)
        .unwrap()
        .with_rgb_f32(&mut rgb_f32)
        .unwrap()
        .with_rgba(&mut rgba)
        .unwrap()
        .with_rgba_u16(&mut rgba_u16)
        .unwrap()
        .with_luma(&mut luma)
        .unwrap()
        .with_luma_u16(&mut luma_u16)
        .unwrap()
        .with_hsv(&mut h, &mut s_, &mut v_)
        .unwrap();
    rgbf32_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  }

  // Reference: the full-res sink over the (exact) binned f32 RGB,
  // re-encoded as an LE-wire plane exactly as the source arrived.
  let binned_wire = as_le_rgbf32(&rgb_f32);
  let mut ref_rgb = vec![0u8; OUT * OUT * 3];
  let mut ref_rgb_u16 = vec![0u16; OUT * OUT * 3];
  let mut ref_rgba = vec![0u8; OUT * OUT * 4];
  let mut ref_rgba_u16 = vec![0u16; OUT * OUT * 4];
  let mut ref_luma = vec![0u8; OUT * OUT];
  let mut ref_luma_u16 = vec![0u16; OUT * OUT];
  let mut ref_h = vec![0u8; OUT * OUT];
  let mut ref_s = vec![0u8; OUT * OUT];
  let mut ref_v = vec![0u8; OUT * OUT];
  {
    let binned =
      Rgbf32LeFrame::try_new(&binned_wire, OUT as u32, OUT as u32, (OUT * 3) as u32).unwrap();
    let mut sink = MixedSinker::<Rgbf32>::new(OUT, OUT)
      .with_rgb(&mut ref_rgb)
      .unwrap()
      .with_rgb_u16(&mut ref_rgb_u16)
      .unwrap()
      .with_rgba(&mut ref_rgba)
      .unwrap()
      .with_rgba_u16(&mut ref_rgba_u16)
      .unwrap()
      .with_luma(&mut ref_luma)
      .unwrap()
      .with_luma_u16(&mut ref_luma_u16)
      .unwrap()
      .with_hsv(&mut ref_h, &mut ref_s, &mut ref_v)
      .unwrap();
    rgbf32_to(&binned, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  }
  assert_eq!(rgb, ref_rgb, "rgb");
  assert_eq!(rgb_u16, ref_rgb_u16, "rgb_u16");
  assert_eq!(rgba, ref_rgba, "rgba");
  assert_eq!(rgba_u16, ref_rgba_u16, "rgba_u16");
  assert_eq!(luma, ref_luma, "luma");
  assert_eq!(luma_u16, ref_luma_u16, "luma_u16");
  assert_eq!(h, ref_h, "h");
  assert_eq!(s_, ref_s, "s");
  assert_eq!(v_, ref_v, "v");
}

#[test]
fn rgbf32_identity_plan_matches_new_sink() {
  let host = packed_frame_f32();
  let wire = as_le_rgbf32(&host);
  let src = Rgbf32LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();

  let mut direct = vec![0.0f32; SRC * SRC * 3];
  {
    let mut sink = MixedSinker::<Rgbf32>::new(SRC, SRC)
      .with_rgb_f32(&mut direct)
      .unwrap();
    rgbf32_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  }
  let mut via_area = vec![0.0f32; SRC * SRC * 3];
  {
    let mut sink =
      MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(SRC, SRC))
        .unwrap()
        .with_rgb_f32(&mut via_area)
        .unwrap();
    rgbf32_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  }
  assert_eq!(direct, via_area);
}

#[test]
fn rgbf32_no_output_sink_is_a_noop() {
  // A resampled sink with no attached output neither allocates the
  // stream nor enforces sequencing — the documented legal no-op.
  let host = packed_frame_f32();
  let wire = as_le_rgbf32(&host);
  let src = Rgbf32LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();

  let mut sink =
    MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
      .unwrap();
  rgbf32_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  assert!(
    !sink.rgb_stream_f32_allocated(),
    "no-output sink allocated the f32 stream"
  );
  assert_eq!(
    sink.rgb_scratch_capacity(),
    0,
    "no-output sink grew the narrow scratch"
  );
}

#[test]
fn rgbf32_f32_only_downscale_does_not_size_the_narrow_scratch() {
  let host = packed_frame_f32();
  let wire = as_le_rgbf32(&host);
  let src = Rgbf32LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();

  // f32-only sink (only rgb_f32 attached): the binned row is copied
  // losslessly, so the u8 narrow scratch is never sized.
  let mut rgb_f32 = vec![0.0f32; OUT * OUT * 3];
  let mut sink =
    MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
      .unwrap()
      .with_rgb_f32(&mut rgb_f32)
      .unwrap();
  rgbf32_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  assert_eq!(
    sink.rgb_scratch_capacity(),
    0,
    "f32-only sink grew the u8 narrow scratch"
  );

  // u16-only sink likewise stays clear of the u8 narrow scratch
  // (rgb_u16 derives directly from the float binned row).
  let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
  let mut sink_u16 =
    MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
      .unwrap()
      .with_rgb_u16(&mut rgb_u16)
      .unwrap();
  rgbf32_to(&src, true, sink_u16.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  assert_eq!(
    sink_u16.rgb_scratch_capacity(),
    0,
    "u16-only sink grew the u8 narrow scratch"
  );

  // Positive control: attaching a u8 output sizes it to the out-width
  // RGB row.
  let mut rgb = vec![0u8; OUT * OUT * 3];
  let mut sink2 =
    MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
      .unwrap()
      .with_rgb(&mut rgb)
      .unwrap();
  rgbf32_to(&src, true, sink2.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
  assert!(
    sink2.rgb_scratch_capacity() >= OUT * 3,
    "u8 output did not size the narrow scratch"
  );
}

#[test]
fn rgbf32_out_of_sequence_first_row_rejected_before_allocation() {
  let host = packed_frame_f32();
  let wire = as_le_rgbf32(&host);
  let row3 = &wire[3 * SRC * 3..4 * SRC * 3];

  let mut rgb_f32 = vec![0.0f32; OUT * OUT * 3];
  let mut sink =
    MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
      .unwrap()
      .with_rgb_f32(&mut rgb_f32)
      .unwrap();
  sink.begin_frame(SRC as u32, SRC as u32).unwrap();
  let err = sink
    .process(Rgbf32Row::for_tests(row3, 3, KernelMatrix::Bt709, true))
    .unwrap_err();
  assert!(
    matches!(
      err,
      MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
    ),
    "expected OutOfSequenceRow, got {err:?}"
  );
  // The out-of-sequence first row must be rejected before the f32
  // stream or the narrow scratch is allocated.
  assert!(
    !sink.rgb_stream_f32_allocated(),
    "stream allocated for a rejected row"
  );
  assert_eq!(
    sink.rgb_scratch_capacity(),
    0,
    "scratch grown for a rejected row"
  );
}

#[test]
fn rgbf32_rejected_first_row_does_not_poison_output_retry() {
  // A rejected out-of-sequence FIRST row must store no frozen-output
  // snapshot (the split packed-float-RGB preflight rejects the OOS first
  // row before its freeze), so retrying row 0 after attaching a new output
  // succeeds instead of tripping ResampleOutputsChanged.
  let host = packed_frame_f32();
  let wire = as_le_rgbf32(&host);
  let mut rgb_f32 = vec![0.0f32; OUT * OUT * 3];
  let mut sink =
    MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
      .unwrap()
      .with_rgb_f32(&mut rgb_f32)
      .unwrap();
  sink.begin_frame(SRC as u32, SRC as u32).unwrap();
  let err = sink
    .process(Rgbf32Row::for_tests(
      &wire[3 * SRC * 3..4 * SRC * 3],
      3,
      KernelMatrix::Bt709,
      true,
    ))
    .unwrap_err();
  assert!(
    matches!(
      err,
      MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
    ),
    "expected OutOfSequenceRow, got {err:?}"
  );
  let mut luma = vec![0u8; OUT * OUT];
  sink.set_luma(&mut luma).unwrap();
  sink
    .process(Rgbf32Row::for_tests(
      &wire[..SRC * 3],
      0,
      KernelMatrix::Bt709,
      true,
    ))
    .expect("row 0 must succeed after a rejected out-of-sequence first row");
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf32_first_row_scratch_oom_leaves_stream_and_freeze_uncommitted_for_retry() {
  let host = packed_frame_f32();
  let wire = as_le_rgbf32(&host);
  let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
  let mut luma = vec![0u8; OUT * OUT];
  {
    let mut sink =
      MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_rgb_u16(&mut rgb_u16)
        .unwrap();
    sink.begin_frame(SRC as u32, SRC as u32).unwrap();
    // Arm the single-shot source-RGB-`f32`-scratch grow failpoint: `Rgbf32` stages
    // its wire row into the host-`f32` scratch, so the row-0 area stream box builds
    // OK (into a local) and the scratch grow — which runs AFTER the stream build and
    // BEFORE the field insert — refuses, surfacing AllocationFailed with BOTH the
    // stream field `None` and the freeze uncommitted (the commit-together atomic
    // shape; the pre-fix ordering would have left the stream committed).
    crate::sinker::mixed::arm_source_rgb_f32_scratch_failure();
    let err = sink
      .process(Rgbf32Row::for_tests(
        &wire[..SRC * 3],
        0,
        KernelMatrix::Bt709,
        true,
      ))
      .unwrap_err();
    assert!(
      matches!(
        err,
        MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
      ),
      "first-row scratch OOM must surface AllocationFailed, got {err:?}"
    );
    // DIRECT stream-rollback proof (the non-vacuous part): the `f32` area stream
    // field must be `None` — the fix builds it into a LOCAL and inserts only after
    // the scratch grows, so a failed scratch leaves nothing committed. The
    // insert-before-scratch shape would leave it `Some` here, so this assertion FAILS
    // against that shape and PASSES against the fix (a `try_box` box failure cannot
    // make this distinction — it fails before any stream exists to insert).
    assert!(
      !sink.rgb_stream_f32_allocated(),
      "a scratch OOM must leave the rgb_stream_f32 field None (no partial commit)"
    );
    // The freeze was likewise never committed — attaching luma (a CHANGED output
    // set) and replaying from row 0 is ACCEPTED, not ResampleOutputsChanged.
    sink.set_luma(&mut luma).unwrap();
    for r in 0..SRC {
      sink
        .process(Rgbf32Row::for_tests(
          &wire[r * SRC * 3..(r + 1) * SRC * 3],
          r,
          KernelMatrix::Bt709,
          true,
        ))
        .expect("frame replay after a first-row scratch OOM must succeed");
    }
  }
  assert!(
    rgb_u16.iter().any(|&b| b != 0),
    "the recovered frame produced real rgb_u16 output (scratch OOM was recoverable)"
  );
  assert!(
    luma.iter().any(|&b| b != 0),
    "the newly-attached luma output was driven on the accepted retry"
  );
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf32_first_row_emit_scratch_oom_leaves_stream_and_freeze_uncommitted_for_retry() {
  // The EMIT-owned out-width u8 narrow scratch (grown for a u8 `rgb` output), NOT
  // the source f32 staging scratch. The combined helper pre-grows every emit-owned
  // scratch via an `emit(None)` pregrow call BEFORE the freeze (mirroring the u16 /
  // u32 helpers), so a first-row OOM in it surfaces with the stream field `None` and
  // `resample_outputs` uncommitted — never a partial commit.
  let host = packed_frame_f32();
  let wire = as_le_rgbf32(&host);
  let mut rgb = vec![0u8; OUT * OUT * 3];
  let mut luma = vec![0u8; OUT * OUT];
  {
    let mut sink =
      MixedSinker::<Rgbf32, 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();
    // Arm the u8 narrow-scratch (`source_rgb_scratch`) grow failpoint — the scratch
    // the `Rgbf32` emit grows for its u8 `rgb` output, AFTER the stream box builds.
    // The pregrow call runs it before the freeze, so this OOM must leave BOTH the
    // stream field `None` and the freeze uncommitted (the post-freeze-grow shape
    // would have left the stream committed).
    crate::sinker::mixed::arm_source_rgb_scratch_failure();
    let err = sink
      .process(Rgbf32Row::for_tests(
        &wire[..SRC * 3],
        0,
        KernelMatrix::Bt709,
        true,
      ))
      .unwrap_err();
    assert!(
      matches!(
        err,
        MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
      ),
      "first-row emit-scratch OOM must surface AllocationFailed, got {err:?}"
    );
    // DIRECT stream-rollback proof: an emit-OWNED scratch OOM must ALSO leave the
    // field `None` — the helper pregrows the emit scratch before the stream insert +
    // freeze, so nothing is committed. The post-freeze-grow shape would leave it
    // `Some` here (freeze committed, stream inserted), so this assertion FAILS
    // against that shape and PASSES against the fix.
    assert!(
      !sink.rgb_stream_f32_allocated(),
      "an emit-owned scratch OOM must leave the rgb_stream_f32 field None (no partial commit)"
    );
    // The freeze was likewise never committed — attaching luma (a CHANGED output
    // set) and replaying from row 0 is ACCEPTED, not ResampleOutputsChanged.
    sink.set_luma(&mut luma).unwrap();
    for r in 0..SRC {
      sink
        .process(Rgbf32Row::for_tests(
          &wire[r * SRC * 3..(r + 1) * SRC * 3],
          r,
          KernelMatrix::Bt709,
          true,
        ))
        .expect("frame replay after a first-row emit-scratch OOM must succeed");
    }
  }
  assert!(
    rgb.iter().any(|&b| b != 0),
    "the recovered frame produced real rgb output (emit-scratch OOM was recoverable)"
  );
  assert!(
    luma.iter().any(|&b| b != 0),
    "the newly-attached luma output was driven on the accepted retry"
  );
}

#[test]
fn rgbf32_mid_frame_out_of_sequence_rejected() {
  let host = packed_frame_f32();
  let wire = as_le_rgbf32(&host);

  let mut rgb_f32 = vec![0.0f32; OUT * OUT * 3];
  let mut sink =
    MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
      .unwrap()
      .with_rgb_f32(&mut rgb_f32)
      .unwrap();
  sink.begin_frame(SRC as u32, SRC as u32).unwrap();
  sink
    .process(Rgbf32Row::for_tests(
      &wire[..SRC * 3],
      0,
      KernelMatrix::Bt709,
      true,
    ))
    .unwrap();
  // Skip row 1 — feeding row 2 next must be rejected.
  let err = sink
    .process(Rgbf32Row::for_tests(
      &wire[2 * SRC * 3..3 * SRC * 3],
      2,
      KernelMatrix::Bt709,
      true,
    ))
    .unwrap_err();
  assert!(matches!(
    err,
    MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
  ));
}

#[test]
fn rgbf32_mid_frame_output_change_rejected() {
  let host = packed_frame_f32();
  let wire = as_le_rgbf32(&host);

  let mut rgb_f32 = vec![0.0f32; OUT * OUT * 3];
  let mut luma = vec![0u8; OUT * OUT];
  {
    let mut sink =
      MixedSinker::<Rgbf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
        .unwrap()
        .with_rgb_f32(&mut rgb_f32)
        .unwrap();
    sink.begin_frame(SRC as u32, SRC as u32).unwrap();
    sink
      .process(Rgbf32Row::for_tests(
        &wire[..SRC * 3],
        0,
        KernelMatrix::Bt709,
        true,
      ))
      .unwrap();
    sink.set_luma(&mut luma).unwrap();
    let err = sink
      .process(Rgbf32Row::for_tests(
        &wire[SRC * 3..SRC * 6],
        1,
        KernelMatrix::Bt709,
        true,
      ))
      .unwrap_err();
    assert!(matches!(err, MixedSinkerError::ResampleOutputsChanged(_)));
  }
  assert!(luma.iter().all(|&l| l == 0));
}