Coverage Report

Created: 2026-08-31 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/codec/rfx.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * RemoteFX Codec Library
4
 *
5
 * Copyright 2011 Vic Lee
6
 * Copyright 2015 Thincast Technologies GmbH
7
 * Copyright 2015 Norbert Federa <norbert.federa@thincast.com>
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
22
#include <freerdp/config.h>
23
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
28
#include <winpr/assert.h>
29
#include <winpr/cast.h>
30
#include <winpr/crt.h>
31
#include <winpr/tchar.h>
32
#include <winpr/sysinfo.h>
33
#include <winpr/registry.h>
34
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>
42
43
#include "rfx_constants.h"
44
#include "rfx_types.h"
45
#include "rfx_decode.h"
46
#include "rfx_encode.h"
47
#include "rfx_quantization.h"
48
#include "rfx_dwt.h"
49
#include "rfx_rlgr.h"
50
#include "../core/utils.h"
51
52
#include "sse/rfx_sse2.h"
53
#include "neon/rfx_neon.h"
54
55
#define TAG FREERDP_TAG("codec")
56
57
22.7k
#define RFX_KEY "Software\\%s\\RemoteFX"
58
59
/**
60
 * The quantization values control the compression rate and quality. The value
61
 * range is between 6 and 15. The higher value, the higher compression rate
62
 * and lower quality.
63
 *
64
 * This is the default values being use by the MS RDP server, and we will also
65
 * use it as our default values for the encoder. It can be overridden by setting
66
 * the context->num_quants and context->quants member.
67
 *
68
 * The order of the values are:
69
 * LL3, LH3, HL3, HH3, LH2, HL2, HH2, LH1, HL1, HH1
70
 */
71
static const UINT32 rfx_default_quantization_values[] = { 6, 6, 6, 6, 7, 7, 8, 8, 8, 9 };
72
73
static inline BOOL rfx_write_progressive_tile_simple(RFX_CONTEXT* WINPR_RESTRICT rfx,
74
                                                     wStream* WINPR_RESTRICT s,
75
                                                     const RFX_TILE* WINPR_RESTRICT tile);
76
static BOOL rfx_message_clear(RFX_CONTEXT* WINPR_RESTRICT context,
77
                              RFX_MESSAGE* WINPR_RESTRICT message);
78
79
static inline void rfx_profiler_create(RFX_CONTEXT* WINPR_RESTRICT context)
80
11.3k
{
81
11.3k
  if (!context || !context->priv)
82
0
    return;
83
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_decode_rgb, "rfx_decode_rgb")
84
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_decode_component, "rfx_decode_component")
85
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_rlgr_decode, "rfx_rlgr_decode")
86
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_differential_decode, "rfx_differential_decode")
87
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_quantization_decode, "rfx_quantization_decode")
88
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_decode, "rfx_dwt_2d_decode")
89
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_ycbcr_to_rgb, "prims->yCbCrToRGB")
90
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_encode_rgb, "rfx_encode_rgb")
91
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_encode_component, "rfx_encode_component")
92
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_rlgr_encode, "rfx_rlgr_encode")
93
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_differential_encode, "rfx_differential_encode")
94
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_quantization_encode, "rfx_quantization_encode")
95
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_encode, "rfx_dwt_2d_encode")
96
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_rgb_to_ycbcr, "prims->RGBToYCbCr")
97
11.3k
  PROFILER_CREATE(context->priv->prof_rfx_encode_format_rgb, "rfx_encode_format_rgb")
98
11.3k
}
99
100
static inline void rfx_profiler_free(RFX_CONTEXT* WINPR_RESTRICT context)
101
11.3k
{
102
11.3k
  if (!context || !context->priv)
103
0
    return;
104
11.3k
  PROFILER_FREE(context->priv->prof_rfx_decode_rgb)
105
11.3k
  PROFILER_FREE(context->priv->prof_rfx_decode_component)
106
11.3k
  PROFILER_FREE(context->priv->prof_rfx_rlgr_decode)
107
11.3k
  PROFILER_FREE(context->priv->prof_rfx_differential_decode)
108
11.3k
  PROFILER_FREE(context->priv->prof_rfx_quantization_decode)
109
11.3k
  PROFILER_FREE(context->priv->prof_rfx_dwt_2d_decode)
110
11.3k
  PROFILER_FREE(context->priv->prof_rfx_ycbcr_to_rgb)
111
11.3k
  PROFILER_FREE(context->priv->prof_rfx_encode_rgb)
112
11.3k
  PROFILER_FREE(context->priv->prof_rfx_encode_component)
113
11.3k
  PROFILER_FREE(context->priv->prof_rfx_rlgr_encode)
114
11.3k
  PROFILER_FREE(context->priv->prof_rfx_differential_encode)
115
11.3k
  PROFILER_FREE(context->priv->prof_rfx_quantization_encode)
116
11.3k
  PROFILER_FREE(context->priv->prof_rfx_dwt_2d_encode)
117
11.3k
  PROFILER_FREE(context->priv->prof_rfx_rgb_to_ycbcr)
118
11.3k
  PROFILER_FREE(context->priv->prof_rfx_encode_format_rgb)
119
11.3k
}
120
121
static inline void rfx_profiler_print(RFX_CONTEXT* WINPR_RESTRICT context)
122
11.3k
{
123
11.3k
  if (!context || !context->priv)
124
0
    return;
125
126
11.3k
  PROFILER_PRINT_HEADER
127
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_decode_rgb)
128
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_decode_component)
129
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_rlgr_decode)
130
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_differential_decode)
131
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_quantization_decode)
132
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_decode)
133
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_ycbcr_to_rgb)
134
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_encode_rgb)
135
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_encode_component)
136
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_rlgr_encode)
137
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_differential_encode)
138
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_quantization_encode)
139
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_encode)
140
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_rgb_to_ycbcr)
141
11.3k
  PROFILER_PRINT(context->priv->prof_rfx_encode_format_rgb)
142
11.3k
  PROFILER_PRINT_FOOTER
143
11.3k
}
144
145
static inline void rfx_tile_init(void* obj)
146
0
{
147
0
  RFX_TILE* tile = (RFX_TILE*)obj;
148
0
  if (tile)
149
0
  {
150
0
    tile->x = 0;
151
0
    tile->y = 0;
152
0
    tile->YLen = 0;
153
0
    tile->YData = nullptr;
154
0
    tile->CbLen = 0;
155
0
    tile->CbData = nullptr;
156
0
    tile->CrLen = 0;
157
0
    tile->CrData = nullptr;
158
0
  }
159
0
}
160
161
static inline void* rfx_decoder_tile_new(const void* val)
162
0
{
163
0
  const size_t size = 4ULL * 64ULL * 64ULL;
164
0
  RFX_TILE* tile = nullptr;
165
0
  WINPR_UNUSED(val);
166
167
0
  if (!(tile = (RFX_TILE*)winpr_aligned_calloc(1, sizeof(RFX_TILE), 32)))
168
0
    return nullptr;
169
170
0
  if (!(tile->data = (BYTE*)winpr_aligned_malloc(size, 16)))
171
0
  {
172
0
    winpr_aligned_free(tile);
173
0
    return nullptr;
174
0
  }
175
0
  memset(tile->data, 0xff, size);
176
0
  tile->allocated = TRUE;
177
0
  return tile;
178
0
}
179
180
static inline void rfx_decoder_tile_free(void* obj)
181
0
{
182
0
  RFX_TILE* tile = (RFX_TILE*)obj;
183
184
0
  if (tile)
185
0
  {
186
0
    if (tile->allocated)
187
0
      winpr_aligned_free(tile->data);
188
189
0
    winpr_aligned_free(tile);
190
0
  }
191
0
}
192
193
static inline void* rfx_encoder_tile_new(const void* val)
194
0
{
195
0
  WINPR_UNUSED(val);
196
0
  return winpr_aligned_calloc(1, sizeof(RFX_TILE), 32);
197
0
}
198
199
static inline void rfx_encoder_tile_free(void* obj)
200
0
{
201
0
  winpr_aligned_free(obj);
202
0
}
203
204
RFX_CONTEXT* rfx_context_new(BOOL encoder)
205
5.67k
{
206
5.67k
  return rfx_context_new_ex(encoder, 0);
207
5.67k
}
208
209
RFX_CONTEXT* rfx_context_new_ex(BOOL encoder, UINT32 ThreadingFlags)
210
11.3k
{
211
11.3k
  RFX_CONTEXT_PRIV* priv = nullptr;
212
11.3k
  RFX_CONTEXT* context = (RFX_CONTEXT*)winpr_aligned_calloc(1, sizeof(RFX_CONTEXT), 32);
213
214
11.3k
  if (!context)
215
0
    return nullptr;
216
217
11.3k
  context->encoder = encoder;
218
11.3k
  context->currentMessage.freeArray = TRUE;
219
11.3k
  context->priv = priv = (RFX_CONTEXT_PRIV*)winpr_aligned_calloc(1, sizeof(RFX_CONTEXT_PRIV), 32);
220
221
11.3k
  if (!priv)
222
0
    goto fail;
223
224
11.3k
  priv->log = WLog_Get("com.freerdp.codec.rfx");
225
11.3k
  priv->TilePool = ObjectPool_New(TRUE);
226
227
11.3k
  if (!priv->TilePool)
228
0
    goto fail;
229
230
11.3k
  {
231
11.3k
    wObject* pool = ObjectPool_Object(priv->TilePool);
232
11.3k
    pool->fnObjectInit = rfx_tile_init;
233
234
11.3k
    if (context->encoder)
235
0
    {
236
0
      pool->fnObjectNew = rfx_encoder_tile_new;
237
0
      pool->fnObjectFree = rfx_encoder_tile_free;
238
0
    }
239
11.3k
    else
240
11.3k
    {
241
11.3k
      pool->fnObjectNew = rfx_decoder_tile_new;
242
11.3k
      pool->fnObjectFree = rfx_decoder_tile_free;
243
11.3k
    }
244
11.3k
  }
245
246
  /*
247
   * align buffers to 16 byte boundary (needed for SSE/NEON instructions)
248
   *
249
   * y_r_buffer, cb_g_buffer, cr_b_buffer: 64 * 64 * sizeof(INT16) = 8192 (0x2000)
250
   * dwt_buffer: 32 * 32 * 2 * 2 * sizeof(INT16) = 8192, maximum sub-band width is 32
251
   *
252
   * Additionally we add 32 bytes (16 in front and 16 at the back of the buffer)
253
   * in order to allow optimized functions (SEE, NEON) to read from positions
254
   * that are actually in front/beyond the buffer. Offset calculations are
255
   * performed at the BufferPool_Take function calls in rfx_encode/decode.c.
256
   *
257
   * We then multiply by 3 to use a single, partionned buffer for all 3 channels.
258
   */
259
11.3k
  priv->BufferPool = BufferPool_New(TRUE, (8192ULL + 32ULL) * 3ULL, 16);
260
261
11.3k
  if (!priv->BufferPool)
262
0
    goto fail;
263
264
11.3k
  priv->UseThreads = FALSE;
265
11.3k
  if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS))
266
11.3k
    priv->UseThreads = TRUE;
267
268
11.3k
  {
269
11.3k
    char* key = freerdp_getApplicatonDetailsRegKey(RFX_KEY);
270
11.3k
    if (key)
271
11.3k
    {
272
11.3k
      HKEY hKey = nullptr;
273
11.3k
      const LONG status =
274
11.3k
          RegOpenKeyExA(HKEY_LOCAL_MACHINE, RFX_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
275
11.3k
      free(key);
276
277
11.3k
      if (status == ERROR_SUCCESS)
278
0
      {
279
0
        DWORD dwType = 0;
280
0
        DWORD dwValue = 0;
281
0
        DWORD dwSize = sizeof(dwValue);
282
283
0
        if (RegQueryValueEx(hKey, _T("UseThreads"), nullptr, &dwType, (BYTE*)&dwValue,
284
0
                            &dwSize) == ERROR_SUCCESS)
285
0
          priv->UseThreads = dwValue ? 1 : 0;
286
287
0
        RegCloseKey(hKey);
288
0
      }
289
11.3k
    }
290
11.3k
  }
291
292
11.3k
  if (priv->UseThreads)
293
11.3k
  {
294
    /* Call primitives_get here in order to avoid race conditions when using primitives_get */
295
    /* from multiple threads. This call will initialize all function pointers correctly     */
296
    /* before any decoding threads are started */
297
11.3k
    if (!primitives_get())
298
0
      goto fail;
299
11.3k
  }
300
301
  /* initialize the default pixel format */
302
11.3k
  rfx_context_set_pixel_format(context, PIXEL_FORMAT_BGRX32);
303
  /* create profilers for default decoding routines */
304
11.3k
  rfx_profiler_create(context);
305
  /* set up default routines */
306
11.3k
  context->quantization_decode = rfx_quantization_decode;
307
11.3k
  context->quantization_encode = rfx_quantization_encode;
308
11.3k
  context->dwt_2d_decode = rfx_dwt_2d_decode;
309
11.3k
  context->dwt_2d_extrapolate_decode = rfx_dwt_2d_extrapolate_decode;
310
11.3k
  context->dwt_2d_encode = rfx_dwt_2d_encode;
311
11.3k
  context->rlgr_decode = rfx_rlgr_decode;
312
11.3k
  context->rlgr_encode = rfx_rlgr_encode;
313
11.3k
  rfx_init_sse2(context);
314
11.3k
  rfx_init_neon(context);
315
11.3k
  context->state = RFX_STATE_SEND_HEADERS;
316
11.3k
  context->expectedDataBlockType = WBT_FRAME_BEGIN;
317
11.3k
  return context;
318
0
fail:
319
0
  WINPR_PRAGMA_DIAG_PUSH
320
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
321
0
  rfx_context_free(context);
322
0
  WINPR_PRAGMA_DIAG_POP
323
0
  return nullptr;
324
11.3k
}
325
326
void rfx_context_free(RFX_CONTEXT* context)
327
11.3k
{
328
11.3k
  RFX_CONTEXT_PRIV* priv = nullptr;
329
330
11.3k
  if (!context)
331
0
    return;
332
333
11.3k
  WINPR_ASSERT(nullptr != context);
334
335
11.3k
  priv = context->priv;
336
11.3k
  WINPR_ASSERT(nullptr != priv);
337
11.3k
  WINPR_ASSERT(nullptr != priv->TilePool);
338
11.3k
  WINPR_ASSERT(nullptr != priv->BufferPool);
339
340
  /* coverity[address_free] */
341
11.3k
  rfx_message_clear(context, &context->currentMessage);
342
11.3k
  winpr_aligned_free(context->quants);
343
11.3k
  rfx_profiler_print(context);
344
11.3k
  rfx_profiler_free(context);
345
346
11.3k
  if (priv)
347
11.3k
  {
348
11.3k
    ObjectPool_Free(priv->TilePool);
349
11.3k
    if (priv->UseThreads)
350
11.3k
    {
351
11.3k
      winpr_aligned_free((void*)priv->workObjects);
352
11.3k
      winpr_aligned_free(priv->tileWorkParams);
353
#ifdef WITH_PROFILER
354
      WLog_VRB(
355
          TAG,
356
          "WARNING: Profiling results probably unusable with multithreaded RemoteFX codec!");
357
#endif
358
11.3k
    }
359
360
11.3k
    BufferPool_Free(priv->BufferPool);
361
11.3k
    winpr_aligned_free(priv);
362
11.3k
  }
363
11.3k
  winpr_aligned_free(context);
364
11.3k
}
365
366
static inline RFX_TILE* rfx_message_get_tile(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index)
367
0
{
368
0
  WINPR_ASSERT(message);
369
0
  WINPR_ASSERT(message->tiles);
370
0
  WINPR_ASSERT(index < message->numTiles);
371
0
  return message->tiles[index];
372
0
}
373
374
static inline const RFX_RECT* rfx_message_get_rect_const(const RFX_MESSAGE* WINPR_RESTRICT message,
375
                                                         UINT32 index)
