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