openh264 0.9.8

Idiomatic bindings for OpenH264.
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
use super::{Rgb8SourceLayout, Yuv420Planes, wide};

const RGB_PIXEL_LEN: usize = 3;
const RGBA_PIXEL_LEN: usize = 4;
const FIXED_Y_COEFFICIENT: i16 = 74;
const FIXED_RV_COEFFICIENT: i16 = 102;
const FIXED_GU_COEFFICIENT: i16 = -25;
const FIXED_GV_COEFFICIENT: i16 = -52;
const FIXED_BU_COEFFICIENT: i16 = 129;

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
pub unsafe fn write_yuv420_to_rgb_avx2_rgb(
    y_plane: &[u8],
    u_plane: &[u8],
    v_plane: &[u8],
    dim: (usize, usize),
    strides: (usize, usize, usize),
    target: &mut [u8],
) {
    let (width, height) = dim;
    for y in 0..height {
        let y_row = &y_plane[y * strides.0..y * strides.0 + width];
        let u_row = &u_plane[(y / 2) * strides.1..(y / 2) * strides.1 + width / 2];
        let v_row = &v_plane[(y / 2) * strides.2..(y / 2) * strides.2 + width / 2];
        let target = &mut target[y * width * RGB_PIXEL_LEN..(y + 1) * width * RGB_PIXEL_LEN];

        let mut x = 0;
        while x + 16 <= width {
            // SAFETY: The loop condition leaves sixteen luma and eight chroma bytes, plus
            // sixteen RGB output pixels, in the respective slices.
            unsafe {
                write_yuv420_to_rgb_avx2_rgb_16(
                    y_row.as_ptr().add(x),
                    u_row.as_ptr().add(x / 2),
                    v_row.as_ptr().add(x / 2),
                    target.as_mut_ptr().add(x * RGB_PIXEL_LEN),
                );
            }
            x += 16;
        }

        if x != width {
            wide::write_yuv420_to_rgb_wide_row(
                &y_row[x..],
                &u_row[x / 2..],
                &v_row[x / 2..],
                &mut target[x * RGB_PIXEL_LEN..],
                RGB_PIXEL_LEN,
            );
        }
    }
}

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
pub unsafe fn write_yuv420_to_rgb_avx2_rgba(
    y_plane: &[u8],
    u_plane: &[u8],
    v_plane: &[u8],
    dim: (usize, usize),
    strides: (usize, usize, usize),
    target: &mut [u8],
) {
    let (width, height) = dim;
    for y in 0..height {
        let y_row = &y_plane[y * strides.0..y * strides.0 + width];
        let u_row = &u_plane[(y / 2) * strides.1..(y / 2) * strides.1 + width / 2];
        let v_row = &v_plane[(y / 2) * strides.2..(y / 2) * strides.2 + width / 2];
        let target = &mut target[y * width * RGBA_PIXEL_LEN..(y + 1) * width * RGBA_PIXEL_LEN];

        let mut x = 0;
        while x + 16 <= width {
            // SAFETY: The loop condition leaves sixteen luma and eight chroma bytes, plus
            // sixteen RGBA output pixels, in the respective slices.
            unsafe {
                write_yuv420_to_rgb_avx2_rgba_16(
                    y_row.as_ptr().add(x),
                    u_row.as_ptr().add(x / 2),
                    v_row.as_ptr().add(x / 2),
                    target.as_mut_ptr().add(x * RGBA_PIXEL_LEN),
                );
            }
            x += 16;
        }

        if x != width {
            wide::write_yuv420_to_rgb_wide_row(
                &y_row[x..],
                &u_row[x / 2..],
                &v_row[x / 2..],
                &mut target[x * RGBA_PIXEL_LEN..],
                RGBA_PIXEL_LEN,
            );
        }
    }
}

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
#[allow(clippy::many_single_char_names, clippy::wildcard_imports)]
unsafe fn write_yuv420_to_rgb_avx2_rgba_16(y_ptr: *const u8, u_ptr: *const u8, v_ptr: *const u8, target: *mut u8) {
    use std::arch::x86_64::*;

    // SAFETY: The caller guarantees that all source and destination ranges are valid.
    unsafe {
        let (r, g, b) = yuv420_to_rgb_avx2_16(y_ptr, u_ptr, v_ptr);
        let alpha = _mm_set1_epi8(-1);
        let rg_low = _mm_unpacklo_epi8(r, g);
        let rg_high = _mm_unpackhi_epi8(r, g);
        let ba_low = _mm_unpacklo_epi8(b, alpha);
        let ba_high = _mm_unpackhi_epi8(b, alpha);

        _mm_storeu_si128(target.cast(), _mm_unpacklo_epi16(rg_low, ba_low));
        _mm_storeu_si128(target.add(16).cast(), _mm_unpackhi_epi16(rg_low, ba_low));
        _mm_storeu_si128(target.add(32).cast(), _mm_unpacklo_epi16(rg_high, ba_high));
        _mm_storeu_si128(target.add(48).cast(), _mm_unpackhi_epi16(rg_high, ba_high));
    }
}

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
#[allow(clippy::wildcard_imports)]
unsafe fn write_yuv420_to_rgb_avx2_rgb_16(y_ptr: *const u8, u_ptr: *const u8, v_ptr: *const u8, target: *mut u8) {
    use std::arch::x86_64::*;

    // SAFETY: The caller guarantees that all source and destination ranges are valid.
    unsafe {
        let (r, g, b) = yuv420_to_rgb_avx2_16(y_ptr, u_ptr, v_ptr);
        let (first_low, tail_low) = pack_rgb8_avx2_8(r, g, b);
        let (first_high, tail_high) = pack_rgb8_avx2_8(_mm_srli_si128::<8>(r), _mm_srli_si128::<8>(g), _mm_srli_si128::<8>(b));

        _mm_storeu_si128(target.cast(), first_low);
        _mm_storeu_si128(target.add(16).cast(), _mm_unpacklo_epi64(tail_low, first_high));
        _mm_storeu_si128(
            target.add(32).cast(),
            _mm_unpacklo_epi64(_mm_srli_si128::<8>(first_high), tail_high),
        );
    }
}

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
#[allow(clippy::many_single_char_names, clippy::wildcard_imports)]
unsafe fn yuv420_to_rgb_avx2_16(
    y_ptr: *const u8,
    u_ptr: *const u8,
    v_ptr: *const u8,
) -> (
    std::arch::x86_64::__m128i,
    std::arch::x86_64::__m128i,
    std::arch::x86_64::__m128i,
) {
    use std::arch::x86_64::*;

    // SAFETY: The caller guarantees that all source ranges are valid.
    unsafe {
        let y = _mm256_sub_epi16(_mm256_cvtepu8_epi16(_mm_loadu_si128(y_ptr.cast())), _mm256_set1_epi16(16));
        let u = _mm256_sub_epi16(
            _mm256_cvtepu8_epi16(_mm_unpacklo_epi8(_mm_loadl_epi64(u_ptr.cast()), _mm_loadl_epi64(u_ptr.cast()))),
            _mm256_set1_epi16(128),
        );
        let v = _mm256_sub_epi16(
            _mm256_cvtepu8_epi16(_mm_unpacklo_epi8(_mm_loadl_epi64(v_ptr.cast()), _mm_loadl_epi64(v_ptr.cast()))),
            _mm256_set1_epi16(128),
        );

        // The coefficients are scaled by 64 so all intermediate values fit in i16.
        // Saturating addition preserves the final u8 clamp for values above 255.
        let y = _mm256_add_epi16(
            _mm256_mullo_epi16(y, _mm256_set1_epi16(FIXED_Y_COEFFICIENT)),
            _mm256_srai_epi16::<1>(y),
        );
        let r_v = _mm256_add_epi16(
            _mm256_mullo_epi16(v, _mm256_set1_epi16(FIXED_RV_COEFFICIENT)),
            _mm256_srai_epi16::<2>(v),
        );
        let r = fixed_bt601_avx2(y, _mm256_setzero_si256(), r_v);
        let g = fixed_bt601_avx2(
            y,
            _mm256_mullo_epi16(u, _mm256_set1_epi16(FIXED_GU_COEFFICIENT)),
            _mm256_mullo_epi16(v, _mm256_set1_epi16(FIXED_GV_COEFFICIENT)),
        );
        let b = fixed_bt601_avx2(
            y,
            _mm256_mullo_epi16(u, _mm256_set1_epi16(FIXED_BU_COEFFICIENT)),
            _mm256_setzero_si256(),
        );
        (pack_avx2_i16_to_u8(r), pack_avx2_i16_to_u8(g), pack_avx2_i16_to_u8(b))
    }
}

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
#[allow(clippy::wildcard_imports)]
unsafe fn pack_rgb8_avx2_8(
    r: std::arch::x86_64::__m128i,
    g: std::arch::x86_64::__m128i,
    b: std::arch::x86_64::__m128i,
) -> (std::arch::x86_64::__m128i, std::arch::x86_64::__m128i) {
    use std::arch::x86_64::*;

    // AVX2 CPUs also support SSSE3, which provides the byte shuffles.
    let rg = _mm_unpacklo_epi8(r, g);
    let first_from_rg = _mm_shuffle_epi8(
        rg,
        _mm_setr_epi8(0, 1, -128, 2, 3, -128, 4, 5, -128, 6, 7, -128, 8, 9, -128, 10),
    );
    let first_from_b = _mm_shuffle_epi8(
        b,
        _mm_setr_epi8(-128, -128, 0, -128, -128, 1, -128, -128, 2, -128, -128, 3, -128, -128, 4, -128),
    );
    let tail_from_rg = _mm_shuffle_epi8(
        rg,
        _mm_setr_epi8(
            11, -128, 12, 13, -128, 14, 15, -128, -128, -128, -128, -128, -128, -128, -128, -128,
        ),
    );
    let tail_from_b = _mm_shuffle_epi8(
        b,
        _mm_setr_epi8(
            -128, 5, -128, -128, 6, -128, -128, 7, -128, -128, -128, -128, -128, -128, -128, -128,
        ),
    );

    (
        _mm_or_si128(first_from_rg, first_from_b),
        _mm_or_si128(tail_from_rg, tail_from_b),
    )
}

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
#[allow(clippy::wildcard_imports)]
unsafe fn fixed_bt601_avx2(
    y: std::arch::x86_64::__m256i,
    u: std::arch::x86_64::__m256i,
    v: std::arch::x86_64::__m256i,
) -> std::arch::x86_64::__m256i {
    use std::arch::x86_64::*;

    let value = _mm256_adds_epi16(_mm256_adds_epi16(y, u), _mm256_adds_epi16(v, _mm256_set1_epi16(32)));
    _mm256_srai_epi16::<6>(value)
}

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
#[allow(clippy::wildcard_imports)]
fn pack_avx2_i16_to_u8(value: std::arch::x86_64::__m256i) -> std::arch::x86_64::__m128i {
    use std::arch::x86_64::*;

    let values = _mm256_permute4x64_epi64::<216>(_mm256_packus_epi16(value, value));
    _mm256_castsi256_si128(values)
}

