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