diff --git a/Cargo.lock b/Cargo.lock index 3a37323..7171621 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -994,7 +994,7 @@ dependencies = [ [[package]] name = "yuvutils-rs" -version = "0.4.5" +version = "0.4.6" [[package]] name = "zune-core" diff --git a/Cargo.toml b/Cargo.toml index d79921b..2abccff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,11 +2,11 @@ workspace = { members = ["app"] } [package] name = "yuvutils-rs" -version = "0.4.5" +version = "0.4.6" edition = "2021" description = "High performance utilities for YUV format handling and conversion." readme = "README.md" -keywords = ["yuv", "ycbcr", "yuv2rgb", "rgb2yuv"] +keywords = ["yuv", "ycbcr", "yuv2rgb", "rgb2yuv", "converter"] license = "Apache-2.0 OR BSD-3-Clause" authors = ["Radzivon Bartoshyk"] documentation = "https://github.com/awxkee/yuvutils-rs" diff --git a/README.md b/README.md index d875103..6ada913 100644 --- a/README.md +++ b/README.md @@ -83,3 +83,10 @@ ycgco420_to_rgb(&y_plane, y_stride, width, height, YuvRange::TV); ``` + +This project is licensed under either of + +- BSD-3-Clause License (see [LICENSE](LICENSE.md)) +- Apache License, Version 2.0 (see [LICENSE](LICENSE-APACHE.md)) + +at your option. diff --git a/src/avx2/avx2_utils.rs b/src/avx2/avx2_utils.rs index 3e17547..89aa243 100644 --- a/src/avx2/avx2_utils.rs +++ b/src/avx2/avx2_utils.rs @@ -128,8 +128,7 @@ pub unsafe fn avx2_interleave_odd(x: __m256i) -> __m256i { 21, 21, 23, 23, 25, 25, 27, 27, 29, 29, 31, 31); - let new_lane = _mm256_shuffle_epi8(x, shuffle); - new_lane + _mm256_shuffle_epi8(x, shuffle) } #[inline] @@ -144,8 +143,7 @@ pub unsafe fn avx2_interleave_even(x: __m256i) -> __m256i { 20, 20, 22, 22, 24, 24, 26, 26, 28, 28, 30, 30); - let new_lane = _mm256_shuffle_epi8(x, shuffle); - new_lane + _mm256_shuffle_epi8(x, shuffle) } #[inline] diff --git a/src/avx2/rgb_to_ycgco.rs b/src/avx2/rgb_to_ycgco.rs index 94e2350..2f4e241 100644 --- a/src/avx2/rgb_to_ycgco.rs +++ b/src/avx2/rgb_to_ycgco.rs @@ -153,5 +153,5 @@ pub unsafe fn avx2_rgb_to_ycgco_row let u_pixels; let v_pixels; - let y_pixels; let y_ptr = y_plane.as_ptr().add(y_pos); - y_pixels = ( + let y_pixels = ( _mm256_loadu_si256(y_ptr as *const __m256i), _mm256_loadu_si256(y_ptr.add(32) as *const __m256i), ); @@ -70,21 +69,12 @@ pub unsafe fn yuv_to_yuy2_avx2_row let (low_y, high_y) = _mm256_deinterleave_x2_epi8(y_pixels.0, y_pixels.1); - let storage; - match yuy2_target { - Yuy2Description::YUYV => { - storage = (low_y, u_pixels, high_y, v_pixels); - } - Yuy2Description::UYVY => { - storage = (u_pixels, low_y, v_pixels, high_y); - } - Yuy2Description::YVYU => { - storage = (low_y, v_pixels, high_y, u_pixels); - } - Yuy2Description::VYUY => { - storage = (v_pixels, low_y, u_pixels, high_y); - } - } + let storage = match yuy2_target { + Yuy2Description::YUYV => (low_y, u_pixels, high_y, v_pixels), + Yuy2Description::UYVY => (u_pixels, low_y, v_pixels, high_y), + Yuy2Description::YVYU => (low_y, v_pixels, high_y, u_pixels), + Yuy2Description::VYUY => (v_pixels, low_y, u_pixels, high_y), + }; let dst_offset = yuy2_offset + x * 4; diff --git a/src/avx2/yuy2_to_rgb.rs b/src/avx2/yuy2_to_rgb.rs index dfc43e7..4b50746 100644 --- a/src/avx2/yuy2_to_rgb.rs +++ b/src/avx2/yuy2_to_rgb.rs @@ -41,8 +41,8 @@ pub fn yuy2_to_rgb_avx( let v_luma_coeff = _mm256_set1_epi16(transform.y_coef as i16); let v_cr_coeff = _mm256_set1_epi16(transform.cr_coef as i16); let v_cb_coeff = _mm256_set1_epi16(transform.cb_coef as i16); - let v_g_coeff_1 = _mm256_set1_epi16(-1i16 * transform.g_coeff_1 as i16); - let v_g_coeff_2 = _mm256_set1_epi16(-1i16 * transform.g_coeff_2 as i16); + let v_g_coeff_1 = _mm256_set1_epi16(-(transform.g_coeff_1 as i16)); + let v_g_coeff_2 = _mm256_set1_epi16(-(transform.g_coeff_2 as i16)); let v_alpha = _mm256_set1_epi8(255u8 as i8); let zeros = _mm256_setzero_si256(); let rounding_const = _mm256_set1_epi16(1 << 5); diff --git a/src/avx512bw/avx512_utils.rs b/src/avx512bw/avx512_utils.rs index 7811437..13af90c 100644 --- a/src/avx512bw/avx512_utils.rs +++ b/src/avx512bw/avx512_utils.rs @@ -109,7 +109,7 @@ pub unsafe fn avx512_div_by255(v: __m512i) -> __m512i { let x = _mm512_adds_epi16(v, rounding); let multiplier = _mm512_set1_epi16(-32640); let r = _mm512_mulhi_epu16(x, multiplier); - return _mm512_srli_epi16::<7>(r); + _mm512_srli_epi16::<7>(r) } #[inline] diff --git a/src/avx512bw/ycgco_to_rgb.rs b/src/avx512bw/ycgco_to_rgb.rs index d40cdc5..ea1361b 100644 --- a/src/avx512bw/ycgco_to_rgb.rs +++ b/src/avx512bw/ycgco_to_rgb.rs @@ -201,5 +201,5 @@ pub unsafe fn avx512_ycgco_to_rgb_row( let mut gbr_start_ptr = gbr.add(3 * _cx); let mut rgb_start_ptr = rgb.add(channels * _cx); - let v_max_colors = vdupq_n_u16(1 << bit_depth - 1); + let v_max_colors = vdupq_n_u16((1 << bit_depth) - 1); let rgb_part_size = channels * 8; diff --git a/src/neon/rgb_to_y.rs b/src/neon/rgb_to_y.rs index dd2da8c..94831d4 100644 --- a/src/neon/rgb_to_y.rs +++ b/src/neon/rgb_to_y.rs @@ -111,5 +111,5 @@ pub unsafe fn neon_rgb_to_y_row( cx += 16; } - return cx; + cx } diff --git a/src/neon/rgb_to_ycgco.rs b/src/neon/rgb_to_ycgco.rs index 9675a90..bcea196 100644 --- a/src/neon/rgb_to_ycgco.rs +++ b/src/neon/rgb_to_ycgco.rs @@ -138,5 +138,5 @@ pub unsafe fn neon_rgb_to_ycgco_row( let mut cx = start_cx; - while cx + 16 < width as usize { + while cx + 16 < width { let y_values = vsubq_u8(vld1q_u8(y_ptr.add(y_offset + cx)), y_corr); let y_high = vreinterpretq_s16_u16(vmull_high_u8(y_values, v_luma_coeff)); diff --git a/src/neon/ycgco_to_rgb.rs b/src/neon/ycgco_to_rgb.rs index f86d512..cfdc22f 100644 --- a/src/neon/ycgco_to_rgb.rs +++ b/src/neon/ycgco_to_rgb.rs @@ -152,5 +152,5 @@ pub unsafe fn neon_ycgco_to_rgb_row(y_vl); } - y_values = vsubq_s16(vreinterpretq_s16_u16(y_vl), y_corr); + let y_values: int16x8_t = vsubq_s16(vreinterpretq_s16_u16(y_vl), y_corr); match chroma_subsampling { YuvChromaSample::YUV420 | YuvChromaSample::YUV422 => { diff --git a/src/neon/yuv_nv_p16_to_rgb.rs b/src/neon/yuv_nv_p16_to_rgb.rs index e825180..af6868d 100644 --- a/src/neon/yuv_nv_p16_to_rgb.rs +++ b/src/neon/yuv_nv_p16_to_rgb.rs @@ -56,8 +56,8 @@ pub unsafe fn neon_yuv_nv_p16_to_rgba_row< let v_cr_coeff = vdup_n_s16(cr_coef as i16); let v_cb_coeff = vdup_n_s16(cb_coef as i16); let v_min_values = vdupq_n_s16(0i16); - let v_g_coeff_1 = vdup_n_s16(-1i16 * (g_coef_1 as i16)); - let v_g_coeff_2 = vdup_n_s16(-1i16 * (g_coef_2 as i16)); + let v_g_coeff_1 = vdup_n_s16(-(g_coef_1 as i16)); + let v_g_coeff_2 = vdup_n_s16(-(g_coef_2 as i16)); let mut cx = start_cx; let mut ux = start_ux; @@ -65,8 +65,6 @@ pub unsafe fn neon_yuv_nv_p16_to_rgba_row< let v_big_shift_count = vdupq_n_s16(-(16i16 - BIT_DEPTH as i16)); while cx + 8 < width as usize { - let y_values: int16x8_t; - let u_high: int16x4_t; let v_high: int16x4_t; let u_low: int16x4_t; @@ -79,7 +77,7 @@ pub unsafe fn neon_yuv_nv_p16_to_rgba_row< if bytes_position == YuvBytesPacking::MostSignificantBytes { y_vl = vshlq_u16(y_vl, v_big_shift_count); } - y_values = vsubq_s16(vreinterpretq_s16_u16(y_vl), y_corr); + let y_values: int16x8_t = vsubq_s16(vreinterpretq_s16_u16(y_vl), y_corr); match chroma_subsampling { YuvChromaSample::YUV420 | YuvChromaSample::YUV422 => { diff --git a/src/neon/yuv_nv_to_rgba.rs b/src/neon/yuv_nv_to_rgba.rs index a171282..56eadce 100644 --- a/src/neon/yuv_nv_to_rgba.rs +++ b/src/neon/yuv_nv_to_rgba.rs @@ -43,8 +43,8 @@ pub unsafe fn neon_yuv_nv_to_rgba_row< let v_cr_coeff = vdupq_n_s16(transform.cr_coef as i16); let v_cb_coeff = vdupq_n_s16(transform.cb_coef as i16); let v_min_values = vdupq_n_s16(0i16); - let v_g_coeff_1 = vdupq_n_s16(-1i16 * (transform.g_coeff_1 as i16)); - let v_g_coeff_2 = vdupq_n_s16(-1i16 * (transform.g_coeff_2 as i16)); + let v_g_coeff_1 = vdupq_n_s16(-(transform.g_coeff_1 as i16)); + let v_g_coeff_2 = vdupq_n_s16(-(transform.g_coeff_2 as i16)); let v_alpha = vdupq_n_u8(255u8); let mut cx = start_cx; @@ -179,6 +179,7 @@ pub unsafe fn neon_yuv_nv_to_rgba_row< u_low_u8 = vtbl1_u8(uv_values, shuffle_u); v_low_u8 = vtbl1_u8(uv_values, shuffle_v); + #[allow(clippy::manual_swap)] if order == YuvNVOrder::VU { let new_v = u_low_u8; u_low_u8 = v_low_u8; diff --git a/src/neon/yuv_p10_to_rgba.rs b/src/neon/yuv_p10_to_rgba.rs index a46a256..281d857 100644 --- a/src/neon/yuv_p10_to_rgba.rs +++ b/src/neon/yuv_p10_to_rgba.rs @@ -44,8 +44,8 @@ pub unsafe fn neon_yuv_p10_to_rgba_row< let v_cr_coeff = vdup_n_s16(transform.cr_coef as i16); let v_cb_coeff = vdup_n_s16(transform.cb_coef as i16); let v_min_values = vdupq_n_s16(0i16); - let v_g_coeff_1 = vdup_n_s16(-1i16 * (transform.g_coeff_1 as i16)); - let v_g_coeff_2 = vdup_n_s16(-1i16 * (transform.g_coeff_2 as i16)); + let v_g_coeff_1 = vdup_n_s16(-(transform.g_coeff_1 as i16)); + let v_g_coeff_2 = vdup_n_s16(-(transform.g_coeff_2 as i16)); let v_alpha = vdup_n_u8(255u8); let rounding_const = vdupq_n_s32(1 << 5); diff --git a/src/neon/yuv_to_rgba.rs b/src/neon/yuv_to_rgba.rs index eb4d8cc..560eb2b 100644 --- a/src/neon/yuv_to_rgba.rs +++ b/src/neon/yuv_to_rgba.rs @@ -45,8 +45,8 @@ pub unsafe fn neon_yuv_to_rgba_row( let u_pixels; let v_pixels; - let y_pixels; - - y_pixels = vld1q_u8_x2(y_plane.as_ptr().add(y_pos)); + let y_pixels = vld1q_u8_x2(y_plane.as_ptr().add(y_pos)); if chroma_subsampling == YuvChromaSample::YUV444 { let full_u = vld1q_u8_x2(u_plane.as_ptr().add(u_pos)); @@ -62,21 +60,12 @@ pub fn yuv_to_yuy2_neon_impl( let low_y = vcombine_u8(vget_low_u8(y_pixels_low), vget_low_u8(y_pixels_high)); let high_y = vcombine_u8(vget_high_u8(y_pixels_low), vget_high_u8(y_pixels_high)); - let storage; - match yuy2_target { - Yuy2Description::YUYV => { - storage = uint8x16x4_t(low_y, u_pixels, high_y, v_pixels); - } - Yuy2Description::UYVY => { - storage = uint8x16x4_t(u_pixels, low_y, v_pixels, high_y); - } - Yuy2Description::YVYU => { - storage = uint8x16x4_t(low_y, v_pixels, high_y, u_pixels); - } - Yuy2Description::VYUY => { - storage = uint8x16x4_t(v_pixels, low_y, u_pixels, high_y); - } - } + let storage = match yuy2_target { + Yuy2Description::YUYV => uint8x16x4_t(low_y, u_pixels, high_y, v_pixels), + Yuy2Description::UYVY => uint8x16x4_t(u_pixels, low_y, v_pixels, high_y), + Yuy2Description::YVYU => uint8x16x4_t(low_y, v_pixels, high_y, u_pixels), + Yuy2Description::VYUY => uint8x16x4_t(v_pixels, low_y, u_pixels, high_y), + }; let dst_offset = yuy2_offset + x * 4; @@ -126,21 +115,12 @@ pub fn yuv_to_yuy2_neon_impl( let low_y = vget_low_u8(y_pixels); let high_y = vget_high_u8(y_pixels); - let storage; - match yuy2_target { - Yuy2Description::YUYV => { - storage = uint8x8x4_t(low_y, u_pixels, high_y, v_pixels); - } - Yuy2Description::UYVY => { - storage = uint8x8x4_t(u_pixels, low_y, v_pixels, high_y); - } - Yuy2Description::YVYU => { - storage = uint8x8x4_t(low_y, v_pixels, high_y, u_pixels); - } - Yuy2Description::VYUY => { - storage = uint8x8x4_t(v_pixels, low_y, u_pixels, high_y); - } - } + let storage = match yuy2_target { + Yuy2Description::YUYV => uint8x8x4_t(low_y, u_pixels, high_y, v_pixels), + Yuy2Description::UYVY => uint8x8x4_t(u_pixels, low_y, v_pixels, high_y), + Yuy2Description::YVYU => uint8x8x4_t(low_y, v_pixels, high_y, u_pixels), + Yuy2Description::VYUY => uint8x8x4_t(v_pixels, low_y, u_pixels, high_y), + }; let dst_offset = yuy2_offset + x * 4; @@ -158,9 +138,9 @@ pub fn yuv_to_yuy2_neon_impl( } } - return YuvToYuy2Navigation { + YuvToYuy2Navigation { cx: _cx, uv_x: _uv_x, x: _yuy2_x, - }; + } } diff --git a/src/neon/yuy2_to_rgb.rs b/src/neon/yuy2_to_rgb.rs index bd87ee6..84082ad 100644 --- a/src/neon/yuy2_to_rgb.rs +++ b/src/neon/yuy2_to_rgb.rs @@ -36,8 +36,8 @@ pub fn yuy2_to_rgb_neon( let v_cr_coeff = vdupq_n_s16(transform.cr_coef as i16); let v_cb_coeff = vdupq_n_s16(transform.cb_coef as i16); let v_min_values = vdupq_n_s16(0i16); - let v_g_coeff_1 = vdupq_n_s16(-1i16 * transform.g_coeff_1 as i16); - let v_g_coeff_2 = vdupq_n_s16(-1i16 * transform.g_coeff_2 as i16); + let v_g_coeff_1 = vdupq_n_s16(-(transform.g_coeff_1 as i16)); + let v_g_coeff_2 = vdupq_n_s16(-(transform.g_coeff_2 as i16)); let v_alpha = vdupq_n_u8(255u8); for x in (_yuy2_x..max_x_16).step_by(16) { diff --git a/src/rgb_to_nv_p16.rs b/src/rgb_to_nv_p16.rs index fa2ef8b..184cedc 100644 --- a/src/rgb_to_nv_p16.rs +++ b/src/rgb_to_nv_p16.rs @@ -21,11 +21,10 @@ fn transform_integer v << packing, YuvBytesPacking::LeastSignificantBytes => v, } as u16; - let endian_prepared = match endianness { + match endianness { YuvEndianness::BigEndian => packed_bytes.to_be(), YuvEndianness::LittleEndian => packed_bytes.to_le(), - }; - endian_prepared + } } fn rgbx_to_yuv_bi_planar_10_impl< @@ -57,9 +56,10 @@ fn rgbx_to_yuv_bi_planar_10_impl< let transform_precise = get_forward_transform(max_range, range.range_y, range.range_uv, kr_kb.kr, kr_kb.kb); let transform = transform_precise.to_integers(8); - const ROUNDING_CONST_BIAS: i32 = 1 << 7; - let bias_y = range.bias_y as i32 * (1 << 8) + ROUNDING_CONST_BIAS; - let bias_uv = range.bias_uv as i32 * (1 << 8) + ROUNDING_CONST_BIAS; + const PRECISION: i32 = 8; + const ROUNDING_CONST_BIAS: i32 = 1 << (PRECISION - 1); + let bias_y = range.bias_y as i32 * (1 << PRECISION) + ROUNDING_CONST_BIAS; + let bias_uv = range.bias_uv as i32 * (1 << PRECISION) + ROUNDING_CONST_BIAS; let iterator_step = match chroma_subsampling { YuvChromaSample::YUV420 => 2usize, @@ -92,9 +92,12 @@ fn rgbx_to_yuv_bi_planar_10_impl< let r = unsafe { src.add(src_chans.get_r_channel_offset()).read_unaligned() } as i32; let g = unsafe { src.add(src_chans.get_g_channel_offset()).read_unaligned() } as i32; let b = unsafe { src.add(src_chans.get_b_channel_offset()).read_unaligned() } as i32; - let y_0 = (r * transform.yr + g * transform.yg + b * transform.yb + bias_y) >> 8; - let cb = (r * transform.cb_r + g * transform.cb_g + b * transform.cb_b + bias_uv) >> 8; - let cr = (r * transform.cr_r + g * transform.cr_g + b * transform.cr_b + bias_uv) >> 8; + let y_0 = + (r * transform.yr + g * transform.yg + b * transform.yb + bias_y) >> PRECISION; + let cb = (r * transform.cb_r + g * transform.cb_g + b * transform.cb_b + bias_uv) + >> PRECISION; + let cr = (r * transform.cr_r + g * transform.cr_g + b * transform.cr_b + bias_uv) + >> PRECISION; unsafe { y_st_ptr.add(x).write_unaligned(transform_integer::< ENDIANNESS, @@ -133,8 +136,8 @@ fn rgbx_to_yuv_bi_planar_10_impl< let b = unsafe { src.add(src_chans.get_b_channel_offset()).read_unaligned() } as i32; - let y_1 = - (r * transform.yr + g * transform.yg + b * transform.yb + bias_y) >> 8; + let y_1 = (r * transform.yr + g * transform.yg + b * transform.yb + bias_y) + >> PRECISION; unsafe { y_st_ptr.add(x + 1).write_unaligned(transform_integer::< ENDIANNESS, @@ -707,7 +710,6 @@ pub fn bgr_to_yuv_nv12_p16( ); } - /// Convert BGR image data to YUV 420 bi-planar (NV21 10-bit) format with 10 or 12 bit depth. /// /// This function performs BGR to YUV conversion and stores the result in YUV420 bi-planar format, @@ -2229,7 +2231,16 @@ pub fn bgra_to_yuv_nv24_p16( }, }; dispatcher( - y_plane, y_stride, uv_plane, uv_stride, bgra, bgra_stride, bit_depth, width, height, range, + y_plane, + y_stride, + uv_plane, + uv_stride, + bgra, + bgra_stride, + bit_depth, + width, + height, + range, matrix, ); } @@ -2321,7 +2332,16 @@ pub fn bgra_to_yuv_nv42_p16( }, }; dispatcher( - y_plane, y_stride, uv_plane, uv_stride, bgra, bgra_stride, bit_depth, width, height, range, + y_plane, + y_stride, + uv_plane, + uv_stride, + bgra, + bgra_stride, + bit_depth, + width, + height, + range, matrix, ); } @@ -2413,7 +2433,16 @@ pub fn rgba_to_yuv_nv24_p16( }, }; dispatcher( - y_plane, y_stride, uv_plane, uv_stride, rgba, rgba_stride, bit_depth, width, height, range, + y_plane, + y_stride, + uv_plane, + uv_stride, + rgba, + rgba_stride, + bit_depth, + width, + height, + range, matrix, ); } @@ -2505,7 +2534,16 @@ pub fn rgba_to_yuv_nv42_p16( }, }; dispatcher( - y_plane, y_stride, uv_plane, uv_stride, rgba, rgba_stride, bit_depth, width, height, range, + y_plane, + y_stride, + uv_plane, + uv_stride, + rgba, + rgba_stride, + bit_depth, + width, + height, + range, matrix, ); -} \ No newline at end of file +} diff --git a/src/rgb_to_y.rs b/src/rgb_to_y.rs index 2e6c199..8a31c90 100644 --- a/src/rgb_to_y.rs +++ b/src/rgb_to_y.rs @@ -69,7 +69,7 @@ fn rgbx_to_y( &transform, &range, y_plane.as_mut_ptr(), - &rgba, + rgba, y_offset, rgba_offset, _cx, @@ -82,7 +82,7 @@ fn rgbx_to_y( &transform, &range, y_plane.as_mut_ptr(), - &rgba, + rgba, y_offset, rgba_offset, _cx, @@ -95,7 +95,7 @@ fn rgbx_to_y( &transform, &range, y_plane.as_mut_ptr(), - &rgba, + rgba, y_offset, rgba_offset, _cx, @@ -111,7 +111,7 @@ fn rgbx_to_y( &transform, &range, y_plane.as_mut_ptr(), - &rgba, + rgba, y_offset, rgba_offset, _cx, diff --git a/src/rgb_to_ycgco.rs b/src/rgb_to_ycgco.rs index 7c98370..d89b3ba 100644 --- a/src/rgb_to_ycgco.rs +++ b/src/rgb_to_ycgco.rs @@ -86,7 +86,7 @@ fn rgbx_to_ycgco( y_plane.as_mut_ptr(), cg_plane.as_mut_ptr(), co_plane.as_mut_ptr(), - &rgba, + rgba, y_offset, cg_offset, co_offset, @@ -104,7 +104,7 @@ fn rgbx_to_ycgco( y_plane.as_mut_ptr(), cg_plane.as_mut_ptr(), co_plane.as_mut_ptr(), - &rgba, + rgba, y_offset, cg_offset, co_offset, @@ -122,7 +122,7 @@ fn rgbx_to_ycgco( y_plane.as_mut_ptr(), cg_plane.as_mut_ptr(), co_plane.as_mut_ptr(), - &rgba, + rgba, y_offset, cg_offset, co_offset, @@ -143,7 +143,7 @@ fn rgbx_to_ycgco( y_plane.as_mut_ptr(), cg_plane.as_mut_ptr(), co_plane.as_mut_ptr(), - &rgba, + rgba, y_offset, cg_offset, co_offset, @@ -167,7 +167,7 @@ fn rgbx_to_ycgco( let mut b = unsafe { *src0.get_unchecked(source_channels.get_b_channel_offset()) } as i32; - let hg = g * range_reduction_y >> 1; + let hg = (g * range_reduction_y) >> 1; let y_0 = (hg + ((r * range_reduction_y + b * range_reduction_y) >> 2) + bias_y) >> 8; r *= range_reduction_uv; g *= range_reduction_uv; @@ -200,7 +200,7 @@ fn rgbx_to_ycgco( let b = unsafe { *src1.get_unchecked(source_channels.get_b_channel_offset()) } as i32; - let hg_1 = g * range_reduction_y >> 1; + let hg_1 = (g * range_reduction_y) >> 1; let y_1 = (hg_1 + ((r * range_reduction_y + b * range_reduction_y) >> 2) + bias_y) diff --git a/src/rgb_to_yuv_p16.rs b/src/rgb_to_yuv_p16.rs index fc73955..1e97eea 100644 --- a/src/rgb_to_yuv_p16.rs +++ b/src/rgb_to_yuv_p16.rs @@ -25,11 +25,10 @@ fn transform_integer v << packing, YuvBytesPacking::LeastSignificantBytes => v, } as u16; - let endian_prepared = match endianness { + match endianness { YuvEndianness::BigEndian => packed_bytes.to_be(), YuvEndianness::LittleEndian => packed_bytes.to_le(), - }; - endian_prepared + } } fn rgbx_to_yuv_impl< @@ -94,10 +93,10 @@ fn rgbx_to_yuv_impl< let mut _cx = 0usize; let mut _ux = 0usize; - let y_st_ptr = unsafe { y_dst_ptr.offset(y_offset as isize) as *mut u16 }; - let u_st_ptr = unsafe { u_dst_ptr.offset(u_offset as isize) as *mut u16 }; - let v_st_ptr = unsafe { v_dst_ptr.offset(v_offset as isize) as *mut u16 }; - let rgb_ld_ptr = unsafe { rgb_src_ptr.offset(rgba_offset as isize) as *const u16 }; + let y_st_ptr = unsafe { y_dst_ptr.add(y_offset) as *mut u16 }; + let u_st_ptr = unsafe { u_dst_ptr.add(u_offset) as *mut u16 }; + let v_st_ptr = unsafe { v_dst_ptr.add(v_offset) as *mut u16 }; + let rgb_ld_ptr = unsafe { rgb_src_ptr.add(rgba_offset) as *const u16 }; #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] unsafe { @@ -153,9 +152,12 @@ fn rgbx_to_yuv_impl< let r = unsafe { src.add(src_chans.get_r_channel_offset()).read_unaligned() } as i32; let g = unsafe { src.add(src_chans.get_g_channel_offset()).read_unaligned() } as i32; let b = unsafe { src.add(src_chans.get_b_channel_offset()).read_unaligned() } as i32; - let y_0 = (r * transform.yr + g * transform.yg + b * transform.yb + bias_y) >> PRECISION; - let cb = (r * transform.cb_r + g * transform.cb_g + b * transform.cb_b + bias_uv) >> PRECISION; - let cr = (r * transform.cr_r + g * transform.cr_g + b * transform.cr_b + bias_uv) >> PRECISION; + let y_0 = + (r * transform.yr + g * transform.yg + b * transform.yb + bias_y) >> PRECISION; + let cb = (r * transform.cb_r + g * transform.cb_g + b * transform.cb_b + bias_uv) + >> PRECISION; + let cr = (r * transform.cr_r + g * transform.cr_g + b * transform.cr_b + bias_uv) + >> PRECISION; unsafe { y_st_ptr.add(x).write_unaligned(transform_integer::< ENDIANNESS, @@ -199,8 +201,8 @@ fn rgbx_to_yuv_impl< let b = unsafe { src.add(src_chans.get_b_channel_offset()).read_unaligned() } as i32; - let y_1 = - (r * transform.yr + g * transform.yg + b * transform.yb + bias_y) >> PRECISION; + let y_1 = (r * transform.yr + g * transform.yg + b * transform.yb + bias_y) + >> PRECISION; unsafe { y_st_ptr.add(x + 1).write_unaligned(transform_integer::< ENDIANNESS, diff --git a/src/rgba_to_yuv.rs b/src/rgba_to_yuv.rs index a1e88ef..e0d9dc3 100644 --- a/src/rgba_to_yuv.rs +++ b/src/rgba_to_yuv.rs @@ -99,7 +99,7 @@ fn rgbx_to_yuv8( y_plane.as_mut_ptr(), u_plane.as_mut_ptr(), v_plane.as_mut_ptr(), - &rgba, + rgba, y_offset, u_offset, v_offset, @@ -120,7 +120,7 @@ fn rgbx_to_yuv8( y_plane.as_mut_ptr(), u_plane.as_mut_ptr(), v_plane.as_mut_ptr(), - &rgba, + rgba, y_offset, u_offset, v_offset, @@ -140,7 +140,7 @@ fn rgbx_to_yuv8( y_plane.as_mut_ptr(), u_plane.as_mut_ptr(), v_plane.as_mut_ptr(), - &rgba, + rgba, y_offset, u_offset, v_offset, @@ -162,7 +162,7 @@ fn rgbx_to_yuv8( y_plane.as_mut_ptr(), u_plane.as_mut_ptr(), v_plane.as_mut_ptr(), - &rgba, + rgba, y_offset, u_offset, v_offset, diff --git a/src/sharpyuv/sharp_gamma.rs b/src/sharpyuv/sharp_gamma.rs index d22a2eb..d8975a6 100644 --- a/src/sharpyuv/sharp_gamma.rs +++ b/src/sharpyuv/sharp_gamma.rs @@ -10,10 +10,10 @@ pub fn srgb_to_linear(gamma: f32) -> f32 { if gamma < 0f32 { 0f32 - } else if gamma < 12.92f32 * 0.0030412825601275209f32 { + } else if gamma < 12.92f32 * 0.003_041_282_5_f32 { gamma * (1f32 / 12.92f32) } else if gamma < 1.0f32 { - ((gamma + 0.0550107189475866f32) / 1.0550107189475866f32).powf(2.4f32) + ((gamma + 0.055_010_717_f32) / 1.055_010_7_f32).powf(2.4f32) } else { 1.0f32 } @@ -24,10 +24,10 @@ pub fn srgb_to_linear(gamma: f32) -> f32 { pub fn srgb_from_linear(linear: f32) -> f32 { if linear < 0.0f32 { 0.0f32 - } else if linear < 0.0030412825601275209f32 { + } else if linear < 0.003_041_282_5_f32 { linear * 12.92f32 } else if linear < 1.0f32 { - 1.0550107189475866f32 * linear.powf(1.0f32 / 2.4f32) - 0.0550107189475866f32 + 1.055_010_7_f32 * linear.powf(1.0f32 / 2.4f32) - 0.055_010_717_f32 } else { 1.0f32 } @@ -38,10 +38,10 @@ pub fn srgb_from_linear(linear: f32) -> f32 { pub fn rec709_to_linear(gamma: f32) -> f32 { if gamma < 0.0f32 { 0.0f32 - } else if gamma < 4.5f32 * 0.018053968510807f32 { + } else if gamma < 4.5f32 * 0.018_053_97_f32 { gamma * (1f32 / 4.5f32) } else if gamma < 1.0f32 { - ((gamma + 0.09929682680944f32) / 1.09929682680944f32).powf(1.0f32 / 0.45f32) + ((gamma + 0.099_296_82_f32) / 1.099_296_8_f32).powf(1.0f32 / 0.45f32) } else { 1.0f32 } @@ -52,10 +52,10 @@ pub fn rec709_to_linear(gamma: f32) -> f32 { pub fn rec709_from_linear(linear: f32) -> f32 { if linear < 0.0f32 { 0.0f32 - } else if linear < 0.018053968510807f32 { + } else if linear < 0.018_053_97_f32 { linear * 4.5f32 } else if linear < 1.0f32 { - 1.09929682680944f32 * linear.powf(0.45f32) - 0.09929682680944f32 + 1.099_296_8_f32 * linear.powf(0.45f32) - 0.099_296_82_f32 } else { 1.0f32 } diff --git a/src/sharpyuv/sharp_rgba_to_yuv.rs b/src/sharpyuv/sharp_rgba_to_yuv.rs index 8e056b6..f1bb20a 100644 --- a/src/sharpyuv/sharp_rgba_to_yuv.rs +++ b/src/sharpyuv/sharp_rgba_to_yuv.rs @@ -109,6 +109,7 @@ fn rgbx_to_sharp_yuv( let mut _cx = 0usize; let mut _ux = 0usize; + #[allow(clippy::explicit_counter_loop)] for x in (_cx..width as usize).step_by(iterator_step) { unsafe { let px = x * channels; diff --git a/src/sse/rgb_to_y.rs b/src/sse/rgb_to_y.rs index 73d87ac..f462121 100644 --- a/src/sse/rgb_to_y.rs +++ b/src/sse/rgb_to_y.rs @@ -104,5 +104,5 @@ pub unsafe fn sse_rgb_to_y( cx += 16; } - return cx; + cx } diff --git a/src/sse/rgb_to_ycgco.rs b/src/sse/rgb_to_ycgco.rs index 33d3f41..00266a4 100644 --- a/src/sse/rgb_to_ycgco.rs +++ b/src/sse/rgb_to_ycgco.rs @@ -156,5 +156,5 @@ pub unsafe fn sse_rgb_to_ycgco_row __m128i { #[rustfmt::skip] let shuffle = _mm_setr_epi8(0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14); - let new_lane = _mm_shuffle_epi8(x, shuffle); - new_lane + _mm_shuffle_epi8(x, shuffle) } #[inline] #[target_feature(enable = "sse4.1")] pub unsafe fn sse_interleave_odd(x: __m128i) -> __m128i { - #[rustfmt::skip] - let shuffle = _mm_setr_epi8(1, 1, 3, 3, 5, 5, 7, 7, - 9, 9, 11, 11, 13, 13, 15, 15); - let new_lane = _mm_shuffle_epi8(x, shuffle); - new_lane + let shuffle = _mm_setr_epi8(1, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11, 11, 13, 13, 15, 15); + _mm_shuffle_epi8(x, shuffle) } #[inline] @@ -229,8 +225,7 @@ pub unsafe fn sse_store_rgb_half_u8(ptr: *mut u8, r: __m128i, g: __m128i, b: __m pub unsafe fn sse_pairwise_widen_avg(v: __m128i) -> __m128i { let sums = _mm_maddubs_epi16(v, _mm_set1_epi8(1)); let shifted = _mm_srli_epi16::<1>(_mm_add_epi16(sums, _mm_set1_epi16(1))); - let packed = _mm_packus_epi16(shifted, shifted); - packed + _mm_packus_epi16(shifted, shifted) } #[inline] @@ -246,8 +241,7 @@ pub unsafe fn sse_div_by255(v: __m128i) -> __m128i { #[inline(always)] pub unsafe fn sse_pairwise_avg_epi16(a: __m128i, b: __m128i) -> __m128i { let sums = _mm_hadd_epi16(a, b); - let shifted = _mm_srli_epi16::<1>(_mm_add_epi16(sums, _mm_set1_epi16(1))); - shifted + _mm_srli_epi16::<1>(_mm_add_epi16(sums, _mm_set1_epi16(1))) } #[inline(always)] @@ -274,8 +268,7 @@ pub unsafe fn _mm_combinel_epi8(a: __m128i, b: __m128i) -> __m128i { let a_low = _mm_castps_si128(_mm_movelh_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(a))); let b_low = _mm_castps_si128(_mm_movelh_ps(_mm_castsi128_ps(b), _mm_castsi128_ps(b))); - let combined = _mm_unpacklo_epi64(a_low, b_low); - combined + _mm_unpacklo_epi64(a_low, b_low) } #[inline(always)] @@ -283,8 +276,7 @@ pub unsafe fn _mm_combineh_epi8(a: __m128i, b: __m128i) -> __m128i { let a_low = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(a))); let b_low = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(b), _mm_castsi128_ps(b))); - let combined = _mm_unpacklo_epi64(a_low, b_low); - combined + _mm_unpacklo_epi64(a_low, b_low) } #[inline] @@ -299,11 +291,10 @@ pub unsafe fn _mm_storeu_si128_x4(ptr: *mut u8, vals: __mm128x4) { #[inline] #[target_feature(enable = "sse4.1")] pub unsafe fn _mm_getlow_epi8(a: __m128i) -> __m128i { - let half = _mm_castps_si128(_mm_movelh_ps( + _mm_castps_si128(_mm_movelh_ps( _mm_castsi128_ps(a), _mm_castsi128_ps(_mm_setzero_si128()), - )); - half + )) } #[inline] diff --git a/src/sse/ycgco_to_rgb.rs b/src/sse/ycgco_to_rgb.rs index 7157f49..1db6c70 100644 --- a/src/sse/ycgco_to_rgb.rs +++ b/src/sse/ycgco_to_rgb.rs @@ -176,5 +176,5 @@ pub unsafe fn sse_ycgco_to_rgb_row { diff --git a/src/sse/yuv_nv_to_rgba.rs b/src/sse/yuv_nv_to_rgba.rs index 87c7d70..f069e21 100644 --- a/src/sse/yuv_nv_to_rgba.rs +++ b/src/sse/yuv_nv_to_rgba.rs @@ -52,8 +52,8 @@ pub unsafe fn sse_yuv_nv_to_rgba< let v_cr_coeff = _mm_set1_epi16(transform.cr_coef as i16); let v_cb_coeff = _mm_set1_epi16(transform.cb_coef as i16); let v_min_values = _mm_setzero_si128(); - let v_g_coeff_1 = _mm_set1_epi16(-1 * transform.g_coeff_1 as i16); - let v_g_coeff_2 = _mm_set1_epi16(-1 * transform.g_coeff_2 as i16); + let v_g_coeff_1 = _mm_set1_epi16(-(transform.g_coeff_1 as i16)); + let v_g_coeff_2 = _mm_set1_epi16(-(transform.g_coeff_2 as i16)); let v_alpha = _mm_set1_epi8(255u8 as i8); let rounding_const = _mm_set1_epi16(1 << 5); diff --git a/src/sse/yuv_to_rgba.rs b/src/sse/yuv_to_rgba.rs index 48c92a3..bf951eb 100644 --- a/src/sse/yuv_to_rgba.rs +++ b/src/sse/yuv_to_rgba.rs @@ -49,8 +49,8 @@ pub unsafe fn sse_yuv_to_rgba_row let u_pixels; let v_pixels; - let y_pixels; - - y_pixels = _mm_loadu_si128_x2(y_plane.as_ptr().add(y_pos)); + let y_pixels = _mm_loadu_si128_x2(y_plane.as_ptr().add(y_pos)); if chroma_subsampling == YuvChromaSample::YUV444 { let full_u = _mm_loadu_si128_x2(u_plane.as_ptr().add(u_pos)); @@ -70,21 +68,12 @@ pub unsafe fn yuv_to_yuy2_sse_impl let low_y = _mm_combinel_epi8(y_pixels_low, y_pixels_high); let high_y = _mm_combineh_epi8(y_pixels_low, y_pixels_high); - let storage; - match yuy2_target { - Yuy2Description::YUYV => { - storage = __mm128x4(low_y, u_pixels, high_y, v_pixels); - } - Yuy2Description::UYVY => { - storage = __mm128x4(u_pixels, low_y, v_pixels, high_y); - } - Yuy2Description::YVYU => { - storage = __mm128x4(low_y, v_pixels, high_y, u_pixels); - } - Yuy2Description::VYUY => { - storage = __mm128x4(v_pixels, low_y, u_pixels, high_y); - } - } + let storage = match yuy2_target { + Yuy2Description::YUYV => __mm128x4(low_y, u_pixels, high_y, v_pixels), + Yuy2Description::UYVY => __mm128x4(u_pixels, low_y, v_pixels, high_y), + Yuy2Description::YVYU => __mm128x4(low_y, v_pixels, high_y, u_pixels), + Yuy2Description::VYUY => __mm128x4(v_pixels, low_y, u_pixels, high_y), + }; let dst_offset = yuy2_offset + x * 4; @@ -137,21 +126,12 @@ pub unsafe fn yuv_to_yuy2_sse_impl let low_y = _mm_getlow_epi8(y_pixels); let high_y = _mm_gethigh_epi8(y_pixels); - let storage; - match yuy2_target { - Yuy2Description::YUYV => { - storage = __mm128x4(low_y, u_pixels, high_y, v_pixels); - } - Yuy2Description::UYVY => { - storage = __mm128x4(u_pixels, low_y, v_pixels, high_y); - } - Yuy2Description::YVYU => { - storage = __mm128x4(low_y, v_pixels, high_y, u_pixels); - } - Yuy2Description::VYUY => { - storage = __mm128x4(v_pixels, low_y, u_pixels, high_y); - } - } + let storage = match yuy2_target { + Yuy2Description::YUYV => __mm128x4(low_y, u_pixels, high_y, v_pixels), + Yuy2Description::UYVY => __mm128x4(u_pixels, low_y, v_pixels, high_y), + Yuy2Description::YVYU => __mm128x4(low_y, v_pixels, high_y, u_pixels), + Yuy2Description::VYUY => __mm128x4(v_pixels, low_y, u_pixels, high_y), + }; let inverleaved = sse_interleave_rgba(storage.0, storage.1, storage.2, storage.3); let converted = __mm128x4(inverleaved.0, inverleaved.1, inverleaved.2, inverleaved.3); @@ -175,9 +155,9 @@ pub unsafe fn yuv_to_yuy2_sse_impl } } - return YuvToYuy2Navigation { + YuvToYuy2Navigation { cx: _cx, uv_x: _uv_x, x: _yuy2_x, - }; + } } diff --git a/src/sse/yuy2_to_rgb.rs b/src/sse/yuy2_to_rgb.rs index 226aa7e..5f076ec 100644 --- a/src/sse/yuy2_to_rgb.rs +++ b/src/sse/yuy2_to_rgb.rs @@ -39,8 +39,8 @@ pub fn yuy2_to_rgb_sse( let v_luma_coeff = _mm_set1_epi16(transform.y_coef as i16); let v_cr_coeff = _mm_set1_epi16(transform.cr_coef as i16); let v_cb_coeff = _mm_set1_epi16(transform.cb_coef as i16); - let v_g_coeff_1 = _mm_set1_epi16(-1i16 * transform.g_coeff_1 as i16); - let v_g_coeff_2 = _mm_set1_epi16(-1i16 * transform.g_coeff_2 as i16); + let v_g_coeff_1 = _mm_set1_epi16(-(transform.g_coeff_1 as i16)); + let v_g_coeff_2 = _mm_set1_epi16(-(transform.g_coeff_2 as i16)); let v_alpha = _mm_set1_epi8(255u8 as i8); let rounding_const = _mm_set1_epi16(1 << 5); diff --git a/src/y_to_rgb.rs b/src/y_to_rgb.rs index 979c096..404fb87 100644 --- a/src/y_to_rgb.rs +++ b/src/y_to_rgb.rs @@ -109,7 +109,7 @@ fn y_to_rgbx( let y_value = (unsafe { *y_plane.get_unchecked(y_offset + x) } as i32 - bias_y) * y_coef; - let r = (y_value + ROUNDING_CONST >> PRECISION).min(255i32).max(0); + let r = ((y_value + ROUNDING_CONST) >> PRECISION).min(255i32).max(0); let px = x * channels; diff --git a/src/yuv_nv_p10_to_rgba.rs b/src/yuv_nv_p10_to_rgba.rs index c10a001..41bf34d 100644 --- a/src/yuv_nv_p10_to_rgba.rs +++ b/src/yuv_nv_p10_to_rgba.rs @@ -72,8 +72,8 @@ fn yuv_nv_p10_to_image_impl< let mut _ux = 0usize; - let y_ld_ptr = unsafe { y_src_ptr.offset(y_offset as isize) as *const u16 }; - let uv_ld_ptr = unsafe { uv_src_ptr.offset(uv_offset as isize) as *const u16 }; + let y_ld_ptr = unsafe { y_src_ptr.add(y_offset) as *const u16 }; + let uv_ld_ptr = unsafe { uv_src_ptr.add(uv_offset) as *const u16 }; #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] unsafe { @@ -116,9 +116,9 @@ fn yuv_nv_p10_to_image_impl< .read_unaligned() }) as i32; if bytes_position == YuvBytesPacking::MostSignificantBytes { - y_vl = y_vl >> 6; - cb_vl = cb_vl >> 6; - cr_vl = cr_vl >> 6; + y_vl >>= 6; + cb_vl >>= 6; + cr_vl >>= 6; } y_value = (y_vl - bias_y) * y_coef; @@ -138,9 +138,9 @@ fn yuv_nv_p10_to_image_impl< .read_unaligned() }) as i32; if bytes_position == YuvBytesPacking::MostSignificantBytes { - y_vl = y_vl >> 6; - cb_vl = cb_vl >> 6; - cr_vl = cr_vl >> 6; + y_vl >>= 6; + cb_vl >>= 6; + cr_vl >>= 6; } y_value = (y_vl - bias_y) * y_coef; @@ -151,8 +151,8 @@ fn yuv_nv_p10_to_image_impl< match uv_order { YuvNVOrder::UV => { - cb_value = cb_value - bias_uv; - cr_value = cr_value - bias_uv; + cb_value -= bias_uv; + cr_value -= bias_uv; } YuvNVOrder::VU => { cr_value = cb_value - bias_uv; @@ -189,27 +189,26 @@ fn yuv_nv_p10_to_image_impl< { let next_px = x + 1; if next_px < width as usize { - let y_value: i32; - match endianness { + let y_value: i32 = match endianness { YuvEndianness::BigEndian => { let mut y_vl = u16::from_be(unsafe { y_ld_ptr.add(next_px).read_unaligned() }) as i32; if bytes_position == YuvBytesPacking::MostSignificantBytes { - y_vl = y_vl >> 6; + y_vl >>= 6; } - y_value = (y_vl - bias_y) * y_coef; + (y_vl - bias_y) * y_coef } YuvEndianness::LittleEndian => { let mut y_vl = u16::from_le(unsafe { y_ld_ptr.add(next_px).read_unaligned() }) as i32; if bytes_position == YuvBytesPacking::MostSignificantBytes { - y_vl = y_vl >> 6; + y_vl >>= 6; } - y_value = (y_vl - bias_y) * y_coef; + (y_vl - bias_y) * y_coef } - } + }; let r_u16 = (y_value + cr_coef * cr_value + ROUNDING_CONST) >> 8; let b_u16 = (y_value + cb_coef * cb_value + ROUNDING_CONST) >> 8; diff --git a/src/yuv_nv_p16_to_rgb.rs b/src/yuv_nv_p16_to_rgb.rs index e247bdd..05ccf65 100644 --- a/src/yuv_nv_p16_to_rgb.rs +++ b/src/yuv_nv_p16_to_rgb.rs @@ -80,8 +80,8 @@ fn yuv_nv_p16_to_image_impl< let msb_shift = 16 - BIT_DEPTH; - let y_ld_ptr = unsafe { y_src_ptr.offset(y_offset as isize) as *const u16 }; - let uv_ld_ptr = unsafe { uv_src_ptr.offset(uv_offset as isize) as *const u16 }; + let y_ld_ptr = unsafe { y_src_ptr.add(y_offset) as *const u16 }; + let uv_ld_ptr = unsafe { uv_src_ptr.add(uv_offset) as *const u16 }; let dst_st_ptr = unsafe { dst_ptr.add(dst_offset) as *mut u16 }; #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] @@ -150,9 +150,9 @@ fn yuv_nv_p16_to_image_impl< .read_unaligned() }) as i32; if bytes_position == YuvBytesPacking::MostSignificantBytes { - y_vl = y_vl >> msb_shift; - cb_vl = cb_vl >> msb_shift; - cr_vl = cr_vl >> msb_shift; + y_vl >>= msb_shift; + cb_vl >>= msb_shift; + cr_vl >>= msb_shift; } y_value = (y_vl - bias_y) * y_coef; @@ -172,9 +172,9 @@ fn yuv_nv_p16_to_image_impl< .read_unaligned() }) as i32; if bytes_position == YuvBytesPacking::MostSignificantBytes { - y_vl = y_vl >> msb_shift; - cb_vl = cb_vl >> msb_shift; - cr_vl = cr_vl >> msb_shift; + y_vl >>= msb_shift; + cb_vl >>= msb_shift; + cr_vl >>= msb_shift; } y_value = (y_vl - bias_y) * y_coef; @@ -185,8 +185,8 @@ fn yuv_nv_p16_to_image_impl< match uv_order { YuvNVOrder::UV => { - cb_value = cb_value - bias_uv; - cr_value = cr_value - bias_uv; + cb_value -= bias_uv; + cr_value -= bias_uv; } YuvNVOrder::VU => { cr_value = cb_value - bias_uv; @@ -230,27 +230,26 @@ fn yuv_nv_p16_to_image_impl< { let next_px = x + 1; if next_px < width as usize { - let y_value: i32; - match endianness { + let y_value: i32 = match endianness { YuvEndianness::BigEndian => { let mut y_vl = u16::from_be(unsafe { y_ld_ptr.add(next_px).read_unaligned() }) as i32; if bytes_position == YuvBytesPacking::MostSignificantBytes { - y_vl = y_vl >> msb_shift; + y_vl >>= msb_shift; } - y_value = (y_vl - bias_y) * y_coef; + (y_vl - bias_y) * y_coef } YuvEndianness::LittleEndian => { let mut y_vl = u16::from_le(unsafe { y_ld_ptr.add(next_px).read_unaligned() }) as i32; if bytes_position == YuvBytesPacking::MostSignificantBytes { - y_vl = y_vl >> msb_shift; + y_vl >>= msb_shift; } - y_value = (y_vl - bias_y) * y_coef; + (y_vl - bias_y) * y_coef } - } + }; let r_p16 = (y_value + cr_coef * cr_value + ROUNDING_CONST) >> PRECISION; let b_p16 = (y_value + cb_coef * cb_value + ROUNDING_CONST) >> PRECISION; diff --git a/src/yuv_nv_to_rgba.rs b/src/yuv_nv_to_rgba.rs index 9c7d6db..fc06a72 100644 --- a/src/yuv_nv_to_rgba.rs +++ b/src/yuv_nv_to_rgba.rs @@ -190,13 +190,12 @@ fn yuv_nv12_to_rgbx< for x in (cx..width as usize).step_by(iterator_step) { let y_value = (unsafe { *y_plane.get_unchecked(y_offset + x) } as i32 - bias_y) * y_coef; - let cb_value: i32; - let cr_value: i32; let cb_pos = uv_offset + ux; - - cb_value = unsafe { *uv_plane.get_unchecked(cb_pos + order.get_u_position()) } as i32 + let cb_value: i32 = unsafe { *uv_plane.get_unchecked(cb_pos + order.get_u_position()) } + as i32 - bias_uv; - cr_value = unsafe { *uv_plane.get_unchecked(cb_pos + order.get_v_position()) } as i32 + let cr_value: i32 = unsafe { *uv_plane.get_unchecked(cb_pos + order.get_v_position()) } + as i32 - bias_uv; let r = ((y_value + cr_coef * cr_value + ROUNDING_CONST) >> PRECISION) diff --git a/src/yuv_p10_rgba.rs b/src/yuv_p10_rgba.rs index 78ff7c3..f677402 100644 --- a/src/yuv_p10_rgba.rs +++ b/src/yuv_p10_rgba.rs @@ -71,9 +71,9 @@ fn yuv_p10_to_image_impl< let mut x = 0usize; let mut cx = 0usize; - let y_ld_ptr = unsafe { y_src_ptr.offset(y_offset as isize) as *const u16 }; - let u_ld_ptr = unsafe { u_src_ptr.offset(u_offset as isize) as *const u16 }; - let v_ld_ptr = unsafe { v_src_ptr.offset(v_offset as isize) as *const u16 }; + let y_ld_ptr = unsafe { y_src_ptr.add(y_offset) as *const u16 }; + let u_ld_ptr = unsafe { u_src_ptr.add(u_offset) as *const u16 }; + let v_ld_ptr = unsafe { v_src_ptr.add(v_offset) as *const u16 }; #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] unsafe { @@ -110,9 +110,9 @@ fn yuv_p10_to_image_impl< let mut cr_vl = u16::from_be(unsafe { v_ld_ptr.add(cx).read_unaligned() }) as i32; if bytes_position == YuvBytesPacking::MostSignificantBytes { - y_vl = y_vl >> 6; - cb_vl = cb_vl >> 6; - cr_vl = cr_vl >> 6; + y_vl >>= 6; + cb_vl >>= 6; + cr_vl >>= 6; } y_value = (y_vl - bias_y) * y_coef; @@ -126,9 +126,9 @@ fn yuv_p10_to_image_impl< let mut cr_vl = u16::from_le(unsafe { v_ld_ptr.add(cx).read_unaligned() }) as i32; if bytes_position == YuvBytesPacking::MostSignificantBytes { - y_vl = y_vl >> 6; - cb_vl = cb_vl >> 6; - cr_vl = cr_vl >> 6; + y_vl >>= 6; + cb_vl >>= 6; + cr_vl >>= 6; } y_value = (y_vl - bias_y) * y_coef; @@ -165,25 +165,24 @@ fn yuv_p10_to_image_impl< x += 1; if x + 1 < width as usize { - let y_value: i32; - match endianness { + let y_value: i32 = match endianness { YuvEndianness::BigEndian => { let mut y_vl = u16::from_be(unsafe { y_ld_ptr.add(x).read_unaligned() }) as i32; if bytes_position == YuvBytesPacking::MostSignificantBytes { - y_vl = y_vl >> 6; + y_vl >>= 6; } - y_value = (y_vl - bias_y) * y_coef; + (y_vl - bias_y) * y_coef } YuvEndianness::LittleEndian => { let mut y_vl = u16::from_le(unsafe { y_ld_ptr.add(x).read_unaligned() }) as i32; if bytes_position == YuvBytesPacking::MostSignificantBytes { - y_vl = y_vl >> 6; + y_vl >>= 6; } - y_value = (y_vl - bias_y) * y_coef; + (y_vl - bias_y) * y_coef } - } + }; let r_u16 = (y_value + cr_coef * cr_value + ROUNDING_CONST) >> (PRECISION + 2); let b_u16 = (y_value + cb_coef * cb_value + ROUNDING_CONST) >> (PRECISION + 2); diff --git a/src/yuv_support.rs b/src/yuv_support.rs index e5a783c..8e701f6 100644 --- a/src/yuv_support.rs +++ b/src/yuv_support.rs @@ -34,7 +34,7 @@ impl CbCrInverseTransform { impl CbCrInverseTransform { /// Integral transformation adds an error not less than 1% - pub fn to_integers(&self, precision: u32) -> CbCrInverseTransform { + pub fn to_integers(self, precision: u32) -> CbCrInverseTransform { let precision_scale: i32 = 1i32 << (precision as i32); let cr_coef = (self.cr_coef * precision_scale as f32).round() as i32; let cb_coef = (self.cb_coef * precision_scale as f32).round() as i32; @@ -182,9 +182,9 @@ pub const fn get_yuv_range(depth: u32, range: YuvRange) -> YuvChromaRange { #[repr(C)] #[derive(Debug, Copy, Clone, PartialOrd, PartialEq)] /// Declares standard prebuilt YUV conversion matrices, check [ITU-R](https://www.itu.int/rec/T-REC-H.273/en) information for more info -/// MJPEG Matrix corresponds Bt.601 + Full Range +/// JPEG YUV Matrix corresponds Bt.601 + Full Range pub enum YuvStandardMatrix { - /// If you want to encode/decode MJPEG use Bt.601 + Full Range + /// If you want to encode/decode JPEG YUV use Bt.601 + Full Range Bt601, Bt709, Bt2020, @@ -239,17 +239,16 @@ impl YuvNVOrder { pub const fn get_u_position(&self) -> usize { match self { YuvNVOrder::UV => 0, - YuvNVOrder::VU => 1 + YuvNVOrder::VU => 1, } } #[inline] pub const fn get_v_position(&self) -> usize { match self { YuvNVOrder::UV => 1, - YuvNVOrder::VU => 0 + YuvNVOrder::VU => 0, } } - } impl From for YuvNVOrder { @@ -310,6 +309,7 @@ impl From for YuvEndianness { #[repr(u8)] #[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[allow(clippy::too_long_first_doc_paragraph)] /// Most of the cases of storage bytes is least significant whereas b`0000000111111` integers stored in low part, /// however most modern hardware encoders (Apple, Android manufacturers) uses most significant bytes /// where same number stored as b`111111000000` and need to be shifted right before working with this. @@ -415,6 +415,7 @@ impl YuvSourceChannels { #[repr(usize)] #[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[allow(clippy::upper_case_acronyms)] pub(crate) enum Yuy2Description { YUYV = 0, UYVY = 1, diff --git a/src/yuv_to_yuy2.rs b/src/yuv_to_yuy2.rs index 392558b..6cee756 100644 --- a/src/yuv_to_yuy2.rs +++ b/src/yuv_to_yuy2.rs @@ -123,13 +123,15 @@ fn yuv_to_yuy2_impl( if chroma_subsampling == YuvChromaSample::YUV444 { u_value = unsafe { - (*u_plane.get_unchecked(u_pos) as u32 - + *u_plane.get_unchecked(u_pos + 1) as u32) + 1 + ((*u_plane.get_unchecked(u_pos) as u32 + + *u_plane.get_unchecked(u_pos + 1) as u32) + + 1) >> 1 } as u8; v_value = unsafe { - (*v_plane.get_unchecked(v_pos) as u32 - + *v_plane.get_unchecked(v_pos + 1) as u32) + 1 + ((*v_plane.get_unchecked(v_pos) as u32 + + *v_plane.get_unchecked(v_pos + 1) as u32) + + 1) >> 1 } as u8; } else { diff --git a/src/yuv_to_yuy2_p16.rs b/src/yuv_to_yuy2_p16.rs index 1cc0a9e..4e4f924 100644 --- a/src/yuv_to_yuy2_p16.rs +++ b/src/yuv_to_yuy2_p16.rs @@ -47,11 +47,13 @@ fn yuv_to_yuy2_impl_p16( let (u_value, v_value); if chroma_subsampling == YuvChromaSample::YUV444 { - u_value = ((u_src_ptr.read_unaligned() as u32 - + u_src_ptr.add(1).read_unaligned() as u32) + 1 + u_value = (((u_src_ptr.read_unaligned() as u32 + + u_src_ptr.add(1).read_unaligned() as u32) + + 1) >> 1) as u16; - v_value = ((v_src_ptr.read_unaligned() as u32 - + v_src_ptr.add(1).read_unaligned() as u32) + 1 + v_value = (((v_src_ptr.read_unaligned() as u32 + + v_src_ptr.add(1).read_unaligned() as u32) + + 1) >> 1) as u16; } else { u_value = u_src_ptr.read_unaligned(); diff --git a/src/yuy2_to_rgb.rs b/src/yuy2_to_rgb.rs index 5b6b4a2..fc95042 100644 --- a/src/yuy2_to_rgb.rs +++ b/src/yuy2_to_rgb.rs @@ -110,23 +110,20 @@ fn yuy2_to_rgb_impl( let rgb_pos = rgb_offset + _cx * channels; let yuy2_offset = yuy_offset + x * 4; - let yuy2_plane_shifted = unsafe { yuy2_store.get_unchecked(yuy2_offset..) }; + unsafe { + let yuy2_plane_shifted = yuy2_store.get_unchecked(yuy2_offset..); - let first_y = - unsafe { *yuy2_plane_shifted.get_unchecked(yuy2_source.get_first_y_position()) }; - let second_y = - unsafe { *yuy2_plane_shifted.get_unchecked(yuy2_source.get_second_y_position()) }; - let u_value = - unsafe { *yuy2_plane_shifted.get_unchecked(yuy2_source.get_u_position()) }; - let v_value = - unsafe { *yuy2_plane_shifted.get_unchecked(yuy2_source.get_v_position()) }; + let first_y = *yuy2_plane_shifted.get_unchecked(yuy2_source.get_first_y_position()); + let second_y = + *yuy2_plane_shifted.get_unchecked(yuy2_source.get_second_y_position()); + let u_value = *yuy2_plane_shifted.get_unchecked(yuy2_source.get_u_position()); + let v_value = *yuy2_plane_shifted.get_unchecked(yuy2_source.get_v_position()); - let cb = u_value as i32 - bias_uv; - let cr = v_value as i32 - bias_uv; - let f_y = (first_y as i32 - bias_y) * y_coef; - let s_y = (second_y as i32 - bias_y) * y_coef; + let cb = u_value as i32 - bias_uv; + let cr = v_value as i32 - bias_uv; + let f_y = (first_y as i32 - bias_y) * y_coef; + let s_y = (second_y as i32 - bias_y) * y_coef; - unsafe { let dst0 = rgb_store.get_unchecked_mut(rgb_pos..); let r0 = ((f_y + cr_coef * cr + ROUNDING_CONST) >> PRECISION).clamp(0, 255); let b0 = ((f_y + cb_coef * cb + ROUNDING_CONST) >> PRECISION).clamp(0, 255);