// RGB24 needs three byte-shuffles per sixteen pixels. Two 32-pixel row tiles
// amortize that setup, while smaller rows are faster with the scalar fallback.
const AVX2_RGB_TO_YUV_MIN_WIDTH: usize = 64;

#[derive(Copy, Clone)]
enum Rgb8Layout {
    Rgb24,
    Rgba32,
    Bgra32,
}

struct Rgb8ToYuvRowPair {
    source0: *const u8,
    source1: *const u8,
    y0: *mut u8,
    y1: *mut u8,
    u: *mut u8,
    v: *mut u8,
    width: usize,
    pixel_stride: usize,
    rgb_offsets: (usize, usize, usize),
}

/// Writes a supported contiguous RGB source to I420 using AVX2.
///
/// Returns `false` for unsupported layouts or dimensions too small to amortize
/// the RGB deinterleaving work.
#[target_feature(enable = "avx2")]
#[allow(clippy::similar_names)]
pub(super) unsafe fn write_rgb8_to_yuv420_avx2(rgb_data: &[u8], layout: Rgb8SourceLayout, target: &mut Yuv420Planes<'_>) -> bool {
    let (width, height) = layout.dimensions;
    let dimensions_padded = layout.dimensions_padded;
    let pixel_stride = layout.pixel_stride;
    let rgb_offsets = layout.rgb_offsets;
    let pixel_layout = match (pixel_stride, rgb_offsets) {
        (3, (0, 1, 2)) => Rgb8Layout::Rgb24,
        (4, (0, 1, 2)) => Rgb8Layout::Rgba32,
        (4, (2, 1, 0)) => Rgb8Layout::Bgra32,
        _ => return false,
    };

    if width < AVX2_RGB_TO_YUV_MIN_WIDTH
        || width % 2 != 0
        || height % 2 != 0
        || dimensions_padded.0 < width
        || dimensions_padded.1 < height
    {
        return false;
    }

    let Some(source_row_len) = dimensions_padded.0.checked_mul(pixel_stride) else {
        return false;
    };
    let Some(source_len) = source_row_len.checked_mul(height) else {
        return false;
    };
    let Some(y_len) = width.checked_mul(height) else {
        return false;
    };
    let uv_len = y_len / 4;
    if rgb_data.len() < source_len || target.y.len() < y_len || target.u.len() < uv_len || target.v.len() < uv_len {
        return false;
    }

    // SAFETY: The validated lengths above cover every source and destination
    // range. Each vector tile is guarded by a sixteen-pixel loop bound.
    unsafe {
        let source = rgb_data.as_ptr();
        let y_plane = target.y.as_mut_ptr();
        let u_plane = target.u.as_mut_ptr();
        let v_plane = target.v.as_mut_ptr();
        let half_width = width / 2;

        for row in (0..height).step_by(2) {
            let row_pair = Rgb8ToYuvRowPair {
                source0: source.add(row * source_row_len),
                source1: source.add((row + 1) * source_row_len),
                y0: y_plane.add(row * width),
                y1: y_plane.add((row + 1) * width),
                u: u_plane.add((row / 2) * half_width),
                v: v_plane.add((row / 2) * half_width),
                width,
                pixel_stride,
                rgb_offsets,
            };

            let mut x = 0;
            while x + 32 <= width {
                write_rgb8_to_yuv420_avx2_16(
                    row_pair.source0.add(x * pixel_stride),
                    row_pair.source1.add(x * pixel_stride),
                    row_pair.y0.add(x),
                    row_pair.y1.add(x),
                    row_pair.u.add(x / 2),
                    row_pair.v.add(x / 2),
                    pixel_layout,
                );
                write_rgb8_to_yuv420_avx2_16(
                    row_pair.source0.add((x + 16) * pixel_stride),
                    row_pair.source1.add((x + 16) * pixel_stride),
                    row_pair.y0.add(x + 16),
                    row_pair.y1.add(x + 16),
                    row_pair.u.add(x / 2 + 8),
                    row_pair.v.add(x / 2 + 8),
                    pixel_layout,
                );
                x += 32;
            }

            while x + 16 <= width {
                write_rgb8_to_yuv420_avx2_16(
                    row_pair.source0.add(x * pixel_stride),
                    row_pair.source1.add(x * pixel_stride),
                    row_pair.y0.add(x),
                    row_pair.y1.add(x),
                    row_pair.u.add(x / 2),
                    row_pair.v.add(x / 2),
                    pixel_layout,
                );
                x += 16;
            }

            if x != width {
                write_rgb8_to_yuv420_tail(&row_pair, x);
            }
        }
    }

    true
}

