colconv 0.1.0

SIMD-dispatched color-conversion kernels covering the FFmpeg AVPixelFormat space, with a Sink-based API so consumers pick which derived outputs (RGB / Luma / HSV / custom) they want without paying for the ones they don't.
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
use core::arch::aarch64::*;

use super::endian::load_endian_u32x4;
use crate::row::scalar;

// ===== BGR ↔ RGB byte swap ==============================================

/// Swaps the outer two channels of each packed 3‑byte triple. Drives
/// both `bgr_to_rgb_row` and `rgb_to_bgr_row` since the transformation
/// is self‑inverse.
///
/// NEON makes this almost free: `vld3q_u8` deinterleaves 16 pixels into
/// three channel vectors `(ch0, ch1, ch2)`, and `vst3q_u8` re‑interleaves
/// them — passing the deinterleaved vectors back in reversed order
/// `(ch2, ch1, ch0)` swaps the outer channels in a single store.
///
/// # Safety
///
/// 1. NEON must be available (same obligation as the other NEON kernels).
/// 2. `input.len() >= 3 * width`.
/// 3. `output.len() >= 3 * width`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn bgr_rgb_swap_row(input: &[u8], output: &mut [u8], width: usize) {
  debug_assert!(input.len() >= width * 3, "input row too short");
  debug_assert!(output.len() >= width * 3, "output row too short");

  // SAFETY: NEON availability is the caller's obligation per the
  // `# Safety` section. All pointer adds are bounded by the
  // `while x + 16 <= width` condition and the caller‑promised
  // slice lengths.
  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      let triple = vld3q_u8(input.as_ptr().add(x * 3));
      let swapped = uint8x16x3_t(triple.2, triple.1, triple.0);
      vst3q_u8(output.as_mut_ptr().add(x * 3), swapped);
      x += 16;
    }
    if x < width {
      scalar::bgr_rgb_swap_row(
        &input[x * 3..width * 3],
        &mut output[x * 3..width * 3],
        width - x,
      );
    }
  }
}

// ===== Packed-RGBA shuffles (Ship 9b) ====================================

/// Drops the alpha byte from packed `R, G, B, A` input, producing
/// packed `R, G, B` output. NEON makes this nearly free: `vld4q_u8`
/// deinterleaves 16 RGBA pixels into four u8x16 channel vectors
/// `(R, G, B, A)`, and `vst3q_u8` re-interleaves three of them as
/// RGB triples — the alpha vector is simply discarded.
///
/// # Safety
///
/// 1. NEON must be available (caller obligation, same as the other
///    NEON kernels).
/// 2. `rgba.len() >= 4 * width`.
/// 3. `rgb_out.len() >= 3 * width`.
/// 4. `rgba` / `rgb_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgba_to_rgb_row(rgba: &[u8], rgb_out: &mut [u8], width: usize) {
  debug_assert!(rgba.len() >= width * 4, "rgba row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  // SAFETY: NEON availability is the caller's obligation. All pointer
  // adds are bounded by the `while x + 16 <= width` condition and the
  // caller-promised slice lengths.
  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      let quad = vld4q_u8(rgba.as_ptr().add(x * 4));
      let triple = uint8x16x3_t(quad.0, quad.1, quad.2);
      vst3q_u8(rgb_out.as_mut_ptr().add(x * 3), triple);
      x += 16;
    }
    if x < width {
      scalar::rgba_to_rgb_row(
        &rgba[x * 4..width * 4],
        &mut rgb_out[x * 3..width * 3],
        width - x,
      );
    }
  }
}

/// Swaps R↔B in packed `B, G, R, A` input, producing packed
/// `R, G, B, A` (alpha lane preserved). `vld4q_u8` deinterleaves
/// 16 BGRA pixels into channel vectors `(B, G, R, A)`; `vst4q_u8`
/// interleaves them back as `(R, G, B, A)` simply by reordering
/// the channel-vector tuple.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `bgra.len() >= 4 * width`.
/// 3. `rgba_out.len() >= 4 * width`.
/// 4. `bgra` / `rgba_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn bgra_to_rgba_row(bgra: &[u8], rgba_out: &mut [u8], width: usize) {
  debug_assert!(bgra.len() >= width * 4, "bgra row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  // SAFETY: NEON availability is the caller's obligation.
  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      let quad = vld4q_u8(bgra.as_ptr().add(x * 4));
      let swapped = uint8x16x4_t(quad.2, quad.1, quad.0, quad.3);
      vst4q_u8(rgba_out.as_mut_ptr().add(x * 4), swapped);
      x += 16;
    }
    if x < width {
      scalar::bgra_to_rgba_row(
        &bgra[x * 4..width * 4],
        &mut rgba_out[x * 4..width * 4],
        width - x,
      );
    }
  }
}

