g2meet.c 52.2 KB
Newer Older
Kostya Shishkov's avatar
Kostya Shishkov committed
1
/*
Kostya Shishkov's avatar
Kostya Shishkov committed
2
 * Go2Webinar / Go2Meeting decoder
Kostya Shishkov's avatar
Kostya Shishkov committed
3
 * Copyright (c) 2012 Konstantin Shishkov
Kostya Shishkov's avatar
Kostya Shishkov committed
4
 * Copyright (c) 2013 Maxim Poliakovski
Kostya Shishkov's avatar
Kostya Shishkov committed
5
 *
6
 * This file is part of FFmpeg.
Kostya Shishkov's avatar
Kostya Shishkov committed
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
Kostya Shishkov's avatar
Kostya Shishkov committed
9 10 11 12
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * 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,
Kostya Shishkov's avatar
Kostya Shishkov committed
14 15 16 17 18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
Kostya Shishkov's avatar
Kostya Shishkov committed
20 21 22 23 24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
Kostya Shishkov's avatar
Kostya Shishkov committed
25
 * Go2Webinar / Go2Meeting decoder
Kostya Shishkov's avatar
Kostya Shishkov committed
26 27
 */

28
#include <inttypes.h>
Kostya Shishkov's avatar
Kostya Shishkov committed
29 30 31
#include <zlib.h>

#include "libavutil/intreadwrite.h"
32

Kostya Shishkov's avatar
Kostya Shishkov committed
33
#include "avcodec.h"
34
#include "blockdsp.h"
Kostya Shishkov's avatar
Kostya Shishkov committed
35
#include "bytestream.h"
Kostya Shishkov's avatar
Kostya Shishkov committed
36
#include "elsdec.h"
Kostya Shishkov's avatar
Kostya Shishkov committed
37
#include "get_bits.h"
38
#include "idctdsp.h"
Kostya Shishkov's avatar
Kostya Shishkov committed
39
#include "internal.h"
40
#include "jpegtables.h"
Kostya Shishkov's avatar
Kostya Shishkov committed
41 42
#include "mjpeg.h"

Kostya Shishkov's avatar
Kostya Shishkov committed
43 44 45
#define EPIC_PIX_STACK_SIZE 1024
#define EPIC_PIX_STACK_MAX  (EPIC_PIX_STACK_SIZE - 1)

Kostya Shishkov's avatar
Kostya Shishkov committed
46
enum ChunkType {
47
    DISPLAY_INFO = 0xC8,
Kostya Shishkov's avatar
Kostya Shishkov committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    TILE_DATA,
    CURSOR_POS,
    CURSOR_SHAPE,
    CHUNK_CC,
    CHUNK_CD
};

enum Compression {
    COMPR_EPIC_J_B = 2,
    COMPR_KEMPF_J_B,
};

static const uint8_t luma_quant[64] = {
     8,  6,  5,  8, 12, 20, 26, 31,
     6,  6,  7, 10, 13, 29, 30, 28,
     7,  7,  8, 12, 20, 29, 35, 28,
     7,  9, 11, 15, 26, 44, 40, 31,
     9, 11, 19, 28, 34, 55, 52, 39,
    12, 18, 28, 32, 41, 52, 57, 46,
    25, 32, 39, 44, 52, 61, 60, 51,
    36, 46, 48, 49, 56, 50, 52, 50
};

static const uint8_t chroma_quant[64] = {
     9,  9, 12, 24, 50, 50, 50, 50,
     9, 11, 13, 33, 50, 50, 50, 50,
    12, 13, 28, 50, 50, 50, 50, 50,
    24, 33, 50, 50, 50, 50, 50, 50,
    50, 50, 50, 50, 50, 50, 50, 50,
    50, 50, 50, 50, 50, 50, 50, 50,
    50, 50, 50, 50, 50, 50, 50, 50,
    50, 50, 50, 50, 50, 50, 50, 50,
};

Kostya Shishkov's avatar
Kostya Shishkov committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
typedef struct ePICPixListElem {
    struct ePICPixListElem *next;
    uint32_t               pixel;
    uint8_t                rung;
} ePICPixListElem;

typedef struct ePICPixHashElem {
    uint32_t                pix_id;
    struct ePICPixListElem  *list;
} ePICPixHashElem;

#define EPIC_HASH_SIZE 256
typedef struct ePICPixHash {
    ePICPixHashElem *bucket[EPIC_HASH_SIZE];
    int              bucket_size[EPIC_HASH_SIZE];
    int              bucket_fill[EPIC_HASH_SIZE];
} ePICPixHash;

typedef struct ePICContext {
    ElsDecCtx        els_ctx;
    int              next_run_pos;
    ElsUnsignedRung  unsigned_rung;
    uint8_t          W_flag_rung;
    uint8_t          N_flag_rung;
    uint8_t          W_ctx_rung[256];
    uint8_t          N_ctx_rung[512];
    uint8_t          nw_pred_rung[256];
    uint8_t          ne_pred_rung[256];
    uint8_t          prev_row_rung[14];
    uint8_t          runlen_zeroes[14];
    uint8_t          runlen_one;
    int              stack_pos;
    uint32_t         stack[EPIC_PIX_STACK_SIZE];
    ePICPixHash      hash;
} ePICContext;

Kostya Shishkov's avatar
Kostya Shishkov committed
118
typedef struct JPGContext {
119
    BlockDSPContext bdsp;
120
    IDCTDSPContext idsp;
Kostya Shishkov's avatar
Kostya Shishkov committed
121 122 123 124 125 126 127 128 129 130
    ScanTable  scantable;

    VLC        dc_vlc[2], ac_vlc[2];
    int        prev_dc[3];
    DECLARE_ALIGNED(16, int16_t, block)[6][64];

    uint8_t    *buf;
} JPGContext;

typedef struct G2MContext {
Kostya Shishkov's avatar
Kostya Shishkov committed
131
    ePICContext ec;
Kostya Shishkov's avatar
Kostya Shishkov committed
132
    JPGContext jc;
Kostya Shishkov's avatar
Kostya Shishkov committed
133

Kostya Shishkov's avatar
Kostya Shishkov committed
134 135 136 137
    int        version;

    int        compression;
    int        width, height, bpp;
138
    int        orig_width, orig_height;
Kostya Shishkov's avatar
Kostya Shishkov committed
139 140 141 142 143 144 145 146
    int        tile_width, tile_height;
    int        tiles_x, tiles_y, tile_x, tile_y;

    int        got_header;

    uint8_t    *framebuf;
    int        framebuf_stride, old_width, old_height;

Kostya Shishkov's avatar
Kostya Shishkov committed
147 148 149
    uint8_t    *synth_tile, *jpeg_tile, *epic_buf, *epic_buf_base;
    int        tile_stride, epic_buf_stride, old_tile_w, old_tile_h;
    int        swapuv;
Kostya Shishkov's avatar
Kostya Shishkov committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

    uint8_t    *kempf_buf, *kempf_flags;

    uint8_t    *cursor;
    int        cursor_stride;
    int        cursor_fmt;
    int        cursor_w, cursor_h, cursor_x, cursor_y;
    int        cursor_hot_x, cursor_hot_y;
} G2MContext;

static av_cold int build_vlc(VLC *vlc, const uint8_t *bits_table,
                             const uint8_t *val_table, int nb_codes,
                             int is_ac)
{
    uint8_t  huff_size[256] = { 0 };
    uint16_t huff_code[256];
    uint16_t huff_sym[256];
    int i;

    ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);

    for (i = 0; i < 256; i++)
        huff_sym[i] = i + 16 * is_ac;

    if (is_ac)
        huff_sym[0] = 16 * 256;

    return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
                              huff_code, 2, 2, huff_sym, 2, 2, 0);
}

