FreeRDP
Loading...
Searching...
No Matches
TestFreeRDPCodecH264.c
1
2#include <math.h>
3#include <string.h>
4#include <stdlib.h>
5#include <errno.h>
6
7#include <winpr/sysinfo.h>
8
9#include <freerdp/freerdp.h>
10#include <freerdp/codec/color.h>
11
12#if defined(WITH_APPLE_VIDEOTOOLBOX)
13#include <CoreMedia/CoreMedia.h>
14#include <freerdp/codec/video.h>
15#include "../h264.h"
16#endif
17
18WINPR_ATTR_NODISCARD
19static const char* print_ns(UINT64 start, UINT64 end, char* buffer, size_t len)
20{
21 WINPR_ASSERT(buffer);
22
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);
29 return buffer;
30}
31
32WINPR_ATTR_NODISCARD
33static BOOL testContextOptions(BOOL compressor, uint32_t width, uint32_t height)
34{
35 BOOL rc = FALSE;
36 const UINT64 start = winpr_GetUnixTimeNS();
37 /* Test the requested encoder or decoder. */
38 H264_CONTEXT* h264 = h264_context_new(compressor);
39 if (!h264)
40 {
41 (void)fprintf(stderr, "[%s] h264_context_new failed\n", __func__);
42 return FALSE;
43 }
44
45 struct optpair_s
46 {
47 H264_CONTEXT_OPTION opt;
48 uint32_t val;
49 };
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++)
56 {
57 const struct optpair_s* cur = &optpair[x];
58 if (!h264_context_set_option(h264, cur->opt, cur->val))
59 {
60 (void)fprintf(stderr,
61 "[%s] h264_context_set_option %" PRIuz "{ %d, %" PRIu32 " } failed\n",
62 __func__, x, cur->opt, cur->val);
63 goto fail;
64 }
65 }
66 if (!h264_context_reset(h264, width, height))
67 {
68 (void)fprintf(stderr, "[%s] h264_context_reset\n", __func__);
69 goto fail;
70 }
71
72 rc = TRUE;
73fail:
74 h264_context_free(h264);
75 const UINT64 end = winpr_GetUnixTimeNS();
76
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)));
80 return rc;
81}
82
83WINPR_ATTR_MALLOC(free, 1)
84static void* allocRGB(uint32_t format, uint32_t width, uint32_t height, uint32_t* pstride)
85{
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);
90
91 uint8_t* rgb = calloc(stride, height);
92 if (!rgb)
93 return nullptr;
94
95 const UINT32 color = FreeRDPGetColor(format, 0x12, 0xaa, 0x63, 0x80);
96 for (size_t y = 0; y < height; y++)
97 {
98 uint8_t* line = &rgb[y * stride];
99 for (size_t x = 0; x < width; x++)
100 {
101 FreeRDPWriteColor(&line[bpp * x], format, color);
102 }
103 }
104 return rgb;
105}
106
107WINPR_ATTR_NODISCARD
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)
112{
113 const double maxDiff = 8.0;
114 const double diffR = fabs(1.0 * a - b);
115 if (diffR > maxDiff)
116 {
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);
121 return FALSE;
122 }
123 return TRUE;
124}
125
126WINPR_ATTR_NODISCARD
127static BOOL compareRGBColors(size_t line, size_t offset, uint32_t colora, uint32_t colorb,
128 uint32_t format)
129{
130 BYTE cR = 0;
131 BYTE cG = 0;
132 BYTE cB = 0;
133 BYTE cA = 0;
134 BYTE dR = 0;
135 BYTE dG = 0;
136 BYTE dB = 0;
137 BYTE dA = 0;
138
139 FreeRDPSplitColor(colora, format, &cR, &cG, &cB, &cA, nullptr);
140 FreeRDPSplitColor(colorb, format, &dR, &dG, &dB, &dA, nullptr);
141
142 if (!printIfFail(line, offset, "red", cR, dR))
143 return FALSE;
144 if (!printIfFail(line, offset, "green", cG, dG))
145 return FALSE;
146 if (!printIfFail(line, offset, "blue", cB, dB))
147 return FALSE;
148 if (FreeRDPColorHasAlpha(format))
149 {
150 if (!printIfFail(line, offset, "alpha", cA, dA))
151 return FALSE;
152 }
153 return TRUE;
154}
155
156WINPR_ATTR_NODISCARD
157static BOOL compareRGBLine(size_t line, const uint8_t* src, const uint8_t* dst, uint32_t format,
158 size_t width)
159{
160 const size_t bpp = FreeRDPGetBytesPerPixel(format);
161 for (size_t x = 0; x < width; x++)
162 {
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))
166 return FALSE;
167 }
168 return TRUE;
169}
170
171WINPR_ATTR_NODISCARD
172static BOOL compareRGB(const uint8_t* src, const uint8_t* dst, uint32_t format, size_t width,
173 size_t stride, size_t height)
174{
175 WINPR_ASSERT(src);
176 WINPR_ASSERT(dst);
177
178 const size_t bits = FreeRDPGetBitsPerPixel(format);
179 if (bits != 32)
180 {
181 printf("skipping color compare for format %s, only 32bpp colors are checked\n",
182 FreeRDPGetColorFormatName(format));
183 return TRUE;
184 }
185
186 for (size_t y = 0; y < height; y++)
187 {
188 const uint8_t* csrc = &src[y * stride];
189 const uint8_t* cdst = &dst[y * stride];
190 if (!compareRGBLine(y, csrc, cdst, format, width))
191 return FALSE;
192 }
193 return TRUE;
194}
195
196WINPR_ATTR_NODISCARD
197static BOOL testEncode(uint32_t format, uint32_t width, uint32_t height)
198{
199 BOOL rc = FALSE;
200 void* src = nullptr;
201 void* out = nullptr;
202 void* copy = nullptr;
203 RDPGFX_H264_METABLOCK meta = WINPR_C_ARRAY_INIT;
204 H264_CONTEXT* h264 = h264_context_new(TRUE);
205 H264_CONTEXT* h264dec = h264_context_new(FALSE);
206 if (!h264 || !h264dec)
207 {
208 (void)fprintf(stderr, "[%s] encoder=%p, decoder=%p\n", __func__, (void*)h264,
209 (void*)h264dec);
210 goto fail;
211 }
212
213 if (!h264_context_reset(h264, width, height))
214 {
215 (void)fprintf(stderr, "[%s] h264_context_reset(encoder) failed\n", __func__);
216 goto fail;
217 }
218 if (!h264_context_reset(h264dec, width, height))
219 {
220 (void)fprintf(stderr, "[%s] h264_context_reset(decoder) failed\n", __func__);
221 goto fail;
222 }
223
224 uint32_t stride = 0;
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))
229 {
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);
234 goto fail;
235 }
236
237 copy = calloc(height, stride);
238 if (!copy)
239 goto fail;
240 memcpy(copy, src, 1ull * height * stride);
241
242 const RECTANGLE_16 rect = { .left = 0, .top = 0, .right = width, .bottom = height };
243 uint32_t dstsize = 0;
244 uint8_t* dst = nullptr;
245 const int res1 =
246 avc420_compress(h264, src, format, stride, width, height, &rect, &dst, &dstsize, &meta);
247 if (res1 < 0)
248 {
249 (void)fprintf(stderr, "[%s] avc420_compress failed: %d\n", __func__, res1);
250 goto fail;
251 }
252 if ((dstsize == 0) || !dst)
253 {
254 (void)fprintf(stderr, "[%s] dstsize=%" PRIu32 ", dst=%p\n", __func__, dstsize, (void*)dst);
255 goto fail;
256 }
257 if (memcmp(copy, src, 1ull * height * stride) != 0)
258 {
259 (void)fprintf(stderr, "[%s] avc420_compress modified source data!\n", __func__);
260 goto fail;
261 }
262
263 const int res2 =
264 avc420_decompress(h264dec, dst, dstsize, out, format, stride, width, height, &rect, 1);
265 if (res2 < 0)
266 {
267 (void)fprintf(stderr, "[%s] avc420_decompress failed: %d\n", __func__, res2);
268 goto fail;
269 }
270
271 rc = compareRGB(src, out, format, width, stride, height);
272fail:
273 if (!rc)
274 (void)fprintf(stderr, "[%s] run failed\n", __func__);
275 h264_context_free(h264);
276 h264_context_free(h264dec);
277 free_h264_metablock(&meta);
278 free(src);
279 free(copy);
280 free(out);
281 return rc;
282}
283
284WINPR_ATTR_NODISCARD
285static BOOL testEncodeOffsetRegion(void)
286{
287 BOOL rc = FALSE;
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 };
293 RDPGFX_H264_METABLOCK meta = WINPR_C_ARRAY_INIT;
294 H264_CONTEXT* h264 = h264_context_new(TRUE);
295 UINT32 stride = 0;
296 BYTE* src = allocRGB(format, width, height, &stride);
297 BYTE* dst = nullptr;
298 UINT32 dstSize = 0;
299
300 if (!h264 || !src || !h264_context_reset(h264, width, height))
301 goto fail;
302
303 memset(src, 0, (size_t)stride * height);
304 const int res =
305 avc420_compress(h264, src, format, stride, width, height, &full, &dst, &dstSize, &meta);
306 if (res < 0)
307 {
308
309 (void)fprintf(stderr, "[%s] first avc420_compress failed: %d\n", __func__, res);
310 goto fail;
311 }
312 free_h264_metablock(&meta);
313
314 for (UINT32 y = region.top; y < region.bottom; y++)
315 {
316 BYTE* row = &src[(size_t)y * stride + (size_t)region.left * 4];
317 memset(row, 0xFF, (size_t)(region.right - region.left) * 4);
318 }
319
320 dst = nullptr;
321 dstSize = 0;
322 const int res2 =
323 avc420_compress(h264, src, format, stride, width, height, &region, &dst, &dstSize, &meta);
324 if (res2 < 0)
325 {
326 (void)fprintf(stderr, "[%s] second avc420_compress failed: %d\n", __func__, res2);
327 goto fail;
328 }
329 if ((meta.numRegionRects == 0) || (meta.regionRects[0].left != region.left) ||
330 (meta.regionRects[0].top != region.top))
331 {
332 (void)fprintf(
333 stderr, "%s failed: first changed tile does not start at (%" PRIu16 ", %" PRIu16 ")\n",
334 __func__, region.left, region.top);
335 goto fail;
336 }
337
338 rc = TRUE;
339fail:
340 if (!rc)
341 (void)fprintf(stderr, "[%s] run failed\n", __func__);
342 h264_context_free(h264);
343 free_h264_metablock(&meta);
344 free(src);
345 return rc;
346}
347
348/* 64x64 solid 0x3366CC, generated with
349 * ffmpeg -f lavfi -i color=c=0x3366CC:s=64x64 -frames:v 1 -c:v libx264 -profile:v baseline
350 * -bsf:v h264_mp4toannexb,filter_units=remove_types=6 -f h264 */
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
357};
358
359WINPR_ATTR_NODISCARD
360static BOOL testDecode(BOOL separateParameters)
361{
362 BOOL rc = FALSE;
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);
370
371 if (!h264 || !dst || !h264_context_reset(h264, width, height))
372 goto fail;
373
374 /* The video channel can deliver SPS/PPS separately from the picture. */
375 const UINT32 offset = separateParameters ? 36 : 0; /* IDR start code follows SPS/PPS. */
376 if (separateParameters && (avc420_decompress(h264, solid_idr_64x64, offset, dst, format, stride,
377 width, height, &rect, 1) < 0))
378 goto fail;
379
380 /* The picture must be available immediately, with the expected RGB values. */
381 const INT32 res =
382 avc420_decompress(h264, solid_idr_64x64 + offset, sizeof(solid_idr_64x64) - offset, dst,
383 format, stride, width, height, &rect, 1);
384 if (res < 0)
385 {
386 (void)fprintf(stderr, "[%s] avc420_decompress failed: %" PRId32 "\n", __func__, res);
387 goto fail;
388 }
389
390 for (UINT32 y = 0; y < height; y++)
391 {
392 for (UINT32 x = 0; x < width; x++)
393 {
394 BYTE r = 0;
395 BYTE g = 0;
396 BYTE b = 0;
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))
400 {
401 (void)fprintf(
402 stderr, "[%s] pixel %" PRIu32 ",%" PRIu32 " is %02X%02X%02X, expected 3366CC\n",
403 __func__, x, y, r, g, b);
404 goto fail;
405 }
406 }
407 }
408
409 rc = TRUE;
410fail:
411 h264_context_free(h264);
412 free(dst);
413 return rc;
414}
415
416/* 64x64 solid 0x3366CC HEVC Main without picture reordering (as RDP servers send it),
417 * generated with
418 * ffmpeg -f lavfi -i color=c=0x3366CC:s=64x64 -frames:v 1 -c:v libx265 -tune zerolatency
419 * -bsf:v "hevc_mp4toannexb,filter_units=remove_types=39|40" -f hevc */
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
428};
429
430/* H264_CONTEXT_OPTION_HEVC: the picture must be available immediately, as for H264.
431 * Skipped if no subsystem can decode HEVC (only MediaFoundation can, and only with the
432 * HEVC Video Extensions installed). */
433WINPR_ATTR_NODISCARD
434static BOOL testDecodeHEVC(void)
435{
436 BOOL rc = FALSE;
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);
444
445 if (!h264 || !dst)
446 goto fail;
447
448 if (!h264_context_set_option(h264, H264_CONTEXT_OPTION_HEVC, TRUE))
449 {
450 (void)fprintf(stderr, "[%s] no HEVC decoder available, skipping\n", __func__);
451 rc = TRUE;
452 goto fail;
453 }
454
455 if (!h264_context_get_option(h264, H264_CONTEXT_OPTION_HEVC) ||
456 !h264_context_reset(h264, width, height))
457 goto fail;
458
459 const INT32 res = avc420_decompress(h264, solid_idr_64x64_hevc, sizeof(solid_idr_64x64_hevc),
460 dst, format, stride, width, height, &rect, 1);
461 if (res < 0)
462 {
463 (void)fprintf(stderr, "[%s] avc420_decompress failed: %" PRId32 "\n", __func__, res);
464 goto fail;
465 }
466
467 for (UINT32 y = 0; y < height; y++)
468 {
469 for (UINT32 x = 0; x < width; x++)
470 {
471 BYTE r = 0;
472 BYTE g = 0;
473 BYTE b = 0;
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))
477 {
478 (void)fprintf(
479 stderr, "[%s] pixel %" PRIu32 ",%" PRIu32 " is %02X%02X%02X, expected 3366CC\n",
480 __func__, x, y, r, g, b);
481 goto fail;
482 }
483 }
484 }
485
486 rc = TRUE;
487fail:
488 h264_context_free(h264);
489 free(dst);
490 return rc;
491}
492
493/* 16x16 HEVC 4:4:4 (Range Extensions) IDR, lossless: one pixel wide columns alternating red
494 * (even x) and blue (odd x), which 4:2:0 cannot represent. YCbCr BT.709 full range, but the
495 * VUI says RGB (matrix 0) as in the streams Windows servers send. Generated with
496 * ffmpeg -f lavfi -i "color=black:s=16x16,format=gbrp,
497 * geq=r='if(mod(X\,2)\,0\,255)':g=0:b='if(mod(X\,2)\,255\,0)'" -frames:v 1
498 * -vf "scale=out_color_matrix=bt709:out_range=full,format=yuv444p" -c:v libx265
499 * -tune zerolatency -x265-params lossless=1:colormatrix=gbr:range=limited:
500 * transfer=iec61966-2-1:info=0 -bsf:v "filter_units=remove_types=39|40" -f hevc */
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, 0x3b, 0x3a, 0xd5, 0x8e, 0x18, 0x61, 0xac, 0xdf, 0x8d, 0xf8,
511 0xdf, 0x8d, 0xd4, 0x30, 0xc2, 0xc8, 0x8f, 0xf5, 0x8c, 0x3a, 0x78, 0x7d, 0x3f, 0x86, 0x3a, 0x3f,
512 0xd1, 0xc6, 0xf5, 0x7f, 0x53, 0xa6, 0x31, 0xd1, 0x5e, 0xe5, 0xee, 0x5e, 0xe5, 0x95, 0xfc, 0x76,
513 0xb7, 0xae, 0xf0, 0xfe, 0x01, 0x62, 0x12, 0x64, 0xc9, 0x93, 0x13, 0xd3, 0x16, 0x82, 0xbf, 0xfa,
514 0xe6, 0xfd, 0x70, 0xad, 0x65, 0x6a, 0x9d, 0xc7, 0xbc, 0x7b, 0xc7, 0xbc, 0x78, 0xcf, 0x83, 0x1f,
515 0xaf, 0x3c, 0x4a, 0xae, 0x23, 0x6e, 0x1b, 0xcb, 0xd4, 0x2e, 0x02, 0xe0, 0x2e, 0x02, 0xdd, 0x6e,
516 0x22, 0x83, 0x78, 0x04, 0xba, 0x47, 0x92, 0x6b, 0x28, 0xd0, 0x3b, 0x3e, 0xbe, 0xba, 0x07, 0x42,
517 0xf9, 0x18, 0x62, 0xf2, 0x67, 0x96, 0xe5, 0xe8, 0x58, 0xb0, 0x23, 0x53, 0x38, 0x11, 0x03, 0x66,
518 0xd9, 0xab, 0x63, 0xe1, 0x34, 0xc9, 0xd6, 0xc7, 0x40, 0x26, 0xcd, 0x9b, 0x34, 0xf5, 0x9f, 0xa3,
519 0xff, 0xf0, 0xb4, 0xf8, 0x52, 0x08, 0x30, 0x3f, 0xd9, 0x57, 0x35, 0x73, 0x57, 0x35, 0x6a, 0x33,
520 0xc0, 0xdb, 0x0b, 0x42, 0x54, 0xa0, 0xc8, 0x9f, 0x42, 0x2a, 0xa7, 0x9e, 0x79, 0xe7, 0x9e, 0x77,
521 0x76, 0x89, 0xb6, 0x1c, 0x43, 0x9c, 0x3a, 0x15, 0x40, 0xa9, 0x52, 0xa5, 0x22, 0x95, 0x05, 0x3f,
522 0xf3, 0xc3, 0xf9, 0xdb, 0x39, 0xc0, 0x33, 0x14, 0x45, 0xc4, 0x5c, 0x45, 0xc4, 0x54, 0xb9, 0x95,
523 0xbe, 0x2c, 0x8c, 0xd2, 0x44, 0xa2, 0x3d, 0x88, 0x1e, 0x93, 0xb9, 0x3b, 0x93, 0xb9, 0x38, 0xd0,
524 0x12, 0xbb, 0xe9, 0xad, 0x17, 0x82, 0x0c, 0xbe, 0xe3, 0x83, 0xec, 0xbb, 0x51, 0x55, 0x54, 0xbd,
525 0xba, 0xa0, 0x0c, 0x26, 0xe3, 0x1b, 0x2e, 0xdc, 0x5f, 0xf0, 0x28, 0x3c, 0x15, 0x75, 0x39, 0x5b,
526 0xe4, 0xb1, 0x1f, 0x38, 0x5f, 0xdf, 0x9a, 0xae, 0x59, 0x71, 0xf5, 0x79, 0x21, 0x85, 0xe2, 0x03,
527 0xa6, 0x81, 0x22, 0xbb, 0xc6, 0x10, 0x8d, 0xc9, 0xa9, 0xf7, 0x04, 0x6a, 0xd7, 0x57, 0xf5, 0xd6,
528 0x08, 0xb1, 0x49, 0xec, 0x23, 0xc9, 0x1a, 0x7e, 0x4e, 0x87, 0x05, 0x6c, 0xdc, 0x34, 0x02, 0xf7,
529 0x05, 0xc8, 0x69, 0xaa, 0xce, 0xf9, 0x24, 0xfb, 0xfc, 0x13, 0xcc, 0x3e, 0xa6, 0xef, 0x26, 0x3c,
530 0xb3, 0x12, 0x58, 0xfb, 0x4e, 0x8f, 0x29, 0xff, 0x02, 0x72, 0x65, 0xf7, 0xb1, 0x2e, 0xb4, 0x45,
531 0xb9, 0xf3, 0xf3, 0xe3, 0xfd, 0x09, 0xed, 0xd6, 0x5b, 0x5d, 0x6d, 0xfe, 0x16, 0x05, 0x6d, 0x5c,
532 0x74, 0xa7, 0xc0, 0x93, 0x4b, 0x3d, 0x87, 0x14, 0xe7, 0x25, 0x2d, 0x8e, 0x19, 0x9b, 0x03, 0x8d,
533 0x70, 0xba, 0xb0, 0x13, 0x2c, 0xb8
534};
535
536/* H264_CONTEXT_OPTION_HEVC444: every pixel keeps its own colour, also in a region that does not
537 * start at the picture origin, and pixels outside the regions are left alone. Skipped if no
538 * subsystem can decode HEVC 4:4:4. */
539WINPR_ATTR_NODISCARD
540static BOOL testDecodeHEVC444(void)
541{
542 BOOL rc = FALSE;
543 const UINT32 width = 16;
544 const UINT32 height = 16;
545 const UINT32 format = PIXEL_FORMAT_BGRX32;
546 const UINT32 stride = width * 4;
547 /* two regions, the right one starting at an odd column; column 15 and row 0 stay unset */
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);
553
554 if (!h264 || !dst)
555 goto fail;
556 memset(dst, fill, 1ull * stride * height);
557
558 if (!h264_context_set_option(h264, H264_CONTEXT_OPTION_HEVC, TRUE) ||
559 !h264_context_set_option(h264, H264_CONTEXT_OPTION_HEVC444, TRUE))
560 {
561 (void)fprintf(stderr, "[%s] no HEVC 4:4:4 decoder available, skipping\n", __func__);
562 rc = TRUE;
563 goto fail;
564 }
565
566 if (!h264_context_get_option(h264, H264_CONTEXT_OPTION_HEVC444) ||
567 !h264_context_reset(h264, width, height))
568 goto fail;
569
570 const INT32 res =
571 avc420_decompress(h264, columns_idr_16x16_hevc444, sizeof(columns_idr_16x16_hevc444), dst,
572 format, stride, width, height, rects, ARRAYSIZE(rects));
573 if (res < 0)
574 {
575 (void)fprintf(stderr, "[%s] avc420_decompress failed: %" PRId32 "\n", __func__, res);
576 goto fail;
577 }
578
579 for (UINT32 y = 0; y < height; y++)
580 {
581 for (UINT32 x = 0; x < width; x++)
582 {
583 const BYTE* px = &dst[y * stride + x * 4];
584 const BOOL inside = (y >= 1) && (x != 8) && (x != 15);
585 if (!inside)
586 {
587 if ((px[0] != fill) || (px[1] != fill) || (px[2] != fill) || (px[3] != fill))
588 {
589 (void)fprintf(
590 stderr, "[%s] pixel %" PRIu32 ",%" PRIu32 " outside the regions changed\n",
591 __func__, x, y);
592 goto fail;
593 }
594 continue;
595 }
596
597 BYTE r = 0;
598 BYTE g = 0;
599 BYTE b = 0;
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))
604 {
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);
609 goto fail;
610 }
611 }
612 }
613
614 rc = TRUE;
615fail:
616 h264_context_free(h264);
617 free(dst);
618 return rc;
619}
620
621/* 64x64 I420: Y=64/U=90/V=170; the P-frame adds a 16x16 Y=96 block at (8,16).
622 * Generated with libx264: -qp 18 -profile:v high, bframes=0:slices=2:repeat-headers=1;
623 * SEI removed. The auxiliary frame depends on the preceding main frame. */
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,
632};
633
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,
638};
639
640WINPR_ATTR_NODISCARD
641static BOOL testDecodeAvc444(UINT32 codec)
642{
643 BOOL rc = FALSE;
644 const RECTANGLE_16 full = { 0, 0, 64, 64 };
645 const RECTANGLE_16 partial = { 16, 16, 48, 32 };
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);
655
656 if (!combinedContext || !separateContext || !h264_context_reset(combinedContext, 64, 64) ||
657 !h264_context_reset(separateContext, 64, 64))
658 goto fail;
659 memset(separate, 0xA5, sizeof(separate));
660
661 /* Op 0 decodes main and dependent auxiliary frames in one update. */
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)
664 goto fail;
665
666 /* Ops 1 and 2 deliver the same frames as separate partial updates. */
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)
669 goto fail;
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)
673 goto fail;
674
675 /* Both the main picture and the auxiliary correction must produce output. */
676 if ((memcmp(luma, untouched, sizeof(luma)) == 0) ||
677 (memcmp(luma, &separate[sampleOffset], sizeof(luma)) == 0))
678 {
679 (void)fprintf(stderr, "[%s] missing main or auxiliary output\n", __func__);
680 goto fail;
681 }
682
683 /* Compare RGB inside the update; keep all bytes unchanged outside. */
684 for (UINT32 y = 0; y < 64; y++)
685 {
686 for (UINT32 x = 0; x < 64; x++)
687 {
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)
693 {
694 (void)fprintf(stderr, "[%s] pixel %" PRIu32 ",%" PRIu32 " differs\n", __func__, x,
695 y);
696 goto fail;
697 }
698 }
699 }
700
701 rc = TRUE;
702fail:
703 if (!rc)
704 (void)fprintf(stderr, "[%s] codec 0x%04" PRIX32 " failed\n", __func__, codec);
705 h264_context_free(combinedContext);
706 h264_context_free(separateContext);
707 return rc;
708}
709
710#if defined(WITH_APPLE_VIDEOTOOLBOX)
711/* VideoToolbox writes SPS/PPS first, each with a four-byte Annex B start code. */
712WINPR_ATTR_NODISCARD
713static BOOL testH264Range(const BYTE* data, size_t size, BOOL fullRange)
714{
715 WINPR_ASSERT(data);
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;
719 size_t offset = 0;
720 for (size_t i = 0; i < ARRAYSIZE(parameters); i++)
721 {
722 if ((size - offset < 5) || (memcmp(data + offset, startCode, sizeof(startCode)) != 0) ||
723 ((data[offset + 4] & 0x1F) != 7 + i))
724 return FALSE;
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))
730 offset++;
731 if (size - offset < sizeof(startCode))
732 return FALSE;
733 lengths[i] = offset - start;
734 }
735 CMVideoFormatDescriptionRef format = nullptr;
736 if (CMVideoFormatDescriptionCreateFromH264ParameterSets(
737 kCFAllocatorDefault, ARRAYSIZE(parameters), parameters, lengths, 4, &format) != noErr)
738 return FALSE;
739 const BOOL actual = CMFormatDescriptionGetExtension(
740 format, kCMFormatDescriptionExtension_FullRangeVideo) == kCFBooleanTrue
741 ? TRUE
742 : FALSE;
743 CFRelease(format);
744 if (actual != fullRange)
745 (void)fprintf(stderr, "[%s] full range %d, expected %d\n", __func__, actual, fullRange);
746 return actual == fullRange ? TRUE : FALSE;
747}
748
749WINPR_ATTR_NODISCARD
750static BOOL testCameraEncode(UINT32 width, UINT32 height)
751{
752 BOOL rc = FALSE;
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))
761 goto fail;
762
763 for (size_t frame = 0; frame < ARRAYSIZE(samples); frame++)
764 {
765 /* Camera redirection reconfigures before every sample. */
766 if (!freerdp_video_context_reconfigure(video, width, height, 30, 0,
767 H264_CAMERA_VIDEO_REAL_TIME))
768 goto fail;
769 memset(nv12, samples[frame][0], pixels);
770 for (size_t i = 0; i < pixels / 2; i += 2)
771 {
772 nv12[pixels + i] = samples[frame][1];
773 nv12[pixels + i + 1] = samples[frame][2];
774 }
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))
780 goto fail;
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))
785 goto fail;
786 /* Inspect decoded planes: the desktop RGB converter assumes full-range YUV. */
787 for (size_t plane = 0; plane < 3; plane++)
788 {
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))
794 goto fail;
795 }
796 }
797 rc = TRUE;
798fail:
799 if (!rc)
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);
805 free(nv12);
806 return rc;
807}
808
809WINPR_ATTR_NODISCARD
810static BOOL testEncodeRange(void)
811{
812 BOOL rc = FALSE;
813 H264_CONTEXT* encoder = h264_context_new(TRUE);
814 if (!encoder || !h264_context_reset(encoder, 64, 48))
815 goto fail;
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)
819 goto fail;
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++)
827 {
828 BYTE* encoded = nullptr;
829 UINT32 size = 0;
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))
835 goto fail;
836 }
837 rc = TRUE;
838fail:
839 if (!rc)
840 (void)fprintf(stderr, "[%s] encoder range change failed\n", __func__);
841 h264_context_free(encoder);
842 return rc;
843}
844#endif
845
846WINPR_ATTR_NODISCARD
847int TestFreeRDPCodecH264(int argc, char* argv[])
848{
849 WINPR_ASSERT(argv);
850
851 WINPR_UNUSED(argc);
852 WINPR_UNUSED(argv);
853
854 UINT32 width = 124;
855 UINT32 height = 54;
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,
861 };
862
863 if (argc == 3)
864 {
865 errno = 0;
866 width = strtoul(argv[1], nullptr, 0);
867 height = strtoul(argv[2], nullptr, 0);
868 if ((errno != 0) || (width == 0) || (height == 0))
869 {
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)));
873 return -1;
874 }
875 }
876
877 /* The shared flag includes all H.264 backends. */
878#if !defined(WITH_GFX_H264)
879 (void)fprintf(stderr, "[%s] skipping, no H264 encoder/decoder support compiled in\n", __func__);
880 return 0;
881#endif
882
883#if defined(WITH_H264_WEBCODECS)
884 /* WebCodecs is probed at runtime; node (ctest) has no VideoDecoder. */
885 H264_CONTEXT* probe = h264_context_new(FALSE);
886 if (!probe)
887 {
888 (void)fprintf(stderr, "[%s] skipping, the runtime cannot decode H.264 with WebCodecs\n",
889 __func__);
890 return 0;
891 }
892 h264_context_free(probe);
893#endif
894
895 /* Decoder tests also run when no encoder backend is configured. */
896 if (!testContextOptions(FALSE, width, height))
897 return -1;
898 if (!testDecode(FALSE))
899 return -1;
900#if defined(WITH_APPLE_VIDEOTOOLBOX) || defined(WITH_H264_WEBCODECS)
901 if (!testDecode(TRUE))
902 return -1;
903#endif
904 if (!testDecodeHEVC())
905 return -1;
906 if (!testDecodeHEVC444())
907 return -1;
908 /* Both AVC444 packing formats must support combined and separate updates. */
909 if (!testDecodeAvc444(RDPGFX_CODECID_AVC444) || !testDecodeAvc444(RDPGFX_CODECID_AVC444v2))
910 return -1;
911
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)
915 /* Windows ships the Media Foundation H.264 encoder, but not every edition: N editions
916 * and Windows Server without the Media Foundation feature have none. */
917 H264_CONTEXT* encoder = h264_context_new(TRUE);
918 if (!encoder)
919 {
920 (void)fprintf(stderr, "[%s] H.264 compression tests skipped: no encoder on this system\n",
921 __func__);
922 return 0;
923 }
924 h264_context_free(encoder);
925#endif
926
927 /* Decoder-only builds cannot run the compression tests. */
928 if (!testContextOptions(TRUE, width, height))
929 return -1;
930 if (!testEncodeOffsetRegion())
931 return -1;
932
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);
938 if (useVideoToolbox)
939 {
940 if (!testCameraEncode(64, 48) || !testCameraEncode(128, 96) || !testEncodeRange())
941 return -1;
942 }
943#endif
944
945 for (size_t x = 0; x < ARRAYSIZE(formats); x++)
946 {
947 const UINT32 SrcFormat = formats[x];
948 for (size_t y = 0; y < ARRAYSIZE(formats); y++)
949 {
950 if (!testEncode(SrcFormat, width, height))
951 return -1;
952 }
953 }
954#else
955 (void)fprintf(stderr, "H.264 compression tests skipped: no encoder configured\n");
956#endif
957
958 return 0;
959}