Coverage Report

Created: 2026-01-09 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/codec/progressive.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Progressive Codec Bitmap Compression
4
 *
5
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2019 Armin Novak <armin.novak@thincast.com>
7
 * Copyright 2019 Thincast Technologies GmbH
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 <winpr/cast.h>
26
#include <winpr/crt.h>
27
#include <winpr/print.h>
28
#include <winpr/bitstream.h>
29
30
#include <freerdp/primitives.h>
31
#include <freerdp/codec/color.h>
32
#include <freerdp/codec/progressive.h>
33
#include <freerdp/codec/region.h>
34
#include <freerdp/log.h>
35
36
#include "rfx_differential.h"
37
#include "rfx_quantization.h"
38
#include "rfx_dwt.h"
39
#include "rfx_rlgr.h"
40
#include "rfx_constants.h"
41
#include "rfx_types.h"
42
#include "progressive.h"
43
44
6.00k
#define TAG FREERDP_TAG("codec.progressive")
45
46
typedef struct
47
{
48
  BOOL nonLL;
49
  wBitStream* srl;
50
  wBitStream* raw;
51
52
  /* SRL state */
53
54
  UINT32 kp;
55
  int nz;
56
  BOOL mode;
57
} RFX_PROGRESSIVE_UPGRADE_STATE;
58
59
static inline void
60
progressive_component_codec_quant_read(wStream* WINPR_RESTRICT s,
61
                                       RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT quantVal)
62
3.38k
{
63
3.38k
  BYTE b = 0;
64
3.38k
  Stream_Read_UINT8(s, b);
65
3.38k
  quantVal->LL3 = b & 0x0F;
66
3.38k
  quantVal->HL3 = b >> 4;
67
3.38k
  Stream_Read_UINT8(s, b);
68
3.38k
  quantVal->LH3 = b & 0x0F;
69
3.38k
  quantVal->HH3 = b >> 4;
70
3.38k
  Stream_Read_UINT8(s, b);
71
3.38k
  quantVal->HL2 = b & 0x0F;
72
3.38k
  quantVal->LH2 = b >> 4;
73
3.38k
  Stream_Read_UINT8(s, b);
74
3.38k
  quantVal->HH2 = b & 0x0F;
75
3.38k
  quantVal->HL1 = b >> 4;
76
3.38k
  Stream_Read_UINT8(s, b);
77
3.38k
  quantVal->LH1 = b & 0x0F;
78
3.38k
  quantVal->HH1 = b >> 4;
79
3.38k
}
80
81
static inline void progressive_rfx_quant_add(const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q1,
82
                                             const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q2,
83
                                             RFX_COMPONENT_CODEC_QUANT* dst)
84
0
{
85
0
  dst->HL1 = q1->HL1 + q2->HL1; /* HL1 */
86
0
  dst->LH1 = q1->LH1 + q2->LH1; /* LH1 */
87
0
  dst->HH1 = q1->HH1 + q2->HH1; /* HH1 */
88
0
  dst->HL2 = q1->HL2 + q2->HL2; /* HL2 */
89
0
  dst->LH2 = q1->LH2 + q2->LH2; /* LH2 */
90
0
  dst->HH2 = q1->HH2 + q2->HH2; /* HH2 */
91
0
  dst->HL3 = q1->HL3 + q2->HL3; /* HL3 */
92
0
  dst->LH3 = q1->LH3 + q2->LH3; /* LH3 */
93
0
  dst->HH3 = q1->HH3 + q2->HH3; /* HH3 */
94
0
  dst->LL3 = q1->LL3 + q2->LL3; /* LL3 */
95
0
}
96
97
static inline void progressive_rfx_quant_lsub(RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q, int val)
98
0
{
99
0
  q->HL1 -= val; /* HL1 */
100
0
  q->LH1 -= val; /* LH1 */
101
0
  q->HH1 -= val; /* HH1 */
102
0
  q->HL2 -= val; /* HL2 */
103
0
  q->LH2 -= val; /* LH2 */
104
0
  q->HH2 -= val; /* HH2 */
105
0
  q->HL3 -= val; /* HL3 */
106
0
  q->LH3 -= val; /* LH3 */
107
0
  q->HH3 -= val; /* HH3 */
108
0
  q->LL3 -= val; /* LL3 */
109
0
}
110
111
static inline void progressive_rfx_quant_sub(const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q1,
112
                                             const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q2,
113
                                             RFX_COMPONENT_CODEC_QUANT* dst)
114
0
{
115
0
  dst->HL1 = q1->HL1 - q2->HL1; /* HL1 */
116
0
  dst->LH1 = q1->LH1 - q2->LH1; /* LH1 */
117
0
  dst->HH1 = q1->HH1 - q2->HH1; /* HH1 */
118
0
  dst->HL2 = q1->HL2 - q2->HL2; /* HL2 */
119
0
  dst->LH2 = q1->LH2 - q2->LH2; /* LH2 */
120
0
  dst->HH2 = q1->HH2 - q2->HH2; /* HH2 */
121
0
  dst->HL3 = q1->HL3 - q2->HL3; /* HL3 */
122
0
  dst->LH3 = q1->LH3 - q2->LH3; /* LH3 */
123
0
  dst->HH3 = q1->HH3 - q2->HH3; /* HH3 */
124
0
  dst->LL3 = q1->LL3 - q2->LL3; /* LL3 */
125
0
}
126
127
static inline BOOL
128
progressive_rfx_quant_lcmp_less_equal(const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q, int val)
129
60
{
130
60
  if (q->HL1 > val)
131
0
    return FALSE; /* HL1 */
132
133
60
  if (q->LH1 > val)
134
0
    return FALSE; /* LH1 */
135
136
60
  if (q->HH1 > val)
137
0
    return FALSE; /* HH1 */
138
139
60
  if (q->HL2 > val)
140
0
    return FALSE; /* HL2 */
141
142
60
  if (q->LH2 > val)
143
0
    return FALSE; /* LH2 */
144
145
60
  if (q->HH2 > val)
146
0
    return FALSE; /* HH2 */
147
148
60
  if (q->HL3 > val)
149
0
    return FALSE; /* HL3 */
150
151
60
  if (q->LH3 > val)
152
0
    return FALSE; /* LH3 */
153
154
60
  if (q->HH3 > val)
155
0
    return FALSE; /* HH3 */
156
157
60
  if (q->LL3 > val)
158
0
    return FALSE; /* LL3 */
159
160
60
  return TRUE;
161
60
}
162
163
static inline BOOL
164
progressive_rfx_quant_lcmp_greater_equal(const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q, int val)
165
86
{
166
86
  if (q->HL1 < val)
167
7
    return FALSE; /* HL1 */
168
169
79
  if (q->LH1 < val)
170
3
    return FALSE; /* LH1 */
171
172
76
  if (q->HH1 < val)
173
2
    return FALSE; /* HH1 */
174
175
74
  if (q->HL2 < val)
176
2
    return FALSE; /* HL2 */
177
178
72
  if (q->LH2 < val)
179
2
    return FALSE; /* LH2 */
180
181
70
  if (q->HH2 < val)
182
1
    return FALSE; /* HH2 */
183
184
69
  if (q->HL3 < val)
185
3
    return FALSE; /* HL3 */
186
187
66
  if (q->LH3 < val)
188
2
    return FALSE; /* LH3 */
189
190
64
  if (q->HH3 < val)
191
2
    return FALSE; /* HH3 */
192
193
62
  if (q->LL3 < val)
194
2
    return FALSE; /* LL3 */
195
196
60
  return TRUE;
197
62
}
198
199
static inline BOOL
200
progressive_rfx_quant_cmp_equal(const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q1,
201
                                const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q2)
202
0
{
203
0
  if (q1->HL1 != q2->HL1)
204
0
    return FALSE; /* HL1 */
205
206
0
  if (q1->LH1 != q2->LH1)
207
0
    return FALSE; /* LH1 */
208
209
0
  if (q1->HH1 != q2->HH1)
210
0
    return FALSE; /* HH1 */
211
212
0
  if (q1->HL2 != q2->HL2)
213
0
    return FALSE; /* HL2 */
214
215
0
  if (q1->LH2 != q2->LH2)
216
0
    return FALSE; /* LH2 */
217
218
0
  if (q1->HH2 != q2->HH2)
219
0
    return FALSE; /* HH2 */
220
221
0
  if (q1->HL3 != q2->HL3)
222
0
    return FALSE; /* HL3 */
223
224
0
  if (q1->LH3 != q2->LH3)
225
0
    return FALSE; /* LH3 */
226
227
0
  if (q1->HH3 != q2->HH3)
228
0
    return FALSE; /* HH3 */
229
230
0
  if (q1->LL3 != q2->LL3)
231
0
    return FALSE; /* LL3 */
232
233
0
  return TRUE;
234
0
}
235
236
static inline BOOL progressive_set_surface_data(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
237
                                                UINT16 surfaceId,
238
                                                PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT pData)
239
6.00k
{
240
6.00k
  ULONG_PTR key = 0;
241
6.00k
  key = ((ULONG_PTR)surfaceId) + 1;
242
243
6.00k
  if (pData)
244
6.00k
    return HashTable_Insert(progressive->SurfaceContexts, (void*)key, pData);
245
246
0
  HashTable_Remove(progressive->SurfaceContexts, (void*)key);
247
0
  return TRUE;
248
6.00k
}
249
250
static inline PROGRESSIVE_SURFACE_CONTEXT*
251
progressive_get_surface_data(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, UINT16 surfaceId)
252
12.0k
{
253
12.0k
  void* key = (void*)(((ULONG_PTR)surfaceId) + 1);
254
255
12.0k
  if (!progressive)
256
0
    return NULL;
257
258
12.0k
  return HashTable_GetItemValue(progressive->SurfaceContexts, key);
259
12.0k
}
260
261
static void progressive_tile_free(RFX_PROGRESSIVE_TILE* WINPR_RESTRICT tile)
262
1.32M
{
263
1.32M
  if (tile)
264
1.32M
  {
265
1.32M
    winpr_aligned_free(tile->sign);
266
1.32M
    winpr_aligned_free(tile->current);
267
1.32M
    winpr_aligned_free(tile->data);
268
1.32M
    winpr_aligned_free(tile);
269
1.32M
  }
270
1.32M
}
271
272
static void progressive_surface_context_free(void* ptr)
273
12.0k
{
274
12.0k
  PROGRESSIVE_SURFACE_CONTEXT* surface = ptr;
275
276
12.0k
  if (!surface)
277
6.00k
    return;
278
279
6.00k
  if (surface->tiles)
280
6.00k
  {
281
1.33M
    for (size_t index = 0; index < surface->tilesSize; index++)
282
1.32M
    {
283
1.32M
      RFX_PROGRESSIVE_TILE* tile = surface->tiles[index];
284
1.32M
      progressive_tile_free(tile);
285
1.32M
    }
286
6.00k
  }
287
288
6.00k
  winpr_aligned_free((void*)surface->tiles);
289
6.00k
  winpr_aligned_free(surface->updatedTileIndices);
290
6.00k
  winpr_aligned_free(surface);
291
6.00k
}
292
293
static inline RFX_PROGRESSIVE_TILE* progressive_tile_new(void)
294
1.32M
{
295
1.32M
  RFX_PROGRESSIVE_TILE* tile = winpr_aligned_calloc(1, sizeof(RFX_PROGRESSIVE_TILE), 32);
296
1.32M
  if (!tile)
297
0
    goto fail;
298
299
1.32M
  tile->width = 64;
300
1.32M
  tile->height = 64;
301
1.32M
  tile->stride = 4 * tile->width;
302
303
1.32M
  {
304
1.32M
    const size_t dataLen = 1ull * tile->stride * tile->height;
305
1.32M
    tile->data = (BYTE*)winpr_aligned_malloc(dataLen, 16);
306
1.32M
    if (!tile->data)
307
0
      goto fail;
308
1.32M
    memset(tile->data, 0xFF, dataLen);
309
1.32M
  }
310
311
0
  {
312
1.32M
    const size_t signLen = (8192ULL + 32ULL) * 3ULL;
313
1.32M
    tile->sign = (BYTE*)winpr_aligned_malloc(signLen, 16);
314
1.32M
  }
315
316
1.32M
  if (!tile->sign)
317
0
    goto fail;
318
319
1.32M
  {
320
1.32M
    const size_t currentLen = (8192ULL + 32ULL) * 3ULL;
321
1.32M
    tile->current = (BYTE*)winpr_aligned_malloc(currentLen, 16);
322
1.32M
  }
323
1.32M
  if (!tile->current)
324
0
    goto fail;
325
326
1.32M
  return tile;
327
328
0
fail:
329
0
  progressive_tile_free(tile);
330
0
  return NULL;
331
1.32M
}
332
333
static inline BOOL
334
progressive_allocate_tile_cache(PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface, size_t min)
335
6.00k
{
336
6.00k
  size_t oldIndex = 0;
337
338
6.00k
  WINPR_ASSERT(surface);
339
6.00k
  WINPR_ASSERT(surface->gridSize > 0);
340
341
6.00k
  if (surface->tiles)
342
0
  {
343
0
    oldIndex = surface->gridSize;
344
0
    while (surface->gridSize < min)
345
0
      surface->gridSize += 1024;
346
0
  }
347
348
6.00k
  void* tmp = winpr_aligned_recalloc((void*)surface->tiles, surface->gridSize,
349
6.00k
                                     sizeof(RFX_PROGRESSIVE_TILE*), 32);
350
6.00k
  if (!tmp)
351
0
    return FALSE;
352
6.00k
  surface->tilesSize = surface->gridSize;
353
6.00k
  surface->tiles = (RFX_PROGRESSIVE_TILE**)tmp;
354
355
1.33M
  for (size_t x = oldIndex; x < surface->tilesSize; x++)
356
1.32M
  {
357
1.32M
    surface->tiles[x] = progressive_tile_new();
358
1.32M
    if (!surface->tiles[x])
359
0
      return FALSE;
360
1.32M
  }
361
362
6.00k
  tmp =
363
6.00k
      winpr_aligned_recalloc(surface->updatedTileIndices, surface->gridSize, sizeof(UINT32), 32);
364
6.00k
  if (!tmp)
365
0
    return FALSE;
366
367
6.00k
  surface->updatedTileIndices = tmp;
368
369
6.00k
  return TRUE;
370
6.00k
}
371
372
static PROGRESSIVE_SURFACE_CONTEXT* progressive_surface_context_new(UINT16 surfaceId, UINT32 width,
373
                                                                    UINT32 height)
374
6.00k
{
375
6.00k
  PROGRESSIVE_SURFACE_CONTEXT* surface = (PROGRESSIVE_SURFACE_CONTEXT*)winpr_aligned_calloc(
376
6.00k
      1, sizeof(PROGRESSIVE_SURFACE_CONTEXT), 32);
377
378
6.00k
  if (!surface)
379
0
    return NULL;
380
381
6.00k
  surface->id = surfaceId;
382
6.00k
  surface->width = width;
383
6.00k
  surface->height = height;
384
6.00k
  surface->gridWidth = (width + (64 - width % 64)) / 64;
385
6.00k
  surface->gridHeight = (height + (64 - height % 64)) / 64;
386
6.00k
  surface->gridSize = surface->gridWidth * surface->gridHeight;
387
388
6.00k
  if (!progressive_allocate_tile_cache(surface, surface->gridSize))
389
0
  {
390
0
    progressive_surface_context_free(surface);
391
0
    return NULL;
392
0
  }
393
394
6.00k
  return surface;
395
6.00k
}
396
397
static inline BOOL
398
progressive_surface_tile_replace(PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
399
                                 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
400
                                 const RFX_PROGRESSIVE_TILE* WINPR_RESTRICT tile, BOOL upgrade)