#[target_feature(enable = "avx2")]
#[allow(clippy::many_single_char_names, clippy::wildcard_imports)]
unsafe fn write_rgb8_to_yuv420_avx2_16(
    source0: *const u8,
    source1: *const u8,
    y0: *mut u8,
    y1: *mut u8,
    u: *mut u8,
    v: *mut u8,
    layout: Rgb8Layout,
) {
    use std::arch::x86_64::*;

    // SAFETY: The caller guarantees sixteen source pixels in each row and
    // sixteen luma plus eight chroma output bytes.
    unsafe {
        let (r0, g0, b0) = unpack_rgb8_avx2_16(source0, layout);
        let (r1, g1, b1) = unpack_rgb8_avx2_16(source1, layout);

        _mm_storeu_si128(y0.cast(), rgb_to_y_avx2_16(r0, g0, b0));
        _mm_storeu_si128(y1.cast(), rgb_to_y_avx2_16(r1, g1, b1));

        let r = average_2x2_avx2_16(r0, r1);
        let g = average_2x2_avx2_16(g0, g1);
        let b = average_2x2_avx2_16(b0, b1);
        _mm_storel_epi64(u.cast(), rgb_to_u_avx2_8(r, g, b));
        _mm_storel_epi64(v.cast(), rgb_to_v_avx2_8(r, g, b));
    }
}

