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