376
0
{
377
0
  WINPR_ASSERT(message);
378
0
  WINPR_ASSERT(message->rects);
379
0
  WINPR_ASSERT(index < message->numRects);
380
0
  return &message->rects[index];
381
0
}
382
383
static inline RFX_RECT* rfx_message_get_rect(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index)
384
0
{
385
0
  WINPR_ASSERT(message);
386
0
  WINPR_ASSERT(message->rects);
387
0
  WINPR_ASSERT(index < message->numRects);
388
0
  return &message->rects[index];
389
0
}
390
391
void rfx_context_set_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 pixel_format)
392
11.3k
{
393
11.3k
  WINPR_ASSERT(context);
394
11.3k
  context->pixel_format = pixel_format;
395
11.3k
  const UINT32 bpp = FreeRDPGetBitsPerPixel(pixel_format);
396
11.3k
  context->bits_per_pixel = WINPR_ASSERTING_INT_CAST(UINT8, bpp);
397
11.3k
}
398
399
UINT32 rfx_context_get_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context)
400
0
{
401
0
  WINPR_ASSERT(context);
402
0
  return context->pixel_format;
403
0
}
404
405
void rfx_context_set_palette(RFX_CONTEXT* WINPR_RESTRICT context,
406
                             const BYTE* WINPR_RESTRICT palette)
407
0
{
408
0
  WINPR_ASSERT(context);
409
0
  context->palette = palette;
410
0
}
411
412
const BYTE* rfx_context_get_palette(RFX_CONTEXT* WINPR_RESTRICT context)
413
0
{
414
0
  WINPR_ASSERT(context);
415
0
  return context->palette;
416
0
}
417
418
BOOL rfx_context_reset(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
419
0
{
420
0
  if (!context)
421
0
    return FALSE;
422
423
0
  context->width = WINPR_ASSERTING_INT_CAST(UINT16, width);
424
0
  context->height = WINPR_ASSERTING_INT_CAST(UINT16, height);
425
0
  context->state = RFX_STATE_SEND_HEADERS;
426
0
  context->expectedDataBlockType = WBT_FRAME_BEGIN;
427
0
  context->frameIdx = 0;
428
0
  return TRUE;
429
0
}
430
431
static inline BOOL rfx_process_message_sync(RFX_CONTEXT* WINPR_RESTRICT context,
432
                                            wStream* WINPR_RESTRICT s)
433
884
{
434
884
  UINT32 magic = 0;
435
436
884
  WINPR_ASSERT(context);
437
884
  WINPR_ASSERT(context->priv);
438
884
  context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_SYNC;
439
440
  /* RFX_SYNC */
441
884
  if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
442
5
    return FALSE;
443
444
879
  Stream_Read_UINT32(s, magic); /* magic (4 bytes), 0xCACCACCA */
445
879
  if (magic != WF_MAGIC)
446
74
  {
447
74
    WLog_Print(context->priv->log, WLOG_ERROR, "invalid magic number 0x%08" PRIX32 "", magic);
448
74
    return FALSE;
449
74
  }
450
451
805
  Stream_Read_UINT16(s, context->version); /* version (2 bytes), WF_VERSION_1_0 (0x0100) */
452
805
  if (context->version != WF_VERSION_1_0)
453
14
  {
454
14
    WLog_Print(context->priv->log, WLOG_ERROR, "invalid version number 0x%08" PRIX32 "",
455
14
               context->version);
456
14
    return FALSE;
457
14
  }
458
459
791
  WLog_Print(context->priv->log, WLOG_DEBUG, "version 0x%08" PRIX32 "", context->version);
460
791
  context->decodedHeaderBlocks |= RFX_DECODED_SYNC;
461
791
  return TRUE;
462
805
}
463
464
static inline BOOL rfx_process_message_codec_versions(RFX_CONTEXT* WINPR_RESTRICT context,
465
                                                      wStream* WINPR_RESTRICT s)
466
1.13k
{
467
1.13k
  BYTE numCodecs = 0;
468
469
1.13k
  WINPR_ASSERT(context);
470
1.13k
  WINPR_ASSERT(context->priv);
471
1.13k
  context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_VERSIONS;
472
473
1.13k
  if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
474
6
    return FALSE;
475
476
1.12k
  Stream_Read_UINT8(s, numCodecs);         /* numCodecs (1 byte), must be set to 0x01 */
477
1.12k
  Stream_Read_UINT8(s, context->codec_id); /* codecId (1 byte), must be set to 0x01 */
478
1.12k
  Stream_Read_UINT16(
479
1.12k
      s, context->codec_version); /* version (2 bytes), must be set to WF_VERSION_1_0 (0x0100)  */
480
481
1.12k
  if (numCodecs != 1)
482
650
  {
483
650
    WLog_Print(context->priv->log, WLOG_ERROR, "numCodes is 0x%02" PRIX8 " (must be 0x01)",
484
650
               numCodecs);
485
650
    return FALSE;
486
650
  }
487
488
474
  if (context->codec_id != 0x01)
489
16
  {
490
16
    WLog_Print(context->priv->log, WLOG_ERROR, "invalid codec id (0x%02" PRIX32 ")",
491
16
               context->codec_id);
492
16
    return FALSE;
493
16
  }
494
495
458
  if (context->codec_version != WF_VERSION_1_0)
496
23
  {
497
23
    WLog_Print(context->priv->log, WLOG_ERROR, "invalid codec version (0x%08" PRIX32 ")",
498
23
               context->codec_version);
499
23
    return FALSE;
500
23
  }
501
502
435
  WLog_Print(context->priv->log, WLOG_DEBUG, "id %" PRIu32 " version 0x%" PRIX32 ".",
503
435
             context->codec_id, context->codec_version);
504
435
  context->decodedHeaderBlocks |= RFX_DECODED_VERSIONS;
505
435
  return TRUE;
506
458
}
507
508
static inline BOOL rfx_process_message_channels(RFX_CONTEXT* WINPR_RESTRICT context,
509
                                                wStream* WINPR_RESTRICT s)
510
314
{
511
314
  BYTE channelId = 0;
512
314
  BYTE numChannels = 0;
513
514
314
  WINPR_ASSERT(context);
515
314
  WINPR_ASSERT(context->priv);
516
314
  context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CHANNELS;
517
518
314
  if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 1))
519
43
    return FALSE;
520
521
271
  Stream_Read_UINT8(s, numChannels); /* numChannels (1 byte), must bet set to 0x01 */
522
523
  /* In RDVH sessions, numChannels will represent the number of virtual monitors
524
   * configured and does not always be set to 0x01 as [MS-RDPRFX] said.
525
   */
526
271
  if (numChannels < 1)
527
2
  {
528
2
    WLog_Print(context->priv->log, WLOG_ERROR, "no channels announced");
529
2
    return FALSE;
530
2
  }
531
532
269
  if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, numChannels, 5ull))
533
7
    return FALSE;
534
535
  /* RFX_CHANNELT */
536
262
  Stream_Read_UINT8(s, channelId); /* channelId (1 byte), must be set to 0x00 */
537
538
262
  if (channelId != 0x00)
539
8
  {
540
8
    WLog_Print(context->priv->log, WLOG_ERROR, "channelId:0x%02" PRIX8 ", expected:0x00",
541
8
               channelId);
542
8
    return FALSE;
543
8
  }
544
545
254
  Stream_Read_UINT16(s, context->width);  /* width (2 bytes) */
546
254
  Stream_Read_UINT16(s, context->height); /* height (2 bytes) */
547
548
254
  if (!context->width || !context->height)
549
4
  {
550
4
    WLog_Print(context->priv->log, WLOG_ERROR,
551
4
               "invalid channel with/height: %" PRIu16 "x%" PRIu16 "", context->width,
552
4
               context->height);
553
4
    return FALSE;
554
4
  }
555
556
  /* Now, only the first monitor can be used, therefore the other channels will be ignored. */
557
250
  Stream_Seek(s, 5ULL * (numChannels - 1));
558
250
  WLog_Print(context->priv->log, WLOG_DEBUG,
559
250
             "numChannels %" PRIu8 " id %" PRIu8 ", %" PRIu16 "x%" PRIu16 ".", numChannels,
560
250
             channelId, context->width, context->height);
561
250
  context->decodedHeaderBlocks |= RFX_DECODED_CHANNELS;
562
250
  return TRUE;
563
254
}
564
565
static inline BOOL rfx_process_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
566
                                               wStream* WINPR_RESTRICT s)
567
316
{
568
316
  BYTE ctxId = 0;
569
316
  UINT16 tileSize = 0;
570
316
  UINT16 properties = 0;
571
572
316
  WINPR_ASSERT(context);
573
316
  WINPR_ASSERT(context->priv);
574
316
  context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CONTEXT;
575
576
316
  if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 5))
577
2
    return FALSE;
578
579
314
  Stream_Read_UINT8(s, ctxId);     /* ctxId (1 byte), must be set to 0x00 */
580
314
  Stream_Read_UINT16(s, tileSize); /* tileSize (2 bytes), must be set to CT_TILE_64x64 (0x0040) */
581
314
  Stream_Read_UINT16(s, properties); /* properties (2 bytes) */
582
314
  WLog_Print(context->priv->log, WLOG_DEBUG,
583
314
             "ctxId %" PRIu8 " tileSize %" PRIu16 " properties 0x%04" PRIX16 ".", ctxId, tileSize,
584
314
             properties);
585
314
  context->properties = properties;
586
314
  context->flags = (properties & 0x0007);
587
588
314
  if (context->flags == CODEC_MODE)
589
88
  {
590
88
    WLog_Print(context->priv->log, WLOG_DEBUG, "codec is in image mode.");
591
88
  }
592
226
  else
593
226
  {
594
226
    WLog_Print(context->priv->log, WLOG_DEBUG, "codec is in video mode.");
595
226
  }
596
597
314
  switch ((properties & 0x1E00) >> 9)
598
314
  {
599
82
    case CLW_ENTROPY_RLGR1:
600
82
      context->mode = RLGR1;
601
82
      WLog_Print(context->priv->log, WLOG_DEBUG, "RLGR1.");
602
82
      break;
603
604
221
    case CLW_ENTROPY_RLGR3:
605
221
      context->mode = RLGR3;
606
221
      WLog_Print(context->priv->log, WLOG_DEBUG, "RLGR3.");
607
221
      break;
608
609
11
    default:
610
11
      WLog_Print(context->priv->log, WLOG_ERROR, "unknown RLGR algorithm.");
611
11
      return FALSE;
612
314
  }
613
614
303
  context->decodedHeaderBlocks |= RFX_DECODED_CONTEXT;
615
303
  return TRUE;
616
314
}
617
618
static inline BOOL rfx_process_message_frame_begin(
619
    RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message,
620
    wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType)
621
0
{
622
0
  UINT32 frameIdx = 0;
623
0
  UINT16 numRegions = 0;
624
625
0
  WINPR_ASSERT(context);
626
0
  WINPR_ASSERT(context->priv);
627
0
  WINPR_ASSERT(message);
628
0
  WINPR_ASSERT(pExpectedBlockType);
629
630
0
  if (*pExpectedBlockType != WBT_FRAME_BEGIN)
631
0
  {
632
0
    WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected wants WBT_FRAME_BEGIN");
633
0
    return FALSE;
634
0
  }
635
636
0
  *pExpectedBlockType = WBT_REGION;
637
638
0
  if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
639
0
    return FALSE;
640
641
0
  Stream_Read_UINT32(
642
0
      s, frameIdx); /* frameIdx (4 bytes), if codec is in video mode, must be ignored */
643
0
  Stream_Read_UINT16(s, numRegions); /* numRegions (2 bytes) */
644
0
  WLog_Print(context->priv->log, WLOG_DEBUG,
645
0
             "RFX_FRAME_BEGIN: frameIdx: %" PRIu32 " numRegions: %" PRIu16 "", frameIdx,
646
0
             numRegions);
647
0
  return TRUE;
648
0
}
649
650
static inline BOOL rfx_process_message_frame_end(
651
    RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message,
652
    WINPR_ATTR_UNUSED wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType)
653
0
{
654
0
  WINPR_ASSERT(context);
655
0
  WINPR_ASSERT(context->priv);
656
0
  WINPR_ASSERT(message);
657
0
  WINPR_ASSERT(s);
658
0
  WINPR_ASSERT(pExpectedBlockType);
659
660
0
  if (*pExpectedBlockType != WBT_FRAME_END)
661
0
  {
662
0
    WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected, wants WBT_FRAME_END");
663
0
    return FALSE;
664
0
  }
665
666
0
  *pExpectedBlockType = WBT_FRAME_BEGIN;
667
0
  WLog_Print(context->priv->log, WLOG_DEBUG, "RFX_FRAME_END");
668
0
  return TRUE;
669
0
}
670
671
static inline BOOL rfx_resize_rects(RFX_MESSAGE* WINPR_RESTRICT message)
672
0
{
673
0
  WINPR_ASSERT(message);
674
675
0
  RFX_RECT* tmpRects =
676
0
      winpr_aligned_recalloc(message->rects, message->numRects, sizeof(RFX_RECT), 32);
677
0
  if (!tmpRects)
678
0
    return FALSE;
679
0
  message->rects = tmpRects;
680
0
  return TRUE;
681
0
}
682
683
static inline BOOL rfx_process_message_region(RFX_CONTEXT* WINPR_RESTRICT context,
684
                                              RFX_MESSAGE* WINPR_RESTRICT message,
685
                                              wStream* WINPR_RESTRICT s,
686
                                              UINT16* WINPR_RESTRICT pExpectedBlockType)
687
0
{
688
0
  UINT16 regionType = 0;
689
0
  UINT16 numTileSets = 0;
690
691
0
  WINPR_ASSERT(context);
692
0
  WINPR_ASSERT(context->priv);
693
0
  WINPR_ASSERT(message);
694
0
  WINPR_ASSERT(pExpectedBlockType);
695
696
0
  if (*pExpectedBlockType != WBT_REGION)
697
0
  {
698
0
    WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected wants WBT_REGION");
699
0
    return FALSE;
700
0
  }
701
702
0
  *pExpectedBlockType = WBT_EXTENSION;
703
704
0
  if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 3))