401
98
{
402
98
  RFX_PROGRESSIVE_TILE* t = NULL;
403
404
98
  size_t zIdx = 0;
405
98
  if (!surface || !tile)
406
0
    return FALSE;
407
408
98
  zIdx = (tile->yIdx * surface->gridWidth) + tile->xIdx;
409
410
98
  if (zIdx >= surface->tilesSize)
411
14
  {
412
14
    WLog_ERR(TAG, "Invalid zIndex %" PRIuz, zIdx);
413
14
    return FALSE;
414
14
  }
415
416
84
  t = surface->tiles[zIdx];
417
418
84
  t->blockType = tile->blockType;
419
84
  t->blockLen = tile->blockLen;
420
84
  t->quantIdxY = tile->quantIdxY;
421
84
  t->quantIdxCb = tile->quantIdxCb;
422
84
  t->quantIdxCr = tile->quantIdxCr;
423
84
  t->xIdx = tile->xIdx;
424
84
  t->yIdx = tile->yIdx;
425
84
  t->flags = tile->flags;
426
84
  t->quality = tile->quality;
427
84
  t->x = tile->xIdx * t->width;
428
84
  t->y = tile->yIdx * t->height;
429
430
84
  if (upgrade)
431
44
  {
432
44
    t->ySrlLen = tile->ySrlLen;
433
44
    t->yRawLen = tile->yRawLen;
434
44
    t->cbSrlLen = tile->cbSrlLen;
435
44
    t->cbRawLen = tile->cbRawLen;
436
44
    t->crSrlLen = tile->crSrlLen;
437
44
    t->crRawLen = tile->crRawLen;
438
44
    t->ySrlData = tile->ySrlData;
439
44
    t->yRawData = tile->yRawData;
440
44
    t->cbSrlData = tile->cbSrlData;
441
44
    t->cbRawData = tile->cbRawData;
442
44
    t->crSrlData = tile->crSrlData;
443
44
    t->crRawData = tile->crRawData;
444
44
  }
445
40
  else
446
40
  {
447
40
    t->yLen = tile->yLen;
448
40
    t->cbLen = tile->cbLen;
449
40
    t->crLen = tile->crLen;
450
40
    t->tailLen = tile->tailLen;
451
40
    t->yData = tile->yData;
452
40
    t->cbData = tile->cbData;
453
40
    t->crData = tile->crData;
454
40
    t->tailData = tile->tailData;
455
40
  }
456
457
84
  if (region->usedTiles >= region->numTiles)
458
3
  {
459
3
    WLog_ERR(TAG, "Invalid tile count, only expected %" PRIu16 ", got %" PRIu16,
460
3
             region->numTiles, region->usedTiles);
461
3
    return FALSE;
462
3
  }
463
464
81
  region->tiles[region->usedTiles++] = t;
465
81
  if (!t->dirty)
466
59
  {
467
59
    if (surface->numUpdatedTiles >= surface->gridSize)
468
0
    {
469
0
      if (!progressive_allocate_tile_cache(surface, surface->numUpdatedTiles + 1))
470
0
        return FALSE;
471
0
    }
472
473
59
    surface->updatedTileIndices[surface->numUpdatedTiles++] = (UINT32)zIdx;
474
59
  }
475
476
81
  t->dirty = TRUE;
477
81
  return TRUE;
478
81
}
479
480
INT32 progressive_create_surface_context(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
481
                                         UINT16 surfaceId, UINT32 width, UINT32 height)
482
6.00k
{
483
6.00k
  PROGRESSIVE_SURFACE_CONTEXT* surface = progressive_get_surface_data(progressive, surfaceId);
484
485
6.00k
  if (!surface)
486
6.00k
  {
487
6.00k
    surface = progressive_surface_context_new(surfaceId, width, height);
488
489
6.00k
    if (!surface)
490
0
      return -1;
491
492
6.00k
    if (!progressive_set_surface_data(progressive, surfaceId, (void*)surface))
493
0
    {
494
0
      progressive_surface_context_free(surface);
495
0
      return -1;
496
0
    }
497
6.00k
  }
498
499
6.00k
  return 1;
500
6.00k
}
501
502
int progressive_delete_surface_context(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
503
                                       UINT16 surfaceId)
504
0
{
505
0
  progressive_set_surface_data(progressive, surfaceId, NULL);
506
507
0
  return 1;
508
0
}
509
510
/*
511
 * Band     Offset      Dimensions  Size
512
 *
513
 * HL1      0           31x33       1023
514
 * LH1      1023        33x31       1023
515
 * HH1      2046        31x31       961
516
 *
517
 * HL2      3007        16x17       272
518
 * LH2      3279        17x16       272
519
 * HH2      3551        16x16       256
520
 *
521
 * HL3      3807        8x9         72
522
 * LH3      3879        9x8         72
523
 * HH3      3951        8x8         64
524
 *
525
 * LL3      4015        9x9         81
526
 */
527
528
static int16_t clampi16(int val)
529
0
{
530
0
  if (val < INT16_MIN)
531
0
    return INT16_MIN;
532
0
  if (val > INT16_MAX)
533
0
    return INT16_MAX;
534
0
  return (int16_t)val;
535
0
}
536
537
static inline void progressive_rfx_idwt_x(const INT16* WINPR_RESTRICT pLowBand, size_t nLowStep,
538
                                          const INT16* WINPR_RESTRICT pHighBand, size_t nHighStep,
539
                                          INT16* WINPR_RESTRICT pDstBand, size_t nDstStep,
540
                                          size_t nLowCount, size_t nHighCount, size_t nDstCount)
541
0
{
542
0
  INT16 H1 = 0;
543
0
  INT16 X1 = 0;
544
545
0
  for (size_t i = 0; i < nDstCount; i++)
546
0
  {
547
0
    const INT16* pL = pLowBand;
548
0
    const INT16* pH = pHighBand;
549
0
    INT16* pX = pDstBand;
550
0
    INT16 H0 = *pH++;
551
0
    INT16 L0 = *pL++;
552
0
    INT16 X0 = clampi16((int32_t)L0 - H0);
553
0
    INT16 X2 = clampi16((int32_t)L0 - H0);
554
555
0
    for (size_t j = 0; j < (nHighCount - 1); j++)
556
0
    {
557
0
      H1 = *pH;
558
0
      pH++;
559
0
      L0 = *pL;
560
0
      pL++;
561
0
      X2 = clampi16((int32_t)L0 - ((H0 + H1) / 2));
562
0
      X1 = clampi16((int32_t)((X0 + X2) / 2) + (2 * H0));
563
0
      pX[0] = X0;
564
0
      pX[1] = X1;
565
0
      pX += 2;
566
0
      X0 = X2;
567
0
      H0 = H1;
568
0
    }
569
570
0
    if (nLowCount <= (nHighCount + 1))
571
0
    {
572
0
      if (nLowCount <= nHighCount)
573
0
      {
574
0
        pX[0] = X2;
575
0
        pX[1] = clampi16((int32_t)X2 + (2 * H0));
576
0
      }
577
0
      else
578
0
      {
579
0
        L0 = *pL;
580
0
        pL++;
581
0
        X0 = clampi16((int32_t)L0 - H0);
582
0
        pX[0] = X2;
583
0
        pX[1] = clampi16((int32_t)((X0 + X2) / 2) + (2 * H0));
584
0
        pX[2] = X0;
585
0
      }
586
0
    }
587
0
    else
588
0
    {
589
0
      L0 = *pL;
590
0
      pL++;
591
0
      X0 = clampi16((int32_t)L0 - (H0 / 2));
592
0
      pX[0] = X2;
593
0
      pX[1] = clampi16((int32_t)((X0 + X2) / 2) + (2 * H0));
594
0
      pX[2] = X0;
595
0
      L0 = *pL;
596
0
      pL++;
597
0
      pX[3] = clampi16((int32_t)(X0 + L0) / 2);
598
0
    }
599
600
0
    pLowBand += nLowStep;
601
0
    pHighBand += nHighStep;
602
0
    pDstBand += nDstStep;
603
0
  }
604
0
}
605
606
static inline void progressive_rfx_idwt_y(const INT16* WINPR_RESTRICT pLowBand, size_t nLowStep,
607
                                          const INT16* WINPR_RESTRICT pHighBand, size_t nHighStep,
608
                                          INT16* WINPR_RESTRICT pDstBand, size_t nDstStep,
609
                                          size_t nLowCount, size_t nHighCount, size_t nDstCount)
610
0
{
611
0
  for (size_t i = 0; i < nDstCount; i++)
612
0
  {
613
0
    INT16 H1 = 0;
614
0
    INT16 X1 = 0;
615
0
    const INT16* pL = pLowBand;
616
0
    const INT16* pH = pHighBand;
617
0
    INT16* pX = pDstBand;
618
0
    INT16 H0 = *pH;
619
0
    pH += nHighStep;
620
0
    INT16 L0 = *pL;
621
0
    pL += nLowStep;
622
0
    int16_t X0 = clampi16((int32_t)L0 - H0);
623
0
    int16_t X2 = clampi16((int32_t)L0 - H0);
624
625
0
    for (size_t j = 0; j < (nHighCount - 1); j++)
626
0
    {
627
0
      H1 = *pH;
628
0
      pH += nHighStep;
629
0
      L0 = *pL;
630
0
      pL += nLowStep;
631
0
      X2 = clampi16((int32_t)L0 - ((H0 + H1) / 2));
632
0
      X1 = clampi16((int32_t)((X0 + X2) / 2) + (2 * H0));
633
0
      *pX = X0;
634
0
      pX += nDstStep;
635
0
      *pX = X1;
636
0
      pX += nDstStep;
637
0
      X0 = X2;
638
0
      H0 = H1;
639
0
    }
640
641
0
    if (nLowCount <= (nHighCount + 1))
642
0
    {
643
0
      if (nLowCount <= nHighCount)
644
0
      {
645
0
        *pX = X2;
646
0
        pX += nDstStep;
647
0
        *pX = clampi16((int32_t)X2 + (2 * H0));
648
0
      }
649
0
      else
650
0
      {
651
0
        L0 = *pL;
652
0
        X0 = clampi16((int32_t)L0 - H0);
653
0
        *pX = X2;
654
0
        pX += nDstStep;
655
0
        *pX = clampi16((int32_t)((X0 + X2) / 2) + (2 * H0));
656
0
        pX += nDstStep;
657
0
        *pX = X0;
658
0
      }
659
0
    }
660
0
    else
661
0
    {
662
0
      L0 = *pL;
663
0
      pL += nLowStep;
664
0
      X0 = clampi16((int32_t)L0 - (H0 / 2));
665
0
      *pX = X2;
666
0
      pX += nDstStep;
667
0
      *pX = clampi16((int32_t)((X0 + X2) / 2) + (2 * H0));
668
0
      pX += nDstStep;
669
0
      *pX = X0;
670
0
      pX += nDstStep;
671
0
      L0 = *pL;
672
0
      *pX = clampi16((int32_t)(X0 + L0) / 2);
673
0
    }
674
675
0
    pLowBand++;
676
0
    pHighBand++;
677
0
    pDstBand++;
678
0
  }
679
0
}
680
681
static inline size_t progressive_rfx_get_band_l_count(size_t level)
682
0
{
683
0
  return (64 >> level) + 1;
684
0
}
685
686
static inline size_t progressive_rfx_get_band_h_count(size_t level)
687
0
{
688
0
  if (level == 1)
689
0
    return (64 >> 1) - 1;
690
0
  else
691
0
    return (64 + (1 << (level - 1))) >> level;
692
0
}
693
694
static inline void progressive_rfx_dwt_2d_decode_block(INT16* WINPR_RESTRICT buffer,
695
                                                       INT16* WINPR_RESTRICT temp, size_t level)
696
0
{
697
0
  size_t nDstStepX = 0;
698
0
  size_t nDstStepY = 0;
699
0
  const INT16* WINPR_RESTRICT HL = NULL;
700
0
  const INT16* WINPR_RESTRICT LH = NULL;
701
0
  const INT16* WINPR_RESTRICT HH = NULL;
702
0
  INT16* WINPR_RESTRICT LL = NULL;
703
0
  INT16* WINPR_RESTRICT L = NULL;
704
0
  INT16* WINPR_RESTRICT H = NULL;
705
0
  INT16* WINPR_RESTRICT LLx = NULL;
706
707
0
  const size_t nBandL = progressive_rfx_get_band_l_count(level);
708
0
  const size_t nBandH = progressive_rfx_get_band_h_count(level);
709
0
  size_t offset = 0;
710
711
0
  HL = &buffer[offset];
712
0
  offset += (nBandH * nBandL);
713
0
  LH = &buffer[offset];
714
0
  offset += (nBandL * nBandH);
715
0
  HH = &buffer[offset];
716
0
  offset += (nBandH * nBandH);
717
0
  LL = &buffer[offset];
718
0
  nDstStepX = (nBandL + nBandH);
719
0
  nDstStepY = (nBandL + nBandH);
720
0
  offset = 0;
721
0
  L = &temp[offset];
722
0
  offset += (nBandL * nDstStepX);
723
0
  H = &temp[offset];
724
0
  LLx = &buffer[0];
725
726
  /* horizontal (LL + HL -> L) */
727
0
  progressive_rfx_idwt_x(LL, nBandL, HL, nBandH, L, nDstStepX, nBandL, nBandH, nBandL);
728
729
  /* horizontal (LH + HH -> H) */
730
0
  progressive_rfx_idwt_x(LH, nBandL, HH, nBandH, H, nDstStepX, nBandL, nBandH, nBandH);
731
732
  /* vertical (L + H -> LL) */
733
0
  progressive_rfx_idwt_y(L, nDstStepX, H, nDstStepX, LLx, nDstStepY, nBandL, nBandH,
734
0
                         nBandL + nBandH);
735
0
}
736
737
void rfx_dwt_2d_extrapolate_decode(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT temp)
738
0
{
739
0
  WINPR_ASSERT(buffer);
740
0
  WINPR_ASSERT(temp);
741
0
  progressive_rfx_dwt_2d_decode_block(&buffer[3807], temp, 3);
742
0
  progressive_rfx_dwt_2d_decode_block(&buffer[3007], temp, 2);
743
0
  progressive_rfx_dwt_2d_decode_block(&buffer[0], temp, 1);
744
0
}
745
746
static inline int progressive_rfx_dwt_2d_decode(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
747
                                                INT16* WINPR_RESTRICT buffer,
748
                                                INT16* WINPR_RESTRICT current, BOOL coeffDiff,
749
                                                BOOL extrapolate, BOOL reverse)
