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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
//! Native fast-tier decimator for the **high-bit planar non-4:2:0** YUV
//! families — `Yuv422p10/12/14/16` (4:2:2), `Yuv444p10/12/14/16` (4:4:4),
//! `Yuv440p10/12` (4:4:0), LE + BE wire.
//!
//! The `u16` twin of the 8-bit non-4:2:0 planar
//! [`yuv_planar_process_native`](super::planar_8bit::yuv_planar_process_native),
//! and the non-4:2:0 sibling of the high-bit 4:2:0
//! [`yuv420p16_process_native`](super::subsampled_4_2_0_high_bit::yuv420p16_process_native):
//! bin the host-native Y / U / V planes straight to the output grid, then
//! convert ONCE per output row at output width through the 4:4:4 kernel (the
//! binned chroma is full-width at output resolution, so the convert is always
//! 4:4:4) — vs the row-stage tier
//! ([`packed_yuv422_triple_resample`](super::packed_yuv422_triple_resample) /
//! [`packed_yuv444_triple_resample`](super::packed_yuv444_triple_resample)),
//! which converts each source row at source width then bins. This is the
//! demand-driven P2 fast tier: default-on, within a small tolerance of the
//! row-stage tier (NOT byte-identical to the direct path), bench-gated.
//!
//! The three formats differ only in the chroma grid and its vertical cadence,
//! both supplied by the per-format caller (`chroma_vsub` + a `build_chroma_plan`
//! closure) so this body stays layout-agnostic — identical to the 8-bit twin:
//! - `Yuv422p` (4:2:2): chroma `w/2 x h` — half width, FULL height; a chroma
//!   row per Y row (`chroma_vsub == 1`); chroma plan a plain
//!   [`ResamplePlan::area`](crate::resample::ResamplePlan::area) over
//!   `(w/2, h)`; de-interleave width `w/2`.
//! - `Yuv444p` (4:4:4): chroma `w x h` — identical to Y; same lockstep cadence
//!   (`chroma_vsub == 1`); chroma plan equals the luma plan; de-interleave
//!   width `w`.
//! - `Yuv440p` (4:4:0): chroma `w x h/2` — FULL width, half height; a chroma
//!   row per TWO Y rows (`chroma_vsub == 2`, like 4:2:0 vertically); chroma
//!   plan [`ResamplePlan::area_chroma_440`](crate::resample::ResamplePlan::area_chroma_440)
//!   (full-width horizontal, luma-domain `area_halved` vertical); de-interleave
//!   width `w`.
//!
//! The triple-bin + convert-kernel independence is the highest risk (the #37
//! lesson: the u8 and u16 `YUV→RGB` kernels round INDEPENDENTLY, and a
//! u16-output kernel must never be narrowed from a u8 one or vice versa). Per
//! output row the staged host-native u16 Y / U / V feed:
//! - `luma` (u8): `dst = (Y >> (BITS - 8)) as u8`.
//! - `rgb_u16` / `rgba_u16`: the native-depth 4:4:4 u16 kernel (the generic
//!   `yuv444pN_to_rgb_u16_row_endian` for BITS ∈ {9, 10, 12, 14}, the dedicated
//!   `yuv444p16_to_rgb_u16_row_endian` for BITS = 16) — its OWN i32/i64 kernel,
//!   never a narrowing of the u8 colour — then expand to rgba_u16.
//! - `rgb` / `rgba` / `hsv` (u8): the u16-INPUT → u8-OUTPUT 4:4:4 kernel (the
//!   SAME one the row-stage 4:4:4 high-bit path uses), then fan to rgba / hsv.
//!
//! ★ THE NATIVE-DEPTH CLAMP. For sub-16-bit BITS (9/10/12/14) every colour
//! sample is clamped to `(1 << BITS) - 1` — done INSIDE the convert kernels
//! (`yuv_444p_n_to_rgb_or_rgba_u16_row` masks the inputs `& (1<<BITS)-1` and
//! clamps each output to `(1<<BITS)-1`; the i64 BITS=16 kernel covers the full
//! u16 range). The binned Y / U / V are area means of in-range host-native
//! samples, so they stay in range; the native-Y `luma` output is `>> (BITS-8)`
//! into u8, always in range. The optional native-depth `luma_u16` output is the
//! binned Y clamped to `(1 << BITS) - 1` (host-native u16, NOT narrowed) — the
//! formats with no `luma_u16` channel (planar / semi-planar) pass `&mut None`;
//! the packed Y2xx family threads its real buffer so attaching `luma_u16` no
//! longer falls the pipeline back to the row-stage tier. The bin-then-convert
//! test oracle MUST run the SAME clamping kernels (via an identity-resolution
//! high-bit 4:4:4 sink) so the comparison is clamp-for-clamp exact, never
//! against an unclamped value.
//!
//! The staged Y / U / V are HOST-NATIVE (the wire row is de-interleaved to
//! host-native u16 via [`deinterleave_y_high_bit_masked`](super::deinterleave_y_high_bit_masked)
//! into private source-width scratch BEFORE the [`AreaStream`] bins it —
//! matching the row-stage path), so every convert kernel runs with
//! `BE = HOST_NATIVE_BE` (= `from_ne`, a no-op load on every host) regardless
//! of the source wire endianness. A hard-coded `false` (`from_le`) would
//! byte-swap the already-native value on a big-endian target, corrupting
//! default-on native colour.

use super::{
  GeometryOverflow, HsvFrameMut, MixedSinkerError, deinterleave_y_high_bit_masked,
  frozen_outputs_check, planar_8bit::native_preflight_core_check_only,
};
use crate::{
  KernelMatrix,
  resample::{AreaStream, PlanGeometry, ResampleError, ResamplePlan, try_box, try_zeroed},
  row::{
    expand_rgb_to_rgba_row, expand_rgb_u16_to_rgba_u16_row, rgb_to_hsv_row,
    yuv444p9_to_rgb_row_endian, yuv444p9_to_rgb_u16_row_endian, yuv444p10_to_rgb_row_endian,
    yuv444p10_to_rgb_u16_row_endian, yuv444p12_to_rgb_row_endian, yuv444p12_to_rgb_u16_row_endian,
    yuv444p14_to_rgb_row_endian, yuv444p14_to_rgb_u16_row_endian, yuv444p16_to_rgb_row_endian,
    yuv444p16_to_rgb_u16_row_endian,
  },
};