705
0
    return FALSE;
706
707
0
  Stream_Seek_UINT8(s);                     /* regionFlags (1 byte) */
708
0
  Stream_Read_UINT16(s, message->numRects); /* numRects (2 bytes) */
709
710
0
  if (message->numRects < 1)
711
0
  {
712
    /*
713
       If numRects is zero the decoder must generate a rectangle with
714
       coordinates (0, 0, width, height).
715
       See [MS-RDPRFX] (revision >= 17.0) 2.2.2.3.3 TS_RFX_REGION
716
       https://msdn.microsoft.com/en-us/library/ff635233.aspx
717
    */
718
0
    message->numRects = 1;
719
0
    if (!rfx_resize_rects(message))
720
0
      return FALSE;
721
722
0
    message->rects->x = 0;
723
0
    message->rects->y = 0;
724
0
    message->rects->width = context->width;
725
0
    message->rects->height = context->height;
726
0
    return TRUE;
727
0
  }
728
729
0
  if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, message->numRects, 8ull))
730
0
    return FALSE;
731
732
0
  if (!rfx_resize_rects(message))
733
0
    return FALSE;
734
735
  /* rects */
736
0
  for (UINT16 i = 0; i < message->numRects; i++)
737
0
  {
738
0
    RFX_RECT* rect = rfx_message_get_rect(message, i);
739
    /* RFX_RECT */
740
0
    Stream_Read_UINT16(s, rect->x);      /* x (2 bytes) */
741
0
    Stream_Read_UINT16(s, rect->y);      /* y (2 bytes) */
742
0
    Stream_Read_UINT16(s, rect->width);  /* width (2 bytes) */
743
0
    Stream_Read_UINT16(s, rect->height); /* height (2 bytes) */
744
0
    WLog_Print(context->priv->log, WLOG_DEBUG,
745
0
               "rect %" PRIu16 " (x,y=%" PRIu16 ",%" PRIu16 " w,h=%" PRIu16 " %" PRIu16 ").", i,
746
0
               rect->x, rect->y, rect->width, rect->height);
747
0
  }
748
749
0
  if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4))
750
0
    return FALSE;
751
752
0
  Stream_Read_UINT16(s, regionType);  /*regionType (2 bytes): MUST be set to CBT_REGION (0xCAC1)*/
753
0
  Stream_Read_UINT16(s, numTileSets); /*numTilesets (2 bytes): MUST be set to 0x0001.*/
754
755
0
  if (regionType != CBT_REGION)
756
0
  {
757
0
    WLog_Print(context->priv->log, WLOG_ERROR, "invalid region type 0x%04" PRIX16 "",
758
0
               regionType);
759
0
    return TRUE;
760
0
  }
761
762
0
  if (numTileSets != 0x0001)
763
0
  {
764
0
    WLog_Print(context->priv->log, WLOG_ERROR, "invalid number of tilesets (%" PRIu16 ")",
765
0
               numTileSets);
766
0
    return FALSE;
767
0
  }
768
769
0
  return TRUE;
770
0
}
771
772
typedef struct
773
{
774
  RFX_TILE* tile;
775
  RFX_CONTEXT* context;
776
} RFX_TILE_PROCESS_WORK_PARAM;
777
778
static inline void CALLBACK
779
rfx_process_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
780
                                       void* context, WINPR_ATTR_UNUSED PTP_WORK work)
781
0
{
782
0
  RFX_TILE_PROCESS_WORK_PARAM* param = (RFX_TILE_PROCESS_WORK_PARAM*)context;
783
0
  WINPR_ASSERT(param);
784
0
  WINPR_ASSERT(param->context);
785
0
  WINPR_ASSERT(param->context->priv);
786
787
0
  if (!rfx_decode_rgb(param->context, param->tile, param->tile->data, 64 * 4))
788
0
    WLog_Print(param->context->priv->log, WLOG_ERROR, "rfx_decode_rgb failed");
789
0
}
790
791
static inline BOOL rfx_allocate_tiles(RFX_MESSAGE* WINPR_RESTRICT message, size_t count,
792
                                      BOOL allocOnly)
793
0
{
794
0
  WINPR_ASSERT(message);
795
796
0
  RFX_TILE** tmpTiles =
797
0
      (RFX_TILE**)winpr_aligned_recalloc((void*)message->tiles, count, sizeof(RFX_TILE*), 32);
798
0
  if (!tmpTiles && (count != 0))
799
0
    return FALSE;
800
801
0
  message->tiles = tmpTiles;
802
0
  if (!allocOnly)
803
0
    message->numTiles = WINPR_ASSERTING_INT_CAST(UINT16, count);
804
0
  else
805
0
  {
806
0
    WINPR_ASSERT(message->numTiles <= count);
807
0
  }
808
0
  message->allocatedTiles = count;
809
810
0
  return TRUE;
811
0
}
812
813
static inline BOOL rfx_process_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
814
                                               RFX_MESSAGE* WINPR_RESTRICT message,
815
                                               wStream* WINPR_RESTRICT s,
816
                                               UINT16* WINPR_RESTRICT pExpectedBlockType)
817
0
{
818
0
  BOOL rc = 0;
819
0
  size_t close_cnt = 0;
820
0
  BYTE quant = 0;
821
0
  RFX_TILE* tile = nullptr;
822
0
  UINT32* quants = nullptr;
823
0
  UINT16 subtype = 0;
824
0
  UINT16 numTiles = 0;
825
0
  UINT32 blockLen = 0;
826
0
  UINT32 blockType = 0;
827
0
  UINT32 tilesDataSize = 0;
828
0
  PTP_WORK* work_objects = nullptr;
829
0
  RFX_TILE_PROCESS_WORK_PARAM* params = nullptr;
830
0
  void* pmem = nullptr;
831
832
0
  WINPR_ASSERT(context);
833
0
  WINPR_ASSERT(context->priv);
834
0
  WINPR_ASSERT(message);
835
0
  WINPR_ASSERT(pExpectedBlockType);
836
837
0
  if (*pExpectedBlockType != WBT_EXTENSION)
838
0
  {
839
0
    WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected wants a tileset");
840
0
    return FALSE;
841
0
  }
842
843
0
  *pExpectedBlockType = WBT_FRAME_END;
844
845
0
  if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 14))
846
0
    return FALSE;
847
848
0
  Stream_Read_UINT16(s, subtype); /* subtype (2 bytes) must be set to CBT_TILESET (0xCAC2) */
849
0
  if (subtype != CBT_TILESET)
850
0
  {
851
0
    WLog_Print(context->priv->log, WLOG_ERROR, "invalid subtype, expected CBT_TILESET.");
852
0
    return FALSE;
853
0
  }
854
855
0
  Stream_Seek_UINT16(s);                   /* idx (2 bytes), must be set to 0x0000 */
856
0
  Stream_Seek_UINT16(s);                   /* properties (2 bytes) */
857
0
  Stream_Read_UINT8(s, context->numQuant); /* numQuant (1 byte) */
858
0
  Stream_Seek_UINT8(s);                    /* tileSize (1 byte), must be set to 0x40 */
859
860
0
  if (context->numQuant < 1)
861
0
  {
862
0
    WLog_Print(context->priv->log, WLOG_ERROR, "no quantization value.");
863
0
    return FALSE;
864
0
  }
865
866
0
  Stream_Read_UINT16(s, numTiles); /* numTiles (2 bytes) */
867
0
  if (numTiles < 1)
868
0
  {
869
    /* Windows Server 2012 (not R2) can send empty tile sets */
870
0
    return TRUE;
871
0
  }
872
873
0
  Stream_Read_UINT32(s, tilesDataSize); /* tilesDataSize (4 bytes) */
874
875
0
  if (!(pmem = winpr_aligned_recalloc(context->quants, context->numQuant,
876
0
                                      NR_QUANT_VALUES * sizeof(UINT32), 32)))
877
0
    return FALSE;
878
879
0
  quants = context->quants = (UINT32*)pmem;
880
881
  /* quantVals */
882
0
  if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, context->numQuant, 5ull))
883
0
    return FALSE;
884
885
0
  for (size_t i = 0; i < context->numQuant; i++)
886
0
  {
887
    /* RFX_CODEC_QUANT */
888
0
    Stream_Read_UINT8(s, quant);
889
0
    *quants++ = (quant & 0x0F);
890
0
    *quants++ = (quant >> 4);
891
0
    Stream_Read_UINT8(s, quant);
892
0
    *quants++ = (quant & 0x0F);
893
0
    *quants++ = (quant >> 4);
894
0
    Stream_Read_UINT8(s, quant);
895
0
    *quants++ = (quant & 0x0F);
896
0
    *quants++ = (quant >> 4);
897
0
    Stream_Read_UINT8(s, quant);
898
0
    *quants++ = (quant & 0x0F);
899
0
    *quants++ = (quant >> 4);
900
0
    Stream_Read_UINT8(s, quant);
901
0
    *quants++ = (quant & 0x0F);
902
0
    *quants++ = (quant >> 4);
903
0
    WLog_Print(
904
0
        context->priv->log, WLOG_DEBUG,
905
0
        "quant %" PRIuz " (%" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32
906
0
        " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 ").",
907
0
        i, context->quants[i * NR_QUANT_VALUES], context->quants[i * NR_QUANT_VALUES + 1],
908
0
        context->quants[i * NR_QUANT_VALUES + 2], context->quants[i * NR_QUANT_VALUES + 3],
909
0
        context->quants[i * NR_QUANT_VALUES + 4], context->quants[i * NR_QUANT_VALUES + 5],
910
0
        context->quants[i * NR_QUANT_VALUES + 6], context->quants[i * NR_QUANT_VALUES + 7],
911
0
        context->quants[i * NR_QUANT_VALUES + 8], context->quants[i * NR_QUANT_VALUES + 9]);
912
0
  }
913
914
0
  for (size_t i = 0; i < message->numTiles; i++)
915
0
  {
916
0
    ObjectPool_Return(context->priv->TilePool, message->tiles[i]);
917
0
    message->tiles[i] = nullptr;
918
0
  }
919
920
0
  if (!rfx_allocate_tiles(message, numTiles, FALSE))
921
0
    return FALSE;
922
923
0
  if (context->priv->UseThreads)
924
0
  {
925
0
    work_objects = (PTP_WORK*)winpr_aligned_calloc(message->numTiles, sizeof(PTP_WORK), 32);
926
0
    params = (RFX_TILE_PROCESS_WORK_PARAM*)winpr_aligned_recalloc(
927
0
        nullptr, message->numTiles, sizeof(RFX_TILE_PROCESS_WORK_PARAM), 32);
928
929
0
    if (!work_objects)
930
0
    {
931
0
      winpr_aligned_free(params);
932
0
      return FALSE;
933
0
    }
934
935
0
    if (!params)
936
0
    {
937
0
      winpr_aligned_free((void*)work_objects);
938
0
      return FALSE;
939
0
    }
940
0
  }
941
942
  /* tiles */
943
0
  close_cnt = 0;
944
0
  rc = FALSE;
945
946
0
  if (Stream_GetRemainingLength(s) >= tilesDataSize)
947
0
  {
948
0
    rc = TRUE;
949
0
    for (size_t i = 0; i < message->numTiles; i++)
950
0
    {
951
0
      wStream subBuffer;
952
0
      wStream* sub = nullptr;
953
954
0
      if (!(tile = (RFX_TILE*)ObjectPool_Take(context->priv->TilePool)))
955
0
      {
956
0
        WLog_Print(context->priv->log, WLOG_ERROR,
957
0
                   "RfxMessageTileSet failed to get tile from object pool");
958
0
        rc = FALSE;
959
0
        break;
960
0
      }
961
962
0
      message->tiles[i] = tile;
963
964
      /* RFX_TILE */
965
0
      if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6))
966
0
      {
967
0
        WLog_Print(context->priv->log, WLOG_ERROR,
968
0
                   "RfxMessageTileSet packet too small to read tile %" PRIuz "/%" PRIu16 "",
969
0
                   i, message->numTiles);
970
0
        rc = FALSE;
971
0
        break;
972
0
      }
973
974
0
      sub = Stream_StaticInit(&subBuffer, Stream_Pointer(s), Stream_GetRemainingLength(s));
975
0
      Stream_Read_UINT16(
976
0
          sub, blockType); /* blockType (2 bytes), must be set to CBT_TILE (0xCAC3) */
977
0
      Stream_Read_UINT32(sub, blockLen); /* blockLen (4 bytes) */
978
979
0
      if (!Stream_SafeSeek(s, blockLen))
980
0
      {
981
0
        rc = FALSE;
982
0
        break;
983
0
      }
984
0
      if ((blockLen < 6 + 13) ||
985
0
          (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, sub, blockLen - 6)))
986
0
      {
987
0
        WLog_Print(context->priv->log, WLOG_ERROR,
988
0
                   "RfxMessageTileSet not enough bytes to read tile %" PRIuz "/%" PRIu16
989
0
                   " with blocklen=%" PRIu32 "",
990
0
                   i, message->numTiles, blockLen);
991
0
        rc = FALSE;
992
0
        break;
993
0
      }
994
995
0
      if (blockType != CBT_TILE)
996
0
      {
997
0
        WLog_Print(context->priv->log, WLOG_ERROR,
998
0
                   "unknown block type 0x%" PRIX32 ", expected CBT_TILE (0xCAC3).",
999
0
                   blockType);
1000
0
        rc = FALSE;
1001
0
        break;
1002
0
      }
1003
1004
0
      Stream_Read_UINT8(sub, tile->quantIdxY);  /* quantIdxY (1 byte) */
1005
0
      Stream_Read_UINT8(sub, tile->quantIdxCb); /* quantIdxCb (1 byte) */
1006
0
      Stream_Read_UINT8(sub, tile->quantIdxCr); /* quantIdxCr (1 byte) */
1007
0
      if (tile->quantIdxY >= context->numQuant)
1008
0
      {
1009
0
        WLog_Print(context->priv->log, WLOG_ERROR,
1010
0
                   "quantIdxY %" PRIu8 " >= numQuant %" PRIu8, tile->quantIdxY,
1011
0
                   context->numQuant);
1012
0
        rc = FALSE;
1013
0
        break;
1014
0
      }
1015
0
      if (tile->quantIdxCb >= context->numQuant)
1016
0
      {
1017
0
        WLog_Print(context->priv->log, WLOG_ERROR,
1018
0
                   "quantIdxCb %" PRIu8 " >= numQuant %" PRIu8, tile->quantIdxCb,
1019
0
                   context->numQuant);
1020
0
        rc = FALSE;
1021
0
        break;
1022
0
      }
1023
0
      if (tile->quantIdxCr >= context->numQuant)
