FreeRDP
Loading...
Searching...
No Matches
TestFreeRDPCodecClear.c
1#include <winpr/crt.h>
2#include <winpr/print.h>
3#include <winpr/platform.h>
4
5#include <freerdp/codec/clear.h>
6
7/* Mirrors the internal CLEAR_BAND_MAX_HEIGHT used by clear_compress()'s band encoder
8 * (libfreerdp/codec/clear.c), so tests can size images to span multiple bands. */
9#define CLEAR_TEST_BAND_HEIGHT 52
10
11WINPR_PRAGMA_DIAG_PUSH
12WINPR_PRAGMA_DIAG_IGNORED_UNUSED_CONST_VAR
13/* [MS-RDPEGFX] 4.1.1.1 Example 1 */
14static const BYTE PREPARE_CLEAR_EXAMPLE_1[] = "\x03\xc3\x11\x00";
15static const BYTE TEST_CLEAR_EXAMPLE_1[] = "\x03\xc3\x11\x00";
16WINPR_PRAGMA_DIAG_POP
17
18/* [MS-RDPEGFX] 4.1.1.1 Example 2 */
19static const BYTE TEST_CLEAR_EXAMPLE_2[] =
20 "\x00\x0d\x00\x00\x00\x00\x00\x00\x00\x00\x82\x00\x00\x00\x00\x00"
21 "\x00\x00\x4e\x00\x11\x00\x75\x00\x00\x00\x02\x0e\xff\xff\xff\x00"
22 "\x00\x00\xdb\xff\xff\x00\x3a\x90\xff\xb6\x66\x66\xb6\xff\xb6\x66"
23 "\x00\x90\xdb\xff\x00\x00\x3a\xdb\x90\x3a\x3a\x90\xdb\x66\x00\x00"
24 "\xff\xff\xb6\x64\x64\x64\x11\x04\x11\x4c\x11\x4c\x11\x4c\x11\x4c"
25 "\x11\x4c\x00\x47\x13\x00\x01\x01\x04\x00\x01\x00\x00\x47\x16\x00"
26 "\x11\x02\x00\x47\x29\x00\x11\x01\x00\x49\x0a\x00\x01\x00\x04\x00"
27 "\x01\x00\x00\x4a\x0a\x00\x09\x00\x01\x00\x00\x47\x05\x00\x01\x01"
28 "\x1c\x00\x01\x00\x11\x4c\x11\x4c\x11\x4c\x00\x47\x0d\x4d\x00\x4d";
29
30/* [MS-RDPEGFX] 4.1.1.1 Example 3 */
31static const BYTE TEST_CLEAR_EXAMPLE_3[] =
32 "\x00\xdf\x0e\x00\x00\x00\x8b\x00\x00\x00\x00\x00\x00\x00\xfe\xfe"
33 "\xfe\xff\x80\x05\xff\xff\xff\x40\xfe\xfe\xfe\x40\x00\x00\x3f\x00"
34 "\x03\x00\x0b\x00\xfe\xfe\xfe\xc5\xd0\xc6\xd0\xc7\xd0\x68\xd4\x69"
35 "\xd4\x6a\xd4\x6b\xd4\x6c\xd4\x6d\xd4\x1a\xd4\x1a\xd4\xa6\xd0\x6e"
36 "\xd4\x6f\xd4\x70\xd4\x71\xd4\x72\xd4\x73\xd4\x74\xd4\x21\xd4\x22"
37 "\xd4\x23\xd4\x24\xd4\x25\xd4\xd9\xd0\xda\xd0\xdb\xd0\xc5\xd0\xc5"
38 "\xd0\xdc\xd0\xc2\xd0\x21\xd4\x22\xd4\x23\xd4\x24\xd4\x25\xd4\xc9"
39 "\xd0\xca\xd0\x5a\xd4\x2b\xd1\x28\xd1\x2c\xd1\x75\xd4\x27\xd4\x28"
40 "\xd4\x29\xd4\x2a\xd4\x1a\xd4\x1a\xd4\x1a\xd4\xb7\xd0\xb8\xd0\xb9"
41 "\xd0\xba\xd0\xbb\xd0\xbc\xd0\xbd\xd0\xbe\xd0\xbf\xd0\xc0\xd0\xc1"
42 "\xd0\xc2\xd0\xc3\xd0\xc4\xd0";
43
44/* [MS-RDPEGFX] 4.1.1.1 Example 4 */
45static const BYTE TEST_CLEAR_EXAMPLE_4[] =
46 "\x01\x0b\x78\x00\x00\x00\x00\x00\x46\x00\x00\x00\x00\x00\x00\x00"
47 "\x00\x00\x06\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x0f\xff\xff\xff"
48 "\xff\xff\xff\xff\xff\xff\xb6\xff\xff\xff\xff\xff\xff\xff\xff\xff"
49 "\xb6\x66\xff\xff\xff\xff\xff\xff\xff\xb6\x66\xdb\x90\x3a\xff\xff"
50 "\xb6\xff\xff\xff\xff\xff\xff\xff\xff\xff\x46\x91\x47\x91\x48\x91"
51 "\x49\x91\x4a\x91\x1b\x91";
52
53static BOOL test_ClearDecompressExample(UINT32 nr, UINT32 width, UINT32 height,
54 const BYTE* pSrcData, const UINT32 SrcSize)
55{
56 BOOL rc = FALSE;
57 int status = 0;
58 BYTE* pDstData = calloc(4ULL * width, height);
59 CLEAR_CONTEXT* clear = clear_context_new(FALSE);
60
61 if (!clear || !pDstData)
62 goto fail;
63
64 status = clear_decompress(clear, pSrcData, SrcSize, width, height, pDstData,
65 PIXEL_FORMAT_XRGB32, 0, 0, 0, width, height, nullptr);
66 (void)printf("clear_decompress example %" PRIu32 " status: %d\n", nr, status);
67 (void)fflush(stdout);
68 rc = (status == 0);
69fail:
70 clear_context_free(clear);
71 free(pDstData);
72 return rc;
73}
74
75/* subcodecByteCount of a CLEARCODEC_BITMAP_STREAM */
76static UINT32 test_clear_subcodec_size(const BYTE* pCompressed, UINT32 compressedSize)
77{
78 if (compressedSize < 14)
79 return 0;
80 return (UINT32)pCompressed[10] | ((UINT32)pCompressed[11] << 8) |
81 ((UINT32)pCompressed[12] << 16) | ((UINT32)pCompressed[13] << 24);
82}
83
84/* Compare two BGRX32 images; NSCodec is lossy, so a tolerance per channel is allowed */
85static BOOL test_clear_compare(const BYTE* pSrc, const BYTE* pDst, UINT32 width, UINT32 height,
86 UINT32 tolerance)
87{
88 for (size_t i = 0; i < 4ull * width * height; i++)
89 {
90 if ((i % 4) == 3)
91 continue;
92 const int diff = (int)pSrc[i] - (int)pDst[i];
93 if ((UINT32)abs(diff) > tolerance)
94 {
95 (void)fprintf(stderr,
96 "pixel %" PRIuz " channel %" PRIuz ": %" PRIu8 " != %" PRIu8
97 " (tolerance %" PRIu32 ")\n",
98 i / 4, i % 4, pSrc[i], pDst[i], tolerance);
99 return FALSE;
100 }
101 }
102 return TRUE;
103}
104
105#define CLEAR_TEST_NSC_TOLERANCE 8
106
107static BOOL test_ClearCompressDecompressRoundtrip(UINT32 width, UINT32 height)
108{
109 BOOL rc = FALSE;
110 BYTE* pDstData = nullptr;
111 BYTE* pCompressed = nullptr;
112 UINT32 compressedSize = 0;
113 const UINT32 DstFormat = PIXEL_FORMAT_BGRX32;
114 const UINT32 srcStep = width * FreeRDPGetBytesPerPixel(DstFormat);
115 BYTE* pSrcData = calloc(1ULL * srcStep, height);
116 CLEAR_CONTEXT* clearEnc = clear_context_new(TRUE);
117 CLEAR_CONTEXT* clearDec = clear_context_new(FALSE);
118
119 pDstData = calloc(1ULL * srcStep, height);
120 if (!pSrcData || !pDstData || !clearEnc || !clearDec)
121 goto fail;
122
123 /* Mix of flat runs (compresses well) and a varying pattern (forces many short runs). */
124 for (UINT32 y = 0; y < height; y++)
125 {
126 BYTE* pLine = &pSrcData[1ULL * y * srcStep];
127 for (UINT32 x = 0; x < width; x++)
128 {
129 BYTE r = 0;
130 BYTE g = 0;
131 BYTE b = 0;
132
133 if (x < width / 2)
134 {
135 r = 0x10;
136 g = 0x80;
137 b = 0xF0;
138 }
139 else
140 {
141 r = WINPR_ASSERTING_INT_CAST(BYTE, (x * 7 + y * 13) & 0xFF);
142 g = WINPR_ASSERTING_INT_CAST(BYTE, (x * 3 + y * 5) & 0xFF);
143 b = WINPR_ASSERTING_INT_CAST(BYTE, (x + y) & 0xFF);
144 }
145
146 const UINT32 color = FreeRDPGetColor(DstFormat, r, g, b, 0xFF);
147 FreeRDPWriteColor(&pLine[1ULL * x * FreeRDPGetBytesPerPixel(DstFormat)], DstFormat,
148 color);
149 }
150 }
151
152 if (!clear_compress(clearEnc, pSrcData, DstFormat, srcStep, width, height, &pCompressed,
153 &compressedSize))
154 {
155 (void)fprintf(stderr, "clear_compress failed for %" PRIu32 "x%" PRIu32 "\n", width, height);
156 goto fail;
157 }
158
159 if (clear_decompress(clearDec, pCompressed, compressedSize, width, height, pDstData, DstFormat,
160 srcStep, 0, 0, width, height, nullptr) != 0)
161 {
162 (void)fprintf(stderr, "clear_decompress failed for %" PRIu32 "x%" PRIu32 "\n", width,
163 height);
164 goto fail;
165 }
166
167 {
168 const UINT32 tolerance = (test_clear_subcodec_size(pCompressed, compressedSize) > 0)
169 ? CLEAR_TEST_NSC_TOLERANCE
170 : 0;
171 if (!test_clear_compare(pSrcData, pDstData, width, height, tolerance))
172 {
173 (void)fprintf(stderr, "clear roundtrip mismatch for %" PRIu32 "x%" PRIu32 "\n", width,
174 height);
175 goto fail;
176 }
177 }
178
179 rc = TRUE;
180fail:
181 clear_context_free(clearEnc);
182 clear_context_free(clearDec);
183 free(pSrcData);
184 free(pDstData);
185 return rc;
186}
187
188/* Content that varies unpredictably along each row (defeats row-major residual RLE) but is
189 * constant down every column (every band's V-Bar for a given x is byte-identical), across
190 * enough rows for several 52-row bands. This exercises clear_compress_bands_data()'s
191 * VBAR_CACHE_HIT/SHORT_VBAR_CACHE_MISS path and should compress far better than residual-only
192 * encoding of the same image would. */
193static BOOL test_ClearCompressBandsRoundtrip(void)
194{
195 BOOL rc = FALSE;
196 const UINT32 width = 37;
197 const UINT32 height = 3 * CLEAR_TEST_BAND_HEIGHT + 5;
198 BYTE* pDstData = nullptr;
199 BYTE* pCompressed = nullptr;
200 UINT32 compressedSize = 0;
201 const UINT32 DstFormat = PIXEL_FORMAT_BGRX32;
202 const UINT32 srcStep = width * FreeRDPGetBytesPerPixel(DstFormat);
203 BYTE* pSrcData = calloc(1ULL * srcStep, height);
204 CLEAR_CONTEXT* clearEnc = clear_context_new(TRUE);
205 CLEAR_CONTEXT* clearDec = clear_context_new(FALSE);
206
207 pDstData = calloc(1ULL * srcStep, height);
208 if (!pSrcData || !pDstData || !clearEnc || !clearDec)
209 goto fail;
210
211 for (UINT32 y = 0; y < height; y++)
212 {
213 BYTE* pLine = &pSrcData[1ULL * y * srcStep];
214 for (UINT32 x = 0; x < width; x++)
215 {
216 const BYTE r = WINPR_ASSERTING_INT_CAST(BYTE, (x * 97 + x * x * 13) & 0xFF);
217 const BYTE g = WINPR_ASSERTING_INT_CAST(BYTE, (x * 53 + x * x * 7) & 0xFF);
218 const BYTE b = WINPR_ASSERTING_INT_CAST(BYTE, (x * 31) & 0xFF);
219 const UINT32 color = FreeRDPGetColor(DstFormat, r, g, b, 0xFF);
220 FreeRDPWriteColor(&pLine[1ULL * x * FreeRDPGetBytesPerPixel(DstFormat)], DstFormat,
221 color);
222 }
223 }
224
225 if (!clear_compress(clearEnc, pSrcData, DstFormat, srcStep, width, height, &pCompressed,
226 &compressedSize))
227 {
228 (void)fprintf(stderr, "clear_compress (bands) failed for %" PRIu32 "x%" PRIu32 "\n", width,
229 height);
230 goto fail;
231 }
232
233 if (clear_decompress(clearDec, pCompressed, compressedSize, width, height, pDstData, DstFormat,
234 srcStep, 0, 0, width, height, nullptr) != 0)
235 {
236 (void)fprintf(stderr, "clear_decompress (bands) failed for %" PRIu32 "x%" PRIu32 "\n",
237 width, height);
238 goto fail;
239 }
240
241 if (test_clear_subcodec_size(pCompressed, compressedSize) != 0)
242 {
243 (void)fprintf(stderr, "clear bands: unexpected subcodec layer\n");
244 goto fail;
245 }
246
247 if (memcmp(pSrcData, pDstData, 1ULL * srcStep * height) != 0)
248 {
249 (void)fprintf(stderr, "clear bands roundtrip mismatch for %" PRIu32 "x%" PRIu32 "\n", width,
250 height);
251 goto fail;
252 }
253
254 /* Column content repeats identically in every band, so from the second band onward every
255 * column should be a 2-byte VBAR_CACHE_HIT: only the first (52-row) and last (5-row, a
256 * different height so it can't match the cache) bands pay the full raw per-column cost. A
257 * residual-only encoding of this per-row-varying content would cost close to the raw BGRX32
258 * size (4 bytes/pixel); confirm the V-Bar cache brought the total well under half of that. */
259 if (compressedSize >= (1ULL * srcStep * height) / 2)
260 {
261 (void)fprintf(stderr,
262 "clear bands compressedSize %" PRIu32 " did not benefit from caching (raw "
263 "size %" PRIu32 "x%" PRIu32 " * 4)\n",
264 compressedSize, width, height);
265 goto fail;
266 }
267
268 rc = TRUE;
269fail:
270 clear_context_free(clearEnc);
271 clear_context_free(clearDec);
272 free(pSrcData);
273 free(pDstData);
274 return rc;
275}
276
277/* A smooth gradient where nearly every pixel has its own color: encoded with a single NSCodec
278 * subcodec. The width is not a multiple of 8 and the source buffer has no padding. */
279static BOOL test_ClearCompressNSCodecRoundtrip(UINT32 SrcFormat, UINT32 width, UINT32 height)
280{
281 BOOL rc = FALSE;
282 BYTE* pCompressed = nullptr;
283 UINT32 compressedSize = 0;
284 const UINT32 bpp = FreeRDPGetBytesPerPixel(SrcFormat);
285 const UINT32 srcStep = width * bpp;
286 const UINT32 dstStep = width * 4;
287 BYTE* pSrcData = calloc(1ULL * srcStep, height);
288 BYTE* pRefData = calloc(1ULL * dstStep, height);
289 BYTE* pDstData = calloc(1ULL * dstStep, height);
290 CLEAR_CONTEXT* clearEnc = clear_context_new(TRUE);
291 CLEAR_CONTEXT* clearDec = clear_context_new(FALSE);
292
293 if (!pSrcData || !pRefData || !pDstData || !clearEnc || !clearDec)
294 goto fail;
295
296 for (UINT32 y = 0; y < height; y++)
297 {
298 for (UINT32 x = 0; x < width; x++)
299 {
300 const BYTE r = WINPR_ASSERTING_INT_CAST(BYTE, (x * 2) & 0xFF);
301 const BYTE g = WINPR_ASSERTING_INT_CAST(BYTE, (y * 2) & 0xFF);
302 const BYTE b = WINPR_ASSERTING_INT_CAST(BYTE, ((x + y) / 2) & 0xFF);
303 FreeRDPWriteColor(&pSrcData[1ULL * y * srcStep + 1ULL * x * bpp], SrcFormat,
304 FreeRDPGetColor(SrcFormat, r, g, b, 0x80));
305 FreeRDPWriteColor(&pRefData[1ULL * y * dstStep + 4ULL * x], PIXEL_FORMAT_BGRX32,
306 FreeRDPGetColor(PIXEL_FORMAT_BGRX32, r, g, b, 0xFF));
307 }
308 }
309
310 if (!clear_compress(clearEnc, pSrcData, SrcFormat, srcStep, width, height, &pCompressed,
311 &compressedSize))
312 {
313 (void)fprintf(stderr, "clear_compress (nscodec) failed for %" PRIu32 "x%" PRIu32 "\n",
314 width, height);
315 goto fail;
316 }
317
318 if (test_clear_subcodec_size(pCompressed, compressedSize) == 0)
319 {
320 (void)fprintf(stderr, "clear (nscodec): no subcodec layer for %" PRIu32 "x%" PRIu32 "\n",
321 width, height);
322 goto fail;
323 }
324
325 if (clear_decompress(clearDec, pCompressed, compressedSize, width, height, pDstData,
326 PIXEL_FORMAT_BGRX32, dstStep, 0, 0, width, height, nullptr) != 0)
327 {
328 (void)fprintf(stderr, "clear_decompress (nscodec) failed for %" PRIu32 "x%" PRIu32 "\n",
329 width, height);
330 goto fail;
331 }
332
333 if (!test_clear_compare(pRefData, pDstData, width, height, CLEAR_TEST_NSC_TOLERANCE))
334 {
335 (void)fprintf(stderr, "clear nscodec roundtrip mismatch for %" PRIu32 "x%" PRIu32 "\n",
336 width, height);
337 goto fail;
338 }
339
340 rc = TRUE;
341fail:
342 clear_context_free(clearEnc);
343 clear_context_free(clearDec);
344 free(pSrcData);
345 free(pRefData);
346 free(pDstData);
347 return rc;
348}
349
350int TestFreeRDPCodecClear(int argc, char* argv[])
351{
352 WINPR_UNUSED(argc);
353 WINPR_UNUSED(argv);
354
355 /* Example 1 needs a filled glyph cache
356 if (!test_ClearDecompressExample(1, 8, 9, TEST_CLEAR_EXAMPLE_1,
357 sizeof(TEST_CLEAR_EXAMPLE_1)))
358 return -1;
359 */
360 if (!test_ClearDecompressExample(2, 78, 17, TEST_CLEAR_EXAMPLE_2, sizeof(TEST_CLEAR_EXAMPLE_2)))
361 return -1;
362
363 if (!test_ClearDecompressExample(3, 64, 24, TEST_CLEAR_EXAMPLE_3, sizeof(TEST_CLEAR_EXAMPLE_3)))
364 return -1;
365
366 if (!test_ClearDecompressExample(4, 7, 15, TEST_CLEAR_EXAMPLE_4, sizeof(TEST_CLEAR_EXAMPLE_4)))
367 return -1;
368
369 if (!test_ClearCompressDecompressRoundtrip(64, 48))
370 return -1;
371
372 if (!test_ClearCompressDecompressRoundtrip(1, 1))
373 return -1;
374
375 if (!test_ClearCompressDecompressRoundtrip(3, 200))
376 return -1;
377
378 if (!test_ClearCompressBandsRoundtrip())
379 return -1;
380
381 if (!test_ClearCompressNSCodecRoundtrip(PIXEL_FORMAT_BGRX32, 61, 37))
382 return -1;
383
384 if (!test_ClearCompressNSCodecRoundtrip(PIXEL_FORMAT_ARGB32, 100, 70))
385 return -1;
386
387 if (!test_ClearCompressNSCodecRoundtrip(PIXEL_FORMAT_RGB24, 13, 9))
388 return -1;
389
390 return 0;
391}