ferrisma-libwebp 0.1.0

A mechanical Rust port of libwebp, the WebP reference implementation
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
// libwebp v1.6.0, src/dsp/upsampling_sse2.c:
//
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the COPYING file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
// -----------------------------------------------------------------------------
//
// SSE2 version of YUV to RGB upsampling functions.
//
// Author: somnath@google.com (Somnath Banerjee)

//! Translation of `src/dsp/upsampling_sse2.c` from libwebp v1.6.0.
//!
//! Work package: WP18 (SIMD x86 decoder).
//!
//! The SSE2 fancy upsamplers and YUV 4:4:4 converters, behind
//! `webp_upsamplers`, `webp_get_line_pair_converter` and
//! `webp_yuv444_converters` of `src/dsp/upsampling.rs` (ADR 0008). Each
//! `#[target_feature]` function keeps the upstream name; each function that an
//! `Init` function assigns to a pointer has a safe `pub(crate)` entry point
//! named after it without the suffix. `WebPInitUpsamplers` and
//! `WebPInitYUV444Converters` run their SSE2 and SSE4.1 `Init` functions
//! independently, so the SSE4.1 assignments of `upsampling_sse41.rs`
//! override these whenever `features().sse4_1` holds.
//!
//! - `WebPInitUpsamplersSSE2` assigns `WebPUpsamplers[MODE_RGBA, MODE_rgbA]`:
//!   `upsample_rgba_line_pair`; `[MODE_BGRA, MODE_bgrA]`:
//!   `upsample_bgra_line_pair`; `[MODE_RGB]`: `upsample_rgb_line_pair`;
//!   `[MODE_BGR]`: `upsample_bgr_line_pair`; `[MODE_ARGB, MODE_Argb]`:
//!   `upsample_argb_line_pair`; `[MODE_RGB_565]`:
//!   `upsample_rgb565_line_pair`; `[MODE_RGBA_4444, MODE_rgbA_4444]`:
//!   `upsample_rgba4444_line_pair`. `webp_get_line_pair_converter`
//!   returns the BGRA or ARGB entry point, as `WebPGetLinePairConverter`
//!   returns the pointer after `WebPInitUpsamplers`.
//! - `WebPInitYUV444ConvertersSSE2` assigns `WebPYUV444Converters` for the
//!   same modes: `yuv444_to_rgba`, `yuv444_to_bgra`, `yuv444_to_rgb`,
//!   `yuv444_to_bgr`, `yuv444_to_argb`, `yuv444_to_rgb565`,
//!   `yuv444_to_rgba4444`.
//!
//! # Deviations from upstream
//!
//! 1. **Unaligned loads and stores** (the class of `common_sse2.rs`,
//!    deviation 1): `PACK_AND_STORE` stores with `_mm_store_si128` into the
//!    16-byte aligned cache `r_u`; here the stores are unaligned and the
//!    cache is a plain zeroed array of `14 * 32` bytes, the part of C's
//!    `14 * 32 + 15` bytes that the aligned pointer `r_u` uses. The offsets
//!    within it (`r_v = r_u + 32`, the bottom rows at `+ 64`, the temporary
//!    rows at `4 * 32`, `8 * 32` and `12 * 32`) are C's.
//! 2. **Macros.** `SSE2_UPSAMPLE_FUNC` and `YUV444_FUNC` generate one
//!    function per output format in C; here each is a generic
//!    `#[target_feature]` function over the per-pixel conversion and the
//!    32-pixel converter, which closures defined in the SSE2 functions pass
//!    (closures inherit their function's target features), and each format
//!    is a function that calls it, as `upsampling.rs` does for the C
//!    functions. `GET_M`, `PACK_AND_STORE`, `UPSAMPLE_32PIXELS`,
//!    `UPSAMPLE_LAST_BLOCK` and `CONVERT2RGB_32` are functions of the values
//!    the macros take from their scope.
//! 3. **Bottom row.** C's `bottom_y` and `bottom_dst`, NULL together, are
//!    one `Option` (WP4, deviation 5); the temporary bottom row of the last
//!    block exists when it is `Some`, as C's `tmp_bottom` is non-NULL then.
//! 4. **Panics on short buffers** come at the vector access, before or after
//!    other writes than in the scalar translation (`common_sse2.rs`,
//!    deviation 3).
//!
//! # Excluded undefined behavior
//!
//! None. The functions give the bytes of the C functions for every input;
//! like them they need `len >= 1`, as both read the first pixel
//! unconditionally.
//!
//! # Checks not applicable
//!
//! 1. **`WEBP_REDUCE_CSP`** is unset in the reference configuration, so every
//!    format is compiled and translated; `FANCY_UPSAMPLING` is defined by
//!    `dsp.h` (WP4).
//! 2. **`WebPInitUpsamplersSSE2` and `WebPInitYUV444ConvertersSSE2`** only
//!    assign pointers (ADR 0003); their assignments are the selection in the
//!    dispatch functions listed above. `CONVERT2RGB` is only `#undef`'d.
//! 3. **Differential probe.** The parity tests compare each function with the
//!    C translation it replaces (`tests/wp18_upsampling.rs`); the diagnostic
//!    `release-simd` oracle runs the WP4 probes against upstream's SSE2.

#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64 as arch;

use arch::{
    __m128i, _mm_and_si128, _mm_avg_epu8, _mm_or_si128, _mm_set1_epi8, _mm_sub_epi8,
    _mm_unpackhi_epi8, _mm_unpacklo_epi8, _mm_xor_si128,
};

use crate::dsp::common_sse2::{loadu_si128, storeu_si128};
use crate::dsp::upsampling::{
    WebPYUV444Converter, webp_yuv444_to_argb_c, webp_yuv444_to_bgr_c, webp_yuv444_to_bgra_c,
    webp_yuv444_to_rgb_c, webp_yuv444_to_rgb565_c, webp_yuv444_to_rgba_c,
    webp_yuv444_to_rgba4444_c,
};
use crate::dsp::yuv::{
    vp8_yuv_to_argb, vp8_yuv_to_bgr, vp8_yuv_to_bgra, vp8_yuv_to_rgb, vp8_yuv_to_rgb565,
    vp8_yuv_to_rgba, vp8_yuv_to_rgba4444,
};
use crate::dsp::yuv_sse2::{
    vp8_yuv_to_argb32_sse2, vp8_yuv_to_bgr32_sse2, vp8_yuv_to_bgra32_sse2, vp8_yuv_to_rgb32_sse2,
    vp8_yuv_to_rgb56532_sse2, vp8_yuv_to_rgba32_sse2, vp8_yuv_to_rgba444432_sse2,
};

/// C's conversion of an `int` sample in `[0, 255]` to the `uint8_t`
/// parameter of the per-pixel conversion.
#[expect(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    reason = "C converts the int sample to uint8_t"
)]
fn to_u8(v: i32) -> u8 {
    v as u8
}

// We compute (9*a + 3*b + 3*c + d + 8) / 16 as follows
// u = (9*a + 3*b + 3*c + d + 8) / 16
//   = (a + (a + 3*b + 3*c + d) / 8 + 1) / 2
//   = (a + m + 1) / 2
// where m = (a + 3*b + 3*c + d) / 8
//         = ((a + b + c + d) / 2 + b + c) / 4
//
// Let's say  k = (a + b + c + d) / 4.
// We can compute k as
// k = (s + t + 1) / 2 - ((a^d) | (b^c) | (s^t)) & 1
// where s = (a + d + 1) / 2 and t = (b + c + 1) / 2
//
// Then m can be written as
// m = (k + t + 1) / 2 - (((b^c) & (s^t)) | (k^t)) & 1

// upstream: src/dsp/upsampling_sse2.c GET_M
/// Computes `out = (k + in + 1) / 2 - ((ij & (s^t)) | (k^in)) & 1`
#[target_feature(enable = "sse2")]
#[inline]
fn get_m(k: __m128i, st: __m128i, one: __m128i, ij: __m128i, input: __m128i) -> __m128i {
    let tmp0 = _mm_avg_epu8(k, input); // (k + in + 1) / 2
    let tmp1 = _mm_and_si128(ij, st); // (ij) & (s^t)
    let tmp2 = _mm_xor_si128(k, input); // (k^in)
    let tmp3 = _mm_or_si128(tmp1, tmp2); // ((ij) & (s^t)) | (k^in)
    let tmp4 = _mm_and_si128(tmp3, one); // & 1 -> lsb_correction
    _mm_sub_epi8(tmp0, tmp4) // (k + in + 1) / 2 - lsb_correction
}

// upstream: src/dsp/upsampling_sse2.c PACK_AND_STORE
/// pack and store two alternating pixel rows
#[target_feature(enable = "sse2")]
#[inline]
fn pack_and_store(a: __m128i, b: __m128i, da: __m128i, db: __m128i, out: &mut [u8]) {
    let t_a = _mm_avg_epu8(a, da); // (9a + 3b + 3c +  d + 8) / 16
    let t_b = _mm_avg_epu8(b, db); // (3a + 9b +  c + 3d + 8) / 16
    let t_1 = _mm_unpacklo_epi8(t_a, t_b);
    let t_2 = _mm_unpackhi_epi8(t_a, t_b);
    storeu_si128(&mut out[0..], t_1);
    storeu_si128(&mut out[16..], t_2);
}

// upstream: src/dsp/upsampling_sse2.c UPSAMPLE_32PIXELS
/// Loads 17 pixels each from rows r1 and r2 and generates 32 pixels.
#[target_feature(enable = "sse2")]
#[inline]
#[expect(clippy::many_single_char_names, reason = "the C names of the samples")]
fn upsample_32pixels(r1: &[u8], r2: &[u8], out: &mut [u8]) {
    let one = _mm_set1_epi8(1);
    let a = loadu_si128(&r1[0..]);
    let b = loadu_si128(&r1[1..]);
    let c = loadu_si128(&r2[0..]);
    let d = loadu_si128(&r2[1..]);

    let s = _mm_avg_epu8(a, d); // s = (a + d + 1) / 2
    let t = _mm_avg_epu8(b, c); // t = (b + c + 1) / 2
    let st = _mm_xor_si128(s, t); // st = s^t

    let ad = _mm_xor_si128(a, d); // ad = a^d
    let bc = _mm_xor_si128(b, c); // bc = b^c

    let t1 = _mm_or_si128(ad, bc); // (a^d) | (b^c)
    let t2 = _mm_or_si128(t1, st); // (a^d) | (b^c) | (s^t)
    let t3 = _mm_and_si128(t2, one); // (a^d) | (b^c) | (s^t) & 1
    let t4 = _mm_avg_epu8(s, t);
    let k = _mm_sub_epi8(t4, t3); // k = (a + b + c + d) / 4

    let diag1 = get_m(k, st, one, bc, t); // diag1 = (a + 3b + 3c + d) / 8
    let diag2 = get_m(k, st, one, ad, s); // diag2 = (3a + b + c + 3d) / 8

    // pack the alternate pixels
    pack_and_store(a, b, diag1, diag2, &mut out[0..]); // store top
    pack_and_store(c, d, diag2, diag1, &mut out[2 * 32..]); // store bottom
}

// upstream: src/dsp/upsampling_sse2.c Upsample32Pixels_SSE2
/// Turn the macro into a function for reducing code-size when non-critical
#[target_feature(enable = "sse2")]
fn upsample32_pixels_sse2(r1: &[u8], r2: &[u8], out: &mut [u8]) {
    upsample_32pixels(r1, r2, out);
}

// upstream: src/dsp/upsampling_sse2.c UPSAMPLE_LAST_BLOCK
/// Upsamples the last `num_pixels` (1 to 17) samples of `tb` and `bb`, with the
/// last sample replicated to 17.
#[target_feature(enable = "sse2")]
#[inline]
fn upsample_last_block(tb: &[u8], bb: &[u8], num_pixels: usize, out: &mut [u8]) {
    let mut r1 = [0_u8; 17];
    let mut r2 = [0_u8; 17];
    r1[..num_pixels].copy_from_slice(&tb[..num_pixels]);
    r2[..num_pixels].copy_from_slice(&bb[..num_pixels]);
    // replicate last byte
    let last1 = r1[num_pixels - 1];
    r1[num_pixels..].fill(last1);
    let last2 = r2[num_pixels - 1];
    r2[num_pixels..].fill(last2);
    // using the shared function instead of the macro saves ~3k code size
    upsample32_pixels_sse2(&r1, &r2, out);
}

