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
// libwebp v1.6.0, src/dsp/upsampling.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.
// -----------------------------------------------------------------------------
//
// YUV to RGB upsampling functions.
//
// Author: somnath@google.com (Somnath Banerjee)

//! Translation of `src/dsp/upsampling.c` from libwebp v1.6.0.
//!
//! Work package: WP4 (pixel ops).
//!
//! # Deviations from upstream
//!
//! Changes of representation that the ADRs allow, or behavior that the Rust
//! side cannot reproduce exactly. Each records the reason.
//!
//! 1. **`WebPGetLinePairConverter`** returns the function pointer of the table,
//!    `WebPUpsampleLinePairFunc`. The C caller `picture_csp_enc.c` calls the
//!    returned function, so a getter that returns a function type is the
//!    faithful form.
//! 2. **Bottom row of a line pair.** C passes `bottom_y` and `bottom_dst`,
//!    which are NULL together in use. The Rust form takes one
//!    `Option<(&[u8], &mut [u8])>`. C also allows `bottom_dst` without
//!    `bottom_y`, where it is not written; the Rust callers pass `None` there.

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::webp::decode::WEBP_CSP_MODE;

// upstream: src/dsp/dsp.h WebPUpsampleLinePairFunc
/// Converts one pair of luma rows, and the chroma rows that they share, to the
/// output colorspace. The bottom row is optional: `None` stands for a NULL
/// `bottom_y` with its `bottom_dst`, which C passes together (ADR 0003).
pub type WebPUpsampleLinePairFunc =
    fn(&[u8], Option<(&[u8], &mut [u8])>, &[u8], &[u8], &[u8], &[u8], &mut [u8], i32);

// upstream: src/dsp/dsp.h WebPYUV444Converter
/// Converts a row of YUV 4:4:4 samples to the output colorspace.
pub type WebPYUV444Converter = fn(&[u8], &[u8], &[u8], &mut [u8], i32);

// upstream: src/dsp/upsampling.c LOAD_UV
/// Packs a U sample in the low 16 bits and a V sample in the high 16 bits.
#[must_use]
fn load_uv(u: u8, v: u8) -> u32 {
    u32::from(u) | (u32::from(v) << 16)
}

// upstream: src/dsp/upsampling.c UPSAMPLE_FUNC
/// Converts one pair of rows, with fancy upsampling of the chroma. The
/// samples of the pair are interpolated diagonally, as the C macro does, and
/// `func` converts each output pixel with its luma and the interpolated U and
/// V. The chroma samples are packed in one `u32` per pixel, U in the low lane
/// and V in the high lane, and each shift keeps the lanes apart as the C code
/// does. The `# Panics` below apply to every slice of the pair.
///
/// # Panics
///
/// Panics when a slice is shorter than the samples or pixels that `len` needs.
/// `len` must be at least 1, as in C, where the first pixel is always read.
#[expect(
    clippy::too_many_arguments,
    reason = "mirrors the parameters of the UPSAMPLE_FUNC functions"
)]
#[expect(
    clippy::similar_names,
    reason = "the C names tl_uv, t_uv and uv of the packed samples"
)]
fn upsample_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,
    xstep: usize,
    func: impl Fn(u8, u8, u8, &mut [u8]),
) {
    let len = usize::try_from(len).expect("the row length is not negative");
    // `len - 1 >> 1` in C: the number of pixel pairs after the first pixel.
    let last_pixel_pair = (len - 1) >> 1;
    let mut bottom = bottom;
    let mut tl_uv = load_uv(top_u[0], top_v[0]); // top-left sample
    let mut l_uv = load_uv(cur_u[0], cur_v[0]); // left-sample
    let emit = |y: u8, uv: u32, dst: &mut [u8]| {
        func(
            y,
            u8::try_from(uv & 0xff).expect("the low lane is a byte"),
            u8::try_from(uv >> 16).expect("the high lane is a byte"),
            dst,
        );
    };
    {
        let uv0 = (3 * tl_uv + l_uv + 0x0002_0002) >> 2;
        emit(top_y[0], uv0, &mut top_dst[..]);
    }
    if let Some((bottom_y, bottom_dst)) = bottom.as_mut() {
        let uv0 = (3 * l_uv + tl_uv + 0x0002_0002) >> 2;
        emit(bottom_y[0], uv0, &mut bottom_dst[..]);
    }
    for x in 1..=last_pixel_pair {
        let t_uv = load_uv(top_u[x], top_v[x]); // top sample
        let uv = load_uv(cur_u[x], cur_v[x]); // sample
        // precompute invariant values associated with first and second diagonals
        let avg = tl_uv + t_uv + l_uv + uv + 0x0008_0008;
        let diag_12 = (avg + 2 * (t_uv + l_uv)) >> 3;
        let diag_03 = (avg + 2 * (tl_uv + uv)) >> 3;
        {
            let uv0 = u32::midpoint(diag_12, tl_uv);
            let uv1 = u32::midpoint(diag_03, t_uv);
            emit(top_y[2 * x - 1], uv0, &mut top_dst[(2 * x - 1) * xstep..]);
            emit(top_y[2 * x], uv1, &mut top_dst[2 * x * xstep..]);
        }
        if let Some((bottom_y, bottom_dst)) = bottom.as_mut() {
            let uv0 = u32::midpoint(diag_03, l_uv);
            let uv1 = u32::midpoint(diag_12, uv);
            emit(
                bottom_y[2 * x - 1],
                uv0,
                &mut bottom_dst[(2 * x - 1) * xstep..],
            );
            emit(bottom_y[2 * x], uv1, &mut bottom_dst[2 * x * xstep..]);
        }
        tl_uv = t_uv;
        l_uv = uv;
    }
    if len & 1 == 0 {
        {
            let uv0 = (3 * tl_uv + l_uv + 0x0002_0002) >> 2;
            emit(top_y[len - 1], uv0, &mut top_dst[(len - 1) * xstep..]);
        }
        if let Some((bottom_y, bottom_dst)) = bottom.as_mut() {
            let uv0 = (3 * l_uv + tl_uv + 0x0002_0002) >> 2;
            emit(bottom_y[len - 1], uv0, &mut bottom_dst[(len - 1) * xstep..]);
        }
    }
}

