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.

869 lines
27KB

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