Coverage Report

Created: 2026-05-16 07:49

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