22#include <freerdp/config.h>
28#include <winpr/assert.h>
29#include <winpr/cast.h>
31#include <winpr/tchar.h>
32#include <winpr/sysinfo.h>
33#include <winpr/registry.h>
35#include <freerdp/log.h>
36#include <freerdp/settings.h>
37#include <freerdp/codec/rfx.h>
38#include <freerdp/constants.h>
39#include <freerdp/primitives.h>
40#include <freerdp/codec/region.h>
41#include <freerdp/build-config.h>
43#include "rfx_constants.h"
45#include "rfx_decode.h"
46#include "rfx_encode.h"
47#include "rfx_quantization.h"
50#include "../core/utils.h"
52#include "sse/rfx_sse2.h"
53#include "neon/rfx_neon.h"
55#define TAG FREERDP_TAG("codec")
57#define RFX_KEY "Software\\%s\\RemoteFX"
71static const UINT32 rfx_default_quantization_values[] = { 6, 6, 6, 6, 7, 7, 8, 8, 8, 9 };
73static inline BOOL rfx_write_progressive_tile_simple(RFX_CONTEXT* WINPR_RESTRICT rfx,
75 const RFX_TILE* WINPR_RESTRICT tile);
77static inline void rfx_profiler_create(RFX_CONTEXT* WINPR_RESTRICT context)
79 if (!context || !context->priv)
81 PROFILER_CREATE(context->priv->prof_rfx_decode_rgb,
"rfx_decode_rgb")
82 PROFILER_CREATE(context->priv->prof_rfx_decode_component, "rfx_decode_component")
83 PROFILER_CREATE(context->priv->prof_rfx_rlgr_decode, "rfx_rlgr_decode")
84 PROFILER_CREATE(context->priv->prof_rfx_differential_decode, "rfx_differential_decode")
85 PROFILER_CREATE(context->priv->prof_rfx_quantization_decode, "rfx_quantization_decode")
86 PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_decode, "rfx_dwt_2d_decode")
87 PROFILER_CREATE(context->priv->prof_rfx_ycbcr_to_rgb, "prims->yCbCrToRGB")
88 PROFILER_CREATE(context->priv->prof_rfx_encode_rgb, "rfx_encode_rgb")
89 PROFILER_CREATE(context->priv->prof_rfx_encode_component, "rfx_encode_component")
90 PROFILER_CREATE(context->priv->prof_rfx_rlgr_encode, "rfx_rlgr_encode")
91 PROFILER_CREATE(context->priv->prof_rfx_differential_encode, "rfx_differential_encode")
92 PROFILER_CREATE(context->priv->prof_rfx_quantization_encode, "rfx_quantization_encode")
93 PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_encode, "rfx_dwt_2d_encode")
94 PROFILER_CREATE(context->priv->prof_rfx_rgb_to_ycbcr, "prims->RGBToYCbCr")
95 PROFILER_CREATE(context->priv->prof_rfx_encode_format_rgb, "rfx_encode_format_rgb")
98static inline
void rfx_profiler_free(RFX_CONTEXT* WINPR_RESTRICT context)
100 if (!context || !context->priv)
102 PROFILER_FREE(context->priv->prof_rfx_decode_rgb)
103 PROFILER_FREE(context->priv->prof_rfx_decode_component)
104 PROFILER_FREE(context->priv->prof_rfx_rlgr_decode)
105 PROFILER_FREE(context->priv->prof_rfx_differential_decode)
106 PROFILER_FREE(context->priv->prof_rfx_quantization_decode)
107 PROFILER_FREE(context->priv->prof_rfx_dwt_2d_decode)
108 PROFILER_FREE(context->priv->prof_rfx_ycbcr_to_rgb)
109 PROFILER_FREE(context->priv->prof_rfx_encode_rgb)
110 PROFILER_FREE(context->priv->prof_rfx_encode_component)
111 PROFILER_FREE(context->priv->prof_rfx_rlgr_encode)
112 PROFILER_FREE(context->priv->prof_rfx_differential_encode)
113 PROFILER_FREE(context->priv->prof_rfx_quantization_encode)
114 PROFILER_FREE(context->priv->prof_rfx_dwt_2d_encode)
115 PROFILER_FREE(context->priv->prof_rfx_rgb_to_ycbcr)
116 PROFILER_FREE(context->priv->prof_rfx_encode_format_rgb)
119static inline
void rfx_profiler_print(RFX_CONTEXT* WINPR_RESTRICT context)
121 if (!context || !context->priv)
124 PROFILER_PRINT_HEADER
125 PROFILER_PRINT(context->priv->prof_rfx_decode_rgb)
126 PROFILER_PRINT(context->priv->prof_rfx_decode_component)
127 PROFILER_PRINT(context->priv->prof_rfx_rlgr_decode)
128 PROFILER_PRINT(context->priv->prof_rfx_differential_decode)
129 PROFILER_PRINT(context->priv->prof_rfx_quantization_decode)
130 PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_decode)
131 PROFILER_PRINT(context->priv->prof_rfx_ycbcr_to_rgb)
132 PROFILER_PRINT(context->priv->prof_rfx_encode_rgb)
133 PROFILER_PRINT(context->priv->prof_rfx_encode_component)
134 PROFILER_PRINT(context->priv->prof_rfx_rlgr_encode)
135 PROFILER_PRINT(context->priv->prof_rfx_differential_encode)
136 PROFILER_PRINT(context->priv->prof_rfx_quantization_encode)
137 PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_encode)
138 PROFILER_PRINT(context->priv->prof_rfx_rgb_to_ycbcr)
139 PROFILER_PRINT(context->priv->prof_rfx_encode_format_rgb)
140 PROFILER_PRINT_FOOTER
143static inline
void rfx_tile_init(
void* obj)
151 tile->YData =
nullptr;
153 tile->CbData =
nullptr;
155 tile->CrData =
nullptr;
159static inline void* rfx_decoder_tile_new(
const void* val)
161 const size_t size = 4ULL * 64ULL * 64ULL;
168 if (!(tile->data = (BYTE*)winpr_aligned_malloc(size, 16)))
170 winpr_aligned_free(tile);
173 memset(tile->data, 0xff, size);
174 tile->allocated = TRUE;
178static inline void rfx_decoder_tile_free(
void* obj)
185 winpr_aligned_free(tile->data);
187 winpr_aligned_free(tile);
191static inline void* rfx_encoder_tile_new(
const void* val)
194 return winpr_aligned_calloc(1,
sizeof(
RFX_TILE), 32);
197static inline void rfx_encoder_tile_free(
void* obj)
199 winpr_aligned_free(obj);
202RFX_CONTEXT* rfx_context_new(BOOL encoder)
204 return rfx_context_new_ex(encoder, 0);
207RFX_CONTEXT* rfx_context_new_ex(BOOL encoder, UINT32 ThreadingFlags)
209 RFX_CONTEXT_PRIV* priv =
nullptr;
210 RFX_CONTEXT* context = (RFX_CONTEXT*)winpr_aligned_calloc(1,
sizeof(RFX_CONTEXT), 32);
215 context->encoder = encoder;
216 context->currentMessage.freeArray = TRUE;
217 context->priv = priv = (RFX_CONTEXT_PRIV*)winpr_aligned_calloc(1,
sizeof(RFX_CONTEXT_PRIV), 32);
222 priv->log = WLog_Get(
"com.freerdp.codec.rfx");
223 WLog_OpenAppender(priv->log);
224 priv->TilePool = ObjectPool_New(TRUE);
230 wObject* pool = ObjectPool_Object(priv->TilePool);
233 if (context->encoder)
258 priv->BufferPool = BufferPool_New(TRUE, (8192ULL + 32ULL) * 3ULL, 16);
260 if (!priv->BufferPool)
263 priv->UseThreads = FALSE;
264 if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS))
265 priv->UseThreads = TRUE;
268 char* key = freerdp_getApplicatonDetailsRegKey(RFX_KEY);
273 RegOpenKeyExA(HKEY_LOCAL_MACHINE, RFX_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
276 if (status == ERROR_SUCCESS)
280 DWORD dwSize =
sizeof(dwValue);
282 if (RegQueryValueEx(hKey, _T(
"UseThreads"),
nullptr, &dwType, (BYTE*)&dwValue,
283 &dwSize) == ERROR_SUCCESS)
284 priv->UseThreads = dwValue ? 1 : 0;
291 if (priv->UseThreads)
296 if (!primitives_get())
301 rfx_context_set_pixel_format(context, PIXEL_FORMAT_BGRX32);
303 rfx_profiler_create(context);
305 context->quantization_decode = rfx_quantization_decode;
306 context->quantization_encode = rfx_quantization_encode;
307 context->dwt_2d_decode = rfx_dwt_2d_decode;
308 context->dwt_2d_extrapolate_decode = rfx_dwt_2d_extrapolate_decode;
309 context->dwt_2d_encode = rfx_dwt_2d_encode;
310 context->rlgr_decode = rfx_rlgr_decode;
311 context->rlgr_encode = rfx_rlgr_encode;
312 rfx_init_sse2(context);
313 rfx_init_neon(context);
314 context->state = RFX_STATE_SEND_HEADERS;
315 context->expectedDataBlockType = WBT_FRAME_BEGIN;
318 WINPR_PRAGMA_DIAG_PUSH
319 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
320 rfx_context_free(context);
321 WINPR_PRAGMA_DIAG_POP
325void rfx_context_free(RFX_CONTEXT* context)
327 RFX_CONTEXT_PRIV* priv =
nullptr;
332 WINPR_ASSERT(
nullptr != context);
334 priv = context->priv;
335 WINPR_ASSERT(
nullptr != priv);
336 WINPR_ASSERT(
nullptr != priv->TilePool);
337 WINPR_ASSERT(
nullptr != priv->BufferPool);
340 rfx_message_free(context, &context->currentMessage);
341 winpr_aligned_free(context->quants);
342 rfx_profiler_print(context);
343 rfx_profiler_free(context);
347 ObjectPool_Free(priv->TilePool);
348 if (priv->UseThreads)
350 winpr_aligned_free((
void*)priv->workObjects);
351 winpr_aligned_free(priv->tileWorkParams);
355 "WARNING: Profiling results probably unusable with multithreaded RemoteFX codec!");
359 BufferPool_Free(priv->BufferPool);
360 winpr_aligned_free(priv);
362 winpr_aligned_free(context);
365static inline RFX_TILE* rfx_message_get_tile(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index)
367 WINPR_ASSERT(message);
368 WINPR_ASSERT(message->tiles);
369 WINPR_ASSERT(index < message->numTiles);
370 return message->tiles[index];
373static inline const RFX_RECT* rfx_message_get_rect_const(
const RFX_MESSAGE* WINPR_RESTRICT message,
376 WINPR_ASSERT(message);
377 WINPR_ASSERT(message->rects);
378 WINPR_ASSERT(index < message->numRects);
379 return &message->rects[index];
382static inline RFX_RECT* rfx_message_get_rect(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index)
384 WINPR_ASSERT(message);
385 WINPR_ASSERT(message->rects);
386 WINPR_ASSERT(index < message->numRects);
387 return &message->rects[index];
390void rfx_context_set_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 pixel_format)
392 WINPR_ASSERT(context);
393 context->pixel_format = pixel_format;
394 const UINT32 bpp = FreeRDPGetBitsPerPixel(pixel_format);
395 context->bits_per_pixel = WINPR_ASSERTING_INT_CAST(UINT8, bpp);
398UINT32 rfx_context_get_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context)
400 WINPR_ASSERT(context);
401 return context->pixel_format;
404void rfx_context_set_palette(RFX_CONTEXT* WINPR_RESTRICT context,
405 const BYTE* WINPR_RESTRICT palette)
407 WINPR_ASSERT(context);
408 context->palette = palette;
411const BYTE* rfx_context_get_palette(RFX_CONTEXT* WINPR_RESTRICT context)
413 WINPR_ASSERT(context);
414 return context->palette;
417BOOL rfx_context_reset(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
422 context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
423 context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
424 context->state = RFX_STATE_SEND_HEADERS;
425 context->expectedDataBlockType = WBT_FRAME_BEGIN;
426 context->frameIdx = 0;
430static inline BOOL rfx_process_message_sync(RFX_CONTEXT* WINPR_RESTRICT context,
435 WINPR_ASSERT(context);
436 WINPR_ASSERT(context->priv);
437 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_SYNC;
440 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
443 Stream_Read_UINT32(s, magic);
444 if (magic != WF_MAGIC)
446 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid magic number 0x%08" PRIX32
"", magic);
450 Stream_Read_UINT16(s, context->version);
451 if (context->version != WF_VERSION_1_0)
453 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid version number 0x%08" PRIX32
"",
458 WLog_Print(context->priv->log, WLOG_DEBUG,
"version 0x%08" PRIX32
"", context->version);
459 context->decodedHeaderBlocks |= RFX_DECODED_SYNC;
463static inline BOOL rfx_process_message_codec_versions(RFX_CONTEXT* WINPR_RESTRICT context,
468 WINPR_ASSERT(context);
469 WINPR_ASSERT(context->priv);
470 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_VERSIONS;
472 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
475 Stream_Read_UINT8(s, numCodecs);
476 Stream_Read_UINT8(s, context->codec_id);
478 s, context->codec_version);
482 WLog_Print(context->priv->log, WLOG_ERROR,
"numCodes is 0x%02" PRIX8
" (must be 0x01)",
487 if (context->codec_id != 0x01)
489 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codec id (0x%02" PRIX32
")",
494 if (context->codec_version != WF_VERSION_1_0)
496 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codec version (0x%08" PRIX32
")",
497 context->codec_version);
501 WLog_Print(context->priv->log, WLOG_DEBUG,
"id %" PRIu32
" version 0x%" PRIX32
".",
502 context->codec_id, context->codec_version);
503 context->decodedHeaderBlocks |= RFX_DECODED_VERSIONS;
507static inline BOOL rfx_process_message_channels(RFX_CONTEXT* WINPR_RESTRICT context,
511 BYTE numChannels = 0;
513 WINPR_ASSERT(context);
514 WINPR_ASSERT(context->priv);
515 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CHANNELS;
517 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 1))
520 Stream_Read_UINT8(s, numChannels);
527 WLog_Print(context->priv->log, WLOG_ERROR,
"no channels announced");
531 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, numChannels, 5ull))
535 Stream_Read_UINT8(s, channelId);
537 if (channelId != 0x00)
539 WLog_Print(context->priv->log, WLOG_ERROR,
"channelId:0x%02" PRIX8
", expected:0x00",
544 Stream_Read_UINT16(s, context->width);
545 Stream_Read_UINT16(s, context->height);
547 if (!context->width || !context->height)
549 WLog_Print(context->priv->log, WLOG_ERROR,
550 "invalid channel with/height: %" PRIu16
"x%" PRIu16
"", context->width,
556 Stream_Seek(s, 5ULL * (numChannels - 1));
557 WLog_Print(context->priv->log, WLOG_DEBUG,
558 "numChannels %" PRIu8
" id %" PRIu8
", %" PRIu16
"x%" PRIu16
".", numChannels,
559 channelId, context->width, context->height);
560 context->decodedHeaderBlocks |= RFX_DECODED_CHANNELS;
564static inline BOOL rfx_process_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
569 UINT16 properties = 0;
571 WINPR_ASSERT(context);
572 WINPR_ASSERT(context->priv);
573 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CONTEXT;
575 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 5))
578 Stream_Read_UINT8(s, ctxId);
579 Stream_Read_UINT16(s, tileSize);
580 Stream_Read_UINT16(s, properties);
581 WLog_Print(context->priv->log, WLOG_DEBUG,
582 "ctxId %" PRIu8
" tileSize %" PRIu16
" properties 0x%04" PRIX16
".", ctxId, tileSize,
584 context->properties = properties;
585 context->flags = (properties & 0x0007);
587 if (context->flags == CODEC_MODE)
589 WLog_Print(context->priv->log, WLOG_DEBUG,
"codec is in image mode.");
593 WLog_Print(context->priv->log, WLOG_DEBUG,
"codec is in video mode.");
596 switch ((properties & 0x1E00) >> 9)
598 case CLW_ENTROPY_RLGR1:
599 context->mode = RLGR1;
600 WLog_Print(context->priv->log, WLOG_DEBUG,
"RLGR1.");
603 case CLW_ENTROPY_RLGR3:
604 context->mode = RLGR3;
605 WLog_Print(context->priv->log, WLOG_DEBUG,
"RLGR3.");
609 WLog_Print(context->priv->log, WLOG_ERROR,
"unknown RLGR algorithm.");
613 context->decodedHeaderBlocks |= RFX_DECODED_CONTEXT;
617static inline BOOL rfx_process_message_frame_begin(
618 RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message,
619 wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType)
622 UINT16 numRegions = 0;
624 WINPR_ASSERT(context);
625 WINPR_ASSERT(context->priv);
626 WINPR_ASSERT(message);
627 WINPR_ASSERT(pExpectedBlockType);
629 if (*pExpectedBlockType != WBT_FRAME_BEGIN)
631 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants WBT_FRAME_BEGIN");
635 *pExpectedBlockType = WBT_REGION;
637 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
642 Stream_Read_UINT16(s, numRegions);
643 WLog_Print(context->priv->log, WLOG_DEBUG,
644 "RFX_FRAME_BEGIN: frameIdx: %" PRIu32
" numRegions: %" PRIu16
"", frameIdx,
649static inline BOOL rfx_process_message_frame_end(
650 RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message,
651 WINPR_ATTR_UNUSED
wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType)
653 WINPR_ASSERT(context);
654 WINPR_ASSERT(context->priv);
655 WINPR_ASSERT(message);
657 WINPR_ASSERT(pExpectedBlockType);
659 if (*pExpectedBlockType != WBT_FRAME_END)
661 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected, wants WBT_FRAME_END");
665 *pExpectedBlockType = WBT_FRAME_BEGIN;
666 WLog_Print(context->priv->log, WLOG_DEBUG,
"RFX_FRAME_END");
670static inline BOOL rfx_resize_rects(RFX_MESSAGE* WINPR_RESTRICT message)
672 WINPR_ASSERT(message);
675 winpr_aligned_recalloc(message->rects, message->numRects,
sizeof(
RFX_RECT), 32);
678 message->rects = tmpRects;
682static inline BOOL rfx_process_message_region(RFX_CONTEXT* WINPR_RESTRICT context,
683 RFX_MESSAGE* WINPR_RESTRICT message,
685 UINT16* WINPR_RESTRICT pExpectedBlockType)
687 UINT16 regionType = 0;
688 UINT16 numTileSets = 0;
690 WINPR_ASSERT(context);
691 WINPR_ASSERT(context->priv);
692 WINPR_ASSERT(message);
693 WINPR_ASSERT(pExpectedBlockType);
695 if (*pExpectedBlockType != WBT_REGION)
697 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants WBT_REGION");
701 *pExpectedBlockType = WBT_EXTENSION;
703 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 3))
706 Stream_Seek_UINT8(s);
707 Stream_Read_UINT16(s, message->numRects);
709 if (message->numRects < 1)
717 message->numRects = 1;
718 if (!rfx_resize_rects(message))
721 message->rects->x = 0;
722 message->rects->y = 0;
723 message->rects->width = context->width;
724 message->rects->height = context->height;
728 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, message->numRects, 8ull))
731 if (!rfx_resize_rects(message))
735 for (UINT16 i = 0; i < message->numRects; i++)
737 RFX_RECT* rect = rfx_message_get_rect(message, i);
739 Stream_Read_UINT16(s, rect->x);
740 Stream_Read_UINT16(s, rect->y);
741 Stream_Read_UINT16(s, rect->width);
742 Stream_Read_UINT16(s, rect->height);
743 WLog_Print(context->priv->log, WLOG_DEBUG,
744 "rect %" PRIu16
" (x,y=%" PRIu16
",%" PRIu16
" w,h=%" PRIu16
" %" PRIu16
").", i,
745 rect->x, rect->y, rect->width, rect->height);
748 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
751 Stream_Read_UINT16(s, regionType);
752 Stream_Read_UINT16(s, numTileSets);
754 if (regionType != CBT_REGION)
756 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid region type 0x%04" PRIX16
"",
761 if (numTileSets != 0x0001)
763 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid number of tilesets (%" PRIu16
")",
774 RFX_CONTEXT* context;
775} RFX_TILE_PROCESS_WORK_PARAM;
777static inline void CALLBACK
778rfx_process_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
779 void* context, WINPR_ATTR_UNUSED PTP_WORK work)
781 RFX_TILE_PROCESS_WORK_PARAM* param = (RFX_TILE_PROCESS_WORK_PARAM*)context;
783 WINPR_ASSERT(param->context);
784 WINPR_ASSERT(param->context->priv);
786 if (!rfx_decode_rgb(param->context, param->tile, param->tile->data, 64 * 4))
787 WLog_Print(param->context->priv->log, WLOG_ERROR,
"rfx_decode_rgb failed");
790static inline BOOL rfx_allocate_tiles(RFX_MESSAGE* WINPR_RESTRICT message,
size_t count,
793 WINPR_ASSERT(message);
796 (
RFX_TILE**)winpr_aligned_recalloc((
void*)message->tiles, count,
sizeof(
RFX_TILE*), 32);
797 if (!tmpTiles && (count != 0))
800 message->tiles = tmpTiles;
802 message->numTiles = WINPR_ASSERTING_INT_CAST(UINT16, count);
805 WINPR_ASSERT(message->numTiles <= count);
807 message->allocatedTiles = count;
812static inline BOOL rfx_process_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
813 RFX_MESSAGE* WINPR_RESTRICT message,
815 UINT16* WINPR_RESTRICT pExpectedBlockType)
818 size_t close_cnt = 0;
821 UINT32* quants =
nullptr;
825 UINT32 blockType = 0;
826 UINT32 tilesDataSize = 0;
827 PTP_WORK* work_objects =
nullptr;
828 RFX_TILE_PROCESS_WORK_PARAM* params =
nullptr;
829 void* pmem =
nullptr;
831 WINPR_ASSERT(context);
832 WINPR_ASSERT(context->priv);
833 WINPR_ASSERT(message);
834 WINPR_ASSERT(pExpectedBlockType);
836 if (*pExpectedBlockType != WBT_EXTENSION)
838 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants a tileset");
842 *pExpectedBlockType = WBT_FRAME_END;
844 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 14))
847 Stream_Read_UINT16(s, subtype);
848 if (subtype != CBT_TILESET)
850 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid subtype, expected CBT_TILESET.");
854 Stream_Seek_UINT16(s);
855 Stream_Seek_UINT16(s);
856 Stream_Read_UINT8(s, context->numQuant);
857 Stream_Seek_UINT8(s);
859 if (context->numQuant < 1)
861 WLog_Print(context->priv->log, WLOG_ERROR,
"no quantization value.");
865 Stream_Read_UINT16(s, numTiles);
872 Stream_Read_UINT32(s, tilesDataSize);
875 winpr_aligned_recalloc(context->quants, context->numQuant, 10 *
sizeof(UINT32), 32)))
878 quants = context->quants = (UINT32*)pmem;
881 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, context->numQuant, 5ull))
884 for (
size_t i = 0; i < context->numQuant; i++)
887 Stream_Read_UINT8(s, quant);
888 *quants++ = (quant & 0x0F);
889 *quants++ = (quant >> 4);
890 Stream_Read_UINT8(s, quant);
891 *quants++ = (quant & 0x0F);
892 *quants++ = (quant >> 4);
893 Stream_Read_UINT8(s, quant);
894 *quants++ = (quant & 0x0F);
895 *quants++ = (quant >> 4);
896 Stream_Read_UINT8(s, quant);
897 *quants++ = (quant & 0x0F);
898 *quants++ = (quant >> 4);
899 Stream_Read_UINT8(s, quant);
900 *quants++ = (quant & 0x0F);
901 *quants++ = (quant >> 4);
902 WLog_Print(context->priv->log, WLOG_DEBUG,
903 "quant %" PRIuz
" (%" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
904 " %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
").",
905 i, context->quants[i * 10], context->quants[i * 10 + 1],
906 context->quants[i * 10 + 2], context->quants[i * 10 + 3],
907 context->quants[i * 10 + 4], context->quants[i * 10 + 5],
908 context->quants[i * 10 + 6], context->quants[i * 10 + 7],
909 context->quants[i * 10 + 8], context->quants[i * 10 + 9]);
912 for (
size_t i = 0; i < message->numTiles; i++)
914 ObjectPool_Return(context->priv->TilePool, message->tiles[i]);
915 message->tiles[i] =
nullptr;
918 if (!rfx_allocate_tiles(message, numTiles, FALSE))
921 if (context->priv->UseThreads)
923 work_objects = (PTP_WORK*)winpr_aligned_calloc(message->numTiles,
sizeof(PTP_WORK), 32);
924 params = (RFX_TILE_PROCESS_WORK_PARAM*)winpr_aligned_recalloc(
925 nullptr, message->numTiles,
sizeof(RFX_TILE_PROCESS_WORK_PARAM), 32);
929 winpr_aligned_free(params);
935 winpr_aligned_free((
void*)work_objects);
944 if (Stream_GetRemainingLength(s) >= tilesDataSize)
947 for (
size_t i = 0; i < message->numTiles; i++)
952 if (!(tile = (
RFX_TILE*)ObjectPool_Take(context->priv->TilePool)))
954 WLog_Print(context->priv->log, WLOG_ERROR,
955 "RfxMessageTileSet failed to get tile from object pool");
960 message->tiles[i] = tile;
963 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
965 WLog_Print(context->priv->log, WLOG_ERROR,
966 "RfxMessageTileSet packet too small to read tile %" PRIuz
"/%" PRIu16
"",
967 i, message->numTiles);
972 sub = Stream_StaticInit(&subBuffer, Stream_Pointer(s), Stream_GetRemainingLength(s));
975 Stream_Read_UINT32(sub, blockLen);
977 if (!Stream_SafeSeek(s, blockLen))
982 if ((blockLen < 6 + 13) ||
983 (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, sub, blockLen - 6)))
985 WLog_Print(context->priv->log, WLOG_ERROR,
986 "RfxMessageTileSet not enough bytes to read tile %" PRIuz
"/%" PRIu16
987 " with blocklen=%" PRIu32
"",
988 i, message->numTiles, blockLen);
993 if (blockType != CBT_TILE)
995 WLog_Print(context->priv->log, WLOG_ERROR,
996 "unknown block type 0x%" PRIX32
", expected CBT_TILE (0xCAC3).",
1002 Stream_Read_UINT8(sub, tile->quantIdxY);
1003 Stream_Read_UINT8(sub, tile->quantIdxCb);
1004 Stream_Read_UINT8(sub, tile->quantIdxCr);
1005 if (tile->quantIdxY >= context->numQuant)
1007 WLog_Print(context->priv->log, WLOG_ERROR,
1008 "quantIdxY %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxY,
1013 if (tile->quantIdxCb >= context->numQuant)
1015 WLog_Print(context->priv->log, WLOG_ERROR,
1016 "quantIdxCb %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxCb,
1021 if (tile->quantIdxCr >= context->numQuant)
1023 WLog_Print(context->priv->log, WLOG_ERROR,
1024 "quantIdxCr %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxCr,
1030 Stream_Read_UINT16(sub, tile->xIdx);
1031 Stream_Read_UINT16(sub, tile->yIdx);
1032 Stream_Read_UINT16(sub, tile->YLen);
1033 Stream_Read_UINT16(sub, tile->CbLen);
1034 Stream_Read_UINT16(sub, tile->CrLen);
1035 Stream_GetPointer(sub, tile->YData);
1036 if (!Stream_SafeSeek(sub, tile->YLen))
1041 Stream_GetPointer(sub, tile->CbData);
1042 if (!Stream_SafeSeek(sub, tile->CbLen))
1047 Stream_GetPointer(sub, tile->CrData);
1048 if (!Stream_SafeSeek(sub, tile->CrLen))
1053 tile->x = tile->xIdx * 64;
1054 tile->y = tile->yIdx * 64;
1056 if (context->priv->UseThreads)
1064 params[i].context = context;
1065 params[i].tile = message->tiles[i];
1067 if (!(work_objects[i] = CreateThreadpoolWork(rfx_process_message_tile_work_callback,
1068 (
void*)¶ms[i],
nullptr)))
1070 WLog_Print(context->priv->log, WLOG_ERROR,
"CreateThreadpoolWork failed.");
1075 SubmitThreadpoolWork(work_objects[i]);
1080 if (!rfx_decode_rgb(context, tile, tile->data, 64 * 4))
1089 if (context->priv->UseThreads)
1091 for (
size_t i = 0; i < close_cnt; i++)
1093 WaitForThreadpoolWorkCallbacks(work_objects[i], FALSE);
1094 CloseThreadpoolWork(work_objects[i]);
1098 winpr_aligned_free((
void*)work_objects);
1099 winpr_aligned_free(params);
1101 for (
size_t i = 0; i < message->numTiles; i++)
1103 if (!(tile = message->tiles[i]))
1106 tile->YLen = tile->CbLen = tile->CrLen = 0;
1107 tile->YData = tile->CbData = tile->CrData =
nullptr;
1113BOOL rfx_process_message(RFX_CONTEXT* WINPR_RESTRICT context,
const BYTE* WINPR_RESTRICT data,
1114 UINT32 length, UINT32 left, UINT32 top, BYTE* WINPR_RESTRICT dst,
1115 UINT32 dstFormat, UINT32 dstStride, UINT32 dstHeight,
1116 REGION16* WINPR_RESTRICT invalidRegion)
1118 REGION16 updateRegion = WINPR_C_ARRAY_INIT;
1119 wStream inStream = WINPR_C_ARRAY_INIT;
1122 if (!context || !data || !length)
1125 WINPR_ASSERT(context->priv);
1126 RFX_MESSAGE* message = &context->currentMessage;
1128 wStream* s = Stream_StaticConstInit(&inStream, data, length);
1130 while (ok && Stream_GetRemainingLength(s) > 6)
1132 wStream subStreamBuffer = WINPR_C_ARRAY_INIT;
1133 size_t extraBlockLen = 0;
1134 UINT32 blockLen = 0;
1135 UINT32 blockType = 0;
1138 Stream_Read_UINT16(s, blockType);
1139 Stream_Read_UINT32(s, blockLen);
1140 WLog_Print(context->priv->log, WLOG_DEBUG,
"blockType 0x%" PRIX32
" blockLen %" PRIu32
"",
1141 blockType, blockLen);
1145 WLog_Print(context->priv->log, WLOG_ERROR,
"blockLen too small(%" PRIu32
")", blockLen);
1149 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, blockLen - 6))
1152 if (blockType > WBT_CONTEXT && context->decodedHeaderBlocks != RFX_DECODED_HEADERS)
1154 WLog_Print(context->priv->log, WLOG_ERROR,
"incomplete header blocks processing");
1158 if (blockType >= WBT_CONTEXT && blockType <= WBT_EXTENSION)
1162 UINT8 channelId = 0;
1164 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1168 Stream_Read_UINT8(s, codecId);
1169 Stream_Read_UINT8(s, channelId);
1171 if (codecId != 0x01)
1173 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codecId 0x%02" PRIX8
"",
1178 if (blockType == WBT_CONTEXT)
1181 if (channelId != 0xFF)
1183 WLog_Print(context->priv->log, WLOG_ERROR,
1184 "invalid channelId 0x%02" PRIX8
" for blockType 0x%08" PRIX32
"",
1185 channelId, blockType);
1192 if (channelId != 0x00)
1194 WLog_Print(context->priv->log, WLOG_ERROR,
1195 "invalid channelId 0x%02" PRIX8
" for blockType WBT_CONTEXT",
1202 const size_t blockLenNoHeader = blockLen - 6;
1203 if (blockLenNoHeader < extraBlockLen)
1205 WLog_Print(context->priv->log, WLOG_ERROR,
1206 "blockLen too small(%" PRIu32
"), must be >= 6 + %" PRIuz, blockLen,
1211 const size_t subStreamLen = blockLenNoHeader - extraBlockLen;
1212 wStream* subStream = Stream_StaticInit(&subStreamBuffer, Stream_Pointer(s), subStreamLen);
1213 Stream_Seek(s, subStreamLen);
1222 ok = rfx_process_message_sync(context, subStream);
1226 ok = rfx_process_message_context(context, subStream);
1229 case WBT_CODEC_VERSIONS:
1230 ok = rfx_process_message_codec_versions(context, subStream);
1234 ok = rfx_process_message_channels(context, subStream);
1244 case WBT_FRAME_BEGIN:
1245 ok = rfx_process_message_frame_begin(context, message, subStream,
1246 &context->expectedDataBlockType);
1250 ok = rfx_process_message_region(context, message, subStream,
1251 &context->expectedDataBlockType);
1255 ok = rfx_process_message_tileset(context, message, subStream,
1256 &context->expectedDataBlockType);
1260 ok = rfx_process_message_frame_end(context, message, subStream,
1261 &context->expectedDataBlockType);
1265 WLog_Print(context->priv->log, WLOG_ERROR,
"unknown blockType 0x%" PRIX32
"",
1273 UINT32 nbUpdateRects = 0;
1274 REGION16 clippingRects = WINPR_C_ARRAY_INIT;
1276 const DWORD formatSize = FreeRDPGetBytesPerPixel(context->pixel_format);
1277 const UINT32 dstWidth = dstStride / FreeRDPGetBytesPerPixel(dstFormat);
1278 region16_init(&clippingRects);
1280 WINPR_ASSERT(dstWidth <= UINT16_MAX);
1281 WINPR_ASSERT(dstHeight <= UINT16_MAX);
1282 for (UINT32 i = 0; i < message->numRects; i++)
1285 const RFX_RECT* rect = &(message->rects[i]);
1287 WINPR_ASSERT(left + rect->x <= UINT16_MAX);
1288 WINPR_ASSERT(top + rect->y <= UINT16_MAX);
1289 WINPR_ASSERT(clippingRect.left + rect->width <= UINT16_MAX);
1290 WINPR_ASSERT(clippingRect.top + rect->height <= UINT16_MAX);
1292 clippingRect.left = WINPR_ASSERTING_INT_CAST(UINT16, MIN(left + rect->x, dstWidth));
1293 clippingRect.top = WINPR_ASSERTING_INT_CAST(UINT16, MIN(top + rect->y, dstHeight));
1295 const UINT32 rw = 1UL * clippingRect.left + rect->width;
1296 const UINT32 rh = 1UL * clippingRect.top + rect->height;
1297 const uint16_t right = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rw, dstWidth));
1298 const uint16_t bottom = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rh, dstHeight));
1299 clippingRect.right = right;
1300 clippingRect.bottom = bottom;
1301 if (!region16_union_rect(&clippingRects, &clippingRects, &clippingRect))
1303 region16_uninit(&updateRegion);
1304 region16_uninit(&clippingRects);
1309 for (UINT32 i = 0; i < message->numTiles; i++)
1312 const RFX_TILE* tile = rfx_message_get_tile(message, i);
1314 WINPR_ASSERT(left + tile->x <= UINT16_MAX);
1315 WINPR_ASSERT(top + tile->y <= UINT16_MAX);
1317 updateRect.left = (UINT16)left + tile->x;
1318 updateRect.top = (UINT16)top + tile->y;
1319 updateRect.right = updateRect.left + 64;
1320 updateRect.bottom = updateRect.top + 64;
1321 region16_init(&updateRegion);
1322 if (!region16_intersect_rect(&updateRegion, &clippingRects, &updateRect))
1324 region16_uninit(&updateRegion);
1325 region16_uninit(&clippingRects);
1328 updateRects = region16_rects(&updateRegion, &nbUpdateRects);
1330 for (UINT32 j = 0; j < nbUpdateRects; j++)
1333 const UINT32 stride = 64 * formatSize;
1334 const UINT32 nXDst = cur->left;
1335 const UINT32 nYDst = cur->top;
1336 const UINT32 nXSrc = nXDst - updateRect.left;
1337 const UINT32 nYSrc = nYDst - updateRect.top;
1338 const UINT32 nWidth = cur->right - cur->left;
1339 const UINT32 nHeight = cur->bottom - cur->top;
1341 if (!freerdp_image_copy_no_overlap(
1342 dst, dstFormat, dstStride, nXDst, nYDst, nWidth, nHeight, tile->data,
1343 context->pixel_format, stride, nXSrc, nYSrc,
nullptr, FREERDP_FLIP_NONE))
1345 region16_uninit(&updateRegion);
1346 region16_uninit(&clippingRects);
1347 WLog_Print(context->priv->log, WLOG_ERROR,
1348 "nbUpdateRectx[%" PRIu32
" (%" PRIu32
")] freerdp_image_copy failed",
1355 if (!region16_union_rect(invalidRegion, invalidRegion, cur))
1357 region16_uninit(&updateRegion);
1358 region16_uninit(&clippingRects);
1364 region16_uninit(&updateRegion);
1367 region16_uninit(&clippingRects);
1372 rfx_message_free(context, message);
1373 context->currentMessage.freeArray = TRUE;
1376 WLog_Print(context->priv->log, WLOG_ERROR,
"failed");
1380const UINT32* rfx_message_get_quants(
const RFX_MESSAGE* WINPR_RESTRICT message,
1381 UINT16* WINPR_RESTRICT numQuantVals)
1383 WINPR_ASSERT(message);
1385 *numQuantVals = message->numQuant;
1386 return message->quantVals;
1389const RFX_TILE** rfx_message_get_tiles(
const RFX_MESSAGE* WINPR_RESTRICT message,
1390 UINT16* WINPR_RESTRICT numTiles)
1392 WINPR_ASSERT(message);
1394 *numTiles = message->numTiles;
1401 cnv.pp = message->tiles;
1405UINT16 rfx_message_get_tile_count(
const RFX_MESSAGE* WINPR_RESTRICT message)
1407 WINPR_ASSERT(message);
1408 return message->numTiles;
1411const RFX_RECT* rfx_message_get_rects(
const RFX_MESSAGE* WINPR_RESTRICT message,
1412 UINT16* WINPR_RESTRICT numRects)
1414 WINPR_ASSERT(message);
1416 *numRects = message->numRects;
1417 return message->rects;
1420UINT16 rfx_message_get_rect_count(
const RFX_MESSAGE* WINPR_RESTRICT message)
1422 WINPR_ASSERT(message);
1423 return message->numRects;
1426void rfx_message_free(RFX_CONTEXT* WINPR_RESTRICT context, RFX_MESSAGE* WINPR_RESTRICT message)
1431 winpr_aligned_free(message->rects);
1435 for (
size_t i = 0; i < message->numTiles; i++)
1437 RFX_TILE* tile = message->tiles[i];
1441 if (tile->YCbCrData)
1443 BufferPool_Return(context->priv->BufferPool, tile->YCbCrData);
1444 tile->YCbCrData =
nullptr;
1447 ObjectPool_Return(context->priv->TilePool, (
void*)tile);
1450 rfx_allocate_tiles(message, 0, FALSE);
1453 const BOOL freeArray = message->freeArray;
1454 const RFX_MESSAGE empty = WINPR_C_ARRAY_INIT;
1458 winpr_aligned_free(message);
1461static inline void rfx_update_context_properties(RFX_CONTEXT* WINPR_RESTRICT context)
1463 UINT16 properties = 0;
1465 WINPR_ASSERT(context);
1468 properties |= (context->flags << 1);
1469 properties |= (COL_CONV_ICT << 4);
1470 properties |= (CLW_XFORM_DWT_53_A << 6);
1471 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 10);
1472 properties |= (SCALAR_QUANTIZATION << 14);
1473 context->properties = properties;
1477rfx_write_message_sync(WINPR_ATTR_UNUSED
const RFX_CONTEXT* WINPR_RESTRICT context,
1478 WINPR_ATTR_UNUSED
wStream* WINPR_RESTRICT s)
1480 WINPR_ASSERT(context);
1482 Stream_Write_UINT16(s, WBT_SYNC);
1483 Stream_Write_UINT32(s, 12);
1484 Stream_Write_UINT32(s, WF_MAGIC);
1485 Stream_Write_UINT16(s, WF_VERSION_1_0);
1489rfx_write_message_codec_versions(WINPR_ATTR_UNUSED
const RFX_CONTEXT* WINPR_RESTRICT context,
1492 WINPR_ASSERT(context);
1494 Stream_Write_UINT16(s, WBT_CODEC_VERSIONS);
1495 Stream_Write_UINT32(s, 10);
1496 Stream_Write_UINT8(s, 1);
1497 Stream_Write_UINT8(s, 1);
1498 Stream_Write_UINT16(s, WF_VERSION_1_0);
1501static inline void rfx_write_message_channels(
const RFX_CONTEXT* WINPR_RESTRICT context,
1504 WINPR_ASSERT(context);
1506 Stream_Write_UINT16(s, WBT_CHANNELS);
1507 Stream_Write_UINT32(s, 12);
1508 Stream_Write_UINT8(s, 1);
1509 Stream_Write_UINT8(s, 0);
1510 Stream_Write_UINT16(s, context->width);
1511 Stream_Write_UINT16(s, context->height);
1514static inline void rfx_write_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
1517 UINT16 properties = 0;
1518 WINPR_ASSERT(context);
1520 Stream_Write_UINT16(s, WBT_CONTEXT);
1521 Stream_Write_UINT32(s, 13);
1522 Stream_Write_UINT8(s, 1);
1523 Stream_Write_UINT8(s, 0xFF);
1524 Stream_Write_UINT8(s, 0);
1525 Stream_Write_UINT16(s, CT_TILE_64x64);
1527 properties = context->flags;
1528 properties |= (COL_CONV_ICT << 3);
1529 properties |= (CLW_XFORM_DWT_53_A << 5);
1530 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 9);
1531 properties |= (SCALAR_QUANTIZATION << 13);
1532 Stream_Write_UINT16(s, properties);
1533 rfx_update_context_properties(context);
1536static inline BOOL rfx_compose_message_header(RFX_CONTEXT* WINPR_RESTRICT context,
1539 WINPR_ASSERT(context);
1540 if (!Stream_EnsureRemainingCapacity(s, 12 + 10 + 12 + 13))
1543 rfx_write_message_sync(context, s);
1544 rfx_write_message_context(context, s);
1545 rfx_write_message_codec_versions(context, s);
1546 rfx_write_message_channels(context, s);
1550static inline size_t rfx_tile_length(
const RFX_TILE* WINPR_RESTRICT tile)
1553 return 19ull + tile->YLen + tile->CbLen + tile->CrLen;
1556static inline BOOL rfx_write_tile(
wStream* WINPR_RESTRICT s,
const RFX_TILE* WINPR_RESTRICT tile)
1558 const size_t blockLen = rfx_tile_length(tile);
1559 if (blockLen > UINT32_MAX)
1561 if (!Stream_EnsureRemainingCapacity(s, blockLen))
1564 Stream_Write_UINT16(s, CBT_TILE);
1565 Stream_Write_UINT32(s, (UINT32)blockLen);
1566 Stream_Write_UINT8(s, tile->quantIdxY);
1567 Stream_Write_UINT8(s, tile->quantIdxCb);
1568 Stream_Write_UINT8(s, tile->quantIdxCr);
1569 Stream_Write_UINT16(s, tile->xIdx);
1570 Stream_Write_UINT16(s, tile->yIdx);
1571 Stream_Write_UINT16(s, tile->YLen);
1572 Stream_Write_UINT16(s, tile->CbLen);
1573 Stream_Write_UINT16(s, tile->CrLen);
1574 Stream_Write(s, tile->YData, tile->YLen);
1575 Stream_Write(s, tile->CbData, tile->CbLen);
1576 Stream_Write(s, tile->CrData, tile->CrLen);
1580struct S_RFX_TILE_COMPOSE_WORK_PARAM
1583 RFX_CONTEXT* context;
1586static inline void CALLBACK
1587rfx_compose_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
1588 void* context, WINPR_ATTR_UNUSED PTP_WORK work)
1590 RFX_TILE_COMPOSE_WORK_PARAM* param = (RFX_TILE_COMPOSE_WORK_PARAM*)context;
1591 WINPR_ASSERT(param);
1592 WINPR_ASSERT(param->context);
1593 WINPR_ASSERT(param->context->priv);
1595 if (!rfx_encode_rgb(param->context, param->tile))
1596 WLog_Print(param->context->priv->log, WLOG_ERROR,
"rfx_encode_rgb failed");
1599static inline BOOL computeRegion(
const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1600 REGION16* WINPR_RESTRICT region,
size_t width,
size_t height)
1602 const RECTANGLE_16 mainRect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, width),
1603 WINPR_ASSERTING_INT_CAST(UINT16, height) };
1605 WINPR_ASSERT(rects);
1606 for (
size_t i = 0; i < numRects; i++)
1610 rect16.left = rect->x;
1611 rect16.top = rect->y;
1612 rect16.right = rect->x + rect->width;
1613 rect16.bottom = rect->y + rect->height;
1615 if (!region16_union_rect(region, region, &rect16))
1619 return region16_intersect_rect(region, region, &mainRect);
1622#define TILE_NO(v) ((v) / 64)
1624static inline BOOL setupWorkers(RFX_CONTEXT* WINPR_RESTRICT context,
size_t nbTiles)
1626 WINPR_ASSERT(context);
1628 RFX_CONTEXT_PRIV* priv = context->priv;
1631 void* pmem =
nullptr;
1633 if (!context->priv->UseThreads)
1636 if (!(pmem = winpr_aligned_recalloc((
void*)priv->workObjects, nbTiles,
sizeof(PTP_WORK), 32)))
1639 priv->workObjects = (PTP_WORK*)pmem;
1641 if (!(pmem = winpr_aligned_recalloc(priv->tileWorkParams, nbTiles,
1642 sizeof(RFX_TILE_COMPOSE_WORK_PARAM), 32)))
1645 priv->tileWorkParams = (RFX_TILE_COMPOSE_WORK_PARAM*)pmem;
1649static inline BOOL rfx_ensure_tiles(RFX_MESSAGE* WINPR_RESTRICT message,
size_t count)
1651 WINPR_ASSERT(message);
1653 if (message->numTiles + count <= message->allocatedTiles)
1656 const size_t alloc = MAX(message->allocatedTiles + 1024, message->numTiles + count);
1657 return rfx_allocate_tiles(message, alloc, TRUE);
1660RFX_MESSAGE* rfx_encode_message(RFX_CONTEXT* WINPR_RESTRICT context,
1661 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1662 const BYTE* WINPR_RESTRICT data, UINT32 w, UINT32 h,
size_t s)
1664 const UINT32 width = w;
1665 const UINT32 height = h;
1666 const UINT32 scanline = (UINT32)s;
1667 RFX_MESSAGE* message =
nullptr;
1668 PTP_WORK* workObject =
nullptr;
1669 RFX_TILE_COMPOSE_WORK_PARAM* workParam =
nullptr;
1670 BOOL success = FALSE;
1671 REGION16 rectsRegion = WINPR_C_ARRAY_INIT;
1672 REGION16 tilesRegion = WINPR_C_ARRAY_INIT;
1677 WINPR_ASSERT(rects);
1678 WINPR_ASSERT(numRects > 0);
1679 WINPR_ASSERT(w > 0);
1680 WINPR_ASSERT(h > 0);
1681 WINPR_ASSERT(s > 0);
1683 if (!(message = (RFX_MESSAGE*)winpr_aligned_calloc(1,
sizeof(RFX_MESSAGE), 32)))
1686 region16_init(&tilesRegion);
1687 region16_init(&rectsRegion);
1689 if (context->state == RFX_STATE_SEND_HEADERS)
1690 rfx_update_context_properties(context);
1692 message->frameIdx = context->frameIdx++;
1694 if (!context->numQuant)
1696 WINPR_ASSERT(context->quants ==
nullptr);
1697 if (!(context->quants =
1698 (UINT32*)winpr_aligned_malloc(
sizeof(rfx_default_quantization_values), 32)))
1699 goto skip_encoding_loop;
1701 CopyMemory(context->quants, &rfx_default_quantization_values,
1702 sizeof(rfx_default_quantization_values));
1703 context->numQuant = 1;
1704 context->quantIdxY = 0;
1705 context->quantIdxCb = 0;
1706 context->quantIdxCr = 0;
1709 message->numQuant = context->numQuant;
1710 message->quantVals = context->quants;
1713 const UINT32 bytesPerPixel = (context->bits_per_pixel / 8);
1714 if (!computeRegion(rects, numRects, &rectsRegion, width, height))
1715 goto skip_encoding_loop;
1718 const RECTANGLE_16* extents = region16_extents(&rectsRegion);
1719 WINPR_ASSERT((INT32)extents->right - extents->left > 0);
1720 WINPR_ASSERT((INT32)extents->bottom - extents->top > 0);
1721 const UINT32 maxTilesX = 1 + TILE_NO(extents->right - 1) - TILE_NO(extents->left);
1722 const UINT32 maxTilesY = 1 + TILE_NO(extents->bottom - 1) - TILE_NO(extents->top);
1723 const UINT32 maxNbTiles = maxTilesX * maxTilesY;
1725 if (!rfx_ensure_tiles(message, maxNbTiles))
1726 goto skip_encoding_loop;
1728 if (!setupWorkers(context, maxNbTiles))
1729 goto skip_encoding_loop;
1732 if (context->priv->UseThreads)
1734 workObject = context->priv->workObjects;
1735 workParam = context->priv->tileWorkParams;
1739 UINT32 regionNbRects = 0;
1740 regionRect = region16_rects(&rectsRegion, ®ionNbRects);
1742 if (!(message->rects = winpr_aligned_calloc(regionNbRects,
sizeof(
RFX_RECT), 32)))
1743 goto skip_encoding_loop;
1745 message->numRects = WINPR_ASSERTING_INT_CAST(UINT16, regionNbRects);
1747 for (UINT32 i = 0; i < regionNbRects; i++, regionRect++)
1749 RFX_RECT* rfxRect = &message->rects[i];
1750 UINT32 startTileX = regionRect->left / 64;
1751 UINT32 endTileX = (regionRect->right - 1) / 64;
1752 UINT32 startTileY = regionRect->top / 64;
1753 UINT32 endTileY = (regionRect->bottom - 1) / 64;
1754 rfxRect->x = regionRect->left;
1755 rfxRect->y = regionRect->top;
1756 rfxRect->width = (regionRect->right - regionRect->left);
1757 rfxRect->height = (regionRect->bottom - regionRect->top);
1759 for (UINT32 yIdx = startTileY, gridRelY = startTileY * 64; yIdx <= endTileY;
1760 yIdx++, gridRelY += 64)
1762 UINT32 tileHeight = 64;
1764 if ((yIdx == endTileY) && (gridRelY + 64 > height))
1765 tileHeight = height - gridRelY;
1767 currentTileRect.top = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1768 currentTileRect.bottom =
1769 WINPR_ASSERTING_INT_CAST(UINT16, gridRelY + tileHeight);
1771 for (UINT32 xIdx = startTileX, gridRelX = startTileX * 64; xIdx <= endTileX;
1772 xIdx++, gridRelX += 64)
1779 UINT32 tileWidth = 64;
1781 if ((xIdx == endTileX) && (gridRelX + 64 > width))
1783 tileWidth = (width - gridRelX);
1786 currentTileRect.left = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1787 currentTileRect.right =
1788 WINPR_ASSERTING_INT_CAST(UINT16, gridRelX + tileWidth);
1791 if (region16_intersects_rect(&tilesRegion, ¤tTileRect))
1796 goto skip_encoding_loop;
1798 tile->xIdx = WINPR_ASSERTING_INT_CAST(UINT16, xIdx);
1799 tile->yIdx = WINPR_ASSERTING_INT_CAST(UINT16, yIdx);
1800 tile->x = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1801 tile->y = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1802 tile->scanline = scanline;
1804 tile->width = tileWidth;
1805 tile->height = tileHeight;
1806 const UINT32 ax = gridRelX;
1807 const UINT32 ay = gridRelY;
1809 if (tile->data && tile->allocated)
1811 winpr_aligned_free(tile->data);
1812 tile->allocated = FALSE;
1816 cnv.cpv = &data[(ay * scanline) + (ax * bytesPerPixel)];
1817 tile->data = cnv.pv;
1818 tile->quantIdxY = context->quantIdxY;
1819 tile->quantIdxCb = context->quantIdxCb;
1820 tile->quantIdxCr = context->quantIdxCr;
1821 tile->YLen = tile->CbLen = tile->CrLen = 0;
1823 if (!(tile->YCbCrData =
1824 (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
1825 goto skip_encoding_loop;
1827 tile->YData = &(tile->YCbCrData[((8192 + 32) * 0) + 16]);
1828 tile->CbData = &(tile->YCbCrData[((8192 + 32) * 1) + 16]);
1829 tile->CrData = &(tile->YCbCrData[((8192 + 32) * 2) + 16]);
1831 if (!rfx_ensure_tiles(message, 1))
1832 goto skip_encoding_loop;
1833 message->tiles[message->numTiles++] = tile;
1835 if (context->priv->UseThreads)
1837 workParam->context = context;
1838 workParam->tile = tile;
1841 CreateThreadpoolWork(rfx_compose_message_tile_work_callback,
1842 (
void*)workParam,
nullptr)))
1844 goto skip_encoding_loop;
1847 SubmitThreadpoolWork(*workObject);
1853 if (!rfx_encode_rgb(context, tile))
1854 goto skip_encoding_loop;
1857 if (!region16_union_rect(&tilesRegion, &tilesRegion, ¤tTileRect))
1858 goto skip_encoding_loop;
1871 message->tilesDataSize = 0;
1872 workObject = context->priv->workObjects;
1874 for (UINT32 i = 0; i < message->numTiles; i++)
1876 if (context->priv->UseThreads)
1880 WaitForThreadpoolWorkCallbacks(*workObject, FALSE);
1881 CloseThreadpoolWork(*workObject);
1887 const RFX_TILE* tile = message->tiles[i];
1888 const size_t tlen = rfx_tile_length(tile);
1889 message->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tlen);
1892 region16_uninit(&tilesRegion);
1893 region16_uninit(&rectsRegion);
1898 WLog_Print(context->priv->log, WLOG_ERROR,
"failed");
1900 rfx_message_free(context, message);
1901 region16_uninit(&tilesRegion);
1902 region16_uninit(&rectsRegion);
1906static inline BOOL rfx_clone_rects(RFX_MESSAGE* WINPR_RESTRICT dst,
1907 const RFX_MESSAGE* WINPR_RESTRICT src)
1912 WINPR_ASSERT(dst->rects ==
nullptr);
1913 WINPR_ASSERT(dst->numRects == 0);
1915 if (src->numRects == 0)
1918 dst->rects = winpr_aligned_calloc(src->numRects,
sizeof(
RECTANGLE_16), 32);
1921 dst->numRects = src->numRects;
1922 for (
size_t x = 0; x < src->numRects; x++)
1924 dst->rects[x] = src->rects[x];
1929static inline BOOL rfx_clone_quants(RFX_MESSAGE* WINPR_RESTRICT dst,
1930 const RFX_MESSAGE* WINPR_RESTRICT src)
1935 WINPR_ASSERT(dst->quantVals ==
nullptr);
1936 WINPR_ASSERT(dst->numQuant == 0);
1938 if (src->numQuant == 0)
1942 dst->quantVals = src->quantVals;
1943 dst->numQuant = src->numQuant;
1948static inline RFX_MESSAGE* rfx_split_message(RFX_CONTEXT* WINPR_RESTRICT context,
1949 RFX_MESSAGE* WINPR_RESTRICT message,
1950 size_t* WINPR_RESTRICT numMessages,
size_t maxDataSize)
1952 WINPR_ASSERT(context);
1953 WINPR_ASSERT(message);
1954 WINPR_ASSERT(numMessages);
1956 maxDataSize -= 1024;
1957 *numMessages = ((message->tilesDataSize + maxDataSize) / maxDataSize) * 4ull;
1959 RFX_MESSAGE* messages =
1960 (RFX_MESSAGE*)winpr_aligned_calloc((*numMessages),
sizeof(RFX_MESSAGE), 32);
1965 for (UINT16 i = 0; i < message->numTiles; i++)
1967 RFX_TILE* tile = message->tiles[i];
1968 RFX_MESSAGE* msg = &messages[j];
1973 const size_t tileDataSize = rfx_tile_length(tile);
1975 if ((msg->tilesDataSize + tileDataSize) > ((UINT32)maxDataSize))
1978 if (msg->numTiles == 0)
1980 msg->frameIdx = message->frameIdx + j;
1981 if (!rfx_clone_quants(msg, message))
1983 if (!rfx_clone_rects(msg, message))
1985 msg->freeArray = TRUE;
1986 if (!rfx_allocate_tiles(msg, message->numTiles, TRUE))
1990 msg->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tileDataSize);
1992 WINPR_ASSERT(msg->numTiles < msg->allocatedTiles);
1993 msg->tiles[msg->numTiles++] = message->tiles[i];
1994 message->tiles[i] =
nullptr;
1997 *numMessages = j + 1ULL;
1998 context->frameIdx += j;
1999 message->numTiles = 0;
2003 for (
size_t i = 0; i < j; i++)
2004 rfx_allocate_tiles(&messages[i], 0, FALSE);
2006 winpr_aligned_free(messages);
2010const RFX_MESSAGE* rfx_message_list_get(
const RFX_MESSAGE_LIST* WINPR_RESTRICT messages,
size_t idx)
2012 WINPR_ASSERT(messages);
2013 if (idx >= messages->count)
2015 WINPR_ASSERT(messages->list);
2016 return &messages->list[idx];
2019void rfx_message_list_free(RFX_MESSAGE_LIST* messages)
2023 for (
size_t x = 0; x < messages->count; x++)
2024 rfx_message_free(messages->context, &messages->list[x]);
2028static inline RFX_MESSAGE_LIST* rfx_message_list_new(RFX_CONTEXT* WINPR_RESTRICT context,
2029 RFX_MESSAGE* WINPR_RESTRICT messages,
2032 WINPR_ASSERT(context);
2033 RFX_MESSAGE_LIST* msg = calloc(1,
sizeof(RFX_MESSAGE_LIST));
2036 msg->context = context;
2038 msg->list = messages;
2042RFX_MESSAGE_LIST* rfx_encode_messages(RFX_CONTEXT* WINPR_RESTRICT context,
2043 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
2044 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2045 UINT32 scanline,
size_t* WINPR_RESTRICT numMessages,
2048 WINPR_ASSERT(context);
2049 WINPR_ASSERT(numMessages);
2051 RFX_MESSAGE* message =
2052 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2056 RFX_MESSAGE* list = rfx_split_message(context, message, numMessages, maxDataSize);
2057 rfx_message_free(context, message);
2061 return rfx_message_list_new(context, list, *numMessages);
2064static inline BOOL rfx_write_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
2066 const RFX_MESSAGE* WINPR_RESTRICT message)
2068 WINPR_ASSERT(context);
2069 WINPR_ASSERT(message);
2071 const UINT32 blockLen = 22 + (message->numQuant * 5) + message->tilesDataSize;
2073 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2076 Stream_Write_UINT16(s, WBT_EXTENSION);
2077 Stream_Write_UINT32(s, blockLen);
2078 Stream_Write_UINT8(s, 1);
2079 Stream_Write_UINT8(s, 0);
2080 Stream_Write_UINT16(s, CBT_TILESET);
2081 Stream_Write_UINT16(s, 0);
2082 Stream_Write_UINT16(s, context->properties);
2084 s, WINPR_ASSERTING_INT_CAST(uint8_t, message->numQuant));
2085 Stream_Write_UINT8(s, 0x40);
2086 Stream_Write_UINT16(s, message->numTiles);
2087 Stream_Write_UINT32(s, message->tilesDataSize);
2089 UINT32* quantVals = message->quantVals;
2090 for (
size_t i = 0; i < message->numQuant * 5ul; i++)
2092 WINPR_ASSERT(quantVals);
2093 Stream_Write_UINT8(s,
2094 WINPR_ASSERTING_INT_CAST(uint8_t, quantVals[0] + (quantVals[1] << 4)));
2098 for (
size_t i = 0; i < message->numTiles; i++)
2100 RFX_TILE* tile = message->tiles[i];
2104 if (!rfx_write_tile(s, tile))
2108#ifdef WITH_DEBUG_RFX
2109 WLog_Print(context->priv->log, WLOG_DEBUG,
2110 "numQuant: %" PRIu16
" numTiles: %" PRIu16
" tilesDataSize: %" PRIu32
"",
2111 message->numQuant, message->numTiles, message->tilesDataSize);
2117rfx_write_message_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2118 wStream* WINPR_RESTRICT s,
const RFX_MESSAGE* WINPR_RESTRICT message)
2120 WINPR_ASSERT(context);
2121 WINPR_ASSERT(message);
2123 if (!Stream_EnsureRemainingCapacity(s, 14))
2126 Stream_Write_UINT16(s, WBT_FRAME_BEGIN);
2127 Stream_Write_UINT32(s, 14);
2128 Stream_Write_UINT8(s, 1);
2129 Stream_Write_UINT8(s, 0);
2130 Stream_Write_UINT32(s, message->frameIdx);
2131 Stream_Write_UINT16(s, 1);
2135static inline BOOL rfx_write_message_region(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2137 const RFX_MESSAGE* WINPR_RESTRICT message)
2139 WINPR_ASSERT(context);
2140 WINPR_ASSERT(message);
2142 const size_t blockLen = 15 + (message->numRects * 8);
2143 if (blockLen > UINT32_MAX)
2146 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2149 Stream_Write_UINT16(s, WBT_REGION);
2150 Stream_Write_UINT32(s, (UINT32)blockLen);
2151 Stream_Write_UINT8(s, 1);
2152 Stream_Write_UINT8(s, 0);
2153 Stream_Write_UINT8(s, 1);
2154 Stream_Write_UINT16(s, message->numRects);
2156 for (UINT16 i = 0; i < message->numRects; i++)
2158 const RFX_RECT* rect = rfx_message_get_rect_const(message, i);
2162 Stream_Write_UINT16(s, rect->x);
2163 Stream_Write_UINT16(s, rect->y);
2164 Stream_Write_UINT16(s, rect->width);
2165 Stream_Write_UINT16(s, rect->height);
2168 Stream_Write_UINT16(s, CBT_REGION);
2169 Stream_Write_UINT16(s, 1);
2174rfx_write_message_frame_end(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2176 WINPR_ATTR_UNUSED
const RFX_MESSAGE* WINPR_RESTRICT message)
2178 WINPR_ASSERT(context);
2179 WINPR_ASSERT(message);
2181 if (!Stream_EnsureRemainingCapacity(s, 8))
2184 Stream_Write_UINT16(s, WBT_FRAME_END);
2185 Stream_Write_UINT32(s, 8);
2186 Stream_Write_UINT8(s, 1);
2187 Stream_Write_UINT8(s, 0);
2191BOOL rfx_write_message(RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2192 const RFX_MESSAGE* WINPR_RESTRICT message)
2194 WINPR_ASSERT(context);
2195 WINPR_ASSERT(message);
2197 if (context->state == RFX_STATE_SEND_HEADERS)
2199 if (!rfx_compose_message_header(context, s))
2202 context->state = RFX_STATE_SEND_FRAME_DATA;
2205 if (!rfx_write_message_frame_begin(context, s, message) ||
2206 !rfx_write_message_region(context, s, message) ||
2207 !rfx_write_message_tileset(context, s, message) ||
2208 !rfx_write_message_frame_end(context, s, message))
2216BOOL rfx_compose_message(RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2217 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
2218 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2221 WINPR_ASSERT(context);
2222 RFX_MESSAGE* message =
2223 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2227 const BOOL ret = rfx_write_message(context, s, message);
2228 rfx_message_free(context, message);
2232BOOL rfx_context_set_mode(RFX_CONTEXT* WINPR_RESTRICT context, RLGR_MODE mode)
2234 WINPR_ASSERT(context);
2235 context->mode = mode;
2239RLGR_MODE rfx_context_get_mode(RFX_CONTEXT* WINPR_RESTRICT context)
2241 WINPR_ASSERT(context);
2242 return context->mode;
2245UINT32 rfx_context_get_frame_idx(
const RFX_CONTEXT* WINPR_RESTRICT context)
2247 WINPR_ASSERT(context);
2248 return context->frameIdx;
2251UINT32 rfx_message_get_frame_idx(
const RFX_MESSAGE* WINPR_RESTRICT message)
2253 WINPR_ASSERT(message);
2254 return message->frameIdx;
2257static inline BOOL rfx_write_progressive_wb_sync(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2260 const UINT32 blockLen = 12;
2264 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2267 Stream_Write_UINT16(s, PROGRESSIVE_WBT_SYNC);
2268 Stream_Write_UINT32(s, blockLen);
2269 Stream_Write_UINT32(s, 0xCACCACCA);
2270 Stream_Write_UINT16(s, 0x0100);
2275rfx_write_progressive_wb_context(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2278 const UINT32 blockLen = 10;
2282 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2285 Stream_Write_UINT16(s, PROGRESSIVE_WBT_CONTEXT);
2286 Stream_Write_UINT32(s, blockLen);
2287 Stream_Write_UINT8(s, 0);
2288 Stream_Write_UINT16(s, 64);
2289 Stream_Write_UINT8(s, 0);
2293static inline BOOL rfx_write_progressive_region(RFX_CONTEXT* WINPR_RESTRICT rfx,
2295 const RFX_MESSAGE* WINPR_RESTRICT msg)
2298 UINT32 blockLen = 18;
2299 UINT32 tilesDataSize = 0;
2300 const size_t start = Stream_GetPosition(s);
2306 blockLen += msg->numRects * 8;
2307 blockLen += msg->numQuant * 5;
2308 tilesDataSize = msg->numTiles * 22UL;
2309 for (UINT16 i = 0; i < msg->numTiles; i++)
2311 const RFX_TILE* tile = msg->tiles[i];
2313 tilesDataSize += tile->YLen + tile->CbLen + tile->CrLen;
2315 blockLen += tilesDataSize;
2317 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2320 Stream_Write_UINT16(s, PROGRESSIVE_WBT_REGION);
2321 Stream_Write_UINT32(s, blockLen);
2322 Stream_Write_UINT8(s, 64);
2323 Stream_Write_UINT16(s, msg->numRects);
2324 WINPR_ASSERT(msg->numQuant <= UINT8_MAX);
2325 Stream_Write_UINT8(s, (UINT8)msg->numQuant);
2326 Stream_Write_UINT8(s, 0);
2327 Stream_Write_UINT8(s, 0);
2328 Stream_Write_UINT16(s, msg->numTiles);
2329 Stream_Write_UINT32(s, tilesDataSize);
2331 for (UINT16 i = 0; i < msg->numRects; i++)
2334 const RFX_RECT* r = &msg->rects[i];
2335 Stream_Write_UINT16(s, r->x);
2336 Stream_Write_UINT16(s, r->y);
2337 Stream_Write_UINT16(s, r->width);
2338 Stream_Write_UINT16(s, r->height);
2349 for (UINT16 i = 0; i < msg->numQuant; i++)
2351 const UINT32* qv = &msg->quantVals[10ULL * i];
2353 Stream_Write_UINT8(s, (UINT8)(qv[0] + (qv[2] << 4)));
2354 Stream_Write_UINT8(s, (UINT8)(qv[1] + (qv[3] << 4)));
2355 Stream_Write_UINT8(s, (UINT8)(qv[5] + (qv[4] << 4)));
2356 Stream_Write_UINT8(s, (UINT8)(qv[6] + (qv[8] << 4)));
2357 Stream_Write_UINT8(s, (UINT8)(qv[7] + (qv[9] << 4)));
2360 for (UINT16 i = 0; i < msg->numTiles; i++)
2362 const RFX_TILE* tile = msg->tiles[i];
2363 if (!rfx_write_progressive_tile_simple(rfx, s, tile))
2367 const size_t end = Stream_GetPosition(s);
2368 const size_t used = end - start;
2369 return (used == blockLen);
2373rfx_write_progressive_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2374 wStream* WINPR_RESTRICT s,
const RFX_MESSAGE* WINPR_RESTRICT msg)
2376 const UINT32 blockLen = 12;
2381 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2384 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_BEGIN);
2385 Stream_Write_UINT32(s, blockLen);
2386 Stream_Write_UINT32(s, msg->frameIdx);
2387 Stream_Write_UINT16(s, 1);
2393rfx_write_progressive_frame_end(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2396 const UINT32 blockLen = 6;
2400 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2403 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_END);
2404 Stream_Write_UINT32(s, blockLen);
2410rfx_write_progressive_tile_simple(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2413 UINT32 blockLen = 0;
2418 blockLen = 22 + tile->YLen + tile->CbLen + tile->CrLen;
2419 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2422 Stream_Write_UINT16(s, PROGRESSIVE_WBT_TILE_SIMPLE);
2423 Stream_Write_UINT32(s, blockLen);
2424 Stream_Write_UINT8(s, tile->quantIdxY);
2425 Stream_Write_UINT8(s, tile->quantIdxCb);
2426 Stream_Write_UINT8(s, tile->quantIdxCr);
2427 Stream_Write_UINT16(s, tile->xIdx);
2428 Stream_Write_UINT16(s, tile->yIdx);
2429 Stream_Write_UINT8(s, 0);
2430 Stream_Write_UINT16(s, tile->YLen);
2431 Stream_Write_UINT16(s, tile->CbLen);
2432 Stream_Write_UINT16(s, tile->CrLen);
2433 Stream_Write_UINT16(s, 0);
2434 Stream_Write(s, tile->YData, tile->YLen);
2435 Stream_Write(s, tile->CbData, tile->CbLen);
2436 Stream_Write(s, tile->CrData, tile->CrLen);
2441const char* rfx_get_progressive_block_type_string(UINT16 blockType)
2445 case PROGRESSIVE_WBT_SYNC:
2446 return "PROGRESSIVE_WBT_SYNC";
2448 case PROGRESSIVE_WBT_FRAME_BEGIN:
2449 return "PROGRESSIVE_WBT_FRAME_BEGIN";
2451 case PROGRESSIVE_WBT_FRAME_END:
2452 return "PROGRESSIVE_WBT_FRAME_END";
2454 case PROGRESSIVE_WBT_CONTEXT:
2455 return "PROGRESSIVE_WBT_CONTEXT";
2457 case PROGRESSIVE_WBT_REGION:
2458 return "PROGRESSIVE_WBT_REGION";
2460 case PROGRESSIVE_WBT_TILE_SIMPLE:
2461 return "PROGRESSIVE_WBT_TILE_SIMPLE";
2463 case PROGRESSIVE_WBT_TILE_FIRST:
2464 return "PROGRESSIVE_WBT_TILE_FIRST";
2466 case PROGRESSIVE_WBT_TILE_UPGRADE:
2467 return "PROGRESSIVE_WBT_TILE_UPGRADE";
2470 return "PROGRESSIVE_WBT_UNKNOWN";
2474BOOL rfx_write_message_progressive_simple(RFX_CONTEXT* WINPR_RESTRICT rfx,
2476 const RFX_MESSAGE* WINPR_RESTRICT msg)
2482 if (rfx->mode != RLGR1)
2484 WLog_ERR(TAG,
"error, RLGR1 mode is required!");
2488 if (!rfx_write_progressive_wb_sync(rfx, s))
2491 if (!rfx_write_progressive_wb_context(rfx, s))
2494 if (!rfx_write_progressive_frame_begin(rfx, s, msg))
2497 if (!rfx_write_progressive_region(rfx, s, msg))
2500 if (!rfx_write_progressive_frame_end(rfx, s))
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree
OBJECT_INIT_FN fnObjectInit
OBJECT_NEW_FN fnObjectNew