Commit 3c834b6c authored by fbarchard@google.com's avatar fbarchard@google.com

add an exhaustic yuv color space test, and relax yuv conversion results to allow for future change.

BUG=324
TESTED=TestFullYUV
R=harryjin@google.com

Review URL: https://webrtc-codereview.appspot.com/37689004

git-svn-id: http://libyuv.googlecode.com/svn/trunk@1234 16f28f9a-4ce2-e073-06de-1de4eb20be90
parent c3d09f60
......@@ -115,11 +115,11 @@ TESTCS(TestI422, I422ToARGB, ARGBToI422, 0, 1, 0, 7)
TESTCS(TestJ420, J420ToARGB, ARGBToJ420, 1, 2, benchmark_width_, 3)
TESTCS(TestJ422, J422ToARGB, ARGBToJ422, 0, 1, 0, 4)
void TestYUVToRGB(int y, int u, int v, int &r, int &g, int &b,
int benchmark_width_, int benchmark_height_) {
const int kPixels = benchmark_width_ * benchmark_height_;
const int kHalfPixels = ((benchmark_width_ + 1) / 2) *
((benchmark_height_ + 1) / 2);
void YUVToRGB(int y, int u, int v, int* r, int* g, int* b) {
const int kWidth = 128;
const int kHeight = 2;
const int kPixels = kWidth * kHeight;
const int kHalfPixels = ((kWidth + 1) / 2) * ((kHeight + 1) / 2);
align_buffer_64(orig_y, kPixels);
align_buffer_64(orig_u, kHalfPixels);
align_buffer_64(orig_v, kHalfPixels);
......@@ -131,15 +131,15 @@ void TestYUVToRGB(int y, int u, int v, int &r, int &g, int &b,
MemRandomize(orig_pixels, kPixels * 4);
/* YUV converted to ARGB. */
I420ToARGB(orig_y, benchmark_width_,
orig_u, (benchmark_width_ + 1) / 2,
orig_v, (benchmark_width_ + 1) / 2,
orig_pixels, benchmark_width_ * 4,
benchmark_width_, benchmark_height_);
I420ToARGB(orig_y, kWidth,
orig_u, (kWidth + 1) / 2,
orig_v, (kWidth + 1) / 2,
orig_pixels, kWidth * 4,
kWidth, kHeight);
b = orig_pixels[0];
g = orig_pixels[1];
r = orig_pixels[2];
*b = orig_pixels[0];
*g = orig_pixels[1];
*r = orig_pixels[2];
free_aligned_buffer_64(orig_pixels);
free_aligned_buffer_64(orig_y);
......@@ -147,8 +147,8 @@ void TestYUVToRGB(int y, int u, int v, int &r, int &g, int &b,
free_aligned_buffer_64(orig_v);
}
int Clamp(double f) {
int i = static_cast<int>(round(f));
int RoundToByte(double f) {
int i = lrintf(f);
if (i < 0) {
i = 0;
}
......@@ -158,16 +158,20 @@ int Clamp(double f) {
return i;
}
void TestYUVToRGBReference(int y, int u, int v, int &r, int &g, int &b) {
r = Clamp((y - 16) * 1.164 + (v - 128) * 1.596);
g = Clamp((y - 16) * 1.164 + (u - 128) * -0.391 + (v - 128) * -0.813);
b = Clamp((y - 16) * 1.164 + (u - 128) * 2.018);
void YUVToRGBReference(int y, int u, int v, int* r, int* g, int* b) {
*r = RoundToByte((y - 16) * 1.164 + (v - 128) * 1.596);
*g = RoundToByte((y - 16) * 1.164 + (u - 128) * -0.391 + (v - 128) * -0.813);
*b = RoundToByte((y - 16) * 1.164 + (u - 128) * 2.018);
}
// TODO(fbarchard): Remove prototype once integrated.
// C prototype code
// TODO(fbarchard): Consider adjusting bias for accurate center point instead
// of accurate starting point.
#define YG 4901241 /* round(1.164 * 64 * 256 * 257) */
#define YGB 1192 /* round(1.164 * 64 * 16 ) */
#define YG 4901247 /* round(1.164 * 64 * 256) = 19071 * 0x0101 */
#define YGB 1192 /* round(1.164 * 64 * 16) */
#define UB -128 /* -min(128, round(2.018 * 64)) */
#define UG 25 /* -round(-0.391 * 64) */
......@@ -182,89 +186,89 @@ void TestYUVToRGBReference(int y, int u, int v, int &r, int &g, int &b) {
#define BG (UG * 128 + VG * 128 - YGB)
#define BR (UR * 128 + VR * 128 - YGB)
void TestYUVToRGBInt(int y, int u, int v, int &r, int &g, int &b) {
uint32 y1 = (uint32)(y * YG) >> 16;
b = Clamp((int32)(y1 - (v * VB + u * UB) + BB) >> 6);
g = Clamp((int32)(y1 - (v * VG + u * UG) + BG) >> 6);
r = Clamp((int32)(y1 - (v * VR + u * UR) + BR) >> 6);
void YUVToRGBInt(int y, int u, int v, int* r, int* g, int* b) {
uint32 y1 = static_cast<uint32>(y * YG) >> 16;
*b = RoundToByte(static_cast<int32>(y1 - (v * VB + u * UB) + BB) >> 6);
*g = RoundToByte(static_cast<int32>(y1 - (v * VG + u * UG) + BG) >> 6);
*r = RoundToByte(static_cast<int32>(y1 - (v * VR + u * UR) + BR) >> 6);
}
TEST_F(libyuvTest, TestYUV) {
int r0, g0, b0;
// black
TestYUVToRGBReference(16, 128, 128, r0, g0, b0);
YUVToRGBReference(16, 128, 128, &r0, &g0, &b0);
EXPECT_EQ(0, r0);
EXPECT_EQ(0, g0);
EXPECT_EQ(0, b0);
int r1, g1, b1;
TestYUVToRGB(16, 128, 128, r1, g1, b1, benchmark_width_, benchmark_height_);
YUVToRGB(16, 128, 128, &r1, &g1, &b1);
EXPECT_EQ(0, r1);
EXPECT_EQ(0, g1);
EXPECT_EQ(0, b1);
// white
TestYUVToRGBReference(240, 128, 128, r0, g0, b0);
YUVToRGBReference(240, 128, 128, &r0, &g0, &b0);
EXPECT_EQ(255, r0);
EXPECT_EQ(255, g0);
EXPECT_EQ(255, b0);
TestYUVToRGB(240, 128, 128, r1, g1, b1, benchmark_width_, benchmark_height_);
YUVToRGB(240, 128, 128, &r1, &g1, &b1);
EXPECT_EQ(255, r1);
EXPECT_EQ(255, g1);
EXPECT_EQ(255, b1);
// cyan (less red)
TestYUVToRGBReference(240, 255, 0, r0, g0, b0);
YUVToRGBReference(240, 255, 0, &r0, &g0, &b0);
EXPECT_EQ(56, r0);
EXPECT_EQ(255, g0);
EXPECT_EQ(255, b0);
TestYUVToRGB(240, 255, 0, r1, g1, b1, benchmark_width_, benchmark_height_);
EXPECT_EQ(55, r1);
YUVToRGB(240, 255, 0, &r1, &g1, &b1);
EXPECT_NEAR(56, r1, 1);
EXPECT_EQ(255, g1);
EXPECT_EQ(255, b1);
// green (less red and blue)
TestYUVToRGBReference(240, 0, 0, r0, g0, b0);
YUVToRGBReference(240, 0, 0, &r0, &g0, &b0);
EXPECT_EQ(56, r0);
EXPECT_EQ(255, g0);
EXPECT_EQ(2, b0);
TestYUVToRGB(240, 0, 0, r1, g1, b1, benchmark_width_, benchmark_height_);
EXPECT_EQ(55, r1);
YUVToRGB(240, 0, 0, &r1, &g1, &b1);
EXPECT_NEAR(56, r1, 1);
EXPECT_EQ(255, g1);
EXPECT_EQ(5, b1);
EXPECT_NEAR(6, b1, 1);
int r2, g2, b2;
for (int i = 0; i < 255; ++i) {
TestYUVToRGBReference(i, 128, 128, r0, g0, b0);
TestYUVToRGB(i, 128, 128, r1, g1, b1, benchmark_width_, benchmark_height_);
TestYUVToRGBInt(i, 128, 128, r2, g2, b2);
for (int i = 0; i < 256; ++i) {
YUVToRGBReference(i, 128, 128, &r0, &g0, &b0);
YUVToRGB(i, 128, 128, &r1, &g1, &b1);
YUVToRGBInt(i, 128, 128, &r2, &g2, &b2);
EXPECT_NEAR(r0, r1, 3);
EXPECT_NEAR(g0, g1, 3);
EXPECT_NEAR(b0, b1, 3);
EXPECT_NEAR(b0, b1, 5);
EXPECT_NEAR(r0, r2, 1);
EXPECT_NEAR(g0, g2, 1);
EXPECT_NEAR(b0, b2, 1);
TestYUVToRGBReference(i, 0, 0, r0, g0, b0);
TestYUVToRGB(i, 0, 0, r1, g1, b1, benchmark_width_, benchmark_height_);
TestYUVToRGBInt(i, 0, 0, r2, g2, b2);
YUVToRGBReference(i, 0, 0, &r0, &g0, &b0);
YUVToRGB(i, 0, 0, &r1, &g1, &b1);
YUVToRGBInt(i, 0, 0, &r2, &g2, &b2);
EXPECT_NEAR(r0, r1, 3);
EXPECT_NEAR(g0, g1, 3);
EXPECT_NEAR(b0, b1, 3);
EXPECT_NEAR(b0, b1, 5);
EXPECT_NEAR(r0, r2, 1);
EXPECT_NEAR(g0, g2, 1);
EXPECT_NEAR(b0, b2, 3);
TestYUVToRGBReference(i, 0, 255, r0, g0, b0);
TestYUVToRGB(i, 0, 255, r1, g1, b1, benchmark_width_, benchmark_height_);
TestYUVToRGBInt(i, 0, 255, r2, g2, b2);
YUVToRGBReference(i, 0, 255, &r0, &g0, &b0);
YUVToRGB(i, 0, 255, &r1, &g1, &b1);
YUVToRGBInt(i, 0, 255, &r2, &g2, &b2);
EXPECT_NEAR(r0, r1, 3);
EXPECT_NEAR(g0, g1, 3);
EXPECT_NEAR(b0, b1, 3);
EXPECT_NEAR(b0, b1, 5);
EXPECT_NEAR(r0, r2, 1);
EXPECT_NEAR(g0, g2, 1);
EXPECT_NEAR(b0, b2, 3);
......@@ -275,9 +279,46 @@ TEST_F(libyuvTest, TestYUV) {
int ur = random() & 255;
int vr = random() & 255;
TestYUVToRGBReference(yr, ur, vr, r0, g0, b0);
TestYUVToRGB(yr, ur, vr, r1, g1, b1, benchmark_width_, benchmark_height_);
TestYUVToRGBInt(yr, ur, vr, r2, g2, b2);
YUVToRGBReference(yr, ur, vr, &r0, &g0, &b0);
YUVToRGB(yr, ur, vr, &r1, &g1, &b1);
YUVToRGBInt(yr, ur, vr, &r2, &g2, &b2);
EXPECT_NEAR(r0, r1, 3);
EXPECT_NEAR(g0, g1, 3);
EXPECT_NEAR(b0, b1, 5);
EXPECT_NEAR(r0, r2, 1);
EXPECT_NEAR(g0, g2, 1);
EXPECT_NEAR(b0, b2, 3);
}
}
TEST_F(libyuvTest, TestGreyYUV) {
int r0, g0, b0;
int r1, g1, b1;
int r2, g2, b2;
for (int y = 0; y < 256; ++y) {
YUVToRGBReference(y, 128, 128, &r0, &g0, &b0);
YUVToRGB(y, 128, 128, &r1, &g1, &b1);
YUVToRGBInt(y, 128, 128, &r2, &g2, &b2);
EXPECT_NEAR(r0, r1, 3);
EXPECT_NEAR(g0, g1, 3);
EXPECT_NEAR(b0, b1, 5);
EXPECT_NEAR(r0, r2, 1);
EXPECT_NEAR(g0, g2, 1);
EXPECT_NEAR(b0, b2, 3);
}
}
// TODO(fbarchard): Speed up this test or disable it.
TEST_F(libyuvTest, TestFullYUV) {
int r0, g0, b0;
int r1, g1, b1;
int r2, g2, b2;
for (int y = 0; y < 256; ++y) {
for (int u = 0; u < 256; ++u) {
for (int v = 0; v < 256; ++v) {
YUVToRGBReference(y, u, v, &r0, &g0, &b0);
YUVToRGB(y, u, v, &r1, &g1, &b1);
YUVToRGBInt(y, u, v, &r2, &g2, &b2);
EXPECT_NEAR(r0, r1, 3);
EXPECT_NEAR(g0, g1, 3);
EXPECT_NEAR(b0, b1, 5);
......@@ -285,6 +326,8 @@ TEST_F(libyuvTest, TestYUV) {
EXPECT_NEAR(g0, g2, 1);
EXPECT_NEAR(b0, b2, 3);
}
}
}
}
} // namespace libyuv
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment