Skip to content

Commit

Permalink
Some small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed May 19, 2024
1 parent dfb3595 commit 5a2e2a9
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
20 changes: 11 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
pub use yuv_p10_rgba::yuv444_p10_be_to_bgra;
7 changes: 4 additions & 3 deletions src/rgb_to_y.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ fn rgbx_to_y<const ORIGIN_CHANNELS: u8>(

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;
}
Expand Down
15 changes: 8 additions & 7 deletions src/rgba_to_nv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>
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;

Expand Down Expand Up @@ -253,8 +253,9 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>
}
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()]
Expand All @@ -263,7 +264,7 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>
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;
}
}
_ => {}
Expand Down Expand Up @@ -310,7 +311,7 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>
/// 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],
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
115 changes: 111 additions & 4 deletions src/yuv_nv_p10_to_rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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,
);
}
12 changes: 6 additions & 6 deletions src/yuv_p10_rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit 5a2e2a9

Please sign in to comment.