FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vmnc.c
Go to the documentation of this file.
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 
31 #include "libavutil/common.h"
32 #include "libavutil/intreadwrite.h"
33 #include "avcodec.h"
34 #include "internal.h"
35 
36 enum EncTypes {
37  MAGIC_WMVd = 0x574D5664,
44 };
45 
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
52 };
53 
54 /*
55  * Decoder context
56  */
57 typedef struct VmncContext {
60 
61  int bpp;
62  int bpp2;
63  int bigendian;
64  uint8_t pal[768];
65  int width, height;
66 
67  /* cursor data */
68  int cur_w, cur_h;
69  int cur_x, cur_y;
70  int cur_hx, cur_hy;
73 } VmncContext;
74 
75 /* read pixel value from stream */
76 static av_always_inline int vmnc_get_pixel(const uint8_t* buf, int bpp, int be) {
77  switch(bpp * 2 + be) {
78  case 2:
79  case 3: return *buf;
80  case 4: return AV_RL16(buf);
81  case 5: return AV_RB16(buf);
82  case 8: return AV_RL32(buf);
83  case 9: return AV_RB32(buf);
84  default: return 0;
85  }
86 }
87 
88 static void load_cursor(VmncContext *c, const uint8_t *src)
89 {
90  int i, j, p;
91  const int bpp = c->bpp2;
92  uint8_t *dst8 = c->curbits;
93  uint16_t *dst16 = (uint16_t*)c->curbits;
94  uint32_t *dst32 = (uint32_t*)c->curbits;
95 
96  for(j = 0; j < c->cur_h; j++) {
97  for(i = 0; i < c->cur_w; i++) {
98  p = vmnc_get_pixel(src, bpp, c->bigendian);
99  src += bpp;
100  if(bpp == 1) *dst8++ = p;
101  if(bpp == 2) *dst16++ = p;
102  if(bpp == 4) *dst32++ = p;
103  }
104  }
105  dst8 = c->curmask;
106  dst16 = (uint16_t*)c->curmask;
107  dst32 = (uint32_t*)c->curmask;
108  for(j = 0; j < c->cur_h; j++) {
109  for(i = 0; i < c->cur_w; i++) {
110  p = vmnc_get_pixel(src, bpp, c->bigendian);
111  src += bpp;
112  if(bpp == 1) *dst8++ = p;
113  if(bpp == 2) *dst16++ = p;
114  if(bpp == 4) *dst32++ = p;
115  }
116  }
117 }
118 
119 static void put_cursor(uint8_t *dst, int stride, VmncContext *c, int dx, int dy)
120 {
121  int i, j;
122  int w, h, x, y;
123  w = c->cur_w;
124  if(c->width < c->cur_x + c->cur_w) w = c->width - c->cur_x;
125  h = c->cur_h;
126  if(c->height < c->cur_y + c->cur_h) h = c->height - c->cur_y;
127  x = c->cur_x;
128  y = c->cur_y;
129  if(x < 0) {
130  w += x;
131  x = 0;
132  }
133  if(y < 0) {
134  h += y;
135  y = 0;
136  }
137 
138  if((w < 1) || (h < 1)) return;
139  dst += x * c->bpp2 + y * stride;
140 
141  if(c->bpp2 == 1) {
142  uint8_t* cd = c->curbits, *msk = c->curmask;
143  for(j = 0; j < h; j++) {
144  for(i = 0; i < w; i++)
145  dst[i] = (dst[i] & cd[i]) ^ msk[i];
146  msk += c->cur_w;
147  cd += c->cur_w;
148  dst += stride;
149  }
150  } else if(c->bpp2 == 2) {
151  uint16_t* cd = (uint16_t*)c->curbits, *msk = (uint16_t*)c->curmask;
152  uint16_t* dst2;
153  for(j = 0; j < h; j++) {
154  dst2 = (uint16_t*)dst;
155  for(i = 0; i < w; i++)
156  dst2[i] = (dst2[i] & cd[i]) ^ msk[i];
157  msk += c->cur_w;
158  cd += c->cur_w;
159  dst += stride;
160  }
161  } else if(c->bpp2 == 4) {
162  uint32_t* cd = (uint32_t*)c->curbits, *msk = (uint32_t*)c->curmask;
163  uint32_t* dst2;
164  for(j = 0; j < h; j++) {
165  dst2 = (uint32_t*)dst;
166  for(i = 0; i < w; i++)
167  dst2[i] = (dst2[i] & cd[i]) ^ msk[i];
168  msk += c->cur_w;
169  cd += c->cur_w;
170  dst += stride;
171  }
172  }
173 }
174 
175 /* fill rectangle with given color */
176 static av_always_inline void paint_rect(uint8_t *dst, int dx, int dy, int w, int h, int color, int bpp, int stride)
177 {
178  int i, j;
179  dst += dx * bpp + dy * stride;
180  if(bpp == 1){
181  for(j = 0; j < h; j++) {
182  memset(dst, color, w);
183  dst += stride;
184  }
185  }else if(bpp == 2){
186  uint16_t* dst2;
187  for(j = 0; j < h; j++) {
188  dst2 = (uint16_t*)dst;
189  for(i = 0; i < w; i++) {
190  *dst2++ = color;
191  }
192  dst += stride;
193  }
194  }else if(bpp == 4){
195  uint32_t* dst2;
196  for(j = 0; j < h; j++) {
197  dst2 = (uint32_t*)dst;
198  for(i = 0; i < w; i++) {
199  dst2[i] = color;
200  }
201  dst += stride;
202  }
203  }
204 }
205 
206 static av_always_inline void paint_raw(uint8_t *dst, int w, int h, const uint8_t* src, int bpp, int be, int stride)
207 {
208  int i, j, p;
209  for(j = 0; j < h; j++) {
210  for(i = 0; i < w; i++) {
211  p = vmnc_get_pixel(src, bpp, be);
212  src += bpp;
213  switch(bpp){
214  case 1:
215  dst[i] = p;
216  break;
217  case 2:
218  ((uint16_t*)dst)[i] = p;
219  break;
220  case 4:
221  ((uint32_t*)dst)[i] = p;
222  break;
223  }
224  }
225  dst += stride;
226  }
227 }
228 
229 static int decode_hextile(VmncContext *c, uint8_t* dst, const uint8_t* src, int ssize, int w, int h, int stride)
230 {
231  int i, j, k;
232  int bg = 0, fg = 0, rects, color, flags, xy, wh;
233  const int bpp = c->bpp2;
234  uint8_t *dst2;
235  int bw = 16, bh = 16;
236  const uint8_t *ssrc=src;
237 
238  for(j = 0; j < h; j += 16) {
239  dst2 = dst;
240  bw = 16;
241  if(j + 16 > h) bh = h - j;
242  for(i = 0; i < w; i += 16, dst2 += 16 * bpp) {
243  if(src - ssrc >= ssize) {
244  av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n");
245  return -1;
246  }
247  if(i + 16 > w) bw = w - i;
248  flags = *src++;
249  if(flags & HT_RAW) {
250  if(src - ssrc > ssize - bw * bh * bpp) {
251  av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n");
252  return -1;
253  }
254  paint_raw(dst2, bw, bh, src, bpp, c->bigendian, stride);
255  src += bw * bh * bpp;
256  } else {
257  if(flags & HT_BKG) {
258  bg = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp;
259  }
260  if(flags & HT_FG) {
261  fg = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp;
262  }
263  rects = 0;
264  if(flags & HT_SUB)
265  rects = *src++;
266  color = !!(flags & HT_CLR);
267 
268  paint_rect(dst2, 0, 0, bw, bh, bg, bpp, stride);
269 
270  if(src - ssrc > ssize - rects * (color * bpp + 2)) {
271  av_log(c->avctx, AV_LOG_ERROR, "Premature end of data!\n");
272  return -1;
273  }
274  for(k = 0; k < rects; k++) {
275  if(color) {
276  fg = vmnc_get_pixel(src, bpp, c->bigendian); src += bpp;
277  }
278  xy = *src++;
279  wh = *src++;
280  paint_rect(dst2, xy >> 4, xy & 0xF, (wh>>4)+1, (wh & 0xF)+1, fg, bpp, stride);
281  }
282  }
283  }
284  dst += stride * 16;
285  }
286  return src - ssrc;
287 }
288 
289 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
290  AVPacket *avpkt)
291 {
292  const uint8_t *buf = avpkt->data;
293  int buf_size = avpkt->size;
294  VmncContext * const c = avctx->priv_data;
295  uint8_t *outptr;
296  const uint8_t *src = buf;
297  int dx, dy, w, h, depth, enc, chunks, res, size_left, ret;
298 
299  if ((ret = ff_reget_buffer(avctx, &c->pic)) < 0)
300  return ret;
301 
302  c->pic.key_frame = 0;
304 
305  //restore screen after cursor
306  if(c->screendta) {
307  int i;
308  w = c->cur_w;
309  if(c->width < c->cur_x + w) w = c->width - c->cur_x;
310  h = c->cur_h;
311  if(c->height < c->cur_y + h) h = c->height - c->cur_y;
312  dx = c->cur_x;
313  if(dx < 0) {
314  w += dx;
315  dx = 0;
316  }
317  dy = c->cur_y;
318  if(dy < 0) {
319  h += dy;
320  dy = 0;
321  }
322  if((w > 0) && (h > 0)) {
323  outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0];
324  for(i = 0; i < h; i++) {
325  memcpy(outptr, c->screendta + i * c->cur_w * c->bpp2, w * c->bpp2);
326  outptr += c->pic.linesize[0];
327  }
328  }
329  }
330  src += 2;
331  chunks = AV_RB16(src); src += 2;
332  while(chunks--) {
333  if(buf_size - (src - buf) < 12) {
334  av_log(avctx, AV_LOG_ERROR, "Premature end of data!\n");
335  return -1;
336  }
337  dx = AV_RB16(src); src += 2;
338  dy = AV_RB16(src); src += 2;
339  w = AV_RB16(src); src += 2;
340  h = AV_RB16(src); src += 2;
341  enc = AV_RB32(src); src += 4;
342  outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0];
343  size_left = buf_size - (src - buf);
344  switch(enc) {
345  case MAGIC_WMVd: // cursor
346  if (w*(int64_t)h*c->bpp2 > INT_MAX/2 - 2) {
347  av_log(avctx, AV_LOG_ERROR, "dimensions too large\n");
348  return AVERROR_INVALIDDATA;
349  }
350  if(size_left < 2 + w * h * c->bpp2 * 2) {
351  av_log(avctx, AV_LOG_ERROR, "Premature end of data! (need %i got %i)\n", 2 + w * h * c->bpp2 * 2, size_left);
352  return -1;
353  }
354  src += 2;
355  c->cur_w = w;
356  c->cur_h = h;
357  c->cur_hx = dx;
358  c->cur_hy = dy;
359  if((c->cur_hx > c->cur_w) || (c->cur_hy > c->cur_h)) {
360  av_log(avctx, AV_LOG_ERROR, "Cursor hot spot is not in image: %ix%i of %ix%i cursor size\n", c->cur_hx, c->cur_hy, c->cur_w, c->cur_h);
361  c->cur_hx = c->cur_hy = 0;
362  }
363  c->curbits = av_realloc(c->curbits, c->cur_w * c->cur_h * c->bpp2);
364  c->curmask = av_realloc(c->curmask, c->cur_w * c->cur_h * c->bpp2);
365  c->screendta = av_realloc(c->screendta, c->cur_w * c->cur_h * c->bpp2);
366  load_cursor(c, src);
367  src += w * h * c->bpp2 * 2;
368  break;
369  case MAGIC_WMVe: // unknown
370  src += 2;
371  break;
372  case MAGIC_WMVf: // update cursor position
373  c->cur_x = dx - c->cur_hx;
374  c->cur_y = dy - c->cur_hy;
375  break;
376  case MAGIC_WMVg: // unknown
377  src += 10;
378  break;
379  case MAGIC_WMVh: // unknown
380  src += 4;
381  break;
382  case MAGIC_WMVi: // ServerInitialization struct
383  c->pic.key_frame = 1;
385  depth = *src++;
386  if(depth != c->bpp) {
387  av_log(avctx, AV_LOG_INFO, "Depth mismatch. Container %i bpp, Frame data: %i bpp\n", c->bpp, depth);
388  }
389  src++;
390  c->bigendian = *src++;
391  if(c->bigendian & (~1)) {
392  av_log(avctx, AV_LOG_INFO, "Invalid header: bigendian flag = %i\n", c->bigendian);
393  return -1;
394  }
395  //skip the rest of pixel format data
396  src += 13;
397  break;
398  case MAGIC_WMVj: // unknown
399  src += 2;
400  break;
401  case 0x00000000: // raw rectangle data
402  if((dx + w > c->width) || (dy + h > c->height)) {
403  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);
404  return -1;
405  }
406  if(size_left < w * h * c->bpp2) {
407  av_log(avctx, AV_LOG_ERROR, "Premature end of data! (need %i got %i)\n", w * h * c->bpp2, size_left);
408  return -1;
409  }
410  paint_raw(outptr, w, h, src, c->bpp2, c->bigendian, c->pic.linesize[0]);
411  src += w * h * c->bpp2;
412  break;
413  case 0x00000005: // HexTile encoded rectangle
414  if((dx + w > c->width) || (dy + h > c->height)) {
415  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);
416  return -1;
417  }
418  res = decode_hextile(c, outptr, src, size_left, w, h, c->pic.linesize[0]);
419  if(res < 0)
420  return -1;
421  src += res;
422  break;
423  default:
424  av_log(avctx, AV_LOG_ERROR, "Unsupported block type 0x%08X\n", enc);
425  chunks = 0; // leave chunks decoding loop
426  }
427  }
428  if(c->screendta){
429  int i;
430  //save screen data before painting cursor
431  w = c->cur_w;
432  if(c->width < c->cur_x + w) w = c->width - c->cur_x;
433  h = c->cur_h;
434  if(c->height < c->cur_y + h) h = c->height - c->cur_y;
435  dx = c->cur_x;
436  if(dx < 0) {
437  w += dx;
438  dx = 0;
439  }
440  dy = c->cur_y;
441  if(dy < 0) {
442  h += dy;
443  dy = 0;
444  }
445  if((w > 0) && (h > 0)) {
446  outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0];
447  for(i = 0; i < h; i++) {
448  memcpy(c->screendta + i * c->cur_w * c->bpp2, outptr, w * c->bpp2);
449  outptr += c->pic.linesize[0];
450  }
451  outptr = c->pic.data[0];
452  put_cursor(outptr, c->pic.linesize[0], c, c->cur_x, c->cur_y);
453  }
454  }
455  *got_frame = 1;
456  if ((ret = av_frame_ref(data, &c->pic)) < 0)
457  return ret;
458 
459  /* always report that the buffer was completely consumed */
460  return buf_size;
461 }
462 
463 
464 
465 /*
466  *
467  * Init VMnc decoder
468  *
469  */
471 {
472  VmncContext * const c = avctx->priv_data;
473 
474  c->avctx = avctx;
475 
476  c->width = avctx->width;
477  c->height = avctx->height;
478 
479  c->bpp = avctx->bits_per_coded_sample;
480  c->bpp2 = c->bpp/8;
482 
483  switch(c->bpp){
484  case 8:
485  avctx->pix_fmt = AV_PIX_FMT_PAL8;
486  break;
487  case 16:
488  avctx->pix_fmt = AV_PIX_FMT_RGB555;
489  break;
490  case 32:
491  avctx->pix_fmt = AV_PIX_FMT_RGB32;
492  break;
493  default:
494  av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", c->bpp);
495  return AVERROR_INVALIDDATA;
496  }
497 
499 
500  return 0;
501 }
502 
503 
504 
505 /*
506  *
507  * Uninit VMnc decoder
508  *
509  */
511 {
512  VmncContext * const c = avctx->priv_data;
513 
514  av_frame_unref(&c->pic);
515 
516  av_free(c->curbits);
517  av_free(c->curmask);
518  av_free(c->screendta);
519  return 0;
520 }
521 
523  .name = "vmnc",
524  .type = AVMEDIA_TYPE_VIDEO,
525  .id = AV_CODEC_ID_VMNC,
526  .priv_data_size = sizeof(VmncContext),
527  .init = decode_init,
528  .close = decode_end,
529  .decode = decode_frame,
530  .capabilities = CODEC_CAP_DR1,
531  .long_name = NULL_IF_CONFIG_SMALL("VMware Screen Codec / VMware Video"),
532 };