// The staged Y / U / V the `AreaStream` produces are HOST-NATIVE u16 (the wire
// was decoded to native by `deinterleave_y_high_bit_masked` BEFORE binning), so every
// `*_to_*_row_endian` convert below must read them with `BE = HOST_NATIVE_BE`
// to keep the kernel's `from_le` / `from_be` load a no-op on EVERY host.
// Mirrors `subsampled_4_2_0_high_bit::native`'s `HOST_NATIVE_BE`.
const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");

#[cfg(all(test, feature = "std", feature = "yuv-planar"))]
std::thread_local! {
  /// Fail the (`skip` + 1)th real source-scratch grow of a row: `None` disarmed;
  /// `Some(k)` skips `k` real grows then fails the next (so `Some(0)` fails the
  /// first — the `y_src` grow — and `Some(1)` / `Some(2)` fail the later `u_src`
  /// / `v_src` grows). A counter, not a bool, so a test can target a LATER
  /// source grow and prove that a late failure still leaves the join / freeze /
  /// output atomic and the row retryable.
  static FORCE_PLANAR_HB_NATIVE_ALLOC_FAILURE: core::cell::Cell<Option<usize>> =
    const { core::cell::Cell::new(None) };
}

/// Arms the source-scratch allocation failpoint to fail the FIRST fallible
/// source-scratch grow of the **next** output-bearing high-bit non-4:2:0 planar
/// native row on the current thread (the `y_src` grow). Consumed when it fires,
/// so it triggers exactly once and cannot leak into a later test. Test-only —
/// mirrors `arm_native_u16_alloc_failure` for the 4:2:0 high-bit planar native
/// tier. Only the high-bit planar native suite arms it, and that suite is gated
/// on `rgb` (its colour oracle), so the setter is dead in a
/// `yuv-planar`-without-`rgb` build even though the thread-local it sets is read
/// by the (always-`yuv-planar`) native join.
#[cfg(all(test, feature = "std", feature = "yuv-planar", feature = "rgb"))]
pub(crate) fn arm_planar_hb_native_alloc_failure() {
  FORCE_PLANAR_HB_NATIVE_ALLOC_FAILURE.with(|f| f.set(Some(0)));
}

/// Arms the source-scratch failpoint to fail the source-scratch grow AFTER
/// `skip` real grows — `skip = 1` fails the SECOND source grow (`u_src`, after
/// `y_src`) and `skip = 2` the THIRD (`v_src`). Lets a test prove that a LATER
/// source-plane grow failing still leaves the cached join, the `resample_outputs`
/// freeze, and the output frame untouched (the row retryable), even though an
/// earlier source scratch already grew. Test-only; `rgb`-gated like its sibling.
#[cfg(all(test, feature = "std", feature = "yuv-planar", feature = "rgb"))]
pub(crate) fn arm_planar_hb_native_alloc_failure_after(skip: usize) {
  FORCE_PLANAR_HB_NATIVE_ALLOC_FAILURE.with(|f| f.set(Some(skip)));
}

#[cfg(all(test, feature = "std", feature = "yuv-planar"))]
std::thread_local! {
  static FORCE_PLANAR_HB_NATIVE_CHROMA_FAILURE: core::cell::Cell<bool> =
    const { core::cell::Cell::new(false) };
}

/// Arms a failpoint that fires when (and only when) the native join PLANS its
/// chroma grid — which happens exactly when colour output is requested. A
/// luma-only sink must never reach it, so an armed flag survives a luma-only
/// row unconsumed (the regression assertion) and is taken by the first colour
/// row. Test-only.
///
/// Only the high-bit non-4:2:0 native suites that own a colour oracle arm it —
/// V210 / Y2xx / P2xx / P4xx / packed-4:4:4 (each pulling in `yuv-planar` for
/// the join) plus the `rgb`-gated planar high-bit native suite — so the setter
/// is dead in a `yuv-planar`-without-any-consumer build even though the
/// thread-local it sets is read by the (always-`yuv-planar`) native join.
#[cfg(all(
  test,
  feature = "std",
  feature = "yuv-planar",
  any(
    feature = "rgb",
    feature = "v210",
    feature = "y2xx",
    feature = "yuv-444-packed",
    feature = "yuv-semi-planar"
  )
))]
pub(crate) fn arm_planar_hb_native_chroma_failure() {
  FORCE_PLANAR_HB_NATIVE_CHROMA_FAILURE.with(|f| f.set(true));
}

#[cfg(all(test, feature = "std", feature = "yuv-planar"))]
std::thread_local! {
  static FORCE_PLANAR_HB_NATIVE_RGB_U16_SCRATCH_FAILURE: core::cell::Cell<bool> =
    const { core::cell::Cell::new(false) };
}

/// Arms the native-depth u16 colour-scratch (`rgb_scratch_u16`) grow failpoint
/// for the next output-bearing row, consumed (take-on-read) when it fires so it
/// triggers once. The u16 colour scratch grows AFTER the u8 colour scratch, so a
/// test arming this proves a LATER pre-feed grow failing still leaves the cached
/// join, the `resample_outputs` freeze, and the output frame untouched and the
/// row retryable. Test-only; `rgb`-gated like the source-scratch failpoint.
#[cfg(all(test, feature = "std", feature = "yuv-planar", feature = "rgb"))]
pub(crate) fn arm_planar_hb_native_rgb_u16_scratch_failure() {
  FORCE_PLANAR_HB_NATIVE_RGB_U16_SCRATCH_FAILURE.with(|f| f.set(true));
}

