Coverage Report

Created: 2026-02-26 06:50

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