Coverage Report

Created: 2026-10-28 06:54

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