/// Native decimation join for the high-bit non-4:2:0 planar families — the
/// `u16` twin of [`NativePlanarYuv`](super::planar_8bit::NativePlanarYuv) and
/// the non-4:2:0 sibling of
/// [`NativeYuv420U16`](super::subsampled_4_2_0_high_bit::NativeYuv420U16). Y
/// streams on the frame grid; U / V on the format's chroma grid (its vertical
/// cadence captured by [`Self::chroma_vsub`]), every plane binned to FULL
/// output resolution and converted ONCE per output row at output width through
/// the 4:4:4 kernels. Each plane's in-order emissions stage into a two-slot
/// ring (`out_y & 1`); the moment all participating planes hold an output row
/// it finalizes — so no output-geometry alignment constraint ever applies. A
/// plane may lead another by at most one source row (the chroma grid is within
/// a factor of two of the luma grid vertically), which the two slots absorb.
/// For the lockstep formats (`chroma_vsub == 1`) the planes never lead, but
/// the two-slot machinery is harmless and shared.
pub(crate) struct NativePlanarYuvU16 {
  y: AreaStream<u16>,
  /// Source-width host-native Y de-interleave scratch (the wire Y plane
  /// normalized via [`deinterleave_y_high_bit_masked`] before [`Self::y`] bins it).
  /// Lazily grown to `src_w` `u16` on the first output-bearing row; empty
  /// otherwise.
  y_src: std::vec::Vec<u16>,
  /// Two-slot staging ring, `2 * out_w` (slot = `out_y & 1`).
  y_stage: std::vec::Vec<u16>,
  /// Chroma half of the join — absent for luma-only sinks, which therefore
  /// never read the chroma planes (the documented fast path). Decided at
  /// creation: the frozen-output contract makes the attached set
  /// frame-constant.
  chroma: Option<NativePlanarChromaU16>,
  /// Source-row width of the chroma planes (`w/2` for 4:2:2, `w` for 4:4:4 /
  /// 4:4:0) — the de-interleave length the chroma scratch is grown to and
  /// fed at. Distinct from the luma `src_w`.
  chroma_w: usize,
  /// Vertical chroma subsample factor: 1 for 4:2:2 / 4:4:4 (a chroma row per
  /// luma row), 2 for 4:4:0 (a chroma row per two luma rows). The chroma
  /// stream is fed when `idx % chroma_vsub == 0`, at `cidx = idx / chroma_vsub`,
  /// and the sequence check expects `chroma.next_y() == idx.div_ceil(chroma_vsub)`.
  chroma_vsub: usize,
  /// `staged[plane][slot]` — plane 0 = Y, 1 = U, 2 = V.
  staged: [[bool; 2]; 3],
  /// Next output row to finalize.
  next_emit: usize,
  /// Whether the cached chroma plan was built with a non-zero chroma phase
  /// (RFC #238 centered siting). The join is built once and only `reset`
  /// between frames, so a reused sink whose `chroma_location` moved to a
  /// different phase must REBUILD it — the caller compares this against the
  /// current siting and drops the join on a mismatch. `false` for a luma-only
  /// join (no chroma plan) and for every co-sited / unphased layout.
  chroma_centered: bool,
  /// The vertical (`Bottom`, `v = 1`) companion of [`Self::chroma_centered`]
  /// (RFC #238 S8c — the high-bit 4:4:0 native tier). Set when the cached chroma
  /// plan folds the `v = 1` phase; [`ResamplePlan::has_chroma_phase`] cannot tell
  /// a co-sited plan from a bottom-folded one (4:4:0 has no horizontal phase, so
  /// `chroma_centered` stays `false` for it), so the caller compares THIS flag to
  /// rebuild a reused join whose vertical siting moved. `false` for a luma-only
  /// join and every co-sited layout.
  chroma_bottom: bool,
  /// The vertical (`Top`, `v = 0`) FORWARD companion of [`Self::chroma_bottom`]
  /// (RFC #238 — the high-bit 4:4:0 native tier). Set when the cached chroma plan
  /// folds the `v = 0` forward triangle. Co-sited and `Top` both leave
  /// `chroma_centered` / `chroma_bottom` `false`, so the caller compares THIS
  /// flag to rebuild a reused join whose vertical siting moved to / from Top.
  /// `false` for a luma-only join and every non-Top layout.
  chroma_top: bool,
}

/// Chroma-grid streams, source de-interleave scratch, and staging of
/// [`NativePlanarYuvU16`].
struct NativePlanarChromaU16 {
  u: AreaStream<u16>,
  v: AreaStream<u16>,
  /// Source-width (chroma-width) host-native U / V de-interleave scratch.
  /// Lazily grown to `chroma_w` `u16` each on the first chroma-bearing output
  /// row; empty otherwise.
  u_src: std::vec::Vec<u16>,
  v_src: std::vec::Vec<u16>,
  u_stage: std::vec::Vec<u16>,
  v_stage: std::vec::Vec<u16>,
}