// upstream: src/dsp/upsampling_sse2.c CONVERT2RGB_32
/// Converts the 32 pixels at `cur_x` of the top row and, when present, of the
/// bottom row, with the upsampled U and V of `r_u` (`r_u`, `r_v = r_u + 32`,
/// and the bottom row's at `+ 64`).
#[target_feature(enable = "sse2")]
#[inline]
fn convert2rgb_32(
    func32: &impl Fn(&[u8], &[u8], &[u8], &mut [u8]),
    xstep: usize,
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_dst: &mut [u8],
    cur_x: usize,
    r_u: &[u8],
) {
    func32(
        &top_y[cur_x..],
        &r_u[0..],
        &r_u[32..],
        &mut top_dst[cur_x * xstep..],
    );
    if let Some((bottom_y, bottom_dst)) = bottom {
        func32(
            &bottom_y[cur_x..],
            &r_u[64..],
            &r_u[32 + 64..],
            &mut bottom_dst[cur_x * xstep..],
        );
    }
}

// upstream: src/dsp/upsampling_sse2.c SSE2_UPSAMPLE_FUNC
/// The fancy upsampler of one output format: `func` converts one pixel and
/// `func32` 32 pixels (C: the macro's `FUNC` and `FUNC##32_SSE2`).
#[target_feature(enable = "sse2")]
#[expect(
    clippy::too_many_arguments,
    reason = "the C parameters with the macro's"
)]
#[expect(clippy::similar_names, reason = "the C names u0_t and v0_t")]
#[expect(
    clippy::int_plus_one,
    reason = "the C loop condition pos + 32 + 1 <= len"
)]
fn sse2_upsample_func(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
    xstep: usize,
    func: impl Fn(u8, u8, u8, &mut [u8]),
    func32: impl Fn(&[u8], &[u8], &[u8], &mut [u8]),
) {
    let len = usize::try_from(len).expect("the row length is not negative");
    let mut bottom = bottom;
    // 16byte-aligned array to cache reconstructed u and v
    let mut uv_buf = [0_u8; 14 * 32];

    {
        // Treat the first pixel in regular way
        let u_diag = ((i32::from(top_u[0]) + i32::from(cur_u[0])) >> 1) + 1;
        let v_diag = ((i32::from(top_v[0]) + i32::from(cur_v[0])) >> 1) + 1;
        let u0_t = (i32::from(top_u[0]) + u_diag) >> 1;
        let v0_t = (i32::from(top_v[0]) + v_diag) >> 1;
        func(top_y[0], to_u8(u0_t), to_u8(v0_t), top_dst);
        if let Some((bottom_y, bottom_dst)) = bottom.as_mut() {
            let u0_b = (i32::from(cur_u[0]) + u_diag) >> 1;
            let v0_b = (i32::from(cur_v[0]) + v_diag) >> 1;
            func(bottom_y[0], to_u8(u0_b), to_u8(v0_b), bottom_dst);
        }
    }
    // For UPSAMPLE_32PIXELS, 17 u/v values must be read-able for each block
    let mut pos = 1;
    let mut uv_pos = 0;
    while pos + 32 + 1 <= len {
        upsample_32pixels(&top_u[uv_pos..], &cur_u[uv_pos..], &mut uv_buf[0..]);
        upsample_32pixels(&top_v[uv_pos..], &cur_v[uv_pos..], &mut uv_buf[32..]);
        convert2rgb_32(
            &func32,
            xstep,
            top_y,
            bottom.as_mut().map(|(y, dst)| (*y, &mut **dst)),
            top_dst,
            pos,
            &uv_buf,
        );
        pos += 32;
        uv_pos += 16;
    }
    if len > 1 {
        let left_over = ((len + 1) >> 1) - (pos >> 1);
        let (r_u, rest) = uv_buf.split_at_mut(4 * 32);
        let (tmp_top_dst, rest) = rest.split_at_mut(4 * 32);
        let (tmp_bottom_dst, rest) = rest.split_at_mut(4 * 32);
        let (tmp_top, tmp_bottom) = rest.split_at_mut(32);
        debug_assert!(left_over > 0);
        upsample_last_block(&top_u[uv_pos..], &cur_u[uv_pos..], left_over, &mut r_u[0..]);
        upsample_last_block(
            &top_v[uv_pos..],
            &cur_v[uv_pos..],
            left_over,
            &mut r_u[32..],
        );
        tmp_top[..len - pos].copy_from_slice(&top_y[pos..len]);
        if let Some((bottom_y, _)) = bottom.as_ref() {
            tmp_bottom[..len - pos].copy_from_slice(&bottom_y[pos..len]);
        }
        let tmp_bottom_pair = if bottom.is_some() {
            Some((&*tmp_bottom, &mut *tmp_bottom_dst))
        } else {
            None
        };
        convert2rgb_32(
            &func32,
            xstep,
            tmp_top,
            tmp_bottom_pair,
            tmp_top_dst,
            0,
            r_u,
        );
        top_dst[pos * xstep..len * xstep].copy_from_slice(&tmp_top_dst[..(len - pos) * xstep]);
        if let Some((_, bottom_dst)) = bottom {
            bottom_dst[pos * xstep..len * xstep]
                .copy_from_slice(&tmp_bottom_dst[..(len - pos) * xstep]);
        }
    }
}

