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.

557 lines
17KB

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