impl NativePlanarYuvU16 {
  /// `build_chroma_plan` lazily builds the format's chroma grid against the
  /// SAME output geometry as `plan` (the luma plan) — invoked ONLY when colour
  /// is needed, so a luma-only sink never plans or allocates chroma state.
  /// `chroma_vsub` is its vertical cadence; `chroma_w` its source-row width.
  /// All are supplied by the per-format caller so this body stays
  /// layout-agnostic.
  fn new(
    plan: &ResamplePlan,
    build_chroma_plan: impl FnOnce() -> Result<ResamplePlan, ResampleError>,
    chroma_vsub: usize,
    chroma_w: usize,
    w: usize,
    h: usize,
    need_color: bool,
  ) -> Result<Self, ResampleError> {
    // The native high-bit planar join is integer area-only; reject a filter
    // plan before building any plane's area stream.
    if plan.kind().is_filter() {
      return Err(plan.unsupported_filter());
    }
    let y = AreaStream::new(plan.h(), plan.v(), w, h, 1)?;
    let alloc =
      |_| ResampleError::AllocationFailed(PlanGeometry::new(w, h, plan.out_w(), plan.out_h()));
    let stage_len = plan.out_w().checked_mul(2).ok_or_else(|| {
      ResampleError::Overflow(PlanGeometry::new(w, h, plan.out_w(), plan.out_h()))
    })?;
    // Recorded so a reused sink rebuilds (not merely resets) the join when the
    // frame's chroma siting phase changes; stays `false` for a luma-only join.
    let mut chroma_centered = false;
    // RFC #238 S8c: the vertical (`Bottom`) companion — set when the chroma plan
    // folds the `v = 1` phase (the high-bit 4:4:0 native tier). 4:4:0 has no
    // horizontal phase, so `chroma_centered` stays `false` for it and the
    // vertical axis is recorded separately here.
    let mut chroma_bottom = false;
    // RFC #238 Top companion — set when the chroma plan folds the `v = 0`
    // FORWARD phase (the high-bit 4:4:0 native tier). Co-sited and `Top` share
    // `chroma_bottom = false`, so the Top axis is recorded separately.
    let mut chroma_top = false;
    let chroma = if need_color {
      #[cfg(all(test, feature = "std", feature = "yuv-planar"))]
      if FORCE_PLANAR_HB_NATIVE_CHROMA_FAILURE.with(|f| f.take()) {
        return Err(ResampleError::AllocationFailed(PlanGeometry::new(
          w,
          h,
          plan.out_w(),
          plan.out_h(),
        )));
      }
      let chroma_plan = build_chroma_plan()?;
      chroma_centered = chroma_plan.has_chroma_phase();
      chroma_bottom = chroma_plan.has_chroma_v_phase();
      chroma_top = chroma_plan.has_chroma_v_top();
      Some(NativePlanarChromaU16 {
        u: AreaStream::new(
          chroma_plan.h(),
          chroma_plan.v(),
          chroma_plan.src_w(),
          chroma_plan.src_h(),
          1,
        )?,
        v: AreaStream::new(
          chroma_plan.h(),
          chroma_plan.v(),
          chroma_plan.src_w(),
          chroma_plan.src_h(),
          1,
        )?,
        u_src: std::vec::Vec::new(),
        v_src: std::vec::Vec::new(),
        u_stage: try_zeroed(stage_len).map_err(alloc)?,
        v_stage: try_zeroed(stage_len).map_err(alloc)?,
      })
    } else {
      None
    };
    Ok(Self {
      y,
      y_src: std::vec::Vec::new(),
      y_stage: try_zeroed(stage_len).map_err(alloc)?,
      chroma,
      chroma_w,
      chroma_vsub,
      staged: [[false; 2]; 3],
      next_emit: 0,
      chroma_centered,
      chroma_bottom,
      chroma_top,
    })
  }

  pub(crate) fn reset(&mut self) {
    self.y.reset();
    if let Some(chroma) = self.chroma.as_mut() {
      chroma.u.reset();
      chroma.v.reset();
    }
    self.staged = [[false; 2]; 3];
    self.next_emit = 0;
  }

  /// Next Y source row this join expects — the per-row sequence counter (the
  /// native path bins Y on every output-bearing row, luma implicit).
  pub(crate) fn next_y(&self) -> usize {
    self.y.next_y()
  }

  /// The cached chroma plan's centered-phase flag (RFC #238), `Some` only when
  /// a chroma half is present — a luma-only join carries no phase, so its
  /// siting is irrelevant. The native call site drops-and-rebuilds a reused
  /// join whose horizontal chroma phase no longer matches the frame's siting.
  pub(crate) fn chroma_phase_centered(&self) -> Option<bool> {
    self.chroma.as_ref().map(|_| self.chroma_centered)
  }

  /// The cached chroma plan's vertical (`Bottom`, `v = 1`) phase flag (RFC #238
  /// S8c), `Some` only when a chroma half is present — a luma-only join carries no
  /// phase, so its siting is irrelevant. The native call site drops-and-rebuilds a
  /// reused join whose vertical chroma phase no longer matches the frame's siting
  /// (the 4:4:0 companion of [`Self::chroma_phase_centered`]).
  pub(crate) fn chroma_bottom(&self) -> Option<bool> {
    self.chroma.as_ref().map(|_| self.chroma_bottom)
  }

  /// The cached chroma plan's vertical (`Top`, `v = 0`) FORWARD phase flag
  /// (RFC #238), `Some` only when a chroma half is present. Co-sited and `Top`
  /// share [`Self::chroma_bottom`] `Some(false)`, so the native call site
  /// compares THIS flag too to rebuild a reused join whose vertical siting moved
  /// to / from Top.
  pub(crate) fn chroma_top(&self) -> Option<bool> {
    self.chroma.as_ref().map(|_| self.chroma_top)
  }

  /// Sequencing preflight across all three plane streams — checked before any
  /// plane is fed so a violating call mutates nothing. Chroma rows advance
  /// once per `chroma_vsub` source rows, so their expected counter is
  /// `idx.div_ceil(chroma_vsub)`.
  fn check_sequence(&self, idx: usize) -> Result<(), MixedSinkerError> {
    if self.y.next_y() != idx {
      return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
        crate::resample::OutOfSequenceRow::new(self.y.next_y(), idx),
      )));
    }
    if let Some(chroma) = self.chroma.as_ref() {
      let chroma_expected = idx.div_ceil(self.chroma_vsub);
      for stream in [&chroma.u, &chroma.v] {
        if stream.next_y() != chroma_expected {
          return Err(MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(
            crate::resample::OutOfSequenceRow::new(
              stream.next_y().saturating_mul(self.chroma_vsub),
              idx,
            ),
          )));
        }
      }
    }
    Ok(())
  }
}

