Coverage Report

Created: 2024-09-08 06:18

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