24 #include <winpr/assert.h>
25 #include <freerdp/config.h>
27 #include <freerdp/codec/interleaved.h>
28 #include <freerdp/log.h>
30 #define TAG FREERDP_TAG("codec")
32 #define UNROLL_BODY(_exp, _count) \
35 for (size_t x = 0; x < (_count); x++) \
44 #define UNROLL_MULTIPLE(_condition, _exp, _count) \
47 while ((_condition) >= (_count)) \
49 UNROLL_BODY(_exp, _count); \
50 (_condition) -= (_count); \
54 #define UNROLL(_condition, _exp) \
57 UNROLL_MULTIPLE(_condition, _exp, 16); \
58 UNROLL_MULTIPLE(_condition, _exp, 4); \
59 UNROLL_MULTIPLE(_condition, _exp, 1); \
69 #define REGULAR_BG_RUN 0x00
70 #define MEGA_MEGA_BG_RUN 0xF0
71 #define REGULAR_FG_RUN 0x01
72 #define MEGA_MEGA_FG_RUN 0xF1
73 #define LITE_SET_FG_FG_RUN 0x0C
74 #define MEGA_MEGA_SET_FG_RUN 0xF6
75 #define LITE_DITHERED_RUN 0x0E
76 #define MEGA_MEGA_DITHERED_RUN 0xF8
77 #define REGULAR_COLOR_RUN 0x03
78 #define MEGA_MEGA_COLOR_RUN 0xF3
79 #define REGULAR_FGBG_IMAGE 0x02
80 #define MEGA_MEGA_FGBG_IMAGE 0xF2
81 #define LITE_SET_FG_FGBG_IMAGE 0x0D
82 #define MEGA_MEGA_SET_FGBG_IMAGE 0xF7
83 #define REGULAR_COLOR_IMAGE 0x04
84 #define MEGA_MEGA_COLOR_IMAGE 0xF4
85 #define SPECIAL_FGBG_1 0xF9
86 #define SPECIAL_FGBG_2 0xFA
87 #define SPECIAL_WHITE 0xFD
88 #define SPECIAL_BLACK 0xFE
90 #define BLACK_PIXEL 0x000000
94 static const BYTE g_MaskSpecialFgBg1 = 0x03;
95 static const BYTE g_MaskSpecialFgBg2 = 0x05;
97 static const BYTE g_MaskRegularRunLength = 0x1F;
98 static const BYTE g_MaskLiteRunLength = 0x0F;
100 static const char* rle_code_str(UINT32 code)
105 return "REGULAR_BG_RUN";
106 case MEGA_MEGA_BG_RUN:
107 return "MEGA_MEGA_BG_RUN";
109 return "REGULAR_FG_RUN";
110 case MEGA_MEGA_FG_RUN:
111 return "MEGA_MEGA_FG_RUN";
112 case LITE_SET_FG_FG_RUN:
113 return "LITE_SET_FG_FG_RUN";
114 case MEGA_MEGA_SET_FG_RUN:
115 return "MEGA_MEGA_SET_FG_RUN";
116 case LITE_DITHERED_RUN:
117 return "LITE_DITHERED_RUN";
118 case MEGA_MEGA_DITHERED_RUN:
119 return "MEGA_MEGA_DITHERED_RUN";
120 case REGULAR_COLOR_RUN:
121 return "REGULAR_COLOR_RUN";
122 case MEGA_MEGA_COLOR_RUN:
123 return "MEGA_MEGA_COLOR_RUN";
124 case REGULAR_FGBG_IMAGE:
125 return "REGULAR_FGBG_IMAGE";
126 case MEGA_MEGA_FGBG_IMAGE:
127 return "MEGA_MEGA_FGBG_IMAGE";
128 case LITE_SET_FG_FGBG_IMAGE:
129 return "LITE_SET_FG_FGBG_IMAGE";
130 case MEGA_MEGA_SET_FGBG_IMAGE:
131 return "MEGA_MEGA_SET_FGBG_IMAGE";
132 case REGULAR_COLOR_IMAGE:
133 return "REGULAR_COLOR_IMAGE";
134 case MEGA_MEGA_COLOR_IMAGE:
135 return "MEGA_MEGA_COLOR_IMAGE";
137 return "SPECIAL_FGBG_1";
139 return "SPECIAL_FGBG_2";
141 return "SPECIAL_WHITE";
143 return "SPECIAL_BLACK";
149 static const char* rle_code_str_buffer(UINT32 code,
char* buffer,
size_t size)
151 const char* str = rle_code_str(code);
152 (void)_snprintf(buffer, size,
"%s [0x%08" PRIx32
"]", str, code);
156 #define buffer_within_range(pbSrc, size, pbEnd) \
157 buffer_within_range_((pbSrc), (size), (pbEnd), __func__, __FILE__, __LINE__)
158 static INLINE BOOL buffer_within_range_(
const void* pbSrc,
size_t size,
const void* pbEnd,
159 const char* fkt,
const char* file,
size_t line)
165 if ((
const char*)pbSrc + size > (
const char*)pbEnd)
167 WLog_ERR(TAG,
"[%s:%" PRIuz
"] pbSrc=%p + %" PRIuz
" > pbEnd=%p", fkt, line, pbSrc, size,
178 static INLINE UINT32 ExtractCodeId(BYTE bOrderHdr)
180 if ((bOrderHdr & 0xC0U) != 0xC0U)
185 return bOrderHdr >> 5;
187 else if ((bOrderHdr & 0xF0U) == 0xF0U)
197 return bOrderHdr >> 4;
204 static UINT ExtractRunLengthRegularFgBg(
const BYTE* pbOrderHdr,
const BYTE* pbEnd, UINT32* advance)
208 WINPR_ASSERT(pbOrderHdr);
210 WINPR_ASSERT(advance);
212 runLength = (*pbOrderHdr) & g_MaskRegularRunLength;
215 if (!buffer_within_range(pbOrderHdr, 2, pbEnd))
220 runLength = *(pbOrderHdr + 1) + 1;
224 runLength = runLength * 8;
229 static UINT ExtractRunLengthLiteFgBg(
const BYTE* pbOrderHdr,
const BYTE* pbEnd, UINT32* advance)
233 WINPR_ASSERT(pbOrderHdr);
235 WINPR_ASSERT(advance);
237 runLength = *pbOrderHdr & g_MaskLiteRunLength;
240 if (!buffer_within_range(pbOrderHdr, 2, pbEnd))
245 runLength = *(pbOrderHdr + 1) + 1;
249 runLength = runLength * 8;
254 static UINT ExtractRunLengthRegular(
const BYTE* pbOrderHdr,
const BYTE* pbEnd, UINT32* advance)
258 WINPR_ASSERT(pbOrderHdr);
260 WINPR_ASSERT(advance);
262 runLength = *pbOrderHdr & g_MaskRegularRunLength;
265 if (!buffer_within_range(pbOrderHdr, 2, pbEnd))
270 runLength = *(pbOrderHdr + 1) + 32;
277 static UINT ExtractRunLengthMegaMega(
const BYTE* pbOrderHdr,
const BYTE* pbEnd, UINT32* advance)
281 WINPR_ASSERT(pbOrderHdr);
283 WINPR_ASSERT(advance);
285 if (!buffer_within_range(pbOrderHdr, 3, pbEnd))
291 runLength = ((UINT16)pbOrderHdr[1]) | (((UINT16)pbOrderHdr[2]) << 8);
297 static UINT ExtractRunLengthLite(
const BYTE* pbOrderHdr,
const BYTE* pbEnd, UINT32* advance)
301 WINPR_ASSERT(pbOrderHdr);
303 WINPR_ASSERT(advance);
305 runLength = *pbOrderHdr & g_MaskLiteRunLength;
308 if (!buffer_within_range(pbOrderHdr, 2, pbEnd))
313 runLength = *(pbOrderHdr + 1) + 16;
319 static INLINE UINT32 ExtractRunLength(UINT32 code,
const BYTE* pbOrderHdr,
const BYTE* pbEnd,
322 UINT32 runLength = 0;
325 WINPR_ASSERT(pbOrderHdr);
327 WINPR_ASSERT(advance);
330 if (!buffer_within_range(pbOrderHdr, 0, pbEnd))
335 case REGULAR_FGBG_IMAGE:
336 runLength = ExtractRunLengthRegularFgBg(pbOrderHdr, pbEnd, &ladvance);
339 case LITE_SET_FG_FGBG_IMAGE:
340 runLength = ExtractRunLengthLiteFgBg(pbOrderHdr, pbEnd, &ladvance);
345 case REGULAR_COLOR_RUN:
346 case REGULAR_COLOR_IMAGE:
347 runLength = ExtractRunLengthRegular(pbOrderHdr, pbEnd, &ladvance);
350 case LITE_SET_FG_FG_RUN:
351 case LITE_DITHERED_RUN:
352 runLength = ExtractRunLengthLite(pbOrderHdr, pbEnd, &ladvance);
355 case MEGA_MEGA_BG_RUN:
356 case MEGA_MEGA_FG_RUN:
357 case MEGA_MEGA_SET_FG_RUN:
358 case MEGA_MEGA_DITHERED_RUN:
359 case MEGA_MEGA_COLOR_RUN:
360 case MEGA_MEGA_FGBG_IMAGE:
361 case MEGA_MEGA_SET_FGBG_IMAGE:
362 case MEGA_MEGA_COLOR_IMAGE:
363 runLength = ExtractRunLengthMegaMega(pbOrderHdr, pbEnd, &ladvance);
376 #define ensure_capacity(start, end, size, base) \
377 ensure_capacity_((start), (end), (size), (base), __func__, __FILE__, __LINE__)
378 static INLINE BOOL ensure_capacity_(
const BYTE* start,
const BYTE* end,
size_t size,
size_t base,
379 const char* fkt,
const char* file,
size_t line)
381 const size_t available = (uintptr_t)end - (uintptr_t)start;
382 const BOOL rc = available >= size * base;
383 const BOOL res = rc && (start <= end);
387 "[%s:%" PRIuz
"] failed: start=%p <= end=%p, available=%" PRIuz
" >= size=%" PRIuz
389 fkt, line, start, end, available, size, base);
393 static INLINE
void write_pixel_8(BYTE* _buf, BYTE _pix)
399 static INLINE
void write_pixel_24(BYTE* _buf, UINT32 _pix)
402 (_buf)[0] = (BYTE)(_pix);
403 (_buf)[1] = (BYTE)((_pix) >> 8);
404 (_buf)[2] = (BYTE)((_pix) >> 16);
407 static INLINE
void write_pixel_16(BYTE* _buf, UINT16 _pix)
410 _buf[0] = _pix & 0xFF;
411 _buf[1] = (_pix >> 8) & 0xFF;
414 #undef DESTWRITEPIXEL
417 #undef WRITEFGBGIMAGE
418 #undef WRITEFIRSTLINEFGBGIMAGE
426 #define WHITE_PIXEL 0xFF
427 #define DESTWRITEPIXEL(_buf, _pix) \
430 write_pixel_8(_buf, _pix); \
433 #define DESTREADPIXEL(_pix, _buf) _pix = (_buf)[0]
434 #define SRCREADPIXEL(_pix, _buf) \
437 (_pix) = (_buf)[0]; \
441 #define WRITEFGBGIMAGE WriteFgBgImage8to8
442 #define WRITEFIRSTLINEFGBGIMAGE WriteFirstLineFgBgImage8to8
443 #define RLEDECOMPRESS RleDecompress8to8
445 #undef ENSURE_CAPACITY
446 #define ENSURE_CAPACITY(_start, _end, _size) ensure_capacity(_start, _end, _size, 1)
447 #include "include/bitmap.h"
449 #undef DESTWRITEPIXEL
452 #undef WRITEFGBGIMAGE
453 #undef WRITEFIRSTLINEFGBGIMAGE
461 #define WHITE_PIXEL 0xFFFF
462 #define DESTWRITEPIXEL(_buf, _pix) \
465 write_pixel_16(_buf, _pix); \
468 #define DESTREADPIXEL(_pix, _buf) _pix = ((UINT16*)(_buf))[0]
469 #define SRCREADPIXEL(_pix, _buf) \
472 (_pix) = (_buf)[0] | ((_buf)[1] << 8); \
475 #define WRITEFGBGIMAGE WriteFgBgImage16to16
476 #define WRITEFIRSTLINEFGBGIMAGE WriteFirstLineFgBgImage16to16
477 #define RLEDECOMPRESS RleDecompress16to16
479 #undef ENSURE_CAPACITY
480 #define ENSURE_CAPACITY(_start, _end, _size) ensure_capacity(_start, _end, _size, 2)
481 #include "include/bitmap.h"
483 #undef DESTWRITEPIXEL
486 #undef WRITEFGBGIMAGE
487 #undef WRITEFIRSTLINEFGBGIMAGE
495 #define WHITE_PIXEL 0xffffff
496 #define DESTWRITEPIXEL(_buf, _pix) \
499 write_pixel_24(_buf, _pix); \
502 #define DESTREADPIXEL(_pix, _buf) _pix = (_buf)[0] | ((_buf)[1] << 8) | ((_buf)[2] << 16)
503 #define SRCREADPIXEL(_pix, _buf) \
506 (_pix) = (_buf)[0] | ((_buf)[1] << 8) | ((_buf)[2] << 16); \
510 #define WRITEFGBGIMAGE WriteFgBgImage24to24
511 #define WRITEFIRSTLINEFGBGIMAGE WriteFirstLineFgBgImage24to24
512 #define RLEDECOMPRESS RleDecompress24to24
514 #undef ENSURE_CAPACITY
515 #define ENSURE_CAPACITY(_start, _end, _size) ensure_capacity(_start, _end, _size, 3)
516 #include "include/bitmap.h"
518 struct S_BITMAP_INTERLEAVED_CONTEXT
528 BOOL interleaved_decompress(BITMAP_INTERLEAVED_CONTEXT* WINPR_RESTRICT interleaved,
529 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, UINT32 nSrcWidth,
530 UINT32 nSrcHeight, UINT32 bpp, BYTE* WINPR_RESTRICT pDstData,
531 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
532 UINT32 nDstWidth, UINT32 nDstHeight,
533 const gdiPalette* WINPR_RESTRICT palette)
536 UINT32 SrcFormat = 0;
537 UINT32 BufferSize = 0;
539 if (!interleaved || !pSrcData || !pDstData)
541 WLog_ERR(TAG,
"invalid arguments: interleaved=%p, pSrcData=%p, pDstData=%p", interleaved,
549 scanline = nSrcWidth * 3;
550 SrcFormat = PIXEL_FORMAT_BGR24;
554 scanline = nSrcWidth * 2;
555 SrcFormat = PIXEL_FORMAT_RGB16;
559 scanline = nSrcWidth * 2;
560 SrcFormat = PIXEL_FORMAT_RGB15;
564 scanline = nSrcWidth;
565 SrcFormat = PIXEL_FORMAT_RGB8;
569 WLog_ERR(TAG,
"Invalid color depth %" PRIu32
"", bpp);
573 BufferSize = scanline * nSrcHeight;
575 if (BufferSize > interleaved->TempSize)
577 interleaved->TempBuffer =
578 winpr_aligned_recalloc(interleaved->TempBuffer, BufferSize,
sizeof(BYTE), 16);
579 interleaved->TempSize = BufferSize;
582 if (!interleaved->TempBuffer)
584 WLog_ERR(TAG,
"interleaved->TempBuffer=%p", interleaved->TempBuffer);
591 if (!RleDecompress24to24(pSrcData, SrcSize, interleaved->TempBuffer, scanline,
592 nSrcWidth, nSrcHeight))
594 WLog_ERR(TAG,
"RleDecompress24to24 failed");
602 if (!RleDecompress16to16(pSrcData, SrcSize, interleaved->TempBuffer, scanline,
603 nSrcWidth, nSrcHeight))
605 WLog_ERR(TAG,
"RleDecompress16to16 failed");
612 if (!RleDecompress8to8(pSrcData, SrcSize, interleaved->TempBuffer, scanline, nSrcWidth,
615 WLog_ERR(TAG,
"RleDecompress8to8 failed");
622 WLog_ERR(TAG,
"Invalid color depth %" PRIu32
"", bpp);
626 if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
627 nDstHeight, interleaved->TempBuffer, SrcFormat, scanline, 0,
628 0, palette, FREERDP_FLIP_VERTICAL | FREERDP_KEEP_DST_ALPHA))
630 WLog_ERR(TAG,
"freerdp_image_copy failed");
636 BOOL interleaved_compress(BITMAP_INTERLEAVED_CONTEXT* WINPR_RESTRICT interleaved,
637 BYTE* WINPR_RESTRICT pDstData, UINT32* WINPR_RESTRICT pDstSize,
638 UINT32 nWidth, UINT32 nHeight,
const BYTE* WINPR_RESTRICT pSrcData,
639 UINT32 SrcFormat, UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
640 const gdiPalette* WINPR_RESTRICT palette, UINT32 bpp)
644 UINT32 DstFormat = 0;
645 const UINT32 maxSize = 64 * 64 * 4;
647 if (!interleaved || !pDstData || !pSrcData)
650 if ((nWidth == 0) || (nHeight == 0))
655 WLog_ERR(TAG,
"interleaved_compress: width is not a multiple of 4");
659 if ((nWidth > 64) || (nHeight > 64))
662 "interleaved_compress: width (%" PRIu32
") or height (%" PRIu32
663 ") is greater than 64",
671 DstFormat = PIXEL_FORMAT_BGRX32;
675 DstFormat = PIXEL_FORMAT_RGB16;
679 DstFormat = PIXEL_FORMAT_RGB15;
686 if (!freerdp_image_copy_no_overlap(interleaved->TempBuffer, DstFormat, 0, 0, 0, nWidth, nHeight,
687 pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc, palette,
688 FREERDP_KEEP_DST_ALPHA))
691 s = Stream_New(pDstData, *pDstSize);
696 Stream_SetPosition(interleaved->bts, 0);
698 if (freerdp_bitmap_compress(interleaved->TempBuffer, nWidth, nHeight, s, bpp, maxSize,
699 nHeight - 1, interleaved->bts, 0) < 0)
704 Stream_SealLength(s);
705 *pDstSize = (UINT32)Stream_Length(s);
706 Stream_Free(s, FALSE);
710 BOOL bitmap_interleaved_context_reset(BITMAP_INTERLEAVED_CONTEXT* WINPR_RESTRICT interleaved)
718 BITMAP_INTERLEAVED_CONTEXT* bitmap_interleaved_context_new(BOOL Compressor)
720 BITMAP_INTERLEAVED_CONTEXT* interleaved = NULL;
721 interleaved = (BITMAP_INTERLEAVED_CONTEXT*)winpr_aligned_recalloc(
722 NULL, 1,
sizeof(BITMAP_INTERLEAVED_CONTEXT), 32);
726 interleaved->TempSize = 64 * 64 * 4;
727 interleaved->TempBuffer = winpr_aligned_calloc(interleaved->TempSize,
sizeof(BYTE), 16);
729 if (!interleaved->TempBuffer)
732 interleaved->bts = Stream_New(NULL, interleaved->TempSize);
734 if (!interleaved->bts)
741 WINPR_PRAGMA_DIAG_PUSH
742 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
743 bitmap_interleaved_context_free(interleaved);
744 WINPR_PRAGMA_DIAG_POP
748 void bitmap_interleaved_context_free(BITMAP_INTERLEAVED_CONTEXT* WINPR_RESTRICT interleaved)
753 winpr_aligned_free(interleaved->TempBuffer);
754 Stream_Free(interleaved->bts, TRUE);
755 winpr_aligned_free(interleaved);