1024
0
      {
1025
0
        WLog_Print(context->priv->log, WLOG_ERROR,
1026
0
                   "quantIdxCr %" PRIu8 " >= numQuant %" PRIu8, tile->quantIdxCr,
1027
0
                   context->numQuant);
1028
0
        rc = FALSE;
1029
0
        break;
1030
0
      }
1031
1032
0
      Stream_Read_UINT16(sub, tile->xIdx);  /* xIdx (2 bytes) */
1033
0
      Stream_Read_UINT16(sub, tile->yIdx);  /* yIdx (2 bytes) */
1034
0
      Stream_Read_UINT16(sub, tile->YLen);  /* YLen (2 bytes) */
1035
0
      Stream_Read_UINT16(sub, tile->CbLen); /* CbLen (2 bytes) */
1036
0
      Stream_Read_UINT16(sub, tile->CrLen); /* CrLen (2 bytes) */
1037
0
      tile->YData = Stream_PointerAs(sub, BYTE);
1038
0
      if (!Stream_SafeSeek(sub, tile->YLen))
1039
0
      {
1040
0
        rc = FALSE;
1041
0
        break;
1042
0
      }
1043
0
      tile->CbData = Stream_PointerAs(sub, BYTE);
1044
0
      if (!Stream_SafeSeek(sub, tile->CbLen))
1045
0
      {
1046
0
        rc = FALSE;
1047
0
        break;
1048
0
      }
1049
0
      tile->CrData = Stream_PointerAs(sub, BYTE);
1050
0
      if (!Stream_SafeSeek(sub, tile->CrLen))
1051
0
      {
1052
0
        rc = FALSE;
1053
0
        break;
1054
0
      }
1055
0
      tile->x = tile->xIdx * 64;
1056
0
      tile->y = tile->yIdx * 64;
1057
1058
0
      if (context->priv->UseThreads)
1059
0
      {
1060
0
        if (!params)
1061
0
        {
1062
0
          rc = FALSE;
1063
0
          break;
1064
0
        }
1065
1066
0
        params[i].context = context;
1067
0
        params[i].tile = message->tiles[i];
1068
1069
0
        if (!(work_objects[i] = CreateThreadpoolWork(rfx_process_message_tile_work_callback,
1070
0
                                                     (void*)&params[i], nullptr)))
1071
0
        {
1072
0
          WLog_Print(context->priv->log, WLOG_ERROR, "CreateThreadpoolWork failed.");
1073
0
          rc = FALSE;
1074
0
          break;
1075
0
        }
1076
1077
0
        SubmitThreadpoolWork(work_objects[i]);
1078
0
        close_cnt = i + 1;
1079
0
      }
1080
0
      else
1081
0
      {
1082
0
        if (!rfx_decode_rgb(context, tile, tile->data, 64 * 4))
1083
0
        {
1084
0
          rc = FALSE;
1085
0
          break;
1086
0
        }
1087
0
      }
1088
0
    }
1089
0
  }
1090
1091
0
  if (context->priv->UseThreads)
1092
0
  {
1093
0
    for (size_t i = 0; i < close_cnt; i++)
1094
0
    {
1095
0
      WaitForThreadpoolWorkCallbacks(work_objects[i], FALSE);
1096
0
      CloseThreadpoolWork(work_objects[i]);
1097
0
    }
1098
0
  }
1099
1100
0
  winpr_aligned_free((void*)work_objects);
1101
0
  winpr_aligned_free(params);
1102
1103
0
  for (size_t i = 0; i < message->numTiles; i++)
1104
0
  {
1105
0
    if (!(tile = message->tiles[i]))
1106
0
      continue;
1107
1108
0
    tile->YLen = tile->CbLen = tile->CrLen = 0;
1109
0
    tile->YData = tile->CbData = tile->CrData = nullptr;
1110
0
  }
1111
1112
0
  return rc;
1113
0
}
1114
1115
BOOL rfx_process_message(RFX_CONTEXT* WINPR_RESTRICT context, const BYTE* WINPR_RESTRICT data,
1116
                         UINT32 length, UINT32 left, UINT32 top, BYTE* WINPR_RESTRICT dst,
1117
                         UINT32 dstFormat, UINT32 dstStride, UINT32 dstHeight,
1118
                         REGION16* WINPR_RESTRICT invalidRegion)
1119
6.55k
{
1120
6.55k
  REGION16 updateRegion = WINPR_C_ARRAY_INIT;
1121
6.55k
  wStream inStream = WINPR_C_ARRAY_INIT;
1122
6.55k
  BOOL ok = TRUE;
1123
1124
6.55k
  if (!context || !data || !length)
1125
0
    return FALSE;
1126
1127
6.55k
  WINPR_ASSERT(context->priv);
1128
6.55k
  RFX_MESSAGE* message = &context->currentMessage;
1129
1130
6.55k
  wStream* s = Stream_StaticConstInit(&inStream, data, length);
1131
1132
9.19k
  while (ok && Stream_GetRemainingLength(s) > 6)
1133
6.58k
  {
1134
6.58k
    wStream subStreamBuffer = WINPR_C_ARRAY_INIT;
1135
6.58k
    size_t extraBlockLen = 0;
1136
6.58k
    UINT32 blockLen = 0;
1137
6.58k
    UINT32 blockType = 0;
1138
1139
    /* RFX_BLOCKT */
1140
6.58k
    Stream_Read_UINT16(s, blockType); /* blockType (2 bytes) */
1141
6.58k
    Stream_Read_UINT32(s, blockLen);  /* blockLen (4 bytes) */
1142
6.58k
    WLog_Print(context->priv->log, WLOG_DEBUG, "blockType 0x%" PRIX32 " blockLen %" PRIu32 "",
1143
6.58k
               blockType, blockLen);
1144
1145
6.58k
    if (blockLen < 6)
1146
1.28k
    {
1147
1.28k
      WLog_Print(context->priv->log, WLOG_ERROR, "blockLen too small(%" PRIu32 ")", blockLen);
1148
1.28k
      return FALSE;
1149
1.28k
    }
1150
1151
5.29k
    if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, blockLen - 6))
1152
2.13k
      return FALSE;
1153
1154
3.16k
    if (blockType > WBT_CONTEXT && context->decodedHeaderBlocks != RFX_DECODED_HEADERS)
1155
210
    {
1156
210
      WLog_Print(context->priv->log, WLOG_ERROR, "incomplete header blocks processing");
1157
210
      return FALSE;
1158
210
    }
1159
1160
2.95k
    if (blockType >= WBT_CONTEXT && blockType <= WBT_EXTENSION)
1161
381
    {
1162
      /* RFX_CODEC_CHANNELT */
1163
381
      UINT8 codecId = 0;
1164
381
      UINT8 channelId = 0;
1165
1166
381
      if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2))
1167
1
        return FALSE;
1168
1169
380
      extraBlockLen = 2;
1170
380
      Stream_Read_UINT8(s, codecId);   /* codecId (1 byte) must be set to 0x01 */
1171
380
      Stream_Read_UINT8(s, channelId); /* channelId (1 byte) 0xFF or 0x00, see below */
1172
1173
380
      if (codecId != 0x01)
1174
29
      {
1175
29
        WLog_Print(context->priv->log, WLOG_ERROR, "invalid codecId 0x%02" PRIX8 "",
1176
29
                   codecId);
1177
29
        return FALSE;
1178
29
      }
1179
1180
351
      if (blockType == WBT_CONTEXT)
1181
351
      {
1182
        /* If the blockType is set to WBT_CONTEXT, then channelId MUST be set to 0xFF.*/
1183
351
        if (channelId != 0xFF)
1184
33
        {
1185
33
          WLog_Print(context->priv->log, WLOG_ERROR,
1186
33
                     "invalid channelId 0x%02" PRIX8 " for blockType 0x%08" PRIX32 "",
1187
33
                     channelId, blockType);
1188
33
          return FALSE;
1189
33
        }
1190
351
      }
1191
0
      else
1192
0
      {
1193
        /* For all other values of blockType, channelId MUST be set to 0x00. */
1194
0
        if (channelId != 0x00)
1195
0
        {
1196
0
          WLog_Print(context->priv->log, WLOG_ERROR,
1197
0
                     "invalid channelId 0x%02" PRIX8 " for blockType WBT_CONTEXT",
1198
0
                     channelId);
1199
0
          return FALSE;
1200
0
        }
1201
0
      }
1202
351
    }
1203
1204
2.88k
    const size_t blockLenNoHeader = blockLen - 6;
1205
2.88k
    if (blockLenNoHeader < extraBlockLen)
1206
2
    {
1207
2
      WLog_Print(context->priv->log, WLOG_ERROR,
1208
2
                 "blockLen too small(%" PRIu32 "), must be >= 6 + %" PRIuz, blockLen,
1209
2
                 extraBlockLen);
1210
2
      return FALSE;
1211
2
    }
1212
1213
2.88k
    const size_t subStreamLen = blockLenNoHeader - extraBlockLen;
1214
2.88k
    wStream* subStream = Stream_StaticInit(&subStreamBuffer, Stream_Pointer(s), subStreamLen);
1215
2.88k
    Stream_Seek(s, subStreamLen);
1216
1217
2.88k
    switch (blockType)
1218
2.88k
    {
1219
      /* Header messages:
1220
       * The stream MUST start with the header messages and any of these headers can appear
1221
       * in the stream at a later stage. The header messages can be repeated.
1222
       */
1223
884
      case WBT_SYNC:
1224
884
        ok = rfx_process_message_sync(context, subStream);
1225
884
        break;
1226
1227
316
      case WBT_CONTEXT:
1228
316
        ok = rfx_process_message_context(context, subStream);
1229
316
        break;
1230
1231
1.13k
      case WBT_CODEC_VERSIONS:
1232
1.13k
        ok = rfx_process_message_codec_versions(context, subStream);
1233
1.13k
        break;
1234
1235
314
      case WBT_CHANNELS:
1236
314
        ok = rfx_process_message_channels(context, subStream);
1237
314
        break;
1238
1239
        /* Data messages:
1240
         * The data associated with each encoded frame or image is always bracketed by the
1241
         * TS_RFX_FRAME_BEGIN (section 2.2.2.3.1) and TS_RFX_FRAME_END (section 2.2.2.3.2)
1242
         * messages. There MUST only be one TS_RFX_REGION (section 2.2.2.3.3) message per
1243
         * frame and one TS_RFX_TILESET (section 2.2.2.3.4) message per TS_RFX_REGION.
1244
         */
1245
1246
0
      case WBT_FRAME_BEGIN:
1247
0
        ok = rfx_process_message_frame_begin(context, message, subStream,
1248
0
                                             &context->expectedDataBlockType);
1249
0
        break;
1250
1251
0
      case WBT_REGION:
1252
0
        ok = rfx_process_message_region(context, message, subStream,
1253
0
                                        &context->expectedDataBlockType);
1254
0
        break;
1255
1256
0
      case WBT_EXTENSION:
1257
0
        ok = rfx_process_message_tileset(context, message, subStream,
1258
0
                                         &context->expectedDataBlockType);
1259
0
        break;
1260
1261
0
      case WBT_FRAME_END:
1262
0
        ok = rfx_process_message_frame_end(context, message, subStream,
1263
0
                                           &context->expectedDataBlockType);
1264
0
        break;
1265
1266
241
      default:
1267
241
        WLog_Print(context->priv->log, WLOG_ERROR, "unknown blockType 0x%" PRIX32 "",
1268
241
                   blockType);
1269
241
        return FALSE;
1270
2.88k
    }
1271
2.88k
  }
1272
1273
2.61k
  if (ok)
1274
1.75k
  {
1275
1.75k
    UINT32 nbUpdateRects = 0;
1276
1.75k
    REGION16 clippingRects = WINPR_C_ARRAY_INIT;
1277
1.75k
    const RECTANGLE_16* updateRects = nullptr;
1278
1.75k
    const DWORD formatSize = FreeRDPGetBytesPerPixel(context->pixel_format);
1279
1.75k
    const UINT32 dstWidth = dstStride / FreeRDPGetBytesPerPixel(dstFormat);
1280
1.75k
    region16_init(&clippingRects);
1281
1282
1.75k
    WINPR_ASSERT(dstWidth <= UINT16_MAX);
1283
1.75k
    WINPR_ASSERT(dstHeight <= UINT16_MAX);
1284
1.75k
    for (UINT32 i = 0; i < message->numRects; i++)
1285
0
    {
1286
0
      RECTANGLE_16 clippingRect = WINPR_C_ARRAY_INIT;
1287
0
      const RFX_RECT* rect = &(message->rects[i]);
1288
1289
0
      WINPR_ASSERT(left + rect->x <= UINT16_MAX);
1290
0
      WINPR_ASSERT(top + rect->y <= UINT16_MAX);
1291
0
      WINPR_ASSERT(clippingRect.left + rect->width <= UINT16_MAX);
1292
0
      WINPR_ASSERT(clippingRect.top + rect->height <= UINT16_MAX);
1293
1294
0
      clippingRect.left = WINPR_ASSERTING_INT_CAST(UINT16, MIN(left + rect->x, dstWidth));
1295
0
      clippingRect.top = WINPR_ASSERTING_INT_CAST(UINT16, MIN(top + rect->y, dstHeight));
1296
1297
0
      const UINT32 rw = 1UL * clippingRect.left + rect->width;
1298
0
      const UINT32 rh = 1UL * clippingRect.top + rect->height;
1299
0
      const uint16_t right = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rw, dstWidth));
1300
0
      const uint16_t bottom = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rh, dstHeight));
1301
0
      clippingRect.right = right;
1302
0
      clippingRect.bottom = bottom;
1303
0
      if (!region16_union_rect(&clippingRects, &clippingRects, &clippingRect))
1304
0
      {
1305
0
        region16_uninit(&updateRegion);
1306
0
        region16_uninit(&clippingRects);
1307
0
        return FALSE;
1308
0
      }
1309
0
    }
1310
1311
1.75k
    for (UINT32 i = 0; i < message->numTiles; i++)