/// Compare-only pre-feed rejection preflight for the [`NativePlanarYuvU16`] join:
/// the 4-point rejection (no-output short-circuit, first-row out-of-sequence,
/// output-set compare, post-compare sequence) via
/// [`native_preflight_core_check_only`], keyed on the u16-join-typed expected row.
/// Threads the native-depth u16 colour outputs (`rgb_u16` / `rgba_u16`) plus the
/// optional native-depth `luma_u16` into the output-set compare — the planar /
/// semi-planar families expose no `luma_u16` and pass `&None`, while the packed
/// Y2xx family threads its real buffer so a mid-frame `luma_u16` attach is
/// classified by the compare (`ResampleOutputsChanged`), not the route guard.
///
/// **NO commit** — it COMPARES the frozen output set against a fresh snapshot
/// instead of storing it. Every native non-4:2:0 caller runs it: the routing
/// wrappers, the semi-planar 4:2:2 / 4:4:4 P-format wrappers (`p2xx` / `p4xx`),
/// the MSB `yuv444p_msb` wrapper, and [`yuv_planar16_process_native`] itself.
/// [`yuv_planar16_process_native`] builds its join into a LOCAL and grows its
/// colour / source scratch AFTER this passes, then performs the SINGLE output-set
/// freeze once every pre-feed allocation (the wrappers' de-pack grows AND the
/// delegate's colour / source grows) has succeeded — so a first-row / rebuild-row
/// / later-grow failure leaves the cached join, the `resample_outputs` freeze, and
/// the output frame untouched and the row retryable (the persistent scratch may
/// keep grown capacity — join / freeze / output-atomic, not memory-exact). The
/// high-bit non-4:2:0 sibling of
/// [`native_planar_preflight_check_only`](super::planar_8bit::native_planar_preflight_check_only),
/// mirroring how the 8-bit semi-planar non-4:2:0 wrapper defers to its delegate.
#[allow(clippy::too_many_arguments)]
pub(crate) fn native_planar_hb_preflight_check_only(
  join: &Option<std::boxed::Box<NativePlanarYuvU16>>,
  resample_outputs: &Option<super::FrozenOutputs>,
  rgb: &Option<&mut [u8]>,
  rgba: &Option<&mut [u8]>,
  rgb_u16: &Option<&mut [u16]>,
  rgba_u16: &Option<&mut [u16]>,
  luma: &Option<&mut [u8]>,
  luma_u16: &Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  idx: usize,
  need_luma: bool,
  need_color: bool,
) -> Result<bool, MixedSinkerError> {
  native_preflight_core_check_only(
    join.as_ref().map_or(0, |j| j.next_y()),
    resample_outputs,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    luma,
    luma_u16,
    hsv,
    idx,
    need_luma,
    need_color,
  )
}

/// Grows a source-de-interleave scratch to `len` `u16` under the planner's
/// recoverable-allocation contract, with the test-only source-scratch failpoint
/// (a skip counter over the row's real source grows — the FIRST by default, a
/// later `u_src` / `v_src` when armed via `arm_planar_hb_native_alloc_failure_after`;
/// mirrors the 4:2:0 high-bit `grow_src_scratch`). Runs after the preflight and
/// after the join is built, so a rejected row never reaches it. A failure here is
/// pre-commit: the caller's join / freeze / output stay untouched and the row is
/// retryable, though any earlier source scratch keeps its already-grown capacity.
#[cfg_attr(not(tarpaulin), inline(always))]
fn grow_src_scratch(
  scratch: &mut std::vec::Vec<u16>,
  len: usize,
  w: usize,
  h: usize,
  plan: &ResamplePlan,
) -> Result<(), MixedSinkerError> {
  if scratch.len() < len {
    // Skip-counter failpoint: fail the armed grow (consuming the arm), otherwise
    // decrement so a later grow of the same row fires it.
    #[cfg(all(test, feature = "std", feature = "yuv-planar"))]
    if let Some(remaining) = FORCE_PLANAR_HB_NATIVE_ALLOC_FAILURE.with(|f| f.get()) {
      if remaining == 0 {
        FORCE_PLANAR_HB_NATIVE_ALLOC_FAILURE.with(|f| f.set(None));
        return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
          PlanGeometry::new(w, h, plan.out_w(), plan.out_h()),
        )));
      }
      FORCE_PLANAR_HB_NATIVE_ALLOC_FAILURE.with(|f| f.set(Some(remaining - 1)));
    }
    scratch
      .try_reserve_exact(len - scratch.len())
      .map_err(|_| {
        MixedSinkerError::Resample(ResampleError::AllocationFailed(PlanGeometry::new(
          w,
          h,
          plan.out_w(),
          plan.out_h(),
        )))
      })?;
    scratch.resize(len, 0);
  }
  Ok(())
}

