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