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.

524 lines
15KB

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