#[expect(
    clippy::too_many_arguments,
    reason = "the parameters of WebPUpsampleLinePairFunc"
)]
// upstream: src/dsp/upsampling.c UpsampleRgbaLinePair_C
pub fn upsample_rgba_line_pair_c(
    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,
) {
    upsample_line_pair(
        top_y,
        bottom,
        top_u,
        top_v,
        cur_u,
        cur_v,
        top_dst,
        len,
        4,
        vp8_yuv_to_rgba,
    );
}

#[expect(
    clippy::too_many_arguments,
    reason = "the parameters of WebPUpsampleLinePairFunc"
)]
// upstream: src/dsp/upsampling.c UpsampleBgraLinePair_C
pub fn upsample_bgra_line_pair_c(
    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,
) {
    upsample_line_pair(
        top_y,
        bottom,
        top_u,
        top_v,
        cur_u,
        cur_v,
        top_dst,
        len,
        4,
        vp8_yuv_to_bgra,
    );
}

#[expect(
    clippy::too_many_arguments,
    reason = "the parameters of WebPUpsampleLinePairFunc"
)]
// upstream: src/dsp/upsampling.c UpsampleArgbLinePair_C
pub fn upsample_argb_line_pair_c(
    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,
) {
    upsample_line_pair(
        top_y,
        bottom,
        top_u,
        top_v,
        cur_u,
        cur_v,
        top_dst,
        len,
        4,
        vp8_yuv_to_argb,
    );
}

#[expect(
    clippy::too_many_arguments,
    reason = "the parameters of WebPUpsampleLinePairFunc"
)]
// upstream: src/dsp/upsampling.c UpsampleRgbLinePair_C
pub fn upsample_rgb_line_pair_c(
    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,
) {
    upsample_line_pair(
        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);
        },
    );
}

#[expect(
    clippy::too_many_arguments,
    reason = "the parameters of WebPUpsampleLinePairFunc"
)]
// upstream: src/dsp/upsampling.c UpsampleBgrLinePair_C
pub fn upsample_bgr_line_pair_c(
    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,
) {
    upsample_line_pair(
        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);
        },
    );
}

#[expect(
    clippy::too_many_arguments,
    reason = "the parameters of WebPUpsampleLinePairFunc"
)]
// upstream: src/dsp/upsampling.c UpsampleRgba4444LinePair_C
pub fn upsample_rgba4444_line_pair_c(
    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,
) {
    upsample_line_pair(
        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);
        },
    );
}

