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.

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