/// Swaps R↔B and drops alpha from packed `B, G, R, A` input,
/// producing packed `R, G, B`. Combines [`rgba_to_rgb_row`]'s
/// alpha drop with [`bgra_to_rgba_row`]'s channel swap in one pass:
/// `vld4q_u8` → reorder vectors → `vst3q_u8` (alpha discarded).
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `bgra.len() >= 4 * width`.
/// 3. `rgb_out.len() >= 3 * width`.
/// 4. `bgra` / `rgb_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn bgra_to_rgb_row(bgra: &[u8], rgb_out: &mut [u8], width: usize) {
  debug_assert!(bgra.len() >= width * 4, "bgra row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  // SAFETY: NEON availability is the caller's obligation.
  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      let quad = vld4q_u8(bgra.as_ptr().add(x * 4));
      let triple = uint8x16x3_t(quad.2, quad.1, quad.0);
      vst3q_u8(rgb_out.as_mut_ptr().add(x * 3), triple);
      x += 16;
    }
    if x < width {
      scalar::bgra_to_rgb_row(
        &bgra[x * 4..width * 4],
        &mut rgb_out[x * 3..width * 3],
        width - x,
      );
    }
  }
}

// ===== Leading-alpha shuffles (Ship 9c) ==================================

/// Drops the leading alpha byte from packed `A, R, G, B` input,
/// producing packed `R, G, B`. `vld4q_u8` deinterleaves 16 ARGB
/// pixels into channel vectors `(A, R, G, B)`; `vst3q_u8` interleaves
/// channels 1..3 (R, G, B) — the alpha vector is discarded.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `argb.len() >= 4 * width`.
/// 3. `rgb_out.len() >= 3 * width`.
/// 4. `argb` / `rgb_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn argb_to_rgb_row(argb: &[u8], rgb_out: &mut [u8], width: usize) {
  debug_assert!(argb.len() >= width * 4, "argb row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      let quad = vld4q_u8(argb.as_ptr().add(x * 4));
      let triple = uint8x16x3_t(quad.1, quad.2, quad.3);
      vst3q_u8(rgb_out.as_mut_ptr().add(x * 3), triple);
      x += 16;
    }
    if x < width {
      scalar::argb_to_rgb_row(
        &argb[x * 4..width * 4],
        &mut rgb_out[x * 3..width * 3],
        width - x,
      );
    }
  }
}

/// Swaps R↔B and drops leading alpha from packed `A, B, G, R`
/// input, producing packed `R, G, B`. `vld4q_u8` deinterleaves into
/// `(A, B, G, R)` channel vectors; `vst3q_u8` interleaves
/// `(channel 3, channel 2, channel 1)` = `(R, G, B)`.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `abgr.len() >= 4 * width`.
/// 3. `rgb_out.len() >= 3 * width`.
/// 4. `abgr` / `rgb_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn abgr_to_rgb_row(abgr: &[u8], rgb_out: &mut [u8], width: usize) {
  debug_assert!(abgr.len() >= width * 4, "abgr row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      let quad = vld4q_u8(abgr.as_ptr().add(x * 4));
      let triple = uint8x16x3_t(quad.3, quad.2, quad.1);
      vst3q_u8(rgb_out.as_mut_ptr().add(x * 3), triple);
      x += 16;
    }
    if x < width {
      scalar::abgr_to_rgb_row(
        &abgr[x * 4..width * 4],
        &mut rgb_out[x * 3..width * 3],
        width - x,
      );
    }
  }
}