#[expect(
    clippy::too_many_arguments,
    reason = "the parameters of WebPUpsampleLinePairFunc"
)]
// upstream: src/dsp/upsampling.c UpsampleRgb565LinePair_C
pub fn upsample_rgb565_line_pair_c(
    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,
) {
    upsample_line_pair(
        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);
        },
    );
}

// upstream: src/dsp/upsampling.c WebPUpsamplers
// The table is filled by `WebPInitUpsamplers`, whose portable effect is to
// assign these pointers; its SIMD branches are not compiled in the reference
// configuration (ADR 0002). MODE_YUV and MODE_YUVA have no upsampler.
static WEBP_UPSAMPLERS: [Option<WebPUpsampleLinePairFunc>; 13] = [
    Some(upsample_rgb_line_pair_c),      // MODE_RGB
    Some(upsample_rgba_line_pair_c),     // MODE_RGBA
    Some(upsample_bgr_line_pair_c),      // MODE_BGR
    Some(upsample_bgra_line_pair_c),     // MODE_BGRA
    Some(upsample_argb_line_pair_c),     // MODE_ARGB
    Some(upsample_rgba4444_line_pair_c), // MODE_RGBA_4444
    Some(upsample_rgb565_line_pair_c),   // MODE_RGB_565
    Some(upsample_rgba_line_pair_c),     // MODE_rgbA
    Some(upsample_bgra_line_pair_c),     // MODE_bgrA
    Some(upsample_argb_line_pair_c),     // MODE_Argb
    Some(upsample_rgba4444_line_pair_c), // MODE_rgbA_4444
    None,                                // MODE_YUV
    None,                                // MODE_YUVA
];

// upstream: src/dsp/upsampling.c WebPUpsamplers
/// Converts one pair of rows with the upsampler of `mode`,
/// `WebPUpsamplers[mode]`.
///
/// # Panics
///
/// Panics for `MODE_YUV` and `MODE_YUVA`, which have no upsampler (NULL in C).
#[expect(
    clippy::too_many_arguments,
    reason = "mirrors the parameters of WebPUpsampleLinePairFunc, with the mode first"
)]
pub fn webp_upsamplers(
    mode: WEBP_CSP_MODE,
    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,
) {
    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
    {
        let features = crate::dsp::cpu::features();
        if features.sse4_1 {
            match mode {
                WEBP_CSP_MODE::MODE_RGB => {
                    return crate::dsp::upsampling_sse41::upsample_rgb_line_pair(
                        top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
                    );
                }
                WEBP_CSP_MODE::MODE_BGR => {
                    return crate::dsp::upsampling_sse41::upsample_bgr_line_pair(
                        top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
                    );
                }
                _ => {}
            }
        }
        if features.sse2 {
            match mode {
                WEBP_CSP_MODE::MODE_RGBA | WEBP_CSP_MODE::MODE_rgbA => {
                    return crate::dsp::upsampling_sse2::upsample_rgba_line_pair(
                        top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
                    );
                }
                WEBP_CSP_MODE::MODE_BGRA | WEBP_CSP_MODE::MODE_bgrA => {
                    return crate::dsp::upsampling_sse2::upsample_bgra_line_pair(
                        top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
                    );
                }
                WEBP_CSP_MODE::MODE_RGB => {
                    return crate::dsp::upsampling_sse2::upsample_rgb_line_pair(
                        top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
                    );
                }
                WEBP_CSP_MODE::MODE_BGR => {
                    return crate::dsp::upsampling_sse2::upsample_bgr_line_pair(
                        top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
                    );
                }
                WEBP_CSP_MODE::MODE_ARGB | WEBP_CSP_MODE::MODE_Argb => {
                    return crate::dsp::upsampling_sse2::upsample_argb_line_pair(
                        top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
                    );
                }
                WEBP_CSP_MODE::MODE_RGB_565 => {
                    return crate::dsp::upsampling_sse2::upsample_rgb565_line_pair(
                        top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
                    );
                }
                WEBP_CSP_MODE::MODE_RGBA_4444 | WEBP_CSP_MODE::MODE_rgbA_4444 => {
                    return crate::dsp::upsampling_sse2::upsample_rgba4444_line_pair(
                        top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
                    );
                }
                _ => {}
            }
        }
    }
    #[cfg(all(feature = "simd", target_arch = "aarch64"))]
    {
        if let Some(upsampler) = crate::dsp::upsampling_neon::upsamplers(mode) {
            return upsampler(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
        }
    }
    #[allow(
        unreachable_code,
        reason = "the C path is reached only without a SIMD variant"
    )]
    let upsampler = WEBP_UPSAMPLERS[mode.0 as usize].expect("YUV modes have no upsampler");
    upsampler(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
}

