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