/// Native-tier path for the high-bit non-4:2:0 planar families. Const-generic
/// over `BITS` (one body, the live call sites 9 / 10 / 12 / 14 / 16) and `BE`
/// (the source wire endianness). `chroma_vsub` is the format's vertical chroma
/// cadence (1 for 4:2:2 / 4:4:4, 2 for 4:4:0), `chroma_w` its source-row width
/// (`w/2` for 4:2:2, `w` for 4:4:4 / 4:4:0), and `build_chroma_plan` builds its
/// chroma grid against the same output geometry; all three are supplied by the
/// per-format caller so this body is layout-agnostic. Preflight-transactional
/// (mirrors the 8-bit non-4:2:0 twin
/// [`yuv_planar_process_native`](super::planar_8bit::yuv_planar_process_native)):
/// the compare-only pre-feed preflight, the join built into a LOCAL, then the
/// colour and source scratch sizing — and only once every pre-feed allocation
/// has succeeded does it COMMIT (install the rebuilt join, freeze the output
/// set), sequence-check, and feed. So on ANY pre-feed allocation failure the
/// cached join, the `resample_outputs` freeze, and the output frame stay
/// untouched and the row is retryable (the deterministic typed error, never a
/// committed-then-corrupt state); the persistent reused scratch buffers
/// (`rgb_scratch` / `rgb_scratch_u16` / the join's source-plane scratch) MAY keep
/// already-grown capacity, which a retry reuses correctly and the sink frees on
/// drop. Join / freeze / output-atomic, not memory-exact — the same contract as
/// the 8-bit twin. Nothing is fallible after the first feed.
#[allow(clippy::too_many_arguments)]
pub(crate) fn yuv_planar16_process_native<const BITS: u32, const BE: bool>(
  plan: &ResamplePlan,
  native: &mut Option<std::boxed::Box<NativePlanarYuvU16>>,
  resample_outputs: &mut Option<super::FrozenOutputs>,
  rgb: &mut Option<&mut [u8]>,
  rgba: &mut Option<&mut [u8]>,
  rgb_u16: &mut Option<&mut [u16]>,
  rgba_u16: &mut Option<&mut [u16]>,
  luma: &mut Option<&mut [u8]>,
  luma_u16: &mut Option<&mut [u16]>,
  hsv: &mut Option<HsvFrameMut<'_>>,
  rgb_scratch: &mut std::vec::Vec<u8>,
  rgb_scratch_u16: &mut std::vec::Vec<u16>,
  y_row: &[u16],
  u_row: &[u16],
  v_row: &[u16],
  matrix: KernelMatrix,
  full_range: bool,
  idx: usize,
  w: usize,
  h: usize,
  chroma_vsub: usize,
  chroma_w: usize,
  build_chroma_plan: impl FnOnce() -> Result<ResamplePlan, ResampleError>,
  use_simd: bool,
) -> Result<(), MixedSinkerError> {
  const {
    assert!(
      BITS > 8 && BITS <= 16,
      "BITS must be in (8, 16] for high-bit non-4:2:0 planar YUV"
    )
  };
  let ow = plan.out_w();
  let need_luma = luma.is_some() || luma_u16.is_some();
  let need_color_u8 = rgb.is_some() || rgba.is_some() || hsv.is_some();
  let need_color_u16 = rgb_u16.is_some() || rgba_u16.is_some();
  let need_color = need_color_u8 || need_color_u16;

  // Compare-only pre-feed rejection preflight (no-output short-circuit,
  // first-row out-of-sequence, output-set compare, post-compare sequence) ahead
  // of any fallible allocation — the check-only twin
  // [`native_planar_hb_preflight_check_only`]. It does NOT commit the output-set
  // freeze; that commit is deferred below until every pre-feed allocation has
  // succeeded, so a first-row / rebuild-row failure leaves the cached join and
  // `resample_outputs` untouched. The direct planar callers reach this as their
  // only preflight; the semi-planar 4:2:2 wrapper runs the identical compare
  // first and the double-run is idempotent. `Ok(false)` is the no-output no-op.
  if !native_planar_hb_preflight_check_only(
    native,
    resample_outputs,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    luma,
    luma_u16,
    hsv,
    idx,
    need_luma,
    need_color,
  )? {
    return Ok(());
  }

  // A fresh join is needed on a first-ever build (`None`) or a colour-capability
  // change (the cached join's chroma half no longer matches `need_color` —
  // outputs attached / detached since the previous frame; the frozen check pins
  // them WITHIN a frame, not across frames). Build the replacement into a LOCAL
  // and do NOT clear the field yet: the cached join stays intact until every
  // pre-feed allocation has succeeded, so a build / box refusal leaves it, the
  // `resample_outputs` freeze, and the output frame untouched and the row
  // retryable.
  let rebuild = native.is_none()
    || native
      .as_ref()
      .is_some_and(|join| join.chroma.is_some() != need_color);
  let mut new_join = if rebuild {
    // Build the join (its de-interleave scratch allocates recoverably) and box it
    // recoverably (`try_box`, not `Box::new` — the latter aborts on OOM).
    let join = NativePlanarYuvU16::new(
      plan,
      build_chroma_plan,
      chroma_vsub,
      chroma_w,
      w,
      h,
      need_color,
    )?;
    Some(try_box(join).map_err(|_| {
      MixedSinkerError::Resample(ResampleError::AllocationFailed(PlanGeometry::new(
        w,
        h,
        plan.out_w(),
        plan.out_h(),
      )))
    })?)
  } else {
    None
  };

  // Colour OUTPUT scratch at output width (one binned row converts here before
  // fanning to the caller buffers). Both grows are fallible and precede the
  // commit, so a failure here (e.g. the u16 grow after the u8 grow already
  // succeeded) leaves the join / freeze / output untouched and the row retryable;
  // the already-grown persistent scratch simply keeps its capacity for the retry.
  if need_color_u8 {
    let row_bytes =
      ow.checked_mul(3)
        .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
          ow,
          plan.out_h(),
          3,
        )))?;
    if rgb_scratch.len() < row_bytes {
      rgb_scratch
        .try_reserve_exact(row_bytes - rgb_scratch.len())
        .map_err(|_| {
          MixedSinkerError::Resample(ResampleError::AllocationFailed(PlanGeometry::new(
            w,
            h,
            plan.out_w(),
            plan.out_h(),
          )))
        })?;
      rgb_scratch.resize(row_bytes, 0);
    }
  }
  if need_color_u16 {
    let row_elems =
      ow.checked_mul(3)
        .ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
          ow,
          plan.out_h(),
          3,
        )))?;
    if rgb_scratch_u16.len() < row_elems {
      #[cfg(all(test, feature = "std", feature = "yuv-planar"))]
      if FORCE_PLANAR_HB_NATIVE_RGB_U16_SCRATCH_FAILURE.with(|f| f.take()) {
        return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
          PlanGeometry::new(w, h, plan.out_w(), plan.out_h()),
        )));
      }
      rgb_scratch_u16
        .try_reserve_exact(row_elems - rgb_scratch_u16.len())
        .map_err(|_| {
          MixedSinkerError::Resample(ResampleError::AllocationFailed(PlanGeometry::new(
            w,
            h,
            plan.out_w(),
            plan.out_h(),
          )))
        })?;
      rgb_scratch_u16.resize(row_elems, 0);
    }
  }

  // Source-width de-interleave scratch (wire → host-native), grown after the
  // colour-output scratch. Its y_src / u_src / v_src grows carry the test
  // failpoint (a skip counter, so a test can fail a LATER source grow). Grown on
  // the rebuilt LOCAL join if rebuilding, else the cached join — all fallible,
  // all BEFORE the commit, so a failure at any of them leaves the join / freeze /
  // output untouched and the row retryable (an earlier source scratch keeps its
  // grown capacity). The chroma scratch is grown only on chroma-bearing rows,
  // exactly where the join reads chroma.
  let feed_chroma;
  {
    let target: &mut NativePlanarYuvU16 = match new_join.as_deref_mut() {
      Some(join) => join,
      None => native.as_deref_mut().expect("reuse implies a cached join"),
    };
    grow_src_scratch(&mut target.y_src, w, w, h, plan)?;
    feed_chroma = target.chroma.is_some() && idx.is_multiple_of(target.chroma_vsub);
    if feed_chroma {
      // Split-borrow: grow the two chroma scratches without holding `target`
      // immutably across the call.
      let cw = target.chroma_w;
      let chroma = target.chroma.as_mut().expect("feed_chroma implies Some");
      grow_src_scratch(&mut chroma.u_src, cw, w, h, plan)?;
      grow_src_scratch(&mut chroma.v_src, cw, w, h, plan)?;
    }
  }

  // Every pre-feed allocation succeeded — COMMIT: install the rebuilt join
  // (dropping the old) then freeze the output set. A build only reaches here on
  // the first output-bearing row (its `resample_outputs` is still `None`), so the
  // freeze just stores the snapshot; a reuse row builds nothing and re-freezes
  // nothing new. The native-depth u16 colour outputs are frozen as present (the
  // 8-bit twin freezes them absent).
  if let Some(boxed) = new_join {
    *native = Some(boxed);
  }
  frozen_outputs_check(
    resample_outputs,
    luma,
    luma_u16,
    rgb,
    rgba,
    rgb_u16,
    rgba_u16,
    &None,
    &None,
    &None,
    &None,
    &None,
    hsv,
    &None,
    idx,
  )?;
  let join = native.as_mut().expect("built or reused in the preflight");
  join.check_sequence(idx)?;

  // De-interleave the wire planes into host-native scratch. Everything past
  // this point is infallible.
  deinterleave_y_high_bit_masked::<BITS, BE>(y_row, &mut join.y_src, w);
  if feed_chroma {
    let cw = join.chroma_w;
    let chroma = join.chroma.as_mut().expect("feed_chroma implies Some");
    // Mask BEFORE the AreaStream bins these: the native tier averages the
    // source chroma, and the colour kernel's per-pixel extract_hb would then
    // mask only the binned result. `avg(dirty) & mask` can differ from
    // `avg(dirty & mask)` (e.g. 10-bit 0 and 0x400 average to 0x200, which
    // survives the mask, vs per-sample 0), so dirty bits must be cleared per
    // source sample here, not after binning.
    deinterleave_y_high_bit_masked::<BITS, BE>(u_row, &mut chroma.u_src, cw);
    deinterleave_y_high_bit_masked::<BITS, BE>(v_row, &mut chroma.v_src, cw);
  }

  // Feed the planes into their streams. The Y plane bins every row; the chroma
  // planes only on `idx % chroma_vsub == 0` rows (`cidx = idx / chroma_vsub`).
  let NativePlanarYuvU16 {
    y,
    y_src,
    y_stage,
    chroma,
    chroma_w,
    chroma_vsub,
    staged,
    next_emit,
    // Cache keys read only at build / the native call site; the feed loop
    // ignores them.
    chroma_centered: _,
    chroma_bottom: _,
    chroma_top: _,
  } = &mut **join;
  y.feed_row(idx, &y_src[..w], use_simd, |oy, out_row| {
    let slot = oy & 1;
    y_stage[slot * ow..slot * ow + ow].copy_from_slice(out_row);
    staged[0][slot] = true;
  })?;
  if let Some(c) = chroma.as_mut()
    && idx.is_multiple_of(*chroma_vsub)
  {
    let cidx = idx / *chroma_vsub;
    let cw = *chroma_w;
    let NativePlanarChromaU16 {
      u,
      v,
      u_src,
      v_src,
      u_stage,
      v_stage,
    } = c;
    u.feed_row(cidx, &u_src[..cw], use_simd, |oy, out_row| {
      let slot = oy & 1;
      u_stage[slot * ow..slot * ow + ow].copy_from_slice(out_row);
      staged[1][slot] = true;
    })?;
    v.feed_row(cidx, &v_src[..cw], use_simd, |oy, out_row| {
      let slot = oy & 1;
      v_stage[slot * ow..slot * ow + ow].copy_from_slice(out_row);
      staged[2][slot] = true;
    })?;
  }

  // Drain every output row whose participating planes are staged.
  while *next_emit < plan.out_h() {
    let slot = *next_emit & 1;
    let chroma_ready = match chroma.as_ref() {
      Some(_) => staged[1][slot] && staged[2][slot],
      None => true,
    };
    if !(staged[0][slot] && chroma_ready) {
      break;
    }
    let oy = *next_emit;
    let y_out = &y_stage[slot * ow..slot * ow + ow];

    if need_luma {
      // Clamp to the native max before EITHER luma emit: an overrange binned Y
      // (from out-of-gamut input whose high bits exceed BITS) must saturate, not
      // wrap — the row-stage luma path narrows / passes through the clamped u16.
      let native_max: u16 = ((1u32 << BITS) - 1) as u16;
      // u8 luma: the clamped binned Y narrowed `>> (BITS - 8)`.
      if let Some(buf) = luma.as_deref_mut() {
        for (dst, &src) in buf[oy * ow..(oy + 1) * ow].iter_mut().zip(y_out) {
          *dst = (src.min(native_max) >> (BITS - 8)) as u8;
        }
      }
      // Native-depth luma_u16: the SAME clamped binned Y, host-native u16, NOT
      // narrowed — keeping `luma_u16 <= (1 << BITS) - 1` (the native-depth
      // contract). The packed Y2xx family threads its real buffer here; the
      // planar / semi-planar families pass `&mut None` and skip this.
      if let Some(buf) = luma_u16.as_deref_mut() {
        for (dst, &src) in buf[oy * ow..(oy + 1) * ow].iter_mut().zip(y_out) {
          *dst = src.min(native_max);
        }
      }
    }

    if let Some(c) = chroma.as_ref() {
      let u_out = &c.u_stage[slot * ow..slot * ow + ow];
      let v_out = &c.v_stage[slot * ow..slot * ow + ow];

      // Native-depth u16 colour — its OWN independent kernel, never a
      // narrowing of the u8 colour (the #37 contract). The staged Y / U / V
      // are host-native, so `BE = HOST_NATIVE_BE`.
      if need_color_u16 {
        let out_rgb = &mut rgb_scratch_u16[..ow * 3];
        emit_rgb_u16::<BITS>(
          y_out, u_out, v_out, out_rgb, ow, matrix, full_range, use_simd,
        );
        if let Some(buf) = rgb_u16.as_deref_mut() {
          buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(out_rgb);
        }
        if let Some(buf) = rgba_u16.as_deref_mut() {
          expand_rgb_u16_to_rgba_u16_row::<BITS>(
            out_rgb,
            &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow],
            ow,
          );
        }
      }

      // u8 colour — the u16-INPUT → u8-OUTPUT 4:4:4 kernel (the same one the
      // row-stage 4:4:4 high-bit path uses), independent of the u16 colour.
      if need_color_u8 {
        let out_rgb = &mut rgb_scratch[..ow * 3];
        emit_rgb_u8::<BITS>(
          y_out, u_out, v_out, out_rgb, ow, matrix, full_range, use_simd,
        );
        if let Some(buf) = rgb.as_deref_mut() {
          buf[oy * 3 * ow..(oy + 1) * 3 * ow].copy_from_slice(out_rgb);
        }
        if let Some(hsv) = hsv.as_mut() {
          let (hp, sp, vp) = hsv.hsv();
          rgb_to_hsv_row(
            out_rgb,
            &mut hp[oy * ow..(oy + 1) * ow],
            &mut sp[oy * ow..(oy + 1) * ow],
            &mut vp[oy * ow..(oy + 1) * ow],
            ow,
            use_simd,
          );
        }
        if let Some(buf) = rgba.as_deref_mut() {
          expand_rgb_to_rgba_row(out_rgb, &mut buf[oy * 4 * ow..(oy + 1) * 4 * ow], ow);
        }
      }
    }
    staged[0][slot] = false;
    staged[1][slot] = false;
    staged[2][slot] = false;
    *next_emit += 1;
  }
  Ok(())
}