750
0
{
751
0
  const primitives_t* prims = primitives_get();
752
753
0
  if (!progressive || !buffer || !current)
754
0
    return -1;
755
756
0
  const uint32_t belements = 4096;
757
0
  const uint32_t bsize = belements * sizeof(INT16);
758
0
  if (reverse)
759
0
    memcpy(buffer, current, bsize);
760
0
  else if (!coeffDiff)
761
0
    memcpy(current, buffer, bsize);
762
0
  else
763
0
    prims->add_16s_inplace(buffer, current, belements);
764
765
0
  INT16* temp = (INT16*)BufferPool_Take(progressive->bufferPool, -1); /* DWT buffer */
766
767
0
  if (!temp)
768
0
    return -2;
769
770
0
  if (!extrapolate)
771
0
  {
772
0
    progressive->rfx_context->dwt_2d_decode(buffer, temp);
773
0
  }
774
0
  else
775
0
  {
776
0
    WINPR_ASSERT(progressive->rfx_context->dwt_2d_extrapolate_decode);
777
0
    progressive->rfx_context->dwt_2d_extrapolate_decode(buffer, temp);
778
0
  }
779
0
  BufferPool_Return(progressive->bufferPool, temp);
780
0
  return 1;
781
0
}
782
783
static inline void progressive_rfx_decode_block(const primitives_t* prims,
784
                                                INT16* WINPR_RESTRICT buffer, UINT32 length,
785
                                                UINT32 shift)
786
0
{
787
0
  if (!shift)
788
0
    return;
789
790
0
  prims->lShiftC_16s_inplace(buffer, shift, length);
791
0
}
792
793
static inline int
794
progressive_rfx_decode_component(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
795
                                 const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT shift,
796
                                 const BYTE* WINPR_RESTRICT data, UINT32 length,
797
                                 INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT current,
798
                                 INT16* WINPR_RESTRICT sign, BOOL coeffDiff,
799
                                 WINPR_ATTR_UNUSED BOOL subbandDiff, BOOL extrapolate)