1312
0
    {
1313
0
      RECTANGLE_16 updateRect = WINPR_C_ARRAY_INIT;
1314
0
      const RFX_TILE* tile = rfx_message_get_tile(message, i);
1315
1316
0
      WINPR_ASSERT(left + tile->x <= UINT16_MAX);
1317
0
      WINPR_ASSERT(top + tile->y <= UINT16_MAX);
1318
1319
0
      updateRect.left = (UINT16)left + tile->x;
1320
0
      updateRect.top = (UINT16)top + tile->y;
1321
0
      updateRect.right = updateRect.left + 64;
1322
0
      updateRect.bottom = updateRect.top + 64;
1323
0
      region16_init(&updateRegion);
1324
0
      if (!region16_intersect_rect(&updateRegion, &clippingRects, &updateRect))
1325
0
      {
1326
0
        region16_uninit(&updateRegion);
1327
0
        region16_uninit(&clippingRects);
1328
0
        return FALSE;
1329
0
      }
1330
0
      updateRects = region16_rects(&updateRegion, &nbUpdateRects);
1331
1332
0
      for (UINT32 j = 0; j < nbUpdateRects; j++)
1333
0
      {
1334
0
        const RECTANGLE_16* cur = &updateRects[j];
1335
0
        const UINT32 stride = 64 * formatSize;
1336
0
        const UINT32 nXDst = cur->left;
1337
0
        const UINT32 nYDst = cur->top;
1338
0
        const UINT32 nXSrc = nXDst - updateRect.left;
1339
0
        const UINT32 nYSrc = nYDst - updateRect.top;
1340
0
        const UINT32 nWidth = cur->right - cur->left;
1341
0
        const UINT32 nHeight = cur->bottom - cur->top;
1342
1343
0
        if (!freerdp_image_copy_no_overlap(
1344
0
                dst, dstFormat, dstStride, nXDst, nYDst, nWidth, nHeight, tile->data,
1345
0
                context->pixel_format, stride, nXSrc, nYSrc, nullptr, FREERDP_FLIP_NONE))
1346
0
        {
1347
0
          region16_uninit(&updateRegion);
1348
0
          region16_uninit(&clippingRects);
1349
0
          WLog_Print(context->priv->log, WLOG_ERROR,
1350
0
                     "nbUpdateRectx[%" PRIu32 " (%" PRIu32 ")] freerdp_image_copy failed",
1351
0
                     j, nbUpdateRects);
1352
0
          return FALSE;
1353
0
        }
1354
1355
0
        if (invalidRegion)
1356
0
        {
1357
0
          if (!region16_union_rect(invalidRegion, invalidRegion, cur))
1358
0
          {
1359
0
            region16_uninit(&updateRegion);
1360
0
            region16_uninit(&clippingRects);
1361
0
            return FALSE;
1362
0
          }
1363
0
        }
1364
0
      }
1365
1366
0
      region16_uninit(&updateRegion);
1367
0
    }
1368
1369
1.75k
    region16_uninit(&clippingRects);
1370
1.75k
    return TRUE;
1371
1.75k
  }
1372
865
  else
1373
865
  {
1374
865
    rfx_message_clear(context, message);
1375
865
    context->currentMessage.freeArray = TRUE;
1376
865
  }
1377
1378
865
  WLog_Print(context->priv->log, WLOG_ERROR, "failed");
1379
865
  return FALSE;
1380
2.61k
}
1381
1382
const UINT32* rfx_message_get_quants(const RFX_MESSAGE* WINPR_RESTRICT message,
1383
                                     UINT16* WINPR_RESTRICT numQuantVals)
1384
0
{
1385
0
  WINPR_ASSERT(message);
1386
0
  if (numQuantVals)
1387
0
    *numQuantVals = message->numQuant;
1388
0
  return message->quantVals;
1389
0
}
1390
1391
const RFX_TILE** rfx_message_get_tiles(const RFX_MESSAGE* WINPR_RESTRICT message,
1392
                                       UINT16* WINPR_RESTRICT numTiles)
1393
0
{
1394
0
  WINPR_ASSERT(message);
1395
0
  if (numTiles)
1396
0
    *numTiles = message->numTiles;
1397
1398
0
  union
1399
0
  {
1400
0
    RFX_TILE** pp;
1401
0
    const RFX_TILE** ppc;
1402
0
  } cnv;
1403
0
  cnv.pp = message->tiles;
1404
0
  return cnv.ppc;
1405
0
}
1406
1407
UINT16 rfx_message_get_tile_count(const RFX_MESSAGE* WINPR_RESTRICT message)
1408
0
{
1409
0
  WINPR_ASSERT(message);
1410
0
  return message->numTiles;
1411
0
}
1412
1413
const RFX_RECT* rfx_message_get_rects(const RFX_MESSAGE* WINPR_RESTRICT message,
1414
                                      UINT16* WINPR_RESTRICT numRects)
1415
0
{
1416
0
  WINPR_ASSERT(message);
1417
0
  if (numRects)
1418
0
    *numRects = message->numRects;
1419
0
  return message->rects;
1420
0
}
1421
1422
UINT16 rfx_message_get_rect_count(const RFX_MESSAGE* WINPR_RESTRICT message)
1423
0
{
1424
0
  WINPR_ASSERT(message);
1425
0
  return message->numRects;
1426
0
}
1427
1428
static BOOL rfx_message_clear(RFX_CONTEXT* WINPR_RESTRICT context,
1429
                              RFX_MESSAGE* WINPR_RESTRICT message)
1430
12.2k
{
1431
12.2k
  if (!message)
1432
0
    return FALSE;
1433
1434
12.2k
  winpr_aligned_free(message->rects);
1435
1436
12.2k
  if (message->tiles)
1437
0
  {
1438
0
    for (size_t i = 0; i < message->numTiles; i++)
1439
0
    {
1440
0
      RFX_TILE* tile = message->tiles[i];
1441
0
      if (!tile)
1442
0
        continue;
1443
1444
0
      if (tile->YCbCrData)
1445
0
      {
1446
0
        BufferPool_Return(context->priv->BufferPool, tile->YCbCrData);
1447
0
        tile->YCbCrData = nullptr;
1448
0
      }
1449
1450
0
      ObjectPool_Return(context->priv->TilePool, (void*)tile);
1451
0
    }
1452
1453
0
    rfx_allocate_tiles(message, 0, FALSE);
1454
0
  }
1455
1456
12.2k
  const BOOL freeArray = message->freeArray;
1457
12.2k
  const RFX_MESSAGE empty = WINPR_C_ARRAY_INIT;
1458
12.2k
  *message = empty;
1459
12.2k
  return freeArray;
1460
12.2k
}
1461
1462
void rfx_message_free(RFX_CONTEXT* WINPR_RESTRICT context, RFX_MESSAGE* WINPR_RESTRICT message)
1463
0
{
1464
0
  if (!rfx_message_clear(context, message))
1465
0
    winpr_aligned_free(message);
1466
0
}
1467
1468
static inline void rfx_update_context_properties(RFX_CONTEXT* WINPR_RESTRICT context)
1469
0
{
1470
0
  UINT16 properties = 0;
1471
1472
0
  WINPR_ASSERT(context);
1473
  /* properties in tilesets: note that this has different format from the one in TS_RFX_CONTEXT */
1474
0
  properties = 1;                          /* lt */
1475
0
  properties |= (context->flags << 1);     /* flags */
1476
0
  properties |= (COL_CONV_ICT << 4);       /* cct */
1477
0
  properties |= (CLW_XFORM_DWT_53_A << 6); /* xft */
1478
0
  properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 10); /* et */
1479
0
  properties |= (SCALAR_QUANTIZATION << 14);                                              /* qt */
1480
0
  context->properties = properties;
1481
0
}
1482
1483
static inline void
1484
rfx_write_message_sync(WINPR_ATTR_UNUSED const RFX_CONTEXT* WINPR_RESTRICT context,
1485
                       WINPR_ATTR_UNUSED wStream* WINPR_RESTRICT s)
1486
0
{
1487
0
  WINPR_ASSERT(context);
1488
1489
0
  Stream_Write_UINT16(s, WBT_SYNC);       /* BlockT.blockType (2 bytes) */
1490
0
  Stream_Write_UINT32(s, 12);             /* BlockT.blockLen (4 bytes) */
1491
0
  Stream_Write_UINT32(s, WF_MAGIC);       /* magic (4 bytes) */
1492
0
  Stream_Write_UINT16(s, WF_VERSION_1_0); /* version (2 bytes) */
1493
0
}
1494
1495
static inline void
1496
rfx_write_message_codec_versions(WINPR_ATTR_UNUSED const RFX_CONTEXT* WINPR_RESTRICT context,
1497
                                 wStream* WINPR_RESTRICT s)
1498
0
{
1499
0
  WINPR_ASSERT(context);
1500
1501
0
  Stream_Write_UINT16(s, WBT_CODEC_VERSIONS); /* BlockT.blockType (2 bytes) */
1502
0
  Stream_Write_UINT32(s, 10);                 /* BlockT.blockLen (4 bytes) */
1503
0
  Stream_Write_UINT8(s, 1);                   /* numCodecs (1 byte) */
1504
0
  Stream_Write_UINT8(s, 1);                   /* codecs.codecId (1 byte) */
1505
0
  Stream_Write_UINT16(s, WF_VERSION_1_0);     /* codecs.version (2 bytes) */
1506
0
}
1507
1508
static inline void rfx_write_message_channels(const RFX_CONTEXT* WINPR_RESTRICT context,
1509
                                              wStream* WINPR_RESTRICT s)
1510
0
{
1511
0
  WINPR_ASSERT(context);
1512
1513
0
  Stream_Write_UINT16(s, WBT_CHANNELS);    /* BlockT.blockType (2 bytes) */
1514
0
  Stream_Write_UINT32(s, 12);              /* BlockT.blockLen (4 bytes) */
1515
0
  Stream_Write_UINT8(s, 1);                /* numChannels (1 byte) */
1516
0
  Stream_Write_UINT8(s, 0);                /* Channel.channelId (1 byte) */
1517
0
  Stream_Write_UINT16(s, context->width);  /* Channel.width (2 bytes) */
1518
0
  Stream_Write_UINT16(s, context->height); /* Channel.height (2 bytes) */
1519
0
}
1520
1521
static inline void rfx_write_message_context(RFX_CONTEXT* WINPR_RESTRICT context,
1522
                                             wStream* WINPR_RESTRICT s)
1523
0
{
1524
0
  UINT16 properties = 0;
1525
0
  WINPR_ASSERT(context);
1526
1527
0
  Stream_Write_UINT16(s, WBT_CONTEXT);   /* CodecChannelT.blockType (2 bytes) */
1528
0
  Stream_Write_UINT32(s, 13);            /* CodecChannelT.blockLen (4 bytes) */
1529
0
  Stream_Write_UINT8(s, 1);              /* CodecChannelT.codecId (1 byte) */
1530
0
  Stream_Write_UINT8(s, 0xFF);           /* CodecChannelT.channelId (1 byte) */
1531
0
  Stream_Write_UINT8(s, 0);              /* ctxId (1 byte) */
1532
0
  Stream_Write_UINT16(s, CT_TILE_64x64); /* tileSize (2 bytes) */
1533
  /* properties */
1534
0
  properties = context->flags;             /* flags */
1535
0
  properties |= (COL_CONV_ICT << 3);       /* cct */
1536
0
  properties |= (CLW_XFORM_DWT_53_A << 5); /* xft */
1537
0
  properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 9); /* et */
1538
0
  properties |= (SCALAR_QUANTIZATION << 13);                                             /* qt */
1539
0
  Stream_Write_UINT16(s, properties); /* properties (2 bytes) */
1540
0
  rfx_update_context_properties(context);
1541
0
}
1542
1543
static inline BOOL rfx_compose_message_header(RFX_CONTEXT* WINPR_RESTRICT context,
1544
                                              wStream* WINPR_RESTRICT s)
1545
0
{
1546
0
  WINPR_ASSERT(context);
1547
0
  if (!Stream_EnsureRemainingCapacity(s, 12 + 10 + 12 + 13))
1548
0
    return FALSE;
1549
1550
0
  rfx_write_message_sync(context, s);
1551
0
  rfx_write_message_context(context, s);
1552
0
  rfx_write_message_codec_versions(context, s);
1553
0
  rfx_write_message_channels(context, s);
1554
0
  return TRUE;
1555
0
}
1556
1557
static inline size_t rfx_tile_length(const RFX_TILE* WINPR_RESTRICT tile)
1558
0
{
1559
0
  WINPR_ASSERT(tile);
1560
0
  return 19ull + tile->YLen + tile->CbLen + tile->CrLen;
1561
0
}
1562
1563
static inline BOOL rfx_write_tile(wStream* WINPR_RESTRICT s, const RFX_TILE* WINPR_RESTRICT tile)
1564
0
{
1565
0
  const size_t blockLen = rfx_tile_length(tile);
1566
0
  if (blockLen > UINT32_MAX)
1567
0
    return FALSE;
1568
0
  if (!Stream_EnsureRemainingCapacity(s, blockLen))
1569
0
    return FALSE;
1570
1571
0
  Stream_Write_UINT16(s, CBT_TILE);           /* BlockT.blockType (2 bytes) */
1572
0
  Stream_Write_UINT32(s, (UINT32)blockLen);   /* BlockT.blockLen (4 bytes) */
1573
0
  Stream_Write_UINT8(s, tile->quantIdxY);     /* quantIdxY (1 byte) */
1574
0
  Stream_Write_UINT8(s, tile->quantIdxCb);    /* quantIdxCb (1 byte) */
1575
0
  Stream_Write_UINT8(s, tile->quantIdxCr);    /* quantIdxCr (1 byte) */
1576
0
  Stream_Write_UINT16(s, tile->xIdx);         /* xIdx (2 bytes) */
1577
0
  Stream_Write_UINT16(s, tile->yIdx);         /* yIdx (2 bytes) */
1578
0
  Stream_Write_UINT16(s, tile->YLen);         /* YLen (2 bytes) */
1579
0
  Stream_Write_UINT16(s, tile->CbLen);        /* CbLen (2 bytes) */
1580
0
  Stream_Write_UINT16(s, tile->CrLen);        /* CrLen (2 bytes) */
1581
0
  Stream_Write(s, tile->YData, tile->YLen);   /* YData */
1582
0
  Stream_Write(s, tile->CbData, tile->CbLen); /* CbData */
1583
0
  Stream_Write(s, tile->CrData, tile->CrLen); /* CrData */
1584
0
  return TRUE;
1585
0
}
1586
1587
struct S_RFX_TILE_COMPOSE_WORK_PARAM
1588
{
1589
  RFX_TILE* tile;
1590
  RFX_CONTEXT* context;
1591
};
1592
1593
static inline void CALLBACK
1594
rfx_compose_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance,
1595
                                       void* context, WINPR_ATTR_UNUSED PTP_WORK work)
1596
0
{
1597
0
  RFX_TILE_COMPOSE_WORK_PARAM* param = (RFX_TILE_COMPOSE_WORK_PARAM*)context;
1598
0
  WINPR_ASSERT(param);
1599
0
  WINPR_ASSERT(param->context);
1600
0
  WINPR_ASSERT(param->context->priv);
1601
1602
0
  if (!rfx_encode_rgb(param->context, param->tile))
1603
0
    WLog_Print(param->context->priv->log, WLOG_ERROR, "rfx_encode_rgb failed");
1604
0
}
1605
1606
static inline BOOL computeRegion(const RFX_RECT* WINPR_RESTRICT rects, size_t numRects,
1607
                                 REGION16* WINPR_RESTRICT region, size_t width, size_t height)
1608
0
{
1609
0
  const RECTANGLE_16 mainRect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, width),
1610
0
                                WINPR_ASSERTING_INT_CAST(UINT16, height) };
1611
1612
0
  WINPR_ASSERT(rects);
