From 7adbef59143af49fb153812d1d0368522e70dcaf Mon Sep 17 00:00:00 2001
From: Jyri Sarha <jsarha@ti.com>
Date: Tue, 9 May 2017 17:37:15 +0300
Subject: Add different YCbCr encodings.

---
 kms++util/src/color.cpp   | 74 +++++++++++++++++++++++++++++++++++++----------
 kms++util/src/testpat.cpp | 18 ++++++------
 2 files changed, 68 insertions(+), 24 deletions(-)

(limited to 'kms++util/src')

diff --git a/kms++util/src/color.cpp b/kms++util/src/color.cpp
index ae8a4b4..2e6f217 100644
--- a/kms++util/src/color.cpp
+++ b/kms++util/src/color.cpp
@@ -59,11 +59,40 @@ uint16_t RGB::bgr565() const
 	return ((b >> 3) << 11) | ((g >> 2) << 5) | ((r >> 3) << 0);
 }
 
-YUV RGB::yuv() const
-{
-	return YUV(*this);
-}
-
+YUV RGB::yuv(YUVType type) const
+{
+	return YUV(*this, type);
+}
+
+#define CF_ONE (256)
+#define CF(a, b, c) { ((int) ((a) * CF_ONE)), ((int) ((b) * CF_ONE)), ((int) ((c) * CF_ONE)) }
+#define CLAMP(a) ((a) > (CF_ONE-1) ? (CF_ONE-1) : (a) < 0 ? 0 : (a))
+
+const int YUVcoef[static_cast<unsigned>(YUVType::MAX)][3][3] = {
+	[static_cast<unsigned>(YUVType::BT601_Lim)] = {
+		CF( 0.257,  0.504,  0.098),
+		CF(-0.148, -0.291,  0.439),
+		CF( 0.439, -0.368, -0.071) },
+	[static_cast<unsigned>(YUVType::BT601_Full)] = {
+		CF( 0.299,  0.587,  0.114),
+		CF(-0.169, -0.331,  0.500),
+		CF( 0.500, -0.419, -0.081) },
+	[static_cast<unsigned>(YUVType::BT709_Lim)] = {
+		CF( 0.1826,  0.6142,  0.0620),
+		CF(-0.1006, -0.3386,  0.4392),
+		CF( 0.4392, -0.3989, -0.0403) },
+	[static_cast<unsigned>(YUVType::BT709_Full)] = {
+		CF( 0.2126,  0.7152,  0.0722),
+		CF(-0.1146, -0.3854,  0.5000),
+		CF( 0.5000, -0.4542, -0.0468) },
+};
+
+const int YUVoffset[static_cast<unsigned>(YUVType::MAX)][3] = {
+	[static_cast<unsigned>(YUVType::BT601_Lim)]  = CF(0.0625,  0.5,  0.5),
+	[static_cast<unsigned>(YUVType::BT601_Full)] = CF(     0,  0.5,  0.5),
+	[static_cast<unsigned>(YUVType::BT709_Lim)]  = CF(0.0625,  0.5,  0.5),
+	[static_cast<unsigned>(YUVType::BT709_Full)] = CF(     0,  0.5,  0.5),
+};
 
 YUV::YUV()
 {
@@ -78,26 +107,41 @@ YUV::YUV(uint8_t y, uint8_t u, uint8_t v)
 	this->a = 0;
 }
 
-static inline uint8_t MAKE_YUV_601_Y(uint8_t r, uint8_t g, uint8_t b)
+static inline
+uint8_t MAKE_YUV_Y(uint8_t r, uint8_t g, uint8_t b, YUVType type)
 {
-	return (((66 * r + 129 * g +  25 * b + 128) >> 8) + 16);
+	unsigned tidx = static_cast<unsigned>(type);
+
+	return CLAMP(((YUVcoef[tidx][0][0] * r + YUVcoef[tidx][0][1] * g +
+		      YUVcoef[tidx][0][2] * b + CF_ONE/2) / CF_ONE) +
+		     YUVoffset[tidx][0]);
 }
 
-static inline uint8_t MAKE_YUV_601_U(uint8_t r, uint8_t g, uint8_t b)
+static inline
+uint8_t MAKE_YUV_U(uint8_t r, uint8_t g, uint8_t b, YUVType type)
 {
-	return (((-38 * r -  74 * g + 112 * b + 128) >> 8) + 128);
+	unsigned tidx = static_cast<unsigned>(type);
+
+	return CLAMP(((YUVcoef[tidx][1][0] * r + YUVcoef[tidx][1][1] * g +
+		       YUVcoef[tidx][1][2] * b + CF_ONE/2) / CF_ONE) +
+		     YUVoffset[tidx][1]);
 }
 