/// u16-output 4:4:4 conversion at output width — the staged Y / U / V are
/// host-native, so `big_endian = HOST_NATIVE_BE`. Dispatches on `BITS` to the
/// per-format wrapper, which pins the generic 4:4:4 u16 kernel to a supported
/// `BITS` (9/10/12/14 → the `i32` `yuv_444p_n_to_rgb_u16_row` family; 16 → the
/// dedicated `i64`-chroma `yuv444p16_to_rgb_u16_row`).
///
/// A runtime `if BITS == 16 { dedicated } else { generic::<BITS> }` will not
/// do: a const-generic `if` still MONOMORPHIZES the `else` arm at `BITS = 16`,
/// where the generic kernel const-asserts against 16. A `match` over the four
/// CONCRETE (non-generic) per-format wrappers calls only the live one and
/// never instantiates the invalid `::<16>` form. Identical to the 4:2:0
/// high-bit native's `emit_rgb_u16`.
#[cfg_attr(not(tarpaulin), inline(always))]
#[allow(clippy::too_many_arguments)]
fn emit_rgb_u16<const BITS: u32>(
  y: &[u16],
  u: &[u16],
  v: &[u16],
  rgb_out: &mut [u16],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
  use_simd: bool,
) {
  match BITS {
    9 => yuv444p9_to_rgb_u16_row_endian(
      y,
      u,
      v,
      rgb_out,
      width,
      matrix,
      full_range,
      use_simd,
      HOST_NATIVE_BE,
    ),
    10 => yuv444p10_to_rgb_u16_row_endian(
      y,
      u,
      v,
      rgb_out,
      width,
      matrix,
      full_range,
      use_simd,
      HOST_NATIVE_BE,
    ),
    12 => yuv444p12_to_rgb_u16_row_endian(
      y,
      u,
      v,
      rgb_out,
      width,
      matrix,
      full_range,
      use_simd,
      HOST_NATIVE_BE,
    ),
    14 => yuv444p14_to_rgb_u16_row_endian(
      y,
      u,
      v,
      rgb_out,
      width,
      matrix,
      full_range,
      use_simd,
      HOST_NATIVE_BE,
    ),
    16 => yuv444p16_to_rgb_u16_row_endian(
      y,
      u,
      v,
      rgb_out,
      width,
      matrix,
      full_range,
      use_simd,
      HOST_NATIVE_BE,
    ),
    _ => unreachable!("BITS pinned to 9/10/12/14/16 by the call sites"),
  }
}