static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c)
{
    int ret;

    ret = build_vlc(&c->dc_vlc[0], avpriv_mjpeg_bits_dc_luminance,
                    avpriv_mjpeg_val_dc, 12, 0);
    if (ret)
        return ret;
    ret = build_vlc(&c->dc_vlc[1], avpriv_mjpeg_bits_dc_chrominance,
                    avpriv_mjpeg_val_dc, 12, 0);
    if (ret)
        return ret;
    ret = build_vlc(&c->ac_vlc[0], avpriv_mjpeg_bits_ac_luminance,
                    avpriv_mjpeg_val_ac_luminance, 251, 1);
    if (ret)
        return ret;
    ret = build_vlc(&c->ac_vlc[1], avpriv_mjpeg_bits_ac_chrominance,
                    avpriv_mjpeg_val_ac_chrominance, 251, 1);
    if (ret)
        return ret;

202
    ff_blockdsp_init(&c->bdsp, avctx);
203 204
    ff_idctdsp_init(&c->idsp, avctx);
    ff_init_scantable(c->idsp.idct_permutation, &c->scantable,
Kostya Shishkov's avatar
Kostya Shishkov committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
                      ff_zigzag_direct);

    return 0;
}

static av_cold void jpg_free_context(JPGContext *ctx)
{
    int i;

    for (i = 0; i < 2; i++) {
        ff_free_vlc(&ctx->dc_vlc[i]);
        ff_free_vlc(&ctx->ac_vlc[i]);
    }

    av_freep(&ctx->buf);
}

static void jpg_unescape(const uint8_t *src, int src_size,
                         uint8_t *dst, int *dst_size)
{
    const uint8_t *src_end = src + src_size;
    uint8_t *dst_start = dst;

    while (src < src_end) {
        uint8_t x = *src++;

        *dst++ = x;

        if (x == 0xFF && !*src)
            src++;
    }
    *dst_size = dst - dst_start;
}

static int jpg_decode_block(JPGContext *c, GetBitContext *gb,
                            int plane, int16_t *block)
{
    int dc, val, pos;
    const int is_chroma = !!plane;
    const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant;

246
    c->bdsp.clear_block(block);
Kostya Shishkov's avatar
Kostya Shishkov committed
247 248 249 250 251
    dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 3);
    if (dc < 0)
        return AVERROR_INVALIDDATA;
    if (dc)
        dc = get_xbits(gb, dc);
252 253
    dc                = dc * qmat[0] + c->prev_dc[plane];
    block[0]          = dc;
Kostya Shishkov's avatar
Kostya Shishkov committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267
    c->prev_dc[plane] = dc;

    pos = 0;
    while (pos < 63) {
        val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 3);
        if (val < 0)
            return AVERROR_INVALIDDATA;
        pos += val >> 4;
        val &= 0xF;
        if (pos > 63)
            return val ? AVERROR_INVALIDDATA : 0;
        if (val) {
            int nbits = val;

268 269
            val                                 = get_xbits(gb, nbits);
            val                                *= qmat[ff_zigzag_direct[pos]];
Kostya Shishkov's avatar
Kostya Shishkov committed
270 271 272 273 274 275
            block[c->scantable.permutated[pos]] = val;
        }
    }
    return 0;
}

Kostya Shishkov's avatar
Kostya Shishkov committed
276
static inline void yuv2rgb(uint8_t *out, int ridx, int Y, int U, int V)
Kostya Shishkov's avatar
Kostya Shishkov committed
277
{
Kostya Shishkov's avatar
Kostya Shishkov committed
278 279 280
    out[ridx]     = av_clip_uint8(Y +              (91881 * V + 32768 >> 16));
    out[1]        = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
    out[2 - ridx] = av_clip_uint8(Y + (116130 * U             + 32768 >> 16));
Kostya Shishkov's avatar
Kostya Shishkov committed
281 282 283 284 285 286 287 288 289 290 291 292 293
}

static int jpg_decode_data(JPGContext *c, int width, int height,
                           const uint8_t *src, int src_size,
                           uint8_t *dst, int dst_stride,
                           const uint8_t *mask, int mask_stride, int num_mbs,
                           int swapuv)
{
    GetBitContext gb;
    int mb_w, mb_h, mb_x, mb_y, i, j;
    int bx, by;
    int unesc_size;
    int ret;
Kostya Shishkov's avatar
Kostya Shishkov committed
294
    const int ridx = swapuv ? 2 : 0;
Kostya Shishkov's avatar
Kostya Shishkov committed
295

296
    if ((ret = av_reallocp(&c->buf,
297
                           src_size + AV_INPUT_BUFFER_PADDING_SIZE)) < 0)
298
        return ret;
Kostya Shishkov's avatar
Kostya Shishkov committed
299
    jpg_unescape(src, src_size, c->buf, &unesc_size);
300
    memset(c->buf + unesc_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
301 302
    if((ret = init_get_bits8(&gb, c->buf, unesc_size)) < 0)
        return ret;
Kostya Shishkov's avatar
Kostya Shishkov committed
303 304 305 306 307 308

    width = FFALIGN(width, 16);
    mb_w  =  width        >> 4;
    mb_h  = (height + 15) >> 4;

    if (!num_mbs)
309
        num_mbs = mb_w * mb_h * 4;
Kostya Shishkov's avatar
Kostya Shishkov committed
310 311 312

    for (i = 0; i < 3; i++)
        c->prev_dc[i] = 1024;
313 314
    bx =
    by = 0;
315
    c->bdsp.clear_blocks(c->block[0]);
Kostya Shishkov's avatar
Kostya Shishkov committed
316 317
    for (mb_y = 0; mb_y < mb_h; mb_y++) {
        for (mb_x = 0; mb_x < mb_w; mb_x++) {
318 319 320
            if (mask && !mask[mb_x * 2] && !mask[mb_x * 2 + 1] &&
                !mask[mb_x * 2 +     mask_stride] &&
                !mask[mb_x * 2 + 1 + mask_stride]) {
Kostya Shishkov's avatar
Kostya Shishkov committed
321 322 323 324 325
                bx += 16;
                continue;
            }
            for (j = 0; j < 2; j++) {
                for (i = 0; i < 2; i++) {
326 327 328
                    if (mask && !mask[mb_x * 2 + i + j * mask_stride])
                        continue;
                    num_mbs--;
Kostya Shishkov's avatar
Kostya Shishkov committed
329 330 331
                    if ((ret = jpg_decode_block(c, &gb, 0,
                                                c->block[i + j * 2])) != 0)
                        return ret;
332
                    c->idsp.idct(c->block[i + j * 2]);
Kostya Shishkov's avatar
Kostya Shishkov committed
333 334 335 336 337
                }
            }
            for (i = 1; i < 3; i++) {
                if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0)
                    return ret;
338
                c->idsp.idct(c->block[i + 3]);
Kostya Shishkov's avatar
Kostya Shishkov committed
339 340 341 342 343 344 345 346
            }

            for (j = 0; j < 16; j++) {
                uint8_t *out = dst + bx * 3 + (by + j) * dst_stride;
                for (i = 0; i < 16; i++) {
                    int Y, U, V;

                    Y = c->block[(j >> 3) * 2 + (i >> 3)][(i & 7) + (j & 7) * 8];
Kostya Shishkov's avatar
Kostya Shishkov committed
347 348 349
                    U = c->block[4][(i >> 1) + (j >> 1) * 8] - 128;
                    V = c->block[5][(i >> 1) + (j >> 1) * 8] - 128;
                    yuv2rgb(out + i * 3, ridx, Y, U, V);
Kostya Shishkov's avatar
Kostya Shishkov committed
350 351 352
                }
            }

353
            if (!num_mbs)
Kostya Shishkov's avatar
Kostya Shishkov committed
354 355 356 357 358 359
                return 0;
            bx += 16;
        }
        bx  = 0;
        by += 16;
        if (mask)
360
            mask += mask_stride * 2;
Kostya Shishkov's avatar
Kostya Shishkov committed
361 362 363 364 365
    }

    return 0;
}

Kostya Shishkov's avatar
Kostya Shishkov committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
#define LOAD_NEIGHBOURS(x)      \
    W   = curr_row[(x)   - 1];  \
    N   = above_row[(x)];       \
    WW  = curr_row[(x)   - 2];  \
    NW  = above_row[(x)  - 1];  \
    NE  = above_row[(x)  + 1];  \
    NN  = above2_row[(x)];      \
    NNW = above2_row[(x) - 1];  \
    NWW = above_row[(x)  - 2];  \
    NNE = above2_row[(x) + 1]

