2 * VMware Screen Codec (VMnc) decoder
3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * VMware Screen Codec (VMnc) decoder
25 * As Alex Beregszaszi discovered, this is effectively RFB data dump
31 #include "libavutil/common.h"
32 #include "libavutil/intreadwrite.h"
37 MAGIC_WMVd = 0x574D5664,
47 HT_RAW = 1, // tile is raw
48 HT_BKG = 2, // background color is present
49 HT_FG = 4, // foreground color is present
50 HT_SUB = 8, // subrects are present
51 HT_CLR = 16 // each subrect has own color
57 typedef struct VmncContext {
58 AVCodecContext *avctx;
71 uint8_t *curbits, *curmask;
75 /* read pixel value from stream */
76 static av_always_inline int vmnc_get_pixel(const uint8_t *buf, int bpp, int be)
78 switch (bpp * 2 + be) {
95 static void load_cursor(VmncContext *c, const uint8_t *src)
98 const int bpp = c->bpp2;
99 uint8_t *dst8 = c->curbits;
100 uint16_t *dst16 = (uint16_t *)c->curbits;
101 uint32_t *dst32 = (uint32_t *)c->curbits;
103 for (j = 0; j < c->cur_h; j++) {
104 for (i = 0; i < c->cur_w; i++) {
105 p = vmnc_get_pixel(src, bpp, c->bigendian);
116 dst16 = (uint16_t*)c->curmask;
117 dst32 = (uint32_t*)c->curmask;
118 for (j = 0; j < c->cur_h; j++) {
119 for (i = 0; i < c->cur_w; i++) {
120 p = vmnc_get_pixel(src, bpp, c->bigendian);
132 static void put_cursor(uint8_t *dst, int stride, VmncContext *c, int dx, int dy)
137 if (c->width < c->cur_x + c->cur_w)
138 w = c->width - c->cur_x;
140 if (c->height < c->cur_y + c->cur_h)
141 h = c->height - c->cur_y;
153 if ((w < 1) || (h < 1))
155 dst += x * c->bpp2 + y * stride;
158 uint8_t *cd = c->curbits, *msk = c->curmask;
159 for (j = 0; j < h; j++) {
160 for (i = 0; i < w; i++)
161 dst[i] = (dst[i] & cd[i]) ^ msk[i];
166 } else if (c->bpp2 == 2) {
167 uint16_t *cd = (uint16_t*)c->curbits, *msk = (uint16_t*)c->curmask;
169 for (j = 0; j < h; j++) {
170 dst2 = (uint16_t*)dst;
171 for (i = 0; i < w; i++)
172 dst2[i] = (dst2[i] & cd[i]) ^ msk[i];
177 } else if (c->bpp2 == 4) {
178 uint32_t *cd = (uint32_t*)c->curbits, *msk = (uint32_t*)c->curmask;
180 for (j = 0; j < h; j++) {
181 dst2 = (uint32_t*)dst;
182 for (i = 0; i < w; i++)
183 dst2[i] = (dst2[i] & cd[i]) ^ msk[i];
191 /* fill rectangle with given color */
192 static av_always_inline void paint_rect(uint8_t *dst, int dx, int dy,
193 int w, int h, int color,
197 dst += dx * bpp + dy * stride;
199 for (j = 0; j < h; j++) {
200 memset(dst, color, w);
203 } else if (bpp == 2) {
205 for (j = 0; j < h; j++) {
206 dst2 = (uint16_t*)dst;
207 for (i = 0; i < w; i++)
211 } else if (bpp == 4) {
213 for (j = 0; j < h; j++) {
214 dst2 = (uint32_t*)dst;
215 for (i = 0; i < w; i++)
222 static av_always_inline void paint_raw(uint8_t *dst, int w, int h,
223 const uint8_t *src, int bpp,
227 for (j = 0; j < h; j++) {
228 for (i = 0; i < w; i++) {
229 p = vmnc_get_pixel(src, bpp, be);
236 ((uint16_t*)dst)[i] = p;
239 ((uint32_t*)dst)[i] = p;
247 static int decode_hextile(VmncContext *c, uint8_t *dst, const uint8_t *src,
248 int ssize, int w, int h, int stride)
251 int bg = 0, fg = 0, rects, color, flags, xy, wh;
252 const int bpp = c->bpp2;
254 int bw = 16, bh = 16;
255 const uint8_t *ssrc = src;
257 for (j = 0; j < h; j += 16) {
262 for (i = 0; i < w; i += 16, dst2 += 16 * bpp) {
263 if (src - ssrc >= ssize) {
264 av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n");
270 if (flags & HT_RAW) {
271 if (src - ssrc > ssize - bw * bh * bpp) {
272 av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n");
275 paint_raw(dst2, bw, bh, src, bpp, c->bigendian, stride);
276 src += bw * bh * bpp;
278 if (flags & HT_BKG) {
279 bg = vmnc_get_pixel(src, bpp, c->bigendian);
283 fg = vmnc_get_pixel(src, bpp, c->bigendian);
289 color = !!(flags & HT_CLR);
291 paint_rect(dst2, 0, 0, bw, bh, bg, bpp, stride);
293 if (src - ssrc > ssize - rects * (color * bpp + 2)) {
294 av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n");
297 for (k = 0; k < rects; k++) {
299 fg = vmnc_get_pixel(src, bpp, c->bigendian);
304 paint_rect(dst2, xy >> 4, xy & 0xF, (wh >> 4) + 1,
305 (wh & 0xF) + 1, fg, bpp, stride);
314 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
317 const uint8_t *buf = avpkt->data;
318 int buf_size = avpkt->size;
319 VmncContext * const c = avctx->priv_data;
321 const uint8_t *src = buf;
322 int dx, dy, w, h, depth, enc, chunks, res, size_left, ret;
324 if ((ret = ff_reget_buffer(avctx, &c->pic)) < 0) {
325 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
329 c->pic.key_frame = 0;
330 c->pic.pict_type = AV_PICTURE_TYPE_P;
332 // restore screen after cursor
336 if (c->width < c->cur_x + w)
337 w = c->width - c->cur_x;
339 if (c->height < c->cur_y + h)
340 h = c->height - c->cur_y;
351 if ((w > 0) && (h > 0)) {
352 outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0];
353 for (i = 0; i < h; i++) {
354 memcpy(outptr, c->screendta + i * c->cur_w * c->bpp2,
356 outptr += c->pic.linesize[0];
361 chunks = AV_RB16(src);
374 outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0];
375 size_left = buf_size - (src - buf);
377 case MAGIC_WMVd: // cursor
378 if (size_left < 2 + w * h * c->bpp2 * 2) {
379 av_log(avctx, AV_LOG_ERROR,
380 "Premature end of data! (need %i got %i)\n",
381 2 + w * h * c->bpp2 * 2, size_left);
389 if ((c->cur_hx > c->cur_w) || (c->cur_hy > c->cur_h)) {
390 av_log(avctx, AV_LOG_ERROR,
391 "Cursor hot spot is not in image: "
392 "%ix%i of %ix%i cursor size\n",
393 c->cur_hx, c->cur_hy, c->cur_w, c->cur_h);
394 c->cur_hx = c->cur_hy = 0;
396 c->curbits = av_realloc(c->curbits, c->cur_w * c->cur_h * c->bpp2);
397 c->curmask = av_realloc(c->curmask, c->cur_w * c->cur_h * c->bpp2);
398 c->screendta = av_realloc(c->screendta, c->cur_w * c->cur_h * c->bpp2);
400 src += w * h * c->bpp2 * 2;
402 case MAGIC_WMVe: // unknown
405 case MAGIC_WMVf: // update cursor position
406 c->cur_x = dx - c->cur_hx;
407 c->cur_y = dy - c->cur_hy;
409 case MAGIC_WMVg: // unknown
412 case MAGIC_WMVh: // unknown
415 case MAGIC_WMVi: // ServerInitialization struct
416 c->pic.key_frame = 1;
417 c->pic.pict_type = AV_PICTURE_TYPE_I;
419 if (depth != c->bpp) {
420 av_log(avctx, AV_LOG_INFO,
421 "Depth mismatch. Container %i bpp, "
422 "Frame data: %i bpp\n",
426 c->bigendian = *src++;
427 if (c->bigendian & (~1)) {
428 av_log(avctx, AV_LOG_INFO,
429 "Invalid header: bigendian flag = %i\n", c->bigendian);
432 // skip the rest of pixel format data
435 case MAGIC_WMVj: // unknown
438 case 0x00000000: // raw rectangle data
439 if ((dx + w > c->width) || (dy + h > c->height)) {
440 av_log(avctx, AV_LOG_ERROR,
441 "Incorrect frame size: %ix%i+%ix%i of %ix%i\n",
442 w, h, dx, dy, c->width, c->height);
445 if (size_left < w * h * c->bpp2) {
446 av_log(avctx, AV_LOG_ERROR,
447 "Premature end of data! (need %i got %i)\n",
448 w * h * c->bpp2, size_left);
451 paint_raw(outptr, w, h, src, c->bpp2, c->bigendian,
453 src += w * h * c->bpp2;
455 case 0x00000005: // HexTile encoded rectangle
456 if ((dx + w > c->width) || (dy + h > c->height)) {
457 av_log(avctx, AV_LOG_ERROR,
458 "Incorrect frame size: %ix%i+%ix%i of %ix%i\n",
459 w, h, dx, dy, c->width, c->height);
462 res = decode_hextile(c, outptr, src, size_left, w, h,
469 av_log(avctx, AV_LOG_ERROR, "Unsupported block type 0x%08X\n", enc);
470 chunks = 0; // leave chunks decoding loop
475 // save screen data before painting cursor
477 if (c->width < c->cur_x + w)
478 w = c->width - c->cur_x;
480 if (c->height < c->cur_y + h)
481 h = c->height - c->cur_y;
492 if ((w > 0) && (h > 0)) {
493 outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0];
494 for (i = 0; i < h; i++) {
495 memcpy(c->screendta + i * c->cur_w * c->bpp2, outptr,
497 outptr += c->pic.linesize[0];
499 outptr = c->pic.data[0];
500 put_cursor(outptr, c->pic.linesize[0], c, c->cur_x, c->cur_y);
504 if ((ret = av_frame_ref(data, &c->pic)) < 0)
507 /* always report that the buffer was completely consumed */
511 static av_cold int decode_init(AVCodecContext *avctx)
513 VmncContext * const c = avctx->priv_data;
516 c->width = avctx->width;
517 c->height = avctx->height;
518 c->bpp = avctx->bits_per_coded_sample;
519 c->bpp2 = c->bpp / 8;
523 avctx->pix_fmt = AV_PIX_FMT_PAL8;
526 avctx->pix_fmt = AV_PIX_FMT_RGB555;
529 avctx->pix_fmt = AV_PIX_FMT_RGB32;
532 av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", c->bpp);
533 return AVERROR_INVALIDDATA;
536 avcodec_get_frame_defaults(&c->pic);
541 static av_cold int decode_end(AVCodecContext *avctx)
543 VmncContext * const c = avctx->priv_data;
545 av_frame_unref(&c->pic);
549 av_free(c->screendta);
553 AVCodec ff_vmnc_decoder = {
555 .long_name = NULL_IF_CONFIG_SMALL("VMware Screen Codec / VMware Video"),
556 .type = AVMEDIA_TYPE_VIDEO,
557 .id = AV_CODEC_ID_VMNC,
558 .priv_data_size = sizeof(VmncContext),
561 .decode = decode_frame,
562 .capabilities = CODEC_CAP_DR1,