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,
nullptr);
73 FreeRDPSplitColor(dColor, format, &dR, &dG, &dB, &dA,
nullptr);
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 BOOL get_size(BOOL large, UINT32* width, UINT32* height)
126 UINT32 shift = large ? 8 : 1;
127 if (winpr_RAND(width,
sizeof(*width)) < 0)
129 if (winpr_RAND(height,
sizeof(*height)) < 0)
131 *width = (*width % 64 + 1) << shift;
132 *height = (*height % 64 + 1);
136static BOOL check_padding(
const BYTE* psrc,
size_t size,
size_t padding,
const char* buffer)
139 const BYTE* src =
nullptr;
140 const BYTE* esrc =
nullptr;
141 size_t halfPad = (padding + 1) / 2;
146 src = psrc - halfPad;
147 esrc = src + size + halfPad;
149 for (
size_t x = 0; x < halfPad; x++)
151 const BYTE s = *src++;
152 const BYTE d = *esrc++;
158 while ((x < halfPad) && (*esrc++ !=
'A'))
161 (void)fprintf(stderr,
162 "Buffer underflow detected %02" PRIx8
" != %02X %s [%" PRIuz
"-%" PRIuz
164 d,
'A', buffer, start, x);
172 while ((x < halfPad) && (*esrc++ !=
'A'))
175 (void)fprintf(stderr,
176 "Buffer overflow detected %02" PRIx8
" != %02X %s [%" PRIuz
"-%" PRIuz
178 d,
'A', buffer, start, x);
186static void* set_padding(
size_t size,
size_t padding)
188 size_t halfPad = (padding + 1) / 2;
189 BYTE* psrc =
nullptr;
190 BYTE* src = winpr_aligned_malloc(size + 2 * halfPad, 16);
195 memset(&src[0],
'A', halfPad);
196 memset(&src[halfPad], PADDING_FILL_VALUE, size);
197 memset(&src[halfPad + size],
'A', halfPad);
198 psrc = &src[halfPad];
200 if (!check_padding(psrc, size, padding,
"init"))
202 winpr_aligned_free(src);
209static void free_padding(
void* src,
size_t padding)
216 ptr = ((BYTE*)src) - (padding + 1) / 2;
217 winpr_aligned_free(ptr);
232 BYTE* luma[3] = WINPR_C_ARRAY_INIT;
233 BYTE* chroma[3] = WINPR_C_ARRAY_INIT;
234 BYTE* yuv[3] = WINPR_C_ARRAY_INIT;
235 BYTE* pmain[3] = WINPR_C_ARRAY_INIT;
236 BYTE* paux[3] = WINPR_C_ARRAY_INIT;
237 UINT32 lumaStride[3] = WINPR_C_ARRAY_INIT;
238 UINT32 chromaStride[3] = WINPR_C_ARRAY_INIT;
239 UINT32 yuvStride[3] = WINPR_C_ARRAY_INIT;
240 const size_t padding = 10000;
242 PROFILER_DEFINE(yuvCombine)
243 PROFILER_DEFINE(yuvSplit)
249 awidth = roi.width + 16 - roi.width % 16;
250 aheight = roi.height + 16 - roi.height % 16;
251 (void)fprintf(stderr,
252 "Running YUVCombine on frame size %" PRIu32
"x%" PRIu32
" [%" PRIuz
"x%" PRIuz
254 roi.width, roi.height, awidth, aheight);
255 PROFILER_CREATE(yuvCombine,
"YUV420CombineToYUV444")
256 PROFILER_CREATE(yuvSplit, "YUV444SplitToYUV420")
259 rect.right = roi.width;
260 rect.bottom = roi.height;
262 if (!prims || !prims->YUV420CombineToYUV444)
265 for (UINT32 x = 0; x < 3; x++)
267 size_t halfStride = ((x > 0) ? awidth / 2 : awidth);
268 size_t size = aheight * awidth;
269 size_t halfSize = ((x > 0) ? halfStride * aheight / 2 : awidth * aheight);
270 yuvStride[x] = awidth;
272 if (!(yuv[x] = set_padding(size, padding)))
275 lumaStride[x] = halfStride;
277 if (!(luma[x] = set_padding(halfSize, padding)))
280 if (!(pmain[x] = set_padding(halfSize, padding)))
283 chromaStride[x] = halfStride;
285 if (!(chroma[x] = set_padding(halfSize, padding)))
288 if (!(paux[x] = set_padding(halfSize, padding)))
291 memset(luma[x], WINPR_ASSERTING_INT_CAST(
int, 0xAB + 3 * x), halfSize);
292 memset(chroma[x], WINPR_ASSERTING_INT_CAST(
int, 0x80 + 2 * x), halfSize);
294 if (!check_padding(luma[x], halfSize, padding,
"luma"))
297 if (!check_padding(chroma[x], halfSize, padding,
"chroma"))
300 if (!check_padding(pmain[x], halfSize, padding,
"main"))
303 if (!check_padding(paux[x], halfSize, padding,
"aux"))
306 if (!check_padding(yuv[x], size, padding,
"yuv"))
310 PROFILER_ENTER(yuvCombine)
313 if (prims->YUV420CombineToYUV444(AVC444_LUMA, cnv.cpv, lumaStride, roi.width, roi.height, yuv,
314 yuvStride, &rect) != PRIMITIVES_SUCCESS)
316 PROFILER_EXIT(yuvCombine)
321 if (prims->YUV420CombineToYUV444(AVC444_CHROMAv1, cnv.cpv, chromaStride, roi.width, roi.height,
322 yuv, yuvStride, &rect) != PRIMITIVES_SUCCESS)
324 PROFILER_EXIT(yuvCombine)
328 PROFILER_EXIT(yuvCombine)
330 for (
size_t x = 0; x < 3; x++)
332 size_t halfStride = ((x > 0) ? awidth / 2 : awidth);
333 size_t size = 1ULL * aheight * awidth;
334 size_t halfSize = ((x > 0) ? halfStride * aheight / 2 : awidth * aheight);
336 if (!check_padding(luma[x], halfSize, padding,
"luma"))
339 if (!check_padding(chroma[x], halfSize, padding,
"chroma"))
342 if (!check_padding(yuv[x], size, padding,
"yuv"))
346 PROFILER_ENTER(yuvSplit)
349 if (prims->YUV444SplitToYUV420(cnv.cpv, yuvStride, pmain, lumaStride, paux, chromaStride,
350 &roi) != PRIMITIVES_SUCCESS)
352 PROFILER_EXIT(yuvSplit)
356 PROFILER_EXIT(yuvSplit)
358 for (UINT32 x = 0; x < 3; x++)
360 size_t halfStride = ((x > 0) ? awidth / 2 : awidth);
361 size_t size = aheight * awidth;
362 size_t halfSize = ((x > 0) ? halfStride * aheight / 2 : awidth * aheight);
364 if (!check_padding(pmain[x], halfSize, padding,
"main"))
367 if (!check_padding(paux[x], halfSize, padding,
"aux"))
370 if (!check_padding(yuv[x], size, padding,
"yuv"))
374 for (
size_t i = 0; i < 3; i++)
376 for (
size_t y = 0; y < roi.height; y++)
378 UINT32 w = roi.width;
379 UINT32 lstride = lumaStride[i];
380 UINT32 cstride = chromaStride[i];
384 w = (roi.width + 3) / 4;
386 if (roi.height > (roi.height + 1) / 2)
390 if (!similar(luma[i] + y * lstride, pmain[i] + y * lstride, w))
401 UINT32 rem = roi.height % 16;
403 if (y > roi.height - rem)
407 if (!similar(chroma[i] + y * cstride, paux[i] + y * cstride, w))
412 PROFILER_PRINT_HEADER
413 PROFILER_PRINT(yuvSplit)
414 PROFILER_PRINT(yuvCombine)
415 PROFILER_PRINT_FOOTER
418 printf(
"[%s] run %s.\n", __func__, (rc) ?
"SUCCESS" :
"FAILED");
419 PROFILER_FREE(yuvCombine)
420 PROFILER_FREE(yuvSplit)
422 for (UINT32 x = 0; x < 3; x++)
424 free_padding(yuv[x], padding);
425 free_padding(luma[x], padding);
426 free_padding(chroma[x], padding);
427 free_padding(pmain[x], padding);
428 free_padding(paux[x], padding);
444 BYTE* yuv[3] = WINPR_C_ARRAY_INIT;
445 UINT32 yuv_step[3] = WINPR_C_ARRAY_INIT;
447 BYTE* rgb_dst =
nullptr;
451 size_t padding = 100ULL * 16ULL;
453 const UINT32 formats[] = { PIXEL_FORMAT_XRGB32, PIXEL_FORMAT_XBGR32, PIXEL_FORMAT_ARGB32,
454 PIXEL_FORMAT_ABGR32, PIXEL_FORMAT_RGBA32, PIXEL_FORMAT_RGBX32,
455 PIXEL_FORMAT_BGRA32, PIXEL_FORMAT_BGRX32 };
456 PROFILER_DEFINE(rgbToYUV420)
457 PROFILER_DEFINE(rgbToYUV444)
458 PROFILER_DEFINE(yuv420ToRGB)
459 PROFILER_DEFINE(yuv444ToRGB)
461 awidth = roi.width + 16 - roi.width % 16;
462 aheight = roi.height + 16 - roi.height % 16;
463 stride = 1ULL * awidth *
sizeof(UINT32);
464 size = 1ULL * awidth * aheight;
471 if (!prims || !prims->RGBToYUV444_8u_P3AC4R || !prims->YUV444ToRGB_8u_P3AC4R)
476 uvwidth = (awidth + 1) / 2;
477 uvsize = (aheight + 1) / 2 * uvwidth;
479 if (!prims || !prims->RGBToYUV420_8u_P3AC4R || !prims->YUV420ToRGB_8u_P3AC4R)
483 (void)fprintf(stderr,
"Running AVC%s on frame size %" PRIu32
"x%" PRIu32
"\n",
484 use444 ?
"444" :
"420", roi.width, roi.height);
487 if (!(rgb = set_padding(size *
sizeof(UINT32), padding)))
490 if (!(rgb_dst = set_padding(size *
sizeof(UINT32), padding)))
493 if (!(yuv[0] = set_padding(size, padding)))
496 if (!(yuv[1] = set_padding(uvsize, padding)))
499 if (!(yuv[2] = set_padding(uvsize, padding)))
502 for (
size_t y = 0; y < roi.height; y++)
504 BYTE* line = &rgb[y * stride];
505 if (winpr_RAND(line, stride) < 0)
509 yuv_step[0] = awidth;
510 yuv_step[1] = uvwidth;
511 yuv_step[2] = uvwidth;
513 for (UINT32 x = 0; x < ARRAYSIZE(formats); x++)
516 const UINT32 DstFormat = formats[x];
517 printf(
"Testing destination color format %s\n", FreeRDPGetColorFormatName(DstFormat));
518 memset(rgb_dst, PADDING_FILL_VALUE, size *
sizeof(UINT32));
520 PROFILER_CREATE(rgbToYUV420,
"RGBToYUV420")
521 PROFILER_CREATE(rgbToYUV444, "RGBToYUV444")
522 PROFILER_CREATE(yuv420ToRGB, "YUV420ToRGB")
523 PROFILER_CREATE(yuv444ToRGB, "YUV444ToRGB")
527 PROFILER_ENTER(rgbToYUV444)
528 rc = prims->RGBToYUV444_8u_P3AC4R(rgb, DstFormat, stride, yuv, yuv_step, &roi);
529 PROFILER_EXIT(rgbToYUV444)
531 if (rc != PRIMITIVES_SUCCESS)
534 PROFILER_PRINT_HEADER
535 PROFILER_PRINT(rgbToYUV444)
536 PROFILER_PRINT_FOOTER
540 PROFILER_ENTER(rgbToYUV420)
541 rc = prims->RGBToYUV420_8u_P3AC4R(rgb, DstFormat, stride, yuv, yuv_step, &roi);
542 PROFILER_EXIT(rgbToYUV420)
544 if (rc != PRIMITIVES_SUCCESS)
547 PROFILER_PRINT_HEADER
548 PROFILER_PRINT(rgbToYUV420)
549 PROFILER_PRINT_FOOTER
552 if (!check_padding(rgb, size *
sizeof(UINT32), padding,
"rgb"))
558 if ((!check_padding(yuv[0], size, padding,
"Y")) ||
559 (!check_padding(yuv[1], uvsize, padding,
"U")) ||
560 (!check_padding(yuv[2], uvsize, padding,
"V")))
569 PROFILER_ENTER(yuv444ToRGB)
570 rc = prims->YUV444ToRGB_8u_P3AC4R(cnv.cpv, yuv_step, rgb_dst, stride, DstFormat, &roi);
571 PROFILER_EXIT(yuv444ToRGB)
573 if (rc != PRIMITIVES_SUCCESS)
577 PROFILER_EXIT(yuv444ToRGB)
578 PROFILER_PRINT_HEADER
579 PROFILER_PRINT(yuv444ToRGB)
580 PROFILER_PRINT_FOOTER
582 if (rc != PRIMITIVES_SUCCESS)
587 PROFILER_ENTER(yuv420ToRGB)
589 if (prims->YUV420ToRGB_8u_P3AC4R(cnv.cpv, yuv_step, rgb_dst, stride, DstFormat, &roi) !=
592 PROFILER_EXIT(yuv420ToRGB)
596 PROFILER_EXIT(yuv420ToRGB)
597 PROFILER_PRINT_HEADER
598 PROFILER_PRINT(yuv420ToRGB)
599 PROFILER_PRINT_FOOTER
602 if (!check_padding(rgb_dst, size *
sizeof(UINT32), padding,
"rgb dst"))
605 if ((!check_padding(yuv[0], size, padding,
"Y")) ||
606 (!check_padding(yuv[1], uvsize, padding,
"U")) ||
607 (!check_padding(yuv[2], uvsize, padding,
"V")))
611 for (
size_t y = 0; y < roi.height; y++)
613 BYTE* srgb = &rgb[y * stride];
614 BYTE* drgb = &rgb_dst[y * stride];
616 if (!similarRGB(y, srgb, drgb, roi.width, DstFormat, use444))
622 PROFILER_FREE(rgbToYUV420)
623 PROFILER_FREE(rgbToYUV444)
624 PROFILER_FREE(yuv420ToRGB)
625 PROFILER_FREE(yuv444ToRGB)
630 printf(
"[%s] run %s.\n", __func__, (res) ?
"SUCCESS" :
"FAILED");
631 free_padding(rgb, padding);
632 free_padding(rgb_dst, padding);
633 free_padding(yuv[0], padding);
634 free_padding(yuv[1], padding);
635 free_padding(yuv[2], padding);
639static BOOL allocate_yuv420(BYTE** planes, UINT32 width, UINT32 height, UINT32 padding)
641 const size_t size = 1ULL * width * height;
642 const size_t uvwidth = (1ULL + width) / 2;
643 const size_t uvsize = (1ULL + height) / 2 * uvwidth;
645 if (!(planes[0] = set_padding(size, padding)))
648 if (!(planes[1] = set_padding(uvsize, padding)))
651 if (!(planes[2] = set_padding(uvsize, padding)))
656 free_padding(planes[0], padding);
657 free_padding(planes[1], padding);
658 free_padding(planes[2], padding);
662static void free_yuv420(BYTE** planes, UINT32 padding)
667 free_padding(planes[0], padding);
668 free_padding(planes[1], padding);
669 free_padding(planes[2], padding);
675static BOOL check_yuv420(BYTE** planes, UINT32 width, UINT32 height, UINT32 padding)
677 const size_t size = 1ULL * width * height;
678 const size_t uvwidth = (width + 1) / 2;
679 const size_t uvsize = (height + 1) / 2 * uvwidth;
680 const BOOL yOk = check_padding(planes[0], size, padding,
"Y");
681 const BOOL uOk = check_padding(planes[1], uvsize, padding,
"U");
682 const BOOL vOk = check_padding(planes[2], uvsize, padding,
"V");
683 return (yOk && uOk && vOk);
686static BOOL check_for_mismatches(
const BYTE* planeA,
const BYTE* planeB, UINT32 size)
690 for (UINT32 x = 0; x < size; x++)
692 const BYTE a = planeA[x];
693 const BYTE b = planeB[x];
695 if (fabsf((
float)a - (
float)b) > 2.0f)
698 (void)fprintf(stderr,
"[%08x] %02x != %02x\n", x, a, b);
705static BOOL compare_yuv420(BYTE** planesA, BYTE** planesB, UINT32 width, UINT32 height,
709 const size_t size = 1ULL * width * height;
710 const size_t uvwidth = (1ULL * width + 1) / 2;
711 const size_t uvsize = (1ULL * height + 1) / 2 * uvwidth;
713 if (check_for_mismatches(planesA[0], planesB[0], size))
715 (void)fprintf(stderr,
"Mismatch in Y planes!\n");
719 if (check_for_mismatches(planesA[1], planesB[1], uvsize))
721 (void)fprintf(stderr,
"Mismatch in U planes!\n");
725 if (check_for_mismatches(planesA[2], planesB[2], uvsize))
727 (void)fprintf(stderr,
"Mismatch in V planes!\n");
734static UINT32 prand(UINT32 max)
739 if (winpr_RAND(&tmp,
sizeof(tmp)) < 0)
741 (void)fprintf(stderr,
"winpr_RAND failed, retry...\n");
745 return tmp % (max - 1) + 1;
753 BYTE* luma[3] = WINPR_C_ARRAY_INIT;
754 BYTE* chroma[3] = WINPR_C_ARRAY_INIT;
755 BYTE* lumaGeneric[3] = WINPR_C_ARRAY_INIT;
756 BYTE* chromaGeneric[3] = WINPR_C_ARRAY_INIT;
761 const size_t padding = 0x1000;
763 fn_RGBToAVC444YUV_t fkt =
nullptr;
764 fn_RGBToAVC444YUV_t gen =
nullptr;
765 const UINT32 formats[] = { PIXEL_FORMAT_XRGB32, PIXEL_FORMAT_XBGR32, PIXEL_FORMAT_ARGB32,
766 PIXEL_FORMAT_ABGR32, PIXEL_FORMAT_RGBA32, PIXEL_FORMAT_RGBX32,
767 PIXEL_FORMAT_BGRA32, PIXEL_FORMAT_BGRX32 };
768 PROFILER_DEFINE(rgbToYUV444)
769 PROFILER_DEFINE(rgbToYUV444opt)
773 if (awidth % 16 != 0)
774 awidth += 16 - roi.width % 16;
776 aheight = roi.height;
778 if (aheight % 16 != 0)
779 aheight += 16 - roi.height % 16;
781 stride = 1ULL * awidth *
sizeof(UINT32);
782 size = 1ULL * awidth * aheight;
783 uvwidth = 1ULL * (awidth + 1) / 2;
785 if (!prims || !generic)
791 fkt = prims->RGBToAVC444YUV;
792 gen =
generic->RGBToAVC444YUV;
796 fkt = prims->RGBToAVC444YUVv2;
797 gen =
generic->RGBToAVC444YUVv2;
807 (void)fprintf(stderr,
"Running AVC444 on frame size %" PRIu32
"x%" PRIu32
"\n", roi.width,
811 if (!(rgb = set_padding(size *
sizeof(UINT32), padding)))
814 if (!allocate_yuv420(luma, awidth, aheight, padding))
817 if (!allocate_yuv420(chroma, awidth, aheight, padding))
820 if (!allocate_yuv420(lumaGeneric, awidth, aheight, padding))
823 if (!allocate_yuv420(chromaGeneric, awidth, aheight, padding))
826 for (
size_t y = 0; y < roi.height; y++)
828 BYTE* line = &rgb[y * stride];
830 if (winpr_RAND(line, 4ULL * roi.width) < 0)
834 yuv_step[0] = awidth;
835 yuv_step[1] = uvwidth;
836 yuv_step[2] = uvwidth;
838 for (UINT32 x = 0; x < ARRAYSIZE(formats); x++)
841 const UINT32 DstFormat = formats[x];
842 printf(
"Testing destination color format %s\n", FreeRDPGetColorFormatName(DstFormat));
843 PROFILER_CREATE(rgbToYUV444,
"RGBToYUV444-generic")
844 PROFILER_CREATE(rgbToYUV444opt, "RGBToYUV444-optimized")
846 for (UINT32 cnt = 0; cnt < 10; cnt++)
848 PROFILER_ENTER(rgbToYUV444opt)
849 rc = fkt(rgb, DstFormat, stride, luma, yuv_step, chroma, yuv_step, &roi);
850 PROFILER_EXIT(rgbToYUV444opt)
852 if (rc != PRIMITIVES_SUCCESS)
856 PROFILER_PRINT_HEADER
857 PROFILER_PRINT(rgbToYUV444opt)
858 PROFILER_PRINT_FOOTER
860 if (!check_padding(rgb, size *
sizeof(UINT32), padding,
"rgb"))
866 if (!check_yuv420(luma, awidth, aheight, padding) ||
867 !check_yuv420(chroma, awidth, aheight, padding))
873 for (UINT32 cnt = 0; cnt < 10; cnt++)
875 PROFILER_ENTER(rgbToYUV444)
876 rc = gen(rgb, DstFormat, stride, lumaGeneric, yuv_step, chromaGeneric, yuv_step, &roi);
877 PROFILER_EXIT(rgbToYUV444)
879 if (rc != PRIMITIVES_SUCCESS)
883 PROFILER_PRINT_HEADER
884 PROFILER_PRINT(rgbToYUV444)
885 PROFILER_PRINT_FOOTER
887 if (!check_padding(rgb, size *
sizeof(UINT32), padding,
"rgb"))
893 if (!check_yuv420(lumaGeneric, awidth, aheight, padding) ||
894 !check_yuv420(chromaGeneric, awidth, aheight, padding))
900 if (!compare_yuv420(luma, lumaGeneric, awidth, aheight, padding) ||
901 !compare_yuv420(chroma, chromaGeneric, awidth, aheight, padding))
908 PROFILER_FREE(rgbToYUV444)
909 PROFILER_FREE(rgbToYUV444opt)
911 if (rc != PRIMITIVES_SUCCESS)
917 printf(
"[%s][version %u] run %s.\n", __func__, (
unsigned)version, (res) ?
"SUCCESS" :
"FAILED");
918 free_padding(rgb, padding);
919 free_yuv420(luma, padding);
920 free_yuv420(chroma, padding);
921 free_yuv420(lumaGeneric, padding);
922 free_yuv420(chromaGeneric, padding);
929 for (UINT32 type = PRIMITIVES_PURE_SOFT; type <= PRIMITIVES_AUTODETECT; type++)
934 printf(
"primitives type %d not supported\n", type);
938 for (UINT32 x = 0; x < 5; x++)
941 printf(
"-------------------- GENERIC ------------------------\n");
943 if (!TestPrimitiveYUV(prims, roi, TRUE))
946 printf(
"---------------------- END --------------------------\n");
947 printf(
"-------------------- GENERIC ------------------------\n");
949 if (!TestPrimitiveYUV(prims, roi, FALSE))
952 printf(
"---------------------- END --------------------------\n");
953 printf(
"-------------------- GENERIC ------------------------\n");
955 if (!TestPrimitiveYUVCombine(prims, roi))
958 printf(
"---------------------- END --------------------------\n");
959 printf(
"-------------------- GENERIC ------------------------\n");
961 if (!TestPrimitiveRgbToLumaChroma(prims, roi, 1))
964 printf(
"---------------------- END --------------------------\n");
965 printf(
"-------------------- GENERIC ------------------------\n");
967 if (!TestPrimitiveRgbToLumaChroma(prims, roi, 2))
970 printf(
"---------------------- END --------------------------\n");
975 printf(
"[%s] run %s.\n", __func__, (rc) ?
"SUCCESS" :
"FAILED");
979static void free_yuv(BYTE* yuv[3])
981 for (
size_t x = 0; x < 3; x++)
988static BOOL allocate_yuv(BYTE* yuv[3],
prim_size_t roi)
990 yuv[0] = calloc(roi.width, roi.height);
991 yuv[1] = calloc(roi.width, roi.height);
992 yuv[2] = calloc(roi.width, roi.height);
994 if (!yuv[0] || !yuv[1] || !yuv[2])
1000 if (winpr_RAND(yuv[0], 1ULL * roi.width * roi.height) < 0)
1002 if (winpr_RAND(yuv[1], 1ULL * roi.width * roi.height) < 0)
1004 if (winpr_RAND(yuv[2], 1ULL * roi.width * roi.height) < 0)
1009static BOOL yuv444_to_rgb(BYTE* rgb,
size_t stride,
const BYTE* yuv[3],
const UINT32 yuvStep[3],
1012 for (
size_t y = 0; y < roi.height; y++)
1014 const BYTE* yline[3] = {
1015 yuv[0] + y * roi.width,
1016 yuv[1] + y * roi.width,
1017 yuv[2] + y * roi.width,
1019 BYTE* line = &rgb[y * stride];
1021 for (
size_t x = 0; x < roi.width; x++)
1023 const BYTE Y = yline[0][x];
1024 const BYTE U = yline[1][x];
1025 const BYTE V = yline[2][x];
1027 writeYUVPixel(&line[x * 4], PIXEL_FORMAT_BGRX32, Y, U, V, writePixelBGRX);
1035static BOOL compare_yuv444_to_rgb(
prim_size_t roi, DWORD type)
1038 const UINT32 format = PIXEL_FORMAT_BGRA32;
1039 BYTE* yuv[3] = WINPR_C_ARRAY_INIT;
1040 const UINT32 yuvStep[3] = { roi.width, roi.width, roi.width };
1041 const size_t stride = 4ULL * roi.width;
1046 printf(
"primitives type %" PRIu32
" not supported, skipping\n", type);
1050 BYTE* rgb1 = calloc(roi.height, stride);
1051 BYTE* rgb2 = calloc(roi.height, stride);
1053 primitives_t* soft = primitives_get_by_type(PRIMITIVES_PURE_SOFT);
1056 if (!allocate_yuv(yuv, roi) || !rgb1 || !rgb2)
1059 const BYTE* cyuv[] = { yuv[0], yuv[1], yuv[2] };
1060 if (soft->YUV444ToRGB_8u_P3AC4R(cyuv, yuvStep, rgb1, stride, format, &roi) !=
1063 if (prims->YUV444ToRGB_8u_P3AC4R(cyuv, yuvStep, rgb2, stride, format, &roi) !=
1067 for (
size_t y = 0; y < roi.height; y++)
1069 const BYTE* yline[3] = {
1070 yuv[0] + y * roi.width,
1071 yuv[1] + y * roi.width,
1072 yuv[2] + y * roi.width,
1074 const BYTE* line1 = &rgb1[y * stride];
1075 const BYTE* line2 = &rgb2[y * stride];
1077 for (
size_t x = 0; x < roi.width; x++)
1079 const int Y = yline[0][x];
1080 const int U = yline[1][x];
1081 const int V = yline[2][x];
1082 const UINT32 color1 = FreeRDPReadColor(&line1[x * 4], format);
1083 const UINT32 color2 = FreeRDPReadColor(&line2[x * 4], format);
1088 FreeRDPSplitColor(color1, format, &r1, &g1, &b1,
nullptr,
nullptr);
1093 FreeRDPSplitColor(color2, format, &r2, &g2, &b2,
nullptr,
nullptr);
1095 const int dr12 = abs(r1 - r2);
1096 const int dg12 = abs(g1 - g2);
1097 const int db12 = abs(b1 - b2);
1099 if ((dr12 != 0) || (dg12 != 0) || (db12 != 0))
1102 printf(
"\tdiff 1/2: yuv {%d, %d, %d}, rgb {%d, %d, %d}\n", Y, U, V, dr12, dg12,
1107 if ((dr12 > 0) || (dg12 > 0) || (db12 > 0))
1109 (void)fprintf(stderr,
1110 "[%" PRIuz
"x%" PRIuz
1111 "] generic and optimized data mismatch: r[0x%" PRIx8
"|0x%" PRIx8
1112 "] g[0x%" PRIx8
"|0x%" PRIx8
"] b[0x%" PRIx8
"|0x%" PRIx8
"]\n",
1113 x, y, r1, r2, g1, g2, b1, b2);
1114 (void)fprintf(stderr,
"roi: %dx%d\n", roi.width, roi.height);
1115 winpr_HexDump(
"y0", WLOG_INFO, &yline[0][x], 16);
1116 winpr_HexDump(
"y1", WLOG_INFO, &yline[0][x + roi.width], 16);
1117 winpr_HexDump(
"u0", WLOG_INFO, &yline[1][x], 16);
1118 winpr_HexDump(
"u1", WLOG_INFO, &yline[1][x + roi.width], 16);
1119 winpr_HexDump(
"v0", WLOG_INFO, &yline[2][x], 16);
1120 winpr_HexDump(
"v1", WLOG_INFO, &yline[2][x + roi.width], 16);
1121 winpr_HexDump(
"foo1", WLOG_INFO, &line1[x * 4], 16);
1122 winpr_HexDump(
"foo2", WLOG_INFO, &line2[x * 4], 16);
1130 printf(
"%s finished with %s\n", __func__, rc ?
"SUCCESS" :
"FAILURE");
1141static BOOL compare_rgb_to_yuv444(
prim_size_t roi, DWORD type)
1144 const UINT32 format = PIXEL_FORMAT_BGRA32;
1145 const size_t stride = 4ULL * roi.width;
1146 const UINT32 yuvStep[] = { roi.width, roi.width, roi.width };
1147 BYTE* yuv1[3] = WINPR_C_ARRAY_INIT;
1148 BYTE* yuv2[3] = WINPR_C_ARRAY_INIT;
1153 printf(
"primitives type %" PRIu32
" not supported, skipping\n", type);
1157 BYTE* rgb = calloc(roi.height, stride);
1159 primitives_t* soft = primitives_get_by_type(PRIMITIVES_PURE_SOFT);
1163 if (!allocate_yuv(yuv1, roi) || !allocate_yuv(yuv2, roi))
1166 if (soft->RGBToYUV444_8u_P3AC4R(rgb, format, stride, yuv1, yuvStep, &roi) != PRIMITIVES_SUCCESS)
1168 if (prims->RGBToYUV444_8u_P3AC4R(rgb, format, stride, yuv2, yuvStep, &roi) !=
1172 for (
size_t y = 0; y < roi.height; y++)
1174 const BYTE* yline1[3] = {
1175 yuv1[0] + y * roi.width,
1176 yuv1[1] + y * roi.width,
1177 yuv1[2] + y * roi.width,
1179 const BYTE* yline2[3] = {
1180 yuv2[0] + y * roi.width,
1181 yuv2[1] + y * roi.width,
1182 yuv2[2] + y * roi.width,
1185 for (
size_t x = 0; x < ARRAYSIZE(yline1); x++)
1187 if (memcmp(yline1[x], yline2[x], yuvStep[x]) != 0)
1189 (void)fprintf(stderr,
"[%s] compare failed in line %" PRIuz, __func__, x);
1197 printf(
"%s finished with %s\n", __func__, rc ?
"SUCCESS" :
"FAILURE");
1208static BOOL compare_yuv420_to_rgb(
prim_size_t roi, DWORD type)
1211 const UINT32 format = PIXEL_FORMAT_BGRA32;
1212 BYTE* yuv[3] = WINPR_C_ARRAY_INIT;
1213 const UINT32 yuvStep[3] = { roi.width, roi.width / 2, roi.width / 2 };
1214 const size_t stride = 4ULL * roi.width;
1219 printf(
"primitives type %" PRIu32
" not supported, skipping\n", type);
1223 BYTE* rgb1 = calloc(roi.height, stride);
1224 BYTE* rgb2 = calloc(roi.height, stride);
1226 primitives_t* soft = primitives_get_by_type(PRIMITIVES_PURE_SOFT);
1229 if (!allocate_yuv(yuv, roi) || !rgb1 || !rgb2)
1232 const BYTE* cyuv[3] = { yuv[0], yuv[1], yuv[2] };
1233 if (soft->YUV420ToRGB_8u_P3AC4R(cyuv, yuvStep, rgb1, stride, format, &roi) !=
1236 if (prims->YUV420ToRGB_8u_P3AC4R(cyuv, yuvStep, rgb2, stride, format, &roi) !=
1240 for (
size_t y = 0; y < roi.height; y++)
1242 const BYTE* yline[3] = {
1243 yuv[0] + y * yuvStep[0],
1244 yuv[1] + y * yuvStep[1],
1245 yuv[2] + y * yuvStep[2],
1247 const BYTE* line1 = &rgb1[y * stride];
1248 const BYTE* line2 = &rgb2[y * stride];
1250 for (
size_t x = 0; x < roi.width; x++)
1252 const int Y = yline[0][x];
1253 const int U = yline[1][x / 2];
1254 const int V = yline[2][x / 2];
1255 const UINT32 color1 = FreeRDPReadColor(&line1[x * 4], format);
1256 const UINT32 color2 = FreeRDPReadColor(&line2[x * 4], format);
1261 FreeRDPSplitColor(color1, format, &r1, &g1, &b1,
nullptr,
nullptr);
1266 FreeRDPSplitColor(color2, format, &r2, &g2, &b2,
nullptr,
nullptr);
1268 const int dr12 = abs(r1 - r2);
1269 const int dg12 = abs(g1 - g2);
1270 const int db12 = abs(b1 - b2);
1272 if ((dr12 != 0) || (dg12 != 0) || (db12 != 0))
1275 printf(
"\tdiff 1/2: yuv {%d, %d, %d}, rgb {%d, %d, %d}\n", Y, U, V, dr12, dg12,
1280 if ((dr12 > 0) || (dg12 > 0) || (db12 > 0))
1282 printf(
"[%s] failed: r[%" PRIx8
"|%" PRIx8
"] g[%" PRIx8
"|%" PRIx8
"] b[%" PRIx8
1284 __func__, r1, r2, g1, g2, b1, b2);
1292 printf(
"%s finished with %s\n", __func__, rc ?
"SUCCESS" :
"FAILURE");
1300static BOOL similarYUV(
const BYTE* line1,
const BYTE* line2,
size_t len)
1302 for (
size_t x = 0; x < len; x++)
1304 const int a = line1[x];
1305 const int b = line2[x];
1306 const int diff = abs(a - b);
1312static int similarY(
const BYTE* a,
const BYTE* b,
size_t size,
size_t type)
1319 for (
size_t x = 0; x < size; x++)
1321 const int ba = a[x];
1322 const int bb = b[x];
1323 const int diff = abs(ba - bb);
1330 return memcmp(a, b, size);
1336static BOOL compare_rgb_to_yuv420(
prim_size_t roi, DWORD type)
1339 const UINT32 format = PIXEL_FORMAT_BGRA32;
1340 const size_t stride = 4ULL * roi.width;
1341 const UINT32 yuvStep[] = { roi.width, roi.width / 2, roi.width / 2 };
1342 BYTE* yuv1[3] = WINPR_C_ARRAY_INIT;
1343 BYTE* yuv2[3] = WINPR_C_ARRAY_INIT;
1348 printf(
"primitives type %" PRIu32
" not supported, skipping\n", type);
1352 BYTE* rgb = calloc(roi.height, stride);
1353 BYTE* rgbcopy = calloc(roi.height, stride);
1355 primitives_t* soft = primitives_get_by_type(PRIMITIVES_PURE_SOFT);
1356 if (!soft || !rgb || !rgbcopy)
1359 if (winpr_RAND(rgb, roi.height * stride) < 0)
1361 memcpy(rgbcopy, rgb, roi.height * stride);
1363 if (!allocate_yuv(yuv1, roi) || !allocate_yuv(yuv2, roi))
1366 if (soft->RGBToYUV420_8u_P3AC4R(rgb, format, stride, yuv1, yuvStep, &roi) != PRIMITIVES_SUCCESS)
1368 if (memcmp(rgb, rgbcopy, roi.height * stride) != 0)
1370 if (prims->RGBToYUV420_8u_P3AC4R(rgb, format, stride, yuv2, yuvStep, &roi) !=
1374 for (
size_t y = 0; y < roi.height; y++)
1377 if (((y + 1) >= roi.height) && ((y % 2) == 0))
1380 const BYTE* yline1[3] = {
1381 &yuv1[0][y * yuvStep[0]],
1382 &yuv1[1][(y / 2) * yuvStep[1]],
1383 &yuv1[2][(y / 2) * yuvStep[2]],
1385 const BYTE* yline2[3] = {
1386 &yuv2[0][y * yuvStep[0]],
1387 &yuv2[1][(y / 2) * yuvStep[1]],
1388 &yuv2[2][(y / 2) * yuvStep[2]],
1391 for (
size_t x = 0; x < ARRAYSIZE(yline1); x++)
1393 if (similarY(yline1[x], yline2[x], yuvStep[x], x) != 0)
1395 (void)fprintf(stderr,
1396 "[%s] compare failed in component %" PRIuz
", line %" PRIuz
"\n",
1398 (void)fprintf(stderr,
"[%s] roi %" PRIu32
"x%" PRIu32
"\n", __func__, roi.width,
1400 winpr_HexDump(TAG, WLOG_WARN, yline1[x], yuvStep[x]);
1401 winpr_HexDump(TAG, WLOG_WARN, yline2[x], yuvStep[x]);
1402 winpr_HexDump(TAG, WLOG_WARN, &rgb[y * stride], stride);
1410 printf(
"%s finished with %s\n", __func__, rc ?
"SUCCESS" :
"FAILURE");
1419int TestPrimitivesYUV(
int argc,
char* argv[])
1421 BOOL large = (argc > 1);
1430 char* str = argv[1];
1431 char* ptr = strchr(str,
'x');
1437 roi.width = strtoul(str,
nullptr, 0);
1439 roi.height = strtoul(str,
nullptr, 0);
1450 get_size(large, &roi.width, &roi.height);
1452 prim_test_setup(FALSE);
1454 for (UINT32 type = PRIMITIVES_PURE_SOFT; type <= PRIMITIVES_AUTODETECT; type++)
1456 if (!compare_yuv444_to_rgb(roi, type))
1458 if (!compare_rgb_to_yuv444(roi, type))
1461 if (!compare_yuv420_to_rgb(roi, type))
1463 if (!compare_rgb_to_yuv420(roi, type))
1467 if (!run_tests(roi))
1472 printf(
"[%s] finished, status %s [%d]\n", __func__, (rc == 0) ?
"SUCCESS" :
"FAILURE", rc);