#define UPDATE_NEIGHBOURS(x)    \
    NNW = NN;                   \
    NN  = NNE;                  \
    NWW = NW;                   \
    NW  = N;                    \
    N   = NE;                   \
    NE  = above_row[(x)  + 1];  \
    NNE = above2_row[(x) + 1]

#define R_shift 16
#define G_shift  8
#define B_shift  0

/* improved djb2 hash from http://www.cse.yorku.ca/~oz/hash.html */
static int djb2_hash(uint32_t key)
{
393
    uint32_t h = 5381;
Kostya Shishkov's avatar
Kostya Shishkov committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557

    h = (h * 33) ^ ((key >> 24) & 0xFF); // xxx: probably not needed at all
    h = (h * 33) ^ ((key >> 16) & 0xFF);
    h = (h * 33) ^ ((key >>  8) & 0xFF);
    h = (h * 33) ^  (key        & 0xFF);

    return h & (EPIC_HASH_SIZE - 1);
}

static void epic_hash_init(ePICPixHash *hash)
{
    memset(hash, 0, sizeof(*hash));
}

static ePICPixHashElem *epic_hash_find(const ePICPixHash *hash, uint32_t key)
{
    int i, idx = djb2_hash(key);
    ePICPixHashElem *bucket = hash->bucket[idx];

    for (i = 0; i < hash->bucket_fill[idx]; i++)
        if (bucket[i].pix_id == key)
            return &bucket[i];

    return NULL;
}

static ePICPixHashElem *epic_hash_add(ePICPixHash *hash, uint32_t key)
{
    ePICPixHashElem *bucket, *ret;
    int idx = djb2_hash(key);

    if (hash->bucket_size[idx] > INT_MAX / sizeof(**hash->bucket))
        return NULL;

    if (!(hash->bucket_fill[idx] < hash->bucket_size[idx])) {
        int new_size = hash->bucket_size[idx] + 16;
        bucket = av_realloc(hash->bucket[idx], new_size * sizeof(*bucket));
        if (!bucket)
            return NULL;
        hash->bucket[idx]      = bucket;
        hash->bucket_size[idx] = new_size;
    }

    ret = &hash->bucket[idx][hash->bucket_fill[idx]++];
    memset(ret, 0, sizeof(*ret));
    ret->pix_id = key;
    return ret;
}

static int epic_add_pixel_to_cache(ePICPixHash *hash, uint32_t key, uint32_t pix)
{
    ePICPixListElem *new_elem;
    ePICPixHashElem *hash_elem = epic_hash_find(hash, key);

    if (!hash_elem) {
        if (!(hash_elem = epic_hash_add(hash, key)))
            return AVERROR(ENOMEM);
    }

    new_elem = av_mallocz(sizeof(*new_elem));
    if (!new_elem)
        return AVERROR(ENOMEM);

    new_elem->pixel = pix;
    new_elem->next  = hash_elem->list;
    hash_elem->list = new_elem;

    return 0;
}

static inline int epic_cache_entries_for_pixel(const ePICPixHash *hash,
                                               uint32_t pix)
{
    ePICPixHashElem *hash_elem = epic_hash_find(hash, pix);

    if (hash_elem != NULL && hash_elem->list != NULL)
        return 1;

    return 0;
}

static void epic_free_pixel_cache(ePICPixHash *hash)
{
    int i, j;

    for (i = 0; i < EPIC_HASH_SIZE; i++) {
        for (j = 0; j < hash->bucket_fill[i]; j++) {
            ePICPixListElem *list_elem = hash->bucket[i][j].list;
            while (list_elem) {
                ePICPixListElem *tmp = list_elem->next;
                av_free(list_elem);
                list_elem = tmp;
            }
        }
        av_freep(&hash->bucket[i]);
        hash->bucket_size[i] =
        hash->bucket_fill[i] = 0;
    }
}

static inline int is_pixel_on_stack(const ePICContext *dc, uint32_t pix)
{
    int i;

    for (i = 0; i < dc->stack_pos; i++)
        if (dc->stack[i] == pix)
            break;

    return i != dc->stack_pos;
}

#define TOSIGNED(val) (((val) >> 1) ^ -((val) & 1))

static inline int epic_decode_component_pred(ePICContext *dc,
                                             int N, int W, int NW)
{
    unsigned delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
    return mid_pred(N, N + W - NW, W) - TOSIGNED(delta);
}

static uint32_t epic_decode_pixel_pred(ePICContext *dc, int x, int y,
                                       const uint32_t *curr_row,
                                       const uint32_t *above_row)
{
    uint32_t N, W, NW, pred;
    unsigned delta;
    int GN, GW, GNW, R, G, B;

    if (x && y) {
        W  = curr_row[x  - 1];
        N  = above_row[x];
        NW = above_row[x - 1];

        GN  = (N  >> G_shift) & 0xFF;
        GW  = (W  >> G_shift) & 0xFF;
        GNW = (NW >> G_shift) & 0xFF;

        G = epic_decode_component_pred(dc, GN, GW, GNW);

        R = G + epic_decode_component_pred(dc,
                                           ((N  >> R_shift) & 0xFF) - GN,
                                           ((W  >> R_shift) & 0xFF) - GW,
                                           ((NW >> R_shift) & 0xFF) - GNW);

        B = G + epic_decode_component_pred(dc,
                                           ((N  >> B_shift) & 0xFF) - GN,
                                           ((W  >> B_shift) & 0xFF) - GW,
                                           ((NW >> B_shift) & 0xFF) - GNW);
    } else {
        if (x)
            pred = curr_row[x - 1];
        else
            pred = above_row[x];

        delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
        R     = ((pred >> R_shift) & 0xFF) - TOSIGNED(delta);

        delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
        G     = ((pred >> G_shift) & 0xFF) - TOSIGNED(delta);

        delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
        B     = ((pred >> B_shift) & 0xFF) - TOSIGNED(delta);
    }

558 559 560 561 562
    if (R<0 || G<0 || B<0) {
        av_log(NULL, AV_LOG_ERROR, "RGB %d %d %d is out of range\n", R, G, B);
        return 0;
    }

Kostya Shishkov's avatar
Kostya Shishkov committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
    return (R << R_shift) | (G << G_shift) | (B << B_shift);
}

static int epic_predict_pixel(ePICContext *dc, uint8_t *rung,
                              uint32_t *pPix, uint32_t pix)
{
    if (!ff_els_decode_bit(&dc->els_ctx, rung)) {
        *pPix = pix;
        return 1;
    }
    dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
    return 0;
}

static int epic_handle_edges(ePICContext *dc, int x, int y,
                             const uint32_t *curr_row,
                             const uint32_t *above_row, uint32_t *pPix)
{
    uint32_t pix;

    if (!x && !y) { /* special case: top-left pixel */
        /* the top-left pixel is coded independently with 3 unsigned numbers */
        *pPix = (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << R_shift) |
                (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << G_shift) |
                (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << B_shift);
        return 1;
    }

    if (x) { /* predict from W first */
        pix = curr_row[x - 1];
        if (epic_predict_pixel(dc, &dc->W_flag_rung, pPix, pix))
            return 1;
    }

    if (y) { /* then try to predict from N */
        pix = above_row[x];
        if (!dc->stack_pos || dc->stack[0] != pix) {
            if (epic_predict_pixel(dc, &dc->N_flag_rung, pPix, pix))
                return 1;
        }
    }

    return 0;
}

