7#include <winpr/sysinfo.h>
9#include <freerdp/freerdp.h>
10#include <freerdp/codec/color.h>
12#if defined(WITH_APPLE_VIDEOTOOLBOX)
13#include <CoreMedia/CoreMedia.h>
14#include <freerdp/codec/video.h>
19static const char* print_ns(UINT64 start, UINT64 end,
char* buffer,
size_t len)
23 const UINT64 diff = end - start;
24 const unsigned ns = diff % 1000;
25 const unsigned us = (diff / 1000) % 1000;
26 const unsigned ms = (diff / 1000000) % 1000;
27 const unsigned s = (diff / 1000000000ULL) % 1000;
28 (void)_snprintf(buffer, len,
"%03u %03u %03u %03uns", s, ms, us, ns);
33static BOOL testContextOptions(BOOL compressor, uint32_t width, uint32_t height)
36 const UINT64 start = winpr_GetUnixTimeNS();
38 H264_CONTEXT* h264 = h264_context_new(compressor);
41 (void)fprintf(stderr,
"[%s] h264_context_new failed\n", __func__);
47 H264_CONTEXT_OPTION opt;
50 const struct optpair_s optpair[] = { { H264_CONTEXT_OPTION_RATECONTROL, H264_RATECONTROL_VBR },
51 { H264_CONTEXT_OPTION_BITRATE, 2323 },
52 { H264_CONTEXT_OPTION_FRAMERATE, 23 },
53 { H264_CONTEXT_OPTION_QP, 21 },
54 { H264_CONTEXT_OPTION_USAGETYPE, 23 } };
55 for (
size_t x = 0; x < ARRAYSIZE(optpair); x++)
57 const struct optpair_s* cur = &optpair[x];
58 if (!h264_context_set_option(h264, cur->opt, cur->val))
61 "[%s] h264_context_set_option %" PRIuz
"{ %d, %" PRIu32
" } failed\n",
62 __func__, x, cur->opt, cur->val);
66 if (!h264_context_reset(h264, width, height))
68 (void)fprintf(stderr,
"[%s] h264_context_reset\n", __func__);
74 h264_context_free(h264);
75 const UINT64 end = winpr_GetUnixTimeNS();
77 char buffer[64] = WINPR_C_ARRAY_INIT;
78 printf(
"[%s] %" PRIu32
"x%" PRIu32
" took %s\n", __func__, width, height,
79 print_ns(start, end, buffer,
sizeof(buffer)));
83WINPR_ATTR_MALLOC(free, 1)
84static
void* allocRGB(uint32_t format, uint32_t width, uint32_t height, uint32_t* pstride)
86 const size_t bpp = FreeRDPGetBytesPerPixel(format);
87 const size_t stride = bpp * width + 32;
88 WINPR_ASSERT(pstride);
89 *pstride = WINPR_ASSERTING_INT_CAST(uint32_t, stride);
91 uint8_t* rgb = calloc(stride, height);
95 const UINT32 color = FreeRDPGetColor(format, 0x12, 0xaa, 0x63, 0x80);
96 for (
size_t y = 0; y < height; y++)
98 uint8_t* line = &rgb[y * stride];
99 for (
size_t x = 0; x < width; x++)
101 FreeRDPWriteColor(&line[bpp * x], format, color);
108#define printIfFail(line, offset, what, a, b) \
109 printIfFail_(__func__, __LINE__, (line), (offset), (what), (a), (b))
110static BOOL printIfFail_(
const char* fkt,
size_t fline,
size_t line,
size_t offset,
111 const char* what, BYTE a, BYTE b)
113 const double maxDiff = 8.0;
114 const double diffR = fabs(1.0 * a - b);
117 (void)fprintf(stderr,
118 "[%s:%" PRIuz
"]{%" PRIuz
"x%" PRIuz
"} %s diff %lf [0x%02" PRIx8
119 " - 0x%02" PRIx8
"]\n",
120 fkt, fline, offset, line, what, diffR, a, b);
127static BOOL compareRGBColors(
size_t line,
size_t offset, uint32_t colora, uint32_t colorb,
139 FreeRDPSplitColor(colora, format, &cR, &cG, &cB, &cA,
nullptr);
140 FreeRDPSplitColor(colorb, format, &dR, &dG, &dB, &dA,
nullptr);
142 if (!printIfFail(line, offset,
"red", cR, dR))
144 if (!printIfFail(line, offset,
"green", cG, dG))
146 if (!printIfFail(line, offset,
"blue", cB, dB))
148 if (FreeRDPColorHasAlpha(format))
150 if (!printIfFail(line, offset,
"alpha", cA, dA))
157static BOOL compareRGBLine(
size_t line,
const uint8_t* src,
const uint8_t* dst, uint32_t format,
160 const size_t bpp = FreeRDPGetBytesPerPixel(format);
161 for (
size_t x = 0; x < width; x++)
163 const UINT32 srccolor = FreeRDPReadColor(&src[x * bpp], format);
164 const UINT32 dstcolor = FreeRDPReadColor(&dst[x * bpp], format);
165 if (!compareRGBColors(line, x, srccolor, dstcolor, format))
172static BOOL compareRGB(
const uint8_t* src,
const uint8_t* dst, uint32_t format,
size_t width,
173 size_t stride,
size_t height)
178 const size_t bits = FreeRDPGetBitsPerPixel(format);
181 printf(
"skipping color compare for format %s, only 32bpp colors are checked\n",
182 FreeRDPGetColorFormatName(format));
186 for (
size_t y = 0; y < height; y++)
188 const uint8_t* csrc = &src[y * stride];
189 const uint8_t* cdst = &dst[y * stride];
190 if (!compareRGBLine(y, csrc, cdst, format, width))
197static BOOL testEncode(uint32_t format, uint32_t width, uint32_t height)
202 void* copy =
nullptr;
204 H264_CONTEXT* h264 = h264_context_new(TRUE);
205 H264_CONTEXT* h264dec = h264_context_new(FALSE);
206 if (!h264 || !h264dec)
208 (void)fprintf(stderr,
"[%s] encoder=%p, decoder=%p\n", __func__, (
void*)h264,
213 if (!h264_context_reset(h264, width, height))
215 (void)fprintf(stderr,
"[%s] h264_context_reset(encoder) failed\n", __func__);
218 if (!h264_context_reset(h264dec, width, height))
220 (void)fprintf(stderr,
"[%s] h264_context_reset(decoder) failed\n", __func__);
225 uint32_t ostride = 0;
226 src = allocRGB(format, width, height, &stride);
227 out = allocRGB(format, width, height, &ostride);
228 if (!src || !out || (stride < width) || (stride != ostride))
230 (void)fprintf(stderr,
231 "[%s] src=%p, out=%p, stride(%" PRIu32
") < width(%" PRIu32
232 "), stride != ostride(%" PRIu32
")\n",
233 __func__, src, out, stride, width, ostride);
237 copy = calloc(height, stride);
240 memcpy(copy, src, 1ull * height * stride);
242 const RECTANGLE_16 rect = { .left = 0, .top = 0, .right = width, .bottom = height };
243 uint32_t dstsize = 0;
244 uint8_t* dst =
nullptr;
246 avc420_compress(h264, src, format, stride, width, height, &rect, &dst, &dstsize, &meta);
249 (void)fprintf(stderr,
"[%s] avc420_compress failed: %d\n", __func__, res1);
252 if ((dstsize == 0) || !dst)
254 (void)fprintf(stderr,
"[%s] dstsize=%" PRIu32
", dst=%p\n", __func__, dstsize, (
void*)dst);
257 if (memcmp(copy, src, 1ull * height * stride) != 0)
259 (void)fprintf(stderr,
"[%s] avc420_compress modified source data!\n", __func__);
264 avc420_decompress(h264dec, dst, dstsize, out, format, stride, width, height, &rect, 1);
267 (void)fprintf(stderr,
"[%s] avc420_decompress failed: %d\n", __func__, res2);
271 rc = compareRGB(src, out, format, width, stride, height);
274 (void)fprintf(stderr,
"[%s] run failed\n", __func__);
275 h264_context_free(h264);
276 h264_context_free(h264dec);
277 free_h264_metablock(&meta);
285static BOOL testEncodeOffsetRegion(
void)
288 const UINT32 width = 192;
289 const UINT32 height = 128;
290 const UINT32 format = PIXEL_FORMAT_BGRA32;
291 const RECTANGLE_16 full = { .left = 0, .top = 0, .right = width, .bottom = height };
292 const RECTANGLE_16 region = { .left = 32, .top = 16, .right = 160, .bottom = 112 };
294 H264_CONTEXT* h264 = h264_context_new(TRUE);
296 BYTE* src = allocRGB(format, width, height, &stride);
300 if (!h264 || !src || !h264_context_reset(h264, width, height))
303 memset(src, 0, (
size_t)stride * height);
305 avc420_compress(h264, src, format, stride, width, height, &full, &dst, &dstSize, &meta);
309 (void)fprintf(stderr,
"[%s] first avc420_compress failed: %d\n", __func__, res);
312 free_h264_metablock(&meta);
314 for (UINT32 y = region.top; y < region.bottom; y++)
316 BYTE* row = &src[(size_t)y * stride + (
size_t)region.left * 4];
317 memset(row, 0xFF, (
size_t)(region.right - region.left) * 4);
323 avc420_compress(h264, src, format, stride, width, height, ®ion, &dst, &dstSize, &meta);
326 (void)fprintf(stderr,
"[%s] second avc420_compress failed: %d\n", __func__, res2);
329 if ((meta.numRegionRects == 0) || (meta.regionRects[0].left != region.left) ||
330 (meta.regionRects[0].top != region.top))
333 stderr,
"%s failed: first changed tile does not start at (%" PRIu16
", %" PRIu16
")\n",
334 __func__, region.left, region.top);
341 (void)fprintf(stderr,
"[%s] run failed\n", __func__);
342 h264_context_free(h264);
343 free_h264_metablock(&meta);
351static const BYTE solid_idr_64x64[] = {
352 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xc0, 0x0a, 0xd9, 0x04, 0x26, 0xc0, 0x44, 0x00,
353 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xc8, 0x3c, 0x48, 0x99, 0x20, 0x00,
354 0x00, 0x00, 0x01, 0x68, 0xcb, 0x83, 0xcb, 0x20, 0x00, 0x00, 0x01, 0x65, 0x88, 0x84,
355 0x0a, 0xf1, 0x18, 0xa0, 0x00, 0x22, 0x4b, 0x1c, 0x00, 0x04, 0x53, 0xa3, 0x80, 0x00,
356 0x85, 0x8c, 0x9c, 0x9c, 0x9d, 0x75, 0xd7, 0x5d, 0x75, 0xd7, 0x5d, 0x75, 0xd7, 0x5e
360static BOOL testDecode(BOOL separateParameters)
363 const UINT32 width = 64;
364 const UINT32 height = 64;
365 const UINT32 format = PIXEL_FORMAT_BGRX32;
366 const UINT32 stride = width * 4;
367 const RECTANGLE_16 rect = { .left = 0, .top = 0, .right = width, .bottom = height };
368 H264_CONTEXT* h264 = h264_context_new(FALSE);
369 BYTE* dst = calloc(stride, height);
371 if (!h264 || !dst || !h264_context_reset(h264, width, height))
375 const UINT32 offset = separateParameters ? 36 : 0;
376 if (separateParameters && (avc420_decompress(h264, solid_idr_64x64, offset, dst, format, stride,
377 width, height, &rect, 1) < 0))
382 avc420_decompress(h264, solid_idr_64x64 + offset,
sizeof(solid_idr_64x64) - offset, dst,
383 format, stride, width, height, &rect, 1);
386 (void)fprintf(stderr,
"[%s] avc420_decompress failed: %" PRId32
"\n", __func__, res);
390 for (UINT32 y = 0; y < height; y++)
392 for (UINT32 x = 0; x < width; x++)
397 const UINT32 color = FreeRDPReadColor(&dst[y * stride + x * 4], format);
398 FreeRDPSplitColor(color, format, &r, &g, &b,
nullptr,
nullptr);
399 if ((abs(r - 0x33) > 16) || (abs(g - 0x66) > 16) || (abs(b - 0xCC) > 16))
402 stderr,
"[%s] pixel %" PRIu32
",%" PRIu32
" is %02X%02X%02X, expected 3366CC\n",
403 __func__, x, y, r, g, b);
411 h264_context_free(h264);
420static const BYTE solid_idr_64x64_hevc[] = {
421 0x00, 0x00, 0x00, 0x01, 0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00,
422 0x90, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x1e, 0x92, 0x80, 0x90, 0x00, 0x00, 0x00, 0x01,
423 0x42, 0x01, 0x01, 0x01, 0x60, 0x00, 0x00, 0x03, 0x00, 0x90, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03,
424 0x00, 0x1e, 0xa0, 0x20, 0x81, 0x05, 0x96, 0x4a, 0x92, 0x4c, 0xaf, 0x01, 0x68, 0x08, 0x00, 0x00,
425 0x03, 0x00, 0x08, 0x00, 0x00, 0x03, 0x00, 0xc8, 0x40, 0x00, 0x00, 0x00, 0x01, 0x44, 0x01, 0xc1,
426 0x72, 0xb4, 0x22, 0x40, 0x00, 0x00, 0x01, 0x28, 0x01, 0xaf, 0x78, 0xeb, 0x02, 0x01, 0xff, 0xe2,
427 0x61, 0xe8, 0x39, 0xd3, 0x4f, 0xed, 0xbf, 0xd4, 0x54, 0x79, 0xc0
434static BOOL testDecodeHEVC(
void)
437 const UINT32 width = 64;
438 const UINT32 height = 64;
439 const UINT32 format = PIXEL_FORMAT_BGRX32;
440 const UINT32 stride = width * 4;
441 const RECTANGLE_16 rect = { .left = 0, .top = 0, .right = width, .bottom = height };
442 H264_CONTEXT* h264 = h264_context_new(FALSE);
443 BYTE* dst = calloc(stride, height);
448 if (!h264_context_set_option(h264, H264_CONTEXT_OPTION_HEVC, TRUE))
450 (void)fprintf(stderr,
"[%s] no HEVC decoder available, skipping\n", __func__);
455 if (!h264_context_get_option(h264, H264_CONTEXT_OPTION_HEVC) ||
456 !h264_context_reset(h264, width, height))
459 const INT32 res = avc420_decompress(h264, solid_idr_64x64_hevc,
sizeof(solid_idr_64x64_hevc),
460 dst, format, stride, width, height, &rect, 1);
463 (void)fprintf(stderr,
"[%s] avc420_decompress failed: %" PRId32
"\n", __func__, res);
467 for (UINT32 y = 0; y < height; y++)
469 for (UINT32 x = 0; x < width; x++)
474 const UINT32 color = FreeRDPReadColor(&dst[y * stride + x * 4], format);
475 FreeRDPSplitColor(color, format, &r, &g, &b,
nullptr,
nullptr);
476 if ((abs(r - 0x33) > 16) || (abs(g - 0x66) > 16) || (abs(b - 0xCC) > 16))
479 stderr,
"[%s] pixel %" PRIu32
",%" PRIu32
" is %02X%02X%02X, expected 3366CC\n",
480 __func__, x, y, r, g, b);
488 h264_context_free(h264);
501static const BYTE columns_idr_16x16_hevc444[] = {
502 0x00, 0x00, 0x00, 0x01, 0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x04, 0x08, 0x00, 0x00, 0x03, 0x00,
503 0x9e, 0x08, 0x00, 0x00, 0x03, 0x00, 0x00, 0xff, 0x92, 0x80, 0x90, 0x00, 0x00, 0x00, 0x01, 0x42,
504 0x01, 0x01, 0x04, 0x08, 0x00, 0x00, 0x03, 0x00, 0x9e, 0x08, 0x00, 0x00, 0x03, 0x00, 0x00, 0xff,
505 0x90, 0x11, 0x08, 0xb2, 0xc9, 0x55, 0x79, 0x5e, 0x02, 0xd4, 0x08, 0x34, 0x00, 0x10, 0x00, 0x00,
506 0x03, 0x00, 0x10, 0x00, 0x00, 0x03, 0x01, 0x90, 0x80, 0x00, 0x00, 0x00, 0x01, 0x44, 0x01, 0xc1,
507 0x70, 0x30, 0x62, 0x91, 0x20, 0x00, 0x00, 0x01, 0x28, 0x01, 0xaf, 0x05, 0xb8, 0x24, 0x9a, 0x1c,
508 0x60, 0x4b, 0x3f, 0xff, 0xf5, 0x3f, 0xea, 0x3f, 0x80, 0xfc, 0xe7, 0x91, 0xf1, 0x9f, 0x19, 0xe4,
509 0x7c, 0x6f, 0x93, 0xe4, 0xfc, 0x6f, 0x93, 0xf1, 0xbe, 0x4f, 0x92, 0x54, 0x3f, 0xfd, 0xfd, 0xb2,
510 0x97, 0x10, 0xff, 0x71, 0xae, 0xdf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9b, 0x03, 0x8d,
533 0x70, 0xba, 0xb0, 0x13, 0x2c, 0xb8
540static BOOL testDecodeHEVC444(
void)
543 const UINT32 width = 16;
544 const UINT32 height = 16;
545 const UINT32 format = PIXEL_FORMAT_BGRX32;
546 const UINT32 stride = width * 4;
548 const RECTANGLE_16 rects[] = { { .left = 0, .top = 1, .right = 8, .bottom = height },
549 { .left = 9, .top = 1, .right = 15, .bottom = height } };
550 const BYTE fill = 0x42;
551 H264_CONTEXT* h264 = h264_context_new(FALSE);
552 BYTE* dst = malloc(1ull * stride * height);
556 memset(dst, fill, 1ull * stride * height);
558 if (!h264_context_set_option(h264, H264_CONTEXT_OPTION_HEVC, TRUE) ||
559 !h264_context_set_option(h264, H264_CONTEXT_OPTION_HEVC444, TRUE))
561 (void)fprintf(stderr,
"[%s] no HEVC 4:4:4 decoder available, skipping\n", __func__);
566 if (!h264_context_get_option(h264, H264_CONTEXT_OPTION_HEVC444) ||
567 !h264_context_reset(h264, width, height))
571 avc420_decompress(h264, columns_idr_16x16_hevc444,
sizeof(columns_idr_16x16_hevc444), dst,
572 format, stride, width, height, rects, ARRAYSIZE(rects));
575 (void)fprintf(stderr,
"[%s] avc420_decompress failed: %" PRId32
"\n", __func__, res);
579 for (UINT32 y = 0; y < height; y++)
581 for (UINT32 x = 0; x < width; x++)
583 const BYTE* px = &dst[y * stride + x * 4];
584 const BOOL inside = (y >= 1) && (x != 8) && (x != 15);
587 if ((px[0] != fill) || (px[1] != fill) || (px[2] != fill) || (px[3] != fill))
590 stderr,
"[%s] pixel %" PRIu32
",%" PRIu32
" outside the regions changed\n",
600 FreeRDPSplitColor(FreeRDPReadColor(px, format), format, &r, &g, &b,
nullptr,
nullptr);
601 const BYTE er = (x % 2) ? 0x00 : 0xFF;
602 const BYTE eb = (x % 2) ? 0xFF : 0x00;
603 if ((abs(r - er) > 8) || (g > 8) || (abs(b - eb) > 8))
605 (void)fprintf(stderr,
606 "[%s] pixel %" PRIu32
",%" PRIu32
607 " is %02X%02X%02X, expected %02X00%02X\n",
608 __func__, x, y, r, g, b, er, eb);
616 h264_context_free(h264);
624static const BYTE avc444_idr[] = {
625 0x00, 0x00, 0x00, 0x01, 0x67, 0x64, 0x00, 0x0A, 0xAC, 0xB2, 0x08, 0x4D, 0x08, 0x00, 0x00, 0x03,
626 0x00, 0x08, 0x00, 0x00, 0x03, 0x01, 0xE0, 0x78, 0x91, 0x32, 0x40, 0x00, 0x00, 0x00, 0x01, 0x68,
627 0xEB, 0xC0, 0x8C, 0xB2, 0x2C, 0x00, 0x00, 0x00, 0x01, 0x09, 0x10, 0x00, 0x00, 0x01, 0x65, 0x88,
628 0x84, 0x3A, 0xFE, 0xC7, 0x6F, 0xE6, 0x59, 0x2A, 0x80, 0xC4, 0x0B, 0xD4, 0xF7, 0xD5, 0xB5, 0x93,
629 0x2C, 0x2D, 0x6E, 0xCB, 0x72, 0xFB, 0xC9, 0x1D, 0x47, 0x81, 0x00, 0x00, 0x01, 0x65, 0x12, 0x22,
630 0x10, 0xEB, 0xFE, 0xC7, 0x6F, 0xE6, 0x59, 0x2A, 0x80, 0xC4, 0x0B, 0xD4, 0xF7, 0xD5, 0xB5, 0x93,
631 0x2C, 0x2D, 0x6E, 0xCB, 0x72, 0xFB, 0xC9, 0x1D, 0x47, 0x81,
634static const BYTE avc444_p[] = {
635 0x00, 0x00, 0x00, 0x01, 0x09, 0x30, 0x00, 0x00, 0x01, 0x41, 0x9A, 0x3B, 0x1F, 0xF9,
636 0x59, 0xCD, 0xEF, 0xFF, 0xFC, 0xF8, 0xAA, 0xD6, 0x9F, 0x73, 0x05, 0x6A, 0x32, 0x45,
637 0xA5, 0x7B, 0x00, 0x00, 0x01, 0x41, 0x12, 0x68, 0xEC, 0x7F, 0xFA, 0xE1, 0x80,
641static BOOL testDecodeAvc444(UINT32 codec)
646 const UINT32 stride = 64 * 4;
647 const UINT32 format = PIXEL_FORMAT_BGRX32;
648 const BYTE untouched[4] = { 0xA5, 0xA5, 0xA5, 0xA5 };
649 const size_t sampleOffset = partial.top * stride + partial.left * 4;
650 BYTE luma[4] = WINPR_C_ARRAY_INIT;
651 BYTE combined[64 * 64 * 4] = WINPR_C_ARRAY_INIT;
652 BYTE separate[64 * 64 * 4] = WINPR_C_ARRAY_INIT;
653 H264_CONTEXT* combinedContext = h264_context_new(FALSE);
654 H264_CONTEXT* separateContext = h264_context_new(FALSE);
656 if (!combinedContext || !separateContext || !h264_context_reset(combinedContext, 64, 64) ||
657 !h264_context_reset(separateContext, 64, 64))
659 memset(separate, 0xA5,
sizeof(separate));
662 if (avc444_decompress(combinedContext, 0, &full, 1, avc444_idr,
sizeof(avc444_idr), &full, 1,
663 avc444_p,
sizeof(avc444_p), combined, format, stride, 64, 64, codec) != 0)
667 if (avc444_decompress(separateContext, 1, &partial, 1, avc444_idr,
sizeof(avc444_idr),
nullptr,
668 0,
nullptr, 0, separate, format, stride, 64, 64, codec) != 0)
670 memcpy(luma, &separate[sampleOffset],
sizeof(luma));
671 if (avc444_decompress(separateContext, 2, &partial, 1, avc444_p,
sizeof(avc444_p),
nullptr, 0,
672 nullptr, 0, separate, format, stride, 64, 64, codec) != 0)
676 if ((memcmp(luma, untouched,
sizeof(luma)) == 0) ||
677 (memcmp(luma, &separate[sampleOffset],
sizeof(luma)) == 0))
679 (void)fprintf(stderr,
"[%s] missing main or auxiliary output\n", __func__);
684 for (UINT32 y = 0; y < 64; y++)
686 for (UINT32 x = 0; x < 64; x++)
688 const size_t offset = y * stride + x * 4;
689 const BOOL inside = (x >= partial.left) && (x < partial.right) && (y >= partial.top) &&
690 (y < partial.bottom);
691 const BYTE* expected = inside ? &combined[offset] : untouched;
692 if (memcmp(&separate[offset], expected, inside ? 3 : 4) != 0)
694 (void)fprintf(stderr,
"[%s] pixel %" PRIu32
",%" PRIu32
" differs\n", __func__, x,
704 (void)fprintf(stderr,
"[%s] codec 0x%04" PRIX32
" failed\n", __func__, codec);
705 h264_context_free(combinedContext);
706 h264_context_free(separateContext);
710#if defined(WITH_APPLE_VIDEOTOOLBOX)
713static BOOL testH264Range(
const BYTE* data,
size_t size, BOOL fullRange)
716 const BYTE startCode[] = { 0, 0, 0, 1 };
717 const BYTE* parameters[2] = WINPR_C_ARRAY_INIT;
718 size_t lengths[2] = WINPR_C_ARRAY_INIT;
720 for (
size_t i = 0; i < ARRAYSIZE(parameters); i++)
722 if ((size - offset < 5) || (memcmp(data + offset, startCode,
sizeof(startCode)) != 0) ||
723 ((data[offset + 4] & 0x1F) != 7 + i))
725 offset +=
sizeof(startCode);
726 parameters[i] = data + offset;
727 const size_t start = offset;
728 while ((size - offset >=
sizeof(startCode)) &&
729 (memcmp(data + offset, startCode,
sizeof(startCode)) != 0))
731 if (size - offset <
sizeof(startCode))
733 lengths[i] = offset - start;
735 CMVideoFormatDescriptionRef format =
nullptr;
736 if (CMVideoFormatDescriptionCreateFromH264ParameterSets(
737 kCFAllocatorDefault, ARRAYSIZE(parameters), parameters, lengths, 4, &format) != noErr)
739 const BOOL actual = CMFormatDescriptionGetExtension(
740 format, kCMFormatDescriptionExtension_FullRangeVideo) == kCFBooleanTrue
744 if (actual != fullRange)
745 (void)fprintf(stderr,
"[%s] full range %d, expected %d\n", __func__, actual, fullRange);
746 return actual == fullRange ? TRUE : FALSE;
750static BOOL testCameraEncode(UINT32 width, UINT32 height)
753 const BYTE samples[][3] = { { 16, 128, 128 }, { 235, 128, 128 }, { 96, 64, 192 } };
754 const size_t pixels = (size_t)width * height;
755 BYTE* nv12 = calloc(pixels * 3 / 2, 1);
756 wStream* output = Stream_New(
nullptr, 1);
757 FREERDP_VIDEO_CONTEXT* video = freerdp_video_context_new(width, height);
758 H264_CONTEXT* decoder = h264_context_new(FALSE);
759 if (!nv12 || !output || !video || !decoder || !h264_context_reset(decoder, width, height) ||
760 !freerdp_video_conversion_supported(FREERDP_VIDEO_FORMAT_NV12, FREERDP_VIDEO_FORMAT_H264))
763 for (
size_t frame = 0; frame < ARRAYSIZE(samples); frame++)
766 if (!freerdp_video_context_reconfigure(video, width, height, 30, 0,
767 H264_CAMERA_VIDEO_REAL_TIME))
769 memset(nv12, samples[frame][0], pixels);
770 for (
size_t i = 0; i < pixels / 2; i += 2)
772 nv12[pixels + i] = samples[frame][1];
773 nv12[pixels + i + 1] = samples[frame][2];
775 Stream_ResetPosition(output);
776 Stream_Write_UINT8(output, 0x5a);
777 if (!freerdp_video_sample_convert(video, FREERDP_VIDEO_FORMAT_NV12, nv12, pixels * 3 / 2,
778 FREERDP_VIDEO_FORMAT_H264, output) ||
779 (Stream_GetPosition(output) <= 1) || (Stream_Buffer(output)[0] != 0x5a))
781 const BYTE* encoded = Stream_Buffer(output) + 1;
782 const size_t size = Stream_GetPosition(output) - 1;
783 if ((size > UINT32_MAX) || ((frame == 0) && !testH264Range(encoded, size, FALSE)) ||
784 (decoder->subsystem->Decompress(decoder, encoded, (UINT32)size) <= 0))
787 for (
size_t plane = 0; plane < 3; plane++)
789 const UINT32 divisor = plane == 0 ? 1 : 2;
790 const size_t center =
791 (size_t)(height / (2 * divisor)) * decoder->iStride[plane] + width / (2 * divisor);
792 if (!decoder->pYUVData[plane] ||
793 (abs(decoder->pYUVData[plane][center] - samples[frame][plane]) > 4))
800 (void)fprintf(stderr,
"[%s] NV12 camera encoding failed at %" PRIu32
"x%" PRIu32
"\n",
801 __func__, width, height);
802 h264_context_free(decoder);
803 freerdp_video_context_free(video);
804 Stream_Free(output, TRUE);
810static BOOL testEncodeRange(
void)
813 H264_CONTEXT* encoder = h264_context_new(TRUE);
814 if (!encoder || !h264_context_reset(encoder, 64, 48))
816 BYTE* planes[3] = WINPR_C_ARRAY_INIT;
817 UINT32 strides[3] = WINPR_C_ARRAY_INIT;
818 if (h264_get_yuv_buffer(encoder, 64, 64, 48, planes, strides) < 0)
820 for (
size_t plane = 0; plane < 3; plane++)
821 memset(planes[plane], 128, (
size_t)strides[plane] * (plane == 0 ? 48 : 24));
822 const H264_USAGETYPE usage[] = { H264_SCREEN_CONTENT_REAL_TIME, H264_CAMERA_VIDEO_REAL_TIME,
823 H264_SCREEN_CONTENT_NON_REAL_TIME,
824 H264_CAMERA_VIDEO_NON_REAL_TIME,
825 H264_SCREEN_CONTENT_REAL_TIME };
826 for (
size_t i = 0; i < ARRAYSIZE(usage); i++)
828 BYTE* encoded =
nullptr;
830 const BOOL fullRange = i % 2 == 0 ? TRUE : FALSE;
831 if (!h264_context_set_option(encoder, H264_CONTEXT_OPTION_USAGETYPE, usage[i]) ||
832 (h264_context_get_option(encoder, H264_CONTEXT_OPTION_USAGETYPE) != usage[i]) ||
833 (h264_compress(encoder, &encoded, &size) < 0) || !encoded ||
834 !testH264Range(encoded, size, fullRange))
840 (void)fprintf(stderr,
"[%s] encoder range change failed\n", __func__);
841 h264_context_free(encoder);
847int TestFreeRDPCodecH264(
int argc,
char* argv[])
856 const UINT32 formats[] = {
857 PIXEL_FORMAT_ABGR15, PIXEL_FORMAT_ARGB15, PIXEL_FORMAT_BGR15, PIXEL_FORMAT_BGR16,
858 PIXEL_FORMAT_BGR24, PIXEL_FORMAT_RGB15, PIXEL_FORMAT_RGB16, PIXEL_FORMAT_RGB24,
859 PIXEL_FORMAT_ABGR32, PIXEL_FORMAT_ARGB32, PIXEL_FORMAT_XBGR32, PIXEL_FORMAT_XRGB32,
860 PIXEL_FORMAT_BGRA32, PIXEL_FORMAT_RGBA32, PIXEL_FORMAT_BGRX32, PIXEL_FORMAT_RGBX32,
866 width = strtoul(argv[1],
nullptr, 0);
867 height = strtoul(argv[2],
nullptr, 0);
868 if ((errno != 0) || (width == 0) || (height == 0))
870 char buffer[128] = WINPR_C_ARRAY_INIT;
871 (void)fprintf(stderr,
"%s failed: width=%" PRIu32
", height=%" PRIu32
", errno=%s\n",
872 __func__, width, height, winpr_strerror(errno, buffer,
sizeof(buffer)));
878#if !defined(WITH_GFX_H264)
879 (void)fprintf(stderr,
"[%s] skipping, no H264 encoder/decoder support compiled in\n", __func__);
883#if defined(WITH_H264_WEBCODECS)
885 H264_CONTEXT* probe = h264_context_new(FALSE);
888 (void)fprintf(stderr,
"[%s] skipping, the runtime cannot decode H.264 with WebCodecs\n",
892 h264_context_free(probe);
896 if (!testContextOptions(FALSE, width, height))
898 if (!testDecode(FALSE))
900#if defined(WITH_APPLE_VIDEOTOOLBOX) || defined(WITH_H264_WEBCODECS)
901 if (!testDecode(TRUE))
904 if (!testDecodeHEVC())
906 if (!testDecodeHEVC444())
909 if (!testDecodeAvc444(RDPGFX_CODECID_AVC444) || !testDecodeAvc444(RDPGFX_CODECID_AVC444v2))
912#if defined(WITH_MEDIA_FOUNDATION) || defined(WITH_OPENH264) || defined(WITH_VIDEO_FFMPEG) || \
913 defined(WITH_MEDIACODEC) || defined(WITH_APPLE_VIDEOTOOLBOX)
914#if defined(WITH_MEDIA_FOUNDATION)
917 H264_CONTEXT* encoder = h264_context_new(TRUE);
920 (void)fprintf(stderr,
"[%s] H.264 compression tests skipped: no encoder on this system\n",
924 h264_context_free(encoder);
928 if (!testContextOptions(TRUE, width, height))
930 if (!testEncodeOffsetRegion())
933#if defined(WITH_APPLE_VIDEOTOOLBOX)
934 H264_CONTEXT* vtEncoder = h264_context_new(TRUE);
935 const BOOL useVideoToolbox =
936 vtEncoder && (strcmp(vtEncoder->subsystem->name,
"VideoToolbox") == 0) ? TRUE : FALSE;
937 h264_context_free(vtEncoder);
940 if (!testCameraEncode(64, 48) || !testCameraEncode(128, 96) || !testEncodeRange())
945 for (
size_t x = 0; x < ARRAYSIZE(formats); x++)
947 const UINT32 SrcFormat = formats[x];
948 for (
size_t y = 0; y < ARRAYSIZE(formats); y++)
950 if (!testEncode(SrcFormat, width, height))
955 (void)fprintf(stderr,
"H.264 compression tests skipped: no encoder configured\n");