-static inline uint8_t MAKE_YUV_601_V(uint8_t r, uint8_t g, uint8_t b)
+static inline
+uint8_t MAKE_YUV_V(uint8_t r, uint8_t g, uint8_t b, YUVType type)
 {
-	return (((112 * r -  94 * g -  18 * b + 128) >> 8) + 128);
+	unsigned tidx = static_cast<unsigned>(type);
+
+	return CLAMP(((YUVcoef[tidx][2][0] * r + YUVcoef[tidx][2][1] * g +
+		       YUVcoef[tidx][2][2] * b + CF_ONE/2) / CF_ONE) +
+		     YUVoffset[tidx][2]);
 }
 
-YUV::YUV(const RGB& rgb)
+YUV::YUV(const RGB& rgb, YUVType type)
 {
-	this->y = MAKE_YUV_601_Y(rgb.r, rgb.g, rgb.b);
-	this->u = MAKE_YUV_601_U(rgb.r, rgb.g, rgb.b);
-	this->v = MAKE_YUV_601_V(rgb.r, rgb.g, rgb.b);
+	this->y = MAKE_YUV_Y(rgb.r, rgb.g, rgb.b, type);
+	this->u = MAKE_YUV_U(rgb.r, rgb.g, rgb.b, type);
+	this->v = MAKE_YUV_V(rgb.r, rgb.g, rgb.b, type);
 	this->a = rgb.a;
 }
 }
diff --git a/kms++util/src/testpat.cpp b/kms++util/src/testpat.cpp
index fbe3b2e..dbd8fe6 100644
--- a/kms++util/src/testpat.cpp
+++ b/kms++util/src/testpat.cpp
@@ -97,7 +97,7 @@ static RGB get_test_pattern_pixel(IMappedFramebuffer& fb, unsigned x, unsigned y
 	}
 }
 
-static void draw_test_pattern_part(IMappedFramebuffer& fb, unsigned start_y, unsigned end_y)
+static void draw_test_pattern_part(IMappedFramebuffer& fb, unsigned start_y, unsigned end_y, YUVType yuvt)
 {
 	unsigned x, y;
 	unsigned w = fb.width();
@@ -127,7 +127,7 @@ static void draw_test_pattern_part(IMappedFramebuffer& fb, unsigned start_y, uns
 			for (x = 0; x < w; x += 2) {
 				RGB pixel1 = get_test_pattern_pixel(fb, x, y);
 				RGB pixel2 = get_test_pattern_pixel(fb, x + 1, y);
-				draw_yuv422_macropixel(fb, x, y, pixel1.yuv(), pixel2.yuv());
+				draw_yuv422_macropixel(fb, x, y, pixel1.yuv(yuvt), pixel2.yuv(yuvt));
 			}
 		}
 		break;
@@ -141,8 +141,8 @@ static void draw_test_pattern_part(IMappedFramebuffer& fb, unsigned start_y, uns
 				RGB pixel01 = get_test_pattern_pixel(fb, x, y + 1);
 				RGB pixel11 = get_test_pattern_pixel(fb, x + 1, y + 1);
 				draw_yuv420_macropixel(fb, x, y,
-						       pixel00.yuv(), pixel10.yuv(),
-						       pixel01.yuv(), pixel11.yuv());
+						       pixel00.yuv(yuvt), pixel10.yuv(yuvt),
+						       pixel01.yuv(yuvt), pixel11.yuv(yuvt));
 			}
 		}
 		break;
@@ -151,10 +151,10 @@ static void draw_test_pattern_part(IMappedFramebuffer& fb, unsigned start_y, uns
 	}
 }
 
-static void draw_test_pattern_impl(IMappedFramebuffer& fb)
+static void draw_test_pattern_impl(IMappedFramebuffer& fb, YUVType yuvt)
 {
 	if (fb.height() < 20) {
-		draw_test_pattern_part(fb, 0, fb.height());
+		draw_test_pattern_part(fb, 0, fb.height(), yuvt);
 		return;
 	}
 
@@ -174,21 +174,21 @@ static void draw_test_pattern_impl(IMappedFramebuffer& fb)
 		if (n == num_threads - 1)
 			end = fb.height();
 
-		workers.push_back(thread([&fb, start, end]() { draw_test_pattern_part(fb, start, end); }));
+		workers.push_back(thread([&fb, start, end, yuvt]() { draw_test_pattern_part(fb, start, end, yuvt); }));
 	}
 
 	for (thread& t : workers)
 		t.join();
 }
 
-void draw_test_pattern(IMappedFramebuffer &fb)
+void draw_test_pattern(IMappedFramebuffer &fb, YUVType yuvt)
 {
 #ifdef DRAW_PERF_PRINT
 	Stopwatch sw;
 	sw.start();
 #endif
 
-	draw_test_pattern_impl(fb);
+	draw_test_pattern_impl(fb, yuvt);
 
 #ifdef DRAW_PERF_PRINT
 	double us = sw.elapsed_us();
-- 
cgit v1.2.3