/// Rotates leading alpha to trailing position in packed `A, R, G, B`
/// input, producing packed `R, G, B, A`. `vld4q_u8` deinterleaves
/// into `(A, R, G, B)` channel vectors; `vst4q_u8` interleaves
/// `(R, G, B, A)` = `(channel 1, 2, 3, 0)`.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `argb.len() >= 4 * width`.
/// 3. `rgba_out.len() >= 4 * width`.
/// 4. `argb` / `rgba_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn argb_to_rgba_row(argb: &[u8], rgba_out: &mut [u8], width: usize) {
  debug_assert!(argb.len() >= width * 4, "argb row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      let quad = vld4q_u8(argb.as_ptr().add(x * 4));
      let rotated = uint8x16x4_t(quad.1, quad.2, quad.3, quad.0);
      vst4q_u8(rgba_out.as_mut_ptr().add(x * 4), rotated);
      x += 16;
    }
    if x < width {
      scalar::argb_to_rgba_row(
        &argb[x * 4..width * 4],
        &mut rgba_out[x * 4..width * 4],
        width - x,
      );
    }
  }
}

/// Reverses byte order in packed `A, B, G, R` input, producing
/// packed `R, G, B, A`. `vld4q_u8` deinterleaves into `(A, B, G, R)`
/// channel vectors; `vst4q_u8` interleaves them in reverse
/// `(R, G, B, A)` = `(channel 3, 2, 1, 0)`.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `abgr.len() >= 4 * width`.
/// 3. `rgba_out.len() >= 4 * width`.
/// 4. `abgr` / `rgba_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn abgr_to_rgba_row(abgr: &[u8], rgba_out: &mut [u8], width: usize) {
  debug_assert!(abgr.len() >= width * 4, "abgr row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      let quad = vld4q_u8(abgr.as_ptr().add(x * 4));
      let reversed = uint8x16x4_t(quad.3, quad.2, quad.1, quad.0);
      vst4q_u8(rgba_out.as_mut_ptr().add(x * 4), reversed);
      x += 16;
    }
    if x < width {
      scalar::abgr_to_rgba_row(
        &abgr[x * 4..width * 4],
        &mut rgba_out[x * 4..width * 4],
        width - x,
      );
    }
  }
}

// ===== Padding-byte to RGBA shuffles (Ship 9d) ===========================

/// Drops the leading padding byte from packed `X, R, G, B` input,
/// producing packed `R, G, B, A` with `A = 0xFF`. `vld4q_u8`
/// deinterleaves into channel vectors `(X, R, G, B)`; we reinterleave
/// `(R, G, B, splat(0xFF))` via `vst4q_u8`.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `xrgb.len() >= 4 * width`.
/// 3. `rgba_out.len() >= 4 * width`.
/// 4. `xrgb` / `rgba_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn xrgb_to_rgba_row(xrgb: &[u8], rgba_out: &mut [u8], width: usize) {
  debug_assert!(xrgb.len() >= width * 4, "xrgb row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let alpha = vdupq_n_u8(0xFF);
    let mut x = 0usize;
    while x + 16 <= width {
      let quad = vld4q_u8(xrgb.as_ptr().add(x * 4));
      let out = uint8x16x4_t(quad.1, quad.2, quad.3, alpha);
      vst4q_u8(rgba_out.as_mut_ptr().add(x * 4), out);
      x += 16;
    }
    if x < width {
      scalar::xrgb_to_rgba_row(
        &xrgb[x * 4..width * 4],
        &mut rgba_out[x * 4..width * 4],
        width - x,
      );
    }
  }
}

/// Drops the trailing padding byte from packed `R, G, B, X` input,
/// producing packed `R, G, B, A` with `A = 0xFF`.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `rgbx.len() >= 4 * width`.
/// 3. `rgba_out.len() >= 4 * width`.
/// 4. `rgbx` / `rgba_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbx_to_rgba_row(rgbx: &[u8], rgba_out: &mut [u8], width: usize) {
  debug_assert!(rgbx.len() >= width * 4, "rgbx row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let alpha = vdupq_n_u8(0xFF);
    let mut x = 0usize;
    while x + 16 <= width {
      let quad = vld4q_u8(rgbx.as_ptr().add(x * 4));
      let out = uint8x16x4_t(quad.0, quad.1, quad.2, alpha);
      vst4q_u8(rgba_out.as_mut_ptr().add(x * 4), out);
      x += 16;
    }
    if x < width {
      scalar::rgbx_to_rgba_row(
        &rgbx[x * 4..width * 4],
        &mut rgba_out[x * 4..width * 4],
        width - x,
      );
    }
  }
}