static int epic_decode_run_length(ePICContext *dc, int x, int y, int tile_width,
                                  const uint32_t *curr_row,
                                  const uint32_t *above_row,
                                  const uint32_t *above2_row,
                                  uint32_t *pPix, int *pRun)
{
    int idx, got_pixel = 0, WWneW, old_WWneW = 0;
    uint32_t W, WW, N, NN, NW, NE, NWW, NNW, NNE;

    *pRun = 0;

    LOAD_NEIGHBOURS(x);

    if (dc->next_run_pos == x) {
        /* can't reuse W for the new pixel in this case */
        WWneW = 1;
    } else {
        idx = (WW  != W)  << 7 |
              (NW  != W)  << 6 |
              (N   != NE) << 5 |
              (NW  != N)  << 4 |
              (NWW != NW) << 3 |
              (NNE != NE) << 2 |
              (NN  != N)  << 1 |
              (NNW != NW);
        WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
634 635
        if (WWneW < 0)
            return WWneW;
Kostya Shishkov's avatar
Kostya Shishkov committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
    }

    if (WWneW)
        dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = W;
    else {
        *pPix     = W;
        got_pixel = 1;
    }

    do {
        int NWneW = 1;
        if (got_pixel) // pixel value already known (derived from either W or N)
            NWneW = *pPix != N;
        else { // pixel value is unknown and will be decoded later
            NWneW = *pRun ? NWneW : NW != W;

            /* TODO: RFC this mess! */
            switch (((NW != N) << 2) | (NWneW << 1) | WWneW) {
            case 0:
                break; // do nothing here
            case 3:
            case 5:
            case 6:
            case 7:
                if (!is_pixel_on_stack(dc, N)) {
                    idx = WWneW       << 8 |
                          (*pRun ? old_WWneW : WW != W) << 7 |
                          NWneW       << 6 |
                          (N   != NE) << 5 |
                          (NW  != N)  << 4 |
                          (NWW != NW) << 3 |
                          (NNE != NE) << 2 |
                          (NN  != N)  << 1 |
                          (NNW != NW);
                    if (!ff_els_decode_bit(&dc->els_ctx, &dc->N_ctx_rung[idx])) {
                        NWneW = 0;
                        *pPix = N;
                        got_pixel = 1;
                        break;
                    }
                }
                /* fall through */
            default:
                NWneW = 1;
                old_WWneW = WWneW;
                if (!is_pixel_on_stack(dc, N))
                    dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = N;
            }
        }

        (*pRun)++;
        if (x + *pRun >= tile_width - 1)
            break;

        UPDATE_NEIGHBOURS(x + *pRun);

        if (!NWneW && NW == N && N == NE) {
            int pos, run, rle;
            int start_pos = x + *pRun;

            /* scan for a run of pix in the line above */
            uint32_t pix = above_row[start_pos + 1];
            for (pos = start_pos + 2; pos < tile_width; pos++)
                if (!(above_row[pos] == pix))
                    break;
            run = pos - start_pos - 1;
702
            idx = av_ceil_log2(run);
Kostya Shishkov's avatar
Kostya Shishkov committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
            if (ff_els_decode_bit(&dc->els_ctx, &dc->prev_row_rung[idx]))
                *pRun += run;
            else {
                int flag;
                /* run-length is coded as plain binary number of idx - 1 bits */
                for (pos = idx - 1, rle = 0, flag = 0; pos >= 0; pos--) {
                    if ((1 << pos) + rle < run &&
                        ff_els_decode_bit(&dc->els_ctx,
                                          flag ? &dc->runlen_one
                                               : &dc->runlen_zeroes[pos])) {
                        flag = 1;
                        rle |= 1 << pos;
                    }
                }
                *pRun += rle;
                break; // return immediately
            }
            if (x + *pRun >= tile_width - 1)
                break;

            LOAD_NEIGHBOURS(x + *pRun);
            WWneW = 0;
            NWneW = 0;
        }

        idx = WWneW       << 7 |
              NWneW       << 6 |
              (N   != NE) << 5 |
              (NW  != N)  << 4 |
              (NWW != NW) << 3 |
              (NNE != NE) << 2 |
              (NN  != N)  << 1 |
              (NNW != NW);
        WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
    } while (!WWneW);

    dc->next_run_pos = x + *pRun;
    return got_pixel;
}

static int epic_predict_pixel2(ePICContext *dc, uint8_t *rung,
                               uint32_t *pPix, uint32_t pix)
{
    if (ff_els_decode_bit(&dc->els_ctx, rung)) {
        *pPix = pix;
        return 1;
    }
    dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
    return 0;
}

static int epic_predict_from_NW_NE(ePICContext *dc, int x, int y, int run,
                                   int tile_width, const uint32_t *curr_row,
                                   const uint32_t *above_row, uint32_t *pPix)
{
    int pos;

    /* try to reuse the NW pixel first */
    if (x && y) {
        uint32_t NW = above_row[x - 1];
        if (NW != curr_row[x - 1] && NW != above_row[x] && !is_pixel_on_stack(dc, NW)) {
            if (epic_predict_pixel2(dc, &dc->nw_pred_rung[NW & 0xFF], pPix, NW))
                return 1;
        }
    }

    /* try to reuse the NE[x + run, y] pixel */
    pos = x + run - 1;
    if (pos < tile_width - 1 && y) {
        uint32_t NE = above_row[pos + 1];
        if (NE != above_row[pos] && !is_pixel_on_stack(dc, NE)) {
            if (epic_predict_pixel2(dc, &dc->ne_pred_rung[NE & 0xFF], pPix, NE))
                return 1;
        }
    }

    return 0;
}

static int epic_decode_from_cache(ePICContext *dc, uint32_t W, uint32_t *pPix)
{
    ePICPixListElem *list, *prev = NULL;
    ePICPixHashElem *hash_elem = epic_hash_find(&dc->hash, W);

    if (!hash_elem || !hash_elem->list)
        return 0;

    list = hash_elem->list;
    while (list) {
        if (!is_pixel_on_stack(dc, list->pixel)) {
            if (ff_els_decode_bit(&dc->els_ctx, &list->rung)) {
                *pPix = list->pixel;
                if (list != hash_elem->list) {
                    prev->next      = list->next;
                    list->next      = hash_elem->list;
                    hash_elem->list = list;
                }
                return 1;
            }
            dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = list->pixel;
        }
        prev = list;
        list = list->next;
    }

    return 0;
}

static int epic_decode_tile(ePICContext *dc, uint8_t *out, int tile_height,
                            int tile_width, int stride)
{
    int x, y;
    uint32_t pix;
    uint32_t *curr_row = NULL, *above_row = NULL, *above2_row;

    for (y = 0; y < tile_height; y++, out += stride) {
        above2_row = above_row;
        above_row  = curr_row;
        curr_row   = (uint32_t *) out;

        for (x = 0, dc->next_run_pos = 0; x < tile_width;) {
            if (dc->els_ctx.err)
                return AVERROR_INVALIDDATA; // bail out in the case of ELS overflow

            pix = curr_row[x - 1]; // get W pixel

            if (y >= 1 && x >= 2 &&
                pix != curr_row[x - 2]  && pix != above_row[x - 1] &&
                pix != above_row[x - 2] && pix != above_row[x] &&
                !epic_cache_entries_for_pixel(&dc->hash, pix)) {
                curr_row[x] = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
                x++;
            } else {
                int got_pixel, run;
                dc->stack_pos = 0; // empty stack

                if (y < 2 || x < 2 || x == tile_width - 1) {
                    run       = 1;
                    got_pixel = epic_handle_edges(dc, x, y, curr_row, above_row, &pix);
842
                } else {
Kostya Shishkov's avatar
Kostya Shishkov committed
843 844 845
                    got_pixel = epic_decode_run_length(dc, x, y, tile_width,
                                                       curr_row, above_row,
                                                       above2_row, &pix, &run);
846 847 848
                    if (got_pixel < 0)
                        return got_pixel;
                }
Kostya Shishkov's avatar
Kostya Shishkov committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902

                if (!got_pixel && !epic_predict_from_NW_NE(dc, x, y, run,
                                                           tile_width, curr_row,
                                                           above_row, &pix)) {
                    uint32_t ref_pix = curr_row[x - 1];
                    if (!x || !epic_decode_from_cache(dc, ref_pix, &pix)) {
                        pix = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
                        if (x) {
                            int ret = epic_add_pixel_to_cache(&dc->hash,
                                                              ref_pix,
                                                              pix);
                            if (ret)
                                return ret;
                        }
                    }
                }
                for (; run > 0; x++, run--)
                    curr_row[x] = pix;
            }
        }
    }

    return 0;
}

