yuv 0.8.20

High performance utilities for YUV format handling and conversion.
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
/*
 * Copyright (c) Radzivon Bartoshyk, 10/2024. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1.  Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2.  Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3.  Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

use crate::avx512bw::avx512_utils::{
    _mm256_interleave_epi8, _mm512_expand8_unordered_to_10, avx512_create,
    avx512_store_rgba_for_yuv_u8, avx512_zip_epi8,
};
use crate::internals::ProcessedOffset;
use crate::yuv_support::{
    CbCrInverseTransform, YuvChromaRange, YuvChromaSubsampling, YuvSourceChannels,
};
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

pub(crate) fn avx512_yuv_to_rgba<
    const DESTINATION_CHANNELS: u8,
    const SAMPLING: u8,
    const HAS_VBMI: bool,
>(
    range: &YuvChromaRange,
    transform: &CbCrInverseTransform<i32>,
    y_plane: &[u8],
    u_plane: &[u8],
    v_plane: &[u8],
    rgba: &mut [u8],
    start_cx: usize,
    start_ux: usize,
    width: usize,
) -> ProcessedOffset {
    unsafe {
        if HAS_VBMI {
            avx512_yuv_to_rgba_bmi_impl::<DESTINATION_CHANNELS, SAMPLING>(
                range, transform, y_plane, u_plane, v_plane, rgba, start_cx, start_ux, width,
            )
        } else {
            avx512_yuv_to_rgba_def_impl::<DESTINATION_CHANNELS, SAMPLING>(
                range, transform, y_plane, u_plane, v_plane, rgba, start_cx, start_ux, width,
            )
        }
    }
}

#[target_feature(enable = "avx512bw", enable = "avx512f", enable = "avx512vbmi")]
unsafe fn avx512_yuv_to_rgba_bmi_impl<const DESTINATION_CHANNELS: u8, const SAMPLING: u8>(
    range: &YuvChromaRange,
    transform: &CbCrInverseTransform<i32>,
    y_plane: &[u8],
    u_plane: &[u8],
    v_plane: &[u8],
    rgba: &mut [u8],
    start_cx: usize,
    start_ux: usize,
    width: usize,
) -> ProcessedOffset {
    avx512_yuv_to_rgba_impl::<DESTINATION_CHANNELS, SAMPLING, true>(
        range, transform, y_plane, u_plane, v_plane, rgba, start_cx, start_ux, width,
    )
}

#[target_feature(enable = "avx512bw", enable = "avx512f")]
unsafe fn avx512_yuv_to_rgba_def_impl<const DESTINATION_CHANNELS: u8, const SAMPLING: u8>(
    range: &YuvChromaRange,
    transform: &CbCrInverseTransform<i32>,
    y_plane: &[u8],
    u_plane: &[u8],
    v_plane: &[u8],
    rgba: &mut [u8],
    start_cx: usize,
    start_ux: usize,
    width: usize,
) -> ProcessedOffset {
    avx512_yuv_to_rgba_impl::<DESTINATION_CHANNELS, SAMPLING, false>(
        range, transform, y_plane, u_plane, v_plane, rgba, start_cx, start_ux, width,
    )
}

#[inline(always)]
unsafe fn avx512_yuv_to_rgba_impl<
    const DESTINATION_CHANNELS: u8,
    const SAMPLING: u8,
    const HAS_VBMI: bool,
>(
    range: &YuvChromaRange,
    transform: &CbCrInverseTransform<i32>,
    y_plane: &[u8],
    u_plane: &[u8],
    v_plane: &[u8],
    rgba: &mut [u8],
    start_cx: usize,
    start_ux: usize,
    width: usize,
) -> ProcessedOffset {
    let chroma_subsampling: YuvChromaSubsampling = SAMPLING.into();
    let destination_channels: YuvSourceChannels = DESTINATION_CHANNELS.into();
    let channels = destination_channels.get_channels_count();

    let mut cx = start_cx;
    let mut uv_x = start_ux;
    let y_ptr = y_plane.as_ptr();
    let u_ptr = u_plane.as_ptr();
    let v_ptr = v_plane.as_ptr();
    let rgba_ptr = rgba.as_mut_ptr();

    let y_corr = _mm512_set1_epi8(range.bias_y as i8);
    let uv_corr = _mm512_set1_epi16((range.bias_uv as i16) << 2);
    let v_luma_coeff = _mm512_set1_epi16(transform.y_coef as i16);
    let v_cr_coeff = _mm512_set1_epi16(transform.cr_coef as i16);
    let v_cb_coeff = _mm512_set1_epi16(transform.cb_coef as i16);
    let v_g_coeff_1 = _mm512_set1_epi16(transform.g_coeff_1 as i16);
    let v_g_coeff_2 = _mm512_set1_epi16(transform.g_coeff_2 as i16);

    while cx + 128 < width {
        let y_vl0 = _mm512_loadu_si512(y_ptr.add(cx) as *const _);
        let y_vl1 = _mm512_loadu_si512(y_ptr.add(cx + 64) as *const _);

        let (u_high00, v_high00, u_low00, v_low00, u_high10, v_high10, u_low10, v_low10);

        match chroma_subsampling {
            YuvChromaSubsampling::Yuv420 | YuvChromaSubsampling::Yuv422 => {
                let u_values_full = _mm512_loadu_si512(u_ptr.add(uv_x) as *const _);
                let v_values_full = _mm512_loadu_si512(v_ptr.add(uv_x) as *const _);

                let (u_values0, u_values1) =
                    avx512_zip_epi8::<HAS_VBMI>(u_values_full, u_values_full);
                let (v_values0, v_values1) =
                    avx512_zip_epi8::<HAS_VBMI>(v_values_full, v_values_full);

                let uhw0 = _mm512_unpackhi_epi8(u_values0, _mm512_setzero_si512());
                let vhw0 = _mm512_unpackhi_epi8(v_values0, _mm512_setzero_si512());
                let ulw0 = _mm512_unpacklo_epi8(u_values0, _mm512_setzero_si512());
                let vlw0 = _mm512_unpacklo_epi8(v_values0, _mm512_setzero_si512());

                u_high00 = _mm512_slli_epi16::<2>(uhw0);
                v_high00 = _mm512_slli_epi16::<2>(vhw0);
                u_low00 = _mm512_slli_epi16::<2>(ulw0);
                v_low00 = _mm512_slli_epi16::<2>(vlw0);

                let uhw1 = _mm512_unpackhi_epi8(u_values1, _mm512_setzero_si512());
                let vhw1 = _mm512_unpackhi_epi8(v_values1, _mm512_setzero_si512());
                let ulw1 = _mm512_unpacklo_epi8(u_values1, _mm512_setzero_si512());
                let vlw1 = _mm512_unpacklo_epi8(v_values1, _mm512_setzero_si512());

                u_high10 = _mm512_slli_epi16::<2>(uhw1);
                v_high10 = _mm512_slli_epi16::<2>(vhw1);
                u_low10 = _mm512_slli_epi16::<2>(ulw1);
                v_low10 = _mm512_slli_epi16::<2>(vlw1);
            }
            YuvChromaSubsampling::Yuv444 => {
                let u_values0 = _mm512_loadu_si512(u_ptr.add(uv_x) as *const _);
                let u_values1 = _mm512_loadu_si512(u_ptr.add(uv_x + 64) as *const _);
                let v_values0 = _mm512_loadu_si512(v_ptr.add(uv_x) as *const _);
                let v_values1 = _mm512_loadu_si512(v_ptr.add(uv_x + 64) as *const _);

                let uhw0 = _mm512_unpackhi_epi8(u_values0, _mm512_setzero_si512());
                let vhw0 = _mm512_unpackhi_epi8(v_values0, _mm512_setzero_si512());
                let ulw0 = _mm512_unpacklo_epi8(u_values0, _mm512_setzero_si512());
                let vlw0 = _mm512_unpacklo_epi8(v_values0, _mm512_setzero_si512());

                u_high00 = _mm512_slli_epi16::<2>(uhw0);
                v_high00 = _mm512_slli_epi16::<2>(vhw0);
                u_low00 = _mm512_slli_epi16::<2>(ulw0);
                v_low00 = _mm512_slli_epi16::<2>(vlw0);

                let uhw1 = _mm512_unpackhi_epi8(u_values1, _mm512_setzero_si512());
                let vhw1 = _mm512_unpackhi_epi8(v_values1, _mm512_setzero_si512());
                let ulw1 = _mm512_unpacklo_epi8(u_values1, _mm512_setzero_si512());
                let vlw1 = _mm512_unpacklo_epi8(v_values1, _mm512_setzero_si512());

                u_high10 = _mm512_slli_epi16::<2>(uhw1);
                v_high10 = _mm512_slli_epi16::<2>(vhw1);
                u_low10 = _mm512_slli_epi16::<2>(ulw1);
                v_low10 = _mm512_slli_epi16::<2>(vlw1);
            }
        }

        let y_values0 = _mm512_subs_epu8(y_vl0, y_corr);
        let y_values1 = _mm512_subs_epu8(y_vl1, y_corr);

        let y0_10 = _mm512_expand8_unordered_to_10(y_values0);
        let y1_10 = _mm512_expand8_unordered_to_10(y_values1);

        let u_high0 = _mm512_sub_epi16(u_high00, uv_corr);
        let v_high0 = _mm512_sub_epi16(v_high00, uv_corr);
        let y_high0 = _mm512_mulhrs_epi16(y0_10.1, v_luma_coeff);

        let u_high1 = _mm512_sub_epi16(u_high10, uv_corr);
        let v_high1 = _mm512_sub_epi16(v_high10, uv_corr);
        let y_high1 = _mm512_mulhrs_epi16(y1_10.1, v_luma_coeff);

        let r_high0 = _mm512_add_epi16(y_high0, _mm512_mulhrs_epi16(v_high0, v_cr_coeff));
        let b_high0 = _mm512_add_epi16(y_high0, _mm512_mulhrs_epi16(u_high0, v_cb_coeff));
        let g_high0 = _mm512_sub_epi16(
            y_high0,
            _mm512_add_epi16(
                _mm512_mulhrs_epi16(v_high0, v_g_coeff_1),
                _mm512_mulhrs_epi16(u_high0, v_g_coeff_2),
            ),
        );

        let r_high1 = _mm512_add_epi16(y_high1, _mm512_mulhrs_epi16(v_high1, v_cr_coeff));
        let b_high1 = _mm512_add_epi16(y_high1, _mm512_mulhrs_epi16(u_high1, v_cb_coeff));
        let g_high1 = _mm512_sub_epi16(
            y_high1,
            _mm512_add_epi16(
                _mm512_mulhrs_epi16(v_high1, v_g_coeff_1),
                _mm512_mulhrs_epi16(u_high1, v_g_coeff_2),
            ),
        );

        let u_low0 = _mm512_sub_epi16(u_low00, uv_corr);
        let v_low0 = _mm512_sub_epi16(v_low00, uv_corr);
        let y_low0 = _mm512_mulhrs_epi16(y0_10.0, v_luma_coeff);

        let u_low1 = _mm512_sub_epi16(u_low10, uv_corr);
        let v_low1 = _mm512_sub_epi16(v_low10, uv_corr);
        let y_low1 = _mm512_mulhrs_epi16(y1_10.0, v_luma_coeff);

        let r_low0 = _mm512_add_epi16(y_low0, _mm512_mulhrs_epi16(v_low0, v_cr_coeff));
        let b_low0 = _mm512_add_epi16(y_low0, _mm512_mulhrs_epi16(u_low0, v_cb_coeff));
        let g_low0 = _mm512_sub_epi16(
            y_low0,
            _mm512_add_epi16(
                _mm512_mulhrs_epi16(v_low0, v_g_coeff_1),
                _mm512_mulhrs_epi16(u_low0, v_g_coeff_2),
            ),
        );

        let r_low1 = _mm512_add_epi16(y_low1, _mm512_mulhrs_epi16(v_low1, v_cr_coeff));
        let b_low1 = _mm512_add_epi16(y_low1, _mm512_mulhrs_epi16(u_low1, v_cb_coeff));
        let g_low1 = _mm512_sub_epi16(
            y_low1,
            _mm512_add_epi16(
                _mm512_mulhrs_epi16(v_low1, v_g_coeff_1),
                _mm512_mulhrs_epi16(u_low1, v_g_coeff_2),
            ),
        );

        let r_values0 = _mm512_packus_epi16(r_low0, r_high0);
        let g_values0 = _mm512_packus_epi16(g_low0, g_high0);
        let b_values0 = _mm512_packus_epi16(b_low0, b_high0);

        let r_values1 = _mm512_packus_epi16(r_low1, r_high1);
        let g_values1 = _mm512_packus_epi16(g_low1, g_high1);
        let b_values1 = _mm512_packus_epi16(b_low1, b_high1);

        let dst_shift = cx * channels;

        let v_alpha = _mm512_set1_epi8(255u8 as i8);
        avx512_store_rgba_for_yuv_u8::<DESTINATION_CHANNELS, HAS_VBMI>(
            rgba_ptr.add(dst_shift),
            r_values0,
            g_values0,
            b_values0,
            v_alpha,
        );

        avx512_store_rgba_for_yuv_u8::<DESTINATION_CHANNELS, HAS_VBMI>(
            rgba_ptr.add(dst_shift + 64 * channels),
            r_values1,
            g_values1,
            b_values1,
            v_alpha,
        );

        cx += 128;

        match chroma_subsampling {
            YuvChromaSubsampling::Yuv420 | YuvChromaSubsampling::Yuv422 => {
                uv_x += 64;
            }
            YuvChromaSubsampling::Yuv444 => {
                uv_x += 128;
            }
        }
    }

    while cx + 64 < width {
        let y_values = _mm512_subs_epu8(_mm512_loadu_si512(y_ptr.add(cx) as *const _), y_corr);

        let (u_high0, v_high0, u_low0, v_low0);

        match chroma_subsampling {
            YuvChromaSubsampling::Yuv420 | YuvChromaSubsampling::Yuv422 => {
                let u_values = _mm256_loadu_si256(u_ptr.add(uv_x) as *const _);
                let v_values = _mm256_loadu_si256(v_ptr.add(uv_x) as *const _);

                let (lu_low0, lu_high0) = _mm256_interleave_epi8(u_values, u_values);
                let (lv_low0, lv_high0) = _mm256_interleave_epi8(v_values, v_values);

                let u_values = avx512_create(lu_low0, lu_high0);
                let v_values = avx512_create(lv_low0, lv_high0);

                u_high0 =
                    _mm512_slli_epi16::<2>(_mm512_unpackhi_epi8(u_values, _mm512_setzero_si512()));
                v_high0 =
                    _mm512_slli_epi16::<2>(_mm512_unpackhi_epi8(v_values, _mm512_setzero_si512()));
                u_low0 =
                    _mm512_slli_epi16::<2>(_mm512_unpacklo_epi8(u_values, _mm512_setzero_si512()));
                v_low0 =
                    _mm512_slli_epi16::<2>(_mm512_unpacklo_epi8(v_values, _mm512_setzero_si512()));
            }
            YuvChromaSubsampling::Yuv444 => {
                let u_values = _mm512_loadu_si512(u_ptr.add(uv_x) as *const _);
                let v_values = _mm512_loadu_si512(v_ptr.add(uv_x) as *const _);

                u_high0 =
                    _mm512_slli_epi16::<2>(_mm512_unpackhi_epi8(u_values, _mm512_setzero_si512()));
                v_high0 =
                    _mm512_slli_epi16::<2>(_mm512_unpackhi_epi8(v_values, _mm512_setzero_si512()));
                u_low0 =
                    _mm512_slli_epi16::<2>(_mm512_unpacklo_epi8(u_values, _mm512_setzero_si512()));
                v_low0 =
                    _mm512_slli_epi16::<2>(_mm512_unpacklo_epi8(v_values, _mm512_setzero_si512()));
            }
        }

        let y_10 = _mm512_expand8_unordered_to_10(y_values);

        let u_high = _mm512_sub_epi16(u_high0, uv_corr);
        let v_high = _mm512_sub_epi16(v_high0, uv_corr);
        let y_high = _mm512_mulhrs_epi16(y_10.1, v_luma_coeff);

        let r_high = _mm512_add_epi16(y_high, _mm512_mulhrs_epi16(v_high, v_cr_coeff));
        let b_high = _mm512_add_epi16(y_high, _mm512_mulhrs_epi16(u_high, v_cb_coeff));
        let g_high = _mm512_sub_epi16(
            y_high,
            _mm512_add_epi16(
                _mm512_mulhrs_epi16(v_high, v_g_coeff_1),
                _mm512_mulhrs_epi16(u_high, v_g_coeff_2),
            ),
        );

        let u_low = _mm512_sub_epi16(u_low0, uv_corr);
        let v_low = _mm512_sub_epi16(v_low0, uv_corr);
        let y_low = _mm512_mulhrs_epi16(y_10.0, v_luma_coeff);

        let r_low = _mm512_add_epi16(y_low, _mm512_mulhrs_epi16(v_low, v_cr_coeff));
        let b_low = _mm512_add_epi16(y_low, _mm512_mulhrs_epi16(u_low, v_cb_coeff));
        let g_low = _mm512_sub_epi16(
            y_low,
            _mm512_add_epi16(
                _mm512_mulhrs_epi16(v_low, v_g_coeff_1),
                _mm512_mulhrs_epi16(u_low, v_g_coeff_2),
            ),
        );

        let r_values = _mm512_packus_epi16(r_low, r_high);
        let g_values = _mm512_packus_epi16(g_low, g_high);
        let b_values = _mm512_packus_epi16(b_low, b_high);

        let dst_shift = cx * channels;

        let v_alpha = _mm512_set1_epi8(255u8 as i8);

        avx512_store_rgba_for_yuv_u8::<DESTINATION_CHANNELS, HAS_VBMI>(
            rgba_ptr.add(dst_shift),
            r_values,
            g_values,
            b_values,
            v_alpha,
        );

        cx += 64;

        match chroma_subsampling {
            YuvChromaSubsampling::Yuv420 | YuvChromaSubsampling::Yuv422 => {
                uv_x += 32;
            }
            YuvChromaSubsampling::Yuv444 => {
                uv_x += 64;
            }
        }
    }

    ProcessedOffset { cx, ux: uv_x }
}