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