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