2#include <freerdp/config.h>
10#include <winpr/print.h>
12#include <winpr/wlog.h>
13#include <winpr/crypto.h>
14#include <freerdp/primitives.h>
15#include <freerdp/utils/profiler.h>
17#include "../prim_internal.h"
21#define PADDING_FILL_VALUE 0x37
25static BOOL similar(
const BYTE* src,
const BYTE* dst,
size_t size)
27 for (
size_t x = 0; x < size; x++)
29 int diff = src[x] - dst[x];
33 (void)fprintf(stderr,
"%" PRIuz
" %02" PRIX8
" : %02" PRIX8
" diff=%d\n", x, src[x],
42static BOOL similarRGB(
size_t y,
const BYTE* src,
const BYTE* dst,
size_t size, UINT32 format,
50 const UINT32 bpp = FreeRDPGetBytesPerPixel(format);
51 BYTE fill = PADDING_FILL_VALUE;
52 if (!FreeRDPColorHasAlpha(format))
55 for (
size_t x = 0; x < size; x++)
57 const LONG maxDiff = 4;
68 sColor = FreeRDPReadColor(src, format);
69 dColor = FreeRDPReadColor(dst, format);
72 FreeRDPSplitColor(sColor, format, &sR, &sG, &sB, &sA, NULL);
73 FreeRDPSplitColor(dColor, format, &dR, &dG, &dB, &dA, NULL);
75 const long diffr = labs(1L * sR - dR);
76 const long diffg = labs(1L * sG - dG);
77 const long diffb = labs(1L * sB - dB);
78 if ((diffr > maxDiff) || (diffg > maxDiff) || (diffb > maxDiff))
86 if (use444 && ((x % 2) == 0) && ((y % 2) == 0))
91 const BYTE sY = RGB2Y(sR, sG, sB);
92 const BYTE sU = RGB2U(sR, sG, sB);
93 const BYTE sV = RGB2V(sR, sG, sB);
94 const BYTE dY = RGB2Y(dR, dG, dB);
95 const BYTE dU = RGB2U(dR, dG, dB);
96 const BYTE dV = RGB2V(dR, dG, dB);
98 "[%s] Color value mismatch R[%02X %02X], G[%02X %02X], B[%02X %02X] at "
99 "position %" PRIuz
"x%" PRIuz
"\n",
100 use444 ?
"AVC444" :
"AVC420", sR, dR, sG, dG, sA, dA, x, y);
101 (void)fprintf(stderr,
102 "[%s] Color value mismatch Y[%02X %02X], U[%02X %02X], V[%02X %02X] at "
103 "position %" PRIuz
"x%" PRIuz
"\n",
104 use444 ?
"AVC444" :
"AVC420", sY, dY, sU, dU, sV, dV, x, y);
113 "[%s] Invalid destination alpha value 0x%02X [expected 0x%02X] at position %" PRIuz
115 use444 ?
"AVC444" :
"AVC420", dA, fill, x, y);
124static void get_size(BOOL large, UINT32* width, UINT32* height)
126 UINT32 shift = large ? 8 : 1;
127 winpr_RAND(width,
sizeof(*width));
128 winpr_RAND(height,
sizeof(*height));
129 *width = (*width % 64 + 1) << shift;
130 *height = (*height % 64 + 1);
133static BOOL check_padding(
const BYTE* psrc,
size_t size,
size_t padding,
const char* buffer)
136 const BYTE* src = NULL;
137 const BYTE* esrc = NULL;
138 size_t halfPad = (padding + 1) / 2;
143 src = psrc - halfPad;
144 esrc = src + size + halfPad;
146 for (
size_t x = 0; x < halfPad; x++)
148 const BYTE s = *src++;
149 const BYTE d = *esrc++;
155 while ((x < halfPad) && (*esrc++ !=
'A'))
158 (void)fprintf(stderr,
159 "Buffer underflow detected %02" PRIx8
" != %02X %s [%" PRIuz
"-%" PRIuz
161 d,
'A', buffer, start, x);
169 while ((x < halfPad) && (*esrc++ !=
'A'))
172 (void)fprintf(stderr,
173 "Buffer overflow detected %02" PRIx8
" != %02X %s [%" PRIuz
"-%" PRIuz
175 d,
'A', buffer, start, x);
183static void* set_padding(
size_t size,
size_t padding)
185 size_t halfPad = (padding + 1) / 2;
187 BYTE* src = winpr_aligned_malloc(size + 2 * halfPad, 16);
192 memset(&src[0],
'A', halfPad);
193 memset(&src[halfPad], PADDING_FILL_VALUE, size);
194 memset(&src[halfPad + size],
'A', halfPad);
195 psrc = &src[halfPad];
197 if (!check_padding(psrc, size, padding,
"init"))
199 winpr_aligned_free(src);
206static void free_padding(
void* src,
size_t padding)
213 ptr = ((BYTE*)src) - (padding + 1) / 2;
214 winpr_aligned_free(ptr);
229 BYTE* luma[3] = { 0 };
230 BYTE* chroma[3] = { 0 };
231 BYTE* yuv[3] = { 0 };
232 BYTE* pmain[3] = { 0 };
233 BYTE* paux[3] = { 0 };
234 UINT32 lumaStride[3] = { 0 };
235 UINT32 chromaStride[3] = { 0 };
236 UINT32 yuvStride[3] = { 0 };
237 const size_t padding = 10000;
239 PROFILER_DEFINE(yuvCombine)
240 PROFILER_DEFINE(yuvSplit)
246 awidth = roi.width + 16 - roi.width % 16;
247 aheight = roi.height + 16 - roi.height % 16;
248 (void)fprintf(stderr,
249 "Running YUVCombine on frame size %" PRIu32
"x%" PRIu32
" [%" PRIuz
"x%" PRIuz
251 roi.width, roi.height, awidth, aheight);
252 PROFILER_CREATE(yuvCombine,
"YUV420CombineToYUV444")
253 PROFILER_CREATE(yuvSplit, "YUV444SplitToYUV420")
256 rect.right = roi.width;
257 rect.bottom = roi.height;
259 if (!prims || !prims->YUV420CombineToYUV444)
262 for (UINT32 x = 0; x < 3; x++)
264 size_t halfStride = ((x > 0) ? awidth / 2 : awidth);
265 size_t size = aheight * awidth;
266 size_t halfSize = ((x > 0) ? halfStride * aheight / 2 : awidth * aheight);
267 yuvStride[x] = awidth;
269 if (!(yuv[x] = set_padding(size, padding)))
272 lumaStride[x] = halfStride;
274 if (!(luma[x] = set_padding(halfSize, padding)))
277 if (!(pmain[x] = set_padding(halfSize, padding)))
280 chromaStride[x] = halfStride;
282 if (!(chroma[x] = set_padding(halfSize, padding)))
285 if (!(paux[x] = set_padding(halfSize, padding)))
288 memset(luma[x], WINPR_ASSERTING_INT_CAST(
int, 0xAB + 3 * x), halfSize);
289 memset(chroma[x], WINPR_ASSERTING_INT_CAST(
int, 0x80 + 2 * x), halfSize);
291 if (!check_padding(luma[x], halfSize, padding,
"luma"))
294 if (!check_padding(chroma[x], halfSize, padding,
"chroma"))
297 if (!check_padding(pmain[x], halfSize, padding,
"main"))
300 if (!check_padding(paux[x], halfSize, padding,
"aux"))
303 if (!check_padding(yuv[x], size, padding,
"yuv"))
307 PROFILER_ENTER(yuvCombine)
310 if (prims->YUV420CombineToYUV444(AVC444_LUMA, cnv.cpv, lumaStride, roi.width, roi.height, yuv,
311 yuvStride, &rect) != PRIMITIVES_SUCCESS)
313 PROFILER_EXIT(yuvCombine)
318 if (prims->YUV420CombineToYUV444(AVC444_CHROMAv1, cnv.cpv, chromaStride, roi.width, roi.height,
319 yuv, yuvStride, &rect) != PRIMITIVES_SUCCESS)
321 PROFILER_EXIT(yuvCombine)
325 PROFILER_EXIT(yuvCombine)
327 for (
size_t x = 0; x < 3; x++)
329 size_t halfStride = ((x > 0) ? awidth / 2 : awidth);
330 size_t size = 1ULL * aheight * awidth;
331 size_t halfSize = ((x > 0) ? halfStride * aheight / 2 : awidth * aheight);
333 if (!check_padding(luma[x], halfSize, padding,
"luma"))
336 if (!check_padding(chroma[x], halfSize, padding,
"chroma"))
339 if (!check_padding(yuv[x], size, padding,
"yuv"))
343 PROFILER_ENTER(yuvSplit)
346 if (prims->YUV444SplitToYUV420(cnv.cpv, yuvStride, pmain, lumaStride, paux, chromaStride,
347 &roi) != PRIMITIVES_SUCCESS)
349 PROFILER_EXIT(yuvSplit)
353 PROFILER_EXIT(yuvSplit)
355 for (UINT32 x = 0; x < 3; x++)
357 size_t halfStride = ((x > 0) ? awidth / 2 : awidth);
358 size_t size = aheight * awidth;
359 size_t halfSize = ((x > 0) ? halfStride * aheight / 2 : awidth * aheight);
361 if (!check_padding(pmain[x], halfSize, padding,
"main"))
364 if (!check_padding(paux[x], halfSize, padding,
"aux"))
367 if (!check_padding(yuv[x], size, padding,
"yuv"))
371 for (
size_t i = 0; i < 3; i++)
373 for (
size_t y = 0; y < roi.height; y++)
375 UINT32 w = roi.width;
376 UINT32 lstride = lumaStride[i];
377 UINT32 cstride = chromaStride[i];
381 w = (roi.width + 3) / 4;
383 if (roi.height > (roi.height + 1) / 2)
387 if (!similar(luma[i] + y * lstride, pmain[i] + y * lstride, w))
398 UINT32 rem = roi.height % 16;
400 if (y > roi.height - rem)
404 if (!similar(chroma[i] + y * cstride, paux[i] + y * cstride, w))
409 PROFILER_PRINT_HEADER
410 PROFILER_PRINT(yuvSplit)
411 PROFILER_PRINT(yuvCombine)
412 PROFILER_PRINT_FOOTER
415 printf(
"[%s] run %s.\n", __func__, (rc) ?
"SUCCESS" :
"FAILED");
416 PROFILER_FREE(yuvCombine)
417 PROFILER_FREE(yuvSplit)
419 for (UINT32 x = 0; x < 3; x++)
421 free_padding(yuv[x], padding);
422 free_padding(luma[x], padding);
423 free_padding(chroma[x], padding);
424 free_padding(pmain[x], padding);
425 free_padding(paux[x], padding);
441 BYTE* yuv[3] = { 0 };
442 UINT32 yuv_step[3] = { 0 };
444 BYTE* rgb_dst = NULL;
448 size_t padding = 100ULL * 16ULL;
450 const UINT32 formats[] = { PIXEL_FORMAT_XRGB32, PIXEL_FORMAT_XBGR32, PIXEL_FORMAT_ARGB32,
451 PIXEL_FORMAT_ABGR32, PIXEL_FORMAT_RGBA32, PIXEL_FORMAT_RGBX32,
452 PIXEL_FORMAT_BGRA32, PIXEL_FORMAT_BGRX32 };
453 PROFILER_DEFINE(rgbToYUV420)
454 PROFILER_DEFINE(rgbToYUV444)
455 PROFILER_DEFINE(yuv420ToRGB)
456 PROFILER_DEFINE(yuv444ToRGB)
458 awidth = roi.width + 16 - roi.width % 16;
459 aheight = roi.height + 16 - roi.height % 16;
460 stride = 1ULL * awidth *
sizeof(UINT32);
461 size = 1ULL * awidth * aheight;
468 if (!prims || !prims->RGBToYUV444_8u_P3AC4R || !prims->YUV444ToRGB_8u_P3AC4R)
473 uvwidth = (awidth + 1) / 2;
474 uvsize = (aheight + 1) / 2 * uvwidth;
476 if (!prims || !prims->RGBToYUV420_8u_P3AC4R || !prims->YUV420ToRGB_8u_P3AC4R)
480 (void)fprintf(stderr,
"Running AVC%s on frame size %" PRIu32
"x%" PRIu32
"\n",
481 use444 ?
"444" :
"420", roi.width, roi.height);
484 if (!(rgb = set_padding(size *
sizeof(UINT32), padding)))
487 if (!(rgb_dst = set_padding(size *
sizeof(UINT32), padding)))
490 if (!(yuv[0] = set_padding(size, padding)))
493 if (!(yuv[1] = set_padding(uvsize, padding)))
496 if (!(yuv[2] = set_padding(uvsize, padding)))
499 for (
size_t y = 0; y < roi.height; y++)
501 BYTE* line = &rgb[y * stride];
502 winpr_RAND(line, stride);
505 yuv_step[0] = awidth;
506 yuv_step[1] = uvwidth;
507 yuv_step[2] = uvwidth;
509 for (UINT32 x = 0; x < ARRAYSIZE(formats); x++)
512 const UINT32 DstFormat = formats[x];
513 printf(
"Testing destination color format %s\n", FreeRDPGetColorFormatName(DstFormat));
514 memset(rgb_dst, PADDING_FILL_VALUE, size *
sizeof(UINT32));
516 PROFILER_CREATE(rgbToYUV420,
"RGBToYUV420")
517 PROFILER_CREATE(rgbToYUV444, "RGBToYUV444")
518 PROFILER_CREATE(yuv420ToRGB, "YUV420ToRGB")
519 PROFILER_CREATE(yuv444ToRGB, "YUV444ToRGB")
523 PROFILER_ENTER(rgbToYUV444)
524 rc = prims->RGBToYUV444_8u_P3AC4R(rgb, DstFormat, stride, yuv, yuv_step, &roi);
525 PROFILER_EXIT(rgbToYUV444)
527 if (rc != PRIMITIVES_SUCCESS)
530 PROFILER_PRINT_HEADER
531 PROFILER_PRINT(rgbToYUV444)
532 PROFILER_PRINT_FOOTER
536 PROFILER_ENTER(rgbToYUV420)
537 rc = prims->RGBToYUV420_8u_P3AC4R(rgb, DstFormat, stride, yuv, yuv_step, &roi);
538 PROFILER_EXIT(rgbToYUV420)
540 if (rc != PRIMITIVES_SUCCESS)
543 PROFILER_PRINT_HEADER
544 PROFILER_PRINT(rgbToYUV420)
545 PROFILER_PRINT_FOOTER
548 if (!check_padding(rgb, size *
sizeof(UINT32), padding,
"rgb"))
554 if ((!check_padding(yuv[0], size, padding,
"Y")) ||
555 (!check_padding(yuv[1], uvsize, padding,
"U")) ||
556 (!check_padding(yuv[2], uvsize, padding,
"V")))
565 PROFILER_ENTER(yuv444ToRGB)
566 rc = prims->YUV444ToRGB_8u_P3AC4R(cnv.cpv, yuv_step, rgb_dst, stride, DstFormat, &roi);
567 PROFILER_EXIT(yuv444ToRGB)
569 if (rc != PRIMITIVES_SUCCESS)
573 PROFILER_EXIT(yuv444ToRGB)
574 PROFILER_PRINT_HEADER
575 PROFILER_PRINT(yuv444ToRGB)
576 PROFILER_PRINT_FOOTER
578 if (rc != PRIMITIVES_SUCCESS)
583 PROFILER_ENTER(yuv420ToRGB)
585 if (prims->YUV420ToRGB_8u_P3AC4R(cnv.cpv, yuv_step, rgb_dst, stride, DstFormat, &roi) !=
588 PROFILER_EXIT(yuv420ToRGB)
592 PROFILER_EXIT(yuv420ToRGB)
593 PROFILER_PRINT_HEADER
594 PROFILER_PRINT(yuv420ToRGB)
595 PROFILER_PRINT_FOOTER
598 if (!check_padding(rgb_dst, size *
sizeof(UINT32), padding,
"rgb dst"))
601 if ((!check_padding(yuv[0], size, padding,
"Y")) ||
602 (!check_padding(yuv[1], uvsize, padding,
"U")) ||
603 (!check_padding(yuv[2], uvsize, padding,
"V")))
607 for (
size_t y = 0; y < roi.height; y++)
609 BYTE* srgb = &rgb[y * stride];
610 BYTE* drgb = &rgb_dst[y * stride];
612 if (!similarRGB(y, srgb, drgb, roi.width, DstFormat, use444))
618 PROFILER_FREE(rgbToYUV420)
619 PROFILER_FREE(rgbToYUV444)
620 PROFILER_FREE(yuv420ToRGB)
621 PROFILER_FREE(yuv444ToRGB)
626 printf(
"[%s] run %s.\n", __func__, (res) ?
"SUCCESS" :
"FAILED");
627 free_padding(rgb, padding);
628 free_padding(rgb_dst, padding);
629 free_padding(yuv[0], padding);
630 free_padding(yuv[1], padding);
631 free_padding(yuv[2], padding);
635static BOOL allocate_yuv420(BYTE** planes, UINT32 width, UINT32 height, UINT32 padding)
637 const size_t size = 1ULL * width * height;
638 const size_t uvwidth = (1ULL + width) / 2;
639 const size_t uvsize = (1ULL + height) / 2 * uvwidth;
641 if (!(planes[0] = set_padding(size, padding)))
644 if (!(planes[1] = set_padding(uvsize, padding)))
647 if (!(planes[2] = set_padding(uvsize, padding)))
652 free_padding(planes[0], padding);
653 free_padding(planes[1], padding);
654 free_padding(planes[2], padding);
658static void free_yuv420(BYTE** planes, UINT32 padding)
663 free_padding(planes[0], padding);
664 free_padding(planes[1], padding);
665 free_padding(planes[2], padding);
671static BOOL check_yuv420(BYTE** planes, UINT32 width, UINT32 height, UINT32 padding)
673 const size_t size = 1ULL * width * height;
674 const size_t uvwidth = (width + 1) / 2;
675 const size_t uvsize = (height + 1) / 2 * uvwidth;
676 const BOOL yOk = check_padding(planes[0], size, padding,
"Y");
677 const BOOL uOk = check_padding(planes[1], uvsize, padding,
"U");
678 const BOOL vOk = check_padding(planes[2], uvsize, padding,
"V");
679 return (yOk && uOk && vOk);
682static BOOL check_for_mismatches(
const BYTE* planeA,
const BYTE* planeB, UINT32 size)
686 for (UINT32 x = 0; x < size; x++)
688 const BYTE a = planeA[x];
689 const BYTE b = planeB[x];
691 if (fabsf((
float)a - (
float)b) > 2.0f)
694 (void)fprintf(stderr,
"[%08x] %02x != %02x\n", x, a, b);
701static BOOL compare_yuv420(BYTE** planesA, BYTE** planesB, UINT32 width, UINT32 height,
705 const size_t size = 1ULL * width * height;
706 const size_t uvwidth = (1ULL * width + 1) / 2;
707 const size_t uvsize = (1ULL * height + 1) / 2 * uvwidth;
709 if (check_for_mismatches(planesA[0], planesB[0], size))
711 (void)fprintf(stderr,
"Mismatch in Y planes!\n");
715 if (check_for_mismatches(planesA[1], planesB[1], uvsize))
717 (void)fprintf(stderr,
"Mismatch in U planes!\n");
721 if (check_for_mismatches(planesA[2], planesB[2], uvsize))
723 (void)fprintf(stderr,
"Mismatch in V planes!\n");
730static UINT32 prand(UINT32 max)
735 winpr_RAND(&tmp,
sizeof(tmp));
736 return tmp % (max - 1) + 1;
744 BYTE* luma[3] = { 0 };
745 BYTE* chroma[3] = { 0 };
746 BYTE* lumaGeneric[3] = { 0 };
747 BYTE* chromaGeneric[3] = { 0 };
752 const size_t padding = 0x1000;
754 fn_RGBToAVC444YUV_t fkt = NULL;
755 fn_RGBToAVC444YUV_t gen = NULL;
756 const UINT32 formats[] = { PIXEL_FORMAT_XRGB32, PIXEL_FORMAT_XBGR32, PIXEL_FORMAT_ARGB32,
757 PIXEL_FORMAT_ABGR32, PIXEL_FORMAT_RGBA32, PIXEL_FORMAT_RGBX32,
758 PIXEL_FORMAT_BGRA32, PIXEL_FORMAT_BGRX32 };
759 PROFILER_DEFINE(rgbToYUV444)
760 PROFILER_DEFINE(rgbToYUV444opt)
764 if (awidth % 16 != 0)
765 awidth += 16 - roi.width % 16;
767 aheight = roi.height;
769 if (aheight % 16 != 0)
770 aheight += 16 - roi.height % 16;
772 stride = 1ULL * awidth *
sizeof(UINT32);
773 size = 1ULL * awidth * aheight;
774 uvwidth = 1ULL * (awidth + 1) / 2;
776 if (!prims || !generic)
782 fkt = prims->RGBToAVC444YUV;
783 gen =
generic->RGBToAVC444YUV;
787 fkt = prims->RGBToAVC444YUVv2;
788 gen =
generic->RGBToAVC444YUVv2;
798 (void)fprintf(stderr,
"Running AVC444 on frame size %" PRIu32
"x%" PRIu32
"\n", roi.width,
802 if (!(rgb = set_padding(size *
sizeof(UINT32), padding)))
805 if (!allocate_yuv420(luma, awidth, aheight, padding))
808 if (!allocate_yuv420(chroma, awidth, aheight, padding))
811 if (!allocate_yuv420(lumaGeneric, awidth, aheight, padding))
814 if (!allocate_yuv420(chromaGeneric, awidth, aheight, padding))
817 for (
size_t y = 0; y < roi.height; y++)
819 BYTE* line = &rgb[y * stride];
821 winpr_RAND(line, 4ULL * roi.width);
824 yuv_step[0] = awidth;
825 yuv_step[1] = uvwidth;
826 yuv_step[2] = uvwidth;
828 for (UINT32 x = 0; x < ARRAYSIZE(formats); x++)
831 const UINT32 DstFormat = formats[x];
832 printf(
"Testing destination color format %s\n", FreeRDPGetColorFormatName(DstFormat));
833 PROFILER_CREATE(rgbToYUV444,
"RGBToYUV444-generic")
834 PROFILER_CREATE(rgbToYUV444opt, "RGBToYUV444-optimized")
836 for (UINT32 cnt = 0; cnt < 10; cnt++)
838 PROFILER_ENTER(rgbToYUV444opt)
839 rc = fkt(rgb, DstFormat, stride, luma, yuv_step, chroma, yuv_step, &roi);
840 PROFILER_EXIT(rgbToYUV444opt)
842 if (rc != PRIMITIVES_SUCCESS)
846 PROFILER_PRINT_HEADER
847 PROFILER_PRINT(rgbToYUV444opt)
848 PROFILER_PRINT_FOOTER
850 if (!check_padding(rgb, size *
sizeof(UINT32), padding,
"rgb"))
856 if (!check_yuv420(luma, awidth, aheight, padding) ||
857 !check_yuv420(chroma, awidth, aheight, padding))
863 for (UINT32 cnt = 0; cnt < 10; cnt++)
865 PROFILER_ENTER(rgbToYUV444)
866 rc = gen(rgb, DstFormat, stride, lumaGeneric, yuv_step, chromaGeneric, yuv_step, &roi);
867 PROFILER_EXIT(rgbToYUV444)
869 if (rc != PRIMITIVES_SUCCESS)
873 PROFILER_PRINT_HEADER
874 PROFILER_PRINT(rgbToYUV444)
875 PROFILER_PRINT_FOOTER
877 if (!check_padding(rgb, size *
sizeof(UINT32), padding,
"rgb"))
883 if (!check_yuv420(lumaGeneric, awidth, aheight, padding) ||
884 !check_yuv420(chromaGeneric, awidth, aheight, padding))
890 if (!compare_yuv420(luma, lumaGeneric, awidth, aheight, padding) ||
891 !compare_yuv420(chroma, chromaGeneric, awidth, aheight, padding))
898 PROFILER_FREE(rgbToYUV444)
899 PROFILER_FREE(rgbToYUV444opt)
901 if (rc != PRIMITIVES_SUCCESS)
907 printf(
"[%s][version %u] run %s.\n", __func__, (
unsigned)version, (res) ?
"SUCCESS" :
"FAILED");
908 free_padding(rgb, padding);
909 free_yuv420(luma, padding);
910 free_yuv420(chroma, padding);
911 free_yuv420(lumaGeneric, padding);
912 free_yuv420(chromaGeneric, padding);
919 for (UINT32 type = PRIMITIVES_PURE_SOFT; type <= PRIMITIVES_AUTODETECT; type++)
924 printf(
"primitives type %d not supported\n", type);
928 for (UINT32 x = 0; x < 5; x++)
931 printf(
"-------------------- GENERIC ------------------------\n");
933 if (!TestPrimitiveYUV(prims, roi, TRUE))
936 printf(
"---------------------- END --------------------------\n");
937 printf(
"-------------------- GENERIC ------------------------\n");
939 if (!TestPrimitiveYUV(prims, roi, FALSE))
942 printf(
"---------------------- END --------------------------\n");
943 printf(
"-------------------- GENERIC ------------------------\n");
945 if (!TestPrimitiveYUVCombine(prims, roi))
948 printf(
"---------------------- END --------------------------\n");
949 printf(
"-------------------- GENERIC ------------------------\n");
951 if (!TestPrimitiveRgbToLumaChroma(prims, roi, 1))
954 printf(
"---------------------- END --------------------------\n");
955 printf(
"-------------------- GENERIC ------------------------\n");
957 if (!TestPrimitiveRgbToLumaChroma(prims, roi, 2))
960 printf(
"---------------------- END --------------------------\n");
965 printf(
"[%s] run %s.\n", __func__, (rc) ?
"SUCCESS" :
"FAILED");
969static void free_yuv(BYTE* yuv[3])
971 for (
size_t x = 0; x < 3; x++)
978static BOOL allocate_yuv(BYTE* yuv[3],
prim_size_t roi)
980 yuv[0] = calloc(roi.width, roi.height);
981 yuv[1] = calloc(roi.width, roi.height);
982 yuv[2] = calloc(roi.width, roi.height);
984 if (!yuv[0] || !yuv[1] || !yuv[2])
990 winpr_RAND(yuv[0], 1ULL * roi.width * roi.height);
991 winpr_RAND(yuv[1], 1ULL * roi.width * roi.height);
992 winpr_RAND(yuv[2], 1ULL * roi.width * roi.height);
996static BOOL yuv444_to_rgb(BYTE* rgb,
size_t stride,
const BYTE* yuv[3],
const UINT32 yuvStep[3],
999 for (
size_t y = 0; y < roi.height; y++)
1001 const BYTE* yline[3] = {
1002 yuv[0] + y * roi.width,
1003 yuv[1] + y * roi.width,
1004 yuv[2] + y * roi.width,
1006 BYTE* line = &rgb[y * stride];
1008 for (
size_t x = 0; x < roi.width; x++)
1010 const BYTE Y = yline[0][x];
1011 const BYTE U = yline[1][x];
1012 const BYTE V = yline[2][x];
1014 writeYUVPixel(&line[x * 4], PIXEL_FORMAT_BGRX32, Y, U, V, writePixelBGRX);
1022static BOOL compare_yuv444_to_rgb(
prim_size_t roi, DWORD type)
1025 const UINT32 format = PIXEL_FORMAT_BGRA32;
1026 BYTE* yuv[3] = { 0 };
1027 const UINT32 yuvStep[3] = { roi.width, roi.width, roi.width };
1028 const size_t stride = 4ULL * roi.width;
1033 printf(
"primitives type %" PRIu32
" not supported, skipping\n", type);
1037 BYTE* rgb1 = calloc(roi.height, stride);
1038 BYTE* rgb2 = calloc(roi.height, stride);
1040 primitives_t* soft = primitives_get_by_type(PRIMITIVES_PURE_SOFT);
1043 if (!allocate_yuv(yuv, roi) || !rgb1 || !rgb2)
1046 const BYTE* cyuv[] = { yuv[0], yuv[1], yuv[2] };
1047 if (soft->YUV444ToRGB_8u_P3AC4R(cyuv, yuvStep, rgb1, stride, format, &roi) !=
1050 if (prims->YUV444ToRGB_8u_P3AC4R(cyuv, yuvStep, rgb2, stride, format, &roi) !=
1054 for (
size_t y = 0; y < roi.height; y++)
1056 const BYTE* yline[3] = {
1057 yuv[0] + y * roi.width,
1058 yuv[1] + y * roi.width,
1059 yuv[2] + y * roi.width,
1061 const BYTE* line1 = &rgb1[y * stride];
1062 const BYTE* line2 = &rgb2[y * stride];
1064 for (
size_t x = 0; x < roi.width; x++)
1066 const int Y = yline[0][x];
1067 const int U = yline[1][x];
1068 const int V = yline[2][x];
1069 const UINT32 color1 = FreeRDPReadColor(&line1[x * 4], format);
1070 const UINT32 color2 = FreeRDPReadColor(&line2[x * 4], format);
1075 FreeRDPSplitColor(color1, format, &r1, &g1, &b1, NULL, NULL);
1080 FreeRDPSplitColor(color2, format, &r2, &g2, &b2, NULL, NULL);
1082 const int dr12 = abs(r1 - r2);
1083 const int dg12 = abs(g1 - g2);
1084 const int db12 = abs(b1 - b2);
1086 if ((dr12 != 0) || (dg12 != 0) || (db12 != 0))
1089 printf(
"\tdiff 1/2: yuv {%d, %d, %d}, rgb {%d, %d, %d}\n", Y, U, V, dr12, dg12,
1094 if ((dr12 > 0) || (dg12 > 0) || (db12 > 0))
1096 (void)fprintf(stderr,
1097 "[%" PRIuz
"x%" PRIuz
1098 "] generic and optimized data mismatch: r[0x%" PRIx8
"|0x%" PRIx8
1099 "] g[0x%" PRIx8
"|0x%" PRIx8
"] b[0x%" PRIx8
"|0x%" PRIx8
"]\n",
1100 x, y, r1, r2, g1, g2, b1, b2);
1101 (void)fprintf(stderr,
"roi: %dx%d\n", roi.width, roi.height);
1102 winpr_HexDump(
"y0", WLOG_INFO, &yline[0][x], 16);
1103 winpr_HexDump(
"y1", WLOG_INFO, &yline[0][x + roi.width], 16);
1104 winpr_HexDump(
"u0", WLOG_INFO, &yline[1][x], 16);
1105 winpr_HexDump(
"u1", WLOG_INFO, &yline[1][x + roi.width], 16);
1106 winpr_HexDump(
"v0", WLOG_INFO, &yline[2][x], 16);
1107 winpr_HexDump(
"v1", WLOG_INFO, &yline[2][x + roi.width], 16);
1108 winpr_HexDump(
"foo1", WLOG_INFO, &line1[x * 4], 16);
1109 winpr_HexDump(
"foo2", WLOG_INFO, &line2[x * 4], 16);
1117 printf(
"%s finished with %s\n", __func__, rc ?
"SUCCESS" :
"FAILURE");
1128static BOOL compare_rgb_to_yuv444(
prim_size_t roi, DWORD type)
1131 const UINT32 format = PIXEL_FORMAT_BGRA32;
1132 const size_t stride = 4ULL * roi.width;
1133 const UINT32 yuvStep[] = { roi.width, roi.width, roi.width };
1134 BYTE* yuv1[3] = { 0 };
1135 BYTE* yuv2[3] = { 0 };
1140 printf(
"primitives type %" PRIu32
" not supported, skipping\n", type);
1144 BYTE* rgb = calloc(roi.height, stride);
1146 primitives_t* soft = primitives_get_by_type(PRIMITIVES_PURE_SOFT);
1150 if (!allocate_yuv(yuv1, roi) || !allocate_yuv(yuv2, roi))
1153 if (soft->RGBToYUV444_8u_P3AC4R(rgb, format, stride, yuv1, yuvStep, &roi) != PRIMITIVES_SUCCESS)
1155 if (prims->RGBToYUV444_8u_P3AC4R(rgb, format, stride, yuv2, yuvStep, &roi) !=
1159 for (
size_t y = 0; y < roi.height; y++)
1161 const BYTE* yline1[3] = {
1162 yuv1[0] + y * roi.width,
1163 yuv1[1] + y * roi.width,
1164 yuv1[2] + y * roi.width,
1166 const BYTE* yline2[3] = {
1167 yuv2[0] + y * roi.width,
1168 yuv2[1] + y * roi.width,
1169 yuv2[2] + y * roi.width,
1172 for (
size_t x = 0; x < ARRAYSIZE(yline1); x++)
1174 if (memcmp(yline1[x], yline2[x], yuvStep[x]) != 0)
1176 (void)fprintf(stderr,
"[%s] compare failed in line %" PRIuz, __func__, x);
1184 printf(
"%s finished with %s\n", __func__, rc ?
"SUCCESS" :
"FAILURE");
1195static BOOL compare_yuv420_to_rgb(
prim_size_t roi, DWORD type)
1198 const UINT32 format = PIXEL_FORMAT_BGRA32;
1199 BYTE* yuv[3] = { 0 };
1200 const UINT32 yuvStep[3] = { roi.width, roi.width / 2, roi.width / 2 };
1201 const size_t stride = 4ULL * roi.width;
1206 printf(
"primitives type %" PRIu32
" not supported, skipping\n", type);
1210 BYTE* rgb1 = calloc(roi.height, stride);
1211 BYTE* rgb2 = calloc(roi.height, stride);
1213 primitives_t* soft = primitives_get_by_type(PRIMITIVES_PURE_SOFT);
1216 if (!allocate_yuv(yuv, roi) || !rgb1 || !rgb2)
1219 const BYTE* cyuv[3] = { yuv[0], yuv[1], yuv[2] };
1220 if (soft->YUV420ToRGB_8u_P3AC4R(cyuv, yuvStep, rgb1, stride, format, &roi) !=
1223 if (prims->YUV420ToRGB_8u_P3AC4R(cyuv, yuvStep, rgb2, stride, format, &roi) !=
1227 for (
size_t y = 0; y < roi.height; y++)
1229 const BYTE* yline[3] = {
1230 yuv[0] + y * yuvStep[0],
1231 yuv[1] + y * yuvStep[1],
1232 yuv[2] + y * yuvStep[2],
1234 const BYTE* line1 = &rgb1[y * stride];
1235 const BYTE* line2 = &rgb2[y * stride];
1237 for (
size_t x = 0; x < roi.width; x++)
1239 const int Y = yline[0][x];
1240 const int U = yline[1][x / 2];
1241 const int V = yline[2][x / 2];
1242 const UINT32 color1 = FreeRDPReadColor(&line1[x * 4], format);
1243 const UINT32 color2 = FreeRDPReadColor(&line2[x * 4], format);
1248 FreeRDPSplitColor(color1, format, &r1, &g1, &b1, NULL, NULL);
1253 FreeRDPSplitColor(color2, format, &r2, &g2, &b2, NULL, NULL);
1255 const int dr12 = abs(r1 - r2);
1256 const int dg12 = abs(g1 - g2);
1257 const int db12 = abs(b1 - b2);
1259 if ((dr12 != 0) || (dg12 != 0) || (db12 != 0))
1262 printf(
"\tdiff 1/2: yuv {%d, %d, %d}, rgb {%d, %d, %d}\n", Y, U, V, dr12, dg12,
1267 if ((dr12 > 0) || (dg12 > 0) || (db12 > 0))
1269 printf(
"[%s] failed: r[%" PRIx8
"|%" PRIx8
"] g[%" PRIx8
"|%" PRIx8
"] b[%" PRIx8
1271 __func__, r1, r2, g1, g2, b1, b2);
1279 printf(
"%s finished with %s\n", __func__, rc ?
"SUCCESS" :
"FAILURE");
1287static BOOL similarYUV(
const BYTE* line1,
const BYTE* line2,
size_t len)
1289 for (
size_t x = 0; x < len; x++)
1291 const int a = line1[x];
1292 const int b = line2[x];
1293 const int diff = abs(a - b);
1301static int similarY(
const BYTE* a,
const BYTE* b,
size_t size,
size_t type)
1308 for (
size_t x = 0; x < size; x++)
1310 const int ba = a[x];
1311 const int bb = b[x];
1312 const int diff = abs(ba - bb);
1319 return memcmp(a, b, size);
1325static BOOL compare_rgb_to_yuv420(
prim_size_t roi, DWORD type)
1328 const UINT32 format = PIXEL_FORMAT_BGRA32;
1329 const size_t stride = 4ULL * roi.width;
1330 const UINT32 yuvStep[] = { roi.width, roi.width / 2, roi.width / 2 };
1331 BYTE* yuv1[3] = { 0 };
1332 BYTE* yuv2[3] = { 0 };
1337 printf(
"primitives type %" PRIu32
" not supported, skipping\n", type);
1341 BYTE* rgb = calloc(roi.height, stride);
1342 BYTE* rgbcopy = calloc(roi.height, stride);
1344 primitives_t* soft = primitives_get_by_type(PRIMITIVES_PURE_SOFT);
1345 if (!soft || !rgb || !rgbcopy)
1348 winpr_RAND(rgb, roi.height * stride);
1349 memcpy(rgbcopy, rgb, roi.height * stride);
1351 if (!allocate_yuv(yuv1, roi) || !allocate_yuv(yuv2, roi))
1354 if (soft->RGBToYUV420_8u_P3AC4R(rgb, format, stride, yuv1, yuvStep, &roi) != PRIMITIVES_SUCCESS)
1356 if (memcmp(rgb, rgbcopy, roi.height * stride) != 0)
1358 if (prims->RGBToYUV420_8u_P3AC4R(rgb, format, stride, yuv2, yuvStep, &roi) !=
1362 for (
size_t y = 0; y < roi.height; y++)
1365 if (((y + 1) >= roi.height) && ((y % 2) == 0))
1368 const BYTE* yline1[3] = {
1369 &yuv1[0][y * yuvStep[0]],
1370 &yuv1[1][(y / 2) * yuvStep[1]],
1371 &yuv1[2][(y / 2) * yuvStep[2]],
1373 const BYTE* yline2[3] = {
1374 &yuv2[0][y * yuvStep[0]],
1375 &yuv2[1][(y / 2) * yuvStep[1]],
1376 &yuv2[2][(y / 2) * yuvStep[2]],
1379 for (
size_t x = 0; x < ARRAYSIZE(yline1); x++)
1381 if (similarY(yline1[x], yline2[x], yuvStep[x], x) != 0)
1383 (void)fprintf(stderr,
1384 "[%s] compare failed in component %" PRIuz
", line %" PRIuz
"\n",
1386 (void)fprintf(stderr,
"[%s] roi %" PRIu32
"x%" PRIu32
"\n", __func__, roi.width,
1388 winpr_HexDump(TAG, WLOG_WARN, yline1[x], yuvStep[x]);
1389 winpr_HexDump(TAG, WLOG_WARN, yline2[x], yuvStep[x]);
1390 winpr_HexDump(TAG, WLOG_WARN, &rgb[y * stride], stride);
1398 printf(
"%s finished with %s\n", __func__, rc ?
"SUCCESS" :
"FAILURE");
1407int TestPrimitivesYUV(
int argc,
char* argv[])
1409 BOOL large = (argc > 1);
1418 char* str = argv[1];
1419 char* ptr = strchr(str,
'x');
1425 roi.width = strtoul(str, NULL, 0);
1427 roi.height = strtoul(str, NULL, 0);
1438 get_size(large, &roi.width, &roi.height);
1440 prim_test_setup(FALSE);
1442 for (UINT32 type = PRIMITIVES_PURE_SOFT; type <= PRIMITIVES_AUTODETECT; type++)
1444 if (!compare_yuv444_to_rgb(roi, type))
1446 if (!compare_rgb_to_yuv444(roi, type))
1449 if (!compare_yuv420_to_rgb(roi, type))
1451 if (!compare_rgb_to_yuv420(roi, type))
1455 if (!run_tests(roi))
1460 printf(
"[%s] finished, status %s [%d]\n", __func__, (rc == 0) ?
"SUCCESS" :
"FAILURE", rc);