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"
51#include "sse/rfx_sse2.h"
52#include "neon/rfx_neon.h"
54#define TAG FREERDP_TAG("codec")
56#define RFX_KEY "Software\\" FREERDP_VENDOR_STRING "\\" FREERDP_PRODUCT_STRING "\\RemoteFX"
70static const UINT32 rfx_default_quantization_values[] = { 6, 6, 6, 6, 7, 7, 8, 8, 8, 9 };
72static INLINE BOOL rfx_write_progressive_tile_simple(RFX_CONTEXT* WINPR_RESTRICT rfx,
74 const RFX_TILE* WINPR_RESTRICT tile);
76static INLINE
void rfx_profiler_create(RFX_CONTEXT* WINPR_RESTRICT context)
78 if (!context || !context->priv)
80 PROFILER_CREATE(context->priv->prof_rfx_decode_rgb,
"rfx_decode_rgb")
81 PROFILER_CREATE(context->priv->prof_rfx_decode_component, "rfx_decode_component")
82 PROFILER_CREATE(context->priv->prof_rfx_rlgr_decode, "rfx_rlgr_decode")
83 PROFILER_CREATE(context->priv->prof_rfx_differential_decode, "rfx_differential_decode")
84 PROFILER_CREATE(context->priv->prof_rfx_quantization_decode, "rfx_quantization_decode")
85 PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_decode, "rfx_dwt_2d_decode")
86 PROFILER_CREATE(context->priv->prof_rfx_ycbcr_to_rgb, "prims->yCbCrToRGB")
87 PROFILER_CREATE(context->priv->prof_rfx_encode_rgb, "rfx_encode_rgb")
88 PROFILER_CREATE(context->priv->prof_rfx_encode_component, "rfx_encode_component")
89 PROFILER_CREATE(context->priv->prof_rfx_rlgr_encode, "rfx_rlgr_encode")
90 PROFILER_CREATE(context->priv->prof_rfx_differential_encode, "rfx_differential_encode")
91 PROFILER_CREATE(context->priv->prof_rfx_quantization_encode, "rfx_quantization_encode")
92 PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_encode, "rfx_dwt_2d_encode")
93 PROFILER_CREATE(context->priv->prof_rfx_rgb_to_ycbcr, "prims->RGBToYCbCr")
94 PROFILER_CREATE(context->priv->prof_rfx_encode_format_rgb, "rfx_encode_format_rgb")
97static INLINE
void rfx_profiler_free(RFX_CONTEXT* WINPR_RESTRICT context)
99 if (!context || !context->priv)
101 PROFILER_FREE(context->priv->prof_rfx_decode_rgb)
102 PROFILER_FREE(context->priv->prof_rfx_decode_component)
103 PROFILER_FREE(context->priv->prof_rfx_rlgr_decode)
104 PROFILER_FREE(context->priv->prof_rfx_differential_decode)
105 PROFILER_FREE(context->priv->prof_rfx_quantization_decode)
106 PROFILER_FREE(context->priv->prof_rfx_dwt_2d_decode)
107 PROFILER_FREE(context->priv->prof_rfx_ycbcr_to_rgb)
108 PROFILER_FREE(context->priv->prof_rfx_encode_rgb)
109 PROFILER_FREE(context->priv->prof_rfx_encode_component)
110 PROFILER_FREE(context->priv->prof_rfx_rlgr_encode)
111 PROFILER_FREE(context->priv->prof_rfx_differential_encode)
112 PROFILER_FREE(context->priv->prof_rfx_quantization_encode)
113 PROFILER_FREE(context->priv->prof_rfx_dwt_2d_encode)
114 PROFILER_FREE(context->priv->prof_rfx_rgb_to_ycbcr)
115 PROFILER_FREE(context->priv->prof_rfx_encode_format_rgb)
118static INLINE
void rfx_profiler_print(RFX_CONTEXT* WINPR_RESTRICT context)
120 if (!context || !context->priv)
123 PROFILER_PRINT_HEADER
124 PROFILER_PRINT(context->priv->prof_rfx_decode_rgb)
125 PROFILER_PRINT(context->priv->prof_rfx_decode_component)
126 PROFILER_PRINT(context->priv->prof_rfx_rlgr_decode)
127 PROFILER_PRINT(context->priv->prof_rfx_differential_decode)
128 PROFILER_PRINT(context->priv->prof_rfx_quantization_decode)
129 PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_decode)
130 PROFILER_PRINT(context->priv->prof_rfx_ycbcr_to_rgb)
131 PROFILER_PRINT(context->priv->prof_rfx_encode_rgb)
132 PROFILER_PRINT(context->priv->prof_rfx_encode_component)
133 PROFILER_PRINT(context->priv->prof_rfx_rlgr_encode)
134 PROFILER_PRINT(context->priv->prof_rfx_differential_encode)
135 PROFILER_PRINT(context->priv->prof_rfx_quantization_encode)
136 PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_encode)
137 PROFILER_PRINT(context->priv->prof_rfx_rgb_to_ycbcr)
138 PROFILER_PRINT(context->priv->prof_rfx_encode_format_rgb)
139 PROFILER_PRINT_FOOTER
142static INLINE
void rfx_tile_init(
void* obj)
158static INLINE
void* rfx_decoder_tile_new(
const void* val)
160 const size_t size = 4ULL * 64ULL * 64ULL;
167 if (!(tile->data = (BYTE*)winpr_aligned_malloc(size, 16)))
169 winpr_aligned_free(tile);
172 memset(tile->data, 0xff, size);
173 tile->allocated = TRUE;
177static INLINE
void rfx_decoder_tile_free(
void* obj)
184 winpr_aligned_free(tile->data);
186 winpr_aligned_free(tile);
190static INLINE
void* rfx_encoder_tile_new(
const void* val)
193 return winpr_aligned_calloc(1,
sizeof(
RFX_TILE), 32);
196static INLINE
void rfx_encoder_tile_free(
void* obj)
198 winpr_aligned_free(obj);
201RFX_CONTEXT* rfx_context_new(BOOL encoder)
203 return rfx_context_new_ex(encoder, 0);
206RFX_CONTEXT* rfx_context_new_ex(BOOL encoder, UINT32 ThreadingFlags)
208 RFX_CONTEXT_PRIV* priv = NULL;
209 RFX_CONTEXT* context = (RFX_CONTEXT*)winpr_aligned_calloc(1,
sizeof(RFX_CONTEXT), 32);
214 context->encoder = encoder;
215 context->currentMessage.freeArray = TRUE;
216 context->priv = priv = (RFX_CONTEXT_PRIV*)winpr_aligned_calloc(1,
sizeof(RFX_CONTEXT_PRIV), 32);
221 priv->log = WLog_Get(
"com.freerdp.codec.rfx");
222 WLog_OpenAppender(priv->log);
223 priv->TilePool = ObjectPool_New(TRUE);
228 wObject* pool = ObjectPool_Object(priv->TilePool);
229 pool->fnObjectInit = rfx_tile_init;
231 if (context->encoder)
233 pool->fnObjectNew = rfx_encoder_tile_new;
234 pool->fnObjectFree = rfx_encoder_tile_free;
238 pool->fnObjectNew = rfx_decoder_tile_new;
239 pool->fnObjectFree = rfx_decoder_tile_free;
255 priv->BufferPool = BufferPool_New(TRUE, (8192ULL + 32ULL) * 3ULL, 16);
257 if (!priv->BufferPool)
260 if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS))
263 priv->UseThreads = TRUE;
266 RegOpenKeyExA(HKEY_LOCAL_MACHINE, RFX_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
268 if (status == ERROR_SUCCESS)
272 DWORD dwSize =
sizeof(dwValue);
274 if (RegQueryValueEx(hKey, _T(
"UseThreads"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) ==
276 priv->UseThreads = dwValue ? 1 : 0;
283 priv->UseThreads = FALSE;
286 if (priv->UseThreads)
295 rfx_context_set_pixel_format(context, PIXEL_FORMAT_BGRX32);
297 rfx_profiler_create(context);
299 context->quantization_decode = rfx_quantization_decode;
300 context->quantization_encode = rfx_quantization_encode;
301 context->dwt_2d_decode = rfx_dwt_2d_decode;
302 context->dwt_2d_extrapolate_decode = rfx_dwt_2d_extrapolate_decode;
303 context->dwt_2d_encode = rfx_dwt_2d_encode;
304 context->rlgr_decode = rfx_rlgr_decode;
305 context->rlgr_encode = rfx_rlgr_encode;
306 rfx_init_sse2(context);
307 rfx_init_neon(context);
308 context->state = RFX_STATE_SEND_HEADERS;
309 context->expectedDataBlockType = WBT_FRAME_BEGIN;
312 WINPR_PRAGMA_DIAG_PUSH
313 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
314 rfx_context_free(context);
315 WINPR_PRAGMA_DIAG_POP
319void rfx_context_free(RFX_CONTEXT* context)
321 RFX_CONTEXT_PRIV* priv = NULL;
326 WINPR_ASSERT(NULL != context);
328 priv = context->priv;
329 WINPR_ASSERT(NULL != priv);
330 WINPR_ASSERT(NULL != priv->TilePool);
331 WINPR_ASSERT(NULL != priv->BufferPool);
334 rfx_message_free(context, &context->currentMessage);
335 winpr_aligned_free(context->quants);
336 rfx_profiler_print(context);
337 rfx_profiler_free(context);
341 ObjectPool_Free(priv->TilePool);
342 if (priv->UseThreads)
344 winpr_aligned_free((
void*)priv->workObjects);
345 winpr_aligned_free(priv->tileWorkParams);
349 "WARNING: Profiling results probably unusable with multithreaded RemoteFX codec!");
353 BufferPool_Free(priv->BufferPool);
354 winpr_aligned_free(priv);
356 winpr_aligned_free(context);
359static INLINE
RFX_TILE* rfx_message_get_tile(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index)
361 WINPR_ASSERT(message);
362 WINPR_ASSERT(message->tiles);
363 WINPR_ASSERT(index < message->numTiles);
364 return message->tiles[index];
367static INLINE
const RFX_RECT* rfx_message_get_rect_const(
const RFX_MESSAGE* WINPR_RESTRICT message,
370 WINPR_ASSERT(message);
371 WINPR_ASSERT(message->rects);
372 WINPR_ASSERT(index < message->numRects);
373 return &message->rects[index];
376static INLINE
RFX_RECT* rfx_message_get_rect(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index)
378 WINPR_ASSERT(message);
379 WINPR_ASSERT(message->rects);
380 WINPR_ASSERT(index < message->numRects);
381 return &message->rects[index];
384void rfx_context_set_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 pixel_format)
386 WINPR_ASSERT(context);
387 context->pixel_format = pixel_format;
388 const UINT32 bpp = FreeRDPGetBitsPerPixel(pixel_format);
389 context->bits_per_pixel = WINPR_ASSERTING_INT_CAST(UINT8, bpp);
392UINT32 rfx_context_get_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context)
394 WINPR_ASSERT(context);
395 return context->pixel_format;
398void rfx_context_set_palette(RFX_CONTEXT* WINPR_RESTRICT context,
399 const BYTE* WINPR_RESTRICT palette)
401 WINPR_ASSERT(context);
402 context->palette = palette;
405const BYTE* rfx_context_get_palette(RFX_CONTEXT* WINPR_RESTRICT context)
407 WINPR_ASSERT(context);
408 return context->palette;
411BOOL rfx_context_reset(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
416 context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
417 context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
418 context->state = RFX_STATE_SEND_HEADERS;
419 context->expectedDataBlockType = WBT_FRAME_BEGIN;
420 context->frameIdx = 0;
424static INLINE BOOL rfx_process_message_sync(RFX_CONTEXT* WINPR_RESTRICT context,
429 WINPR_ASSERT(context);
430 WINPR_ASSERT(context->priv);
431 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_SYNC;
434 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
437 Stream_Read_UINT32(s, magic);
438 if (magic != WF_MAGIC)
440 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid magic number 0x%08" PRIX32
"", magic);
444 Stream_Read_UINT16(s, context->version);
445 if (context->version != WF_VERSION_1_0)
447 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid version number 0x%08" PRIX32
"",
452 WLog_Print(context->priv->log, WLOG_DEBUG,
"version 0x%08" PRIX32
"", context->version);
453 context->decodedHeaderBlocks |= RFX_DECODED_SYNC;
457static INLINE BOOL rfx_process_message_codec_versions(RFX_CONTEXT* WINPR_RESTRICT context,
462 WINPR_ASSERT(context);
463 WINPR_ASSERT(context->priv);
464 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_VERSIONS;
466 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
469 Stream_Read_UINT8(s, numCodecs);
470 Stream_Read_UINT8(s, context->codec_id);
472 s, context->codec_version);
476 WLog_Print(context->priv->log, WLOG_ERROR,
"numCodes is 0x%02" PRIX8
" (must be 0x01)",
481 if (context->codec_id != 0x01)
483 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codec id (0x%02" PRIX32
")",
488 if (context->codec_version != WF_VERSION_1_0)
490 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codec version (0x%08" PRIX32
")",
491 context->codec_version);
495 WLog_Print(context->priv->log, WLOG_DEBUG,
"id %" PRIu32
" version 0x%" PRIX32
".",
496 context->codec_id, context->codec_version);
497 context->decodedHeaderBlocks |= RFX_DECODED_VERSIONS;
501static INLINE BOOL rfx_process_message_channels(RFX_CONTEXT* WINPR_RESTRICT context,
505 BYTE numChannels = 0;
507 WINPR_ASSERT(context);
508 WINPR_ASSERT(context->priv);
509 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CHANNELS;
511 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 1))
514 Stream_Read_UINT8(s, numChannels);
521 WLog_Print(context->priv->log, WLOG_ERROR,
"no channels announced");
525 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, numChannels, 5ull))
529 Stream_Read_UINT8(s, channelId);
531 if (channelId != 0x00)
533 WLog_Print(context->priv->log, WLOG_ERROR,
"channelId:0x%02" PRIX8
", expected:0x00",
538 Stream_Read_UINT16(s, context->width);
539 Stream_Read_UINT16(s, context->height);
541 if (!context->width || !context->height)
543 WLog_Print(context->priv->log, WLOG_ERROR,
544 "invalid channel with/height: %" PRIu16
"x%" PRIu16
"", context->width,
550 Stream_Seek(s, 5ULL * (numChannels - 1));
551 WLog_Print(context->priv->log, WLOG_DEBUG,
552 "numChannels %" PRIu8
" id %" PRIu8
", %" PRIu16
"x%" PRIu16
".", numChannels,
553 channelId, context->width, context->height);
554 context->decodedHeaderBlocks |= RFX_DECODED_CHANNELS;
558static INLINE BOOL rfx_process_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
563 UINT16 properties = 0;
565 WINPR_ASSERT(context);
566 WINPR_ASSERT(context->priv);
567 context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CONTEXT;
569 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 5))
572 Stream_Read_UINT8(s, ctxId);
573 Stream_Read_UINT16(s, tileSize);
574 Stream_Read_UINT16(s, properties);
575 WLog_Print(context->priv->log, WLOG_DEBUG,
576 "ctxId %" PRIu8
" tileSize %" PRIu16
" properties 0x%04" PRIX16
".", ctxId, tileSize,
578 context->properties = properties;
579 context->flags = (properties & 0x0007);
581 if (context->flags == CODEC_MODE)
583 WLog_Print(context->priv->log, WLOG_DEBUG,
"codec is in image mode.");
587 WLog_Print(context->priv->log, WLOG_DEBUG,
"codec is in video mode.");
590 switch ((properties & 0x1E00) >> 9)
592 case CLW_ENTROPY_RLGR1:
593 context->mode = RLGR1;
594 WLog_Print(context->priv->log, WLOG_DEBUG,
"RLGR1.");
597 case CLW_ENTROPY_RLGR3:
598 context->mode = RLGR3;
599 WLog_Print(context->priv->log, WLOG_DEBUG,
"RLGR3.");
603 WLog_Print(context->priv->log, WLOG_ERROR,
"unknown RLGR algorithm.");
607 context->decodedHeaderBlocks |= RFX_DECODED_CONTEXT;
611static INLINE BOOL rfx_process_message_frame_begin(
612 RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message,
613 wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType)
616 UINT16 numRegions = 0;
618 WINPR_ASSERT(context);
619 WINPR_ASSERT(context->priv);
620 WINPR_ASSERT(message);
621 WINPR_ASSERT(pExpectedBlockType);
623 if (*pExpectedBlockType != WBT_FRAME_BEGIN)
625 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants WBT_FRAME_BEGIN");
629 *pExpectedBlockType = WBT_REGION;
631 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
636 Stream_Read_UINT16(s, numRegions);
637 WLog_Print(context->priv->log, WLOG_DEBUG,
638 "RFX_FRAME_BEGIN: frameIdx: %" PRIu32
" numRegions: %" PRIu16
"", frameIdx,
643static INLINE BOOL rfx_process_message_frame_end(
644 RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message,
645 WINPR_ATTR_UNUSED
wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType)
647 WINPR_ASSERT(context);
648 WINPR_ASSERT(context->priv);
649 WINPR_ASSERT(message);
651 WINPR_ASSERT(pExpectedBlockType);
653 if (*pExpectedBlockType != WBT_FRAME_END)
655 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected, wants WBT_FRAME_END");
659 *pExpectedBlockType = WBT_FRAME_BEGIN;
660 WLog_Print(context->priv->log, WLOG_DEBUG,
"RFX_FRAME_END");
664static INLINE BOOL rfx_resize_rects(RFX_MESSAGE* WINPR_RESTRICT message)
666 WINPR_ASSERT(message);
669 winpr_aligned_recalloc(message->rects, message->numRects,
sizeof(
RFX_RECT), 32);
672 message->rects = tmpRects;
676static INLINE BOOL rfx_process_message_region(RFX_CONTEXT* WINPR_RESTRICT context,
677 RFX_MESSAGE* WINPR_RESTRICT message,
679 UINT16* WINPR_RESTRICT pExpectedBlockType)
681 UINT16 regionType = 0;
682 UINT16 numTileSets = 0;
684 WINPR_ASSERT(context);
685 WINPR_ASSERT(context->priv);
686 WINPR_ASSERT(message);
687 WINPR_ASSERT(pExpectedBlockType);
689 if (*pExpectedBlockType != WBT_REGION)
691 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants WBT_REGION");
695 *pExpectedBlockType = WBT_EXTENSION;
697 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 3))
700 Stream_Seek_UINT8(s);
701 Stream_Read_UINT16(s, message->numRects);
703 if (message->numRects < 1)
711 message->numRects = 1;
712 if (!rfx_resize_rects(message))
715 message->rects->x = 0;
716 message->rects->y = 0;
717 message->rects->width = context->width;
718 message->rects->height = context->height;
722 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, message->numRects, 8ull))
725 if (!rfx_resize_rects(message))
729 for (UINT16 i = 0; i < message->numRects; i++)
731 RFX_RECT* rect = rfx_message_get_rect(message, i);
733 Stream_Read_UINT16(s, rect->x);
734 Stream_Read_UINT16(s, rect->y);
735 Stream_Read_UINT16(s, rect->width);
736 Stream_Read_UINT16(s, rect->height);
737 WLog_Print(context->priv->log, WLOG_DEBUG,
738 "rect %" PRIu16
" (x,y=%" PRIu16
",%" PRIu16
" w,h=%" PRIu16
" %" PRIu16
").", i,
739 rect->x, rect->y, rect->width, rect->height);
742 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
745 Stream_Read_UINT16(s, regionType);
746 Stream_Read_UINT16(s, numTileSets);
748 if (regionType != CBT_REGION)
750 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid region type 0x%04" PRIX16
"",
755 if (numTileSets != 0x0001)
757 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid number of tilesets (%" PRIu16
")",
768 RFX_CONTEXT* context;
769} RFX_TILE_PROCESS_WORK_PARAM;
771static INLINE
void CALLBACK
772rfx_process_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
773 void* context, WINPR_ATTR_UNUSED PTP_WORK work)
775 RFX_TILE_PROCESS_WORK_PARAM* param = (RFX_TILE_PROCESS_WORK_PARAM*)context;
777 rfx_decode_rgb(param->context, param->tile, param->tile->data, 64 * 4);
780static INLINE BOOL rfx_allocate_tiles(RFX_MESSAGE* WINPR_RESTRICT message,
size_t count,
783 WINPR_ASSERT(message);
786 (
RFX_TILE**)winpr_aligned_recalloc((
void*)message->tiles, count,
sizeof(
RFX_TILE*), 32);
787 if (!tmpTiles && (count != 0))
790 message->tiles = tmpTiles;
792 message->numTiles = WINPR_ASSERTING_INT_CAST(UINT16, count);
795 WINPR_ASSERT(message->numTiles <= count);
797 message->allocatedTiles = count;
802static INLINE BOOL rfx_process_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
803 RFX_MESSAGE* WINPR_RESTRICT message,
805 UINT16* WINPR_RESTRICT pExpectedBlockType)
808 size_t close_cnt = 0;
811 UINT32* quants = NULL;
815 UINT32 blockType = 0;
816 UINT32 tilesDataSize = 0;
817 PTP_WORK* work_objects = NULL;
818 RFX_TILE_PROCESS_WORK_PARAM* params = NULL;
821 WINPR_ASSERT(context);
822 WINPR_ASSERT(context->priv);
823 WINPR_ASSERT(message);
824 WINPR_ASSERT(pExpectedBlockType);
826 if (*pExpectedBlockType != WBT_EXTENSION)
828 WLog_Print(context->priv->log, WLOG_ERROR,
"message unexpected wants a tileset");
832 *pExpectedBlockType = WBT_FRAME_END;
834 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 14))
837 Stream_Read_UINT16(s, subtype);
838 if (subtype != CBT_TILESET)
840 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid subtype, expected CBT_TILESET.");
844 Stream_Seek_UINT16(s);
845 Stream_Seek_UINT16(s);
846 Stream_Read_UINT8(s, context->numQuant);
847 Stream_Seek_UINT8(s);
849 if (context->numQuant < 1)
851 WLog_Print(context->priv->log, WLOG_ERROR,
"no quantization value.");
855 Stream_Read_UINT16(s, numTiles);
862 Stream_Read_UINT32(s, tilesDataSize);
865 winpr_aligned_recalloc(context->quants, context->numQuant, 10 *
sizeof(UINT32), 32)))
868 quants = context->quants = (UINT32*)pmem;
871 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, context->numQuant, 5ull))
874 for (
size_t i = 0; i < context->numQuant; i++)
877 Stream_Read_UINT8(s, quant);
878 *quants++ = (quant & 0x0F);
879 *quants++ = (quant >> 4);
880 Stream_Read_UINT8(s, quant);
881 *quants++ = (quant & 0x0F);
882 *quants++ = (quant >> 4);
883 Stream_Read_UINT8(s, quant);
884 *quants++ = (quant & 0x0F);
885 *quants++ = (quant >> 4);
886 Stream_Read_UINT8(s, quant);
887 *quants++ = (quant & 0x0F);
888 *quants++ = (quant >> 4);
889 Stream_Read_UINT8(s, quant);
890 *quants++ = (quant & 0x0F);
891 *quants++ = (quant >> 4);
892 WLog_Print(context->priv->log, WLOG_DEBUG,
893 "quant %" PRIuz
" (%" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
894 " %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
" %" PRIu32
").",
895 i, context->quants[i * 10], context->quants[i * 10 + 1],
896 context->quants[i * 10 + 2], context->quants[i * 10 + 3],
897 context->quants[i * 10 + 4], context->quants[i * 10 + 5],
898 context->quants[i * 10 + 6], context->quants[i * 10 + 7],
899 context->quants[i * 10 + 8], context->quants[i * 10 + 9]);
902 for (
size_t i = 0; i < message->numTiles; i++)
904 ObjectPool_Return(context->priv->TilePool, message->tiles[i]);
905 message->tiles[i] = NULL;
908 if (!rfx_allocate_tiles(message, numTiles, FALSE))
911 if (context->priv->UseThreads)
913 work_objects = (PTP_WORK*)winpr_aligned_calloc(message->numTiles,
sizeof(PTP_WORK), 32);
914 params = (RFX_TILE_PROCESS_WORK_PARAM*)winpr_aligned_recalloc(
915 NULL, message->numTiles,
sizeof(RFX_TILE_PROCESS_WORK_PARAM), 32);
919 winpr_aligned_free(params);
925 winpr_aligned_free((
void*)work_objects);
934 if (Stream_GetRemainingLength(s) >= tilesDataSize)
937 for (
size_t i = 0; i < message->numTiles; i++)
942 if (!(tile = (
RFX_TILE*)ObjectPool_Take(context->priv->TilePool)))
944 WLog_Print(context->priv->log, WLOG_ERROR,
945 "RfxMessageTileSet failed to get tile from object pool");
950 message->tiles[i] = tile;
953 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
955 WLog_Print(context->priv->log, WLOG_ERROR,
956 "RfxMessageTileSet packet too small to read tile %" PRIuz
"/%" PRIu16
"",
957 i, message->numTiles);
962 sub = Stream_StaticInit(&subBuffer, Stream_Pointer(s), Stream_GetRemainingLength(s));
965 Stream_Read_UINT32(sub, blockLen);
967 if (!Stream_SafeSeek(s, blockLen))
972 if ((blockLen < 6 + 13) ||
973 (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, sub, blockLen - 6)))
975 WLog_Print(context->priv->log, WLOG_ERROR,
976 "RfxMessageTileSet not enough bytes to read tile %" PRIuz
"/%" PRIu16
977 " with blocklen=%" PRIu32
"",
978 i, message->numTiles, blockLen);
983 if (blockType != CBT_TILE)
985 WLog_Print(context->priv->log, WLOG_ERROR,
986 "unknown block type 0x%" PRIX32
", expected CBT_TILE (0xCAC3).",
992 Stream_Read_UINT8(sub, tile->quantIdxY);
993 Stream_Read_UINT8(sub, tile->quantIdxCb);
994 Stream_Read_UINT8(sub, tile->quantIdxCr);
995 if (tile->quantIdxY >= context->numQuant)
997 WLog_Print(context->priv->log, WLOG_ERROR,
998 "quantIdxY %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxY,
1003 if (tile->quantIdxCb >= context->numQuant)
1005 WLog_Print(context->priv->log, WLOG_ERROR,
1006 "quantIdxCb %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxCb,
1011 if (tile->quantIdxCr >= context->numQuant)
1013 WLog_Print(context->priv->log, WLOG_ERROR,
1014 "quantIdxCr %" PRIu8
" >= numQuant %" PRIu8, tile->quantIdxCr,
1020 Stream_Read_UINT16(sub, tile->xIdx);
1021 Stream_Read_UINT16(sub, tile->yIdx);
1022 Stream_Read_UINT16(sub, tile->YLen);
1023 Stream_Read_UINT16(sub, tile->CbLen);
1024 Stream_Read_UINT16(sub, tile->CrLen);
1025 Stream_GetPointer(sub, tile->YData);
1026 if (!Stream_SafeSeek(sub, tile->YLen))
1031 Stream_GetPointer(sub, tile->CbData);
1032 if (!Stream_SafeSeek(sub, tile->CbLen))
1037 Stream_GetPointer(sub, tile->CrData);
1038 if (!Stream_SafeSeek(sub, tile->CrLen))
1043 tile->x = tile->xIdx * 64;
1044 tile->y = tile->yIdx * 64;
1046 if (context->priv->UseThreads)
1054 params[i].context = context;
1055 params[i].tile = message->tiles[i];
1057 if (!(work_objects[i] = CreateThreadpoolWork(rfx_process_message_tile_work_callback,
1058 (
void*)¶ms[i], NULL)))
1060 WLog_Print(context->priv->log, WLOG_ERROR,
"CreateThreadpoolWork failed.");
1065 SubmitThreadpoolWork(work_objects[i]);
1070 rfx_decode_rgb(context, tile, tile->data, 64 * 4);
1075 if (context->priv->UseThreads)
1077 for (
size_t i = 0; i < close_cnt; i++)
1079 WaitForThreadpoolWorkCallbacks(work_objects[i], FALSE);
1080 CloseThreadpoolWork(work_objects[i]);
1084 winpr_aligned_free((
void*)work_objects);
1085 winpr_aligned_free(params);
1087 for (
size_t i = 0; i < message->numTiles; i++)
1089 if (!(tile = message->tiles[i]))
1092 tile->YLen = tile->CbLen = tile->CrLen = 0;
1093 tile->YData = tile->CbData = tile->CrData = NULL;
1099BOOL rfx_process_message(RFX_CONTEXT* WINPR_RESTRICT context,
const BYTE* WINPR_RESTRICT data,
1100 UINT32 length, UINT32 left, UINT32 top, BYTE* WINPR_RESTRICT dst,
1101 UINT32 dstFormat, UINT32 dstStride, UINT32 dstHeight,
1102 REGION16* WINPR_RESTRICT invalidRegion)
1108 if (!context || !data || !length)
1111 WINPR_ASSERT(context->priv);
1112 RFX_MESSAGE* message = &context->currentMessage;
1114 wStream* s = Stream_StaticConstInit(&inStream, data, length);
1116 while (ok && Stream_GetRemainingLength(s) > 6)
1118 wStream subStreamBuffer = { 0 };
1119 size_t extraBlockLen = 0;
1120 UINT32 blockLen = 0;
1121 UINT32 blockType = 0;
1124 Stream_Read_UINT16(s, blockType);
1125 Stream_Read_UINT32(s, blockLen);
1126 WLog_Print(context->priv->log, WLOG_DEBUG,
"blockType 0x%" PRIX32
" blockLen %" PRIu32
"",
1127 blockType, blockLen);
1131 WLog_Print(context->priv->log, WLOG_ERROR,
"blockLen too small(%" PRIu32
")", blockLen);
1135 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, blockLen - 6))
1138 if (blockType > WBT_CONTEXT && context->decodedHeaderBlocks != RFX_DECODED_HEADERS)
1140 WLog_Print(context->priv->log, WLOG_ERROR,
"incomplete header blocks processing");
1144 if (blockType >= WBT_CONTEXT && blockType <= WBT_EXTENSION)
1148 UINT8 channelId = 0;
1150 if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1154 Stream_Read_UINT8(s, codecId);
1155 Stream_Read_UINT8(s, channelId);
1157 if (codecId != 0x01)
1159 WLog_Print(context->priv->log, WLOG_ERROR,
"invalid codecId 0x%02" PRIX8
"",
1164 if (blockType == WBT_CONTEXT)
1167 if (channelId != 0xFF)
1169 WLog_Print(context->priv->log, WLOG_ERROR,
1170 "invalid channelId 0x%02" PRIX8
" for blockType 0x%08" PRIX32
"",
1171 channelId, blockType);
1178 if (channelId != 0x00)
1180 WLog_Print(context->priv->log, WLOG_ERROR,
1181 "invalid channelId 0x%02" PRIX8
" for blockType WBT_CONTEXT",
1188 const size_t blockLenNoHeader = blockLen - 6;
1189 if (blockLenNoHeader < extraBlockLen)
1191 WLog_Print(context->priv->log, WLOG_ERROR,
1192 "blockLen too small(%" PRIu32
"), must be >= 6 + %" PRIuz, blockLen,
1197 const size_t subStreamLen = blockLenNoHeader - extraBlockLen;
1198 wStream* subStream = Stream_StaticInit(&subStreamBuffer, Stream_Pointer(s), subStreamLen);
1199 Stream_Seek(s, subStreamLen);
1208 ok = rfx_process_message_sync(context, subStream);
1212 ok = rfx_process_message_context(context, subStream);
1215 case WBT_CODEC_VERSIONS:
1216 ok = rfx_process_message_codec_versions(context, subStream);
1220 ok = rfx_process_message_channels(context, subStream);
1230 case WBT_FRAME_BEGIN:
1231 ok = rfx_process_message_frame_begin(context, message, subStream,
1232 &context->expectedDataBlockType);
1236 ok = rfx_process_message_region(context, message, subStream,
1237 &context->expectedDataBlockType);
1241 ok = rfx_process_message_tileset(context, message, subStream,
1242 &context->expectedDataBlockType);
1246 ok = rfx_process_message_frame_end(context, message, subStream,
1247 &context->expectedDataBlockType);
1251 WLog_Print(context->priv->log, WLOG_ERROR,
"unknown blockType 0x%" PRIX32
"",
1259 UINT32 nbUpdateRects = 0;
1262 const DWORD formatSize = FreeRDPGetBytesPerPixel(context->pixel_format);
1263 const UINT32 dstWidth = dstStride / FreeRDPGetBytesPerPixel(dstFormat);
1264 region16_init(&clippingRects);
1266 WINPR_ASSERT(dstWidth <= UINT16_MAX);
1267 WINPR_ASSERT(dstHeight <= UINT16_MAX);
1268 for (UINT32 i = 0; i < message->numRects; i++)
1271 const RFX_RECT* rect = &(message->rects[i]);
1273 WINPR_ASSERT(left + rect->x <= UINT16_MAX);
1274 WINPR_ASSERT(top + rect->y <= UINT16_MAX);
1275 WINPR_ASSERT(clippingRect.left + rect->width <= UINT16_MAX);
1276 WINPR_ASSERT(clippingRect.top + rect->height <= UINT16_MAX);
1278 clippingRect.left = WINPR_ASSERTING_INT_CAST(UINT16, MIN(left + rect->x, dstWidth));
1279 clippingRect.top = WINPR_ASSERTING_INT_CAST(UINT16, MIN(top + rect->y, dstHeight));
1281 const UINT32 rw = 1UL * clippingRect.left + rect->width;
1282 const UINT32 rh = 1UL * clippingRect.top + rect->height;
1283 const uint16_t right = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rw, dstWidth));
1284 const uint16_t bottom = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rh, dstHeight));
1285 clippingRect.right = right;
1286 clippingRect.bottom = bottom;
1287 region16_union_rect(&clippingRects, &clippingRects, &clippingRect);
1290 for (UINT32 i = 0; i < message->numTiles; i++)
1293 const RFX_TILE* tile = rfx_message_get_tile(message, i);
1295 WINPR_ASSERT(left + tile->x <= UINT16_MAX);
1296 WINPR_ASSERT(top + tile->y <= UINT16_MAX);
1298 updateRect.left = (UINT16)left + tile->x;
1299 updateRect.top = (UINT16)top + tile->y;
1300 updateRect.right = updateRect.left + 64;
1301 updateRect.bottom = updateRect.top + 64;
1302 region16_init(&updateRegion);
1303 region16_intersect_rect(&updateRegion, &clippingRects, &updateRect);
1304 updateRects = region16_rects(&updateRegion, &nbUpdateRects);
1306 for (UINT32 j = 0; j < nbUpdateRects; j++)
1309 const UINT32 stride = 64 * formatSize;
1310 const UINT32 nXDst = cur->left;
1311 const UINT32 nYDst = cur->top;
1312 const UINT32 nXSrc = nXDst - updateRect.left;
1313 const UINT32 nYSrc = nYDst - updateRect.top;
1314 const UINT32 nWidth = cur->right - cur->left;
1315 const UINT32 nHeight = cur->bottom - cur->top;
1317 if (!freerdp_image_copy_no_overlap(dst, dstFormat, dstStride, nXDst, nYDst, nWidth,
1318 nHeight, tile->data, context->pixel_format,
1319 stride, nXSrc, nYSrc, NULL, FREERDP_FLIP_NONE))
1321 region16_uninit(&updateRegion);
1322 region16_uninit(&clippingRects);
1323 WLog_Print(context->priv->log, WLOG_ERROR,
1324 "nbUpdateRectx[%" PRIu32
" (%" PRIu32
")] freerdp_image_copy failed",
1330 region16_union_rect(invalidRegion, invalidRegion, cur);
1333 region16_uninit(&updateRegion);
1336 region16_uninit(&clippingRects);
1341 rfx_message_free(context, message);
1342 context->currentMessage.freeArray = TRUE;
1345 WLog_Print(context->priv->log, WLOG_ERROR,
"failed");
1349const UINT32* rfx_message_get_quants(
const RFX_MESSAGE* WINPR_RESTRICT message,
1350 UINT16* WINPR_RESTRICT numQuantVals)
1352 WINPR_ASSERT(message);
1354 *numQuantVals = message->numQuant;
1355 return message->quantVals;
1358const RFX_TILE** rfx_message_get_tiles(
const RFX_MESSAGE* WINPR_RESTRICT message,
1359 UINT16* WINPR_RESTRICT numTiles)
1361 WINPR_ASSERT(message);
1363 *numTiles = message->numTiles;
1370 cnv.pp = message->tiles;
1374UINT16 rfx_message_get_tile_count(
const RFX_MESSAGE* WINPR_RESTRICT message)
1376 WINPR_ASSERT(message);
1377 return message->numTiles;
1380const RFX_RECT* rfx_message_get_rects(
const RFX_MESSAGE* WINPR_RESTRICT message,
1381 UINT16* WINPR_RESTRICT numRects)
1383 WINPR_ASSERT(message);
1385 *numRects = message->numRects;
1386 return message->rects;
1389UINT16 rfx_message_get_rect_count(
const RFX_MESSAGE* WINPR_RESTRICT message)
1391 WINPR_ASSERT(message);
1392 return message->numRects;
1395void rfx_message_free(RFX_CONTEXT* WINPR_RESTRICT context, RFX_MESSAGE* WINPR_RESTRICT message)
1400 winpr_aligned_free(message->rects);
1404 for (
size_t i = 0; i < message->numTiles; i++)
1406 RFX_TILE* tile = message->tiles[i];
1410 if (tile->YCbCrData)
1412 BufferPool_Return(context->priv->BufferPool, tile->YCbCrData);
1413 tile->YCbCrData = NULL;
1416 ObjectPool_Return(context->priv->TilePool, (
void*)tile);
1419 rfx_allocate_tiles(message, 0, FALSE);
1422 const BOOL freeArray = message->freeArray;
1423 const RFX_MESSAGE empty = { 0 };
1427 winpr_aligned_free(message);
1430static INLINE
void rfx_update_context_properties(RFX_CONTEXT* WINPR_RESTRICT context)
1432 UINT16 properties = 0;
1434 WINPR_ASSERT(context);
1437 properties |= (context->flags << 1);
1438 properties |= (COL_CONV_ICT << 4);
1439 properties |= (CLW_XFORM_DWT_53_A << 6);
1440 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 10);
1441 properties |= (SCALAR_QUANTIZATION << 14);
1442 context->properties = properties;
1446rfx_write_message_sync(WINPR_ATTR_UNUSED
const RFX_CONTEXT* WINPR_RESTRICT context,
1447 WINPR_ATTR_UNUSED
wStream* WINPR_RESTRICT s)
1449 WINPR_ASSERT(context);
1451 Stream_Write_UINT16(s, WBT_SYNC);
1452 Stream_Write_UINT32(s, 12);
1453 Stream_Write_UINT32(s, WF_MAGIC);
1454 Stream_Write_UINT16(s, WF_VERSION_1_0);
1458rfx_write_message_codec_versions(WINPR_ATTR_UNUSED
const RFX_CONTEXT* WINPR_RESTRICT context,
1461 WINPR_ASSERT(context);
1463 Stream_Write_UINT16(s, WBT_CODEC_VERSIONS);
1464 Stream_Write_UINT32(s, 10);
1465 Stream_Write_UINT8(s, 1);
1466 Stream_Write_UINT8(s, 1);
1467 Stream_Write_UINT16(s, WF_VERSION_1_0);
1470static INLINE
void rfx_write_message_channels(
const RFX_CONTEXT* WINPR_RESTRICT context,
1473 WINPR_ASSERT(context);
1475 Stream_Write_UINT16(s, WBT_CHANNELS);
1476 Stream_Write_UINT32(s, 12);
1477 Stream_Write_UINT8(s, 1);
1478 Stream_Write_UINT8(s, 0);
1479 Stream_Write_UINT16(s, context->width);
1480 Stream_Write_UINT16(s, context->height);
1483static INLINE
void rfx_write_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
1486 UINT16 properties = 0;
1487 WINPR_ASSERT(context);
1489 Stream_Write_UINT16(s, WBT_CONTEXT);
1490 Stream_Write_UINT32(s, 13);
1491 Stream_Write_UINT8(s, 1);
1492 Stream_Write_UINT8(s, 0xFF);
1493 Stream_Write_UINT8(s, 0);
1494 Stream_Write_UINT16(s, CT_TILE_64x64);
1496 properties = context->flags;
1497 properties |= (COL_CONV_ICT << 3);
1498 properties |= (CLW_XFORM_DWT_53_A << 5);
1499 properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 9);
1500 properties |= (SCALAR_QUANTIZATION << 13);
1501 Stream_Write_UINT16(s, properties);
1502 rfx_update_context_properties(context);
1505static INLINE BOOL rfx_compose_message_header(RFX_CONTEXT* WINPR_RESTRICT context,
1508 WINPR_ASSERT(context);
1509 if (!Stream_EnsureRemainingCapacity(s, 12 + 10 + 12 + 13))
1512 rfx_write_message_sync(context, s);
1513 rfx_write_message_context(context, s);
1514 rfx_write_message_codec_versions(context, s);
1515 rfx_write_message_channels(context, s);
1519static INLINE
size_t rfx_tile_length(
const RFX_TILE* WINPR_RESTRICT tile)
1522 return 19ull + tile->YLen + tile->CbLen + tile->CrLen;
1525static INLINE BOOL rfx_write_tile(
wStream* WINPR_RESTRICT s,
const RFX_TILE* WINPR_RESTRICT tile)
1527 const size_t blockLen = rfx_tile_length(tile);
1528 if (blockLen > UINT32_MAX)
1530 if (!Stream_EnsureRemainingCapacity(s, blockLen))
1533 Stream_Write_UINT16(s, CBT_TILE);
1534 Stream_Write_UINT32(s, (UINT32)blockLen);
1535 Stream_Write_UINT8(s, tile->quantIdxY);
1536 Stream_Write_UINT8(s, tile->quantIdxCb);
1537 Stream_Write_UINT8(s, tile->quantIdxCr);
1538 Stream_Write_UINT16(s, tile->xIdx);
1539 Stream_Write_UINT16(s, tile->yIdx);
1540 Stream_Write_UINT16(s, tile->YLen);
1541 Stream_Write_UINT16(s, tile->CbLen);
1542 Stream_Write_UINT16(s, tile->CrLen);
1543 Stream_Write(s, tile->YData, tile->YLen);
1544 Stream_Write(s, tile->CbData, tile->CbLen);
1545 Stream_Write(s, tile->CrData, tile->CrLen);
1549struct S_RFX_TILE_COMPOSE_WORK_PARAM
1552 RFX_CONTEXT* context;
1555static INLINE
void CALLBACK
1556rfx_compose_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
1557 void* context, WINPR_ATTR_UNUSED PTP_WORK work)
1559 RFX_TILE_COMPOSE_WORK_PARAM* param = (RFX_TILE_COMPOSE_WORK_PARAM*)context;
1560 WINPR_ASSERT(param);
1561 rfx_encode_rgb(param->context, param->tile);
1564static INLINE BOOL computeRegion(
const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1565 REGION16* WINPR_RESTRICT region,
size_t width,
size_t height)
1567 const RECTANGLE_16 mainRect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, width),
1568 WINPR_ASSERTING_INT_CAST(UINT16, height) };
1570 WINPR_ASSERT(rects);
1571 for (
size_t i = 0; i < numRects; i++)
1575 rect16.left = rect->x;
1576 rect16.top = rect->y;
1577 rect16.right = rect->x + rect->width;
1578 rect16.bottom = rect->y + rect->height;
1580 if (!region16_union_rect(region, region, &rect16))
1584 return region16_intersect_rect(region, region, &mainRect);
1587#define TILE_NO(v) ((v) / 64)
1589static INLINE BOOL setupWorkers(RFX_CONTEXT* WINPR_RESTRICT context,
size_t nbTiles)
1591 WINPR_ASSERT(context);
1593 RFX_CONTEXT_PRIV* priv = context->priv;
1598 if (!context->priv->UseThreads)
1601 if (!(pmem = winpr_aligned_recalloc((
void*)priv->workObjects, nbTiles,
sizeof(PTP_WORK), 32)))
1604 priv->workObjects = (PTP_WORK*)pmem;
1606 if (!(pmem = winpr_aligned_recalloc(priv->tileWorkParams, nbTiles,
1607 sizeof(RFX_TILE_COMPOSE_WORK_PARAM), 32)))
1610 priv->tileWorkParams = (RFX_TILE_COMPOSE_WORK_PARAM*)pmem;
1614static INLINE BOOL rfx_ensure_tiles(RFX_MESSAGE* WINPR_RESTRICT message,
size_t count)
1616 WINPR_ASSERT(message);
1618 if (message->numTiles + count <= message->allocatedTiles)
1621 const size_t alloc = MAX(message->allocatedTiles + 1024, message->numTiles + count);
1622 return rfx_allocate_tiles(message, alloc, TRUE);
1625RFX_MESSAGE* rfx_encode_message(RFX_CONTEXT* WINPR_RESTRICT context,
1626 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1627 const BYTE* WINPR_RESTRICT data, UINT32 w, UINT32 h,
size_t s)
1629 const UINT32 width = w;
1630 const UINT32 height = h;
1631 const UINT32 scanline = (UINT32)s;
1632 RFX_MESSAGE* message = NULL;
1633 PTP_WORK* workObject = NULL;
1634 RFX_TILE_COMPOSE_WORK_PARAM* workParam = NULL;
1635 BOOL success = FALSE;
1642 WINPR_ASSERT(rects);
1643 WINPR_ASSERT(numRects > 0);
1644 WINPR_ASSERT(w > 0);
1645 WINPR_ASSERT(h > 0);
1646 WINPR_ASSERT(s > 0);
1648 if (!(message = (RFX_MESSAGE*)winpr_aligned_calloc(1,
sizeof(RFX_MESSAGE), 32)))
1651 region16_init(&tilesRegion);
1652 region16_init(&rectsRegion);
1654 if (context->state == RFX_STATE_SEND_HEADERS)
1655 rfx_update_context_properties(context);
1657 message->frameIdx = context->frameIdx++;
1659 if (!context->numQuant)
1661 WINPR_ASSERT(context->quants == NULL);
1662 if (!(context->quants =
1663 (UINT32*)winpr_aligned_malloc(
sizeof(rfx_default_quantization_values), 32)))
1664 goto skip_encoding_loop;
1666 CopyMemory(context->quants, &rfx_default_quantization_values,
1667 sizeof(rfx_default_quantization_values));
1668 context->numQuant = 1;
1669 context->quantIdxY = 0;
1670 context->quantIdxCb = 0;
1671 context->quantIdxCr = 0;
1674 message->numQuant = context->numQuant;
1675 message->quantVals = context->quants;
1676 const UINT32 bytesPerPixel = (context->bits_per_pixel / 8);
1678 if (!computeRegion(rects, numRects, &rectsRegion, width, height))
1679 goto skip_encoding_loop;
1681 const RECTANGLE_16* extents = region16_extents(&rectsRegion);
1682 WINPR_ASSERT((INT32)extents->right - extents->left > 0);
1683 WINPR_ASSERT((INT32)extents->bottom - extents->top > 0);
1684 const UINT32 maxTilesX = 1 + TILE_NO(extents->right - 1) - TILE_NO(extents->left);
1685 const UINT32 maxTilesY = 1 + TILE_NO(extents->bottom - 1) - TILE_NO(extents->top);
1686 const UINT32 maxNbTiles = maxTilesX * maxTilesY;
1688 if (!rfx_ensure_tiles(message, maxNbTiles))
1689 goto skip_encoding_loop;
1691 if (!setupWorkers(context, maxNbTiles))
1692 goto skip_encoding_loop;
1694 if (context->priv->UseThreads)
1696 workObject = context->priv->workObjects;
1697 workParam = context->priv->tileWorkParams;
1700 UINT32 regionNbRects = 0;
1701 regionRect = region16_rects(&rectsRegion, ®ionNbRects);
1703 if (!(message->rects = winpr_aligned_calloc(regionNbRects,
sizeof(
RFX_RECT), 32)))
1704 goto skip_encoding_loop;
1706 message->numRects = WINPR_ASSERTING_INT_CAST(UINT16, regionNbRects);
1708 for (UINT32 i = 0; i < regionNbRects; i++, regionRect++)
1710 RFX_RECT* rfxRect = &message->rects[i];
1711 UINT32 startTileX = regionRect->left / 64;
1712 UINT32 endTileX = (regionRect->right - 1) / 64;
1713 UINT32 startTileY = regionRect->top / 64;
1714 UINT32 endTileY = (regionRect->bottom - 1) / 64;
1715 rfxRect->x = regionRect->left;
1716 rfxRect->y = regionRect->top;
1717 rfxRect->width = (regionRect->right - regionRect->left);
1718 rfxRect->height = (regionRect->bottom - regionRect->top);
1720 for (UINT32 yIdx = startTileY, gridRelY = startTileY * 64; yIdx <= endTileY;
1721 yIdx++, gridRelY += 64)
1723 UINT32 tileHeight = 64;
1725 if ((yIdx == endTileY) && (gridRelY + 64 > height))
1726 tileHeight = height - gridRelY;
1728 currentTileRect.top = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1729 currentTileRect.bottom = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY + tileHeight);
1731 for (UINT32 xIdx = startTileX, gridRelX = startTileX * 64; xIdx <= endTileX;
1732 xIdx++, gridRelX += 64)
1739 UINT32 tileWidth = 64;
1741 if ((xIdx == endTileX) && (gridRelX + 64 > width))
1743 tileWidth = (width - gridRelX);
1746 currentTileRect.left = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1747 currentTileRect.right = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX + tileWidth);
1750 if (region16_intersects_rect(&tilesRegion, ¤tTileRect))
1755 goto skip_encoding_loop;
1757 tile->xIdx = WINPR_ASSERTING_INT_CAST(UINT16, xIdx);
1758 tile->yIdx = WINPR_ASSERTING_INT_CAST(UINT16, yIdx);
1759 tile->x = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1760 tile->y = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1761 tile->scanline = scanline;
1763 tile->width = tileWidth;
1764 tile->height = tileHeight;
1765 const UINT32 ax = gridRelX;
1766 const UINT32 ay = gridRelY;
1768 if (tile->data && tile->allocated)
1770 winpr_aligned_free(tile->data);
1771 tile->allocated = FALSE;
1775 cnv.cpv = &data[(ay * scanline) + (ax * bytesPerPixel)];
1776 tile->data = cnv.pv;
1777 tile->quantIdxY = context->quantIdxY;
1778 tile->quantIdxCb = context->quantIdxCb;
1779 tile->quantIdxCr = context->quantIdxCr;
1780 tile->YLen = tile->CbLen = tile->CrLen = 0;
1782 if (!(tile->YCbCrData = (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
1783 goto skip_encoding_loop;
1785 tile->YData = &(tile->YCbCrData[((8192 + 32) * 0) + 16]);
1786 tile->CbData = &(tile->YCbCrData[((8192 + 32) * 1) + 16]);
1787 tile->CrData = &(tile->YCbCrData[((8192 + 32) * 2) + 16]);
1789 if (!rfx_ensure_tiles(message, 1))
1790 goto skip_encoding_loop;
1791 message->tiles[message->numTiles++] = tile;
1793 if (context->priv->UseThreads)
1795 workParam->context = context;
1796 workParam->tile = tile;
1798 if (!(*workObject = CreateThreadpoolWork(rfx_compose_message_tile_work_callback,
1799 (
void*)workParam, NULL)))
1801 goto skip_encoding_loop;
1804 SubmitThreadpoolWork(*workObject);
1810 rfx_encode_rgb(context, tile);
1813 if (!region16_union_rect(&tilesRegion, &tilesRegion, ¤tTileRect))
1814 goto skip_encoding_loop;
1825 message->tilesDataSize = 0;
1826 workObject = context->priv->workObjects;
1828 for (UINT32 i = 0; i < message->numTiles; i++)
1830 if (context->priv->UseThreads)
1834 WaitForThreadpoolWorkCallbacks(*workObject, FALSE);
1835 CloseThreadpoolWork(*workObject);
1841 const RFX_TILE* tile = message->tiles[i];
1842 const size_t tlen = rfx_tile_length(tile);
1843 message->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tlen);
1846 region16_uninit(&tilesRegion);
1847 region16_uninit(&rectsRegion);
1852 WLog_Print(context->priv->log, WLOG_ERROR,
"failed");
1854 rfx_message_free(context, message);
1855 region16_uninit(&tilesRegion);
1856 region16_uninit(&rectsRegion);
1860static INLINE BOOL rfx_clone_rects(RFX_MESSAGE* WINPR_RESTRICT dst,
1861 const RFX_MESSAGE* WINPR_RESTRICT src)
1866 WINPR_ASSERT(dst->rects == NULL);
1867 WINPR_ASSERT(dst->numRects == 0);
1869 if (src->numRects == 0)
1872 dst->rects = winpr_aligned_calloc(src->numRects,
sizeof(
RECTANGLE_16), 32);
1875 dst->numRects = src->numRects;
1876 for (
size_t x = 0; x < src->numRects; x++)
1878 dst->rects[x] = src->rects[x];
1883static INLINE BOOL rfx_clone_quants(RFX_MESSAGE* WINPR_RESTRICT dst,
1884 const RFX_MESSAGE* WINPR_RESTRICT src)
1889 WINPR_ASSERT(dst->quantVals == NULL);
1890 WINPR_ASSERT(dst->numQuant == 0);
1892 if (src->numQuant == 0)
1896 dst->quantVals = src->quantVals;
1897 dst->numQuant = src->numQuant;
1902static INLINE RFX_MESSAGE* rfx_split_message(RFX_CONTEXT* WINPR_RESTRICT context,
1903 RFX_MESSAGE* WINPR_RESTRICT message,
1904 size_t* WINPR_RESTRICT numMessages,
size_t maxDataSize)
1906 WINPR_ASSERT(context);
1907 WINPR_ASSERT(message);
1908 WINPR_ASSERT(numMessages);
1910 maxDataSize -= 1024;
1911 *numMessages = ((message->tilesDataSize + maxDataSize) / maxDataSize) * 4ull;
1913 RFX_MESSAGE* messages =
1914 (RFX_MESSAGE*)winpr_aligned_calloc((*numMessages),
sizeof(RFX_MESSAGE), 32);
1919 for (UINT16 i = 0; i < message->numTiles; i++)
1921 RFX_TILE* tile = message->tiles[i];
1922 RFX_MESSAGE* msg = &messages[j];
1927 const size_t tileDataSize = rfx_tile_length(tile);
1929 if ((msg->tilesDataSize + tileDataSize) > ((UINT32)maxDataSize))
1932 if (msg->numTiles == 0)
1934 msg->frameIdx = message->frameIdx + j;
1935 if (!rfx_clone_quants(msg, message))
1937 if (!rfx_clone_rects(msg, message))
1939 msg->freeArray = TRUE;
1940 if (!rfx_allocate_tiles(msg, message->numTiles, TRUE))
1944 msg->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tileDataSize);
1946 WINPR_ASSERT(msg->numTiles < msg->allocatedTiles);
1947 msg->tiles[msg->numTiles++] = message->tiles[i];
1948 message->tiles[i] = NULL;
1951 *numMessages = j + 1ULL;
1952 context->frameIdx += j;
1953 message->numTiles = 0;
1957 for (
size_t i = 0; i < j; i++)
1958 rfx_allocate_tiles(&messages[i], 0, FALSE);
1960 winpr_aligned_free(messages);
1964const RFX_MESSAGE* rfx_message_list_get(
const RFX_MESSAGE_LIST* WINPR_RESTRICT messages,
size_t idx)
1966 WINPR_ASSERT(messages);
1967 if (idx >= messages->count)
1969 WINPR_ASSERT(messages->list);
1970 return &messages->list[idx];
1973void rfx_message_list_free(RFX_MESSAGE_LIST* messages)
1977 for (
size_t x = 0; x < messages->count; x++)
1978 rfx_message_free(messages->context, &messages->list[x]);
1982static INLINE RFX_MESSAGE_LIST* rfx_message_list_new(RFX_CONTEXT* WINPR_RESTRICT context,
1983 RFX_MESSAGE* WINPR_RESTRICT messages,
1986 WINPR_ASSERT(context);
1987 RFX_MESSAGE_LIST* msg = calloc(1,
sizeof(RFX_MESSAGE_LIST));
1990 msg->context = context;
1992 msg->list = messages;
1996RFX_MESSAGE_LIST* rfx_encode_messages(RFX_CONTEXT* WINPR_RESTRICT context,
1997 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
1998 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
1999 UINT32 scanline,
size_t* WINPR_RESTRICT numMessages,
2002 WINPR_ASSERT(context);
2003 WINPR_ASSERT(numMessages);
2005 RFX_MESSAGE* message =
2006 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2010 RFX_MESSAGE* list = rfx_split_message(context, message, numMessages, maxDataSize);
2011 rfx_message_free(context, message);
2015 return rfx_message_list_new(context, list, *numMessages);
2018static INLINE BOOL rfx_write_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
2020 const RFX_MESSAGE* WINPR_RESTRICT message)
2022 WINPR_ASSERT(context);
2023 WINPR_ASSERT(message);
2025 const UINT32 blockLen = 22 + (message->numQuant * 5) + message->tilesDataSize;
2027 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2030 Stream_Write_UINT16(s, WBT_EXTENSION);
2031 Stream_Write_UINT32(s, blockLen);
2032 Stream_Write_UINT8(s, 1);
2033 Stream_Write_UINT8(s, 0);
2034 Stream_Write_UINT16(s, CBT_TILESET);
2035 Stream_Write_UINT16(s, 0);
2036 Stream_Write_UINT16(s, context->properties);
2038 s, WINPR_ASSERTING_INT_CAST(uint8_t, message->numQuant));
2039 Stream_Write_UINT8(s, 0x40);
2040 Stream_Write_UINT16(s, message->numTiles);
2041 Stream_Write_UINT32(s, message->tilesDataSize);
2043 UINT32* quantVals = message->quantVals;
2044 for (
size_t i = 0; i < message->numQuant * 5ul; i++)
2046 WINPR_ASSERT(quantVals);
2047 Stream_Write_UINT8(s,
2048 WINPR_ASSERTING_INT_CAST(uint8_t, quantVals[0] + (quantVals[1] << 4)));
2052 for (
size_t i = 0; i < message->numTiles; i++)
2054 RFX_TILE* tile = message->tiles[i];
2058 if (!rfx_write_tile(s, tile))
2062#ifdef WITH_DEBUG_RFX
2063 WLog_Print(context->priv->log, WLOG_DEBUG,
2064 "numQuant: %" PRIu16
" numTiles: %" PRIu16
" tilesDataSize: %" PRIu32
"",
2065 message->numQuant, message->numTiles, message->tilesDataSize);
2071rfx_write_message_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2072 wStream* WINPR_RESTRICT s,
const RFX_MESSAGE* WINPR_RESTRICT message)
2074 WINPR_ASSERT(context);
2075 WINPR_ASSERT(message);
2077 if (!Stream_EnsureRemainingCapacity(s, 14))
2080 Stream_Write_UINT16(s, WBT_FRAME_BEGIN);
2081 Stream_Write_UINT32(s, 14);
2082 Stream_Write_UINT8(s, 1);
2083 Stream_Write_UINT8(s, 0);
2084 Stream_Write_UINT32(s, message->frameIdx);
2085 Stream_Write_UINT16(s, 1);
2089static INLINE BOOL rfx_write_message_region(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2091 const RFX_MESSAGE* WINPR_RESTRICT message)
2093 WINPR_ASSERT(context);
2094 WINPR_ASSERT(message);
2096 const size_t blockLen = 15 + (message->numRects * 8);
2097 if (blockLen > UINT32_MAX)
2100 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2103 Stream_Write_UINT16(s, WBT_REGION);
2104 Stream_Write_UINT32(s, (UINT32)blockLen);
2105 Stream_Write_UINT8(s, 1);
2106 Stream_Write_UINT8(s, 0);
2107 Stream_Write_UINT8(s, 1);
2108 Stream_Write_UINT16(s, message->numRects);
2110 for (UINT16 i = 0; i < message->numRects; i++)
2112 const RFX_RECT* rect = rfx_message_get_rect_const(message, i);
2116 Stream_Write_UINT16(s, rect->x);
2117 Stream_Write_UINT16(s, rect->y);
2118 Stream_Write_UINT16(s, rect->width);
2119 Stream_Write_UINT16(s, rect->height);
2122 Stream_Write_UINT16(s, CBT_REGION);
2123 Stream_Write_UINT16(s, 1);
2127static INLINE BOOL rfx_write_message_frame_end(
2128 WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2129 WINPR_ATTR_UNUSED
const RFX_MESSAGE* WINPR_RESTRICT message)
2131 WINPR_ASSERT(context);
2132 WINPR_ASSERT(message);
2134 if (!Stream_EnsureRemainingCapacity(s, 8))
2137 Stream_Write_UINT16(s, WBT_FRAME_END);
2138 Stream_Write_UINT32(s, 8);
2139 Stream_Write_UINT8(s, 1);
2140 Stream_Write_UINT8(s, 0);
2144BOOL rfx_write_message(RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2145 const RFX_MESSAGE* WINPR_RESTRICT message)
2147 WINPR_ASSERT(context);
2148 WINPR_ASSERT(message);
2150 if (context->state == RFX_STATE_SEND_HEADERS)
2152 if (!rfx_compose_message_header(context, s))
2155 context->state = RFX_STATE_SEND_FRAME_DATA;
2158 if (!rfx_write_message_frame_begin(context, s, message) ||
2159 !rfx_write_message_region(context, s, message) ||
2160 !rfx_write_message_tileset(context, s, message) ||
2161 !rfx_write_message_frame_end(context, s, message))
2169BOOL rfx_compose_message(RFX_CONTEXT* WINPR_RESTRICT context,
wStream* WINPR_RESTRICT s,
2170 const RFX_RECT* WINPR_RESTRICT rects,
size_t numRects,
2171 const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2174 WINPR_ASSERT(context);
2175 RFX_MESSAGE* message =
2176 rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2180 const BOOL ret = rfx_write_message(context, s, message);
2181 rfx_message_free(context, message);
2185BOOL rfx_context_set_mode(RFX_CONTEXT* WINPR_RESTRICT context, RLGR_MODE mode)
2187 WINPR_ASSERT(context);
2188 context->mode = mode;
2192RLGR_MODE rfx_context_get_mode(RFX_CONTEXT* WINPR_RESTRICT context)
2194 WINPR_ASSERT(context);
2195 return context->mode;
2198UINT32 rfx_context_get_frame_idx(
const RFX_CONTEXT* WINPR_RESTRICT context)
2200 WINPR_ASSERT(context);
2201 return context->frameIdx;
2204UINT32 rfx_message_get_frame_idx(
const RFX_MESSAGE* WINPR_RESTRICT message)
2206 WINPR_ASSERT(message);
2207 return message->frameIdx;
2210static INLINE BOOL rfx_write_progressive_wb_sync(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2213 const UINT32 blockLen = 12;
2217 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2220 Stream_Write_UINT16(s, PROGRESSIVE_WBT_SYNC);
2221 Stream_Write_UINT32(s, blockLen);
2222 Stream_Write_UINT32(s, 0xCACCACCA);
2223 Stream_Write_UINT16(s, 0x0100);
2227static INLINE BOOL rfx_write_progressive_wb_context(
2228 WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
wStream* WINPR_RESTRICT s)
2230 const UINT32 blockLen = 10;
2234 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2237 Stream_Write_UINT16(s, PROGRESSIVE_WBT_CONTEXT);
2238 Stream_Write_UINT32(s, blockLen);
2239 Stream_Write_UINT8(s, 0);
2240 Stream_Write_UINT16(s, 64);
2241 Stream_Write_UINT8(s, 0);
2245static INLINE BOOL rfx_write_progressive_region(RFX_CONTEXT* WINPR_RESTRICT rfx,
2247 const RFX_MESSAGE* WINPR_RESTRICT msg)
2250 UINT32 blockLen = 18;
2251 UINT32 tilesDataSize = 0;
2252 const size_t start = Stream_GetPosition(s);
2258 blockLen += msg->numRects * 8;
2259 blockLen += msg->numQuant * 5;
2260 tilesDataSize = msg->numTiles * 22UL;
2261 for (UINT16 i = 0; i < msg->numTiles; i++)
2263 const RFX_TILE* tile = msg->tiles[i];
2265 tilesDataSize += tile->YLen + tile->CbLen + tile->CrLen;
2267 blockLen += tilesDataSize;
2269 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2272 Stream_Write_UINT16(s, PROGRESSIVE_WBT_REGION);
2273 Stream_Write_UINT32(s, blockLen);
2274 Stream_Write_UINT8(s, 64);
2275 Stream_Write_UINT16(s, msg->numRects);
2276 WINPR_ASSERT(msg->numQuant <= UINT8_MAX);
2277 Stream_Write_UINT8(s, (UINT8)msg->numQuant);
2278 Stream_Write_UINT8(s, 0);
2279 Stream_Write_UINT8(s, 0);
2280 Stream_Write_UINT16(s, msg->numTiles);
2281 Stream_Write_UINT32(s, tilesDataSize);
2283 for (UINT16 i = 0; i < msg->numRects; i++)
2286 const RFX_RECT* r = &msg->rects[i];
2287 Stream_Write_UINT16(s, r->x);
2288 Stream_Write_UINT16(s, r->y);
2289 Stream_Write_UINT16(s, r->width);
2290 Stream_Write_UINT16(s, r->height);
2301 for (UINT16 i = 0; i < msg->numQuant; i++)
2303 const UINT32* qv = &msg->quantVals[10ULL * i];
2305 Stream_Write_UINT8(s, (UINT8)(qv[0] + (qv[2] << 4)));
2306 Stream_Write_UINT8(s, (UINT8)(qv[1] + (qv[3] << 4)));
2307 Stream_Write_UINT8(s, (UINT8)(qv[5] + (qv[4] << 4)));
2308 Stream_Write_UINT8(s, (UINT8)(qv[6] + (qv[8] << 4)));
2309 Stream_Write_UINT8(s, (UINT8)(qv[7] + (qv[9] << 4)));
2312 for (UINT16 i = 0; i < msg->numTiles; i++)
2314 const RFX_TILE* tile = msg->tiles[i];
2315 if (!rfx_write_progressive_tile_simple(rfx, s, tile))
2319 const size_t end = Stream_GetPosition(s);
2320 const size_t used = end - start;
2321 return (used == blockLen);
2325rfx_write_progressive_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2326 wStream* WINPR_RESTRICT s,
const RFX_MESSAGE* WINPR_RESTRICT msg)
2328 const UINT32 blockLen = 12;
2333 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2336 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_BEGIN);
2337 Stream_Write_UINT32(s, blockLen);
2338 Stream_Write_UINT32(s, msg->frameIdx);
2339 Stream_Write_UINT16(s, 1);
2344static INLINE BOOL rfx_write_progressive_frame_end(
2345 WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
wStream* WINPR_RESTRICT s)
2347 const UINT32 blockLen = 6;
2351 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2354 Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_END);
2355 Stream_Write_UINT32(s, blockLen);
2361rfx_write_progressive_tile_simple(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2364 UINT32 blockLen = 0;
2369 blockLen = 22 + tile->YLen + tile->CbLen + tile->CrLen;
2370 if (!Stream_EnsureRemainingCapacity(s, blockLen))
2373 Stream_Write_UINT16(s, PROGRESSIVE_WBT_TILE_SIMPLE);
2374 Stream_Write_UINT32(s, blockLen);
2375 Stream_Write_UINT8(s, tile->quantIdxY);
2376 Stream_Write_UINT8(s, tile->quantIdxCb);
2377 Stream_Write_UINT8(s, tile->quantIdxCr);
2378 Stream_Write_UINT16(s, tile->xIdx);
2379 Stream_Write_UINT16(s, tile->yIdx);
2380 Stream_Write_UINT8(s, 0);
2381 Stream_Write_UINT16(s, tile->YLen);
2382 Stream_Write_UINT16(s, tile->CbLen);
2383 Stream_Write_UINT16(s, tile->CrLen);
2384 Stream_Write_UINT16(s, 0);
2385 Stream_Write(s, tile->YData, tile->YLen);
2386 Stream_Write(s, tile->CbData, tile->CbLen);
2387 Stream_Write(s, tile->CrData, tile->CrLen);
2392const char* rfx_get_progressive_block_type_string(UINT16 blockType)
2396 case PROGRESSIVE_WBT_SYNC:
2397 return "PROGRESSIVE_WBT_SYNC";
2399 case PROGRESSIVE_WBT_FRAME_BEGIN:
2400 return "PROGRESSIVE_WBT_FRAME_BEGIN";
2402 case PROGRESSIVE_WBT_FRAME_END:
2403 return "PROGRESSIVE_WBT_FRAME_END";
2405 case PROGRESSIVE_WBT_CONTEXT:
2406 return "PROGRESSIVE_WBT_CONTEXT";
2408 case PROGRESSIVE_WBT_REGION:
2409 return "PROGRESSIVE_WBT_REGION";
2411 case PROGRESSIVE_WBT_TILE_SIMPLE:
2412 return "PROGRESSIVE_WBT_TILE_SIMPLE";
2414 case PROGRESSIVE_WBT_TILE_FIRST:
2415 return "PROGRESSIVE_WBT_TILE_FIRST";
2417 case PROGRESSIVE_WBT_TILE_UPGRADE:
2418 return "PROGRESSIVE_WBT_TILE_UPGRADE";
2421 return "PROGRESSIVE_WBT_UNKNOWN";
2425BOOL rfx_write_message_progressive_simple(RFX_CONTEXT* WINPR_RESTRICT context,
2427 const RFX_MESSAGE* WINPR_RESTRICT msg)
2431 WINPR_ASSERT(context);
2433 if (context->mode != RLGR1)
2435 WLog_ERR(TAG,
"error, RLGR1 mode is required!");
2439 if (!rfx_write_progressive_wb_sync(context, s))
2442 if (!rfx_write_progressive_wb_context(context, s))
2445 if (!rfx_write_progressive_frame_begin(context, s, msg))
2448 if (!rfx_write_progressive_region(context, s, msg))
2451 if (!rfx_write_progressive_frame_end(context, s))
This struct contains function pointer to initialize/free objects.