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