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.

588 lines
18KB

  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. int rect_x, rect_y, rect_w, rect_h;
  271. if (color)
  272. fg = vmnc_get_pixel(gb, bpp, c->bigendian);
  273. xy = bytestream2_get_byte(gb);
  274. wh = bytestream2_get_byte(gb);
  275. rect_x = xy >> 4;
  276. rect_y = xy & 0xF;
  277. rect_w = (wh >> 4) + 1;
  278. rect_h = (wh & 0xF) + 1;
  279. if (rect_x + rect_w > bw || rect_y + rect_h > bh) {
  280. av_log(c->avctx, AV_LOG_ERROR, "Invalid subrect\n");
  281. return AVERROR_INVALIDDATA;
  282. }
  283. paint_rect(dst2, rect_x, rect_y,
  284. rect_w, rect_h, fg, bpp, stride);
  285. }
  286. }
  287. }
  288. dst += stride * 16;
  289. }
  290. return 0;
  291. }
  292. static void reset_buffers(VmncContext *c)
  293. {
  294. av_freep(&c->curbits);
  295. av_freep(&c->curmask);
  296. av_freep(&c->screendta);
  297. c->cur_w = c->cur_h = 0;
  298. }
  299. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  300. AVPacket *avpkt)
  301. {
  302. const uint8_t *buf = avpkt->data;
  303. int buf_size = avpkt->size;
  304. VmncContext * const c = avctx->priv_data;
  305. GetByteContext *gb = &c->gb;
  306. uint8_t *outptr;
  307. int dx, dy, w, h, depth, enc, chunks, res, size_left, ret;
  308. if ((ret = ff_reget_buffer(avctx, c->pic)) < 0) {
  309. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  310. return ret;
  311. }
  312. bytestream2_init(gb, buf, buf_size);
  313. c->pic->key_frame = 0;
  314. c->pic->pict_type = AV_PICTURE_TYPE_P;
  315. // restore screen after cursor
  316. if (c->screendta) {
  317. int i;
  318. w = c->cur_w;
  319. if (c->width < c->cur_x + w)
  320. w = c->width - c->cur_x;
  321. h = c->cur_h;
  322. if (c->height < c->cur_y + h)
  323. h = c->height - c->cur_y;
  324. dx = c->cur_x;
  325. if (dx < 0) {
  326. w += dx;
  327. dx = 0;
  328. }
  329. dy = c->cur_y;
  330. if (dy < 0) {
  331. h += dy;
  332. dy = 0;
  333. }
  334. if ((w > 0) && (h > 0)) {
  335. outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0];
  336. for (i = 0; i < h; i++) {
  337. memcpy(outptr, c->screendta + i * c->cur_w * c->bpp2,
  338. w * c->bpp2);
  339. outptr += c->pic->linesize[0];
  340. }
  341. }
  342. }
  343. bytestream2_skip(gb, 2);
  344. chunks = bytestream2_get_be16(gb);
  345. while (chunks--) {
  346. dx = bytestream2_get_be16(gb);
  347. dy = bytestream2_get_be16(gb);
  348. w = bytestream2_get_be16(gb);
  349. h = bytestream2_get_be16(gb);
  350. enc = bytestream2_get_be32(gb);
  351. outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0];
  352. size_left = bytestream2_get_bytes_left(gb);
  353. switch (enc) {
  354. case MAGIC_WMVd: // cursor
  355. if (size_left < 2 + w * h * c->bpp2 * 2) {
  356. av_log(avctx, AV_LOG_ERROR,
  357. "Premature end of data! (need %i got %i)\n",
  358. 2 + w * h * c->bpp2 * 2, size_left);
  359. return AVERROR_INVALIDDATA;
  360. }
  361. bytestream2_skip(gb, 2);
  362. c->cur_w = w;
  363. c->cur_h = h;
  364. c->cur_hx = dx;
  365. c->cur_hy = dy;
  366. if ((c->cur_hx > c->cur_w) || (c->cur_hy > c->cur_h)) {
  367. av_log(avctx, AV_LOG_ERROR,
  368. "Cursor hot spot is not in image: "
  369. "%ix%i of %ix%i cursor size\n",
  370. c->cur_hx, c->cur_hy, c->cur_w, c->cur_h);
  371. c->cur_hx = c->cur_hy = 0;
  372. }
  373. if (c->cur_w * c->cur_h >= INT_MAX / c->bpp2) {
  374. reset_buffers(c);
  375. return AVERROR(EINVAL);
  376. } else {
  377. int screen_size = c->cur_w * c->cur_h * c->bpp2;
  378. if ((ret = av_reallocp(&c->curbits, screen_size)) < 0 ||
  379. (ret = av_reallocp(&c->curmask, screen_size)) < 0 ||
  380. (ret = av_reallocp(&c->screendta, screen_size)) < 0) {
  381. reset_buffers(c);
  382. return ret;
  383. }
  384. }
  385. load_cursor(c);
  386. break;
  387. case MAGIC_WMVe: // unknown
  388. bytestream2_skip(gb, 2);
  389. break;
  390. case MAGIC_WMVf: // update cursor position
  391. c->cur_x = dx - c->cur_hx;
  392. c->cur_y = dy - c->cur_hy;
  393. break;
  394. case MAGIC_WMVg: // unknown
  395. bytestream2_skip(gb, 10);
  396. break;
  397. case MAGIC_WMVh: // unknown
  398. bytestream2_skip(gb, 4);
  399. break;
  400. case MAGIC_WMVi: // ServerInitialization struct
  401. c->pic->key_frame = 1;
  402. c->pic->pict_type = AV_PICTURE_TYPE_I;
  403. depth = bytestream2_get_byte(gb);
  404. if (depth != c->bpp) {
  405. av_log(avctx, AV_LOG_WARNING, "Depth mismatch. "
  406. "Container %i bpp / Codec %i bpp\n", c->bpp, depth);
  407. if (depth != 8 && depth != 16 && depth != 32) {
  408. av_log(avctx, AV_LOG_ERROR,
  409. "Unsupported codec bitdepth %i\n", depth);
  410. return AVERROR_INVALIDDATA;
  411. }
  412. /* reset values */
  413. c->bpp = depth;
  414. c->bpp2 = c->bpp / 8;
  415. }
  416. bytestream2_skip(gb, 1);
  417. c->bigendian = bytestream2_get_byte(gb);
  418. if (c->bigendian & (~1)) {
  419. av_log(avctx, AV_LOG_INFO,
  420. "Invalid header: bigendian flag = %i\n", c->bigendian);
  421. return AVERROR_INVALIDDATA;
  422. }
  423. //skip the rest of pixel format data
  424. bytestream2_skip(gb, 13);
  425. break;
  426. case MAGIC_WMVj: // unknown
  427. bytestream2_skip(gb, 2);
  428. break;
  429. case 0x00000000: // raw rectangle data
  430. if ((dx + w > c->width) || (dy + h > c->height)) {
  431. av_log(avctx, AV_LOG_ERROR,
  432. "Incorrect frame size: %ix%i+%ix%i of %ix%i\n",
  433. w, h, dx, dy, c->width, c->height);
  434. return AVERROR_INVALIDDATA;
  435. }
  436. if (size_left < w * h * c->bpp2) {
  437. av_log(avctx, AV_LOG_ERROR,
  438. "Premature end of data! (need %i got %i)\n",
  439. w * h * c->bpp2, size_left);
  440. return AVERROR_INVALIDDATA;
  441. }
  442. paint_raw(outptr, w, h, gb, c->bpp2, c->bigendian,
  443. c->pic->linesize[0]);
  444. break;
  445. case 0x00000005: // HexTile encoded rectangle
  446. if ((dx + w > c->width) || (dy + h > c->height)) {
  447. av_log(avctx, AV_LOG_ERROR,
  448. "Incorrect frame size: %ix%i+%ix%i of %ix%i\n",
  449. w, h, dx, dy, c->width, c->height);
  450. return AVERROR_INVALIDDATA;
  451. }
  452. res = decode_hextile(c, outptr, gb, w, h, c->pic->linesize[0]);
  453. if (res < 0)
  454. return res;
  455. break;
  456. default:
  457. av_log(avctx, AV_LOG_ERROR, "Unsupported block type 0x%08X\n", enc);
  458. chunks = 0; // leave chunks decoding loop
  459. }
  460. }
  461. if (c->screendta) {
  462. int i;
  463. // save screen data before painting cursor
  464. w = c->cur_w;
  465. if (c->width < c->cur_x + w)
  466. w = c->width - c->cur_x;
  467. h = c->cur_h;
  468. if (c->height < c->cur_y + h)
  469. h = c->height - c->cur_y;
  470. dx = c->cur_x;
  471. if (dx < 0) {
  472. w += dx;
  473. dx = 0;
  474. }
  475. dy = c->cur_y;
  476. if (dy < 0) {
  477. h += dy;
  478. dy = 0;
  479. }
  480. if ((w > 0) && (h > 0)) {
  481. outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0];
  482. for (i = 0; i < h; i++) {
  483. memcpy(c->screendta + i * c->cur_w * c->bpp2, outptr,
  484. w * c->bpp2);
  485. outptr += c->pic->linesize[0];
  486. }
  487. outptr = c->pic->data[0];
  488. put_cursor(outptr, c->pic->linesize[0], c, c->cur_x, c->cur_y);
  489. }
  490. }
  491. *got_frame = 1;
  492. if ((ret = av_frame_ref(data, c->pic)) < 0)
  493. return ret;
  494. /* always report that the buffer was completely consumed */
  495. return buf_size;
  496. }
  497. static av_cold int decode_init(AVCodecContext *avctx)
  498. {
  499. VmncContext * const c = avctx->priv_data;
  500. c->avctx = avctx;
  501. c->width = avctx->width;
  502. c->height = avctx->height;
  503. c->bpp = avctx->bits_per_coded_sample;
  504. c->bpp2 = c->bpp / 8;
  505. switch (c->bpp) {
  506. case 8:
  507. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  508. break;
  509. case 16:
  510. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  511. break;
  512. case 24:
  513. /* 24 bits is not technically supported, but some clients might
  514. * mistakenly set it -- delay the actual check until decode_frame() */
  515. case 32:
  516. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  517. break;
  518. default:
  519. av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", c->bpp);
  520. return AVERROR_INVALIDDATA;
  521. }
  522. c->pic = av_frame_alloc();
  523. if (!c->pic)
  524. return AVERROR(ENOMEM);
  525. return 0;
  526. }
  527. static av_cold int decode_end(AVCodecContext *avctx)
  528. {
  529. VmncContext * const c = avctx->priv_data;
  530. av_frame_free(&c->pic);
  531. av_free(c->curbits);
  532. av_free(c->curmask);
  533. av_free(c->screendta);
  534. return 0;
  535. }
  536. AVCodec ff_vmnc_decoder = {
  537. .name = "vmnc",
  538. .long_name = NULL_IF_CONFIG_SMALL("VMware Screen Codec / VMware Video"),
  539. .type = AVMEDIA_TYPE_VIDEO,
  540. .id = AV_CODEC_ID_VMNC,
  541. .priv_data_size = sizeof(VmncContext),
  542. .init = decode_init,
  543. .close = decode_end,
  544. .decode = decode_frame,
  545. .capabilities = AV_CODEC_CAP_DR1,
  546. };