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