Coverage Report

Created: 2026-03-04 06:14

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