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.

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