Coverage Report

Created: 2026-01-16 07:48

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