#[target_feature(enable = "avx2")]
unsafe fn unpack_rgb8_avx2_16(
    source: *const u8,
    layout: Rgb8Layout,
) -> (
    std::arch::x86_64::__m128i,
    std::arch::x86_64::__m128i,
    std::arch::x86_64::__m128i,
) {
    match layout {
        // SAFETY: The caller provides sixteen pixels in the selected layout.
        Rgb8Layout::Rgb24 => unsafe { unpack_rgb24_avx2_16(source) },
        Rgb8Layout::Rgba32 => unsafe { unpack_rgba_avx2_16(source) },
        Rgb8Layout::Bgra32 => unsafe { unpack_bgra_avx2_16(source) },
    }
}

#[target_feature(enable = "avx2")]
#[allow(clippy::wildcard_imports)]
unsafe fn unpack_rgb24_avx2_16(
    source: *const u8,
) -> (
    std::arch::x86_64::__m128i,
    std::arch::x86_64::__m128i,
    std::arch::x86_64::__m128i,
) {
    use std::arch::x86_64::*;

    // SAFETY: The caller guarantees that the forty-eight source bytes are valid.
    unsafe {
        let first = _mm_loadu_si128(source.cast());
        let second = _mm_loadu_si128(source.add(16).cast());
        let third = _mm_loadu_si128(source.add(32).cast());

        let r_first = _mm_shuffle_epi8(
            first,
            _mm_setr_epi8(0, 3, 6, 9, 12, 15, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128),
        );
        let r_second = _mm_slli_si128::<6>(_mm_shuffle_epi8(
            second,
            _mm_setr_epi8(
                2, 5, 8, 11, 14, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
            ),
        ));
        let r_third = _mm_slli_si128::<11>(_mm_shuffle_epi8(
            third,
            _mm_setr_epi8(
                1, 4, 7, 10, 13, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
            ),
        ));

        let g_first = _mm_shuffle_epi8(
            first,
            _mm_setr_epi8(
                1, 4, 7, 10, 13, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
            ),
        );
        let g_second = _mm_slli_si128::<5>(_mm_shuffle_epi8(
            second,
            _mm_setr_epi8(0, 3, 6, 9, 12, 15, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128),
        ));
        let g_third = _mm_slli_si128::<11>(_mm_shuffle_epi8(
            third,
            _mm_setr_epi8(
                2, 5, 8, 11, 14, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
            ),
        ));

        let b_first = _mm_shuffle_epi8(
            first,
            _mm_setr_epi8(
                2, 5, 8, 11, 14, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
            ),
        );
        let b_second = _mm_slli_si128::<5>(_mm_shuffle_epi8(
            second,
            _mm_setr_epi8(
                1, 4, 7, 10, 13, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
            ),
        ));
        let b_third = _mm_slli_si128::<10>(_mm_shuffle_epi8(
            third,
            _mm_setr_epi8(0, 3, 6, 9, 12, 15, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128),
        ));

        (
            _mm_or_si128(_mm_or_si128(r_first, r_second), r_third),
            _mm_or_si128(_mm_or_si128(g_first, g_second), g_third),
            _mm_or_si128(_mm_or_si128(b_first, b_second), b_third),
        )
    }
}

