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.

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