1613
0
  for (size_t i = 0; i < numRects; i++)
1614
0
  {
1615
0
    const RFX_RECT* rect = &rects[i];
1616
0
    RECTANGLE_16 rect16 = WINPR_C_ARRAY_INIT;
1617
0
    rect16.left = rect->x;
1618
0
    rect16.top = rect->y;
1619
0
    rect16.right = rect->x + rect->width;
1620
0
    rect16.bottom = rect->y + rect->height;
1621
1622
0
    if (!region16_union_rect(region, region, &rect16))
1623
0
      return FALSE;
1624
0
  }
1625
1626
0
  return region16_intersect_rect(region, region, &mainRect);
1627
0
}
1628
1629
0
#define TILE_NO(v) ((v) / 64)
1630
1631
static inline BOOL setupWorkers(RFX_CONTEXT* WINPR_RESTRICT context, size_t nbTiles)
1632
0
{
1633
0
  WINPR_ASSERT(context);
1634
1635
0
  RFX_CONTEXT_PRIV* priv = context->priv;
1636
0
  WINPR_ASSERT(priv);
1637
1638
0
  void* pmem = nullptr;
1639
1640
0
  if (!context->priv->UseThreads)
1641
0
    return TRUE;
1642
1643
0
  if (!(pmem = winpr_aligned_recalloc((void*)priv->workObjects, nbTiles, sizeof(PTP_WORK), 32)))
1644
0
    return FALSE;
1645
1646
0
  priv->workObjects = (PTP_WORK*)pmem;
1647
1648
0
  if (!(pmem = winpr_aligned_recalloc(priv->tileWorkParams, nbTiles,
1649
0
                                      sizeof(RFX_TILE_COMPOSE_WORK_PARAM), 32)))
1650
0
    return FALSE;
1651
1652
0
  priv->tileWorkParams = (RFX_TILE_COMPOSE_WORK_PARAM*)pmem;
1653
0
  return TRUE;
1654
0
}
1655
1656
static inline BOOL rfx_ensure_tiles(RFX_MESSAGE* WINPR_RESTRICT message, size_t count)
1657
0
{
1658
0
  WINPR_ASSERT(message);
1659
1660
0
  if (message->numTiles + count <= message->allocatedTiles)
1661
0
    return TRUE;
1662
1663
0
  const size_t alloc = MAX(message->allocatedTiles + 1024, message->numTiles + count);
1664
0
  return rfx_allocate_tiles(message, alloc, TRUE);
1665
0
}
1666
1667
RFX_MESSAGE* rfx_encode_message(RFX_CONTEXT* WINPR_RESTRICT context,
1668
                                const RFX_RECT* WINPR_RESTRICT rects, size_t numRects,
1669
                                const BYTE* WINPR_RESTRICT data, UINT32 w, UINT32 h, size_t s)
1670
0
{
1671
0
  const UINT32 width = w;
1672
0
  const UINT32 height = h;
1673
0
  const UINT32 scanline = (UINT32)s;
1674
0
  RFX_MESSAGE* message = nullptr;
1675
0
  PTP_WORK* workObject = nullptr;
1676
0
  RFX_TILE_COMPOSE_WORK_PARAM* workParam = nullptr;
1677
0
  BOOL success = FALSE;
1678
0
  REGION16 rectsRegion = WINPR_C_ARRAY_INIT;
1679
0
  REGION16 tilesRegion = WINPR_C_ARRAY_INIT;
1680
0
  RECTANGLE_16 currentTileRect = WINPR_C_ARRAY_INIT;
1681
0
  const RECTANGLE_16* regionRect = nullptr;
1682
1683
0
  WINPR_ASSERT(data);
1684
0
  WINPR_ASSERT(rects);
1685
0
  WINPR_ASSERT(numRects > 0);
1686
0
  WINPR_ASSERT(w > 0);
1687
0
  WINPR_ASSERT(h > 0);
1688
0
  WINPR_ASSERT(s > 0);
1689
1690
0
  if (!(message = (RFX_MESSAGE*)winpr_aligned_calloc(1, sizeof(RFX_MESSAGE), 32)))
1691
0
    return nullptr;
1692
1693
0
  region16_init(&tilesRegion);
1694
0
  region16_init(&rectsRegion);
1695
1696
0
  if (context->state == RFX_STATE_SEND_HEADERS)
1697
0
    rfx_update_context_properties(context);
1698
1699
0
  message->frameIdx = context->frameIdx++;
1700
1701
0
  if (!context->numQuant)
1702
0
  {
1703
0
    WINPR_ASSERT(context->quants == nullptr);
1704
0
    WINPR_ASSERT(NR_QUANT_VALUES == ARRAYSIZE(rfx_default_quantization_values));
1705
0
    if (!(context->quants =
1706
0
              (UINT32*)winpr_aligned_malloc(sizeof(rfx_default_quantization_values), 32)))
1707
0
      goto skip_encoding_loop;
1708
1709
0
    CopyMemory(context->quants, &rfx_default_quantization_values,
1710
0
               sizeof(rfx_default_quantization_values));
1711
0
    context->numQuant = 1;
1712
0
    context->quantIdxY = 0;
1713
0
    context->quantIdxCb = 0;
1714
0
    context->quantIdxCr = 0;
1715
0
  }
1716
1717
0
  message->numQuant = context->numQuant;
1718
0
  message->quantVals = context->quants;
1719
1720
0
  {
1721
0
    const UINT32 bytesPerPixel = (context->bits_per_pixel / 8);
1722
0
    if (!computeRegion(rects, numRects, &rectsRegion, width, height))
1723
0
      goto skip_encoding_loop;
1724
1725
0
    {
1726
0
      const RECTANGLE_16* extents = region16_extents(&rectsRegion);
1727
0
      WINPR_ASSERT((INT32)extents->right - extents->left > 0);
1728
0
      WINPR_ASSERT((INT32)extents->bottom - extents->top > 0);
1729
0
      const UINT32 maxTilesX = 1 + TILE_NO(extents->right - 1) - TILE_NO(extents->left);
1730
0
      const UINT32 maxTilesY = 1 + TILE_NO(extents->bottom - 1) - TILE_NO(extents->top);
1731
0
      const UINT32 maxNbTiles = maxTilesX * maxTilesY;
1732
1733
0
      if (!rfx_ensure_tiles(message, maxNbTiles))
1734
0
        goto skip_encoding_loop;
1735
1736
0
      if (!setupWorkers(context, maxNbTiles))
1737
0
        goto skip_encoding_loop;
1738
0
    }
1739
1740
0
    if (context->priv->UseThreads)
1741
0
    {
1742
0
      workObject = context->priv->workObjects;
1743
0
      workParam = context->priv->tileWorkParams;
1744
0
    }
1745
1746
0
    {
1747
0
      UINT32 regionNbRects = 0;
1748
0
      regionRect = region16_rects(&rectsRegion, &regionNbRects);
1749
1750
0
      if (!(message->rects = winpr_aligned_calloc(regionNbRects, sizeof(RFX_RECT), 32)))
1751
0
        goto skip_encoding_loop;
1752
1753
0
      message->numRects = WINPR_ASSERTING_INT_CAST(UINT16, regionNbRects);
1754
1755
0
      for (UINT32 i = 0; i < regionNbRects; i++, regionRect++)
1756
0
      {
1757
0
        RFX_RECT* rfxRect = &message->rects[i];
1758
0
        UINT32 startTileX = regionRect->left / 64;
1759
0
        UINT32 endTileX = (regionRect->right - 1) / 64;
1760
0
        UINT32 startTileY = regionRect->top / 64;
1761
0
        UINT32 endTileY = (regionRect->bottom - 1) / 64;
1762
0
        rfxRect->x = regionRect->left;
1763
0
        rfxRect->y = regionRect->top;
1764
0
        rfxRect->width = (regionRect->right - regionRect->left);
1765
0
        rfxRect->height = (regionRect->bottom - regionRect->top);
1766
1767
0
        for (UINT32 yIdx = startTileY, gridRelY = startTileY * 64; yIdx <= endTileY;
1768
0
             yIdx++, gridRelY += 64)
1769
0
        {
1770
0
          UINT32 tileHeight = 64;
1771
1772
0
          if ((yIdx == endTileY) && (gridRelY + 64 > height))
1773
0
            tileHeight = height - gridRelY;
1774
1775
0
          currentTileRect.top = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1776
0
          currentTileRect.bottom =
1777
0
              WINPR_ASSERTING_INT_CAST(UINT16, gridRelY + tileHeight);
1778
1779
0
          for (UINT32 xIdx = startTileX, gridRelX = startTileX * 64; xIdx <= endTileX;
1780
0
               xIdx++, gridRelX += 64)
1781
0
          {
1782
0
            union
1783
0
            {
1784
0
              const BYTE* cpv;
1785
0
              BYTE* pv;
1786
0
            } cnv;
1787
0
            UINT32 tileWidth = 64;
1788
1789
0
            if ((xIdx == endTileX) && (gridRelX + 64 > width))
1790
0
            {
1791
0
              tileWidth = (width - gridRelX);
1792
0
            }
1793
1794
0
            currentTileRect.left = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1795
0
            currentTileRect.right =
1796
0
                WINPR_ASSERTING_INT_CAST(UINT16, gridRelX + tileWidth);
1797
1798
            /* checks if this tile is already treated */
1799
0
            if (region16_intersects_rect(&tilesRegion, &currentTileRect))
1800
0
              continue;
1801
1802
0
            RFX_TILE* tile = (RFX_TILE*)ObjectPool_Take(context->priv->TilePool);
1803
0
            if (!tile)
1804
0
              goto skip_encoding_loop;
1805
1806
0
            tile->xIdx = WINPR_ASSERTING_INT_CAST(UINT16, xIdx);
1807
0
            tile->yIdx = WINPR_ASSERTING_INT_CAST(UINT16, yIdx);
1808
0
            tile->x = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX);
1809
0
            tile->y = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY);
1810
0
            tile->scanline = scanline;
1811
1812
0
            tile->width = tileWidth;
1813
0
            tile->height = tileHeight;
1814
0
            const UINT32 ax = gridRelX;
1815
0
            const UINT32 ay = gridRelY;
1816
1817
0
            if (tile->data && tile->allocated)
1818
0
            {
1819
0
              winpr_aligned_free(tile->data);
1820
0
              tile->allocated = FALSE;
1821
0
            }
1822
1823
            /* Cast away const */
1824
0
            cnv.cpv = &data[(ay * scanline) + (ax * bytesPerPixel)];
1825
0
            tile->data = cnv.pv;
1826
0
            tile->quantIdxY = context->quantIdxY;
1827
0
            tile->quantIdxCb = context->quantIdxCb;
1828
0
            tile->quantIdxCr = context->quantIdxCr;
1829
0
            tile->YLen = tile->CbLen = tile->CrLen = 0;
1830
1831
0
            if (!(tile->YCbCrData =
1832
0
                      (BYTE*)BufferPool_Take(context->priv->BufferPool, -1)))
1833
0
              goto skip_encoding_loop;
1834
1835
0
            tile->YData = &(tile->YCbCrData[((8192 + 32) * 0) + 16]);
1836
0
            tile->CbData = &(tile->YCbCrData[((8192 + 32) * 1) + 16]);
1837
0
            tile->CrData = &(tile->YCbCrData[((8192 + 32) * 2) + 16]);
1838
1839
0
            if (!rfx_ensure_tiles(message, 1))
1840
0
              goto skip_encoding_loop;
1841
0
            message->tiles[message->numTiles++] = tile;
1842
1843
0
            if (context->priv->UseThreads)
1844
0
            {
1845
0
              workParam->context = context;
1846
0
              workParam->tile = tile;
1847
1848
0
              if (!(*workObject =
1849
0
                        CreateThreadpoolWork(rfx_compose_message_tile_work_callback,
1850
0
                                             (void*)workParam, nullptr)))
1851
0
              {
1852
0
                goto skip_encoding_loop;
1853
0
              }
1854
1855
0
              SubmitThreadpoolWork(*workObject);
1856
0
              workObject++;
1857
0
              workParam++;
1858
0
            }
1859
0
            else
1860
0
            {
1861
0
              if (!rfx_encode_rgb(context, tile))
1862
0
                goto skip_encoding_loop;
1863
0
            }
1864
1865
0
            if (!region16_union_rect(&tilesRegion, &tilesRegion, &currentTileRect))
1866
0
              goto skip_encoding_loop;
1867
0
          } /* xIdx */
1868
0
        } /* yIdx */
1869
0
      } /* rects */
1870
0
    }
1871
0
  }
1872
1873
0
  success = TRUE;
1874
0
skip_encoding_loop:
1875
1876
  /* when using threads ensure all computations are done */
1877
0
  if (success)
1878
0
  {
1879
0
    message->tilesDataSize = 0;
1880
0
    workObject = context->priv->workObjects;
1881
1882
0
    for (UINT32 i = 0; i < message->numTiles; i++)
1883
0
    {
1884
0
      if (context->priv->UseThreads)
1885
0
      {
1886
0
        if (*workObject)
1887
0
        {
1888
0
          WaitForThreadpoolWorkCallbacks(*workObject, FALSE);
1889
0
          CloseThreadpoolWork(*workObject);
1890
0
        }
1891
1892
0
        workObject++;
1893
0
      }
1894
1895
0
      const RFX_TILE* tile = message->tiles[i];
1896
0
      const size_t tlen = rfx_tile_length(tile);
1897
0
      message->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tlen);
1898
0
    }
1899
1900
0
    region16_uninit(&tilesRegion);
1901
0
    region16_uninit(&rectsRegion);
1902
1903
0
    return message;
1904
0
  }
1905
1906
0
  WLog_Print(context->priv->log, WLOG_ERROR, "failed");
1907
1908
0
  rfx_message_free(context, message);
1909
0
  region16_uninit(&tilesRegion);
1910
0
  region16_uninit(&rectsRegion);
1911
0
  return nullptr;
1912
0
}
1913
1914
static inline BOOL rfx_clone_rects(RFX_MESSAGE* WINPR_RESTRICT dst,
1915
                                   const RFX_MESSAGE* WINPR_RESTRICT src)
1916
0
{
1917
0
  WINPR_ASSERT(dst);
1918
0
  WINPR_ASSERT(src);
1919
1920
0
  WINPR_ASSERT(dst->rects == nullptr);
1921
0
  WINPR_ASSERT(dst->numRects == 0);
1922
1923
0
  if (src->numRects == 0)
1924
0
    return TRUE;
1925
1926
0
  dst->rects = winpr_aligned_calloc(src->numRects, sizeof(RFX_RECT), 32);
1927
0
  if (!dst->rects)
1928
0
    return FALSE;
1929
0
  dst->numRects = src->numRects;
1930
0
  for (size_t x = 0; x < src->numRects; x++)
1931
0
  {
1932
0
    dst->rects[x] = src->rects[x];
1933
0
  }
1934
0
  return TRUE;
1935
0
}
1936
1937
static inline BOOL rfx_clone_quants(RFX_MESSAGE* WINPR_RESTRICT dst,
1938
                                    const RFX_MESSAGE* WINPR_RESTRICT src)
