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.

912 lines
28KB

  1. /*
  2. * Go2Webinar decoder
  3. * Copyright (c) 2012 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. if (src_end - src < npal * 3)
  328. return AVERROR_INVALIDDATA;
  329. memcpy(pal, src, npal * 3);
  330. src += npal * 3;
  331. if (sub_type != 2) {
  332. for (i = 0; i < npal; i++) {
  333. if (!memcmp(pal + i * 3, transp, 3)) {
  334. tidx = i;
  335. break;
  336. }
  337. }
  338. }
  339. if (src_end - src < 2)
  340. return 0;
  341. zsize = (src[0] << 8) | src[1];
  342. src += 2;
  343. if (src_end - src < zsize + (sub_type != 2))
  344. return AVERROR_INVALIDDATA;
  345. ret = uncompress(c->kempf_buf, &dlen, src, zsize);
  346. if (ret)
  347. return AVERROR_INVALIDDATA;
  348. src += zsize;
  349. if (sub_type == 2) {
  350. kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
  351. NULL, 0, width, height, pal, npal, tidx);
  352. return 0;
  353. }
  354. nblocks = *src++ + 1;
  355. cblocks = 0;
  356. bstride = FFALIGN(width, 16) >> 3;
  357. // blocks are coded LSB and we need normal bitreader for JPEG data
  358. bits = 0;
  359. for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
  360. for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
  361. if (!bits) {
  362. if (src >= src_end)
  363. return AVERROR_INVALIDDATA;
  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 + 15, 16) * 3;
  393. aligned_height = c->height + 15;
  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, 16) * 3;
  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, cursor_fmt==1 ? 32 : 1) * 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. }
  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. }
  511. break;
  512. case 32: // full colour
  513. /* skip monochrome version of the cursor and decode RGBA instead */
  514. bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
  515. for (j = 0; j < c->cursor_h; j++) {
  516. for (i = 0; i < c->cursor_w; i++) {
  517. int val = bytestream2_get_be32(gb);
  518. *dst++ = val >> 0;
  519. *dst++ = val >> 8;
  520. *dst++ = val >> 16;
  521. *dst++ = val >> 24;
  522. }
  523. }
  524. break;
  525. default:
  526. return AVERROR_PATCHWELCOME;
  527. }
  528. return 0;
  529. }
  530. #define APPLY_ALPHA(src, new, alpha) \
  531. src = (src * (256 - alpha) + new * alpha) >> 8
  532. static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
  533. {
  534. int i, j;
  535. int x, y, w, h;
  536. const uint8_t *cursor;
  537. if (!c->cursor)
  538. return;
  539. x = c->cursor_x - c->cursor_hot_x;
  540. y = c->cursor_y - c->cursor_hot_y;
  541. cursor = c->cursor;
  542. w = c->cursor_w;
  543. h = c->cursor_h;
  544. if (x + w > c->width)
  545. w = c->width - x;
  546. if (y + h > c->height)
  547. h = c->height - y;
  548. if (x < 0) {
  549. w += x;
  550. cursor += -x * 4;
  551. } else {
  552. dst += x * 3;
  553. }
  554. if (y < 0) {
  555. h += y;
  556. cursor += -y * c->cursor_stride;
  557. } else {
  558. dst += y * stride;
  559. }
  560. if (w < 0 || h < 0)
  561. return;
  562. for (j = 0; j < h; j++) {
  563. for (i = 0; i < w; i++) {
  564. uint8_t alpha = cursor[i * 4];
  565. APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
  566. APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
  567. APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
  568. }
  569. dst += stride;
  570. cursor += c->cursor_stride;
  571. }
  572. }
  573. static int g2m_decode_frame(AVCodecContext *avctx, void *data,
  574. int *got_picture_ptr, AVPacket *avpkt)
  575. {
  576. const uint8_t *buf = avpkt->data;
  577. int buf_size = avpkt->size;
  578. G2MContext *c = avctx->priv_data;
  579. AVFrame *pic = data;
  580. GetByteContext bc, tbc;
  581. int magic;
  582. int got_header = 0;
  583. uint32_t chunk_size, r_mask, g_mask, b_mask;
  584. int chunk_type, chunk_start;
  585. int i;
  586. int ret;
  587. if (buf_size < 12) {
  588. av_log(avctx, AV_LOG_ERROR,
  589. "Frame should have at least 12 bytes, got %d instead\n",
  590. buf_size);
  591. return AVERROR_INVALIDDATA;
  592. }
  593. bytestream2_init(&bc, buf, buf_size);
  594. magic = bytestream2_get_be32(&bc);
  595. if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
  596. (magic & 0xF) < 2 || (magic & 0xF) > 4) {
  597. av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
  598. return AVERROR_INVALIDDATA;
  599. }
  600. if ((magic & 0xF) != 4) {
  601. av_log(avctx, AV_LOG_ERROR, "G2M2 and G2M3 are not yet supported\n");
  602. return AVERROR(ENOSYS);
  603. }
  604. while (bytestream2_get_bytes_left(&bc) > 5) {
  605. chunk_size = bytestream2_get_le32(&bc) - 1;
  606. chunk_type = bytestream2_get_byte(&bc);
  607. chunk_start = bytestream2_tell(&bc);
  608. if (chunk_size > bytestream2_get_bytes_left(&bc)) {
  609. av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %"PRIu32" type %02X\n",
  610. chunk_size, chunk_type);
  611. break;
  612. }
  613. switch (chunk_type) {
  614. case DISPLAY_INFO:
  615. got_header =
  616. c->got_header = 0;
  617. if (chunk_size < 21) {
  618. av_log(avctx, AV_LOG_ERROR, "Invalid display info size %"PRIu32"\n",
  619. chunk_size);
  620. break;
  621. }
  622. c->width = bytestream2_get_be32(&bc);
  623. c->height = bytestream2_get_be32(&bc);
  624. if (c->width < 16 || c->width > avctx->width ||
  625. c->height < 16 || c->height > avctx->height) {
  626. av_log(avctx, AV_LOG_ERROR,
  627. "Invalid frame dimensions %dx%d\n",
  628. c->width, c->height);
  629. ret = AVERROR_INVALIDDATA;
  630. goto header_fail;
  631. }
  632. if (c->width != avctx->width || c->height != avctx->height) {
  633. ret = ff_set_dimensions(avctx, c->width, c->height);
  634. if (ret < 0)
  635. goto header_fail;
  636. }
  637. c->compression = bytestream2_get_be32(&bc);
  638. if (c->compression != 2 && c->compression != 3) {
  639. av_log(avctx, AV_LOG_ERROR,
  640. "Unknown compression method %d\n",
  641. c->compression);
  642. ret = AVERROR_PATCHWELCOME;
  643. goto header_fail;
  644. }
  645. c->tile_width = bytestream2_get_be32(&bc);
  646. c->tile_height = bytestream2_get_be32(&bc);
  647. if (!c->tile_width || !c->tile_height ||
  648. ((c->tile_width | c->tile_height) & 0xF)) {
  649. av_log(avctx, AV_LOG_ERROR,
  650. "Invalid tile dimensions %dx%d\n",
  651. c->tile_width, c->tile_height);
  652. ret = AVERROR_INVALIDDATA;
  653. goto header_fail;
  654. }
  655. c->tiles_x = (c->width + c->tile_width - 1) / c->tile_width;
  656. c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
  657. c->bpp = bytestream2_get_byte(&bc);
  658. if (c->bpp == 32) {
  659. if (bytestream2_get_bytes_left(&bc) < 16 ||
  660. (chunk_size - 21) < 16) {
  661. av_log(avctx, AV_LOG_ERROR,
  662. "Display info: missing bitmasks!\n");
  663. ret = AVERROR_INVALIDDATA;
  664. goto header_fail;
  665. }
  666. r_mask = bytestream2_get_be32(&bc);
  667. g_mask = bytestream2_get_be32(&bc);
  668. b_mask = bytestream2_get_be32(&bc);
  669. if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
  670. av_log(avctx, AV_LOG_ERROR,
  671. "Invalid or unsupported bitmasks: R=%"PRIX32", G=%"PRIX32", B=%"PRIX32"\n",
  672. r_mask, g_mask, b_mask);
  673. ret = AVERROR_PATCHWELCOME;
  674. goto header_fail;
  675. }
  676. } else {
  677. avpriv_request_sample(avctx, "bpp=%d", c->bpp);
  678. ret = AVERROR_PATCHWELCOME;
  679. goto header_fail;
  680. }
  681. if (g2m_init_buffers(c)) {
  682. ret = AVERROR(ENOMEM);
  683. goto header_fail;
  684. }
  685. got_header = 1;
  686. break;
  687. case TILE_DATA:
  688. if (!c->tiles_x || !c->tiles_y) {
  689. av_log(avctx, AV_LOG_WARNING,
  690. "No display info - skipping tile\n");
  691. break;
  692. }
  693. if (chunk_size < 2) {
  694. av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %d\n",
  695. chunk_size);
  696. break;
  697. }
  698. c->tile_x = bytestream2_get_byte(&bc);
  699. c->tile_y = bytestream2_get_byte(&bc);
  700. if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
  701. av_log(avctx, AV_LOG_ERROR,
  702. "Invalid tile pos %d,%d (in %dx%d grid)\n",
  703. c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
  704. break;
  705. }
  706. ret = 0;
  707. switch (c->compression) {
  708. case COMPR_EPIC_J_B:
  709. av_log(avctx, AV_LOG_ERROR,
  710. "ePIC j-b compression is not implemented yet\n");
  711. return AVERROR(ENOSYS);
  712. case COMPR_KEMPF_J_B:
  713. ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
  714. buf + bytestream2_tell(&bc),
  715. chunk_size - 2);
  716. break;
  717. }
  718. if (ret && c->framebuf)
  719. av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
  720. c->tile_x, c->tile_y);
  721. break;
  722. case CURSOR_POS:
  723. if (chunk_size < 5) {
  724. av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %"PRIu32"\n",
  725. chunk_size);
  726. break;
  727. }
  728. c->cursor_x = bytestream2_get_be16(&bc);
  729. c->cursor_y = bytestream2_get_be16(&bc);
  730. break;
  731. case CURSOR_SHAPE:
  732. if (chunk_size < 8) {
  733. av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"\n",
  734. chunk_size);
  735. break;
  736. }
  737. bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
  738. chunk_size - 4);
  739. g2m_load_cursor(avctx, c, &tbc);
  740. break;
  741. case CHUNK_CC:
  742. case CHUNK_CD:
  743. break;
  744. default:
  745. av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02"PRIX32"\n",
  746. chunk_type);
  747. }
  748. /* navigate to next chunk */
  749. bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
  750. }
  751. if (got_header)
  752. c->got_header = 1;
  753. if (c->width && c->height && c->framebuf) {
  754. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  755. return ret;
  756. pic->key_frame = got_header;
  757. pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  758. for (i = 0; i < avctx->height; i++)
  759. memcpy(pic->data[0] + i * pic->linesize[0],
  760. c->framebuf + i * c->framebuf_stride,
  761. c->width * 3);
  762. g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);
  763. *got_picture_ptr = 1;
  764. }
  765. return buf_size;
  766. header_fail:
  767. c->width =
  768. c->height = 0;
  769. c->tiles_x =
  770. c->tiles_y = 0;
  771. return ret;
  772. }
  773. static av_cold int g2m_decode_init(AVCodecContext *avctx)
  774. {
  775. G2MContext *const c = avctx->priv_data;
  776. int ret;
  777. if ((ret = jpg_init(avctx, &c->jc)) != 0) {
  778. av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
  779. jpg_free_context(&c->jc);
  780. return AVERROR(ENOMEM);
  781. }
  782. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  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. };