Coverage Report

Created: 2026-10-15 06:26

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