Coverage Report

Created: 2026-09-14 06:32

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