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.

913 lines
29KB

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