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