/src/FreeRDP/libfreerdp/codec/rfx.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * RemoteFX Codec Library |
4 | | * |
5 | | * Copyright 2011 Vic Lee |
6 | | * Copyright 2015 Thincast Technologies GmbH |
7 | | * Copyright 2015 Norbert Federa <norbert.federa@thincast.com> |
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 <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | |
28 | | #include <winpr/assert.h> |
29 | | #include <winpr/cast.h> |
30 | | #include <winpr/crt.h> |
31 | | #include <winpr/tchar.h> |
32 | | #include <winpr/sysinfo.h> |
33 | | #include <winpr/registry.h> |
34 | | |
35 | | #include <freerdp/log.h> |
36 | | #include <freerdp/settings.h> |
37 | | #include <freerdp/codec/rfx.h> |
38 | | #include <freerdp/constants.h> |
39 | | #include <freerdp/primitives.h> |
40 | | #include <freerdp/codec/region.h> |
41 | | #include <freerdp/build-config.h> |
42 | | |
43 | | #include "rfx_constants.h" |
44 | | #include "rfx_types.h" |
45 | | #include "rfx_decode.h" |
46 | | #include "rfx_encode.h" |
47 | | #include "rfx_quantization.h" |
48 | | #include "rfx_dwt.h" |
49 | | #include "rfx_rlgr.h" |
50 | | |
51 | | #include "sse/rfx_sse2.h" |
52 | | #include "neon/rfx_neon.h" |
53 | | |
54 | | #define TAG FREERDP_TAG("codec") |
55 | | |
56 | 12.0k | #define RFX_KEY "Software\\" FREERDP_VENDOR_STRING "\\" FREERDP_PRODUCT_STRING "\\RemoteFX" |
57 | | |
58 | | /** |
59 | | * The quantization values control the compression rate and quality. The value |
60 | | * range is between 6 and 15. The higher value, the higher compression rate |
61 | | * and lower quality. |
62 | | * |
63 | | * This is the default values being use by the MS RDP server, and we will also |
64 | | * use it as our default values for the encoder. It can be overridden by setting |
65 | | * the context->num_quants and context->quants member. |
66 | | * |
67 | | * The order of the values are: |
68 | | * LL3, LH3, HL3, HH3, LH2, HL2, HH2, LH1, HL1, HH1 |
69 | | */ |
70 | | static const UINT32 rfx_default_quantization_values[] = { 6, 6, 6, 6, 7, 7, 8, 8, 8, 9 }; |
71 | | |
72 | | static inline BOOL rfx_write_progressive_tile_simple(RFX_CONTEXT* WINPR_RESTRICT rfx, |
73 | | wStream* WINPR_RESTRICT s, |
74 | | const RFX_TILE* WINPR_RESTRICT tile); |
75 | | |
76 | | static inline void rfx_profiler_create(RFX_CONTEXT* WINPR_RESTRICT context) |
77 | 12.0k | { |
78 | 12.0k | if (!context || !context->priv) |
79 | 0 | return; |
80 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_decode_rgb, "rfx_decode_rgb") |
81 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_decode_component, "rfx_decode_component") |
82 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_rlgr_decode, "rfx_rlgr_decode") |
83 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_differential_decode, "rfx_differential_decode") |
84 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_quantization_decode, "rfx_quantization_decode") |
85 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_decode, "rfx_dwt_2d_decode") |
86 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_ycbcr_to_rgb, "prims->yCbCrToRGB") |
87 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_encode_rgb, "rfx_encode_rgb") |
88 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_encode_component, "rfx_encode_component") |
89 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_rlgr_encode, "rfx_rlgr_encode") |
90 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_differential_encode, "rfx_differential_encode") |
91 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_quantization_encode, "rfx_quantization_encode") |
92 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_dwt_2d_encode, "rfx_dwt_2d_encode") |
93 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_rgb_to_ycbcr, "prims->RGBToYCbCr") |
94 | 12.0k | PROFILER_CREATE(context->priv->prof_rfx_encode_format_rgb, "rfx_encode_format_rgb") |
95 | 12.0k | } |
96 | | |
97 | | static inline void rfx_profiler_free(RFX_CONTEXT* WINPR_RESTRICT context) |
98 | 12.0k | { |
99 | 12.0k | if (!context || !context->priv) |
100 | 0 | return; |
101 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_decode_rgb) |
102 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_decode_component) |
103 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_rlgr_decode) |
104 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_differential_decode) |
105 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_quantization_decode) |
106 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_dwt_2d_decode) |
107 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_ycbcr_to_rgb) |
108 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_encode_rgb) |
109 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_encode_component) |
110 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_rlgr_encode) |
111 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_differential_encode) |
112 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_quantization_encode) |
113 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_dwt_2d_encode) |
114 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_rgb_to_ycbcr) |
115 | 12.0k | PROFILER_FREE(context->priv->prof_rfx_encode_format_rgb) |
116 | 12.0k | } |
117 | | |
118 | | static inline void rfx_profiler_print(RFX_CONTEXT* WINPR_RESTRICT context) |
119 | 12.0k | { |
120 | 12.0k | if (!context || !context->priv) |
121 | 0 | return; |
122 | | |
123 | 12.0k | PROFILER_PRINT_HEADER |
124 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_decode_rgb) |
125 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_decode_component) |
126 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_rlgr_decode) |
127 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_differential_decode) |
128 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_quantization_decode) |
129 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_decode) |
130 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_ycbcr_to_rgb) |
131 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_encode_rgb) |
132 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_encode_component) |
133 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_rlgr_encode) |
134 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_differential_encode) |
135 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_quantization_encode) |
136 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_dwt_2d_encode) |
137 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_rgb_to_ycbcr) |
138 | 12.0k | PROFILER_PRINT(context->priv->prof_rfx_encode_format_rgb) |
139 | 12.0k | PROFILER_PRINT_FOOTER |
140 | 12.0k | } |
141 | | |
142 | | static inline void rfx_tile_init(void* obj) |
143 | 0 | { |
144 | 0 | RFX_TILE* tile = (RFX_TILE*)obj; |
145 | 0 | if (tile) |
146 | 0 | { |
147 | 0 | tile->x = 0; |
148 | 0 | tile->y = 0; |
149 | 0 | tile->YLen = 0; |
150 | 0 | tile->YData = NULL; |
151 | 0 | tile->CbLen = 0; |
152 | 0 | tile->CbData = NULL; |
153 | 0 | tile->CrLen = 0; |
154 | 0 | tile->CrData = NULL; |
155 | 0 | } |
156 | 0 | } |
157 | | |
158 | | static inline void* rfx_decoder_tile_new(const void* val) |
159 | 0 | { |
160 | 0 | const size_t size = 4ULL * 64ULL * 64ULL; |
161 | 0 | RFX_TILE* tile = NULL; |
162 | 0 | WINPR_UNUSED(val); |
163 | |
|
164 | 0 | if (!(tile = (RFX_TILE*)winpr_aligned_calloc(1, sizeof(RFX_TILE), 32))) |
165 | 0 | return NULL; |
166 | | |
167 | 0 | if (!(tile->data = (BYTE*)winpr_aligned_malloc(size, 16))) |
168 | 0 | { |
169 | 0 | winpr_aligned_free(tile); |
170 | 0 | return NULL; |
171 | 0 | } |
172 | 0 | memset(tile->data, 0xff, size); |
173 | 0 | tile->allocated = TRUE; |
174 | 0 | return tile; |
175 | 0 | } |
176 | | |
177 | | static inline void rfx_decoder_tile_free(void* obj) |
178 | 0 | { |
179 | 0 | RFX_TILE* tile = (RFX_TILE*)obj; |
180 | |
|
181 | 0 | if (tile) |
182 | 0 | { |
183 | 0 | if (tile->allocated) |
184 | 0 | winpr_aligned_free(tile->data); |
185 | |
|
186 | 0 | winpr_aligned_free(tile); |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | | static inline void* rfx_encoder_tile_new(const void* val) |
191 | 0 | { |
192 | 0 | WINPR_UNUSED(val); |
193 | 0 | return winpr_aligned_calloc(1, sizeof(RFX_TILE), 32); |
194 | 0 | } |
195 | | |
196 | | static inline void rfx_encoder_tile_free(void* obj) |
197 | 0 | { |
198 | 0 | winpr_aligned_free(obj); |
199 | 0 | } |
200 | | |
201 | | RFX_CONTEXT* rfx_context_new(BOOL encoder) |
202 | 6.00k | { |
203 | 6.00k | return rfx_context_new_ex(encoder, 0); |
204 | 6.00k | } |
205 | | |
206 | | RFX_CONTEXT* rfx_context_new_ex(BOOL encoder, UINT32 ThreadingFlags) |
207 | 12.0k | { |
208 | 12.0k | RFX_CONTEXT_PRIV* priv = NULL; |
209 | 12.0k | RFX_CONTEXT* context = (RFX_CONTEXT*)winpr_aligned_calloc(1, sizeof(RFX_CONTEXT), 32); |
210 | | |
211 | 12.0k | if (!context) |
212 | 0 | return NULL; |
213 | | |
214 | 12.0k | context->encoder = encoder; |
215 | 12.0k | context->currentMessage.freeArray = TRUE; |
216 | 12.0k | context->priv = priv = (RFX_CONTEXT_PRIV*)winpr_aligned_calloc(1, sizeof(RFX_CONTEXT_PRIV), 32); |
217 | | |
218 | 12.0k | if (!priv) |
219 | 0 | goto fail; |
220 | | |
221 | 12.0k | priv->log = WLog_Get("com.freerdp.codec.rfx"); |
222 | 12.0k | WLog_OpenAppender(priv->log); |
223 | 12.0k | priv->TilePool = ObjectPool_New(TRUE); |
224 | | |
225 | 12.0k | if (!priv->TilePool) |
226 | 0 | goto fail; |
227 | | |
228 | 12.0k | { |
229 | 12.0k | wObject* pool = ObjectPool_Object(priv->TilePool); |
230 | 12.0k | pool->fnObjectInit = rfx_tile_init; |
231 | | |
232 | 12.0k | if (context->encoder) |
233 | 0 | { |
234 | 0 | pool->fnObjectNew = rfx_encoder_tile_new; |
235 | 0 | pool->fnObjectFree = rfx_encoder_tile_free; |
236 | 0 | } |
237 | 12.0k | else |
238 | 12.0k | { |
239 | 12.0k | pool->fnObjectNew = rfx_decoder_tile_new; |
240 | 12.0k | pool->fnObjectFree = rfx_decoder_tile_free; |
241 | 12.0k | } |
242 | 12.0k | } |
243 | | |
244 | | /* |
245 | | * align buffers to 16 byte boundary (needed for SSE/NEON instructions) |
246 | | * |
247 | | * y_r_buffer, cb_g_buffer, cr_b_buffer: 64 * 64 * sizeof(INT16) = 8192 (0x2000) |
248 | | * dwt_buffer: 32 * 32 * 2 * 2 * sizeof(INT16) = 8192, maximum sub-band width is 32 |
249 | | * |
250 | | * Additionally we add 32 bytes (16 in front and 16 at the back of the buffer) |
251 | | * in order to allow optimized functions (SEE, NEON) to read from positions |
252 | | * that are actually in front/beyond the buffer. Offset calculations are |
253 | | * performed at the BufferPool_Take function calls in rfx_encode/decode.c. |
254 | | * |
255 | | * We then multiply by 3 to use a single, partionned buffer for all 3 channels. |
256 | | */ |
257 | 12.0k | priv->BufferPool = BufferPool_New(TRUE, (8192ULL + 32ULL) * 3ULL, 16); |
258 | | |
259 | 12.0k | if (!priv->BufferPool) |
260 | 0 | goto fail; |
261 | | |
262 | 12.0k | if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS)) |
263 | 12.0k | { |
264 | 12.0k | HKEY hKey = NULL; |
265 | 12.0k | priv->UseThreads = TRUE; |
266 | | |
267 | 12.0k | const LONG status = |
268 | 12.0k | RegOpenKeyExA(HKEY_LOCAL_MACHINE, RFX_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey); |
269 | | |
270 | 12.0k | if (status == ERROR_SUCCESS) |
271 | 0 | { |
272 | 0 | DWORD dwType = 0; |
273 | 0 | DWORD dwValue = 0; |
274 | 0 | DWORD dwSize = sizeof(dwValue); |
275 | |
|
276 | 0 | if (RegQueryValueEx(hKey, _T("UseThreads"), NULL, &dwType, (BYTE*)&dwValue, &dwSize) == |
277 | 0 | ERROR_SUCCESS) |
278 | 0 | priv->UseThreads = dwValue ? 1 : 0; |
279 | |
|
280 | 0 | RegCloseKey(hKey); |
281 | 0 | } |
282 | 12.0k | } |
283 | 0 | else |
284 | 0 | { |
285 | 0 | priv->UseThreads = FALSE; |
286 | 0 | } |
287 | | |
288 | 12.0k | if (priv->UseThreads) |
289 | 12.0k | { |
290 | | /* Call primitives_get here in order to avoid race conditions when using primitives_get */ |
291 | | /* from multiple threads. This call will initialize all function pointers correctly */ |
292 | | /* before any decoding threads are started */ |
293 | 12.0k | primitives_get(); |
294 | 12.0k | } |
295 | | |
296 | | /* initialize the default pixel format */ |
297 | 12.0k | rfx_context_set_pixel_format(context, PIXEL_FORMAT_BGRX32); |
298 | | /* create profilers for default decoding routines */ |
299 | 12.0k | rfx_profiler_create(context); |
300 | | /* set up default routines */ |
301 | 12.0k | context->quantization_decode = rfx_quantization_decode; |
302 | 12.0k | context->quantization_encode = rfx_quantization_encode; |
303 | 12.0k | context->dwt_2d_decode = rfx_dwt_2d_decode; |
304 | 12.0k | context->dwt_2d_extrapolate_decode = rfx_dwt_2d_extrapolate_decode; |
305 | 12.0k | context->dwt_2d_encode = rfx_dwt_2d_encode; |
306 | 12.0k | context->rlgr_decode = rfx_rlgr_decode; |
307 | 12.0k | context->rlgr_encode = rfx_rlgr_encode; |
308 | 12.0k | rfx_init_sse2(context); |
309 | 12.0k | rfx_init_neon(context); |
310 | 12.0k | context->state = RFX_STATE_SEND_HEADERS; |
311 | 12.0k | context->expectedDataBlockType = WBT_FRAME_BEGIN; |
312 | 12.0k | return context; |
313 | 0 | fail: |
314 | 0 | WINPR_PRAGMA_DIAG_PUSH |
315 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
316 | 0 | rfx_context_free(context); |
317 | 0 | WINPR_PRAGMA_DIAG_POP |
318 | 0 | return NULL; |
319 | 12.0k | } |
320 | | |
321 | | void rfx_context_free(RFX_CONTEXT* context) |
322 | 12.0k | { |
323 | 12.0k | RFX_CONTEXT_PRIV* priv = NULL; |
324 | | |
325 | 12.0k | if (!context) |
326 | 0 | return; |
327 | | |
328 | 12.0k | WINPR_ASSERT(NULL != context); |
329 | | |
330 | 12.0k | priv = context->priv; |
331 | 12.0k | WINPR_ASSERT(NULL != priv); |
332 | 12.0k | WINPR_ASSERT(NULL != priv->TilePool); |
333 | 12.0k | WINPR_ASSERT(NULL != priv->BufferPool); |
334 | | |
335 | | /* coverity[address_free] */ |
336 | 12.0k | rfx_message_free(context, &context->currentMessage); |
337 | 12.0k | winpr_aligned_free(context->quants); |
338 | 12.0k | rfx_profiler_print(context); |
339 | 12.0k | rfx_profiler_free(context); |
340 | | |
341 | 12.0k | if (priv) |
342 | 12.0k | { |
343 | 12.0k | ObjectPool_Free(priv->TilePool); |
344 | 12.0k | if (priv->UseThreads) |
345 | 12.0k | { |
346 | 12.0k | winpr_aligned_free((void*)priv->workObjects); |
347 | 12.0k | winpr_aligned_free(priv->tileWorkParams); |
348 | | #ifdef WITH_PROFILER |
349 | | WLog_VRB( |
350 | | TAG, |
351 | | "WARNING: Profiling results probably unusable with multithreaded RemoteFX codec!"); |
352 | | #endif |
353 | 12.0k | } |
354 | | |
355 | 12.0k | BufferPool_Free(priv->BufferPool); |
356 | 12.0k | winpr_aligned_free(priv); |
357 | 12.0k | } |
358 | 12.0k | winpr_aligned_free(context); |
359 | 12.0k | } |
360 | | |
361 | | static inline RFX_TILE* rfx_message_get_tile(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index) |
362 | 0 | { |
363 | 0 | WINPR_ASSERT(message); |
364 | 0 | WINPR_ASSERT(message->tiles); |
365 | 0 | WINPR_ASSERT(index < message->numTiles); |
366 | 0 | return message->tiles[index]; |
367 | 0 | } |
368 | | |
369 | | static inline const RFX_RECT* rfx_message_get_rect_const(const RFX_MESSAGE* WINPR_RESTRICT message, |
370 | | UINT32 index) |
371 | 0 | { |
372 | 0 | WINPR_ASSERT(message); |
373 | 0 | WINPR_ASSERT(message->rects); |
374 | 0 | WINPR_ASSERT(index < message->numRects); |
375 | 0 | return &message->rects[index]; |
376 | 0 | } |
377 | | |
378 | | static inline RFX_RECT* rfx_message_get_rect(RFX_MESSAGE* WINPR_RESTRICT message, UINT32 index) |
379 | 0 | { |
380 | 0 | WINPR_ASSERT(message); |
381 | 0 | WINPR_ASSERT(message->rects); |
382 | 0 | WINPR_ASSERT(index < message->numRects); |
383 | 0 | return &message->rects[index]; |
384 | 0 | } |
385 | | |
386 | | void rfx_context_set_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 pixel_format) |
387 | 12.0k | { |
388 | 12.0k | WINPR_ASSERT(context); |
389 | 12.0k | context->pixel_format = pixel_format; |
390 | 12.0k | const UINT32 bpp = FreeRDPGetBitsPerPixel(pixel_format); |
391 | 24.0k | context->bits_per_pixel = WINPR_ASSERTING_INT_CAST(UINT8, bpp); |
392 | 24.0k | } |
393 | | |
394 | | UINT32 rfx_context_get_pixel_format(RFX_CONTEXT* WINPR_RESTRICT context) |
395 | 0 | { |
396 | 0 | WINPR_ASSERT(context); |
397 | 0 | return context->pixel_format; |
398 | 0 | } |
399 | | |
400 | | void rfx_context_set_palette(RFX_CONTEXT* WINPR_RESTRICT context, |
401 | | const BYTE* WINPR_RESTRICT palette) |
402 | 0 | { |
403 | 0 | WINPR_ASSERT(context); |
404 | 0 | context->palette = palette; |
405 | 0 | } |
406 | | |
407 | | const BYTE* rfx_context_get_palette(RFX_CONTEXT* WINPR_RESTRICT context) |
408 | 0 | { |
409 | 0 | WINPR_ASSERT(context); |
410 | 0 | return context->palette; |
411 | 0 | } |
412 | | |
413 | | BOOL rfx_context_reset(RFX_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height) |
414 | 0 | { |
415 | 0 | if (!context) |
416 | 0 | return FALSE; |
417 | | |
418 | 0 | context->width = WINPR_ASSERTING_INT_CAST(UINT16, width); |
419 | 0 | context->height = WINPR_ASSERTING_INT_CAST(UINT16, height); |
420 | 0 | context->state = RFX_STATE_SEND_HEADERS; |
421 | 0 | context->expectedDataBlockType = WBT_FRAME_BEGIN; |
422 | 0 | context->frameIdx = 0; |
423 | 0 | return TRUE; |
424 | 0 | } |
425 | | |
426 | | static inline BOOL rfx_process_message_sync(RFX_CONTEXT* WINPR_RESTRICT context, |
427 | | wStream* WINPR_RESTRICT s) |
428 | 1.58k | { |
429 | 1.58k | UINT32 magic = 0; |
430 | | |
431 | 1.58k | WINPR_ASSERT(context); |
432 | 1.58k | WINPR_ASSERT(context->priv); |
433 | 1.58k | context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_SYNC; |
434 | | |
435 | | /* RFX_SYNC */ |
436 | 1.58k | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6)) |
437 | 4 | return FALSE; |
438 | | |
439 | 1.57k | Stream_Read_UINT32(s, magic); /* magic (4 bytes), 0xCACCACCA */ |
440 | 1.57k | if (magic != WF_MAGIC) |
441 | 66 | { |
442 | 66 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid magic number 0x%08" PRIX32 "", magic); |
443 | 66 | return FALSE; |
444 | 66 | } |
445 | | |
446 | 1.51k | Stream_Read_UINT16(s, context->version); /* version (2 bytes), WF_VERSION_1_0 (0x0100) */ |
447 | 1.51k | if (context->version != WF_VERSION_1_0) |
448 | 9 | { |
449 | 9 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid version number 0x%08" PRIX32 "", |
450 | 9 | context->version); |
451 | 9 | return FALSE; |
452 | 9 | } |
453 | | |
454 | 1.50k | WLog_Print(context->priv->log, WLOG_DEBUG, "version 0x%08" PRIX32 "", context->version); |
455 | 1.50k | context->decodedHeaderBlocks |= RFX_DECODED_SYNC; |
456 | 1.50k | return TRUE; |
457 | 1.51k | } |
458 | | |
459 | | static inline BOOL rfx_process_message_codec_versions(RFX_CONTEXT* WINPR_RESTRICT context, |
460 | | wStream* WINPR_RESTRICT s) |
461 | 1.22k | { |
462 | 1.22k | BYTE numCodecs = 0; |
463 | | |
464 | 1.22k | WINPR_ASSERT(context); |
465 | 1.22k | WINPR_ASSERT(context->priv); |
466 | 1.22k | context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_VERSIONS; |
467 | | |
468 | 1.22k | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4)) |
469 | 7 | return FALSE; |
470 | | |
471 | 1.21k | Stream_Read_UINT8(s, numCodecs); /* numCodecs (1 byte), must be set to 0x01 */ |
472 | 1.21k | Stream_Read_UINT8(s, context->codec_id); /* codecId (1 byte), must be set to 0x01 */ |
473 | 1.21k | Stream_Read_UINT16( |
474 | 1.21k | s, context->codec_version); /* version (2 bytes), must be set to WF_VERSION_1_0 (0x0100) */ |
475 | | |
476 | 1.21k | if (numCodecs != 1) |
477 | 675 | { |
478 | 675 | WLog_Print(context->priv->log, WLOG_ERROR, "numCodes is 0x%02" PRIX8 " (must be 0x01)", |
479 | 675 | numCodecs); |
480 | 675 | return FALSE; |
481 | 675 | } |
482 | | |
483 | 540 | if (context->codec_id != 0x01) |
484 | 12 | { |
485 | 12 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid codec id (0x%02" PRIX32 ")", |
486 | 12 | context->codec_id); |
487 | 12 | return FALSE; |
488 | 12 | } |
489 | | |
490 | 528 | if (context->codec_version != WF_VERSION_1_0) |
491 | 3 | { |
492 | 3 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid codec version (0x%08" PRIX32 ")", |
493 | 3 | context->codec_version); |
494 | 3 | return FALSE; |
495 | 3 | } |
496 | | |
497 | 525 | WLog_Print(context->priv->log, WLOG_DEBUG, "id %" PRIu32 " version 0x%" PRIX32 ".", |
498 | 525 | context->codec_id, context->codec_version); |
499 | 525 | context->decodedHeaderBlocks |= RFX_DECODED_VERSIONS; |
500 | 525 | return TRUE; |
501 | 528 | } |
502 | | |
503 | | static inline BOOL rfx_process_message_channels(RFX_CONTEXT* WINPR_RESTRICT context, |
504 | | wStream* WINPR_RESTRICT s) |
505 | 447 | { |
506 | 447 | BYTE channelId = 0; |
507 | 447 | BYTE numChannels = 0; |
508 | | |
509 | 447 | WINPR_ASSERT(context); |
510 | 447 | WINPR_ASSERT(context->priv); |
511 | 447 | context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CHANNELS; |
512 | | |
513 | 447 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 1)) |
514 | 50 | return FALSE; |
515 | | |
516 | 397 | Stream_Read_UINT8(s, numChannels); /* numChannels (1 byte), must bet set to 0x01 */ |
517 | | |
518 | | /* In RDVH sessions, numChannels will represent the number of virtual monitors |
519 | | * configured and does not always be set to 0x01 as [MS-RDPRFX] said. |
520 | | */ |
521 | 397 | if (numChannels < 1) |
522 | 1 | { |
523 | 1 | WLog_Print(context->priv->log, WLOG_ERROR, "no channels announced"); |
524 | 1 | return FALSE; |
525 | 1 | } |
526 | | |
527 | 396 | if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, numChannels, 5ull)) |
528 | 6 | return FALSE; |
529 | | |
530 | | /* RFX_CHANNELT */ |
531 | 390 | Stream_Read_UINT8(s, channelId); /* channelId (1 byte), must be set to 0x00 */ |
532 | | |
533 | 390 | if (channelId != 0x00) |
534 | 2 | { |
535 | 2 | WLog_Print(context->priv->log, WLOG_ERROR, "channelId:0x%02" PRIX8 ", expected:0x00", |
536 | 2 | channelId); |
537 | 2 | return FALSE; |
538 | 2 | } |
539 | | |
540 | 388 | Stream_Read_UINT16(s, context->width); /* width (2 bytes) */ |
541 | 388 | Stream_Read_UINT16(s, context->height); /* height (2 bytes) */ |
542 | | |
543 | 388 | if (!context->width || !context->height) |
544 | 9 | { |
545 | 9 | WLog_Print(context->priv->log, WLOG_ERROR, |
546 | 9 | "invalid channel with/height: %" PRIu16 "x%" PRIu16 "", context->width, |
547 | 9 | context->height); |
548 | 9 | return FALSE; |
549 | 9 | } |
550 | | |
551 | | /* Now, only the first monitor can be used, therefore the other channels will be ignored. */ |
552 | 379 | Stream_Seek(s, 5ULL * (numChannels - 1)); |
553 | 379 | WLog_Print(context->priv->log, WLOG_DEBUG, |
554 | 379 | "numChannels %" PRIu8 " id %" PRIu8 ", %" PRIu16 "x%" PRIu16 ".", numChannels, |
555 | 379 | channelId, context->width, context->height); |
556 | 379 | context->decodedHeaderBlocks |= RFX_DECODED_CHANNELS; |
557 | 379 | return TRUE; |
558 | 388 | } |
559 | | |
560 | | static inline BOOL rfx_process_message_context(RFX_CONTEXT* WINPR_RESTRICT context, |
561 | | wStream* WINPR_RESTRICT s) |
562 | 317 | { |
563 | 317 | BYTE ctxId = 0; |
564 | 317 | UINT16 tileSize = 0; |
565 | 317 | UINT16 properties = 0; |
566 | | |
567 | 317 | WINPR_ASSERT(context); |
568 | 317 | WINPR_ASSERT(context->priv); |
569 | 317 | context->decodedHeaderBlocks &= (uint32_t)~RFX_DECODED_CONTEXT; |
570 | | |
571 | 317 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 5)) |
572 | 2 | return FALSE; |
573 | | |
574 | 315 | Stream_Read_UINT8(s, ctxId); /* ctxId (1 byte), must be set to 0x00 */ |
575 | 315 | Stream_Read_UINT16(s, tileSize); /* tileSize (2 bytes), must be set to CT_TILE_64x64 (0x0040) */ |
576 | 315 | Stream_Read_UINT16(s, properties); /* properties (2 bytes) */ |
577 | 315 | WLog_Print(context->priv->log, WLOG_DEBUG, |
578 | 315 | "ctxId %" PRIu8 " tileSize %" PRIu16 " properties 0x%04" PRIX16 ".", ctxId, tileSize, |
579 | 315 | properties); |
580 | 315 | context->properties = properties; |
581 | 315 | context->flags = (properties & 0x0007); |
582 | | |
583 | 315 | if (context->flags == CODEC_MODE) |
584 | 88 | { |
585 | 88 | WLog_Print(context->priv->log, WLOG_DEBUG, "codec is in image mode."); |
586 | 88 | } |
587 | 227 | else |
588 | 227 | { |
589 | 227 | WLog_Print(context->priv->log, WLOG_DEBUG, "codec is in video mode."); |
590 | 227 | } |
591 | | |
592 | 315 | switch ((properties & 0x1E00) >> 9) |
593 | 315 | { |
594 | 89 | case CLW_ENTROPY_RLGR1: |
595 | 89 | context->mode = RLGR1; |
596 | 89 | WLog_Print(context->priv->log, WLOG_DEBUG, "RLGR1."); |
597 | 89 | break; |
598 | | |
599 | 216 | case CLW_ENTROPY_RLGR3: |
600 | 216 | context->mode = RLGR3; |
601 | 216 | WLog_Print(context->priv->log, WLOG_DEBUG, "RLGR3."); |
602 | 216 | break; |
603 | | |
604 | 10 | default: |
605 | 10 | WLog_Print(context->priv->log, WLOG_ERROR, "unknown RLGR algorithm."); |
606 | 10 | return FALSE; |
607 | 315 | } |
608 | | |
609 | 305 | context->decodedHeaderBlocks |= RFX_DECODED_CONTEXT; |
610 | 305 | return TRUE; |
611 | 315 | } |
612 | | |
613 | | static inline BOOL rfx_process_message_frame_begin( |
614 | | RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message, |
615 | | wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType) |
616 | 0 | { |
617 | 0 | UINT32 frameIdx = 0; |
618 | 0 | UINT16 numRegions = 0; |
619 | |
|
620 | 0 | WINPR_ASSERT(context); |
621 | 0 | WINPR_ASSERT(context->priv); |
622 | 0 | WINPR_ASSERT(message); |
623 | 0 | WINPR_ASSERT(pExpectedBlockType); |
624 | | |
625 | 0 | if (*pExpectedBlockType != WBT_FRAME_BEGIN) |
626 | 0 | { |
627 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected wants WBT_FRAME_BEGIN"); |
628 | 0 | return FALSE; |
629 | 0 | } |
630 | | |
631 | 0 | *pExpectedBlockType = WBT_REGION; |
632 | |
|
633 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6)) |
634 | 0 | return FALSE; |
635 | | |
636 | 0 | Stream_Read_UINT32( |
637 | 0 | s, frameIdx); /* frameIdx (4 bytes), if codec is in video mode, must be ignored */ |
638 | 0 | Stream_Read_UINT16(s, numRegions); /* numRegions (2 bytes) */ |
639 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, |
640 | 0 | "RFX_FRAME_BEGIN: frameIdx: %" PRIu32 " numRegions: %" PRIu16 "", frameIdx, |
641 | 0 | numRegions); |
642 | 0 | return TRUE; |
643 | 0 | } |
644 | | |
645 | | static inline BOOL rfx_process_message_frame_end( |
646 | | RFX_CONTEXT* WINPR_RESTRICT context, WINPR_ATTR_UNUSED RFX_MESSAGE* WINPR_RESTRICT message, |
647 | | WINPR_ATTR_UNUSED wStream* WINPR_RESTRICT s, UINT16* WINPR_RESTRICT pExpectedBlockType) |
648 | 0 | { |
649 | 0 | WINPR_ASSERT(context); |
650 | 0 | WINPR_ASSERT(context->priv); |
651 | 0 | WINPR_ASSERT(message); |
652 | 0 | WINPR_ASSERT(s); |
653 | 0 | WINPR_ASSERT(pExpectedBlockType); |
654 | | |
655 | 0 | if (*pExpectedBlockType != WBT_FRAME_END) |
656 | 0 | { |
657 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected, wants WBT_FRAME_END"); |
658 | 0 | return FALSE; |
659 | 0 | } |
660 | | |
661 | 0 | *pExpectedBlockType = WBT_FRAME_BEGIN; |
662 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, "RFX_FRAME_END"); |
663 | 0 | return TRUE; |
664 | 0 | } |
665 | | |
666 | | static inline BOOL rfx_resize_rects(RFX_MESSAGE* WINPR_RESTRICT message) |
667 | 0 | { |
668 | 0 | WINPR_ASSERT(message); |
669 | | |
670 | 0 | RFX_RECT* tmpRects = |
671 | 0 | winpr_aligned_recalloc(message->rects, message->numRects, sizeof(RFX_RECT), 32); |
672 | 0 | if (!tmpRects) |
673 | 0 | return FALSE; |
674 | 0 | message->rects = tmpRects; |
675 | 0 | return TRUE; |
676 | 0 | } |
677 | | |
678 | | static inline BOOL rfx_process_message_region(RFX_CONTEXT* WINPR_RESTRICT context, |
679 | | RFX_MESSAGE* WINPR_RESTRICT message, |
680 | | wStream* WINPR_RESTRICT s, |
681 | | UINT16* WINPR_RESTRICT pExpectedBlockType) |
682 | 0 | { |
683 | 0 | UINT16 regionType = 0; |
684 | 0 | UINT16 numTileSets = 0; |
685 | |
|
686 | 0 | WINPR_ASSERT(context); |
687 | 0 | WINPR_ASSERT(context->priv); |
688 | 0 | WINPR_ASSERT(message); |
689 | 0 | WINPR_ASSERT(pExpectedBlockType); |
690 | | |
691 | 0 | if (*pExpectedBlockType != WBT_REGION) |
692 | 0 | { |
693 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected wants WBT_REGION"); |
694 | 0 | return FALSE; |
695 | 0 | } |
696 | | |
697 | 0 | *pExpectedBlockType = WBT_EXTENSION; |
698 | |
|
699 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 3)) |
700 | 0 | return FALSE; |
701 | | |
702 | 0 | Stream_Seek_UINT8(s); /* regionFlags (1 byte) */ |
703 | 0 | Stream_Read_UINT16(s, message->numRects); /* numRects (2 bytes) */ |
704 | |
|
705 | 0 | if (message->numRects < 1) |
706 | 0 | { |
707 | | /* |
708 | | If numRects is zero the decoder must generate a rectangle with |
709 | | coordinates (0, 0, width, height). |
710 | | See [MS-RDPRFX] (revision >= 17.0) 2.2.2.3.3 TS_RFX_REGION |
711 | | https://msdn.microsoft.com/en-us/library/ff635233.aspx |
712 | | */ |
713 | 0 | message->numRects = 1; |
714 | 0 | if (!rfx_resize_rects(message)) |
715 | 0 | return FALSE; |
716 | | |
717 | 0 | message->rects->x = 0; |
718 | 0 | message->rects->y = 0; |
719 | 0 | message->rects->width = context->width; |
720 | 0 | message->rects->height = context->height; |
721 | 0 | return TRUE; |
722 | 0 | } |
723 | | |
724 | 0 | if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, message->numRects, 8ull)) |
725 | 0 | return FALSE; |
726 | | |
727 | 0 | if (!rfx_resize_rects(message)) |
728 | 0 | return FALSE; |
729 | | |
730 | | /* rects */ |
731 | 0 | for (UINT16 i = 0; i < message->numRects; i++) |
732 | 0 | { |
733 | 0 | RFX_RECT* rect = rfx_message_get_rect(message, i); |
734 | | /* RFX_RECT */ |
735 | 0 | Stream_Read_UINT16(s, rect->x); /* x (2 bytes) */ |
736 | 0 | Stream_Read_UINT16(s, rect->y); /* y (2 bytes) */ |
737 | 0 | Stream_Read_UINT16(s, rect->width); /* width (2 bytes) */ |
738 | 0 | Stream_Read_UINT16(s, rect->height); /* height (2 bytes) */ |
739 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, |
740 | 0 | "rect %" PRIu16 " (x,y=%" PRIu16 ",%" PRIu16 " w,h=%" PRIu16 " %" PRIu16 ").", i, |
741 | 0 | rect->x, rect->y, rect->width, rect->height); |
742 | 0 | } |
743 | |
|
744 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 4)) |
745 | 0 | return FALSE; |
746 | | |
747 | 0 | Stream_Read_UINT16(s, regionType); /*regionType (2 bytes): MUST be set to CBT_REGION (0xCAC1)*/ |
748 | 0 | Stream_Read_UINT16(s, numTileSets); /*numTilesets (2 bytes): MUST be set to 0x0001.*/ |
749 | |
|
750 | 0 | if (regionType != CBT_REGION) |
751 | 0 | { |
752 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid region type 0x%04" PRIX16 "", |
753 | 0 | regionType); |
754 | 0 | return TRUE; |
755 | 0 | } |
756 | | |
757 | 0 | if (numTileSets != 0x0001) |
758 | 0 | { |
759 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid number of tilesets (%" PRIu16 ")", |
760 | 0 | numTileSets); |
761 | 0 | return FALSE; |
762 | 0 | } |
763 | | |
764 | 0 | return TRUE; |
765 | 0 | } |
766 | | |
767 | | typedef struct |
768 | | { |
769 | | RFX_TILE* tile; |
770 | | RFX_CONTEXT* context; |
771 | | } RFX_TILE_PROCESS_WORK_PARAM; |
772 | | |
773 | | static inline void CALLBACK |
774 | | rfx_process_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance, |
775 | | void* context, WINPR_ATTR_UNUSED PTP_WORK work) |
776 | 0 | { |
777 | 0 | RFX_TILE_PROCESS_WORK_PARAM* param = (RFX_TILE_PROCESS_WORK_PARAM*)context; |
778 | 0 | WINPR_ASSERT(param); |
779 | 0 | rfx_decode_rgb(param->context, param->tile, param->tile->data, 64 * 4); |
780 | 0 | } |
781 | | |
782 | | static inline BOOL rfx_allocate_tiles(RFX_MESSAGE* WINPR_RESTRICT message, size_t count, |
783 | | BOOL allocOnly) |
784 | 0 | { |
785 | 0 | WINPR_ASSERT(message); |
786 | | |
787 | 0 | RFX_TILE** tmpTiles = |
788 | 0 | (RFX_TILE**)winpr_aligned_recalloc((void*)message->tiles, count, sizeof(RFX_TILE*), 32); |
789 | 0 | if (!tmpTiles && (count != 0)) |
790 | 0 | return FALSE; |
791 | | |
792 | 0 | message->tiles = tmpTiles; |
793 | 0 | if (!allocOnly) |
794 | 0 | message->numTiles = WINPR_ASSERTING_INT_CAST(UINT16, count); |
795 | 0 | else |
796 | 0 | { |
797 | 0 | WINPR_ASSERT(message->numTiles <= count); |
798 | 0 | } |
799 | 0 | message->allocatedTiles = count; |
800 | |
|
801 | 0 | return TRUE; |
802 | 0 | } |
803 | | |
804 | | static inline BOOL rfx_process_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context, |
805 | | RFX_MESSAGE* WINPR_RESTRICT message, |
806 | | wStream* WINPR_RESTRICT s, |
807 | | UINT16* WINPR_RESTRICT pExpectedBlockType) |
808 | 0 | { |
809 | 0 | BOOL rc = 0; |
810 | 0 | size_t close_cnt = 0; |
811 | 0 | BYTE quant = 0; |
812 | 0 | RFX_TILE* tile = NULL; |
813 | 0 | UINT32* quants = NULL; |
814 | 0 | UINT16 subtype = 0; |
815 | 0 | UINT16 numTiles = 0; |
816 | 0 | UINT32 blockLen = 0; |
817 | 0 | UINT32 blockType = 0; |
818 | 0 | UINT32 tilesDataSize = 0; |
819 | 0 | PTP_WORK* work_objects = NULL; |
820 | 0 | RFX_TILE_PROCESS_WORK_PARAM* params = NULL; |
821 | 0 | void* pmem = NULL; |
822 | |
|
823 | 0 | WINPR_ASSERT(context); |
824 | 0 | WINPR_ASSERT(context->priv); |
825 | 0 | WINPR_ASSERT(message); |
826 | 0 | WINPR_ASSERT(pExpectedBlockType); |
827 | | |
828 | 0 | if (*pExpectedBlockType != WBT_EXTENSION) |
829 | 0 | { |
830 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "message unexpected wants a tileset"); |
831 | 0 | return FALSE; |
832 | 0 | } |
833 | | |
834 | 0 | *pExpectedBlockType = WBT_FRAME_END; |
835 | |
|
836 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 14)) |
837 | 0 | return FALSE; |
838 | | |
839 | 0 | Stream_Read_UINT16(s, subtype); /* subtype (2 bytes) must be set to CBT_TILESET (0xCAC2) */ |
840 | 0 | if (subtype != CBT_TILESET) |
841 | 0 | { |
842 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid subtype, expected CBT_TILESET."); |
843 | 0 | return FALSE; |
844 | 0 | } |
845 | | |
846 | 0 | Stream_Seek_UINT16(s); /* idx (2 bytes), must be set to 0x0000 */ |
847 | 0 | Stream_Seek_UINT16(s); /* properties (2 bytes) */ |
848 | 0 | Stream_Read_UINT8(s, context->numQuant); /* numQuant (1 byte) */ |
849 | 0 | Stream_Seek_UINT8(s); /* tileSize (1 byte), must be set to 0x40 */ |
850 | |
|
851 | 0 | if (context->numQuant < 1) |
852 | 0 | { |
853 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "no quantization value."); |
854 | 0 | return FALSE; |
855 | 0 | } |
856 | | |
857 | 0 | Stream_Read_UINT16(s, numTiles); /* numTiles (2 bytes) */ |
858 | 0 | if (numTiles < 1) |
859 | 0 | { |
860 | | /* Windows Server 2012 (not R2) can send empty tile sets */ |
861 | 0 | return TRUE; |
862 | 0 | } |
863 | | |
864 | 0 | Stream_Read_UINT32(s, tilesDataSize); /* tilesDataSize (4 bytes) */ |
865 | |
|
866 | 0 | if (!(pmem = |
867 | 0 | winpr_aligned_recalloc(context->quants, context->numQuant, 10 * sizeof(UINT32), 32))) |
868 | 0 | return FALSE; |
869 | | |
870 | 0 | quants = context->quants = (UINT32*)pmem; |
871 | | |
872 | | /* quantVals */ |
873 | 0 | if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(context->priv->log, s, context->numQuant, 5ull)) |
874 | 0 | return FALSE; |
875 | | |
876 | 0 | for (size_t i = 0; i < context->numQuant; i++) |
877 | 0 | { |
878 | | /* RFX_CODEC_QUANT */ |
879 | 0 | Stream_Read_UINT8(s, quant); |
880 | 0 | *quants++ = (quant & 0x0F); |
881 | 0 | *quants++ = (quant >> 4); |
882 | 0 | Stream_Read_UINT8(s, quant); |
883 | 0 | *quants++ = (quant & 0x0F); |
884 | 0 | *quants++ = (quant >> 4); |
885 | 0 | Stream_Read_UINT8(s, quant); |
886 | 0 | *quants++ = (quant & 0x0F); |
887 | 0 | *quants++ = (quant >> 4); |
888 | 0 | Stream_Read_UINT8(s, quant); |
889 | 0 | *quants++ = (quant & 0x0F); |
890 | 0 | *quants++ = (quant >> 4); |
891 | 0 | Stream_Read_UINT8(s, quant); |
892 | 0 | *quants++ = (quant & 0x0F); |
893 | 0 | *quants++ = (quant >> 4); |
894 | 0 | WLog_Print(context->priv->log, WLOG_DEBUG, |
895 | 0 | "quant %" PRIuz " (%" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 |
896 | 0 | " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 ").", |
897 | 0 | i, context->quants[i * 10], context->quants[i * 10 + 1], |
898 | 0 | context->quants[i * 10 + 2], context->quants[i * 10 + 3], |
899 | 0 | context->quants[i * 10 + 4], context->quants[i * 10 + 5], |
900 | 0 | context->quants[i * 10 + 6], context->quants[i * 10 + 7], |
901 | 0 | context->quants[i * 10 + 8], context->quants[i * 10 + 9]); |
902 | 0 | } |
903 | |
|
904 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
905 | 0 | { |
906 | 0 | ObjectPool_Return(context->priv->TilePool, message->tiles[i]); |
907 | 0 | message->tiles[i] = NULL; |
908 | 0 | } |
909 | |
|
910 | 0 | if (!rfx_allocate_tiles(message, numTiles, FALSE)) |
911 | 0 | return FALSE; |
912 | | |
913 | 0 | if (context->priv->UseThreads) |
914 | 0 | { |
915 | 0 | work_objects = (PTP_WORK*)winpr_aligned_calloc(message->numTiles, sizeof(PTP_WORK), 32); |
916 | 0 | params = (RFX_TILE_PROCESS_WORK_PARAM*)winpr_aligned_recalloc( |
917 | 0 | NULL, message->numTiles, sizeof(RFX_TILE_PROCESS_WORK_PARAM), 32); |
918 | |
|
919 | 0 | if (!work_objects) |
920 | 0 | { |
921 | 0 | winpr_aligned_free(params); |
922 | 0 | return FALSE; |
923 | 0 | } |
924 | | |
925 | 0 | if (!params) |
926 | 0 | { |
927 | 0 | winpr_aligned_free((void*)work_objects); |
928 | 0 | return FALSE; |
929 | 0 | } |
930 | 0 | } |
931 | | |
932 | | /* tiles */ |
933 | 0 | close_cnt = 0; |
934 | 0 | rc = FALSE; |
935 | |
|
936 | 0 | if (Stream_GetRemainingLength(s) >= tilesDataSize) |
937 | 0 | { |
938 | 0 | rc = TRUE; |
939 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
940 | 0 | { |
941 | 0 | wStream subBuffer; |
942 | 0 | wStream* sub = NULL; |
943 | |
|
944 | 0 | if (!(tile = (RFX_TILE*)ObjectPool_Take(context->priv->TilePool))) |
945 | 0 | { |
946 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
947 | 0 | "RfxMessageTileSet failed to get tile from object pool"); |
948 | 0 | rc = FALSE; |
949 | 0 | break; |
950 | 0 | } |
951 | | |
952 | 0 | message->tiles[i] = tile; |
953 | | |
954 | | /* RFX_TILE */ |
955 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 6)) |
956 | 0 | { |
957 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
958 | 0 | "RfxMessageTileSet packet too small to read tile %" PRIuz "/%" PRIu16 "", |
959 | 0 | i, message->numTiles); |
960 | 0 | rc = FALSE; |
961 | 0 | break; |
962 | 0 | } |
963 | | |
964 | 0 | sub = Stream_StaticInit(&subBuffer, Stream_Pointer(s), Stream_GetRemainingLength(s)); |
965 | 0 | Stream_Read_UINT16( |
966 | 0 | sub, blockType); /* blockType (2 bytes), must be set to CBT_TILE (0xCAC3) */ |
967 | 0 | Stream_Read_UINT32(sub, blockLen); /* blockLen (4 bytes) */ |
968 | |
|
969 | 0 | if (!Stream_SafeSeek(s, blockLen)) |
970 | 0 | { |
971 | 0 | rc = FALSE; |
972 | 0 | break; |
973 | 0 | } |
974 | 0 | if ((blockLen < 6 + 13) || |
975 | 0 | (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, sub, blockLen - 6))) |
976 | 0 | { |
977 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
978 | 0 | "RfxMessageTileSet not enough bytes to read tile %" PRIuz "/%" PRIu16 |
979 | 0 | " with blocklen=%" PRIu32 "", |
980 | 0 | i, message->numTiles, blockLen); |
981 | 0 | rc = FALSE; |
982 | 0 | break; |
983 | 0 | } |
984 | | |
985 | 0 | if (blockType != CBT_TILE) |
986 | 0 | { |
987 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
988 | 0 | "unknown block type 0x%" PRIX32 ", expected CBT_TILE (0xCAC3).", |
989 | 0 | blockType); |
990 | 0 | rc = FALSE; |
991 | 0 | break; |
992 | 0 | } |
993 | | |
994 | 0 | Stream_Read_UINT8(sub, tile->quantIdxY); /* quantIdxY (1 byte) */ |
995 | 0 | Stream_Read_UINT8(sub, tile->quantIdxCb); /* quantIdxCb (1 byte) */ |
996 | 0 | Stream_Read_UINT8(sub, tile->quantIdxCr); /* quantIdxCr (1 byte) */ |
997 | 0 | if (tile->quantIdxY >= context->numQuant) |
998 | 0 | { |
999 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1000 | 0 | "quantIdxY %" PRIu8 " >= numQuant %" PRIu8, tile->quantIdxY, |
1001 | 0 | context->numQuant); |
1002 | 0 | rc = FALSE; |
1003 | 0 | break; |
1004 | 0 | } |
1005 | 0 | if (tile->quantIdxCb >= context->numQuant) |
1006 | 0 | { |
1007 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1008 | 0 | "quantIdxCb %" PRIu8 " >= numQuant %" PRIu8, tile->quantIdxCb, |
1009 | 0 | context->numQuant); |
1010 | 0 | rc = FALSE; |
1011 | 0 | break; |
1012 | 0 | } |
1013 | 0 | if (tile->quantIdxCr >= context->numQuant) |
1014 | 0 | { |
1015 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1016 | 0 | "quantIdxCr %" PRIu8 " >= numQuant %" PRIu8, tile->quantIdxCr, |
1017 | 0 | context->numQuant); |
1018 | 0 | rc = FALSE; |
1019 | 0 | break; |
1020 | 0 | } |
1021 | | |
1022 | 0 | Stream_Read_UINT16(sub, tile->xIdx); /* xIdx (2 bytes) */ |
1023 | 0 | Stream_Read_UINT16(sub, tile->yIdx); /* yIdx (2 bytes) */ |
1024 | 0 | Stream_Read_UINT16(sub, tile->YLen); /* YLen (2 bytes) */ |
1025 | 0 | Stream_Read_UINT16(sub, tile->CbLen); /* CbLen (2 bytes) */ |
1026 | 0 | Stream_Read_UINT16(sub, tile->CrLen); /* CrLen (2 bytes) */ |
1027 | 0 | Stream_GetPointer(sub, tile->YData); |
1028 | 0 | if (!Stream_SafeSeek(sub, tile->YLen)) |
1029 | 0 | { |
1030 | 0 | rc = FALSE; |
1031 | 0 | break; |
1032 | 0 | } |
1033 | 0 | Stream_GetPointer(sub, tile->CbData); |
1034 | 0 | if (!Stream_SafeSeek(sub, tile->CbLen)) |
1035 | 0 | { |
1036 | 0 | rc = FALSE; |
1037 | 0 | break; |
1038 | 0 | } |
1039 | 0 | Stream_GetPointer(sub, tile->CrData); |
1040 | 0 | if (!Stream_SafeSeek(sub, tile->CrLen)) |
1041 | 0 | { |
1042 | 0 | rc = FALSE; |
1043 | 0 | break; |
1044 | 0 | } |
1045 | 0 | tile->x = tile->xIdx * 64; |
1046 | 0 | tile->y = tile->yIdx * 64; |
1047 | |
|
1048 | 0 | if (context->priv->UseThreads) |
1049 | 0 | { |
1050 | 0 | if (!params) |
1051 | 0 | { |
1052 | 0 | rc = FALSE; |
1053 | 0 | break; |
1054 | 0 | } |
1055 | | |
1056 | 0 | params[i].context = context; |
1057 | 0 | params[i].tile = message->tiles[i]; |
1058 | |
|
1059 | 0 | if (!(work_objects[i] = CreateThreadpoolWork(rfx_process_message_tile_work_callback, |
1060 | 0 | (void*)¶ms[i], NULL))) |
1061 | 0 | { |
1062 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "CreateThreadpoolWork failed."); |
1063 | 0 | rc = FALSE; |
1064 | 0 | break; |
1065 | 0 | } |
1066 | | |
1067 | 0 | SubmitThreadpoolWork(work_objects[i]); |
1068 | 0 | close_cnt = i + 1; |
1069 | 0 | } |
1070 | 0 | else |
1071 | 0 | { |
1072 | 0 | rfx_decode_rgb(context, tile, tile->data, 64 * 4); |
1073 | 0 | } |
1074 | 0 | } |
1075 | 0 | } |
1076 | |
|
1077 | 0 | if (context->priv->UseThreads) |
1078 | 0 | { |
1079 | 0 | for (size_t i = 0; i < close_cnt; i++) |
1080 | 0 | { |
1081 | 0 | WaitForThreadpoolWorkCallbacks(work_objects[i], FALSE); |
1082 | 0 | CloseThreadpoolWork(work_objects[i]); |
1083 | 0 | } |
1084 | 0 | } |
1085 | |
|
1086 | 0 | winpr_aligned_free((void*)work_objects); |
1087 | 0 | winpr_aligned_free(params); |
1088 | |
|
1089 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
1090 | 0 | { |
1091 | 0 | if (!(tile = message->tiles[i])) |
1092 | 0 | continue; |
1093 | | |
1094 | 0 | tile->YLen = tile->CbLen = tile->CrLen = 0; |
1095 | 0 | tile->YData = tile->CbData = tile->CrData = NULL; |
1096 | 0 | } |
1097 | |
|
1098 | 0 | return rc; |
1099 | 0 | } |
1100 | | |
1101 | | BOOL rfx_process_message(RFX_CONTEXT* WINPR_RESTRICT context, const BYTE* WINPR_RESTRICT data, |
1102 | | UINT32 length, UINT32 left, UINT32 top, BYTE* WINPR_RESTRICT dst, |
1103 | | UINT32 dstFormat, UINT32 dstStride, UINT32 dstHeight, |
1104 | | REGION16* WINPR_RESTRICT invalidRegion) |
1105 | 7.01k | { |
1106 | 7.01k | REGION16 updateRegion = { 0 }; |
1107 | 7.01k | wStream inStream = { 0 }; |
1108 | 7.01k | BOOL ok = TRUE; |
1109 | | |
1110 | 7.01k | if (!context || !data || !length) |
1111 | 0 | return FALSE; |
1112 | | |
1113 | 7.01k | WINPR_ASSERT(context->priv); |
1114 | 7.01k | RFX_MESSAGE* message = &context->currentMessage; |
1115 | | |
1116 | 7.01k | wStream* s = Stream_StaticConstInit(&inStream, data, length); |
1117 | | |
1118 | 10.5k | while (ok && Stream_GetRemainingLength(s) > 6) |
1119 | 7.70k | { |
1120 | 7.70k | wStream subStreamBuffer = { 0 }; |
1121 | 7.70k | size_t extraBlockLen = 0; |
1122 | 7.70k | UINT32 blockLen = 0; |
1123 | 7.70k | UINT32 blockType = 0; |
1124 | | |
1125 | | /* RFX_BLOCKT */ |
1126 | 7.70k | Stream_Read_UINT16(s, blockType); /* blockType (2 bytes) */ |
1127 | 7.70k | Stream_Read_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
1128 | 7.70k | WLog_Print(context->priv->log, WLOG_DEBUG, "blockType 0x%" PRIX32 " blockLen %" PRIu32 "", |
1129 | 7.70k | blockType, blockLen); |
1130 | | |
1131 | 7.70k | if (blockLen < 6) |
1132 | 1.40k | { |
1133 | 1.40k | WLog_Print(context->priv->log, WLOG_ERROR, "blockLen too small(%" PRIu32 ")", blockLen); |
1134 | 1.40k | return FALSE; |
1135 | 1.40k | } |
1136 | | |
1137 | 6.30k | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, blockLen - 6)) |
1138 | 2.25k | return FALSE; |
1139 | | |
1140 | 4.05k | if (blockType > WBT_CONTEXT && context->decodedHeaderBlocks != RFX_DECODED_HEADERS) |
1141 | 210 | { |
1142 | 210 | WLog_Print(context->priv->log, WLOG_ERROR, "incomplete header blocks processing"); |
1143 | 210 | return FALSE; |
1144 | 210 | } |
1145 | | |
1146 | 3.84k | if (blockType >= WBT_CONTEXT && blockType <= WBT_EXTENSION) |
1147 | 380 | { |
1148 | | /* RFX_CODEC_CHANNELT */ |
1149 | 380 | UINT8 codecId = 0; |
1150 | 380 | UINT8 channelId = 0; |
1151 | | |
1152 | 380 | if (!Stream_CheckAndLogRequiredLengthWLog(context->priv->log, s, 2)) |
1153 | 1 | return FALSE; |
1154 | | |
1155 | 379 | extraBlockLen = 2; |
1156 | 379 | Stream_Read_UINT8(s, codecId); /* codecId (1 byte) must be set to 0x01 */ |
1157 | 379 | Stream_Read_UINT8(s, channelId); /* channelId (1 byte) 0xFF or 0x00, see below */ |
1158 | | |
1159 | 379 | if (codecId != 0x01) |
1160 | 34 | { |
1161 | 34 | WLog_Print(context->priv->log, WLOG_ERROR, "invalid codecId 0x%02" PRIX8 "", |
1162 | 34 | codecId); |
1163 | 34 | return FALSE; |
1164 | 34 | } |
1165 | | |
1166 | 345 | if (blockType == WBT_CONTEXT) |
1167 | 345 | { |
1168 | | /* If the blockType is set to WBT_CONTEXT, then channelId MUST be set to 0xFF.*/ |
1169 | 345 | if (channelId != 0xFF) |
1170 | 26 | { |
1171 | 26 | WLog_Print(context->priv->log, WLOG_ERROR, |
1172 | 26 | "invalid channelId 0x%02" PRIX8 " for blockType 0x%08" PRIX32 "", |
1173 | 26 | channelId, blockType); |
1174 | 26 | return FALSE; |
1175 | 26 | } |
1176 | 345 | } |
1177 | 0 | else |
1178 | 0 | { |
1179 | | /* For all other values of blockType, channelId MUST be set to 0x00. */ |
1180 | 0 | if (channelId != 0x00) |
1181 | 0 | { |
1182 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1183 | 0 | "invalid channelId 0x%02" PRIX8 " for blockType WBT_CONTEXT", |
1184 | 0 | channelId); |
1185 | 0 | return FALSE; |
1186 | 0 | } |
1187 | 0 | } |
1188 | 345 | } |
1189 | | |
1190 | 3.78k | const size_t blockLenNoHeader = blockLen - 6; |
1191 | 3.78k | if (blockLenNoHeader < extraBlockLen) |
1192 | 2 | { |
1193 | 2 | WLog_Print(context->priv->log, WLOG_ERROR, |
1194 | 2 | "blockLen too small(%" PRIu32 "), must be >= 6 + %" PRIuz, blockLen, |
1195 | 2 | extraBlockLen); |
1196 | 2 | return FALSE; |
1197 | 2 | } |
1198 | | |
1199 | 3.77k | const size_t subStreamLen = blockLenNoHeader - extraBlockLen; |
1200 | 3.77k | wStream* subStream = Stream_StaticInit(&subStreamBuffer, Stream_Pointer(s), subStreamLen); |
1201 | 3.77k | Stream_Seek(s, subStreamLen); |
1202 | | |
1203 | 3.77k | switch (blockType) |
1204 | 3.77k | { |
1205 | | /* Header messages: |
1206 | | * The stream MUST start with the header messages and any of these headers can appear |
1207 | | * in the stream at a later stage. The header messages can be repeated. |
1208 | | */ |
1209 | 1.58k | case WBT_SYNC: |
1210 | 1.58k | ok = rfx_process_message_sync(context, subStream); |
1211 | 1.58k | break; |
1212 | | |
1213 | 317 | case WBT_CONTEXT: |
1214 | 317 | ok = rfx_process_message_context(context, subStream); |
1215 | 317 | break; |
1216 | | |
1217 | 1.22k | case WBT_CODEC_VERSIONS: |
1218 | 1.22k | ok = rfx_process_message_codec_versions(context, subStream); |
1219 | 1.22k | break; |
1220 | | |
1221 | 447 | case WBT_CHANNELS: |
1222 | 447 | ok = rfx_process_message_channels(context, subStream); |
1223 | 447 | break; |
1224 | | |
1225 | | /* Data messages: |
1226 | | * The data associated with each encoded frame or image is always bracketed by the |
1227 | | * TS_RFX_FRAME_BEGIN (section 2.2.2.3.1) and TS_RFX_FRAME_END (section 2.2.2.3.2) |
1228 | | * messages. There MUST only be one TS_RFX_REGION (section 2.2.2.3.3) message per |
1229 | | * frame and one TS_RFX_TILESET (section 2.2.2.3.4) message per TS_RFX_REGION. |
1230 | | */ |
1231 | | |
1232 | 0 | case WBT_FRAME_BEGIN: |
1233 | 0 | ok = rfx_process_message_frame_begin(context, message, subStream, |
1234 | 0 | &context->expectedDataBlockType); |
1235 | 0 | break; |
1236 | | |
1237 | 0 | case WBT_REGION: |
1238 | 0 | ok = rfx_process_message_region(context, message, subStream, |
1239 | 0 | &context->expectedDataBlockType); |
1240 | 0 | break; |
1241 | | |
1242 | 0 | case WBT_EXTENSION: |
1243 | 0 | ok = rfx_process_message_tileset(context, message, subStream, |
1244 | 0 | &context->expectedDataBlockType); |
1245 | 0 | break; |
1246 | | |
1247 | 0 | case WBT_FRAME_END: |
1248 | 0 | ok = rfx_process_message_frame_end(context, message, subStream, |
1249 | 0 | &context->expectedDataBlockType); |
1250 | 0 | break; |
1251 | | |
1252 | 211 | default: |
1253 | 211 | WLog_Print(context->priv->log, WLOG_ERROR, "unknown blockType 0x%" PRIX32 "", |
1254 | 211 | blockType); |
1255 | 211 | return FALSE; |
1256 | 3.77k | } |
1257 | 3.77k | } |
1258 | | |
1259 | 2.88k | if (ok) |
1260 | 2.02k | { |
1261 | 2.02k | UINT32 nbUpdateRects = 0; |
1262 | 2.02k | REGION16 clippingRects = { 0 }; |
1263 | 2.02k | const RECTANGLE_16* updateRects = NULL; |
1264 | 2.02k | const DWORD formatSize = FreeRDPGetBytesPerPixel(context->pixel_format); |
1265 | 2.02k | const UINT32 dstWidth = dstStride / FreeRDPGetBytesPerPixel(dstFormat); |
1266 | 2.02k | region16_init(&clippingRects); |
1267 | | |
1268 | 2.02k | WINPR_ASSERT(dstWidth <= UINT16_MAX); |
1269 | 2.02k | WINPR_ASSERT(dstHeight <= UINT16_MAX); |
1270 | 2.02k | for (UINT32 i = 0; i < message->numRects; i++) |
1271 | 0 | { |
1272 | 0 | RECTANGLE_16 clippingRect = { 0 }; |
1273 | 0 | const RFX_RECT* rect = &(message->rects[i]); |
1274 | |
|
1275 | 0 | WINPR_ASSERT(left + rect->x <= UINT16_MAX); |
1276 | 0 | WINPR_ASSERT(top + rect->y <= UINT16_MAX); |
1277 | 0 | WINPR_ASSERT(clippingRect.left + rect->width <= UINT16_MAX); |
1278 | 0 | WINPR_ASSERT(clippingRect.top + rect->height <= UINT16_MAX); |
1279 | | |
1280 | 0 | clippingRect.left = WINPR_ASSERTING_INT_CAST(UINT16, MIN(left + rect->x, dstWidth)); |
1281 | 0 | clippingRect.top = WINPR_ASSERTING_INT_CAST(UINT16, MIN(top + rect->y, dstHeight)); |
1282 | |
|
1283 | 0 | const UINT32 rw = 1UL * clippingRect.left + rect->width; |
1284 | 0 | const UINT32 rh = 1UL * clippingRect.top + rect->height; |
1285 | 0 | const uint16_t right = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rw, dstWidth)); |
1286 | 0 | const uint16_t bottom = WINPR_ASSERTING_INT_CAST(UINT16, MIN(rh, dstHeight)); |
1287 | 0 | clippingRect.right = right; |
1288 | 0 | clippingRect.bottom = bottom; |
1289 | 0 | region16_union_rect(&clippingRects, &clippingRects, &clippingRect); |
1290 | 0 | } |
1291 | | |
1292 | 2.02k | for (UINT32 i = 0; i < message->numTiles; i++) |
1293 | 0 | { |
1294 | 0 | RECTANGLE_16 updateRect = { 0 }; |
1295 | 0 | const RFX_TILE* tile = rfx_message_get_tile(message, i); |
1296 | |
|
1297 | 0 | WINPR_ASSERT(left + tile->x <= UINT16_MAX); |
1298 | 0 | WINPR_ASSERT(top + tile->y <= UINT16_MAX); |
1299 | | |
1300 | 0 | updateRect.left = (UINT16)left + tile->x; |
1301 | 0 | updateRect.top = (UINT16)top + tile->y; |
1302 | 0 | updateRect.right = updateRect.left + 64; |
1303 | 0 | updateRect.bottom = updateRect.top + 64; |
1304 | 0 | region16_init(&updateRegion); |
1305 | 0 | region16_intersect_rect(&updateRegion, &clippingRects, &updateRect); |
1306 | 0 | updateRects = region16_rects(&updateRegion, &nbUpdateRects); |
1307 | |
|
1308 | 0 | for (UINT32 j = 0; j < nbUpdateRects; j++) |
1309 | 0 | { |
1310 | 0 | const RECTANGLE_16* cur = &updateRects[j]; |
1311 | 0 | const UINT32 stride = 64 * formatSize; |
1312 | 0 | const UINT32 nXDst = cur->left; |
1313 | 0 | const UINT32 nYDst = cur->top; |
1314 | 0 | const UINT32 nXSrc = nXDst - updateRect.left; |
1315 | 0 | const UINT32 nYSrc = nYDst - updateRect.top; |
1316 | 0 | const UINT32 nWidth = cur->right - cur->left; |
1317 | 0 | const UINT32 nHeight = cur->bottom - cur->top; |
1318 | |
|
1319 | 0 | if (!freerdp_image_copy_no_overlap(dst, dstFormat, dstStride, nXDst, nYDst, nWidth, |
1320 | 0 | nHeight, tile->data, context->pixel_format, |
1321 | 0 | stride, nXSrc, nYSrc, NULL, FREERDP_FLIP_NONE)) |
1322 | 0 | { |
1323 | 0 | region16_uninit(&updateRegion); |
1324 | 0 | region16_uninit(&clippingRects); |
1325 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, |
1326 | 0 | "nbUpdateRectx[%" PRIu32 " (%" PRIu32 ")] freerdp_image_copy failed", |
1327 | 0 | j, nbUpdateRects); |
1328 | 0 | return FALSE; |
1329 | 0 | } |
1330 | | |
1331 | 0 | if (invalidRegion) |
1332 | 0 | region16_union_rect(invalidRegion, invalidRegion, cur); |
1333 | 0 | } |
1334 | | |
1335 | 0 | region16_uninit(&updateRegion); |
1336 | 0 | } |
1337 | | |
1338 | 2.02k | region16_uninit(&clippingRects); |
1339 | 2.02k | return TRUE; |
1340 | 2.02k | } |
1341 | 856 | else |
1342 | 856 | { |
1343 | 856 | rfx_message_free(context, message); |
1344 | 856 | context->currentMessage.freeArray = TRUE; |
1345 | 856 | } |
1346 | | |
1347 | 856 | WLog_Print(context->priv->log, WLOG_ERROR, "failed"); |
1348 | 856 | return FALSE; |
1349 | 2.88k | } |
1350 | | |
1351 | | const UINT32* rfx_message_get_quants(const RFX_MESSAGE* WINPR_RESTRICT message, |
1352 | | UINT16* WINPR_RESTRICT numQuantVals) |
1353 | 0 | { |
1354 | 0 | WINPR_ASSERT(message); |
1355 | 0 | if (numQuantVals) |
1356 | 0 | *numQuantVals = message->numQuant; |
1357 | 0 | return message->quantVals; |
1358 | 0 | } |
1359 | | |
1360 | | const RFX_TILE** rfx_message_get_tiles(const RFX_MESSAGE* WINPR_RESTRICT message, |
1361 | | UINT16* WINPR_RESTRICT numTiles) |
1362 | 0 | { |
1363 | 0 | WINPR_ASSERT(message); |
1364 | 0 | if (numTiles) |
1365 | 0 | *numTiles = message->numTiles; |
1366 | |
|
1367 | 0 | union |
1368 | 0 | { |
1369 | 0 | RFX_TILE** pp; |
1370 | 0 | const RFX_TILE** ppc; |
1371 | 0 | } cnv; |
1372 | 0 | cnv.pp = message->tiles; |
1373 | 0 | return cnv.ppc; |
1374 | 0 | } |
1375 | | |
1376 | | UINT16 rfx_message_get_tile_count(const RFX_MESSAGE* WINPR_RESTRICT message) |
1377 | 0 | { |
1378 | 0 | WINPR_ASSERT(message); |
1379 | 0 | return message->numTiles; |
1380 | 0 | } |
1381 | | |
1382 | | const RFX_RECT* rfx_message_get_rects(const RFX_MESSAGE* WINPR_RESTRICT message, |
1383 | | UINT16* WINPR_RESTRICT numRects) |
1384 | 0 | { |
1385 | 0 | WINPR_ASSERT(message); |
1386 | 0 | if (numRects) |
1387 | 0 | *numRects = message->numRects; |
1388 | 0 | return message->rects; |
1389 | 0 | } |
1390 | | |
1391 | | UINT16 rfx_message_get_rect_count(const RFX_MESSAGE* WINPR_RESTRICT message) |
1392 | 0 | { |
1393 | 0 | WINPR_ASSERT(message); |
1394 | 0 | return message->numRects; |
1395 | 0 | } |
1396 | | |
1397 | | void rfx_message_free(RFX_CONTEXT* WINPR_RESTRICT context, RFX_MESSAGE* WINPR_RESTRICT message) |
1398 | 12.8k | { |
1399 | 12.8k | if (!message) |
1400 | 0 | return; |
1401 | | |
1402 | 12.8k | winpr_aligned_free(message->rects); |
1403 | | |
1404 | 12.8k | if (message->tiles) |
1405 | 0 | { |
1406 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
1407 | 0 | { |
1408 | 0 | RFX_TILE* tile = message->tiles[i]; |
1409 | 0 | if (!tile) |
1410 | 0 | continue; |
1411 | | |
1412 | 0 | if (tile->YCbCrData) |
1413 | 0 | { |
1414 | 0 | BufferPool_Return(context->priv->BufferPool, tile->YCbCrData); |
1415 | 0 | tile->YCbCrData = NULL; |
1416 | 0 | } |
1417 | |
|
1418 | 0 | ObjectPool_Return(context->priv->TilePool, (void*)tile); |
1419 | 0 | } |
1420 | |
|
1421 | 0 | rfx_allocate_tiles(message, 0, FALSE); |
1422 | 0 | } |
1423 | | |
1424 | 12.8k | const BOOL freeArray = message->freeArray; |
1425 | 12.8k | const RFX_MESSAGE empty = { 0 }; |
1426 | 12.8k | *message = empty; |
1427 | | |
1428 | 12.8k | if (!freeArray) |
1429 | 0 | winpr_aligned_free(message); |
1430 | 12.8k | } |
1431 | | |
1432 | | static inline void rfx_update_context_properties(RFX_CONTEXT* WINPR_RESTRICT context) |
1433 | 0 | { |
1434 | 0 | UINT16 properties = 0; |
1435 | |
|
1436 | 0 | WINPR_ASSERT(context); |
1437 | | /* properties in tilesets: note that this has different format from the one in TS_RFX_CONTEXT */ |
1438 | 0 | properties = 1; /* lt */ |
1439 | 0 | properties |= (context->flags << 1); /* flags */ |
1440 | 0 | properties |= (COL_CONV_ICT << 4); /* cct */ |
1441 | 0 | properties |= (CLW_XFORM_DWT_53_A << 6); /* xft */ |
1442 | 0 | properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 10); /* et */ |
1443 | 0 | properties |= (SCALAR_QUANTIZATION << 14); /* qt */ |
1444 | 0 | context->properties = properties; |
1445 | 0 | } |
1446 | | |
1447 | | static inline void |
1448 | | rfx_write_message_sync(WINPR_ATTR_UNUSED const RFX_CONTEXT* WINPR_RESTRICT context, |
1449 | | WINPR_ATTR_UNUSED wStream* WINPR_RESTRICT s) |
1450 | 0 | { |
1451 | 0 | WINPR_ASSERT(context); |
1452 | | |
1453 | 0 | Stream_Write_UINT16(s, WBT_SYNC); /* BlockT.blockType (2 bytes) */ |
1454 | 0 | Stream_Write_UINT32(s, 12); /* BlockT.blockLen (4 bytes) */ |
1455 | 0 | Stream_Write_UINT32(s, WF_MAGIC); /* magic (4 bytes) */ |
1456 | 0 | Stream_Write_UINT16(s, WF_VERSION_1_0); /* version (2 bytes) */ |
1457 | 0 | } |
1458 | | |
1459 | | static inline void |
1460 | | rfx_write_message_codec_versions(WINPR_ATTR_UNUSED const RFX_CONTEXT* WINPR_RESTRICT context, |
1461 | | wStream* WINPR_RESTRICT s) |
1462 | 0 | { |
1463 | 0 | WINPR_ASSERT(context); |
1464 | | |
1465 | 0 | Stream_Write_UINT16(s, WBT_CODEC_VERSIONS); /* BlockT.blockType (2 bytes) */ |
1466 | 0 | Stream_Write_UINT32(s, 10); /* BlockT.blockLen (4 bytes) */ |
1467 | 0 | Stream_Write_UINT8(s, 1); /* numCodecs (1 byte) */ |
1468 | 0 | Stream_Write_UINT8(s, 1); /* codecs.codecId (1 byte) */ |
1469 | 0 | Stream_Write_UINT16(s, WF_VERSION_1_0); /* codecs.version (2 bytes) */ |
1470 | 0 | } |
1471 | | |
1472 | | static inline void rfx_write_message_channels(const RFX_CONTEXT* WINPR_RESTRICT context, |
1473 | | wStream* WINPR_RESTRICT s) |
1474 | 0 | { |
1475 | 0 | WINPR_ASSERT(context); |
1476 | | |
1477 | 0 | Stream_Write_UINT16(s, WBT_CHANNELS); /* BlockT.blockType (2 bytes) */ |
1478 | 0 | Stream_Write_UINT32(s, 12); /* BlockT.blockLen (4 bytes) */ |
1479 | 0 | Stream_Write_UINT8(s, 1); /* numChannels (1 byte) */ |
1480 | 0 | Stream_Write_UINT8(s, 0); /* Channel.channelId (1 byte) */ |
1481 | 0 | Stream_Write_UINT16(s, context->width); /* Channel.width (2 bytes) */ |
1482 | 0 | Stream_Write_UINT16(s, context->height); /* Channel.height (2 bytes) */ |
1483 | 0 | } |
1484 | | |
1485 | | static inline void rfx_write_message_context(RFX_CONTEXT* WINPR_RESTRICT context, |
1486 | | wStream* WINPR_RESTRICT s) |
1487 | 0 | { |
1488 | 0 | UINT16 properties = 0; |
1489 | 0 | WINPR_ASSERT(context); |
1490 | | |
1491 | 0 | Stream_Write_UINT16(s, WBT_CONTEXT); /* CodecChannelT.blockType (2 bytes) */ |
1492 | 0 | Stream_Write_UINT32(s, 13); /* CodecChannelT.blockLen (4 bytes) */ |
1493 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId (1 byte) */ |
1494 | 0 | Stream_Write_UINT8(s, 0xFF); /* CodecChannelT.channelId (1 byte) */ |
1495 | 0 | Stream_Write_UINT8(s, 0); /* ctxId (1 byte) */ |
1496 | 0 | Stream_Write_UINT16(s, CT_TILE_64x64); /* tileSize (2 bytes) */ |
1497 | | /* properties */ |
1498 | 0 | properties = context->flags; /* flags */ |
1499 | 0 | properties |= (COL_CONV_ICT << 3); /* cct */ |
1500 | 0 | properties |= (CLW_XFORM_DWT_53_A << 5); /* xft */ |
1501 | 0 | properties |= ((context->mode == RLGR1 ? CLW_ENTROPY_RLGR1 : CLW_ENTROPY_RLGR3) << 9); /* et */ |
1502 | 0 | properties |= (SCALAR_QUANTIZATION << 13); /* qt */ |
1503 | 0 | Stream_Write_UINT16(s, properties); /* properties (2 bytes) */ |
1504 | 0 | rfx_update_context_properties(context); |
1505 | 0 | } |
1506 | | |
1507 | | static inline BOOL rfx_compose_message_header(RFX_CONTEXT* WINPR_RESTRICT context, |
1508 | | wStream* WINPR_RESTRICT s) |
1509 | 0 | { |
1510 | 0 | WINPR_ASSERT(context); |
1511 | 0 | if (!Stream_EnsureRemainingCapacity(s, 12 + 10 + 12 + 13)) |
1512 | 0 | return FALSE; |
1513 | | |
1514 | 0 | rfx_write_message_sync(context, s); |
1515 | 0 | rfx_write_message_context(context, s); |
1516 | 0 | rfx_write_message_codec_versions(context, s); |
1517 | 0 | rfx_write_message_channels(context, s); |
1518 | 0 | return TRUE; |
1519 | 0 | } |
1520 | | |
1521 | | static inline size_t rfx_tile_length(const RFX_TILE* WINPR_RESTRICT tile) |
1522 | 0 | { |
1523 | 0 | WINPR_ASSERT(tile); |
1524 | 0 | return 19ull + tile->YLen + tile->CbLen + tile->CrLen; |
1525 | 0 | } |
1526 | | |
1527 | | static inline BOOL rfx_write_tile(wStream* WINPR_RESTRICT s, const RFX_TILE* WINPR_RESTRICT tile) |
1528 | 0 | { |
1529 | 0 | const size_t blockLen = rfx_tile_length(tile); |
1530 | 0 | if (blockLen > UINT32_MAX) |
1531 | 0 | return FALSE; |
1532 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
1533 | 0 | return FALSE; |
1534 | | |
1535 | 0 | Stream_Write_UINT16(s, CBT_TILE); /* BlockT.blockType (2 bytes) */ |
1536 | 0 | Stream_Write_UINT32(s, (UINT32)blockLen); /* BlockT.blockLen (4 bytes) */ |
1537 | 0 | Stream_Write_UINT8(s, tile->quantIdxY); /* quantIdxY (1 byte) */ |
1538 | 0 | Stream_Write_UINT8(s, tile->quantIdxCb); /* quantIdxCb (1 byte) */ |
1539 | 0 | Stream_Write_UINT8(s, tile->quantIdxCr); /* quantIdxCr (1 byte) */ |
1540 | 0 | Stream_Write_UINT16(s, tile->xIdx); /* xIdx (2 bytes) */ |
1541 | 0 | Stream_Write_UINT16(s, tile->yIdx); /* yIdx (2 bytes) */ |
1542 | 0 | Stream_Write_UINT16(s, tile->YLen); /* YLen (2 bytes) */ |
1543 | 0 | Stream_Write_UINT16(s, tile->CbLen); /* CbLen (2 bytes) */ |
1544 | 0 | Stream_Write_UINT16(s, tile->CrLen); /* CrLen (2 bytes) */ |
1545 | 0 | Stream_Write(s, tile->YData, tile->YLen); /* YData */ |
1546 | 0 | Stream_Write(s, tile->CbData, tile->CbLen); /* CbData */ |
1547 | 0 | Stream_Write(s, tile->CrData, tile->CrLen); /* CrData */ |
1548 | 0 | return TRUE; |
1549 | 0 | } |
1550 | | |
1551 | | struct S_RFX_TILE_COMPOSE_WORK_PARAM |
1552 | | { |
1553 | | RFX_TILE* tile; |
1554 | | RFX_CONTEXT* context; |
1555 | | }; |
1556 | | |
1557 | | static inline void CALLBACK |
1558 | | rfx_compose_message_tile_work_callback(WINPR_ATTR_UNUSED PTP_CALLBACK_INSTANCE instance, |
1559 | | void* context, WINPR_ATTR_UNUSED PTP_WORK work) |
1560 | 0 | { |
1561 | 0 | RFX_TILE_COMPOSE_WORK_PARAM* param = (RFX_TILE_COMPOSE_WORK_PARAM*)context; |
1562 | 0 | WINPR_ASSERT(param); |
1563 | 0 | rfx_encode_rgb(param->context, param->tile); |
1564 | 0 | } |
1565 | | |
1566 | | static inline BOOL computeRegion(const RFX_RECT* WINPR_RESTRICT rects, size_t numRects, |
1567 | | REGION16* WINPR_RESTRICT region, size_t width, size_t height) |
1568 | 0 | { |
1569 | 0 | const RECTANGLE_16 mainRect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, width), |
1570 | 0 | WINPR_ASSERTING_INT_CAST(UINT16, height) }; |
1571 | |
|
1572 | 0 | WINPR_ASSERT(rects); |
1573 | 0 | for (size_t i = 0; i < numRects; i++) |
1574 | 0 | { |
1575 | 0 | const RFX_RECT* rect = &rects[i]; |
1576 | 0 | RECTANGLE_16 rect16 = { 0 }; |
1577 | 0 | rect16.left = rect->x; |
1578 | 0 | rect16.top = rect->y; |
1579 | 0 | rect16.right = rect->x + rect->width; |
1580 | 0 | rect16.bottom = rect->y + rect->height; |
1581 | |
|
1582 | 0 | if (!region16_union_rect(region, region, &rect16)) |
1583 | 0 | return FALSE; |
1584 | 0 | } |
1585 | | |
1586 | 0 | return region16_intersect_rect(region, region, &mainRect); |
1587 | 0 | } |
1588 | | |
1589 | 0 | #define TILE_NO(v) ((v) / 64) |
1590 | | |
1591 | | static inline BOOL setupWorkers(RFX_CONTEXT* WINPR_RESTRICT context, size_t nbTiles) |
1592 | 0 | { |
1593 | 0 | WINPR_ASSERT(context); |
1594 | | |
1595 | 0 | RFX_CONTEXT_PRIV* priv = context->priv; |
1596 | 0 | WINPR_ASSERT(priv); |
1597 | | |
1598 | 0 | void* pmem = NULL; |
1599 | |
|
1600 | 0 | if (!context->priv->UseThreads) |
1601 | 0 | return TRUE; |
1602 | | |
1603 | 0 | if (!(pmem = winpr_aligned_recalloc((void*)priv->workObjects, nbTiles, sizeof(PTP_WORK), 32))) |
1604 | 0 | return FALSE; |
1605 | | |
1606 | 0 | priv->workObjects = (PTP_WORK*)pmem; |
1607 | |
|
1608 | 0 | if (!(pmem = winpr_aligned_recalloc(priv->tileWorkParams, nbTiles, |
1609 | 0 | sizeof(RFX_TILE_COMPOSE_WORK_PARAM), 32))) |
1610 | 0 | return FALSE; |
1611 | | |
1612 | 0 | priv->tileWorkParams = (RFX_TILE_COMPOSE_WORK_PARAM*)pmem; |
1613 | 0 | return TRUE; |
1614 | 0 | } |
1615 | | |
1616 | | static inline BOOL rfx_ensure_tiles(RFX_MESSAGE* WINPR_RESTRICT message, size_t count) |
1617 | 0 | { |
1618 | 0 | WINPR_ASSERT(message); |
1619 | | |
1620 | 0 | if (message->numTiles + count <= message->allocatedTiles) |
1621 | 0 | return TRUE; |
1622 | | |
1623 | 0 | const size_t alloc = MAX(message->allocatedTiles + 1024, message->numTiles + count); |
1624 | 0 | return rfx_allocate_tiles(message, alloc, TRUE); |
1625 | 0 | } |
1626 | | |
1627 | | RFX_MESSAGE* rfx_encode_message(RFX_CONTEXT* WINPR_RESTRICT context, |
1628 | | const RFX_RECT* WINPR_RESTRICT rects, size_t numRects, |
1629 | | const BYTE* WINPR_RESTRICT data, UINT32 w, UINT32 h, size_t s) |
1630 | 0 | { |
1631 | 0 | const UINT32 width = w; |
1632 | 0 | const UINT32 height = h; |
1633 | 0 | const UINT32 scanline = (UINT32)s; |
1634 | 0 | RFX_MESSAGE* message = NULL; |
1635 | 0 | PTP_WORK* workObject = NULL; |
1636 | 0 | RFX_TILE_COMPOSE_WORK_PARAM* workParam = NULL; |
1637 | 0 | BOOL success = FALSE; |
1638 | 0 | REGION16 rectsRegion = { 0 }; |
1639 | 0 | REGION16 tilesRegion = { 0 }; |
1640 | 0 | RECTANGLE_16 currentTileRect = { 0 }; |
1641 | 0 | const RECTANGLE_16* regionRect = NULL; |
1642 | |
|
1643 | 0 | WINPR_ASSERT(data); |
1644 | 0 | WINPR_ASSERT(rects); |
1645 | 0 | WINPR_ASSERT(numRects > 0); |
1646 | 0 | WINPR_ASSERT(w > 0); |
1647 | 0 | WINPR_ASSERT(h > 0); |
1648 | 0 | WINPR_ASSERT(s > 0); |
1649 | | |
1650 | 0 | if (!(message = (RFX_MESSAGE*)winpr_aligned_calloc(1, sizeof(RFX_MESSAGE), 32))) |
1651 | 0 | return NULL; |
1652 | | |
1653 | 0 | region16_init(&tilesRegion); |
1654 | 0 | region16_init(&rectsRegion); |
1655 | |
|
1656 | 0 | if (context->state == RFX_STATE_SEND_HEADERS) |
1657 | 0 | rfx_update_context_properties(context); |
1658 | |
|
1659 | 0 | message->frameIdx = context->frameIdx++; |
1660 | |
|
1661 | 0 | if (!context->numQuant) |
1662 | 0 | { |
1663 | 0 | WINPR_ASSERT(context->quants == NULL); |
1664 | 0 | if (!(context->quants = |
1665 | 0 | (UINT32*)winpr_aligned_malloc(sizeof(rfx_default_quantization_values), 32))) |
1666 | 0 | goto skip_encoding_loop; |
1667 | | |
1668 | 0 | CopyMemory(context->quants, &rfx_default_quantization_values, |
1669 | 0 | sizeof(rfx_default_quantization_values)); |
1670 | 0 | context->numQuant = 1; |
1671 | 0 | context->quantIdxY = 0; |
1672 | 0 | context->quantIdxCb = 0; |
1673 | 0 | context->quantIdxCr = 0; |
1674 | 0 | } |
1675 | | |
1676 | 0 | message->numQuant = context->numQuant; |
1677 | 0 | message->quantVals = context->quants; |
1678 | |
|
1679 | 0 | { |
1680 | 0 | const UINT32 bytesPerPixel = (context->bits_per_pixel / 8); |
1681 | 0 | if (!computeRegion(rects, numRects, &rectsRegion, width, height)) |
1682 | 0 | goto skip_encoding_loop; |
1683 | | |
1684 | 0 | { |
1685 | 0 | const RECTANGLE_16* extents = region16_extents(&rectsRegion); |
1686 | 0 | WINPR_ASSERT((INT32)extents->right - extents->left > 0); |
1687 | 0 | WINPR_ASSERT((INT32)extents->bottom - extents->top > 0); |
1688 | 0 | const UINT32 maxTilesX = 1 + TILE_NO(extents->right - 1) - TILE_NO(extents->left); |
1689 | 0 | const UINT32 maxTilesY = 1 + TILE_NO(extents->bottom - 1) - TILE_NO(extents->top); |
1690 | 0 | const UINT32 maxNbTiles = maxTilesX * maxTilesY; |
1691 | |
|
1692 | 0 | if (!rfx_ensure_tiles(message, maxNbTiles)) |
1693 | 0 | goto skip_encoding_loop; |
1694 | | |
1695 | 0 | if (!setupWorkers(context, maxNbTiles)) |
1696 | 0 | goto skip_encoding_loop; |
1697 | 0 | } |
1698 | | |
1699 | 0 | if (context->priv->UseThreads) |
1700 | 0 | { |
1701 | 0 | workObject = context->priv->workObjects; |
1702 | 0 | workParam = context->priv->tileWorkParams; |
1703 | 0 | } |
1704 | |
|
1705 | 0 | { |
1706 | 0 | UINT32 regionNbRects = 0; |
1707 | 0 | regionRect = region16_rects(&rectsRegion, ®ionNbRects); |
1708 | |
|
1709 | 0 | if (!(message->rects = winpr_aligned_calloc(regionNbRects, sizeof(RFX_RECT), 32))) |
1710 | 0 | goto skip_encoding_loop; |
1711 | | |
1712 | 0 | message->numRects = WINPR_ASSERTING_INT_CAST(UINT16, regionNbRects); |
1713 | |
|
1714 | 0 | for (UINT32 i = 0; i < regionNbRects; i++, regionRect++) |
1715 | 0 | { |
1716 | 0 | RFX_RECT* rfxRect = &message->rects[i]; |
1717 | 0 | UINT32 startTileX = regionRect->left / 64; |
1718 | 0 | UINT32 endTileX = (regionRect->right - 1) / 64; |
1719 | 0 | UINT32 startTileY = regionRect->top / 64; |
1720 | 0 | UINT32 endTileY = (regionRect->bottom - 1) / 64; |
1721 | 0 | rfxRect->x = regionRect->left; |
1722 | 0 | rfxRect->y = regionRect->top; |
1723 | 0 | rfxRect->width = (regionRect->right - regionRect->left); |
1724 | 0 | rfxRect->height = (regionRect->bottom - regionRect->top); |
1725 | |
|
1726 | 0 | for (UINT32 yIdx = startTileY, gridRelY = startTileY * 64; yIdx <= endTileY; |
1727 | 0 | yIdx++, gridRelY += 64) |
1728 | 0 | { |
1729 | 0 | UINT32 tileHeight = 64; |
1730 | |
|
1731 | 0 | if ((yIdx == endTileY) && (gridRelY + 64 > height)) |
1732 | 0 | tileHeight = height - gridRelY; |
1733 | |
|
1734 | 0 | currentTileRect.top = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY); |
1735 | 0 | currentTileRect.bottom = |
1736 | 0 | WINPR_ASSERTING_INT_CAST(UINT16, gridRelY + tileHeight); |
1737 | |
|
1738 | 0 | for (UINT32 xIdx = startTileX, gridRelX = startTileX * 64; xIdx <= endTileX; |
1739 | 0 | xIdx++, gridRelX += 64) |
1740 | 0 | { |
1741 | 0 | union |
1742 | 0 | { |
1743 | 0 | const BYTE* cpv; |
1744 | 0 | BYTE* pv; |
1745 | 0 | } cnv; |
1746 | 0 | UINT32 tileWidth = 64; |
1747 | |
|
1748 | 0 | if ((xIdx == endTileX) && (gridRelX + 64 > width)) |
1749 | 0 | { |
1750 | 0 | tileWidth = (width - gridRelX); |
1751 | 0 | } |
1752 | |
|
1753 | 0 | currentTileRect.left = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX); |
1754 | 0 | currentTileRect.right = |
1755 | 0 | WINPR_ASSERTING_INT_CAST(UINT16, gridRelX + tileWidth); |
1756 | | |
1757 | | /* checks if this tile is already treated */ |
1758 | 0 | if (region16_intersects_rect(&tilesRegion, ¤tTileRect)) |
1759 | 0 | continue; |
1760 | | |
1761 | 0 | RFX_TILE* tile = (RFX_TILE*)ObjectPool_Take(context->priv->TilePool); |
1762 | 0 | if (!tile) |
1763 | 0 | goto skip_encoding_loop; |
1764 | | |
1765 | 0 | tile->xIdx = WINPR_ASSERTING_INT_CAST(UINT16, xIdx); |
1766 | 0 | tile->yIdx = WINPR_ASSERTING_INT_CAST(UINT16, yIdx); |
1767 | 0 | tile->x = WINPR_ASSERTING_INT_CAST(UINT16, gridRelX); |
1768 | 0 | tile->y = WINPR_ASSERTING_INT_CAST(UINT16, gridRelY); |
1769 | 0 | tile->scanline = scanline; |
1770 | |
|
1771 | 0 | tile->width = tileWidth; |
1772 | 0 | tile->height = tileHeight; |
1773 | 0 | const UINT32 ax = gridRelX; |
1774 | 0 | const UINT32 ay = gridRelY; |
1775 | |
|
1776 | 0 | if (tile->data && tile->allocated) |
1777 | 0 | { |
1778 | 0 | winpr_aligned_free(tile->data); |
1779 | 0 | tile->allocated = FALSE; |
1780 | 0 | } |
1781 | | |
1782 | | /* Cast away const */ |
1783 | 0 | cnv.cpv = &data[(ay * scanline) + (ax * bytesPerPixel)]; |
1784 | 0 | tile->data = cnv.pv; |
1785 | 0 | tile->quantIdxY = context->quantIdxY; |
1786 | 0 | tile->quantIdxCb = context->quantIdxCb; |
1787 | 0 | tile->quantIdxCr = context->quantIdxCr; |
1788 | 0 | tile->YLen = tile->CbLen = tile->CrLen = 0; |
1789 | |
|
1790 | 0 | if (!(tile->YCbCrData = |
1791 | 0 | (BYTE*)BufferPool_Take(context->priv->BufferPool, -1))) |
1792 | 0 | goto skip_encoding_loop; |
1793 | | |
1794 | 0 | tile->YData = &(tile->YCbCrData[((8192 + 32) * 0) + 16]); |
1795 | 0 | tile->CbData = &(tile->YCbCrData[((8192 + 32) * 1) + 16]); |
1796 | 0 | tile->CrData = &(tile->YCbCrData[((8192 + 32) * 2) + 16]); |
1797 | |
|
1798 | 0 | if (!rfx_ensure_tiles(message, 1)) |
1799 | 0 | goto skip_encoding_loop; |
1800 | 0 | message->tiles[message->numTiles++] = tile; |
1801 | |
|
1802 | 0 | if (context->priv->UseThreads) |
1803 | 0 | { |
1804 | 0 | workParam->context = context; |
1805 | 0 | workParam->tile = tile; |
1806 | |
|
1807 | 0 | if (!(*workObject = |
1808 | 0 | CreateThreadpoolWork(rfx_compose_message_tile_work_callback, |
1809 | 0 | (void*)workParam, NULL))) |
1810 | 0 | { |
1811 | 0 | goto skip_encoding_loop; |
1812 | 0 | } |
1813 | | |
1814 | 0 | SubmitThreadpoolWork(*workObject); |
1815 | 0 | workObject++; |
1816 | 0 | workParam++; |
1817 | 0 | } |
1818 | 0 | else |
1819 | 0 | { |
1820 | 0 | rfx_encode_rgb(context, tile); |
1821 | 0 | } |
1822 | | |
1823 | 0 | if (!region16_union_rect(&tilesRegion, &tilesRegion, ¤tTileRect)) |
1824 | 0 | goto skip_encoding_loop; |
1825 | 0 | } /* xIdx */ |
1826 | 0 | } /* yIdx */ |
1827 | 0 | } /* rects */ |
1828 | 0 | } |
1829 | 0 | } |
1830 | | |
1831 | 0 | success = TRUE; |
1832 | 0 | skip_encoding_loop: |
1833 | | |
1834 | | /* when using threads ensure all computations are done */ |
1835 | 0 | if (success) |
1836 | 0 | { |
1837 | 0 | message->tilesDataSize = 0; |
1838 | 0 | workObject = context->priv->workObjects; |
1839 | |
|
1840 | 0 | for (UINT32 i = 0; i < message->numTiles; i++) |
1841 | 0 | { |
1842 | 0 | if (context->priv->UseThreads) |
1843 | 0 | { |
1844 | 0 | if (*workObject) |
1845 | 0 | { |
1846 | 0 | WaitForThreadpoolWorkCallbacks(*workObject, FALSE); |
1847 | 0 | CloseThreadpoolWork(*workObject); |
1848 | 0 | } |
1849 | |
|
1850 | 0 | workObject++; |
1851 | 0 | } |
1852 | |
|
1853 | 0 | const RFX_TILE* tile = message->tiles[i]; |
1854 | 0 | const size_t tlen = rfx_tile_length(tile); |
1855 | 0 | message->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tlen); |
1856 | 0 | } |
1857 | |
|
1858 | 0 | region16_uninit(&tilesRegion); |
1859 | 0 | region16_uninit(&rectsRegion); |
1860 | |
|
1861 | 0 | return message; |
1862 | 0 | } |
1863 | | |
1864 | 0 | WLog_Print(context->priv->log, WLOG_ERROR, "failed"); |
1865 | |
|
1866 | 0 | rfx_message_free(context, message); |
1867 | 0 | region16_uninit(&tilesRegion); |
1868 | 0 | region16_uninit(&rectsRegion); |
1869 | 0 | return NULL; |
1870 | 0 | } |
1871 | | |
1872 | | static inline BOOL rfx_clone_rects(RFX_MESSAGE* WINPR_RESTRICT dst, |
1873 | | const RFX_MESSAGE* WINPR_RESTRICT src) |
1874 | 0 | { |
1875 | 0 | WINPR_ASSERT(dst); |
1876 | 0 | WINPR_ASSERT(src); |
1877 | | |
1878 | 0 | WINPR_ASSERT(dst->rects == NULL); |
1879 | 0 | WINPR_ASSERT(dst->numRects == 0); |
1880 | | |
1881 | 0 | if (src->numRects == 0) |
1882 | 0 | return TRUE; |
1883 | | |
1884 | 0 | dst->rects = winpr_aligned_calloc(src->numRects, sizeof(RECTANGLE_16), 32); |
1885 | 0 | if (!dst->rects) |
1886 | 0 | return FALSE; |
1887 | 0 | dst->numRects = src->numRects; |
1888 | 0 | for (size_t x = 0; x < src->numRects; x++) |
1889 | 0 | { |
1890 | 0 | dst->rects[x] = src->rects[x]; |
1891 | 0 | } |
1892 | 0 | return TRUE; |
1893 | 0 | } |
1894 | | |
1895 | | static inline BOOL rfx_clone_quants(RFX_MESSAGE* WINPR_RESTRICT dst, |
1896 | | const RFX_MESSAGE* WINPR_RESTRICT src) |
1897 | 0 | { |
1898 | 0 | WINPR_ASSERT(dst); |
1899 | 0 | WINPR_ASSERT(src); |
1900 | | |
1901 | 0 | WINPR_ASSERT(dst->quantVals == NULL); |
1902 | 0 | WINPR_ASSERT(dst->numQuant == 0); |
1903 | | |
1904 | 0 | if (src->numQuant == 0) |
1905 | 0 | return TRUE; |
1906 | | |
1907 | | /* quantVals are part of context */ |
1908 | 0 | dst->quantVals = src->quantVals; |
1909 | 0 | dst->numQuant = src->numQuant; |
1910 | |
|
1911 | 0 | return TRUE; |
1912 | 0 | } |
1913 | | |
1914 | | static inline RFX_MESSAGE* rfx_split_message(RFX_CONTEXT* WINPR_RESTRICT context, |
1915 | | RFX_MESSAGE* WINPR_RESTRICT message, |
1916 | | size_t* WINPR_RESTRICT numMessages, size_t maxDataSize) |
1917 | 0 | { |
1918 | 0 | WINPR_ASSERT(context); |
1919 | 0 | WINPR_ASSERT(message); |
1920 | 0 | WINPR_ASSERT(numMessages); |
1921 | | |
1922 | 0 | maxDataSize -= 1024; /* reserve enough space for headers */ |
1923 | 0 | *numMessages = ((message->tilesDataSize + maxDataSize) / maxDataSize) * 4ull; |
1924 | |
|
1925 | 0 | RFX_MESSAGE* messages = |
1926 | 0 | (RFX_MESSAGE*)winpr_aligned_calloc((*numMessages), sizeof(RFX_MESSAGE), 32); |
1927 | 0 | if (!messages) |
1928 | 0 | return NULL; |
1929 | | |
1930 | 0 | UINT32 j = 0; |
1931 | 0 | for (UINT16 i = 0; i < message->numTiles; i++) |
1932 | 0 | { |
1933 | 0 | RFX_TILE* tile = message->tiles[i]; |
1934 | 0 | RFX_MESSAGE* msg = &messages[j]; |
1935 | |
|
1936 | 0 | WINPR_ASSERT(tile); |
1937 | 0 | WINPR_ASSERT(msg); |
1938 | | |
1939 | 0 | const size_t tileDataSize = rfx_tile_length(tile); |
1940 | |
|
1941 | 0 | if ((msg->tilesDataSize + tileDataSize) > ((UINT32)maxDataSize)) |
1942 | 0 | j++; |
1943 | |
|
1944 | 0 | if (msg->numTiles == 0) |
1945 | 0 | { |
1946 | 0 | msg->frameIdx = message->frameIdx + j; |
1947 | 0 | if (!rfx_clone_quants(msg, message)) |
1948 | 0 | goto free_messages; |
1949 | 0 | if (!rfx_clone_rects(msg, message)) |
1950 | 0 | goto free_messages; |
1951 | 0 | msg->freeArray = TRUE; |
1952 | 0 | if (!rfx_allocate_tiles(msg, message->numTiles, TRUE)) |
1953 | 0 | goto free_messages; |
1954 | 0 | } |
1955 | | |
1956 | 0 | msg->tilesDataSize += WINPR_ASSERTING_INT_CAST(uint32_t, tileDataSize); |
1957 | |
|
1958 | 0 | WINPR_ASSERT(msg->numTiles < msg->allocatedTiles); |
1959 | 0 | msg->tiles[msg->numTiles++] = message->tiles[i]; |
1960 | 0 | message->tiles[i] = NULL; |
1961 | 0 | } |
1962 | | |
1963 | 0 | *numMessages = j + 1ULL; |
1964 | 0 | context->frameIdx += j; |
1965 | 0 | message->numTiles = 0; |
1966 | 0 | return messages; |
1967 | 0 | free_messages: |
1968 | |
|
1969 | 0 | for (size_t i = 0; i < j; i++) |
1970 | 0 | rfx_allocate_tiles(&messages[i], 0, FALSE); |
1971 | |
|
1972 | 0 | winpr_aligned_free(messages); |
1973 | 0 | return NULL; |
1974 | 0 | } |
1975 | | |
1976 | | const RFX_MESSAGE* rfx_message_list_get(const RFX_MESSAGE_LIST* WINPR_RESTRICT messages, size_t idx) |
1977 | 0 | { |
1978 | 0 | WINPR_ASSERT(messages); |
1979 | 0 | if (idx >= messages->count) |
1980 | 0 | return NULL; |
1981 | 0 | WINPR_ASSERT(messages->list); |
1982 | 0 | return &messages->list[idx]; |
1983 | 0 | } |
1984 | | |
1985 | | void rfx_message_list_free(RFX_MESSAGE_LIST* messages) |
1986 | 0 | { |
1987 | 0 | if (!messages) |
1988 | 0 | return; |
1989 | 0 | for (size_t x = 0; x < messages->count; x++) |
1990 | 0 | rfx_message_free(messages->context, &messages->list[x]); |
1991 | 0 | free(messages); |
1992 | 0 | } |
1993 | | |
1994 | | static inline RFX_MESSAGE_LIST* rfx_message_list_new(RFX_CONTEXT* WINPR_RESTRICT context, |
1995 | | RFX_MESSAGE* WINPR_RESTRICT messages, |
1996 | | size_t count) |
1997 | 0 | { |
1998 | 0 | WINPR_ASSERT(context); |
1999 | 0 | RFX_MESSAGE_LIST* msg = calloc(1, sizeof(RFX_MESSAGE_LIST)); |
2000 | 0 | WINPR_ASSERT(msg); |
2001 | | |
2002 | 0 | msg->context = context; |
2003 | 0 | msg->count = count; |
2004 | 0 | msg->list = messages; |
2005 | 0 | return msg; |
2006 | 0 | } |
2007 | | |
2008 | | RFX_MESSAGE_LIST* rfx_encode_messages(RFX_CONTEXT* WINPR_RESTRICT context, |
2009 | | const RFX_RECT* WINPR_RESTRICT rects, size_t numRects, |
2010 | | const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height, |
2011 | | UINT32 scanline, size_t* WINPR_RESTRICT numMessages, |
2012 | | size_t maxDataSize) |
2013 | 0 | { |
2014 | 0 | WINPR_ASSERT(context); |
2015 | 0 | WINPR_ASSERT(numMessages); |
2016 | | |
2017 | 0 | RFX_MESSAGE* message = |
2018 | 0 | rfx_encode_message(context, rects, numRects, data, width, height, scanline); |
2019 | 0 | if (!message) |
2020 | 0 | return NULL; |
2021 | | |
2022 | 0 | RFX_MESSAGE* list = rfx_split_message(context, message, numMessages, maxDataSize); |
2023 | 0 | rfx_message_free(context, message); |
2024 | 0 | if (!list) |
2025 | 0 | return NULL; |
2026 | | |
2027 | 0 | return rfx_message_list_new(context, list, *numMessages); |
2028 | 0 | } |
2029 | | |
2030 | | static inline BOOL rfx_write_message_tileset(RFX_CONTEXT* WINPR_RESTRICT context, |
2031 | | wStream* WINPR_RESTRICT s, |
2032 | | const RFX_MESSAGE* WINPR_RESTRICT message) |
2033 | 0 | { |
2034 | 0 | WINPR_ASSERT(context); |
2035 | 0 | WINPR_ASSERT(message); |
2036 | | |
2037 | 0 | const UINT32 blockLen = 22 + (message->numQuant * 5) + message->tilesDataSize; |
2038 | |
|
2039 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2040 | 0 | return FALSE; |
2041 | | |
2042 | 0 | Stream_Write_UINT16(s, WBT_EXTENSION); /* CodecChannelT.blockType (2 bytes) */ |
2043 | 0 | Stream_Write_UINT32(s, blockLen); /* set CodecChannelT.blockLen (4 bytes) */ |
2044 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId (1 byte) */ |
2045 | 0 | Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId (1 byte) */ |
2046 | 0 | Stream_Write_UINT16(s, CBT_TILESET); /* subtype (2 bytes) */ |
2047 | 0 | Stream_Write_UINT16(s, 0); /* idx (2 bytes) */ |
2048 | 0 | Stream_Write_UINT16(s, context->properties); /* properties (2 bytes) */ |
2049 | 0 | Stream_Write_UINT8( |
2050 | 0 | s, WINPR_ASSERTING_INT_CAST(uint8_t, message->numQuant)); /* numQuant (1 byte) */ |
2051 | 0 | Stream_Write_UINT8(s, 0x40); /* tileSize (1 byte) */ |
2052 | 0 | Stream_Write_UINT16(s, message->numTiles); /* numTiles (2 bytes) */ |
2053 | 0 | Stream_Write_UINT32(s, message->tilesDataSize); /* tilesDataSize (4 bytes) */ |
2054 | | |
2055 | 0 | UINT32* quantVals = message->quantVals; |
2056 | 0 | for (size_t i = 0; i < message->numQuant * 5ul; i++) |
2057 | 0 | { |
2058 | 0 | WINPR_ASSERT(quantVals); |
2059 | 0 | Stream_Write_UINT8(s, |
2060 | 0 | WINPR_ASSERTING_INT_CAST(uint8_t, quantVals[0] + (quantVals[1] << 4))); |
2061 | 0 | quantVals += 2; |
2062 | 0 | } |
2063 | | |
2064 | 0 | for (size_t i = 0; i < message->numTiles; i++) |
2065 | 0 | { |
2066 | 0 | RFX_TILE* tile = message->tiles[i]; |
2067 | 0 | if (!tile) |
2068 | 0 | return FALSE; |
2069 | | |
2070 | 0 | if (!rfx_write_tile(s, tile)) |
2071 | 0 | return FALSE; |
2072 | 0 | } |
2073 | | |
2074 | | #ifdef WITH_DEBUG_RFX |
2075 | | WLog_Print(context->priv->log, WLOG_DEBUG, |
2076 | | "numQuant: %" PRIu16 " numTiles: %" PRIu16 " tilesDataSize: %" PRIu32 "", |
2077 | | message->numQuant, message->numTiles, message->tilesDataSize); |
2078 | | #endif |
2079 | 0 | return TRUE; |
2080 | 0 | } |
2081 | | |
2082 | | static inline BOOL |
2083 | | rfx_write_message_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context, |
2084 | | wStream* WINPR_RESTRICT s, const RFX_MESSAGE* WINPR_RESTRICT message) |
2085 | 0 | { |
2086 | 0 | WINPR_ASSERT(context); |
2087 | 0 | WINPR_ASSERT(message); |
2088 | | |
2089 | 0 | if (!Stream_EnsureRemainingCapacity(s, 14)) |
2090 | 0 | return FALSE; |
2091 | | |
2092 | 0 | Stream_Write_UINT16(s, WBT_FRAME_BEGIN); /* CodecChannelT.blockType */ |
2093 | 0 | Stream_Write_UINT32(s, 14); /* CodecChannelT.blockLen */ |
2094 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId */ |
2095 | 0 | Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId */ |
2096 | 0 | Stream_Write_UINT32(s, message->frameIdx); /* frameIdx */ |
2097 | 0 | Stream_Write_UINT16(s, 1); /* numRegions */ |
2098 | 0 | return TRUE; |
2099 | 0 | } |
2100 | | |
2101 | | static inline BOOL rfx_write_message_region(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context, |
2102 | | wStream* WINPR_RESTRICT s, |
2103 | | const RFX_MESSAGE* WINPR_RESTRICT message) |
2104 | 0 | { |
2105 | 0 | WINPR_ASSERT(context); |
2106 | 0 | WINPR_ASSERT(message); |
2107 | | |
2108 | 0 | const size_t blockLen = 15 + (message->numRects * 8); |
2109 | 0 | if (blockLen > UINT32_MAX) |
2110 | 0 | return FALSE; |
2111 | | |
2112 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2113 | 0 | return FALSE; |
2114 | | |
2115 | 0 | Stream_Write_UINT16(s, WBT_REGION); /* CodecChannelT.blockType (2 bytes) */ |
2116 | 0 | Stream_Write_UINT32(s, (UINT32)blockLen); /* set CodecChannelT.blockLen (4 bytes) */ |
2117 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId (1 byte) */ |
2118 | 0 | Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId (1 byte) */ |
2119 | 0 | Stream_Write_UINT8(s, 1); /* regionFlags (1 byte) */ |
2120 | 0 | Stream_Write_UINT16(s, message->numRects); /* numRects (2 bytes) */ |
2121 | | |
2122 | 0 | for (UINT16 i = 0; i < message->numRects; i++) |
2123 | 0 | { |
2124 | 0 | const RFX_RECT* rect = rfx_message_get_rect_const(message, i); |
2125 | 0 | WINPR_ASSERT(rect); |
2126 | | |
2127 | | /* Clipping rectangles are relative to destLeft, destTop */ |
2128 | 0 | Stream_Write_UINT16(s, rect->x); /* x (2 bytes) */ |
2129 | 0 | Stream_Write_UINT16(s, rect->y); /* y (2 bytes) */ |
2130 | 0 | Stream_Write_UINT16(s, rect->width); /* width (2 bytes) */ |
2131 | 0 | Stream_Write_UINT16(s, rect->height); /* height (2 bytes) */ |
2132 | 0 | } |
2133 | | |
2134 | 0 | Stream_Write_UINT16(s, CBT_REGION); /* regionType (2 bytes) */ |
2135 | 0 | Stream_Write_UINT16(s, 1); /* numTilesets (2 bytes) */ |
2136 | 0 | return TRUE; |
2137 | 0 | } |
2138 | | |
2139 | | static inline BOOL |
2140 | | rfx_write_message_frame_end(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT context, |
2141 | | wStream* WINPR_RESTRICT s, |
2142 | | WINPR_ATTR_UNUSED const RFX_MESSAGE* WINPR_RESTRICT message) |
2143 | 0 | { |
2144 | 0 | WINPR_ASSERT(context); |
2145 | 0 | WINPR_ASSERT(message); |
2146 | | |
2147 | 0 | if (!Stream_EnsureRemainingCapacity(s, 8)) |
2148 | 0 | return FALSE; |
2149 | | |
2150 | 0 | Stream_Write_UINT16(s, WBT_FRAME_END); /* CodecChannelT.blockType */ |
2151 | 0 | Stream_Write_UINT32(s, 8); /* CodecChannelT.blockLen */ |
2152 | 0 | Stream_Write_UINT8(s, 1); /* CodecChannelT.codecId */ |
2153 | 0 | Stream_Write_UINT8(s, 0); /* CodecChannelT.channelId */ |
2154 | 0 | return TRUE; |
2155 | 0 | } |
2156 | | |
2157 | | BOOL rfx_write_message(RFX_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s, |
2158 | | const RFX_MESSAGE* WINPR_RESTRICT message) |
2159 | 0 | { |
2160 | 0 | WINPR_ASSERT(context); |
2161 | 0 | WINPR_ASSERT(message); |
2162 | | |
2163 | 0 | if (context->state == RFX_STATE_SEND_HEADERS) |
2164 | 0 | { |
2165 | 0 | if (!rfx_compose_message_header(context, s)) |
2166 | 0 | return FALSE; |
2167 | | |
2168 | 0 | context->state = RFX_STATE_SEND_FRAME_DATA; |
2169 | 0 | } |
2170 | | |
2171 | 0 | if (!rfx_write_message_frame_begin(context, s, message) || |
2172 | 0 | !rfx_write_message_region(context, s, message) || |
2173 | 0 | !rfx_write_message_tileset(context, s, message) || |
2174 | 0 | !rfx_write_message_frame_end(context, s, message)) |
2175 | 0 | { |
2176 | 0 | return FALSE; |
2177 | 0 | } |
2178 | | |
2179 | 0 | return TRUE; |
2180 | 0 | } |
2181 | | |
2182 | | BOOL rfx_compose_message(RFX_CONTEXT* WINPR_RESTRICT context, wStream* WINPR_RESTRICT s, |
2183 | | const RFX_RECT* WINPR_RESTRICT rects, size_t numRects, |
2184 | | const BYTE* WINPR_RESTRICT data, UINT32 width, UINT32 height, |
2185 | | UINT32 scanline) |
2186 | 0 | { |
2187 | 0 | WINPR_ASSERT(context); |
2188 | 0 | RFX_MESSAGE* message = |
2189 | 0 | rfx_encode_message(context, rects, numRects, data, width, height, scanline); |
2190 | 0 | if (!message) |
2191 | 0 | return FALSE; |
2192 | | |
2193 | 0 | const BOOL ret = rfx_write_message(context, s, message); |
2194 | 0 | rfx_message_free(context, message); |
2195 | 0 | return ret; |
2196 | 0 | } |
2197 | | |
2198 | | BOOL rfx_context_set_mode(RFX_CONTEXT* WINPR_RESTRICT context, RLGR_MODE mode) |
2199 | 0 | { |
2200 | 0 | WINPR_ASSERT(context); |
2201 | 0 | context->mode = mode; |
2202 | 0 | return TRUE; |
2203 | 0 | } |
2204 | | |
2205 | | RLGR_MODE rfx_context_get_mode(RFX_CONTEXT* WINPR_RESTRICT context) |
2206 | 0 | { |
2207 | 0 | WINPR_ASSERT(context); |
2208 | 0 | return context->mode; |
2209 | 0 | } |
2210 | | |
2211 | | UINT32 rfx_context_get_frame_idx(const RFX_CONTEXT* WINPR_RESTRICT context) |
2212 | 0 | { |
2213 | 0 | WINPR_ASSERT(context); |
2214 | 0 | return context->frameIdx; |
2215 | 0 | } |
2216 | | |
2217 | | UINT32 rfx_message_get_frame_idx(const RFX_MESSAGE* WINPR_RESTRICT message) |
2218 | 0 | { |
2219 | 0 | WINPR_ASSERT(message); |
2220 | 0 | return message->frameIdx; |
2221 | 0 | } |
2222 | | |
2223 | | static inline BOOL rfx_write_progressive_wb_sync(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx, |
2224 | | wStream* WINPR_RESTRICT s) |
2225 | 0 | { |
2226 | 0 | const UINT32 blockLen = 12; |
2227 | 0 | WINPR_ASSERT(rfx); |
2228 | 0 | WINPR_ASSERT(s); |
2229 | | |
2230 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2231 | 0 | return FALSE; |
2232 | | |
2233 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_SYNC); /* blockType (2 bytes) */ |
2234 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2235 | 0 | Stream_Write_UINT32(s, 0xCACCACCA); /* magic (4 bytes) */ |
2236 | 0 | Stream_Write_UINT16(s, 0x0100); /* version (2 bytes) */ |
2237 | 0 | return TRUE; |
2238 | 0 | } |
2239 | | |
2240 | | static inline BOOL |
2241 | | rfx_write_progressive_wb_context(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx, |
2242 | | wStream* WINPR_RESTRICT s) |
2243 | 0 | { |
2244 | 0 | const UINT32 blockLen = 10; |
2245 | 0 | WINPR_ASSERT(rfx); |
2246 | 0 | WINPR_ASSERT(s); |
2247 | | |
2248 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2249 | 0 | return FALSE; |
2250 | | |
2251 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_CONTEXT); /* blockType (2 bytes) */ |
2252 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2253 | 0 | Stream_Write_UINT8(s, 0); /* ctxId (1 byte) */ |
2254 | 0 | Stream_Write_UINT16(s, 64); /* tileSize (2 bytes) */ |
2255 | 0 | Stream_Write_UINT8(s, 0); /* flags (1 byte) */ |
2256 | 0 | return TRUE; |
2257 | 0 | } |
2258 | | |
2259 | | static inline BOOL rfx_write_progressive_region(RFX_CONTEXT* WINPR_RESTRICT rfx, |
2260 | | wStream* WINPR_RESTRICT s, |
2261 | | const RFX_MESSAGE* WINPR_RESTRICT msg) |
2262 | 0 | { |
2263 | | /* RFX_REGION */ |
2264 | 0 | UINT32 blockLen = 18; |
2265 | 0 | UINT32 tilesDataSize = 0; |
2266 | 0 | const size_t start = Stream_GetPosition(s); |
2267 | |
|
2268 | 0 | WINPR_ASSERT(rfx); |
2269 | 0 | WINPR_ASSERT(s); |
2270 | 0 | WINPR_ASSERT(msg); |
2271 | | |
2272 | 0 | blockLen += msg->numRects * 8; |
2273 | 0 | blockLen += msg->numQuant * 5; |
2274 | 0 | tilesDataSize = msg->numTiles * 22UL; |
2275 | 0 | for (UINT16 i = 0; i < msg->numTiles; i++) |
2276 | 0 | { |
2277 | 0 | const RFX_TILE* tile = msg->tiles[i]; |
2278 | 0 | WINPR_ASSERT(tile); |
2279 | 0 | tilesDataSize += tile->YLen + tile->CbLen + tile->CrLen; |
2280 | 0 | } |
2281 | 0 | blockLen += tilesDataSize; |
2282 | |
|
2283 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2284 | 0 | return FALSE; |
2285 | | |
2286 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_REGION); /* blockType (2 bytes) */ |
2287 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2288 | 0 | Stream_Write_UINT8(s, 64); /* tileSize (1 byte) */ |
2289 | 0 | Stream_Write_UINT16(s, msg->numRects); /* numRects (2 bytes) */ |
2290 | 0 | WINPR_ASSERT(msg->numQuant <= UINT8_MAX); |
2291 | 0 | Stream_Write_UINT8(s, (UINT8)msg->numQuant); /* numQuant (1 byte) */ |
2292 | 0 | Stream_Write_UINT8(s, 0); /* numProgQuant (1 byte) */ |
2293 | 0 | Stream_Write_UINT8(s, 0); /* flags (1 byte) */ |
2294 | 0 | Stream_Write_UINT16(s, msg->numTiles); /* numTiles (2 bytes) */ |
2295 | 0 | Stream_Write_UINT32(s, tilesDataSize); /* tilesDataSize (4 bytes) */ |
2296 | | |
2297 | 0 | for (UINT16 i = 0; i < msg->numRects; i++) |
2298 | 0 | { |
2299 | | /* TS_RFX_RECT */ |
2300 | 0 | const RFX_RECT* r = &msg->rects[i]; |
2301 | 0 | Stream_Write_UINT16(s, r->x); /* x (2 bytes) */ |
2302 | 0 | Stream_Write_UINT16(s, r->y); /* y (2 bytes) */ |
2303 | 0 | Stream_Write_UINT16(s, r->width); /* width (2 bytes) */ |
2304 | 0 | Stream_Write_UINT16(s, r->height); /* height (2 bytes) */ |
2305 | 0 | } |
2306 | | |
2307 | | /** |
2308 | | * Note: The RFX_COMPONENT_CODEC_QUANT structure differs from the |
2309 | | * TS_RFX_CODEC_QUANT ([MS-RDPRFX] section 2.2.2.1.5) structure with respect |
2310 | | * to the order of the bands. |
2311 | | * 0 1 2 3 4 5 6 7 8 9 |
2312 | | * RDPRFX: LL3, LH3, HL3, HH3, LH2, HL2, HH2, LH1, HL1, HH1 |
2313 | | * RDPEGFX: LL3, HL3, LH3, HH3, HL2, LH2, HH2, HL1, LH1, HH1 |
2314 | | */ |
2315 | 0 | for (UINT16 i = 0; i < msg->numQuant; i++) |
2316 | 0 | { |
2317 | 0 | const UINT32* qv = &msg->quantVals[10ULL * i]; |
2318 | | /* RFX_COMPONENT_CODEC_QUANT */ |
2319 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[0] + (qv[2] << 4))); /* LL3 (4-bit), HL3 (4-bit) */ |
2320 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[1] + (qv[3] << 4))); /* LH3 (4-bit), HH3 (4-bit) */ |
2321 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[5] + (qv[4] << 4))); /* HL2 (4-bit), LH2 (4-bit) */ |
2322 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[6] + (qv[8] << 4))); /* HH2 (4-bit), HL1 (4-bit) */ |
2323 | 0 | Stream_Write_UINT8(s, (UINT8)(qv[7] + (qv[9] << 4))); /* LH1 (4-bit), HH1 (4-bit) */ |
2324 | 0 | } |
2325 | | |
2326 | 0 | for (UINT16 i = 0; i < msg->numTiles; i++) |
2327 | 0 | { |
2328 | 0 | const RFX_TILE* tile = msg->tiles[i]; |
2329 | 0 | if (!rfx_write_progressive_tile_simple(rfx, s, tile)) |
2330 | 0 | return FALSE; |
2331 | 0 | } |
2332 | | |
2333 | 0 | const size_t end = Stream_GetPosition(s); |
2334 | 0 | const size_t used = end - start; |
2335 | 0 | return (used == blockLen); |
2336 | 0 | } |
2337 | | |
2338 | | static inline BOOL |
2339 | | rfx_write_progressive_frame_begin(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx, |
2340 | | wStream* WINPR_RESTRICT s, const RFX_MESSAGE* WINPR_RESTRICT msg) |
2341 | 0 | { |
2342 | 0 | const UINT32 blockLen = 12; |
2343 | 0 | WINPR_ASSERT(rfx); |
2344 | 0 | WINPR_ASSERT(s); |
2345 | 0 | WINPR_ASSERT(msg); |
2346 | | |
2347 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2348 | 0 | return FALSE; |
2349 | | |
2350 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_BEGIN); /* blockType (2 bytes) */ |
2351 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2352 | 0 | Stream_Write_UINT32(s, msg->frameIdx); /* frameIndex (4 bytes) */ |
2353 | 0 | Stream_Write_UINT16(s, 1); /* regionCount (2 bytes) */ |
2354 | | |
2355 | 0 | return TRUE; |
2356 | 0 | } |
2357 | | |
2358 | | static inline BOOL |
2359 | | rfx_write_progressive_frame_end(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx, |
2360 | | wStream* WINPR_RESTRICT s) |
2361 | 0 | { |
2362 | 0 | const UINT32 blockLen = 6; |
2363 | 0 | WINPR_ASSERT(rfx); |
2364 | 0 | WINPR_ASSERT(s); |
2365 | | |
2366 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2367 | 0 | return FALSE; |
2368 | | |
2369 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_FRAME_END); /* blockType (2 bytes) */ |
2370 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2371 | | |
2372 | 0 | return TRUE; |
2373 | 0 | } |
2374 | | |
2375 | | static inline BOOL |
2376 | | rfx_write_progressive_tile_simple(WINPR_ATTR_UNUSED RFX_CONTEXT* WINPR_RESTRICT rfx, |
2377 | | wStream* WINPR_RESTRICT s, const RFX_TILE* WINPR_RESTRICT tile) |
2378 | 0 | { |
2379 | 0 | UINT32 blockLen = 0; |
2380 | 0 | WINPR_ASSERT(rfx); |
2381 | 0 | WINPR_ASSERT(s); |
2382 | 0 | WINPR_ASSERT(tile); |
2383 | | |
2384 | 0 | blockLen = 22 + tile->YLen + tile->CbLen + tile->CrLen; |
2385 | 0 | if (!Stream_EnsureRemainingCapacity(s, blockLen)) |
2386 | 0 | return FALSE; |
2387 | | |
2388 | 0 | Stream_Write_UINT16(s, PROGRESSIVE_WBT_TILE_SIMPLE); /* blockType (2 bytes) */ |
2389 | 0 | Stream_Write_UINT32(s, blockLen); /* blockLen (4 bytes) */ |
2390 | 0 | Stream_Write_UINT8(s, tile->quantIdxY); /* quantIdxY (1 byte) */ |
2391 | 0 | Stream_Write_UINT8(s, tile->quantIdxCb); /* quantIdxCb (1 byte) */ |
2392 | 0 | Stream_Write_UINT8(s, tile->quantIdxCr); /* quantIdxCr (1 byte) */ |
2393 | 0 | Stream_Write_UINT16(s, tile->xIdx); /* xIdx (2 bytes) */ |
2394 | 0 | Stream_Write_UINT16(s, tile->yIdx); /* yIdx (2 bytes) */ |
2395 | 0 | Stream_Write_UINT8(s, 0); /* flags (1 byte) */ |
2396 | 0 | Stream_Write_UINT16(s, tile->YLen); /* YLen (2 bytes) */ |
2397 | 0 | Stream_Write_UINT16(s, tile->CbLen); /* CbLen (2 bytes) */ |
2398 | 0 | Stream_Write_UINT16(s, tile->CrLen); /* CrLen (2 bytes) */ |
2399 | 0 | Stream_Write_UINT16(s, 0); /* tailLen (2 bytes) */ |
2400 | 0 | Stream_Write(s, tile->YData, tile->YLen); /* YData */ |
2401 | 0 | Stream_Write(s, tile->CbData, tile->CbLen); /* CbData */ |
2402 | 0 | Stream_Write(s, tile->CrData, tile->CrLen); /* CrData */ |
2403 | |
|
2404 | 0 | return TRUE; |
2405 | 0 | } |
2406 | | |
2407 | | const char* rfx_get_progressive_block_type_string(UINT16 blockType) |
2408 | 38 | { |
2409 | 38 | switch (blockType) |
2410 | 38 | { |
2411 | 1 | case PROGRESSIVE_WBT_SYNC: |
2412 | 1 | return "PROGRESSIVE_WBT_SYNC"; |
2413 | | |
2414 | 0 | case PROGRESSIVE_WBT_FRAME_BEGIN: |
2415 | 0 | return "PROGRESSIVE_WBT_FRAME_BEGIN"; |
2416 | | |
2417 | 1 | case PROGRESSIVE_WBT_FRAME_END: |
2418 | 1 | return "PROGRESSIVE_WBT_FRAME_END"; |
2419 | | |
2420 | 0 | case PROGRESSIVE_WBT_CONTEXT: |
2421 | 0 | return "PROGRESSIVE_WBT_CONTEXT"; |
2422 | | |
2423 | 0 | case PROGRESSIVE_WBT_REGION: |
2424 | 0 | return "PROGRESSIVE_WBT_REGION"; |
2425 | | |
2426 | 0 | case PROGRESSIVE_WBT_TILE_SIMPLE: |
2427 | 0 | return "PROGRESSIVE_WBT_TILE_SIMPLE"; |
2428 | | |
2429 | 0 | case PROGRESSIVE_WBT_TILE_FIRST: |
2430 | 0 | return "PROGRESSIVE_WBT_TILE_FIRST"; |
2431 | | |
2432 | 0 | case PROGRESSIVE_WBT_TILE_UPGRADE: |
2433 | 0 | return "PROGRESSIVE_WBT_TILE_UPGRADE"; |
2434 | | |
2435 | 36 | default: |
2436 | 36 | return "PROGRESSIVE_WBT_UNKNOWN"; |
2437 | 38 | } |
2438 | 38 | } |
2439 | | |
2440 | | BOOL rfx_write_message_progressive_simple(RFX_CONTEXT* WINPR_RESTRICT context, |
2441 | | wStream* WINPR_RESTRICT s, |
2442 | | const RFX_MESSAGE* WINPR_RESTRICT msg) |
2443 | 0 | { |
2444 | 0 | WINPR_ASSERT(s); |
2445 | 0 | WINPR_ASSERT(msg); |
2446 | 0 | WINPR_ASSERT(context); |
2447 | | |
2448 | 0 | if (context->mode != RLGR1) |
2449 | 0 | { |
2450 | 0 | WLog_ERR(TAG, "error, RLGR1 mode is required!"); |
2451 | 0 | return FALSE; |
2452 | 0 | } |
2453 | | |
2454 | 0 | if (!rfx_write_progressive_wb_sync(context, s)) |
2455 | 0 | return FALSE; |
2456 | | |
2457 | 0 | if (!rfx_write_progressive_wb_context(context, s)) |
2458 | 0 | return FALSE; |
2459 | | |
2460 | 0 | if (!rfx_write_progressive_frame_begin(context, s, msg)) |
2461 | 0 | return FALSE; |
2462 | | |
2463 | 0 | if (!rfx_write_progressive_region(context, s, msg)) |
2464 | 0 | return FALSE; |
2465 | | |
2466 | 0 | if (!rfx_write_progressive_frame_end(context, s)) |
2467 | 0 | return FALSE; |
2468 | | |
2469 | 0 | return TRUE; |
2470 | 0 | } |