static int epic_jb_decode_tile(G2MContext *c, int tile_x, int tile_y,
                               const uint8_t *src, size_t src_size,
                               AVCodecContext *avctx)
{
    uint8_t prefix, mask = 0x80;
    int extrabytes, tile_width, tile_height, awidth, aheight;
    size_t els_dsize;
    uint8_t *dst;

    if (!src_size)
        return 0;

    /* get data size of the ELS partition as unsigned variable-length integer */
    prefix = *src++;
    src_size--;
    for (extrabytes = 0; (prefix & mask) && (extrabytes < 7); extrabytes++)
        mask >>= 1;
    if (extrabytes > 3 || src_size < extrabytes) {
        av_log(avctx, AV_LOG_ERROR, "ePIC: invalid data size VLI\n");
        return AVERROR_INVALIDDATA;
    }

    els_dsize = prefix & ((0x80 >> extrabytes) - 1); // mask out the length prefix
    while (extrabytes-- > 0) {
        els_dsize = (els_dsize << 8) | *src++;
        src_size--;
    }

    if (src_size < els_dsize) {
903
        av_log(avctx, AV_LOG_ERROR, "ePIC: data too short, needed %"SIZE_SPECIFIER", got %"SIZE_SPECIFIER"\n",
Kostya Shishkov's avatar
Kostya Shishkov committed
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
               els_dsize, src_size);
        return AVERROR_INVALIDDATA;
    }

    tile_width  = FFMIN(c->width  - tile_x * c->tile_width,  c->tile_width);
    tile_height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
    awidth      = FFALIGN(tile_width,  16);
    aheight     = FFALIGN(tile_height, 16);

    if (els_dsize) {
        int ret, i, j, k;
        uint8_t tr_r, tr_g, tr_b, *buf;
        uint32_t *in;
        /* ELS decoder initializations */
        memset(&c->ec, 0, sizeof(c->ec));
        ff_els_decoder_init(&c->ec.els_ctx, src, els_dsize);
        epic_hash_init(&c->ec.hash);

        /* decode transparent pixel value */
        tr_r = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
        tr_g = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
        tr_b = ff_els_decode_unsigned(&c->ec.els_ctx, &c->ec.unsigned_rung);
        if (c->ec.els_ctx.err != 0) {
            av_log(avctx, AV_LOG_ERROR,
                   "ePIC: couldn't decode transparency pixel!\n");
            return AVERROR_INVALIDDATA;
        }

        ret = epic_decode_tile(&c->ec, c->epic_buf, tile_height, tile_width,
                               c->epic_buf_stride);

        epic_free_pixel_cache(&c->ec.hash);
        ff_els_decoder_uninit(&c->ec.unsigned_rung);

        if (ret) {
            av_log(avctx, AV_LOG_ERROR,
                   "ePIC: tile decoding failed, frame=%d, tile_x=%d, tile_y=%d\n",
                   avctx->frame_number, tile_x, tile_y);
            return AVERROR_INVALIDDATA;
        }

        buf = c->epic_buf;
        dst = c->framebuf + tile_x * c->tile_width * 3 +
              tile_y * c->tile_height * c->framebuf_stride;

        for (j = 0; j < tile_height; j++) {
            uint8_t *out = dst;
            in  = (uint32_t *) buf;
            for (i = 0; i < tile_width; i++) {
                out[0] = (in[i] >> R_shift) & 0xFF;
                out[1] = (in[i] >> G_shift) & 0xFF;
                out[2] = (in[i] >> B_shift) & 0xFF;
                out   += 3;
            }
            buf += c->epic_buf_stride;
            dst += c->framebuf_stride;
        }

        if (src_size > els_dsize) {
            uint8_t *jpg;
            uint32_t tr;
            int bstride = FFALIGN(tile_width, 16) >> 3;
            int nblocks = 0;
            int estride = c->epic_buf_stride >> 2;

            src      += els_dsize;
            src_size -= els_dsize;

            in = (uint32_t *) c->epic_buf;
            tr = (tr_r << R_shift) | (tr_g << G_shift) | (tr_b << B_shift);

            memset(c->kempf_flags, 0,
                   (aheight >> 3) * bstride * sizeof(*c->kempf_flags));
            for (j = 0; j < tile_height; j += 8) {
                for (i = 0; i < tile_width; i += 8) {
                    c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 0;
                    for (k = 0; k < 8 * 8; k++) {
                        if (in[i + (k & 7) + (k >> 3) * estride] == tr) {
                            c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 1;
                            nblocks++;
                            break;
                        }
                    }
                }
                in += 8 * estride;
            }

            memset(c->jpeg_tile, 0, c->tile_stride * aheight);
            jpg_decode_data(&c->jc, awidth, aheight, src, src_size,
                            c->jpeg_tile, c->tile_stride,
                            c->kempf_flags, bstride, nblocks, c->swapuv);

            in  = (uint32_t *) c->epic_buf;
            dst = c->framebuf + tile_x * c->tile_width * 3 +
                  tile_y * c->tile_height * c->framebuf_stride;
            jpg = c->jpeg_tile;
            for (j = 0; j < tile_height; j++) {
                for (i = 0; i < tile_width; i++)
                    if (in[i] == tr)
                        memcpy(dst + i * 3, jpg + i * 3, 3);
                in  += c->epic_buf_stride >> 2;
                dst += c->framebuf_stride;
                jpg += c->tile_stride;
            }
        }
    } else {
        dst = c->framebuf + tile_x * c->tile_width * 3 +
              tile_y * c->tile_height * c->framebuf_stride;
        return jpg_decode_data(&c->jc, tile_width, tile_height, src, src_size,
                               dst, c->framebuf_stride, NULL, 0, 0, c->swapuv);
    }

    return 0;
}

1019
static int kempf_restore_buf(const uint8_t *src, int len,
Kostya Shishkov's avatar
Kostya Shishkov committed
1020 1021 1022 1023 1024 1025 1026
                              uint8_t *dst, int stride,
                              const uint8_t *jpeg_tile, int tile_stride,
                              int width, int height,
                              const uint8_t *pal, int npal, int tidx)
{
    GetBitContext gb;
    int i, j, nb, col;
1027
    int ret;
Kostya Shishkov's avatar
Kostya Shishkov committed
1028
    int align_width = FFALIGN(width, 16);
Kostya Shishkov's avatar
Kostya Shishkov committed
1029

1030 1031
    if ((ret = init_get_bits8(&gb, src, len)) < 0)
        return ret;
Kostya Shishkov's avatar
Kostya Shishkov committed
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

    if (npal <= 2)       nb = 1;
    else if (npal <= 4)  nb = 2;
    else if (npal <= 16) nb = 4;
    else                 nb = 8;

    for (j = 0; j < height; j++, dst += stride, jpeg_tile += tile_stride) {
        if (get_bits(&gb, 8))
            continue;
        for (i = 0; i < width; i++) {
            col = get_bits(&gb, nb);
            if (col != tidx)
                memcpy(dst + i * 3, pal + col * 3, 3);
            else
                memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
        }
Kostya Shishkov's avatar
Kostya Shishkov committed
1048
        skip_bits_long(&gb, nb * (align_width - width));
Kostya Shishkov's avatar
Kostya Shishkov committed
1049
    }
1050 1051

    return 0;
Kostya Shishkov's avatar
Kostya Shishkov committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
}

static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y,
                             const uint8_t *src, int src_size)
{
    int width, height;
    int hdr, zsize, npal, tidx = -1, ret;
    int i, j;
    const uint8_t *src_end = src + src_size;
    uint8_t pal[768], transp[3];
    uLongf dlen = (c->tile_width + 1) * c->tile_height;
    int sub_type;
    int nblocks, cblocks, bstride;
    int bits, bitbuf, coded;
    uint8_t *dst = c->framebuf + tile_x * c->tile_width * 3 +
                   tile_y * c->tile_height * c->framebuf_stride;

    if (src_size < 2)
        return AVERROR_INVALIDDATA;

    width  = FFMIN(c->width  - tile_x * c->tile_width,  c->tile_width);
    height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);

1075
    hdr      = *src++;
