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/imgutils.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mem_internal.h"
37 #include "bytestream.h"
42 #include "jpegtables.h"
46 #define EPIC_PIX_STACK_SIZE 1024
47 #define EPIC_PIX_STACK_MAX (EPIC_PIX_STACK_SIZE - 1)
63 static const uint8_t luma_quant[64] = {
64 8, 6, 5, 8, 12, 20, 26, 31,
65 6, 6, 7, 10, 13, 29, 30, 28,
66 7, 7, 8, 12, 20, 29, 35, 28,
67 7, 9, 11, 15, 26, 44, 40, 31,
68 9, 11, 19, 28, 34, 55, 52, 39,
69 12, 18, 28, 32, 41, 52, 57, 46,
70 25, 32, 39, 44, 52, 61, 60, 51,
71 36, 46, 48, 49, 56, 50, 52, 50
74 static const uint8_t chroma_quant[64] = {
75 9, 9, 12, 24, 50, 50, 50, 50,
76 9, 11, 13, 33, 50, 50, 50, 50,
77 12, 13, 28, 50, 50, 50, 50, 50,
78 24, 33, 50, 50, 50, 50, 50, 50,
79 50, 50, 50, 50, 50, 50, 50, 50,
80 50, 50, 50, 50, 50, 50, 50, 50,
81 50, 50, 50, 50, 50, 50, 50, 50,
82 50, 50, 50, 50, 50, 50, 50, 50,
85 typedef struct ePICPixListElem {
86 struct ePICPixListElem *next;
91 typedef struct ePICPixHashElem {
93 struct ePICPixListElem *list;
96 #define EPIC_HASH_SIZE 256
97 typedef struct ePICPixHash {
98 ePICPixHashElem *bucket[EPIC_HASH_SIZE];
99 int bucket_size[EPIC_HASH_SIZE];
100 int bucket_fill[EPIC_HASH_SIZE];
103 typedef struct ePICContext {
106 ElsUnsignedRung unsigned_rung;
109 uint8_t W_ctx_rung[256];
110 uint8_t N_ctx_rung[512];
111 uint8_t nw_pred_rung[256];
112 uint8_t ne_pred_rung[256];
113 uint8_t prev_row_rung[14];
114 uint8_t runlen_zeroes[14];
117 uint32_t stack[EPIC_PIX_STACK_SIZE];
121 typedef struct JPGContext {
122 BlockDSPContext bdsp;
126 VLC dc_vlc[2], ac_vlc[2];
128 DECLARE_ALIGNED(32, int16_t, block)[6][64];
133 typedef struct G2MContext {
140 int width, height, bpp;
141 int orig_width, orig_height;
142 int tile_width, tile_height;
143 int tiles_x, tiles_y, tile_x, tile_y;
148 int framebuf_stride, old_width, old_height;
150 uint8_t *synth_tile, *jpeg_tile, *epic_buf, *epic_buf_base;
151 int tile_stride, epic_buf_stride, old_tile_w, old_tile_h;
154 uint8_t *kempf_buf, *kempf_flags;
159 int cursor_w, cursor_h, cursor_x, cursor_y;
160 int cursor_hot_x, cursor_hot_y;
163 static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c)
167 ret = ff_mjpeg_build_vlc(&c->dc_vlc[0], avpriv_mjpeg_bits_dc_luminance,
168 avpriv_mjpeg_val_dc, 0, avctx);
171 ret = ff_mjpeg_build_vlc(&c->dc_vlc[1], avpriv_mjpeg_bits_dc_chrominance,
172 avpriv_mjpeg_val_dc, 0, avctx);
175 ret = ff_mjpeg_build_vlc(&c->ac_vlc[0], avpriv_mjpeg_bits_ac_luminance,
176 avpriv_mjpeg_val_ac_luminance, 1, avctx);
179 ret = ff_mjpeg_build_vlc(&c->ac_vlc[1], avpriv_mjpeg_bits_ac_chrominance,
180 avpriv_mjpeg_val_ac_chrominance, 1, avctx);
184 ff_blockdsp_init(&c->bdsp, avctx);
185 ff_idctdsp_init(&c->idsp, avctx);
186 ff_init_scantable(c->idsp.idct_permutation, &c->scantable,
192 static av_cold void jpg_free_context(JPGContext *ctx)
196 for (i = 0; i < 2; i++) {
197 ff_free_vlc(&ctx->dc_vlc[i]);
198 ff_free_vlc(&ctx->ac_vlc[i]);
204 static void jpg_unescape(const uint8_t *src, int src_size,
205 uint8_t *dst, int *dst_size)
207 const uint8_t *src_end = src + src_size;
208 uint8_t *dst_start = dst;
210 while (src < src_end) {
215 if (x == 0xFF && !*src)
218 *dst_size = dst - dst_start;
221 static int jpg_decode_block(JPGContext *c, GetBitContext *gb,
222 int plane, int16_t *block)
225 const int is_chroma = !!plane;
226 const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant;
228 if (get_bits_left(gb) < 1)
229 return AVERROR_INVALIDDATA;
231 c->bdsp.clear_block(block);
232 dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 2);
234 return AVERROR_INVALIDDATA;
236 dc = get_xbits(gb, dc);
237 dc = dc * qmat[0] + c->prev_dc[plane];
239 c->prev_dc[plane] = dc;
243 val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 2);
245 return AVERROR_INVALIDDATA;
249 return val ? AVERROR_INVALIDDATA : 0;
253 val = get_xbits(gb, nbits);
254 val *= qmat[ff_zigzag_direct[pos]];
255 block[c->scantable.permutated[pos]] = val;
261 static inline void yuv2rgb(uint8_t *out, int ridx, int Y, int U, int V)
263 out[ridx] = av_clip_uint8(Y + (91881 * V + 32768 >> 16));
264 out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
265 out[2 - ridx] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
268 static int jpg_decode_data(JPGContext *c, int width, int height,
269 const uint8_t *src, int src_size,
270 uint8_t *dst, int dst_stride,
271 const uint8_t *mask, int mask_stride, int num_mbs,
275 int mb_w, mb_h, mb_x, mb_y, i, j;
279 const int ridx = swapuv ? 2 : 0;
281 if ((ret = av_reallocp(&c->buf,
282 src_size + AV_INPUT_BUFFER_PADDING_SIZE)) < 0)
284 jpg_unescape(src, src_size, c->buf, &unesc_size);
285 memset(c->buf + unesc_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
286 if((ret = init_get_bits8(&gb, c->buf, unesc_size)) < 0)
289 width = FFALIGN(width, 16);
291 mb_h = (height + 15) >> 4;
294 num_mbs = mb_w * mb_h * 4;
296 for (i = 0; i < 3; i++)
297 c->prev_dc[i] = 1024;
300 c->bdsp.clear_blocks(c->block[0]);
301 for (mb_y = 0; mb_y < mb_h; mb_y++) {
302 for (mb_x = 0; mb_x < mb_w; mb_x++) {
303 if (mask && !mask[mb_x * 2] && !mask[mb_x * 2 + 1] &&
304 !mask[mb_x * 2 + mask_stride] &&
305 !mask[mb_x * 2 + 1 + mask_stride]) {
309 for (j = 0; j < 2; j++) {
310 for (i = 0; i < 2; i++) {
311 if (mask && !mask[mb_x * 2 + i + j * mask_stride])
314 if ((ret = jpg_decode_block(c, &gb, 0,
315 c->block[i + j * 2])) != 0)
317 c->idsp.idct(c->block[i + j * 2]);
320 for (i = 1; i < 3; i++) {
321 if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0)
323 c->idsp.idct(c->block[i + 3]);
326 for (j = 0; j < 16; j++) {
327 uint8_t *out = dst + bx * 3 + (by + j) * dst_stride;
328 for (i = 0; i < 16; i++) {
331 Y = c->block[(j >> 3) * 2 + (i >> 3)][(i & 7) + (j & 7) * 8];
332 U = c->block[4][(i >> 1) + (j >> 1) * 8] - 128;
333 V = c->block[5][(i >> 1) + (j >> 1) * 8] - 128;
334 yuv2rgb(out + i * 3, ridx, Y, U, V);
345 mask += mask_stride * 2;
351 #define LOAD_NEIGHBOURS(x) \
352 W = curr_row[(x) - 1]; \
353 N = above_row[(x)]; \
354 WW = curr_row[(x) - 2]; \
355 NW = above_row[(x) - 1]; \
356 NE = above_row[(x) + 1]; \
357 NN = above2_row[(x)]; \
358 NNW = above2_row[(x) - 1]; \
359 NWW = above_row[(x) - 2]; \
360 NNE = above2_row[(x) + 1]
362 #define UPDATE_NEIGHBOURS(x) \
368 NE = above_row[(x) + 1]; \
369 NNE = above2_row[(x) + 1]
375 /* improved djb2 hash from http://www.cse.yorku.ca/~oz/hash.html */
376 static int djb2_hash(uint32_t key)
380 h = (h * 33) ^ ((key >> 24) & 0xFF); // xxx: probably not needed at all
381 h = (h * 33) ^ ((key >> 16) & 0xFF);
382 h = (h * 33) ^ ((key >> 8) & 0xFF);
383 h = (h * 33) ^ (key & 0xFF);
385 return h & (EPIC_HASH_SIZE - 1);
388 static void epic_hash_init(ePICPixHash *hash)
390 memset(hash, 0, sizeof(*hash));
393 static ePICPixHashElem *epic_hash_find(const ePICPixHash *hash, uint32_t key)
395 int i, idx = djb2_hash(key);
396 ePICPixHashElem *bucket = hash->bucket[idx];
398 for (i = 0; i < hash->bucket_fill[idx]; i++)
399 if (bucket[i].pix_id == key)
405 static ePICPixHashElem *epic_hash_add(ePICPixHash *hash, uint32_t key)
407 ePICPixHashElem *bucket, *ret;
408 int idx = djb2_hash(key);
410 if (hash->bucket_size[idx] > INT_MAX / sizeof(**hash->bucket))
413 if (!(hash->bucket_fill[idx] < hash->bucket_size[idx])) {
414 int new_size = hash->bucket_size[idx] + 16;
415 bucket = av_realloc(hash->bucket[idx], new_size * sizeof(*bucket));
418 hash->bucket[idx] = bucket;
419 hash->bucket_size[idx] = new_size;
422 ret = &hash->bucket[idx][hash->bucket_fill[idx]++];
423 memset(ret, 0, sizeof(*ret));
428 static int epic_add_pixel_to_cache(ePICPixHash *hash, uint32_t key, uint32_t pix)
430 ePICPixListElem *new_elem;
431 ePICPixHashElem *hash_elem = epic_hash_find(hash, key);
434 if (!(hash_elem = epic_hash_add(hash, key)))
435 return AVERROR(ENOMEM);
438 new_elem = av_mallocz(sizeof(*new_elem));
440 return AVERROR(ENOMEM);
442 new_elem->pixel = pix;
443 new_elem->next = hash_elem->list;
444 hash_elem->list = new_elem;
449 static inline int epic_cache_entries_for_pixel(const ePICPixHash *hash,
452 ePICPixHashElem *hash_elem = epic_hash_find(hash, pix);
454 if (hash_elem != NULL && hash_elem->list != NULL)
460 static void epic_free_pixel_cache(ePICPixHash *hash)
464 for (i = 0; i < EPIC_HASH_SIZE; i++) {
465 for (j = 0; j < hash->bucket_fill[i]; j++) {
466 ePICPixListElem *list_elem = hash->bucket[i][j].list;
468 ePICPixListElem *tmp = list_elem->next;
473 av_freep(&hash->bucket[i]);
474 hash->bucket_size[i] =
475 hash->bucket_fill[i] = 0;
479 static inline int is_pixel_on_stack(const ePICContext *dc, uint32_t pix)
483 for (i = 0; i < dc->stack_pos; i++)
484 if (dc->stack[i] == pix)
487 return i != dc->stack_pos;
490 #define TOSIGNED(val) (((val) >> 1) ^ -((val) & 1))
492 static inline int epic_decode_component_pred(ePICContext *dc,
493 int N, int W, int NW)
495 unsigned delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
496 return mid_pred(N, N + W - NW, W) - TOSIGNED(delta);
499 static uint32_t epic_decode_pixel_pred(ePICContext *dc, int x, int y,
500 const uint32_t *curr_row,
501 const uint32_t *above_row)
503 uint32_t N, W, NW, pred;
505 int GN, GW, GNW, R, G, B;
510 NW = above_row[x - 1];
512 GN = (N >> G_shift) & 0xFF;
513 GW = (W >> G_shift) & 0xFF;
514 GNW = (NW >> G_shift) & 0xFF;
516 G = epic_decode_component_pred(dc, GN, GW, GNW);
518 R = G + epic_decode_component_pred(dc,
519 ((N >> R_shift) & 0xFF) - GN,
520 ((W >> R_shift) & 0xFF) - GW,
521 ((NW >> R_shift) & 0xFF) - GNW);
523 B = G + epic_decode_component_pred(dc,
524 ((N >> B_shift) & 0xFF) - GN,
525 ((W >> B_shift) & 0xFF) - GW,
526 ((NW >> B_shift) & 0xFF) - GNW);
529 pred = curr_row[x - 1];
533 delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
534 R = ((pred >> R_shift) & 0xFF) - TOSIGNED(delta);
536 delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
537 G = ((pred >> G_shift) & 0xFF) - TOSIGNED(delta);
539 delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
540 B = ((pred >> B_shift) & 0xFF) - TOSIGNED(delta);
543 if (R<0 || G<0 || B<0 || R > 255 || G > 255 || B > 255) {
544 avpriv_request_sample(NULL, "RGB %d %d %d (out of range)", R, G, B);
548 return (R << R_shift) | (G << G_shift) | (B << B_shift);
551 static int epic_predict_pixel(ePICContext *dc, uint8_t *rung,
552 uint32_t *pPix, uint32_t pix)
554 if (!ff_els_decode_bit(&dc->els_ctx, rung)) {
558 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
562 static int epic_handle_edges(ePICContext *dc, int x, int y,
563 const uint32_t *curr_row,
564 const uint32_t *above_row, uint32_t *pPix)
568 if (!x && !y) { /* special case: top-left pixel */
569 /* the top-left pixel is coded independently with 3 unsigned numbers */
570 *pPix = (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << R_shift) |
571 (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << G_shift) |
572 (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << B_shift);
576 if (x) { /* predict from W first */
577 pix = curr_row[x - 1];
578 if (epic_predict_pixel(dc, &dc->W_flag_rung, pPix, pix))
582 if (y) { /* then try to predict from N */
584 if (!dc->stack_pos || dc->stack[0] != pix) {
585 if (epic_predict_pixel(dc, &dc->N_flag_rung, pPix, pix))
593 static int epic_decode_run_length(ePICContext *dc, int x, int y, int tile_width,
594 const uint32_t *curr_row,
595 const uint32_t *above_row,
596 const uint32_t *above2_row,
597 uint32_t *pPix, int *pRun)
599 int idx, got_pixel = 0, WWneW, old_WWneW = 0;
600 uint32_t W, WW, N, NN, NW, NE, NWW, NNW, NNE;
606 if (dc->next_run_pos == x) {
607 /* can't reuse W for the new pixel in this case */
610 idx = (WW != W) << 7 |
618 WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
624 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = W;
632 if (got_pixel) // pixel value already known (derived from either W or N)
634 else { // pixel value is unknown and will be decoded later
635 NWneW = *pRun ? NWneW : NW != W;
637 /* TODO: RFC this mess! */
638 switch (((NW != N) << 2) | (NWneW << 1) | WWneW) {
640 break; // do nothing here
645 if (!is_pixel_on_stack(dc, N)) {
647 (*pRun ? old_WWneW : WW != W) << 7 |
655 if (!ff_els_decode_bit(&dc->els_ctx, &dc->N_ctx_rung[idx])) {
666 if (!is_pixel_on_stack(dc, N))
667 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = N;
672 if (x + *pRun >= tile_width - 1)
675 UPDATE_NEIGHBOURS(x + *pRun);
677 if (!NWneW && NW == N && N == NE) {
679 int start_pos = x + *pRun;
681 /* scan for a run of pix in the line above */
682 uint32_t pix = above_row[start_pos + 1];
683 for (pos = start_pos + 2; pos < tile_width; pos++)
684 if (!(above_row[pos] == pix))
686 run = pos - start_pos - 1;
687 idx = av_ceil_log2(run);
688 if (ff_els_decode_bit(&dc->els_ctx, &dc->prev_row_rung[idx]))
692 /* run-length is coded as plain binary number of idx - 1 bits */
693 for (pos = idx - 1, rle = 0, flag = 0; pos >= 0; pos--) {
694 if ((1 << pos) + rle < run &&
695 ff_els_decode_bit(&dc->els_ctx,
696 flag ? &dc->runlen_one
697 : &dc->runlen_zeroes[pos])) {
703 break; // return immediately
705 if (x + *pRun >= tile_width - 1)
708 LOAD_NEIGHBOURS(x + *pRun);
721 WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
724 dc->next_run_pos = x + *pRun;
728 static int epic_predict_pixel2(ePICContext *dc, uint8_t *rung,
729 uint32_t *pPix, uint32_t pix)
731 if (ff_els_decode_bit(&dc->els_ctx, rung)) {
735 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
739 static int epic_predict_from_NW_NE(ePICContext *dc, int x, int y, int run,
740 int tile_width, const uint32_t *curr_row,
741 const uint32_t *above_row, uint32_t *pPix)
745 /* try to reuse the NW pixel first */
747 uint32_t NW = above_row[x - 1];
748 if (NW != curr_row[x - 1] && NW != above_row[x] && !is_pixel_on_stack(dc, NW)) {
749 if (epic_predict_pixel2(dc, &dc->nw_pred_rung[NW & 0xFF], pPix, NW))
754 /* try to reuse the NE[x + run, y] pixel */
756 if (pos < tile_width - 1 && y) {
757 uint32_t NE = above_row[pos + 1];
758 if (NE != above_row[pos] && !is_pixel_on_stack(dc, NE)) {
759 if (epic_predict_pixel2(dc, &dc->ne_pred_rung[NE & 0xFF], pPix, NE))
767 static int epic_decode_from_cache(ePICContext *dc, uint32_t W, uint32_t *pPix)
769 ePICPixListElem *list, *prev = NULL;
770 ePICPixHashElem *hash_elem = epic_hash_find(&dc->hash, W);
772 if (!hash_elem || !hash_elem->list)
775 list = hash_elem->list;
777 if (!is_pixel_on_stack(dc, list->pixel)) {
778 if (ff_els_decode_bit(&dc->els_ctx, &list->rung)) {
780 if (list != hash_elem->list) {
781 prev->next = list->next;
782 list->next = hash_elem->list;
783 hash_elem->list = list;
787 dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = list->pixel;
796 static int epic_decode_tile(ePICContext *dc, uint8_t *out, int tile_height,
797 int tile_width, int stride)
801 uint32_t *curr_row = NULL, *above_row = NULL, *above2_row;
803 for (y = 0; y < tile_height; y++, out += stride) {
804 above2_row = above_row;
805 above_row = curr_row;
806 curr_row = (uint32_t *) out;
808 for (x = 0, dc->next_run_pos = 0; x < tile_width;) {
810 return AVERROR_INVALIDDATA; // bail out in the case of ELS overflow
812 pix = curr_row[x - 1]; // get W pixel
814 if (y >= 1 && x >= 2 &&
815 pix != curr_row[x - 2] && pix != above_row[x - 1] &&
816 pix != above_row[x - 2] && pix != above_row[x] &&
817 !epic_cache_entries_for_pixel(&dc->hash, pix)) {
818 curr_row[x] = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
822 dc->stack_pos = 0; // empty stack
824 if (y < 2 || x < 2 || x == tile_width - 1) {
826 got_pixel = epic_handle_edges(dc, x, y, curr_row, above_row, &pix);
828 got_pixel = epic_decode_run_length(dc, x, y, tile_width,
830 above2_row, &pix, &run);
835 if (!got_pixel && !epic_predict_from_NW_NE(dc, x, y, run,
836 tile_width, curr_row,
838 uint32_t ref_pix = curr_row[x - 1];
839 if (!x || !epic_decode_from_cache(dc, ref_pix, &pix)) {
840 pix = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
841 if (is_pixel_on_stack(dc, pix))
842 return AVERROR_INVALIDDATA;
845 int ret = epic_add_pixel_to_cache(&dc->hash,
853 for (; run > 0; x++, run--)
862 static int epic_jb_decode_tile(G2MContext *c, int tile_x, int tile_y,
863 const uint8_t *src, size_t src_size,
864 AVCodecContext *avctx)
866 uint8_t prefix, mask = 0x80;
867 int extrabytes, tile_width, tile_height, awidth, aheight;
874 /* get data size of the ELS partition as unsigned variable-length integer */
877 for (extrabytes = 0; (prefix & mask) && (extrabytes < 7); extrabytes++)
879 if (extrabytes > 3 || src_size < extrabytes) {
880 av_log(avctx, AV_LOG_ERROR, "ePIC: invalid data size VLI\n");
881 return AVERROR_INVALIDDATA;
884 els_dsize = prefix & ((0x80 >> extrabytes) - 1); // mask out the length prefix
885 while (extrabytes-- > 0) {
886 els_dsize = (els_dsize << 8) | *src++;
890 if (src_size < els_dsize) {
891 av_log(avctx, AV_LOG_ERROR, "ePIC: data too short, needed %"SIZE_SPECIFIER", got %"SIZE_SPECIFIER"\n",
892 els_dsize, src_size);
893 return AVERROR_INVALIDDATA;
896 tile_width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
897 tile_height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
898 awidth = FFALIGN(tile_width, 16);
899 aheight = FFALIGN(tile_height, 16);
901 if (tile_width > (1 << FF_ARRAY_ELEMS(c->ec.prev_row_rung))) {
902 avpriv_request_sample(avctx, "large tile width");
903 return AVERROR_INVALIDDATA;
908 uint8_t tr_r, tr_g, tr_b, *buf;
910 /* ELS decoder initializations */
911 memset(&c->ec, 0, sizeof(c->ec));
912 ff_els_decoder_init(&c->ec.els_ctx, src, els_dsize);
913 epic_hash_init(&c->ec.hash);
915 /* decode transparent pixel value */
916 tr_r = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
917 tr_g = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
918 tr_b = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
919 if (c->ec.els_ctx.err != 0) {
920 av_log(avctx, AV_LOG_ERROR,
921 "ePIC: couldn't decode transparency pixel!\n");
922 ff_els_decoder_uninit(&c->ec.unsigned_rung);
923 return AVERROR_INVALIDDATA;
926 ret = epic_decode_tile(&c->ec, c->epic_buf, tile_height, tile_width,
929 epic_free_pixel_cache(&c->ec.hash);
930 ff_els_decoder_uninit(&c->ec.unsigned_rung);
933 av_log(avctx, AV_LOG_ERROR,
934 "ePIC: tile decoding failed, frame=%d, tile_x=%d, tile_y=%d\n",
935 avctx->frame_number, tile_x, tile_y);
936 return AVERROR_INVALIDDATA;
940 dst = c->framebuf + tile_x * c->tile_width * 3 +
941 tile_y * c->tile_height * c->framebuf_stride;
943 for (j = 0; j < tile_height; j++) {
945 in = (uint32_t *) buf;
946 for (i = 0; i < tile_width; i++) {
947 out[0] = (in[i] >> R_shift) & 0xFF;
948 out[1] = (in[i] >> G_shift) & 0xFF;
949 out[2] = (in[i] >> B_shift) & 0xFF;
952 buf += c->epic_buf_stride;
953 dst += c->framebuf_stride;
956 if (src_size > els_dsize) {
959 int bstride = FFALIGN(tile_width, 16) >> 3;
961 int estride = c->epic_buf_stride >> 2;
964 src_size -= els_dsize;
966 in = (uint32_t *) c->epic_buf;
967 tr = (tr_r << R_shift) | (tr_g << G_shift) | (tr_b << B_shift);
969 memset(c->kempf_flags, 0,
970 (aheight >> 3) * bstride * sizeof(*c->kempf_flags));
971 for (j = 0; j < tile_height; j += 8) {
972 for (i = 0; i < tile_width; i += 8) {
973 c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 0;
974 for (k = 0; k < 8 * 8; k++) {
975 if (in[i + (k & 7) + (k >> 3) * estride] == tr) {
976 c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 1;
985 memset(c->jpeg_tile, 0, c->tile_stride * aheight);
986 jpg_decode_data(&c->jc, awidth, aheight, src, src_size,
987 c->jpeg_tile, c->tile_stride,
988 c->kempf_flags, bstride, nblocks, c->swapuv);
990 in = (uint32_t *) c->epic_buf;
991 dst = c->framebuf + tile_x * c->tile_width * 3 +
992 tile_y * c->tile_height * c->framebuf_stride;
994 for (j = 0; j < tile_height; j++) {
995 for (i = 0; i < tile_width; i++)
997 memcpy(dst + i * 3, jpg + i * 3, 3);
998 in += c->epic_buf_stride >> 2;
999 dst += c->framebuf_stride;
1000 jpg += c->tile_stride;
1004 dst = c->framebuf + tile_x * c->tile_width * 3 +
1005 tile_y * c->tile_height * c->framebuf_stride;
1006 return jpg_decode_data(&c->jc, tile_width, tile_height, src, src_size,
1007 dst, c->framebuf_stride, NULL, 0, 0, c->swapuv);
1013 static int kempf_restore_buf(const uint8_t *src, int len,
1014 uint8_t *dst, int stride,
1015 const uint8_t *jpeg_tile, int tile_stride,
1016 int width, int height,
1017 const uint8_t *pal, int npal, int tidx)
1022 int align_width = FFALIGN(width, 16);
1024 if ((ret = init_get_bits8(&gb, src, len)) < 0)
1027 if (npal <= 2) nb = 1;
1028 else if (npal <= 4) nb = 2;
1029 else if (npal <= 16) nb = 4;
1032 for (j = 0; j < height; j++, dst += stride, jpeg_tile = FF_PTR_ADD(jpeg_tile, tile_stride)) {
1033 if (get_bits(&gb, 8))
1035 for (i = 0; i < width; i++) {
1036 col = get_bits(&gb, nb);
1038 memcpy(dst + i * 3, pal + col * 3, 3);
1040 memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
1042 skip_bits_long(&gb, nb * (align_width - width));
1048 static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y,
1049 const uint8_t *src, int src_size)
1052 int hdr, zsize, npal, tidx = -1, ret;
1054 const uint8_t *src_end = src + src_size;
1055 uint8_t pal[768], transp[3];
1056 uLongf dlen = (c->tile_width + 1) * c->tile_height;
1058 int nblocks, cblocks, bstride;
1059 int bits, bitbuf, coded;
1060 uint8_t *dst = c->framebuf + tile_x * c->tile_width * 3 +
1061 tile_y * c->tile_height * c->framebuf_stride;
1064 return AVERROR_INVALIDDATA;
1066 width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
1067 height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
1070 sub_type = hdr >> 5;
1071 if (sub_type == 0) {
1073 memcpy(transp, src, 3);
1075 for (j = 0; j < height; j++, dst += c->framebuf_stride)
1076 for (i = 0; i < width; i++)
1077 memcpy(dst + i * 3, transp, 3);
1079 } else if (sub_type == 1) {
1080 return jpg_decode_data(&c->jc, width, height, src, src_end - src,
1081 dst, c->framebuf_stride, NULL, 0, 0, 0);
1084 if (sub_type != 2) {
1085 memcpy(transp, src, 3);
1089 if (src_end - src < npal * 3)
1090 return AVERROR_INVALIDDATA;
1091 memcpy(pal, src, npal * 3);
1093 if (sub_type != 2) {
1094 for (i = 0; i < npal; i++) {
1095 if (!memcmp(pal + i * 3, transp, 3)) {
1102 if (src_end - src < 2)
1104 zsize = (src[0] << 8) | src[1];
1107 if (src_end - src < zsize + (sub_type != 2))
1108 return AVERROR_INVALIDDATA;
1110 ret = uncompress(c->kempf_buf, &dlen, src, zsize);
1112 return AVERROR_INVALIDDATA;
1115 if (sub_type == 2) {
1116 kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
1117 NULL, 0, width, height, pal, npal, tidx);
1121 nblocks = *src++ + 1;
1123 bstride = FFALIGN(width, 16) >> 3;
1124 // blocks are coded LSB and we need normal bitreader for JPEG data
1126 for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
1127 for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
1130 return AVERROR_INVALIDDATA;
1138 if (cblocks > nblocks)
1139 return AVERROR_INVALIDDATA;
1140 c->kempf_flags[j * 2 + i * 2 * bstride] =
1141 c->kempf_flags[j * 2 + 1 + i * 2 * bstride] =
1142 c->kempf_flags[j * 2 + (i * 2 + 1) * bstride] =
1143 c->kempf_flags[j * 2 + 1 + (i * 2 + 1) * bstride] = coded;
1147 memset(c->jpeg_tile, 0, c->tile_stride * height);
1148 jpg_decode_data(&c->jc, width, height, src, src_end - src,
1149 c->jpeg_tile, c->tile_stride,
1150 c->kempf_flags, bstride, nblocks * 4, 0);
1152 kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
1153 c->jpeg_tile, c->tile_stride,
1154 width, height, pal, npal, tidx);
1159 static int g2m_init_buffers(G2MContext *c)
1163 if (!c->framebuf || c->old_width < c->width || c->old_height < c->height) {
1164 c->framebuf_stride = FFALIGN(c->width + 15, 16) * 3;
1165 aligned_height = c->height + 15;
1166 av_free(c->framebuf);
1167 c->framebuf = av_mallocz_array(c->framebuf_stride, aligned_height);
1169 return AVERROR(ENOMEM);
1171 if (!c->synth_tile || !c->jpeg_tile ||
1172 (c->compression == 2 && !c->epic_buf_base) ||
1173 c->old_tile_w < c->tile_width ||
1174 c->old_tile_h < c->tile_height) {
1175 c->tile_stride = FFALIGN(c->tile_width, 16) * 3;
1176 c->epic_buf_stride = FFALIGN(c->tile_width * 4, 16);
1177 aligned_height = FFALIGN(c->tile_height, 16);
1178 av_freep(&c->synth_tile);
1179 av_freep(&c->jpeg_tile);
1180 av_freep(&c->kempf_buf);
1181 av_freep(&c->kempf_flags);
1182 av_freep(&c->epic_buf_base);
1184 c->synth_tile = av_mallocz(c->tile_stride * aligned_height);
1185 c->jpeg_tile = av_mallocz(c->tile_stride * aligned_height);
1186 c->kempf_buf = av_mallocz((c->tile_width + 1) * aligned_height +
1187 AV_INPUT_BUFFER_PADDING_SIZE);
1188 c->kempf_flags = av_mallocz(c->tile_width * aligned_height);
1189 if (!c->synth_tile || !c->jpeg_tile ||
1190 !c->kempf_buf || !c->kempf_flags)
1191 return AVERROR(ENOMEM);
1192 if (c->compression == 2) {
1193 c->epic_buf_base = av_mallocz(c->epic_buf_stride * aligned_height + 4);
1194 if (!c->epic_buf_base)
1195 return AVERROR(ENOMEM);
1196 c->epic_buf = c->epic_buf_base + 4;
1203 static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c,
1209 uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
1210 uint32_t cursor_hot_x, cursor_hot_y;
1211 int cursor_fmt, err;
1213 cur_size = bytestream2_get_be32(gb);
1214 cursor_w = bytestream2_get_byte(gb);
1215 cursor_h = bytestream2_get_byte(gb);
1216 cursor_hot_x = bytestream2_get_byte(gb);
1217 cursor_hot_y = bytestream2_get_byte(gb);
1218 cursor_fmt = bytestream2_get_byte(gb);
1220 cursor_stride = FFALIGN(cursor_w, cursor_fmt==1 ? 32 : 1) * 4;
1222 if (cursor_w < 1 || cursor_w > 256 ||
1223 cursor_h < 1 || cursor_h > 256) {
1224 av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %"PRIu32"x%"PRIu32"\n",
1225 cursor_w, cursor_h);
1226 return AVERROR_INVALIDDATA;
1228 if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
1229 av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %"PRIu32",%"PRIu32"\n",
1230 cursor_hot_x, cursor_hot_y);
1231 cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);
1232 cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);
1234 if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||
1235 c->cursor_w * c->cursor_h / 4 > cur_size) {
1236 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"/%u\n",
1237 cur_size, bytestream2_get_bytes_left(gb));
1238 return AVERROR_INVALIDDATA;
1240 if (cursor_fmt != 1 && cursor_fmt != 32) {
1241 avpriv_report_missing_feature(avctx, "Cursor format %d",
1243 return AVERROR_PATCHWELCOME;
1246 if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
1247 av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
1251 c->cursor_w = cursor_w;
1252 c->cursor_h = cursor_h;
1253 c->cursor_hot_x = cursor_hot_x;
1254 c->cursor_hot_y = cursor_hot_y;
1255 c->cursor_fmt = cursor_fmt;
1256 c->cursor_stride = cursor_stride;
1259 switch (c->cursor_fmt) {
1260 case 1: // old monochrome
1261 for (j = 0; j < c->cursor_h; j++) {
1262 for (i = 0; i < c->cursor_w; i += 32) {
1263 bits = bytestream2_get_be32(gb);
1264 for (k = 0; k < 32; k++) {
1265 dst[0] = !!(bits & 0x80000000);
1273 for (j = 0; j < c->cursor_h; j++) {
1274 for (i = 0; i < c->cursor_w; i += 32) {
1275 bits = bytestream2_get_be32(gb);
1276 for (k = 0; k < 32; k++) {
1277 int mask_bit = !!(bits & 0x80000000);
1278 switch (dst[0] * 2 + mask_bit) {
1303 case 32: // full colour
1304 /* skip monochrome version of the cursor and decode RGBA instead */
1305 bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
1306 for (j = 0; j < c->cursor_h; j++) {
1307 for (i = 0; i < c->cursor_w; i++) {
1308 int val = bytestream2_get_be32(gb);
1317 return AVERROR_PATCHWELCOME;
1322 #define APPLY_ALPHA(src, new, alpha) \
1323 src = (src * (256 - alpha) + new * alpha) >> 8
1325 static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
1329 const uint8_t *cursor;
1334 x = c->cursor_x - c->cursor_hot_x;
1335 y = c->cursor_y - c->cursor_hot_y;
1341 if (x + w > c->width)
1343 if (y + h > c->height)
1357 cursor += -y * c->cursor_stride;
1362 for (j = 0; j < h; j++) {
1363 for (i = 0; i < w; i++) {
1364 uint8_t alpha = cursor[i * 4];
1365 APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
1366 APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
1367 APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
1370 cursor += c->cursor_stride;
1374 static int g2m_decode_frame(AVCodecContext *avctx, void *data,
1375 int *got_picture_ptr, AVPacket *avpkt)
1377 const uint8_t *buf = avpkt->data;
1378 int buf_size = avpkt->size;
1379 G2MContext *c = avctx->priv_data;
1380 AVFrame *pic = data;
1381 GetByteContext bc, tbc;
1384 uint32_t chunk_size, r_mask, g_mask, b_mask;
1385 int chunk_type, chunk_start;
1389 if (buf_size < 12) {
1390 av_log(avctx, AV_LOG_ERROR,
1391 "Frame should have at least 12 bytes, got %d instead\n",
1393 return AVERROR_INVALIDDATA;
1396 bytestream2_init(&bc, buf, buf_size);
1398 magic = bytestream2_get_be32(&bc);
1399 if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
1400 (magic & 0xF) < 2 || (magic & 0xF) > 5) {
1401 av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
1402 return AVERROR_INVALIDDATA;
1405 c->swapuv = magic == MKBETAG('G', '2', 'M', '2');
1407 while (bytestream2_get_bytes_left(&bc) > 5) {
1408 chunk_size = bytestream2_get_le32(&bc) - 1;
1409 chunk_type = bytestream2_get_byte(&bc);
1410 chunk_start = bytestream2_tell(&bc);
1411 if (chunk_size > bytestream2_get_bytes_left(&bc)) {
1412 av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %"PRIu32" type %02X\n",
1413 chunk_size, chunk_type);
1416 switch (chunk_type) {
1420 if (chunk_size < 21) {
1421 av_log(avctx, AV_LOG_ERROR, "Invalid display info size %"PRIu32"\n",
1425 c->width = bytestream2_get_be32(&bc);
1426 c->height = bytestream2_get_be32(&bc);
1427 if (c->width < 16 || c->height < 16) {
1428 av_log(avctx, AV_LOG_ERROR,
1429 "Invalid frame dimensions %dx%d\n",
1430 c->width, c->height);
1431 ret = AVERROR_INVALIDDATA;
1434 if (c->width != avctx->width || c->height != avctx->height) {
1435 ret = ff_set_dimensions(avctx, c->width, c->height);
1439 c->compression = bytestream2_get_be32(&bc);
1440 if (c->compression != 2 && c->compression != 3) {
1441 avpriv_report_missing_feature(avctx, "Compression method %d",
1443 ret = AVERROR_PATCHWELCOME;
1446 c->tile_width = bytestream2_get_be32(&bc);
1447 c->tile_height = bytestream2_get_be32(&bc);
1448 if (c->tile_width <= 0 || c->tile_height <= 0 ||
1449 ((c->tile_width | c->tile_height) & 0xF) ||
1450 c->tile_width * (uint64_t)c->tile_height >= INT_MAX / 4 ||
1451 av_image_check_size2(c->tile_width, c->tile_height, avctx->max_pixels, avctx->pix_fmt, 0, avctx) < 0
1453 av_log(avctx, AV_LOG_ERROR,
1454 "Invalid tile dimensions %dx%d\n",
1455 c->tile_width, c->tile_height);
1456 ret = AVERROR_INVALIDDATA;
1459 c->tiles_x = (c->width + c->tile_width - 1) / c->tile_width;
1460 c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
1461 c->bpp = bytestream2_get_byte(&bc);
1463 if (bytestream2_get_bytes_left(&bc) < 16 ||
1464 (chunk_size - 21) < 16) {
1465 av_log(avctx, AV_LOG_ERROR,
1466 "Display info: missing bitmasks!\n");
1467 ret = AVERROR_INVALIDDATA;
1470 r_mask = bytestream2_get_be32(&bc);
1471 g_mask = bytestream2_get_be32(&bc);
1472 b_mask = bytestream2_get_be32(&bc);
1473 if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
1474 avpriv_report_missing_feature(avctx,
1475 "Bitmasks: R=%"PRIX32", G=%"PRIX32", B=%"PRIX32,
1476 r_mask, g_mask, b_mask);
1477 ret = AVERROR_PATCHWELCOME;
1481 avpriv_request_sample(avctx, "bpp=%d", c->bpp);
1482 ret = AVERROR_PATCHWELCOME;
1485 if (g2m_init_buffers(c)) {
1486 ret = AVERROR(ENOMEM);
1492 if (!c->tiles_x || !c->tiles_y) {
1493 av_log(avctx, AV_LOG_WARNING,
1494 "No display info - skipping tile\n");
1497 if (chunk_size < 2) {
1498 av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %"PRIu32"\n",
1502 c->tile_x = bytestream2_get_byte(&bc);
1503 c->tile_y = bytestream2_get_byte(&bc);
1504 if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
1505 av_log(avctx, AV_LOG_ERROR,
1506 "Invalid tile pos %d,%d (in %dx%d grid)\n",
1507 c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
1511 switch (c->compression) {
1512 case COMPR_EPIC_J_B:
1513 ret = epic_jb_decode_tile(c, c->tile_x, c->tile_y,
1514 buf + bytestream2_tell(&bc),
1515 chunk_size - 2, avctx);
1517 case COMPR_KEMPF_J_B:
1518 ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
1519 buf + bytestream2_tell(&bc),
1523 if (ret && c->framebuf)
1524 av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
1525 c->tile_x, c->tile_y);
1528 if (chunk_size < 5) {
1529 av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %"PRIu32"\n",
1533 c->cursor_x = bytestream2_get_be16(&bc);
1534 c->cursor_y = bytestream2_get_be16(&bc);
1537 if (chunk_size < 8) {
1538 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"\n",
1542 bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
1544 g2m_load_cursor(avctx, c, &tbc);
1550 av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02d\n",
1554 /* navigate to next chunk */
1555 bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
1560 if (c->width && c->height && c->framebuf) {
1561 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
1564 pic->key_frame = got_header;
1565 pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1567 for (i = 0; i < avctx->height; i++)
1568 memcpy(pic->data[0] + i * pic->linesize[0],
1569 c->framebuf + i * c->framebuf_stride,
1571 g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);
1573 *got_picture_ptr = 1;
1588 static av_cold int g2m_decode_init(AVCodecContext *avctx)
1590 G2MContext *const c = avctx->priv_data;
1593 if ((ret = jpg_init(avctx, &c->jc)) != 0) {
1594 av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
1595 jpg_free_context(&c->jc);
1596 return AVERROR(ENOMEM);
1599 avctx->pix_fmt = AV_PIX_FMT_RGB24;
1601 // store original sizes and check against those if resize happens
1602 c->orig_width = avctx->width;
1603 c->orig_height = avctx->height;
1608 static av_cold int g2m_decode_end(AVCodecContext *avctx)
1610 G2MContext *const c = avctx->priv_data;
1612 jpg_free_context(&c->jc);
1614 av_freep(&c->epic_buf_base);
1616 av_freep(&c->kempf_buf);
1617 av_freep(&c->kempf_flags);
1618 av_freep(&c->synth_tile);
1619 av_freep(&c->jpeg_tile);
1620 av_freep(&c->cursor);
1621 av_freep(&c->framebuf);
1626 const AVCodec ff_g2m_decoder = {
1628 .long_name = NULL_IF_CONFIG_SMALL("Go2Meeting"),
1629 .type = AVMEDIA_TYPE_VIDEO,
1630 .id = AV_CODEC_ID_G2M,
1631 .priv_data_size = sizeof(G2MContext),
1632 .init = g2m_decode_init,
1633 .close = g2m_decode_end,
1634 .decode = g2m_decode_frame,
1635 .capabilities = AV_CODEC_CAP_DR1,
1636 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,