#[target_feature(enable = "avx2")]
unsafe fn unpack_rgba_avx2_16(
    source: *const u8,
) -> (
    std::arch::x86_64::__m128i,
    std::arch::x86_64::__m128i,
    std::arch::x86_64::__m128i,
) {
    // SAFETY: The caller guarantees sixteen RGBA pixels.
    unsafe { unpack_xgba_avx2_16::<0, 1, 2>(source) }
}

#[target_feature(enable = "avx2")]
unsafe fn unpack_bgra_avx2_16(
    source: *const u8,
) -> (
    std::arch::x86_64::__m128i,
    std::arch::x86_64::__m128i,
    std::arch::x86_64::__m128i,
) {
    // SAFETY: The caller guarantees sixteen BGRA pixels.
    unsafe { unpack_xgba_avx2_16::<2, 1, 0>(source) }
}

#[target_feature(enable = "avx2")]
#[allow(clippy::wildcard_imports)]
unsafe fn unpack_xgba_avx2_16<const R: i8, const G: i8, const B: i8>(
    source: *const u8,
) -> (
    std::arch::x86_64::__m128i,
    std::arch::x86_64::__m128i,
    std::arch::x86_64::__m128i,
) {
    use std::arch::x86_64::*;

    // SAFETY: The caller guarantees that the sixty-four source bytes are valid.
    unsafe {
        let first = _mm_loadu_si128(source.cast());
        let second = _mm_loadu_si128(source.add(16).cast());
        let third = _mm_loadu_si128(source.add(32).cast());
        let fourth = _mm_loadu_si128(source.add(48).cast());
        (
            unpack_xgba_component_avx2_16::<R>(first, second, third, fourth),
            unpack_xgba_component_avx2_16::<G>(first, second, third, fourth),
            unpack_xgba_component_avx2_16::<B>(first, second, third, fourth),
        )
    }
}