Kostya Shishkov's avatar
Kostya Shishkov committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
    sub_type = hdr >> 5;
    if (sub_type == 0) {
        int j;
        memcpy(transp, src, 3);
        src += 3;
        for (j = 0; j < height; j++, dst += c->framebuf_stride)
            for (i = 0; i < width; i++)
                memcpy(dst + i * 3, transp, 3);
        return 0;
    } else if (sub_type == 1) {
        return jpg_decode_data(&c->jc, width, height, src, src_end - src,
                               dst, c->framebuf_stride, NULL, 0, 0, 0);
    }

    if (sub_type != 2) {
        memcpy(transp, src, 3);
        src += 3;
    }
    npal = *src++ + 1;
1095 1096
    if (src_end - src < npal * 3)
        return AVERROR_INVALIDDATA;
1097 1098
    memcpy(pal, src, npal * 3);
    src += npal * 3;
Kostya Shishkov's avatar
Kostya Shishkov committed
1099 1100 1101
    if (sub_type != 2) {
        for (i = 0; i < npal; i++) {
            if (!memcmp(pal + i * 3, transp, 3)) {
1102 1103
                tidx = i;
                break;
Kostya Shishkov's avatar
Kostya Shishkov committed
1104 1105 1106 1107 1108 1109
            }
        }
    }

    if (src_end - src < 2)
        return 0;
1110 1111
    zsize = (src[0] << 8) | src[1];
    src  += 2;
Kostya Shishkov's avatar
Kostya Shishkov committed
1112

1113
    if (src_end - src < zsize + (sub_type != 2))
Kostya Shishkov's avatar
Kostya Shishkov committed
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
        return AVERROR_INVALIDDATA;

    ret = uncompress(c->kempf_buf, &dlen, src, zsize);
    if (ret)
        return AVERROR_INVALIDDATA;
    src += zsize;

    if (sub_type == 2) {
        kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
                          NULL, 0, width, height, pal, npal, tidx);
        return 0;
    }

    nblocks = *src++ + 1;
    cblocks = 0;
1129
    bstride = FFALIGN(width, 16) >> 3;
Kostya Shishkov's avatar
Kostya Shishkov committed
1130 1131 1132 1133 1134
    // blocks are coded LSB and we need normal bitreader for JPEG data
    bits = 0;
    for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
        for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
            if (!bits) {
1135 1136
                if (src >= src_end)
                    return AVERROR_INVALIDDATA;
Kostya Shishkov's avatar
Kostya Shishkov committed
1137 1138 1139 1140 1141 1142 1143 1144 1145
                bitbuf = *src++;
                bits   = 8;
            }
            coded = bitbuf & 1;
            bits--;
            bitbuf >>= 1;
            cblocks += coded;
            if (cblocks > nblocks)
                return AVERROR_INVALIDDATA;
1146 1147 1148 1149
            c->kempf_flags[j * 2 +      i * 2      * bstride] =
            c->kempf_flags[j * 2 + 1 +  i * 2      * bstride] =
            c->kempf_flags[j * 2 +     (i * 2 + 1) * bstride] =
            c->kempf_flags[j * 2 + 1 + (i * 2 + 1) * bstride] = coded;
Kostya Shishkov's avatar
Kostya Shishkov committed
1150 1151 1152 1153 1154 1155
        }
    }

    memset(c->jpeg_tile, 0, c->tile_stride * height);
    jpg_decode_data(&c->jc, width, height, src, src_end - src,
                    c->jpeg_tile, c->tile_stride,
1156
                    c->kempf_flags, bstride, nblocks * 4, 0);
Kostya Shishkov's avatar
Kostya Shishkov committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168

    kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
                      c->jpeg_tile, c->tile_stride,
                      width, height, pal, npal, tidx);

    return 0;
}

static int g2m_init_buffers(G2MContext *c)
{
    int aligned_height;

1169
    if (!c->framebuf || c->old_width < c->width || c->old_height < c->height) {
1170 1171
        c->framebuf_stride = FFALIGN(c->width + 15, 16) * 3;
        aligned_height     = c->height + 15;
Kostya Shishkov's avatar
Kostya Shishkov committed
1172
        av_free(c->framebuf);
1173
        c->framebuf = av_mallocz_array(c->framebuf_stride, aligned_height);
Kostya Shishkov's avatar
Kostya Shishkov committed
1174 1175 1176 1177
        if (!c->framebuf)
            return AVERROR(ENOMEM);
    }
    if (!c->synth_tile || !c->jpeg_tile ||
Kostya Shishkov's avatar
Kostya Shishkov committed
1178
        (c->compression == 2 && !c->epic_buf_base) ||
Kostya Shishkov's avatar
Kostya Shishkov committed
1179 1180
        c->old_tile_w < c->tile_width ||
        c->old_tile_h < c->tile_height) {
1181
        c->tile_stride     = FFALIGN(c->tile_width, 16) * 3;
Kostya Shishkov's avatar
Kostya Shishkov committed
1182 1183
        c->epic_buf_stride = FFALIGN(c->tile_width * 4, 16);
        aligned_height     = FFALIGN(c->tile_height,    16);
1184 1185 1186 1187 1188 1189
        av_freep(&c->synth_tile);
        av_freep(&c->jpeg_tile);
        av_freep(&c->kempf_buf);
        av_freep(&c->kempf_flags);
        av_freep(&c->epic_buf_base);
        c->epic_buf    = NULL;
Kostya Shishkov's avatar
Kostya Shishkov committed
1190 1191
        c->synth_tile  = av_mallocz(c->tile_stride      * aligned_height);
        c->jpeg_tile   = av_mallocz(c->tile_stride      * aligned_height);
Kostya Shishkov's avatar
Kostya Shishkov committed
1192
        c->kempf_buf   = av_mallocz((c->tile_width + 1) * aligned_height +
1193
                                    AV_INPUT_BUFFER_PADDING_SIZE);
Kostya Shishkov's avatar
Kostya Shishkov committed
1194
        c->kempf_flags = av_mallocz(c->tile_width       * aligned_height);
Kostya Shishkov's avatar
Kostya Shishkov committed
1195 1196 1197
        if (!c->synth_tile || !c->jpeg_tile ||
            !c->kempf_buf || !c->kempf_flags)
            return AVERROR(ENOMEM);
Kostya Shishkov's avatar
Kostya Shishkov committed
1198 1199 1200 1201 1202 1203
        if (c->compression == 2) {
            c->epic_buf_base = av_mallocz(c->epic_buf_stride * aligned_height + 4);
            if (!c->epic_buf_base)
                return AVERROR(ENOMEM);
            c->epic_buf = c->epic_buf_base + 4;
        }
Kostya Shishkov's avatar
Kostya Shishkov committed
1204 1205 1206 1207 1208
    }

    return 0;
}

1209 1210
static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c,
                           GetByteContext *gb)
Kostya Shishkov's avatar
Kostya Shishkov committed
1211 1212 1213 1214
{
    int i, j, k;
    uint8_t *dst;
    uint32_t bits;
1215 1216
    uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
    uint32_t cursor_hot_x, cursor_hot_y;
1217
    int cursor_fmt, err;
Kostya Shishkov's avatar
Kostya Shishkov committed
1218

1219 1220 1221 1222 1223 1224
    cur_size     = bytestream2_get_be32(gb);
    cursor_w     = bytestream2_get_byte(gb);
    cursor_h     = bytestream2_get_byte(gb);
    cursor_hot_x = bytestream2_get_byte(gb);
    cursor_hot_y = bytestream2_get_byte(gb);
    cursor_fmt   = bytestream2_get_byte(gb);
1225

1226
    cursor_stride = FFALIGN(cursor_w, cursor_fmt==1 ? 32 : 1) * 4;
1227 1228 1229

    if (cursor_w < 1 || cursor_w > 256 ||
        cursor_h < 1 || cursor_h > 256) {
1230
        av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %"PRIu32"x%"PRIu32"\n",
1231 1232 1233 1234
               cursor_w, cursor_h);
        return AVERROR_INVALIDDATA;
    }
    if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
1235
        av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %"PRIu32",%"PRIu32"\n",
1236 1237 1238 1239 1240 1241
               cursor_hot_x, cursor_hot_y);
        cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);
        cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);
    }
    if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||
        c->cursor_w * c->cursor_h / 4 > cur_size) {
1242
        av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"/%u\n",
1243 1244 1245 1246 1247 1248 1249 1250 1251
               cur_size, bytestream2_get_bytes_left(gb));
        return AVERROR_INVALIDDATA;
    }
    if (cursor_fmt != 1 && cursor_fmt != 32) {
        avpriv_report_missing_feature(avctx, "Cursor format %d",
                                      cursor_fmt);
        return AVERROR_PATCHWELCOME;
    }

