diff --git a/Cargo.lock b/Cargo.lock index e346f3e..a0cad5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "yuvutils-rs" -version = "0.1.10" +version = "0.1.11" diff --git a/Cargo.toml b/Cargo.toml index daa0d8b..2b18966 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "yuvutils-rs" -version = "0.1.10" +version = "0.1.11" edition = "2021" description = "Rust utilities for YUV format handling and conversion." readme = "README.md" diff --git a/src/lib.rs b/src/lib.rs index f675aea..8b410ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,12 +12,14 @@ mod rgba_to_nv; pub use yuv_support::YuvStandardMatrix; pub use yuv_support::YuvRange; -pub use yuv_nv_p10_to_rgba::yuv_nv12_p10_to_bgra_be; -pub use yuv_nv_p10_to_rgba::yuv_nv16_p10_to_bgra_be; +pub use yuv_nv_p10_to_rgba::yuv_nv12_p10_be_to_bgra; +pub use yuv_nv_p10_to_rgba::yuv_nv16_p10_be_to_bgra; pub use yuv_nv_p10_to_rgba::yuv_nv12_p10_to_bgra; pub use yuv_nv_p10_to_rgba::yuv_nv16_p10_to_bgra; pub use yuv_nv_p10_to_rgba::yuv_nv12_p10_msb_to_bgra; pub use yuv_nv_p10_to_rgba::yuv_nv16_p10_msb_to_bgra; +pub use yuv_nv_p10_to_rgba::yuv_nv12_p10_msb_to_rgba; +pub use yuv_nv_p10_to_rgba::yuv_nv16_p10_msb_to_rgba; pub use yuv_nv_to_rgba::yuv_nv12_to_bgra; pub use yuv_nv_to_rgba::yuv_nv21_to_bgra; @@ -32,7 +34,7 @@ pub use yuv_nv_to_rgba::yuv_nv42_to_rgba; pub use yuv_nv_to_rgba::yuv_nv42_to_rgb; pub use yuv_nv_to_rgba::yuv_nv42_to_bgra; -pub use rgba_to_nv::rgb_to_yuv_nv_16; +pub use rgba_to_nv::rgb_to_yuv_nv16; pub use rgba_to_nv::rgba_to_yuv_nv16; pub use rgba_to_nv::bgra_to_yuv_nv16; pub use rgba_to_nv::rgb_to_yuv_nv12; @@ -78,13 +80,13 @@ pub use y_to_rgb::yuv400_to_bgra; pub use yuv_p10_rgba::yuv420_p10_to_bgra; pub use yuv_p10_rgba::yuv422_p10_to_bgra; -pub use yuv_p10_rgba::yuv420_p10_to_bgra_be; -pub use yuv_p10_rgba::yuv422_p10_to_bgra_be; +pub use yuv_p10_rgba::yuv420_p10_be_to_bgra; +pub use yuv_p10_rgba::yuv422_p10_be_to_bgra; pub use yuv_p10_rgba::yuv420_p10_to_rgba; pub use yuv_p10_rgba::yuv422_p10_to_rgba; -pub use yuv_p10_rgba::yuv420_p10_to_rgba_be; -pub use yuv_p10_rgba::yuv422_p10_to_rgba_be; +pub use yuv_p10_rgba::yuv420_p10_be_to_rgba; +pub use yuv_p10_rgba::yuv422_p10_be_to_rgba; pub use yuv_p10_rgba::yuv444_p10_to_rgba; -pub use yuv_p10_rgba::yuv444_p10_to_rgba_be; +pub use yuv_p10_rgba::yuv444_p10_be_to_rgba; pub use yuv_p10_rgba::yuv444_p10_to_bgra; -pub use yuv_p10_rgba::yuv444_p10_to_bgra_be; \ No newline at end of file +pub use yuv_p10_rgba::yuv444_p10_be_to_bgra; \ No newline at end of file diff --git a/src/rgb_to_y.rs b/src/rgb_to_y.rs index b90f089..577c57b 100644 --- a/src/rgb_to_y.rs +++ b/src/rgb_to_y.rs @@ -134,9 +134,10 @@ fn rgbx_to_y( for x in cx..width as usize { let px = x * channels; - let r = rgba[rgba_offset + px + source_channels.get_r_channel_offset()] as i32; - let g = rgba[rgba_offset + px + source_channels.get_g_channel_offset()] as i32; - let b = rgba[rgba_offset + px + source_channels.get_b_channel_offset()] as i32; + let dst_offset = rgba_offset + px; + let r = rgba[dst_offset + source_channels.get_r_channel_offset()] as i32; + let g = rgba[dst_offset + source_channels.get_g_channel_offset()] as i32; + let b = rgba[dst_offset + source_channels.get_b_channel_offset()] as i32; let y = (r * transform.yr + g * transform.yg + b * transform.yb + bias_y) >> 8; y_plane[y_offset + x] = y as u8; } diff --git a/src/rgba_to_nv.rs b/src/rgba_to_nv.rs index 0067177..cb08044 100644 --- a/src/rgba_to_nv.rs +++ b/src/rgba_to_nv.rs @@ -39,7 +39,7 @@ fn rgbx_to_nv kr_kb.kb, ); let transform = transform_precise.to_integers(8); - let precision_scale = (1 << 8) as f32; + let precision_scale = (1i32 << 8i32) as f32; let bias_y = ((range.bias_y as f32 + 0.5f32) * precision_scale) as i32; let bias_uv = ((range.bias_uv as f32 + 0.5f32) * precision_scale) as i32; @@ -253,8 +253,9 @@ fn rgbx_to_nv } match chroma_subsampling { YuvChromaSample::YUV420 | YuvChromaSample::YUV422 => { - if x + 1 < width as usize { - let next_px = (x + 1) * channels; + let next_x = x + 1; + if next_x < width as usize { + let next_px = next_x * channels; let r = rgba[rgba_offset + next_px + source_channels.get_r_channel_offset()] as i32; let g = rgba[rgba_offset + next_px + source_channels.get_g_channel_offset()] @@ -263,7 +264,7 @@ fn rgbx_to_nv as i32; let y_1 = (r * transform.yr + g * transform.yg + b * transform.yb + bias_y) >> 8; - y_plane[y_offset + x + 1] = y_1 as u8; + y_plane[y_offset + next_x] = y_1 as u8; } } _ => {} @@ -310,7 +311,7 @@ fn rgbx_to_nv /// This function panics if the lengths of the planes or the input RGB data are not valid based /// on the specified width, height, and strides, or if invalid YUV range or matrix is provided. /// -pub fn rgb_to_yuv_nv_16( +pub fn rgb_to_yuv_nv16( y_plane: &mut [u8], y_stride: u32, uv_plane: &mut [u8], @@ -481,7 +482,7 @@ pub fn rgb_to_yuv_nv12( ); } -/// Convert RGBA image data to YUV NV16 bi-planar format. +/// Convert RGBA image data to YUV NV12 bi-planar format. /// /// This function performs RGBA to YUV conversion and stores the result in YUV NV12 bi-planar format, /// with plane for Y (luminance), and bi-plane UV (chrominance) components. @@ -534,7 +535,7 @@ pub fn rgba_to_yuv_nv12( ); } -/// Convert BGRA image data to YUV NV16 bi-planar format. +/// Convert BGRA image data to YUV NV12 bi-planar format. /// /// This function performs BGRA to YUV conversion and stores the result in YUV NV12 bi-planar format, /// with plane for Y (luminance), and bi-plane UV (chrominance) components. diff --git a/src/yuv_nv_p10_to_rgba.rs b/src/yuv_nv_p10_to_rgba.rs index be87127..014e9f1 100644 --- a/src/yuv_nv_p10_to_rgba.rs +++ b/src/yuv_nv_p10_to_rgba.rs @@ -489,7 +489,7 @@ pub fn yuv_nv16_p10_to_bgra( /// This function panics if the lengths of the planes or the input BGRA data are not valid based /// on the specified width, height, and strides, or if invalid YUV range or matrix is provided. /// -pub fn yuv_nv12_p10_to_bgra_be( +pub fn yuv_nv12_p10_be_to_bgra( y_plane: &[u16], y_stride: u32, uv_plane: &[u16], @@ -541,7 +541,7 @@ pub fn yuv_nv12_p10_to_bgra_be( /// This function panics if the lengths of the planes or the input BGRA data are not valid based /// on the specified width, height, and strides, or if invalid YUV range or matrix is provided. /// -pub fn yuv_nv16_p10_to_bgra_be( +pub fn yuv_nv16_p10_be_to_bgra( y_plane: &[u16], y_stride: u32, uv_plane: &[u16], @@ -577,7 +577,7 @@ pub fn yuv_nv16_p10_to_bgra_be( /// /// This function takes YUV NV16 data with 10-bit precision and MSB ordering, /// and converts it to BGRA format with 8-bit precision. -/// This format is used by apple and corresponds to kCVPixelFormatType_420YpCbCr10BiPlanarFullRange/kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange +/// This format is used by Apple and corresponds to kCVPixelFormatType_420YpCbCr10BiPlanarFullRange/kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange /// /// # Arguments /// @@ -630,7 +630,7 @@ pub fn yuv_nv12_p10_msb_to_bgra( /// /// This function takes YUV NV16 data with 10-bit precision and MSB ordering, /// and converts it to BGRA format with 8-bit precision. -/// This format is used by apple and corresponds to kCVPixelFormatType_422YpCbCr10BiPlanarFullRange/kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange +/// This format is used by Apple and corresponds to kCVPixelFormatType_422YpCbCr10BiPlanarFullRange/kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange /// /// # Arguments /// @@ -678,3 +678,110 @@ pub fn yuv_nv16_p10_msb_to_bgra( matrix, ); } + + +/// Convert YUV NV12 format with 10-bit pixel format (MSB) to RGBA format. +/// +/// This function takes YUV NV16 data with 10-bit precision and MSB ordering, +/// and converts it to RGBA format with 8-bit precision. +/// This format is used by Apple and corresponds to kCVPixelFormatType_420YpCbCr10BiPlanarFullRange/kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange +/// +/// # Arguments +/// +/// * `y_plane` - A slice containing Y (luminance) with 10 bit depth stored in Most Significant Bytes of u16. +/// * `y_stride` - The stride (bytes per row) for the Y plane. +/// * `uv_plane` - A slice to load the UV (chrominance) with 10 bit depth stored in Most Significant Bytes of u16. +/// * `uv_stride` - The stride (bytes per row) for the UV plane. +/// * `width` - The width of the YUV image. +/// * `height` - The height of the YUV image. +/// * `bgra_data` - A mutable slice to store the converted RGBA data. +/// +/// # Panics +/// +/// This function panics if the lengths of the planes or the input RGBA data are not valid based +/// on the specified width, height, and strides, or if invalid YUV range or matrix is provided. +/// +pub fn yuv_nv12_p10_msb_to_rgba( + y_plane: &[u16], + y_stride: u32, + uv_plane: &[u16], + uv_stride: u32, + bgra: &mut [u8], + bgra_stride: u32, + width: u32, + height: u32, + range: YuvRange, + matrix: YuvStandardMatrix, +) { + yuv_nv12_p10_to_bgra_impl::< + { YuvSourceChannels::Rgba as u8 }, + { YuvNVOrder::UV as u8 }, + { YuvChromaSample::YUV420 as u8 }, + { YuvEndian::LittleEndian as u8 }, + { YuvBytesPosition::MostSignificantBytes as u8 }, + >( + y_plane, + y_stride, + uv_plane, + uv_stride, + bgra, + bgra_stride, + width, + height, + range, + matrix, + ); +} + +/// Convert YUV NV16 format with 10-bit pixel format (MSB) to RGBA format. +/// +/// This function takes YUV NV16 data with 10-bit precision and MSB ordering, +/// and converts it to RGBA format with 8-bit precision. +/// This format is used by Apple and corresponds to kCVPixelFormatType_422YpCbCr10BiPlanarFullRange/kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange +/// +/// # Arguments +/// +/// * `y_plane` - A slice containing Y (luminance) with 10 bit depth stored in Most Significant Bytes of u16. +/// * `y_stride` - The stride (bytes per row) for the Y plane. +/// * `uv_plane` - A slice to load the UV (chrominance) with 10 bit depth stored in Most Significant Bytes of u16. +/// * `uv_stride` - The stride (bytes per row) for the UV plane. +/// * `width` - The width of the YUV image. +/// * `height` - The height of the YUV image. +/// * `bgra_data` - A mutable slice to store the converted RGBA data. +/// +/// # Panics +/// +/// This function panics if the lengths of the planes or the input RGBA data are not valid based +/// on the specified width, height, and strides, or if invalid YUV range or matrix is provided. +/// +pub fn yuv_nv16_p10_msb_to_rgba( + y_plane: &[u16], + y_stride: u32, + uv_plane: &[u16], + uv_stride: u32, + bgra: &mut [u8], + bgra_stride: u32, + width: u32, + height: u32, + range: YuvRange, + matrix: YuvStandardMatrix, +) { + yuv_nv12_p10_to_bgra_impl::< + { YuvSourceChannels::Rgba as u8 }, + { YuvNVOrder::UV as u8 }, + { YuvChromaSample::YUV422 as u8 }, + { YuvEndian::LittleEndian as u8 }, + { YuvBytesPosition::MostSignificantBytes as u8 }, + >( + y_plane, + y_stride, + uv_plane, + uv_stride, + bgra, + bgra_stride, + width, + height, + range, + matrix, + ); +} diff --git a/src/yuv_p10_rgba.rs b/src/yuv_p10_rgba.rs index 602bb2e..9add00b 100644 --- a/src/yuv_p10_rgba.rs +++ b/src/yuv_p10_rgba.rs @@ -459,7 +459,7 @@ pub fn yuv422_p10_to_bgra( /// This function panics if the lengths of the planes or the input BGRA data are not valid based /// on the specified width, height, and strides, or if invalid YUV range or matrix is provided. /// -pub fn yuv420_p10_to_bgra_be( +pub fn yuv420_p10_be_to_bgra( y_plane: &[u16], y_stride: u32, u_plane: &[u16], @@ -519,7 +519,7 @@ pub fn yuv420_p10_to_bgra_be( /// This function panics if the lengths of the planes or the input BGRA data are not valid based /// on the specified width, height, and strides, or if invalid YUV range or matrix is provided. /// -pub fn yuv422_p10_to_bgra_be( +pub fn yuv422_p10_be_to_bgra( y_plane: &[u16], y_stride: u32, u_plane: &[u16], @@ -699,7 +699,7 @@ pub fn yuv422_p10_to_rgba( /// This function panics if the lengths of the planes or the input RGBA data are not valid based /// on the specified width, height, and strides, or if invalid YUV range or matrix is provided. /// -pub fn yuv420_p10_to_rgba_be( +pub fn yuv420_p10_be_to_rgba( y_plane: &[u16], y_stride: u32, u_plane: &[u16], @@ -759,7 +759,7 @@ pub fn yuv420_p10_to_rgba_be( /// This function panics if the lengths of the planes or the input RGBA data are not valid based /// on the specified width, height, and strides, or if invalid YUV range or matrix is provided. /// -pub fn yuv422_p10_to_rgba_be( +pub fn yuv422_p10_be_to_rgba( y_plane: &[u16], y_stride: u32, u_plane: &[u16], @@ -879,7 +879,7 @@ pub fn yuv444_p10_to_rgba( /// This function panics if the lengths of the planes or the input RGBA data are not valid based /// on the specified width, height, and strides, or if invalid YUV range or matrix is provided. /// -pub fn yuv444_p10_to_rgba_be( +pub fn yuv444_p10_be_to_rgba( y_plane: &[u16], y_stride: u32, u_plane: &[u16], @@ -999,7 +999,7 @@ pub fn yuv444_p10_to_bgra( /// This function panics if the lengths of the planes or the input BGRA data are not valid based /// on the specified width, height, and strides, or if invalid YUV range or matrix is provided. /// -pub fn yuv444_p10_to_bgra_be( +pub fn yuv444_p10_be_to_bgra( y_plane: &[u16], y_stride: u32, u_plane: &[u16],