/// Reverses RGB and drops leading padding from packed `X, B, G, R`
/// input, producing packed `R, G, B, A` with `A = 0xFF`.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `xbgr.len() >= 4 * width`.
/// 3. `rgba_out.len() >= 4 * width`.
/// 4. `xbgr` / `rgba_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn xbgr_to_rgba_row(xbgr: &[u8], rgba_out: &mut [u8], width: usize) {
  debug_assert!(xbgr.len() >= width * 4, "xbgr row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let alpha = vdupq_n_u8(0xFF);
    let mut x = 0usize;
    while x + 16 <= width {
      let quad = vld4q_u8(xbgr.as_ptr().add(x * 4));
      let out = uint8x16x4_t(quad.3, quad.2, quad.1, alpha);
      vst4q_u8(rgba_out.as_mut_ptr().add(x * 4), out);
      x += 16;
    }
    if x < width {
      scalar::xbgr_to_rgba_row(
        &xbgr[x * 4..width * 4],
        &mut rgba_out[x * 4..width * 4],
        width - x,
      );
    }
  }
}

/// Reverses RGB and drops trailing padding from packed `B, G, R, X`
/// input, producing packed `R, G, B, A` with `A = 0xFF`.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `bgrx.len() >= 4 * width`.
/// 3. `rgba_out.len() >= 4 * width`.
/// 4. `bgrx` / `rgba_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn bgrx_to_rgba_row(bgrx: &[u8], rgba_out: &mut [u8], width: usize) {
  debug_assert!(bgrx.len() >= width * 4, "bgrx row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let alpha = vdupq_n_u8(0xFF);
    let mut x = 0usize;
    while x + 16 <= width {
      let quad = vld4q_u8(bgrx.as_ptr().add(x * 4));
      let out = uint8x16x4_t(quad.2, quad.1, quad.0, alpha);
      vst4q_u8(rgba_out.as_mut_ptr().add(x * 4), out);
      x += 16;
    }
    if x < width {
      scalar::bgrx_to_rgba_row(
        &bgrx[x * 4..width * 4],
        &mut rgba_out[x * 4..width * 4],
        width - x,
      );
    }
  }
}

// ===== 10-bit packed RGB shuffles (Ship 9e) ==============================
//
// Endian-aware u32x4 loads: callers pass `<const BE: bool>` straight to
// `load_endian_u32x4`, which compiles down to a plain `vld1q_u32` when
// `BE == HOST_NATIVE_BE` and inserts a `vrev32q_u8` byte-swap
// otherwise. After the load, the u32 lanes are in host-native order so
// the shift-and-mask field extraction below works for both LE and BE
// inputs without further branching.

/// Extracts a 10-bit channel as a `u8` (top 8 bits) from each of 4
/// `u32` pixels in a `uint32x4_t`. Returns 4 `u16` lanes packed in a
/// `uint16x4_t`. The dropped low 2 bits of each 10-bit value match
/// the scalar `>> 2` truncation.
#[inline(always)]
unsafe fn x2_extract_10bit_u8_lane(pix: uint32x4_t, shift: i32) -> uint16x4_t {
  unsafe {
    // Shift down then narrow + saturate. Values are bounded to
    // `[0, 255]` so saturation never triggers.
    let shifted = match shift {
      22 => vshrq_n_u32(pix, 22),
      12 => vshrq_n_u32(pix, 12),
      2 => vshrq_n_u32(pix, 2),
      _ => unreachable!(),
    };
    let mask = vdupq_n_u32(0xFF);
    vqmovn_u32(vandq_u32(shifted, mask))
  }
}

/// Extracts a 10-bit channel into the low 10 bits of a `u16`. 4
/// pixels in → 4 `u16` lanes out.
#[inline(always)]
unsafe fn x2_extract_10bit_u16_lane(pix: uint32x4_t, shift: i32) -> uint16x4_t {
  unsafe {
    let shifted = match shift {
      20 => vshrq_n_u32(pix, 20),
      10 => vshrq_n_u32(pix, 10),
      0 => pix,
      _ => unreachable!(),
    };
    let mask = vdupq_n_u32(0x3FF);
    vqmovn_u32(vandq_u32(shifted, mask))
  }
}