// upstream: src/dsp/upsampling.c WebPGetLinePairConverter
/// Returns the upsampler for ARGB-like output (`alpha_is_last` false) or for
/// RGBA-like output (`alpha_is_last` true). The C function first runs
/// `WebPInitUpsamplers`, whose only effect is the table itself (ADR 0003).
///
/// # Panics
///
/// Never panics: the BGRA and ARGB entries of the table are set.
#[must_use]
pub fn webp_get_line_pair_converter(alpha_is_last: bool) -> WebPUpsampleLinePairFunc {
    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
    {
        // `WebPInitUpsamplersSSE41` assigns neither entry.
        let features = crate::dsp::cpu::features();
        if features.sse2 {
            return if alpha_is_last {
                crate::dsp::upsampling_sse2::upsample_bgra_line_pair
            } else {
                crate::dsp::upsampling_sse2::upsample_argb_line_pair
            };
        }
    }
    #[allow(
        unreachable_code,
        reason = "the C path is reached only without a SIMD variant"
    )]
    let mode = if alpha_is_last {
        WEBP_CSP_MODE::MODE_BGRA
    } else {
        WEBP_CSP_MODE::MODE_ARGB
    };
    #[cfg(all(feature = "simd", target_arch = "aarch64"))]
    {
        if let Some(upsampler) = crate::dsp::upsampling_neon::upsamplers(mode) {
            return upsampler;
        }
    }
    WEBP_UPSAMPLERS[mode.0 as usize].expect("the ARGB and BGRA upsamplers exist")
}

// upstream: src/dsp/upsampling.c YUV444_FUNC
/// Converts a row of YUV 4:4:4 samples, one output pixel per sample, with
/// `func` as the per-pixel conversion.
fn yuv444_row(
    y: &[u8],
    u: &[u8],
    v: &[u8],
    dst: &mut [u8],
    len: i32,
    xstep: usize,
    func: impl Fn(u8, u8, u8, &mut [u8]),
) {
    let len = usize::try_from(len).expect("the row length is not negative");
    for i in 0..len {
        func(y[i], u[i], v[i], &mut dst[i * xstep..]);
    }
}

// upstream: src/dsp/upsampling.c WebPYuv444ToRgba_C
pub(crate) fn webp_yuv444_to_rgba_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_row(y, u, v, dst, len, 4, vp8_yuv_to_rgba);
}

// upstream: src/dsp/upsampling.c WebPYuv444ToBgra_C
pub(crate) fn webp_yuv444_to_bgra_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_row(y, u, v, dst, len, 4, vp8_yuv_to_bgra);
}

// upstream: src/dsp/upsampling.c WebPYuv444ToArgb_C
pub(crate) fn webp_yuv444_to_argb_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_row(y, u, v, dst, len, 4, vp8_yuv_to_argb);
}

// upstream: src/dsp/upsampling.c WebPYuv444ToRgb_C
pub(crate) fn webp_yuv444_to_rgb_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_row(y, u, v, dst, len, 3, |y, u, v, out| {
        vp8_yuv_to_rgb(i32::from(y), i32::from(u), i32::from(v), out);
    });
}

// upstream: src/dsp/upsampling.c WebPYuv444ToBgr_C
pub(crate) fn webp_yuv444_to_bgr_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_row(y, u, v, dst, len, 3, |y, u, v, out| {
        vp8_yuv_to_bgr(i32::from(y), i32::from(u), i32::from(v), out);
    });
}

// upstream: src/dsp/upsampling.c WebPYuv444ToRgba4444_C
pub(crate) fn webp_yuv444_to_rgba4444_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_row(y, u, v, dst, len, 2, |y, u, v, out| {
        vp8_yuv_to_rgba4444(i32::from(y), i32::from(u), i32::from(v), out);
    });
}

