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.

1638 lines
52KB

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