mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-04-28 10:14:14 +00:00
testyuv: added validation of P010 YUV format
Also added conversion between RGB and P010, using XRGB2101010 as a bridging format in PQ space
This commit is contained in:
@@ -15,8 +15,8 @@
|
||||
#include "testyuv_cvt.h"
|
||||
#include "testutils.h"
|
||||
|
||||
/* 422 (YUY2, etc) formats are the largest */
|
||||
#define MAX_YUV_SURFACE_SIZE(W, H, P) (H * 4 * (W + P + 1) / 2)
|
||||
/* 422 (YUY2, etc) and P010 formats are the largest */
|
||||
#define MAX_YUV_SURFACE_SIZE(W, H, P) ((H + 1) * ((W + 1) + P) * 4)
|
||||
|
||||
/* Return true if the YUV format is packed pixels */
|
||||
static SDL_bool is_packed_yuv_format(Uint32 format)
|
||||
@@ -65,9 +65,8 @@ static SDL_Surface *generate_test_pattern(int pattern_size)
|
||||
return pattern;
|
||||
}
|
||||
|
||||
static SDL_bool verify_yuv_data(Uint32 format, SDL_Colorspace colorspace, const Uint8 *yuv, int yuv_pitch, SDL_Surface *surface)
|
||||
static SDL_bool verify_yuv_data(Uint32 format, SDL_Colorspace colorspace, SDL_PropertiesID props, const Uint8 *yuv, int yuv_pitch, SDL_Surface *surface, int tolerance)
|
||||
{
|
||||
const int tolerance = 20;
|
||||
const int size = (surface->h * surface->pitch);
|
||||
Uint8 *rgb;
|
||||
SDL_bool result = SDL_FALSE;
|
||||
@@ -78,7 +77,7 @@ static SDL_bool verify_yuv_data(Uint32 format, SDL_Colorspace colorspace, const
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
if (SDL_ConvertPixelsAndColorspace(surface->w, surface->h, format, colorspace, 0, yuv, yuv_pitch, surface->format->format, SDL_COLORSPACE_SRGB, 0, rgb, surface->pitch) == 0) {
|
||||
if (SDL_ConvertPixelsAndColorspace(surface->w, surface->h, format, colorspace, props, yuv, yuv_pitch, surface->format->format, SDL_COLORSPACE_SRGB, 0, rgb, surface->pitch) == 0) {
|
||||
int x, y;
|
||||
result = SDL_TRUE;
|
||||
for (y = 0; y < surface->h; ++y) {
|
||||
@@ -124,6 +123,9 @@ static int run_automated_tests(int pattern_size, int extra_pitch)
|
||||
int yuv1_pitch, yuv2_pitch;
|
||||
YUV_CONVERSION_MODE mode;
|
||||
SDL_Colorspace colorspace;
|
||||
SDL_PropertiesID props;
|
||||
const int tight_tolerance = 20;
|
||||
const int loose_tolerance = 333;
|
||||
int result = -1;
|
||||
|
||||
if (!pattern || !yuv1 || !yuv2) {
|
||||
@@ -134,6 +136,10 @@ static int run_automated_tests(int pattern_size, int extra_pitch)
|
||||
mode = GetYUVConversionModeForResolution(pattern->w, pattern->h);
|
||||
colorspace = GetColorspaceForYUVConversionMode(mode);
|
||||
|
||||
/* All tests are being done with SDR content */
|
||||
props = SDL_CreateProperties();
|
||||
SDL_SetFloatProperty(props, SDL_PROP_SURFACE_HDR_HEADROOM_FLOAT, 1.0f);
|
||||
|
||||
/* Verify conversion from YUV formats */
|
||||
for (i = 0; i < SDL_arraysize(formats); ++i) {
|
||||
if (!ConvertRGBtoYUV(formats[i], pattern->pixels, pattern->pitch, yuv1, pattern->w, pattern->h, mode, 0, 100)) {
|
||||
@@ -141,7 +147,7 @@ static int run_automated_tests(int pattern_size, int extra_pitch)
|
||||
goto done;
|
||||
}
|
||||
yuv1_pitch = CalculateYUVPitch(formats[i], pattern->w);
|
||||
if (!verify_yuv_data(formats[i], colorspace, yuv1, yuv1_pitch, pattern)) {
|
||||
if (!verify_yuv_data(formats[i], colorspace, props, yuv1, yuv1_pitch, pattern, tight_tolerance)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed conversion from %s to RGB\n", SDL_GetPixelFormatName(formats[i]));
|
||||
goto done;
|
||||
}
|
||||
@@ -154,7 +160,7 @@ static int run_automated_tests(int pattern_size, int extra_pitch)
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(pattern->format->format), SDL_GetPixelFormatName(formats[i]), SDL_GetError());
|
||||
goto done;
|
||||
}
|
||||
if (!verify_yuv_data(formats[i], colorspace, yuv1, yuv1_pitch, pattern)) {
|
||||
if (!verify_yuv_data(formats[i], colorspace, props, yuv1, yuv1_pitch, pattern, tight_tolerance)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed conversion from RGB to %s\n", SDL_GetPixelFormatName(formats[i]));
|
||||
goto done;
|
||||
}
|
||||
@@ -173,7 +179,7 @@ static int run_automated_tests(int pattern_size, int extra_pitch)
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(formats[i]), SDL_GetPixelFormatName(formats[j]), SDL_GetError());
|
||||
goto done;
|
||||
}
|
||||
if (!verify_yuv_data(formats[j], colorspace, yuv2, yuv2_pitch, pattern)) {
|
||||
if (!verify_yuv_data(formats[j], colorspace, props, yuv2, yuv2_pitch, pattern, tight_tolerance)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed conversion from %s to %s\n", SDL_GetPixelFormatName(formats[i]), SDL_GetPixelFormatName(formats[j]));
|
||||
goto done;
|
||||
}
|
||||
@@ -198,13 +204,37 @@ static int run_automated_tests(int pattern_size, int extra_pitch)
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(formats[i]), SDL_GetPixelFormatName(formats[j]), SDL_GetError());
|
||||
goto done;
|
||||
}
|
||||
if (!verify_yuv_data(formats[j], colorspace, yuv1, yuv2_pitch, pattern)) {
|
||||
if (!verify_yuv_data(formats[j], colorspace, props, yuv1, yuv2_pitch, pattern, tight_tolerance)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed conversion from %s to %s\n", SDL_GetPixelFormatName(formats[i]), SDL_GetPixelFormatName(formats[j]));
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Verify round trip through BT.2020 */
|
||||
colorspace = SDL_COLORSPACE_BT2020_FULL;
|
||||
if (!ConvertRGBtoYUV(SDL_PIXELFORMAT_P010, pattern->pixels, pattern->pitch, yuv1, pattern->w, pattern->h, YUV_CONVERSION_BT2020, 0, 100)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "ConvertRGBtoYUV() doesn't support converting to %s\n", SDL_GetPixelFormatName(SDL_PIXELFORMAT_P010));
|
||||
goto done;
|
||||
}
|
||||
yuv1_pitch = CalculateYUVPitch(SDL_PIXELFORMAT_P010, pattern->w);
|
||||
if (!verify_yuv_data(SDL_PIXELFORMAT_P010, colorspace, props, yuv1, yuv1_pitch, pattern, tight_tolerance)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed conversion from %s to RGB\n", SDL_GetPixelFormatName(SDL_PIXELFORMAT_P010));
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* The pitch needs to be Uint16 aligned for P010 pixels */
|
||||
yuv1_pitch = CalculateYUVPitch(SDL_PIXELFORMAT_P010, pattern->w) + ((extra_pitch + 1) & ~1);
|
||||
if (SDL_ConvertPixelsAndColorspace(pattern->w, pattern->h, pattern->format->format, SDL_COLORSPACE_SRGB, 0, pattern->pixels, pattern->pitch, SDL_PIXELFORMAT_P010, colorspace, props, yuv1, yuv1_pitch) < 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(pattern->format->format), SDL_GetPixelFormatName(SDL_PIXELFORMAT_P010), SDL_GetError());
|
||||
goto done;
|
||||
}
|
||||
/* Going through XRGB2101010 format during P010 conversion is slightly lossy, so use looser tolerance here */
|
||||
if (!verify_yuv_data(SDL_PIXELFORMAT_P010, colorspace, props, yuv1, yuv1_pitch, pattern, loose_tolerance)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed conversion from RGB to %s\n", SDL_GetPixelFormatName(SDL_PIXELFORMAT_P010));
|
||||
goto done;
|
||||
}
|
||||
|
||||
result = 0;
|
||||
|
||||
done:
|
||||
@@ -222,6 +252,8 @@ int main(int argc, char **argv)
|
||||
int pattern_size;
|
||||
int extra_pitch;
|
||||
} automated_test_params[] = {
|
||||
/* Test: single pixel */
|
||||
{ SDL_FALSE, 1, 0 },
|
||||
/* Test: even width and height */
|
||||
{ SDL_FALSE, 2, 0 },
|
||||
{ SDL_FALSE, 4, 0 },
|
||||
@@ -258,9 +290,10 @@ int main(int argc, char **argv)
|
||||
const char *yuv_mode_name;
|
||||
Uint32 yuv_format = SDL_PIXELFORMAT_YV12;
|
||||
const char *yuv_format_name;
|
||||
SDL_Colorspace yuv_colorspace;
|
||||
Uint32 rgb_format = SDL_PIXELFORMAT_RGBX8888;
|
||||
SDL_Colorspace rgb_colorspace = SDL_COLORSPACE_SRGB;
|
||||
SDL_PropertiesID props;
|
||||
SDL_Colorspace colorspace;
|
||||
SDL_bool monochrome = SDL_FALSE;
|
||||
int luminance = 100;
|
||||
int current = 0;
|
||||
@@ -295,6 +328,9 @@ int main(int argc, char **argv)
|
||||
} else if (SDL_strcmp(argv[i], "--bt709") == 0) {
|
||||
SetYUVConversionMode(YUV_CONVERSION_BT709);
|
||||
consumed = 1;
|
||||
} else if (SDL_strcmp(argv[i], "--bt2020") == 0) {
|
||||
SetYUVConversionMode(YUV_CONVERSION_BT2020);
|
||||
consumed = 1;
|
||||
} else if (SDL_strcmp(argv[i], "--auto") == 0) {
|
||||
SetYUVConversionMode(YUV_CONVERSION_AUTOMATIC);
|
||||
consumed = 1;
|
||||
@@ -356,7 +392,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
if (consumed <= 0) {
|
||||
static const char *options[] = {
|
||||
"[--jpeg|--bt601|--bt709|--auto]",
|
||||
"[--jpeg|--bt601|--bt709|--bt2020|--auto]",
|
||||
"[--yv12|--iyuv|--yuy2|--uyvy|--yvyu|--nv12|--nv21]",
|
||||
"[--rgb555|--rgb565|--rgb24|--argb|--abgr|--rgba|--bgra]",
|
||||
"[--monochrome] [--luminance N%]",
|
||||
@@ -405,11 +441,17 @@ int main(int argc, char **argv)
|
||||
case YUV_CONVERSION_BT709:
|
||||
yuv_mode_name = "BT.709";
|
||||
break;
|
||||
case YUV_CONVERSION_BT2020:
|
||||
yuv_mode_name = "BT.2020";
|
||||
yuv_format = SDL_PIXELFORMAT_P010;
|
||||
rgb_format = SDL_PIXELFORMAT_XBGR2101010;
|
||||
rgb_colorspace = SDL_COLORSPACE_HDR10;
|
||||
break;
|
||||
default:
|
||||
yuv_mode_name = "UNKNOWN";
|
||||
break;
|
||||
}
|
||||
colorspace = GetColorspaceForYUVConversionMode(yuv_mode);
|
||||
yuv_colorspace = GetColorspaceForYUVConversionMode(yuv_mode);
|
||||
|
||||
raw_yuv = SDL_calloc(1, MAX_YUV_SURFACE_SIZE(original->w, original->h, 0));
|
||||
ConvertRGBtoYUV(yuv_format, original->pixels, original->pitch, raw_yuv, original->w, original->h, yuv_mode, monochrome, luminance);
|
||||
@@ -421,9 +463,13 @@ int main(int argc, char **argv)
|
||||
return 3;
|
||||
}
|
||||
|
||||
/* All tests are being done with SDR content */
|
||||
props = SDL_GetSurfaceProperties(converted);
|
||||
SDL_SetFloatProperty(props, SDL_PROP_SURFACE_HDR_HEADROOM_FLOAT, 1.0f);
|
||||
|
||||
then = SDL_GetTicks();
|
||||
for (i = 0; i < iterations; ++i) {
|
||||
SDL_ConvertPixelsAndColorspace(original->w, original->h, yuv_format, colorspace, 0, raw_yuv, pitch, rgb_format, SDL_COLORSPACE_SRGB, 0, converted->pixels, converted->pitch);
|
||||
SDL_ConvertPixelsAndColorspace(original->w, original->h, yuv_format, yuv_colorspace, props, raw_yuv, pitch, rgb_format, rgb_colorspace, props, converted->pixels, converted->pitch);
|
||||
}
|
||||
now = SDL_GetTicks();
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%d iterations in %" SDL_PRIu64 " ms, %.2fms each\n", iterations, (now - then), (float)(now - then) / iterations);
|
||||
@@ -443,7 +489,8 @@ int main(int argc, char **argv)
|
||||
output[0] = SDL_CreateTextureFromSurface(renderer, original);
|
||||
output[1] = SDL_CreateTextureFromSurface(renderer, converted);
|
||||
props = SDL_CreateProperties();
|
||||
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER, colorspace);
|
||||
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER, yuv_colorspace);
|
||||
SDL_SetFloatProperty(props, SDL_PROP_TEXTURE_CREATE_HDR_HEADROOM_FLOAT, 1.0f);
|
||||
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_FORMAT_NUMBER, yuv_format);
|
||||
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_ACCESS_NUMBER, SDL_TEXTUREACCESS_STREAMING);
|
||||
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_WIDTH_NUMBER, original->w);
|
||||
|
||||
@@ -70,6 +70,9 @@ SDL_Colorspace GetColorspaceForYUVConversionMode(YUV_CONVERSION_MODE mode)
|
||||
SDL_MATRIX_COEFFICIENTS_BT709,
|
||||
SDL_CHROMA_LOCATION_CENTER);
|
||||
break;
|
||||
case YUV_CONVERSION_BT2020:
|
||||
colorspace = SDL_COLORSPACE_BT2020_FULL;
|
||||
break;
|
||||
default:
|
||||
colorspace = SDL_COLORSPACE_UNKNOWN;
|
||||
break;
|
||||
@@ -82,7 +85,54 @@ static float clip3(float x, float y, float z)
|
||||
return (z < x) ? x : ((z > y) ? y : z);
|
||||
}
|
||||
|
||||
static void RGBtoYUV(const Uint8 *rgb, int *yuv, YUV_CONVERSION_MODE mode, int monochrome, int luminance)
|
||||
static float sRGBtoNits(float v)
|
||||
{
|
||||
/* Normalize from 0..255 */
|
||||
v /= 255.0f;
|
||||
|
||||
/* Convert from sRGB */
|
||||
v = v <= 0.04045f ? (v / 12.92f) : SDL_powf(((v + 0.055f) / 1.055f), 2.4f);
|
||||
|
||||
/* Convert to nits, using a default SDR whitepoint of 203 */
|
||||
v *= 203.0f;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
static float PQfromNits(float v)
|
||||
{
|
||||
const float c1 = 0.8359375f;
|
||||
const float c2 = 18.8515625f;
|
||||
const float c3 = 18.6875f;
|
||||
const float m1 = 0.1593017578125f;
|
||||
const float m2 = 78.84375f;
|
||||
|
||||
float y = SDL_clamp(v / 10000.0f, 0.0f, 1.0f);
|
||||
float num = c1 + c2 * SDL_powf(y, m1);
|
||||
float den = 1.0f + c3 * SDL_powf(y, m1);
|
||||
return SDL_powf(num / den, m2);
|
||||
}
|
||||
|
||||
void ConvertRec709toRec2020(float *fR, float *fG, float *fB)
|
||||
{
|
||||
static const float mat709to2020[] = {
|
||||
0.627404f, 0.329283f, 0.043313f,
|
||||
0.069097f, 0.919541f, 0.011362f,
|
||||
0.016391f, 0.088013f, 0.895595f,
|
||||
};
|
||||
const float *matrix = mat709to2020;
|
||||
float v[3];
|
||||
|
||||
v[0] = *fR;
|
||||
v[1] = *fG;
|
||||
v[2] = *fB;
|
||||
|
||||
*fR = matrix[0 * 3 + 0] * v[0] + matrix[0 * 3 + 1] * v[1] + matrix[0 * 3 + 2] * v[2];
|
||||
*fG = matrix[1 * 3 + 0] * v[0] + matrix[1 * 3 + 1] * v[1] + matrix[1 * 3 + 2] * v[2];
|
||||
*fB = matrix[2 * 3 + 0] * v[0] + matrix[2 * 3 + 1] * v[1] + matrix[2 * 3 + 2] * v[2];
|
||||
}
|
||||
|
||||
static void RGBtoYUV(const Uint8 *rgb, int rgb_bits, int *yuv, int yuv_bits, YUV_CONVERSION_MODE mode, int monochrome, int luminance)
|
||||
{
|
||||
/**
|
||||
* This formula is from Microsoft's documentation:
|
||||
@@ -93,56 +143,82 @@ static void RGBtoYUV(const Uint8 *rgb, int *yuv, YUV_CONVERSION_MODE mode, int m
|
||||
* V = clip3(0, (2^M)-1, floor(2^(M-8) * (112*(R-L) / ((1-Kr)*S) + 128) + 0.5));
|
||||
*/
|
||||
SDL_bool studio_RGB = SDL_FALSE;
|
||||
SDL_bool studio_YUV = SDL_FALSE;
|
||||
SDL_bool full_range_YUV = SDL_FALSE;
|
||||
float N, M, S, Z, R, G, B, L, Kr, Kb, Y, U, V;
|
||||
|
||||
N = 8.0f; /* 8 bit RGB */
|
||||
M = 8.0f; /* 8 bit YUV */
|
||||
if (mode == YUV_CONVERSION_BT709) {
|
||||
/* BT.709 */
|
||||
Kr = 0.2126f;
|
||||
Kb = 0.0722f;
|
||||
} else {
|
||||
N = (float)rgb_bits;
|
||||
M = (float)yuv_bits;
|
||||
switch (mode) {
|
||||
case YUV_CONVERSION_JPEG:
|
||||
case YUV_CONVERSION_BT601:
|
||||
/* BT.601 */
|
||||
Kr = 0.299f;
|
||||
Kb = 0.114f;
|
||||
break;
|
||||
case YUV_CONVERSION_BT709:
|
||||
/* BT.709 */
|
||||
Kr = 0.2126f;
|
||||
Kb = 0.0722f;
|
||||
break;
|
||||
case YUV_CONVERSION_BT2020:
|
||||
/* BT.2020 */
|
||||
Kr = 0.2627f;
|
||||
Kb = 0.0593f;
|
||||
break;
|
||||
default:
|
||||
/* Invalid */
|
||||
Kr = 1.0f;
|
||||
Kb = 1.0f;
|
||||
break;
|
||||
}
|
||||
|
||||
if (mode == YUV_CONVERSION_JPEG) {
|
||||
studio_YUV = SDL_FALSE;
|
||||
} else {
|
||||
studio_YUV = SDL_TRUE;
|
||||
R = rgb[0];
|
||||
G = rgb[1];
|
||||
B = rgb[2];
|
||||
|
||||
if (mode == YUV_CONVERSION_JPEG || mode == YUV_CONVERSION_BT2020) {
|
||||
full_range_YUV = SDL_TRUE;
|
||||
}
|
||||
|
||||
if (studio_RGB || !studio_YUV) {
|
||||
if (mode == YUV_CONVERSION_BT2020) {
|
||||
/* Input is sRGB, need to convert to BT.2020 PQ YUV */
|
||||
R = sRGBtoNits(R);
|
||||
G = sRGBtoNits(G);
|
||||
B = sRGBtoNits(B);
|
||||
ConvertRec709toRec2020(&R, &G, &B);
|
||||
R = PQfromNits(R);
|
||||
G = PQfromNits(G);
|
||||
B = PQfromNits(B);
|
||||
S = 1.0f;
|
||||
Z = 0.0f;
|
||||
} else if (studio_RGB) {
|
||||
S = 219.0f * SDL_powf(2.0f, N - 8);
|
||||
Z = 16.0f * SDL_powf(2.0f, N - 8);
|
||||
} else {
|
||||
S = 255.0f;
|
||||
Z = 0.0f;
|
||||
}
|
||||
R = rgb[0];
|
||||
G = rgb[1];
|
||||
B = rgb[2];
|
||||
L = Kr * R + Kb * B + (1 - Kr - Kb) * G;
|
||||
Y = SDL_floorf(SDL_powf(2.0f, (M - 8)) * (219.0f * (L - Z) / S + 16) + 0.5f);
|
||||
U = clip3(0, SDL_powf(2.0f, M) - 1, SDL_floorf(SDL_powf(2.0f, (M - 8)) * (112.0f * (B - L) / ((1.0f - Kb) * S) + 128) + 0.5f));
|
||||
V = clip3(0, SDL_powf(2.0f, M) - 1, SDL_floorf(SDL_powf(2.0f, (M - 8)) * (112.0f * (R - L) / ((1.0f - Kr) * S) + 128) + 0.5f));
|
||||
if (monochrome) {
|
||||
R = L;
|
||||
B = L;
|
||||
}
|
||||
if (full_range_YUV) {
|
||||
Y = SDL_floorf((SDL_powf(2.0f, M) - 1) * ((L - Z) / S) + 0.5f);
|
||||
U = clip3(0, SDL_powf(2.0f, M) - 1, SDL_floorf((SDL_powf(2.0f, M) / 2 - 1) * ((B - L) / ((1.0f - Kb) * S)) + SDL_powf(2.0f, M) / 2 + 0.5f));
|
||||
V = clip3(0, SDL_powf(2.0f, M) - 1, SDL_floorf((SDL_powf(2.0f, M) / 2 - 1) * ((R - L) / ((1.0f - Kr) * S)) + SDL_powf(2.0f, M) / 2 + 0.5f));
|
||||
} else {
|
||||
Y = SDL_floorf(SDL_powf(2.0f, (M - 8)) * (219.0f * (L - Z) / S + 16) + 0.5f);
|
||||
U = clip3(0, SDL_powf(2.0f, M) - 1, SDL_floorf(SDL_powf(2.0f, (M - 8)) * (112.0f * (B - L) / ((1.0f - Kb) * S) + 128) + 0.5f));
|
||||
V = clip3(0, SDL_powf(2.0f, M) - 1, SDL_floorf(SDL_powf(2.0f, (M - 8)) * (112.0f * (R - L) / ((1.0f - Kr) * S) + 128) + 0.5f));
|
||||
}
|
||||
|
||||
yuv[0] = (int)Y;
|
||||
yuv[1] = (int)U;
|
||||
yuv[2] = (int)V;
|
||||
|
||||
if (monochrome) {
|
||||
yuv[1] = 128;
|
||||
yuv[2] = 128;
|
||||
}
|
||||
|
||||
if (luminance != 100) {
|
||||
yuv[0] = (int)SDL_roundf(yuv[0] * (luminance / 100.0f));
|
||||
if (yuv[0] > 255) {
|
||||
yuv[0] = 255;
|
||||
}
|
||||
yuv[0] = (int)clip3(0, SDL_powf(2.0f, M) - 1, SDL_roundf(yuv[0] * (luminance / 100.0f)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,19 +264,19 @@ static void ConvertRGBtoPlanar2x2(Uint32 format, Uint8 *src, int pitch, Uint8 *o
|
||||
|
||||
for (y = 0; y < (h - 1); y += 2) {
|
||||
for (x = 0; x < (w - 1); x += 2) {
|
||||
RGBtoYUV(rgb1, yuv[0], mode, monochrome, luminance);
|
||||
RGBtoYUV(rgb1, 8, yuv[0], 8, mode, monochrome, luminance);
|
||||
rgb1 += 3;
|
||||
*Y1++ = (Uint8)yuv[0][0];
|
||||
|
||||
RGBtoYUV(rgb1, yuv[1], mode, monochrome, luminance);
|
||||
RGBtoYUV(rgb1, 8, yuv[1], 8, mode, monochrome, luminance);
|
||||
rgb1 += 3;
|
||||
*Y1++ = (Uint8)yuv[1][0];
|
||||
|
||||
RGBtoYUV(rgb2, yuv[2], mode, monochrome, luminance);
|
||||
RGBtoYUV(rgb2, 8, yuv[2], 8, mode, monochrome, luminance);
|
||||
rgb2 += 3;
|
||||
*Y2++ = (Uint8)yuv[2][0];
|
||||
|
||||
RGBtoYUV(rgb2, yuv[3], mode, monochrome, luminance);
|
||||
RGBtoYUV(rgb2, 8, yuv[3], 8, mode, monochrome, luminance);
|
||||
rgb2 += 3;
|
||||
*Y2++ = (Uint8)yuv[3][0];
|
||||
|
||||
@@ -212,11 +288,11 @@ static void ConvertRGBtoPlanar2x2(Uint32 format, Uint8 *src, int pitch, Uint8 *o
|
||||
}
|
||||
/* Last column */
|
||||
if (x == (w - 1)) {
|
||||
RGBtoYUV(rgb1, yuv[0], mode, monochrome, luminance);
|
||||
RGBtoYUV(rgb1, 8, yuv[0], 8, mode, monochrome, luminance);
|
||||
rgb1 += 3;
|
||||
*Y1++ = (Uint8)yuv[0][0];
|
||||
|
||||
RGBtoYUV(rgb2, yuv[2], mode, monochrome, luminance);
|
||||
RGBtoYUV(rgb2, 8, yuv[2], 8, mode, monochrome, luminance);
|
||||
rgb2 += 3;
|
||||
*Y2++ = (Uint8)yuv[2][0];
|
||||
|
||||
@@ -234,11 +310,11 @@ static void ConvertRGBtoPlanar2x2(Uint32 format, Uint8 *src, int pitch, Uint8 *o
|
||||
/* Last row */
|
||||
if (y == (h - 1)) {
|
||||
for (x = 0; x < (w - 1); x += 2) {
|
||||
RGBtoYUV(rgb1, yuv[0], mode, monochrome, luminance);
|
||||
RGBtoYUV(rgb1, 8, yuv[0], 8, mode, monochrome, luminance);
|
||||
rgb1 += 3;
|
||||
*Y1++ = (Uint8)yuv[0][0];
|
||||
|
||||
RGBtoYUV(rgb1, yuv[1], mode, monochrome, luminance);
|
||||
RGBtoYUV(rgb1, 8, yuv[1], 8, mode, monochrome, luminance);
|
||||
rgb1 += 3;
|
||||
*Y1++ = (Uint8)yuv[1][0];
|
||||
|
||||
@@ -250,7 +326,7 @@ static void ConvertRGBtoPlanar2x2(Uint32 format, Uint8 *src, int pitch, Uint8 *o
|
||||
}
|
||||
/* Last column */
|
||||
if (x == (w - 1)) {
|
||||
RGBtoYUV(rgb1, yuv[0], mode, monochrome, luminance);
|
||||
RGBtoYUV(rgb1, 8, yuv[0], 8, mode, monochrome, luminance);
|
||||
*Y1++ = (Uint8)yuv[0][0];
|
||||
|
||||
*U = (Uint8)yuv[0][1];
|
||||
@@ -262,6 +338,112 @@ static void ConvertRGBtoPlanar2x2(Uint32 format, Uint8 *src, int pitch, Uint8 *o
|
||||
}
|
||||
}
|
||||
|
||||
static Uint16 Pack10to16(int v)
|
||||
{
|
||||
return (Uint16)(v << 6);
|
||||
}
|
||||
|
||||
static void ConvertRGBtoPlanar2x2_P010(Uint32 format, Uint8 *src, int pitch, Uint8 *out, int w, int h, YUV_CONVERSION_MODE mode, int monochrome, int luminance)
|
||||
{
|
||||
int x, y;
|
||||
int yuv[4][3];
|
||||
Uint16 *Y1, *Y2, *U, *V;
|
||||
Uint8 *rgb1, *rgb2;
|
||||
int rgb_row_advance = (pitch - w * 3) + pitch;
|
||||
int UV_advance;
|
||||
|
||||
rgb1 = src;
|
||||
rgb2 = src + pitch;
|
||||
|
||||
Y1 = (Uint16 *)out;
|
||||
Y2 = Y1 + w;
|
||||
switch (format) {
|
||||
case SDL_PIXELFORMAT_P010:
|
||||
U = (Y1 + h * w);
|
||||
V = U + 1;
|
||||
UV_advance = 2;
|
||||
break;
|
||||
default:
|
||||
SDL_assert(!"Unsupported planar YUV format");
|
||||
return;
|
||||
}
|
||||
|
||||
for (y = 0; y < (h - 1); y += 2) {
|
||||
for (x = 0; x < (w - 1); x += 2) {
|
||||
RGBtoYUV(rgb1, 8, yuv[0], 10, mode, monochrome, luminance);
|
||||
rgb1 += 3;
|
||||
*Y1++ = Pack10to16(yuv[0][0]);
|
||||
|
||||
RGBtoYUV(rgb1, 8, yuv[1], 10, mode, monochrome, luminance);
|
||||
rgb1 += 3;
|
||||
*Y1++ = Pack10to16(yuv[1][0]);
|
||||
|
||||
RGBtoYUV(rgb2, 8, yuv[2], 10, mode, monochrome, luminance);
|
||||
rgb2 += 3;
|
||||
*Y2++ = Pack10to16(yuv[2][0]);
|
||||
|
||||
RGBtoYUV(rgb2, 8, yuv[3], 10, mode, monochrome, luminance);
|
||||
rgb2 += 3;
|
||||
*Y2++ = Pack10to16(yuv[3][0]);
|
||||
|
||||
*U = Pack10to16((int)SDL_floorf((yuv[0][1] + yuv[1][1] + yuv[2][1] + yuv[3][1]) / 4.0f + 0.5f));
|
||||
U += UV_advance;
|
||||
|
||||
*V = Pack10to16((int)SDL_floorf((yuv[0][2] + yuv[1][2] + yuv[2][2] + yuv[3][2]) / 4.0f + 0.5f));
|
||||
V += UV_advance;
|
||||
}
|
||||
/* Last column */
|
||||
if (x == (w - 1)) {
|
||||
RGBtoYUV(rgb1, 8, yuv[0], 10, mode, monochrome, luminance);
|
||||
rgb1 += 3;
|
||||
*Y1++ = Pack10to16(yuv[0][0]);
|
||||
|
||||
RGBtoYUV(rgb2, 8, yuv[2], 10, mode, monochrome, luminance);
|
||||
rgb2 += 3;
|
||||
*Y2++ = Pack10to16(yuv[2][0]);
|
||||
|
||||
*U = Pack10to16((int)SDL_floorf((yuv[0][1] + yuv[2][1]) / 2.0f + 0.5f));
|
||||
U += UV_advance;
|
||||
|
||||
*V = Pack10to16((int)SDL_floorf((yuv[0][2] + yuv[2][2]) / 2.0f + 0.5f));
|
||||
V += UV_advance;
|
||||
}
|
||||
Y1 += w;
|
||||
Y2 += w;
|
||||
rgb1 += rgb_row_advance;
|
||||
rgb2 += rgb_row_advance;
|
||||
}
|
||||
/* Last row */
|
||||
if (y == (h - 1)) {
|
||||
for (x = 0; x < (w - 1); x += 2) {
|
||||
RGBtoYUV(rgb1, 8, yuv[0], 10, mode, monochrome, luminance);
|
||||
rgb1 += 3;
|
||||
*Y1++ = Pack10to16(yuv[0][0]);
|
||||
|
||||
RGBtoYUV(rgb1, 8, yuv[1], 10, mode, monochrome, luminance);
|
||||
rgb1 += 3;
|
||||
*Y1++ = Pack10to16(yuv[1][0]);
|
||||
|
||||
*U = Pack10to16((int)SDL_floorf((yuv[0][1] + yuv[1][1]) / 2.0f + 0.5f));
|
||||
U += UV_advance;
|
||||
|
||||
*V = Pack10to16((int)SDL_floorf((yuv[0][2] + yuv[1][2]) / 2.0f + 0.5f));
|
||||
V += UV_advance;
|
||||
}
|
||||
/* Last column */
|
||||
if (x == (w - 1)) {
|
||||
RGBtoYUV(rgb1, 8, yuv[0], 10, mode, monochrome, luminance);
|
||||
*Y1++ = Pack10to16(yuv[0][0]);
|
||||
|
||||
*U = Pack10to16(yuv[0][1]);
|
||||
U += UV_advance;
|
||||
|
||||
*V = Pack10to16(yuv[0][2]);
|
||||
V += UV_advance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void ConvertRGBtoPacked4(Uint32 format, Uint8 *src, int pitch, Uint8 *out, int w, int h, YUV_CONVERSION_MODE mode, int monochrome, int luminance)
|
||||
{
|
||||
int x, y;
|
||||
@@ -298,12 +480,12 @@ static void ConvertRGBtoPacked4(Uint32 format, Uint8 *src, int pitch, Uint8 *out
|
||||
|
||||
for (y = 0; y < h; ++y) {
|
||||
for (x = 0; x < (w - 1); x += 2) {
|
||||
RGBtoYUV(rgb, yuv[0], mode, monochrome, luminance);
|
||||
RGBtoYUV(rgb, 8, yuv[0], 8, mode, monochrome, luminance);
|
||||
rgb += 3;
|
||||
*Y1 = (Uint8)yuv[0][0];
|
||||
Y1 += 4;
|
||||
|
||||
RGBtoYUV(rgb, yuv[1], mode, monochrome, luminance);
|
||||
RGBtoYUV(rgb, 8, yuv[1], 8, mode, monochrome, luminance);
|
||||
rgb += 3;
|
||||
*Y2 = (Uint8)yuv[1][0];
|
||||
Y2 += 4;
|
||||
@@ -316,7 +498,7 @@ static void ConvertRGBtoPacked4(Uint32 format, Uint8 *src, int pitch, Uint8 *out
|
||||
}
|
||||
/* Last column */
|
||||
if (x == (w - 1)) {
|
||||
RGBtoYUV(rgb, yuv[0], mode, monochrome, luminance);
|
||||
RGBtoYUV(rgb, 8, yuv[0], 8, mode, monochrome, luminance);
|
||||
rgb += 3;
|
||||
*Y2 = *Y1 = (Uint8)yuv[0][0];
|
||||
Y1 += 4;
|
||||
@@ -335,6 +517,9 @@ static void ConvertRGBtoPacked4(Uint32 format, Uint8 *src, int pitch, Uint8 *out
|
||||
SDL_bool ConvertRGBtoYUV(Uint32 format, Uint8 *src, int pitch, Uint8 *out, int w, int h, YUV_CONVERSION_MODE mode, int monochrome, int luminance)
|
||||
{
|
||||
switch (format) {
|
||||
case SDL_PIXELFORMAT_P010:
|
||||
ConvertRGBtoPlanar2x2_P010(format, src, pitch, out, w, h, mode, monochrome, luminance);
|
||||
return SDL_TRUE;
|
||||
case SDL_PIXELFORMAT_YV12:
|
||||
case SDL_PIXELFORMAT_IYUV:
|
||||
case SDL_PIXELFORMAT_NV12:
|
||||
@@ -354,6 +539,8 @@ SDL_bool ConvertRGBtoYUV(Uint32 format, Uint8 *src, int pitch, Uint8 *out, int w
|
||||
int CalculateYUVPitch(Uint32 format, int width)
|
||||
{
|
||||
switch (format) {
|
||||
case SDL_PIXELFORMAT_P010:
|
||||
return width * 2;
|
||||
case SDL_PIXELFORMAT_YV12:
|
||||
case SDL_PIXELFORMAT_IYUV:
|
||||
case SDL_PIXELFORMAT_NV12:
|
||||
|
||||
@@ -17,6 +17,7 @@ typedef enum
|
||||
YUV_CONVERSION_JPEG, /**< Full range JPEG */
|
||||
YUV_CONVERSION_BT601, /**< BT.601 (the default) */
|
||||
YUV_CONVERSION_BT709, /**< BT.709 */
|
||||
YUV_CONVERSION_BT2020, /**< BT.2020 */
|
||||
YUV_CONVERSION_AUTOMATIC /**< BT.601 for SD content, BT.709 for HD content */
|
||||
} YUV_CONVERSION_MODE;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user