/// u8-output 4:4:4 conversion at output width (the u16-INPUT → u8-OUTPUT kernel
/// — the SAME one the row-stage 4:4:4 high-bit path uses). Same per-format
/// dispatch + `big_endian = HOST_NATIVE_BE` rationale as [`emit_rgb_u16`].
#[cfg_attr(not(tarpaulin), inline(always))]
#[allow(clippy::too_many_arguments)]
fn emit_rgb_u8<const BITS: u32>(
  y: &[u16],
  u: &[u16],
  v: &[u16],
  rgb_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
  use_simd: bool,
) {
  match BITS {
    9 => yuv444p9_to_rgb_row_endian(
      y,
      u,
      v,
      rgb_out,
      width,
      matrix,
      full_range,
      use_simd,
      HOST_NATIVE_BE,
    ),
    10 => yuv444p10_to_rgb_row_endian(
      y,
      u,
      v,
      rgb_out,
      width,
      matrix,
      full_range,
      use_simd,
      HOST_NATIVE_BE,
    ),
    12 => yuv444p12_to_rgb_row_endian(
      y,
      u,
      v,
      rgb_out,
      width,
      matrix,
      full_range,
      use_simd,
      HOST_NATIVE_BE,
    ),
    14 => yuv444p14_to_rgb_row_endian(
      y,
      u,
      v,
      rgb_out,
      width,
      matrix,
      full_range,
      use_simd,
      HOST_NATIVE_BE,
    ),
    16 => yuv444p16_to_rgb_row_endian(
      y,
      u,
      v,
      rgb_out,
      width,
      matrix,
      full_range,
      use_simd,
      HOST_NATIVE_BE,
    ),
    _ => unreachable!("BITS pinned to 9/10/12/14/16 by the call sites"),
  }
}