/// NEON X2RGB10→RGB. 16 pixels per iteration: load 4 `u32x4`
/// vectors, extract R/G/B channels, narrow to `u8x16` per channel,
/// `vst3q_u8`.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `x2rgb10.len() >= 4 * width`; `rgb_out.len() >= 3 * width`.
/// 3. `x2rgb10` / `rgb_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn x2rgb10_to_rgb_row<const BE: bool>(
  x2rgb10: &[u8],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(x2rgb10.len() >= width * 4, "x2rgb10 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      let p0 = load_endian_u32x4::<BE>(x2rgb10.as_ptr().add(x * 4));
      let p1 = load_endian_u32x4::<BE>(x2rgb10.as_ptr().add(x * 4 + 16));
      let p2 = load_endian_u32x4::<BE>(x2rgb10.as_ptr().add(x * 4 + 32));
      let p3 = load_endian_u32x4::<BE>(x2rgb10.as_ptr().add(x * 4 + 48));

      // X2RGB10: R at >>22, G at >>12, B at >>2 (top 8 of 10-bit).
      let r_lo = vcombine_u16(
        x2_extract_10bit_u8_lane(p0, 22),
        x2_extract_10bit_u8_lane(p1, 22),
      );
      let r_hi = vcombine_u16(
        x2_extract_10bit_u8_lane(p2, 22),
        x2_extract_10bit_u8_lane(p3, 22),
      );
      let g_lo = vcombine_u16(
        x2_extract_10bit_u8_lane(p0, 12),
        x2_extract_10bit_u8_lane(p1, 12),
      );
      let g_hi = vcombine_u16(
        x2_extract_10bit_u8_lane(p2, 12),
        x2_extract_10bit_u8_lane(p3, 12),
      );
      let b_lo = vcombine_u16(
        x2_extract_10bit_u8_lane(p0, 2),
        x2_extract_10bit_u8_lane(p1, 2),
      );
      let b_hi = vcombine_u16(
        x2_extract_10bit_u8_lane(p2, 2),
        x2_extract_10bit_u8_lane(p3, 2),
      );

      let r = vcombine_u8(vqmovn_u16(r_lo), vqmovn_u16(r_hi));
      let g = vcombine_u8(vqmovn_u16(g_lo), vqmovn_u16(g_hi));
      let b = vcombine_u8(vqmovn_u16(b_lo), vqmovn_u16(b_hi));

      let rgb = uint8x16x3_t(r, g, b);
      vst3q_u8(rgb_out.as_mut_ptr().add(x * 3), rgb);

      x += 16;
    }
    if x < width {
      scalar::x2rgb10_to_rgb_row::<BE>(
        &x2rgb10[x * 4..width * 4],
        &mut rgb_out[x * 3..width * 3],
        width - x,
      );
    }
  }
}

/// NEON X2RGB10→RGBA. 16 pixels per iteration; alpha forced to
/// `0xFF` via `vdupq_n_u8`.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `x2rgb10.len() >= 4 * width`; `rgba_out.len() >= 4 * width`.
/// 3. `x2rgb10` / `rgba_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn x2rgb10_to_rgba_row<const BE: bool>(
  x2rgb10: &[u8],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(x2rgb10.len() >= width * 4, "x2rgb10 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let alpha = vdupq_n_u8(0xFF);
    let mut x = 0usize;
    while x + 16 <= width {
      let p0 = load_endian_u32x4::<BE>(x2rgb10.as_ptr().add(x * 4));
      let p1 = load_endian_u32x4::<BE>(x2rgb10.as_ptr().add(x * 4 + 16));
      let p2 = load_endian_u32x4::<BE>(x2rgb10.as_ptr().add(x * 4 + 32));
      let p3 = load_endian_u32x4::<BE>(x2rgb10.as_ptr().add(x * 4 + 48));

      let r_lo = vcombine_u16(
        x2_extract_10bit_u8_lane(p0, 22),
        x2_extract_10bit_u8_lane(p1, 22),
      );
      let r_hi = vcombine_u16(
        x2_extract_10bit_u8_lane(p2, 22),
        x2_extract_10bit_u8_lane(p3, 22),
      );
      let g_lo = vcombine_u16(
        x2_extract_10bit_u8_lane(p0, 12),
        x2_extract_10bit_u8_lane(p1, 12),
      );
      let g_hi = vcombine_u16(
        x2_extract_10bit_u8_lane(p2, 12),
        x2_extract_10bit_u8_lane(p3, 12),
      );
      let b_lo = vcombine_u16(
        x2_extract_10bit_u8_lane(p0, 2),
        x2_extract_10bit_u8_lane(p1, 2),
      );
      let b_hi = vcombine_u16(
        x2_extract_10bit_u8_lane(p2, 2),
        x2_extract_10bit_u8_lane(p3, 2),
      );

      let r = vcombine_u8(vqmovn_u16(r_lo), vqmovn_u16(r_hi));
      let g = vcombine_u8(vqmovn_u16(g_lo), vqmovn_u16(g_hi));
      let b = vcombine_u8(vqmovn_u16(b_lo), vqmovn_u16(b_hi));

      let rgba = uint8x16x4_t(r, g, b, alpha);
      vst4q_u8(rgba_out.as_mut_ptr().add(x * 4), rgba);

      x += 16;
    }
    if x < width {
      scalar::x2rgb10_to_rgba_row::<BE>(
        &x2rgb10[x * 4..width * 4],
        &mut rgba_out[x * 4..width * 4],
        width - x,
      );
    }
  }
}

