]> git.sesse.net Git - ffmpeg/blob - libavcodec/g2meet.c
Remove all SH4 architecture optimizations
[ffmpeg] / libavcodec / g2meet.c
1 /*
2  * Go2Webinar decoder
3  * Copyright (c) 2012 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Go2Webinar decoder
25  */
26
27 #include <zlib.h>
28
29 #include "libavutil/intreadwrite.h"
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "dsputil.h"
33 #include "get_bits.h"
34 #include "internal.h"
35 #include "mjpeg.h"
36
37 enum ChunkType {
38     DISPLAY_INFO = 0xC8,
39     TILE_DATA,
40     CURSOR_POS,
41     CURSOR_SHAPE,
42     CHUNK_CC,
43     CHUNK_CD
44 };
45
46 enum Compression {
47     COMPR_EPIC_J_B = 2,
48     COMPR_KEMPF_J_B,
49 };
50
51 static const uint8_t luma_quant[64] = {
52      8,  6,  5,  8, 12, 20, 26, 31,
53      6,  6,  7, 10, 13, 29, 30, 28,
54      7,  7,  8, 12, 20, 29, 35, 28,
55      7,  9, 11, 15, 26, 44, 40, 31,
56      9, 11, 19, 28, 34, 55, 52, 39,
57     12, 18, 28, 32, 41, 52, 57, 46,
58     25, 32, 39, 44, 52, 61, 60, 51,
59     36, 46, 48, 49, 56, 50, 52, 50
60 };
61
62 static const uint8_t chroma_quant[64] = {
63      9,  9, 12, 24, 50, 50, 50, 50,
64      9, 11, 13, 33, 50, 50, 50, 50,
65     12, 13, 28, 50, 50, 50, 50, 50,
66     24, 33, 50, 50, 50, 50, 50, 50,
67     50, 50, 50, 50, 50, 50, 50, 50,
68     50, 50, 50, 50, 50, 50, 50, 50,
69     50, 50, 50, 50, 50, 50, 50, 50,
70     50, 50, 50, 50, 50, 50, 50, 50,
71 };
72
73 typedef struct JPGContext {
74     DSPContext dsp;
75     ScanTable  scantable;
76
77     VLC        dc_vlc[2], ac_vlc[2];
78     int        prev_dc[3];
79     DECLARE_ALIGNED(16, int16_t, block)[6][64];
80
81     uint8_t    *buf;
82 } JPGContext;
83
84 typedef struct G2MContext {
85     JPGContext jc;
86     int        version;
87
88     int        compression;
89     int        width, height, bpp;
90     int        tile_width, tile_height;
91     int        tiles_x, tiles_y, tile_x, tile_y;
92
93     int        got_header;
94
95     uint8_t    *framebuf;
96     int        framebuf_stride, old_width, old_height;
97
98     uint8_t    *synth_tile, *jpeg_tile;
99     int        tile_stride, old_tile_w, old_tile_h;
100
101     uint8_t    *kempf_buf, *kempf_flags;
102
103     uint8_t    *cursor;
104     int        cursor_stride;
105     int        cursor_fmt;
106     int        cursor_w, cursor_h, cursor_x, cursor_y;
107     int        cursor_hot_x, cursor_hot_y;
108 } G2MContext;
109
110 static av_cold int build_vlc(VLC *vlc, const uint8_t *bits_table,
111                              const uint8_t *val_table, int nb_codes,
112                              int is_ac)
113 {
114     uint8_t  huff_size[256] = { 0 };
115     uint16_t huff_code[256];
116     uint16_t huff_sym[256];
117     int i;
118
119     ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
120
121     for (i = 0; i < 256; i++)
122         huff_sym[i] = i + 16 * is_ac;
123
124     if (is_ac)
125         huff_sym[0] = 16 * 256;
126
127     return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
128                               huff_code, 2, 2, huff_sym, 2, 2, 0);
129 }
130
131 static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c)
132 {
133     int ret;
134
135     ret = build_vlc(&c->dc_vlc[0], avpriv_mjpeg_bits_dc_luminance,
136                     avpriv_mjpeg_val_dc, 12, 0);
137     if (ret)
138         return ret;
139     ret = build_vlc(&c->dc_vlc[1], avpriv_mjpeg_bits_dc_chrominance,
140                     avpriv_mjpeg_val_dc, 12, 0);
141     if (ret)
142         return ret;
143     ret = build_vlc(&c->ac_vlc[0], avpriv_mjpeg_bits_ac_luminance,
144                     avpriv_mjpeg_val_ac_luminance, 251, 1);
145     if (ret)
146         return ret;
147     ret = build_vlc(&c->ac_vlc[1], avpriv_mjpeg_bits_ac_chrominance,
148                     avpriv_mjpeg_val_ac_chrominance, 251, 1);
149     if (ret)
150         return ret;
151
152     ff_dsputil_init(&c->dsp, avctx);
153     ff_init_scantable(c->dsp.idct_permutation, &c->scantable,
154                       ff_zigzag_direct);
155
156     return 0;
157 }
158
159 static av_cold void jpg_free_context(JPGContext *ctx)
160 {
161     int i;
162
163     for (i = 0; i < 2; i++) {
164         ff_free_vlc(&ctx->dc_vlc[i]);
165         ff_free_vlc(&ctx->ac_vlc[i]);
166     }
167
168     av_freep(&ctx->buf);
169 }
170
171 static void jpg_unescape(const uint8_t *src, int src_size,
172                          uint8_t *dst, int *dst_size)
173 {
174     const uint8_t *src_end = src + src_size;
175     uint8_t *dst_start = dst;
176
177     while (src < src_end) {
178         uint8_t x = *src++;
179
180         *dst++ = x;
181
182         if (x == 0xFF && !*src)
183             src++;
184     }
185     *dst_size = dst - dst_start;
186 }
187
188 static int jpg_decode_block(JPGContext *c, GetBitContext *gb,
189                             int plane, int16_t *block)
190 {
191     int dc, val, pos;
192     const int is_chroma = !!plane;
193     const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant;
194
195     c->dsp.clear_block(block);
196     dc = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 3);
197     if (dc < 0)
198         return AVERROR_INVALIDDATA;
199     if (dc)
200         dc = get_xbits(gb, dc);
201     dc                = dc * qmat[0] + c->prev_dc[plane];
202     block[0]          = dc;
203     c->prev_dc[plane] = dc;
204
205     pos = 0;
206     while (pos < 63) {
207         val = get_vlc2(gb, c->ac_vlc[is_chroma].table, 9, 3);
208         if (val < 0)
209             return AVERROR_INVALIDDATA;
210         pos += val >> 4;
211         val &= 0xF;
212         if (pos > 63)
213             return val ? AVERROR_INVALIDDATA : 0;
214         if (val) {
215             int nbits = val;
216
217             val                                 = get_xbits(gb, nbits);
218             val                                *= qmat[ff_zigzag_direct[pos]];
219             block[c->scantable.permutated[pos]] = val;
220         }
221     }
222     return 0;
223 }
224
225 static inline void yuv2rgb(uint8_t *out, int Y, int U, int V)
226 {
227     out[0] = av_clip_uint8(Y + (             91881 * V + 32768 >> 16));
228     out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
229     out[2] = av_clip_uint8(Y + (116130 * U             + 32768 >> 16));
230 }
231
232 static int jpg_decode_data(JPGContext *c, int width, int height,
233                            const uint8_t *src, int src_size,
234                            uint8_t *dst, int dst_stride,
235                            const uint8_t *mask, int mask_stride, int num_mbs,
236                            int swapuv)
237 {
238     GetBitContext gb;
239     int mb_w, mb_h, mb_x, mb_y, i, j;
240     int bx, by;
241     int unesc_size;
242     int ret;
243
244     if ((ret = av_reallocp(&c->buf,
245                            src_size + FF_INPUT_BUFFER_PADDING_SIZE)) < 0)
246         return ret;
247     jpg_unescape(src, src_size, c->buf, &unesc_size);
248     memset(c->buf + unesc_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
249     init_get_bits(&gb, c->buf, unesc_size * 8);
250
251     width = FFALIGN(width, 16);
252     mb_w  =  width        >> 4;
253     mb_h  = (height + 15) >> 4;
254
255     if (!num_mbs)
256         num_mbs = mb_w * mb_h * 4;
257
258     for (i = 0; i < 3; i++)
259         c->prev_dc[i] = 1024;
260     bx = by = 0;
261     c->dsp.clear_blocks(c->block[0]);
262     for (mb_y = 0; mb_y < mb_h; mb_y++) {
263         for (mb_x = 0; mb_x < mb_w; mb_x++) {
264             if (mask && !mask[mb_x * 2] && !mask[mb_x * 2 + 1] &&
265                 !mask[mb_x * 2 +     mask_stride] &&
266                 !mask[mb_x * 2 + 1 + mask_stride]) {
267                 bx += 16;
268                 continue;
269             }
270             for (j = 0; j < 2; j++) {
271                 for (i = 0; i < 2; i++) {
272                     if (mask && !mask[mb_x * 2 + i + j * mask_stride])
273                         continue;
274                     num_mbs--;
275                     if ((ret = jpg_decode_block(c, &gb, 0,
276                                                 c->block[i + j * 2])) != 0)
277                         return ret;
278                     c->dsp.idct(c->block[i + j * 2]);
279                 }
280             }
281             for (i = 1; i < 3; i++) {
282                 if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0)
283                     return ret;
284                 c->dsp.idct(c->block[i + 3]);
285             }
286
287             for (j = 0; j < 16; j++) {
288                 uint8_t *out = dst + bx * 3 + (by + j) * dst_stride;
289                 for (i = 0; i < 16; i++) {
290                     int Y, U, V;
291
292                     Y = c->block[(j >> 3) * 2 + (i >> 3)][(i & 7) + (j & 7) * 8];
293                     U = c->block[4 ^ swapuv][(i >> 1) + (j >> 1) * 8] - 128;
294                     V = c->block[5 ^ swapuv][(i >> 1) + (j >> 1) * 8] - 128;
295                     yuv2rgb(out + i * 3, Y, U, V);
296                 }
297             }
298
299             if (!num_mbs)
300                 return 0;
301             bx += 16;
302         }
303         bx  = 0;
304         by += 16;
305         if (mask)
306             mask += mask_stride * 2;
307     }
308
309     return 0;
310 }
311
312 static void kempf_restore_buf(const uint8_t *src, int len,
313                               uint8_t *dst, int stride,
314                               const uint8_t *jpeg_tile, int tile_stride,
315                               int width, int height,
316                               const uint8_t *pal, int npal, int tidx)
317 {
318     GetBitContext gb;
319     int i, j, nb, col;
320
321     init_get_bits(&gb, src, len * 8);
322
323     if (npal <= 2)       nb = 1;
324     else if (npal <= 4)  nb = 2;
325     else if (npal <= 16) nb = 4;
326     else                 nb = 8;
327
328     for (j = 0; j < height; j++, dst += stride, jpeg_tile += tile_stride) {
329         if (get_bits(&gb, 8))
330             continue;
331         for (i = 0; i < width; i++) {
332             col = get_bits(&gb, nb);
333             if (col != tidx)
334                 memcpy(dst + i * 3, pal + col * 3, 3);
335             else
336                 memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
337         }
338     }
339 }
340
341 static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y,
342                              const uint8_t *src, int src_size)
343 {
344     int width, height;
345     int hdr, zsize, npal, tidx = -1, ret;
346     int i, j;
347     const uint8_t *src_end = src + src_size;
348     uint8_t pal[768], transp[3];
349     uLongf dlen = (c->tile_width + 1) * c->tile_height;
350     int sub_type;
351     int nblocks, cblocks, bstride;
352     int bits, bitbuf, coded;
353     uint8_t *dst = c->framebuf + tile_x * c->tile_width * 3 +
354                    tile_y * c->tile_height * c->framebuf_stride;
355
356     if (src_size < 2)
357         return AVERROR_INVALIDDATA;
358
359     width  = FFMIN(c->width  - tile_x * c->tile_width,  c->tile_width);
360     height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
361
362     hdr      = *src++;
363     sub_type = hdr >> 5;
364     if (sub_type == 0) {
365         int j;
366         memcpy(transp, src, 3);
367         src += 3;
368         for (j = 0; j < height; j++, dst += c->framebuf_stride)
369             for (i = 0; i < width; i++)
370                 memcpy(dst + i * 3, transp, 3);
371         return 0;
372     } else if (sub_type == 1) {
373         return jpg_decode_data(&c->jc, width, height, src, src_end - src,
374                                dst, c->framebuf_stride, NULL, 0, 0, 0);
375     }
376
377     if (sub_type != 2) {
378         memcpy(transp, src, 3);
379         src += 3;
380     }
381     npal = *src++ + 1;
382     memcpy(pal, src, npal * 3);
383     src += npal * 3;
384     if (sub_type != 2) {
385         for (i = 0; i < npal; i++) {
386             if (!memcmp(pal + i * 3, transp, 3)) {
387                 tidx = i;
388                 break;
389             }
390         }
391     }
392
393     if (src_end - src < 2)
394         return 0;
395     zsize = (src[0] << 8) | src[1];
396     src  += 2;
397
398     if (src_end - src < zsize)
399         return AVERROR_INVALIDDATA;
400
401     ret = uncompress(c->kempf_buf, &dlen, src, zsize);
402     if (ret)
403         return AVERROR_INVALIDDATA;
404     src += zsize;
405
406     if (sub_type == 2) {
407         kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
408                           NULL, 0, width, height, pal, npal, tidx);
409         return 0;
410     }
411
412     nblocks = *src++ + 1;
413     cblocks = 0;
414     bstride = FFALIGN(width, 16) >> 3;
415     // blocks are coded LSB and we need normal bitreader for JPEG data
416     bits = 0;
417     for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
418         for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
419             if (!bits) {
420                 bitbuf = *src++;
421                 bits   = 8;
422             }
423             coded = bitbuf & 1;
424             bits--;
425             bitbuf >>= 1;
426             cblocks += coded;
427             if (cblocks > nblocks)
428                 return AVERROR_INVALIDDATA;
429             c->kempf_flags[j * 2 +      i * 2      * bstride] =
430             c->kempf_flags[j * 2 + 1 +  i * 2      * bstride] =
431             c->kempf_flags[j * 2 +     (i * 2 + 1) * bstride] =
432             c->kempf_flags[j * 2 + 1 + (i * 2 + 1) * bstride] = coded;
433         }
434     }
435
436     memset(c->jpeg_tile, 0, c->tile_stride * height);
437     jpg_decode_data(&c->jc, width, height, src, src_end - src,
438                     c->jpeg_tile, c->tile_stride,
439                     c->kempf_flags, bstride, nblocks * 4, 0);
440
441     kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
442                       c->jpeg_tile, c->tile_stride,
443                       width, height, pal, npal, tidx);
444
445     return 0;
446 }
447
448 static int g2m_init_buffers(G2MContext *c)
449 {
450     int aligned_height;
451
452     if (!c->framebuf || c->old_width < c->width || c->old_height < c->height) {
453         c->framebuf_stride = FFALIGN(c->width * 3, 16);
454         aligned_height     = FFALIGN(c->height,    16);
455         av_free(c->framebuf);
456         c->framebuf = av_mallocz(c->framebuf_stride * aligned_height);
457         if (!c->framebuf)
458             return AVERROR(ENOMEM);
459     }
460     if (!c->synth_tile || !c->jpeg_tile ||
461         c->old_tile_w < c->tile_width ||
462         c->old_tile_h < c->tile_height) {
463         c->tile_stride = FFALIGN(c->tile_width * 3, 16);
464         aligned_height = FFALIGN(c->tile_height,    16);
465         av_free(c->synth_tile);
466         av_free(c->jpeg_tile);
467         av_free(c->kempf_buf);
468         av_free(c->kempf_flags);
469         c->synth_tile  = av_mallocz(c->tile_stride      * aligned_height);
470         c->jpeg_tile   = av_mallocz(c->tile_stride      * aligned_height);
471         c->kempf_buf   = av_mallocz((c->tile_width + 1) * aligned_height
472                                     + FF_INPUT_BUFFER_PADDING_SIZE);
473         c->kempf_flags = av_mallocz( c->tile_width      * aligned_height);
474         if (!c->synth_tile || !c->jpeg_tile ||
475             !c->kempf_buf || !c->kempf_flags)
476             return AVERROR(ENOMEM);
477     }
478
479     return 0;
480 }
481
482 static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c,
483                            GetByteContext *gb)
484 {
485     int i, j, k;
486     uint8_t *dst;
487     uint32_t bits;
488     uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
489     uint32_t cursor_hot_x, cursor_hot_y;
490     int cursor_fmt, err;
491
492     cur_size     = bytestream2_get_be32(gb);
493     cursor_w     = bytestream2_get_byte(gb);
494     cursor_h     = bytestream2_get_byte(gb);
495     cursor_hot_x = bytestream2_get_byte(gb);
496     cursor_hot_y = bytestream2_get_byte(gb);
497     cursor_fmt   = bytestream2_get_byte(gb);
498
499     cursor_stride = FFALIGN(cursor_w, 32) * 4;
500
501     if (cursor_w < 1 || cursor_w > 256 ||
502         cursor_h < 1 || cursor_h > 256) {
503         av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %dx%d\n",
504                cursor_w, cursor_h);
505         return AVERROR_INVALIDDATA;
506     }
507     if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
508         av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %d,%d\n",
509                cursor_hot_x, cursor_hot_y);
510         cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);
511         cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);
512     }
513     if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||
514         c->cursor_w * c->cursor_h / 4 > cur_size) {
515         av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %d/%d\n",
516                cur_size, bytestream2_get_bytes_left(gb));
517         return AVERROR_INVALIDDATA;
518     }
519     if (cursor_fmt != 1 && cursor_fmt != 32) {
520         avpriv_report_missing_feature(avctx, "Cursor format %d",
521                                       cursor_fmt);
522         return AVERROR_PATCHWELCOME;
523     }
524
525     if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
526         av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
527         return err;
528     }
529
530     c->cursor_w      = cursor_w;
531     c->cursor_h      = cursor_h;
532     c->cursor_hot_x  = cursor_hot_x;
533     c->cursor_hot_y  = cursor_hot_y;
534     c->cursor_fmt    = cursor_fmt;
535     c->cursor_stride = cursor_stride;
536
537     dst = c->cursor;
538     switch (c->cursor_fmt) {
539     case 1: // old monochrome
540         for (j = 0; j < c->cursor_h; j++) {
541             for (i = 0; i < c->cursor_w; i += 32) {
542                 bits = bytestream2_get_be32(gb);
543                 for (k = 0; k < 32; k++) {
544                     dst[0] = !!(bits & 0x80000000);
545                     dst   += 4;
546                     bits <<= 1;
547                 }
548             }
549             dst += c->cursor_stride - c->cursor_w * 4;
550         }
551
552         dst = c->cursor;
553         for (j = 0; j < c->cursor_h; j++) {
554             for (i = 0; i < c->cursor_w; i += 32) {
555                 bits = bytestream2_get_be32(gb);
556                 for (k = 0; k < 32; k++) {
557                     int mask_bit = !!(bits & 0x80000000);
558                     switch (dst[0] * 2 + mask_bit) {
559                     case 0:
560                         dst[0] = 0xFF;
561                         dst[1] = 0x00;
562                         dst[2] = 0x00;
563                         dst[3] = 0x00;
564                         break;
565                     case 1:
566                         dst[0] = 0xFF;
567                         dst[1] = 0xFF;
568                         dst[2] = 0xFF;
569                         dst[3] = 0xFF;
570                         break;
571                     default:
572                         dst[0] = 0x00;
573                         dst[1] = 0x00;
574                         dst[2] = 0x00;
575                         dst[3] = 0x00;
576                     }
577                     dst   += 4;
578                     bits <<= 1;
579                 }
580             }
581             dst += c->cursor_stride - c->cursor_w * 4;
582         }
583         break;
584     case 32: // full colour
585         /* skip monochrome version of the cursor and decode RGBA instead */
586         bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
587         for (j = 0; j < c->cursor_h; j++) {
588             for (i = 0; i < c->cursor_w; i++) {
589                 int val = bytestream2_get_be32(gb);
590                 *dst++ = val >>  0;
591                 *dst++ = val >>  8;
592                 *dst++ = val >> 16;
593                 *dst++ = val >> 24;
594             }
595             dst += c->cursor_stride - c->cursor_w * 4;
596         }
597         break;
598     default:
599         return AVERROR_PATCHWELCOME;
600     }
601     return 0;
602 }
603
604 #define APPLY_ALPHA(src, new, alpha) \
605     src = (src * (256 - alpha) + new * alpha) >> 8
606
607 static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
608 {
609     int i, j;
610     int x, y, w, h;
611     const uint8_t *cursor;
612
613     if (!c->cursor)
614         return;
615
616     x = c->cursor_x - c->cursor_hot_x;
617     y = c->cursor_y - c->cursor_hot_y;
618
619     cursor = c->cursor;
620     w      = c->cursor_w;
621     h      = c->cursor_h;
622
623     if (x + w > c->width)
624         w = c->width - x;
625     if (y + h > c->height)
626         h = c->height - y;
627     if (x < 0) {
628         w      +=  x;
629         cursor += -x * 4;
630     } else {
631         dst    +=  x * 3;
632     }
633     if (y < 0) {
634         h      +=  y;
635         cursor += -y * c->cursor_stride;
636     } else {
637         dst    +=  y * stride;
638     }
639     if (w < 0 || h < 0)
640         return;
641
642     for (j = 0; j < h; j++) {
643         for (i = 0; i < w; i++) {
644             uint8_t alpha = cursor[i * 4];
645             APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
646             APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
647             APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
648         }
649         dst    += stride;
650         cursor += c->cursor_stride;
651     }
652 }
653
654 static int g2m_decode_frame(AVCodecContext *avctx, void *data,
655                             int *got_picture_ptr, AVPacket *avpkt)
656 {
657     const uint8_t *buf = avpkt->data;
658     int buf_size = avpkt->size;
659     G2MContext *c = avctx->priv_data;
660     AVFrame *pic = data;
661     GetByteContext bc, tbc;
662     int magic;
663     int got_header = 0;
664     uint32_t chunk_size, r_mask, g_mask, b_mask;
665     int chunk_type, chunk_start;
666     int i;
667     int ret;
668
669     if (buf_size < 12) {
670         av_log(avctx, AV_LOG_ERROR,
671                "Frame should have at least 12 bytes, got %d instead\n",
672                buf_size);
673         return AVERROR_INVALIDDATA;
674     }
675
676     bytestream2_init(&bc, buf, buf_size);
677
678     magic = bytestream2_get_be32(&bc);
679     if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
680         (magic & 0xF) < 2 || (magic & 0xF) > 4) {
681         av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
682         return AVERROR_INVALIDDATA;
683     }
684
685     if ((magic & 0xF) != 4) {
686         av_log(avctx, AV_LOG_ERROR, "G2M2 and G2M3 are not yet supported\n");
687         return AVERROR(ENOSYS);
688     }
689
690     while (bytestream2_get_bytes_left(&bc) > 5) {
691         chunk_size  = bytestream2_get_le32(&bc) - 1;
692         chunk_type  = bytestream2_get_byte(&bc);
693         chunk_start = bytestream2_tell(&bc);
694         if (chunk_size > bytestream2_get_bytes_left(&bc)) {
695             av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %d type %02X\n",
696                    chunk_size, chunk_type);
697             break;
698         }
699         switch (chunk_type) {
700         case DISPLAY_INFO:
701             c->got_header = 0;
702             if (chunk_size < 21) {
703                 av_log(avctx, AV_LOG_ERROR, "Invalid display info size %d\n",
704                        chunk_size);
705                 break;
706             }
707             c->width  = bytestream2_get_be32(&bc);
708             c->height = bytestream2_get_be32(&bc);
709             if (c->width  < 16 || c->width  > avctx->width ||
710                 c->height < 16 || c->height > avctx->height) {
711                 av_log(avctx, AV_LOG_ERROR,
712                        "Invalid frame dimensions %dx%d\n",
713                        c->width, c->height);
714                 ret = AVERROR_INVALIDDATA;
715                 goto header_fail;
716             }
717             if (c->width != avctx->width || c->height != avctx->height)
718                 ff_set_dimensions(avctx, c->width, c->height);
719             c->compression = bytestream2_get_be32(&bc);
720             if (c->compression != 2 && c->compression != 3) {
721                 av_log(avctx, AV_LOG_ERROR,
722                        "Unknown compression method %d\n",
723                        c->compression);
724                 return AVERROR_PATCHWELCOME;
725             }
726             c->tile_width  = bytestream2_get_be32(&bc);
727             c->tile_height = bytestream2_get_be32(&bc);
728             if (!c->tile_width || !c->tile_height ||
729                 ((c->tile_width | c->tile_height) & 0xF)) {
730                 av_log(avctx, AV_LOG_ERROR,
731                        "Invalid tile dimensions %dx%d\n",
732                        c->tile_width, c->tile_height);
733                 ret = AVERROR_INVALIDDATA;
734                 goto header_fail;
735             }
736             c->tiles_x = (c->width  + c->tile_width  - 1) / c->tile_width;
737             c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
738             c->bpp     = bytestream2_get_byte(&bc);
739             if (c->bpp == 32) {
740                 if (bytestream2_get_bytes_left(&bc) < 16 ||
741                     (chunk_size - 21) < 16) {
742                     av_log(avctx, AV_LOG_ERROR,
743                            "Display info: missing bitmasks!\n");
744                     return AVERROR_INVALIDDATA;
745                 }
746                 r_mask = bytestream2_get_be32(&bc);
747                 g_mask = bytestream2_get_be32(&bc);
748                 b_mask = bytestream2_get_be32(&bc);
749                 if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
750                     av_log(avctx, AV_LOG_ERROR,
751                            "Invalid or unsupported bitmasks: R=%X, G=%X, B=%X\n",
752                            r_mask, g_mask, b_mask);
753                     return AVERROR_PATCHWELCOME;
754                 }
755             } else {
756                 avpriv_request_sample(avctx, "bpp=%d", c->bpp);
757                 return AVERROR_PATCHWELCOME;
758             }
759             if (g2m_init_buffers(c)) {
760                 ret = AVERROR(ENOMEM);
761                 goto header_fail;
762             }
763             got_header = 1;
764             break;
765         case TILE_DATA:
766             if (!c->tiles_x || !c->tiles_y) {
767                 av_log(avctx, AV_LOG_WARNING,
768                        "No display info - skipping tile\n");
769                 break;
770             }
771             if (chunk_size < 2) {
772                 av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %d\n",
773                        chunk_size);
774                 break;
775             }
776             c->tile_x = bytestream2_get_byte(&bc);
777             c->tile_y = bytestream2_get_byte(&bc);
778             if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
779                 av_log(avctx, AV_LOG_ERROR,
780                        "Invalid tile pos %d,%d (in %dx%d grid)\n",
781                        c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
782                 break;
783             }
784             ret = 0;
785             switch (c->compression) {
786             case COMPR_EPIC_J_B:
787                 av_log(avctx, AV_LOG_ERROR,
788                        "ePIC j-b compression is not implemented yet\n");
789                 return AVERROR(ENOSYS);
790             case COMPR_KEMPF_J_B:
791                 ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
792                                         buf + bytestream2_tell(&bc),
793                                         chunk_size - 2);
794                 break;
795             }
796             if (ret && c->framebuf)
797                 av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
798                        c->tile_x, c->tile_y);
799             break;
800         case CURSOR_POS:
801             if (chunk_size < 5) {
802                 av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %d\n",
803                        chunk_size);
804                 break;
805             }
806             c->cursor_x = bytestream2_get_be16(&bc);
807             c->cursor_y = bytestream2_get_be16(&bc);
808             break;
809         case CURSOR_SHAPE:
810             if (chunk_size < 8) {
811                 av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %d\n",
812                        chunk_size);
813                 break;
814             }
815             bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
816                              chunk_size - 4);
817             g2m_load_cursor(avctx, c, &tbc);
818             break;
819         case CHUNK_CC:
820         case CHUNK_CD:
821             break;
822         default:
823             av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02X\n",
824                    chunk_type);
825         }
826
827         /* navigate to next chunk */
828         bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
829     }
830     if (got_header)
831         c->got_header = 1;
832
833     if (c->width && c->height) {
834         if ((ret = ff_get_buffer(avctx, pic, 0)) < 0) {
835             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
836             return ret;
837         }
838
839         pic->key_frame = got_header;
840         pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
841
842         for (i = 0; i < avctx->height; i++)
843             memcpy(pic->data[0] + i * pic->linesize[0],
844                    c->framebuf + i * c->framebuf_stride,
845                    c->width * 3);
846         g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);
847
848         *got_picture_ptr = 1;
849     }
850
851     return buf_size;
852
853 header_fail:
854     c->width   =
855     c->height  = 0;
856     c->tiles_x =
857     c->tiles_y = 0;
858     return ret;
859 }
860
861 static av_cold int g2m_decode_init(AVCodecContext *avctx)
862 {
863     G2MContext *const c = avctx->priv_data;
864     int ret;
865
866     if ((ret = jpg_init(avctx, &c->jc)) != 0) {
867         av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
868         jpg_free_context(&c->jc);
869         return AVERROR(ENOMEM);
870     }
871
872     avctx->pix_fmt = AV_PIX_FMT_RGB24;
873
874     return 0;
875 }
876
877 static av_cold int g2m_decode_end(AVCodecContext *avctx)
878 {
879     G2MContext *const c = avctx->priv_data;
880
881     jpg_free_context(&c->jc);
882
883     av_freep(&c->kempf_buf);
884     av_freep(&c->kempf_flags);
885     av_freep(&c->synth_tile);
886     av_freep(&c->jpeg_tile);
887     av_freep(&c->cursor);
888     av_freep(&c->framebuf);
889
890     return 0;
891 }
892
893 AVCodec ff_g2m_decoder = {
894     .name           = "g2m",
895     .long_name      = NULL_IF_CONFIG_SMALL("Go2Meeting"),
896     .type           = AVMEDIA_TYPE_VIDEO,
897     .id             = AV_CODEC_ID_G2M,
898     .priv_data_size = sizeof(G2MContext),
899     .init           = g2m_decode_init,
900     .close          = g2m_decode_end,
901     .decode         = g2m_decode_frame,
902     .capabilities   = CODEC_CAP_DR1,
903 };