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