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.

1649 lines
53KB

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