23#include <freerdp/config.h>
26#include <winpr/print.h>
27#include <winpr/bitstream.h>
29#include <freerdp/codec/color.h>
30#include <freerdp/codec/clear.h>
31#include <freerdp/log.h>
33#define TAG FREERDP_TAG("codec.clear")
35#define CLEARCODEC_FLAG_GLYPH_INDEX 0x01
36#define CLEARCODEC_FLAG_GLYPH_HIT 0x02
37#define CLEARCODEC_FLAG_CACHE_RESET 0x04
39#define CLEARCODEC_VBAR_SIZE 32768
40#define CLEARCODEC_VBAR_SHORT_SIZE 16384
46#define CLEAR_VBAR_HASH_SIZE 65536u
47#define CLEAR_VBAR_HASH_EMPTY 0u
48#define CLEAR_VBAR_HASH_DELETED 0xFFFFFFFFu
49#define CLEAR_BAND_MAX_HEIGHT 52u
74 CLEAR_GLYPH_ENTRY GlyphCache[4000];
75 UINT32 VBarStorageCursor;
76 CLEAR_VBAR_ENTRY VBarStorage[CLEARCODEC_VBAR_SIZE];
77 UINT32 ShortVBarStorageCursor;
78 CLEAR_VBAR_ENTRY ShortVBarStorage[CLEARCODEC_VBAR_SHORT_SIZE];
81 UINT32* VBarHashSlots;
93static const UINT32 CLEAR_LOG2_FLOOR[256] = {
94 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
95 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
96 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
97 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
98 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
99 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
100 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
101 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
104static const BYTE CLEAR_8BIT_MASKS[9] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
106static void clear_reset_vbar_storage(CLEAR_CONTEXT* WINPR_RESTRICT clear, BOOL zero)
110 for (
size_t i = 0; i < ARRAYSIZE(clear->VBarStorage); i++)
111 winpr_aligned_free(clear->VBarStorage[i].pixels);
113 ZeroMemory(clear->VBarStorage,
sizeof(clear->VBarStorage));
115 if (clear->VBarHashSlots)
116 ZeroMemory(clear->VBarHashSlots, CLEAR_VBAR_HASH_SIZE *
sizeof(UINT32));
119 clear->VBarStorageCursor = 0;
123 for (
size_t i = 0; i < ARRAYSIZE(clear->ShortVBarStorage); i++)
124 winpr_aligned_free(clear->ShortVBarStorage[i].pixels);
126 ZeroMemory(clear->ShortVBarStorage,
sizeof(clear->ShortVBarStorage));
129 clear->ShortVBarStorageCursor = 0;
132static void clear_reset_glyph_cache(CLEAR_CONTEXT* WINPR_RESTRICT clear)
134 for (
size_t i = 0; i < ARRAYSIZE(clear->GlyphCache); i++)
135 winpr_aligned_free(clear->GlyphCache[i].pixels);
137 ZeroMemory(clear->GlyphCache,
sizeof(clear->GlyphCache));
140static BOOL convert_color(BYTE* WINPR_RESTRICT dst, UINT32 nDstStep, UINT32 DstFormat, UINT32 nXDst,
141 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
142 const BYTE* WINPR_RESTRICT src, UINT32 nSrcStep, UINT32 SrcFormat,
143 UINT32 nDstWidth, UINT32 nDstHeight,
144 const gdiPalette* WINPR_RESTRICT palette)
146 if (nWidth + nXDst > nDstWidth)
147 nWidth = nDstWidth - nXDst;
149 if (nHeight + nYDst > nDstHeight)
150 nHeight = nDstHeight - nYDst;
152 return freerdp_image_copy_no_overlap(dst, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
153 src, SrcFormat, nSrcStep, 0, 0, palette,
154 FREERDP_KEEP_DST_ALPHA);
157static BOOL clear_decompress_nscodec(wLog* log, NSC_CONTEXT* WINPR_RESTRICT nsc, UINT32 width,
158 UINT32 height,
wStream* WINPR_RESTRICT s,
159 UINT32 bitmapDataByteCount, BYTE* WINPR_RESTRICT pDstData,
160 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDstRel,
161 UINT32 nYDstRel, UINT32 nDstWidth, UINT32 nDstHeight)
165 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, bitmapDataByteCount))
168 rc = nsc_process_message(nsc, 32, width, height, Stream_Pointer(s), bitmapDataByteCount,
169 pDstData, DstFormat, nDstStep, nXDstRel, nYDstRel, nDstWidth,
170 nDstHeight, FREERDP_FLIP_NONE);
171 Stream_Seek(s, bitmapDataByteCount);
175static BOOL clear_decompress_subcode_rlex(wLog* log,
wStream* WINPR_RESTRICT s,
176 UINT32 bitmapDataByteCount, UINT32 width, UINT32 height,
177 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
178 UINT32 nDstStep, UINT32 nXDstRel, UINT32 nYDstRel,
179 UINT32 nDstWidth, UINT32 nDstHeight)
183 UINT32 bitmapDataOffset = 0;
184 BYTE paletteCount = 0;
185 UINT32 palette[128] = WINPR_C_ARRAY_INIT;
187 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, bitmapDataByteCount))
190 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 1))
192 Stream_Read_UINT8(s, paletteCount);
193 bitmapDataOffset = 1 + (paletteCount * 3);
195 if ((paletteCount > 127) || (paletteCount < 1))
197 WLog_Print(log, WLOG_ERROR,
"paletteCount %" PRIu8
"", paletteCount);
201 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(log, s, paletteCount, 3ull))
204 for (UINT32 i = 0; i < paletteCount; i++)
206 const BYTE b = Stream_Get_UINT8(s);
207 const BYTE g = Stream_Get_UINT8(s);
208 const BYTE r = Stream_Get_UINT8(s);
209 palette[i] = FreeRDPGetColor(DstFormat, r, g, b, 0xFF);
212 UINT32 pixelIndex = 0;
213 if ((height == 0) || (width == 0) || (width > UINT32_MAX / height))
216 const UINT32 pixelCount = width * height;
217 const UINT32 numBits = CLEAR_LOG2_FLOOR[paletteCount - 1] + 1;
219 while (bitmapDataOffset < bitmapDataByteCount)
221 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 2))
224 const UINT32 tmp = Stream_Get_UINT8(s);
225 UINT32 runLengthFactor = Stream_Get_UINT8(s);
227 bitmapDataOffset += 2;
228 const BYTE suiteDepth = (tmp >> numBits) & CLEAR_8BIT_MASKS[(8 - numBits)];
229 const BYTE stopIndex = tmp & CLEAR_8BIT_MASKS[numBits];
230 const BYTE startIndex = stopIndex - suiteDepth;
232 if (runLengthFactor >= 0xFF)
234 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 2))
237 Stream_Read_UINT16(s, runLengthFactor);
238 bitmapDataOffset += 2;
240 if (runLengthFactor >= 0xFFFF)
242 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
245 Stream_Read_UINT32(s, runLengthFactor);
246 bitmapDataOffset += 4;
250 if (startIndex >= paletteCount)
252 WLog_Print(log, WLOG_ERROR,
"startIndex %" PRIu8
" > paletteCount %" PRIu8
"]",
253 startIndex, paletteCount);
257 if (stopIndex >= paletteCount)
259 WLog_Print(log, WLOG_ERROR,
"stopIndex %" PRIu8
" > paletteCount %" PRIu8
"]",
260 stopIndex, paletteCount);
264 BYTE suiteIndex = startIndex;
266 if (suiteIndex > 127)
268 WLog_Print(log, WLOG_ERROR,
"suiteIndex %" PRIu8
" > 127]", suiteIndex);
272 const UINT32 color = palette[suiteIndex];
273 if ((pixelIndex > UINT32_MAX - runLengthFactor) ||
274 ((pixelIndex + runLengthFactor) > pixelCount))
276 WLog_Print(log, WLOG_ERROR,
277 "pixelIndex %" PRIu32
" + runLengthFactor %" PRIu32
" > pixelCount %" PRIu32
279 pixelIndex, runLengthFactor, pixelCount);
283 for (UINT32 i = 0; i < runLengthFactor; i++)
285 BYTE* pTmpData = &pDstData[(nXDstRel + x) * FreeRDPGetBytesPerPixel(DstFormat) +
286 (nYDstRel + y) * nDstStep];
288 if ((nXDstRel + x < nDstWidth) && (nYDstRel + y < nDstHeight))
289 FreeRDPWriteColor(pTmpData, DstFormat, color);
298 pixelIndex += runLengthFactor;
300 if ((pixelIndex > UINT32_MAX - 1 - suiteDepth) ||
301 ((pixelIndex + (suiteDepth + 1)) > pixelCount))
303 WLog_Print(log, WLOG_ERROR,
304 "pixelIndex %" PRIu32
" + suiteDepth %" PRIu8
" + 1 > pixelCount %" PRIu32
306 pixelIndex, suiteDepth, pixelCount);
310 for (UINT32 i = 0; i <= suiteDepth; i++)
312 BYTE* pTmpData = &pDstData[(nXDstRel + x) * FreeRDPGetBytesPerPixel(DstFormat) +
313 (nYDstRel + y) * nDstStep];
314 UINT32 ccolor = palette[suiteIndex];
316 if (suiteIndex > 127)
318 WLog_Print(log, WLOG_ERROR,
"suiteIndex %" PRIu8
" > 127", suiteIndex);
324 if ((nXDstRel + x < nDstWidth) && (nYDstRel + y < nDstHeight))
325 FreeRDPWriteColor(pTmpData, DstFormat, ccolor);
334 pixelIndex += (suiteDepth + 1);
337 if (pixelIndex != pixelCount)
339 WLog_Print(log, WLOG_ERROR,
"pixelIndex %" PRIu32
" != pixelCount %" PRIu32
"", pixelIndex,
347static BOOL clear_resize_buffer(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 width, UINT32 height)
352 const UINT64 size = 1ull * (width + 16ull) * (height + 16ull);
353 const size_t bpp = FreeRDPGetBytesPerPixel(clear->format);
354 if (size > UINT32_MAX / bpp)
357 if (size > clear->TempSize / bpp)
359 BYTE* tmp = (BYTE*)winpr_aligned_recalloc(clear->TempBuffer,
360 WINPR_ASSERTING_INT_CAST(
size_t, size), bpp, 32);
364 WLog_Print(clear->log, WLOG_ERROR,
365 "clear->TempBuffer winpr_aligned_recalloc failed for %" PRIu64
" bytes",
370 clear->TempSize = WINPR_ASSERTING_INT_CAST(
size_t, size* bpp);
371 clear->TempBuffer = tmp;
377static BOOL clear_decompress_residual_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
378 wStream* WINPR_RESTRICT s, UINT32 residualByteCount,
379 UINT32 nWidth, UINT32 nHeight,
380 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
381 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
382 UINT32 nDstWidth, UINT32 nDstHeight,
383 const gdiPalette* WINPR_RESTRICT palette)
386 UINT32 suboffset = 0;
387 BYTE* dstBuffer =
nullptr;
388 UINT32 pixelIndex = 0;
389 UINT32 pixelCount = 0;
391 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, residualByteCount))
396 pixelCount = nWidth * nHeight;
398 if (!clear_resize_buffer(clear, nWidth, nHeight))
401 dstBuffer = clear->TempBuffer;
403 while (suboffset < residualByteCount)
408 UINT32 runLengthFactor = 0;
411 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 4))
414 Stream_Read_UINT8(s, b);
415 Stream_Read_UINT8(s, g);
416 Stream_Read_UINT8(s, r);
417 Stream_Read_UINT8(s, runLengthFactor);
419 color = FreeRDPGetColor(clear->format, r, g, b, 0xFF);
421 if (runLengthFactor >= 0xFF)
423 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 2))
426 Stream_Read_UINT16(s, runLengthFactor);
429 if (runLengthFactor >= 0xFFFF)
431 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 4))
434 Stream_Read_UINT32(s, runLengthFactor);
439 if ((pixelIndex >= pixelCount) || (runLengthFactor > (pixelCount - pixelIndex)))
441 WLog_Print(clear->log, WLOG_ERROR,
442 "pixelIndex %" PRIu32
" + runLengthFactor %" PRIu32
" > pixelCount %" PRIu32
444 pixelIndex, runLengthFactor, pixelCount);
448 for (UINT32 i = 0; i < runLengthFactor; i++)
450 FreeRDPWriteColor(dstBuffer, clear->format, color);
451 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
454 pixelIndex += runLengthFactor;
457 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format);
459 if (pixelIndex != pixelCount)
461 WLog_Print(clear->log, WLOG_ERROR,
"pixelIndex %" PRIu32
" != pixelCount %" PRIu32
"",
462 pixelIndex, pixelCount);
466 return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight,
467 clear->TempBuffer, nSrcStep, clear->format, nDstWidth, nDstHeight,
471static BOOL clear_decompress_subcodecs_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
472 wStream* WINPR_RESTRICT s, UINT32 subcodecByteCount,
473 UINT32 nWidth, UINT32 nHeight,
474 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
475 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
476 UINT32 nDstWidth, UINT32 nDstHeight,
477 const gdiPalette* WINPR_RESTRICT palette)
479 UINT32 suboffset = 0;
481 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, subcodecByteCount))
484 while (suboffset < subcodecByteCount)
486 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 13))
489 const UINT16 xStart = Stream_Get_UINT16(s);
490 const UINT16 yStart = Stream_Get_UINT16(s);
491 const UINT16 width = Stream_Get_UINT16(s);
492 const UINT16 height = Stream_Get_UINT16(s);
493 const UINT32 bitmapDataByteCount = Stream_Get_UINT32(s);
494 const UINT8 subcodecId = Stream_Get_UINT8(s);
497 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, bitmapDataByteCount))
500 const UINT32 nXDstRel = nXDst + xStart;
501 const UINT32 nYDstRel = nYDst + yStart;
502 if (1ull * nXDstRel + width > nDstWidth)
504 WLog_Print(clear->log, WLOG_ERROR,
505 "nXDstRel %" PRIu32
" + width %" PRIu16
" > nDstWidth %" PRIu32
"", nXDstRel,
509 if (1ull * nYDstRel + height > nDstHeight)
511 WLog_Print(clear->log, WLOG_ERROR,
512 "nYDstRel %" PRIu32
" + height %" PRIu16
" > nDstHeight %" PRIu32
"",
513 nYDstRel, height, nDstHeight);
517 if (1ull * xStart + width > nWidth)
519 WLog_Print(clear->log, WLOG_ERROR,
520 "xStart %" PRIu16
" + width %" PRIu16
" > nWidth %" PRIu32
"", xStart, width,
524 if (1ull * yStart + height > nHeight)
526 WLog_Print(clear->log, WLOG_ERROR,
527 "yStart %" PRIu16
" + height %" PRIu16
" > nHeight %" PRIu32
"", yStart,
532 if (!clear_resize_buffer(clear, width, height))
539 const UINT32 nSrcStep = width * FreeRDPGetBytesPerPixel(PIXEL_FORMAT_BGR24);
540 const size_t nSrcSize = 1ull * nSrcStep * height;
542 if (bitmapDataByteCount != nSrcSize)
544 WLog_Print(clear->log, WLOG_ERROR,
545 "bitmapDataByteCount %" PRIu32
" != nSrcSize %" PRIuz
"",
546 bitmapDataByteCount, nSrcSize);
550 if (!convert_color(pDstData, nDstStep, DstFormat, nXDstRel, nYDstRel, width, height,
551 Stream_Pointer(s), nSrcStep, PIXEL_FORMAT_BGR24, nDstWidth,
552 nDstHeight, palette))
555 Stream_Seek(s, bitmapDataByteCount);
560 if (!clear_decompress_nscodec(clear->log, clear->nsc, width, height, s,
561 bitmapDataByteCount, pDstData, DstFormat, nDstStep,
562 nXDstRel, nYDstRel, nDstWidth, nDstHeight))
568 if (!clear_decompress_subcode_rlex(clear->log, s, bitmapDataByteCount, width,
569 height, pDstData, DstFormat, nDstStep, nXDstRel,
570 nYDstRel, nDstWidth, nDstHeight))
576 WLog_Print(clear->log, WLOG_ERROR,
"Unknown subcodec ID %" PRIu8
"", subcodecId);
580 suboffset += bitmapDataByteCount;
586static BOOL resize_vbar_entry(CLEAR_CONTEXT* WINPR_RESTRICT clear,
587 CLEAR_VBAR_ENTRY* WINPR_RESTRICT vBarEntry, UINT32 bpp)
589 if (vBarEntry->count > vBarEntry->size)
591 const UINT32 oldPos = vBarEntry->size * bpp;
592 const UINT32 diffSize = (vBarEntry->count - vBarEntry->size) * bpp;
595 (BYTE*)winpr_aligned_recalloc(vBarEntry->pixels, vBarEntry->count, 1ull * bpp, 32);
599 WLog_Print(clear->log, WLOG_ERROR,
600 "vBarEntry->pixels winpr_aligned_recalloc %" PRIu32
" failed",
601 vBarEntry->count * bpp);
605 memset(&tmp[oldPos], 0, diffSize);
606 vBarEntry->pixels = tmp;
607 vBarEntry->size = vBarEntry->count;
610 if (!vBarEntry->pixels && vBarEntry->size)
612 WLog_Print(clear->log, WLOG_ERROR,
613 "vBarEntry->pixels is nullptr but vBarEntry->size is %" PRIu32
"",
621static BOOL clear_decompress_bands_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
622 wStream* WINPR_RESTRICT s, UINT32 bandsByteCount,
623 UINT32 nWidth, UINT32 nHeight,
624 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
625 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
626 UINT32 nDstWidth, UINT32 nDstHeight)
628 UINT32 suboffset = 0;
630 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, bandsByteCount))
633 while (suboffset < bandsByteCount)
643 UINT16 vBarHeader = 0;
646 UINT32 vBarCount = 0;
647 UINT32 vBarPixelCount = 0;
648 UINT32 vBarShortPixelCount = 0;
650 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 11))
653 Stream_Read_UINT16(s, xStart);
654 Stream_Read_UINT16(s, xEnd);
655 Stream_Read_UINT16(s, yStart);
656 Stream_Read_UINT16(s, yEnd);
657 Stream_Read_UINT8(s, cb);
658 Stream_Read_UINT8(s, cg);
659 Stream_Read_UINT8(s, cr);
661 colorBkg = FreeRDPGetColor(clear->format, cr, cg, cb, 0xFF);
665 WLog_Print(clear->log, WLOG_ERROR,
"xEnd %" PRIu16
" < xStart %" PRIu16
"", xEnd,
672 WLog_Print(clear->log, WLOG_ERROR,
"yEnd %" PRIu16
" < yStart %" PRIu16
"", yEnd,
677 vBarCount = (xEnd - xStart) + 1;
679 for (UINT32 i = 0; i < vBarCount; i++)
681 UINT32 vBarHeight = 0;
682 CLEAR_VBAR_ENTRY* vBarEntry =
nullptr;
683 CLEAR_VBAR_ENTRY* vBarShortEntry =
nullptr;
684 BOOL vBarUpdate = FALSE;
685 const BYTE* cpSrcPixel =
nullptr;
687 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 2))
690 Stream_Read_UINT16(s, vBarHeader);
692 vBarHeight = (yEnd - yStart + 1);
696 WLog_Print(clear->log, WLOG_ERROR,
"vBarHeight (%" PRIu32
") > 52", vBarHeight);
700 if ((vBarHeader & 0xC000) == 0x4000)
702 const UINT16 vBarIndex = (vBarHeader & 0x3FFF);
703 vBarShortEntry = &(clear->ShortVBarStorage[vBarIndex]);
707 WLog_Print(clear->log, WLOG_ERROR,
"missing vBarShortEntry %" PRIu16
"",
712 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 1))
715 Stream_Read_UINT8(s, vBarYOn);
717 vBarShortPixelCount = vBarShortEntry->count;
720 else if ((vBarHeader & 0xC000) == 0x0000)
722 vBarYOn = (vBarHeader & 0xFF);
723 vBarYOff = ((vBarHeader >> 8) & 0x3F);
725 if (vBarYOff < vBarYOn)
727 WLog_Print(clear->log, WLOG_ERROR,
"vBarYOff %" PRIu16
" < vBarYOn %" PRIu16
"",
732 vBarShortPixelCount = (vBarYOff - vBarYOn);
734 if (vBarShortPixelCount > 52)
736 WLog_Print(clear->log, WLOG_ERROR,
"vBarShortPixelCount %" PRIu32
" > 52",
737 vBarShortPixelCount);
741 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(clear->log, s, vBarShortPixelCount,
745 if (clear->ShortVBarStorageCursor >= CLEARCODEC_VBAR_SHORT_SIZE)
747 WLog_Print(clear->log, WLOG_ERROR,
748 "clear->ShortVBarStorageCursor %" PRIu32
749 " >= CLEARCODEC_VBAR_SHORT_SIZE (%" PRId32
")",
750 clear->ShortVBarStorageCursor, CLEARCODEC_VBAR_SHORT_SIZE);
754 vBarShortEntry = &(clear->ShortVBarStorage[clear->ShortVBarStorageCursor]);
755 vBarShortEntry->count = vBarShortPixelCount;
757 if (!resize_vbar_entry(clear, vBarShortEntry,
758 FreeRDPGetBytesPerPixel(clear->format)))
761 for (
size_t y = 0; y < vBarShortPixelCount; y++)
767 &vBarShortEntry->pixels[y * FreeRDPGetBytesPerPixel(clear->format)];
769 Stream_Read_UINT8(s, b);
770 Stream_Read_UINT8(s, g);
771 Stream_Read_UINT8(s, r);
772 color = FreeRDPGetColor(clear->format, r, g, b, 0xFF);
774 if (!FreeRDPWriteColor(dstBuffer, clear->format, color))
778 suboffset += (vBarShortPixelCount * 3);
779 clear->ShortVBarStorageCursor =
780 (clear->ShortVBarStorageCursor + 1) % CLEARCODEC_VBAR_SHORT_SIZE;
783 else if ((vBarHeader & 0x8000) == 0x8000)
785 const UINT16 vBarIndex = (vBarHeader & 0x7FFF);
786 vBarEntry = &(clear->VBarStorage[vBarIndex]);
789 if (vBarEntry->size == 0)
791 WLog_Print(clear->log, WLOG_WARN,
792 "Empty cache index %" PRIu16
", filling dummy data", vBarIndex);
793 vBarEntry->count = vBarHeight;
795 if (!resize_vbar_entry(clear, vBarEntry,
796 FreeRDPGetBytesPerPixel(clear->format)))
802 WLog_Print(clear->log, WLOG_ERROR,
"invalid vBarHeader 0x%04" PRIX16
"",
809 BYTE* pSrcPixel =
nullptr;
810 BYTE* dstBuffer =
nullptr;
812 if (clear->VBarStorageCursor >= CLEARCODEC_VBAR_SIZE)
814 WLog_Print(clear->log, WLOG_ERROR,
815 "clear->VBarStorageCursor %" PRIu32
816 " >= CLEARCODEC_VBAR_SIZE %" PRId32
"",
817 clear->VBarStorageCursor, CLEARCODEC_VBAR_SIZE);
821 vBarEntry = &(clear->VBarStorage[clear->VBarStorageCursor]);
822 vBarPixelCount = vBarHeight;
823 vBarEntry->count = vBarPixelCount;
825 if (!resize_vbar_entry(clear, vBarEntry, FreeRDPGetBytesPerPixel(clear->format)))
828 dstBuffer = vBarEntry->pixels;
831 UINT32 count = vBarYOn;
833 if ((y + count) > vBarPixelCount)
834 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
840 FreeRDPWriteColor(dstBuffer, clear->format, colorBkg);
841 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
850 count = vBarShortPixelCount;
852 if ((y + count) > vBarPixelCount)
853 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
857 const size_t offset =
858 (1ull * y - vBarYOn) * FreeRDPGetBytesPerPixel(clear->format);
859 pSrcPixel = &vBarShortEntry->pixels[offset];
860 if (offset + count > vBarShortEntry->count)
862 WLog_Print(clear->log, WLOG_ERROR,
863 "offset + count > vBarShortEntry->count");
867 for (
size_t x = 0; x < count; x++)
870 color = FreeRDPReadColor(&pSrcPixel[x * FreeRDPGetBytesPerPixel(clear->format)],
873 if (!FreeRDPWriteColor(dstBuffer, clear->format, color))
876 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
880 y = vBarYOn + vBarShortPixelCount;
881 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
887 if (!FreeRDPWriteColor(dstBuffer, clear->format, colorBkg))
890 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
894 vBarEntry->count = vBarPixelCount;
895 clear->VBarStorageCursor = (clear->VBarStorageCursor + 1) % CLEARCODEC_VBAR_SIZE;
898 if (vBarEntry->count != vBarHeight)
900 WLog_Print(clear->log, WLOG_ERROR,
901 "vBarEntry->count %" PRIu32
" != vBarHeight %" PRIu32
"",
902 vBarEntry->count, vBarHeight);
903 vBarEntry->count = vBarHeight;
905 if (!resize_vbar_entry(clear, vBarEntry, FreeRDPGetBytesPerPixel(clear->format)))
909 const UINT32 nXDstRel = nXDst + xStart;
910 const UINT32 nYDstRel = nYDst + yStart;
911 cpSrcPixel = vBarEntry->pixels;
915 UINT32 count = vBarEntry->count;
920 if (nXDstRel + i >= nDstWidth)
923 for (UINT32 y = 0; y < count; y++)
925 if (nYDstRel + y >= nDstHeight)
929 &pDstData[((nYDstRel + y) * nDstStep) +
930 ((nXDstRel + i) * FreeRDPGetBytesPerPixel(DstFormat))];
931 UINT32 color = FreeRDPReadColor(cpSrcPixel, clear->format);
932 color = FreeRDPConvertColor(color, clear->format, DstFormat,
nullptr);
934 if (!FreeRDPWriteColor(pDstPixel8, DstFormat, color))
937 cpSrcPixel += FreeRDPGetBytesPerPixel(clear->format);
946static BOOL clear_decompress_glyph_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
947 wStream* WINPR_RESTRICT s, UINT32 glyphFlags, UINT32 nWidth,
948 UINT32 nHeight, BYTE* WINPR_RESTRICT pDstData,
949 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
950 UINT32 nYDst, UINT32 nDstWidth, UINT32 nDstHeight,
951 const gdiPalette* WINPR_RESTRICT palette,
952 BYTE** WINPR_RESTRICT ppGlyphData)
954 UINT16 glyphIndex = 0;
957 *ppGlyphData =
nullptr;
959 if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT) && !(glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX))
961 WLog_Print(clear->log, WLOG_ERROR,
"Invalid glyph flags %08" PRIX32
"", glyphFlags);
965 if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX) == 0)
968 if ((nWidth * nHeight) > (1024 * 1024))
970 WLog_Print(clear->log, WLOG_ERROR,
"glyph too large: %" PRIu32
"x%" PRIu32
"", nWidth,
975 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 2))
978 Stream_Read_UINT16(s, glyphIndex);
980 if (glyphIndex >= 4000)
982 WLog_Print(clear->log, WLOG_ERROR,
"Invalid glyphIndex %" PRIu16
"", glyphIndex);
986 if (glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT)
989 CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]);
990 BYTE* glyphData =
nullptr;
994 WLog_Print(clear->log, WLOG_ERROR,
"clear->GlyphCache[%" PRIu16
"]=nullptr",
999 glyphData = (BYTE*)glyphEntry->pixels;
1003 WLog_Print(clear->log, WLOG_ERROR,
"clear->GlyphCache[%" PRIu16
"]->pixels=nullptr",
1008 if ((nWidth * nHeight) > glyphEntry->count)
1010 WLog_Print(clear->log, WLOG_ERROR,
1011 "(nWidth %" PRIu32
" * nHeight %" PRIu32
") > glyphEntry->count %" PRIu32
"",
1012 nWidth, nHeight, glyphEntry->count);
1016 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format);
1017 return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight,
1018 glyphData, nSrcStep, clear->format, nDstWidth, nDstHeight, palette);
1021 if (glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX)
1023 const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format);
1024 CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]);
1025 const size_t count = 1ull * nWidth * nHeight;
1026 const size_t hlimit = SIZE_MAX / ((nWidth > 0) ? nWidth : 1);
1027 if ((nWidth == 0) || (nHeight == 0) || (hlimit < nHeight))
1029 const char* exceeded = (hlimit < nHeight) ?
"within" :
"outside";
1030 WLog_Print(clear->log, WLOG_ERROR,
1031 "CLEARCODEC_FLAG_GLYPH_INDEX: nWidth=%" PRIu32
", nHeight=%" PRIu32
1032 ", nWidth * nHeight is %s allowed range",
1033 nWidth, nHeight, exceeded);
1037 if (count > glyphEntry->size)
1039 BYTE* tmp = winpr_aligned_recalloc(glyphEntry->pixels, count, 1ull * bpp, 32);
1043 WLog_Print(clear->log, WLOG_ERROR,
1044 "glyphEntry->pixels winpr_aligned_recalloc %" PRIuz
" failed!",
1049 glyphEntry->count = WINPR_ASSERTING_INT_CAST(UINT32, count);
1050 glyphEntry->size = glyphEntry->count;
1051 glyphEntry->pixels = WINPR_PACKED_ALIGN_CAST(UINT32*, tmp);
1054 if (!glyphEntry->pixels)
1056 WLog_Print(clear->log, WLOG_ERROR,
"glyphEntry->pixels=nullptr");
1061 *ppGlyphData = (BYTE*)glyphEntry->pixels;
1069static inline BOOL updateContextFormat(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 DstFormat,
1072 if (!clear || !clear->nsc)
1077 if (clear->formatSet)
1079 if (!FreeRDPAreColorFormatsEqualNoAlpha(clear->format, DstFormat))
1082 clear->log, WLOG_ERROR,
1083 "Color format changed from %s to %s during decompression calls, usage error!",
1084 FreeRDPGetColorFormatName(clear->format), FreeRDPGetColorFormatName(DstFormat));
1089 clear->formatSet = TRUE;
1091 clear->format = DstFormat;
1092 return nsc_context_set_parameters(clear->nsc, NSC_COLOR_FORMAT, DstFormat);
1095INT32 clear_decompress(CLEAR_CONTEXT* WINPR_RESTRICT clear,
const BYTE* WINPR_RESTRICT pSrcData,
1096 UINT32 SrcSize, UINT32 nWidth, UINT32 nHeight, BYTE* WINPR_RESTRICT pDstData,
1097 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
1098 UINT32 nDstWidth, UINT32 nDstHeight,
1099 const gdiPalette* WINPR_RESTRICT palette)
1103 BYTE glyphFlags = 0;
1104 UINT32 residualByteCount = 0;
1105 UINT32 bandsByteCount = 0;
1106 UINT32 subcodecByteCount = 0;
1107 wStream sbuffer = WINPR_C_ARRAY_INIT;
1108 BYTE* glyphData =
nullptr;
1113 if ((nDstWidth == 0) || (nDstHeight == 0))
1116 if ((nWidth > 0xFFFF) || (nHeight > 0xFFFF))
1119 if (nXDst > nDstWidth)
1121 WLog_Print(clear->log, WLOG_WARN,
"nXDst %" PRIu32
" > nDstWidth %" PRIu32, nXDst,
1126 if (nYDst > nDstHeight)
1128 WLog_Print(clear->log, WLOG_WARN,
"nYDst %" PRIu32
" > nDstHeight %" PRIu32, nYDst,
1133 wStream* s = Stream_StaticConstInit(&sbuffer, pSrcData, SrcSize);
1138 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 2))
1141 if (!updateContextFormat(clear, DstFormat, FALSE))
1144 Stream_Read_UINT8(s, glyphFlags);
1145 Stream_Read_UINT8(s, seqNumber);
1147 if (!clear->seqNumber && seqNumber)
1148 clear->seqNumber = seqNumber;
1150 if (seqNumber != clear->seqNumber)
1152 WLog_Print(clear->log, WLOG_ERROR,
"Sequence number unexpected %" PRIu8
" - %" PRIu32
"",
1153 seqNumber, clear->seqNumber);
1154 WLog_Print(clear->log, WLOG_ERROR,
"seqNumber %" PRIu8
" != clear->seqNumber %" PRIu32
"",
1155 seqNumber, clear->seqNumber);
1159 clear->seqNumber = (seqNumber + 1) % 256;
1161 if (glyphFlags & CLEARCODEC_FLAG_CACHE_RESET)
1163 clear_reset_vbar_storage(clear, FALSE);
1166 if (!clear_decompress_glyph_data(clear, s, glyphFlags, nWidth, nHeight, pDstData, DstFormat,
1167 nDstStep, nXDst, nYDst, nDstWidth, nDstHeight, palette,
1170 WLog_Print(clear->log, WLOG_ERROR,
"clear_decompress_glyph_data failed!");
1175 if (Stream_GetRemainingLength(s) < 12)
1177 const UINT32 mask = (CLEARCODEC_FLAG_GLYPH_HIT | CLEARCODEC_FLAG_GLYPH_INDEX);
1179 if ((glyphFlags & mask) == mask)
1182 WLog_Print(clear->log, WLOG_ERROR,
1183 "invalid glyphFlags, missing flags: 0x%02" PRIx8
" & 0x%02" PRIx32
1185 glyphFlags, mask, glyphFlags & mask);
1189 Stream_Read_UINT32(s, residualByteCount);
1190 Stream_Read_UINT32(s, bandsByteCount);
1191 Stream_Read_UINT32(s, subcodecByteCount);
1193 if (residualByteCount > 0)
1195 if (!clear_decompress_residual_data(clear, s, residualByteCount, nWidth, nHeight, pDstData,
1196 DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1197 nDstHeight, palette))
1199 WLog_Print(clear->log, WLOG_ERROR,
"clear_decompress_residual_data failed!");
1204 if (bandsByteCount > 0)
1206 if (!clear_decompress_bands_data(clear, s, bandsByteCount, nWidth, nHeight, pDstData,
1207 DstFormat, nDstStep, nXDst, nYDst, nDstWidth, nDstHeight))
1209 WLog_Print(clear->log, WLOG_ERROR,
"clear_decompress_bands_data failed!");
1214 if (subcodecByteCount > 0)
1216 if (!clear_decompress_subcodecs_data(clear, s, subcodecByteCount, nWidth, nHeight, pDstData,
1217 DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1218 nDstHeight, palette))
1220 WLog_Print(clear->log, WLOG_ERROR,
"clear_decompress_subcodecs_data failed!");
1227 uint32_t w = MIN(nWidth, nDstWidth);
1228 if (nXDst > nDstWidth)
1230 WLog_Print(clear->log, WLOG_WARN,
1231 "glyphData copy area x exceeds destination: x=%" PRIu32
" > %" PRIu32, nXDst,
1235 else if (nXDst + w > nDstWidth)
1237 WLog_Print(clear->log, WLOG_WARN,
1238 "glyphData copy area x + width exceeds destination: x=%" PRIu32
" + %" PRIu32
1240 nXDst, w, nDstWidth);
1241 w = nDstWidth - nXDst;
1246 WLog_Print(clear->log, WLOG_WARN,
1247 "glyphData copy area width truncated: requested=%" PRIu32
1248 ", truncated to %" PRIu32,
1252 uint32_t h = MIN(nHeight, nDstHeight);
1253 if (nYDst > nDstHeight)
1255 WLog_Print(clear->log, WLOG_WARN,
1256 "glyphData copy area y exceeds destination: y=%" PRIu32
" > %" PRIu32, nYDst,
1260 else if (nYDst + h > nDstHeight)
1262 WLog_Print(clear->log, WLOG_WARN,
1263 "glyphData copy area y + height exceeds destination: x=%" PRIu32
1264 " + %" PRIu32
" > %" PRIu32,
1265 nYDst, h, nDstHeight);
1266 h = nDstHeight - nYDst;
1271 WLog_Print(clear->log, WLOG_WARN,
1272 "glyphData copy area height truncated: requested=%" PRIu32
1273 ", truncated to %" PRIu32,
1277 if (!freerdp_image_copy_no_overlap(glyphData, clear->format, 0, 0, 0, w, h, pDstData,
1278 DstFormat, nDstStep, nXDst, nYDst, palette,
1279 FREERDP_KEEP_DST_ALPHA))
1291static void clear_write_run_segment(
wStream* WINPR_RESTRICT s, BYTE r, BYTE g, BYTE b,
1294 Stream_Write_UINT8(s, b);
1295 Stream_Write_UINT8(s, g);
1296 Stream_Write_UINT8(s, r);
1298 if (runLength < 0xFF)
1299 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(UINT8, runLength));
1300 else if (runLength < 0xFFFF)
1302 Stream_Write_UINT8(s, 0xFF);
1303 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(UINT16, runLength));
1307 Stream_Write_UINT8(s, 0xFF);
1308 Stream_Write_UINT16(s, 0xFFFF);
1309 Stream_Write_UINT32(s, runLength);
1316static BOOL clear_compress_residual_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
1318 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcFormat,
1319 UINT32 nSrcStep, UINT32 nWidth, UINT32 nHeight)
1321 const UINT32 bpp = FreeRDPGetBytesPerPixel(SrcFormat);
1322 const UINT64 pixelCount = 1ull * nWidth * nHeight;
1324 const UINT64 requiredBytes = pixelCount * 4ull;
1325 BOOL havePending = FALSE;
1329 UINT32 runLength = 0;
1331 if (!Stream_EnsureRemainingCapacity(s, WINPR_ASSERTING_INT_CAST(
size_t, requiredBytes)))
1333 WLog_Print(clear->log, WLOG_ERROR,
1334 "Stream_EnsureRemainingCapacity failed for %" PRIu64
" bytes", requiredBytes);
1338 for (UINT32 y = 0; y < nHeight; y++)
1340 const BYTE* pSrcPixel = &pSrcData[1ull * y * nSrcStep];
1342 for (UINT32 x = 0; x < nWidth; x++)
1348 const UINT32 color = FreeRDPReadColor(pSrcPixel, SrcFormat);
1350 FreeRDPSplitColor(color, SrcFormat, &r, &g, &b, &a,
nullptr);
1352 if (havePending && (r == pr) && (g == pg) && (b == pb))
1359 clear_write_run_segment(s, pr, pg, pb, runLength);
1370 clear_write_run_segment(s, pr, pg, pb, runLength);
1378static UINT64 clear_vbar_content_hash(
const BYTE* WINPR_RESTRICT data,
size_t len)
1380 UINT64 hash = 0xcbf29ce484222325ull;
1382 for (
size_t i = 0; i < len; i++)
1385 hash *= 0x100000001b3ull;
1394static UINT32 clear_vbar_hash_lookup(CLEAR_CONTEXT* WINPR_RESTRICT clear,
1395 const BYTE* WINPR_RESTRICT data, UINT32 count)
1397 const size_t len = 1ull * count * 3;
1398 const UINT64 hash = clear_vbar_content_hash(data, len);
1399 UINT32 i = WINPR_ASSERTING_INT_CAST(UINT32, hash & (CLEAR_VBAR_HASH_SIZE - 1));
1401 for (UINT32 probe = 0; probe < CLEAR_VBAR_HASH_SIZE; probe++)
1403 const UINT32 slot = clear->VBarHashSlots[i];
1405 if (slot == CLEAR_VBAR_HASH_EMPTY)
1408 if (slot != CLEAR_VBAR_HASH_DELETED)
1410 const UINT32 index = slot - 1;
1411 const CLEAR_VBAR_ENTRY* entry = &clear->VBarStorage[index];
1413 if ((entry->count == count) && (memcmp(entry->pixels, data, len) == 0))
1417 i = (i + 1) & (CLEAR_VBAR_HASH_SIZE - 1);
1426static void clear_vbar_hash_remove(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 index)
1428 const CLEAR_VBAR_ENTRY* entry = &clear->VBarStorage[index];
1430 if ((entry->count == 0) || !entry->pixels)
1433 const size_t len = 1ull * entry->count * 3;
1434 const UINT64 hash = clear_vbar_content_hash(entry->pixels, len);
1435 UINT32 i = WINPR_ASSERTING_INT_CAST(UINT32, hash & (CLEAR_VBAR_HASH_SIZE - 1));
1437 for (UINT32 probe = 0; probe < CLEAR_VBAR_HASH_SIZE; probe++)
1439 if (clear->VBarHashSlots[i] == (index + 1))
1441 clear->VBarHashSlots[i] = CLEAR_VBAR_HASH_DELETED;
1445 if (clear->VBarHashSlots[i] == CLEAR_VBAR_HASH_EMPTY)
1448 i = (i + 1) & (CLEAR_VBAR_HASH_SIZE - 1);
1453static void clear_vbar_hash_insert(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 index)
1455 const CLEAR_VBAR_ENTRY* entry = &clear->VBarStorage[index];
1456 const size_t len = 1ull * entry->count * 3;
1457 const UINT64 hash = clear_vbar_content_hash(entry->pixels, len);
1458 UINT32 i = WINPR_ASSERTING_INT_CAST(UINT32, hash & (CLEAR_VBAR_HASH_SIZE - 1));
1460 for (UINT32 probe = 0; probe < CLEAR_VBAR_HASH_SIZE; probe++)
1462 const UINT32 slot = clear->VBarHashSlots[i];
1464 if ((slot == CLEAR_VBAR_HASH_EMPTY) || (slot == CLEAR_VBAR_HASH_DELETED))
1466 clear->VBarHashSlots[i] = index + 1;
1470 i = (i + 1) & (CLEAR_VBAR_HASH_SIZE - 1);
1475static BOOL clear_compress_write_vbar_hit(
wStream* WINPR_RESTRICT s, UINT32 index)
1477 if (!Stream_EnsureRemainingCapacity(s, 2))
1480 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(UINT16, 0x8000u | index));
1488static BOOL clear_compress_write_vbar_miss(CLEAR_CONTEXT* WINPR_RESTRICT clear,
1490 const BYTE* WINPR_RESTRICT columnBgr, UINT32 count)
1492 if (!Stream_EnsureRemainingCapacity(s, 2ull + 1ull * count * 3))
1495 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(UINT16, count << 8));
1496 Stream_Write(s, columnBgr, 1ull * count * 3);
1498 const UINT32 index = clear->VBarStorageCursor;
1499 CLEAR_VBAR_ENTRY* entry = &clear->VBarStorage[index];
1501 clear_vbar_hash_remove(clear, index);
1502 entry->count = count;
1504 if (!resize_vbar_entry(clear, entry, 3))
1507 CopyMemory(entry->pixels, columnBgr, 1ull * count * 3);
1508 clear_vbar_hash_insert(clear, index);
1510 clear->VBarStorageCursor = (clear->VBarStorageCursor + 1) % CLEARCODEC_VBAR_SIZE;
1519static BOOL clear_compress_bands_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
1521 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcFormat,
1522 UINT32 nSrcStep, UINT32 nWidth, UINT32 nHeight)
1524 const UINT32 bpp = FreeRDPGetBytesPerPixel(SrcFormat);
1525 BYTE columnBgr[1ull * CLEAR_BAND_MAX_HEIGHT * 3];
1527 for (UINT32 yStart = 0; yStart < nHeight; yStart += CLEAR_BAND_MAX_HEIGHT)
1529 const UINT32 bandHeight = MIN(CLEAR_BAND_MAX_HEIGHT, nHeight - yStart);
1530 const UINT32 yEnd = yStart + bandHeight - 1;
1532 if (!Stream_EnsureRemainingCapacity(s, 11))
1535 Stream_Write_UINT16(s, 0);
1536 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(UINT16, nWidth - 1));
1537 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(UINT16, yStart));
1538 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(UINT16, yEnd));
1539 Stream_Write_UINT8(s, 0);
1540 Stream_Write_UINT8(s, 0);
1541 Stream_Write_UINT8(s, 0);
1543 for (UINT32 x = 0; x < nWidth; x++)
1545 for (UINT32 y = 0; y < bandHeight; y++)
1551 const BYTE* pPixel = &pSrcData[(1ull * (yStart + y)) * nSrcStep + 1ull * x * bpp];
1552 const UINT32 color = FreeRDPReadColor(pPixel, SrcFormat);
1553 FreeRDPSplitColor(color, SrcFormat, &r, &g, &b, &a,
nullptr);
1554 columnBgr[y * 3 + 0] = b;
1555 columnBgr[y * 3 + 1] = g;
1556 columnBgr[y * 3 + 2] = r;
1559 const UINT32 hit = clear_vbar_hash_lookup(clear, columnBgr, bandHeight);
1561 if (hit != UINT32_MAX)
1563 if (!clear_compress_write_vbar_hit(s, hit))
1566 else if (!clear_compress_write_vbar_miss(clear, s, columnBgr, bandHeight))
1574static BOOL clear_write_uint32_at(
wStream* WINPR_RESTRICT s,
size_t pos, UINT32 value)
1576 const size_t end = Stream_GetPosition(s);
1577 if (!Stream_SetPosition(s, pos))
1579 Stream_Write_UINT32(s, value);
1580 return Stream_SetPosition(s, end);
1583static BOOL clear_compress_finish(CLEAR_CONTEXT* WINPR_RESTRICT clear,
wStream* WINPR_RESTRICT s,
1584 BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize)
1586 clear->seqNumber = (clear->seqNumber + 1) % 256;
1588 *ppDstData = Stream_Buffer(s);
1589 *pDstSize = WINPR_ASSERTING_INT_CAST(UINT32, Stream_GetPosition(s));
1595static UINT32 clear_count_colors(CLEAR_CONTEXT* WINPR_RESTRICT clear,
1596 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcFormat,
1597 UINT32 nSrcStep, UINT32 nWidth, UINT32 nHeight, UINT32 maxColors)
1599 const UINT32 bpp = FreeRDPGetBytesPerPixel(SrcFormat);
1603 while ((1ull << bits) < 2ull * (maxColors + 1ull))
1605 const size_t setSize = 1ull << bits;
1607 if (setSize > clear->ColorSetSize)
1609 UINT32* tmp = realloc(clear->ColorSet, setSize *
sizeof(UINT32));
1612 clear->ColorSet = tmp;
1613 clear->ColorSetSize = setSize;
1615 ZeroMemory(clear->ColorSet, setSize *
sizeof(UINT32));
1617 const size_t mask = setSize - 1;
1619 for (UINT32 y = 0; y < nHeight; y++)
1621 const BYTE* pLine = &pSrcData[1ull * y * nSrcStep];
1622 for (UINT32 x = 0; x < nWidth; x++)
1627 const UINT32 color = FreeRDPReadColor(&pLine[1ull * x * bpp], SrcFormat);
1628 FreeRDPSplitColor(color, SrcFormat, &r, &g, &b,
nullptr,
nullptr);
1631 const UINT32 key = 0x01000000u | ((UINT32)r << 16) | ((UINT32)g << 8) | b;
1632 size_t slot = (key * 2654435761u) >> (32 - bits);
1634 UINT32 cur = clear->ColorSet[slot];
1635 while ((cur != 0) && (cur != key))
1637 slot = (slot + 1) & mask;
1638 if (slot >= clear->ColorSetSize)
1640 cur = clear->ColorSet[slot];
1643 if (clear->ColorSet[slot] == 0)
1645 clear->ColorSet[slot] = key;
1646 if (++count > maxColors)
1657static BOOL clear_compress_nscodec(CLEAR_CONTEXT* WINPR_RESTRICT clear,
1658 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcFormat,
1659 UINT32 nSrcStep, UINT32 nWidth, UINT32 nHeight)
1661 const UINT32 nscStep = nWidth * FreeRDPGetBytesPerPixel(PIXEL_FORMAT_BGRX32);
1664 const size_t size = 1ull * nscStep * nHeight + 32;
1665 if (size > clear->NscBufferSize)
1667 BYTE* tmp = winpr_aligned_recalloc(clear->NscBuffer, size,
sizeof(BYTE), 32);
1670 clear->NscBuffer = tmp;
1671 clear->NscBufferSize = size;
1675 if (!freerdp_image_copy_no_overlap(clear->NscBuffer, PIXEL_FORMAT_BGRX32, nscStep, 0, 0, nWidth,
1676 nHeight, pSrcData, SrcFormat, nSrcStep, 0, 0,
nullptr,
1677 FREERDP_FLIP_VERTICAL))
1680 if (!nsc_context_set_parameters(clear->nsc, NSC_COLOR_FORMAT, PIXEL_FORMAT_BGRX32))
1683 Stream_ResetPosition(clear->nscStream);
1684 return nsc_compose_message(clear->nsc, clear->nscStream, clear->NscBuffer, nWidth, nHeight,
1691static SSIZE_T clear_compress_subcodecs(CLEAR_CONTEXT* WINPR_RESTRICT clear,
1693 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcFormat,
1694 UINT32 nSrcStep, UINT32 nWidth, UINT32 nHeight,
1697 const UINT64 nPixels = 1ull * nWidth * nHeight;
1703 const UINT32 maxColors = WINPR_ASSERTING_INT_CAST(UINT32, nPixels * 2 / 5);
1704 const UINT32 colors =
1705 clear_count_colors(clear, pSrcData, SrcFormat, nSrcStep, nWidth, nHeight, maxColors);
1706 if (colors == UINT32_MAX)
1708 if (colors <= maxColors)
1711 if (!clear_compress_nscodec(clear, pSrcData, SrcFormat, nSrcStep, nWidth, nHeight))
1714 const size_t nscSize = Stream_GetPosition(clear->nscStream);
1715 const size_t subcodecSize = 13 + nscSize;
1716 if ((nscSize >= 3 * nPixels) || (subcodecSize >= maxSize))
1719 if (!Stream_EnsureRemainingCapacity(s, subcodecSize))
1722 Stream_Write_UINT16(s, 0);
1723 Stream_Write_UINT16(s, 0);
1724 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(UINT16, nWidth));
1725 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(UINT16, nHeight));
1726 Stream_Write_UINT32(s, WINPR_ASSERTING_INT_CAST(UINT32, nscSize));
1727 Stream_Write_UINT8(s, 1);
1728 Stream_Write(s, Stream_Buffer(clear->nscStream), nscSize);
1729 return WINPR_ASSERTING_INT_CAST(SSIZE_T, subcodecSize);
1732BOOL clear_compress(CLEAR_CONTEXT* WINPR_RESTRICT clear,
const BYTE* WINPR_RESTRICT pSrcData,
1733 UINT32 SrcFormat, UINT32 nSrcStep, UINT32 nWidth, UINT32 nHeight,
1734 BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize)
1736 if (!clear || !clear->Compressor || !clear->bs)
1739 if (!pSrcData || !ppDstData || !pDstSize)
1742 if ((nWidth == 0) || (nHeight == 0) || (nWidth > 0xFFFF) || (nHeight > 0xFFFF))
1746 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(SrcFormat);
1749 if (!Stream_SetPosition(s, 0))
1752 if (!Stream_EnsureRemainingCapacity(s, 14))
1755 Stream_Write_UINT8(s, 0);
1756 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(UINT8, clear->seqNumber));
1758 const size_t residualByteCountPos = Stream_GetPosition(s);
1759 Stream_Write_UINT32(s, 0);
1760 const size_t bandsByteCountPos = Stream_GetPosition(s);
1761 Stream_Write_UINT32(s, 0);
1762 const size_t subcodecByteCountPos = Stream_GetPosition(s);
1763 Stream_Write_UINT32(s, 0);
1765 const size_t residualStart = Stream_GetPosition(s);
1767 if (!clear_compress_residual_data(clear, s, pSrcData, SrcFormat, nSrcStep, nWidth, nHeight))
1770 UINT32 residualByteCount =
1771 WINPR_ASSERTING_INT_CAST(UINT32, Stream_GetPosition(s) - residualStart);
1775 if (!Stream_SetPosition(s, residualStart))
1777 const SSIZE_T subcodecByteCount = clear_compress_subcodecs(
1778 clear, s, pSrcData, SrcFormat, nSrcStep, nWidth, nHeight, residualByteCount);
1779 if (subcodecByteCount < 0)
1781 if (subcodecByteCount > 0)
1783 if (!clear_write_uint32_at(s, subcodecByteCountPos,
1784 WINPR_ASSERTING_INT_CAST(UINT32, subcodecByteCount)))
1786 return clear_compress_finish(clear, s, ppDstData, pDstSize);
1788 if (!Stream_SetPosition(s, residualStart + residualByteCount))
1803 UINT32 bandsByteCount = 0;
1804 const UINT32 numBands = (nHeight + CLEAR_BAND_MAX_HEIGHT - 1) / CLEAR_BAND_MAX_HEIGHT;
1805 const UINT64 minBandsCost = (1ull * numBands * 11ull) + (2ull * numBands * nWidth);
1807 if (clear->VBarHashSlots && (residualByteCount > minBandsCost))
1809 if (!Stream_SetPosition(s, residualStart))
1812 if (!clear_compress_bands_data(clear, s, pSrcData, SrcFormat, nSrcStep, nWidth, nHeight))
1815 bandsByteCount = WINPR_ASSERTING_INT_CAST(UINT32, Stream_GetPosition(s) - residualStart);
1816 residualByteCount = 0;
1819 if (!clear_write_uint32_at(s, residualByteCountPos, residualByteCount) ||
1820 !clear_write_uint32_at(s, bandsByteCountPos, bandsByteCount))
1823 return clear_compress_finish(clear, s, ppDstData, pDstSize);
1826BOOL clear_context_reset(CLEAR_CONTEXT* WINPR_RESTRICT clear)
1835 clear->seqNumber = 0;
1839CLEAR_CONTEXT* clear_context_new(BOOL Compressor)
1841 CLEAR_CONTEXT* clear = (CLEAR_CONTEXT*)winpr_aligned_calloc(1,
sizeof(CLEAR_CONTEXT), 32);
1845 clear->log = WLog_Get(TAG);
1846 clear->Compressor = Compressor;
1847 clear->nsc = nsc_context_new();
1852 if (!updateContextFormat(clear, PIXEL_FORMAT_BGRX32, TRUE))
1855 if (!clear_resize_buffer(clear, 512, 512))
1858 if (!clear->TempBuffer)
1863 clear->bs = Stream_New(
nullptr, 1024);
1867 clear->nscStream = Stream_New(
nullptr, 1024);
1868 if (!clear->nscStream)
1871 if (!nsc_context_set_parameters(clear->nsc, NSC_COLOR_LOSS_LEVEL, 3) ||
1872 !nsc_context_set_parameters(clear->nsc, NSC_ALLOW_SUBSAMPLING, 0))
1875 clear->VBarHashSlots =
1876 (UINT32*)winpr_aligned_calloc(CLEAR_VBAR_HASH_SIZE,
sizeof(UINT32), 32);
1877 if (!clear->VBarHashSlots)
1881 if (!clear_context_reset(clear))
1886 WINPR_PRAGMA_DIAG_PUSH
1887 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1888 clear_context_free(clear);
1889 WINPR_PRAGMA_DIAG_POP
1893void clear_context_free(CLEAR_CONTEXT* WINPR_RESTRICT clear)
1898 nsc_context_free(clear->nsc);
1899 winpr_aligned_free(clear->TempBuffer);
1900 Stream_Free(clear->bs, TRUE);
1902 clear_reset_vbar_storage(clear, TRUE);
1903 clear_reset_glyph_cache(clear);
1904 winpr_aligned_free(clear->VBarHashSlots);
1905 winpr_aligned_free(clear->NscBuffer);
1906 Stream_Free(clear->nscStream, TRUE);
1907 free(clear->ColorSet);
1909 winpr_aligned_free(clear);