📄 vmnc.c
字号:
/* * VMware Screen Codec (VMnc) decoder * Copyright (c) 2006 Konstantin Shishkov * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *//** * @file vmnc.c * VMware Screen Codec (VMnc) decoder * As Alex Beregszaszi discovered, this is effectively RFB data dump */#include <stdio.h>#include <stdlib.h>#include "avcodec.h"enum EncTypes { MAGIC_WMVd = 0x574D5664, MAGIC_WMVe, MAGIC_WMVf, MAGIC_WMVg, MAGIC_WMVh, MAGIC_WMVi, MAGIC_WMVj};enum HexTile_Flags { HT_RAW = 1, // tile is raw HT_BKG = 2, // background color is present HT_FG = 4, // foreground color is present HT_SUB = 8, // subrects are present HT_CLR = 16 // each subrect has own color};/* * Decoder context */typedef struct VmncContext { AVCodecContext *avctx; AVFrame pic; int bpp; int bpp2; int bigendian; uint8_t pal[768]; int width, height; /* cursor data */ int cur_w, cur_h; int cur_x, cur_y; int cur_hx, cur_hy; uint8_t* curbits, *curmask; uint8_t* screendta;} VmncContext;/* read pixel value from stream */static av_always_inline int vmnc_get_pixel(const uint8_t* buf, int bpp, int be) { switch(bpp * 2 + be) { case 2: case 3: return *buf; case 4: return AV_RL16(buf); case 5: return AV_RB16(buf); case 8: return AV_RL32(buf); case 9: return AV_RB32(buf); default: return 0; }}static void load_cursor(VmncContext *c, const uint8_t *src){ int i, j, p; const int bpp = c->bpp2; uint8_t *dst8 = c->curbits; uint16_t *dst16 = (uint16_t*)c->curbits; uint32_t *dst32 = (uint32_t*)c->curbits; for(j = 0; j < c->cur_h; j++) { for(i = 0; i < c->cur_w; i++) { p = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp; if(bpp == 1) *dst8++ = p; if(bpp == 2) *dst16++ = p; if(bpp == 4) *dst32++ = p; } } dst8 = c->curmask; dst16 = (uint16_t*)c->curmask; dst32 = (uint32_t*)c->curmask; for(j = 0; j < c->cur_h; j++) { for(i = 0; i < c->cur_w; i++) { p = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp; if(bpp == 1) *dst8++ = p; if(bpp == 2) *dst16++ = p; if(bpp == 4) *dst32++ = p; } }}static void put_cursor(uint8_t *dst, int stride, VmncContext *c, int dx, int dy){ int i, j; int w, h, x, y; w = c->cur_w; if(c->width < c->cur_x + c->cur_w) w = c->width - c->cur_x; h = c->cur_h; if(c->height < c->cur_y + c->cur_h) h = c->height - c->cur_y; x = c->cur_x; y = c->cur_y; if(x < 0) { w += x; x = 0; } if(y < 0) { h += y; y = 0; } if((w < 1) || (h < 1)) return; dst += x * c->bpp2 + y * stride; if(c->bpp2 == 1) { uint8_t* cd = c->curbits, *msk = c->curmask; for(j = 0; j < h; j++) { for(i = 0; i < w; i++) dst[i] = (dst[i] & cd[i]) ^ msk[i]; msk += c->cur_w; cd += c->cur_w; dst += stride; } } else if(c->bpp2 == 2) { uint16_t* cd = (uint16_t*)c->curbits, *msk = (uint16_t*)c->curmask; uint16_t* dst2; for(j = 0; j < h; j++) { dst2 = (uint16_t*)dst; for(i = 0; i < w; i++) dst2[i] = (dst2[i] & cd[i]) ^ msk[i]; msk += c->cur_w; cd += c->cur_w; dst += stride; } } else if(c->bpp2 == 4) { uint32_t* cd = (uint32_t*)c->curbits, *msk = (uint32_t*)c->curmask; uint32_t* dst2; for(j = 0; j < h; j++) { dst2 = (uint32_t*)dst; for(i = 0; i < w; i++) dst2[i] = (dst2[i] & cd[i]) ^ msk[i]; msk += c->cur_w; cd += c->cur_w; dst += stride; } }}/* fill rectangle with given color */static av_always_inline void paint_rect(uint8_t *dst, int dx, int dy, int w, int h, int color, int bpp, int stride){ int i, j; dst += dx * bpp + dy * stride; if(bpp == 1){ for(j = 0; j < h; j++) { memset(dst, color, w); dst += stride; } }else if(bpp == 2){ uint16_t* dst2; for(j = 0; j < h; j++) { dst2 = (uint16_t*)dst; for(i = 0; i < w; i++) { *dst2++ = color; } dst += stride; } }else if(bpp == 4){ uint32_t* dst2; for(j = 0; j < h; j++) { dst2 = (uint32_t*)dst; for(i = 0; i < w; i++) { dst2[i] = color; } dst += stride; } }}static av_always_inline void paint_raw(uint8_t *dst, int w, int h, const uint8_t* src, int bpp, int be, int stride){ int i, j, p; for(j = 0; j < h; j++) { for(i = 0; i < w; i++) { p = vmnc_get_pixel(src, bpp, be); src += bpp; switch(bpp){ case 1: dst[i] = p; break; case 2: ((uint16_t*)dst)[i] = p; break; case 4: ((uint32_t*)dst)[i] = p; break; } } dst += stride; }}static int decode_hextile(VmncContext *c, uint8_t* dst, const uint8_t* src, int ssize, int w, int h, int stride){ int i, j, k; int bg = 0, fg = 0, rects, color, flags, xy, wh; const int bpp = c->bpp2; uint8_t *dst2; int bw = 16, bh = 16; const uint8_t *ssrc=src; for(j = 0; j < h; j += 16) { dst2 = dst; bw = 16; if(j + 16 > h) bh = h - j; for(i = 0; i < w; i += 16, dst2 += 16 * bpp) { if(src - ssrc >= ssize) { av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n"); return -1; } if(i + 16 > w) bw = w - i; flags = *src++; if(flags & HT_RAW) { if(src - ssrc > ssize - bw * bh * bpp) { av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n"); return -1; } paint_raw(dst2, bw, bh, src, bpp, c->bigendian, stride); src += bw * bh * bpp; } else { if(flags & HT_BKG) { bg = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp; } if(flags & HT_FG) { fg = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp; } rects = 0; if(flags & HT_SUB) rects = *src++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -