Coverage Report

Created: 2023-09-25 06:56

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