/// NEON X2RGB10→u16 RGB native (10-bit, low-bit aligned). 8 pixels
/// per iteration.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `x2rgb10.len() >= 4 * width`; `rgb_out.len() >= 3 * width`.
/// 3. `x2rgb10` / `rgb_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn x2rgb10_to_rgb_u16_row<const BE: bool>(
  x2rgb10: &[u8],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(x2rgb10.len() >= width * 4, "x2rgb10 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let p0 = load_endian_u32x4::<BE>(x2rgb10.as_ptr().add(x * 4));
      let p1 = load_endian_u32x4::<BE>(x2rgb10.as_ptr().add(x * 4 + 16));

      // Channel low bit positions: R at 20, G at 10, B at 0.
      let r = vcombine_u16(
        x2_extract_10bit_u16_lane(p0, 20),
        x2_extract_10bit_u16_lane(p1, 20),
      );
      let g = vcombine_u16(
        x2_extract_10bit_u16_lane(p0, 10),
        x2_extract_10bit_u16_lane(p1, 10),
      );
      let b = vcombine_u16(
        x2_extract_10bit_u16_lane(p0, 0),
        x2_extract_10bit_u16_lane(p1, 0),
      );

      let rgb = uint16x8x3_t(r, g, b);
      vst3q_u16(rgb_out.as_mut_ptr().add(x * 3), rgb);

      x += 8;
    }
    if x < width {
      scalar::x2rgb10_to_rgb_u16_row::<BE>(
        &x2rgb10[x * 4..width * 4],
        &mut rgb_out[x * 3..width * 3],
        width - x,
      );
    }
  }
}

/// NEON X2BGR10→RGB. 16 pixels per iteration; same shape as
/// [`x2rgb10_to_rgb_row`] but channel shifts swapped (R at >>2,
/// B at >>22).
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn x2bgr10_to_rgb_row<const BE: bool>(
  x2bgr10: &[u8],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(x2bgr10.len() >= width * 4, "x2bgr10 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      let p0 = load_endian_u32x4::<BE>(x2bgr10.as_ptr().add(x * 4));
      let p1 = load_endian_u32x4::<BE>(x2bgr10.as_ptr().add(x * 4 + 16));
      let p2 = load_endian_u32x4::<BE>(x2bgr10.as_ptr().add(x * 4 + 32));
      let p3 = load_endian_u32x4::<BE>(x2bgr10.as_ptr().add(x * 4 + 48));

      let r_lo = vcombine_u16(
        x2_extract_10bit_u8_lane(p0, 2),
        x2_extract_10bit_u8_lane(p1, 2),
      );
      let r_hi = vcombine_u16(
        x2_extract_10bit_u8_lane(p2, 2),
        x2_extract_10bit_u8_lane(p3, 2),
      );
      let g_lo = vcombine_u16(
        x2_extract_10bit_u8_lane(p0, 12),
        x2_extract_10bit_u8_lane(p1, 12),
      );
      let g_hi = vcombine_u16(
        x2_extract_10bit_u8_lane(p2, 12),
        x2_extract_10bit_u8_lane(p3, 12),
      );
      let b_lo = vcombine_u16(
        x2_extract_10bit_u8_lane(p0, 22),
        x2_extract_10bit_u8_lane(p1, 22),
      );
      let b_hi = vcombine_u16(
        x2_extract_10bit_u8_lane(p2, 22),
        x2_extract_10bit_u8_lane(p3, 22),
      );

      let r = vcombine_u8(vqmovn_u16(r_lo), vqmovn_u16(r_hi));
      let g = vcombine_u8(vqmovn_u16(g_lo), vqmovn_u16(g_hi));
      let b = vcombine_u8(vqmovn_u16(b_lo), vqmovn_u16(b_hi));

      let rgb = uint8x16x3_t(r, g, b);
      vst3q_u8(rgb_out.as_mut_ptr().add(x * 3), rgb);

      x += 16;
    }
    if x < width {
      scalar::x2bgr10_to_rgb_row::<BE>(
        &x2bgr10[x * 4..width * 4],
        &mut rgb_out[x * 3..width * 3],
        width - x,
      );
    }
  }
}