800
0
{
801
0
  int status = 0;
802
0
  const primitives_t* prims = primitives_get();
803
804
0
  status = progressive->rfx_context->rlgr_decode(RLGR1, data, length, buffer, 4096);
805
806
0
  if (status < 0)
807
0
    return status;
808
809
0
  CopyMemory(sign, buffer, 4096ULL * 2ULL);
810
0
  if (!extrapolate)
811
0
  {
812
0
    rfx_differential_decode(buffer + 4032, 64);
813
0
    progressive_rfx_decode_block(prims, &buffer[0], 1024, shift->HL1);    /* HL1 */
814
0
    progressive_rfx_decode_block(prims, &buffer[1024], 1024, shift->LH1); /* LH1 */
815
0
    progressive_rfx_decode_block(prims, &buffer[2048], 1024, shift->HH1); /* HH1 */
816
0
    progressive_rfx_decode_block(prims, &buffer[3072], 256, shift->HL2);  /* HL2 */
817
0
    progressive_rfx_decode_block(prims, &buffer[3328], 256, shift->LH2);  /* LH2 */
818
0
    progressive_rfx_decode_block(prims, &buffer[3584], 256, shift->HH2);  /* HH2 */
819
0
    progressive_rfx_decode_block(prims, &buffer[3840], 64, shift->HL3);   /* HL3 */
820
0
    progressive_rfx_decode_block(prims, &buffer[3904], 64, shift->LH3);   /* LH3 */
821
0
    progressive_rfx_decode_block(prims, &buffer[3968], 64, shift->HH3);   /* HH3 */
822
0
    progressive_rfx_decode_block(prims, &buffer[4032], 64, shift->LL3);   /* LL3 */
823
0
  }
824
0
  else
825
0
  {
826
0
    progressive_rfx_decode_block(prims, &buffer[0], 1023, shift->HL1);    /* HL1 */
827
0
    progressive_rfx_decode_block(prims, &buffer[1023], 1023, shift->LH1); /* LH1 */
828
0
    progressive_rfx_decode_block(prims, &buffer[2046], 961, shift->HH1);  /* HH1 */
829
0
    progressive_rfx_decode_block(prims, &buffer[3007], 272, shift->HL2);  /* HL2 */
830
0
    progressive_rfx_decode_block(prims, &buffer[3279], 272, shift->LH2);  /* LH2 */
831
0
    progressive_rfx_decode_block(prims, &buffer[3551], 256, shift->HH2);  /* HH2 */
832
0
    progressive_rfx_decode_block(prims, &buffer[3807], 72, shift->HL3);   /* HL3 */
833
0
    progressive_rfx_decode_block(prims, &buffer[3879], 72, shift->LH3);   /* LH3 */
834
0
    progressive_rfx_decode_block(prims, &buffer[3951], 64, shift->HH3);   /* HH3 */
835
0
    rfx_differential_decode(&buffer[4015], 81);                           /* LL3 */
836
0
    progressive_rfx_decode_block(prims, &buffer[4015], 81, shift->LL3);   /* LL3 */
837
0
  }
838
0
  return progressive_rfx_dwt_2d_decode(progressive, buffer, current, coeffDiff, extrapolate,
839
0
                                       FALSE);
840
0
}
841
842
static inline int
843
progressive_decompress_tile_first(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
844
                                  RFX_PROGRESSIVE_TILE* WINPR_RESTRICT tile,
845
                                  PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
846
                                  const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
847
0
{
848
0
  int rc = 0;
849
0
  BOOL diff = 0;
850
0
  BOOL sub = 0;
851
0
  BOOL extrapolate = 0;
852
0
  BYTE* pBuffer = NULL;
853
0
  INT16* pSign[3];
854
0
  INT16* pSrcDst[3];
855
0
  INT16* pCurrent[3];
856
0
  RFX_COMPONENT_CODEC_QUANT shiftY = { 0 };
857
0
  RFX_COMPONENT_CODEC_QUANT shiftCb = { 0 };
858
0
  RFX_COMPONENT_CODEC_QUANT shiftCr = { 0 };
859
0
  RFX_COMPONENT_CODEC_QUANT* quantY = NULL;
860
0
  RFX_COMPONENT_CODEC_QUANT* quantCb = NULL;
861
0
  RFX_COMPONENT_CODEC_QUANT* quantCr = NULL;
862
0
  RFX_COMPONENT_CODEC_QUANT* quantProgY = NULL;
863
0
  RFX_COMPONENT_CODEC_QUANT* quantProgCb = NULL;
864
0
  RFX_COMPONENT_CODEC_QUANT* quantProgCr = NULL;
865
0
  RFX_PROGRESSIVE_CODEC_QUANT* quantProgVal = NULL;
866
0
  static const prim_size_t roi_64x64 = { 64, 64 };
867
0
  const primitives_t* prims = primitives_get();
868
869
0
  tile->pass = 1;
870
0
  diff = tile->flags & RFX_TILE_DIFFERENCE;
871
0
  sub = context->flags & RFX_SUBBAND_DIFFING;
872
0
  extrapolate = region->flags & RFX_DWT_REDUCE_EXTRAPOLATE;
873
874
#if defined(WITH_DEBUG_CODECS)
875
  WLog_Print(progressive->log, WLOG_DEBUG,
876
             "ProgressiveTile%s: quantIdx Y: %" PRIu8 " Cb: %" PRIu8 " Cr: %" PRIu8
877
             " xIdx: %" PRIu16 " yIdx: %" PRIu16 " flags: 0x%02" PRIX8 " quality: %" PRIu8
878
             " yLen: %" PRIu16 " cbLen: %" PRIu16 " crLen: %" PRIu16 " tailLen: %" PRIu16 "",
879
             (tile->blockType == PROGRESSIVE_WBT_TILE_FIRST) ? "First" : "Simple",
880
             tile->quantIdxY, tile->quantIdxCb, tile->quantIdxCr, tile->xIdx, tile->yIdx,
881
             tile->flags, tile->quality, tile->yLen, tile->cbLen, tile->crLen, tile->tailLen);
882
#endif
883
884
0
  if (tile->quantIdxY >= region->numQuant)
885
0
  {
886
0
    WLog_ERR(TAG, "quantIdxY %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxY, region->numQuant);
887
0
    return -1;
888
0
  }
889
890
0
  quantY = &(region->quantVals[tile->quantIdxY]);
891
892
0
  if (tile->quantIdxCb >= region->numQuant)
893
0
  {
894
0
    WLog_ERR(TAG, "quantIdxCb %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCb,
895
0
             region->numQuant);
896
0
    return -1;
897
0
  }
898
899
0
  quantCb = &(region->quantVals[tile->quantIdxCb]);
900
901
0
  if (tile->quantIdxCr >= region->numQuant)
902
0
  {
903
0
    WLog_ERR(TAG, "quantIdxCr %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCr,
904
0
             region->numQuant);
905
0
    return -1;
906
0
  }
907
908
0
  quantCr = &(region->quantVals[tile->quantIdxCr]);
909
910
0
  if (tile->quality == 0xFF)
911
0
  {
912
0
    quantProgVal = &(progressive->quantProgValFull);
913
0
  }
914
0
  else
915
0
  {
916
0
    if (tile->quality >= region->numProgQuant)
917
0
    {
918
0
      WLog_ERR(TAG, "quality %" PRIu8 " > numProgQuant %" PRIu8, tile->quality,
919
0
               region->numProgQuant);
920
0
      return -1;
921
0
    }
922
923
0
    quantProgVal = &(region->quantProgVals[tile->quality]);
924
0
  }
925
926
0
  quantProgY = &(quantProgVal->yQuantValues);
927
0
  quantProgCb = &(quantProgVal->cbQuantValues);
928
0
  quantProgCr = &(quantProgVal->crQuantValues);
929
930
0
  tile->yQuant = *quantY;
931
0
  tile->cbQuant = *quantCb;
932
0
  tile->crQuant = *quantCr;
933
0
  tile->yProgQuant = *quantProgY;
934
0
  tile->cbProgQuant = *quantProgCb;
935
0
  tile->crProgQuant = *quantProgCr;
936
937
0
  progressive_rfx_quant_add(quantY, quantProgY, &(tile->yBitPos));
938
0
  progressive_rfx_quant_add(quantCb, quantProgCb, &(tile->cbBitPos));
939
0
  progressive_rfx_quant_add(quantCr, quantProgCr, &(tile->crBitPos));
940
0
  progressive_rfx_quant_add(quantY, quantProgY, &shiftY);
941
0
  progressive_rfx_quant_lsub(&shiftY, 1); /* -6 + 5 = -1 */
942
0
  progressive_rfx_quant_add(quantCb, quantProgCb, &shiftCb);
943
0
  progressive_rfx_quant_lsub(&shiftCb, 1); /* -6 + 5 = -1 */
944
0
  progressive_rfx_quant_add(quantCr, quantProgCr, &shiftCr);
945
0
  progressive_rfx_quant_lsub(&shiftCr, 1); /* -6 + 5 = -1 */
946
947
0
  pSign[0] = (INT16*)((&tile->sign[((8192 + 32) * 0) + 16])); /* Y/R buffer */
948
0
  pSign[1] = (INT16*)((&tile->sign[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
949
0
  pSign[2] = (INT16*)((&tile->sign[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
950
951
0
  pCurrent[0] = (INT16*)((&tile->current[((8192 + 32) * 0) + 16])); /* Y/R buffer */
952
0
  pCurrent[1] = (INT16*)((&tile->current[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
953
0
  pCurrent[2] = (INT16*)((&tile->current[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
954
955
0
  pBuffer = (BYTE*)BufferPool_Take(progressive->bufferPool, -1);
956
0
  pSrcDst[0] = (INT16*)((&pBuffer[((8192 + 32) * 0) + 16])); /* Y/R buffer */
957
0
  pSrcDst[1] = (INT16*)((&pBuffer[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
958
0
  pSrcDst[2] = (INT16*)((&pBuffer[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
959
960
0
  rc = progressive_rfx_decode_component(progressive, &shiftY, tile->yData, tile->yLen, pSrcDst[0],
961
0
                                        pCurrent[0], pSign[0], diff, sub, extrapolate); /* Y */
962
0
  if (rc < 0)
963
0
    goto fail;
964
0
  rc = progressive_rfx_decode_component(progressive, &shiftCb, tile->cbData, tile->cbLen,
965
0
                                        pSrcDst[1], pCurrent[1], pSign[1], diff, sub,
966
0
                                        extrapolate); /* Cb */
967
0
  if (rc < 0)
968
0
    goto fail;
969
0
  rc = progressive_rfx_decode_component(progressive, &shiftCr, tile->crData, tile->crLen,
970
0
                                        pSrcDst[2], pCurrent[2], pSign[2], diff, sub,
971
0
                                        extrapolate); /* Cr */
972
0
  if (rc < 0)
973
0
    goto fail;
974
975
0
  {
976
0
    const INT16** ptr = WINPR_REINTERPRET_CAST(pSrcDst, INT16**, const INT16**);
977
0
    rc = prims->yCbCrToRGB_16s8u_P3AC4R(ptr, 64 * 2, tile->data, tile->stride,
978
0
                                        progressive->format, &roi_64x64);
979
0
  }
980
0
fail:
981
0
  BufferPool_Return(progressive->bufferPool, pBuffer);
982
0
  return rc;
983
0
}
984
985
static inline INT16 progressive_rfx_srl_read(RFX_PROGRESSIVE_UPGRADE_STATE* WINPR_RESTRICT state,
986
                                             UINT32 numBits)
987
0
{
988
0
  WINPR_ASSERT(state);
989
990
0
  wBitStream* bs = state->srl;
991
0
  WINPR_ASSERT(bs);
992
993
0
  if (state->nz)
994
0
  {
995
0
    state->nz--;
996
0
    return 0;
997
0
  }
998
999
0
  const UINT32 k = state->kp / 8;
1000
1001
0
  if (!state->mode)
1002
0
  {
1003
    /* zero encoding */
1004
0
    const UINT32 bit = (bs->accumulator & 0x80000000) ? 1 : 0;
1005
0
    BitStream_Shift(bs, 1);
1006
1007
0
    if (!bit)
1008
0
    {
1009
      /* '0' bit, nz >= (1 << k), nz = (1 << k) */
1010
0
      state->nz = (1 << k);
1011
0
      state->kp += 4;
1012
1013
0
      if (state->kp > 80)
1014
0
        state->kp = 80;
1015
1016
0
      state->nz--;
1017
0
      return 0;
1018
0
    }
1019
0
    else
1020
0
    {
1021
      /* '1' bit, nz < (1 << k), nz = next k bits */
1022
0
      state->nz = 0;
1023
0
      state->mode = 1; /* unary encoding is next */
1024
1025
0
      if (k)
1026
0
      {
1027
0
        bs->mask = ((1 << k) - 1);
1028
0
        state->nz =
1029
0
            WINPR_ASSERTING_INT_CAST(int16_t, ((bs->accumulator >> (32u - k)) & bs->mask));
1030
0
        BitStream_Shift(bs, k);
1031
0
      }
1032
1033
0
      if (state->nz)
1034
0
      {
1035
0
        state->nz--;
1036
0
        return 0;
1037
0
      }
1038
0
    }
1039
0
  }
1040
1041
0
  state->mode = 0; /* zero encoding is next */
1042
  /* unary encoding */
1043
  /* read sign bit */
1044
0
  const UINT32 sign = (bs->accumulator & 0x80000000) ? 1 : 0;
1045
0
  BitStream_Shift(bs, 1);
1046
1047
0
  if (state->kp < 6)
1048
0
    state->kp = 0;
1049
0
  else
1050
0
    state->kp -= 6;
1051
1052
0
  if (numBits == 1)
1053
0
    return sign ? -1 : 1;
1054
1055
0
  UINT32 mag = 1;
1056
0
  const UINT32 max = (1 << numBits) - 1;
1057
1058
0
  while (mag < max)
1059
0
  {
1060
0
    const UINT32 bit = (bs->accumulator & 0x80000000) ? 1 : 0;
1061
0
    BitStream_Shift(bs, 1);
1062
1063
0
    if (bit)
1064
0
      break;
1065
1066
0
    mag++;
1067
0
  }
1068
1069
0
  if (mag > INT16_MAX)
1070
0
    mag = INT16_MAX;
1071
0
  return (INT16)(sign ? -1 * (int)mag : (INT16)mag);
1072
0
}
1073
1074
static inline int
1075
progressive_rfx_upgrade_state_finish(RFX_PROGRESSIVE_UPGRADE_STATE* WINPR_RESTRICT state)
1076
0
{
1077
0
  UINT32 pad = 0;
1078
0
  wBitStream* srl = NULL;
1079
0
  wBitStream* raw = NULL;
1080
0
  if (!state)
1081
0
    return -1;
1082
1083
0
  srl = state->srl;
1084
0
  raw = state->raw;
1085
  /* Read trailing bits from RAW/SRL bit streams */
1086
0
  pad = (raw->position % 8) ? (8 - (raw->position % 8)) : 0;
1087
1088
0
  if (pad)
1089
0
    BitStream_Shift(raw, pad);
1090
1091
0
  pad = (srl->position % 8) ? (8 - (srl->position % 8)) : 0;
1092
1093
0
  if (pad)
1094
0
    BitStream_Shift(srl, pad);
1095
1096
0
  if (BitStream_GetRemainingLength(srl) == 8)
1097
0
    BitStream_Shift(srl, 8);
1098
1099
0
  return 1;
1100
0
}
1101
1102
static inline int progressive_rfx_upgrade_block(RFX_PROGRESSIVE_UPGRADE_STATE* WINPR_RESTRICT state,
1103
                                                INT16* WINPR_RESTRICT buffer,
1104
                                                INT16* WINPR_RESTRICT sign, UINT32 length,
1105
                                                UINT32 shift, WINPR_ATTR_UNUSED UINT32 bitPos,
1106
                                                UINT32 numBits)
1107
0
{
1108
0
  if (!numBits)
1109
0
    return 1;
1110
1111
0
  wBitStream* raw = state->raw;
1112
0
  int32_t input = 0;
1113
1114
0
  if (!state->nonLL)
1115
0
  {
1116
0
    for (UINT32 index = 0; index < length; index++)
1117
0
    {
1118
0
      raw->mask = ((1 << numBits) - 1);
1119
0
      input = (INT16)((raw->accumulator >> (32 - numBits)) & raw->mask);
1120
0
      BitStream_Shift(raw, numBits);
1121
1122
0
      const int32_t shifted = input << shift;
1123
0
      const int32_t val = buffer[index] + shifted;
1124
0
      const int16_t ival = WINPR_ASSERTING_INT_CAST(int16_t, val);
1125
0
      buffer[index] = ival;
1126
0
    }
1127
1128
0
    return 1;
1129
0
  }
1130
1131
0
  for (UINT32 index = 0; index < length; index++)
1132
0
  {
1133
0
    if (sign[index] > 0)
1134
0
    {
1135
      /* sign > 0, read from raw */
1136
0
      raw->mask = ((1 << numBits) - 1);
1137
0
      input = (INT16)((raw->accumulator >> (32 - numBits)) & raw->mask);
1138
0
      BitStream_Shift(raw, numBits);
1139
0
    }
1140
0
    else if (sign[index] < 0)
1141
0
    {
1142
      /* sign < 0, read from raw */
1143
0
      raw->mask = ((1 << numBits) - 1);
1144
0
      input = (INT16)((raw->accumulator >> (32 - numBits)) & raw->mask);
1145
0
      BitStream_Shift(raw, numBits);
1146
0
      input *= -1;
1147
0
    }
1148
0
    else
1149
0
    {
1150
      /* sign == 0, read from srl */
1151
0
      input = progressive_rfx_srl_read(state, numBits);
1152
0
      sign[index] = WINPR_ASSERTING_INT_CAST(int16_t, input);
1153
0
    }
1154
1155
0
    const int32_t val = input << shift;
1156
0
    const int32_t ival = buffer[index] + val;
1157
0
    buffer[index] = WINPR_ASSERTING_INT_CAST(INT16, ival);
1158
0
  }
1159
1160
0
  return 1;
1161
0
}
1162
1163
static inline int progressive_rfx_upgrade_component(
1164
    PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1165
    const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT shift,
1166
    const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT bitPos,
1167
    const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT numBits, INT16* WINPR_RESTRICT buffer,
1168
    INT16* WINPR_RESTRICT current, INT16* WINPR_RESTRICT sign, const BYTE* WINPR_RESTRICT srlData,
1169
    UINT32 srlLen, const BYTE* WINPR_RESTRICT rawData, UINT32 rawLen, BOOL coeffDiff,
1170
    WINPR_ATTR_UNUSED BOOL subbandDiff, BOOL extrapolate)
1171
0
{
1172
0
  int rc = 0;
1173
0
  UINT32 aRawLen = 0;
1174
0
  UINT32 aSrlLen = 0;
1175
0
  wBitStream s_srl = { 0 };
1176
0
  wBitStream s_raw = { 0 };
1177
0
  RFX_PROGRESSIVE_UPGRADE_STATE state = { 0 };
1178
1179
0
  state.kp = 8;
1180
0
  state.mode = 0;
1181
0
  state.srl = &s_srl;
1182
0
  state.raw = &s_raw;
1183
0
  BitStream_Attach(state.srl, srlData, srlLen);
1184
0
  BitStream_Fetch(state.srl);
1185
0
  BitStream_Attach(state.raw, rawData, rawLen);
1186
0
  BitStream_Fetch(state.raw);
1187
1188
0
  state.nonLL = TRUE;
1189
0
  rc = progressive_rfx_upgrade_block(&state, &current[0], &sign[0], 1023, shift->HL1, bitPos->HL1,
1190
0
                                     numBits->HL1); /* HL1 */
1191
0
  if (rc < 0)
1192
0
    return rc;
1193
0
  rc = progressive_rfx_upgrade_block(&state, &current[1023], &sign[1023], 1023, shift->LH1,
1194
0
                                     bitPos->LH1, numBits->LH1); /* LH1 */
1195
0
  if (rc < 0)
1196
0
    return rc;
1197
0
  rc = progressive_rfx_upgrade_block(&state, &current[2046], &sign[2046], 961, shift->HH1,
1198
0
                                     bitPos->HH1, numBits->HH1); /* HH1 */
1199
0
  if (rc < 0)
1200
0
    return rc;
1201
0
  rc = progressive_rfx_upgrade_block(&state, &current[3007], &sign[3007], 272, shift->HL2,
1202
0
                                     bitPos->HL2, numBits->HL2); /* HL2 */
1203
0
  if (rc < 0)
1204
0
    return rc;
1205
0
  rc = progressive_rfx_upgrade_block(&state, &current[3279], &sign[3279], 272, shift->LH2,
1206
0
                                     bitPos->LH2, numBits->LH2); /* LH2 */
1207
0
  if (rc < 0)
1208
0
    return rc;
1209
0
  rc = progressive_rfx_upgrade_block(&state, &current[3551], &sign[3551], 256, shift->HH2,
1210
0
                                     bitPos->HH2, numBits->HH2); /* HH2 */
1211
0
  if (rc < 0)
1212
0
    return rc;
1213
0
  rc = progressive_rfx_upgrade_block(&state, &current[3807], &sign[3807], 72, shift->HL3,
1214
0
                                     bitPos->HL3, numBits->HL3); /* HL3 */
1215
0
  if (rc < 0)
1216
0
    return rc;
1217
0
  rc = progressive_rfx_upgrade_block(&state, &current[3879], &sign[3879], 72, shift->LH3,
1218
0
                                     bitPos->LH3, numBits->LH3); /* LH3 */
1219
0
  if (rc < 0)
1220
0
    return rc;
1221
0
  rc = progressive_rfx_upgrade_block(&state, &current[3951], &sign[3951], 64, shift->HH3,
1222
0
                                     bitPos->HH3, numBits->HH3); /* HH3 */
1223
0
  if (rc < 0)
1224
0
    return rc;
1225
1226
0
  state.nonLL = FALSE;
1227
0
  rc = progressive_rfx_upgrade_block(&state, &current[4015], &sign[4015], 81, shift->LL3,
1228
0
                                     bitPos->LL3, numBits->LL3); /* LL3 */
1229
0
  if (rc < 0)
1230
0
    return rc;
1231
0
  rc = progressive_rfx_upgrade_state_finish(&state);
1232
0
  if (rc < 0)
1233
0
    return rc;
1234
0
  aRawLen = (state.raw->position + 7) / 8;
1235
0
  aSrlLen = (state.srl->position + 7) / 8;
1236
1237
0
  if ((aRawLen != rawLen) || (aSrlLen != srlLen))
1238
0
  {
1239
0
    int pRawLen = 0;
1240
0
    int pSrlLen = 0;
1241
1242
0
    if (rawLen)
1243
0
      pRawLen = (int)((((float)aRawLen) / ((float)rawLen)) * 100.0f);
1244
1245
0
    if (srlLen)
1246
0
      pSrlLen = (int)((((float)aSrlLen) / ((float)srlLen)) * 100.0f);
1247
1248
0
    WLog_Print(progressive->log, WLOG_WARN,
1249
0
               "RAW: %" PRIu32 "/%" PRIu32 " %d%% (%" PRIu32 "/%" PRIu32 ":%" PRIu32
1250
0
               ")\tSRL: %" PRIu32 "/%" PRIu32 " %d%% (%" PRIu32 "/%" PRIu32 ":%" PRIu32 ")",
1251
0
               aRawLen, rawLen, pRawLen, state.raw->position, rawLen * 8,
1252
0
               (rawLen * 8) - state.raw->position, aSrlLen, srlLen, pSrlLen,
1253
0
               state.srl->position, srlLen * 8, (srlLen * 8) - state.srl->position);
1254
0
    return -1;
1255
0
  }
1256
1257
0
  return progressive_rfx_dwt_2d_decode(progressive, buffer, current, coeffDiff, extrapolate,
1258
0
                                       TRUE);
1259
0
}
1260
1261
static inline int
1262
progressive_decompress_tile_upgrade(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1263
                                    RFX_PROGRESSIVE_TILE* WINPR_RESTRICT tile,
1264
                                    PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1265
                                    const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
1266
0
{
1267
0
  int status = 0;
1268
0
  BOOL coeffDiff = 0;
1269
0
  BOOL sub = 0;
1270
0
  BOOL extrapolate = 0;
1271
0
  BYTE* pBuffer = NULL;
1272
0
  INT16* pSign[3] = { 0 };
1273
0
  INT16* pSrcDst[3] = { 0 };
1274
0
  INT16* pCurrent[3] = { 0 };
1275
0
  RFX_COMPONENT_CODEC_QUANT shiftY = { 0 };
1276
0
  RFX_COMPONENT_CODEC_QUANT shiftCb = { 0 };
1277
0
  RFX_COMPONENT_CODEC_QUANT shiftCr = { 0 };
1278
0
  RFX_COMPONENT_CODEC_QUANT yBitPos = { 0 };
1279
0
  RFX_COMPONENT_CODEC_QUANT cbBitPos = { 0 };
1280
0
  RFX_COMPONENT_CODEC_QUANT crBitPos = { 0 };
1281
0
  RFX_COMPONENT_CODEC_QUANT yNumBits = { 0 };
1282
0
  RFX_COMPONENT_CODEC_QUANT cbNumBits = { 0 };
1283
0
  RFX_COMPONENT_CODEC_QUANT crNumBits = { 0 };
1284
0
  RFX_COMPONENT_CODEC_QUANT* quantY = NULL;
1285
0
  RFX_COMPONENT_CODEC_QUANT* quantCb = NULL;
1286
0
  RFX_COMPONENT_CODEC_QUANT* quantCr = NULL;
1287
0
  RFX_COMPONENT_CODEC_QUANT* quantProgY = NULL;
1288
0
  RFX_COMPONENT_CODEC_QUANT* quantProgCb = NULL;
1289
0
  RFX_COMPONENT_CODEC_QUANT* quantProgCr = NULL;
1290
0
  RFX_PROGRESSIVE_CODEC_QUANT* quantProg = NULL;
1291
0
  static const prim_size_t roi_64x64 = { 64, 64 };
1292
0
  const primitives_t* prims = primitives_get();
1293
1294
0
  coeffDiff = tile->flags & RFX_TILE_DIFFERENCE;
1295
0
  sub = context->flags & RFX_SUBBAND_DIFFING;
1296
0
  extrapolate = region->flags & RFX_DWT_REDUCE_EXTRAPOLATE;
1297
1298
0
  tile->pass++;
1299
1300
#if defined(WITH_DEBUG_CODECS)
1301
  WLog_Print(progressive->log, WLOG_DEBUG,
1302
             "ProgressiveTileUpgrade: pass: %" PRIu16 " quantIdx Y: %" PRIu8 " Cb: %" PRIu8
1303
             " Cr: %" PRIu8 " xIdx: %" PRIu16 " yIdx: %" PRIu16 " quality: %" PRIu8
1304
             " ySrlLen: %" PRIu16 " yRawLen: %" PRIu16 " cbSrlLen: %" PRIu16 " cbRawLen: %" PRIu16
1305
             " crSrlLen: %" PRIu16 " crRawLen: %" PRIu16 "",
1306
             tile->pass, tile->quantIdxY, tile->quantIdxCb, tile->quantIdxCr, tile->xIdx,
1307
             tile->yIdx, tile->quality, tile->ySrlLen, tile->yRawLen, tile->cbSrlLen,
1308
             tile->cbRawLen, tile->crSrlLen, tile->crRawLen);
1309
#endif
1310
1311
0
  if (tile->quantIdxY >= region->numQuant)
1312
0
  {
1313
0
    WLog_ERR(TAG, "quantIdxY %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxY, region->numQuant);
1314
0
    return -1;
1315
0
  }
1316
1317
0
  quantY = &(region->quantVals[tile->quantIdxY]);
1318
1319
0
  if (tile->quantIdxCb >= region->numQuant)
1320
0
  {
1321
0
    WLog_ERR(TAG, "quantIdxCb %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCb,
1322
0
             region->numQuant);
1323
0
    return -1;
1324
0
  }
1325
1326
0
  quantCb = &(region->quantVals[tile->quantIdxCb]);
1327
1328
0
  if (tile->quantIdxCr >= region->numQuant)
1329
0
  {
1330
0
    WLog_ERR(TAG, "quantIdxCr %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCr,
1331
0
             region->numQuant);
1332
0
    return -1;
1333
0
  }
1334
1335
0
  quantCr = &(region->quantVals[tile->quantIdxCr]);
1336
1337
0
  if (tile->quality == 0xFF)
1338
0
  {
1339
0
    quantProg = &(progressive->quantProgValFull);
1340
0
  }
1341
0
  else
1342
0
  {
1343
0
    if (tile->quality >= region->numProgQuant)
1344
0
    {
1345
0
      WLog_ERR(TAG, "quality %" PRIu8 " > numProgQuant %" PRIu8, tile->quality,
1346
0
               region->numProgQuant);
1347
0
      return -1;
1348
0
    }
1349
1350
0
    quantProg = &(region->quantProgVals[tile->quality]);
1351
0
  }
1352
1353
0
  quantProgY = &(quantProg->yQuantValues);
1354
0
  quantProgCb = &(quantProg->cbQuantValues);
1355
0
  quantProgCr = &(quantProg->crQuantValues);
1356
1357
0
  if (!progressive_rfx_quant_cmp_equal(quantY, &(tile->yQuant)))
1358
0
    WLog_Print(progressive->log, WLOG_WARN, "non-progressive quantY has changed!");
1359
1360
0
  if (!progressive_rfx_quant_cmp_equal(quantCb, &(tile->cbQuant)))
1361
0
    WLog_Print(progressive->log, WLOG_WARN, "non-progressive quantCb has changed!");
1362
1363
0
  if (!progressive_rfx_quant_cmp_equal(quantCr, &(tile->crQuant)))
1364
0
    WLog_Print(progressive->log, WLOG_WARN, "non-progressive quantCr has changed!");
1365
1366
0
  if (!(context->flags & RFX_SUBBAND_DIFFING))
1367
0
    WLog_WARN(TAG, "PROGRESSIVE_BLOCK_CONTEXT::flags & RFX_SUBBAND_DIFFING not set");
1368
1369
0
  progressive_rfx_quant_add(quantY, quantProgY, &yBitPos);
1370
0
  progressive_rfx_quant_add(quantCb, quantProgCb, &cbBitPos);
1371
0
  progressive_rfx_quant_add(quantCr, quantProgCr, &crBitPos);
1372
0
  progressive_rfx_quant_sub(&(tile->yBitPos), &yBitPos, &yNumBits);
1373
0
  progressive_rfx_quant_sub(&(tile->cbBitPos), &cbBitPos, &cbNumBits);
1374
0
  progressive_rfx_quant_sub(&(tile->crBitPos), &crBitPos, &crNumBits);
1375
0
  progressive_rfx_quant_add(quantY, quantProgY, &shiftY);
1376
0
  progressive_rfx_quant_lsub(&shiftY, 1); /* -6 + 5 = -1 */
1377
0
  progressive_rfx_quant_add(quantCb, quantProgCb, &shiftCb);
1378
0
  progressive_rfx_quant_lsub(&shiftCb, 1); /* -6 + 5 = -1 */
1379
0
  progressive_rfx_quant_add(quantCr, quantProgCr, &shiftCr);
1380
0
  progressive_rfx_quant_lsub(&shiftCr, 1); /* -6 + 5 = -1 */
1381
1382
0
  tile->yBitPos = yBitPos;
1383
0
  tile->cbBitPos = cbBitPos;
1384
0
  tile->crBitPos = crBitPos;
1385
0
  tile->yQuant = *quantY;
1386
0
  tile->cbQuant = *quantCb;
1387
0
  tile->crQuant = *quantCr;
1388
0
  tile->yProgQuant = *quantProgY;
1389
0
  tile->cbProgQuant = *quantProgCb;
1390
0
  tile->crProgQuant = *quantProgCr;
1391
1392
0
  pSign[0] = (INT16*)((&tile->sign[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1393
0
  pSign[1] = (INT16*)((&tile->sign[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1394
0
  pSign[2] = (INT16*)((&tile->sign[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1395
1396
0
  pCurrent[0] = (INT16*)((&tile->current[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1397
0
  pCurrent[1] = (INT16*)((&tile->current[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1398
0
  pCurrent[2] = (INT16*)((&tile->current[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1399
1400
0
  pBuffer = (BYTE*)BufferPool_Take(progressive->bufferPool, -1);
1401
0
  pSrcDst[0] = (INT16*)((&pBuffer[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1402
0
  pSrcDst[1] = (INT16*)((&pBuffer[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1403
0
  pSrcDst[2] = (INT16*)((&pBuffer[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1404
1405
0
  status = progressive_rfx_upgrade_component(progressive, &shiftY, quantProgY, &yNumBits,
1406
0
                                             pSrcDst[0], pCurrent[0], pSign[0], tile->ySrlData,
1407
0
                                             tile->ySrlLen, tile->yRawData, tile->yRawLen,
1408
0
                                             coeffDiff, sub, extrapolate); /* Y */
1409
1410
0
  if (status < 0)
1411
0
    goto fail;
1412
1413
0
  status = progressive_rfx_upgrade_component(progressive, &shiftCb, quantProgCb, &cbNumBits,
1414
0
                                             pSrcDst[1], pCurrent[1], pSign[1], tile->cbSrlData,
1415
0
                                             tile->cbSrlLen, tile->cbRawData, tile->cbRawLen,
1416
0
                                             coeffDiff, sub, extrapolate); /* Cb */
1417
1418
0
  if (status < 0)
1419
0
    goto fail;
1420
1421
0
  status = progressive_rfx_upgrade_component(progressive, &shiftCr, quantProgCr, &crNumBits,
1422
0
                                             pSrcDst[2], pCurrent[2], pSign[2], tile->crSrlData,
1423
0
                                             tile->crSrlLen, tile->crRawData, tile->crRawLen,
1424
0
                                             coeffDiff, sub, extrapolate); /* Cr */
1425
1426
0
  if (status < 0)
1427
0
    goto fail;
1428
1429
0
  {
1430
0
    const INT16** ptr = WINPR_REINTERPRET_CAST(pSrcDst, INT16**, const INT16**);
1431
0
    status = prims->yCbCrToRGB_16s8u_P3AC4R(ptr, 64 * 2, tile->data, tile->stride,
1432
0
                                            progressive->format, &roi_64x64);
1433
0
  }
1434
0
fail:
1435
0
  BufferPool_Return(progressive->bufferPool, pBuffer);
1436
0
  return status;
1437
0
}
1438
1439
static inline BOOL progressive_tile_read_upgrade(
1440
    PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, wStream* WINPR_RESTRICT s, UINT16 blockType,
1441
    UINT32 blockLen, PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
1442
    PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1443
    WINPR_ATTR_UNUSED const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
1444
81
{
1445
81
  RFX_PROGRESSIVE_TILE tile = { 0 };
1446
81
  const size_t expect = 20;
1447
1448
81
  if (!Stream_CheckAndLogRequiredLength(TAG, s, expect))
1449
1
    return FALSE;
1450
1451
80
  tile.blockType = blockType;
1452
80
  tile.blockLen = blockLen;
1453
80
  tile.flags = 0;
1454
1455
80
  Stream_Read_UINT8(s, tile.quantIdxY);
1456
80
  Stream_Read_UINT8(s, tile.quantIdxCb);
1457
80
  Stream_Read_UINT8(s, tile.quantIdxCr);
1458
80
  Stream_Read_UINT16(s, tile.xIdx);
1459
80
  Stream_Read_UINT16(s, tile.yIdx);
1460
80
  Stream_Read_UINT8(s, tile.quality);
1461
80
  Stream_Read_UINT16(s, tile.ySrlLen);
1462
80
  Stream_Read_UINT16(s, tile.yRawLen);
1463
80
  Stream_Read_UINT16(s, tile.cbSrlLen);
1464
80
  Stream_Read_UINT16(s, tile.cbRawLen);
1465
80
  Stream_Read_UINT16(s, tile.crSrlLen);
1466
80
  Stream_Read_UINT16(s, tile.crRawLen);
1467
1468
80
  tile.ySrlData = Stream_Pointer(s);
1469
80
  if (!Stream_SafeSeek(s, tile.ySrlLen))
1470
10
  {
1471
10
    WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.ySrlLen);
1472
10
    return FALSE;
1473
10
  }
1474
1475
70
  tile.yRawData = Stream_Pointer(s);
1476
70
  if (!Stream_SafeSeek(s, tile.yRawLen))
1477
2
  {
1478
2
    WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.yRawLen);
1479
2
    return FALSE;
1480
2
  }
1481
1482
68
  tile.cbSrlData = Stream_Pointer(s);
1483
68
  if (!Stream_SafeSeek(s, tile.cbSrlLen))
1484
4
  {
1485
4
    WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
1486
4
               tile.cbSrlLen);
1487
4
    return FALSE;
1488
4
  }
1489
1490
64
  tile.cbRawData = Stream_Pointer(s);
1491
64
  if (!Stream_SafeSeek(s, tile.cbRawLen))
1492
7
  {
1493
7
    WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
1494
7
               tile.cbRawLen);
1495
7
    return FALSE;
1496
7
  }
1497
1498
57
  tile.crSrlData = Stream_Pointer(s);
1499
57
  if (!Stream_SafeSeek(s, tile.crSrlLen))
1500
3
  {
1501
3
    WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
1502
3
               tile.crSrlLen);
1503
3
    return FALSE;
1504
3
  }
1505
1506
54
  tile.crRawData = Stream_Pointer(s);
1507
54
  if (!Stream_SafeSeek(s, tile.crRawLen))
1508
4
  {
1509
4
    WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
1510
4
               tile.crRawLen);
1511
4
    return FALSE;
1512
4
  }
1513
1514
50
  return progressive_surface_tile_replace(surface, region, &tile, TRUE);
1515
54
}
1516
1517
static inline BOOL
1518
progressive_tile_read(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, BOOL simple,
1519
                      wStream* WINPR_RESTRICT s, UINT16 blockType, UINT32 blockLen,
1520
                      PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
1521
                      PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1522
                      WINPR_ATTR_UNUSED const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
1523
69
{
1524
69
  RFX_PROGRESSIVE_TILE tile = { 0 };
1525
69
  size_t expect = simple ? 16 : 17;
1526
1527
69
  if (!Stream_CheckAndLogRequiredLength(TAG, s, expect))
1528
2
    return FALSE;
1529
1530
67
  tile.blockType = blockType;
1531
67
  tile.blockLen = blockLen;
1532
1533
67
  Stream_Read_UINT8(s, tile.quantIdxY);
1534
67
  Stream_Read_UINT8(s, tile.quantIdxCb);
1535
67
  Stream_Read_UINT8(s, tile.quantIdxCr);
1536
67
  Stream_Read_UINT16(s, tile.xIdx);
1537
67
  Stream_Read_UINT16(s, tile.yIdx);
1538
67
  Stream_Read_UINT8(s, tile.flags);
1539
1540
67
  if (!simple)
1541
48
    Stream_Read_UINT8(s, tile.quality);
1542
19
  else
1543
19
    tile.quality = 0xFF;
1544
67
  Stream_Read_UINT16(s, tile.yLen);
1545
67
  Stream_Read_UINT16(s, tile.cbLen);
1546
67
  Stream_Read_UINT16(s, tile.crLen);
1547
67
  Stream_Read_UINT16(s, tile.tailLen);
1548
1549
67
  tile.yData = Stream_Pointer(s);
1550
67
  if (!Stream_SafeSeek(s, tile.yLen))
1551
7
  {
1552
7
    WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.yLen);
1553
7
    return FALSE;
1554
7
  }
1555
1556
60
  tile.cbData = Stream_Pointer(s);
1557
60
  if (!Stream_SafeSeek(s, tile.cbLen))
1558
4
  {
1559
4
    WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.cbLen);
1560
4
    return FALSE;
1561
4
  }
1562
1563
56
  tile.crData = Stream_Pointer(s);
1564
56
  if (!Stream_SafeSeek(s, tile.crLen))
1565
4
  {
1566
4
    WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.crLen);
1567
4
    return FALSE;
1568
4
  }
1569
1570
52
  tile.tailData = Stream_Pointer(s);
1571
52
  if (!Stream_SafeSeek(s, tile.tailLen))
1572
4
  {
1573
4
    WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.tailLen);
1574
4
    return FALSE;
1575
4
  }
1576
1577
48
  return progressive_surface_tile_replace(surface, region, &tile, FALSE);
1578
52
}
1579
1580
static void CALLBACK progressive_process_tiles_tile_work_callback(PTP_CALLBACK_INSTANCE instance,
1581
                                                                  void* context, PTP_WORK work)
1582
0
{
1583
0
  PROGRESSIVE_TILE_PROCESS_WORK_PARAM* param = (PROGRESSIVE_TILE_PROCESS_WORK_PARAM*)context;
1584
1585
0
  WINPR_UNUSED(instance);
1586
0
  WINPR_UNUSED(work);
1587
1588
0
  switch (param->tile->blockType)
1589
0
  {
1590
0
    case PROGRESSIVE_WBT_TILE_SIMPLE:
1591
0
    case PROGRESSIVE_WBT_TILE_FIRST:
1592
0
      progressive_decompress_tile_first(param->progressive, param->tile, param->region,
1593
0
                                        param->context);
1594
0
      break;
1595
1596
0
    case PROGRESSIVE_WBT_TILE_UPGRADE:
1597
0
      progressive_decompress_tile_upgrade(param->progressive, param->tile, param->region,
1598
0
                                          param->context);
1599
0
      break;
1600
0
    default:
1601
0
      WLog_Print(param->progressive->log, WLOG_ERROR, "Invalid block type %04" PRIx16 " (%s)",
1602
0
                 param->tile->blockType,
1603
0
                 rfx_get_progressive_block_type_string(param->tile->blockType));
1604
0
      break;
1605
0
  }
1606
0
}
1607
1608
static inline SSIZE_T
1609
progressive_process_tiles(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1610
                          wStream* WINPR_RESTRICT s,
1611
                          PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1612
                          PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
1613
                          const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
1614
679
{
1615
679
  int status = 0;
1616
679
  size_t end = 0;
1617
679
  const size_t start = Stream_GetPosition(s);
1618
679
  UINT16 blockType = 0;
1619
679
  UINT32 blockLen = 0;
1620
679
  UINT32 count = 0;
1621
679
  UINT16 close_cnt = 0;
1622
1623
679
  WINPR_ASSERT(progressive);
1624
679
  WINPR_ASSERT(region);
1625
1626
679
  if (!Stream_CheckAndLogRequiredLength(TAG, s, region->tileDataSize))
1627
0
    return -1;
1628
1629
743
  while ((Stream_GetRemainingLength(s) >= 6) &&
1630
272
         (region->tileDataSize > (Stream_GetPosition(s) - start)))
1631
247
  {
1632
247
    const size_t pos = Stream_GetPosition(s);
1633
1634
247
    Stream_Read_UINT16(s, blockType);
1635
247
    Stream_Read_UINT32(s, blockLen);
1636
1637
#if defined(WITH_DEBUG_CODECS)
1638
    WLog_Print(progressive->log, WLOG_DEBUG, "%s",
1639
               rfx_get_progressive_block_type_string(blockType));
1640
#endif
1641
1642
247
    if (blockLen < 6)
1643
5
    {
1644
5
      WLog_Print(progressive->log, WLOG_ERROR, "Expected >= %" PRIu32 " remaining %" PRIu32,
1645
5
                 6u, blockLen);
1646
5
      return -1003;
1647
5
    }
1648
242
    if (!Stream_CheckAndLogRequiredLength(TAG, s, blockLen - 6))
1649
54
      return -1003;
1650
1651
188
    switch (blockType)
1652
188
    {
1653
20
      case PROGRESSIVE_WBT_TILE_SIMPLE:
1654
20
        if (!progressive_tile_read(progressive, TRUE, s, blockType, blockLen, surface,
1655
20
                                   region, context))
1656
14
          return -1022;
1657
6
        break;
1658
1659
49
      case PROGRESSIVE_WBT_TILE_FIRST:
1660
49
        if (!progressive_tile_read(progressive, FALSE, s, blockType, blockLen, surface,
1661
49
                                   region, context))
1662
18
          return -1027;
1663
31
        break;
1664
1665
81
      case PROGRESSIVE_WBT_TILE_UPGRADE:
1666
81
        if (!progressive_tile_read_upgrade(progressive, s, blockType, blockLen, surface,
1667
81
                                           region, context))
1668
37
          return -1032;
1669
44
        break;
1670
44
      default:
1671
38
        WLog_ERR(TAG, "Invalid block type %04" PRIx16 " (%s)", blockType,
1672
38
                 rfx_get_progressive_block_type_string(blockType));
1673
38
        return -1039;
1674
188
    }
1675
1676
81
    size_t rem = Stream_GetPosition(s);
1677
81
    if ((rem - pos) != blockLen)
1678
17
    {
1679
17
      WLog_Print(progressive->log, WLOG_ERROR,
1680
17
                 "Actual block read %" PRIuz " but expected %" PRIu32, rem - pos, blockLen);
1681
17
      return -1040;
1682
17
    }
1683
64
    count++;
1684
64
  }
1685
1686
496
  end = Stream_GetPosition(s);
1687
496
  if ((end - start) != region->tileDataSize)
1688
9
  {
1689
9
    WLog_Print(progressive->log, WLOG_ERROR,
1690
9
               "Actual total blocks read %" PRIuz " but expected %" PRIu32, end - start,
1691
9
               region->tileDataSize);
1692
9
    return -1041;
1693
9
  }
1694
1695
487
  if (count != region->numTiles)
1696
33
  {
1697
33
    WLog_Print(progressive->log, WLOG_WARN,
1698
33
               "numTiles inconsistency: actual: %" PRIu32 ", expected: %" PRIu16 "\n", count,
1699
33
               region->numTiles);
1700
33
    return -1044;
1701
33
  }
1702
1703
454
  for (UINT32 idx = 0; idx < region->numTiles; idx++)
1704
0
  {
1705
0
    RFX_PROGRESSIVE_TILE* tile = region->tiles[idx];
1706
0
    PROGRESSIVE_TILE_PROCESS_WORK_PARAM* param = &progressive->params[idx];
1707
0
    param->progressive = progressive;
1708
0
    param->region = region;
1709
0
    param->context = context;
1710
0
    param->tile = tile;
1711
1712
0
    if (progressive->rfx_context->priv->UseThreads)
1713
0
    {
1714
0
      progressive->work_objects[idx] = CreateThreadpoolWork(
1715
0
          progressive_process_tiles_tile_work_callback, (void*)param, NULL);
1716
0
      if (!progressive->work_objects[idx])
1717
0
      {
1718
0
        WLog_Print(progressive->log, WLOG_ERROR,
1719
0
                   "Failed to create ThreadpoolWork for tile %" PRIu32, idx);
1720
0
        status = -1;
1721
0
        break;
1722
0
      }
1723
1724
0
      SubmitThreadpoolWork(progressive->work_objects[idx]);
1725
1726
0
      close_cnt = WINPR_ASSERTING_INT_CAST(UINT16, idx + 1);
1727
0
    }
1728
0
    else
1729
0
    {
1730
0
      progressive_process_tiles_tile_work_callback(0, param, 0);
1731
0
    }
1732
1733
0
    if (status < 0)
1734
0
    {
1735
0
      WLog_Print(progressive->log, WLOG_ERROR, "Failed to decompress %s at %" PRIu16,
1736
0
                 rfx_get_progressive_block_type_string(tile->blockType), idx);
1737
0
      goto fail;
1738
0
    }
1739
0
  }
1740
1741
454
  if (progressive->rfx_context->priv->UseThreads)
1742
454
  {
1743
454
    for (UINT32 idx = 0; idx < close_cnt; idx++)
1744
0
    {
1745
0
      WaitForThreadpoolWorkCallbacks(progressive->work_objects[idx], FALSE);
1746
0
      CloseThreadpoolWork(progressive->work_objects[idx]);
1747
0
    }
1748
454
  }
1749
1750
454
fail:
1751
1752
454
  if (status < 0)
1753
0
    return -1;
1754
1755
454
  return (SSIZE_T)(end - start);
1756
454
}
1757
1758
static inline SSIZE_T progressive_wb_sync(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1759
                                          wStream* WINPR_RESTRICT s, UINT16 blockType,
1760
                                          UINT32 blockLen)
1761
1.30k
{
1762
1.30k
  const UINT32 magic = 0xCACCACCA;
1763
1.30k
  const UINT16 version = 0x0100;
1764
1.30k
  PROGRESSIVE_BLOCK_SYNC sync = { 0 };
1765
1766
1.30k
  sync.blockType = blockType;
1767
1.30k
  sync.blockLen = blockLen;
1768
1769
1.30k
  if (sync.blockLen != 12)
1770
22
  {
1771
22
    WLog_Print(progressive->log, WLOG_ERROR,
1772
22
               "PROGRESSIVE_BLOCK_SYNC::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
1773
22
               sync.blockLen, 12u);
1774
22
    return -1005;
1775
22
  }
1776
1777
1.27k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
1778
0
    return -1004;
1779
1780
#if defined(WITH_DEBUG_CODECS)
1781
  WLog_Print(progressive->log, WLOG_DEBUG, "ProgressiveSync");
1782
#endif
1783
1784
1.27k
  Stream_Read_UINT32(s, sync.magic);
1785
1.27k
  Stream_Read_UINT16(s, sync.version);
1786
1787
1.27k
  if (sync.magic != magic)
1788
57
  {
1789
57
    WLog_Print(progressive->log, WLOG_ERROR,
1790
57
               "PROGRESSIVE_BLOCK_SYNC::magic = 0x%08" PRIx32 " != 0x%08" PRIx32, sync.magic,
1791
57
               magic);
1792
57
    return -1005;
1793
57
  }
1794
1795
1.22k
  if (sync.version != 0x0100)
1796
8
  {
1797
8
    WLog_Print(progressive->log, WLOG_ERROR,
1798
8
               "PROGRESSIVE_BLOCK_SYNC::version = 0x%04" PRIx16 " != 0x%04" PRIu16,
1799
8
               sync.version, version);
1800
8
    return -1006;
1801
8
  }
1802
1803
1.21k
  if ((progressive->state & FLAG_WBT_SYNC) != 0)
1804
1.18k
    WLog_WARN(TAG, "Duplicate PROGRESSIVE_BLOCK_SYNC, ignoring");
1805
1806
1.21k
  progressive->state |= FLAG_WBT_SYNC;
1807
1.21k
  return 0;
1808
1.22k
}
1809
1810
static inline SSIZE_T progressive_wb_frame_begin(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1811
                                                 wStream* WINPR_RESTRICT s, UINT16 blockType,
1812
                                                 UINT32 blockLen)
1813
720
{
1814
720
  PROGRESSIVE_BLOCK_FRAME_BEGIN frameBegin = { 0 };
1815
1816
720
  frameBegin.blockType = blockType;
1817
720
  frameBegin.blockLen = blockLen;
1818
1819
720
  if (frameBegin.blockLen != 12)
1820
62
  {
1821
62
    WLog_Print(progressive->log, WLOG_ERROR,
1822
62
               " RFX_PROGRESSIVE_FRAME_BEGIN::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
1823
62
               frameBegin.blockLen, 12u);
1824
62
    return -1005;
1825
62
  }
1826
1827
658
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
1828
0
    return -1007;
1829
1830
658
  Stream_Read_UINT32(s, frameBegin.frameIndex);
1831
658
  Stream_Read_UINT16(s, frameBegin.regionCount);
1832
1833
#if defined(WITH_DEBUG_CODECS)
1834
  WLog_Print(progressive->log, WLOG_DEBUG,
1835
             "ProgressiveFrameBegin: frameIndex: %" PRIu32 " regionCount: %" PRIu16 "",
1836
             frameBegin.frameIndex, frameBegin.regionCount);
1837
#endif
1838
1839
  /**
1840
   * If the number of elements specified by the regionCount field is
1841
   * larger than the actual number of elements in the regions field,
1842
   * the decoder SHOULD ignore this inconsistency.
1843
   */
1844
1845
658
  if ((progressive->state & FLAG_WBT_FRAME_BEGIN) != 0)
1846
1
  {
1847
1
    WLog_ERR(TAG, "Duplicate RFX_PROGRESSIVE_FRAME_BEGIN in stream, this is not allowed!");
1848
1
    return -1008;
1849
1
  }
1850
1851
657
  if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
1852
1
  {
1853
1
    WLog_ERR(TAG, "RFX_PROGRESSIVE_FRAME_BEGIN after RFX_PROGRESSIVE_FRAME_END in stream, this "
1854
1
                  "is not allowed!");
1855
1
    return -1008;
1856
1
  }
1857
1858
656
  progressive->state |= FLAG_WBT_FRAME_BEGIN;
1859
656
  return 0;
1860
657
}
1861
1862
static inline SSIZE_T progressive_wb_frame_end(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1863
                                               wStream* WINPR_RESTRICT s, UINT16 blockType,
1864
                                               UINT32 blockLen)
1865
2.26k
{
1866
2.26k
  PROGRESSIVE_BLOCK_FRAME_END frameEnd = { 0 };
1867
1868
2.26k
  frameEnd.blockType = blockType;
1869
2.26k
  frameEnd.blockLen = blockLen;
1870
1871
2.26k
  if (frameEnd.blockLen != 6)
1872
88
  {
1873
88
    WLog_Print(progressive->log, WLOG_ERROR,
1874
88
               " RFX_PROGRESSIVE_FRAME_END::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
1875
88
               frameEnd.blockLen, 6u);
1876
88
    return -1005;
1877
88
  }
1878
1879
2.17k
  if (Stream_GetRemainingLength(s) != 0)
1880
0
  {
1881
0
    WLog_Print(progressive->log, WLOG_ERROR,
1882
0
               "ProgressiveFrameEnd short %" PRIuz ", expected %u",
1883
0
               Stream_GetRemainingLength(s), 0U);
1884
0
    return -1008;
1885
0
  }
1886
1887
#if defined(WITH_DEBUG_CODECS)
1888
  WLog_Print(progressive->log, WLOG_DEBUG, "ProgressiveFrameEnd");
1889
#endif
1890
1891
2.17k
  if ((progressive->state & FLAG_WBT_FRAME_BEGIN) == 0)
1892
1.95k
    WLog_WARN(TAG, "RFX_PROGRESSIVE_FRAME_END before RFX_PROGRESSIVE_FRAME_BEGIN, ignoring");
1893
2.17k
  if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
1894
2.09k
    WLog_WARN(TAG, "Duplicate RFX_PROGRESSIVE_FRAME_END, ignoring");
1895
1896
2.17k
  progressive->state |= FLAG_WBT_FRAME_END;
1897
2.17k
  return 0;
1898
2.17k
}
1899
1900
static inline SSIZE_T progressive_wb_context(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1901
                                             wStream* WINPR_RESTRICT s, UINT16 blockType,
1902
                                             UINT32 blockLen)
1903
797
{
1904
797
  PROGRESSIVE_BLOCK_CONTEXT* context = &progressive->context;
1905
797
  context->blockType = blockType;
1906
797
  context->blockLen = blockLen;
1907
1908
797
  if (context->blockLen != 10)
1909
73
  {
1910
73
    WLog_Print(progressive->log, WLOG_ERROR,
1911
73
               "RFX_PROGRESSIVE_CONTEXT::blockLen = 0x%08" PRIx32 " != 0x%08x",
1912
73
               context->blockLen, 10u);
1913
73
    return -1005;
1914
73
  }
1915
1916
724
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1917
0
    return -1009;
1918
1919
724
  Stream_Read_UINT8(s, context->ctxId);
1920
724
  Stream_Read_UINT16(s, context->tileSize);
1921
724
  Stream_Read_UINT8(s, context->flags);
1922
1923
724
  if (context->ctxId != 0x00)
1924
335
    WLog_WARN(TAG, "RFX_PROGRESSIVE_CONTEXT::ctxId != 0x00: %" PRIu8, context->ctxId);
1925
1926
724
  if (context->tileSize != 64)
1927
22
  {
1928
22
    WLog_ERR(TAG, "RFX_PROGRESSIVE_CONTEXT::tileSize != 0x40: %" PRIu16, context->tileSize);
1929
22
    return -1010;
1930
22
  }
1931
1932
702
  if ((progressive->state & FLAG_WBT_FRAME_BEGIN) != 0)
1933
325
    WLog_WARN(TAG, "RFX_PROGRESSIVE_CONTEXT received after RFX_PROGRESSIVE_FRAME_BEGIN");
1934
702
  if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
1935
211
    WLog_WARN(TAG, "RFX_PROGRESSIVE_CONTEXT received after RFX_PROGRESSIVE_FRAME_END");
1936
702
  if ((progressive->state & FLAG_WBT_CONTEXT) != 0)
1937
657
    WLog_WARN(TAG, "Duplicate RFX_PROGRESSIVE_CONTEXT received, ignoring.");
1938
1939
#if defined(WITH_DEBUG_CODECS)
1940
  WLog_Print(progressive->log, WLOG_DEBUG, "ProgressiveContext: flags: 0x%02" PRIX8 "",
1941
             context->flags);
1942
#endif
1943
1944
702
  progressive->state |= FLAG_WBT_CONTEXT;
1945
702
  return 0;
1946
724
}
1947
1948
static inline SSIZE_T
1949
progressive_wb_read_region_header(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1950
                                  wStream* WINPR_RESTRICT s, UINT16 blockType, UINT32 blockLen,
1951
                                  PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region)
1952
1.83k
{
1953
1.83k
  region->usedTiles = 0;
1954
1955
1.83k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
1956
8
    return -1011;
1957
1958
1.82k
  region->blockType = blockType;
1959
1.82k
  region->blockLen = blockLen;
1960
1.82k
  Stream_Read_UINT8(s, region->tileSize);
1961
1.82k
  Stream_Read_UINT16(s, region->numRects);
1962
1.82k
  Stream_Read_UINT8(s, region->numQuant);
1963
1.82k
  Stream_Read_UINT8(s, region->numProgQuant);
1964
1.82k
  Stream_Read_UINT8(s, region->flags);
1965
1.82k
  Stream_Read_UINT16(s, region->numTiles);
1966
1.82k
  Stream_Read_UINT32(s, region->tileDataSize);
1967
1968
1.82k
  if (region->tileSize != 64)
1969
33
  {
1970
33
    WLog_Print(progressive->log, WLOG_ERROR,
1971
33
               "ProgressiveRegion tile size %" PRIu8 ", expected %u", region->tileSize, 64U);
1972
33
    return -1012;
1973
33
  }
1974
1975
1.79k
  if (region->numRects < 1)
1976
2
  {
1977
2
    WLog_Print(progressive->log, WLOG_ERROR, "ProgressiveRegion missing rect count %" PRIu16,
1978
2
               region->numRects);
1979
2
    return -1013;
1980
2
  }
1981
1982
1.78k
  if (region->numQuant > 7)
1983
11
  {
1984
11
    WLog_Print(progressive->log, WLOG_ERROR,
1985
11
               "ProgressiveRegion quant count too high %" PRIu8 ", expected < %u",
1986
11
               region->numQuant, 7U);
1987
11
    return -1014;
1988
11
  }
1989
1990
3.55k
  const SSIZE_T rc = WINPR_ASSERTING_INT_CAST(SSIZE_T, Stream_GetRemainingLength(s));
1991
3.55k
  const SSIZE_T expect = region->numRects * 8ll + region->numQuant * 5ll +
1992
3.55k
                         region->numProgQuant * 16ll + region->tileDataSize;
1993
3.55k
  SSIZE_T len = rc;
1994
3.55k
  if (expect != rc)
1995
835
  {
1996
835
    if (len / 8LL < region->numRects)
1997
55
    {
1998
55
      WLog_Print(progressive->log, WLOG_ERROR,
1999
55
                 "ProgressiveRegion data short for region->rects");
2000
55
      return -1015;
2001
55
    }
2002
780
    len -= region->numRects * 8LL;
2003
2004
780
    if (len / 5LL < region->numQuant)
2005
2
    {
2006
2
      WLog_Print(progressive->log, WLOG_ERROR,
2007
2
                 "ProgressiveRegion data short for region->cQuant");
2008
2
      return -1018;
2009
2
    }
2010
778
    len -= region->numQuant * 5LL;
2011
2012
778
    if (len / 16LL < region->numProgQuant)
2013
3
    {
2014
3
      WLog_Print(progressive->log, WLOG_ERROR,
2015
3
                 "ProgressiveRegion data short for region->cProgQuant");
2016
3
      return -1021;
2017
3
    }
2018
775
    len -= region->numProgQuant * 16LL;
2019
2020
775
    if (len < region->tileDataSize * 1ll)
2021
43
    {
2022
43
      WLog_Print(progressive->log, WLOG_ERROR,
2023
43
                 "ProgressiveRegion data short for region->tiles");
2024
43
      return -1024;
2025
43
    }
2026
732
    len -= region->tileDataSize;
2027
2028
732
    if (len > 0)
2029
732
      WLog_Print(progressive->log, WLOG_WARN,
2030
732
                 "Unused bytes detected, %" PRIdz " bytes not processed", len);
2031
732
  }
2032
2033
1.67k
  return rc;
2034
3.55k
}
2035
2036
static inline SSIZE_T progressive_wb_skip_region(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2037
                                                 wStream* WINPR_RESTRICT s, UINT16 blockType,
2038
                                                 UINT32 blockLen)
2039
1.10k
{
2040
1.10k
  PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region = &progressive->region;
2041
2042
1.10k
  const SSIZE_T rc =
2043
1.10k
      progressive_wb_read_region_header(progressive, s, blockType, blockLen, region);
2044
1.10k
  if (rc < 0)
2045
132
    return rc;
2046
2047
970
  if (!Stream_SafeSeek(s, WINPR_ASSERTING_INT_CAST(size_t, rc)))
2048
0
    return -1111;
2049
2050
970
  return rc;
2051
970
}
2052
2053
static inline SSIZE_T progressive_wb_region(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2054
                                            wStream* WINPR_RESTRICT s, UINT16 blockType,
2055
                                            UINT32 blockLen,
2056
                                            PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
2057
                                            PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region)
2058
1.83k
{
2059
1.83k
  SSIZE_T rc = -1;
2060
1.83k
  UINT16 boxLeft = 0;
2061
1.83k
  UINT16 boxTop = 0;
2062
1.83k
  UINT16 boxRight = 0;
2063
1.83k
  UINT16 boxBottom = 0;
2064
1.83k
  UINT16 idxLeft = 0;
2065
1.83k
  UINT16 idxTop = 0;
2066
1.83k
  UINT16 idxRight = 0;
2067
1.83k
  UINT16 idxBottom = 0;
2068
1.83k
  const PROGRESSIVE_BLOCK_CONTEXT* context = &progressive->context;
2069
2070
1.83k
  if ((progressive->state & FLAG_WBT_FRAME_BEGIN) == 0)
2071
727
  {
2072
727
    WLog_WARN(TAG, "RFX_PROGRESSIVE_REGION before RFX_PROGRESSIVE_FRAME_BEGIN, ignoring");
2073
727
    return progressive_wb_skip_region(progressive, s, blockType, blockLen);
2074
727
  }
2075
1.10k
  if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
2076
375
  {
2077
375
    WLog_WARN(TAG, "RFX_PROGRESSIVE_REGION after RFX_PROGRESSIVE_FRAME_END, ignoring");
2078
375
    return progressive_wb_skip_region(progressive, s, blockType, blockLen);
2079
375
  }
2080
2081
730
  progressive->state |= FLAG_WBT_REGION;
2082
2083
730
  rc = progressive_wb_read_region_header(progressive, s, blockType, blockLen, region);
2084
730
  if (rc < 0)
2085
25
    return rc;
2086
2087
21.0k
  for (UINT16 index = 0; index < region->numRects; index++)
2088
20.2k
  {
2089
20.2k
    RFX_RECT* rect = &(region->rects[index]);
2090
20.2k
    Stream_Read_UINT16(s, rect->x);
2091
20.2k
    Stream_Read_UINT16(s, rect->y);
2092
20.2k
    Stream_Read_UINT16(s, rect->width);
2093
20.2k
    Stream_Read_UINT16(s, rect->height);
2094
20.2k
  }
2095
2096
765
  for (BYTE index = 0; index < region->numQuant; index++)
2097
86
  {
2098
86
    RFX_COMPONENT_CODEC_QUANT* quantVal = &(region->quantVals[index]);
2099
86
    progressive_component_codec_quant_read(s, quantVal);
2100
2101
86
    if (!progressive_rfx_quant_lcmp_greater_equal(quantVal, 6))
2102
26
    {
2103
26
      WLog_Print(progressive->log, WLOG_ERROR,
2104
26
                 "ProgressiveRegion region->cQuant[%" PRIu32 "] < 6", index);
2105
26
      return -1;
2106
26
    }
2107
2108
60
    if (!progressive_rfx_quant_lcmp_less_equal(quantVal, 15))
2109
0
    {
2110
0
      WLog_Print(progressive->log, WLOG_ERROR,
2111
0
                 "ProgressiveRegion region->cQuant[%" PRIu32 "] > 15", index);
2112
0
      return -1;
2113
0
    }
2114
60
  }
2115
2116
1.78k
  for (BYTE index = 0; index < region->numProgQuant; index++)
2117
1.10k
  {
2118
1.10k
    RFX_PROGRESSIVE_CODEC_QUANT* quantProgVal = &(region->quantProgVals[index]);
2119
2120
1.10k
    Stream_Read_UINT8(s, quantProgVal->quality);
2121
2122
1.10k
    progressive_component_codec_quant_read(s, &(quantProgVal->yQuantValues));
2123
1.10k
    progressive_component_codec_quant_read(s, &(quantProgVal->cbQuantValues));
2124
1.10k
    progressive_component_codec_quant_read(s, &(quantProgVal->crQuantValues));
2125
1.10k
  }
2126
2127
#if defined(WITH_DEBUG_CODECS)
2128
  WLog_Print(progressive->log, WLOG_DEBUG,
2129
             "ProgressiveRegion: numRects: %" PRIu16 " numTiles: %" PRIu16
2130
             " tileDataSize: %" PRIu32 " flags: 0x%02" PRIX8 " numQuant: %" PRIu8
2131
             " numProgQuant: %" PRIu8 "",
2132
             region->numRects, region->numTiles, region->tileDataSize, region->flags,
2133
             region->numQuant, region->numProgQuant);
2134
#endif
2135
2136
1.35k
  boxLeft = WINPR_ASSERTING_INT_CAST(UINT16, surface->gridWidth);
2137
1.35k
  boxTop = WINPR_ASSERTING_INT_CAST(UINT16, surface->gridHeight);
2138
1.35k
  boxRight = 0;
2139
1.35k
  boxBottom = 0;
2140
2141
20.9k
  for (UINT16 index = 0; index < region->numRects; index++)
2142
20.2k
  {
2143
20.2k
    RFX_RECT* rect = &(region->rects[index]);
2144
20.2k
    idxLeft = rect->x / 64;
2145
20.2k
    idxTop = rect->y / 64;
2146
20.2k
    idxRight = (rect->x + rect->width + 63) / 64;
2147
20.2k
    idxBottom = (rect->y + rect->height + 63) / 64;
2148
2149
20.2k
    if (idxLeft < boxLeft)
2150
634
      boxLeft = idxLeft;
2151
2152
20.2k
    if (idxTop < boxTop)
2153
540
      boxTop = idxTop;
2154
2155
20.2k
    if (idxRight > boxRight)
2156
1.22k
      boxRight = idxRight;
2157
2158
20.2k
    if (idxBottom > boxBottom)
2159
1.41k
      boxBottom = idxBottom;
2160
2161
#if defined(WITH_DEBUG_CODECS)
2162
    WLog_Print(progressive->log, WLOG_DEBUG,
2163
               "rect[%" PRIu16 "]: x: %" PRIu16 " y: %" PRIu16 " w: %" PRIu16 " h: %" PRIu16 "",
2164
               index, rect->x, rect->y, rect->width, rect->height);
2165
#endif
2166
20.2k
  }
2167
2168
1.35k
  const SSIZE_T res = progressive_process_tiles(progressive, s, region, surface, context);
2169
1.35k
  if (res < 0)
2170
225
    return -1;
2171
454
  return rc;
2172
1.35k
}
2173
2174
static inline SSIZE_T progressive_parse_block(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2175
                                              wStream* WINPR_RESTRICT s,
2176
                                              PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
2177
                                              PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region)
2178
11.7k
{
2179
11.7k
  UINT16 blockType = 0;
2180
11.7k
  UINT32 blockLen = 0;
2181
11.7k
  SSIZE_T rc = -1;
2182
11.7k
  wStream sub = { 0 };
2183
2184
11.7k
  WINPR_ASSERT(progressive);
2185
2186
11.7k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
2187
691
    return -1;
2188
2189
11.0k
  Stream_Read_UINT16(s, blockType);
2190
11.0k
  Stream_Read_UINT32(s, blockLen);
2191
2192
11.0k
  if (blockLen < 6)
2193
1.40k
  {
2194
1.40k
    WLog_WARN(TAG, "Invalid blockLen %" PRIu32 ", expected >= 6", blockLen);
2195
1.40k
    return -1;
2196
1.40k
  }
2197
9.64k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, blockLen - 6))
2198
2.44k
    return -1;
2199
7.20k
  Stream_StaticConstInit(&sub, Stream_Pointer(s), blockLen - 6);
2200
7.20k
  Stream_Seek(s, blockLen - 6);
2201
2202
7.20k
  switch (blockType)
2203
7.20k
  {
2204
1.30k
    case PROGRESSIVE_WBT_SYNC:
2205
1.30k
      rc = progressive_wb_sync(progressive, &sub, blockType, blockLen);
2206
1.30k
      break;
2207
2208
720
    case PROGRESSIVE_WBT_FRAME_BEGIN:
2209
720
      rc = progressive_wb_frame_begin(progressive, &sub, blockType, blockLen);
2210
720
      break;
2211
2212
2.26k
    case PROGRESSIVE_WBT_FRAME_END:
2213
2.26k
      rc = progressive_wb_frame_end(progressive, &sub, blockType, blockLen);
2214
2.26k
      break;
2215
2216
797
    case PROGRESSIVE_WBT_CONTEXT:
2217
797
      rc = progressive_wb_context(progressive, &sub, blockType, blockLen);
2218
797
      break;
2219
2220
1.83k
    case PROGRESSIVE_WBT_REGION:
2221
1.83k
      rc = progressive_wb_region(progressive, &sub, blockType, blockLen, surface, region);
2222
1.83k
      break;
2223
2224
289
    default:
2225
289
      WLog_Print(progressive->log, WLOG_ERROR, "Invalid block type %04" PRIx16, blockType);
2226
289
      return -1;
2227
7.20k
  }
2228
2229
6.91k
  if (rc < 0)
2230
742
    return -1;
2231
2232
6.16k
  if (Stream_GetRemainingLength(&sub) > 0)
2233
18
  {
2234
18
    WLog_Print(progressive->log, WLOG_ERROR,
2235
18
               "block len %" PRIu32 " does not match read data %" PRIuz, blockLen,
2236
18
               blockLen - Stream_GetRemainingLength(&sub));
2237
18
    return -1;
2238
18
  }
2239
2240
6.15k
  return rc;
2241
6.16k
}
2242
2243
static inline BOOL update_tiles(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2244
                                PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
2245
                                BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep,
2246
                                UINT32 nXDst, UINT32 nYDst,
2247
                                PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
2248
                                REGION16* WINPR_RESTRICT invalidRegion)
2249
416
{
2250
416
  BOOL rc = TRUE;
2251
416
  REGION16 clippingRects = { 0 };
2252
416
  region16_init(&clippingRects);
2253
2254
12.7k
  for (UINT32 i = 0; i < region->numRects; i++)
2255
12.3k
  {
2256
12.3k
    RECTANGLE_16 clippingRect = { 0 };
2257
12.3k
    const RFX_RECT* rect = &(region->rects[i]);
2258
2259
12.3k
    clippingRect.left = (UINT16)nXDst + rect->x;
2260
12.3k
    clippingRect.top = (UINT16)nYDst + rect->y;
2261
12.3k
    clippingRect.right = clippingRect.left + rect->width;
2262
12.3k
    clippingRect.bottom = clippingRect.top + rect->height;
2263
12.3k
    region16_union_rect(&clippingRects, &clippingRects, &clippingRect);
2264
12.3k
  }
2265
2266
416
  for (UINT32 i = 0; i < surface->numUpdatedTiles; i++)
2267
0
  {
2268
0
    UINT32 nbUpdateRects = 0;
2269
0
    const RECTANGLE_16* updateRects = NULL;
2270
0
    RECTANGLE_16 updateRect = { 0 };
2271
2272
0
    WINPR_ASSERT(surface->updatedTileIndices);
2273
0
    const UINT32 index = surface->updatedTileIndices[i];
2274
2275
0
    WINPR_ASSERT(index < surface->tilesSize);
2276
0
    RFX_PROGRESSIVE_TILE* tile = surface->tiles[index];
2277
0
    WINPR_ASSERT(tile);
2278
2279
0
    const UINT32 dl = nXDst + tile->x;
2280
0
    updateRect.left = WINPR_ASSERTING_INT_CAST(UINT16, dl);
2281
2282
0
    const UINT32 dt = nYDst + tile->y;
2283
0
    updateRect.top = WINPR_ASSERTING_INT_CAST(UINT16, dt);
2284
0
    updateRect.right = updateRect.left + 64;
2285
0
    updateRect.bottom = updateRect.top + 64;
2286
2287
0
    REGION16 updateRegion = { 0 };
2288
0
    region16_init(&updateRegion);
2289
0
    region16_intersect_rect(&updateRegion, &clippingRects, &updateRect);
2290
0
    updateRects = region16_rects(&updateRegion, &nbUpdateRects);
2291
2292
0
    for (UINT32 j = 0; j < nbUpdateRects; j++)
2293
0
    {
2294
0
      rc = FALSE;
2295
0
      const RECTANGLE_16* rect = &updateRects[j];
2296
0
      if (rect->left < updateRect.left)
2297
0
        break;
2298
0
      const UINT32 nXSrc = rect->left - updateRect.left;
2299
0
      const UINT32 nYSrc = rect->top - updateRect.top;
2300
0
      const UINT32 width = rect->right - rect->left;
2301
0
      const UINT32 height = rect->bottom - rect->top;
2302
2303
0
      if (rect->left + width > surface->width)
2304
0
        break;
2305
0
      if (rect->top + height > surface->height)
2306
0
        break;
2307
0
      rc = freerdp_image_copy_no_overlap(
2308
0
          pDstData, DstFormat, nDstStep, rect->left, rect->top, width, height, tile->data,
2309
0
          progressive->format, tile->stride, nXSrc, nYSrc, NULL, FREERDP_KEEP_DST_ALPHA);
2310
0
      if (!rc)
2311
0
        break;
2312
2313
0
      if (invalidRegion)
2314
0
        region16_union_rect(invalidRegion, invalidRegion, rect);
2315
0
    }
2316
2317
0
    region16_uninit(&updateRegion);
2318
0
    if (!rc)
2319
0
      goto fail;
2320
0
    tile->dirty = FALSE;
2321
0
  }
2322
2323
416
fail:
2324
416
  region16_uninit(&clippingRects);
2325
416
  return rc;
2326
416
}
2327
2328
INT32 progressive_decompress(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2329
                             const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
2330
                             BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep,
2331
                             UINT32 nXDst, UINT32 nYDst, REGION16* WINPR_RESTRICT invalidRegion,
2332
                             UINT16 surfaceId, UINT32 frameId)
2333
6.00k
{
2334
6.00k
  INT32 rc = 1;
2335
2336
6.00k
  WINPR_ASSERT(progressive);
2337
6.00k
  PROGRESSIVE_SURFACE_CONTEXT* surface = progressive_get_surface_data(progressive, surfaceId);
2338
2339
6.00k
  if (!surface)
2340
0
  {
2341
0
    WLog_Print(progressive->log, WLOG_ERROR, "ProgressiveRegion no surface for %" PRIu16,
2342
0
               surfaceId);
2343
0
    return -1001;
2344
0
  }
2345
2346
6.00k
  PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region = &progressive->region;
2347
6.00k
  WINPR_ASSERT(region);
2348
2349
6.00k
  if (surface->frameId != frameId)
2350
0
  {
2351
0
    surface->frameId = frameId;
2352
0
    surface->numUpdatedTiles = 0;
2353
0
  }
2354
2355
6.00k
  wStream ss = { 0 };
2356
6.00k
  wStream* s = Stream_StaticConstInit(&ss, pSrcData, SrcSize);
2357
6.00k
  WINPR_ASSERT(s);
2358
2359
6.00k
  switch (DstFormat)
2360
6.00k
  {
2361
0
    case PIXEL_FORMAT_RGBA32:
2362
0
    case PIXEL_FORMAT_RGBX32:
2363
0
    case PIXEL_FORMAT_BGRA32:
2364
6.00k
    case PIXEL_FORMAT_BGRX32:
2365
6.00k
      progressive->format = DstFormat;
2366
6.00k
      break;
2367
0
    default:
2368
0
      progressive->format = PIXEL_FORMAT_XRGB32;
2369
0
      break;
2370
6.00k
  }
2371
2372
6.00k
  const size_t start = Stream_GetPosition(s);
2373
6.00k
  progressive->state = 0; /* Set state to not initialized */
2374
12.1k
  while (Stream_GetRemainingLength(s) > 0)
2375
11.7k
  {
2376
11.7k
    if (progressive_parse_block(progressive, s, surface, region) < 0)
2377
5.59k
      goto fail;
2378
11.7k
  }
2379
2380
416
  {
2381
416
    const size_t end = Stream_GetPosition(s);
2382
416
    if ((end - start) != SrcSize)
2383
0
    {
2384
0
      WLog_Print(progressive->log, WLOG_ERROR,
2385
0
                 "total block len %" PRIuz " does not match read data %" PRIu32, end - start,
2386
0
                 SrcSize);
2387
0
      rc = -1041;
2388
0
      goto fail;
2389
0
    }
2390
416
  }
2391
2392
416
  if (!update_tiles(progressive, surface, pDstData, DstFormat, nDstStep, nXDst, nYDst, region,
2393
416
                    invalidRegion))
2394
0
    return -2002;
2395
6.00k
fail:
2396
6.00k
  return rc;
2397
416
}
2398
2399
BOOL progressive_rfx_write_message_progressive_simple(
2400
    PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, wStream* WINPR_RESTRICT s,
2401
    const RFX_MESSAGE* WINPR_RESTRICT msg)
2402
0
{
2403
0
  RFX_CONTEXT* context = NULL;
2404
2405
0
  WINPR_ASSERT(progressive);
2406
0
  WINPR_ASSERT(s);
2407
0
  WINPR_ASSERT(msg);
2408
0
  context = progressive->rfx_context;
2409
0
  return rfx_write_message_progressive_simple(context, s, msg);
2410
0
}
2411
2412
int progressive_compress(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2413
                         const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, UINT32 SrcFormat,
2414
                         UINT32 Width, UINT32 Height, UINT32 ScanLine,
2415
                         const REGION16* WINPR_RESTRICT invalidRegion,
2416
                         BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize)
2417
0
{
2418
0
  BOOL rc = FALSE;
2419
0
  int res = -6;
2420
0
  wStream* s = NULL;
2421
0
  UINT32 numRects = 0;
2422
0
  RFX_RECT* rects = NULL;
2423
0
  RFX_MESSAGE* message = NULL;
2424
2425
0
  if (!progressive || !pSrcData || !ppDstData || !pDstSize)
2426
0
  {
2427
0
    return -1;
2428
0
  }
2429
2430
0
  if (ScanLine == 0)
2431
0
  {
2432
0
    switch (SrcFormat)
2433
0
    {
2434
0
      case PIXEL_FORMAT_ABGR32:
2435
0
      case PIXEL_FORMAT_ARGB32:
2436
0
      case PIXEL_FORMAT_XBGR32:
2437
0
      case PIXEL_FORMAT_XRGB32:
2438
0
      case PIXEL_FORMAT_BGRA32:
2439
0
      case PIXEL_FORMAT_BGRX32:
2440
0
      case PIXEL_FORMAT_RGBA32:
2441
0
      case PIXEL_FORMAT_RGBX32:
2442
0
        ScanLine = Width * 4;
2443
0
        break;
2444
0
      default:
2445
0
        return -2;
2446
0
    }
2447
0
  }
2448
2449
0
  if (SrcSize < Height * ScanLine)
2450
0
    return -4;
2451
2452
0
  if (!invalidRegion)
2453
0
  {
2454
0
    numRects = (Width + 63) / 64;
2455
0
    numRects *= (Height + 63) / 64;
2456
0
  }
2457
0
  else
2458
0
  {
2459
0
    const int nr = region16_n_rects(invalidRegion);
2460
0
    numRects = WINPR_ASSERTING_INT_CAST(uint32_t, nr);
2461
0
  }
2462
2463
0
  if (numRects == 0)
2464
0
    return 0;
2465
2466
0
  if (!Stream_EnsureRemainingCapacity(progressive->rects, numRects * sizeof(RFX_RECT)))
2467
0
    return -5;
2468
0
  rects = Stream_BufferAs(progressive->rects, RFX_RECT);
2469
0
  if (invalidRegion)
2470
0
  {
2471
0
    const RECTANGLE_16* region_rects = region16_rects(invalidRegion, NULL);
2472
0
    for (UINT32 idx = 0; idx < numRects; idx++)
2473
0
    {
2474
0
      const RECTANGLE_16* r = &region_rects[idx];
2475
0
      RFX_RECT* rect = &rects[idx];
2476
2477
0
      rect->x = r->left;
2478
0
      rect->y = r->top;
2479
0
      rect->width = r->right - r->left;
2480
0
      rect->height = r->bottom - r->top;
2481
0
    }
2482
0
  }
2483
0
  else
2484
0
  {
2485
0
    UINT16 x = 0;
2486
0
    UINT16 y = 0;
2487
2488
0
    for (UINT32 i = 0; i < numRects; i++)
2489
0
    {
2490
0
      RFX_RECT* r = &rects[i];
2491
0
      r->x = x;
2492
0
      r->y = y;
2493
2494
0
      WINPR_ASSERT(Width >= x);
2495
0
      WINPR_ASSERT(Height >= y);
2496
0
      r->width = MIN(64, WINPR_ASSERTING_INT_CAST(UINT16, Width - x));
2497
0
      r->height = MIN(64, WINPR_ASSERTING_INT_CAST(UINT16, Height - y));
2498
2499
0
      if (x + 64UL >= Width)
2500
0
      {
2501
0
        y += 64;
2502
0
        x = 0;
2503
0
      }
2504
0
      else
2505
0
        x += 64;
2506
2507
0
      WINPR_ASSERT(r->x % 64 == 0);
2508
0
      WINPR_ASSERT(r->y % 64 == 0);
2509
0
      WINPR_ASSERT(r->width <= 64);
2510
0
      WINPR_ASSERT(r->height <= 64);
2511
0
    }
2512
0
  }
2513
0
  s = progressive->buffer;
2514
0
  Stream_SetPosition(s, 0);
2515
2516
0
  progressive->rfx_context->mode = RLGR1;
2517
2518
0
  progressive->rfx_context->width = WINPR_ASSERTING_INT_CAST(UINT16, Width);
2519
0
  progressive->rfx_context->height = WINPR_ASSERTING_INT_CAST(UINT16, Height);
2520
0
  rfx_context_set_pixel_format(progressive->rfx_context, SrcFormat);
2521
0
  message = rfx_encode_message(progressive->rfx_context, rects, numRects, pSrcData, Width, Height,
2522
0
                               ScanLine);
2523
0
  if (!message)
2524
0
  {
2525
0
    WLog_ERR(TAG, "failed to encode rfx message");
2526
0
    goto fail;
2527
0
  }
2528
2529
0
  rc = progressive_rfx_write_message_progressive_simple(progressive, s, message);
2530
0
  rfx_message_free(progressive->rfx_context, message);
2531
0
  if (!rc)
2532
0
    goto fail;
2533
2534
0
  {
2535
0
    const size_t pos = Stream_GetPosition(s);
2536
0
    WINPR_ASSERT(pos <= UINT32_MAX);
2537
0
    *pDstSize = (UINT32)pos;
2538
0
  }
2539
0
  *ppDstData = Stream_Buffer(s);
2540
0
  res = 1;
2541
0
fail:
2542
0
  return res;
2543
0
}
2544
2545
BOOL progressive_context_reset(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive)
2546
0
{
2547
0
  if (!progressive)
2548
0
    return FALSE;
2549
2550
0
  return TRUE;
2551
0
}
2552
2553
PROGRESSIVE_CONTEXT* progressive_context_new(BOOL Compressor)
2554
6.00k
{
2555
6.00k
  return progressive_context_new_ex(Compressor, 0);
2556
6.00k
}
2557
2558
PROGRESSIVE_CONTEXT* progressive_context_new_ex(BOOL Compressor, UINT32 ThreadingFlags)
2559
6.00k
{
2560
6.00k
  PROGRESSIVE_CONTEXT* progressive =
2561
6.00k
      (PROGRESSIVE_CONTEXT*)winpr_aligned_calloc(1, sizeof(PROGRESSIVE_CONTEXT), 32);
2562
2563
6.00k
  if (!progressive)
2564
0
    return NULL;
2565
2566
6.00k
  progressive->Compressor = Compressor;
2567
6.00k
  progressive->quantProgValFull.quality = 100;
2568
6.00k
  progressive->log = WLog_Get(TAG);
2569
6.00k
  if (!progressive->log)
2570
0
    goto fail;
2571
6.00k
  progressive->rfx_context = rfx_context_new_ex(Compressor, ThreadingFlags);
2572
6.00k
  if (!progressive->rfx_context)
2573
0
    goto fail;
2574
6.00k
  progressive->buffer = Stream_New(NULL, 1024);
2575
6.00k
  if (!progressive->buffer)
2576
0
    goto fail;
2577
6.00k
  progressive->rects = Stream_New(NULL, 1024);
2578
6.00k
  if (!progressive->rects)
2579
0
    goto fail;
2580
6.00k
  progressive->bufferPool = BufferPool_New(TRUE, (8192LL + 32LL) * 3LL, 16);
2581
6.00k
  if (!progressive->bufferPool)
2582
0
    goto fail;
2583
6.00k
  progressive->SurfaceContexts = HashTable_New(TRUE);
2584
6.00k
  if (!progressive->SurfaceContexts)
2585
0
    goto fail;
2586
2587
6.00k
  {
2588
6.00k
    wObject* obj = HashTable_ValueObject(progressive->SurfaceContexts);
2589
6.00k
    WINPR_ASSERT(obj);
2590
6.00k
    obj->fnObjectFree = progressive_surface_context_free;
2591
6.00k
  }
2592
0
  return progressive;
2593
0
fail:
2594
0
  WINPR_PRAGMA_DIAG_PUSH
2595
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2596
0
  progressive_context_free(progressive);
2597
0
  WINPR_PRAGMA_DIAG_POP
2598
0
  return NULL;
2599
6.00k
}
2600
2601
void progressive_context_free(PROGRESSIVE_CONTEXT* progressive)
2602
6.00k
{
2603
6.00k
  if (!progressive)
2604
0
    return;
2605
2606
6.00k
  Stream_Free(progressive->buffer, TRUE);
2607
6.00k
  Stream_Free(progressive->rects, TRUE);
2608
6.00k
  rfx_context_free(progressive->rfx_context);
2609
2610
6.00k
  BufferPool_Free(progressive->bufferPool);
2611
6.00k
  HashTable_Free(progressive->SurfaceContexts);
2612
2613
6.00k
  winpr_aligned_free(progressive);
2614
6.00k
}