Coverage Report

Created: 2026-02-26 06:54

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