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