Truly independent web browser
LibGfx+Tests: Bounds check biplanar YUV conversion to the whole stride
Zaggy10247703ccc135a5
diff --git a/Libraries/LibGfx/Rust/src/yuv.rs b/Libraries/LibGfx/Rust/src/yuv.rsindex f594f7998c2..0acfd0efd9b 100644--- a/Libraries/LibGfx/Rust/src/yuv.rs+++ b/Libraries/LibGfx/Rust/src/yuv.rs@@ -181,17 +181,9 @@ 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.+/// Each plane must span its stride times its row count, so its last row is padded up to its stride as well./// `dst` must point to a buffer of at least `dst_stride * height` bytes.#[unsafe(no_mangle)]pub unsafe extern "C" fn yuv_biplanar_u8_to_rgba(@@ -208,12 +200,11 @@ pub unsafe extern "C" fn yuv_biplanar_u8_to_rgba(range: YUVRange,matrix: YUVMatrix,) -> bool {- let Some(y_len) = biplanar_plane_length(y_stride, width as usize, height as usize) else {+ let Some(y_len) = (y_stride as usize).checked_mul(height as usize) else {return false;};- let chroma_width = if subsampling_x { width.div_ceil(2) } else { width } as usize;let chroma_height = if subsampling_y { height.div_ceil(2) } else { height } as usize;- let Some(uv_len) = biplanar_plane_length(uv_stride, chroma_width * 2, chroma_height) else {+ let Some(uv_len) = (uv_stride as usize).checked_mul(chroma_height) else {return false;};@@ -250,7 +241,7 @@ pub unsafe extern "C" fn yuv_biplanar_u8_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.+/// Each plane must span its stride times its row count, so its last row is padded up to its stride as well./// `dst` must point to a buffer of at least `dst_stride * height` bytes./// Samples must sit in the high 10 bits of each u16.#[unsafe(no_mangle)]@@ -268,12 +259,11 @@ pub unsafe extern "C" fn yuv_biplanar_u16_to_rgba(range: YUVRange,matrix: YUVMatrix,) -> bool {- let Some(y_len) = biplanar_plane_length(y_stride, width as usize, height as usize) else {+ let Some(y_len) = (y_stride as usize).checked_mul(height as usize) else {return false;};- let chroma_width = if subsampling_x { width.div_ceil(2) } else { width } as usize;let chroma_height = if subsampling_y { height.div_ceil(2) } else { height } as usize;- let Some(uv_len) = biplanar_plane_length(uv_stride, chroma_width * 2, chroma_height) else {+ let Some(uv_len) = (uv_stride as usize).checked_mul(chroma_height) else {return false;};diff --git a/Libraries/LibGfx/YUVData.cpp b/Libraries/LibGfx/YUVData.cppindex 1ad247a44c7..3897a8ecfe7 100644--- a/Libraries/LibGfx/YUVData.cpp+++ b/Libraries/LibGfx/YUVData.cpp@@ -211,8 +211,7 @@ ErrorOr<NonnullRefPtr<Bitmap>> biplanar_yuv_to_bitmap(IntSize size, u8 bit_depthif (plane.stride < row_byte_count)return false;Checked<size_t> required_size = plane.stride;- required_size *= row_count - 1;- required_size += row_byte_count;+ required_size *= row_count;return !required_size.has_overflow() && plane.data.size() >= required_size.value();};auto chroma_size = subsampling.subsampled_size(size.to_type<size_t>());diff --git a/Libraries/LibGfx/YUVData.h b/Libraries/LibGfx/YUVData.hindex 139658018e8..07d775968a1 100644--- a/Libraries/LibGfx/YUVData.h+++ b/Libraries/LibGfx/YUVData.h@@ -65,7 +65,8 @@ struct BiplanarYUVPlane {};// Converts the bi-planar layouts hardware decoders hand back, such as NV12, or P010 for bit depths above 8. Samples deeper-// than 8 bits sit in the high bits of each 16-bit component.+// than 8 bits sit in the high bits of each 16-bit component. Every row of each plane, the last included, must be+// padded up to its stride.ErrorOr<NonnullRefPtr<Bitmap>> biplanar_yuv_to_bitmap(IntSize, u8 bit_depth, Media::Subsampling, Media::CodingIndependentCodePoints, BiplanarYUVPlane luma, BiplanarYUVPlane chroma);}diff --git a/Tests/Compositor/TestVideoSurfaceImage.mm b/Tests/Compositor/TestVideoSurfaceImage.mmindex 33ffb2f46a0..f6a81b16ac3 100644--- a/Tests/Compositor/TestVideoSurfaceImage.mm+++ b/Tests/Compositor/TestVideoSurfaceImage.mm@@ -762,38 +762,3 @@ TEST_CASE(studio_range_rgb_is_stretched_to_full_range)VERIFY(planes_color.has_value());EXPECT(colors_are_close(*planes_color, expected_color));}--// 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 };-- for (auto subsampling : { Media::Subsampling::yuv420(), Media::Subsampling::yuv422(), Media::Subsampling::yuv444() }) {- for (u8 bit_depth : { 8, 10 }) {- size_t bytes_per_sample = bit_depth > 8 ? 2 : 1;- size_t stride = 16 * bytes_per_sample;- auto chroma_size = subsampling.subsampled_size(size.to_type<size_t>());- auto luma_row_byte_count = size.width() * bytes_per_sample;- auto chroma_row_byte_count = chroma_size.width() * 2 * bytes_per_sample;-- auto padded_luma = MUST(ByteBuffer::create_uninitialized(stride * size.height()));- auto padded_chroma = MUST(ByteBuffer::create_uninitialized(stride * chroma_size.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_size.height() - 1)) + chroma_row_byte_count)));-- auto padded = MUST(Gfx::biplanar_yuv_to_bitmap(size, bit_depth, subsampling, {}, { .data = padded_luma.bytes(), .stride = stride }, { .data = padded_chroma.bytes(), .stride = stride }));- auto unpadded = MUST(Gfx::biplanar_yuv_to_bitmap(size, bit_depth, subsampling, {}, { .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));- }- }- }-}diff --git a/Tests/LibGfx/CMakeLists.txt b/Tests/LibGfx/CMakeLists.txtindex 92b4f22d56b..a73fa353197 100644--- a/Tests/LibGfx/CMakeLists.txt+++ b/Tests/LibGfx/CMakeLists.txt@@ -10,6 +10,7 @@ set(TEST_SOURCESTestSharedFontProvider.cppTestWOFF.cppTestWOFF2.cpp+ TestYUVData.cpp)list(APPEND TEST_SOURCES TestBitmapSequence.cpp TestShareableBitmap.cpp)diff --git a/Tests/LibGfx/TestYUVData.cpp b/Tests/LibGfx/TestYUVData.cppnew file mode 100644index 00000000000..b4feddfa91f--- /dev/null+++ b/Tests/LibGfx/TestYUVData.cpp@@ -0,0 +1,126 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/ByteBuffer.h>+#include <LibGfx/Bitmap.h>+#include <LibGfx/YUVData.h>+#include <LibTest/TestCase.h>++namespace {++enum class SampleAlignment : u8 {+ LowBits,+ HighBits,+};++void write_sample(ByteBuffer& plane, size_t offset, u16 value, u8 bit_depth, SampleAlignment alignment)+{+ if (bit_depth <= 8) {+ plane[offset] = static_cast<u8>(value);+ return;+ }+ if (alignment == SampleAlignment::HighBits)+ value <<= 16 - bit_depth;+ plane[offset] = static_cast<u8>(value & 0xff);+ plane[offset + 1] = static_cast<u8>(value >> 8);+}++bool colors_are_close(Gfx::Color a, Gfx::Color b)+{+ auto close = [](u8 x, u8 y) { return (x > y ? x - y : y - x) <= 1; };+ return close(a.red(), b.red()) && close(a.green(), b.green()) && close(a.blue(), b.blue()) && a.alpha() == b.alpha();+}++constexpr size_t PLANE_STRIDE_IN_SAMPLES = 16;+constexpr u8 PADDING_BYTE = 0xee;++}++// Every luma row and every chroma row holds samples of its own. A row that is left out, or that takes its chroma from+// another row, then converts to different pixels than the planar conversion of the same samples.+TEST_CASE(biplanar_conversion_matches_planar_conversion)+{+ for (int height : { 1, 2, 3, 4, 5 }) {+ Gfx::IntSize size { 5, height };+ for (auto subsampling : { Media::Subsampling::yuv420(), Media::Subsampling::yuv422(), Media::Subsampling::yuv444() }) {+ for (u8 bit_depth : { 8, 10 }) {+ size_t bytes_per_sample = bit_depth > 8 ? 2 : 1;+ u16 scale = bit_depth > 8 ? 4 : 1;+ size_t stride = PLANE_STRIDE_IN_SAMPLES * bytes_per_sample;+ auto chroma_size = subsampling.subsampled_size(size.to_type<size_t>());++ auto luma_sample = [&](size_t x, size_t y) { return static_cast<u16>((60 + (y * 25) + (x * 4)) * scale); };+ auto blue_chroma_sample = [&](size_t x, size_t y) { return static_cast<u16>((110 + (y * 10) + (x * 3)) * scale); };+ auto red_chroma_sample = [&](size_t x, size_t y) { return static_cast<u16>((150 - (y * 10) + (x * 2)) * scale); };++ auto biplanar_luma = MUST(ByteBuffer::create_uninitialized(stride * size.height()));+ auto biplanar_chroma = MUST(ByteBuffer::create_uninitialized(stride * chroma_size.height()));+ biplanar_luma.bytes().fill(PADDING_BYTE);+ biplanar_chroma.bytes().fill(PADDING_BYTE);++ auto plane_sizes = MUST(Gfx::YUVData::plane_sizes(size, bit_depth, subsampling));+ auto planar_luma = MUST(ByteBuffer::create_zeroed(plane_sizes.y));+ auto planar_blue_chroma = MUST(ByteBuffer::create_zeroed(plane_sizes.u));+ auto planar_red_chroma = MUST(ByteBuffer::create_zeroed(plane_sizes.v));++ for (size_t y = 0; y < static_cast<size_t>(size.height()); y++) {+ for (size_t x = 0; x < static_cast<size_t>(size.width()); x++) {+ auto sample = luma_sample(x, y);+ write_sample(biplanar_luma, (y * stride) + (x * bytes_per_sample), sample, bit_depth, SampleAlignment::HighBits);+ write_sample(planar_luma, ((y * size.width()) + x) * bytes_per_sample, sample, bit_depth, SampleAlignment::LowBits);+ }+ }+ for (size_t y = 0; y < chroma_size.height(); y++) {+ for (size_t x = 0; x < chroma_size.width(); x++) {+ auto blue = blue_chroma_sample(x, y);+ auto red = red_chroma_sample(x, y);+ auto biplanar_offset = (y * stride) + (x * 2 * bytes_per_sample);+ write_sample(biplanar_chroma, biplanar_offset, blue, bit_depth, SampleAlignment::HighBits);+ write_sample(biplanar_chroma, biplanar_offset + bytes_per_sample, red, bit_depth, SampleAlignment::HighBits);+ auto planar_offset = ((y * chroma_size.width()) + x) * bytes_per_sample;+ write_sample(planar_blue_chroma, planar_offset, blue, bit_depth, SampleAlignment::LowBits);+ write_sample(planar_red_chroma, planar_offset, red, bit_depth, SampleAlignment::LowBits);+ }+ }++ auto biplanar = MUST(Gfx::biplanar_yuv_to_bitmap(size, bit_depth, subsampling, {},+ { .data = biplanar_luma.bytes(), .stride = stride },+ { .data = biplanar_chroma.bytes(), .stride = stride }));+ auto planar = MUST(MUST(Gfx::YUVData::create(size, bit_depth, subsampling, {},+ planar_luma.bytes(), planar_blue_chroma.bytes(), planar_red_chroma.bytes()))+ .to_bitmap());++ for (int y = 0; y < size.height(); y++) {+ for (int x = 0; x < size.width(); x++) {+ auto biplanar_color = biplanar->get_pixel(x, y);+ auto planar_color = planar->get_pixel(x, y);+ if (!colors_are_close(biplanar_color, planar_color)) {+ FAIL(ByteString::formatted("{}x{} at {} bits, chroma {}x{}: pixel ({}, {}) is {}, but the planar conversion gives {}",+ size.width(), size.height(), bit_depth, chroma_size.width(), chroma_size.height(), x, y, biplanar_color, planar_color));+ }+ }+ }+ }+ }+ }+}++TEST_CASE(biplanar_conversion_refuses_planes_whose_last_row_is_not_padded)+{+ constexpr Gfx::IntSize size { 5, 3 };+ constexpr size_t stride = PLANE_STRIDE_IN_SAMPLES;++ auto luma = MUST(ByteBuffer::create_zeroed(stride * size.height()));+ auto chroma = MUST(ByteBuffer::create_zeroed(stride * 2));+ Gfx::BiplanarYUVPlane padded_luma { .data = luma.bytes(), .stride = stride };+ Gfx::BiplanarYUVPlane padded_chroma { .data = chroma.bytes(), .stride = stride };+ Gfx::BiplanarYUVPlane unpadded_luma { .data = luma.bytes().trim((stride * (size.height() - 1)) + size.width()), .stride = stride };+ Gfx::BiplanarYUVPlane unpadded_chroma { .data = chroma.bytes().trim(stride + 6), .stride = stride };++ EXPECT(!Gfx::biplanar_yuv_to_bitmap(size, 8, Media::Subsampling::yuv420(), {}, padded_luma, padded_chroma).is_error());+ EXPECT(Gfx::biplanar_yuv_to_bitmap(size, 8, Media::Subsampling::yuv420(), {}, unpadded_luma, padded_chroma).is_error());+ EXPECT(Gfx::biplanar_yuv_to_bitmap(size, 8, Media::Subsampling::yuv420(), {}, padded_luma, unpadded_chroma).is_error());+}