1252
    if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
1253
        av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
1254
        return err;
1255 1256 1257 1258 1259 1260 1261 1262
    }

    c->cursor_w      = cursor_w;
    c->cursor_h      = cursor_h;
    c->cursor_hot_x  = cursor_hot_x;
    c->cursor_hot_y  = cursor_hot_y;
    c->cursor_fmt    = cursor_fmt;
    c->cursor_stride = cursor_stride;
Kostya Shishkov's avatar
Kostya Shishkov committed
1263 1264 1265 1266 1267 1268 1269 1270 1271

    dst = c->cursor;
    switch (c->cursor_fmt) {
    case 1: // old monochrome
        for (j = 0; j < c->cursor_h; j++) {
            for (i = 0; i < c->cursor_w; i += 32) {
                bits = bytestream2_get_be32(gb);
                for (k = 0; k < 32; k++) {
                    dst[0] = !!(bits & 0x80000000);
1272
                    dst   += 4;
Kostya Shishkov's avatar
Kostya Shishkov committed
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
                    bits <<= 1;
                }
            }
        }

        dst = c->cursor;
        for (j = 0; j < c->cursor_h; j++) {
            for (i = 0; i < c->cursor_w; i += 32) {
                bits = bytestream2_get_be32(gb);
                for (k = 0; k < 32; k++) {
                    int mask_bit = !!(bits & 0x80000000);
                    switch (dst[0] * 2 + mask_bit) {
                    case 0:
1286 1287 1288 1289
                        dst[0] = 0xFF;
                        dst[1] = 0x00;
                        dst[2] = 0x00;
                        dst[3] = 0x00;
Kostya Shishkov's avatar
Kostya Shishkov committed
1290 1291
                        break;
                    case 1:
1292 1293 1294 1295
                        dst[0] = 0xFF;
                        dst[1] = 0xFF;
                        dst[2] = 0xFF;
                        dst[3] = 0xFF;
Kostya Shishkov's avatar
Kostya Shishkov committed
1296 1297
                        break;
                    default:
1298 1299 1300 1301
                        dst[0] = 0x00;
                        dst[1] = 0x00;
                        dst[2] = 0x00;
                        dst[3] = 0x00;
Kostya Shishkov's avatar
Kostya Shishkov committed
1302
                    }
1303
                    dst   += 4;
Kostya Shishkov's avatar
Kostya Shishkov committed
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
                    bits <<= 1;
                }
            }
        }
        break;
    case 32: // full colour
        /* skip monochrome version of the cursor and decode RGBA instead */
        bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
        for (j = 0; j < c->cursor_h; j++) {
            for (i = 0; i < c->cursor_w; i++) {
                int val = bytestream2_get_be32(gb);
                *dst++ = val >>  0;
                *dst++ = val >>  8;
                *dst++ = val >> 16;
                *dst++ = val >> 24;
            }
        }
        break;
    default:
        return AVERROR_PATCHWELCOME;
    }
    return 0;
}

#define APPLY_ALPHA(src, new, alpha) \
    src = (src * (256 - alpha) + new * alpha) >> 8

static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
{
    int i, j;
    int x, y, w, h;
    const uint8_t *cursor;

    if (!c->cursor)
        return;

    x = c->cursor_x - c->cursor_hot_x;
    y = c->cursor_y - c->cursor_hot_y;

    cursor = c->cursor;
    w      = c->cursor_w;
    h      = c->cursor_h;

    if (x + w > c->width)
        w = c->width - x;
    if (y + h > c->height)
        h = c->height - y;
    if (x < 0) {
        w      +=  x;
        cursor += -x * 4;
    } else {
        dst    +=  x * 3;
    }
    if (y < 0) {
        h      +=  y;
        cursor += -y * c->cursor_stride;
    } else {
        dst    +=  y * stride;
    }
    if (w < 0 || h < 0)
        return;

    for (j = 0; j < h; j++) {
        for (i = 0; i < w; i++) {
            uint8_t alpha = cursor[i * 4];
            APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
            APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
            APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
        }
        dst    += stride;
        cursor += c->cursor_stride;
    }
}

static int g2m_decode_frame(AVCodecContext *avctx, void *data,
                            int *got_picture_ptr, AVPacket *avpkt)
{
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
    G2MContext *c = avctx->priv_data;
    AVFrame *pic = data;
    GetByteContext bc, tbc;
    int magic;
    int got_header = 0;
1388
    uint32_t chunk_size, r_mask, g_mask, b_mask;
1389
    int chunk_type, chunk_start;
Kostya Shishkov's avatar
Kostya Shishkov committed
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
    int i;
    int ret;

    if (buf_size < 12) {
        av_log(avctx, AV_LOG_ERROR,
               "Frame should have at least 12 bytes, got %d instead\n",
               buf_size);
        return AVERROR_INVALIDDATA;
    }

    bytestream2_init(&bc, buf, buf_size);

    magic = bytestream2_get_be32(&bc);
    if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
1404
        (magic & 0xF) < 2 || (magic & 0xF) > 5) {
Kostya Shishkov's avatar
Kostya Shishkov committed
1405 1406 1407 1408
        av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
        return AVERROR_INVALIDDATA;
    }

Kostya Shishkov's avatar
Kostya Shishkov committed
1409
    c->swapuv = magic == MKBETAG('G', '2', 'M', '2');
Kostya Shishkov's avatar
Kostya Shishkov committed
1410 1411

    while (bytestream2_get_bytes_left(&bc) > 5) {
1412 1413 1414
        chunk_size  = bytestream2_get_le32(&bc) - 1;
        chunk_type  = bytestream2_get_byte(&bc);
        chunk_start = bytestream2_tell(&bc);
Kostya Shishkov's avatar
Kostya Shishkov committed
1415
        if (chunk_size > bytestream2_get_bytes_left(&bc)) {
1416
            av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %"PRIu32" type %02X\n",
Kostya Shishkov's avatar
Kostya Shishkov committed
1417 1418 1419 1420
                   chunk_size, chunk_type);
            break;
        }
        switch (chunk_type) {
1421
        case DISPLAY_INFO:
1422
            got_header =
Kostya Shishkov's avatar
Kostya Shishkov committed
1423 1424
            c->got_header = 0;
            if (chunk_size < 21) {
1425
                av_log(avctx, AV_LOG_ERROR, "Invalid display info size %"PRIu32"\n",
Kostya Shishkov's avatar
Kostya Shishkov committed
1426 1427 1428 1429 1430
                       chunk_size);
                break;
            }
            c->width  = bytestream2_get_be32(&bc);
            c->height = bytestream2_get_be32(&bc);
1431
            if (c->width < 16 || c->height < 16) {
Kostya Shishkov's avatar
Kostya Shishkov committed
1432 1433 1434
                av_log(avctx, AV_LOG_ERROR,
                       "Invalid frame dimensions %dx%d\n",
                       c->width, c->height);
1435 1436
                ret = AVERROR_INVALIDDATA;
                goto header_fail;
Kostya Shishkov's avatar
Kostya Shishkov committed
1437
            }
1438 1439 1440 1441 1442
            if (c->width != avctx->width || c->height != avctx->height) {
                ret = ff_set_dimensions(avctx, c->width, c->height);
                if (ret < 0)
                    goto header_fail;
            }
Kostya Shishkov's avatar
Kostya Shishkov committed
1443 1444 1445 1446 1447
            c->compression = bytestream2_get_be32(&bc);
            if (c->compression != 2 && c->compression != 3) {
                av_log(avctx, AV_LOG_ERROR,
                       "Unknown compression method %d\n",
                       c->compression);
1448 1449
                ret = AVERROR_PATCHWELCOME;
                goto header_fail;
Kostya Shishkov's avatar
Kostya Shishkov committed
1450 1451 1452
            }
            c->tile_width  = bytestream2_get_be32(&bc);
            c->tile_height = bytestream2_get_be32(&bc);
1453 1454
            if (c->tile_width <= 0 || c->tile_height <= 0 ||
                ((c->tile_width | c->tile_height) & 0xF) ||
1455
                c->tile_width * (uint64_t)c->tile_height >= INT_MAX / 4
1456
            ) {
Kostya Shishkov's avatar
Kostya Shishkov committed
1457 1458 1459
                av_log(avctx, AV_LOG_ERROR,
                       "Invalid tile dimensions %dx%d\n",
                       c->tile_width, c->tile_height);
1460 1461
                ret = AVERROR_INVALIDDATA;
                goto header_fail;
Kostya Shishkov's avatar
Kostya Shishkov committed
1462 1463 1464
            }
            c->tiles_x = (c->width  + c->tile_width  - 1) / c->tile_width;
            c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
1465
            c->bpp     = bytestream2_get_byte(&bc);
1466
            if (c->bpp == 32) {
1467
                if (bytestream2_get_bytes_left(&bc) < 16 ||
1468
                    (chunk_size - 21) < 16) {
1469 1470
                    av_log(avctx, AV_LOG_ERROR,
                           "Display info: missing bitmasks!\n");
1471 1472
                    ret = AVERROR_INVALIDDATA;
                    goto header_fail;
1473 1474 1475 1476 1477 1478
                }
                r_mask = bytestream2_get_be32(&bc);
                g_mask = bytestream2_get_be32(&bc);
                b_mask = bytestream2_get_be32(&bc);
                if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
                    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 1482
                    ret = AVERROR_PATCHWELCOME;
                    goto header_fail;
1483 1484
                }
            } else {
1485
                avpriv_request_sample(avctx, "bpp=%d", c->bpp);
1486 1487
                ret = AVERROR_PATCHWELCOME;
                goto header_fail;
1488
            }