// SSE2 variants of the fancy upsampler.

// upstream: src/dsp/upsampling_sse2.c UpsampleRgbaLinePair_SSE2
#[target_feature(enable = "sse2")]
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
fn upsample_rgba_line_pair_sse2(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    sse2_upsample_func(
        top_y,
        bottom,
        top_u,
        top_v,
        cur_u,
        cur_v,
        top_dst,
        len,
        4,
        vp8_yuv_to_rgba,
        |y, u, v, dst| vp8_yuv_to_rgba32_sse2(y, u, v, dst),
    );
}

/// Entry point of [`upsample_rgba_line_pair_sse2`] for `webp_upsamplers`
/// (`MODE_RGBA`, `MODE_rgbA`).
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
pub(crate) fn upsample_rgba_line_pair(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    // SAFETY: `webp_upsamplers` calls this entry point only after
    // `features().sse2`, the answer of `VP8GetCPUInfo(kSSE2)`, selected the
    // SSE2 variant, so the CPU has the function's only feature.
    unsafe {
        upsample_rgba_line_pair_sse2(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
    }
}

// upstream: src/dsp/upsampling_sse2.c UpsampleBgraLinePair_SSE2
#[target_feature(enable = "sse2")]
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
fn upsample_bgra_line_pair_sse2(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    sse2_upsample_func(
        top_y,
        bottom,
        top_u,
        top_v,
        cur_u,
        cur_v,
        top_dst,
        len,
        4,
        vp8_yuv_to_bgra,
        |y, u, v, dst| vp8_yuv_to_bgra32_sse2(y, u, v, dst),
    );
}

/// Entry point of [`upsample_bgra_line_pair_sse2`] for `webp_upsamplers`
/// (`MODE_BGRA`, `MODE_bgrA`) and `webp_get_line_pair_converter`.
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
pub(crate) fn upsample_bgra_line_pair(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    // SAFETY: `webp_upsamplers` calls this entry point, and
    // `webp_get_line_pair_converter` returns it, only after `features().sse2`
    // selected the SSE2 variant, so the CPU has SSE2.
    unsafe {
        upsample_bgra_line_pair_sse2(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
    }
}

// upstream: src/dsp/upsampling_sse2.c UpsampleRgbLinePair_SSE2
#[target_feature(enable = "sse2")]
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
fn upsample_rgb_line_pair_sse2(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    sse2_upsample_func(
        top_y,
        bottom,
        top_u,
        top_v,
        cur_u,
        cur_v,
        top_dst,
        len,
        3,
        |y, u, v, dst| vp8_yuv_to_rgb(i32::from(y), i32::from(u), i32::from(v), dst),
        |y, u, v, dst| vp8_yuv_to_rgb32_sse2(y, u, v, dst),
    );
}

/// Entry point of [`upsample_rgb_line_pair_sse2`] for `webp_upsamplers`
/// (`MODE_RGB`).
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
pub(crate) fn upsample_rgb_line_pair(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    // SAFETY: `webp_upsamplers` calls this entry point only after
    // `features().sse2` selected the SSE2 variant, so the CPU has SSE2.
    unsafe {
        upsample_rgb_line_pair_sse2(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
    }
}

// upstream: src/dsp/upsampling_sse2.c UpsampleBgrLinePair_SSE2
#[target_feature(enable = "sse2")]
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
fn upsample_bgr_line_pair_sse2(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    sse2_upsample_func(
        top_y,
        bottom,
        top_u,
        top_v,
        cur_u,
        cur_v,
        top_dst,
        len,
        3,
        |y, u, v, dst| vp8_yuv_to_bgr(i32::from(y), i32::from(u), i32::from(v), dst),
        |y, u, v, dst| vp8_yuv_to_bgr32_sse2(y, u, v, dst),
    );
}

/// Entry point of [`upsample_bgr_line_pair_sse2`] for `webp_upsamplers`
/// (`MODE_BGR`).
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
pub(crate) fn upsample_bgr_line_pair(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    // SAFETY: `webp_upsamplers` calls this entry point only after
    // `features().sse2` selected the SSE2 variant, so the CPU has SSE2.
    unsafe {
        upsample_bgr_line_pair_sse2(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
    }
}

// upstream: src/dsp/upsampling_sse2.c UpsampleArgbLinePair_SSE2
#[target_feature(enable = "sse2")]
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
fn upsample_argb_line_pair_sse2(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    sse2_upsample_func(
        top_y,
        bottom,
        top_u,
        top_v,
        cur_u,
        cur_v,
        top_dst,
        len,
        4,
        vp8_yuv_to_argb,
        |y, u, v, dst| vp8_yuv_to_argb32_sse2(y, u, v, dst),
    );
}

/// Entry point of [`upsample_argb_line_pair_sse2`] for `webp_upsamplers`
/// (`MODE_ARGB`, `MODE_Argb`) and `webp_get_line_pair_converter`.
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
pub(crate) fn upsample_argb_line_pair(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    // SAFETY: `webp_upsamplers` calls this entry point, and
    // `webp_get_line_pair_converter` returns it, only after `features().sse2`
    // selected the SSE2 variant, so the CPU has SSE2.
    unsafe {
        upsample_argb_line_pair_sse2(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
    }
}

// upstream: src/dsp/upsampling_sse2.c UpsampleRgba4444LinePair_SSE2
#[target_feature(enable = "sse2")]
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
fn upsample_rgba4444_line_pair_sse2(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    sse2_upsample_func(
        top_y,
        bottom,
        top_u,
        top_v,
        cur_u,
        cur_v,
        top_dst,
        len,
        2,
        |y, u, v, dst| vp8_yuv_to_rgba4444(i32::from(y), i32::from(u), i32::from(v), dst),
        |y, u, v, dst| vp8_yuv_to_rgba444432_sse2(y, u, v, dst),
    );
}

/// Entry point of [`upsample_rgba4444_line_pair_sse2`] for `webp_upsamplers`
/// (`MODE_RGBA_4444`, `MODE_rgbA_4444`).
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
pub(crate) fn upsample_rgba4444_line_pair(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    // SAFETY: `webp_upsamplers` calls this entry point only after
    // `features().sse2` selected the SSE2 variant, so the CPU has SSE2.
    unsafe {
        upsample_rgba4444_line_pair_sse2(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
    }
}

// upstream: src/dsp/upsampling_sse2.c UpsampleRgb565LinePair_SSE2
#[target_feature(enable = "sse2")]
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
fn upsample_rgb565_line_pair_sse2(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    sse2_upsample_func(
        top_y,
        bottom,
        top_u,
        top_v,
        cur_u,
        cur_v,
        top_dst,
        len,
        2,
        |y, u, v, dst| vp8_yuv_to_rgb565(i32::from(y), i32::from(u), i32::from(v), dst),
        |y, u, v, dst| vp8_yuv_to_rgb56532_sse2(y, u, v, dst),
    );
}

/// Entry point of [`upsample_rgb565_line_pair_sse2`] for `webp_upsamplers`
/// (`MODE_RGB_565`).
#[expect(clippy::too_many_arguments, reason = "the C parameters")]
pub(crate) fn upsample_rgb565_line_pair(
    top_y: &[u8],
    bottom: Option<(&[u8], &mut [u8])>,
    top_u: &[u8],
    top_v: &[u8],
    cur_u: &[u8],
    cur_v: &[u8],
    top_dst: &mut [u8],
    len: i32,
) {
    // SAFETY: `webp_upsamplers` calls this entry point only after
    // `features().sse2` selected the SSE2 variant, so the CPU has SSE2.
    unsafe {
        upsample_rgb565_line_pair_sse2(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
    }
}

//------------------------------------------------------------------------------
// Entry point

// upstream: src/dsp/upsampling_sse2.c WebPInitUpsamplersSSE2 (no counterpart: it only assigns pointers, ADR 0003; the module documentation lists them)

//------------------------------------------------------------------------------

// upstream: src/dsp/upsampling_sse2.c YUV444_FUNC
/// Converts a row of YUV 4:4:4 samples, 32 at a time with `call` and the rest
/// with the C function `call_c` (C: the macro's `CALL` and `CALL_C`).
#[target_feature(enable = "sse2")]
#[inline]
#[expect(
    clippy::too_many_arguments,
    reason = "the C parameters with the macro's"
)]
fn yuv444_func(
    y: &[u8],
    u: &[u8],
    v: &[u8],
    dst: &mut [u8],
    len: i32,
    xstep: usize,
    call: impl Fn(&[u8], &[u8], &[u8], &mut [u8]),
    call_c: WebPYUV444Converter,
) {
    let max_len = len & !31;
    let mut i = 0;
    while i < max_len {
        let at = usize::try_from(i).expect("a non-negative position");
        call(&y[at..], &u[at..], &v[at..], &mut dst[at * xstep..]);
        i += 32;
    }
    if i < len {
        // C-fallback
        let at = usize::try_from(i).expect("a non-negative position");
        call_c(
            &y[at..],
            &u[at..],
            &v[at..],
            &mut dst[at * xstep..],
            len - i,
        );
    }
}

// upstream: src/dsp/upsampling_sse2.c Yuv444ToRgba_SSE2
#[target_feature(enable = "sse2")]
fn yuv444_to_rgba_sse2(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_func(
        y,
        u,
        v,
        dst,
        len,
        4,
        |y, u, v, dst| vp8_yuv_to_rgba32_sse2(y, u, v, dst),
        webp_yuv444_to_rgba_c,
    );
}

/// Entry point of [`yuv444_to_rgba_sse2`] for `webp_yuv444_converters`
/// (`MODE_RGBA`, `MODE_rgbA`).
pub(crate) fn yuv444_to_rgba(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    // SAFETY: `webp_yuv444_converters` calls this entry point only after
    // `features().sse2` selected the SSE2 variant, so the CPU has SSE2.
    unsafe { yuv444_to_rgba_sse2(y, u, v, dst, len) }
}

// upstream: src/dsp/upsampling_sse2.c Yuv444ToBgra_SSE2
#[target_feature(enable = "sse2")]
fn yuv444_to_bgra_sse2(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_func(
        y,
        u,
        v,
        dst,
        len,
        4,
        |y, u, v, dst| vp8_yuv_to_bgra32_sse2(y, u, v, dst),
        webp_yuv444_to_bgra_c,
    );
}

/// Entry point of [`yuv444_to_bgra_sse2`] for `webp_yuv444_converters`
/// (`MODE_BGRA`, `MODE_bgrA`).
pub(crate) fn yuv444_to_bgra(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    // SAFETY: `webp_yuv444_converters` calls this entry point only after
    // `features().sse2` selected the SSE2 variant, so the CPU has SSE2.
    unsafe { yuv444_to_bgra_sse2(y, u, v, dst, len) }
}

// upstream: src/dsp/upsampling_sse2.c Yuv444ToRgb_SSE2
#[target_feature(enable = "sse2")]
fn yuv444_to_rgb_sse2(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_func(
        y,
        u,
        v,
        dst,
        len,
        3,
        |y, u, v, dst| vp8_yuv_to_rgb32_sse2(y, u, v, dst),
        webp_yuv444_to_rgb_c,
    );
}

/// Entry point of [`yuv444_to_rgb_sse2`] for `webp_yuv444_converters`
/// (`MODE_RGB`).
pub(crate) fn yuv444_to_rgb(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    // SAFETY: `webp_yuv444_converters` calls this entry point only after
    // `features().sse2` selected the SSE2 variant, so the CPU has SSE2.
    unsafe { yuv444_to_rgb_sse2(y, u, v, dst, len) }
}

// upstream: src/dsp/upsampling_sse2.c Yuv444ToBgr_SSE2
#[target_feature(enable = "sse2")]
fn yuv444_to_bgr_sse2(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_func(
        y,
        u,
        v,
        dst,
        len,
        3,
        |y, u, v, dst| vp8_yuv_to_bgr32_sse2(y, u, v, dst),
        webp_yuv444_to_bgr_c,
    );
}

/// Entry point of [`yuv444_to_bgr_sse2`] for `webp_yuv444_converters`
/// (`MODE_BGR`).
pub(crate) fn yuv444_to_bgr(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    // SAFETY: `webp_yuv444_converters` calls this entry point only after
    // `features().sse2` selected the SSE2 variant, so the CPU has SSE2.
    unsafe { yuv444_to_bgr_sse2(y, u, v, dst, len) }
}

// upstream: src/dsp/upsampling_sse2.c Yuv444ToArgb_SSE2
#[target_feature(enable = "sse2")]
fn yuv444_to_argb_sse2(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_func(
        y,
        u,
        v,
        dst,
        len,
        4,
        |y, u, v, dst| vp8_yuv_to_argb32_sse2(y, u, v, dst),
        webp_yuv444_to_argb_c,
    );
}

/// Entry point of [`yuv444_to_argb_sse2`] for `webp_yuv444_converters`
/// (`MODE_ARGB`, `MODE_Argb`).
pub(crate) fn yuv444_to_argb(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    // SAFETY: `webp_yuv444_converters` calls this entry point only after
    // `features().sse2` selected the SSE2 variant, so the CPU has SSE2.
    unsafe { yuv444_to_argb_sse2(y, u, v, dst, len) }
}

// upstream: src/dsp/upsampling_sse2.c Yuv444ToRgba4444_SSE2
#[target_feature(enable = "sse2")]
fn yuv444_to_rgba4444_sse2(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_func(
        y,
        u,
        v,
        dst,
        len,
        2,
        |y, u, v, dst| vp8_yuv_to_rgba444432_sse2(y, u, v, dst),
        webp_yuv444_to_rgba4444_c,
    );
}

/// Entry point of [`yuv444_to_rgba4444_sse2`] for `webp_yuv444_converters`
/// (`MODE_RGBA_4444`, `MODE_rgbA_4444`).
pub(crate) fn yuv444_to_rgba4444(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    // SAFETY: `webp_yuv444_converters` calls this entry point only after
    // `features().sse2` selected the SSE2 variant, so the CPU has SSE2.
    unsafe { yuv444_to_rgba4444_sse2(y, u, v, dst, len) }
}

// upstream: src/dsp/upsampling_sse2.c Yuv444ToRgb565_SSE2
#[target_feature(enable = "sse2")]
fn yuv444_to_rgb565_sse2(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_func(
        y,
        u,
        v,
        dst,
        len,
        2,
        |y, u, v, dst| vp8_yuv_to_rgb56532_sse2(y, u, v, dst),
        webp_yuv444_to_rgb565_c,
    );
}

/// Entry point of [`yuv444_to_rgb565_sse2`] for `webp_yuv444_converters`
/// (`MODE_RGB_565`).
pub(crate) fn yuv444_to_rgb565(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    // SAFETY: `webp_yuv444_converters` calls this entry point only after
    // `features().sse2` selected the SSE2 variant, so the CPU has SSE2.
    unsafe { yuv444_to_rgb565_sse2(y, u, v, dst, len) }
}

// upstream: src/dsp/upsampling_sse2.c WebPInitYUV444ConvertersSSE2 (no counterpart: it only assigns pointers, ADR 0003; the module documentation lists them)