Coverage Report

Created: 2025-10-10 06:50

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