// upstream: src/dsp/upsampling.c WebPYuv444ToRgb565_C
pub(crate) fn webp_yuv444_to_rgb565_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
    yuv444_row(y, u, v, dst, len, 2, |y, u, v, out| {
        vp8_yuv_to_rgb565(i32::from(y), i32::from(u), i32::from(v), out);
    });
}

// upstream: src/dsp/upsampling.c WebPYUV444Converters
// The table is filled by `WebPInitYUV444Converters`, which assigns these
// pointers only (ADR 0003). MODE_YUV and MODE_YUVA have no converter.
static WEBP_YUV444_CONVERTERS: [Option<WebPYUV444Converter>; 13] = [
    Some(webp_yuv444_to_rgb_c),      // MODE_RGB
    Some(webp_yuv444_to_rgba_c),     // MODE_RGBA
    Some(webp_yuv444_to_bgr_c),      // MODE_BGR
    Some(webp_yuv444_to_bgra_c),     // MODE_BGRA
    Some(webp_yuv444_to_argb_c),     // MODE_ARGB
    Some(webp_yuv444_to_rgba4444_c), // MODE_RGBA_4444
    Some(webp_yuv444_to_rgb565_c),   // MODE_RGB_565
    Some(webp_yuv444_to_rgba_c),     // MODE_rgbA
    Some(webp_yuv444_to_bgra_c),     // MODE_bgrA
    Some(webp_yuv444_to_argb_c),     // MODE_Argb
    Some(webp_yuv444_to_rgba4444_c), // MODE_rgbA_4444
    None,                            // MODE_YUV
    None,                            // MODE_YUVA
];

// upstream: src/dsp/upsampling.c WebPYUV444Converters
/// Converts a row of YUV 4:4:4 samples with `WebPYUV444Converters[mode]`.
///
/// # Panics
///
/// Panics for `MODE_YUV` and `MODE_YUVA`, which have no converter (NULL in C).
pub fn webp_yuv444_converters(
    mode: WEBP_CSP_MODE,
    y: &[u8],
    u: &[u8],
    v: &[u8],
    dst: &mut [u8],
    len: i32,
) {
    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
    {
        let features = crate::dsp::cpu::features();
        if features.sse4_1 {
            match mode {
                WEBP_CSP_MODE::MODE_RGB => {
                    return crate::dsp::upsampling_sse41::yuv444_to_rgb(y, u, v, dst, len);
                }
                WEBP_CSP_MODE::MODE_BGR => {
                    return crate::dsp::upsampling_sse41::yuv444_to_bgr(y, u, v, dst, len);
                }
                _ => {}
            }
        }
        if features.sse2 {
            match mode {
                WEBP_CSP_MODE::MODE_RGBA | WEBP_CSP_MODE::MODE_rgbA => {
                    return crate::dsp::upsampling_sse2::yuv444_to_rgba(y, u, v, dst, len);
                }
                WEBP_CSP_MODE::MODE_BGRA | WEBP_CSP_MODE::MODE_bgrA => {
                    return crate::dsp::upsampling_sse2::yuv444_to_bgra(y, u, v, dst, len);
                }
                WEBP_CSP_MODE::MODE_RGB => {
                    return crate::dsp::upsampling_sse2::yuv444_to_rgb(y, u, v, dst, len);
                }
                WEBP_CSP_MODE::MODE_BGR => {
                    return crate::dsp::upsampling_sse2::yuv444_to_bgr(y, u, v, dst, len);
                }
                WEBP_CSP_MODE::MODE_ARGB | WEBP_CSP_MODE::MODE_Argb => {
                    return crate::dsp::upsampling_sse2::yuv444_to_argb(y, u, v, dst, len);
                }
                WEBP_CSP_MODE::MODE_RGB_565 => {
                    return crate::dsp::upsampling_sse2::yuv444_to_rgb565(y, u, v, dst, len);
                }
                WEBP_CSP_MODE::MODE_RGBA_4444 | WEBP_CSP_MODE::MODE_rgbA_4444 => {
                    return crate::dsp::upsampling_sse2::yuv444_to_rgba4444(y, u, v, dst, len);
                }
                _ => {}
            }
        }
    }
    #[allow(
        unreachable_code,
        reason = "the C path is reached only without a SIMD variant"
    )]
    let converter = WEBP_YUV444_CONVERTERS[mode.0 as usize].expect("YUV modes have no converter");
    converter(y, u, v, dst, len);
}