1 /*
2 * VMware Screen Codec (VMnc) decoder
3 * Copyright (c) 2006 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * VMware Screen Codec (VMnc) decoder
25 * As Alex Beregszaszi discovered, this is effectively RFB data dump
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
36
45 };
46
49 HT_BKG = 2,
// background color is present
50 HT_FG = 4,
// foreground color is present
52 HT_CLR = 16
// each subrect has own color
53 };
54
55 /*
56 * Decoder context
57 */
61
68
69 /* cursor data */
76
77 /* read pixel value from stream */
79 {
80 switch (bpp * 2 + be) {
81 case 2:
82 case 3:
83 return bytestream2_get_byte(gb);
84 case 4:
85 return bytestream2_get_le16(gb);
86 case 5:
87 return bytestream2_get_be16(gb);
88 case 8:
89 return bytestream2_get_le32(gb);
90 case 9:
91 return bytestream2_get_be32(gb);
92 default: return 0;
93 }
94 }
95
97 {
98 int i, j, p;
99 const int bpp = c->
bpp2;
101 uint16_t *dst16 = (uint16_t *)c->
curbits;
102 uint32_t *dst32 = (uint32_t *)c->
curbits;
103
104 for (j = 0; j < c->
cur_h; j++) {
105 for (i = 0; i < c->
cur_w; i++) {
107 if (bpp == 1)
108 *dst8++ = p;
109 if (bpp == 2)
110 *dst16++ = p;
111 if (bpp == 4)
112 *dst32++ = p;
113 }
114 }
118 for (j = 0; j < c->
cur_h; j++) {
119 for (i = 0; i < c->
cur_w; i++) {
121 if (bpp == 1)
122 *dst8++ = p;
123 if (bpp == 2)
124 *dst16++ = p;
125 if (bpp == 4)
126 *dst32++ = p;
127 }
128 }
129 }
130
132 {
133 int i, j;
143 if (x < 0) {
144 w += x;
145 x = 0;
146 }
147 if (y < 0) {
149 y = 0;
150 }
151
152 if ((w < 1) || (h < 1))
153 return;
154 dst += x * c->
bpp2 + y * stride;
155
158 for (j = 0; j < h; j++) {
159 for (i = 0; i < w; i++)
160 dst[i] = (dst[i] & cd[i]) ^ msk[i];
163 dst += stride;
164 }
165 }
else if (c->
bpp2 == 2) {
166 uint16_t *cd = (uint16_t*)c->
curbits, *msk = (uint16_t*)c->
curmask;
167 uint16_t *dst2;
168 for (j = 0; j < h; j++) {
169 dst2 = (uint16_t*)dst;
170 for (i = 0; i < w; i++)
171 dst2[i] = (dst2[i] & cd[i]) ^ msk[i];
174 dst += stride;
175 }
176 }
else if (c->
bpp2 == 4) {
177 uint32_t *cd = (uint32_t*)c->
curbits, *msk = (uint32_t*)c->
curmask;
178 uint32_t *dst2;
179 for (j = 0; j < h; j++) {
180 dst2 = (uint32_t*)dst;
181 for (i = 0; i < w; i++)
182 dst2[i] = (dst2[i] & cd[i]) ^ msk[i];
185 dst += stride;
186 }
187 }
188 }
189
190 /* fill rectangle with given color */
192 int w,
int h,
int color,
194 {
195 int i, j;
196 dst += dx * bpp + dy * stride;
197 if (bpp == 1) {
198 for (j = 0; j < h; j++) {
199 memset(dst, color, w);
200 dst += stride;
201 }
202 } else if (bpp == 2) {
203 uint16_t *dst2;
204 for (j = 0; j < h; j++) {
205 dst2 = (uint16_t*)dst;
206 for (i = 0; i < w; i++)
207 *dst2++ = color;
208 dst += stride;
209 }
210 } else if (bpp == 4) {
211 uint32_t *dst2;
212 for (j = 0; j < h; j++) {
213 dst2 = (uint32_t*)dst;
214 for (i = 0; i < w; i++)
215 dst2[i] = color;
216 dst += stride;
217 }
218 }
219 }
220
224 {
225 int i, j, p;
226 for (j = 0; j < h; j++) {
227 for (i = 0; i < w; i++) {
229 switch (bpp) {
230 case 1:
231 dst[i] = p;
232 break;
233 case 2:
234 ((uint16_t*)dst)[i] = p;
235 break;
236 case 4:
237 ((uint32_t*)dst)[i] = p;
238 break;
239 }
240 }
241 dst += stride;
242 }
243 }
244
247 {
248 int i, j, k;
249 int bg = 0, fg = 0, rects,
color,
flags, xy, wh;
250 const int bpp = c->
bpp2;
252 int bw = 16, bh = 16;
253
254 for (j = 0; j < h; j += 16) {
255 dst2 = dst;
256 bw = 16;
257 if (j + 16 > h)
258 bh = h - j;
259 for (i = 0; i < w; i += 16, dst2 += 16 * bpp) {
263 }
264 if (i + 16 > w)
265 bw = w - i;
266 flags = bytestream2_get_byte(gb);
271 }
273 } else {
278 rects = 0;
280 rects = bytestream2_get_byte(gb);
281 color = !!(flags &
HT_CLR);
282
283 paint_rect(dst2, 0, 0, bw, bh, bg, bpp, stride);
284
288 }
289 for (k = 0; k < rects; k++) {
290 if (color)
292 xy = bytestream2_get_byte(gb);
293 wh = bytestream2_get_byte(gb);
294 if ( (xy >> 4) + (wh >> 4) + 1 > w - i
295 || (xy & 0xF) + (wh & 0xF)+1 > h - j) {
298 }
300 (wh>>4)+1, (wh & 0xF)+1, fg, bpp, stride);
301 }
302 }
303 }
304 dst += stride * 16;
305 }
306 return 0;
307 }
308
310 {
316
317 }
318
321 {
323 int buf_size = avpkt->
size;
327 int dx, dy, w, h,
depth, enc, chunks, res, size_left,
ret;
328
331
333
336
337 // restore screen after cursor
339 int i;
347 if (dx < 0) {
348 w += dx;
349 dx = 0;
350 }
352 if (dy < 0) {
353 h += dy;
354 dy = 0;
355 }
356 if ((w > 0) && (h > 0)) {
358 for (i = 0; i < h; i++) {
362 }
363 }
364 }
366 chunks = bytestream2_get_be16(gb);
367 while (chunks--) {
370 return -1;
371 }
372 dx = bytestream2_get_be16(gb);
373 dy = bytestream2_get_be16(gb);
374 w = bytestream2_get_be16(gb);
375 h = bytestream2_get_be16(gb);
376 enc = bytestream2_get_be32(gb);
379 switch (enc) {
381 if (w*(int64_t)h*c->
bpp2 > INT_MAX/2 - 2) {
384 }
385 if (size_left < 2 + w * h * c->bpp2 * 2) {
387 "Premature end of data! (need %i got %i)\n",
388 2 + w * h * c->
bpp2 * 2, size_left);
390 }
398 "Cursor hot spot is not in image: "
399 "%ix%i of %ix%i cursor size\n",
402 }
406 } else {
413 }
414 }
416 break;
419 break;
423 break;
426 break;
429 break;
430 case MAGIC_WMVi:
// ServerInitialization struct
433 depth = bytestream2_get_byte(gb);
434 if (depth != c->
bpp) {
436 "Depth mismatch. Container %i bpp, "
437 "Frame data: %i bpp\n",
439 }
444 "Invalid header: bigendian flag = %i\n", c->
bigendian);
446 }
447 //skip the rest of pixel format data
449 break;
452 break;
453 case 0x00000000: // raw rectangle data
456 "Incorrect frame size: %ix%i+%ix%i of %ix%i\n",
459 }
460 if (size_left < w * h * c->bpp2) {
462 "Premature end of data! (need %i got %i)\n",
463 w * h * c->
bpp2, size_left);
465 }
468 break;
469 case 0x00000005: // HexTile encoded rectangle
472 "Incorrect frame size: %ix%i+%ix%i of %ix%i\n",
475 }
477 if (res < 0)
478 return res;
479 break;
480 default:
482 chunks = 0; // leave chunks decoding loop
483 }
484 }
486 int i;
487 // save screen data before painting cursor
495 if (dx < 0) {
496 w += dx;
497 dx = 0;
498 }
500 if (dy < 0) {
501 h += dy;
502 dy = 0;
503 }
504 if ((w > 0) && (h > 0)) {
506 for (i = 0; i < h; i++) {
510 }
513 }
514 }
515 *got_frame = 1;
518
519 /* always report that the buffer was completely consumed */
520 return buf_size;
521 }
522
524 {
526
532
534 case 8:
536 break;
537 case 16:
539 break;
540 case 32:
542 break;
543 default:
546 }
547
551
552 return 0;
553 }
554
556 {
558
560
564 return 0;
565 }
566
577 };