1939
0
{
1940
0
  WINPR_ASSERT(dst);
1941
0
  WINPR_ASSERT(src);
1942
1943
0
  WINPR_ASSERT(dst->quantVals == nullptr);
1944
0
  WINPR_ASSERT(dst->numQuant == 0);
1945
1946
0
  if (src->numQuant == 0)
1947
0
    return TRUE;
1948
1949
  /* quantVals are part of context */
1950
0
  dst->quantVals = src->quantVals;
1951
0
  dst->numQuant = src->numQuant;
1952
1953
0
  return TRUE;
1954
0
}
1955
1956
static inline RFX_MESSAGE* rfx_split_message(RFX_CONTEXT* WINPR_RESTRICT context,
1957
                                             RFX_MESSAGE* WINPR_RESTRICT message,
1958
                                             size_t* WINPR_RESTRICT numMessages, size_t maxDataSize)
1959
0
{
1960
0
  WINPR_ASSERT(context);
1961
0
  WINPR_ASSERT(message);
1962
0
  WINPR_ASSERT(numMessages);
1963
1964
0
  if (maxDataSize <= 1024)
1965
0
    return nullptr;
1966
1967
0
  maxDataSize -= 1024; /* reserve enough space for headers */
1968
0
  *numMessages = ((message->tilesDataSize + maxDataSize) / maxDataSize) * 4ull;
1969
1970
0
  RFX_MESSAGE* messages =
1971
0
      (RFX_MESSAGE*)winpr_aligned_calloc((*numMessages), sizeof(RFX_MESSAGE), 32);
1972
0
  if (!messages)
1973
0
    return nullptr;
1974
1975
0
  UINT32 j = 0;
1976
0
  for (UINT16 i = 0; i < message->numTiles; i++)
1977
0
  {
1978
0
    RFX_TILE* tile = message->tiles[i];
1979
0
    RFX_MESSAGE* msg = &messages[j];
1980
1981
0
    WINPR_ASSERT(tile);
1982
0
    WINPR_ASSERT(msg);
1983
1984
0
    const size_t tileDataSize = rfx_tile_length(tile);
1985
1986
0
    if ((msg->tilesDataSize + tileDataSize) > ((UINT32)maxDataSize))
1987
0
      j++;
1988
1989
0
    if (msg->numTiles == 0)
1990
0
    {
1991
0
      msg->frameIdx = message->frameIdx + j;
1992
0
      if (!rfx_clone_quants(msg, message))
1993
0
        goto free_messages;
1994
0
      if (!rfx_clone_rects(msg, message))
1995
0
        goto free_messages;
1996
0
      msg->freeArray = TRUE;
1997
0
      if (!rfx_allocate_tiles(msg, message->numTiles, TRUE))
1998
0
        goto free_messages;
1999
0
    }
2000
2001
0
    msg->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tileDataSize);
2002
2003
0
    WINPR_ASSERT(msg->numTiles < msg->allocatedTiles);
2004
0
    msg->tiles[msg->numTiles++] = message->tiles[i];
2005
0
    message->tiles[i] = nullptr;
2006
0
  }
2007
2008
0
  *numMessages = j + 1ULL;
2009
0
  context->frameIdx += j;
2010
0
  message->numTiles = 0;
2011
0
  return messages;
2012
0
free_messages:
2013
2014
0
  for (size_t i = 0; i < j; i++)
2015
0
    rfx_allocate_tiles(&messages[i], 0, FALSE);
2016
2017
0
  winpr_aligned_free(messages);
2018
0
  return nullptr;
2019
0
}
2020
2021
const RFX_MESSAGE* rfx_message_list_get(const RFX_MESSAGE_LIST* WINPR_RESTRICT messages, size_t idx)
2022
0
{
2023
0
  WINPR_ASSERT(messages);
2024
0
  if (idx >= messages->count)
2025
0
    return nullptr;
2026
0
  WINPR_ASSERT(messages->list);
2027
0
  return &messages->list[idx];
2028
0
}
2029
2030
void rfx_message_list_free(RFX_MESSAGE_LIST* messages)
2031
0
{
2032
0
  if (!messages)
2033
0
    return;
2034
0
  for (size_t x = 0; x < messages->count; x++)
2035
0
    rfx_message_clear(messages->context, &messages->list[x]);
2036
0
  free(messages);
2037
0
}
2038
2039
static inline RFX_MESSAGE_LIST* rfx_message_list_new(RFX_CONTEXT* WINPR_RESTRICT context,
2040
                                                     RFX_MESSAGE* WINPR_RESTRICT messages,
2041
                                                     size_t count)
2042
0
{
2043
0
  WINPR_ASSERT(context);
2044
0
  RFX_MESSAGE_LIST* msg = calloc(1, sizeof(RFX_MESSAGE_LIST));
2045
0
  WINPR_ASSERT(msg);
2046
2047
0
  msg->context = context;
2048
0
  msg->count = count;
2049
0
  msg->list = messages;
2050
0
  return msg;
2051
0
}
2052
2053
RFX_MESSAGE_LIST* rfx_encode_messages(RFX_CONTEXT* WINPR_RESTRICT context,
2054
                                      const RFX_RECT* WINPR_RESTRICT rects, size_t numRects,
2055
                                      const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2056
                                      UINT32 scanline, size_t* WINPR_RESTRICT numMessages,
2057
                                      size_t maxDataSize)
2058
0
{
2059
0
  WINPR_ASSERT(context);
2060
0
  WINPR_ASSERT(numMessages);
2061
2062
0
  RFX_MESSAGE* message =
2063
0
      rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2064
0
  if (!message)
2065
0
    return nullptr;
2066
2067
0
  RFX_MESSAGE* list = rfx_split_message(context, message, numMessages, maxDataSize);
2068
0
  rfx_message_free(context, message);
2069
0
  if (!list)
2070
0
    return nullptr;
2071
2072
0
  return rfx_message_list_new(context, list, *numMessages);
2073
0
}
2074
2075
static inline BOOL rfx_write_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context,
2076
                                             wStream* WINPR_RESTRICT s,
2077
                                             const RFX_MESSAGE* WINPR_RESTRICT message)
2078
0
{
2079
0
  WINPR_ASSERT(context);
2080
0
  WINPR_ASSERT(message);
2081
2082
0
  const UINT32 blockLen = 22 + (message->numQuant * 5) + message->tilesDataSize;
2083
2084
0
  if (!Stream_EnsureRemainingCapacity(s, blockLen))
2085
0
    return FALSE;
2086
2087
0
  Stream_Write_UINT16(s, WBT_EXTENSION);       /* CodecChannelT.blockType (2 bytes) */
2088
0
  Stream_Write_UINT32(s, blockLen);            /* set CodecChannelT.blockLen (4 bytes) */
2089
0
  Stream_Write_UINT8(s, 1);                    /* CodecChannelT.codecId (1 byte) */
2090
0
  Stream_Write_UINT8(s, 0);                    /* CodecChannelT.channelId (1 byte) */
2091
0
  Stream_Write_UINT16(s, CBT_TILESET);         /* subtype (2 bytes) */
2092
0
  Stream_Write_UINT16(s, 0);                   /* idx (2 bytes) */
2093
0
  Stream_Write_UINT16(s, context->properties); /* properties (2 bytes) */
2094
0
  Stream_Write_UINT8(
2095
0
      s, WINPR_ASSERTING_INT_CAST(uint8_t, message->numQuant)); /* numQuant (1 byte) */
2096
0
  Stream_Write_UINT8(s, 0x40);                                  /* tileSize (1 byte) */
2097
0
  Stream_Write_UINT16(s, message->numTiles);                    /* numTiles (2 bytes) */
2098
0
  Stream_Write_UINT32(s, message->tilesDataSize);               /* tilesDataSize (4 bytes) */
2099
2100
0
  UINT32* quantVals = message->quantVals;
2101
0
  for (size_t i = 0; i < message->numQuant * 5ul; i++)
2102
0
  {
2103
0
    WINPR_ASSERT(quantVals);
2104
0
    Stream_Write_UINT8(s,
2105
0
                       WINPR_ASSERTING_INT_CAST(uint8_t, quantVals[0] + (quantVals[1] << 4)));
2106
0
    quantVals += 2;
2107
0
  }
2108
2109
0
  for (size_t i = 0; i < message->numTiles; i++)
2110
0
  {
2111
0
    RFX_TILE* tile = message->tiles[i];
2112
0
    if (!tile)
2113
0
      return FALSE;
2114
2115
0
    if (!rfx_write_tile(s, tile))
2116
0
      return FALSE;
2117
0
  }
2118
2119
#ifdef WITH_DEBUG_RFX
2120
  WLog_Print(context->priv->log, WLOG_DEBUG,
2121
             "numQuant: %" PRIu16 " numTiles: %" PRIu16 " tilesDataSize: %" PRIu32 "",
2122
             message->numQuant, message->numTiles, message->tilesDataSize);
2123
#endif
2124
0
  return TRUE;
2125
0
}
2126
2127
static inline BOOL
2128
rfx_write_message_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2129
                              wStream* WINPR_RESTRICT s, const RFX_MESSAGE* WINPR_RESTRICT message)
2130
0
{
2131
0
  WINPR_ASSERT(context);
2132
0
  WINPR_ASSERT(message);
2133
2134
0
  if (!Stream_EnsureRemainingCapacity(s, 14))
2135
0
    return FALSE;
2136
2137
0
  Stream_Write_UINT16(s, WBT_FRAME_BEGIN);   /* CodecChannelT.blockType */
2138
0
  Stream_Write_UINT32(s, 14);                /* CodecChannelT.blockLen */
2139
0
  Stream_Write_UINT8(s, 1);                  /* CodecChannelT.codecId */
2140
0
  Stream_Write_UINT8(s, 0);                  /* CodecChannelT.channelId */
2141
0
  Stream_Write_UINT32(s, message->frameIdx); /* frameIdx */
2142
0
  Stream_Write_UINT16(s, 1);                 /* numRegions */
2143
0
  return TRUE;
2144
0
}
2145
2146
static inline BOOL rfx_write_message_region(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2147
                                            wStream* WINPR_RESTRICT s,
2148
                                            const RFX_MESSAGE* WINPR_RESTRICT message)
2149
0
{
2150
0
  WINPR_ASSERT(context);
2151
0
  WINPR_ASSERT(message);
2152
2153
0
  const size_t blockLen = 15 + (message->numRects * 8);
2154
0
  if (blockLen > UINT32_MAX)
2155
0
    return FALSE;
2156
2157
0
  if (!Stream_EnsureRemainingCapacity(s, blockLen))
2158
0
    return FALSE;
2159
2160
0
  Stream_Write_UINT16(s, WBT_REGION);        /* CodecChannelT.blockType (2 bytes) */
2161
0
  Stream_Write_UINT32(s, (UINT32)blockLen);  /* set CodecChannelT.blockLen (4 bytes) */
2162
0
  Stream_Write_UINT8(s, 1);                  /* CodecChannelT.codecId (1 byte) */
2163
0
  Stream_Write_UINT8(s, 0);                  /* CodecChannelT.channelId (1 byte) */
2164
0
  Stream_Write_UINT8(s, 1);                  /* regionFlags (1 byte) */
2165
0
  Stream_Write_UINT16(s, message->numRects); /* numRects (2 bytes) */
2166
2167
0
  for (UINT16 i = 0; i < message->numRects; i++)
2168
0
  {
2169
0
    const RFX_RECT* rect = rfx_message_get_rect_const(message, i);
2170
0
    WINPR_ASSERT(rect);
2171
2172
    /* Clipping rectangles are relative to destLeft, destTop */
2173
0
    Stream_Write_UINT16(s, rect->x);      /* x (2 bytes) */
2174
0
    Stream_Write_UINT16(s, rect->y);      /* y (2 bytes) */
2175
0
    Stream_Write_UINT16(s, rect->width);  /* width (2 bytes) */
2176
0
    Stream_Write_UINT16(s, rect->height); /* height (2 bytes) */
2177
0
  }
2178
2179
0
  Stream_Write_UINT16(s, CBT_REGION); /* regionType (2 bytes) */
2180
0
  Stream_Write_UINT16(s, 1);          /* numTilesets (2 bytes) */
2181
0
  return TRUE;
2182
0
}
2183
2184
static inline BOOL
2185
rfx_write_message_frame_end(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context,
2186
                            wStream* WINPR_RESTRICT s,
2187
                            WINPR_ATTR_UNUSED const RFX_MESSAGE* WINPR_RESTRICT message)
2188
0
{
2189
0
  WINPR_ASSERT(context);
2190
0
  WINPR_ASSERT(message);
2191
2192
0
  if (!Stream_EnsureRemainingCapacity(s, 8))
2193
0
    return FALSE;
2194
2195
0
  Stream_Write_UINT16(s, WBT_FRAME_END); /* CodecChannelT.blockType */
2196
0
  Stream_Write_UINT32(s, 8);             /* CodecChannelT.blockLen */
2197
0
  Stream_Write_UINT8(s, 1);              /* CodecChannelT.codecId */
2198
0
  Stream_Write_UINT8(s, 0);              /* CodecChannelT.channelId */
2199
0
  return TRUE;
2200
0
}
2201
2202
BOOL rfx_write_message(RFX_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
2203
                       const RFX_MESSAGE* WINPR_RESTRICT message)
2204
0
{
2205
0
  WINPR_ASSERT(context);
2206
0
  WINPR_ASSERT(message);
2207
2208
0
  if (context->state == RFX_STATE_SEND_HEADERS)
2209
0
  {
2210
0
    if (!rfx_compose_message_header(context, s))
2211
0
      return FALSE;
2212
2213
0
    context->state = RFX_STATE_SEND_FRAME_DATA;
2214
0
  }
2215
2216
0
  if (!rfx_write_message_frame_begin(context, s, message) ||
2217
0
      !rfx_write_message_region(context, s, message) ||
2218
0
      !rfx_write_message_tileset(context, s, message) ||
2219
0
      !rfx_write_message_frame_end(context, s, message))
2220
0
  {
2221
0
    return FALSE;
2222
0
  }
2223
2224
0
  return TRUE;
2225
0
}
2226
2227
BOOL rfx_compose_message(RFX_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s,
2228
                         const RFX_RECT* WINPR_RESTRICT rects, size_t numRects,
2229
                         const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height,
2230
                         UINT32 scanline)
