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.

1636 lines
52KB

  1. /*
  2. * Go2Webinar / Go2Meeting decoder
  3. * Copyright (c) 2012 Konstantin Shishkov
  4. * Copyright (c) 2013 Maxim Poliakovski
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Go2Webinar / Go2Meeting decoder
  25. */
  26. #include <inttypes.h>
  27. #include <zlib.h>
  28. #include "libavutil/intreadwrite.h"
  29. #include "avcodec.h"
  30. #include "blockdsp.h"
  31. #include "bytestream.h"
  32. #include "elsdec.h"
  33. #include "get_bits.h"
  34. #include "idctdsp.h"
  35. #include "internal.h"
  36. #include "jpegtables.h"
  37. #include "mjpeg.h"
  38. #define EPIC_PIX_STACK_SIZE 1024
  39. #define EPIC_PIX_STACK_MAX (EPIC_PIX_STACK_SIZE - 1)
  40. enum ChunkType {
  41. DISPLAY_INFO = 0xC8,
  42. TILE_DATA,
  43. CURSOR_POS,
  44. CURSOR_SHAPE,
  45. CHUNK_CC,
  46. CHUNK_CD
  47. };
  48. enum Compression {
  49. COMPR_EPIC_J_B = 2,
  50. COMPR_KEMPF_J_B,
  51. };
  52. static const uint8_t luma_quant[64] = {
  53. 8, 6, 5, 8, 12, 20, 26, 31,
  54. 6, 6, 7, 10, 13, 29, 30, 28,
  55. 7, 7, 8, 12, 20, 29, 35, 28,
  56. 7, 9, 11, 15, 26, 44, 40, 31,
  57. 9, 11, 19, 28, 34, 55, 52, 39,
  58. 12, 18, 28, 32, 41, 52, 57, 46,
  59. 25, 32, 39, 44, 52, 61, 60, 51,
  60. 36, 46, 48, 49, 56, 50, 52, 50
  61. };
  62. static const uint8_t chroma_quant[64] = {
  63. 9, 9, 12, 24, 50, 50, 50, 50,
  64. 9, 11, 13, 33, 50, 50, 50, 50,
  65. 12, 13, 28, 50, 50, 50, 50, 50,
  66. 24, 33, 50, 50, 50, 50, 50, 50,
  67. 50, 50, 50, 50, 50, 50, 50, 50,
  68. 50, 50, 50, 50, 50, 50, 50, 50,
  69. 50, 50, 50, 50, 50, 50, 50, 50,
  70. 50, 50, 50, 50, 50, 50, 50, 50,
  71. };
  72. typedef struct ePICPixListElem {
  73. struct ePICPixListElem *next;
  74. uint32_t pixel;
  75. uint8_t rung;
  76. } ePICPixListElem;
  77. typedef struct ePICPixHashElem {
  78. uint32_t pix_id;
  79. struct ePICPixListElem *list;
  80. } ePICPixHashElem;
  81. #define EPIC_HASH_SIZE 256
  82. typedef struct ePICPixHash {
  83. ePICPixHashElem *bucket[EPIC_HASH_SIZE];
  84. int bucket_size[EPIC_HASH_SIZE];
  85. int bucket_fill[EPIC_HASH_SIZE];
  86. } ePICPixHash;
  87. typedef struct ePICContext {
  88. ElsDecCtx els_ctx;
  89. int next_run_pos;
  90. ElsUnsignedRung unsigned_rung;
  91. uint8_t W_flag_rung;
  92. uint8_t N_flag_rung;
  93. uint8_t W_ctx_rung[256];
  94. uint8_t N_ctx_rung[512];
  95. uint8_t nw_pred_rung[256];
  96. uint8_t ne_pred_rung[256];
  97. uint8_t prev_row_rung[14];
  98. uint8_t runlen_zeroes[14];
  99. uint8_t runlen_one;
  100. int stack_pos;
  101. uint32_t stack[EPIC_PIX_STACK_SIZE];
  102. ePICPixHash hash;
  103. } ePICContext;
  104. typedef struct JPGContext {
  105. BlockDSPContext bdsp;
  106. IDCTDSPContext idsp;
  107. ScanTable scantable;
  108. VLC dc_vlc[2], ac_vlc[2];
  109. int prev_dc[3];
  110. DECLARE_ALIGNED(16, int16_t, block)[6][64];
  111. uint8_t *buf;
  112. } JPGContext;
  113. typedef struct G2MContext {
  114. ePICContext ec;
  115. JPGContext jc;
  116. int version;
  117. int compression;
  118. int width, height, bpp;
  119. int orig_width, orig_height;
  120. int tile_width, tile_height;
  121. int tiles_x, tiles_y, tile_x, tile_y;
  122. int got_header;
  123. uint8_t *framebuf;
  124. int framebuf_stride, old_width, old_height;
  125. uint8_t *synth_tile, *jpeg_tile, *epic_buf, *epic_buf_base;
  126. int tile_stride, epic_buf_stride, old_tile_w, old_tile_h;
  127. int swapuv;
  128. uint8_t *kempf_buf, *kempf_flags;
  129. uint8_t *cursor;
  130. int cursor_stride;
  131. int cursor_fmt;
  132. int cursor_w, cursor_h, cursor_x, cursor_y;
  133. int cursor_hot_x, cursor_hot_y;
  134. } G2MContext;
  135. static av_cold int build_vlc(VLC *vlc, const uint8_t *bits_table,
  136. const uint8_t *val_table, int nb_codes,
  137. int is_ac)
  138. {
  139. uint8_t huff_size[256] = { 0 };
  140. uint16_t huff_code[256];
  141. uint16_t huff_sym[256];
  142. int i;
  143. ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  144. for (i = 0; i < 256; i++)
  145. huff_sym[i] = i + 16 * is_ac;
  146. if (is_ac)
  147. huff_sym[0] = 16 * 256;
  148. return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
  149. huff_code, 2, 2, huff_sym, 2, 2, 0);
  150. }
  151. static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c)
  152. {
  153. int ret;
  154. ret = build_vlc(&c->dc_vlc[0], avpriv_mjpeg_bits_dc_luminance,
  155. avpriv_mjpeg_val_dc, 12, 0);
  156. if (ret)
  157. return ret;
  158. ret = build_vlc(&c->dc_vlc[1], avpriv_mjpeg_bits_dc_chrominance,
  159. avpriv_mjpeg_val_dc, 12, 0);
  160. if (ret)
  161. return ret;
  162. ret = build_vlc(&c->ac_vlc[0], avpriv_mjpeg_bits_ac_luminance,
  163. avpriv_mjpeg_val_ac_luminance, 251, 1);
  164. if (ret)
  165. return ret;
  166. ret = build_vlc(&c->ac_vlc[1], avpriv_mjpeg_bits_ac_chrominance,
  167. avpriv_mjpeg_val_ac_chrominance, 251, 1);
  168. if (ret)
  169. return ret;
  170. ff_blockdsp_init(&c->bdsp, avctx);
  171. ff_idctdsp_init(&c->idsp, avctx);
  172. ff_init_scantable(c->idsp.idct_permutation, &c->scantable,
  173. ff_zigzag_direct);
  174. return 0;
  175. }
  176. static av_cold void jpg_free_context(JPGContext *ctx)
  177. {
  178. int i;
  179. for (i = 0; i < 2; i++) {
  180. ff_free_vlc(&ctx->dc_vlc[i]);
  181. ff_free_vlc(&ctx->ac_vlc[i]);
  182. }
  183. av_freep(&ctx->buf);
  184. }
  185. static void jpg_unescape(const uint8_t *src, int src_size,
  186. uint8_t *dst, int *dst_size)
  187. {
  188. const uint8_t *src_end = src + src_size;
  189. uint8_t *dst_start = dst;
  190. while (src < src_end) {
  191. uint8_t x = *src++;
  192. *dst++ = x;
  193. if (x == 0xFF && !*src)
  194. src++;
  195. }
  196. *dst_size = dst - dst_start;
  197. }
  198. static int jpg_decode_block(JPGContext *c, GetBitContext *gb,
  199. int plane, int16_t *block)
  200. {
  201. int dc, val, pos;
  202. const int is_chroma = !!plane;
  203. const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant;
  204. c->bdsp.clear_block(block);
  205. dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 3);
  206. if (dc < 0)
  207. return AVERROR_INVALIDDATA;
  208. if (dc)
  209. dc = get_xbits(gb, dc);
  210. dc = dc * qmat[0] + c->prev_dc[plane];
  211. block[0] = dc;
  212. c->prev_dc[plane] = dc;
  213. pos = 0;
  214. while (pos < 63) {
  215. val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 3);
  216. if (val < 0)
  217. return AVERROR_INVALIDDATA;
  218. pos += val >> 4;
  219. val &= 0xF;
  220. if (pos > 63)
  221. return val ? AVERROR_INVALIDDATA : 0;
  222. if (val) {
  223. int nbits = val;
  224. val = get_xbits(gb, nbits);
  225. val *= qmat[ff_zigzag_direct[pos]];
  226. block[c->scantable.permutated[pos]] = val;
  227. }
  228. }
  229. return 0;
  230. }
  231. static inline void yuv2rgb(uint8_t *out, int ridx, int Y, int U, int V)
  232. {
  233. out[ridx] = av_clip_uint8(Y + (91881 * V + 32768 >> 16));
  234. out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
  235. out[2 - ridx] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
  236. }
  237. static int jpg_decode_data(JPGContext *c, int width, int height,
  238. const uint8_t *src, int src_size,
  239. uint8_t *dst, int dst_stride,
  240. const uint8_t *mask, int mask_stride, int num_mbs,
  241. int swapuv)
  242. {
  243. GetBitContext gb;
  244. int mb_w, mb_h, mb_x, mb_y, i, j;
  245. int bx, by;
  246. int unesc_size;
  247. int ret;
  248. const int ridx = swapuv ? 2 : 0;
  249. if ((ret = av_reallocp(&c->buf,
  250. src_size + AV_INPUT_BUFFER_PADDING_SIZE)) < 0)
  251. return ret;
  252. jpg_unescape(src, src_size, c->buf, &unesc_size);
  253. memset(c->buf + unesc_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  254. if((ret = init_get_bits8(&gb, c->buf, unesc_size)) < 0)
  255. return ret;
  256. width = FFALIGN(width, 16);
  257. mb_w = width >> 4;
  258. mb_h = (height + 15) >> 4;
  259. if (!num_mbs)
  260. num_mbs = mb_w * mb_h * 4;
  261. for (i = 0; i < 3; i++)
  262. c->prev_dc[i] = 1024;
  263. bx =
  264. by = 0;
  265. c->bdsp.clear_blocks(c->block[0]);
  266. for (mb_y = 0; mb_y < mb_h; mb_y++) {
  267. for (mb_x = 0; mb_x < mb_w; mb_x++) {
  268. if (mask && !mask[mb_x * 2] && !mask[mb_x * 2 + 1] &&
  269. !mask[mb_x * 2 + mask_stride] &&
  270. !mask[mb_x * 2 + 1 + mask_stride]) {
  271. bx += 16;
  272. continue;
  273. }
  274. for (j = 0; j < 2; j++) {
  275. for (i = 0; i < 2; i++) {
  276. if (mask && !mask[mb_x * 2 + i + j * mask_stride])
  277. continue;
  278. num_mbs--;
  279. if ((ret = jpg_decode_block(c, &gb, 0,
  280. c->block[i + j * 2])) != 0)
  281. return ret;
  282. c->idsp.idct(c->block[i + j * 2]);
  283. }
  284. }
  285. for (i = 1; i < 3; i++) {
  286. if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0)
  287. return ret;
  288. c->idsp.idct(c->block[i + 3]);
  289. }
  290. for (j = 0; j < 16; j++) {
  291. uint8_t *out = dst + bx * 3 + (by + j) * dst_stride;
  292. for (i = 0; i < 16; i++) {
  293. int Y, U, V;
  294. Y = c->block[(j >> 3) * 2 + (i >> 3)][(i & 7) + (j & 7) * 8];
  295. U = c->block[4][(i >> 1) + (j >> 1) * 8] - 128;
  296. V = c->block[5][(i >> 1) + (j >> 1) * 8] - 128;
  297. yuv2rgb(out + i * 3, ridx, Y, U, V);
  298. }
  299. }
  300. if (!num_mbs)
  301. return 0;
  302. bx += 16;
  303. }
  304. bx = 0;
  305. by += 16;
  306. if (mask)
  307. mask += mask_stride * 2;
  308. }
  309. return 0;
  310. }
  311. #define LOAD_NEIGHBOURS(x) \
  312. W = curr_row[(x) - 1]; \
  313. N = above_row[(x)]; \
  314. WW = curr_row[(x) - 2]; \
  315. NW = above_row[(x) - 1]; \
  316. NE = above_row[(x) + 1]; \
  317. NN = above2_row[(x)]; \
  318. NNW = above2_row[(x) - 1]; \
  319. NWW = above_row[(x) - 2]; \
  320. NNE = above2_row[(x) + 1]
  321. #define UPDATE_NEIGHBOURS(x) \
  322. NNW = NN; \
  323. NN = NNE; \
  324. NWW = NW; \
  325. NW = N; \
  326. N = NE; \
  327. NE = above_row[(x) + 1]; \
  328. NNE = above2_row[(x) + 1]
  329. #define R_shift 16
  330. #define G_shift 8
  331. #define B_shift 0
  332. /* improved djb2 hash from http://www.cse.yorku.ca/~oz/hash.html */
  333. static int djb2_hash(uint32_t key)
  334. {
  335. uint32_t h = 5381;
  336. h = (h * 33) ^ ((key >> 24) & 0xFF); // xxx: probably not needed at all
  337. h = (h * 33) ^ ((key >> 16) & 0xFF);
  338. h = (h * 33) ^ ((key >> 8) & 0xFF);
  339. h = (h * 33) ^ (key & 0xFF);
  340. return h & (EPIC_HASH_SIZE - 1);
  341. }
  342. static void epic_hash_init(ePICPixHash *hash)
  343. {
  344. memset(hash, 0, sizeof(*hash));
  345. }
  346. static ePICPixHashElem *epic_hash_find(const ePICPixHash *hash, uint32_t key)
  347. {
  348. int i, idx = djb2_hash(key);
  349. ePICPixHashElem *bucket = hash->bucket[idx];
  350. for (i = 0; i < hash->bucket_fill[idx]; i++)
  351. if (bucket[i].pix_id == key)
  352. return &bucket[i];
  353. return NULL;
  354. }
  355. static ePICPixHashElem *epic_hash_add(ePICPixHash *hash, uint32_t key)
  356. {
  357. ePICPixHashElem *bucket, *ret;
  358. int idx = djb2_hash(key);
  359. if (hash->bucket_size[idx] > INT_MAX / sizeof(**hash->bucket))
  360. return NULL;
  361. if (!(hash->bucket_fill[idx] < hash->bucket_size[idx])) {
  362. int new_size = hash->bucket_size[idx] + 16;
  363. bucket = av_realloc(hash->bucket[idx], new_size * sizeof(*bucket));
  364. if (!bucket)
  365. return NULL;
  366. hash->bucket[idx] = bucket;
  367. hash->bucket_size[idx] = new_size;
  368. }
  369. ret = &hash->bucket[idx][hash->bucket_fill[idx]++];
  370. memset(ret, 0, sizeof(*ret));
  371. ret->pix_id = key;
  372. return ret;
  373. }
  374. static int epic_add_pixel_to_cache(ePICPixHash *hash, uint32_t key, uint32_t pix)
  375. {
  376. ePICPixListElem *new_elem;
  377. ePICPixHashElem *hash_elem = epic_hash_find(hash, key);
  378. if (!hash_elem) {
  379. if (!(hash_elem = epic_hash_add(hash, key)))
  380. return AVERROR(ENOMEM);
  381. }
  382. new_elem = av_mallocz(sizeof(*new_elem));
  383. if (!new_elem)
  384. return AVERROR(ENOMEM);
  385. new_elem->pixel = pix;
  386. new_elem->next = hash_elem->list;
  387. hash_elem->list = new_elem;
  388. return 0;
  389. }
  390. static inline int epic_cache_entries_for_pixel(const ePICPixHash *hash,
  391. uint32_t pix)
  392. {
  393. ePICPixHashElem *hash_elem = epic_hash_find(hash, pix);
  394. if (hash_elem != NULL && hash_elem->list != NULL)
  395. return 1;
  396. return 0;
  397. }
  398. static void epic_free_pixel_cache(ePICPixHash *hash)
  399. {
  400. int i, j;
  401. for (i = 0; i < EPIC_HASH_SIZE; i++) {
  402. for (j = 0; j < hash->bucket_fill[i]; j++) {
  403. ePICPixListElem *list_elem = hash->bucket[i][j].list;
  404. while (list_elem) {
  405. ePICPixListElem *tmp = list_elem->next;
  406. av_free(list_elem);
  407. list_elem = tmp;
  408. }
  409. }
  410. av_freep(&hash->bucket[i]);
  411. hash->bucket_size[i] =
  412. hash->bucket_fill[i] = 0;
  413. }
  414. }
  415. static inline int is_pixel_on_stack(const ePICContext *dc, uint32_t pix)
  416. {
  417. int i;
  418. for (i = 0; i < dc->stack_pos; i++)
  419. if (dc->stack[i] == pix)
  420. break;
  421. return i != dc->stack_pos;
  422. }
  423. #define TOSIGNED(val) (((val) >> 1) ^ -((val) & 1))
  424. static inline int epic_decode_component_pred(ePICContext *dc,
  425. int N, int W, int NW)
  426. {
  427. unsigned delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
  428. return mid_pred(N, N + W - NW, W) - TOSIGNED(delta);
  429. }
  430. static uint32_t epic_decode_pixel_pred(ePICContext *dc, int x, int y,
  431. const uint32_t *curr_row,
  432. const uint32_t *above_row)
  433. {
  434. uint32_t N, W, NW, pred;
  435. unsigned delta;
  436. int GN, GW, GNW, R, G, B;
  437. if (x && y) {
  438. W = curr_row[x - 1];
  439. N = above_row[x];
  440. NW = above_row[x - 1];
  441. GN = (N >> G_shift) & 0xFF;
  442. GW = (W >> G_shift) & 0xFF;
  443. GNW = (NW >> G_shift) & 0xFF;
  444. G = epic_decode_component_pred(dc, GN, GW, GNW);
  445. R = G + epic_decode_component_pred(dc,
  446. ((N >> R_shift) & 0xFF) - GN,
  447. ((W >> R_shift) & 0xFF) - GW,
  448. ((NW >> R_shift) & 0xFF) - GNW);
  449. B = G + epic_decode_component_pred(dc,
  450. ((N >> B_shift) & 0xFF) - GN,
  451. ((W >> B_shift) & 0xFF) - GW,
  452. ((NW >> B_shift) & 0xFF) - GNW);
  453. } else {
  454. if (x)
  455. pred = curr_row[x - 1];
  456. else
  457. pred = above_row[x];
  458. delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
  459. R = ((pred >> R_shift) & 0xFF) - TOSIGNED(delta);
  460. delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
  461. G = ((pred >> G_shift) & 0xFF) - TOSIGNED(delta);
  462. delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
  463. B = ((pred >> B_shift) & 0xFF) - TOSIGNED(delta);
  464. }
  465. if (R<0 || G<0 || B<0) {
  466. av_log(NULL, AV_LOG_ERROR, "RGB %d %d %d is out of range\n", R, G, B);
  467. return 0;
  468. }
  469. return (R << R_shift) | (G << G_shift) | (B << B_shift);
  470. }
  471. static int epic_predict_pixel(ePICContext *dc, uint8_t *rung,
  472. uint32_t *pPix, uint32_t pix)
  473. {
  474. if (!ff_els_decode_bit(&dc->els_ctx, rung)) {
  475. *pPix = pix;
  476. return 1;
  477. }
  478. dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
  479. return 0;
  480. }
  481. static int epic_handle_edges(ePICContext *dc, int x, int y,
  482. const uint32_t *curr_row,
  483. const uint32_t *above_row, uint32_t *pPix)
  484. {
  485. uint32_t pix;
  486. if (!x && !y) { /* special case: top-left pixel */
  487. /* the top-left pixel is coded independently with 3 unsigned numbers */
  488. *pPix = (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << R_shift) |
  489. (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << G_shift) |
  490. (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << B_shift);
  491. return 1;
  492. }
  493. if (x) { /* predict from W first */
  494. pix = curr_row[x - 1];
  495. if (epic_predict_pixel(dc, &dc->W_flag_rung, pPix, pix))
  496. return 1;
  497. }
  498. if (y) { /* then try to predict from N */
  499. pix = above_row[x];
  500. if (!dc->stack_pos || dc->stack[0] != pix) {
  501. if (epic_predict_pixel(dc, &dc->N_flag_rung, pPix, pix))
  502. return 1;
  503. }
  504. }
  505. return 0;
  506. }
  507. static int epic_decode_run_length(ePICContext *dc, int x, int y, int tile_width,
  508. const uint32_t *curr_row,
  509. const uint32_t *above_row,
  510. const uint32_t *above2_row,
  511. uint32_t *pPix, int *pRun)
  512. {
  513. int idx, got_pixel = 0, WWneW, old_WWneW = 0;
  514. uint32_t W, WW, N, NN, NW, NE, NWW, NNW, NNE;
  515. *pRun = 0;
  516. LOAD_NEIGHBOURS(x);
  517. if (dc->next_run_pos == x) {
  518. /* can't reuse W for the new pixel in this case */
  519. WWneW = 1;
  520. } else {
  521. idx = (WW != W) << 7 |
  522. (NW != W) << 6 |
  523. (N != NE) << 5 |
  524. (NW != N) << 4 |
  525. (NWW != NW) << 3 |
  526. (NNE != NE) << 2 |
  527. (NN != N) << 1 |
  528. (NNW != NW);
  529. WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
  530. }
  531. if (WWneW)
  532. dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = W;
  533. else {
  534. *pPix = W;
  535. got_pixel = 1;
  536. }
  537. do {
  538. int NWneW = 1;
  539. if (got_pixel) // pixel value already known (derived from either W or N)
  540. NWneW = *pPix != N;
  541. else { // pixel value is unknown and will be decoded later
  542. NWneW = *pRun ? NWneW : NW != W;
  543. /* TODO: RFC this mess! */
  544. switch (((NW != N) << 2) | (NWneW << 1) | WWneW) {
  545. case 0:
  546. break; // do nothing here
  547. case 3:
  548. case 5:
  549. case 6:
  550. case 7:
  551. if (!is_pixel_on_stack(dc, N)) {
  552. idx = WWneW << 8 |
  553. (*pRun ? old_WWneW : WW != W) << 7 |
  554. NWneW << 6 |
  555. (N != NE) << 5 |
  556. (NW != N) << 4 |
  557. (NWW != NW) << 3 |
  558. (NNE != NE) << 2 |
  559. (NN != N) << 1 |
  560. (NNW != NW);
  561. if (!ff_els_decode_bit(&dc->els_ctx, &dc->N_ctx_rung[idx])) {
  562. NWneW = 0;
  563. *pPix = N;
  564. got_pixel = 1;
  565. break;
  566. }
  567. }
  568. /* fall through */
  569. default:
  570. NWneW = 1;
  571. old_WWneW = WWneW;
  572. if (!is_pixel_on_stack(dc, N))
  573. dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = N;
  574. }
  575. }
  576. (*pRun)++;
  577. if (x + *pRun >= tile_width - 1)
  578. break;
  579. UPDATE_NEIGHBOURS(x + *pRun);
  580. if (!NWneW && NW == N && N == NE) {
  581. int pos, run, rle;
  582. int start_pos = x + *pRun;
  583. /* scan for a run of pix in the line above */
  584. uint32_t pix = above_row[start_pos + 1];
  585. for (pos = start_pos + 2; pos < tile_width; pos++)
  586. if (!(above_row[pos] == pix))
  587. break;
  588. run = pos - start_pos - 1;
  589. idx = av_ceil_log2(run);
  590. if (ff_els_decode_bit(&dc->els_ctx, &dc->prev_row_rung[idx]))
  591. *pRun += run;
  592. else {
  593. int flag;
  594. /* run-length is coded as plain binary number of idx - 1 bits */
  595. for (pos = idx - 1, rle = 0, flag = 0; pos >= 0; pos--) {
  596. if ((1 << pos) + rle < run &&
  597. ff_els_decode_bit(&dc->els_ctx,
  598. flag ? &dc->runlen_one
  599. : &dc->runlen_zeroes[pos])) {
  600. flag = 1;
  601. rle |= 1 << pos;
  602. }
  603. }
  604. *pRun += rle;
  605. break; // return immediately
  606. }
  607. if (x + *pRun >= tile_width - 1)
  608. break;
  609. LOAD_NEIGHBOURS(x + *pRun);
  610. WWneW = 0;
  611. NWneW = 0;
  612. }
  613. idx = WWneW << 7 |
  614. NWneW << 6 |
  615. (N != NE) << 5 |
  616. (NW != N) << 4 |
  617. (NWW != NW) << 3 |
  618. (NNE != NE) << 2 |
  619. (NN != N) << 1 |
  620. (NNW != NW);
  621. WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
  622. } while (!WWneW);
  623. dc->next_run_pos = x + *pRun;
  624. return got_pixel;
  625. }
  626. static int epic_predict_pixel2(ePICContext *dc, uint8_t *rung,
  627. uint32_t *pPix, uint32_t pix)
  628. {
  629. if (ff_els_decode_bit(&dc->els_ctx, rung)) {
  630. *pPix = pix;
  631. return 1;
  632. }
  633. dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
  634. return 0;
  635. }
  636. static int epic_predict_from_NW_NE(ePICContext *dc, int x, int y, int run,
  637. int tile_width, const uint32_t *curr_row,
  638. const uint32_t *above_row, uint32_t *pPix)
  639. {
  640. int pos;
  641. /* try to reuse the NW pixel first */
  642. if (x && y) {
  643. uint32_t NW = above_row[x - 1];
  644. if (NW != curr_row[x - 1] && NW != above_row[x] && !is_pixel_on_stack(dc, NW)) {
  645. if (epic_predict_pixel2(dc, &dc->nw_pred_rung[NW & 0xFF], pPix, NW))
  646. return 1;
  647. }
  648. }
  649. /* try to reuse the NE[x + run, y] pixel */
  650. pos = x + run - 1;
  651. if (pos < tile_width - 1 && y) {
  652. uint32_t NE = above_row[pos + 1];
  653. if (NE != above_row[pos] && !is_pixel_on_stack(dc, NE)) {
  654. if (epic_predict_pixel2(dc, &dc->ne_pred_rung[NE & 0xFF], pPix, NE))
  655. return 1;
  656. }
  657. }
  658. return 0;
  659. }
  660. static int epic_decode_from_cache(ePICContext *dc, uint32_t W, uint32_t *pPix)
  661. {
  662. ePICPixListElem *list, *prev = NULL;
  663. ePICPixHashElem *hash_elem = epic_hash_find(&dc->hash, W);
  664. if (!hash_elem || !hash_elem->list)
  665. return 0;
  666. list = hash_elem->list;
  667. while (list) {
  668. if (!is_pixel_on_stack(dc, list->pixel)) {
  669. if (ff_els_decode_bit(&dc->els_ctx, &list->rung)) {
  670. *pPix = list->pixel;
  671. if (list != hash_elem->list) {
  672. prev->next = list->next;
  673. list->next = hash_elem->list;
  674. hash_elem->list = list;
  675. }
  676. return 1;
  677. }
  678. dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = list->pixel;
  679. }
  680. prev = list;
  681. list = list->next;
  682. }
  683. return 0;
  684. }
  685. static int epic_decode_tile(ePICContext *dc, uint8_t *out, int tile_height,
  686. int tile_width, int stride)
  687. {
  688. int x, y;
  689. uint32_t pix;
  690. uint32_t *curr_row = NULL, *above_row = NULL, *above2_row;
  691. for (y = 0; y < tile_height; y++, out += stride) {
  692. above2_row = above_row;
  693. above_row = curr_row;
  694. curr_row = (uint32_t *) out;
  695. for (x = 0, dc->next_run_pos = 0; x < tile_width;) {
  696. if (dc->els_ctx.err)
  697. return AVERROR_INVALIDDATA; // bail out in the case of ELS overflow
  698. pix = curr_row[x - 1]; // get W pixel
  699. if (y >= 1 && x >= 2 &&
  700. pix != curr_row[x - 2] && pix != above_row[x - 1] &&
  701. pix != above_row[x - 2] && pix != above_row[x] &&
  702. !epic_cache_entries_for_pixel(&dc->hash, pix)) {
  703. curr_row[x] = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
  704. x++;
  705. } else {
  706. int got_pixel, run;
  707. dc->stack_pos = 0; // empty stack
  708. if (y < 2 || x < 2 || x == tile_width - 1) {
  709. run = 1;
  710. got_pixel = epic_handle_edges(dc, x, y, curr_row, above_row, &pix);
  711. } else
  712. got_pixel = epic_decode_run_length(dc, x, y, tile_width,
  713. curr_row, above_row,
  714. above2_row, &pix, &run);
  715. if (!got_pixel && !epic_predict_from_NW_NE(dc, x, y, run,
  716. tile_width, curr_row,
  717. above_row, &pix)) {
  718. uint32_t ref_pix = curr_row[x - 1];
  719. if (!x || !epic_decode_from_cache(dc, ref_pix, &pix)) {
  720. pix = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
  721. if (x) {
  722. int ret = epic_add_pixel_to_cache(&dc->hash,
  723. ref_pix,
  724. pix);
  725. if (ret)
  726. return ret;
  727. }
  728. }
  729. }
  730. for (; run > 0; x++, run--)
  731. curr_row[x] = pix;
  732. }
  733. }
  734. }
  735. return 0;
  736. }
  737. static int epic_jb_decode_tile(G2MContext *c, int tile_x, int tile_y,
  738. const uint8_t *src, size_t src_size,
  739. AVCodecContext *avctx)
  740. {
  741. uint8_t prefix, mask = 0x80;
  742. int extrabytes, tile_width, tile_height, awidth, aheight;
  743. size_t els_dsize;
  744. uint8_t *dst;
  745. if (!src_size)
  746. return 0;
  747. /* get data size of the ELS partition as unsigned variable-length integer */
  748. prefix = *src++;
  749. src_size--;
  750. for (extrabytes = 0; (prefix & mask) && (extrabytes < 7); extrabytes++)
  751. mask >>= 1;
  752. if (extrabytes > 3 || src_size < extrabytes) {
  753. av_log(avctx, AV_LOG_ERROR, "ePIC: invalid data size VLI\n");
  754. return AVERROR_INVALIDDATA;
  755. }
  756. els_dsize = prefix & ((0x80 >> extrabytes) - 1); // mask out the length prefix
  757. while (extrabytes-- > 0) {
  758. els_dsize = (els_dsize << 8) | *src++;
  759. src_size--;
  760. }
  761. if (src_size < els_dsize) {
  762. av_log(avctx, AV_LOG_ERROR, "ePIC: data too short, needed %zu, got %zu\n",
  763. els_dsize, src_size);
  764. return AVERROR_INVALIDDATA;
  765. }
  766. tile_width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
  767. tile_height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
  768. awidth = FFALIGN(tile_width, 16);
  769. aheight = FFALIGN(tile_height, 16);
  770. if (els_dsize) {
  771. int ret, i, j, k;
  772. uint8_t tr_r, tr_g, tr_b, *buf;
  773. uint32_t *in;
  774. /* ELS decoder initializations */
  775. memset(&c->ec, 0, sizeof(c->ec));
  776. ff_els_decoder_init(&c->ec.els_ctx, src, els_dsize);
  777. epic_hash_init(&c->ec.hash);
  778. /* decode transparent pixel value */
  779. tr_r = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
  780. tr_g = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
  781. tr_b = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
  782. if (c->ec.els_ctx.err != 0) {
  783. av_log(avctx, AV_LOG_ERROR,
  784. "ePIC: couldn't decode transparency pixel!\n");
  785. return AVERROR_INVALIDDATA;
  786. }
  787. ret = epic_decode_tile(&c->ec, c->epic_buf, tile_height, tile_width,
  788. c->epic_buf_stride);
  789. epic_free_pixel_cache(&c->ec.hash);
  790. ff_els_decoder_uninit(&c->ec.unsigned_rung);
  791. if (ret) {
  792. av_log(avctx, AV_LOG_ERROR,
  793. "ePIC: tile decoding failed, frame=%d, tile_x=%d, tile_y=%d\n",
  794. avctx->frame_number, tile_x, tile_y);
  795. return AVERROR_INVALIDDATA;
  796. }
  797. buf = c->epic_buf;
  798. dst = c->framebuf + tile_x * c->tile_width * 3 +
  799. tile_y * c->tile_height * c->framebuf_stride;
  800. for (j = 0; j < tile_height; j++) {
  801. uint8_t *out = dst;
  802. in = (uint32_t *) buf;
  803. for (i = 0; i < tile_width; i++) {
  804. out[0] = (in[i] >> R_shift) & 0xFF;
  805. out[1] = (in[i] >> G_shift) & 0xFF;
  806. out[2] = (in[i] >> B_shift) & 0xFF;
  807. out += 3;
  808. }
  809. buf += c->epic_buf_stride;
  810. dst += c->framebuf_stride;
  811. }
  812. if (src_size > els_dsize) {
  813. uint8_t *jpg;
  814. uint32_t tr;
  815. int bstride = FFALIGN(tile_width, 16) >> 3;
  816. int nblocks = 0;
  817. int estride = c->epic_buf_stride >> 2;
  818. src += els_dsize;
  819. src_size -= els_dsize;
  820. in = (uint32_t *) c->epic_buf;
  821. tr = (tr_r << R_shift) | (tr_g << G_shift) | (tr_b << B_shift);
  822. memset(c->kempf_flags, 0,
  823. (aheight >> 3) * bstride * sizeof(*c->kempf_flags));
  824. for (j = 0; j < tile_height; j += 8) {
  825. for (i = 0; i < tile_width; i += 8) {
  826. c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 0;
  827. for (k = 0; k < 8 * 8; k++) {
  828. if (in[i + (k & 7) + (k >> 3) * estride] == tr) {
  829. c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 1;
  830. nblocks++;
  831. break;
  832. }
  833. }
  834. }
  835. in += 8 * estride;
  836. }
  837. memset(c->jpeg_tile, 0, c->tile_stride * aheight);
  838. jpg_decode_data(&c->jc, awidth, aheight, src, src_size,
  839. c->jpeg_tile, c->tile_stride,
  840. c->kempf_flags, bstride, nblocks, c->swapuv);
  841. in = (uint32_t *) c->epic_buf;
  842. dst = c->framebuf + tile_x * c->tile_width * 3 +
  843. tile_y * c->tile_height * c->framebuf_stride;
  844. jpg = c->jpeg_tile;
  845. for (j = 0; j < tile_height; j++) {
  846. for (i = 0; i < tile_width; i++)
  847. if (in[i] == tr)
  848. memcpy(dst + i * 3, jpg + i * 3, 3);
  849. in += c->epic_buf_stride >> 2;
  850. dst += c->framebuf_stride;
  851. jpg += c->tile_stride;
  852. }
  853. }
  854. } else {
  855. dst = c->framebuf + tile_x * c->tile_width * 3 +
  856. tile_y * c->tile_height * c->framebuf_stride;
  857. return jpg_decode_data(&c->jc, tile_width, tile_height, src, src_size,
  858. dst, c->framebuf_stride, NULL, 0, 0, c->swapuv);
  859. }
  860. return 0;
  861. }
  862. static int kempf_restore_buf(const uint8_t *src, int len,
  863. uint8_t *dst, int stride,
  864. const uint8_t *jpeg_tile, int tile_stride,
  865. int width, int height,
  866. const uint8_t *pal, int npal, int tidx)
  867. {
  868. GetBitContext gb;
  869. int i, j, nb, col;
  870. int ret;
  871. int align_width = FFALIGN(width, 16);
  872. if ((ret = init_get_bits8(&gb, src, len)) < 0)
  873. return ret;
  874. if (npal <= 2) nb = 1;
  875. else if (npal <= 4) nb = 2;
  876. else if (npal <= 16) nb = 4;
  877. else nb = 8;
  878. for (j = 0; j < height; j++, dst += stride, jpeg_tile += tile_stride) {
  879. if (get_bits(&gb, 8))
  880. continue;
  881. for (i = 0; i < width; i++) {
  882. col = get_bits(&gb, nb);
  883. if (col != tidx)
  884. memcpy(dst + i * 3, pal + col * 3, 3);
  885. else
  886. memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
  887. }
  888. skip_bits_long(&gb, nb * (align_width - width));
  889. }
  890. return 0;
  891. }
  892. static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y,
  893. const uint8_t *src, int src_size)
  894. {
  895. int width, height;
  896. int hdr, zsize, npal, tidx = -1, ret;
  897. int i, j;
  898. const uint8_t *src_end = src + src_size;
  899. uint8_t pal[768], transp[3];
  900. uLongf dlen = (c->tile_width + 1) * c->tile_height;
  901. int sub_type;
  902. int nblocks, cblocks, bstride;
  903. int bits, bitbuf, coded;
  904. uint8_t *dst = c->framebuf + tile_x * c->tile_width * 3 +
  905. tile_y * c->tile_height * c->framebuf_stride;
  906. if (src_size < 2)
  907. return AVERROR_INVALIDDATA;
  908. width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
  909. height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
  910. hdr = *src++;
  911. sub_type = hdr >> 5;
  912. if (sub_type == 0) {
  913. int j;
  914. memcpy(transp, src, 3);
  915. src += 3;
  916. for (j = 0; j < height; j++, dst += c->framebuf_stride)
  917. for (i = 0; i < width; i++)
  918. memcpy(dst + i * 3, transp, 3);
  919. return 0;
  920. } else if (sub_type == 1) {
  921. return jpg_decode_data(&c->jc, width, height, src, src_end - src,
  922. dst, c->framebuf_stride, NULL, 0, 0, 0);
  923. }
  924. if (sub_type != 2) {
  925. memcpy(transp, src, 3);
  926. src += 3;
  927. }
  928. npal = *src++ + 1;
  929. if (src_end - src < npal * 3)
  930. return AVERROR_INVALIDDATA;
  931. memcpy(pal, src, npal * 3);
  932. src += npal * 3;
  933. if (sub_type != 2) {
  934. for (i = 0; i < npal; i++) {
  935. if (!memcmp(pal + i * 3, transp, 3)) {
  936. tidx = i;
  937. break;
  938. }
  939. }
  940. }
  941. if (src_end - src < 2)
  942. return 0;
  943. zsize = (src[0] << 8) | src[1];
  944. src += 2;
  945. if (src_end - src < zsize + (sub_type != 2))
  946. return AVERROR_INVALIDDATA;
  947. ret = uncompress(c->kempf_buf, &dlen, src, zsize);
  948. if (ret)
  949. return AVERROR_INVALIDDATA;
  950. src += zsize;
  951. if (sub_type == 2) {
  952. kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
  953. NULL, 0, width, height, pal, npal, tidx);
  954. return 0;
  955. }
  956. nblocks = *src++ + 1;
  957. cblocks = 0;
  958. bstride = FFALIGN(width, 16) >> 3;
  959. // blocks are coded LSB and we need normal bitreader for JPEG data
  960. bits = 0;
  961. for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
  962. for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
  963. if (!bits) {
  964. if (src >= src_end)
  965. return AVERROR_INVALIDDATA;
  966. bitbuf = *src++;
  967. bits = 8;
  968. }
  969. coded = bitbuf & 1;
  970. bits--;
  971. bitbuf >>= 1;
  972. cblocks += coded;
  973. if (cblocks > nblocks)
  974. return AVERROR_INVALIDDATA;
  975. c->kempf_flags[j * 2 + i * 2 * bstride] =
  976. c->kempf_flags[j * 2 + 1 + i * 2 * bstride] =
  977. c->kempf_flags[j * 2 + (i * 2 + 1) * bstride] =
  978. c->kempf_flags[j * 2 + 1 + (i * 2 + 1) * bstride] = coded;
  979. }
  980. }
  981. memset(c->jpeg_tile, 0, c->tile_stride * height);
  982. jpg_decode_data(&c->jc, width, height, src, src_end - src,
  983. c->jpeg_tile, c->tile_stride,
  984. c->kempf_flags, bstride, nblocks * 4, 0);
  985. kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
  986. c->jpeg_tile, c->tile_stride,
  987. width, height, pal, npal, tidx);
  988. return 0;
  989. }
  990. static int g2m_init_buffers(G2MContext *c)
  991. {
  992. int aligned_height;
  993. if (!c->framebuf || c->old_width < c->width || c->old_height < c->height) {
  994. c->framebuf_stride = FFALIGN(c->width + 15, 16) * 3;
  995. aligned_height = c->height + 15;
  996. av_free(c->framebuf);
  997. c->framebuf = av_mallocz_array(c->framebuf_stride, aligned_height);
  998. if (!c->framebuf)
  999. return AVERROR(ENOMEM);
  1000. }
  1001. if (!c->synth_tile || !c->jpeg_tile ||
  1002. (c->compression == 2 && !c->epic_buf_base) ||
  1003. c->old_tile_w < c->tile_width ||
  1004. c->old_tile_h < c->tile_height) {
  1005. c->tile_stride = FFALIGN(c->tile_width, 16) * 3;
  1006. c->epic_buf_stride = FFALIGN(c->tile_width * 4, 16);
  1007. aligned_height = FFALIGN(c->tile_height, 16);
  1008. av_freep(&c->synth_tile);
  1009. av_freep(&c->jpeg_tile);
  1010. av_freep(&c->kempf_buf);
  1011. av_freep(&c->kempf_flags);
  1012. av_freep(&c->epic_buf_base);
  1013. c->epic_buf = NULL;
  1014. c->synth_tile = av_mallocz(c->tile_stride * aligned_height);
  1015. c->jpeg_tile = av_mallocz(c->tile_stride * aligned_height);
  1016. c->kempf_buf = av_mallocz((c->tile_width + 1) * aligned_height +
  1017. AV_INPUT_BUFFER_PADDING_SIZE);
  1018. c->kempf_flags = av_mallocz(c->tile_width * aligned_height);
  1019. if (!c->synth_tile || !c->jpeg_tile ||
  1020. !c->kempf_buf || !c->kempf_flags)
  1021. return AVERROR(ENOMEM);
  1022. if (c->compression == 2) {
  1023. c->epic_buf_base = av_mallocz(c->epic_buf_stride * aligned_height + 4);
  1024. if (!c->epic_buf_base)
  1025. return AVERROR(ENOMEM);
  1026. c->epic_buf = c->epic_buf_base + 4;
  1027. }
  1028. }
  1029. return 0;
  1030. }
  1031. static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c,
  1032. GetByteContext *gb)
  1033. {
  1034. int i, j, k;
  1035. uint8_t *dst;
  1036. uint32_t bits;
  1037. uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
  1038. uint32_t cursor_hot_x, cursor_hot_y;
  1039. int cursor_fmt, err;
  1040. cur_size = bytestream2_get_be32(gb);
  1041. cursor_w = bytestream2_get_byte(gb);
  1042. cursor_h = bytestream2_get_byte(gb);
  1043. cursor_hot_x = bytestream2_get_byte(gb);
  1044. cursor_hot_y = bytestream2_get_byte(gb);
  1045. cursor_fmt = bytestream2_get_byte(gb);
  1046. cursor_stride = FFALIGN(cursor_w, cursor_fmt==1 ? 32 : 1) * 4;
  1047. if (cursor_w < 1 || cursor_w > 256 ||
  1048. cursor_h < 1 || cursor_h > 256) {
  1049. av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %"PRIu32"x%"PRIu32"\n",
  1050. cursor_w, cursor_h);
  1051. return AVERROR_INVALIDDATA;
  1052. }
  1053. if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
  1054. av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %"PRIu32",%"PRIu32"\n",
  1055. cursor_hot_x, cursor_hot_y);
  1056. cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);
  1057. cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);
  1058. }
  1059. if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||
  1060. c->cursor_w * c->cursor_h / 4 > cur_size) {
  1061. av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"/%u\n",
  1062. cur_size, bytestream2_get_bytes_left(gb));
  1063. return AVERROR_INVALIDDATA;
  1064. }
  1065. if (cursor_fmt != 1 && cursor_fmt != 32) {
  1066. avpriv_report_missing_feature(avctx, "Cursor format %d",
  1067. cursor_fmt);
  1068. return AVERROR_PATCHWELCOME;
  1069. }
  1070. if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
  1071. av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
  1072. return err;
  1073. }
  1074. c->cursor_w = cursor_w;
  1075. c->cursor_h = cursor_h;
  1076. c->cursor_hot_x = cursor_hot_x;
  1077. c->cursor_hot_y = cursor_hot_y;
  1078. c->cursor_fmt = cursor_fmt;
  1079. c->cursor_stride = cursor_stride;
  1080. dst = c->cursor;
  1081. switch (c->cursor_fmt) {
  1082. case 1: // old monochrome
  1083. for (j = 0; j < c->cursor_h; j++) {
  1084. for (i = 0; i < c->cursor_w; i += 32) {
  1085. bits = bytestream2_get_be32(gb);
  1086. for (k = 0; k < 32; k++) {
  1087. dst[0] = !!(bits & 0x80000000);
  1088. dst += 4;
  1089. bits <<= 1;
  1090. }
  1091. }
  1092. }
  1093. dst = c->cursor;
  1094. for (j = 0; j < c->cursor_h; j++) {
  1095. for (i = 0; i < c->cursor_w; i += 32) {
  1096. bits = bytestream2_get_be32(gb);
  1097. for (k = 0; k < 32; k++) {
  1098. int mask_bit = !!(bits & 0x80000000);
  1099. switch (dst[0] * 2 + mask_bit) {
  1100. case 0:
  1101. dst[0] = 0xFF;
  1102. dst[1] = 0x00;
  1103. dst[2] = 0x00;
  1104. dst[3] = 0x00;
  1105. break;
  1106. case 1:
  1107. dst[0] = 0xFF;
  1108. dst[1] = 0xFF;
  1109. dst[2] = 0xFF;
  1110. dst[3] = 0xFF;
  1111. break;
  1112. default:
  1113. dst[0] = 0x00;
  1114. dst[1] = 0x00;
  1115. dst[2] = 0x00;
  1116. dst[3] = 0x00;
  1117. }
  1118. dst += 4;
  1119. bits <<= 1;
  1120. }
  1121. }
  1122. }
  1123. break;
  1124. case 32: // full colour
  1125. /* skip monochrome version of the cursor and decode RGBA instead */
  1126. bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
  1127. for (j = 0; j < c->cursor_h; j++) {
  1128. for (i = 0; i < c->cursor_w; i++) {
  1129. int val = bytestream2_get_be32(gb);
  1130. *dst++ = val >> 0;
  1131. *dst++ = val >> 8;
  1132. *dst++ = val >> 16;
  1133. *dst++ = val >> 24;
  1134. }
  1135. }
  1136. break;
  1137. default:
  1138. return AVERROR_PATCHWELCOME;
  1139. }
  1140. return 0;
  1141. }
  1142. #define APPLY_ALPHA(src, new, alpha) \
  1143. src = (src * (256 - alpha) + new * alpha) >> 8
  1144. static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
  1145. {
  1146. int i, j;
  1147. int x, y, w, h;
  1148. const uint8_t *cursor;
  1149. if (!c->cursor)
  1150. return;
  1151. x = c->cursor_x - c->cursor_hot_x;
  1152. y = c->cursor_y - c->cursor_hot_y;
  1153. cursor = c->cursor;
  1154. w = c->cursor_w;
  1155. h = c->cursor_h;
  1156. if (x + w > c->width)
  1157. w = c->width - x;
  1158. if (y + h > c->height)
  1159. h = c->height - y;
  1160. if (x < 0) {
  1161. w += x;
  1162. cursor += -x * 4;
  1163. } else {
  1164. dst += x * 3;
  1165. }
  1166. if (y < 0) {
  1167. h += y;
  1168. cursor += -y * c->cursor_stride;
  1169. } else {
  1170. dst += y * stride;
  1171. }
  1172. if (w < 0 || h < 0)
  1173. return;
  1174. for (j = 0; j < h; j++) {
  1175. for (i = 0; i < w; i++) {
  1176. uint8_t alpha = cursor[i * 4];
  1177. APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
  1178. APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
  1179. APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
  1180. }
  1181. dst += stride;
  1182. cursor += c->cursor_stride;
  1183. }
  1184. }
  1185. static int g2m_decode_frame(AVCodecContext *avctx, void *data,
  1186. int *got_picture_ptr, AVPacket *avpkt)
  1187. {
  1188. const uint8_t *buf = avpkt->data;
  1189. int buf_size = avpkt->size;
  1190. G2MContext *c = avctx->priv_data;
  1191. AVFrame *pic = data;
  1192. GetByteContext bc, tbc;
  1193. int magic;
  1194. int got_header = 0;
  1195. uint32_t chunk_size, r_mask, g_mask, b_mask;
  1196. int chunk_type, chunk_start;
  1197. int i;
  1198. int ret;
  1199. if (buf_size < 12) {
  1200. av_log(avctx, AV_LOG_ERROR,
  1201. "Frame should have at least 12 bytes, got %d instead\n",
  1202. buf_size);
  1203. return AVERROR_INVALIDDATA;
  1204. }
  1205. bytestream2_init(&bc, buf, buf_size);
  1206. magic = bytestream2_get_be32(&bc);
  1207. if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
  1208. (magic & 0xF) < 2 || (magic & 0xF) > 5) {
  1209. av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
  1210. return AVERROR_INVALIDDATA;
  1211. }
  1212. c->swapuv = magic == MKBETAG('G', '2', 'M', '2');
  1213. while (bytestream2_get_bytes_left(&bc) > 5) {
  1214. chunk_size = bytestream2_get_le32(&bc) - 1;
  1215. chunk_type = bytestream2_get_byte(&bc);
  1216. chunk_start = bytestream2_tell(&bc);
  1217. if (chunk_size > bytestream2_get_bytes_left(&bc)) {
  1218. av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %"PRIu32" type %02X\n",
  1219. chunk_size, chunk_type);
  1220. break;
  1221. }
  1222. switch (chunk_type) {
  1223. case DISPLAY_INFO:
  1224. got_header =
  1225. c->got_header = 0;
  1226. if (chunk_size < 21) {
  1227. av_log(avctx, AV_LOG_ERROR, "Invalid display info size %"PRIu32"\n",
  1228. chunk_size);
  1229. break;
  1230. }
  1231. c->width = bytestream2_get_be32(&bc);
  1232. c->height = bytestream2_get_be32(&bc);
  1233. if (c->width < 16 || c->width > c->orig_width ||
  1234. c->height < 16 || c->height > c->orig_height) {
  1235. av_log(avctx, AV_LOG_ERROR,
  1236. "Invalid frame dimensions %dx%d\n",
  1237. c->width, c->height);
  1238. ret = AVERROR_INVALIDDATA;
  1239. goto header_fail;
  1240. }
  1241. if (c->width != avctx->width || c->height != avctx->height) {
  1242. ret = ff_set_dimensions(avctx, c->width, c->height);
  1243. if (ret < 0)
  1244. goto header_fail;
  1245. }
  1246. c->compression = bytestream2_get_be32(&bc);
  1247. if (c->compression != 2 && c->compression != 3) {
  1248. av_log(avctx, AV_LOG_ERROR,
  1249. "Unknown compression method %d\n",
  1250. c->compression);
  1251. ret = AVERROR_PATCHWELCOME;
  1252. goto header_fail;
  1253. }
  1254. c->tile_width = bytestream2_get_be32(&bc);
  1255. c->tile_height = bytestream2_get_be32(&bc);
  1256. if (c->tile_width <= 0 || c->tile_height <= 0 ||
  1257. ((c->tile_width | c->tile_height) & 0xF) ||
  1258. c->tile_width * 4LL * c->tile_height >= INT_MAX
  1259. ) {
  1260. av_log(avctx, AV_LOG_ERROR,
  1261. "Invalid tile dimensions %dx%d\n",
  1262. c->tile_width, c->tile_height);
  1263. ret = AVERROR_INVALIDDATA;
  1264. goto header_fail;
  1265. }
  1266. c->tiles_x = (c->width + c->tile_width - 1) / c->tile_width;
  1267. c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
  1268. c->bpp = bytestream2_get_byte(&bc);
  1269. if (c->bpp == 32) {
  1270. if (bytestream2_get_bytes_left(&bc) < 16 ||
  1271. (chunk_size - 21) < 16) {
  1272. av_log(avctx, AV_LOG_ERROR,
  1273. "Display info: missing bitmasks!\n");
  1274. ret = AVERROR_INVALIDDATA;
  1275. goto header_fail;
  1276. }
  1277. r_mask = bytestream2_get_be32(&bc);
  1278. g_mask = bytestream2_get_be32(&bc);
  1279. b_mask = bytestream2_get_be32(&bc);
  1280. if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
  1281. av_log(avctx, AV_LOG_ERROR,
  1282. "Invalid or unsupported bitmasks: R=%"PRIX32", G=%"PRIX32", B=%"PRIX32"\n",
  1283. r_mask, g_mask, b_mask);
  1284. ret = AVERROR_PATCHWELCOME;
  1285. goto header_fail;
  1286. }
  1287. } else {
  1288. avpriv_request_sample(avctx, "bpp=%d", c->bpp);
  1289. ret = AVERROR_PATCHWELCOME;
  1290. goto header_fail;
  1291. }
  1292. if (g2m_init_buffers(c)) {
  1293. ret = AVERROR(ENOMEM);
  1294. goto header_fail;
  1295. }
  1296. got_header = 1;
  1297. break;
  1298. case TILE_DATA:
  1299. if (!c->tiles_x || !c->tiles_y) {
  1300. av_log(avctx, AV_LOG_WARNING,
  1301. "No display info - skipping tile\n");
  1302. break;
  1303. }
  1304. if (chunk_size < 2) {
  1305. av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %"PRIu32"\n",
  1306. chunk_size);
  1307. break;
  1308. }
  1309. c->tile_x = bytestream2_get_byte(&bc);
  1310. c->tile_y = bytestream2_get_byte(&bc);
  1311. if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
  1312. av_log(avctx, AV_LOG_ERROR,
  1313. "Invalid tile pos %d,%d (in %dx%d grid)\n",
  1314. c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
  1315. break;
  1316. }
  1317. ret = 0;
  1318. switch (c->compression) {
  1319. case COMPR_EPIC_J_B:
  1320. ret = epic_jb_decode_tile(c, c->tile_x, c->tile_y,
  1321. buf + bytestream2_tell(&bc),
  1322. chunk_size - 2, avctx);
  1323. break;
  1324. case COMPR_KEMPF_J_B:
  1325. ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
  1326. buf + bytestream2_tell(&bc),
  1327. chunk_size - 2);
  1328. break;
  1329. }
  1330. if (ret && c->framebuf)
  1331. av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
  1332. c->tile_x, c->tile_y);
  1333. break;
  1334. case CURSOR_POS:
  1335. if (chunk_size < 5) {
  1336. av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %"PRIu32"\n",
  1337. chunk_size);
  1338. break;
  1339. }
  1340. c->cursor_x = bytestream2_get_be16(&bc);
  1341. c->cursor_y = bytestream2_get_be16(&bc);
  1342. break;
  1343. case CURSOR_SHAPE:
  1344. if (chunk_size < 8) {
  1345. av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"\n",
  1346. chunk_size);
  1347. break;
  1348. }
  1349. bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
  1350. chunk_size - 4);
  1351. g2m_load_cursor(avctx, c, &tbc);
  1352. break;
  1353. case CHUNK_CC:
  1354. case CHUNK_CD:
  1355. break;
  1356. default:
  1357. av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02d\n",
  1358. chunk_type);
  1359. }
  1360. /* navigate to next chunk */
  1361. bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
  1362. }
  1363. if (got_header)
  1364. c->got_header = 1;
  1365. if (c->width && c->height && c->framebuf) {
  1366. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  1367. return ret;
  1368. pic->key_frame = got_header;
  1369. pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  1370. for (i = 0; i < avctx->height; i++)
  1371. memcpy(pic->data[0] + i * pic->linesize[0],
  1372. c->framebuf + i * c->framebuf_stride,
  1373. c->width * 3);
  1374. g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);
  1375. *got_picture_ptr = 1;
  1376. }
  1377. return buf_size;
  1378. header_fail:
  1379. c->width =
  1380. c->height = 0;
  1381. c->tiles_x =
  1382. c->tiles_y = 0;
  1383. return ret;
  1384. }
  1385. static av_cold int g2m_decode_init(AVCodecContext *avctx)
  1386. {
  1387. G2MContext *const c = avctx->priv_data;
  1388. int ret;
  1389. if ((ret = jpg_init(avctx, &c->jc)) != 0) {
  1390. av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
  1391. jpg_free_context(&c->jc);
  1392. return AVERROR(ENOMEM);
  1393. }
  1394. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  1395. // store original sizes and check against those if resize happens
  1396. c->orig_width = avctx->width;
  1397. c->orig_height = avctx->height;
  1398. return 0;
  1399. }
  1400. static av_cold int g2m_decode_end(AVCodecContext *avctx)
  1401. {
  1402. G2MContext *const c = avctx->priv_data;
  1403. jpg_free_context(&c->jc);
  1404. av_freep(&c->epic_buf_base);
  1405. c->epic_buf = NULL;
  1406. av_freep(&c->kempf_buf);
  1407. av_freep(&c->kempf_flags);
  1408. av_freep(&c->synth_tile);
  1409. av_freep(&c->jpeg_tile);
  1410. av_freep(&c->cursor);
  1411. av_freep(&c->framebuf);
  1412. return 0;
  1413. }
  1414. AVCodec ff_g2m_decoder = {
  1415. .name = "g2m",
  1416. .long_name = NULL_IF_CONFIG_SMALL("Go2Meeting"),
  1417. .type = AVMEDIA_TYPE_VIDEO,
  1418. .id = AV_CODEC_ID_G2M,
  1419. .priv_data_size = sizeof(G2MContext),
  1420. .init = g2m_decode_init,
  1421. .close = g2m_decode_end,
  1422. .decode = g2m_decode_frame,
  1423. .capabilities = AV_CODEC_CAP_DR1,
  1424. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  1425. };