/src/FreeRDP/libfreerdp/codec/planar.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * RDP6 Planar Codec |
4 | | * |
5 | | * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2016 Armin Novak <armin.novak@thincast.com> |
7 | | * Copyright 2016 Thincast Technologies GmbH |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <freerdp/config.h> |
23 | | |
24 | | #include <winpr/crt.h> |
25 | | #include <winpr/wtypes.h> |
26 | | #include <winpr/assert.h> |
27 | | #include <winpr/cast.h> |
28 | | #include <winpr/print.h> |
29 | | |
30 | | #include <freerdp/primitives.h> |
31 | | #include <freerdp/log.h> |
32 | | #include <freerdp/codec/bitmap.h> |
33 | | #include <freerdp/codec/planar.h> |
34 | | |
35 | | #define TAG FREERDP_TAG("codec") |
36 | | |
37 | | #define PLANAR_ALIGN(val, align) \ |
38 | 11.0k | ((val) % (align) == 0) ? (val) : ((val) + (align) - (val) % (align)) |
39 | | |
40 | | typedef struct |
41 | | { |
42 | | /** |
43 | | * controlByte: |
44 | | * [0-3]: nRunLength |
45 | | * [4-7]: cRawBytes |
46 | | */ |
47 | | BYTE controlByte; |
48 | | BYTE* rawValues; |
49 | | } RDP6_RLE_SEGMENT; |
50 | | |
51 | | typedef struct |
52 | | { |
53 | | UINT32 cSegments; |
54 | | RDP6_RLE_SEGMENT* segments; |
55 | | } RDP6_RLE_SEGMENTS; |
56 | | |
57 | | typedef struct |
58 | | { |
59 | | /** |
60 | | * formatHeader: |
61 | | * [0-2]: Color Loss Level (CLL) |
62 | | * [3] : Chroma Subsampling (CS) |
63 | | * [4] : Run Length Encoding (RLE) |
64 | | * [5] : No Alpha (NA) |
65 | | * [6-7]: Reserved |
66 | | */ |
67 | | BYTE formatHeader; |
68 | | } RDP6_BITMAP_STREAM; |
69 | | |
70 | | struct S_BITMAP_PLANAR_CONTEXT |
71 | | { |
72 | | UINT32 maxWidth; |
73 | | UINT32 maxHeight; |
74 | | UINT32 maxPlaneSize; |
75 | | |
76 | | BOOL AllowSkipAlpha; |
77 | | BOOL AllowRunLengthEncoding; |
78 | | BOOL AllowColorSubsampling; |
79 | | BOOL AllowDynamicColorFidelity; |
80 | | |
81 | | UINT32 ColorLossLevel; |
82 | | |
83 | | BYTE* planes[4]; |
84 | | BYTE* planesBuffer; |
85 | | |
86 | | BYTE* deltaPlanes[4]; |
87 | | BYTE* deltaPlanesBuffer; |
88 | | |
89 | | BYTE* rlePlanes[4]; |
90 | | BYTE* rlePlanesBuffer; |
91 | | |
92 | | BYTE* pTempData; |
93 | | UINT32 nTempStep; |
94 | | |
95 | | BOOL bgr; |
96 | | BOOL topdown; |
97 | | }; |
98 | | |
99 | | static inline BYTE PLANAR_CONTROL_BYTE(UINT32 nRunLength, UINT32 cRawBytes) |
100 | 0 | { |
101 | 0 | return WINPR_ASSERTING_INT_CAST(UINT8, ((nRunLength & 0x0F) | ((cRawBytes & 0x0F) << 4))); |
102 | 0 | } |
103 | | |
104 | | static inline BYTE PLANAR_CONTROL_BYTE_RUN_LENGTH(UINT32 controlByte) |
105 | 24.7M | { |
106 | 24.7M | return (controlByte & 0x0F); |
107 | 24.7M | } |
108 | | static inline BYTE PLANAR_CONTROL_BYTE_RAW_BYTES(UINT32 controlByte) |
109 | 24.7M | { |
110 | 24.7M | return ((controlByte >> 4) & 0x0F); |
111 | 24.7M | } |
112 | | |
113 | | static inline UINT32 planar_invert_format(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL alpha, |
114 | | UINT32 DstFormat) |
115 | 11.6k | { |
116 | 11.6k | WINPR_ASSERT(planar); |
117 | 11.6k | if (planar->bgr && alpha) |
118 | 0 | { |
119 | 0 | switch (DstFormat) |
120 | 0 | { |
121 | 0 | case PIXEL_FORMAT_ARGB32: |
122 | 0 | DstFormat = PIXEL_FORMAT_ABGR32; |
123 | 0 | break; |
124 | 0 | case PIXEL_FORMAT_XRGB32: |
125 | 0 | DstFormat = PIXEL_FORMAT_XBGR32; |
126 | 0 | break; |
127 | 0 | case PIXEL_FORMAT_ABGR32: |
128 | 0 | DstFormat = PIXEL_FORMAT_ARGB32; |
129 | 0 | break; |
130 | 0 | case PIXEL_FORMAT_XBGR32: |
131 | 0 | DstFormat = PIXEL_FORMAT_XRGB32; |
132 | 0 | break; |
133 | 0 | case PIXEL_FORMAT_BGRA32: |
134 | 0 | DstFormat = PIXEL_FORMAT_RGBA32; |
135 | 0 | break; |
136 | 0 | case PIXEL_FORMAT_BGRX32: |
137 | 0 | DstFormat = PIXEL_FORMAT_RGBX32; |
138 | 0 | break; |
139 | 0 | case PIXEL_FORMAT_RGBA32: |
140 | 0 | DstFormat = PIXEL_FORMAT_BGRA32; |
141 | 0 | break; |
142 | 0 | case PIXEL_FORMAT_RGBX32: |
143 | 0 | DstFormat = PIXEL_FORMAT_BGRX32; |
144 | 0 | break; |
145 | 0 | case PIXEL_FORMAT_RGB24: |
146 | 0 | DstFormat = PIXEL_FORMAT_BGR24; |
147 | 0 | break; |
148 | 0 | case PIXEL_FORMAT_BGR24: |
149 | 0 | DstFormat = PIXEL_FORMAT_RGB24; |
150 | 0 | break; |
151 | 0 | case PIXEL_FORMAT_RGB16: |
152 | 0 | DstFormat = PIXEL_FORMAT_BGR16; |
153 | 0 | break; |
154 | 0 | case PIXEL_FORMAT_BGR16: |
155 | 0 | DstFormat = PIXEL_FORMAT_RGB16; |
156 | 0 | break; |
157 | 0 | case PIXEL_FORMAT_ARGB15: |
158 | 0 | DstFormat = PIXEL_FORMAT_ABGR15; |
159 | 0 | break; |
160 | 0 | case PIXEL_FORMAT_RGB15: |
161 | 0 | DstFormat = PIXEL_FORMAT_BGR15; |
162 | 0 | break; |
163 | 0 | case PIXEL_FORMAT_ABGR15: |
164 | 0 | DstFormat = PIXEL_FORMAT_ARGB15; |
165 | 0 | break; |
166 | 0 | case PIXEL_FORMAT_BGR15: |
167 | 0 | DstFormat = PIXEL_FORMAT_RGB15; |
168 | 0 | break; |
169 | 0 | default: |
170 | 0 | break; |
171 | 0 | } |
172 | 0 | } |
173 | 11.6k | return DstFormat; |
174 | 11.6k | } |
175 | | |
176 | | static inline BOOL freerdp_bitmap_planar_compress_plane_rle(const BYTE* WINPR_RESTRICT inPlane, |
177 | | UINT32 width, UINT32 height, |
178 | | BYTE* WINPR_RESTRICT outPlane, |
179 | | UINT32* WINPR_RESTRICT dstSize); |
180 | | static inline BYTE* freerdp_bitmap_planar_delta_encode_plane(const BYTE* WINPR_RESTRICT inPlane, |
181 | | UINT32 width, UINT32 height, |
182 | | BYTE* WINPR_RESTRICT outPlane); |
183 | | |
184 | | static inline INT32 planar_skip_plane_rle(const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, |
185 | | UINT32 nWidth, UINT32 nHeight) |
186 | 3.19k | { |
187 | 3.19k | UINT32 used = 0; |
188 | | |
189 | 3.19k | WINPR_ASSERT(pSrcData); |
190 | | |
191 | 45.8k | for (UINT32 y = 0; y < nHeight; y++) |
192 | 44.8k | { |
193 | 18.4M | for (UINT32 x = 0; x < nWidth;) |
194 | 18.4M | { |
195 | 18.4M | if (used >= SrcSize) |
196 | 465 | { |
197 | 465 | WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used, |
198 | 465 | SrcSize); |
199 | 465 | return -1; |
200 | 465 | } |
201 | | |
202 | 18.4M | const uint8_t controlByte = pSrcData[used++]; |
203 | 18.4M | uint32_t nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte); |
204 | 18.4M | uint32_t cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte); |
205 | | |
206 | 18.4M | if (nRunLength == 1) |
207 | 22.2k | { |
208 | 22.2k | nRunLength = cRawBytes + 16; |
209 | 22.2k | cRawBytes = 0; |
210 | 22.2k | } |
211 | 18.4M | else if (nRunLength == 2) |
212 | 33.6k | { |
213 | 33.6k | nRunLength = cRawBytes + 32; |
214 | 33.6k | cRawBytes = 0; |
215 | 33.6k | } |
216 | | |
217 | 18.4M | used += cRawBytes; |
218 | 18.4M | x += cRawBytes; |
219 | 18.4M | x += nRunLength; |
220 | | |
221 | 18.4M | if (x > nWidth) |
222 | 907 | { |
223 | 907 | WLog_ERR(TAG, "planar plane x %" PRIu32 " exceeds width %" PRIu32, x, nWidth); |
224 | 907 | return -1; |
225 | 907 | } |
226 | | |
227 | 18.4M | if (used > SrcSize) |
228 | 804 | { |
229 | 804 | WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRId32, used, |
230 | 804 | INT32_MAX); |
231 | 804 | return -1; |
232 | 804 | } |
233 | 18.4M | } |
234 | 44.8k | } |
235 | | |
236 | 1.02k | if (used > INT32_MAX) |
237 | 0 | { |
238 | 0 | WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used, SrcSize); |
239 | 0 | return -1; |
240 | 0 | } |
241 | 1.02k | return (INT32)used; |
242 | 1.02k | } |
243 | | |
244 | | static inline UINT8 clamp(INT16 val) |
245 | 939k | { |
246 | 939k | return (UINT8)val; |
247 | 939k | } |
248 | | |
249 | | static inline INT32 planar_decompress_plane_rle_only(const BYTE* WINPR_RESTRICT pSrcData, |
250 | | UINT32 SrcSize, BYTE* WINPR_RESTRICT pDstData, |
251 | | UINT32 nWidth, UINT32 nHeight) |
252 | 528 | { |
253 | 528 | BYTE* previousScanline = NULL; |
254 | 528 | const BYTE* srcp = pSrcData; |
255 | | |
256 | 528 | WINPR_ASSERT(nHeight <= INT32_MAX); |
257 | 528 | WINPR_ASSERT(nWidth <= INT32_MAX); |
258 | | |
259 | 13.9k | for (UINT32 y = 0; y < nHeight; y++) |
260 | 13.4k | { |
261 | 13.4k | BYTE* dstp = &pDstData[1ULL * (y)*nWidth]; |
262 | 13.4k | INT16 pixel = 0; |
263 | 13.4k | BYTE* currentScanline = dstp; |
264 | | |
265 | 6.16M | for (UINT32 x = 0; x < nWidth;) |
266 | 6.14M | { |
267 | 6.14M | BYTE controlByte = *srcp; |
268 | 6.14M | srcp++; |
269 | | |
270 | 6.14M | if ((srcp - pSrcData) > SrcSize * 1ll) |
271 | 0 | { |
272 | 0 | WLog_ERR(TAG, "error reading input buffer"); |
273 | 0 | return -1; |
274 | 0 | } |
275 | | |
276 | 6.14M | UINT32 nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte); |
277 | 6.14M | UINT32 cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte); |
278 | | |
279 | 6.14M | if (nRunLength == 1) |
280 | 6.01k | { |
281 | 6.01k | nRunLength = cRawBytes + 16; |
282 | 6.01k | cRawBytes = 0; |
283 | 6.01k | } |
284 | 6.14M | else if (nRunLength == 2) |
285 | 7.07k | { |
286 | 7.07k | nRunLength = cRawBytes + 32; |
287 | 7.07k | cRawBytes = 0; |
288 | 7.07k | } |
289 | | |
290 | 6.14M | if (((dstp + (cRawBytes + nRunLength)) - currentScanline) > nWidth * 1ll) |
291 | 0 | { |
292 | 0 | WLog_ERR(TAG, "too many pixels in scanline"); |
293 | 0 | return -1; |
294 | 0 | } |
295 | | |
296 | 6.14M | if (!previousScanline) |
297 | 2.44k | { |
298 | | /* first scanline, absolute values */ |
299 | 4.02k | while (cRawBytes > 0) |
300 | 1.57k | { |
301 | 1.57k | pixel = *srcp; |
302 | 1.57k | srcp++; |
303 | 1.57k | *dstp = clamp(pixel); |
304 | 1.57k | dstp++; |
305 | 1.57k | x++; |
306 | 1.57k | cRawBytes--; |
307 | 1.57k | } |
308 | | |
309 | 14.2k | while (nRunLength > 0) |
310 | 11.8k | { |
311 | 11.8k | *dstp = clamp(pixel); |
312 | 11.8k | dstp++; |
313 | 11.8k | x++; |
314 | 11.8k | nRunLength--; |
315 | 11.8k | } |
316 | 2.44k | } |
317 | 6.14M | else |
318 | 6.14M | { |
319 | | /* delta values relative to previous scanline */ |
320 | 6.18M | while (cRawBytes > 0) |
321 | 40.3k | { |
322 | 40.3k | UINT8 deltaValue = *srcp; |
323 | 40.3k | srcp++; |
324 | | |
325 | 40.3k | if (deltaValue & 1) |
326 | 2.93k | { |
327 | 2.93k | deltaValue = deltaValue >> 1; |
328 | 2.93k | deltaValue = deltaValue + 1; |
329 | 2.93k | pixel = WINPR_ASSERTING_INT_CAST(int16_t, -1 * (int16_t)deltaValue); |
330 | 2.93k | } |
331 | 37.4k | else |
332 | 37.4k | { |
333 | 37.4k | deltaValue = deltaValue >> 1; |
334 | 37.4k | pixel = WINPR_ASSERTING_INT_CAST(INT16, deltaValue); |
335 | 37.4k | } |
336 | | |
337 | 40.3k | const INT16 delta = |
338 | 40.3k | WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[x] + pixel); |
339 | 40.3k | *dstp = clamp(delta); |
340 | 40.3k | dstp++; |
341 | 40.3k | x++; |
342 | 40.3k | cRawBytes--; |
343 | 40.3k | } |
344 | | |
345 | 6.49M | while (nRunLength > 0) |
346 | 344k | { |
347 | 344k | const INT16 deltaValue = |
348 | 344k | WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[x] + pixel); |
349 | 344k | *dstp = clamp(deltaValue); |
350 | 344k | dstp++; |
351 | 344k | x++; |
352 | 344k | nRunLength--; |
353 | 344k | } |
354 | 6.14M | } |
355 | 6.14M | } |
356 | | |
357 | 13.4k | previousScanline = currentScanline; |
358 | 13.4k | } |
359 | | |
360 | 528 | return (INT32)(srcp - pSrcData); |
361 | 528 | } |
362 | | |
363 | | static inline INT32 planar_decompress_plane_rle(const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, |
364 | | BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep, |
365 | | UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, |
366 | | UINT32 nHeight, UINT32 nChannel, BOOL vFlip) |
367 | 390 | { |
368 | 390 | INT32 beg = 0; |
369 | 390 | INT32 end = 0; |
370 | 390 | INT32 inc = 0; |
371 | 390 | BYTE* previousScanline = NULL; |
372 | 390 | const BYTE* srcp = pSrcData; |
373 | | |
374 | 390 | WINPR_ASSERT(nHeight <= INT32_MAX); |
375 | 390 | WINPR_ASSERT(nWidth <= INT32_MAX); |
376 | 390 | WINPR_ASSERT(nDstStep <= INT32_MAX); |
377 | | |
378 | 390 | if (vFlip) |
379 | 0 | { |
380 | 0 | beg = (INT32)nHeight - 1; |
381 | 0 | end = -1; |
382 | 0 | inc = -1; |
383 | 0 | } |
384 | 390 | else |
385 | 390 | { |
386 | 390 | beg = 0; |
387 | 390 | end = (INT32)nHeight; |
388 | 390 | inc = 1; |
389 | 390 | } |
390 | | |
391 | 14.5k | for (INT32 y = beg; y != end; y += inc) |
392 | 14.1k | { |
393 | 14.1k | const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (4LL * nXDst) + nChannel * 1LL; |
394 | 14.1k | BYTE* dstp = &pDstData[off]; |
395 | 14.1k | BYTE* currentScanline = dstp; |
396 | 14.1k | INT16 pixel = 0; |
397 | | |
398 | 130k | for (INT32 x = 0; x < (INT32)nWidth;) |
399 | 116k | { |
400 | 116k | const BYTE controlByte = *srcp; |
401 | 116k | srcp++; |
402 | | |
403 | 116k | if ((srcp - pSrcData) > SrcSize * 1ll) |
404 | 0 | { |
405 | 0 | WLog_ERR(TAG, "error reading input buffer"); |
406 | 0 | return -1; |
407 | 0 | } |
408 | | |
409 | 116k | UINT32 nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte); |
410 | 116k | UINT32 cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte); |
411 | | |
412 | 116k | if (nRunLength == 1) |
413 | 6.25k | { |
414 | 6.25k | nRunLength = cRawBytes + 16; |
415 | 6.25k | cRawBytes = 0; |
416 | 6.25k | } |
417 | 110k | else if (nRunLength == 2) |
418 | 9.64k | { |
419 | 9.64k | nRunLength = cRawBytes + 32; |
420 | 9.64k | cRawBytes = 0; |
421 | 9.64k | } |
422 | | |
423 | 116k | if (((dstp + (cRawBytes + nRunLength)) - currentScanline) > nWidth * 4ll) |
424 | 0 | { |
425 | 0 | WLog_ERR(TAG, "too many pixels in scanline"); |
426 | 0 | return -1; |
427 | 0 | } |
428 | | |
429 | 116k | if (!previousScanline) |
430 | 3.06k | { |
431 | | /* first scanline, absolute values */ |
432 | 5.10k | while (cRawBytes > 0) |
433 | 2.04k | { |
434 | 2.04k | pixel = *srcp; |
435 | 2.04k | srcp++; |
436 | 2.04k | *dstp = WINPR_ASSERTING_INT_CAST(BYTE, pixel); |
437 | 2.04k | dstp += 4; |
438 | 2.04k | x++; |
439 | 2.04k | cRawBytes--; |
440 | 2.04k | } |
441 | | |
442 | 15.1k | while (nRunLength > 0) |
443 | 12.0k | { |
444 | 12.0k | *dstp = WINPR_ASSERTING_INT_CAST(BYTE, pixel); |
445 | 12.0k | dstp += 4; |
446 | 12.0k | x++; |
447 | 12.0k | nRunLength--; |
448 | 12.0k | } |
449 | 3.06k | } |
450 | 113k | else |
451 | 113k | { |
452 | | /* delta values relative to previous scanline */ |
453 | 216k | while (cRawBytes > 0) |
454 | 102k | { |
455 | 102k | BYTE deltaValue = *srcp; |
456 | 102k | srcp++; |
457 | | |
458 | 102k | if (deltaValue & 1) |
459 | 1.13k | { |
460 | 1.13k | deltaValue = deltaValue >> 1; |
461 | 1.13k | deltaValue = deltaValue + 1; |
462 | 1.13k | pixel = WINPR_ASSERTING_INT_CAST(int16_t, -deltaValue); |
463 | 1.13k | } |
464 | 101k | else |
465 | 101k | { |
466 | 101k | deltaValue = deltaValue >> 1; |
467 | 101k | pixel = deltaValue; |
468 | 101k | } |
469 | | |
470 | 102k | const INT16 delta = |
471 | 102k | WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[4LL * x] + pixel); |
472 | 102k | *dstp = clamp(delta); |
473 | 102k | dstp += 4; |
474 | 102k | x++; |
475 | 102k | cRawBytes--; |
476 | 102k | } |
477 | | |
478 | 552k | while (nRunLength > 0) |
479 | 439k | { |
480 | 439k | const INT16 deltaValue = |
481 | 439k | WINPR_ASSERTING_INT_CAST(int16_t, pixel + previousScanline[4LL * x]); |
482 | 439k | *dstp = clamp(deltaValue); |
483 | 439k | dstp += 4; |
484 | 439k | x++; |
485 | 439k | nRunLength--; |
486 | 439k | } |
487 | 113k | } |
488 | 116k | } |
489 | | |
490 | 14.1k | previousScanline = currentScanline; |
491 | 14.1k | } |
492 | | |
493 | 390 | return (INT32)(srcp - pSrcData); |
494 | 390 | } |
495 | | |
496 | | static inline INT32 planar_set_plane(BYTE bValue, BYTE* pDstData, UINT32 nDstStep, UINT32 nXDst, |
497 | | UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 nChannel, |
498 | | BOOL vFlip) |
499 | 130 | { |
500 | 130 | INT32 beg = 0; |
501 | 130 | INT32 end = (INT32)nHeight; |
502 | 130 | INT32 inc = 1; |
503 | | |
504 | 130 | WINPR_ASSERT(nHeight <= INT32_MAX); |
505 | 130 | WINPR_ASSERT(nWidth <= INT32_MAX); |
506 | 130 | WINPR_ASSERT(nDstStep <= INT32_MAX); |
507 | | |
508 | 130 | if (vFlip) |
509 | 0 | { |
510 | 0 | beg = (INT32)nHeight - 1; |
511 | 0 | end = -1; |
512 | 0 | inc = -1; |
513 | 0 | } |
514 | | |
515 | 4.83k | for (INT32 y = beg; y != end; y += inc) |
516 | 4.70k | { |
517 | 4.70k | const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (4LL * nXDst) + nChannel * 1LL; |
518 | 4.70k | BYTE* dstp = &pDstData[off]; |
519 | | |
520 | 190k | for (INT32 x = 0; x < (INT32)nWidth; ++x) |
521 | 185k | { |
522 | 185k | *dstp = bValue; |
523 | 185k | dstp += 4; |
524 | 185k | } |
525 | 4.70k | } |
526 | | |
527 | 130 | return 0; |
528 | 130 | } |
529 | | |
530 | | static inline BOOL writeLine(BYTE** WINPR_RESTRICT ppRgba, UINT32 DstFormat, UINT32 width, |
531 | | const BYTE** WINPR_RESTRICT ppR, const BYTE** WINPR_RESTRICT ppG, |
532 | | const BYTE** WINPR_RESTRICT ppB, const BYTE** WINPR_RESTRICT ppA) |
533 | 45.8k | { |
534 | 45.8k | WINPR_ASSERT(ppRgba); |
535 | 45.8k | WINPR_ASSERT(ppR); |
536 | 45.8k | WINPR_ASSERT(ppG); |
537 | 45.8k | WINPR_ASSERT(ppB); |
538 | | |
539 | 45.8k | switch (DstFormat) |
540 | 45.8k | { |
541 | 0 | case PIXEL_FORMAT_BGRA32: |
542 | 0 | for (UINT32 x = 0; x < width; x++) |
543 | 0 | { |
544 | 0 | *(*ppRgba)++ = *(*ppB)++; |
545 | 0 | *(*ppRgba)++ = *(*ppG)++; |
546 | 0 | *(*ppRgba)++ = *(*ppR)++; |
547 | 0 | *(*ppRgba)++ = *(*ppA)++; |
548 | 0 | } |
549 | |
|
550 | 0 | return TRUE; |
551 | | |
552 | 45.8k | case PIXEL_FORMAT_BGRX32: |
553 | 2.28M | for (UINT32 x = 0; x < width; x++) |
554 | 2.23M | { |
555 | 2.23M | *(*ppRgba)++ = *(*ppB)++; |
556 | 2.23M | *(*ppRgba)++ = *(*ppG)++; |
557 | 2.23M | *(*ppRgba)++ = *(*ppR)++; |
558 | 2.23M | *(*ppRgba)++ = 0xFF; |
559 | 2.23M | } |
560 | | |
561 | 45.8k | return TRUE; |
562 | | |
563 | 0 | default: |
564 | 0 | if (ppA) |
565 | 0 | { |
566 | 0 | for (UINT32 x = 0; x < width; x++) |
567 | 0 | { |
568 | 0 | BYTE alpha = *(*ppA)++; |
569 | 0 | UINT32 color = |
570 | 0 | FreeRDPGetColor(DstFormat, *(*ppR)++, *(*ppG)++, *(*ppB)++, alpha); |
571 | 0 | FreeRDPWriteColor(*ppRgba, DstFormat, color); |
572 | 0 | *ppRgba += FreeRDPGetBytesPerPixel(DstFormat); |
573 | 0 | } |
574 | 0 | } |
575 | 0 | else |
576 | 0 | { |
577 | 0 | const BYTE alpha = 0xFF; |
578 | |
|
579 | 0 | for (UINT32 x = 0; x < width; x++) |
580 | 0 | { |
581 | 0 | UINT32 color = |
582 | 0 | FreeRDPGetColor(DstFormat, *(*ppR)++, *(*ppG)++, *(*ppB)++, alpha); |
583 | 0 | FreeRDPWriteColor(*ppRgba, DstFormat, color); |
584 | 0 | *ppRgba += FreeRDPGetBytesPerPixel(DstFormat); |
585 | 0 | } |
586 | 0 | } |
587 | |
|
588 | 0 | return TRUE; |
589 | 45.8k | } |
590 | 45.8k | } |
591 | | |
592 | | static inline BOOL planar_decompress_planes_raw(const BYTE* WINPR_RESTRICT pSrcData[4], |
593 | | BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, |
594 | | UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, |
595 | | UINT32 nWidth, UINT32 nHeight, BOOL vFlip, |
596 | | UINT32 totalHeight) |
597 | 1.05k | { |
598 | 1.05k | INT32 beg = 0; |
599 | 1.05k | INT32 end = 0; |
600 | 1.05k | INT32 inc = 0; |
601 | 1.05k | const BYTE* pR = pSrcData[0]; |
602 | 1.05k | const BYTE* pG = pSrcData[1]; |
603 | 1.05k | const BYTE* pB = pSrcData[2]; |
604 | 1.05k | const BYTE* pA = pSrcData[3]; |
605 | 1.05k | const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat); |
606 | | |
607 | 1.05k | if (vFlip) |
608 | 0 | { |
609 | 0 | beg = WINPR_ASSERTING_INT_CAST(int32_t, nHeight - 1); |
610 | 0 | end = -1; |
611 | 0 | inc = -1; |
612 | 0 | } |
613 | 1.05k | else |
614 | 1.05k | { |
615 | 1.05k | beg = 0; |
616 | 1.05k | end = WINPR_ASSERTING_INT_CAST(int32_t, nHeight); |
617 | 1.05k | inc = 1; |
618 | 1.05k | } |
619 | | |
620 | 1.05k | if (nYDst + nHeight > totalHeight) |
621 | 0 | { |
622 | 0 | WLog_ERR(TAG, |
623 | 0 | "planar plane destination Y %" PRIu32 " + height %" PRIu32 |
624 | 0 | " exceeds totalHeight %" PRIu32, |
625 | 0 | nYDst, nHeight, totalHeight); |
626 | 0 | return FALSE; |
627 | 0 | } |
628 | | |
629 | 1.05k | if ((nXDst + nWidth) * bpp > nDstStep) |
630 | 0 | { |
631 | 0 | WLog_ERR(TAG, |
632 | 0 | "planar plane destination (X %" PRIu32 " + width %" PRIu32 ") * bpp %" PRIu32 |
633 | 0 | " exceeds stride %" PRIu32, |
634 | 0 | nXDst, nWidth, bpp, nDstStep); |
635 | 0 | return FALSE; |
636 | 0 | } |
637 | | |
638 | 46.9k | for (INT32 y = beg; y != end; y += inc) |
639 | 45.8k | { |
640 | 45.8k | BYTE* pRGB = NULL; |
641 | | |
642 | 45.8k | if (y > WINPR_ASSERTING_INT_CAST(INT64, nHeight)) |
643 | 0 | { |
644 | 0 | WLog_ERR(TAG, "planar plane destination Y %" PRId32 " exceeds height %" PRIu32, y, |
645 | 0 | nHeight); |
646 | 0 | return FALSE; |
647 | 0 | } |
648 | | |
649 | 45.8k | const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (1LL * nXDst * bpp); |
650 | 45.8k | pRGB = &pDstData[off]; |
651 | | |
652 | 45.8k | if (!writeLine(&pRGB, DstFormat, nWidth, &pR, &pG, &pB, &pA)) |
653 | 0 | return FALSE; |
654 | 45.8k | } |
655 | | |
656 | 1.05k | return TRUE; |
657 | 1.05k | } |
658 | | |
659 | | static BOOL planar_subsample_expand(const BYTE* WINPR_RESTRICT plane, size_t planeLength, |
660 | | UINT32 nWidth, UINT32 nHeight, UINT32 nPlaneWidth, |
661 | | UINT32 nPlaneHeight, BYTE* WINPR_RESTRICT deltaPlane) |
662 | 380 | { |
663 | 380 | size_t pos = 0; |
664 | 380 | WINPR_UNUSED(planeLength); |
665 | | |
666 | 380 | WINPR_ASSERT(plane); |
667 | 380 | WINPR_ASSERT(deltaPlane); |
668 | | |
669 | 380 | if (nWidth > nPlaneWidth * 2) |
670 | 0 | { |
671 | 0 | WLog_ERR(TAG, "planar subsample width %" PRIu32 " > PlaneWidth %" PRIu32 " * 2", nWidth, |
672 | 0 | nPlaneWidth); |
673 | 0 | return FALSE; |
674 | 0 | } |
675 | | |
676 | 380 | if (nHeight > nPlaneHeight * 2) |
677 | 0 | { |
678 | 0 | WLog_ERR(TAG, "planar subsample height %" PRIu32 " > PlaneHeight %" PRIu32 " * 2", nHeight, |
679 | 0 | nPlaneHeight); |
680 | 0 | return FALSE; |
681 | 0 | } |
682 | | |
683 | 14.1k | for (size_t y = 0; y < nHeight; y++) |
684 | 13.7k | { |
685 | 13.7k | const BYTE* src = plane + y / 2 * nPlaneWidth; |
686 | | |
687 | 556k | for (UINT32 x = 0; x < nWidth; x++) |
688 | 542k | { |
689 | 542k | deltaPlane[pos++] = src[x / 2]; |
690 | 542k | } |
691 | 13.7k | } |
692 | | |
693 | 380 | return TRUE; |
694 | 380 | } |
695 | | |
696 | | #if !defined(WITHOUT_FREERDP_3x_DEPRECATED) |
697 | | BOOL planar_decompress(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, |
698 | | const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, UINT32 nSrcWidth, |
699 | | UINT32 nSrcHeight, BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, |
700 | | UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nDstWidth, |
701 | | UINT32 nDstHeight, BOOL vFlip) |
702 | 11.0k | { |
703 | 11.0k | return freerdp_bitmap_decompress_planar(planar, pSrcData, SrcSize, nSrcWidth, nSrcHeight, |
704 | 11.0k | pDstData, DstFormat, nDstStep, nXDst, nYDst, nDstWidth, |
705 | 11.0k | nDstHeight, vFlip); |
706 | 11.0k | } |
707 | | #endif |
708 | | |
709 | | BOOL freerdp_bitmap_decompress_planar(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, |
710 | | const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, |
711 | | UINT32 nSrcWidth, UINT32 nSrcHeight, |
712 | | BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, |
713 | | UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nDstWidth, |
714 | | UINT32 nDstHeight, BOOL vFlip) |
715 | 11.0k | { |
716 | 11.0k | BOOL useAlpha = FALSE; |
717 | 11.0k | INT32 status = 0; |
718 | 11.0k | INT32 rleSizes[4] = { 0, 0, 0, 0 }; |
719 | 11.0k | UINT32 rawSizes[4] = WINPR_C_ARRAY_INIT; |
720 | 11.0k | UINT32 rawWidths[4] = WINPR_C_ARRAY_INIT; |
721 | 11.0k | UINT32 rawHeights[4] = WINPR_C_ARRAY_INIT; |
722 | 11.0k | const BYTE* planes[4] = WINPR_C_ARRAY_INIT; |
723 | 11.0k | const UINT32 w = MIN(nSrcWidth, nDstWidth); |
724 | 11.0k | const UINT32 h = MIN(nSrcHeight, nDstHeight); |
725 | 11.0k | const primitives_t* prims = primitives_get(); |
726 | | |
727 | 11.0k | WINPR_ASSERT(planar); |
728 | 11.0k | WINPR_ASSERT(prims); |
729 | | |
730 | 11.0k | if (planar->maxWidth < nSrcWidth) |
731 | 0 | return FALSE; |
732 | 11.0k | if (planar->maxHeight < nSrcHeight) |
733 | 0 | return FALSE; |
734 | | |
735 | 11.0k | const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat); |
736 | 11.0k | if (nDstStep <= 0) |
737 | 11.0k | nDstStep = nDstWidth * bpp; |
738 | | |
739 | 11.0k | const BYTE* srcp = pSrcData; |
740 | | |
741 | 11.0k | if (!pSrcData) |
742 | 0 | { |
743 | 0 | WLog_ERR(TAG, "Invalid argument pSrcData=NULL"); |
744 | 0 | return FALSE; |
745 | 0 | } |
746 | | |
747 | 11.0k | if (!pDstData) |
748 | 0 | { |
749 | 0 | WLog_ERR(TAG, "Invalid argument pDstData=NULL"); |
750 | 0 | return FALSE; |
751 | 0 | } |
752 | | |
753 | 11.0k | const BYTE FormatHeader = *srcp++; |
754 | 11.0k | const BYTE cll = (FormatHeader & PLANAR_FORMAT_HEADER_CLL_MASK); |
755 | 11.0k | const BYTE cs = (FormatHeader & PLANAR_FORMAT_HEADER_CS) ? TRUE : FALSE; |
756 | 11.0k | const BYTE rle = (FormatHeader & PLANAR_FORMAT_HEADER_RLE) ? TRUE : FALSE; |
757 | 11.0k | const BYTE alpha = (FormatHeader & PLANAR_FORMAT_HEADER_NA) ? FALSE : TRUE; |
758 | | |
759 | 11.0k | DstFormat = planar_invert_format(planar, alpha, DstFormat); |
760 | | |
761 | 11.0k | if (alpha) |
762 | 5.39k | useAlpha = FreeRDPColorHasAlpha(DstFormat); |
763 | | |
764 | | // WLog_INFO(TAG, "CLL: %"PRIu32" CS: %"PRIu8" RLE: %"PRIu8" ALPHA: %"PRIu8"", cll, cs, rle, |
765 | | // alpha); |
766 | | |
767 | 11.0k | if (!cll && cs) |
768 | 234 | { |
769 | 234 | WLog_ERR(TAG, "Chroma subsampling requires YCoCg and does not work with RGB data"); |
770 | 234 | return FALSE; /* Chroma subsampling requires YCoCg */ |
771 | 234 | } |
772 | | |
773 | 10.7k | const UINT32 subWidth = (nSrcWidth / 2) + (nSrcWidth % 2); |
774 | 10.7k | const UINT32 subHeight = (nSrcHeight / 2) + (nSrcHeight % 2); |
775 | 10.7k | const UINT32 planeSize = nSrcWidth * nSrcHeight; |
776 | 10.7k | const UINT32 subSize = subWidth * subHeight; |
777 | | |
778 | 10.7k | if (!cs) |
779 | 9.29k | { |
780 | 9.29k | rawSizes[0] = planeSize; /* LumaOrRedPlane */ |
781 | 9.29k | rawWidths[0] = nSrcWidth; |
782 | 9.29k | rawHeights[0] = nSrcHeight; |
783 | 9.29k | rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */ |
784 | 9.29k | rawWidths[1] = nSrcWidth; |
785 | 9.29k | rawHeights[1] = nSrcHeight; |
786 | 9.29k | rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */ |
787 | 9.29k | rawWidths[2] = nSrcWidth; |
788 | 9.29k | rawHeights[2] = nSrcHeight; |
789 | 9.29k | rawSizes[3] = planeSize; /* AlphaPlane */ |
790 | 9.29k | rawWidths[3] = nSrcWidth; |
791 | 9.29k | rawHeights[3] = nSrcHeight; |
792 | 9.29k | } |
793 | 1.49k | else /* Chroma Subsampling */ |
794 | 1.49k | { |
795 | 1.49k | rawSizes[0] = planeSize; /* LumaOrRedPlane */ |
796 | 1.49k | rawWidths[0] = nSrcWidth; |
797 | 1.49k | rawHeights[0] = nSrcHeight; |
798 | 1.49k | rawSizes[1] = subSize; /* OrangeChromaOrGreenPlane */ |
799 | 1.49k | rawWidths[1] = subWidth; |
800 | 1.49k | rawHeights[1] = subHeight; |
801 | 1.49k | rawSizes[2] = subSize; /* GreenChromaOrBluePlane */ |
802 | 1.49k | rawWidths[2] = subWidth; |
803 | 1.49k | rawHeights[2] = subHeight; |
804 | 1.49k | rawSizes[3] = planeSize; /* AlphaPlane */ |
805 | 1.49k | rawWidths[3] = nSrcWidth; |
806 | 1.49k | rawHeights[3] = nSrcHeight; |
807 | 1.49k | } |
808 | | |
809 | 10.7k | const size_t diff = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(srcp - pSrcData)); |
810 | 10.7k | if (SrcSize < diff) |
811 | 0 | { |
812 | 0 | WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff); |
813 | 0 | return FALSE; |
814 | 0 | } |
815 | | |
816 | 10.7k | if (!rle) /* RAW */ |
817 | 8.30k | { |
818 | | |
819 | 8.30k | UINT32 base = planeSize * 3; |
820 | 8.30k | if (cs) |
821 | 606 | base = planeSize + planeSize / 2; |
822 | | |
823 | 8.30k | if (alpha) |
824 | 4.88k | { |
825 | 4.88k | if ((SrcSize - diff) < (planeSize + base)) |
826 | 4.42k | { |
827 | 4.42k | WLog_ERR(TAG, "Alpha plane size mismatch %" PRIuz " < %" PRIu32, SrcSize - diff, |
828 | 4.42k | (planeSize + base)); |
829 | 4.42k | return FALSE; |
830 | 4.42k | } |
831 | | |
832 | 455 | planes[3] = srcp; /* AlphaPlane */ |
833 | 455 | planes[0] = planes[3] + rawSizes[3]; /* LumaOrRedPlane */ |
834 | 455 | planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */ |
835 | 455 | planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */ |
836 | | |
837 | 455 | if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize]) |
838 | 0 | { |
839 | 0 | WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p", |
840 | 0 | WINPR_CXX_COMPAT_CAST(const void*, planes[2]), rawSizes[2], |
841 | 0 | WINPR_CXX_COMPAT_CAST(const void*, &pSrcData[SrcSize])); |
842 | 0 | return FALSE; |
843 | 0 | } |
844 | 455 | } |
845 | 3.42k | else |
846 | 3.42k | { |
847 | 3.42k | if ((SrcSize - diff) < base) |
848 | 2.99k | { |
849 | 2.99k | WLog_ERR(TAG, "plane size mismatch %" PRIuz " < %" PRIu32, SrcSize - diff, base); |
850 | 2.99k | return FALSE; |
851 | 2.99k | } |
852 | | |
853 | 428 | planes[0] = srcp; /* LumaOrRedPlane */ |
854 | 428 | planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */ |
855 | 428 | planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */ |
856 | | |
857 | 428 | if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize]) |
858 | 0 | { |
859 | 0 | WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p", |
860 | 0 | WINPR_CXX_COMPAT_CAST(const void*, planes[2]), rawSizes[2], |
861 | 0 | WINPR_CXX_COMPAT_CAST(const void*, &pSrcData[SrcSize])); |
862 | 0 | return FALSE; |
863 | 0 | } |
864 | 428 | } |
865 | 8.30k | } |
866 | 2.48k | else /* RLE */ |
867 | 2.48k | { |
868 | 2.48k | if (alpha) |
869 | 430 | { |
870 | 430 | planes[3] = srcp; |
871 | 430 | rleSizes[3] = planar_skip_plane_rle(planes[3], (UINT32)(SrcSize - diff), rawWidths[3], |
872 | 430 | rawHeights[3]); /* AlphaPlane */ |
873 | | |
874 | 430 | if (rleSizes[3] < 0) |
875 | 412 | return FALSE; |
876 | | |
877 | 18 | planes[0] = planes[3] + rleSizes[3]; |
878 | 18 | } |
879 | 2.05k | else |
880 | 2.05k | planes[0] = srcp; |
881 | | |
882 | 2.07k | const size_t diff0 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[0] - pSrcData)); |
883 | 2.07k | if (SrcSize < diff0) |
884 | 0 | { |
885 | 0 | WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff0); |
886 | 0 | return FALSE; |
887 | 0 | } |
888 | 2.07k | rleSizes[0] = planar_skip_plane_rle(planes[0], (UINT32)(SrcSize - diff0), rawWidths[0], |
889 | 2.07k | rawHeights[0]); /* RedPlane */ |
890 | | |
891 | 2.07k | if (rleSizes[0] < 0) |
892 | 1.70k | return FALSE; |
893 | | |
894 | 366 | planes[1] = planes[0] + rleSizes[0]; |
895 | | |
896 | 366 | const size_t diff1 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[1] - pSrcData)); |
897 | 366 | if (SrcSize < diff1) |
898 | 0 | { |
899 | 0 | WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff1); |
900 | 0 | return FALSE; |
901 | 0 | } |
902 | 366 | rleSizes[1] = planar_skip_plane_rle(planes[1], (UINT32)(SrcSize - diff1), rawWidths[1], |
903 | 366 | rawHeights[1]); /* GreenPlane */ |
904 | | |
905 | 366 | if (rleSizes[1] < 1) |
906 | 34 | return FALSE; |
907 | | |
908 | 332 | planes[2] = planes[1] + rleSizes[1]; |
909 | 332 | const size_t diff2 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[2] - pSrcData)); |
910 | 332 | if (SrcSize < diff2) |
911 | 0 | { |
912 | 0 | WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff); |
913 | 0 | return FALSE; |
914 | 0 | } |
915 | 332 | rleSizes[2] = planar_skip_plane_rle(planes[2], (UINT32)(SrcSize - diff2), rawWidths[2], |
916 | 332 | rawHeights[2]); /* BluePlane */ |
917 | | |
918 | 332 | if (rleSizes[2] < 1) |
919 | 26 | return FALSE; |
920 | 332 | } |
921 | | |
922 | 1.18k | if (!cll) /* RGB */ |
923 | 643 | { |
924 | 643 | UINT32 TempFormat = 0; |
925 | 643 | BYTE* pTempData = pDstData; |
926 | 643 | UINT32 nTempStep = nDstStep; |
927 | 643 | UINT32 nTotalHeight = nYDst + nDstHeight; |
928 | | |
929 | 643 | if (useAlpha) |
930 | 0 | TempFormat = PIXEL_FORMAT_BGRA32; |
931 | 643 | else |
932 | 643 | TempFormat = PIXEL_FORMAT_BGRX32; |
933 | | |
934 | 643 | TempFormat = planar_invert_format(planar, alpha, TempFormat); |
935 | | |
936 | 643 | if ((TempFormat != DstFormat) || (nSrcWidth != nDstWidth) || (nSrcHeight != nDstHeight)) |
937 | 643 | { |
938 | 643 | pTempData = planar->pTempData; |
939 | 643 | nTempStep = planar->nTempStep; |
940 | 643 | nTotalHeight = planar->maxHeight; |
941 | 643 | } |
942 | | |
943 | 643 | if (!rle) /* RAW */ |
944 | 513 | { |
945 | 513 | if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst, |
946 | 513 | nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight)) |
947 | 0 | return FALSE; |
948 | | |
949 | 513 | if (alpha) |
950 | 241 | srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3]; |
951 | 272 | else /* NoAlpha */ |
952 | 272 | srcp += rawSizes[0] + rawSizes[1] + rawSizes[2]; |
953 | | |
954 | 513 | if ((SrcSize - (srcp - pSrcData)) == 1) |
955 | 1 | srcp++; /* pad */ |
956 | 513 | } |
957 | 130 | else /* RLE */ |
958 | 130 | { |
959 | 130 | if (nYDst + nSrcHeight > nTotalHeight) |
960 | 0 | { |
961 | 0 | WLog_ERR(TAG, |
962 | 0 | "planar plane destination Y %" PRIu32 " + height %" PRIu32 |
963 | 0 | " exceeds totalHeight %" PRIu32, |
964 | 0 | nYDst, nSrcHeight, nTotalHeight); |
965 | 0 | return FALSE; |
966 | 0 | } |
967 | | |
968 | 130 | if ((nXDst + nSrcWidth) * bpp > nDstStep) |
969 | 0 | { |
970 | 0 | WLog_ERR(TAG, |
971 | 0 | "planar plane destination (X %" PRIu32 " + width %" PRIu32 |
972 | 0 | ") * bpp %" PRIu32 " exceeds stride %" PRIu32, |
973 | 0 | nXDst, nSrcWidth, bpp, nDstStep); |
974 | 0 | return FALSE; |
975 | 0 | } |
976 | | |
977 | 130 | status = planar_decompress_plane_rle( |
978 | 130 | planes[0], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[0]), pTempData, nTempStep, |
979 | 130 | nXDst, nYDst, nSrcWidth, nSrcHeight, 2, vFlip); /* RedPlane */ |
980 | | |
981 | 130 | if (status < 0) |
982 | 0 | return FALSE; |
983 | | |
984 | 130 | status = planar_decompress_plane_rle( |
985 | 130 | planes[1], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[1]), pTempData, nTempStep, |
986 | 130 | nXDst, nYDst, nSrcWidth, nSrcHeight, 1, vFlip); /* GreenPlane */ |
987 | | |
988 | 130 | if (status < 0) |
989 | 0 | return FALSE; |
990 | | |
991 | 130 | status = planar_decompress_plane_rle( |
992 | 130 | planes[2], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[2]), pTempData, nTempStep, |
993 | 130 | nXDst, nYDst, nSrcWidth, nSrcHeight, 0, vFlip); /* BluePlane */ |
994 | | |
995 | 130 | if (status < 0) |
996 | 0 | return FALSE; |
997 | | |
998 | 130 | srcp += rleSizes[0] + rleSizes[1] + rleSizes[2]; |
999 | | |
1000 | 130 | if (useAlpha) |
1001 | 0 | { |
1002 | 0 | status = planar_decompress_plane_rle( |
1003 | 0 | planes[3], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[3]), pTempData, |
1004 | 0 | nTempStep, nXDst, nYDst, nSrcWidth, nSrcHeight, 3, vFlip); /* AlphaPlane */ |
1005 | 0 | } |
1006 | 130 | else |
1007 | 130 | status = planar_set_plane(0xFF, pTempData, nTempStep, nXDst, nYDst, nSrcWidth, |
1008 | 130 | nSrcHeight, 3, vFlip); |
1009 | | |
1010 | 130 | if (status < 0) |
1011 | 0 | return FALSE; |
1012 | | |
1013 | 130 | if (alpha) |
1014 | 2 | srcp += rleSizes[3]; |
1015 | 130 | } |
1016 | | |
1017 | 643 | if (pTempData != pDstData) |
1018 | 643 | { |
1019 | 643 | if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, w, h, |
1020 | 643 | pTempData, TempFormat, nTempStep, nXDst, nYDst, NULL, |
1021 | 643 | FREERDP_FLIP_NONE)) |
1022 | 643 | { |
1023 | 643 | WLog_ERR(TAG, "planar image copy failed"); |
1024 | 643 | return FALSE; |
1025 | 643 | } |
1026 | 643 | } |
1027 | 643 | } |
1028 | 546 | else /* YCoCg */ |
1029 | 546 | { |
1030 | 546 | UINT32 TempFormat = 0; |
1031 | 546 | BYTE* pTempData = planar->pTempData; |
1032 | 546 | UINT32 nTempStep = planar->nTempStep; |
1033 | 546 | UINT32 nTotalHeight = planar->maxHeight; |
1034 | 546 | BYTE* dst = &pDstData[nXDst * FreeRDPGetBytesPerPixel(DstFormat) + nYDst * nDstStep]; |
1035 | | |
1036 | 546 | if (useAlpha) |
1037 | 0 | TempFormat = PIXEL_FORMAT_BGRA32; |
1038 | 546 | else |
1039 | 546 | TempFormat = PIXEL_FORMAT_BGRX32; |
1040 | | |
1041 | 546 | if (!pTempData) |
1042 | 0 | return FALSE; |
1043 | | |
1044 | 546 | if (rle) /* RLE encoded data. Decode and handle it like raw data. */ |
1045 | 176 | { |
1046 | 176 | BYTE* rleBuffer[4] = WINPR_C_ARRAY_INIT; |
1047 | | |
1048 | 176 | if (!planar->rlePlanesBuffer) |
1049 | 0 | return FALSE; |
1050 | | |
1051 | 176 | rleBuffer[3] = planar->rlePlanesBuffer; /* AlphaPlane */ |
1052 | 176 | rleBuffer[0] = rleBuffer[3] + planeSize; /* LumaOrRedPlane */ |
1053 | 176 | rleBuffer[1] = rleBuffer[0] + planeSize; /* OrangeChromaOrGreenPlane */ |
1054 | 176 | rleBuffer[2] = rleBuffer[1] + planeSize; /* GreenChromaOrBluePlane */ |
1055 | 176 | if (useAlpha) |
1056 | 0 | { |
1057 | 0 | status = planar_decompress_plane_rle_only( |
1058 | 0 | planes[3], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[3]), rleBuffer[3], |
1059 | 0 | rawWidths[3], rawHeights[3]); /* AlphaPlane */ |
1060 | |
|
1061 | 0 | if (status < 0) |
1062 | 0 | return FALSE; |
1063 | 0 | } |
1064 | | |
1065 | 176 | if (alpha) |
1066 | 4 | srcp += rleSizes[3]; |
1067 | | |
1068 | 176 | status = planar_decompress_plane_rle_only( |
1069 | 176 | planes[0], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[0]), rleBuffer[0], |
1070 | 176 | rawWidths[0], rawHeights[0]); /* LumaPlane */ |
1071 | | |
1072 | 176 | if (status < 0) |
1073 | 0 | return FALSE; |
1074 | | |
1075 | 176 | status = planar_decompress_plane_rle_only( |
1076 | 176 | planes[1], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[1]), rleBuffer[1], |
1077 | 176 | rawWidths[1], rawHeights[1]); /* OrangeChromaPlane */ |
1078 | | |
1079 | 176 | if (status < 0) |
1080 | 0 | return FALSE; |
1081 | | |
1082 | 176 | status = planar_decompress_plane_rle_only( |
1083 | 176 | planes[2], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[2]), rleBuffer[2], |
1084 | 176 | rawWidths[2], rawHeights[2]); /* GreenChromaPlane */ |
1085 | | |
1086 | 176 | if (status < 0) |
1087 | 0 | return FALSE; |
1088 | | |
1089 | 176 | planes[0] = rleBuffer[0]; |
1090 | 176 | planes[1] = rleBuffer[1]; |
1091 | 176 | planes[2] = rleBuffer[2]; |
1092 | 176 | planes[3] = rleBuffer[3]; |
1093 | 176 | } |
1094 | | |
1095 | | /* RAW */ |
1096 | 546 | { |
1097 | 546 | if (cs) |
1098 | 190 | { /* Chroma subsampling for Co and Cg: |
1099 | | * Each pixel contains the value that should be expanded to |
1100 | | * [2x,2y;2x+1,2y;2x+1,2y+1;2x;2y+1] */ |
1101 | 190 | if (!planar_subsample_expand(planes[1], rawSizes[1], nSrcWidth, nSrcHeight, |
1102 | 190 | rawWidths[1], rawHeights[1], planar->deltaPlanes[0])) |
1103 | 0 | return FALSE; |
1104 | | |
1105 | 190 | planes[1] = planar->deltaPlanes[0]; |
1106 | 190 | rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */ |
1107 | 190 | rawWidths[1] = nSrcWidth; |
1108 | 190 | rawHeights[1] = nSrcHeight; |
1109 | | |
1110 | 190 | if (!planar_subsample_expand(planes[2], rawSizes[2], nSrcWidth, nSrcHeight, |
1111 | 190 | rawWidths[2], rawHeights[2], planar->deltaPlanes[1])) |
1112 | 0 | return FALSE; |
1113 | | |
1114 | 190 | planes[2] = planar->deltaPlanes[1]; |
1115 | 190 | rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */ |
1116 | 190 | rawWidths[2] = nSrcWidth; |
1117 | 190 | rawHeights[2] = nSrcHeight; |
1118 | 190 | } |
1119 | | |
1120 | 546 | if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst, |
1121 | 546 | nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight)) |
1122 | 0 | return FALSE; |
1123 | | |
1124 | 546 | if (alpha) |
1125 | 218 | srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3]; |
1126 | 328 | else /* NoAlpha */ |
1127 | 328 | srcp += rawSizes[0] + rawSizes[1] + rawSizes[2]; |
1128 | | |
1129 | 546 | if ((SrcSize - (srcp - pSrcData)) == 1) |
1130 | 0 | srcp++; /* pad */ |
1131 | 546 | } |
1132 | | |
1133 | 546 | WINPR_ASSERT(prims->YCoCgToRGB_8u_AC4R); |
1134 | 546 | int rc = prims->YCoCgToRGB_8u_AC4R( |
1135 | 546 | pTempData, WINPR_ASSERTING_INT_CAST(int32_t, nTempStep), dst, DstFormat, |
1136 | 546 | WINPR_ASSERTING_INT_CAST(int32_t, nDstStep), w, h, cll, useAlpha); |
1137 | 546 | if (rc != PRIMITIVES_SUCCESS) |
1138 | 0 | { |
1139 | 0 | WLog_ERR(TAG, "YCoCgToRGB_8u_AC4R failed with %d", rc); |
1140 | 0 | return FALSE; |
1141 | 0 | } |
1142 | 546 | } |
1143 | | |
1144 | 546 | WINPR_UNUSED(srcp); |
1145 | 546 | return TRUE; |
1146 | 1.18k | } |
1147 | | |
1148 | | static inline BOOL freerdp_split_color_planes(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, |
1149 | | const BYTE* WINPR_RESTRICT data, UINT32 format, |
1150 | | UINT32 width, UINT32 height, UINT32 scanline, |
1151 | | BYTE* WINPR_RESTRICT planes[4]) |
1152 | 0 | { |
1153 | 0 | WINPR_ASSERT(planar); |
1154 | |
|
1155 | 0 | if ((width > INT32_MAX) || (height > INT32_MAX) || (scanline > INT32_MAX)) |
1156 | 0 | return FALSE; |
1157 | | |
1158 | 0 | if (scanline == 0) |
1159 | 0 | scanline = width * FreeRDPGetBytesPerPixel(format); |
1160 | |
|
1161 | 0 | if (planar->topdown) |
1162 | 0 | { |
1163 | 0 | UINT32 k = 0; |
1164 | 0 | for (UINT32 i = 0; i < height; i++) |
1165 | 0 | { |
1166 | 0 | const BYTE* pixel = &data[1ULL * scanline * i]; |
1167 | |
|
1168 | 0 | for (UINT32 j = 0; j < width; j++) |
1169 | 0 | { |
1170 | 0 | const UINT32 color = FreeRDPReadColor(pixel, format); |
1171 | 0 | pixel += FreeRDPGetBytesPerPixel(format); |
1172 | 0 | FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k], |
1173 | 0 | &planes[0][k], NULL); |
1174 | 0 | k++; |
1175 | 0 | } |
1176 | 0 | } |
1177 | 0 | } |
1178 | 0 | else |
1179 | 0 | { |
1180 | 0 | UINT32 k = 0; |
1181 | |
|
1182 | 0 | for (INT64 i = (INT64)height - 1; i >= 0; i--) |
1183 | 0 | { |
1184 | 0 | const BYTE* pixel = &data[1ULL * scanline * (UINT32)i]; |
1185 | |
|
1186 | 0 | for (UINT32 j = 0; j < width; j++) |
1187 | 0 | { |
1188 | 0 | const UINT32 color = FreeRDPReadColor(pixel, format); |
1189 | 0 | pixel += FreeRDPGetBytesPerPixel(format); |
1190 | 0 | FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k], |
1191 | 0 | &planes[0][k], NULL); |
1192 | 0 | k++; |
1193 | 0 | } |
1194 | 0 | } |
1195 | 0 | } |
1196 | 0 | return TRUE; |
1197 | 0 | } |
1198 | | |
1199 | | static inline UINT32 freerdp_bitmap_planar_write_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer, |
1200 | | UINT32 cRawBytes, UINT32 nRunLength, |
1201 | | BYTE* WINPR_RESTRICT pOutBuffer, |
1202 | | UINT32 outBufferSize) |
1203 | 0 | { |
1204 | 0 | const BYTE* pInput = pInBuffer; |
1205 | 0 | BYTE* pOutput = pOutBuffer; |
1206 | 0 | BYTE controlByte = 0; |
1207 | 0 | UINT32 nBytesToWrite = 0; |
1208 | |
|
1209 | 0 | if (!cRawBytes && !nRunLength) |
1210 | 0 | return 0; |
1211 | | |
1212 | 0 | if (nRunLength < 3) |
1213 | 0 | { |
1214 | 0 | cRawBytes += nRunLength; |
1215 | 0 | nRunLength = 0; |
1216 | 0 | } |
1217 | |
|
1218 | 0 | while (cRawBytes) |
1219 | 0 | { |
1220 | 0 | if (cRawBytes < 16) |
1221 | 0 | { |
1222 | 0 | if (nRunLength > 15) |
1223 | 0 | { |
1224 | 0 | if (nRunLength < 18) |
1225 | 0 | { |
1226 | 0 | controlByte = PLANAR_CONTROL_BYTE(13, cRawBytes); |
1227 | 0 | nRunLength -= 13; |
1228 | 0 | cRawBytes = 0; |
1229 | 0 | } |
1230 | 0 | else |
1231 | 0 | { |
1232 | 0 | controlByte = PLANAR_CONTROL_BYTE(15, cRawBytes); |
1233 | 0 | nRunLength -= 15; |
1234 | 0 | cRawBytes = 0; |
1235 | 0 | } |
1236 | 0 | } |
1237 | 0 | else |
1238 | 0 | { |
1239 | 0 | controlByte = PLANAR_CONTROL_BYTE(nRunLength, cRawBytes); |
1240 | 0 | nRunLength = 0; |
1241 | 0 | cRawBytes = 0; |
1242 | 0 | } |
1243 | 0 | } |
1244 | 0 | else |
1245 | 0 | { |
1246 | 0 | controlByte = PLANAR_CONTROL_BYTE(0, 15); |
1247 | 0 | cRawBytes -= 15; |
1248 | 0 | } |
1249 | |
|
1250 | 0 | if (outBufferSize < 1) |
1251 | 0 | return 0; |
1252 | | |
1253 | 0 | outBufferSize--; |
1254 | 0 | *pOutput = controlByte; |
1255 | 0 | pOutput++; |
1256 | 0 | nBytesToWrite = (controlByte >> 4); |
1257 | |
|
1258 | 0 | if (nBytesToWrite) |
1259 | 0 | { |
1260 | 0 | if (outBufferSize < nBytesToWrite) |
1261 | 0 | return 0; |
1262 | | |
1263 | 0 | outBufferSize -= nBytesToWrite; |
1264 | 0 | CopyMemory(pOutput, pInput, nBytesToWrite); |
1265 | 0 | pOutput += nBytesToWrite; |
1266 | 0 | pInput += nBytesToWrite; |
1267 | 0 | } |
1268 | 0 | } |
1269 | | |
1270 | 0 | while (nRunLength) |
1271 | 0 | { |
1272 | 0 | if (nRunLength > 47) |
1273 | 0 | { |
1274 | 0 | if (nRunLength < 50) |
1275 | 0 | { |
1276 | 0 | controlByte = PLANAR_CONTROL_BYTE(2, 13); |
1277 | 0 | nRunLength -= 45; |
1278 | 0 | } |
1279 | 0 | else |
1280 | 0 | { |
1281 | 0 | controlByte = PLANAR_CONTROL_BYTE(2, 15); |
1282 | 0 | nRunLength -= 47; |
1283 | 0 | } |
1284 | 0 | } |
1285 | 0 | else if (nRunLength > 31) |
1286 | 0 | { |
1287 | 0 | controlByte = PLANAR_CONTROL_BYTE(2, (nRunLength - 32)); |
1288 | 0 | nRunLength = 0; |
1289 | 0 | } |
1290 | 0 | else if (nRunLength > 15) |
1291 | 0 | { |
1292 | 0 | controlByte = PLANAR_CONTROL_BYTE(1, (nRunLength - 16)); |
1293 | 0 | nRunLength = 0; |
1294 | 0 | } |
1295 | 0 | else |
1296 | 0 | { |
1297 | 0 | controlByte = PLANAR_CONTROL_BYTE(nRunLength, 0); |
1298 | 0 | nRunLength = 0; |
1299 | 0 | } |
1300 | |
|
1301 | 0 | if (outBufferSize < 1) |
1302 | 0 | return 0; |
1303 | | |
1304 | 0 | --outBufferSize; |
1305 | 0 | *pOutput = controlByte; |
1306 | 0 | pOutput++; |
1307 | 0 | } |
1308 | | |
1309 | 0 | const intptr_t diff = (pOutput - pOutBuffer); |
1310 | 0 | if ((diff < 0) || (diff > UINT32_MAX)) |
1311 | 0 | return 0; |
1312 | 0 | return (UINT32)diff; |
1313 | 0 | } |
1314 | | |
1315 | | static inline UINT32 freerdp_bitmap_planar_encode_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer, |
1316 | | UINT32 inBufferSize, |
1317 | | BYTE* WINPR_RESTRICT pOutBuffer, |
1318 | | UINT32 outBufferSize) |
1319 | 0 | { |
1320 | 0 | BYTE symbol = 0; |
1321 | 0 | const BYTE* pInput = pInBuffer; |
1322 | 0 | BYTE* pOutput = pOutBuffer; |
1323 | 0 | const BYTE* pBytes = NULL; |
1324 | 0 | UINT32 cRawBytes = 0; |
1325 | 0 | UINT32 nRunLength = 0; |
1326 | 0 | UINT32 nBytesWritten = 0; |
1327 | 0 | UINT32 nTotalBytesWritten = 0; |
1328 | |
|
1329 | 0 | if (!outBufferSize) |
1330 | 0 | return 0; |
1331 | | |
1332 | 0 | do |
1333 | 0 | { |
1334 | 0 | if (!inBufferSize) |
1335 | 0 | break; |
1336 | | |
1337 | 0 | const UINT32 bSymbolMatch = (symbol == *pInput) ? TRUE : FALSE; |
1338 | 0 | symbol = *pInput; |
1339 | 0 | pInput++; |
1340 | 0 | inBufferSize--; |
1341 | |
|
1342 | 0 | if (nRunLength && !bSymbolMatch) |
1343 | 0 | { |
1344 | 0 | if (nRunLength < 3) |
1345 | 0 | { |
1346 | 0 | cRawBytes += nRunLength; |
1347 | 0 | nRunLength = 0; |
1348 | 0 | } |
1349 | 0 | else |
1350 | 0 | { |
1351 | 0 | pBytes = pInput - (cRawBytes + nRunLength + 1); |
1352 | 0 | nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength, |
1353 | 0 | pOutput, outBufferSize); |
1354 | 0 | nRunLength = 0; |
1355 | |
|
1356 | 0 | if (!nBytesWritten || (nBytesWritten > outBufferSize)) |
1357 | 0 | return nRunLength; |
1358 | | |
1359 | 0 | nTotalBytesWritten += nBytesWritten; |
1360 | 0 | outBufferSize -= nBytesWritten; |
1361 | 0 | pOutput += nBytesWritten; |
1362 | 0 | cRawBytes = 0; |
1363 | 0 | } |
1364 | 0 | } |
1365 | | |
1366 | 0 | nRunLength += bSymbolMatch; |
1367 | 0 | cRawBytes += (!bSymbolMatch) ? TRUE : FALSE; |
1368 | 0 | } while (outBufferSize); |
1369 | | |
1370 | 0 | if (cRawBytes || nRunLength) |
1371 | 0 | { |
1372 | 0 | pBytes = pInput - (cRawBytes + nRunLength); |
1373 | 0 | nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength, |
1374 | 0 | pOutput, outBufferSize); |
1375 | |
|
1376 | 0 | if (!nBytesWritten) |
1377 | 0 | return 0; |
1378 | | |
1379 | 0 | nTotalBytesWritten += nBytesWritten; |
1380 | 0 | } |
1381 | | |
1382 | 0 | if (inBufferSize) |
1383 | 0 | return 0; |
1384 | | |
1385 | 0 | return nTotalBytesWritten; |
1386 | 0 | } |
1387 | | |
1388 | | BOOL freerdp_bitmap_planar_compress_plane_rle(const BYTE* WINPR_RESTRICT inPlane, UINT32 width, |
1389 | | UINT32 height, BYTE* WINPR_RESTRICT outPlane, |
1390 | | UINT32* WINPR_RESTRICT dstSize) |
1391 | 0 | { |
1392 | 0 | if (!outPlane) |
1393 | 0 | return FALSE; |
1394 | | |
1395 | 0 | UINT32 index = 0; |
1396 | 0 | const BYTE* pInput = inPlane; |
1397 | 0 | BYTE* pOutput = outPlane; |
1398 | 0 | UINT32 outBufferSize = *dstSize; |
1399 | 0 | UINT32 nTotalBytesWritten = 0; |
1400 | |
|
1401 | 0 | while (outBufferSize > 0) |
1402 | 0 | { |
1403 | 0 | const UINT32 nBytesWritten = |
1404 | 0 | freerdp_bitmap_planar_encode_rle_bytes(pInput, width, pOutput, outBufferSize); |
1405 | |
|
1406 | 0 | if ((!nBytesWritten) || (nBytesWritten > outBufferSize)) |
1407 | 0 | return FALSE; |
1408 | | |
1409 | 0 | outBufferSize -= nBytesWritten; |
1410 | 0 | nTotalBytesWritten += nBytesWritten; |
1411 | 0 | pOutput += nBytesWritten; |
1412 | 0 | pInput += width; |
1413 | 0 | index++; |
1414 | |
|
1415 | 0 | if (index >= height) |
1416 | 0 | break; |
1417 | 0 | } |
1418 | | |
1419 | 0 | *dstSize = nTotalBytesWritten; |
1420 | 0 | return TRUE; |
1421 | 0 | } |
1422 | | |
1423 | | static inline BOOL freerdp_bitmap_planar_compress_planes_rle(BYTE* WINPR_RESTRICT inPlanes[4], |
1424 | | UINT32 width, UINT32 height, |
1425 | | BYTE* WINPR_RESTRICT outPlanes, |
1426 | | UINT32* WINPR_RESTRICT dstSizes, |
1427 | | BOOL skipAlpha) |
1428 | 0 | { |
1429 | 0 | UINT32 outPlanesSize = width * height * 4; |
1430 | | |
1431 | | /* AlphaPlane */ |
1432 | 0 | if (skipAlpha) |
1433 | 0 | { |
1434 | 0 | dstSizes[0] = 0; |
1435 | 0 | } |
1436 | 0 | else |
1437 | 0 | { |
1438 | 0 | dstSizes[0] = outPlanesSize; |
1439 | |
|
1440 | 0 | if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[0], width, height, outPlanes, |
1441 | 0 | &dstSizes[0])) |
1442 | 0 | return FALSE; |
1443 | | |
1444 | 0 | outPlanes += dstSizes[0]; |
1445 | 0 | outPlanesSize -= dstSizes[0]; |
1446 | 0 | } |
1447 | | |
1448 | | /* LumaOrRedPlane */ |
1449 | 0 | dstSizes[1] = outPlanesSize; |
1450 | |
|
1451 | 0 | if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[1], width, height, outPlanes, |
1452 | 0 | &dstSizes[1])) |
1453 | 0 | return FALSE; |
1454 | | |
1455 | 0 | outPlanes += dstSizes[1]; |
1456 | 0 | outPlanesSize -= dstSizes[1]; |
1457 | | /* OrangeChromaOrGreenPlane */ |
1458 | 0 | dstSizes[2] = outPlanesSize; |
1459 | |
|
1460 | 0 | if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[2], width, height, outPlanes, |
1461 | 0 | &dstSizes[2])) |
1462 | 0 | return FALSE; |
1463 | | |
1464 | 0 | outPlanes += dstSizes[2]; |
1465 | 0 | outPlanesSize -= dstSizes[2]; |
1466 | | /* GreenChromeOrBluePlane */ |
1467 | 0 | dstSizes[3] = outPlanesSize; |
1468 | |
|
1469 | 0 | if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[3], width, height, outPlanes, |
1470 | 0 | &dstSizes[3])) |
1471 | 0 | return FALSE; |
1472 | | |
1473 | 0 | return TRUE; |
1474 | 0 | } |
1475 | | |
1476 | | BYTE* freerdp_bitmap_planar_delta_encode_plane(const BYTE* WINPR_RESTRICT inPlane, UINT32 width, |
1477 | | UINT32 height, BYTE* WINPR_RESTRICT outPlane) |
1478 | 0 | { |
1479 | 0 | if (!outPlane) |
1480 | 0 | { |
1481 | 0 | if (width * height == 0) |
1482 | 0 | return NULL; |
1483 | | |
1484 | 0 | outPlane = (BYTE*)calloc(height, width); |
1485 | 0 | if (!outPlane) |
1486 | 0 | return NULL; |
1487 | 0 | } |
1488 | | |
1489 | | // first line is copied as is |
1490 | 0 | CopyMemory(outPlane, inPlane, width); |
1491 | |
|
1492 | 0 | for (UINT32 y = 1; y < height; y++) |
1493 | 0 | { |
1494 | 0 | const size_t off = 1ull * width * y; |
1495 | 0 | BYTE* outPtr = &outPlane[off]; |
1496 | 0 | const BYTE* srcPtr = &inPlane[off]; |
1497 | 0 | const BYTE* prevLinePtr = &inPlane[off - width]; |
1498 | 0 | for (UINT32 x = 0; x < width; x++) |
1499 | 0 | { |
1500 | 0 | const int delta = (int)srcPtr[x] - (int)prevLinePtr[x]; |
1501 | 0 | const int s2c1i = (delta >= 0) ? delta : (INT_MAX + delta) + 1; |
1502 | 0 | const int8_t s2c1 = WINPR_CXX_COMPAT_CAST(int8_t, s2c1i); |
1503 | 0 | const uint32_t s2c = |
1504 | 0 | (s2c1 >= 0) ? ((UINT32)s2c1 << 1) : (((UINT32)(~(s2c1) + 1) << 1) - 1); |
1505 | 0 | outPtr[x] = (BYTE)s2c; |
1506 | 0 | } |
1507 | 0 | } |
1508 | |
|
1509 | 0 | return outPlane; |
1510 | 0 | } |
1511 | | |
1512 | | static inline BOOL freerdp_bitmap_planar_delta_encode_planes(BYTE* WINPR_RESTRICT inPlanes[4], |
1513 | | UINT32 width, UINT32 height, |
1514 | | BYTE* WINPR_RESTRICT outPlanes[4]) |
1515 | 0 | { |
1516 | 0 | for (UINT32 i = 0; i < 4; i++) |
1517 | 0 | { |
1518 | 0 | outPlanes[i] = |
1519 | 0 | freerdp_bitmap_planar_delta_encode_plane(inPlanes[i], width, height, outPlanes[i]); |
1520 | |
|
1521 | 0 | if (!outPlanes[i]) |
1522 | 0 | return FALSE; |
1523 | 0 | } |
1524 | | |
1525 | 0 | return TRUE; |
1526 | 0 | } |
1527 | | |
1528 | | BYTE* freerdp_bitmap_compress_planar(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context, |
1529 | | const BYTE* WINPR_RESTRICT data, UINT32 format, UINT32 width, |
1530 | | UINT32 height, UINT32 scanline, BYTE* WINPR_RESTRICT dstData, |
1531 | | UINT32* WINPR_RESTRICT pDstSize) |
1532 | 0 | { |
1533 | 0 | UINT32 size = 0; |
1534 | 0 | BYTE* dstp = NULL; |
1535 | 0 | UINT32 dstSizes[4] = WINPR_C_ARRAY_INIT; |
1536 | 0 | BYTE FormatHeader = 0; |
1537 | |
|
1538 | 0 | if (!context || !context->rlePlanesBuffer) |
1539 | 0 | return NULL; |
1540 | | |
1541 | 0 | if (context->AllowSkipAlpha) |
1542 | 0 | FormatHeader |= PLANAR_FORMAT_HEADER_NA; |
1543 | |
|
1544 | 0 | const UINT32 planeSize = width * height; |
1545 | |
|
1546 | 0 | if (!context->AllowSkipAlpha) |
1547 | 0 | format = planar_invert_format(context, TRUE, format); |
1548 | |
|
1549 | 0 | if (!freerdp_split_color_planes(context, data, format, width, height, scanline, |
1550 | 0 | context->planes)) |
1551 | 0 | return NULL; |
1552 | | |
1553 | 0 | if (context->AllowRunLengthEncoding) |
1554 | 0 | { |
1555 | 0 | if (!freerdp_bitmap_planar_delta_encode_planes(context->planes, width, height, |
1556 | 0 | context->deltaPlanes)) |
1557 | 0 | return NULL; |
1558 | | |
1559 | 0 | if (!freerdp_bitmap_planar_compress_planes_rle(context->deltaPlanes, width, height, |
1560 | 0 | context->rlePlanesBuffer, dstSizes, |
1561 | 0 | context->AllowSkipAlpha)) |
1562 | 0 | return NULL; |
1563 | | |
1564 | 0 | { |
1565 | 0 | uint32_t offset = 0; |
1566 | 0 | FormatHeader |= PLANAR_FORMAT_HEADER_RLE; |
1567 | 0 | context->rlePlanes[0] = &context->rlePlanesBuffer[offset]; |
1568 | 0 | offset += dstSizes[0]; |
1569 | 0 | context->rlePlanes[1] = &context->rlePlanesBuffer[offset]; |
1570 | 0 | offset += dstSizes[1]; |
1571 | 0 | context->rlePlanes[2] = &context->rlePlanesBuffer[offset]; |
1572 | 0 | offset += dstSizes[2]; |
1573 | 0 | context->rlePlanes[3] = &context->rlePlanesBuffer[offset]; |
1574 | |
|
1575 | | #if defined(WITH_DEBUG_CODECS) |
1576 | | WLog_DBG(TAG, |
1577 | | "R: [%" PRIu32 "/%" PRIu32 "] G: [%" PRIu32 "/%" PRIu32 "] B: [%" PRIu32 |
1578 | | " / %" PRIu32 "] ", |
1579 | | dstSizes[1], planeSize, dstSizes[2], planeSize, dstSizes[3], planeSize); |
1580 | | #endif |
1581 | 0 | } |
1582 | 0 | } |
1583 | | |
1584 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1585 | 0 | { |
1586 | 0 | if (!context->AllowRunLengthEncoding) |
1587 | 0 | return NULL; |
1588 | | |
1589 | 0 | if (context->rlePlanes[0] == NULL) |
1590 | 0 | return NULL; |
1591 | | |
1592 | 0 | if (context->rlePlanes[1] == NULL) |
1593 | 0 | return NULL; |
1594 | | |
1595 | 0 | if (context->rlePlanes[2] == NULL) |
1596 | 0 | return NULL; |
1597 | | |
1598 | 0 | if (context->rlePlanes[3] == NULL) |
1599 | 0 | return NULL; |
1600 | 0 | } |
1601 | | |
1602 | 0 | if (!dstData) |
1603 | 0 | { |
1604 | 0 | size = 1; |
1605 | |
|
1606 | 0 | if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA)) |
1607 | 0 | { |
1608 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1609 | 0 | size += dstSizes[0]; |
1610 | 0 | else |
1611 | 0 | size += planeSize; |
1612 | 0 | } |
1613 | |
|
1614 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1615 | 0 | size += (dstSizes[1] + dstSizes[2] + dstSizes[3]); |
1616 | 0 | else |
1617 | 0 | size += (planeSize * 3); |
1618 | |
|
1619 | 0 | if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE)) |
1620 | 0 | size++; |
1621 | |
|
1622 | 0 | dstData = malloc(size); |
1623 | |
|
1624 | 0 | if (!dstData) |
1625 | 0 | return NULL; |
1626 | | |
1627 | 0 | *pDstSize = size; |
1628 | 0 | } |
1629 | | |
1630 | 0 | dstp = dstData; |
1631 | 0 | *dstp = FormatHeader; /* FormatHeader */ |
1632 | 0 | dstp++; |
1633 | | |
1634 | | /* AlphaPlane */ |
1635 | |
|
1636 | 0 | if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA)) |
1637 | 0 | { |
1638 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1639 | 0 | { |
1640 | 0 | CopyMemory(dstp, context->rlePlanes[0], dstSizes[0]); /* Alpha */ |
1641 | 0 | dstp += dstSizes[0]; |
1642 | 0 | } |
1643 | 0 | else |
1644 | 0 | { |
1645 | 0 | CopyMemory(dstp, context->planes[0], planeSize); /* Alpha */ |
1646 | 0 | dstp += planeSize; |
1647 | 0 | } |
1648 | 0 | } |
1649 | | |
1650 | | /* LumaOrRedPlane */ |
1651 | |
|
1652 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1653 | 0 | { |
1654 | 0 | CopyMemory(dstp, context->rlePlanes[1], dstSizes[1]); /* Red */ |
1655 | 0 | dstp += dstSizes[1]; |
1656 | 0 | } |
1657 | 0 | else |
1658 | 0 | { |
1659 | 0 | CopyMemory(dstp, context->planes[1], planeSize); /* Red */ |
1660 | 0 | dstp += planeSize; |
1661 | 0 | } |
1662 | | |
1663 | | /* OrangeChromaOrGreenPlane */ |
1664 | |
|
1665 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1666 | 0 | { |
1667 | 0 | CopyMemory(dstp, context->rlePlanes[2], dstSizes[2]); /* Green */ |
1668 | 0 | dstp += dstSizes[2]; |
1669 | 0 | } |
1670 | 0 | else |
1671 | 0 | { |
1672 | 0 | CopyMemory(dstp, context->planes[2], planeSize); /* Green */ |
1673 | 0 | dstp += planeSize; |
1674 | 0 | } |
1675 | | |
1676 | | /* GreenChromeOrBluePlane */ |
1677 | |
|
1678 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1679 | 0 | { |
1680 | 0 | CopyMemory(dstp, context->rlePlanes[3], dstSizes[3]); /* Blue */ |
1681 | 0 | dstp += dstSizes[3]; |
1682 | 0 | } |
1683 | 0 | else |
1684 | 0 | { |
1685 | 0 | CopyMemory(dstp, context->planes[3], planeSize); /* Blue */ |
1686 | 0 | dstp += planeSize; |
1687 | 0 | } |
1688 | | |
1689 | | /* Pad1 (1 byte) */ |
1690 | |
|
1691 | 0 | if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE)) |
1692 | 0 | { |
1693 | 0 | *dstp = 0; |
1694 | 0 | dstp++; |
1695 | 0 | } |
1696 | |
|
1697 | 0 | const intptr_t diff = (dstp - dstData); |
1698 | 0 | if ((diff < 0) || (diff > UINT32_MAX)) |
1699 | 0 | { |
1700 | 0 | free(dstData); |
1701 | 0 | return NULL; |
1702 | 0 | } |
1703 | 0 | size = (UINT32)diff; |
1704 | 0 | *pDstSize = size; |
1705 | 0 | return dstData; |
1706 | 0 | } |
1707 | | |
1708 | | BOOL freerdp_bitmap_planar_context_reset(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context, |
1709 | | UINT32 width, UINT32 height) |
1710 | 5.51k | { |
1711 | 5.51k | if (!context) |
1712 | 0 | return FALSE; |
1713 | | |
1714 | 5.51k | context->bgr = FALSE; |
1715 | 5.51k | context->maxWidth = PLANAR_ALIGN(width, 4); |
1716 | 5.51k | context->maxHeight = PLANAR_ALIGN(height, 4); |
1717 | 5.51k | { |
1718 | 5.51k | const UINT64 tmp = (UINT64)context->maxWidth * context->maxHeight; |
1719 | 5.51k | if (tmp > UINT32_MAX) |
1720 | 0 | return FALSE; |
1721 | 5.51k | context->maxPlaneSize = (UINT32)tmp; |
1722 | 5.51k | } |
1723 | | |
1724 | 5.51k | if (context->maxWidth > UINT32_MAX / 4) |
1725 | 0 | return FALSE; |
1726 | 5.51k | context->nTempStep = context->maxWidth * 4; |
1727 | | |
1728 | 5.51k | memset((void*)context->planes, 0, sizeof(context->planes)); |
1729 | 5.51k | memset((void*)context->rlePlanes, 0, sizeof(context->rlePlanes)); |
1730 | 5.51k | memset((void*)context->deltaPlanes, 0, sizeof(context->deltaPlanes)); |
1731 | | |
1732 | 5.51k | if (context->maxPlaneSize > 0) |
1733 | 5.51k | { |
1734 | 5.51k | void* tmp = winpr_aligned_recalloc(context->planesBuffer, context->maxPlaneSize, 4, 32); |
1735 | 5.51k | if (!tmp) |
1736 | 0 | return FALSE; |
1737 | 5.51k | context->planesBuffer = tmp; |
1738 | | |
1739 | 5.51k | tmp = winpr_aligned_recalloc(context->pTempData, context->maxPlaneSize, 6, 32); |
1740 | 5.51k | if (!tmp) |
1741 | 0 | return FALSE; |
1742 | 5.51k | context->pTempData = tmp; |
1743 | | |
1744 | 5.51k | tmp = winpr_aligned_recalloc(context->deltaPlanesBuffer, context->maxPlaneSize, 4, 32); |
1745 | 5.51k | if (!tmp) |
1746 | 0 | return FALSE; |
1747 | 5.51k | context->deltaPlanesBuffer = tmp; |
1748 | | |
1749 | 5.51k | tmp = winpr_aligned_recalloc(context->rlePlanesBuffer, context->maxPlaneSize, 4, 32); |
1750 | 5.51k | if (!tmp) |
1751 | 0 | return FALSE; |
1752 | 5.51k | context->rlePlanesBuffer = tmp; |
1753 | | |
1754 | 5.51k | context->planes[0] = &context->planesBuffer[0ULL * context->maxPlaneSize]; |
1755 | 5.51k | context->planes[1] = &context->planesBuffer[1ULL * context->maxPlaneSize]; |
1756 | 5.51k | context->planes[2] = &context->planesBuffer[2ULL * context->maxPlaneSize]; |
1757 | 5.51k | context->planes[3] = &context->planesBuffer[3ULL * context->maxPlaneSize]; |
1758 | 5.51k | context->deltaPlanes[0] = &context->deltaPlanesBuffer[0ULL * context->maxPlaneSize]; |
1759 | 5.51k | context->deltaPlanes[1] = &context->deltaPlanesBuffer[1ULL * context->maxPlaneSize]; |
1760 | 5.51k | context->deltaPlanes[2] = &context->deltaPlanesBuffer[2ULL * context->maxPlaneSize]; |
1761 | 5.51k | context->deltaPlanes[3] = &context->deltaPlanesBuffer[3ULL * context->maxPlaneSize]; |
1762 | 5.51k | } |
1763 | 5.51k | return TRUE; |
1764 | 5.51k | } |
1765 | | |
1766 | | BITMAP_PLANAR_CONTEXT* freerdp_bitmap_planar_context_new(DWORD flags, UINT32 maxWidth, |
1767 | | UINT32 maxHeight) |
1768 | 5.51k | { |
1769 | 5.51k | BITMAP_PLANAR_CONTEXT* context = |
1770 | 5.51k | (BITMAP_PLANAR_CONTEXT*)winpr_aligned_calloc(1, sizeof(BITMAP_PLANAR_CONTEXT), 32); |
1771 | | |
1772 | 5.51k | if (!context) |
1773 | 0 | return NULL; |
1774 | | |
1775 | 5.51k | if (flags & PLANAR_FORMAT_HEADER_NA) |
1776 | 5.51k | context->AllowSkipAlpha = TRUE; |
1777 | | |
1778 | 5.51k | if (flags & PLANAR_FORMAT_HEADER_RLE) |
1779 | 5.51k | context->AllowRunLengthEncoding = TRUE; |
1780 | | |
1781 | 5.51k | if (flags & PLANAR_FORMAT_HEADER_CS) |
1782 | 0 | context->AllowColorSubsampling = TRUE; |
1783 | | |
1784 | 5.51k | context->ColorLossLevel = flags & PLANAR_FORMAT_HEADER_CLL_MASK; |
1785 | | |
1786 | 5.51k | if (context->ColorLossLevel) |
1787 | 0 | context->AllowDynamicColorFidelity = TRUE; |
1788 | | |
1789 | 5.51k | if (!freerdp_bitmap_planar_context_reset(context, maxWidth, maxHeight)) |
1790 | 0 | { |
1791 | 0 | WINPR_PRAGMA_DIAG_PUSH |
1792 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
1793 | 0 | freerdp_bitmap_planar_context_free(context); |
1794 | 0 | WINPR_PRAGMA_DIAG_POP |
1795 | 0 | return NULL; |
1796 | 0 | } |
1797 | | |
1798 | 5.51k | return context; |
1799 | 5.51k | } |
1800 | | |
1801 | | void freerdp_bitmap_planar_context_free(BITMAP_PLANAR_CONTEXT* context) |
1802 | 5.51k | { |
1803 | 5.51k | if (!context) |
1804 | 0 | return; |
1805 | | |
1806 | 5.51k | winpr_aligned_free(context->pTempData); |
1807 | 5.51k | winpr_aligned_free(context->planesBuffer); |
1808 | 5.51k | winpr_aligned_free(context->deltaPlanesBuffer); |
1809 | 5.51k | winpr_aligned_free(context->rlePlanesBuffer); |
1810 | 5.51k | winpr_aligned_free(context); |
1811 | 5.51k | } |
1812 | | |
1813 | | void freerdp_planar_switch_bgr(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL bgr) |
1814 | 0 | { |
1815 | 0 | WINPR_ASSERT(planar); |
1816 | 0 | planar->bgr = bgr; |
1817 | 0 | } |
1818 | | |
1819 | | void freerdp_planar_topdown_image(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL topdown) |
1820 | 0 | { |
1821 | 0 | WINPR_ASSERT(planar); |
1822 | 0 | planar->topdown = topdown; |
1823 | 0 | } |