#[target_feature(enable = "avx2")]
#[allow(clippy::wildcard_imports)]
unsafe fn unpack_xgba_component_avx2_16<const OFFSET: i8>(
    first: std::arch::x86_64::__m128i,
    second: std::arch::x86_64::__m128i,
    third: std::arch::x86_64::__m128i,
    fourth: std::arch::x86_64::__m128i,
) -> std::arch::x86_64::__m128i {
    use std::arch::x86_64::*;

    let mask = _mm_setr_epi8(
        OFFSET,
        OFFSET + 4,
        OFFSET + 8,
        OFFSET + 12,
        -128,
        -128,
        -128,
        -128,
        -128,
        -128,
        -128,
        -128,
        -128,
        -128,
        -128,
        -128,
    );
    let first = _mm_shuffle_epi8(first, mask);
    let second = _mm_slli_si128::<4>(_mm_shuffle_epi8(second, mask));
    let third = _mm_slli_si128::<8>(_mm_shuffle_epi8(third, mask));
    let fourth = _mm_slli_si128::<12>(_mm_shuffle_epi8(fourth, mask));
    _mm_or_si128(_mm_or_si128(first, second), _mm_or_si128(third, fourth))
}

#[target_feature(enable = "avx2")]
#[allow(clippy::wildcard_imports)]
unsafe fn rgb_to_y_avx2_16(
    r: std::arch::x86_64::__m128i,
    g: std::arch::x86_64::__m128i,
    b: std::arch::x86_64::__m128i,
) -> std::arch::x86_64::__m128i {
    use std::arch::x86_64::*;

    let r = _mm256_cvtepu8_epi16(r);
    let g = _mm256_cvtepu8_epi16(g);
    let b = _mm256_cvtepu8_epi16(b);
    let y = _mm256_add_epi16(
        _mm256_add_epi16(
            _mm256_mullo_epi16(r, _mm256_set1_epi16(66)),
            _mm256_mullo_epi16(g, _mm256_set1_epi16(129)),
        ),
        _mm256_mullo_epi16(b, _mm256_set1_epi16(25)),
    );
    let y = _mm256_add_epi16(_mm256_srli_epi16::<8>(y), _mm256_set1_epi16(16));
    pack_avx2_i16_to_u8(y)
}

#[target_feature(enable = "avx2")]
#[allow(clippy::wildcard_imports)]
unsafe fn average_2x2_avx2_16(row0: std::arch::x86_64::__m128i, row1: std::arch::x86_64::__m128i) -> std::arch::x86_64::__m128i {
    use std::arch::x86_64::*;

    let zero = _mm_setzero_si128();
    let pair_sum = |row0, row1| {
        let low = _mm_add_epi16(_mm_unpacklo_epi8(row0, zero), _mm_unpacklo_epi8(row1, zero));
        let high = _mm_add_epi16(_mm_unpackhi_epi8(row0, zero), _mm_unpackhi_epi8(row1, zero));
        _mm_packs_epi32(_mm_madd_epi16(low, _mm_set1_epi16(1)), _mm_madd_epi16(high, _mm_set1_epi16(1)))
    };
    _mm_srli_epi16::<2>(_mm_add_epi16(pair_sum(row0, row1), _mm_set1_epi16(2)))
}