2231
0
{
2232
0
  WINPR_ASSERT(context);
2233
0
  RFX_MESSAGE* message =
2234
0
      rfx_encode_message(context, rects, numRects, data, width, height, scanline);
2235
0
  if (!message)
2236
0
    return FALSE;
2237
2238
0
  const BOOL ret = rfx_write_message(context, s, message);
2239
0
  rfx_message_free(context, message);
2240
0
  return ret;
2241
0
}
2242
2243
BOOL rfx_context_set_mode(RFX_CONTEXT* WINPR_RESTRICT context, RLGR_MODE mode)
2244
0
{
2245
0
  WINPR_ASSERT(context);
2246
0
  context->mode = mode;
2247
0
  return TRUE;
2248
0
}
2249
2250
RLGR_MODE rfx_context_get_mode(RFX_CONTEXT* WINPR_RESTRICT context)
2251
0
{
2252
0
  WINPR_ASSERT(context);
2253
0
  return context->mode;
2254
0
}
2255
2256
UINT32 rfx_context_get_frame_idx(const RFX_CONTEXT* WINPR_RESTRICT context)
2257
0
{
2258
0
  WINPR_ASSERT(context);
2259
0
  return context->frameIdx;
2260
0
}
2261
2262
UINT32 rfx_message_get_frame_idx(const RFX_MESSAGE* WINPR_RESTRICT message)
2263
0
{
2264
0
  WINPR_ASSERT(message);
2265
0
  return message->frameIdx;
2266
0
}
2267
2268
static inline BOOL rfx_write_progressive_wb_sync(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2269
                                                 wStream* WINPR_RESTRICT s)
2270
0
{
2271
0
  const UINT32 blockLen = 12;
2272
0
  WINPR_ASSERT(rfx);
2273
0
  WINPR_ASSERT(s);
2274
2275
0
  if (!Stream_EnsureRemainingCapacity(s, blockLen))
2276
0
    return FALSE;
2277
2278
0
  Stream_Write_UINT16(s, PROGRESSIVE_WBT_SYNC); /* blockType (2 bytes) */
2279
0
  Stream_Write_UINT32(s, blockLen);             /* blockLen (4 bytes) */
2280
0
  Stream_Write_UINT32(s, 0xCACCACCA);           /* magic (4 bytes) */
2281
0
  Stream_Write_UINT16(s, 0x0100);               /* version (2 bytes) */
2282
0
  return TRUE;
2283
0
}
2284
2285
static inline BOOL
2286
rfx_write_progressive_wb_context(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2287
                                 wStream* WINPR_RESTRICT s)
2288
0
{
2289
0
  const UINT32 blockLen = 10;
2290
0
  WINPR_ASSERT(rfx);
2291
0
  WINPR_ASSERT(s);
2292
2293
0
  if (!Stream_EnsureRemainingCapacity(s, blockLen))
2294
0
    return FALSE;
2295
2296
0
  Stream_Write_UINT16(s, PROGRESSIVE_WBT_CONTEXT); /* blockType (2 bytes) */
2297
0
  Stream_Write_UINT32(s, blockLen);                /* blockLen (4 bytes) */
2298
0
  Stream_Write_UINT8(s, 0);                        /* ctxId (1 byte) */
2299
0
  Stream_Write_UINT16(s, 64);                      /* tileSize (2 bytes) */
2300
0
  Stream_Write_UINT8(s, 0);                        /* flags (1 byte) */
2301
0
  return TRUE;
2302
0
}
2303
2304
static inline BOOL rfx_write_progressive_region(RFX_CONTEXT* WINPR_RESTRICT rfx,
2305
                                                wStream* WINPR_RESTRICT s,
2306
                                                const RFX_MESSAGE* WINPR_RESTRICT msg)
2307
0
{
2308
  /* RFX_REGION */
2309
0
  UINT32 blockLen = 18;
2310
0
  UINT32 tilesDataSize = 0;
2311
0
  const size_t start = Stream_GetPosition(s);
2312
2313
0
  WINPR_ASSERT(rfx);
2314
0
  WINPR_ASSERT(s);
2315
0
  WINPR_ASSERT(msg);
2316
2317
0
  blockLen += msg->numRects * 8;
2318
0
  blockLen += msg->numQuant * 5;
2319
0
  tilesDataSize = msg->numTiles * 22UL;
2320
0
  for (UINT16 i = 0; i < msg->numTiles; i++)
2321
0
  {
2322
0
    const RFX_TILE* tile = msg->tiles[i];
2323
0
    WINPR_ASSERT(tile);
2324
0
    tilesDataSize += tile->YLen + tile->CbLen + tile->CrLen;
2325
0
  }
2326
0
  blockLen += tilesDataSize;
2327
2328
0
  if (!Stream_EnsureRemainingCapacity(s, blockLen))
2329
0
    return FALSE;
2330
2331
0
  Stream_Write_UINT16(s, PROGRESSIVE_WBT_REGION); /* blockType (2 bytes) */
2332
0
  Stream_Write_UINT32(s, blockLen);               /* blockLen (4 bytes) */
2333
0
  Stream_Write_UINT8(s, 64);                      /* tileSize (1 byte) */
2334
0
  Stream_Write_UINT16(s, msg->numRects);          /* numRects (2 bytes) */
2335
0
  WINPR_ASSERT(msg->numQuant <= UINT8_MAX);
2336
0
  Stream_Write_UINT8(s, (UINT8)msg->numQuant); /* numQuant (1 byte) */
2337
0
  Stream_Write_UINT8(s, 0);                    /* numProgQuant (1 byte) */
2338
0
  Stream_Write_UINT8(s, 0);                    /* flags (1 byte) */
2339
0
  Stream_Write_UINT16(s, msg->numTiles);       /* numTiles (2 bytes) */
2340
0
  Stream_Write_UINT32(s, tilesDataSize);       /* tilesDataSize (4 bytes) */
2341
2342
0
  for (UINT16 i = 0; i < msg->numRects; i++)
2343
0
  {
2344
    /* TS_RFX_RECT */
2345
0
    const RFX_RECT* r = &msg->rects[i];
2346
0
    Stream_Write_UINT16(s, r->x);      /* x (2 bytes) */
2347
0
    Stream_Write_UINT16(s, r->y);      /* y (2 bytes) */
2348
0
    Stream_Write_UINT16(s, r->width);  /* width (2 bytes) */
2349
0
    Stream_Write_UINT16(s, r->height); /* height (2 bytes) */
2350
0
  }
2351
2352
  /**
2353
   * Note: The RFX_COMPONENT_CODEC_QUANT structure differs from the
2354
   * TS_RFX_CODEC_QUANT ([MS-RDPRFX] section 2.2.2.1.5) structure with respect
2355
   * to the order of the bands.
2356
   *             0    1    2   3     4    5    6    7    8    9
2357
   * RDPRFX:   LL3, LH3, HL3, HH3, LH2, HL2, HH2, LH1, HL1, HH1
2358
   * RDPEGFX:  LL3, HL3, LH3, HH3, HL2, LH2, HH2, HL1, LH1, HH1
2359
   */
2360
0
  for (UINT16 i = 0; i < msg->numQuant; i++)
2361
0
  {
2362
0
    const UINT32* qv = &msg->quantVals[10ULL * i];
2363
    /* RFX_COMPONENT_CODEC_QUANT */
2364
0
    Stream_Write_UINT8(s, (UINT8)(qv[0] + (qv[2] << 4))); /* LL3 (4-bit), HL3 (4-bit) */
2365
0
    Stream_Write_UINT8(s, (UINT8)(qv[1] + (qv[3] << 4))); /* LH3 (4-bit), HH3 (4-bit) */
2366
0
    Stream_Write_UINT8(s, (UINT8)(qv[5] + (qv[4] << 4))); /* HL2 (4-bit), LH2 (4-bit) */
2367
0
    Stream_Write_UINT8(s, (UINT8)(qv[6] + (qv[8] << 4))); /* HH2 (4-bit), HL1 (4-bit) */
2368
0
    Stream_Write_UINT8(s, (UINT8)(qv[7] + (qv[9] << 4))); /* LH1 (4-bit), HH1 (4-bit) */
2369
0
  }
2370
2371
0
  for (UINT16 i = 0; i < msg->numTiles; i++)
2372
0
  {
2373
0
    const RFX_TILE* tile = msg->tiles[i];
2374
0
    if (!rfx_write_progressive_tile_simple(rfx, s, tile))
2375
0
      return FALSE;
2376
0
  }
2377
2378
0
  const size_t end = Stream_GetPosition(s);
2379
0
  const size_t used = end - start;
2380
0
  return (used == blockLen);
2381
0
}
2382
2383
static inline BOOL
2384
rfx_write_progressive_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2385
                                  wStream* WINPR_RESTRICT s, const RFX_MESSAGE* WINPR_RESTRICT msg)
2386
0
{
2387
0
  const UINT32 blockLen = 12;
2388
0
  WINPR_ASSERT(rfx);
2389
0
  WINPR_ASSERT(s);
2390
0
  WINPR_ASSERT(msg);
2391
2392
0
  if (!Stream_EnsureRemainingCapacity(s, blockLen))
2393
0
    return FALSE;
2394
2395
0
  Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_BEGIN); /* blockType (2 bytes) */
2396
0
  Stream_Write_UINT32(s, blockLen);                    /* blockLen (4 bytes) */
2397
0
  Stream_Write_UINT32(s, msg->frameIdx);               /* frameIndex (4 bytes) */
2398
0
  Stream_Write_UINT16(s, 1);                           /* regionCount (2 bytes) */
2399
2400
0
  return TRUE;
2401
0
}
2402
2403
static inline BOOL
2404
rfx_write_progressive_frame_end(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2405
                                wStream* WINPR_RESTRICT s)
2406
0
{
2407
0
  const UINT32 blockLen = 6;
2408
0
  WINPR_ASSERT(rfx);
2409
0
  WINPR_ASSERT(s);
2410
2411
0
  if (!Stream_EnsureRemainingCapacity(s, blockLen))
2412
0
    return FALSE;
2413
2414
0
  Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_END); /* blockType (2 bytes) */
2415
0
  Stream_Write_UINT32(s, blockLen);                  /* blockLen (4 bytes) */
2416
2417
0
  return TRUE;
2418
0
}
2419
2420
static inline BOOL
2421
rfx_write_progressive_tile_simple(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx,
2422
                                  wStream* WINPR_RESTRICT s, const RFX_TILE* WINPR_RESTRICT tile)
2423
0
{
2424
0
  UINT32 blockLen = 0;
2425
0
  WINPR_ASSERT(rfx);
2426
0
  WINPR_ASSERT(s);
2427
0
  WINPR_ASSERT(tile);
2428
2429
0
  blockLen = 22 + tile->YLen + tile->CbLen + tile->CrLen;
2430
0
  if (!Stream_EnsureRemainingCapacity(s, blockLen))
2431
0
    return FALSE;
2432
2433
0
  Stream_Write_UINT16(s, PROGRESSIVE_WBT_TILE_SIMPLE); /* blockType (2 bytes) */
2434
0
  Stream_Write_UINT32(s, blockLen);                    /* blockLen (4 bytes) */
2435
0
  Stream_Write_UINT8(s, tile->quantIdxY);              /* quantIdxY (1 byte) */
2436
0
  Stream_Write_UINT8(s, tile->quantIdxCb);             /* quantIdxCb (1 byte) */
2437
0
  Stream_Write_UINT8(s, tile->quantIdxCr);             /* quantIdxCr (1 byte) */
2438
0
  Stream_Write_UINT16(s, tile->xIdx);                  /* xIdx (2 bytes) */
2439
0
  Stream_Write_UINT16(s, tile->yIdx);                  /* yIdx (2 bytes) */
2440
0
  Stream_Write_UINT8(s, 0);                            /* flags (1 byte) */
2441
0
  Stream_Write_UINT16(s, tile->YLen);                  /* YLen (2 bytes) */
2442
0
  Stream_Write_UINT16(s, tile->CbLen);                 /* CbLen (2 bytes) */
2443
0
  Stream_Write_UINT16(s, tile->CrLen);                 /* CrLen (2 bytes) */
2444
0
  Stream_Write_UINT16(s, 0);                           /* tailLen (2 bytes) */
2445
0
  Stream_Write(s, tile->YData, tile->YLen);            /* YData */
2446
0
  Stream_Write(s, tile->CbData, tile->CbLen);          /* CbData */
2447
0
  Stream_Write(s, tile->CrData, tile->CrLen);          /* CrData */
2448
2449
0
  return TRUE;
2450
0
}
2451
2452
const char* rfx_get_progressive_block_type_string(UINT16 blockType)
2453
41
{
2454
41
  switch (blockType)
2455
41
  {
2456
1
    case PROGRESSIVE_WBT_SYNC:
2457
1
      return "PROGRESSIVE_WBT_SYNC";
2458
2459
0
    case PROGRESSIVE_WBT_FRAME_BEGIN:
2460
0
      return "PROGRESSIVE_WBT_FRAME_BEGIN";
2461
2462
1
    case PROGRESSIVE_WBT_FRAME_END:
2463
1
      return "PROGRESSIVE_WBT_FRAME_END";
2464
2465
1
    case PROGRESSIVE_WBT_CONTEXT:
2466
1
      return "PROGRESSIVE_WBT_CONTEXT";
2467
2468
0
    case PROGRESSIVE_WBT_REGION:
2469
0
      return "PROGRESSIVE_WBT_REGION";
2470
2471
0
    case PROGRESSIVE_WBT_TILE_SIMPLE:
2472
0
      return "PROGRESSIVE_WBT_TILE_SIMPLE";
2473
2474
0
    case PROGRESSIVE_WBT_TILE_FIRST:
2475
0
      return "PROGRESSIVE_WBT_TILE_FIRST";
2476
2477
0
    case PROGRESSIVE_WBT_TILE_UPGRADE:
2478
0
      return "PROGRESSIVE_WBT_TILE_UPGRADE";
2479
2480
38
    default:
2481
38
      return "PROGRESSIVE_WBT_UNKNOWN";
2482
41
  }
2483
41
}
2484
2485
BOOL rfx_write_message_progressive_simple(RFX_CONTEXT* WINPR_RESTRICT rfx,
2486
                                          wStream* WINPR_RESTRICT s,
2487
                                          const RFX_MESSAGE* WINPR_RESTRICT msg)
2488
0
{
2489
0
  WINPR_ASSERT(s);
2490
0
  WINPR_ASSERT(msg);
2491
0
  WINPR_ASSERT(rfx);
2492
2493
0
  if (rfx->mode != RLGR1)
2494
0
  {
2495
0
    WLog_ERR(TAG, "error, RLGR1 mode is required!");
2496
0
    return FALSE;
2497
0
  }
2498
2499
0
  if (!rfx_write_progressive_wb_sync(rfx, s))
2500
0
    return FALSE;
2501
2502
0
  if (!rfx_write_progressive_wb_context(rfx, s))
2503
0
    return FALSE;
2504
2505
0
  if (!rfx_write_progressive_frame_begin(rfx, s, msg))
2506
0
    return FALSE;
2507
2508
0
  if (!rfx_write_progressive_region(rfx, s, msg))
2509
0
    return FALSE;
2510
2511
0
  if (!rfx_write_progressive_frame_end(rfx, s))
2512
0
    return FALSE;
2513
2514
0
  return TRUE;
2515
0
}