Coverage Report

Created: 2026-01-16 07:10

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