fix(linux): preserve full NV12 luma range on CUDA

This commit is contained in:
Dave Lane committed 2026-11-19 22:54:09 -05:00
1 parent 00a29943a3
commit 9d0dcc48b9
3 files changed
+59 -5

No files matched your search

+4 -4
View File
@@ -210,10 +210,10 @@ namespace cuda {
dstUV[0] = nv12_chroma_sample(uv_lt.x, uv_rt.x, uv_lb.x, uv_rb.x);
dstUV[1] = nv12_chroma_sample(uv_lt.y, uv_rt.y, uv_lb.y, uv_rb.y);
dstY0[0] = calcY(rgb_lt, color_matrix) * 245.0f; // 245.0f is a magic number to ensure slight changes in luminosity are more visible
dstY0[1] = calcY(rgb_rt, color_matrix) * 245.0f; // 245.0f is a magic number to ensure slight changes in luminosity are more visible
dstY1[0] = calcY(rgb_lb, color_matrix) * 245.0f; // 245.0f is a magic number to ensure slight changes in luminosity are more visible
dstY1[1] = calcY(rgb_rb, color_matrix) * 245.0f; // 245.0f is a magic number to ensure slight changes in luminosity are more visible
dstY0[0] = nv12_luma_sample(calcY(rgb_lt, color_matrix));
dstY0[1] = nv12_luma_sample(calcY(rgb_rt, color_matrix));
dstY1[0] = nv12_luma_sample(calcY(rgb_lb, color_matrix));
dstY1[1] = nv12_luma_sample(calcY(rgb_rb, color_matrix));
}
__global__ void RGBA_to_YUV444(
+12
View File
@@ -33,6 +33,18 @@ namespace cuda {
return static_cast<std::uint8_t>(value);
}
/**
* @brief Quantize an NV12 luma sample from the normalized color matrix.
*
* The matrix already applies the selected full or limited range.
*
* @param y Normalized luma.
* @return The rounded and clamped 8-bit luma code value.
*/
SUNSHINE_CUDA_HOST_DEVICE inline std::uint8_t nv12_luma_sample(float y) {
return clamp_to_uint8(y * 255.0f + 0.5f);
}
/**
* @brief Compute the NV12 chroma sample of a 2x2 pixel block.
*
+43 -1
View File
@@ -1,6 +1,6 @@
/**
* @file tests/unit/platform/linux/test_cuda_color.cpp
* @brief Tests for the 8-bit chroma samples written by the CUDA NV12 conversion.
* @brief Tests for the 8-bit luma and chroma samples written by the CUDA NV12 conversion.
*/
#ifdef __linux__
@@ -163,6 +163,19 @@ TEST(CudaColorTest, ClampsSamplesToTheUint8Range) {
EXPECT_EQ(clamp(std::numeric_limits<float>::quiet_NaN()), 0);
}
/**
* @brief Normalized NV12 luma rounds to the nearest 8-bit code value without wrapping.
*/
TEST(CudaColorTest, Nv12LumaRoundsAndClamps) {
EXPECT_EQ(cuda::nv12_luma_sample(-0.1f), 0);
EXPECT_EQ(cuda::nv12_luma_sample(0.0f), 0);
EXPECT_EQ(cuda::nv12_luma_sample(63.75f / 255.0f), 64);
EXPECT_EQ(cuda::nv12_luma_sample(64.25f / 255.0f), 64);
EXPECT_EQ(cuda::nv12_luma_sample(0.5f), 128);
EXPECT_EQ(cuda::nv12_luma_sample(1.0f), 255);
EXPECT_EQ(cuda::nv12_luma_sample(1.1f), 255);
}
/**
* @brief Full-range pure red keeps the maximum V and pure blue the maximum U.
*
@@ -261,6 +274,35 @@ namespace {
}
} // namespace
/**
* @brief The NV12 kernel preserves neutral luma across both full and limited range.
*
* This runs the kernel on the GPU, so it is skipped on hosts without a CUDA device.
*/
TEST(CudaNv12KernelTest, PreservesNeutralLumaInBothRanges) {
if (!cuda::test::has_device()) {
GTEST_SKIP() << "No CUDA device";
}
constexpr int width = 16;
constexpr int height = 8;
constexpr std::array<std::uint8_t, 4> levels {0, 64, 128, 255};
constexpr std::array<std::uint8_t, 4> limited_codes {16, 71, 126, 235};
for (auto colorspace : nv12_colorspaces) {
for (std::size_t i = 0; i < levels.size(); ++i) {
const rgb8_t gray {levels[i], levels[i], levels[i]};
SCOPED_TRACE(describe(colorspace, gray));
const auto planes = cuda::test::convert_to_nv12(solid_bgra_frame(gray, width, height), width, height, colorspace);
ASSERT_TRUE(planes.has_value());
ASSERT_EQ(planes->y.size(), static_cast<std::size_t>(width) * height);
const auto expected = colorspace.full_range ? levels[i] : limited_codes[i];
for (std::size_t pixel = 0; pixel < planes->y.size(); ++pixel) {
ASSERT_EQ(planes->y[pixel], expected) << "Y at pixel " << pixel;
}
}
}
}
/**
* @brief The NV12 kernel writes the test card's colors with the chroma their colorspace defines.
*