/// NEON X2BGR10→RGBA. 16 pixels per iteration.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn x2bgr10_to_rgba_row<const BE: bool>(
  x2bgr10: &[u8],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(x2bgr10.len() >= width * 4, "x2bgr10 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let alpha = vdupq_n_u8(0xFF);
    let mut x = 0usize;
    while x + 16 <= width {
      let p0 = load_endian_u32x4::<BE>(x2bgr10.as_ptr().add(x * 4));
      let p1 = load_endian_u32x4::<BE>(x2bgr10.as_ptr().add(x * 4 + 16));
      let p2 = load_endian_u32x4::<BE>(x2bgr10.as_ptr().add(x * 4 + 32));
      let p3 = load_endian_u32x4::<BE>(x2bgr10.as_ptr().add(x * 4 + 48));

      let r_lo = vcombine_u16(
        x2_extract_10bit_u8_lane(p0, 2),
        x2_extract_10bit_u8_lane(p1, 2),
      );
      let r_hi = vcombine_u16(
        x2_extract_10bit_u8_lane(p2, 2),
        x2_extract_10bit_u8_lane(p3, 2),
      );
      let g_lo = vcombine_u16(
        x2_extract_10bit_u8_lane(p0, 12),
        x2_extract_10bit_u8_lane(p1, 12),
      );
      let g_hi = vcombine_u16(
        x2_extract_10bit_u8_lane(p2, 12),
        x2_extract_10bit_u8_lane(p3, 12),
      );
      let b_lo = vcombine_u16(
        x2_extract_10bit_u8_lane(p0, 22),
        x2_extract_10bit_u8_lane(p1, 22),
      );
      let b_hi = vcombine_u16(
        x2_extract_10bit_u8_lane(p2, 22),
        x2_extract_10bit_u8_lane(p3, 22),
      );

      let r = vcombine_u8(vqmovn_u16(r_lo), vqmovn_u16(r_hi));
      let g = vcombine_u8(vqmovn_u16(g_lo), vqmovn_u16(g_hi));
      let b = vcombine_u8(vqmovn_u16(b_lo), vqmovn_u16(b_hi));

      let rgba = uint8x16x4_t(r, g, b, alpha);
      vst4q_u8(rgba_out.as_mut_ptr().add(x * 4), rgba);

      x += 16;
    }
    if x < width {
      scalar::x2bgr10_to_rgba_row::<BE>(
        &x2bgr10[x * 4..width * 4],
        &mut rgba_out[x * 4..width * 4],
        width - x,
      );
    }
  }
}

/// NEON X2BGR10→u16 RGB native. 8 pixels per iteration.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn x2bgr10_to_rgb_u16_row<const BE: bool>(
  x2bgr10: &[u8],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(x2bgr10.len() >= width * 4, "x2bgr10 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let p0 = load_endian_u32x4::<BE>(x2bgr10.as_ptr().add(x * 4));
      let p1 = load_endian_u32x4::<BE>(x2bgr10.as_ptr().add(x * 4 + 16));

      // X2BGR10: R at low 10 bits, G at 10..19, B at 20..29.
      let r = vcombine_u16(
        x2_extract_10bit_u16_lane(p0, 0),
        x2_extract_10bit_u16_lane(p1, 0),
      );
      let g = vcombine_u16(
        x2_extract_10bit_u16_lane(p0, 10),
        x2_extract_10bit_u16_lane(p1, 10),
      );
      let b = vcombine_u16(
        x2_extract_10bit_u16_lane(p0, 20),
        x2_extract_10bit_u16_lane(p1, 20),
      );

      let rgb = uint16x8x3_t(r, g, b);
      vst3q_u16(rgb_out.as_mut_ptr().add(x * 3), rgb);

      x += 8;
    }
    if x < width {
      scalar::x2bgr10_to_rgb_u16_row::<BE>(
        &x2bgr10[x * 4..width * 4],
        &mut rgb_out[x * 3..width * 3],
        width - x,
      );
    }
  }
}