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.

535 lines
16KB

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