#[target_feature(enable = "avx2")]
#[allow(clippy::wildcard_imports)]
unsafe fn rgb_to_u_avx2_8(
    r: std::arch::x86_64::__m128i,
    g: std::arch::x86_64::__m128i,
    b: std::arch::x86_64::__m128i,
) -> std::arch::x86_64::__m128i {
    use std::arch::x86_64::*;

    let u = _mm_add_epi16(
        _mm_add_epi16(_mm_mullo_epi16(r, _mm_set1_epi16(-38)), _mm_mullo_epi16(g, _mm_set1_epi16(-74))),
        _mm_mullo_epi16(b, _mm_set1_epi16(112)),
    );
    let u = _mm_add_epi16(_mm_srai_epi16::<8>(u), _mm_set1_epi16(128));
    _mm_packus_epi16(u, _mm_setzero_si128())
}

#[target_feature(enable = "avx2")]
#[allow(clippy::wildcard_imports)]
unsafe fn rgb_to_v_avx2_8(
    r: std::arch::x86_64::__m128i,
    g: std::arch::x86_64::__m128i,
    b: std::arch::x86_64::__m128i,
) -> std::arch::x86_64::__m128i {
    use std::arch::x86_64::*;

    let v = _mm_add_epi16(
        _mm_add_epi16(_mm_mullo_epi16(r, _mm_set1_epi16(112)), _mm_mullo_epi16(g, _mm_set1_epi16(-94))),
        _mm_mullo_epi16(b, _mm_set1_epi16(-18)),
    );
    let v = _mm_add_epi16(_mm_srai_epi16::<8>(v), _mm_set1_epi16(128));
    _mm_packus_epi16(v, _mm_setzero_si128())
}

#[allow(clippy::many_single_char_names)]
unsafe fn write_rgb8_to_yuv420_tail(row_pair: &Rgb8ToYuvRowPair, start_x: usize) {
    let (r_offset, g_offset, b_offset) = row_pair.rgb_offsets;

    // SAFETY: The caller provides the remaining even number of pixels and
    // matching output ranges.
    unsafe {
        for x in start_x..row_pair.width {
            let pixel_offset = x * row_pair.pixel_stride;
            let r0 = *row_pair.source0.add(pixel_offset + r_offset);
            let g0 = *row_pair.source0.add(pixel_offset + g_offset);
            let b0 = *row_pair.source0.add(pixel_offset + b_offset);
            let r1 = *row_pair.source1.add(pixel_offset + r_offset);
            let g1 = *row_pair.source1.add(pixel_offset + g_offset);
            let b1 = *row_pair.source1.add(pixel_offset + b_offset);
            *row_pair.y0.add(x) = rgb_to_y_scalar(r0, g0, b0);
            *row_pair.y1.add(x) = rgb_to_y_scalar(r1, g1, b1);
        }

        for x in (start_x..row_pair.width).step_by(2) {
            let first = x * row_pair.pixel_stride;
            let second = first + row_pair.pixel_stride;
            let r = average_2x2_scalar(
                *row_pair.source0.add(first + r_offset),
                *row_pair.source0.add(second + r_offset),
                *row_pair.source1.add(first + r_offset),
                *row_pair.source1.add(second + r_offset),
            );
            let g = average_2x2_scalar(
                *row_pair.source0.add(first + g_offset),
                *row_pair.source0.add(second + g_offset),
                *row_pair.source1.add(first + g_offset),
                *row_pair.source1.add(second + g_offset),
            );
            let b = average_2x2_scalar(
                *row_pair.source0.add(first + b_offset),
                *row_pair.source0.add(second + b_offset),
                *row_pair.source1.add(first + b_offset),
                *row_pair.source1.add(second + b_offset),
            );
            *row_pair.u.add(x / 2) = (((-38 * r + 112 * b - 74 * g) >> 8) + 128) as u8;
            *row_pair.v.add(x / 2) = (((112 * r - 18 * b - 94 * g) >> 8) + 128) as u8;
        }
    }
}

#[allow(clippy::cast_possible_truncation)]
fn rgb_to_y_scalar(r: u8, g: u8, b: u8) -> u8 {
    (((66 * u32::from(r) + 129 * u32::from(g) + 25 * u32::from(b)) >> 8) + 16) as u8
}

fn average_2x2_scalar(a: u8, b: u8, c: u8, d: u8) -> i16 {
    (i16::from(a) + i16::from(b) + i16::from(c) + i16::from(d) + 2) / 4
}