1489 1490 1491 1492
            if (g2m_init_buffers(c)) {
                ret = AVERROR(ENOMEM);
                goto header_fail;
            }
Kostya Shishkov's avatar
Kostya Shishkov committed
1493 1494 1495 1496 1497
            got_header = 1;
            break;
        case TILE_DATA:
            if (!c->tiles_x || !c->tiles_y) {
                av_log(avctx, AV_LOG_WARNING,
1498
                       "No display info - skipping tile\n");
Kostya Shishkov's avatar
Kostya Shishkov committed
1499 1500 1501
                break;
            }
            if (chunk_size < 2) {
1502
                av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %"PRIu32"\n",
Kostya Shishkov's avatar
Kostya Shishkov committed
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
                       chunk_size);
                break;
            }
            c->tile_x = bytestream2_get_byte(&bc);
            c->tile_y = bytestream2_get_byte(&bc);
            if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
                av_log(avctx, AV_LOG_ERROR,
                       "Invalid tile pos %d,%d (in %dx%d grid)\n",
                       c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
                break;
            }
            ret = 0;
            switch (c->compression) {
            case COMPR_EPIC_J_B:
Kostya Shishkov's avatar
Kostya Shishkov committed
1517 1518 1519 1520
                ret = epic_jb_decode_tile(c, c->tile_x, c->tile_y,
                                          buf + bytestream2_tell(&bc),
                                          chunk_size - 2, avctx);
                break;
Kostya Shishkov's avatar
Kostya Shishkov committed
1521 1522 1523
            case COMPR_KEMPF_J_B:
                ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
                                        buf + bytestream2_tell(&bc),
1524
                                        chunk_size - 2);
Kostya Shishkov's avatar
Kostya Shishkov committed
1525 1526 1527 1528 1529 1530 1531 1532
                break;
            }
            if (ret && c->framebuf)
                av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
                       c->tile_x, c->tile_y);
            break;
        case CURSOR_POS:
            if (chunk_size < 5) {
1533
                av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %"PRIu32"\n",
Kostya Shishkov's avatar
Kostya Shishkov committed
1534 1535 1536 1537 1538 1539 1540 1541
                       chunk_size);
                break;
            }
            c->cursor_x = bytestream2_get_be16(&bc);
            c->cursor_y = bytestream2_get_be16(&bc);
            break;
        case CURSOR_SHAPE:
            if (chunk_size < 8) {
1542
                av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"\n",
Kostya Shishkov's avatar
Kostya Shishkov committed
1543 1544 1545 1546 1547
                       chunk_size);
                break;
            }
            bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
                             chunk_size - 4);
1548
            g2m_load_cursor(avctx, c, &tbc);
Kostya Shishkov's avatar
Kostya Shishkov committed
1549 1550 1551 1552 1553
            break;
        case CHUNK_CC:
        case CHUNK_CD:
            break;
        default:
1554
            av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02d\n",
Kostya Shishkov's avatar
Kostya Shishkov committed
1555 1556
                   chunk_type);
        }
1557 1558 1559

        /* navigate to next chunk */
        bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
Kostya Shishkov's avatar
Kostya Shishkov committed
1560 1561 1562 1563
    }
    if (got_header)
        c->got_header = 1;

1564
    if (c->width && c->height && c->framebuf) {
1565
        if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
Kostya Shishkov's avatar
Kostya Shishkov committed
1566 1567 1568 1569 1570 1571 1572
            return ret;

        pic->key_frame = got_header;
        pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;

        for (i = 0; i < avctx->height; i++)
            memcpy(pic->data[0] + i * pic->linesize[0],
1573
                   c->framebuf + i * c->framebuf_stride,
Kostya Shishkov's avatar
Kostya Shishkov committed
1574 1575 1576 1577 1578 1579 1580
                   c->width * 3);
        g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);

        *got_picture_ptr = 1;
    }

    return buf_size;
1581

1582
header_fail:
1583 1584 1585 1586
    c->width   =
    c->height  = 0;
    c->tiles_x =
    c->tiles_y = 0;
1587 1588
    c->tile_width =
    c->tile_height = 0;
1589
    return ret;
Kostya Shishkov's avatar
Kostya Shishkov committed
1590 1591 1592 1593
}

static av_cold int g2m_decode_init(AVCodecContext *avctx)
{
1594
    G2MContext *const c = avctx->priv_data;
Kostya Shishkov's avatar
Kostya Shishkov committed
1595 1596 1597 1598 1599 1600 1601 1602
    int ret;

    if ((ret = jpg_init(avctx, &c->jc)) != 0) {
        av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
        jpg_free_context(&c->jc);
        return AVERROR(ENOMEM);
    }

1603
    avctx->pix_fmt = AV_PIX_FMT_RGB24;
Kostya Shishkov's avatar
Kostya Shishkov committed
1604

1605 1606 1607 1608
    // store original sizes and check against those if resize happens
    c->orig_width  = avctx->width;
    c->orig_height = avctx->height;

Kostya Shishkov's avatar
Kostya Shishkov committed
1609 1610 1611 1612 1613
    return 0;
}

static av_cold int g2m_decode_end(AVCodecContext *avctx)
{
1614
    G2MContext *const c = avctx->priv_data;
Kostya Shishkov's avatar
Kostya Shishkov committed
1615 1616 1617

    jpg_free_context(&c->jc);

Kostya Shishkov's avatar
Kostya Shishkov committed
1618
    av_freep(&c->epic_buf_base);
1619
    c->epic_buf = NULL;
Kostya Shishkov's avatar
Kostya Shishkov committed
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
    av_freep(&c->kempf_buf);
    av_freep(&c->kempf_flags);
    av_freep(&c->synth_tile);
    av_freep(&c->jpeg_tile);
    av_freep(&c->cursor);
    av_freep(&c->framebuf);

    return 0;
}

AVCodec ff_g2m_decoder = {
    .name           = "g2m",
    .long_name      = NULL_IF_CONFIG_SMALL("Go2Meeting"),
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = AV_CODEC_ID_G2M,
    .priv_data_size = sizeof(G2MContext),
    .init           = g2m_decode_init,
    .close          = g2m_decode_end,
    .decode         = g2m_decode_frame,
1639
    .capabilities   = AV_CODEC_CAP_DR1,
Kostya Shishkov's avatar
Kostya Shishkov committed
1640
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
Kostya Shishkov's avatar
Kostya Shishkov committed
1641
};