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_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 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 if (!rfx_differential_decode(buffer + 4032, 64))
880 return -1;
881 if (!progressive_rfx_decode_block(prims, &buffer[0], 1024, shift->HL1)) /* HL1 */
882 return -1;
883 if (!progressive_rfx_decode_block(prims, &buffer[1024], 1024, shift->LH1)) /* LH1 */
884 return -1;
885 if (!progressive_rfx_decode_block(prims, &buffer[2048], 1024, shift->HH1)) /* HH1 */
886 return -1;
887 if (!progressive_rfx_decode_block(prims, &buffer[3072], 256, shift->HL2)) /* HL2 */
888 return -1;
889 if (!progressive_rfx_decode_block(prims, &buffer[3328], 256, shift->LH2)) /* LH2 */
890 return -1;
891 if (!progressive_rfx_decode_block(prims, &buffer[3584], 256, shift->HH2)) /* HH2 */
892 return -1;
893 if (!progressive_rfx_decode_block(prims, &buffer[3840], 64, shift->HL3)) /* HL3 */
894 return -1;
895 if (!progressive_rfx_decode_block(prims, &buffer[3904], 64, shift->LH3)) /* LH3 */
896 return -1;
897 if (!progressive_rfx_decode_block(prims, &buffer[3968], 64, shift->HH3)) /* HH3 */
898 return -1;
899 if (!progressive_rfx_decode_block(prims, &buffer[4032], 64, shift->LL3)) /* LL3 */
900 return -1;
901 }
902 else
903 {
904 if (!progressive_rfx_decode_block(prims, &buffer[0], 1023, shift->HL1)) /* HL1 */
905 return -1;
906 if (!progressive_rfx_decode_block(prims, &buffer[1023], 1023, shift->LH1)) /* LH1 */
907 return -1;
908 if (!progressive_rfx_decode_block(prims, &buffer[2046], 961, shift->HH1)) /* HH1 */
909 return -1;
910 if (!progressive_rfx_decode_block(prims, &buffer[3007], 272, shift->HL2)) /* HL2 */
911 return -1;
912 if (!progressive_rfx_decode_block(prims, &buffer[3279], 272, shift->LH2)) /* LH2 */
913 return -1;
914 if (!progressive_rfx_decode_block(prims, &buffer[3551], 256, shift->HH2)) /* HH2 */
915 return -1;
916 if (!progressive_rfx_decode_block(prims, &buffer[3807], 72, shift->HL3)) /* HL3 */
917 return -1;
918 if (!progressive_rfx_decode_block(prims, &buffer[3879], 72, shift->LH3)) /* LH3 */
919 return -1;
920 if (!progressive_rfx_decode_block(prims, &buffer[3951], 64, shift->HH3)) /* HH3 */
921 return -1;
922 if (!rfx_differential_decode(&buffer[4015], 81)) /* LL3 */
923 return -1;
924 if (!progressive_rfx_decode_block(prims, &buffer[4015], 81, shift->LL3)) /* LL3 */
925 return -1;
926 }
927 return progressive_rfx_dwt_2d_decode(progressive, buffer, current, coeffDiff, extrapolate,
928 FALSE);
929}
930
931static inline int
932progressive_decompress_tile_first(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
933 RFX_PROGRESSIVE_TILE* WINPR_RESTRICT tile,
934 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
935 const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
936{
937 int rc = 0;
938 BOOL diff = 0;
939 BOOL sub = 0;
940 BOOL extrapolate = 0;
941 BYTE* pBuffer = nullptr;
942 INT16* pSign[3];
943 INT16* pSrcDst[3];
944 INT16* pCurrent[3];
945 RFX_COMPONENT_CODEC_QUANT shiftY = WINPR_C_ARRAY_INIT;
946 RFX_COMPONENT_CODEC_QUANT shiftCb = WINPR_C_ARRAY_INIT;
947 RFX_COMPONENT_CODEC_QUANT shiftCr = WINPR_C_ARRAY_INIT;
948 RFX_COMPONENT_CODEC_QUANT* quantY = nullptr;
949 RFX_COMPONENT_CODEC_QUANT* quantCb = nullptr;
950 RFX_COMPONENT_CODEC_QUANT* quantCr = nullptr;
951 RFX_COMPONENT_CODEC_QUANT* quantProgY = nullptr;
952 RFX_COMPONENT_CODEC_QUANT* quantProgCb = nullptr;
953 RFX_COMPONENT_CODEC_QUANT* quantProgCr = nullptr;
954 RFX_PROGRESSIVE_CODEC_QUANT* quantProgVal = nullptr;
955 static const prim_size_t roi_64x64 = { 64, 64 };
956 const primitives_t* prims = primitives_get();
957
958 tile->pass = 1;
959 diff = tile->flags & RFX_TILE_DIFFERENCE;
960 sub = context->flags & RFX_SUBBAND_DIFFING;
961 extrapolate = region->flags & RFX_DWT_REDUCE_EXTRAPOLATE;
962
963#if defined(WITH_DEBUG_CODECS)
964 WLog_Print(progressive->log, WLOG_DEBUG,
965 "ProgressiveTile%s: quantIdx Y: %" PRIu8 " Cb: %" PRIu8 " Cr: %" PRIu8
966 " xIdx: %" PRIu16 " yIdx: %" PRIu16 " flags: 0x%02" PRIX8 " quality: %" PRIu8
967 " yLen: %" PRIu16 " cbLen: %" PRIu16 " crLen: %" PRIu16 " tailLen: %" PRIu16 "",
968 (tile->blockType == PROGRESSIVE_WBT_TILE_FIRST) ? "First" : "Simple",
969 tile->quantIdxY, tile->quantIdxCb, tile->quantIdxCr, tile->xIdx, tile->yIdx,
970 tile->flags, tile->quality, tile->yLen, tile->cbLen, tile->crLen, tile->tailLen);
971#endif
972
973 if (tile->quantIdxY >= region->numQuant)
974 {
975 WLog_ERR(TAG, "quantIdxY %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxY, region->numQuant);
976 return -1;
977 }
978
979 quantY = &(region->quantVals[tile->quantIdxY]);
980
981 if (tile->quantIdxCb >= region->numQuant)
982 {
983 WLog_ERR(TAG, "quantIdxCb %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCb,
984 region->numQuant);
985 return -1;
986 }
987
988 quantCb = &(region->quantVals[tile->quantIdxCb]);
989
990 if (tile->quantIdxCr >= region->numQuant)
991 {
992 WLog_ERR(TAG, "quantIdxCr %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCr,
993 region->numQuant);
994 return -1;
995 }
996
997 quantCr = &(region->quantVals[tile->quantIdxCr]);
998
999 if (tile->quality == 0xFF)
1000 {
1001 quantProgVal = &(progressive->quantProgValFull);
1002 }
1003 else
1004 {
1005 if (tile->quality >= region->numProgQuant)
1006 {
1007 WLog_ERR(TAG, "quality %" PRIu8 " > numProgQuant %" PRIu8, tile->quality,
1008 region->numProgQuant);
1009 return -1;
1010 }
1011
1012 quantProgVal = &(region->quantProgVals[tile->quality]);
1013 }
1014
1015 quantProgY = &(quantProgVal->yQuantValues);
1016 quantProgCb = &(quantProgVal->cbQuantValues);
1017 quantProgCr = &(quantProgVal->crQuantValues);
1018
1019 tile->yQuant = *quantY;
1020 tile->cbQuant = *quantCb;
1021 tile->crQuant = *quantCr;
1022 tile->yProgQuant = *quantProgY;
1023 tile->cbProgQuant = *quantProgCb;
1024 tile->crProgQuant = *quantProgCr;
1025
1026 progressive_rfx_quant_add(quantY, quantProgY, &(tile->yBitPos));
1027 progressive_rfx_quant_add(quantCb, quantProgCb, &(tile->cbBitPos));
1028 progressive_rfx_quant_add(quantCr, quantProgCr, &(tile->crBitPos));
1029 progressive_rfx_quant_add(quantY, quantProgY, &shiftY);
1030 if (!progressive_rfx_quant_lsub(&shiftY, 1)) /* -6 + 5 = -1 */
1031 goto fail;
1032 progressive_rfx_quant_add(quantCb, quantProgCb, &shiftCb);
1033 if (!progressive_rfx_quant_lsub(&shiftCb, 1)) /* -6 + 5 = -1 */
1034 goto fail;
1035 progressive_rfx_quant_add(quantCr, quantProgCr, &shiftCr);
1036 if (!progressive_rfx_quant_lsub(&shiftCr, 1)) /* -6 + 5 = -1 */
1037 goto fail;
1038
1039 pSign[0] =
1040 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->sign[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1041 pSign[1] =
1042 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->sign[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1043 pSign[2] =
1044 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->sign[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1045
1046 pCurrent[0] =
1047 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->current[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1048 pCurrent[1] =
1049 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->current[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1050 pCurrent[2] =
1051 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->current[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1052
1053 pBuffer = (BYTE*)BufferPool_Take(progressive->bufferPool, -1);
1054 pSrcDst[0] =
1055 WINPR_PACKED_ALIGN_CAST(INT16*, (&pBuffer[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1056 pSrcDst[1] =
1057 WINPR_PACKED_ALIGN_CAST(INT16*, (&pBuffer[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1058 pSrcDst[2] =
1059 WINPR_PACKED_ALIGN_CAST(INT16*, (&pBuffer[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1060
1061 rc = progressive_rfx_decode_component(progressive, &shiftY, tile->yData, tile->yLen, pSrcDst[0],
1062 pCurrent[0], pSign[0], diff, sub, extrapolate); /* Y */
1063 if (rc < 0)
1064 goto fail;
1065 rc = progressive_rfx_decode_component(progressive, &shiftCb, tile->cbData, tile->cbLen,
1066 pSrcDst[1], pCurrent[1], pSign[1], diff, sub,
1067 extrapolate); /* Cb */
1068 if (rc < 0)
1069 goto fail;
1070 rc = progressive_rfx_decode_component(progressive, &shiftCr, tile->crData, tile->crLen,
1071 pSrcDst[2], pCurrent[2], pSign[2], diff, sub,
1072 extrapolate); /* Cr */
1073 if (rc < 0)
1074 goto fail;
1075
1076 {
1077 const INT16** ptr = WINPR_REINTERPRET_CAST(pSrcDst, INT16**, const INT16**);
1078 rc = prims->yCbCrToRGB_16s8u_P3AC4R(ptr, 64 * 2, tile->data, tile->stride,
1079 progressive->format, &roi_64x64);
1080 }
1081fail:
1082 BufferPool_Return(progressive->bufferPool, pBuffer);
1083 return rc;
1084}
1085
1086static inline INT16 progressive_rfx_srl_read(RFX_PROGRESSIVE_UPGRADE_STATE* WINPR_RESTRICT state,
1087 UINT32 numBits)
1088{
1089 WINPR_ASSERT(state);
1090
1091 wBitStream* bs = state->srl;
1092 WINPR_ASSERT(bs);
1093
1094 if (state->nz)
1095 {
1096 state->nz--;
1097 return 0;
1098 }
1099
1100 const UINT32 k = state->kp / 8;
1101
1102 if (!state->mode)
1103 {
1104 /* zero encoding */
1105 const UINT32 bit = (bs->accumulator & 0x80000000) ? 1 : 0;
1106 BitStream_Shift(bs, 1);
1107
1108 if (!bit)
1109 {
1110 /* '0' bit, nz >= (1 << k), nz = (1 << k) */
1111 state->nz = (1 << k);
1112 state->kp += 4;
1113
1114 if (state->kp > 80)
1115 state->kp = 80;
1116
1117 state->nz--;
1118 return 0;
1119 }
1120 else
1121 {
1122 /* '1' bit, nz < (1 << k), nz = next k bits */
1123 state->nz = 0;
1124 state->mode = 1; /* unary encoding is next */
1125
1126 if (k)
1127 {
1128 bs->mask = ((1u << k) - 1);
1129 state->nz =
1130 WINPR_ASSERTING_INT_CAST(int16_t, ((bs->accumulator >> (32u - k)) & bs->mask));
1131 BitStream_Shift(bs, k);
1132 }
1133
1134 if (state->nz)
1135 {
1136 state->nz--;
1137 return 0;
1138 }
1139 }
1140 }
1141
1142 state->mode = 0; /* zero encoding is next */
1143 /* unary encoding */
1144 /* read sign bit */
1145 const UINT32 sign = (bs->accumulator & 0x80000000) ? 1 : 0;
1146 BitStream_Shift(bs, 1);
1147
1148 if (state->kp < 6)
1149 state->kp = 0;
1150 else
1151 state->kp -= 6;
1152
1153 if (numBits == 1)
1154 return sign ? -1 : 1;
1155
1156 UINT32 mag = 1;
1157 const UINT32 max = (1u << numBits) - 1;
1158
1159 while (mag < max)
1160 {
1161 const UINT32 bit = (bs->accumulator & 0x80000000) ? 1 : 0;
1162 BitStream_Shift(bs, 1);
1163
1164 if (bit)
1165 break;
1166
1167 mag++;
1168 }
1169
1170 if (mag > INT16_MAX)
1171 mag = INT16_MAX;
1172 return (INT16)(sign ? -1 * (int)mag : (INT16)mag);
1173}
1174
1175static inline int
1176progressive_rfx_upgrade_state_finish(RFX_PROGRESSIVE_UPGRADE_STATE* WINPR_RESTRICT state)
1177{
1178 UINT32 pad = 0;
1179 wBitStream* srl = nullptr;
1180 wBitStream* raw = nullptr;
1181 if (!state)
1182 return -1;
1183
1184 srl = state->srl;
1185 raw = state->raw;
1186 /* Read trailing bits from RAW/SRL bit streams */
1187 pad = (raw->position % 8) ? (8 - (raw->position % 8)) : 0;
1188
1189 if (pad)
1190 BitStream_Shift(raw, pad);
1191
1192 pad = (srl->position % 8) ? (8 - (srl->position % 8)) : 0;
1193
1194 if (pad)
1195 BitStream_Shift(srl, pad);
1196
1197 if (BitStream_GetRemainingLength(srl) == 8)
1198 BitStream_Shift(srl, 8);
1199
1200 return 1;
1201}
1202
1203static inline int16_t rawShift(wBitStream* raw, UINT32 numBits)
1204{
1205 WINPR_ASSERT(raw);
1206 WINPR_ASSERT(numBits > 0);
1207
1208 raw->mask = ((1u << numBits) - 1);
1209 const int16_t input = (int16_t)((raw->accumulator >> (32 - numBits)) & raw->mask);
1210 BitStream_Shift(raw, numBits);
1211 return input;
1212}
1213
1214static inline int progressive_rfx_upgrade_block(RFX_PROGRESSIVE_UPGRADE_STATE* WINPR_RESTRICT state,
1215 INT16* WINPR_RESTRICT buffer,
1216 INT16* WINPR_RESTRICT sign, UINT32 length,
1217 UINT32 shift, WINPR_ATTR_UNUSED UINT32 bitPos,
1218 UINT32 numBits)
1219{
1220 if (numBits < 1)
1221 return 1;
1222
1223 wBitStream* raw = state->raw;
1224 int32_t input = 0;
1225
1226 if (!state->nonLL)
1227 {
1228 for (UINT32 index = 0; index < length; index++)
1229 {
1230 input = rawShift(raw, numBits);
1231
1232 const int32_t shifted = input << shift;
1233 const int32_t val = buffer[index] + shifted;
1234 const int16_t ival = WINPR_ASSERTING_INT_CAST(int16_t, val);
1235 buffer[index] = ival;
1236 }
1237
1238 return 1;
1239 }
1240
1241 for (UINT32 index = 0; index < length; index++)
1242 {
1243 if (sign[index] > 0)
1244 {
1245 /* sign > 0, read from raw */
1246 input = rawShift(raw, numBits);
1247 }
1248 else if (sign[index] < 0)
1249 {
1250 /* sign < 0, read from raw */
1251 input = rawShift(raw, numBits);
1252 input *= -1;
1253 }
1254 else
1255 {
1256 /* sign == 0, read from srl */
1257 input = progressive_rfx_srl_read(state, numBits);
1258 sign[index] = WINPR_ASSERTING_INT_CAST(int16_t, input);
1259 }
1260
1261 const int32_t val = input << shift;
1262 const int32_t ival = buffer[index] + val;
1263 buffer[index] = WINPR_ASSERTING_INT_CAST(INT16, ival);
1264 }
1265
1266 return 1;
1267}
1268
1269static inline int progressive_rfx_upgrade_component(
1270 PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1271 const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT shift,
1272 const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT bitPos,
1273 const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT numBits, INT16* WINPR_RESTRICT buffer,
1274 INT16* WINPR_RESTRICT current, INT16* WINPR_RESTRICT sign, const BYTE* WINPR_RESTRICT srlData,
1275 UINT32 srlLen, const BYTE* WINPR_RESTRICT rawData, UINT32 rawLen, BOOL coeffDiff,
1276 WINPR_ATTR_UNUSED BOOL subbandDiff, BOOL extrapolate)
1277{
1278 int rc = 0;
1279 wBitStream s_srl = WINPR_C_ARRAY_INIT;
1280 wBitStream s_raw = WINPR_C_ARRAY_INIT;
1281 RFX_PROGRESSIVE_UPGRADE_STATE state = WINPR_C_ARRAY_INIT;
1282
1283 state.kp = 8;
1284 state.mode = 0;
1285 state.srl = &s_srl;
1286 state.raw = &s_raw;
1287 BitStream_Attach(state.srl, srlData, srlLen);
1288 BitStream_Fetch(state.srl);
1289 BitStream_Attach(state.raw, rawData, rawLen);
1290 BitStream_Fetch(state.raw);
1291
1292 state.nonLL = TRUE;
1293 rc = progressive_rfx_upgrade_block(&state, &current[0], &sign[0], 1023, shift->HL1, bitPos->HL1,
1294 numBits->HL1); /* HL1 */
1295 if (rc < 0)
1296 return rc;
1297 rc = progressive_rfx_upgrade_block(&state, &current[1023], &sign[1023], 1023, shift->LH1,
1298 bitPos->LH1, numBits->LH1); /* LH1 */
1299 if (rc < 0)
1300 return rc;
1301 rc = progressive_rfx_upgrade_block(&state, &current[2046], &sign[2046], 961, shift->HH1,
1302 bitPos->HH1, numBits->HH1); /* HH1 */
1303 if (rc < 0)
1304 return rc;
1305 rc = progressive_rfx_upgrade_block(&state, &current[3007], &sign[3007], 272, shift->HL2,
1306 bitPos->HL2, numBits->HL2); /* HL2 */
1307 if (rc < 0)
1308 return rc;
1309 rc = progressive_rfx_upgrade_block(&state, &current[3279], &sign[3279], 272, shift->LH2,
1310 bitPos->LH2, numBits->LH2); /* LH2 */
1311 if (rc < 0)
1312 return rc;
1313 rc = progressive_rfx_upgrade_block(&state, &current[3551], &sign[3551], 256, shift->HH2,
1314 bitPos->HH2, numBits->HH2); /* HH2 */
1315 if (rc < 0)
1316 return rc;
1317 rc = progressive_rfx_upgrade_block(&state, &current[3807], &sign[3807], 72, shift->HL3,
1318 bitPos->HL3, numBits->HL3); /* HL3 */
1319 if (rc < 0)
1320 return rc;
1321 rc = progressive_rfx_upgrade_block(&state, &current[3879], &sign[3879], 72, shift->LH3,
1322 bitPos->LH3, numBits->LH3); /* LH3 */
1323 if (rc < 0)
1324 return rc;
1325 rc = progressive_rfx_upgrade_block(&state, &current[3951], &sign[3951], 64, shift->HH3,
1326 bitPos->HH3, numBits->HH3); /* HH3 */
1327 if (rc < 0)
1328 return rc;
1329
1330 state.nonLL = FALSE;
1331 rc = progressive_rfx_upgrade_block(&state, &current[4015], &sign[4015], 81, shift->LL3,
1332 bitPos->LL3, numBits->LL3); /* LL3 */
1333 if (rc < 0)
1334 return rc;
1335 rc = progressive_rfx_upgrade_state_finish(&state);
1336 if (rc < 0)
1337 return rc;
1338 return progressive_rfx_dwt_2d_decode(progressive, buffer, current, coeffDiff, extrapolate,
1339 TRUE);
1340}
1341
1342static inline int
1343progressive_decompress_tile_upgrade(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1344 RFX_PROGRESSIVE_TILE* WINPR_RESTRICT tile,
1345 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1346 const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
1347{
1348 int status = 0;
1349 BOOL coeffDiff = 0;
1350 BOOL sub = 0;
1351 BOOL extrapolate = 0;
1352 BYTE* pBuffer = nullptr;
1353 INT16* pSign[3] = WINPR_C_ARRAY_INIT;
1354 INT16* pSrcDst[3] = WINPR_C_ARRAY_INIT;
1355 INT16* pCurrent[3] = WINPR_C_ARRAY_INIT;
1356 RFX_COMPONENT_CODEC_QUANT shiftY = WINPR_C_ARRAY_INIT;
1357 RFX_COMPONENT_CODEC_QUANT shiftCb = WINPR_C_ARRAY_INIT;
1358 RFX_COMPONENT_CODEC_QUANT shiftCr = WINPR_C_ARRAY_INIT;
1359 RFX_COMPONENT_CODEC_QUANT yBitPos = WINPR_C_ARRAY_INIT;
1360 RFX_COMPONENT_CODEC_QUANT cbBitPos = WINPR_C_ARRAY_INIT;
1361 RFX_COMPONENT_CODEC_QUANT crBitPos = WINPR_C_ARRAY_INIT;
1362 RFX_COMPONENT_CODEC_QUANT yNumBits = WINPR_C_ARRAY_INIT;
1363 RFX_COMPONENT_CODEC_QUANT cbNumBits = WINPR_C_ARRAY_INIT;
1364 RFX_COMPONENT_CODEC_QUANT crNumBits = WINPR_C_ARRAY_INIT;
1365 RFX_COMPONENT_CODEC_QUANT* quantY = nullptr;
1366 RFX_COMPONENT_CODEC_QUANT* quantCb = nullptr;
1367 RFX_COMPONENT_CODEC_QUANT* quantCr = nullptr;
1368 RFX_COMPONENT_CODEC_QUANT* quantProgY = nullptr;
1369 RFX_COMPONENT_CODEC_QUANT* quantProgCb = nullptr;
1370 RFX_COMPONENT_CODEC_QUANT* quantProgCr = nullptr;
1371 RFX_PROGRESSIVE_CODEC_QUANT* quantProg = nullptr;
1372 static const prim_size_t roi_64x64 = { 64, 64 };
1373 const primitives_t* prims = primitives_get();
1374
1375 coeffDiff = tile->flags & RFX_TILE_DIFFERENCE;
1376 sub = context->flags & RFX_SUBBAND_DIFFING;
1377 extrapolate = region->flags & RFX_DWT_REDUCE_EXTRAPOLATE;
1378
1379 tile->pass++;
1380
1381#if defined(WITH_DEBUG_CODECS)
1382 WLog_Print(progressive->log, WLOG_DEBUG,
1383 "ProgressiveTileUpgrade: pass: %" PRIu16 " quantIdx Y: %" PRIu8 " Cb: %" PRIu8
1384 " Cr: %" PRIu8 " xIdx: %" PRIu16 " yIdx: %" PRIu16 " quality: %" PRIu8
1385 " ySrlLen: %" PRIu16 " yRawLen: %" PRIu16 " cbSrlLen: %" PRIu16 " cbRawLen: %" PRIu16
1386 " crSrlLen: %" PRIu16 " crRawLen: %" PRIu16 "",
1387 tile->pass, tile->quantIdxY, tile->quantIdxCb, tile->quantIdxCr, tile->xIdx,
1388 tile->yIdx, tile->quality, tile->ySrlLen, tile->yRawLen, tile->cbSrlLen,
1389 tile->cbRawLen, tile->crSrlLen, tile->crRawLen);
1390#endif
1391
1392 if (tile->quantIdxY >= region->numQuant)
1393 {
1394 WLog_ERR(TAG, "quantIdxY %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxY, region->numQuant);
1395 return -1;
1396 }
1397
1398 quantY = &(region->quantVals[tile->quantIdxY]);
1399
1400 if (tile->quantIdxCb >= region->numQuant)
1401 {
1402 WLog_ERR(TAG, "quantIdxCb %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCb,
1403 region->numQuant);
1404 return -1;
1405 }
1406
1407 quantCb = &(region->quantVals[tile->quantIdxCb]);
1408
1409 if (tile->quantIdxCr >= region->numQuant)
1410 {
1411 WLog_ERR(TAG, "quantIdxCr %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCr,
1412 region->numQuant);
1413 return -1;
1414 }
1415
1416 quantCr = &(region->quantVals[tile->quantIdxCr]);
1417
1418 if (tile->quality == 0xFF)
1419 {
1420 quantProg = &(progressive->quantProgValFull);
1421 }
1422 else
1423 {
1424 if (tile->quality >= region->numProgQuant)
1425 {
1426 WLog_ERR(TAG, "quality %" PRIu8 " > numProgQuant %" PRIu8, tile->quality,
1427 region->numProgQuant);
1428 return -1;
1429 }
1430
1431 quantProg = &(region->quantProgVals[tile->quality]);
1432 }
1433
1434 quantProgY = &(quantProg->yQuantValues);
1435 quantProgCb = &(quantProg->cbQuantValues);
1436 quantProgCr = &(quantProg->crQuantValues);
1437
1438 if (!progressive_rfx_quant_cmp_equal(quantY, &(tile->yQuant)))
1439 WLog_Print(progressive->log, WLOG_WARN, "non-progressive quantY has changed!");
1440
1441 if (!progressive_rfx_quant_cmp_equal(quantCb, &(tile->cbQuant)))
1442 WLog_Print(progressive->log, WLOG_WARN, "non-progressive quantCb has changed!");
1443
1444 if (!progressive_rfx_quant_cmp_equal(quantCr, &(tile->crQuant)))
1445 WLog_Print(progressive->log, WLOG_WARN, "non-progressive quantCr has changed!");
1446
1447 if (!(context->flags & RFX_SUBBAND_DIFFING))
1448 WLog_WARN(TAG, "PROGRESSIVE_BLOCK_CONTEXT::flags & RFX_SUBBAND_DIFFING not set");
1449
1450 progressive_rfx_quant_add(quantY, quantProgY, &yBitPos);
1451 progressive_rfx_quant_add(quantCb, quantProgCb, &cbBitPos);
1452 progressive_rfx_quant_add(quantCr, quantProgCr, &crBitPos);
1453 if (!progressive_rfx_quant_sub(&(tile->yBitPos), &yBitPos, &yNumBits))
1454 goto fail;
1455 if (!progressive_rfx_quant_sub(&(tile->cbBitPos), &cbBitPos, &cbNumBits))
1456 goto fail;
1457 if (!progressive_rfx_quant_sub(&(tile->crBitPos), &crBitPos, &crNumBits))
1458 goto fail;
1459 progressive_rfx_quant_add(quantY, quantProgY, &shiftY);
1460 if (!progressive_rfx_quant_lsub(&shiftY, 1)) /* -6 + 5 = -1 */
1461 goto fail;
1462 progressive_rfx_quant_add(quantCb, quantProgCb, &shiftCb);
1463 if (!progressive_rfx_quant_lsub(&shiftCb, 1)) /* -6 + 5 = -1 */
1464 goto fail;
1465 progressive_rfx_quant_add(quantCr, quantProgCr, &shiftCr);
1466 if (!progressive_rfx_quant_lsub(&shiftCr, 1)) /* -6 + 5 = -1 */
1467 goto fail;
1468
1469 tile->yBitPos = yBitPos;
1470 tile->cbBitPos = cbBitPos;
1471 tile->crBitPos = crBitPos;
1472 tile->yQuant = *quantY;
1473 tile->cbQuant = *quantCb;
1474 tile->crQuant = *quantCr;
1475 tile->yProgQuant = *quantProgY;
1476 tile->cbProgQuant = *quantProgCb;
1477 tile->crProgQuant = *quantProgCr;
1478
1479 pSign[0] =
1480 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->sign[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1481 pSign[1] =
1482 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->sign[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1483 pSign[2] =
1484 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->sign[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1485
1486 pCurrent[0] =
1487 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->current[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1488 pCurrent[1] =
1489 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->current[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1490 pCurrent[2] =
1491 WINPR_PACKED_ALIGN_CAST(INT16*, (&tile->current[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1492
1493 pBuffer = (BYTE*)BufferPool_Take(progressive->bufferPool, -1);
1494 pSrcDst[0] =
1495 WINPR_PACKED_ALIGN_CAST(INT16*, (&pBuffer[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1496 pSrcDst[1] =
1497 WINPR_PACKED_ALIGN_CAST(INT16*, (&pBuffer[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1498 pSrcDst[2] =
1499 WINPR_PACKED_ALIGN_CAST(INT16*, (&pBuffer[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1500
1501 status = progressive_rfx_upgrade_component(progressive, &shiftY, quantProgY, &yNumBits,
1502 pSrcDst[0], pCurrent[0], pSign[0], tile->ySrlData,
1503 tile->ySrlLen, tile->yRawData, tile->yRawLen,
1504 coeffDiff, sub, extrapolate); /* Y */
1505
1506 if (status < 0)
1507 goto fail;
1508
1509 status = progressive_rfx_upgrade_component(progressive, &shiftCb, quantProgCb, &cbNumBits,
1510 pSrcDst[1], pCurrent[1], pSign[1], tile->cbSrlData,
1511 tile->cbSrlLen, tile->cbRawData, tile->cbRawLen,
1512 coeffDiff, sub, extrapolate); /* Cb */
1513
1514 if (status < 0)
1515 goto fail;
1516
1517 status = progressive_rfx_upgrade_component(progressive, &shiftCr, quantProgCr, &crNumBits,
1518 pSrcDst[2], pCurrent[2], pSign[2], tile->crSrlData,
1519 tile->crSrlLen, tile->crRawData, tile->crRawLen,
1520 coeffDiff, sub, extrapolate); /* Cr */
1521
1522 if (status < 0)
1523 goto fail;
1524
1525 {
1526 const INT16** ptr = WINPR_REINTERPRET_CAST(pSrcDst, INT16**, const INT16**);
1527 status = prims->yCbCrToRGB_16s8u_P3AC4R(ptr, 64 * 2, tile->data, tile->stride,
1528 progressive->format, &roi_64x64);
1529 }
1530fail:
1531 BufferPool_Return(progressive->bufferPool, pBuffer);
1532 return status;
1533}
1534
1535static inline BOOL progressive_tile_read_upgrade(
1536 PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, wStream* WINPR_RESTRICT s, UINT16 blockType,
1537 UINT32 blockLen, PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
1538 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1539 WINPR_ATTR_UNUSED const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
1540{
1541 RFX_PROGRESSIVE_TILE tile = WINPR_C_ARRAY_INIT;
1542 const size_t expect = 20;
1543
1544 if (!Stream_CheckAndLogRequiredLength(TAG, s, expect))
1545 return FALSE;
1546
1547 tile.blockType = blockType;
1548 tile.blockLen = blockLen;
1549 tile.flags = 0;
1550
1551 Stream_Read_UINT8(s, tile.quantIdxY);
1552 Stream_Read_UINT8(s, tile.quantIdxCb);
1553 Stream_Read_UINT8(s, tile.quantIdxCr);
1554 Stream_Read_UINT16(s, tile.xIdx);
1555 Stream_Read_UINT16(s, tile.yIdx);
1556 Stream_Read_UINT8(s, tile.quality);
1557 Stream_Read_UINT16(s, tile.ySrlLen);
1558 Stream_Read_UINT16(s, tile.yRawLen);
1559 Stream_Read_UINT16(s, tile.cbSrlLen);
1560 Stream_Read_UINT16(s, tile.cbRawLen);
1561 Stream_Read_UINT16(s, tile.crSrlLen);
1562 Stream_Read_UINT16(s, tile.crRawLen);
1563
1564 tile.ySrlData = Stream_Pointer(s);
1565 if (!Stream_SafeSeek(s, tile.ySrlLen))
1566 {
1567 WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.ySrlLen);
1568 return FALSE;
1569 }
1570
1571 tile.yRawData = Stream_Pointer(s);
1572 if (!Stream_SafeSeek(s, tile.yRawLen))
1573 {
1574 WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.yRawLen);
1575 return FALSE;
1576 }
1577
1578 tile.cbSrlData = Stream_Pointer(s);
1579 if (!Stream_SafeSeek(s, tile.cbSrlLen))
1580 {
1581 WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
1582 tile.cbSrlLen);
1583 return FALSE;
1584 }
1585
1586 tile.cbRawData = Stream_Pointer(s);
1587 if (!Stream_SafeSeek(s, tile.cbRawLen))
1588 {
1589 WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
1590 tile.cbRawLen);
1591 return FALSE;
1592 }
1593
1594 tile.crSrlData = Stream_Pointer(s);
1595 if (!Stream_SafeSeek(s, tile.crSrlLen))
1596 {
1597 WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
1598 tile.crSrlLen);
1599 return FALSE;
1600 }
1601
1602 tile.crRawData = Stream_Pointer(s);
1603 if (!Stream_SafeSeek(s, tile.crRawLen))
1604 {
1605 WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
1606 tile.crRawLen);
1607 return FALSE;
1608 }
1609
1610 return progressive_surface_tile_replace(surface, region, &tile, TRUE);
1611}
1612
1613static inline BOOL
1614progressive_tile_read(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, BOOL simple,
1615 wStream* WINPR_RESTRICT s, UINT16 blockType, UINT32 blockLen,
1616 PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
1617 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1618 WINPR_ATTR_UNUSED const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
1619{
1620 RFX_PROGRESSIVE_TILE tile = WINPR_C_ARRAY_INIT;
1621 size_t expect = simple ? 16 : 17;
1622
1623 if (!Stream_CheckAndLogRequiredLength(TAG, s, expect))
1624 return FALSE;
1625
1626 tile.blockType = blockType;
1627 tile.blockLen = blockLen;
1628
1629 Stream_Read_UINT8(s, tile.quantIdxY);
1630 Stream_Read_UINT8(s, tile.quantIdxCb);
1631 Stream_Read_UINT8(s, tile.quantIdxCr);
1632 Stream_Read_UINT16(s, tile.xIdx);
1633 Stream_Read_UINT16(s, tile.yIdx);
1634 Stream_Read_UINT8(s, tile.flags);
1635
1636 if (!simple)
1637 Stream_Read_UINT8(s, tile.quality);
1638 else
1639 tile.quality = 0xFF;
1640 Stream_Read_UINT16(s, tile.yLen);
1641 Stream_Read_UINT16(s, tile.cbLen);
1642 Stream_Read_UINT16(s, tile.crLen);
1643 Stream_Read_UINT16(s, tile.tailLen);
1644
1645 tile.yData = Stream_Pointer(s);
1646 if (!Stream_SafeSeek(s, tile.yLen))
1647 {
1648 WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.yLen);
1649 return FALSE;
1650 }
1651
1652 tile.cbData = Stream_Pointer(s);
1653 if (!Stream_SafeSeek(s, tile.cbLen))
1654 {
1655 WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.cbLen);
1656 return FALSE;
1657 }
1658
1659 tile.crData = Stream_Pointer(s);
1660 if (!Stream_SafeSeek(s, tile.crLen))
1661 {
1662 WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.crLen);
1663 return FALSE;
1664 }
1665
1666 tile.tailData = Stream_Pointer(s);
1667 if (!Stream_SafeSeek(s, tile.tailLen))
1668 {
1669 WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.tailLen);
1670 return FALSE;
1671 }
1672
1673 return progressive_surface_tile_replace(surface, region, &tile, FALSE);
1674}
1675
1676static void CALLBACK progressive_process_tiles_tile_work_callback(PTP_CALLBACK_INSTANCE instance,
1677 void* context, PTP_WORK work)
1678{
1680
1681 WINPR_UNUSED(instance);
1682 WINPR_UNUSED(work);
1683
1684 switch (param->tile->blockType)
1685 {
1686 case PROGRESSIVE_WBT_TILE_SIMPLE:
1687 case PROGRESSIVE_WBT_TILE_FIRST:
1688 progressive_decompress_tile_first(param->progressive, param->tile, param->region,
1689 param->context);
1690 break;
1691
1692 case PROGRESSIVE_WBT_TILE_UPGRADE:
1693 progressive_decompress_tile_upgrade(param->progressive, param->tile, param->region,
1694 param->context);
1695 break;
1696 default:
1697 WLog_Print(param->progressive->log, WLOG_ERROR, "Invalid block type %04" PRIx16 " (%s)",
1698 param->tile->blockType,
1699 rfx_get_progressive_block_type_string(param->tile->blockType));
1700 break;
1701 }
1702}
1703
1704static inline SSIZE_T
1705progressive_process_tiles(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1706 wStream* WINPR_RESTRICT s,
1707 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1708 PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
1709 const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
1710{
1711 int status = 0;
1712 size_t end = 0;
1713 const size_t start = Stream_GetPosition(s);
1714 UINT16 blockType = 0;
1715 UINT32 blockLen = 0;
1716 UINT32 count = 0;
1717 UINT16 close_cnt = 0;
1718
1719 WINPR_ASSERT(progressive);
1720 WINPR_ASSERT(region);
1721
1722 if (!Stream_CheckAndLogRequiredLength(TAG, s, region->tileDataSize))
1723 return -1;
1724
1725 while ((Stream_GetRemainingLength(s) >= 6) &&
1726 (region->tileDataSize > (Stream_GetPosition(s) - start)))
1727 {
1728 const size_t pos = Stream_GetPosition(s);
1729
1730 Stream_Read_UINT16(s, blockType);
1731 Stream_Read_UINT32(s, blockLen);
1732
1733#if defined(WITH_DEBUG_CODECS)
1734 WLog_Print(progressive->log, WLOG_DEBUG, "%s",
1735 rfx_get_progressive_block_type_string(blockType));
1736#endif
1737
1738 if (blockLen < 6)
1739 {
1740 WLog_Print(progressive->log, WLOG_ERROR, "Expected >= %" PRIu32 " remaining %" PRIu32,
1741 6u, blockLen);
1742 return -1003;
1743 }
1744 if (!Stream_CheckAndLogRequiredLength(TAG, s, blockLen - 6))
1745 return -1003;
1746
1747 switch (blockType)
1748 {
1749 case PROGRESSIVE_WBT_TILE_SIMPLE:
1750 if (!progressive_tile_read(progressive, TRUE, s, blockType, blockLen, surface,
1751 region, context))
1752 return -1022;
1753 break;
1754
1755 case PROGRESSIVE_WBT_TILE_FIRST:
1756 if (!progressive_tile_read(progressive, FALSE, s, blockType, blockLen, surface,
1757 region, context))
1758 return -1027;
1759 break;
1760
1761 case PROGRESSIVE_WBT_TILE_UPGRADE:
1762 if (!progressive_tile_read_upgrade(progressive, s, blockType, blockLen, surface,
1763 region, context))
1764 return -1032;
1765 break;
1766 default:
1767 WLog_ERR(TAG, "Invalid block type %04" PRIx16 " (%s)", blockType,
1768 rfx_get_progressive_block_type_string(blockType));
1769 return -1039;
1770 }
1771
1772 size_t rem = Stream_GetPosition(s);
1773 if ((rem - pos) != blockLen)
1774 {
1775 WLog_Print(progressive->log, WLOG_ERROR,
1776 "Actual block read %" PRIuz " but expected %" PRIu32, rem - pos, blockLen);
1777 return -1040;
1778 }
1779 count++;
1780 }
1781
1782 end = Stream_GetPosition(s);
1783 if ((end - start) != region->tileDataSize)
1784 {
1785 WLog_Print(progressive->log, WLOG_ERROR,
1786 "Actual total blocks read %" PRIuz " but expected %" PRIu32, end - start,
1787 region->tileDataSize);
1788 return -1041;
1789 }
1790
1791 if (count != region->numTiles)
1792 {
1793 WLog_Print(progressive->log, WLOG_WARN,
1794 "numTiles inconsistency: actual: %" PRIu32 ", expected: %" PRIu16 "\n", count,
1795 region->numTiles);
1796 return -1044;
1797 }
1798
1799 for (UINT32 idx = 0; idx < region->numTiles; idx++)
1800 {
1801 RFX_PROGRESSIVE_TILE* tile = region->tiles[idx];
1802 PROGRESSIVE_TILE_PROCESS_WORK_PARAM* param = &progressive->params[idx];
1803 param->progressive = progressive;
1804 param->region = region;
1805 param->context = context;
1806 param->tile = tile;
1807
1808 if (progressive->rfx_context->priv->UseThreads)
1809 {
1810 progressive->work_objects[idx] = CreateThreadpoolWork(
1811 progressive_process_tiles_tile_work_callback, (void*)param, nullptr);
1812 if (!progressive->work_objects[idx])
1813 {
1814 WLog_Print(progressive->log, WLOG_ERROR,
1815 "Failed to create ThreadpoolWork for tile %" PRIu32, idx);
1816 status = -1;
1817 break;
1818 }
1819
1820 SubmitThreadpoolWork(progressive->work_objects[idx]);
1821
1822 close_cnt = WINPR_ASSERTING_INT_CAST(UINT16, idx + 1);
1823 }
1824 else
1825 {
1826 progressive_process_tiles_tile_work_callback(nullptr, param, nullptr);
1827 }
1828
1829 if (status < 0)
1830 {
1831 WLog_Print(progressive->log, WLOG_ERROR, "Failed to decompress %s at %" PRIu16,
1832 rfx_get_progressive_block_type_string(tile->blockType), idx);
1833 goto fail;
1834 }
1835 }
1836
1837 if (progressive->rfx_context->priv->UseThreads)
1838 {
1839 for (UINT32 idx = 0; idx < close_cnt; idx++)
1840 {
1841 WaitForThreadpoolWorkCallbacks(progressive->work_objects[idx], FALSE);
1842 CloseThreadpoolWork(progressive->work_objects[idx]);
1843 }
1844 }
1845
1846fail:
1847
1848 if (status < 0)
1849 return -1;
1850
1851 return (SSIZE_T)(end - start);
1852}
1853
1854static inline SSIZE_T progressive_wb_sync(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1855 wStream* WINPR_RESTRICT s, UINT16 blockType,
1856 UINT32 blockLen)
1857{
1858 const UINT32 magic = 0xCACCACCA;
1859 const UINT16 version = 0x0100;
1860 PROGRESSIVE_BLOCK_SYNC sync = WINPR_C_ARRAY_INIT;
1861
1862 sync.blockType = blockType;
1863 sync.blockLen = blockLen;
1864
1865 if (sync.blockLen != 12)
1866 {
1867 WLog_Print(progressive->log, WLOG_ERROR,
1868 "PROGRESSIVE_BLOCK_SYNC::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
1869 sync.blockLen, 12u);
1870 return -1005;
1871 }
1872
1873 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
1874 return -1004;
1875
1876#if defined(WITH_DEBUG_CODECS)
1877 WLog_Print(progressive->log, WLOG_DEBUG, "ProgressiveSync");
1878#endif
1879
1880 Stream_Read_UINT32(s, sync.magic);
1881 Stream_Read_UINT16(s, sync.version);
1882
1883 if (sync.magic != magic)
1884 {
1885 WLog_Print(progressive->log, WLOG_ERROR,
1886 "PROGRESSIVE_BLOCK_SYNC::magic = 0x%08" PRIx32 " != 0x%08" PRIx32, sync.magic,
1887 magic);
1888 return -1005;
1889 }
1890
1891 if (sync.version != 0x0100)
1892 {
1893 WLog_Print(progressive->log, WLOG_ERROR,
1894 "PROGRESSIVE_BLOCK_SYNC::version = 0x%04" PRIx16 " != 0x%04" PRIu16,
1895 sync.version, version);
1896 return -1006;
1897 }
1898
1899 if ((progressive->state & FLAG_WBT_SYNC) != 0)
1900 WLog_WARN(TAG, "Duplicate PROGRESSIVE_BLOCK_SYNC, ignoring");
1901
1902 progressive->state |= FLAG_WBT_SYNC;
1903 return 0;
1904}
1905
1906static inline SSIZE_T progressive_wb_frame_begin(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1907 wStream* WINPR_RESTRICT s, UINT16 blockType,
1908 UINT32 blockLen)
1909{
1910 PROGRESSIVE_BLOCK_FRAME_BEGIN frameBegin = WINPR_C_ARRAY_INIT;
1911
1912 frameBegin.blockType = blockType;
1913 frameBegin.blockLen = blockLen;
1914
1915 if (frameBegin.blockLen != 12)
1916 {
1917 WLog_Print(progressive->log, WLOG_ERROR,
1918 " RFX_PROGRESSIVE_FRAME_BEGIN::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
1919 frameBegin.blockLen, 12u);
1920 return -1005;
1921 }
1922
1923 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
1924 return -1007;
1925
1926 Stream_Read_UINT32(s, frameBegin.frameIndex);
1927 Stream_Read_UINT16(s, frameBegin.regionCount);
1928
1929#if defined(WITH_DEBUG_CODECS)
1930 WLog_Print(progressive->log, WLOG_DEBUG,
1931 "ProgressiveFrameBegin: frameIndex: %" PRIu32 " regionCount: %" PRIu16 "",
1932 frameBegin.frameIndex, frameBegin.regionCount);
1933#endif
1934
1941 if ((progressive->state & FLAG_WBT_FRAME_BEGIN) != 0)
1942 {
1943 WLog_ERR(TAG, "Duplicate RFX_PROGRESSIVE_FRAME_BEGIN in stream, this is not allowed!");
1944 return -1008;
1945 }
1946
1947 if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
1948 {
1949 WLog_ERR(TAG, "RFX_PROGRESSIVE_FRAME_BEGIN after RFX_PROGRESSIVE_FRAME_END in stream, this "
1950 "is not allowed!");
1951 return -1008;
1952 }
1953
1954 progressive->state |= FLAG_WBT_FRAME_BEGIN;
1955 return 0;
1956}
1957
1958static inline SSIZE_T progressive_wb_frame_end(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1959 wStream* WINPR_RESTRICT s, UINT16 blockType,
1960 UINT32 blockLen)
1961{
1962 PROGRESSIVE_BLOCK_FRAME_END frameEnd = WINPR_C_ARRAY_INIT;
1963
1964 frameEnd.blockType = blockType;
1965 frameEnd.blockLen = blockLen;
1966
1967 if (frameEnd.blockLen != 6)
1968 {
1969 WLog_Print(progressive->log, WLOG_ERROR,
1970 " RFX_PROGRESSIVE_FRAME_END::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
1971 frameEnd.blockLen, 6u);
1972 return -1005;
1973 }
1974
1975 if (Stream_GetRemainingLength(s) != 0)
1976 {
1977 WLog_Print(progressive->log, WLOG_ERROR,
1978 "ProgressiveFrameEnd short %" PRIuz ", expected %u",
1979 Stream_GetRemainingLength(s), 0U);
1980 return -1008;
1981 }
1982
1983#if defined(WITH_DEBUG_CODECS)
1984 WLog_Print(progressive->log, WLOG_DEBUG, "ProgressiveFrameEnd");
1985#endif
1986
1987 if ((progressive->state & FLAG_WBT_FRAME_BEGIN) == 0)
1988 WLog_WARN(TAG, "RFX_PROGRESSIVE_FRAME_END before RFX_PROGRESSIVE_FRAME_BEGIN, ignoring");
1989 if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
1990 WLog_WARN(TAG, "Duplicate RFX_PROGRESSIVE_FRAME_END, ignoring");
1991
1992 progressive->state |= FLAG_WBT_FRAME_END;
1993 return 0;
1994}
1995
1996static inline SSIZE_T progressive_wb_context(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1997 wStream* WINPR_RESTRICT s, UINT16 blockType,
1998 UINT32 blockLen)
1999{
2000 PROGRESSIVE_BLOCK_CONTEXT* context = &progressive->context;
2001 context->blockType = blockType;
2002 context->blockLen = blockLen;
2003
2004 if (context->blockLen != 10)
2005 {
2006 WLog_Print(progressive->log, WLOG_ERROR,
2007 "RFX_PROGRESSIVE_CONTEXT::blockLen = 0x%08" PRIx32 " != 0x%08x",
2008 context->blockLen, 10u);
2009 return -1005;
2010 }
2011
2012 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2013 return -1009;
2014
2015 Stream_Read_UINT8(s, context->ctxId);
2016 Stream_Read_UINT16(s, context->tileSize);
2017 Stream_Read_UINT8(s, context->flags);
2018
2019 if (context->ctxId != 0x00)
2020 WLog_WARN(TAG, "RFX_PROGRESSIVE_CONTEXT::ctxId != 0x00: %" PRIu8, context->ctxId);
2021
2022 if (context->tileSize != 64)
2023 {
2024 WLog_ERR(TAG, "RFX_PROGRESSIVE_CONTEXT::tileSize != 0x40: %" PRIu16, context->tileSize);
2025 return -1010;
2026 }
2027
2028 if ((progressive->state & FLAG_WBT_FRAME_BEGIN) != 0)
2029 WLog_WARN(TAG, "RFX_PROGRESSIVE_CONTEXT received after RFX_PROGRESSIVE_FRAME_BEGIN");
2030 if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
2031 WLog_WARN(TAG, "RFX_PROGRESSIVE_CONTEXT received after RFX_PROGRESSIVE_FRAME_END");
2032 if ((progressive->state & FLAG_WBT_CONTEXT) != 0)
2033 WLog_WARN(TAG, "Duplicate RFX_PROGRESSIVE_CONTEXT received, ignoring.");
2034
2035#if defined(WITH_DEBUG_CODECS)
2036 WLog_Print(progressive->log, WLOG_DEBUG, "ProgressiveContext: flags: 0x%02" PRIX8 "",
2037 context->flags);
2038#endif
2039
2040 progressive->state |= FLAG_WBT_CONTEXT;
2041 return 0;
2042}
2043
2044static inline SSIZE_T
2045progressive_wb_read_region_header(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2046 wStream* WINPR_RESTRICT s, UINT16 blockType, UINT32 blockLen,
2047 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region)
2048{
2049 region->usedTiles = 0;
2050
2051 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2052 return -1011;
2053
2054 region->blockType = blockType;
2055 region->blockLen = blockLen;
2056 Stream_Read_UINT8(s, region->tileSize);
2057 Stream_Read_UINT16(s, region->numRects);
2058 Stream_Read_UINT8(s, region->numQuant);
2059 Stream_Read_UINT8(s, region->numProgQuant);
2060 Stream_Read_UINT8(s, region->flags);
2061 Stream_Read_UINT16(s, region->numTiles);
2062 Stream_Read_UINT32(s, region->tileDataSize);
2063
2064 if (region->tileSize != 64)
2065 {
2066 WLog_Print(progressive->log, WLOG_ERROR,
2067 "ProgressiveRegion tile size %" PRIu8 ", expected %u", region->tileSize, 64U);
2068 return -1012;
2069 }
2070
2071 if (region->numRects < 1)
2072 {
2073 WLog_Print(progressive->log, WLOG_ERROR, "ProgressiveRegion missing rect count %" PRIu16,
2074 region->numRects);
2075 return -1013;
2076 }
2077
2078 if (region->numQuant > 7)
2079 {
2080 WLog_Print(progressive->log, WLOG_ERROR,
2081 "ProgressiveRegion quant count too high %" PRIu8 ", expected < %u",
2082 region->numQuant, 7U);
2083 return -1014;
2084 }
2085
2086 const SSIZE_T rc = WINPR_ASSERTING_INT_CAST(SSIZE_T, Stream_GetRemainingLength(s));
2087 const SSIZE_T expect = region->numRects * 8ll + region->numQuant * 5ll +
2088 region->numProgQuant * 16ll + region->tileDataSize;
2089 SSIZE_T len = rc;
2090 if (expect != rc)
2091 {
2092 if (len / 8LL < region->numRects)
2093 {
2094 WLog_Print(progressive->log, WLOG_ERROR,
2095 "ProgressiveRegion data short for region->rects");
2096 return -1015;
2097 }
2098 len -= region->numRects * 8LL;
2099
2100 if (len / 5LL < region->numQuant)
2101 {
2102 WLog_Print(progressive->log, WLOG_ERROR,
2103 "ProgressiveRegion data short for region->cQuant");
2104 return -1018;
2105 }
2106 len -= region->numQuant * 5LL;
2107
2108 if (len / 16LL < region->numProgQuant)
2109 {
2110 WLog_Print(progressive->log, WLOG_ERROR,
2111 "ProgressiveRegion data short for region->cProgQuant");
2112 return -1021;
2113 }
2114 len -= region->numProgQuant * 16LL;
2115
2116 if (len < region->tileDataSize * 1ll)
2117 {
2118 WLog_Print(progressive->log, WLOG_ERROR,
2119 "ProgressiveRegion data short for region->tiles");
2120 return -1024;
2121 }
2122 len -= region->tileDataSize;
2123
2124 if (len > 0)
2125 WLog_Print(progressive->log, WLOG_WARN,
2126 "Unused bytes detected, %" PRIdz " bytes not processed", len);
2127 }
2128
2129 return rc;
2130}
2131
2132static inline SSIZE_T progressive_wb_skip_region(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2133 wStream* WINPR_RESTRICT s, UINT16 blockType,
2134 UINT32 blockLen)
2135{
2136 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region = &progressive->region;
2137
2138 const SSIZE_T rc =
2139 progressive_wb_read_region_header(progressive, s, blockType, blockLen, region);
2140 if (rc < 0)
2141 return rc;
2142
2143 if (!Stream_SafeSeek(s, WINPR_ASSERTING_INT_CAST(size_t, rc)))
2144 return -1111;
2145
2146 return rc;
2147}
2148
2149static inline SSIZE_T progressive_wb_region(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2150 wStream* WINPR_RESTRICT s, UINT16 blockType,
2151 UINT32 blockLen,
2152 PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
2153 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region)
2154{
2155 SSIZE_T rc = -1;
2156 UINT16 boxLeft = 0;
2157 UINT16 boxTop = 0;
2158 UINT16 boxRight = 0;
2159 UINT16 boxBottom = 0;
2160 UINT16 idxLeft = 0;
2161 UINT16 idxTop = 0;
2162 UINT16 idxRight = 0;
2163 UINT16 idxBottom = 0;
2164 const PROGRESSIVE_BLOCK_CONTEXT* context = &progressive->context;
2165
2166 if ((progressive->state & FLAG_WBT_FRAME_BEGIN) == 0)
2167 {
2168 WLog_WARN(TAG, "RFX_PROGRESSIVE_REGION before RFX_PROGRESSIVE_FRAME_BEGIN, ignoring");
2169 return progressive_wb_skip_region(progressive, s, blockType, blockLen);
2170 }
2171 if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
2172 {
2173 WLog_WARN(TAG, "RFX_PROGRESSIVE_REGION after RFX_PROGRESSIVE_FRAME_END, ignoring");
2174 return progressive_wb_skip_region(progressive, s, blockType, blockLen);
2175 }
2176
2177 progressive->state |= FLAG_WBT_REGION;
2178
2179 rc = progressive_wb_read_region_header(progressive, s, blockType, blockLen, region);
2180 if (rc < 0)
2181 return rc;
2182
2183 for (UINT16 index = 0; index < region->numRects; index++)
2184 {
2185 RFX_RECT* rect = &(region->rects[index]);
2186 Stream_Read_UINT16(s, rect->x);
2187 Stream_Read_UINT16(s, rect->y);
2188 Stream_Read_UINT16(s, rect->width);
2189 Stream_Read_UINT16(s, rect->height);
2190 }
2191
2192 for (BYTE index = 0; index < region->numQuant; index++)
2193 {
2194 RFX_COMPONENT_CODEC_QUANT* quantVal = &(region->quantVals[index]);
2195 progressive_component_codec_quant_read(s, quantVal);
2196
2197 if (!progressive_rfx_quant_lcmp_greater_equal(quantVal, 6))
2198 {
2199 WLog_Print(progressive->log, WLOG_ERROR,
2200 "ProgressiveRegion region->cQuant[%" PRIu32 "] < 6", index);
2201 return -1;
2202 }
2203
2204 if (!progressive_rfx_quant_lcmp_less_equal(quantVal, 15))
2205 {
2206 WLog_Print(progressive->log, WLOG_ERROR,
2207 "ProgressiveRegion region->cQuant[%" PRIu32 "] > 15", index);
2208 return -1;
2209 }
2210 }
2211
2212 for (BYTE index = 0; index < region->numProgQuant; index++)
2213 {
2214 RFX_PROGRESSIVE_CODEC_QUANT* quantProgVal = &(region->quantProgVals[index]);
2215
2216 Stream_Read_UINT8(s, quantProgVal->quality);
2217
2218 progressive_component_codec_quant_read(s, &(quantProgVal->yQuantValues));
2219 progressive_component_codec_quant_read(s, &(quantProgVal->cbQuantValues));
2220 progressive_component_codec_quant_read(s, &(quantProgVal->crQuantValues));
2221 }
2222
2223#if defined(WITH_DEBUG_CODECS)
2224 WLog_Print(progressive->log, WLOG_DEBUG,
2225 "ProgressiveRegion: numRects: %" PRIu16 " numTiles: %" PRIu16
2226 " tileDataSize: %" PRIu32 " flags: 0x%02" PRIX8 " numQuant: %" PRIu8
2227 " numProgQuant: %" PRIu8 "",
2228 region->numRects, region->numTiles, region->tileDataSize, region->flags,
2229 region->numQuant, region->numProgQuant);
2230#endif
2231
2232 boxLeft = WINPR_ASSERTING_INT_CAST(UINT16, surface->gridWidth);
2233 boxTop = WINPR_ASSERTING_INT_CAST(UINT16, surface->gridHeight);
2234 boxRight = 0;
2235 boxBottom = 0;
2236
2237 for (UINT16 index = 0; index < region->numRects; index++)
2238 {
2239 RFX_RECT* rect = &(region->rects[index]);
2240 idxLeft = rect->x / 64;
2241 idxTop = rect->y / 64;
2242 idxRight = (rect->x + rect->width + 63) / 64;
2243 idxBottom = (rect->y + rect->height + 63) / 64;
2244
2245 if (idxLeft < boxLeft)
2246 boxLeft = idxLeft;
2247
2248 if (idxTop < boxTop)
2249 boxTop = idxTop;
2250
2251 if (idxRight > boxRight)
2252 boxRight = idxRight;
2253
2254 if (idxBottom > boxBottom)
2255 boxBottom = idxBottom;
2256
2257#if defined(WITH_DEBUG_CODECS)
2258 WLog_Print(progressive->log, WLOG_DEBUG,
2259 "rect[%" PRIu16 "]: x: %" PRIu16 " y: %" PRIu16 " w: %" PRIu16 " h: %" PRIu16 "",
2260 index, rect->x, rect->y, rect->width, rect->height);
2261#endif
2262 }
2263
2264 const SSIZE_T res = progressive_process_tiles(progressive, s, region, surface, context);
2265 if (res < 0)
2266 return -1;
2267 return rc;
2268}
2269
2270static inline SSIZE_T progressive_parse_block(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2271 wStream* WINPR_RESTRICT s,
2272 PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
2273 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region)
2274{
2275 UINT16 blockType = 0;
2276 UINT32 blockLen = 0;
2277 SSIZE_T rc = -1;
2278 wStream sub = WINPR_C_ARRAY_INIT;
2279
2280 WINPR_ASSERT(progressive);
2281
2282 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
2283 return -1;
2284
2285 Stream_Read_UINT16(s, blockType);
2286 Stream_Read_UINT32(s, blockLen);
2287
2288 if (blockLen < 6)
2289 {
2290 WLog_WARN(TAG, "Invalid blockLen %" PRIu32 ", expected >= 6", blockLen);
2291 return -1;
2292 }
2293 if (!Stream_CheckAndLogRequiredLength(TAG, s, blockLen - 6))
2294 return -1;
2295 Stream_StaticConstInit(&sub, Stream_Pointer(s), blockLen - 6);
2296 Stream_Seek(s, blockLen - 6);
2297
2298 switch (blockType)
2299 {
2300 case PROGRESSIVE_WBT_SYNC:
2301 rc = progressive_wb_sync(progressive, &sub, blockType, blockLen);
2302 break;
2303
2304 case PROGRESSIVE_WBT_FRAME_BEGIN:
2305 rc = progressive_wb_frame_begin(progressive, &sub, blockType, blockLen);
2306 break;
2307
2308 case PROGRESSIVE_WBT_FRAME_END:
2309 rc = progressive_wb_frame_end(progressive, &sub, blockType, blockLen);
2310 break;
2311
2312 case PROGRESSIVE_WBT_CONTEXT:
2313 rc = progressive_wb_context(progressive, &sub, blockType, blockLen);
2314 break;
2315
2316 case PROGRESSIVE_WBT_REGION:
2317 rc = progressive_wb_region(progressive, &sub, blockType, blockLen, surface, region);
2318 break;
2319
2320 default:
2321 WLog_Print(progressive->log, WLOG_ERROR, "Invalid block type %04" PRIx16, blockType);
2322 return -1;
2323 }
2324
2325 if (rc < 0)
2326 return -1;
2327
2328 if (Stream_GetRemainingLength(&sub) > 0)
2329 {
2330 WLog_Print(progressive->log, WLOG_ERROR,
2331 "block len %" PRIu32 " does not match read data %" PRIuz, blockLen,
2332 blockLen - Stream_GetRemainingLength(&sub));
2333 return -1;
2334 }
2335
2336 return rc;
2337}
2338
2339static inline BOOL update_tiles(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2340 PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
2341 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep,
2342 UINT32 nXDst, UINT32 nYDst,
2343 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
2344 REGION16* WINPR_RESTRICT invalidRegion)
2345{
2346 BOOL rc = TRUE;
2347 REGION16 clippingRects = WINPR_C_ARRAY_INIT;
2348 region16_init(&clippingRects);
2349
2350 for (UINT32 i = 0; i < region->numRects; i++)
2351 {
2352 RECTANGLE_16 clippingRect = WINPR_C_ARRAY_INIT;
2353 const RFX_RECT* rect = &(region->rects[i]);
2354
2355 clippingRect.left = (UINT16)nXDst + rect->x;
2356 clippingRect.top = (UINT16)nYDst + rect->y;
2357 clippingRect.right = clippingRect.left + rect->width;
2358 clippingRect.bottom = clippingRect.top + rect->height;
2359 if (!region16_union_rect(&clippingRects, &clippingRects, &clippingRect))
2360 {
2361 region16_uninit(&clippingRects);
2362 return FALSE;
2363 }
2364 }
2365
2366 for (UINT32 i = 0; i < surface->numUpdatedTiles; i++)
2367 {
2368 UINT32 nbUpdateRects = 0;
2369 const RECTANGLE_16* updateRects = nullptr;
2370 RECTANGLE_16 updateRect = WINPR_C_ARRAY_INIT;
2371
2372 WINPR_ASSERT(surface->updatedTileIndices);
2373 const UINT32 index = surface->updatedTileIndices[i];
2374
2375 WINPR_ASSERT(index < surface->tilesSize);
2376 RFX_PROGRESSIVE_TILE* tile = surface->tiles[index];
2377 WINPR_ASSERT(tile);
2378
2379 const UINT32 dl = nXDst + tile->x;
2380 updateRect.left = WINPR_ASSERTING_INT_CAST(UINT16, dl);
2381
2382 const UINT32 dt = nYDst + tile->y;
2383 updateRect.top = WINPR_ASSERTING_INT_CAST(UINT16, dt);
2384 updateRect.right = updateRect.left + 64;
2385 updateRect.bottom = updateRect.top + 64;
2386
2387 REGION16 updateRegion = WINPR_C_ARRAY_INIT;
2388 region16_init(&updateRegion);
2389 if (!region16_intersect_rect(&updateRegion, &clippingRects, &updateRect))
2390 {
2391 region16_uninit(&updateRegion);
2392 goto fail;
2393 }
2394 updateRects = region16_rects(&updateRegion, &nbUpdateRects);
2395
2396 for (UINT32 j = 0; j < nbUpdateRects; j++)
2397 {
2398 rc = FALSE;
2399 const RECTANGLE_16* rect = &updateRects[j];
2400 if (rect->left < updateRect.left)
2401 break;
2402 const UINT32 nXSrc = rect->left - updateRect.left;
2403 const UINT32 nYSrc = rect->top - updateRect.top;
2404 const UINT32 width = rect->right - rect->left;
2405 const UINT32 height = rect->bottom - rect->top;
2406
2407 if (rect->left + width > surface->width)
2408 break;
2409 if (rect->top + height > surface->height)
2410 break;
2411 rc = freerdp_image_copy_no_overlap(
2412 pDstData, DstFormat, nDstStep, rect->left, rect->top, width, height, tile->data,
2413 progressive->format, tile->stride, nXSrc, nYSrc, nullptr, FREERDP_KEEP_DST_ALPHA);
2414 if (!rc)
2415 break;
2416
2417 if (invalidRegion)
2418 {
2419 if (!region16_union_rect(invalidRegion, invalidRegion, rect))
2420 {
2421 region16_uninit(&updateRegion);
2422 goto fail;
2423 }
2424 }
2425 }
2426
2427 region16_uninit(&updateRegion);
2428 if (!rc)
2429 goto fail;
2430 tile->dirty = FALSE;
2431 }
2432
2433fail:
2434 region16_uninit(&clippingRects);
2435 return rc;
2436}
2437
2438INT32 progressive_decompress(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2439 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
2440 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep,
2441 UINT32 nXDst, UINT32 nYDst, REGION16* WINPR_RESTRICT invalidRegion,
2442 UINT16 surfaceId, UINT32 frameId)
2443{
2444 INT32 rc = 1;
2445
2446 WINPR_ASSERT(progressive);
2447 PROGRESSIVE_SURFACE_CONTEXT* surface = progressive_get_surface_data(progressive, surfaceId);
2448
2449 if (!surface)
2450 {
2451 WLog_Print(progressive->log, WLOG_ERROR, "ProgressiveRegion no surface for %" PRIu16,
2452 surfaceId);
2453 return -1001;
2454 }
2455
2456 PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region = &progressive->region;
2457 WINPR_ASSERT(region);
2458
2459 if (surface->frameId != frameId)
2460 {
2461 surface->frameId = frameId;
2462 surface->numUpdatedTiles = 0;
2463 }
2464
2465 wStream ss = WINPR_C_ARRAY_INIT;
2466 wStream* s = Stream_StaticConstInit(&ss, pSrcData, SrcSize);
2467 WINPR_ASSERT(s);
2468
2469 switch (DstFormat)
2470 {
2471 case PIXEL_FORMAT_RGBA32:
2472 case PIXEL_FORMAT_RGBX32:
2473 case PIXEL_FORMAT_BGRA32:
2474 case PIXEL_FORMAT_BGRX32:
2475 progressive->format = DstFormat;
2476 break;
2477 default:
2478 progressive->format = PIXEL_FORMAT_XRGB32;
2479 break;
2480 }
2481
2482 const size_t start = Stream_GetPosition(s);
2483 progressive->state = 0; /* Set state to not initialized */
2484 while (Stream_GetRemainingLength(s) > 0)
2485 {
2486 if (progressive_parse_block(progressive, s, surface, region) < 0)
2487 goto fail;
2488 }
2489
2490 {
2491 const size_t end = Stream_GetPosition(s);
2492 if ((end - start) != SrcSize)
2493 {
2494 WLog_Print(progressive->log, WLOG_ERROR,
2495 "total block len %" PRIuz " does not match read data %" PRIu32, end - start,
2496 SrcSize);
2497 rc = -1041;
2498 goto fail;
2499 }
2500 }
2501
2502 if (!update_tiles(progressive, surface, pDstData, DstFormat, nDstStep, nXDst, nYDst, region,
2503 invalidRegion))
2504 return -2002;
2505fail:
2506 return rc;
2507}
2508
2509BOOL progressive_rfx_write_message_progressive_simple(
2510 PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, wStream* WINPR_RESTRICT s,
2511 const RFX_MESSAGE* WINPR_RESTRICT msg)
2512{
2513 RFX_CONTEXT* context = nullptr;
2514
2515 WINPR_ASSERT(progressive);
2516 WINPR_ASSERT(s);
2517 WINPR_ASSERT(msg);
2518 context = progressive->rfx_context;
2519 return rfx_write_message_progressive_simple(context, s, msg);
2520}
2521
2522int progressive_compress(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2523 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, UINT32 SrcFormat,
2524 UINT32 Width, UINT32 Height, UINT32 ScanLine,
2525 const REGION16* WINPR_RESTRICT invalidRegion,
2526 BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize)
2527{
2528 BOOL rc = FALSE;
2529 int res = -6;
2530 wStream* s = nullptr;
2531 UINT32 numRects = 0;
2532 RFX_RECT* rects = nullptr;
2533 RFX_MESSAGE* message = nullptr;
2534
2535 if (!progressive || !pSrcData || !ppDstData || !pDstSize)
2536 {
2537 return -1;
2538 }
2539
2540 if (ScanLine == 0)
2541 {
2542 switch (SrcFormat)
2543 {
2544 case PIXEL_FORMAT_ABGR32:
2545 case PIXEL_FORMAT_ARGB32:
2546 case PIXEL_FORMAT_XBGR32:
2547 case PIXEL_FORMAT_XRGB32:
2548 case PIXEL_FORMAT_BGRA32:
2549 case PIXEL_FORMAT_BGRX32:
2550 case PIXEL_FORMAT_RGBA32:
2551 case PIXEL_FORMAT_RGBX32:
2552 ScanLine = Width * 4;
2553 break;
2554 default:
2555 return -2;
2556 }
2557 }
2558
2559 if (SrcSize < Height * ScanLine)
2560 return -4;
2561
2562 if (!invalidRegion)
2563 {
2564 numRects = (Width + 63) / 64;
2565 numRects *= (Height + 63) / 64;
2566 }
2567 else
2568 {
2569 const int nr = region16_n_rects(invalidRegion);
2570 numRects = WINPR_ASSERTING_INT_CAST(uint32_t, nr);
2571 }
2572
2573 if (numRects == 0)
2574 return 0;
2575
2576 if (!Stream_EnsureRemainingCapacity(progressive->rects, numRects * sizeof(RFX_RECT)))
2577 return -5;
2578 rects = Stream_BufferAs(progressive->rects, RFX_RECT);
2579 if (invalidRegion)
2580 {
2581 const RECTANGLE_16* region_rects = region16_rects(invalidRegion, nullptr);
2582 for (UINT32 idx = 0; idx < numRects; idx++)
2583 {
2584 const RECTANGLE_16* r = &region_rects[idx];
2585 RFX_RECT* rect = &rects[idx];
2586
2587 rect->x = r->left;
2588 rect->y = r->top;
2589 rect->width = r->right - r->left;
2590 rect->height = r->bottom - r->top;
2591 }
2592 }
2593 else
2594 {
2595 UINT16 x = 0;
2596 UINT16 y = 0;
2597
2598 for (UINT32 i = 0; i < numRects; i++)
2599 {
2600 RFX_RECT* r = &rects[i];
2601 r->x = x;
2602 r->y = y;
2603
2604 WINPR_ASSERT(Width >= x);
2605 WINPR_ASSERT(Height >= y);
2606 r->width = MIN(64, WINPR_ASSERTING_INT_CAST(UINT16, Width - x));
2607 r->height = MIN(64, WINPR_ASSERTING_INT_CAST(UINT16, Height - y));
2608
2609 if (x + 64UL >= Width)
2610 {
2611 y += 64;
2612 x = 0;
2613 }
2614 else
2615 x += 64;
2616
2617 WINPR_ASSERT(r->x % 64 == 0);
2618 WINPR_ASSERT(r->y % 64 == 0);
2619 WINPR_ASSERT(r->width <= 64);
2620 WINPR_ASSERT(r->height <= 64);
2621 }
2622 }
2623 s = progressive->buffer;
2624 Stream_ResetPosition(s);
2625
2626 progressive->rfx_context->mode = RLGR1;
2627
2628 progressive->rfx_context->width = WINPR_ASSERTING_INT_CAST(UINT16, Width);
2629 progressive->rfx_context->height = WINPR_ASSERTING_INT_CAST(UINT16, Height);
2630 rfx_context_set_pixel_format(progressive->rfx_context, SrcFormat);
2631 message = rfx_encode_message(progressive->rfx_context, rects, numRects, pSrcData, Width, Height,
2632 ScanLine);
2633 if (!message)
2634 {
2635 WLog_ERR(TAG, "failed to encode rfx message");
2636 goto fail;
2637 }
2638
2639 rc = progressive_rfx_write_message_progressive_simple(progressive, s, message);
2640 rfx_message_free(progressive->rfx_context, message);
2641 if (!rc)
2642 goto fail;
2643
2644 {
2645 const size_t pos = Stream_GetPosition(s);
2646 WINPR_ASSERT(pos <= UINT32_MAX);
2647 *pDstSize = (UINT32)pos;
2648 }
2649 *ppDstData = Stream_Buffer(s);
2650 res = 1;
2651fail:
2652 return res;
2653}
2654
2655BOOL progressive_context_reset(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive)
2656{
2657 return (progressive != nullptr);
2658}
2659
2660PROGRESSIVE_CONTEXT* progressive_context_new(BOOL Compressor)
2661{
2662 return progressive_context_new_ex(Compressor, 0);
2663}
2664
2665PROGRESSIVE_CONTEXT* progressive_context_new_ex(BOOL Compressor, UINT32 ThreadingFlags)
2666{
2667 PROGRESSIVE_CONTEXT* progressive =
2668 (PROGRESSIVE_CONTEXT*)winpr_aligned_calloc(1, sizeof(PROGRESSIVE_CONTEXT), 32);
2669
2670 if (!progressive)
2671 return nullptr;
2672
2673 progressive->Compressor = Compressor;
2674 progressive->quantProgValFull.quality = 100;
2675 progressive->log = WLog_Get(TAG);
2676 if (!progressive->log)
2677 goto fail;
2678 progressive->rfx_context = rfx_context_new_ex(Compressor, ThreadingFlags);
2679 if (!progressive->rfx_context)
2680 goto fail;
2681 progressive->buffer = Stream_New(nullptr, 1024);
2682 if (!progressive->buffer)
2683 goto fail;
2684 progressive->rects = Stream_New(nullptr, 1024);
2685 if (!progressive->rects)
2686 goto fail;
2687 progressive->bufferPool = BufferPool_New(TRUE, (8192LL + 32LL) * 3LL, 16);
2688 if (!progressive->bufferPool)
2689 goto fail;
2690 progressive->SurfaceContexts = HashTable_New(TRUE);
2691 if (!progressive->SurfaceContexts)
2692 goto fail;
2693
2694 {
2695 wObject* obj = HashTable_ValueObject(progressive->SurfaceContexts);
2696 WINPR_ASSERT(obj);
2697 obj->fnObjectFree = progressive_surface_context_free;
2698 }
2699 return progressive;
2700fail:
2701 WINPR_PRAGMA_DIAG_PUSH
2702 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2703 progressive_context_free(progressive);
2704 WINPR_PRAGMA_DIAG_POP
2705 return nullptr;
2706}
2707
2708void progressive_context_free(PROGRESSIVE_CONTEXT* progressive)
2709{
2710 if (!progressive)
2711 return;
2712
2713 Stream_Free(progressive->buffer, TRUE);
2714 Stream_Free(progressive->rects, TRUE);
2715 rfx_context_free(progressive->rfx_context);
2716
2717 BufferPool_Free(progressive->bufferPool);
2718 HashTable_Free(progressive->SurfaceContexts);
2719
2720 winpr_aligned_free(progressive);
2721}
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