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.

586 lines
18KB

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