util/ycbcr: Fix adjust_to_range

Also add some tests for this.

Fixes: f6f2e16e35 ("util: add common ycbcr coefficient math code")
Signed-off-by: Benjamin Cheng <benjamin.cheng@amd.com>
Reviewed-by: David Rosca <david.rosca@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/41787>
This commit is contained in:
Benjamin Cheng 2026-05-25 13:19:44 -04:00 committed by Marge Bot
parent 4c03db9ec8
commit 246ca800dd
2 changed files with 58 additions and 1 deletions

View file

@ -518,3 +518,60 @@ TEST(u_ycbcr_test, to_ycbcr_range_and_back)
test_to_ycbcr_range_and_back_coeffs(util_ycbcr_bt709_coeffs);
test_to_ycbcr_range_and_back_coeffs(util_ycbcr_bt2020_coeffs);
}
static void
test_narrow_ycbcr_to_narrow_rgb_coeffs(const float coeffs[3])
{
const unsigned bpc[3] = {8, 8, 8};
float in_range[3][2], out_range[3][2];
util_get_narrow_range_coeffs(in_range, bpc);
util_get_narrow_range_rgb_coeffs(out_range, bpc);
float mat[3][4];
util_get_ycbcr_to_rgb_matrix(mat, coeffs);
util_ycbcr_adjust_from_range(mat, in_range);
util_ycbcr_adjust_to_range(mat, out_range);
const struct {
uint8_t input[3];
uint8_t expected[3];
} test_data[] = {
{ { 16, 128, 128 }, { 16, 16, 16 } },
{ { 235, 128, 128 }, { 235, 235, 235 } },
{ { 126, 128, 128 }, { 126, 126, 126 } },
};
for (size_t i = 0; i < ARRAY_SIZE(test_data); ++i) {
float input[3] = {
test_data[i].input[0] / 255.0f,
test_data[i].input[1] / 255.0f,
test_data[i].input[2] / 255.0f,
};
float result[3];
for (int c = 0; c < 3; ++c) {
result[c] = input[0] * mat[c][0] +
input[1] * mat[c][1] +
input[2] * mat[c][2] +
mat[c][3];
}
float expected[3] = {
test_data[i].expected[0] / 255.0f,
test_data[i].expected[1] / 255.0f,
test_data[i].expected[2] / 255.0f,
};
EXPECT_NEAR(expected[0], result[0], 1e-6);
EXPECT_NEAR(expected[1], result[1], 1e-6);
EXPECT_NEAR(expected[2], result[2], 1e-6);
}
}
TEST(u_ycbcr_test, narrow_ycbcr_to_narrow_rgb)
{
test_narrow_ycbcr_to_narrow_rgb_coeffs(util_ycbcr_bt601_coeffs);
test_narrow_ycbcr_to_narrow_rgb_coeffs(util_ycbcr_bt709_coeffs);
test_narrow_ycbcr_to_narrow_rgb_coeffs(util_ycbcr_bt2020_coeffs);
}

View file

@ -151,7 +151,7 @@ util_ycbcr_adjust_to_range(float mat[3][4],
mat[i][0] = mat[i][0] * tmp;
mat[i][1] = mat[i][1] * tmp;
mat[i][2] = mat[i][2] * tmp;
mat[i][3] -= range[i][1] * tmp;
mat[i][3] = mat[i][3] * tmp - range[i][1] * tmp;
}
}