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