Skip to content

Truly independent web browser

LibGfx+Tests: Bound bi-planar YUV slices by the rows that are required

Zaggy102455073bdcdbb6
diff --git a/Libraries/LibGfx/Rust/src/yuv.rs b/Libraries/LibGfx/Rust/src/yuv.rs
index bad34c08743..df3ea4b9b41 100644
--- a/Libraries/LibGfx/Rust/src/yuv.rs
+++ b/Libraries/LibGfx/Rust/src/yuv.rs
@@ -181,8 +181,17 @@ pub unsafe extern "C" fn yuv_u16_to_rgba(
result.is_ok()
}
+/// The number of elements a plane spans when its rows start `stride` elements apart, but its last row may end right
+/// after its `row_length` elements, without padding up to the stride.
+fn biplanar_plane_length(stride: u32, row_length: usize, rows: usize) -> Option<usize> {
+ (stride as usize)
+ .checked_mul(rows.checked_sub(1)?)?
+ .checked_add(row_length)
+}
+
/// # Safety
/// All plane pointers must be valid for the specified dimensions and strides.
+/// Each plane's last row may end without padding up to its stride.
/// `dst` must point to a buffer of at least `dst_stride * height` bytes.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn yuv_nv12_to_rgba(
@@ -197,8 +206,13 @@ pub unsafe extern "C" fn yuv_nv12_to_rgba(
range: YUVRange,
matrix: YUVMatrix,
) -> bool {
- let y_len = y_stride as usize * height as usize;
- let uv_len = uv_stride as usize * height.div_ceil(2) as usize;
+ let Some(y_len) = biplanar_plane_length(y_stride, width as usize, height as usize) else {
+ return false;
+ };
+ let Some(uv_len) = biplanar_plane_length(uv_stride, width.div_ceil(2) as usize * 2, height.div_ceil(2) as usize)
+ else {
+ return false;
+ };
let bi_planar_image = YuvBiPlanarImage {
y_plane: unsafe { ak::slice_from_raw_parts(y_plane, y_len) },
@@ -226,6 +240,7 @@ pub unsafe extern "C" fn yuv_nv12_to_rgba(
/// # Safety
/// All plane pointers must be valid for the specified dimensions and strides.
/// Strides are in components per row, not bytes.
+/// Each plane's last row may end without padding up to its stride.
/// `dst` must point to a buffer of at least `dst_stride * height` bytes.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn yuv_p010_to_rgba(
@@ -240,8 +255,13 @@ pub unsafe extern "C" fn yuv_p010_to_rgba(
range: YUVRange,
matrix: YUVMatrix,
) -> bool {
- let y_len = y_stride as usize * height as usize;
- let uv_len = uv_stride as usize * height.div_ceil(2) as usize;
+ let Some(y_len) = biplanar_plane_length(y_stride, width as usize, height as usize) else {
+ return false;
+ };
+ let Some(uv_len) = biplanar_plane_length(uv_stride, width.div_ceil(2) as usize * 2, height.div_ceil(2) as usize)
+ else {
+ return false;
+ };
let bi_planar_image = YuvBiPlanarImage {
y_plane: unsafe { ak::slice_from_raw_parts(y_plane, y_len) },
diff --git a/Tests/Compositor/TestVideoSurfaceImage.mm b/Tests/Compositor/TestVideoSurfaceImage.mm
index 6e42f743f28..a0d816cbb94 100644
--- a/Tests/Compositor/TestVideoSurfaceImage.mm
+++ b/Tests/Compositor/TestVideoSurfaceImage.mm
@@ -375,3 +375,37 @@ TEST_CASE(biplanar_conversion_refuses_planes_shorter_than_the_size)
EXPECT(Gfx::biplanar_yuv_to_bitmap({ 64, 48 }, 8, {}, luma_plane, { .data = chroma.bytes().slice(0, 64), .stride = 64 }).is_error());
EXPECT(Gfx::biplanar_yuv_to_bitmap({ 64, 48 }, 8, {}, luma_plane, { .data = chroma.bytes(), .stride = 16 }).is_error());
}
+
+// Only the rows before a plane's last one have to be padded up to its stride, so a plane can end right after its last
+// sample. Such a plane must convert to the same pixels as a padded one.
+TEST_CASE(biplanar_conversion_reads_planes_whose_last_row_is_not_padded)
+{
+ constexpr Gfx::IntSize size { 5, 3 };
+ constexpr size_t chroma_width = 3;
+ constexpr size_t chroma_height = 2;
+
+ for (u8 bit_depth : { 8, 10 }) {
+ size_t bytes_per_sample = bit_depth > 8 ? 2 : 1;
+ size_t stride = 8 * bytes_per_sample;
+ auto luma_row_byte_count = size.width() * bytes_per_sample;
+ auto chroma_row_byte_count = chroma_width * 2 * bytes_per_sample;
+
+ auto padded_luma = MUST(ByteBuffer::create_uninitialized(stride * size.height()));
+ auto padded_chroma = MUST(ByteBuffer::create_uninitialized(stride * chroma_height));
+ for (size_t index = 0; index < padded_luma.size(); index++)
+ padded_luma[index] = static_cast<u8>(40 + (index * 7));
+ for (size_t index = 0; index < padded_chroma.size(); index++)
+ padded_chroma[index] = static_cast<u8>(90 + (index * 13));
+
+ // The unpadded planes are copies in buffers of their own, which end right after their last sample.
+ auto unpadded_luma = MUST(ByteBuffer::copy(padded_luma.bytes().trim((stride * (size.height() - 1)) + luma_row_byte_count)));
+ auto unpadded_chroma = MUST(ByteBuffer::copy(padded_chroma.bytes().trim((stride * (chroma_height - 1)) + chroma_row_byte_count)));
+
+ auto padded = MUST(Gfx::biplanar_yuv_to_bitmap(size, bit_depth, {}, { .data = padded_luma.bytes(), .stride = stride }, { .data = padded_chroma.bytes(), .stride = stride }));
+ auto unpadded = MUST(Gfx::biplanar_yuv_to_bitmap(size, bit_depth, {}, { .data = unpadded_luma.bytes(), .stride = stride }, { .data = unpadded_chroma.bytes(), .stride = stride }));
+ for (int y = 0; y < size.height(); y++) {
+ for (int x = 0; x < size.width(); x++)
+ EXPECT_EQ(unpadded->get_pixel(x, y), padded->get_pixel(x, y));
+ }
+ }
+}