Coverage Report

Created: 2025-10-10 06:50

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