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.

582 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. if (color)
  271. fg = vmnc_get_pixel(gb, bpp, c->bigendian);
  272. xy = bytestream2_get_byte(gb);
  273. wh = bytestream2_get_byte(gb);
  274. if ( (xy >> 4) + (wh >> 4) + 1 > w - i
  275. || (xy & 0xF) + (wh & 0xF)+1 > h - j) {
  276. av_log(c->avctx, AV_LOG_ERROR, "Rectangle outside picture\n");
  277. return AVERROR_INVALIDDATA;
  278. }
  279. paint_rect(dst2, xy >> 4, xy & 0xF,
  280. (wh>>4)+1, (wh & 0xF)+1, fg, bpp, stride);
  281. }
  282. }
  283. }
  284. dst += stride * 16;
  285. }
  286. return 0;
  287. }
  288. static void reset_buffers(VmncContext *c)
  289. {
  290. av_freep(&c->curbits);
  291. av_freep(&c->curmask);
  292. av_freep(&c->screendta);
  293. c->cur_w = c->cur_h = 0;
  294. c->cur_hx = c->cur_hy = 0;
  295. }
  296. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  297. AVPacket *avpkt)
  298. {
  299. const uint8_t *buf = avpkt->data;
  300. int buf_size = avpkt->size;
  301. VmncContext * const c = avctx->priv_data;
  302. GetByteContext *gb = &c->gb;
  303. uint8_t *outptr;
  304. int dx, dy, w, h, depth, enc, chunks, res, size_left, ret;
  305. if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
  306. return ret;
  307. bytestream2_init(gb, buf, buf_size);
  308. c->pic->key_frame = 0;
  309. c->pic->pict_type = AV_PICTURE_TYPE_P;
  310. // restore screen after cursor
  311. if (c->screendta) {
  312. int i;
  313. w = c->cur_w;
  314. if (c->width < c->cur_x + w)
  315. w = c->width - c->cur_x;
  316. h = c->cur_h;
  317. if (c->height < c->cur_y + h)
  318. h = c->height - c->cur_y;
  319. dx = c->cur_x;
  320. if (dx < 0) {
  321. w += dx;
  322. dx = 0;
  323. }
  324. dy = c->cur_y;
  325. if (dy < 0) {
  326. h += dy;
  327. dy = 0;
  328. }
  329. if ((w > 0) && (h > 0)) {
  330. outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0];
  331. for (i = 0; i < h; i++) {
  332. memcpy(outptr, c->screendta + i * c->cur_w * c->bpp2,
  333. w * c->bpp2);
  334. outptr += c->pic->linesize[0];
  335. }
  336. }
  337. }
  338. bytestream2_skip(gb, 2);
  339. chunks = bytestream2_get_be16(gb);
  340. while (chunks--) {
  341. if (bytestream2_get_bytes_left(gb) < 12) {
  342. av_log(avctx, AV_LOG_ERROR, "Premature end of data!\n");
  343. return -1;
  344. }
  345. dx = bytestream2_get_be16(gb);
  346. dy = bytestream2_get_be16(gb);
  347. w = bytestream2_get_be16(gb);
  348. h = bytestream2_get_be16(gb);
  349. enc = bytestream2_get_be32(gb);
  350. outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0];
  351. size_left = bytestream2_get_bytes_left(gb);
  352. switch (enc) {
  353. case MAGIC_WMVd: // cursor
  354. if (w*(int64_t)h*c->bpp2 > INT_MAX/2 - 2) {
  355. av_log(avctx, AV_LOG_ERROR, "dimensions too large\n");
  356. return AVERROR_INVALIDDATA;
  357. }
  358. if (size_left < 2 + w * h * c->bpp2 * 2) {
  359. av_log(avctx, AV_LOG_ERROR,
  360. "Premature end of data! (need %i got %i)\n",
  361. 2 + w * h * c->bpp2 * 2, size_left);
  362. return AVERROR_INVALIDDATA;
  363. }
  364. bytestream2_skip(gb, 2);
  365. c->cur_w = w;
  366. c->cur_h = h;
  367. c->cur_hx = dx;
  368. c->cur_hy = dy;
  369. if ((c->cur_hx > c->cur_w) || (c->cur_hy > c->cur_h)) {
  370. av_log(avctx, AV_LOG_ERROR,
  371. "Cursor hot spot is not in image: "
  372. "%ix%i of %ix%i cursor size\n",
  373. c->cur_hx, c->cur_hy, c->cur_w, c->cur_h);
  374. c->cur_hx = c->cur_hy = 0;
  375. }
  376. if (c->cur_w * c->cur_h >= INT_MAX / c->bpp2) {
  377. reset_buffers(c);
  378. return AVERROR(EINVAL);
  379. } else {
  380. int screen_size = c->cur_w * c->cur_h * c->bpp2;
  381. if ((ret = av_reallocp(&c->curbits, screen_size)) < 0 ||
  382. (ret = av_reallocp(&c->curmask, screen_size)) < 0 ||
  383. (ret = av_reallocp(&c->screendta, screen_size)) < 0) {
  384. reset_buffers(c);
  385. return ret;
  386. }
  387. }
  388. load_cursor(c);
  389. break;
  390. case MAGIC_WMVe: // unknown
  391. bytestream2_skip(gb, 2);
  392. break;
  393. case MAGIC_WMVf: // update cursor position
  394. c->cur_x = dx - c->cur_hx;
  395. c->cur_y = dy - c->cur_hy;
  396. break;
  397. case MAGIC_WMVg: // unknown
  398. bytestream2_skip(gb, 10);
  399. break;
  400. case MAGIC_WMVh: // unknown
  401. bytestream2_skip(gb, 4);
  402. break;
  403. case MAGIC_WMVi: // ServerInitialization struct
  404. c->pic->key_frame = 1;
  405. c->pic->pict_type = AV_PICTURE_TYPE_I;
  406. depth = bytestream2_get_byte(gb);
  407. if (depth != c->bpp) {
  408. av_log(avctx, AV_LOG_INFO,
  409. "Depth mismatch. Container %i bpp, "
  410. "Frame data: %i bpp\n",
  411. c->bpp, depth);
  412. }
  413. bytestream2_skip(gb, 1);
  414. c->bigendian = bytestream2_get_byte(gb);
  415. if (c->bigendian & (~1)) {
  416. av_log(avctx, AV_LOG_INFO,
  417. "Invalid header: bigendian flag = %i\n", c->bigendian);
  418. return AVERROR_INVALIDDATA;
  419. }
  420. //skip the rest of pixel format data
  421. bytestream2_skip(gb, 13);
  422. break;
  423. case MAGIC_WMVj: // unknown
  424. bytestream2_skip(gb, 2);
  425. break;
  426. case 0x00000000: // raw rectangle data
  427. if ((dx + w > c->width) || (dy + h > c->height)) {
  428. av_log(avctx, AV_LOG_ERROR,
  429. "Incorrect frame size: %ix%i+%ix%i of %ix%i\n",
  430. w, h, dx, dy, c->width, c->height);
  431. return AVERROR_INVALIDDATA;
  432. }
  433. if (size_left < w * h * c->bpp2) {
  434. av_log(avctx, AV_LOG_ERROR,
  435. "Premature end of data! (need %i got %i)\n",
  436. w * h * c->bpp2, size_left);
  437. return AVERROR_INVALIDDATA;
  438. }
  439. paint_raw(outptr, w, h, gb, c->bpp2, c->bigendian,
  440. c->pic->linesize[0]);
  441. break;
  442. case 0x00000005: // HexTile encoded rectangle
  443. if ((dx + w > c->width) || (dy + h > c->height)) {
  444. av_log(avctx, AV_LOG_ERROR,
  445. "Incorrect frame size: %ix%i+%ix%i of %ix%i\n",
  446. w, h, dx, dy, c->width, c->height);
  447. return AVERROR_INVALIDDATA;
  448. }
  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. };