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.

904 lines
28KB

  1. /*
  2. * Go2Webinar decoder
  3. * Copyright (c) 2012 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. * Go2Webinar decoder
  24. */
  25. #include <zlib.h>
  26. #include "libavutil/intreadwrite.h"
  27. #include "avcodec.h"
  28. #include "bytestream.h"
  29. #include "dsputil.h"
  30. #include "get_bits.h"
  31. #include "internal.h"
  32. #include "mjpeg.h"
  33. enum ChunkType {
  34. DISPLAY_INFO = 0xC8,
  35. TILE_DATA,
  36. CURSOR_POS,
  37. CURSOR_SHAPE,
  38. CHUNK_CC,
  39. CHUNK_CD
  40. };
  41. enum Compression {
  42. COMPR_EPIC_J_B = 2,
  43. COMPR_KEMPF_J_B,
  44. };
  45. static const uint8_t luma_quant[64] = {
  46. 8, 6, 5, 8, 12, 20, 26, 31,
  47. 6, 6, 7, 10, 13, 29, 30, 28,
  48. 7, 7, 8, 12, 20, 29, 35, 28,
  49. 7, 9, 11, 15, 26, 44, 40, 31,
  50. 9, 11, 19, 28, 34, 55, 52, 39,
  51. 12, 18, 28, 32, 41, 52, 57, 46,
  52. 25, 32, 39, 44, 52, 61, 60, 51,
  53. 36, 46, 48, 49, 56, 50, 52, 50
  54. };
  55. static const uint8_t chroma_quant[64] = {
  56. 9, 9, 12, 24, 50, 50, 50, 50,
  57. 9, 11, 13, 33, 50, 50, 50, 50,
  58. 12, 13, 28, 50, 50, 50, 50, 50,
  59. 24, 33, 50, 50, 50, 50, 50, 50,
  60. 50, 50, 50, 50, 50, 50, 50, 50,
  61. 50, 50, 50, 50, 50, 50, 50, 50,
  62. 50, 50, 50, 50, 50, 50, 50, 50,
  63. 50, 50, 50, 50, 50, 50, 50, 50,
  64. };
  65. typedef struct JPGContext {
  66. DSPContext dsp;
  67. ScanTable scantable;
  68. VLC dc_vlc[2], ac_vlc[2];
  69. int prev_dc[3];
  70. DECLARE_ALIGNED(16, int16_t, block)[6][64];
  71. uint8_t *buf;
  72. } JPGContext;
  73. typedef struct G2MContext {
  74. JPGContext jc;
  75. int version;
  76. int compression;
  77. int width, height, bpp;
  78. int tile_width, tile_height;
  79. int tiles_x, tiles_y, tile_x, tile_y;
  80. int got_header;
  81. uint8_t *framebuf;
  82. int framebuf_stride, old_width, old_height;
  83. uint8_t *synth_tile, *jpeg_tile;
  84. int tile_stride, old_tile_w, old_tile_h;
  85. uint8_t *kempf_buf, *kempf_flags;
  86. uint8_t *cursor;
  87. int cursor_stride;
  88. int cursor_fmt;
  89. int cursor_w, cursor_h, cursor_x, cursor_y;
  90. int cursor_hot_x, cursor_hot_y;
  91. } G2MContext;
  92. static av_cold int build_vlc(VLC *vlc, const uint8_t *bits_table,
  93. const uint8_t *val_table, int nb_codes,
  94. int is_ac)
  95. {
  96. uint8_t huff_size[256] = { 0 };
  97. uint16_t huff_code[256];
  98. uint16_t huff_sym[256];
  99. int i;
  100. ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  101. for (i = 0; i < 256; i++)
  102. huff_sym[i] = i + 16 * is_ac;
  103. if (is_ac)
  104. huff_sym[0] = 16 * 256;
  105. return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
  106. huff_code, 2, 2, huff_sym, 2, 2, 0);
  107. }
  108. static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c)
  109. {
  110. int ret;
  111. ret = build_vlc(&c->dc_vlc[0], avpriv_mjpeg_bits_dc_luminance,
  112. avpriv_mjpeg_val_dc, 12, 0);
  113. if (ret)
  114. return ret;
  115. ret = build_vlc(&c->dc_vlc[1], avpriv_mjpeg_bits_dc_chrominance,
  116. avpriv_mjpeg_val_dc, 12, 0);
  117. if (ret)
  118. return ret;
  119. ret = build_vlc(&c->ac_vlc[0], avpriv_mjpeg_bits_ac_luminance,
  120. avpriv_mjpeg_val_ac_luminance, 251, 1);
  121. if (ret)
  122. return ret;
  123. ret = build_vlc(&c->ac_vlc[1], avpriv_mjpeg_bits_ac_chrominance,
  124. avpriv_mjpeg_val_ac_chrominance, 251, 1);
  125. if (ret)
  126. return ret;
  127. ff_dsputil_init(&c->dsp, avctx);
  128. ff_init_scantable(c->dsp.idct_permutation, &c->scantable,
  129. ff_zigzag_direct);
  130. return 0;
  131. }
  132. static av_cold void jpg_free_context(JPGContext *ctx)
  133. {
  134. int i;
  135. for (i = 0; i < 2; i++) {
  136. ff_free_vlc(&ctx->dc_vlc[i]);
  137. ff_free_vlc(&ctx->ac_vlc[i]);
  138. }
  139. av_freep(&ctx->buf);
  140. }
  141. static void jpg_unescape(const uint8_t *src, int src_size,
  142. uint8_t *dst, int *dst_size)
  143. {
  144. const uint8_t *src_end = src + src_size;
  145. uint8_t *dst_start = dst;
  146. while (src < src_end) {
  147. uint8_t x = *src++;
  148. *dst++ = x;
  149. if (x == 0xFF && !*src)
  150. src++;
  151. }
  152. *dst_size = dst - dst_start;
  153. }
  154. static int jpg_decode_block(JPGContext *c, GetBitContext *gb,
  155. int plane, int16_t *block)
  156. {
  157. int dc, val, pos;
  158. const int is_chroma = !!plane;
  159. const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant;
  160. c->dsp.clear_block(block);
  161. dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 3);
  162. if (dc < 0)
  163. return AVERROR_INVALIDDATA;
  164. if (dc)
  165. dc = get_xbits(gb, dc);
  166. dc = dc * qmat[0] + c->prev_dc[plane];
  167. block[0] = dc;
  168. c->prev_dc[plane] = dc;
  169. pos = 0;
  170. while (pos < 63) {
  171. val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 3);
  172. if (val < 0)
  173. return AVERROR_INVALIDDATA;
  174. pos += val >> 4;
  175. val &= 0xF;
  176. if (pos > 63)
  177. return val ? AVERROR_INVALIDDATA : 0;
  178. if (val) {
  179. int nbits = val;
  180. val = get_xbits(gb, nbits);
  181. val *= qmat[ff_zigzag_direct[pos]];
  182. block[c->scantable.permutated[pos]] = val;
  183. }
  184. }
  185. return 0;
  186. }
  187. static inline void yuv2rgb(uint8_t *out, int Y, int U, int V)
  188. {
  189. out[0] = av_clip_uint8(Y + ( 91881 * V + 32768 >> 16));
  190. out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
  191. out[2] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
  192. }
  193. static int jpg_decode_data(JPGContext *c, int width, int height,
  194. const uint8_t *src, int src_size,
  195. uint8_t *dst, int dst_stride,
  196. const uint8_t *mask, int mask_stride, int num_mbs,
  197. int swapuv)
  198. {
  199. GetBitContext gb;
  200. int mb_w, mb_h, mb_x, mb_y, i, j;
  201. int bx, by;
  202. int unesc_size;
  203. int ret;
  204. if ((ret = av_reallocp(&c->buf,
  205. src_size + FF_INPUT_BUFFER_PADDING_SIZE)) < 0)
  206. return ret;
  207. jpg_unescape(src, src_size, c->buf, &unesc_size);
  208. memset(c->buf + unesc_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  209. init_get_bits(&gb, c->buf, unesc_size * 8);
  210. width = FFALIGN(width, 16);
  211. mb_w = width >> 4;
  212. mb_h = (height + 15) >> 4;
  213. if (!num_mbs)
  214. num_mbs = mb_w * mb_h * 4;
  215. for (i = 0; i < 3; i++)
  216. c->prev_dc[i] = 1024;
  217. bx = by = 0;
  218. c->dsp.clear_blocks(c->block[0]);
  219. for (mb_y = 0; mb_y < mb_h; mb_y++) {
  220. for (mb_x = 0; mb_x < mb_w; mb_x++) {
  221. if (mask && !mask[mb_x * 2] && !mask[mb_x * 2 + 1] &&
  222. !mask[mb_x * 2 + mask_stride] &&
  223. !mask[mb_x * 2 + 1 + mask_stride]) {
  224. bx += 16;
  225. continue;
  226. }
  227. for (j = 0; j < 2; j++) {
  228. for (i = 0; i < 2; i++) {
  229. if (mask && !mask[mb_x * 2 + i + j * mask_stride])
  230. continue;
  231. num_mbs--;
  232. if ((ret = jpg_decode_block(c, &gb, 0,
  233. c->block[i + j * 2])) != 0)
  234. return ret;
  235. c->dsp.idct(c->block[i + j * 2]);
  236. }
  237. }
  238. for (i = 1; i < 3; i++) {
  239. if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0)
  240. return ret;
  241. c->dsp.idct(c->block[i + 3]);
  242. }
  243. for (j = 0; j < 16; j++) {
  244. uint8_t *out = dst + bx * 3 + (by + j) * dst_stride;
  245. for (i = 0; i < 16; i++) {
  246. int Y, U, V;
  247. Y = c->block[(j >> 3) * 2 + (i >> 3)][(i & 7) + (j & 7) * 8];
  248. U = c->block[4 ^ swapuv][(i >> 1) + (j >> 1) * 8] - 128;
  249. V = c->block[5 ^ swapuv][(i >> 1) + (j >> 1) * 8] - 128;
  250. yuv2rgb(out + i * 3, Y, U, V);
  251. }
  252. }
  253. if (!num_mbs)
  254. return 0;
  255. bx += 16;
  256. }
  257. bx = 0;
  258. by += 16;
  259. if (mask)
  260. mask += mask_stride * 2;
  261. }
  262. return 0;
  263. }
  264. static void kempf_restore_buf(const uint8_t *src, int len,
  265. uint8_t *dst, int stride,
  266. const uint8_t *jpeg_tile, int tile_stride,
  267. int width, int height,
  268. const uint8_t *pal, int npal, int tidx)
  269. {
  270. GetBitContext gb;
  271. int i, j, nb, col;
  272. init_get_bits(&gb, src, len * 8);
  273. if (npal <= 2) nb = 1;
  274. else if (npal <= 4) nb = 2;
  275. else if (npal <= 16) nb = 4;
  276. else nb = 8;
  277. for (j = 0; j < height; j++, dst += stride, jpeg_tile += tile_stride) {
  278. if (get_bits(&gb, 8))
  279. continue;
  280. for (i = 0; i < width; i++) {
  281. col = get_bits(&gb, nb);
  282. if (col != tidx)
  283. memcpy(dst + i * 3, pal + col * 3, 3);
  284. else
  285. memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
  286. }
  287. }
  288. }
  289. static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y,
  290. const uint8_t *src, int src_size)
  291. {
  292. int width, height;
  293. int hdr, zsize, npal, tidx = -1, ret;
  294. int i, j;
  295. const uint8_t *src_end = src + src_size;
  296. uint8_t pal[768], transp[3];
  297. uLongf dlen = (c->tile_width + 1) * c->tile_height;
  298. int sub_type;
  299. int nblocks, cblocks, bstride;
  300. int bits, bitbuf, coded;
  301. uint8_t *dst = c->framebuf + tile_x * c->tile_width * 3 +
  302. tile_y * c->tile_height * c->framebuf_stride;
  303. if (src_size < 2)
  304. return AVERROR_INVALIDDATA;
  305. width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
  306. height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
  307. hdr = *src++;
  308. sub_type = hdr >> 5;
  309. if (sub_type == 0) {
  310. int j;
  311. memcpy(transp, src, 3);
  312. src += 3;
  313. for (j = 0; j < height; j++, dst += c->framebuf_stride)
  314. for (i = 0; i < width; i++)
  315. memcpy(dst + i * 3, transp, 3);
  316. return 0;
  317. } else if (sub_type == 1) {
  318. return jpg_decode_data(&c->jc, width, height, src, src_end - src,
  319. dst, c->framebuf_stride, NULL, 0, 0, 0);
  320. }
  321. if (sub_type != 2) {
  322. memcpy(transp, src, 3);
  323. src += 3;
  324. }
  325. npal = *src++ + 1;
  326. memcpy(pal, src, npal * 3);
  327. src += npal * 3;
  328. if (sub_type != 2) {
  329. for (i = 0; i < npal; i++) {
  330. if (!memcmp(pal + i * 3, transp, 3)) {
  331. tidx = i;
  332. break;
  333. }
  334. }
  335. }
  336. if (src_end - src < 2)
  337. return 0;
  338. zsize = (src[0] << 8) | src[1];
  339. src += 2;
  340. if (src_end - src < zsize)
  341. return AVERROR_INVALIDDATA;
  342. ret = uncompress(c->kempf_buf, &dlen, src, zsize);
  343. if (ret)
  344. return AVERROR_INVALIDDATA;
  345. src += zsize;
  346. if (sub_type == 2) {
  347. kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
  348. NULL, 0, width, height, pal, npal, tidx);
  349. return 0;
  350. }
  351. nblocks = *src++ + 1;
  352. cblocks = 0;
  353. bstride = FFALIGN(width, 16) >> 3;
  354. // blocks are coded LSB and we need normal bitreader for JPEG data
  355. bits = 0;
  356. for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
  357. for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
  358. if (!bits) {
  359. bitbuf = *src++;
  360. bits = 8;
  361. }
  362. coded = bitbuf & 1;
  363. bits--;
  364. bitbuf >>= 1;
  365. cblocks += coded;
  366. if (cblocks > nblocks)
  367. return AVERROR_INVALIDDATA;
  368. c->kempf_flags[j * 2 + i * 2 * bstride] =
  369. c->kempf_flags[j * 2 + 1 + i * 2 * bstride] =
  370. c->kempf_flags[j * 2 + (i * 2 + 1) * bstride] =
  371. c->kempf_flags[j * 2 + 1 + (i * 2 + 1) * bstride] = coded;
  372. }
  373. }
  374. memset(c->jpeg_tile, 0, c->tile_stride * height);
  375. jpg_decode_data(&c->jc, width, height, src, src_end - src,
  376. c->jpeg_tile, c->tile_stride,
  377. c->kempf_flags, bstride, nblocks * 4, 0);
  378. kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
  379. c->jpeg_tile, c->tile_stride,
  380. width, height, pal, npal, tidx);
  381. return 0;
  382. }
  383. static int g2m_init_buffers(G2MContext *c)
  384. {
  385. int aligned_height;
  386. if (!c->framebuf || c->old_width < c->width || c->old_height < c->height) {
  387. c->framebuf_stride = FFALIGN(c->width * 3, 16);
  388. aligned_height = FFALIGN(c->height, 16);
  389. av_free(c->framebuf);
  390. c->framebuf = av_mallocz(c->framebuf_stride * aligned_height);
  391. if (!c->framebuf)
  392. return AVERROR(ENOMEM);
  393. }
  394. if (!c->synth_tile || !c->jpeg_tile ||
  395. c->old_tile_w < c->tile_width ||
  396. c->old_tile_h < c->tile_height) {
  397. c->tile_stride = FFALIGN(c->tile_width * 3, 16);
  398. aligned_height = FFALIGN(c->tile_height, 16);
  399. av_free(c->synth_tile);
  400. av_free(c->jpeg_tile);
  401. av_free(c->kempf_buf);
  402. av_free(c->kempf_flags);
  403. c->synth_tile = av_mallocz(c->tile_stride * aligned_height);
  404. c->jpeg_tile = av_mallocz(c->tile_stride * aligned_height);
  405. c->kempf_buf = av_mallocz((c->tile_width + 1) * aligned_height
  406. + FF_INPUT_BUFFER_PADDING_SIZE);
  407. c->kempf_flags = av_mallocz( c->tile_width * aligned_height);
  408. if (!c->synth_tile || !c->jpeg_tile ||
  409. !c->kempf_buf || !c->kempf_flags)
  410. return AVERROR(ENOMEM);
  411. }
  412. return 0;
  413. }
  414. static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c,
  415. GetByteContext *gb)
  416. {
  417. int i, j, k;
  418. uint8_t *dst;
  419. uint32_t bits;
  420. uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
  421. uint32_t cursor_hot_x, cursor_hot_y;
  422. int cursor_fmt, err;
  423. cur_size = bytestream2_get_be32(gb);
  424. cursor_w = bytestream2_get_byte(gb);
  425. cursor_h = bytestream2_get_byte(gb);
  426. cursor_hot_x = bytestream2_get_byte(gb);
  427. cursor_hot_y = bytestream2_get_byte(gb);
  428. cursor_fmt = bytestream2_get_byte(gb);
  429. cursor_stride = FFALIGN(cursor_w, 32) * 4;
  430. if (cursor_w < 1 || cursor_w > 256 ||
  431. cursor_h < 1 || cursor_h > 256) {
  432. av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %dx%d\n",
  433. cursor_w, cursor_h);
  434. return AVERROR_INVALIDDATA;
  435. }
  436. if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
  437. av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %d,%d\n",
  438. cursor_hot_x, cursor_hot_y);
  439. cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);
  440. cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);
  441. }
  442. if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||
  443. c->cursor_w * c->cursor_h / 4 > cur_size) {
  444. av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %d/%d\n",
  445. cur_size, bytestream2_get_bytes_left(gb));
  446. return AVERROR_INVALIDDATA;
  447. }
  448. if (cursor_fmt != 1 && cursor_fmt != 32) {
  449. avpriv_report_missing_feature(avctx, "Cursor format %d",
  450. cursor_fmt);
  451. return AVERROR_PATCHWELCOME;
  452. }
  453. if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
  454. av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
  455. return err;
  456. }
  457. c->cursor_w = cursor_w;
  458. c->cursor_h = cursor_h;
  459. c->cursor_hot_x = cursor_hot_x;
  460. c->cursor_hot_y = cursor_hot_y;
  461. c->cursor_fmt = cursor_fmt;
  462. c->cursor_stride = cursor_stride;
  463. dst = c->cursor;
  464. switch (c->cursor_fmt) {
  465. case 1: // old monochrome
  466. for (j = 0; j < c->cursor_h; j++) {
  467. for (i = 0; i < c->cursor_w; i += 32) {
  468. bits = bytestream2_get_be32(gb);
  469. for (k = 0; k < 32; k++) {
  470. dst[0] = !!(bits & 0x80000000);
  471. dst += 4;
  472. bits <<= 1;
  473. }
  474. }
  475. dst += c->cursor_stride - c->cursor_w * 4;
  476. }
  477. dst = c->cursor;
  478. for (j = 0; j < c->cursor_h; j++) {
  479. for (i = 0; i < c->cursor_w; i += 32) {
  480. bits = bytestream2_get_be32(gb);
  481. for (k = 0; k < 32; k++) {
  482. int mask_bit = !!(bits & 0x80000000);
  483. switch (dst[0] * 2 + mask_bit) {
  484. case 0:
  485. dst[0] = 0xFF;
  486. dst[1] = 0x00;
  487. dst[2] = 0x00;
  488. dst[3] = 0x00;
  489. break;
  490. case 1:
  491. dst[0] = 0xFF;
  492. dst[1] = 0xFF;
  493. dst[2] = 0xFF;
  494. dst[3] = 0xFF;
  495. break;
  496. default:
  497. dst[0] = 0x00;
  498. dst[1] = 0x00;
  499. dst[2] = 0x00;
  500. dst[3] = 0x00;
  501. }
  502. dst += 4;
  503. bits <<= 1;
  504. }
  505. }
  506. dst += c->cursor_stride - c->cursor_w * 4;
  507. }
  508. break;
  509. case 32: // full colour
  510. /* skip monochrome version of the cursor and decode RGBA instead */
  511. bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
  512. for (j = 0; j < c->cursor_h; j++) {
  513. for (i = 0; i < c->cursor_w; i++) {
  514. int val = bytestream2_get_be32(gb);
  515. *dst++ = val >> 0;
  516. *dst++ = val >> 8;
  517. *dst++ = val >> 16;
  518. *dst++ = val >> 24;
  519. }
  520. dst += c->cursor_stride - c->cursor_w * 4;
  521. }
  522. break;
  523. default:
  524. return AVERROR_PATCHWELCOME;
  525. }
  526. return 0;
  527. }
  528. #define APPLY_ALPHA(src, new, alpha) \
  529. src = (src * (256 - alpha) + new * alpha) >> 8
  530. static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
  531. {
  532. int i, j;
  533. int x, y, w, h;
  534. const uint8_t *cursor;
  535. if (!c->cursor)
  536. return;
  537. x = c->cursor_x - c->cursor_hot_x;
  538. y = c->cursor_y - c->cursor_hot_y;
  539. cursor = c->cursor;
  540. w = c->cursor_w;
  541. h = c->cursor_h;
  542. if (x + w > c->width)
  543. w = c->width - x;
  544. if (y + h > c->height)
  545. h = c->height - y;
  546. if (x < 0) {
  547. w += x;
  548. cursor += -x * 4;
  549. } else {
  550. dst += x * 3;
  551. }
  552. if (y < 0) {
  553. h += y;
  554. cursor += -y * c->cursor_stride;
  555. } else {
  556. dst += y * stride;
  557. }
  558. if (w < 0 || h < 0)
  559. return;
  560. for (j = 0; j < h; j++) {
  561. for (i = 0; i < w; i++) {
  562. uint8_t alpha = cursor[i * 4];
  563. APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
  564. APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
  565. APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
  566. }
  567. dst += stride;
  568. cursor += c->cursor_stride;
  569. }
  570. }
  571. static int g2m_decode_frame(AVCodecContext *avctx, void *data,
  572. int *got_picture_ptr, AVPacket *avpkt)
  573. {
  574. const uint8_t *buf = avpkt->data;
  575. int buf_size = avpkt->size;
  576. G2MContext *c = avctx->priv_data;
  577. AVFrame *pic = data;
  578. GetByteContext bc, tbc;
  579. int magic;
  580. int got_header = 0;
  581. uint32_t chunk_size, r_mask, g_mask, b_mask;
  582. int chunk_type, chunk_start;
  583. int i;
  584. int ret;
  585. if (buf_size < 12) {
  586. av_log(avctx, AV_LOG_ERROR,
  587. "Frame should have at least 12 bytes, got %d instead\n",
  588. buf_size);
  589. return AVERROR_INVALIDDATA;
  590. }
  591. bytestream2_init(&bc, buf, buf_size);
  592. magic = bytestream2_get_be32(&bc);
  593. if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
  594. (magic & 0xF) < 2 || (magic & 0xF) > 4) {
  595. av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
  596. return AVERROR_INVALIDDATA;
  597. }
  598. if ((magic & 0xF) != 4) {
  599. av_log(avctx, AV_LOG_ERROR, "G2M2 and G2M3 are not yet supported\n");
  600. return AVERROR(ENOSYS);
  601. }
  602. while (bytestream2_get_bytes_left(&bc) > 5) {
  603. chunk_size = bytestream2_get_le32(&bc) - 1;
  604. chunk_type = bytestream2_get_byte(&bc);
  605. chunk_start = bytestream2_tell(&bc);
  606. if (chunk_size > bytestream2_get_bytes_left(&bc)) {
  607. av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %d type %02X\n",
  608. chunk_size, chunk_type);
  609. break;
  610. }
  611. switch (chunk_type) {
  612. case DISPLAY_INFO:
  613. c->got_header = 0;
  614. if (chunk_size < 21) {
  615. av_log(avctx, AV_LOG_ERROR, "Invalid display info size %d\n",
  616. chunk_size);
  617. break;
  618. }
  619. c->width = bytestream2_get_be32(&bc);
  620. c->height = bytestream2_get_be32(&bc);
  621. if (c->width < 16 || c->width > avctx->width ||
  622. c->height < 16 || c->height > avctx->height) {
  623. av_log(avctx, AV_LOG_ERROR,
  624. "Invalid frame dimensions %dx%d\n",
  625. c->width, c->height);
  626. ret = AVERROR_INVALIDDATA;
  627. goto header_fail;
  628. }
  629. if (c->width != avctx->width || c->height != avctx->height)
  630. ff_set_dimensions(avctx, c->width, c->height);
  631. c->compression = bytestream2_get_be32(&bc);
  632. if (c->compression != 2 && c->compression != 3) {
  633. av_log(avctx, AV_LOG_ERROR,
  634. "Unknown compression method %d\n",
  635. c->compression);
  636. return AVERROR_PATCHWELCOME;
  637. }
  638. c->tile_width = bytestream2_get_be32(&bc);
  639. c->tile_height = bytestream2_get_be32(&bc);
  640. if (!c->tile_width || !c->tile_height ||
  641. ((c->tile_width | c->tile_height) & 0xF)) {
  642. av_log(avctx, AV_LOG_ERROR,
  643. "Invalid tile dimensions %dx%d\n",
  644. c->tile_width, c->tile_height);
  645. ret = AVERROR_INVALIDDATA;
  646. goto header_fail;
  647. }
  648. c->tiles_x = (c->width + c->tile_width - 1) / c->tile_width;
  649. c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
  650. c->bpp = bytestream2_get_byte(&bc);
  651. if (c->bpp == 32) {
  652. if (bytestream2_get_bytes_left(&bc) < 16 ||
  653. (chunk_size - 21) < 16) {
  654. av_log(avctx, AV_LOG_ERROR,
  655. "Display info: missing bitmasks!\n");
  656. return AVERROR_INVALIDDATA;
  657. }
  658. r_mask = bytestream2_get_be32(&bc);
  659. g_mask = bytestream2_get_be32(&bc);
  660. b_mask = bytestream2_get_be32(&bc);
  661. if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
  662. av_log(avctx, AV_LOG_ERROR,
  663. "Invalid or unsupported bitmasks: R=%X, G=%X, B=%X\n",
  664. r_mask, g_mask, b_mask);
  665. return AVERROR_PATCHWELCOME;
  666. }
  667. } else {
  668. avpriv_request_sample(avctx, "bpp=%d", c->bpp);
  669. return AVERROR_PATCHWELCOME;
  670. }
  671. if (g2m_init_buffers(c)) {
  672. ret = AVERROR(ENOMEM);
  673. goto header_fail;
  674. }
  675. got_header = 1;
  676. break;
  677. case TILE_DATA:
  678. if (!c->tiles_x || !c->tiles_y) {
  679. av_log(avctx, AV_LOG_WARNING,
  680. "No display info - skipping tile\n");
  681. break;
  682. }
  683. if (chunk_size < 2) {
  684. av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %d\n",
  685. chunk_size);
  686. break;
  687. }
  688. c->tile_x = bytestream2_get_byte(&bc);
  689. c->tile_y = bytestream2_get_byte(&bc);
  690. if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
  691. av_log(avctx, AV_LOG_ERROR,
  692. "Invalid tile pos %d,%d (in %dx%d grid)\n",
  693. c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
  694. break;
  695. }
  696. ret = 0;
  697. switch (c->compression) {
  698. case COMPR_EPIC_J_B:
  699. av_log(avctx, AV_LOG_ERROR,
  700. "ePIC j-b compression is not implemented yet\n");
  701. return AVERROR(ENOSYS);
  702. case COMPR_KEMPF_J_B:
  703. ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
  704. buf + bytestream2_tell(&bc),
  705. chunk_size - 2);
  706. break;
  707. }
  708. if (ret && c->framebuf)
  709. av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
  710. c->tile_x, c->tile_y);
  711. break;
  712. case CURSOR_POS:
  713. if (chunk_size < 5) {
  714. av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %d\n",
  715. chunk_size);
  716. break;
  717. }
  718. c->cursor_x = bytestream2_get_be16(&bc);
  719. c->cursor_y = bytestream2_get_be16(&bc);
  720. break;
  721. case CURSOR_SHAPE:
  722. if (chunk_size < 8) {
  723. av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %d\n",
  724. chunk_size);
  725. break;
  726. }
  727. bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
  728. chunk_size - 4);
  729. g2m_load_cursor(avctx, c, &tbc);
  730. break;
  731. case CHUNK_CC:
  732. case CHUNK_CD:
  733. break;
  734. default:
  735. av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02X\n",
  736. chunk_type);
  737. }
  738. /* navigate to next chunk */
  739. bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
  740. }
  741. if (got_header)
  742. c->got_header = 1;
  743. if (c->width && c->height) {
  744. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0) {
  745. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  746. return ret;
  747. }
  748. pic->key_frame = got_header;
  749. pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  750. for (i = 0; i < avctx->height; i++)
  751. memcpy(pic->data[0] + i * pic->linesize[0],
  752. c->framebuf + i * c->framebuf_stride,
  753. c->width * 3);
  754. g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);
  755. *got_picture_ptr = 1;
  756. }
  757. return buf_size;
  758. header_fail:
  759. c->width =
  760. c->height = 0;
  761. c->tiles_x =
  762. c->tiles_y = 0;
  763. return ret;
  764. }
  765. static av_cold int g2m_decode_init(AVCodecContext *avctx)
  766. {
  767. G2MContext *const c = avctx->priv_data;
  768. int ret;
  769. if ((ret = jpg_init(avctx, &c->jc)) != 0) {
  770. av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
  771. jpg_free_context(&c->jc);
  772. return AVERROR(ENOMEM);
  773. }
  774. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  775. return 0;
  776. }
  777. static av_cold int g2m_decode_end(AVCodecContext *avctx)
  778. {
  779. G2MContext *const c = avctx->priv_data;
  780. jpg_free_context(&c->jc);
  781. av_freep(&c->kempf_buf);
  782. av_freep(&c->kempf_flags);
  783. av_freep(&c->synth_tile);
  784. av_freep(&c->jpeg_tile);
  785. av_freep(&c->cursor);
  786. av_freep(&c->framebuf);
  787. return 0;
  788. }
  789. AVCodec ff_g2m_decoder = {
  790. .name = "g2m",
  791. .long_name = NULL_IF_CONFIG_SMALL("Go2Meeting"),
  792. .type = AVMEDIA_TYPE_VIDEO,
  793. .id = AV_CODEC_ID_G2M,
  794. .priv_data_size = sizeof(G2MContext),
  795. .init = g2m_decode_init,
  796. .close = g2m_decode_end,
  797. .decode = g2m_decode_frame,
  798. .capabilities = CODEC_CAP_DR1,
  799. };