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.

893 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); src += npal * 3;
  327. if (sub_type != 2) {
  328. for (i = 0; i < npal; i++) {
  329. if (!memcmp(pal + i * 3, transp, 3)) {
  330. tidx = i;
  331. break;
  332. }
  333. }
  334. }
  335. if (src_end - src < 2)
  336. return 0;
  337. zsize = (src[0] << 8) | src[1]; src += 2;
  338. if (src_end - src < zsize)
  339. return AVERROR_INVALIDDATA;
  340. ret = uncompress(c->kempf_buf, &dlen, src, zsize);
  341. if (ret)
  342. return AVERROR_INVALIDDATA;
  343. src += zsize;
  344. if (sub_type == 2) {
  345. kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
  346. NULL, 0, width, height, pal, npal, tidx);
  347. return 0;
  348. }
  349. nblocks = *src++ + 1;
  350. cblocks = 0;
  351. bstride = FFALIGN(width, 16) >> 3;
  352. // blocks are coded LSB and we need normal bitreader for JPEG data
  353. bits = 0;
  354. for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
  355. for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
  356. if (!bits) {
  357. bitbuf = *src++;
  358. bits = 8;
  359. }
  360. coded = bitbuf & 1;
  361. bits--;
  362. bitbuf >>= 1;
  363. cblocks += coded;
  364. if (cblocks > nblocks)
  365. return AVERROR_INVALIDDATA;
  366. c->kempf_flags[j * 2 + i * 2 * bstride] =
  367. c->kempf_flags[j * 2 + 1 + i * 2 * bstride] =
  368. c->kempf_flags[j * 2 + (i * 2 + 1) * bstride] =
  369. c->kempf_flags[j * 2 + 1 + (i * 2 + 1) * bstride] = coded;
  370. }
  371. }
  372. memset(c->jpeg_tile, 0, c->tile_stride * height);
  373. jpg_decode_data(&c->jc, width, height, src, src_end - src,
  374. c->jpeg_tile, c->tile_stride,
  375. c->kempf_flags, bstride, nblocks * 4, 0);
  376. kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
  377. c->jpeg_tile, c->tile_stride,
  378. width, height, pal, npal, tidx);
  379. return 0;
  380. }
  381. static int g2m_init_buffers(G2MContext *c)
  382. {
  383. int aligned_height;
  384. if (!c->framebuf || c->old_width < c->width || c->old_height < c->height) {
  385. c->framebuf_stride = FFALIGN(c->width * 3, 16);
  386. aligned_height = FFALIGN(c->height, 16);
  387. av_free(c->framebuf);
  388. c->framebuf = av_mallocz(c->framebuf_stride * aligned_height);
  389. if (!c->framebuf)
  390. return AVERROR(ENOMEM);
  391. }
  392. if (!c->synth_tile || !c->jpeg_tile ||
  393. c->old_tile_w < c->tile_width ||
  394. c->old_tile_h < c->tile_height) {
  395. c->tile_stride = FFALIGN(c->tile_width * 3, 16);
  396. aligned_height = FFALIGN(c->tile_height, 16);
  397. av_free(c->synth_tile);
  398. av_free(c->jpeg_tile);
  399. av_free(c->kempf_buf);
  400. av_free(c->kempf_flags);
  401. c->synth_tile = av_mallocz(c->tile_stride * aligned_height);
  402. c->jpeg_tile = av_mallocz(c->tile_stride * aligned_height);
  403. c->kempf_buf = av_mallocz((c->tile_width + 1) * aligned_height
  404. + FF_INPUT_BUFFER_PADDING_SIZE);
  405. c->kempf_flags = av_mallocz( c->tile_width * aligned_height);
  406. if (!c->synth_tile || !c->jpeg_tile ||
  407. !c->kempf_buf || !c->kempf_flags)
  408. return AVERROR(ENOMEM);
  409. }
  410. return 0;
  411. }
  412. static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c,
  413. GetByteContext *gb)
  414. {
  415. int i, j, k;
  416. uint8_t *dst;
  417. uint32_t bits;
  418. uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
  419. uint32_t cursor_hot_x, cursor_hot_y;
  420. int cursor_fmt, err;
  421. cur_size = bytestream2_get_be32(gb);
  422. cursor_w = bytestream2_get_byte(gb);
  423. cursor_h = bytestream2_get_byte(gb);
  424. cursor_hot_x = bytestream2_get_byte(gb);
  425. cursor_hot_y = bytestream2_get_byte(gb);
  426. cursor_fmt = bytestream2_get_byte(gb);
  427. cursor_stride = FFALIGN(cursor_w, 32) * 4;
  428. if (cursor_w < 1 || cursor_w > 256 ||
  429. cursor_h < 1 || cursor_h > 256) {
  430. av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %dx%d\n",
  431. cursor_w, cursor_h);
  432. return AVERROR_INVALIDDATA;
  433. }
  434. if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
  435. av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %d,%d\n",
  436. cursor_hot_x, cursor_hot_y);
  437. cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);
  438. cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);
  439. }
  440. if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||
  441. c->cursor_w * c->cursor_h / 4 > cur_size) {
  442. av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %d/%d\n",
  443. cur_size, bytestream2_get_bytes_left(gb));
  444. return AVERROR_INVALIDDATA;
  445. }
  446. if (cursor_fmt != 1 && cursor_fmt != 32) {
  447. avpriv_report_missing_feature(avctx, "Cursor format %d",
  448. cursor_fmt);
  449. return AVERROR_PATCHWELCOME;
  450. }
  451. if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
  452. av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
  453. return err;
  454. }
  455. c->cursor_w = cursor_w;
  456. c->cursor_h = cursor_h;
  457. c->cursor_hot_x = cursor_hot_x;
  458. c->cursor_hot_y = cursor_hot_y;
  459. c->cursor_fmt = cursor_fmt;
  460. c->cursor_stride = cursor_stride;
  461. dst = c->cursor;
  462. switch (c->cursor_fmt) {
  463. case 1: // old monochrome
  464. for (j = 0; j < c->cursor_h; j++) {
  465. for (i = 0; i < c->cursor_w; i += 32) {
  466. bits = bytestream2_get_be32(gb);
  467. for (k = 0; k < 32; k++) {
  468. dst[0] = !!(bits & 0x80000000);
  469. dst += 4;
  470. bits <<= 1;
  471. }
  472. }
  473. dst += c->cursor_stride - c->cursor_w * 4;
  474. }
  475. dst = c->cursor;
  476. for (j = 0; j < c->cursor_h; j++) {
  477. for (i = 0; i < c->cursor_w; i += 32) {
  478. bits = bytestream2_get_be32(gb);
  479. for (k = 0; k < 32; k++) {
  480. int mask_bit = !!(bits & 0x80000000);
  481. switch (dst[0] * 2 + mask_bit) {
  482. case 0:
  483. dst[0] = 0xFF; dst[1] = 0x00;
  484. dst[2] = 0x00; dst[3] = 0x00;
  485. break;
  486. case 1:
  487. dst[0] = 0xFF; dst[1] = 0xFF;
  488. dst[2] = 0xFF; dst[3] = 0xFF;
  489. break;
  490. default:
  491. dst[0] = 0x00; dst[1] = 0x00;
  492. dst[2] = 0x00; dst[3] = 0x00;
  493. }
  494. dst += 4;
  495. bits <<= 1;
  496. }
  497. }
  498. dst += c->cursor_stride - c->cursor_w * 4;
  499. }
  500. break;
  501. case 32: // full colour
  502. /* skip monochrome version of the cursor and decode RGBA instead */
  503. bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
  504. for (j = 0; j < c->cursor_h; j++) {
  505. for (i = 0; i < c->cursor_w; i++) {
  506. int val = bytestream2_get_be32(gb);
  507. *dst++ = val >> 0;
  508. *dst++ = val >> 8;
  509. *dst++ = val >> 16;
  510. *dst++ = val >> 24;
  511. }
  512. dst += c->cursor_stride - c->cursor_w * 4;
  513. }
  514. break;
  515. default:
  516. return AVERROR_PATCHWELCOME;
  517. }
  518. return 0;
  519. }
  520. #define APPLY_ALPHA(src, new, alpha) \
  521. src = (src * (256 - alpha) + new * alpha) >> 8
  522. static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
  523. {
  524. int i, j;
  525. int x, y, w, h;
  526. const uint8_t *cursor;
  527. if (!c->cursor)
  528. return;
  529. x = c->cursor_x - c->cursor_hot_x;
  530. y = c->cursor_y - c->cursor_hot_y;
  531. cursor = c->cursor;
  532. w = c->cursor_w;
  533. h = c->cursor_h;
  534. if (x + w > c->width)
  535. w = c->width - x;
  536. if (y + h > c->height)
  537. h = c->height - y;
  538. if (x < 0) {
  539. w += x;
  540. cursor += -x * 4;
  541. } else {
  542. dst += x * 3;
  543. }
  544. if (y < 0) {
  545. h += y;
  546. cursor += -y * c->cursor_stride;
  547. } else {
  548. dst += y * stride;
  549. }
  550. if (w < 0 || h < 0)
  551. return;
  552. for (j = 0; j < h; j++) {
  553. for (i = 0; i < w; i++) {
  554. uint8_t alpha = cursor[i * 4];
  555. APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
  556. APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
  557. APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
  558. }
  559. dst += stride;
  560. cursor += c->cursor_stride;
  561. }
  562. }
  563. static int g2m_decode_frame(AVCodecContext *avctx, void *data,
  564. int *got_picture_ptr, AVPacket *avpkt)
  565. {
  566. const uint8_t *buf = avpkt->data;
  567. int buf_size = avpkt->size;
  568. G2MContext *c = avctx->priv_data;
  569. AVFrame *pic = data;
  570. GetByteContext bc, tbc;
  571. int magic;
  572. int got_header = 0;
  573. uint32_t chunk_size, r_mask, g_mask, b_mask;
  574. int chunk_type, chunk_start;
  575. int i;
  576. int ret;
  577. if (buf_size < 12) {
  578. av_log(avctx, AV_LOG_ERROR,
  579. "Frame should have at least 12 bytes, got %d instead\n",
  580. buf_size);
  581. return AVERROR_INVALIDDATA;
  582. }
  583. bytestream2_init(&bc, buf, buf_size);
  584. magic = bytestream2_get_be32(&bc);
  585. if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
  586. (magic & 0xF) < 2 || (magic & 0xF) > 4) {
  587. av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
  588. return AVERROR_INVALIDDATA;
  589. }
  590. if ((magic & 0xF) != 4) {
  591. av_log(avctx, AV_LOG_ERROR, "G2M2 and G2M3 are not yet supported\n");
  592. return AVERROR(ENOSYS);
  593. }
  594. while (bytestream2_get_bytes_left(&bc) > 5) {
  595. chunk_size = bytestream2_get_le32(&bc) - 1;
  596. chunk_type = bytestream2_get_byte(&bc);
  597. chunk_start = bytestream2_tell(&bc);
  598. if (chunk_size > bytestream2_get_bytes_left(&bc)) {
  599. av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %d type %02X\n",
  600. chunk_size, chunk_type);
  601. break;
  602. }
  603. switch (chunk_type) {
  604. case DISPLAY_INFO:
  605. c->got_header = 0;
  606. if (chunk_size < 21) {
  607. av_log(avctx, AV_LOG_ERROR, "Invalid display info size %d\n",
  608. chunk_size);
  609. break;
  610. }
  611. c->width = bytestream2_get_be32(&bc);
  612. c->height = bytestream2_get_be32(&bc);
  613. if (c->width < 16 || c->width > avctx->width ||
  614. c->height < 16 || c->height > avctx->height) {
  615. av_log(avctx, AV_LOG_ERROR,
  616. "Invalid frame dimensions %dx%d\n",
  617. c->width, c->height);
  618. ret = AVERROR_INVALIDDATA;
  619. goto header_fail;
  620. }
  621. if (c->width != avctx->width || c->height != avctx->height)
  622. ff_set_dimensions(avctx, c->width, c->height);
  623. c->compression = bytestream2_get_be32(&bc);
  624. if (c->compression != 2 && c->compression != 3) {
  625. av_log(avctx, AV_LOG_ERROR,
  626. "Unknown compression method %d\n",
  627. c->compression);
  628. return AVERROR_PATCHWELCOME;
  629. }
  630. c->tile_width = bytestream2_get_be32(&bc);
  631. c->tile_height = bytestream2_get_be32(&bc);
  632. if (!c->tile_width || !c->tile_height ||
  633. ((c->tile_width | c->tile_height) & 0xF)) {
  634. av_log(avctx, AV_LOG_ERROR,
  635. "Invalid tile dimensions %dx%d\n",
  636. c->tile_width, c->tile_height);
  637. ret = AVERROR_INVALIDDATA;
  638. goto header_fail;
  639. }
  640. c->tiles_x = (c->width + c->tile_width - 1) / c->tile_width;
  641. c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
  642. c->bpp = bytestream2_get_byte(&bc);
  643. if (c->bpp == 32) {
  644. if (bytestream2_get_bytes_left(&bc) < 16 ||
  645. (chunk_size - 21) < 16 ) {
  646. av_log(avctx, AV_LOG_ERROR,
  647. "Display info: missing bitmasks!\n");
  648. return AVERROR_INVALIDDATA;
  649. }
  650. r_mask = bytestream2_get_be32(&bc);
  651. g_mask = bytestream2_get_be32(&bc);
  652. b_mask = bytestream2_get_be32(&bc);
  653. if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
  654. av_log(avctx, AV_LOG_ERROR,
  655. "Invalid or unsupported bitmasks: R=%X, G=%X, B=%X\n",
  656. r_mask, g_mask, b_mask);
  657. return AVERROR_PATCHWELCOME;
  658. }
  659. } else {
  660. avpriv_request_sample(avctx, "bpp=%d", c->bpp);
  661. return AVERROR_PATCHWELCOME;
  662. }
  663. if (g2m_init_buffers(c)) {
  664. ret = AVERROR(ENOMEM);
  665. goto header_fail;
  666. }
  667. got_header = 1;
  668. break;
  669. case TILE_DATA:
  670. if (!c->tiles_x || !c->tiles_y) {
  671. av_log(avctx, AV_LOG_WARNING,
  672. "No display info - skipping tile\n");
  673. break;
  674. }
  675. if (chunk_size < 2) {
  676. av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %d\n",
  677. chunk_size);
  678. break;
  679. }
  680. c->tile_x = bytestream2_get_byte(&bc);
  681. c->tile_y = bytestream2_get_byte(&bc);
  682. if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
  683. av_log(avctx, AV_LOG_ERROR,
  684. "Invalid tile pos %d,%d (in %dx%d grid)\n",
  685. c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
  686. break;
  687. }
  688. ret = 0;
  689. switch (c->compression) {
  690. case COMPR_EPIC_J_B:
  691. av_log(avctx, AV_LOG_ERROR,
  692. "ePIC j-b compression is not implemented yet\n");
  693. return AVERROR(ENOSYS);
  694. case COMPR_KEMPF_J_B:
  695. ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
  696. buf + bytestream2_tell(&bc),
  697. chunk_size - 2);
  698. break;
  699. }
  700. if (ret && c->framebuf)
  701. av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
  702. c->tile_x, c->tile_y);
  703. break;
  704. case CURSOR_POS:
  705. if (chunk_size < 5) {
  706. av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %d\n",
  707. chunk_size);
  708. break;
  709. }
  710. c->cursor_x = bytestream2_get_be16(&bc);
  711. c->cursor_y = bytestream2_get_be16(&bc);
  712. break;
  713. case CURSOR_SHAPE:
  714. if (chunk_size < 8) {
  715. av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %d\n",
  716. chunk_size);
  717. break;
  718. }
  719. bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
  720. chunk_size - 4);
  721. g2m_load_cursor(avctx, c, &tbc);
  722. break;
  723. case CHUNK_CC:
  724. case CHUNK_CD:
  725. break;
  726. default:
  727. av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02X\n",
  728. chunk_type);
  729. }
  730. /* navigate to next chunk */
  731. bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
  732. }
  733. if (got_header)
  734. c->got_header = 1;
  735. if (c->width && c->height) {
  736. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0) {
  737. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  738. return ret;
  739. }
  740. pic->key_frame = got_header;
  741. pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  742. for (i = 0; i < avctx->height; i++)
  743. memcpy(pic->data[0] + i * pic->linesize[0],
  744. c->framebuf + i * c->framebuf_stride,
  745. c->width * 3);
  746. g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);
  747. *got_picture_ptr = 1;
  748. }
  749. return buf_size;
  750. header_fail:
  751. c->width = c->height = 0;
  752. c->tiles_x = c->tiles_y = 0;
  753. return ret;
  754. }
  755. static av_cold int g2m_decode_init(AVCodecContext *avctx)
  756. {
  757. G2MContext * const c = avctx->priv_data;
  758. int ret;
  759. if ((ret = jpg_init(avctx, &c->jc)) != 0) {
  760. av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
  761. jpg_free_context(&c->jc);
  762. return AVERROR(ENOMEM);
  763. }
  764. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  765. return 0;
  766. }
  767. static av_cold int g2m_decode_end(AVCodecContext *avctx)
  768. {
  769. G2MContext * const c = avctx->priv_data;
  770. jpg_free_context(&c->jc);
  771. av_freep(&c->kempf_buf);
  772. av_freep(&c->kempf_flags);
  773. av_freep(&c->synth_tile);
  774. av_freep(&c->jpeg_tile);
  775. av_freep(&c->cursor);
  776. av_freep(&c->framebuf);
  777. return 0;
  778. }
  779. AVCodec ff_g2m_decoder = {
  780. .name = "g2m",
  781. .long_name = NULL_IF_CONFIG_SMALL("Go2Meeting"),
  782. .type = AVMEDIA_TYPE_VIDEO,
  783. .id = AV_CODEC_ID_G2M,
  784. .priv_data_size = sizeof(G2MContext),
  785. .init = g2m_decode_init,
  786. .close = g2m_decode_end,
  787. .decode = g2m_decode_frame,
  788. .capabilities = CODEC_CAP_DR1,
  789. };