2 * Go2Webinar / Go2Meeting decoder
3 * Copyright (c) 2012 Konstantin Shishkov
4 * Copyright (c) 2013 Maxim Poliakovski
6 * This file is part of FFmpeg.
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.
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.
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
25 * Go2Webinar / Go2Meeting decoder
31 #include "libavutil/intreadwrite.h"
35 #include "bytestream.h"
40 #include "jpegtables.h"
43 #define EPIC_PIX_STACK_SIZE 1024
44 #define EPIC_PIX_STACK_MAX (EPIC_PIX_STACK_SIZE - 1)
60 static const uint8_t luma_quant[64] = {
61 8, 6, 5, 8, 12, 20, 26, 31,
62 6, 6, 7, 10, 13, 29, 30, 28,
63 7, 7, 8, 12, 20, 29, 35, 28,
64 7, 9, 11, 15, 26, 44, 40, 31,
65 9, 11, 19, 28, 34, 55, 52, 39,
66 12, 18, 28, 32, 41, 52, 57, 46,
67 25, 32, 39, 44, 52, 61, 60, 51,
68 36, 46, 48, 49, 56, 50, 52, 50
71 static const uint8_t chroma_quant[64] = {
72 9, 9, 12, 24, 50, 50, 50, 50,
73 9, 11, 13, 33, 50, 50, 50, 50,
74 12, 13, 28, 50, 50, 50, 50, 50,
75 24, 33, 50, 50, 50, 50, 50, 50,
76 50, 50, 50, 50, 50, 50, 50, 50,
77 50, 50, 50, 50, 50, 50, 50, 50,
78 50, 50, 50, 50, 50, 50, 50, 50,
79 50, 50, 50, 50, 50, 50, 50, 50,
82 typedef struct ePICPixListElem {
83 struct ePICPixListElem *next;
88 typedef struct ePICPixHashElem {
90 struct ePICPixListElem *list;
93 #define EPIC_HASH_SIZE 256
94 typedef struct ePICPixHash {
95 ePICPixHashElem *bucket[EPIC_HASH_SIZE];
96 int bucket_size[EPIC_HASH_SIZE];
97 int bucket_fill[EPIC_HASH_SIZE];
100 typedef struct ePICContext {
103 ElsUnsignedRung unsigned_rung;
106 uint8_t W_ctx_rung[256];
107 uint8_t N_ctx_rung[512];
108 uint8_t nw_pred_rung[256];
109 uint8_t ne_pred_rung[256];
110 uint8_t prev_row_rung[14];
111 uint8_t runlen_zeroes[14];
114 uint32_t stack[EPIC_PIX_STACK_SIZE];
118 typedef struct JPGContext {
119 BlockDSPContext bdsp;
123 VLC dc_vlc[2], ac_vlc[2];
125 DECLARE_ALIGNED(16, int16_t, block)[6][64];
130 typedef struct G2MContext {
137 int width, height, bpp;
138 int orig_width, orig_height;
139 int tile_width, tile_height;
140 int tiles_x, tiles_y, tile_x, tile_y;
145 int framebuf_stride, old_width, old_height;
147 uint8_t *synth_tile, *jpeg_tile, *epic_buf, *epic_buf_base;
148 int tile_stride, epic_buf_stride, old_tile_w, old_tile_h;
151 uint8_t *kempf_buf, *kempf_flags;
156 int cursor_w, cursor_h, cursor_x, cursor_y;
157 int cursor_hot_x, cursor_hot_y;
160 static av_cold int build_vlc(VLC *vlc, const uint8_t *bits_table,
161 const uint8_t *val_table, int nb_codes,
164 uint8_t huff_size[256] = { 0 };
165 uint16_t huff_code[256];
166 uint16_t huff_sym[256];
169 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
171 for (i = 0; i < 256; i++)
172 huff_sym[i] = i + 16 * is_ac;
175 huff_sym[0] = 16 * 256;
177 return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
178 huff_code, 2, 2, huff_sym, 2, 2, 0);
181 static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c)
185 ret = build_vlc(&c->dc_vlc[0], avpriv_mjpeg_bits_dc_luminance,
186 avpriv_mjpeg_val_dc, 12, 0);
189 ret = build_vlc(&c->dc_vlc[1], avpriv_mjpeg_bits_dc_chrominance,
190 avpriv_mjpeg_val_dc, 12, 0);
193 ret = build_vlc(&c->ac_vlc[0], avpriv_mjpeg_bits_ac_luminance,
194 avpriv_mjpeg_val_ac_luminance, 251, 1);
197 ret = build_vlc(&c->ac_vlc[1], avpriv_mjpeg_bits_ac_chrominance,
198 avpriv_mjpeg_val_ac_chrominance, 251, 1);
202 ff_blockdsp_init(&c->bdsp, avctx);
203 ff_idctdsp_init(&c->idsp, avctx);
204 ff_init_scantable(c->idsp.idct_permutation, &c->scantable,
210 static av_cold void jpg_free_context(JPGContext *ctx)
214 for (i = 0; i < 2; i++) {
215 ff_free_vlc(&ctx->dc_vlc[i]);
216 ff_free_vlc(&ctx->ac_vlc[i]);
222 static void jpg_unescape(const uint8_t *src, int src_size,
223 uint8_t *dst, int *dst_size)
225 const uint8_t *src_end = src + src_size;
226 uint8_t *dst_start = dst;
228 while (src < src_end) {
233 if (x == 0xFF && !*src)
236 *dst_size = dst - dst_start;
239 static int jpg_decode_block(JPGContext *c, GetBitContext *gb,
240 int plane, int16_t *block)
243 const int is_chroma = !!plane;
244 const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant;
246 c->bdsp.clear_block(block);
247 dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 3);
249 return AVERROR_INVALIDDATA;
251 dc = get_xbits(gb, dc);
252 dc = dc * qmat[0] + c->prev_dc[plane];
254 c->prev_dc[plane] = dc;
258 val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 3);
260 return AVERROR_INVALIDDATA;
264 return val ? AVERROR_INVALIDDATA : 0;
268 val = get_xbits(gb, nbits);
269 val *= qmat[ff_zigzag_direct[pos]];
270 block[c->scantable.permutated[pos]] = val;
276 static inline void yuv2rgb(uint8_t *out, int ridx, int Y, int U, int V)
278 out[ridx] = av_clip_uint8(Y + (91881 * V + 32768 >> 16));
279 out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
280 out[2 - ridx] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
283 static int jpg_decode_data(JPGContext *c, int width, int height,
284 const uint8_t *src, int src_size,
285 uint8_t *dst, int dst_stride,
286 const uint8_t *mask, int mask_stride, int num_mbs,
290 int mb_w, mb_h, mb_x, mb_y, i, j;
294 const int ridx = swapuv ? 2 : 0;
296 if ((ret = av_reallocp(&c->buf,
297 src_size + FF_INPUT_BUFFER_PADDING_SIZE)) < 0)
299 jpg_unescape(src, src_size, c->buf, &unesc_size);
300 memset(c->buf + unesc_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
301 if((ret = init_get_bits8(&gb, c->buf, unesc_size)) < 0)
304 width = FFALIGN(width, 16);
306 mb_h = (height + 15) >> 4;
309 num_mbs = mb_w * mb_h * 4;
311 for (i = 0; i < 3; i++)
312 c->prev_dc[i] = 1024;
315 c->bdsp.clear_blocks(c->block[0]);
316 for (mb_y = 0; mb_y < mb_h; mb_y++) {
317 for (mb_x = 0; mb_x < mb_w; mb_x++) {
318 if (mask && !mask[mb_x * 2] && !mask[mb_x * 2 + 1] &&
319 !mask[mb_x * 2 + mask_stride] &&
320 !mask[mb_x * 2 + 1 + mask_stride]) {
324 for (j = 0; j < 2; j++) {
325 for (i = 0; i < 2; i++) {
326 if (mask && !mask[mb_x * 2 + i + j * mask_stride])
329 if ((ret = jpg_decode_block(c, &gb, 0,
330 c->block[i + j * 2])) != 0)
332 c->idsp.idct(c->block[i + j * 2]);
335 for (i = 1; i < 3; i++) {
336 if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0)
338 c->idsp.idct(c->block[i + 3]);
341 for (j = 0; j < 16; j++) {
342 uint8_t *out = dst + bx * 3 + (by + j) * dst_stride;
343 for (i = 0; i < 16; i++) {
346 Y = c->block[(j >> 3) * 2 + (i >> 3)][(i & 7) + (j & 7) * 8];
347 U = c->block[4][(i >> 1) + (j >> 1) * 8] - 128;
348 V = c->block[5][(i >> 1) + (j >> 1) * 8] - 128;
349 yuv2rgb(out + i * 3, ridx, Y, U, V);
360 mask += mask_stride * 2;
366 #define LOAD_NEIGHBOURS(x) \
367 W = curr_row[(x) - 1]; \
368 N = above_row[(x)]; \
369 WW = curr_row[(x) - 2]; \
370 NW = above_row[(x) - 1]; \
371 NE = above_row[(x) + 1]; \
372 NN = above2_row[(x)]; \
373 NNW = above2_row[(x) - 1]; \
374 NWW = above_row[(x) - 2]; \
375 NNE = above2_row[(x) + 1]
377 #define UPDATE_NEIGHBOURS(x) \
383 NE = above_row[(x) + 1]; \
384 NNE = above2_row[(x) + 1]
390 static inline int log2_ceil(uint32_t x)
394 for (--x; x > 0; x >>= 1)
400 /* improved djb2 hash from http://www.cse.yorku.ca/~oz/hash.html */
401 static int djb2_hash(uint32_t key)
405 h = (h * 33) ^ ((key >> 24) & 0xFF); // xxx: probably not needed at all
406 h = (h * 33) ^ ((key >> 16) & 0xFF);
407 h = (h * 33) ^ ((key >> 8) & 0xFF);
408 h = (h * 33) ^ (key & 0xFF);
410 return h & (EPIC_HASH_SIZE - 1);
413 static void epic_hash_init(ePICPixHash *hash)
415 memset(hash, 0, sizeof(*hash));
418 static ePICPixHashElem *epic_hash_find(const ePICPixHash *hash, uint32_t key)
420 int i, idx = djb2_hash(key);
421 ePICPixHashElem *bucket = hash->bucket[idx];
423 for (i = 0; i < hash->bucket_fill[idx]; i++)
424 if (bucket[i].pix_id == key)
430 static ePICPixHashElem *epic_hash_add(ePICPixHash *hash, uint32_t key)
432 ePICPixHashElem *bucket, *ret;
433 int idx = djb2_hash(key);
435 if (hash->bucket_size[idx] > INT_MAX / sizeof(**hash->bucket))
438 if (!(hash->bucket_fill[idx] < hash->bucket_size[idx])) {
439 int new_size = hash->bucket_size[idx] + 16;
440 bucket = av_realloc(hash->bucket[idx], new_size * sizeof(*bucket));
443 hash->bucket[idx] = bucket;
444 hash->bucket_size[idx] = new_size;
447 ret = &hash->bucket[idx][hash->bucket_fill[idx]++];
448 memset(ret, 0, sizeof(*ret));
453 static int epic_add_pixel_to_cache(ePICPixHash *hash, uint32_t key, uint32_t pix)
455 ePICPixListElem *new_elem;
456 ePICPixHashElem *hash_elem = epic_hash_find(hash, key);
459 if (!(hash_elem = epic_hash_add(hash, key)))
460 return AVERROR(ENOMEM);
463 new_elem = av_mallocz(sizeof(*new_elem));
465 return AVERROR(ENOMEM);
467 new_elem->pixel = pix;
468 new_elem->next = hash_elem->list;
469 hash_elem->list = new_elem;
474 static inline int epic_cache_entries_for_pixel(const ePICPixHash *hash,
477 ePICPixHashElem *hash_elem = epic_hash_find(hash, pix);
479 if (hash_elem != NULL && hash_elem->list != NULL)
485 static void epic_free_pixel_cache(ePICPixHash *hash)
489 for (i = 0; i < EPIC_HASH_SIZE; i++) {
490 for (j = 0; j < hash->bucket_fill[i]; j++) {
491 ePICPixListElem *list_elem = hash->bucket[i][j].list;
493 ePICPixListElem *tmp = list_elem->next;
498 av_freep(&hash->bucket[i]);
499 hash->bucket_size[i] =
500 hash->bucket_fill[i] = 0;
504 static inline int is_pixel_on_stack(const ePICContext *dc, uint32_t pix)
508 for (i = 0; i < dc->stack_pos; i++)
509 if (dc->stack[i] == pix)
512 return i != dc->stack_pos;
515 #define TOSIGNED(val) (((val) >> 1) ^ -((val) & 1))
517 static inline int epic_decode_component_pred(ePICContext *dc,
518 int N, int W, int NW)
520 unsigned delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
521 return mid_pred(N, N + W - NW, W) - TOSIGNED(delta);
524 static uint32_t epic_decode_pixel_pred(ePICContext *dc, int x, int y,
525 const uint32_t *curr_row,
526 const uint32_t *above_row)
528 uint32_t N, W, NW, pred;
530 int GN, GW, GNW, R, G, B;
535 NW = above_row[x - 1];
537 GN = (N >> G_shift) & 0xFF;
538 GW = (W >> G_shift) & 0xFF;
539 GNW = (NW >> G_shift) & 0xFF;
541 G = epic_decode_component_pred(dc, GN, GW, GNW);
543 R = G + epic_decode_component_pred(dc,
544 ((N >> R_shift) & 0xFF) - GN,
545 ((W >> R_shift) & 0xFF) - GW,
546 ((NW >> R_shift) & 0xFF) - GNW);
548 B = G + epic_decode_component_pred(dc,
549 ((N >> B_shift) & 0xFF) - GN,
550 ((W >> B_shift) & 0xFF) - GW,
551 ((NW >> B_shift) & 0xFF) - GNW);
554 pred = curr_row[x - 1];
558 delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
559 R = ((pred >> R_shift) & 0xFF) - TOSIGNED(delta);
561 delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
562 G = ((pred >> G_shift) & 0xFF) - TOSIGNED(delta);
564 delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
565 B = ((pred >> B_shift) & 0xFF) - TOSIGNED(delta);
568 return (R << R_shift) | (G << G_shift) | (B << B_shift);
571 static int epic_predict_pixel(ePICContext *dc, uint8_t *rung,
572 uint32_t *pPix, uint32_t pix)
574 if (!ff_els_decode_bit(&dc->els_ctx, rung)) {
578 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
582 static int epic_handle_edges(ePICContext *dc, int x, int y,
583 const uint32_t *curr_row,
584 const uint32_t *above_row, uint32_t *pPix)
588 if (!x && !y) { /* special case: top-left pixel */
589 /* the top-left pixel is coded independently with 3 unsigned numbers */
590 *pPix = (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << R_shift) |
591 (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << G_shift) |
592 (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << B_shift);
596 if (x) { /* predict from W first */
597 pix = curr_row[x - 1];
598 if (epic_predict_pixel(dc, &dc->W_flag_rung, pPix, pix))
602 if (y) { /* then try to predict from N */
604 if (!dc->stack_pos || dc->stack[0] != pix) {
605 if (epic_predict_pixel(dc, &dc->N_flag_rung, pPix, pix))
613 static int epic_decode_run_length(ePICContext *dc, int x, int y, int tile_width,
614 const uint32_t *curr_row,
615 const uint32_t *above_row,
616 const uint32_t *above2_row,
617 uint32_t *pPix, int *pRun)
619 int idx, got_pixel = 0, WWneW, old_WWneW = 0;
620 uint32_t W, WW, N, NN, NW, NE, NWW, NNW, NNE;
626 if (dc->next_run_pos == x) {
627 /* can't reuse W for the new pixel in this case */
630 idx = (WW != W) << 7 |
638 WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
642 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = W;
650 if (got_pixel) // pixel value already known (derived from either W or N)
652 else { // pixel value is unknown and will be decoded later
653 NWneW = *pRun ? NWneW : NW != W;
655 /* TODO: RFC this mess! */
656 switch (((NW != N) << 2) | (NWneW << 1) | WWneW) {
658 break; // do nothing here
663 if (!is_pixel_on_stack(dc, N)) {
665 (*pRun ? old_WWneW : WW != W) << 7 |
673 if (!ff_els_decode_bit(&dc->els_ctx, &dc->N_ctx_rung[idx])) {
684 if (!is_pixel_on_stack(dc, N))
685 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = N;
690 if (x + *pRun >= tile_width - 1)
693 UPDATE_NEIGHBOURS(x + *pRun);
695 if (!NWneW && NW == N && N == NE) {
697 int start_pos = x + *pRun;
699 /* scan for a run of pix in the line above */
700 uint32_t pix = above_row[start_pos + 1];
701 for (pos = start_pos + 2; pos < tile_width; pos++)
702 if (!(above_row[pos] == pix))
704 run = pos - start_pos - 1;
705 idx = log2_ceil(run);
706 if (ff_els_decode_bit(&dc->els_ctx, &dc->prev_row_rung[idx]))
710 /* run-length is coded as plain binary number of idx - 1 bits */
711 for (pos = idx - 1, rle = 0, flag = 0; pos >= 0; pos--) {
712 if ((1 << pos) + rle < run &&
713 ff_els_decode_bit(&dc->els_ctx,
714 flag ? &dc->runlen_one
715 : &dc->runlen_zeroes[pos])) {
721 break; // return immediately
723 if (x + *pRun >= tile_width - 1)
726 LOAD_NEIGHBOURS(x + *pRun);
739 WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
742 dc->next_run_pos = x + *pRun;
746 static int epic_predict_pixel2(ePICContext *dc, uint8_t *rung,
747 uint32_t *pPix, uint32_t pix)
749 if (ff_els_decode_bit(&dc->els_ctx, rung)) {
753 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
757 static int epic_predict_from_NW_NE(ePICContext *dc, int x, int y, int run,
758 int tile_width, const uint32_t *curr_row,
759 const uint32_t *above_row, uint32_t *pPix)
763 /* try to reuse the NW pixel first */
765 uint32_t NW = above_row[x - 1];
766 if (NW != curr_row[x - 1] && NW != above_row[x] && !is_pixel_on_stack(dc, NW)) {
767 if (epic_predict_pixel2(dc, &dc->nw_pred_rung[NW & 0xFF], pPix, NW))
772 /* try to reuse the NE[x + run, y] pixel */
774 if (pos < tile_width - 1 && y) {
775 uint32_t NE = above_row[pos + 1];
776 if (NE != above_row[pos] && !is_pixel_on_stack(dc, NE)) {
777 if (epic_predict_pixel2(dc, &dc->ne_pred_rung[NE & 0xFF], pPix, NE))
785 static int epic_decode_from_cache(ePICContext *dc, uint32_t W, uint32_t *pPix)
787 ePICPixListElem *list, *prev = NULL;
788 ePICPixHashElem *hash_elem = epic_hash_find(&dc->hash, W);
790 if (!hash_elem || !hash_elem->list)
793 list = hash_elem->list;
795 if (!is_pixel_on_stack(dc, list->pixel)) {
796 if (ff_els_decode_bit(&dc->els_ctx, &list->rung)) {
798 if (list != hash_elem->list) {
799 prev->next = list->next;
800 list->next = hash_elem->list;
801 hash_elem->list = list;
805 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = list->pixel;
814 static int epic_decode_tile(ePICContext *dc, uint8_t *out, int tile_height,
815 int tile_width, int stride)
819 uint32_t *curr_row = NULL, *above_row = NULL, *above2_row;
821 for (y = 0; y < tile_height; y++, out += stride) {
822 above2_row = above_row;
823 above_row = curr_row;
824 curr_row = (uint32_t *) out;
826 for (x = 0, dc->next_run_pos = 0; x < tile_width;) {
828 return AVERROR_INVALIDDATA; // bail out in the case of ELS overflow
830 pix = curr_row[x - 1]; // get W pixel
832 if (y >= 1 && x >= 2 &&
833 pix != curr_row[x - 2] && pix != above_row[x - 1] &&
834 pix != above_row[x - 2] && pix != above_row[x] &&
835 !epic_cache_entries_for_pixel(&dc->hash, pix)) {
836 curr_row[x] = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
840 dc->stack_pos = 0; // empty stack
842 if (y < 2 || x < 2 || x == tile_width - 1) {
844 got_pixel = epic_handle_edges(dc, x, y, curr_row, above_row, &pix);
846 got_pixel = epic_decode_run_length(dc, x, y, tile_width,
848 above2_row, &pix, &run);
850 if (!got_pixel && !epic_predict_from_NW_NE(dc, x, y, run,
851 tile_width, curr_row,
853 uint32_t ref_pix = curr_row[x - 1];
854 if (!x || !epic_decode_from_cache(dc, ref_pix, &pix)) {
855 pix = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
857 int ret = epic_add_pixel_to_cache(&dc->hash,
865 for (; run > 0; x++, run--)
874 static int epic_jb_decode_tile(G2MContext *c, int tile_x, int tile_y,
875 const uint8_t *src, size_t src_size,
876 AVCodecContext *avctx)
878 uint8_t prefix, mask = 0x80;
879 int extrabytes, tile_width, tile_height, awidth, aheight;
886 /* get data size of the ELS partition as unsigned variable-length integer */
889 for (extrabytes = 0; (prefix & mask) && (extrabytes < 7); extrabytes++)
891 if (extrabytes > 3 || src_size < extrabytes) {
892 av_log(avctx, AV_LOG_ERROR, "ePIC: invalid data size VLI\n");
893 return AVERROR_INVALIDDATA;
896 els_dsize = prefix & ((0x80 >> extrabytes) - 1); // mask out the length prefix
897 while (extrabytes-- > 0) {
898 els_dsize = (els_dsize << 8) | *src++;
902 if (src_size < els_dsize) {
903 av_log(avctx, AV_LOG_ERROR, "ePIC: data too short, needed %zu, got %zu\n",
904 els_dsize, src_size);
905 return AVERROR_INVALIDDATA;
908 tile_width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
909 tile_height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
910 awidth = FFALIGN(tile_width, 16);
911 aheight = FFALIGN(tile_height, 16);
915 uint8_t tr_r, tr_g, tr_b, *buf;
917 /* ELS decoder initializations */
918 memset(&c->ec, 0, sizeof(c->ec));
919 ff_els_decoder_init(&c->ec.els_ctx, src, els_dsize);
920 epic_hash_init(&c->ec.hash);
922 /* decode transparent pixel value */
923 tr_r = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
924 tr_g = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
925 tr_b = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
926 if (c->ec.els_ctx.err != 0) {
927 av_log(avctx, AV_LOG_ERROR,
928 "ePIC: couldn't decode transparency pixel!\n");
929 return AVERROR_INVALIDDATA;
932 ret = epic_decode_tile(&c->ec, c->epic_buf, tile_height, tile_width,
935 epic_free_pixel_cache(&c->ec.hash);
936 ff_els_decoder_uninit(&c->ec.unsigned_rung);
939 av_log(avctx, AV_LOG_ERROR,
940 "ePIC: tile decoding failed, frame=%d, tile_x=%d, tile_y=%d\n",
941 avctx->frame_number, tile_x, tile_y);
942 return AVERROR_INVALIDDATA;
946 dst = c->framebuf + tile_x * c->tile_width * 3 +
947 tile_y * c->tile_height * c->framebuf_stride;
949 for (j = 0; j < tile_height; j++) {
951 in = (uint32_t *) buf;
952 for (i = 0; i < tile_width; i++) {
953 out[0] = (in[i] >> R_shift) & 0xFF;
954 out[1] = (in[i] >> G_shift) & 0xFF;
955 out[2] = (in[i] >> B_shift) & 0xFF;
958 buf += c->epic_buf_stride;
959 dst += c->framebuf_stride;
962 if (src_size > els_dsize) {
965 int bstride = FFALIGN(tile_width, 16) >> 3;
967 int estride = c->epic_buf_stride >> 2;
970 src_size -= els_dsize;
972 in = (uint32_t *) c->epic_buf;
973 tr = (tr_r << R_shift) | (tr_g << G_shift) | (tr_b << B_shift);
975 memset(c->kempf_flags, 0,
976 (aheight >> 3) * bstride * sizeof(*c->kempf_flags));
977 for (j = 0; j < tile_height; j += 8) {
978 for (i = 0; i < tile_width; i += 8) {
979 c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 0;
980 for (k = 0; k < 8 * 8; k++) {
981 if (in[i + (k & 7) + (k >> 3) * estride] == tr) {
982 c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 1;
991 memset(c->jpeg_tile, 0, c->tile_stride * aheight);
992 jpg_decode_data(&c->jc, awidth, aheight, src, src_size,
993 c->jpeg_tile, c->tile_stride,
994 c->kempf_flags, bstride, nblocks, c->swapuv);
996 in = (uint32_t *) c->epic_buf;
997 dst = c->framebuf + tile_x * c->tile_width * 3 +
998 tile_y * c->tile_height * c->framebuf_stride;
1000 for (j = 0; j < tile_height; j++) {
1001 for (i = 0; i < tile_width; i++)
1003 memcpy(dst + i * 3, jpg + i * 3, 3);
1004 in += c->epic_buf_stride >> 2;
1005 dst += c->framebuf_stride;
1006 jpg += c->tile_stride;
1010 dst = c->framebuf + tile_x * c->tile_width * 3 +
1011 tile_y * c->tile_height * c->framebuf_stride;
1012 return jpg_decode_data(&c->jc, tile_width, tile_height, src, src_size,
1013 dst, c->framebuf_stride, NULL, 0, 0, c->swapuv);
1019 static int kempf_restore_buf(const uint8_t *src, int len,
1020 uint8_t *dst, int stride,
1021 const uint8_t *jpeg_tile, int tile_stride,
1022 int width, int height,
1023 const uint8_t *pal, int npal, int tidx)
1028 int align_width = FFALIGN(width, 16);
1030 if ((ret = init_get_bits8(&gb, src, len)) < 0)
1033 if (npal <= 2) nb = 1;
1034 else if (npal <= 4) nb = 2;
1035 else if (npal <= 16) nb = 4;
1038 for (j = 0; j < height; j++, dst += stride, jpeg_tile += tile_stride) {
1039 if (get_bits(&gb, 8))
1041 for (i = 0; i < width; i++) {
1042 col = get_bits(&gb, nb);
1044 memcpy(dst + i * 3, pal + col * 3, 3);
1046 memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
1048 skip_bits_long(&gb, nb * (align_width - width));
1054 static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y,
1055 const uint8_t *src, int src_size)
1058 int hdr, zsize, npal, tidx = -1, ret;
1060 const uint8_t *src_end = src + src_size;
1061 uint8_t pal[768], transp[3];
1062 uLongf dlen = (c->tile_width + 1) * c->tile_height;
1064 int nblocks, cblocks, bstride;
1065 int bits, bitbuf, coded;
1066 uint8_t *dst = c->framebuf + tile_x * c->tile_width * 3 +
1067 tile_y * c->tile_height * c->framebuf_stride;
1070 return AVERROR_INVALIDDATA;
1072 width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
1073 height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
1076 sub_type = hdr >> 5;
1077 if (sub_type == 0) {
1079 memcpy(transp, src, 3);
1081 for (j = 0; j < height; j++, dst += c->framebuf_stride)
1082 for (i = 0; i < width; i++)
1083 memcpy(dst + i * 3, transp, 3);
1085 } else if (sub_type == 1) {
1086 return jpg_decode_data(&c->jc, width, height, src, src_end - src,
1087 dst, c->framebuf_stride, NULL, 0, 0, 0);
1090 if (sub_type != 2) {
1091 memcpy(transp, src, 3);
1095 if (src_end - src < npal * 3)
1096 return AVERROR_INVALIDDATA;
1097 memcpy(pal, src, npal * 3);
1099 if (sub_type != 2) {
1100 for (i = 0; i < npal; i++) {
1101 if (!memcmp(pal + i * 3, transp, 3)) {
1108 if (src_end - src < 2)
1110 zsize = (src[0] << 8) | src[1];
1113 if (src_end - src < zsize + (sub_type != 2))
1114 return AVERROR_INVALIDDATA;
1116 ret = uncompress(c->kempf_buf, &dlen, src, zsize);
1118 return AVERROR_INVALIDDATA;
1121 if (sub_type == 2) {
1122 kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
1123 NULL, 0, width, height, pal, npal, tidx);
1127 nblocks = *src++ + 1;
1129 bstride = FFALIGN(width, 16) >> 3;
1130 // blocks are coded LSB and we need normal bitreader for JPEG data
1132 for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
1133 for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
1136 return AVERROR_INVALIDDATA;
1144 if (cblocks > nblocks)
1145 return AVERROR_INVALIDDATA;
1146 c->kempf_flags[j * 2 + i * 2 * bstride] =
1147 c->kempf_flags[j * 2 + 1 + i * 2 * bstride] =
1148 c->kempf_flags[j * 2 + (i * 2 + 1) * bstride] =
1149 c->kempf_flags[j * 2 + 1 + (i * 2 + 1) * bstride] = coded;
1153 memset(c->jpeg_tile, 0, c->tile_stride * height);
1154 jpg_decode_data(&c->jc, width, height, src, src_end - src,
1155 c->jpeg_tile, c->tile_stride,
1156 c->kempf_flags, bstride, nblocks * 4, 0);
1158 kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
1159 c->jpeg_tile, c->tile_stride,
1160 width, height, pal, npal, tidx);
1165 static int g2m_init_buffers(G2MContext *c)
1169 if (!c->framebuf || c->old_width < c->width || c->old_height < c->height) {
1170 c->framebuf_stride = FFALIGN(c->width + 15, 16) * 3;
1171 aligned_height = c->height + 15;
1172 av_free(c->framebuf);
1173 c->framebuf = av_mallocz_array(c->framebuf_stride, aligned_height);
1175 return AVERROR(ENOMEM);
1177 if (!c->synth_tile || !c->jpeg_tile ||
1178 (c->compression == 2 && !c->epic_buf_base) ||
1179 c->old_tile_w < c->tile_width ||
1180 c->old_tile_h < c->tile_height) {
1181 c->tile_stride = FFALIGN(c->tile_width, 16) * 3;
1182 c->epic_buf_stride = FFALIGN(c->tile_width * 4, 16);
1183 aligned_height = FFALIGN(c->tile_height, 16);
1184 av_free(c->synth_tile);
1185 av_free(c->jpeg_tile);
1186 av_free(c->kempf_buf);
1187 av_free(c->kempf_flags);
1188 av_free(c->epic_buf_base);
1189 c->synth_tile = av_mallocz(c->tile_stride * aligned_height);
1190 c->jpeg_tile = av_mallocz(c->tile_stride * aligned_height);
1191 c->kempf_buf = av_mallocz((c->tile_width + 1) * aligned_height +
1192 FF_INPUT_BUFFER_PADDING_SIZE);
1193 c->kempf_flags = av_mallocz(c->tile_width * aligned_height);
1194 if (!c->synth_tile || !c->jpeg_tile ||
1195 !c->kempf_buf || !c->kempf_flags)
1196 return AVERROR(ENOMEM);
1197 if (c->compression == 2) {
1198 c->epic_buf_base = av_mallocz(c->epic_buf_stride * aligned_height + 4);
1199 if (!c->epic_buf_base)
1200 return AVERROR(ENOMEM);
1201 c->epic_buf = c->epic_buf_base + 4;
1208 static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c,
1214 uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
1215 uint32_t cursor_hot_x, cursor_hot_y;
1216 int cursor_fmt, err;
1218 cur_size = bytestream2_get_be32(gb);
1219 cursor_w = bytestream2_get_byte(gb);
1220 cursor_h = bytestream2_get_byte(gb);
1221 cursor_hot_x = bytestream2_get_byte(gb);
1222 cursor_hot_y = bytestream2_get_byte(gb);
1223 cursor_fmt = bytestream2_get_byte(gb);
1225 cursor_stride = FFALIGN(cursor_w, cursor_fmt==1 ? 32 : 1) * 4;
1227 if (cursor_w < 1 || cursor_w > 256 ||
1228 cursor_h < 1 || cursor_h > 256) {
1229 av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %"PRIu32"x%"PRIu32"\n",
1230 cursor_w, cursor_h);
1231 return AVERROR_INVALIDDATA;
1233 if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
1234 av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %"PRIu32",%"PRIu32"\n",
1235 cursor_hot_x, cursor_hot_y);
1236 cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);
1237 cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);
1239 if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||
1240 c->cursor_w * c->cursor_h / 4 > cur_size) {
1241 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"/%u\n",
1242 cur_size, bytestream2_get_bytes_left(gb));
1243 return AVERROR_INVALIDDATA;
1245 if (cursor_fmt != 1 && cursor_fmt != 32) {
1246 avpriv_report_missing_feature(avctx, "Cursor format %d",
1248 return AVERROR_PATCHWELCOME;
1251 if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
1252 av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
1256 c->cursor_w = cursor_w;
1257 c->cursor_h = cursor_h;
1258 c->cursor_hot_x = cursor_hot_x;
1259 c->cursor_hot_y = cursor_hot_y;
1260 c->cursor_fmt = cursor_fmt;
1261 c->cursor_stride = cursor_stride;
1264 switch (c->cursor_fmt) {
1265 case 1: // old monochrome
1266 for (j = 0; j < c->cursor_h; j++) {
1267 for (i = 0; i < c->cursor_w; i += 32) {
1268 bits = bytestream2_get_be32(gb);
1269 for (k = 0; k < 32; k++) {
1270 dst[0] = !!(bits & 0x80000000);
1278 for (j = 0; j < c->cursor_h; j++) {
1279 for (i = 0; i < c->cursor_w; i += 32) {
1280 bits = bytestream2_get_be32(gb);
1281 for (k = 0; k < 32; k++) {
1282 int mask_bit = !!(bits & 0x80000000);
1283 switch (dst[0] * 2 + mask_bit) {
1308 case 32: // full colour
1309 /* skip monochrome version of the cursor and decode RGBA instead */
1310 bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
1311 for (j = 0; j < c->cursor_h; j++) {
1312 for (i = 0; i < c->cursor_w; i++) {
1313 int val = bytestream2_get_be32(gb);
1322 return AVERROR_PATCHWELCOME;
1327 #define APPLY_ALPHA(src, new, alpha) \
1328 src = (src * (256 - alpha) + new * alpha) >> 8
1330 static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
1334 const uint8_t *cursor;
1339 x = c->cursor_x - c->cursor_hot_x;
1340 y = c->cursor_y - c->cursor_hot_y;
1346 if (x + w > c->width)
1348 if (y + h > c->height)
1358 cursor += -y * c->cursor_stride;
1365 for (j = 0; j < h; j++) {
1366 for (i = 0; i < w; i++) {
1367 uint8_t alpha = cursor[i * 4];
1368 APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
1369 APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
1370 APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
1373 cursor += c->cursor_stride;
1377 static int g2m_decode_frame(AVCodecContext *avctx, void *data,
1378 int *got_picture_ptr, AVPacket *avpkt)
1380 const uint8_t *buf = avpkt->data;
1381 int buf_size = avpkt->size;
1382 G2MContext *c = avctx->priv_data;
1383 AVFrame *pic = data;
1384 GetByteContext bc, tbc;
1387 uint32_t chunk_size, r_mask, g_mask, b_mask;
1388 int chunk_type, chunk_start;
1392 if (buf_size < 12) {
1393 av_log(avctx, AV_LOG_ERROR,
1394 "Frame should have at least 12 bytes, got %d instead\n",
1396 return AVERROR_INVALIDDATA;
1399 bytestream2_init(&bc, buf, buf_size);
1401 magic = bytestream2_get_be32(&bc);
1402 if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
1403 (magic & 0xF) < 2 || (magic & 0xF) > 5) {
1404 av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
1405 return AVERROR_INVALIDDATA;
1408 c->swapuv = magic == MKBETAG('G', '2', 'M', '2');
1410 while (bytestream2_get_bytes_left(&bc) > 5) {
1411 chunk_size = bytestream2_get_le32(&bc) - 1;
1412 chunk_type = bytestream2_get_byte(&bc);
1413 chunk_start = bytestream2_tell(&bc);
1414 if (chunk_size > bytestream2_get_bytes_left(&bc)) {
1415 av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %"PRIu32" type %02X\n",
1416 chunk_size, chunk_type);
1419 switch (chunk_type) {
1423 if (chunk_size < 21) {
1424 av_log(avctx, AV_LOG_ERROR, "Invalid display info size %"PRIu32"\n",
1428 c->width = bytestream2_get_be32(&bc);
1429 c->height = bytestream2_get_be32(&bc);
1430 if (c->width < 16 || c->width > c->orig_width ||
1431 c->height < 16 || c->height > c->orig_height) {
1432 av_log(avctx, AV_LOG_ERROR,
1433 "Invalid frame dimensions %dx%d\n",
1434 c->width, c->height);
1435 ret = AVERROR_INVALIDDATA;
1438 if (c->width != avctx->width || c->height != avctx->height) {
1439 ret = ff_set_dimensions(avctx, c->width, c->height);
1443 c->compression = bytestream2_get_be32(&bc);
1444 if (c->compression != 2 && c->compression != 3) {
1445 av_log(avctx, AV_LOG_ERROR,
1446 "Unknown compression method %d\n",
1448 ret = AVERROR_PATCHWELCOME;
1451 c->tile_width = bytestream2_get_be32(&bc);
1452 c->tile_height = bytestream2_get_be32(&bc);
1453 if (c->tile_width <= 0 || c->tile_height <= 0 ||
1454 ((c->tile_width | c->tile_height) & 0xF) ||
1455 c->tile_width * 4LL * c->tile_height >= INT_MAX
1457 av_log(avctx, AV_LOG_ERROR,
1458 "Invalid tile dimensions %dx%d\n",
1459 c->tile_width, c->tile_height);
1460 ret = AVERROR_INVALIDDATA;
1463 c->tiles_x = (c->width + c->tile_width - 1) / c->tile_width;
1464 c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
1465 c->bpp = bytestream2_get_byte(&bc);
1467 if (bytestream2_get_bytes_left(&bc) < 16 ||
1468 (chunk_size - 21) < 16) {
1469 av_log(avctx, AV_LOG_ERROR,
1470 "Display info: missing bitmasks!\n");
1471 ret = AVERROR_INVALIDDATA;
1474 r_mask = bytestream2_get_be32(&bc);
1475 g_mask = bytestream2_get_be32(&bc);
1476 b_mask = bytestream2_get_be32(&bc);
1477 if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
1478 av_log(avctx, AV_LOG_ERROR,
1479 "Invalid or unsupported bitmasks: R=%"PRIX32", G=%"PRIX32", B=%"PRIX32"\n",
1480 r_mask, g_mask, b_mask);
1481 ret = AVERROR_PATCHWELCOME;
1485 avpriv_request_sample(avctx, "bpp=%d", c->bpp);
1486 ret = AVERROR_PATCHWELCOME;
1489 if (g2m_init_buffers(c)) {
1490 ret = AVERROR(ENOMEM);
1496 if (!c->tiles_x || !c->tiles_y) {
1497 av_log(avctx, AV_LOG_WARNING,
1498 "No display info - skipping tile\n");
1501 if (chunk_size < 2) {
1502 av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %"PRIu32"\n",
1506 c->tile_x = bytestream2_get_byte(&bc);
1507 c->tile_y = bytestream2_get_byte(&bc);
1508 if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
1509 av_log(avctx, AV_LOG_ERROR,
1510 "Invalid tile pos %d,%d (in %dx%d grid)\n",
1511 c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
1515 switch (c->compression) {
1516 case COMPR_EPIC_J_B:
1517 ret = epic_jb_decode_tile(c, c->tile_x, c->tile_y,
1518 buf + bytestream2_tell(&bc),
1519 chunk_size - 2, avctx);
1521 case COMPR_KEMPF_J_B:
1522 ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
1523 buf + bytestream2_tell(&bc),
1527 if (ret && c->framebuf)
1528 av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
1529 c->tile_x, c->tile_y);
1532 if (chunk_size < 5) {
1533 av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %"PRIu32"\n",
1537 c->cursor_x = bytestream2_get_be16(&bc);
1538 c->cursor_y = bytestream2_get_be16(&bc);
1541 if (chunk_size < 8) {
1542 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"\n",
1546 bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
1548 g2m_load_cursor(avctx, c, &tbc);
1554 av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02d\n",
1558 /* navigate to next chunk */
1559 bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
1564 if (c->width && c->height && c->framebuf) {
1565 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
1568 pic->key_frame = got_header;
1569 pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1571 for (i = 0; i < avctx->height; i++)
1572 memcpy(pic->data[0] + i * pic->linesize[0],
1573 c->framebuf + i * c->framebuf_stride,
1575 g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);
1577 *got_picture_ptr = 1;
1590 static av_cold int g2m_decode_init(AVCodecContext *avctx)
1592 G2MContext *const c = avctx->priv_data;
1595 if ((ret = jpg_init(avctx, &c->jc)) != 0) {
1596 av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
1597 jpg_free_context(&c->jc);
1598 return AVERROR(ENOMEM);
1601 avctx->pix_fmt = AV_PIX_FMT_RGB24;
1603 // store original sizes and check against those if resize happens
1604 c->orig_width = avctx->width;
1605 c->orig_height = avctx->height;
1610 static av_cold int g2m_decode_end(AVCodecContext *avctx)
1612 G2MContext *const c = avctx->priv_data;
1614 jpg_free_context(&c->jc);
1616 av_freep(&c->epic_buf_base);
1617 av_freep(&c->kempf_buf);
1618 av_freep(&c->kempf_flags);
1619 av_freep(&c->synth_tile);
1620 av_freep(&c->jpeg_tile);
1621 av_freep(&c->cursor);
1622 av_freep(&c->framebuf);
1627 AVCodec ff_g2m_decoder = {
1629 .long_name = NULL_IF_CONFIG_SMALL("Go2Meeting"),
1630 .type = AVMEDIA_TYPE_VIDEO,
1631 .id = AV_CODEC_ID_G2M,
1632 .priv_data_size = sizeof(G2MContext),
1633 .init = g2m_decode_init,
1634 .close = g2m_decode_end,
1635 .decode = g2m_decode_frame,
1636 .capabilities = CODEC_CAP_DR1,
1637 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,