Coverage Report

Created: 2026-10-15 06:31

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