You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

576 lines
17KB

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