2 * VMware Screen Codec (VMnc) decoder
3 * Copyright (c) 2006 Konstantin Shishkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * VMware Screen Codec (VMnc) decoder
24 * As Alex Beregszaszi discovered, this is effectively RFB data dump
33 #define MAGIC_WMVi 0x574D5669
36 HT_RAW = 1, // tile is raw
37 HT_BKG = 2, // background color is present
38 HT_FG = 4, // foreground color is present
39 HT_SUB = 8, // subrects are present
40 HT_CLR = 16 // each subrect has own color
46 typedef struct VmncContext {
47 AVCodecContext *avctx;
57 /* read pixel value from stream */
58 static always_inline int vmnc_get_pixel(uint8_t* buf, int bpp, int be) {
59 switch(bpp * 2 + be) {
62 case 4: return LE_16(buf);
63 case 5: return BE_16(buf);
64 case 8: return LE_32(buf);
65 case 9: return BE_32(buf);
70 /* fill rectangle with given colour */
71 static always_inline void paint_rect(uint8_t *dst, int dx, int dy, int w, int h, int color, int bpp, int stride)
74 dst += dx * bpp + dy * stride;
76 for(j = 0; j < h; j++) {
77 memset(dst, color, w);
82 for(j = 0; j < h; j++) {
83 dst2 = (uint16_t*)dst;
84 for(i = 0; i < w; i++) {
91 for(j = 0; j < h; j++) {
92 dst2 = (uint32_t*)dst;
93 for(i = 0; i < w; i++) {
101 static always_inline void paint_raw(uint8_t *dst, int w, int h, uint8_t* src, int bpp, int be, int stride)
104 for(j = 0; j < h; j++) {
105 for(i = 0; i < w; i++) {
106 p = vmnc_get_pixel(src, bpp, be);
113 ((uint16_t*)dst)[i] = p;
116 ((uint32_t*)dst)[i] = p;
124 static int decode_hextile(VmncContext *c, uint8_t* dst, uint8_t* src, int w, int h, int stride)
127 int bg = 0, fg = 0, rects, color, flags, xy, wh;
128 const int bpp = c->bpp2;
130 int bw = 16, bh = 16;
133 for(j = 0; j < h; j += 16) {
136 if(j + 16 > h) bh = h - j;
137 for(i = 0; i < w; i += 16, dst2 += 16 * bpp) {
138 if(i + 16 > w) bw = w - i;
141 paint_raw(dst2, bw, bh, src, bpp, c->bigendian, stride);
142 src += bw * bh * bpp;
145 bg = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp;
148 fg = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp;
153 color = (flags & HT_CLR);
155 paint_rect(dst2, 0, 0, bw, bh, bg, bpp, stride);
157 for(k = 0; k < rects; k++) {
159 fg = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp;
163 paint_rect(dst2, xy >> 4, xy & 0xF, (wh>>4)+1, (wh & 0xF)+1, fg, bpp, stride);
172 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
174 VmncContext * const c = (VmncContext *)avctx->priv_data;
177 int t, dx, dy, w, h, enc, chunks, res;
179 c->pic.reference = 1;
180 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
181 if(avctx->reget_buffer(avctx, &c->pic) < 0){
182 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
191 av_log(avctx, AV_LOG_ERROR, "Frame decoding is not possible. Please report sample to developers.\n");
196 c->pic.key_frame = 1;
197 c->pic.pict_type = FF_I_TYPE;
199 /* parse ServerInitialization struct */
201 w = BE_16(src); src += 2;
202 h = BE_16(src); src += 2;
203 t = BE_32(src); src += 4;
204 if(t != MAGIC_WMVi) {
205 av_log(avctx, AV_LOG_INFO, "Invalid header: magic not found\n");
209 if(depth != c->bpp) {
210 av_log(avctx, AV_LOG_INFO, "Depth mismatch. Container %i bpp, Frame data: %i bpp\n", c->bpp, depth);
213 c->bigendian = *src++;
214 if(c->bigendian & (~1)) {
215 av_log(avctx, AV_LOG_INFO, "Invalid header: bigendian flag = %i\n", c->bigendian);
218 //skip pixel format data
220 chunks = 1; // there should be one chunk with the whole frame, rest could be ignored
222 c->pic.key_frame = 0;
223 c->pic.pict_type = FF_P_TYPE;
226 // decode FramebufferUpdate struct
227 dx = BE_16(src); src += 2;
228 dy = BE_16(src); src += 2;
229 w = BE_16(src); src += 2;
230 h = BE_16(src); src += 2;
231 if((dx + w > c->width) || (dy + h > c->height)) {
232 av_log(avctx, AV_LOG_ERROR, "Incorrect frame size: %ix%i+%ix%i of %ix%i\n", w, h, dx, dy, c->width, c->height);
235 enc = BE_32(src); src += 4;
236 if(enc != 0x00000005) {
237 av_log(avctx, AV_LOG_ERROR, "Only hextile decoding is supported for now\n");
240 av_log(avctx, AV_LOG_INFO, "And this is raw encoding\n");
243 av_log(avctx, AV_LOG_INFO, "And this is CopyRect encoding\n");
246 av_log(avctx, AV_LOG_INFO, "And this is RRE encoding\n");
249 av_log(avctx, AV_LOG_INFO, "And this is CoRRE encoding\n");
252 av_log(avctx, AV_LOG_INFO, "And this is unknown encoding (%i)\n", enc);
256 outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0];
257 res = decode_hextile(c, outptr, src, w, h, c->pic.linesize[0]);
262 *data_size = sizeof(AVFrame);
263 *(AVFrame*)data = c->pic;
265 /* always report that the buffer was completely consumed */
276 static int decode_init(AVCodecContext *avctx)
278 VmncContext * const c = (VmncContext *)avctx->priv_data;
281 avctx->has_b_frames = 0;
283 c->pic.data[0] = NULL;
284 c->width = avctx->width;
285 c->height = avctx->height;
287 if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
290 c->bpp = avctx->bits_per_sample;
295 avctx->pix_fmt = PIX_FMT_PAL8;
298 avctx->pix_fmt = PIX_FMT_RGB555;
301 avctx->pix_fmt = PIX_FMT_RGB32;
304 av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", c->bpp);
314 * Uninit VMnc decoder
317 static int decode_end(AVCodecContext *avctx)
319 VmncContext * const c = (VmncContext *)avctx->priv_data;
322 avctx->release_buffer(avctx, &c->pic);
327 AVCodec vmnc_decoder = {