Commit 481b97a1 authored by Hans Verkuil's avatar Hans Verkuil Committed by Mauro Carvalho Chehab

[media] vivid-tpg: improve colorspace support

Add support for the new AdobeRGB and BT.2020 colorspaces. Also support
explicit Y'CbCr and quantization settings.
Signed-off-by: default avatarHans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@osg.samsung.com>
parent 3930e906
...@@ -296,127 +296,193 @@ static enum tpg_color tpg_get_textfg_color(struct tpg_data *tpg) ...@@ -296,127 +296,193 @@ static enum tpg_color tpg_get_textfg_color(struct tpg_data *tpg)
} }
} }
static u16 color_to_y(struct tpg_data *tpg, int r, int g, int b) static inline int rec709_to_linear(int v)
{ {
switch (tpg->colorspace) { v = clamp(v, 0, 0xff0);
case V4L2_COLORSPACE_SMPTE170M: return tpg_rec709_to_linear[v];
case V4L2_COLORSPACE_470_SYSTEM_M:
case V4L2_COLORSPACE_470_SYSTEM_BG:
return ((16829 * r + 33039 * g + 6416 * b + 16 * 32768) >> 16) + (16 << 4);
case V4L2_COLORSPACE_SMPTE240M:
return ((11932 * r + 39455 * g + 4897 * b + 16 * 32768) >> 16) + (16 << 4);
case V4L2_COLORSPACE_REC709:
case V4L2_COLORSPACE_SRGB:
default:
return ((11966 * r + 40254 * g + 4064 * b + 16 * 32768) >> 16) + (16 << 4);
}
} }
static u16 color_to_cb(struct tpg_data *tpg, int r, int g, int b) static inline int linear_to_rec709(int v)
{ {
switch (tpg->colorspace) { v = clamp(v, 0, 0xff0);
case V4L2_COLORSPACE_SMPTE170M: return tpg_linear_to_rec709[v];
case V4L2_COLORSPACE_470_SYSTEM_M:
case V4L2_COLORSPACE_470_SYSTEM_BG:
return ((-9714 * r - 19070 * g + 28784 * b + 16 * 32768) >> 16) + (128 << 4);
case V4L2_COLORSPACE_SMPTE240M:
return ((-6684 * r - 22100 * g + 28784 * b + 16 * 32768) >> 16) + (128 << 4);
case V4L2_COLORSPACE_REC709:
case V4L2_COLORSPACE_SRGB:
default:
return ((-6596 * r - 22189 * g + 28784 * b + 16 * 32768) >> 16) + (128 << 4);
}
} }
static u16 color_to_cr(struct tpg_data *tpg, int r, int g, int b) static void rgb2ycbcr(const int m[3][3], int r, int g, int b,
int y_offset, int *y, int *cb, int *cr)
{ {
switch (tpg->colorspace) { *y = ((m[0][0] * r + m[0][1] * g + m[0][2] * b) >> 16) + (y_offset << 4);
case V4L2_COLORSPACE_SMPTE170M: *cb = ((m[1][0] * r + m[1][1] * g + m[1][2] * b) >> 16) + (128 << 4);
case V4L2_COLORSPACE_470_SYSTEM_M: *cr = ((m[2][0] * r + m[2][1] * g + m[2][2] * b) >> 16) + (128 << 4);
case V4L2_COLORSPACE_470_SYSTEM_BG:
return ((28784 * r - 24103 * g - 4681 * b + 16 * 32768) >> 16) + (128 << 4);
case V4L2_COLORSPACE_SMPTE240M:
return ((28784 * r - 25606 * g - 3178 * b + 16 * 32768) >> 16) + (128 << 4);
case V4L2_COLORSPACE_REC709:
case V4L2_COLORSPACE_SRGB:
default:
return ((28784 * r - 26145 * g - 2639 * b + 16 * 32768) >> 16) + (128 << 4);
}
} }
static u16 ycbcr_to_r(struct tpg_data *tpg, int y, int cb, int cr) static void color_to_ycbcr(struct tpg_data *tpg, int r, int g, int b,
int *y, int *cb, int *cr)
{ {
int r; #define COEFF(v, r) ((int)(0.5 + (v) * (r) * 256.0))
y -= 16 << 4; static const int bt601[3][3] = {
cb -= 128 << 4; { COEFF(0.299, 219), COEFF(0.587, 219), COEFF(0.114, 219) },
cr -= 128 << 4; { COEFF(-0.169, 224), COEFF(-0.331, 224), COEFF(0.5, 224) },
switch (tpg->colorspace) { { COEFF(0.5, 224), COEFF(-0.419, 224), COEFF(-0.081, 224) },
case V4L2_COLORSPACE_SMPTE170M: };
case V4L2_COLORSPACE_470_SYSTEM_M: static const int bt601_full[3][3] = {
case V4L2_COLORSPACE_470_SYSTEM_BG: { COEFF(0.299, 255), COEFF(0.587, 255), COEFF(0.114, 255) },
r = 4769 * y + 6537 * cr; { COEFF(-0.169, 255), COEFF(-0.331, 255), COEFF(0.5, 255) },
{ COEFF(0.5, 255), COEFF(-0.419, 255), COEFF(-0.081, 255) },
};
static const int rec709[3][3] = {
{ COEFF(0.2126, 219), COEFF(0.7152, 219), COEFF(0.0722, 219) },
{ COEFF(-0.1146, 224), COEFF(-0.3854, 224), COEFF(0.5, 224) },
{ COEFF(0.5, 224), COEFF(-0.4542, 224), COEFF(-0.0458, 224) },
};
static const int rec709_full[3][3] = {
{ COEFF(0.2126, 255), COEFF(0.7152, 255), COEFF(0.0722, 255) },
{ COEFF(-0.1146, 255), COEFF(-0.3854, 255), COEFF(0.5, 255) },
{ COEFF(0.5, 255), COEFF(-0.4542, 255), COEFF(-0.0458, 255) },
};
static const int smpte240m[3][3] = {
{ COEFF(0.212, 219), COEFF(0.701, 219), COEFF(0.087, 219) },
{ COEFF(-0.116, 224), COEFF(-0.384, 224), COEFF(0.5, 224) },
{ COEFF(0.5, 224), COEFF(-0.445, 224), COEFF(-0.055, 224) },
};
static const int bt2020[3][3] = {
{ COEFF(0.2726, 219), COEFF(0.6780, 219), COEFF(0.0593, 219) },
{ COEFF(-0.1396, 224), COEFF(-0.3604, 224), COEFF(0.5, 224) },
{ COEFF(0.5, 224), COEFF(-0.4629, 224), COEFF(-0.0405, 224) },
};
bool full = tpg->real_quantization == V4L2_QUANTIZATION_FULL_RANGE;
int lin_y, yc;
switch (tpg->real_ycbcr_enc) {
case V4L2_YCBCR_ENC_601:
case V4L2_YCBCR_ENC_XV601:
case V4L2_YCBCR_ENC_SYCC:
rgb2ycbcr(full ? bt601_full : bt601, r, g, b, 16, y, cb, cr);
break; break;
case V4L2_COLORSPACE_SMPTE240M: case V4L2_YCBCR_ENC_BT2020:
r = 4769 * y + 7376 * cr; rgb2ycbcr(bt2020, r, g, b, 16, y, cb, cr);
break; break;
case V4L2_COLORSPACE_REC709: case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
case V4L2_COLORSPACE_SRGB: lin_y = (COEFF(0.2627, 255) * rec709_to_linear(r) +
COEFF(0.6780, 255) * rec709_to_linear(g) +
COEFF(0.0593, 255) * rec709_to_linear(b)) >> 16;
yc = linear_to_rec709(lin_y);
*y = (yc * 219) / 255 + (16 << 4);
if (b <= yc)
*cb = (((b - yc) * COEFF(1.0 / 1.9404, 224)) >> 16) + (128 << 4);
else
*cb = (((b - yc) * COEFF(1.0 / 1.5816, 224)) >> 16) + (128 << 4);
if (r <= yc)
*cr = (((r - yc) * COEFF(1.0 / 1.7184, 224)) >> 16) + (128 << 4);
else
*cr = (((r - yc) * COEFF(1.0 / 0.9936, 224)) >> 16) + (128 << 4);
break;
case V4L2_YCBCR_ENC_SMPTE240M:
rgb2ycbcr(smpte240m, r, g, b, 16, y, cb, cr);
break;
case V4L2_YCBCR_ENC_709:
case V4L2_YCBCR_ENC_XV709:
default: default:
r = 4769 * y + 7343 * cr; rgb2ycbcr(full ? rec709_full : rec709, r, g, b, 0, y, cb, cr);
break; break;
} }
return clamp(r >> 12, 0, 0xff0);
} }
static u16 ycbcr_to_g(struct tpg_data *tpg, int y, int cb, int cr) static void ycbcr2rgb(const int m[3][3], int y, int cb, int cr,
int y_offset, int *r, int *g, int *b)
{ {
int g; y -= y_offset << 4;
y -= 16 << 4;
cb -= 128 << 4; cb -= 128 << 4;
cr -= 128 << 4; cr -= 128 << 4;
switch (tpg->colorspace) { *r = m[0][0] * y + m[0][1] * cb + m[0][2] * cr;
case V4L2_COLORSPACE_SMPTE170M: *g = m[1][0] * y + m[1][1] * cb + m[1][2] * cr;
case V4L2_COLORSPACE_470_SYSTEM_M: *b = m[2][0] * y + m[2][1] * cb + m[2][2] * cr;
case V4L2_COLORSPACE_470_SYSTEM_BG: *r = clamp(*r >> 12, 0, 0xff0);
g = 4769 * y - 1605 * cb - 3330 * cr; *g = clamp(*g >> 12, 0, 0xff0);
break; *b = clamp(*b >> 12, 0, 0xff0);
case V4L2_COLORSPACE_SMPTE240M:
g = 4769 * y - 1055 * cb - 2341 * cr;
break;
case V4L2_COLORSPACE_REC709:
case V4L2_COLORSPACE_SRGB:
default:
g = 4769 * y - 873 * cb - 2183 * cr;
break;
}
return clamp(g >> 12, 0, 0xff0);
} }
static u16 ycbcr_to_b(struct tpg_data *tpg, int y, int cb, int cr) static void ycbcr_to_color(struct tpg_data *tpg, int y, int cb, int cr,
int *r, int *g, int *b)
{ {
int b; #undef COEFF
#define COEFF(v, r) ((int)(0.5 + (v) * ((255.0 * 255.0 * 16.0) / (r))))
static const int bt601[3][3] = {
{ COEFF(1, 219), COEFF(0, 224), COEFF(1.4020, 224) },
{ COEFF(1, 219), COEFF(-0.3441, 224), COEFF(-0.7141, 224) },
{ COEFF(1, 219), COEFF(1.7720, 224), COEFF(0, 224) },
};
static const int bt601_full[3][3] = {
{ COEFF(1, 255), COEFF(0, 255), COEFF(1.4020, 255) },
{ COEFF(1, 255), COEFF(-0.3441, 255), COEFF(-0.7141, 255) },
{ COEFF(1, 255), COEFF(1.7720, 255), COEFF(0, 255) },
};
static const int rec709[3][3] = {
{ COEFF(1, 219), COEFF(0, 224), COEFF(1.5748, 224) },
{ COEFF(1, 219), COEFF(-0.1873, 224), COEFF(-0.4681, 224) },
{ COEFF(1, 219), COEFF(1.8556, 224), COEFF(0, 224) },
};
static const int rec709_full[3][3] = {
{ COEFF(1, 255), COEFF(0, 255), COEFF(1.5748, 255) },
{ COEFF(1, 255), COEFF(-0.1873, 255), COEFF(-0.4681, 255) },
{ COEFF(1, 255), COEFF(1.8556, 255), COEFF(0, 255) },
};
static const int smpte240m[3][3] = {
{ COEFF(1, 219), COEFF(0, 224), COEFF(1.5756, 224) },
{ COEFF(1, 219), COEFF(-0.2253, 224), COEFF(-0.4767, 224) },
{ COEFF(1, 219), COEFF(1.8270, 224), COEFF(0, 224) },
};
static const int bt2020[3][3] = {
{ COEFF(1, 219), COEFF(0, 224), COEFF(1.4746, 224) },
{ COEFF(1, 219), COEFF(-0.1646, 224), COEFF(-0.5714, 224) },
{ COEFF(1, 219), COEFF(1.8814, 224), COEFF(0, 224) },
};
bool full = tpg->real_quantization == V4L2_QUANTIZATION_FULL_RANGE;
int lin_r, lin_g, lin_b, lin_y;
switch (tpg->real_ycbcr_enc) {
case V4L2_YCBCR_ENC_601:
case V4L2_YCBCR_ENC_XV601:
case V4L2_YCBCR_ENC_SYCC:
ycbcr2rgb(full ? bt601_full : bt601, y, cb, cr, 16, r, g, b);
break;
case V4L2_YCBCR_ENC_BT2020:
ycbcr2rgb(bt2020, y, cb, cr, 16, r, g, b);
break;
case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
y -= 16 << 4; y -= 16 << 4;
cb -= 128 << 4; cb -= 128 << 4;
cr -= 128 << 4; cr -= 128 << 4;
switch (tpg->colorspace) {
case V4L2_COLORSPACE_SMPTE170M: if (cb <= 0)
case V4L2_COLORSPACE_470_SYSTEM_M: *b = COEFF(1.0, 219) * y + COEFF(1.9404, 224) * cb;
case V4L2_COLORSPACE_470_SYSTEM_BG: else
b = 4769 * y + 7343 * cb; *b = COEFF(1.0, 219) * y + COEFF(1.5816, 224) * cb;
*b = *b >> 12;
if (cr <= 0)
*r = COEFF(1.0, 219) * y + COEFF(1.7184, 224) * cr;
else
*r = COEFF(1.0, 219) * y + COEFF(0.9936, 224) * cr;
*r = *r >> 12;
lin_r = rec709_to_linear(*r);
lin_b = rec709_to_linear(*b);
lin_y = rec709_to_linear((y * 255) / 219);
lin_g = COEFF(1.0 / 0.6780, 255) * lin_y -
COEFF(0.2627 / 0.6780, 255) * lin_r -
COEFF(0.0593 / 0.6780, 255) * lin_b;
*g = linear_to_rec709(lin_g >> 12);
break; break;
case V4L2_COLORSPACE_SMPTE240M: case V4L2_YCBCR_ENC_SMPTE240M:
b = 4769 * y + 8552 * cb; ycbcr2rgb(smpte240m, y, cb, cr, 16, r, g, b);
break; break;
case V4L2_COLORSPACE_REC709: case V4L2_YCBCR_ENC_709:
case V4L2_COLORSPACE_SRGB: case V4L2_YCBCR_ENC_XV709:
default: default:
b = 4769 * y + 8652 * cb; ycbcr2rgb(full ? rec709_full : rec709, y, cb, cr, 16, r, g, b);
break; break;
} }
return clamp(b >> 12, 0, 0xff0);
} }
/* precalculate color bar values to speed up rendering */ /* precalculate color bar values to speed up rendering */
...@@ -456,18 +522,17 @@ static void precalculate_color(struct tpg_data *tpg, int k) ...@@ -456,18 +522,17 @@ static void precalculate_color(struct tpg_data *tpg, int k)
g <<= 4; g <<= 4;
b <<= 4; b <<= 4;
} }
if (tpg->qual == TPG_QUAL_GRAY) if (tpg->qual == TPG_QUAL_GRAY) {
r = g = b = color_to_y(tpg, r, g, b); /* Rec. 709 Luma function */
/* (0.2126, 0.7152, 0.0722) * (255 * 256) */
r = g = b = ((13879 * r + 46688 * g + 4713 * b) >> 16) + (16 << 4);
}
/* /*
* The assumption is that the RGB output is always full range, * The assumption is that the RGB output is always full range,
* so only if the rgb_range overrides the 'real' rgb range do * so only if the rgb_range overrides the 'real' rgb range do
* we need to convert the RGB values. * we need to convert the RGB values.
* *
* Currently there is no way of signalling to userspace if you
* are actually giving it limited range RGB (or full range
* YUV for that matter).
*
* Remember that r, g and b are still in the 0 - 0xff0 range. * Remember that r, g and b are still in the 0 - 0xff0 range.
*/ */
if (tpg->real_rgb_range == V4L2_DV_RGB_RANGE_LIMITED && if (tpg->real_rgb_range == V4L2_DV_RGB_RANGE_LIMITED &&
...@@ -497,12 +562,12 @@ static void precalculate_color(struct tpg_data *tpg, int k) ...@@ -497,12 +562,12 @@ static void precalculate_color(struct tpg_data *tpg, int k)
if (tpg->brightness != 128 || tpg->contrast != 128 || if (tpg->brightness != 128 || tpg->contrast != 128 ||
tpg->saturation != 128 || tpg->hue) { tpg->saturation != 128 || tpg->hue) {
/* Implement these operations */ /* Implement these operations */
int y, cb, cr;
int tmp_cb, tmp_cr;
/* First convert to YCbCr */ /* First convert to YCbCr */
int y = color_to_y(tpg, r, g, b); /* Luma */
int cb = color_to_cb(tpg, r, g, b); /* Cb */ color_to_ycbcr(tpg, r, g, b, &y, &cb, &cr);
int cr = color_to_cr(tpg, r, g, b); /* Cr */
int tmp_cb, tmp_cr;
y = (16 << 4) + ((y - (16 << 4)) * tpg->contrast) / 128; y = (16 << 4) + ((y - (16 << 4)) * tpg->contrast) / 128;
y += (tpg->brightness << 4) - (128 << 4); y += (tpg->brightness << 4) - (128 << 4);
...@@ -520,21 +585,29 @@ static void precalculate_color(struct tpg_data *tpg, int k) ...@@ -520,21 +585,29 @@ static void precalculate_color(struct tpg_data *tpg, int k)
tpg->colors[k][2] = clamp(cr >> 4, 1, 254); tpg->colors[k][2] = clamp(cr >> 4, 1, 254);
return; return;
} }
r = ycbcr_to_r(tpg, y, cb, cr); ycbcr_to_color(tpg, y, cb, cr, &r, &g, &b);
g = ycbcr_to_g(tpg, y, cb, cr);
b = ycbcr_to_b(tpg, y, cb, cr);
} }
if (tpg->is_yuv) { if (tpg->is_yuv) {
/* Convert to YCbCr */ /* Convert to YCbCr */
u16 y = color_to_y(tpg, r, g, b); /* Luma */ int y, cb, cr;
u16 cb = color_to_cb(tpg, r, g, b); /* Cb */
u16 cr = color_to_cr(tpg, r, g, b); /* Cr */
color_to_ycbcr(tpg, r, g, b, &y, &cb, &cr);
if (tpg->real_quantization == V4L2_QUANTIZATION_LIM_RANGE) {
y = clamp(y, 16 << 4, 235 << 4);
cb = clamp(cb, 16 << 4, 240 << 4);
cr = clamp(cr, 16 << 4, 240 << 4);
}
tpg->colors[k][0] = clamp(y >> 4, 1, 254); tpg->colors[k][0] = clamp(y >> 4, 1, 254);
tpg->colors[k][1] = clamp(cb >> 4, 1, 254); tpg->colors[k][1] = clamp(cb >> 4, 1, 254);
tpg->colors[k][2] = clamp(cr >> 4, 1, 254); tpg->colors[k][2] = clamp(cr >> 4, 1, 254);
} else { } else {
if (tpg->real_quantization == V4L2_QUANTIZATION_LIM_RANGE) {
r = (r * 219) / 255 + (16 << 4);
g = (g * 219) / 255 + (16 << 4);
b = (b * 219) / 255 + (16 << 4);
}
switch (tpg->fourcc) { switch (tpg->fourcc) {
case V4L2_PIX_FMT_RGB565: case V4L2_PIX_FMT_RGB565:
case V4L2_PIX_FMT_RGB565X: case V4L2_PIX_FMT_RGB565X:
...@@ -1152,6 +1225,46 @@ static void tpg_recalc(struct tpg_data *tpg) ...@@ -1152,6 +1225,46 @@ static void tpg_recalc(struct tpg_data *tpg)
if (tpg->recalc_colors) { if (tpg->recalc_colors) {
tpg->recalc_colors = false; tpg->recalc_colors = false;
tpg->recalc_lines = true; tpg->recalc_lines = true;
tpg->real_ycbcr_enc = tpg->ycbcr_enc;
tpg->real_quantization = tpg->quantization;
if (tpg->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) {
switch (tpg->colorspace) {
case V4L2_COLORSPACE_REC709:
tpg->real_ycbcr_enc = V4L2_YCBCR_ENC_709;
break;
case V4L2_COLORSPACE_SRGB:
tpg->real_ycbcr_enc = V4L2_YCBCR_ENC_SYCC;
break;
case V4L2_COLORSPACE_BT2020:
tpg->real_ycbcr_enc = V4L2_YCBCR_ENC_BT2020;
break;
case V4L2_COLORSPACE_SMPTE240M:
tpg->real_ycbcr_enc = V4L2_YCBCR_ENC_SMPTE240M;
break;
case V4L2_COLORSPACE_SMPTE170M:
case V4L2_COLORSPACE_470_SYSTEM_M:
case V4L2_COLORSPACE_470_SYSTEM_BG:
case V4L2_COLORSPACE_ADOBERGB:
default:
tpg->real_ycbcr_enc = V4L2_YCBCR_ENC_601;
break;
}
}
if (tpg->quantization == V4L2_QUANTIZATION_DEFAULT) {
tpg->real_quantization = V4L2_QUANTIZATION_FULL_RANGE;
if (tpg->is_yuv) {
switch (tpg->real_ycbcr_enc) {
case V4L2_YCBCR_ENC_SYCC:
case V4L2_YCBCR_ENC_XV601:
case V4L2_YCBCR_ENC_XV709:
break;
default:
tpg->real_quantization =
V4L2_QUANTIZATION_LIM_RANGE;
break;
}
}
}
tpg_precalculate_colors(tpg); tpg_precalculate_colors(tpg);
} }
if (tpg->recalc_square_border) { if (tpg->recalc_square_border) {
......
...@@ -119,6 +119,18 @@ struct tpg_data { ...@@ -119,6 +119,18 @@ struct tpg_data {
u32 fourcc; u32 fourcc;
bool is_yuv; bool is_yuv;
u32 colorspace; u32 colorspace;
u32 ycbcr_enc;
/*
* Stores the actual Y'CbCr encoding, i.e. will never be
* V4L2_YCBCR_ENC_DEFAULT.
*/
u32 real_ycbcr_enc;
u32 quantization;
/*
* Stores the actual quantization, i.e. will never be
* V4L2_QUANTIZATION_DEFAULT.
*/
u32 real_quantization;
enum tpg_video_aspect vid_aspect; enum tpg_video_aspect vid_aspect;
enum tpg_pixel_aspect pix_aspect; enum tpg_pixel_aspect pix_aspect;
unsigned rgb_range; unsigned rgb_range;
...@@ -286,6 +298,32 @@ static inline u32 tpg_g_colorspace(const struct tpg_data *tpg) ...@@ -286,6 +298,32 @@ static inline u32 tpg_g_colorspace(const struct tpg_data *tpg)
return tpg->colorspace; return tpg->colorspace;
} }
static inline void tpg_s_ycbcr_enc(struct tpg_data *tpg, u32 ycbcr_enc)
{
if (tpg->ycbcr_enc == ycbcr_enc)
return;
tpg->ycbcr_enc = ycbcr_enc;
tpg->recalc_colors = true;
}
static inline u32 tpg_g_ycbcr_enc(const struct tpg_data *tpg)
{
return tpg->ycbcr_enc;
}
static inline void tpg_s_quantization(struct tpg_data *tpg, u32 quantization)
{
if (tpg->quantization == quantization)
return;
tpg->quantization = quantization;
tpg->recalc_colors = true;
}
static inline u32 tpg_g_quantization(const struct tpg_data *tpg)
{
return tpg->quantization;
}
static inline unsigned tpg_g_planes(const struct tpg_data *tpg) static inline unsigned tpg_g_planes(const struct tpg_data *tpg)
{ {
return tpg->planes; return tpg->planes;
......
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