Coverage Report

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