FFmpeg  4.0
g2meet.c
Go to the documentation of this file.
1 /*
2  * Go2Webinar / Go2Meeting decoder
3  * Copyright (c) 2012 Konstantin Shishkov
4  * Copyright (c) 2013 Maxim Poliakovski
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * Go2Webinar / Go2Meeting decoder
26  */
27 
28 #include <inttypes.h>
29 #include <zlib.h>
30 
31 #include "libavutil/imgutils.h"
32 #include "libavutil/intreadwrite.h"
33 
34 #include "avcodec.h"
35 #include "blockdsp.h"
36 #include "bytestream.h"
37 #include "elsdec.h"
38 #include "get_bits.h"
39 #include "idctdsp.h"
40 #include "internal.h"
41 #include "jpegtables.h"
42 #include "mjpeg.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,
54 };
55 
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 {
85  uint32_t pixel;
88 
89 typedef struct ePICPixHashElem {
90  uint32_t pix_id;
93 
94 #define EPIC_HASH_SIZE 256
95 typedef struct ePICPixHash {
97  int bucket_size[EPIC_HASH_SIZE];
98  int bucket_fill[EPIC_HASH_SIZE];
99 } ePICPixHash;
100 
101 typedef struct ePICContext {
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];
115  uint32_t stack[EPIC_PIX_STACK_SIZE];
117 } ePICContext;
118 
119 typedef struct JPGContext {
123 
124  VLC dc_vlc[2], ac_vlc[2];
125  int prev_dc[3];
126  DECLARE_ALIGNED(32, int16_t, block)[6][64];
127 
129 } JPGContext;
130 
131 typedef struct G2MContext {
134 
135  int version;
136 
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 
144 
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 
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 
183 {
184  int ret;
185 
187  avpriv_mjpeg_val_dc, 12, 0);
188  if (ret)
189  return ret;
191  avpriv_mjpeg_val_dc, 12, 0);
192  if (ret)
193  return ret;
196  if (ret)
197  return ret;
200  if (ret)
201  return ret;
202 
203  ff_blockdsp_init(&c->bdsp, avctx);
204  ff_idctdsp_init(&c->idsp, avctx);
207 
208  return 0;
209 }
210 
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 
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 = get_vlc2(gb, c->dc_vlc[is_chroma].table, 9, 3);
249  if (dc < 0)
250  return AVERROR_INVALIDDATA;
251  if (dc)
252  dc = get_xbits(gb, 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 = get_vlc2(gb, 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 = get_xbits(gb, 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  GetBitContext gb;
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  if((ret = init_get_bits8(&gb, c->buf, unesc_size)) < 0)
303  return ret;
304 
305  width = FFALIGN(width, 16);
306  mb_w = width >> 4;
307  mb_h = (height + 15) >> 4;
308 
309  if (!num_mbs)
310  num_mbs = mb_w * mb_h * 4;
311 
312  for (i = 0; i < 3; i++)
313  c->prev_dc[i] = 1024;
314  bx =
315  by = 0;
316  c->bdsp.clear_blocks(c->block[0]);
317  for (mb_y = 0; mb_y < mb_h; mb_y++) {
318  for (mb_x = 0; mb_x < mb_w; mb_x++) {
319  if (mask && !mask[mb_x * 2] && !mask[mb_x * 2 + 1] &&
320  !mask[mb_x * 2 + mask_stride] &&
321  !mask[mb_x * 2 + 1 + mask_stride]) {
322  bx += 16;
323  continue;
324  }
325  for (j = 0; j < 2; j++) {
326  for (i = 0; i < 2; i++) {
327  if (mask && !mask[mb_x * 2 + i + j * mask_stride])
328  continue;
329  num_mbs--;
330  if ((ret = jpg_decode_block(c, &gb, 0,
331  c->block[i + j * 2])) != 0)
332  return ret;
333  c->idsp.idct(c->block[i + j * 2]);
334  }
335  }
336  for (i = 1; i < 3; i++) {
337  if ((ret = jpg_decode_block(c, &gb, i, c->block[i + 3])) != 0)
338  return ret;
339  c->idsp.idct(c->block[i + 3]);
340  }
341 
342  for (j = 0; j < 16; j++) {
343  uint8_t *out = dst + bx * 3 + (by + j) * dst_stride;
344  for (i = 0; i < 16; i++) {
345  int Y, U, V;
346 
347  Y = c->block[(j >> 3) * 2 + (i >> 3)][(i & 7) + (j & 7) * 8];
348  U = c->block[4][(i >> 1) + (j >> 1) * 8] - 128;
349  V = c->block[5][(i >> 1) + (j >> 1) * 8] - 128;
350  yuv2rgb(out + i * 3, ridx, Y, U, V);
351  }
352  }
353 
354  if (!num_mbs)
355  return 0;
356  bx += 16;
357  }
358  bx = 0;
359  by += 16;
360  if (mask)
361  mask += mask_stride * 2;
362  }
363 
364  return 0;
365 }
366 
367 #define LOAD_NEIGHBOURS(x) \
368  W = curr_row[(x) - 1]; \
369  N = above_row[(x)]; \
370  WW = curr_row[(x) - 2]; \
371  NW = above_row[(x) - 1]; \
372  NE = above_row[(x) + 1]; \
373  NN = above2_row[(x)]; \
374  NNW = above2_row[(x) - 1]; \
375  NWW = above_row[(x) - 2]; \
376  NNE = above2_row[(x) + 1]
377 
378 #define UPDATE_NEIGHBOURS(x) \
379  NNW = NN; \
380  NN = NNE; \
381  NWW = NW; \
382  NW = N; \
383  N = NE; \
384  NE = above_row[(x) + 1]; \
385  NNE = above2_row[(x) + 1]
386 
387 #define R_shift 16
388 #define G_shift 8
389 #define B_shift 0
390 
391 /* improved djb2 hash from http://www.cse.yorku.ca/~oz/hash.html */
392 static int djb2_hash(uint32_t key)
393 {
394  uint32_t h = 5381;
395 
396  h = (h * 33) ^ ((key >> 24) & 0xFF); // xxx: probably not needed at all
397  h = (h * 33) ^ ((key >> 16) & 0xFF);
398  h = (h * 33) ^ ((key >> 8) & 0xFF);
399  h = (h * 33) ^ (key & 0xFF);
400 
401  return h & (EPIC_HASH_SIZE - 1);
402 }
403 
405 {
406  memset(hash, 0, sizeof(*hash));
407 }
408 
410 {
411  int i, idx = djb2_hash(key);
412  ePICPixHashElem *bucket = hash->bucket[idx];
413 
414  for (i = 0; i < hash->bucket_fill[idx]; i++)
415  if (bucket[i].pix_id == key)
416  return &bucket[i];
417 
418  return NULL;
419 }
420 
422 {
423  ePICPixHashElem *bucket, *ret;
424  int idx = djb2_hash(key);
425 
426  if (hash->bucket_size[idx] > INT_MAX / sizeof(**hash->bucket))
427  return NULL;
428 
429  if (!(hash->bucket_fill[idx] < hash->bucket_size[idx])) {
430  int new_size = hash->bucket_size[idx] + 16;
431  bucket = av_realloc(hash->bucket[idx], new_size * sizeof(*bucket));
432  if (!bucket)
433  return NULL;
434  hash->bucket[idx] = bucket;
435  hash->bucket_size[idx] = new_size;
436  }
437 
438  ret = &hash->bucket[idx][hash->bucket_fill[idx]++];
439  memset(ret, 0, sizeof(*ret));
440  ret->pix_id = key;
441  return ret;
442 }
443 
444 static int epic_add_pixel_to_cache(ePICPixHash *hash, uint32_t key, uint32_t pix)
445 {
446  ePICPixListElem *new_elem;
447  ePICPixHashElem *hash_elem = epic_hash_find(hash, key);
448 
449  if (!hash_elem) {
450  if (!(hash_elem = epic_hash_add(hash, key)))
451  return AVERROR(ENOMEM);
452  }
453 
454  new_elem = av_mallocz(sizeof(*new_elem));
455  if (!new_elem)
456  return AVERROR(ENOMEM);
457 
458  new_elem->pixel = pix;
459  new_elem->next = hash_elem->list;
460  hash_elem->list = new_elem;
461 
462  return 0;
463 }
464 
466  uint32_t pix)
467 {
468  ePICPixHashElem *hash_elem = epic_hash_find(hash, pix);
469 
470  if (hash_elem != NULL && hash_elem->list != NULL)
471  return 1;
472 
473  return 0;
474 }
475 
477 {
478  int i, j;
479 
480  for (i = 0; i < EPIC_HASH_SIZE; i++) {
481  for (j = 0; j < hash->bucket_fill[i]; j++) {
482  ePICPixListElem *list_elem = hash->bucket[i][j].list;
483  while (list_elem) {
484  ePICPixListElem *tmp = list_elem->next;
485  av_free(list_elem);
486  list_elem = tmp;
487  }
488  }
489  av_freep(&hash->bucket[i]);
490  hash->bucket_size[i] =
491  hash->bucket_fill[i] = 0;
492  }
493 }
494 
495 static inline int is_pixel_on_stack(const ePICContext *dc, uint32_t pix)
496 {
497  int i;
498 
499  for (i = 0; i < dc->stack_pos; i++)
500  if (dc->stack[i] == pix)
501  break;
502 
503  return i != dc->stack_pos;
504 }
505 
506 #define TOSIGNED(val) (((val) >> 1) ^ -((val) & 1))
507 
509  int N, int W, int NW)
510 {
511  unsigned delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
512  return mid_pred(N, N + W - NW, W) - TOSIGNED(delta);
513 }
514 
515 static uint32_t epic_decode_pixel_pred(ePICContext *dc, int x, int y,
516  const uint32_t *curr_row,
517  const uint32_t *above_row)
518 {
519  uint32_t N, W, NW, pred;
520  unsigned delta;
521  int GN, GW, GNW, R, G, B;
522 
523  if (x && y) {
524  W = curr_row[x - 1];
525  N = above_row[x];
526  NW = above_row[x - 1];
527 
528  GN = (N >> G_shift) & 0xFF;
529  GW = (W >> G_shift) & 0xFF;
530  GNW = (NW >> G_shift) & 0xFF;
531 
532  G = epic_decode_component_pred(dc, GN, GW, GNW);
533 
534  R = G + epic_decode_component_pred(dc,
535  ((N >> R_shift) & 0xFF) - GN,
536  ((W >> R_shift) & 0xFF) - GW,
537  ((NW >> R_shift) & 0xFF) - GNW);
538 
539  B = G + epic_decode_component_pred(dc,
540  ((N >> B_shift) & 0xFF) - GN,
541  ((W >> B_shift) & 0xFF) - GW,
542  ((NW >> B_shift) & 0xFF) - GNW);
543  } else {
544  if (x)
545  pred = curr_row[x - 1];
546  else
547  pred = above_row[x];
548 
549  delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
550  R = ((pred >> R_shift) & 0xFF) - TOSIGNED(delta);
551 
552  delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
553  G = ((pred >> G_shift) & 0xFF) - TOSIGNED(delta);
554 
555  delta = ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung);
556  B = ((pred >> B_shift) & 0xFF) - TOSIGNED(delta);
557  }
558 
559  if (R<0 || G<0 || B<0) {
560  av_log(NULL, AV_LOG_ERROR, "RGB %d %d %d is out of range\n", R, G, B);
561  return 0;
562  }
563 
564  return (R << R_shift) | (G << G_shift) | (B << B_shift);
565 }
566 
568  uint32_t *pPix, uint32_t pix)
569 {
570  if (!ff_els_decode_bit(&dc->els_ctx, rung)) {
571  *pPix = pix;
572  return 1;
573  }
574  dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
575  return 0;
576 }
577 
578 static int epic_handle_edges(ePICContext *dc, int x, int y,
579  const uint32_t *curr_row,
580  const uint32_t *above_row, uint32_t *pPix)
581 {
582  uint32_t pix;
583 
584  if (!x && !y) { /* special case: top-left pixel */
585  /* the top-left pixel is coded independently with 3 unsigned numbers */
586  *pPix = (ff_els_decode_unsigned(&dc->els_ctx, &dc->unsigned_rung) << R_shift) |
589  return 1;
590  }
591 
592  if (x) { /* predict from W first */
593  pix = curr_row[x - 1];
594  if (epic_predict_pixel(dc, &dc->W_flag_rung, pPix, pix))
595  return 1;
596  }
597 
598  if (y) { /* then try to predict from N */
599  pix = above_row[x];
600  if (!dc->stack_pos || dc->stack[0] != pix) {
601  if (epic_predict_pixel(dc, &dc->N_flag_rung, pPix, pix))
602  return 1;
603  }
604  }
605 
606  return 0;
607 }
608 
609 static int epic_decode_run_length(ePICContext *dc, int x, int y, int tile_width,
610  const uint32_t *curr_row,
611  const uint32_t *above_row,
612  const uint32_t *above2_row,
613  uint32_t *pPix, int *pRun)
614 {
615  int idx, got_pixel = 0, WWneW, old_WWneW = 0;
616  uint32_t W, WW, N, NN, NW, NE, NWW, NNW, NNE;
617 
618  *pRun = 0;
619 
620  LOAD_NEIGHBOURS(x);
621 
622  if (dc->next_run_pos == x) {
623  /* can't reuse W for the new pixel in this case */
624  WWneW = 1;
625  } else {
626  idx = (WW != W) << 7 |
627  (NW != W) << 6 |
628  (N != NE) << 5 |
629  (NW != N) << 4 |
630  (NWW != NW) << 3 |
631  (NNE != NE) << 2 |
632  (NN != N) << 1 |
633  (NNW != NW);
634  WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
635  if (WWneW < 0)
636  return WWneW;
637  }
638 
639  if (WWneW)
640  dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = W;
641  else {
642  *pPix = W;
643  got_pixel = 1;
644  }
645 
646  do {
647  int NWneW = 1;
648  if (got_pixel) // pixel value already known (derived from either W or N)
649  NWneW = *pPix != N;
650  else { // pixel value is unknown and will be decoded later
651  NWneW = *pRun ? NWneW : NW != W;
652 
653  /* TODO: RFC this mess! */
654  switch (((NW != N) << 2) | (NWneW << 1) | WWneW) {
655  case 0:
656  break; // do nothing here
657  case 3:
658  case 5:
659  case 6:
660  case 7:
661  if (!is_pixel_on_stack(dc, N)) {
662  idx = WWneW << 8 |
663  (*pRun ? old_WWneW : WW != W) << 7 |
664  NWneW << 6 |
665  (N != NE) << 5 |
666  (NW != N) << 4 |
667  (NWW != NW) << 3 |
668  (NNE != NE) << 2 |
669  (NN != N) << 1 |
670  (NNW != NW);
671  if (!ff_els_decode_bit(&dc->els_ctx, &dc->N_ctx_rung[idx])) {
672  NWneW = 0;
673  *pPix = N;
674  got_pixel = 1;
675  break;
676  }
677  }
678  /* fall through */
679  default:
680  NWneW = 1;
681  old_WWneW = WWneW;
682  if (!is_pixel_on_stack(dc, N))
683  dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = N;
684  }
685  }
686 
687  (*pRun)++;
688  if (x + *pRun >= tile_width - 1)
689  break;
690 
691  UPDATE_NEIGHBOURS(x + *pRun);
692 
693  if (!NWneW && NW == N && N == NE) {
694  int pos, run, rle;
695  int start_pos = x + *pRun;
696 
697  /* scan for a run of pix in the line above */
698  uint32_t pix = above_row[start_pos + 1];
699  for (pos = start_pos + 2; pos < tile_width; pos++)
700  if (!(above_row[pos] == pix))
701  break;
702  run = pos - start_pos - 1;
703  idx = av_ceil_log2(run);
704  if (ff_els_decode_bit(&dc->els_ctx, &dc->prev_row_rung[idx]))
705  *pRun += run;
706  else {
707  int flag;
708  /* run-length is coded as plain binary number of idx - 1 bits */
709  for (pos = idx - 1, rle = 0, flag = 0; pos >= 0; pos--) {
710  if ((1 << pos) + rle < run &&
712  flag ? &dc->runlen_one
713  : &dc->runlen_zeroes[pos])) {
714  flag = 1;
715  rle |= 1 << pos;
716  }
717  }
718  *pRun += rle;
719  break; // return immediately
720  }
721  if (x + *pRun >= tile_width - 1)
722  break;
723 
724  LOAD_NEIGHBOURS(x + *pRun);
725  WWneW = 0;
726  NWneW = 0;
727  }
728 
729  idx = WWneW << 7 |
730  NWneW << 6 |
731  (N != NE) << 5 |
732  (NW != N) << 4 |
733  (NWW != NW) << 3 |
734  (NNE != NE) << 2 |
735  (NN != N) << 1 |
736  (NNW != NW);
737  WWneW = ff_els_decode_bit(&dc->els_ctx, &dc->W_ctx_rung[idx]);
738  } while (!WWneW);
739 
740  dc->next_run_pos = x + *pRun;
741  return got_pixel;
742 }
743 
745  uint32_t *pPix, uint32_t pix)
746 {
747  if (ff_els_decode_bit(&dc->els_ctx, rung)) {
748  *pPix = pix;
749  return 1;
750  }
751  dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = pix;
752  return 0;
753 }
754 
755 static int epic_predict_from_NW_NE(ePICContext *dc, int x, int y, int run,
756  int tile_width, const uint32_t *curr_row,
757  const uint32_t *above_row, uint32_t *pPix)
758 {
759  int pos;
760 
761  /* try to reuse the NW pixel first */
762  if (x && y) {
763  uint32_t NW = above_row[x - 1];
764  if (NW != curr_row[x - 1] && NW != above_row[x] && !is_pixel_on_stack(dc, NW)) {
765  if (epic_predict_pixel2(dc, &dc->nw_pred_rung[NW & 0xFF], pPix, NW))
766  return 1;
767  }
768  }
769 
770  /* try to reuse the NE[x + run, y] pixel */
771  pos = x + run - 1;
772  if (pos < tile_width - 1 && y) {
773  uint32_t NE = above_row[pos + 1];
774  if (NE != above_row[pos] && !is_pixel_on_stack(dc, NE)) {
775  if (epic_predict_pixel2(dc, &dc->ne_pred_rung[NE & 0xFF], pPix, NE))
776  return 1;
777  }
778  }
779 
780  return 0;
781 }
782 
783 static int epic_decode_from_cache(ePICContext *dc, uint32_t W, uint32_t *pPix)
784 {
785  ePICPixListElem *list, *prev = NULL;
786  ePICPixHashElem *hash_elem = epic_hash_find(&dc->hash, W);
787 
788  if (!hash_elem || !hash_elem->list)
789  return 0;
790 
791  list = hash_elem->list;
792  while (list) {
793  if (!is_pixel_on_stack(dc, list->pixel)) {
794  if (ff_els_decode_bit(&dc->els_ctx, &list->rung)) {
795  *pPix = list->pixel;
796  if (list != hash_elem->list) {
797  prev->next = list->next;
798  list->next = hash_elem->list;
799  hash_elem->list = list;
800  }
801  return 1;
802  }
803  dc->stack[dc->stack_pos++ & EPIC_PIX_STACK_MAX] = list->pixel;
804  }
805  prev = list;
806  list = list->next;
807  }
808 
809  return 0;
810 }
811 
812 static int epic_decode_tile(ePICContext *dc, uint8_t *out, int tile_height,
813  int tile_width, int stride)
814 {
815  int x, y;
816  uint32_t pix;
817  uint32_t *curr_row = NULL, *above_row = NULL, *above2_row;
818 
819  for (y = 0; y < tile_height; y++, out += stride) {
820  above2_row = above_row;
821  above_row = curr_row;
822  curr_row = (uint32_t *) out;
823 
824  for (x = 0, dc->next_run_pos = 0; x < tile_width;) {
825  if (dc->els_ctx.err)
826  return AVERROR_INVALIDDATA; // bail out in the case of ELS overflow
827 
828  pix = curr_row[x - 1]; // get W pixel
829 
830  if (y >= 1 && x >= 2 &&
831  pix != curr_row[x - 2] && pix != above_row[x - 1] &&
832  pix != above_row[x - 2] && pix != above_row[x] &&
833  !epic_cache_entries_for_pixel(&dc->hash, pix)) {
834  curr_row[x] = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
835  x++;
836  } else {
837  int got_pixel, run;
838  dc->stack_pos = 0; // empty stack
839 
840  if (y < 2 || x < 2 || x == tile_width - 1) {
841  run = 1;
842  got_pixel = epic_handle_edges(dc, x, y, curr_row, above_row, &pix);
843  } else {
844  got_pixel = epic_decode_run_length(dc, x, y, tile_width,
845  curr_row, above_row,
846  above2_row, &pix, &run);
847  if (got_pixel < 0)
848  return got_pixel;
849  }
850 
851  if (!got_pixel && !epic_predict_from_NW_NE(dc, x, y, run,
852  tile_width, curr_row,
853  above_row, &pix)) {
854  uint32_t ref_pix = curr_row[x - 1];
855  if (!x || !epic_decode_from_cache(dc, ref_pix, &pix)) {
856  pix = epic_decode_pixel_pred(dc, x, y, curr_row, above_row);
857  if (x) {
858  int ret = epic_add_pixel_to_cache(&dc->hash,
859  ref_pix,
860  pix);
861  if (ret)
862  return ret;
863  }
864  }
865  }
866  for (; run > 0; x++, run--)
867  curr_row[x] = pix;
868  }
869  }
870  }
871 
872  return 0;
873 }
874 
875 static int epic_jb_decode_tile(G2MContext *c, int tile_x, int tile_y,
876  const uint8_t *src, size_t src_size,
877  AVCodecContext *avctx)
878 {
879  uint8_t prefix, mask = 0x80;
880  int extrabytes, tile_width, tile_height, awidth, aheight;
881  size_t els_dsize;
882  uint8_t *dst;
883 
884  if (!src_size)
885  return 0;
886 
887  /* get data size of the ELS partition as unsigned variable-length integer */
888  prefix = *src++;
889  src_size--;
890  for (extrabytes = 0; (prefix & mask) && (extrabytes < 7); extrabytes++)
891  mask >>= 1;
892  if (extrabytes > 3 || src_size < extrabytes) {
893  av_log(avctx, AV_LOG_ERROR, "ePIC: invalid data size VLI\n");
894  return AVERROR_INVALIDDATA;
895  }
896 
897  els_dsize = prefix & ((0x80 >> extrabytes) - 1); // mask out the length prefix
898  while (extrabytes-- > 0) {
899  els_dsize = (els_dsize << 8) | *src++;
900  src_size--;
901  }
902 
903  if (src_size < els_dsize) {
904  av_log(avctx, AV_LOG_ERROR, "ePIC: data too short, needed %"SIZE_SPECIFIER", got %"SIZE_SPECIFIER"\n",
905  els_dsize, src_size);
906  return AVERROR_INVALIDDATA;
907  }
908 
909  tile_width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
910  tile_height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
911  awidth = FFALIGN(tile_width, 16);
912  aheight = FFALIGN(tile_height, 16);
913 
914  if (els_dsize) {
915  int ret, i, j, k;
916  uint8_t tr_r, tr_g, tr_b, *buf;
917  uint32_t *in;
918  /* ELS decoder initializations */
919  memset(&c->ec, 0, sizeof(c->ec));
920  ff_els_decoder_init(&c->ec.els_ctx, src, els_dsize);
921  epic_hash_init(&c->ec.hash);
922 
923  /* decode transparent pixel value */
927  if (c->ec.els_ctx.err != 0) {
928  av_log(avctx, AV_LOG_ERROR,
929  "ePIC: couldn't decode transparency pixel!\n");
930  return AVERROR_INVALIDDATA;
931  }
932 
933  ret = epic_decode_tile(&c->ec, c->epic_buf, tile_height, tile_width,
934  c->epic_buf_stride);
935 
938 
939  if (ret) {
940  av_log(avctx, AV_LOG_ERROR,
941  "ePIC: tile decoding failed, frame=%d, tile_x=%d, tile_y=%d\n",
942  avctx->frame_number, tile_x, tile_y);
943  return AVERROR_INVALIDDATA;
944  }
945 
946  buf = c->epic_buf;
947  dst = c->framebuf + tile_x * c->tile_width * 3 +
948  tile_y * c->tile_height * c->framebuf_stride;
949 
950  for (j = 0; j < tile_height; j++) {
951  uint8_t *out = dst;
952  in = (uint32_t *) buf;
953  for (i = 0; i < tile_width; i++) {
954  out[0] = (in[i] >> R_shift) & 0xFF;
955  out[1] = (in[i] >> G_shift) & 0xFF;
956  out[2] = (in[i] >> B_shift) & 0xFF;
957  out += 3;
958  }
959  buf += c->epic_buf_stride;
960  dst += c->framebuf_stride;
961  }
962 
963  if (src_size > els_dsize) {
964  uint8_t *jpg;
965  uint32_t tr;
966  int bstride = FFALIGN(tile_width, 16) >> 3;
967  int nblocks = 0;
968  int estride = c->epic_buf_stride >> 2;
969 
970  src += els_dsize;
971  src_size -= els_dsize;
972 
973  in = (uint32_t *) c->epic_buf;
974  tr = (tr_r << R_shift) | (tr_g << G_shift) | (tr_b << B_shift);
975 
976  memset(c->kempf_flags, 0,
977  (aheight >> 3) * bstride * sizeof(*c->kempf_flags));
978  for (j = 0; j < tile_height; j += 8) {
979  for (i = 0; i < tile_width; i += 8) {
980  c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 0;
981  for (k = 0; k < 8 * 8; k++) {
982  if (in[i + (k & 7) + (k >> 3) * estride] == tr) {
983  c->kempf_flags[(i >> 3) + (j >> 3) * bstride] = 1;
984  nblocks++;
985  break;
986  }
987  }
988  }
989  in += 8 * estride;
990  }
991 
992  memset(c->jpeg_tile, 0, c->tile_stride * aheight);
993  jpg_decode_data(&c->jc, awidth, aheight, src, src_size,
994  c->jpeg_tile, c->tile_stride,
995  c->kempf_flags, bstride, nblocks, c->swapuv);
996 
997  in = (uint32_t *) c->epic_buf;
998  dst = c->framebuf + tile_x * c->tile_width * 3 +
999  tile_y * c->tile_height * c->framebuf_stride;
1000  jpg = c->jpeg_tile;
1001  for (j = 0; j < tile_height; j++) {
1002  for (i = 0; i < tile_width; i++)
1003  if (in[i] == tr)
1004  memcpy(dst + i * 3, jpg + i * 3, 3);
1005  in += c->epic_buf_stride >> 2;
1006  dst += c->framebuf_stride;
1007  jpg += c->tile_stride;
1008  }
1009  }
1010  } else {
1011  dst = c->framebuf + tile_x * c->tile_width * 3 +
1012  tile_y * c->tile_height * c->framebuf_stride;
1013  return jpg_decode_data(&c->jc, tile_width, tile_height, src, src_size,
1014  dst, c->framebuf_stride, NULL, 0, 0, c->swapuv);
1015  }
1016 
1017  return 0;
1018 }
1019 
1020 static int kempf_restore_buf(const uint8_t *src, int len,
1021  uint8_t *dst, int stride,
1022  const uint8_t *jpeg_tile, int tile_stride,
1023  int width, int height,
1024  const uint8_t *pal, int npal, int tidx)
1025 {
1026  GetBitContext gb;
1027  int i, j, nb, col;
1028  int ret;
1029  int align_width = FFALIGN(width, 16);
1030 
1031  if ((ret = init_get_bits8(&gb, src, len)) < 0)
1032  return ret;
1033 
1034  if (npal <= 2) nb = 1;
1035  else if (npal <= 4) nb = 2;
1036  else if (npal <= 16) nb = 4;
1037  else nb = 8;
1038 
1039  for (j = 0; j < height; j++, dst += stride, jpeg_tile += tile_stride) {
1040  if (get_bits(&gb, 8))
1041  continue;
1042  for (i = 0; i < width; i++) {
1043  col = get_bits(&gb, nb);
1044  if (col != tidx)
1045  memcpy(dst + i * 3, pal + col * 3, 3);
1046  else
1047  memcpy(dst + i * 3, jpeg_tile + i * 3, 3);
1048  }
1049  skip_bits_long(&gb, nb * (align_width - width));
1050  }
1051 
1052  return 0;
1053 }
1054 
1055 static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y,
1056  const uint8_t *src, int src_size)
1057 {
1058  int width, height;
1059  int hdr, zsize, npal, tidx = -1, ret;
1060  int i, j;
1061  const uint8_t *src_end = src + src_size;
1062  uint8_t pal[768], transp[3];
1063  uLongf dlen = (c->tile_width + 1) * c->tile_height;
1064  int sub_type;
1065  int nblocks, cblocks, bstride;
1066  int bits, bitbuf, coded;
1067  uint8_t *dst = c->framebuf + tile_x * c->tile_width * 3 +
1068  tile_y * c->tile_height * c->framebuf_stride;
1069 
1070  if (src_size < 2)
1071  return AVERROR_INVALIDDATA;
1072 
1073  width = FFMIN(c->width - tile_x * c->tile_width, c->tile_width);
1074  height = FFMIN(c->height - tile_y * c->tile_height, c->tile_height);
1075 
1076  hdr = *src++;
1077  sub_type = hdr >> 5;
1078  if (sub_type == 0) {
1079  int j;
1080  memcpy(transp, src, 3);
1081  src += 3;
1082  for (j = 0; j < height; j++, dst += c->framebuf_stride)
1083  for (i = 0; i < width; i++)
1084  memcpy(dst + i * 3, transp, 3);
1085  return 0;
1086  } else if (sub_type == 1) {
1087  return jpg_decode_data(&c->jc, width, height, src, src_end - src,
1088  dst, c->framebuf_stride, NULL, 0, 0, 0);
1089  }
1090 
1091  if (sub_type != 2) {
1092  memcpy(transp, src, 3);
1093  src += 3;
1094  }
1095  npal = *src++ + 1;
1096  if (src_end - src < npal * 3)
1097  return AVERROR_INVALIDDATA;
1098  memcpy(pal, src, npal * 3);
1099  src += npal * 3;
1100  if (sub_type != 2) {
1101  for (i = 0; i < npal; i++) {
1102  if (!memcmp(pal + i * 3, transp, 3)) {
1103  tidx = i;
1104  break;
1105  }
1106  }
1107  }
1108 
1109  if (src_end - src < 2)
1110  return 0;
1111  zsize = (src[0] << 8) | src[1];
1112  src += 2;
1113 
1114  if (src_end - src < zsize + (sub_type != 2))
1115  return AVERROR_INVALIDDATA;
1116 
1117  ret = uncompress(c->kempf_buf, &dlen, src, zsize);
1118  if (ret)
1119  return AVERROR_INVALIDDATA;
1120  src += zsize;
1121 
1122  if (sub_type == 2) {
1123  kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
1124  NULL, 0, width, height, pal, npal, tidx);
1125  return 0;
1126  }
1127 
1128  nblocks = *src++ + 1;
1129  cblocks = 0;
1130  bstride = FFALIGN(width, 16) >> 3;
1131  // blocks are coded LSB and we need normal bitreader for JPEG data
1132  bits = 0;
1133  for (i = 0; i < (FFALIGN(height, 16) >> 4); i++) {
1134  for (j = 0; j < (FFALIGN(width, 16) >> 4); j++) {
1135  if (!bits) {
1136  if (src >= src_end)
1137  return AVERROR_INVALIDDATA;
1138  bitbuf = *src++;
1139  bits = 8;
1140  }
1141  coded = bitbuf & 1;
1142  bits--;
1143  bitbuf >>= 1;
1144  cblocks += coded;
1145  if (cblocks > nblocks)
1146  return AVERROR_INVALIDDATA;
1147  c->kempf_flags[j * 2 + i * 2 * bstride] =
1148  c->kempf_flags[j * 2 + 1 + i * 2 * bstride] =
1149  c->kempf_flags[j * 2 + (i * 2 + 1) * bstride] =
1150  c->kempf_flags[j * 2 + 1 + (i * 2 + 1) * bstride] = coded;
1151  }
1152  }
1153 
1154  memset(c->jpeg_tile, 0, c->tile_stride * height);
1155  jpg_decode_data(&c->jc, width, height, src, src_end - src,
1156  c->jpeg_tile, c->tile_stride,
1157  c->kempf_flags, bstride, nblocks * 4, 0);
1158 
1159  kempf_restore_buf(c->kempf_buf, dlen, dst, c->framebuf_stride,
1160  c->jpeg_tile, c->tile_stride,
1161  width, height, pal, npal, tidx);
1162 
1163  return 0;
1164 }
1165 
1167 {
1168  int aligned_height;
1169 
1170  if (!c->framebuf || c->old_width < c->width || c->old_height < c->height) {
1171  c->framebuf_stride = FFALIGN(c->width + 15, 16) * 3;
1172  aligned_height = c->height + 15;
1173  av_free(c->framebuf);
1174  c->framebuf = av_mallocz_array(c->framebuf_stride, aligned_height);
1175  if (!c->framebuf)
1176  return AVERROR(ENOMEM);
1177  }
1178  if (!c->synth_tile || !c->jpeg_tile ||
1179  (c->compression == 2 && !c->epic_buf_base) ||
1180  c->old_tile_w < c->tile_width ||
1181  c->old_tile_h < c->tile_height) {
1182  c->tile_stride = FFALIGN(c->tile_width, 16) * 3;
1183  c->epic_buf_stride = FFALIGN(c->tile_width * 4, 16);
1184  aligned_height = FFALIGN(c->tile_height, 16);
1185  av_freep(&c->synth_tile);
1186  av_freep(&c->jpeg_tile);
1187  av_freep(&c->kempf_buf);
1188  av_freep(&c->kempf_flags);
1189  av_freep(&c->epic_buf_base);
1190  c->epic_buf = NULL;
1191  c->synth_tile = av_mallocz(c->tile_stride * aligned_height);
1192  c->jpeg_tile = av_mallocz(c->tile_stride * aligned_height);
1193  c->kempf_buf = av_mallocz((c->tile_width + 1) * aligned_height +
1195  c->kempf_flags = av_mallocz(c->tile_width * aligned_height);
1196  if (!c->synth_tile || !c->jpeg_tile ||
1197  !c->kempf_buf || !c->kempf_flags)
1198  return AVERROR(ENOMEM);
1199  if (c->compression == 2) {
1200  c->epic_buf_base = av_mallocz(c->epic_buf_stride * aligned_height + 4);
1201  if (!c->epic_buf_base)
1202  return AVERROR(ENOMEM);
1203  c->epic_buf = c->epic_buf_base + 4;
1204  }
1205  }
1206 
1207  return 0;
1208 }
1209 
1211  GetByteContext *gb)
1212 {
1213  int i, j, k;
1214  uint8_t *dst;
1215  uint32_t bits;
1216  uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
1217  uint32_t cursor_hot_x, cursor_hot_y;
1218  int cursor_fmt, err;
1219 
1220  cur_size = bytestream2_get_be32(gb);
1221  cursor_w = bytestream2_get_byte(gb);
1222  cursor_h = bytestream2_get_byte(gb);
1223  cursor_hot_x = bytestream2_get_byte(gb);
1224  cursor_hot_y = bytestream2_get_byte(gb);
1225  cursor_fmt = bytestream2_get_byte(gb);
1226 
1227  cursor_stride = FFALIGN(cursor_w, cursor_fmt==1 ? 32 : 1) * 4;
1228 
1229  if (cursor_w < 1 || cursor_w > 256 ||
1230  cursor_h < 1 || cursor_h > 256) {
1231  av_log(avctx, AV_LOG_ERROR, "Invalid cursor dimensions %"PRIu32"x%"PRIu32"\n",
1232  cursor_w, cursor_h);
1233  return AVERROR_INVALIDDATA;
1234  }
1235  if (cursor_hot_x > cursor_w || cursor_hot_y > cursor_h) {
1236  av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %"PRIu32",%"PRIu32"\n",
1237  cursor_hot_x, cursor_hot_y);
1238  cursor_hot_x = FFMIN(cursor_hot_x, cursor_w - 1);
1239  cursor_hot_y = FFMIN(cursor_hot_y, cursor_h - 1);
1240  }
1241  if (cur_size - 9 > bytestream2_get_bytes_left(gb) ||
1242  c->cursor_w * c->cursor_h / 4 > cur_size) {
1243  av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"/%u\n",
1244  cur_size, bytestream2_get_bytes_left(gb));
1245  return AVERROR_INVALIDDATA;
1246  }
1247  if (cursor_fmt != 1 && cursor_fmt != 32) {
1248  avpriv_report_missing_feature(avctx, "Cursor format %d",
1249  cursor_fmt);
1250  return AVERROR_PATCHWELCOME;
1251  }
1252 
1253  if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
1254  av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
1255  return err;
1256  }
1257 
1258  c->cursor_w = cursor_w;
1259  c->cursor_h = cursor_h;
1260  c->cursor_hot_x = cursor_hot_x;
1261  c->cursor_hot_y = cursor_hot_y;
1262  c->cursor_fmt = cursor_fmt;
1263  c->cursor_stride = cursor_stride;
1264 
1265  dst = c->cursor;
1266  switch (c->cursor_fmt) {
1267  case 1: // old monochrome
1268  for (j = 0; j < c->cursor_h; j++) {
1269  for (i = 0; i < c->cursor_w; i += 32) {
1270  bits = bytestream2_get_be32(gb);
1271  for (k = 0; k < 32; k++) {
1272  dst[0] = !!(bits & 0x80000000);
1273  dst += 4;
1274  bits <<= 1;
1275  }
1276  }
1277  }
1278 
1279  dst = c->cursor;
1280  for (j = 0; j < c->cursor_h; j++) {
1281  for (i = 0; i < c->cursor_w; i += 32) {
1282  bits = bytestream2_get_be32(gb);
1283  for (k = 0; k < 32; k++) {
1284  int mask_bit = !!(bits & 0x80000000);
1285  switch (dst[0] * 2 + mask_bit) {
1286  case 0:
1287  dst[0] = 0xFF;
1288  dst[1] = 0x00;
1289  dst[2] = 0x00;
1290  dst[3] = 0x00;
1291  break;
1292  case 1:
1293  dst[0] = 0xFF;
1294  dst[1] = 0xFF;
1295  dst[2] = 0xFF;
1296  dst[3] = 0xFF;
1297  break;
1298  default:
1299  dst[0] = 0x00;
1300  dst[1] = 0x00;
1301  dst[2] = 0x00;
1302  dst[3] = 0x00;
1303  }
1304  dst += 4;
1305  bits <<= 1;
1306  }
1307  }
1308  }
1309  break;
1310  case 32: // full colour
1311  /* skip monochrome version of the cursor and decode RGBA instead */
1312  bytestream2_skip(gb, c->cursor_h * (FFALIGN(c->cursor_w, 32) >> 3));
1313  for (j = 0; j < c->cursor_h; j++) {
1314  for (i = 0; i < c->cursor_w; i++) {
1315  int val = bytestream2_get_be32(gb);
1316  *dst++ = val >> 0;
1317  *dst++ = val >> 8;
1318  *dst++ = val >> 16;
1319  *dst++ = val >> 24;
1320  }
1321  }
1322  break;
1323  default:
1324  return AVERROR_PATCHWELCOME;
1325  }
1326  return 0;
1327 }
1328 
1329 #define APPLY_ALPHA(src, new, alpha) \
1330  src = (src * (256 - alpha) + new * alpha) >> 8
1331 
1332 static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
1333 {
1334  int i, j;
1335  int x, y, w, h;
1336  const uint8_t *cursor;
1337 
1338  if (!c->cursor)
1339  return;
1340 
1341  x = c->cursor_x - c->cursor_hot_x;
1342  y = c->cursor_y - c->cursor_hot_y;
1343 
1344  cursor = c->cursor;
1345  w = c->cursor_w;
1346  h = c->cursor_h;
1347 
1348  if (x + w > c->width)
1349  w = c->width - x;
1350  if (y + h > c->height)
1351  h = c->height - y;
1352  if (x < 0) {
1353  w += x;
1354  cursor += -x * 4;
1355  } else {
1356  dst += x * 3;
1357  }
1358  if (y < 0) {
1359  h += y;
1360  cursor += -y * c->cursor_stride;
1361  } else {
1362  dst += y * stride;
1363  }
1364  if (w < 0 || h < 0)
1365  return;
1366 
1367  for (j = 0; j < h; j++) {
1368  for (i = 0; i < w; i++) {
1369  uint8_t alpha = cursor[i * 4];
1370  APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
1371  APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
1372  APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
1373  }
1374  dst += stride;
1375  cursor += c->cursor_stride;
1376  }
1377 }
1378 
1379 static int g2m_decode_frame(AVCodecContext *avctx, void *data,
1380  int *got_picture_ptr, AVPacket *avpkt)
1381 {
1382  const uint8_t *buf = avpkt->data;
1383  int buf_size = avpkt->size;
1384  G2MContext *c = avctx->priv_data;
1385  AVFrame *pic = data;
1386  GetByteContext bc, tbc;
1387  int magic;
1388  int got_header = 0;
1389  uint32_t chunk_size, r_mask, g_mask, b_mask;
1390  int chunk_type, chunk_start;
1391  int i;
1392  int ret;
1393 
1394  if (buf_size < 12) {
1395  av_log(avctx, AV_LOG_ERROR,
1396  "Frame should have at least 12 bytes, got %d instead\n",
1397  buf_size);
1398  return AVERROR_INVALIDDATA;
1399  }
1400 
1401  bytestream2_init(&bc, buf, buf_size);
1402 
1403  magic = bytestream2_get_be32(&bc);
1404  if ((magic & ~0xF) != MKBETAG('G', '2', 'M', '0') ||
1405  (magic & 0xF) < 2 || (magic & 0xF) > 5) {
1406  av_log(avctx, AV_LOG_ERROR, "Wrong magic %08X\n", magic);
1407  return AVERROR_INVALIDDATA;
1408  }
1409 
1410  c->swapuv = magic == MKBETAG('G', '2', 'M', '2');
1411 
1412  while (bytestream2_get_bytes_left(&bc) > 5) {
1413  chunk_size = bytestream2_get_le32(&bc) - 1;
1414  chunk_type = bytestream2_get_byte(&bc);
1415  chunk_start = bytestream2_tell(&bc);
1416  if (chunk_size > bytestream2_get_bytes_left(&bc)) {
1417  av_log(avctx, AV_LOG_ERROR, "Invalid chunk size %"PRIu32" type %02X\n",
1418  chunk_size, chunk_type);
1419  break;
1420  }
1421  switch (chunk_type) {
1422  case DISPLAY_INFO:
1423  got_header =
1424  c->got_header = 0;
1425  if (chunk_size < 21) {
1426  av_log(avctx, AV_LOG_ERROR, "Invalid display info size %"PRIu32"\n",
1427  chunk_size);
1428  break;
1429  }
1430  c->width = bytestream2_get_be32(&bc);
1431  c->height = bytestream2_get_be32(&bc);
1432  if (c->width < 16 || c->height < 16) {
1433  av_log(avctx, AV_LOG_ERROR,
1434  "Invalid frame dimensions %dx%d\n",
1435  c->width, c->height);
1436  ret = AVERROR_INVALIDDATA;
1437  goto header_fail;
1438  }
1439  if (c->width != avctx->width || c->height != avctx->height) {
1440  ret = ff_set_dimensions(avctx, c->width, c->height);
1441  if (ret < 0)
1442  goto header_fail;
1443  }
1444  c->compression = bytestream2_get_be32(&bc);
1445  if (c->compression != 2 && c->compression != 3) {
1446  avpriv_report_missing_feature(avctx, "Compression method %d",
1447  c->compression);
1448  ret = AVERROR_PATCHWELCOME;
1449  goto header_fail;
1450  }
1451  c->tile_width = bytestream2_get_be32(&bc);
1452  c->tile_height = bytestream2_get_be32(&bc);
1453  if (c->tile_width <= 0 || c->tile_height <= 0 ||
1454  ((c->tile_width | c->tile_height) & 0xF) ||
1455  c->tile_width * (uint64_t)c->tile_height >= INT_MAX / 4 ||
1456  av_image_check_size2(c->tile_width, c->tile_height, avctx->max_pixels, avctx->pix_fmt, 0, avctx) < 0
1457  ) {
1458  av_log(avctx, AV_LOG_ERROR,
1459  "Invalid tile dimensions %dx%d\n",
1460  c->tile_width, c->tile_height);
1461  ret = AVERROR_INVALIDDATA;
1462  goto header_fail;
1463  }
1464  c->tiles_x = (c->width + c->tile_width - 1) / c->tile_width;
1465  c->tiles_y = (c->height + c->tile_height - 1) / c->tile_height;
1466  c->bpp = bytestream2_get_byte(&bc);
1467  if (c->bpp == 32) {
1468  if (bytestream2_get_bytes_left(&bc) < 16 ||
1469  (chunk_size - 21) < 16) {
1470  av_log(avctx, AV_LOG_ERROR,
1471  "Display info: missing bitmasks!\n");
1472  ret = AVERROR_INVALIDDATA;
1473  goto header_fail;
1474  }
1475  r_mask = bytestream2_get_be32(&bc);
1476  g_mask = bytestream2_get_be32(&bc);
1477  b_mask = bytestream2_get_be32(&bc);
1478  if (r_mask != 0xFF0000 || g_mask != 0xFF00 || b_mask != 0xFF) {
1480  "Bitmasks: R=%"PRIX32", G=%"PRIX32", B=%"PRIX32,
1481  r_mask, g_mask, b_mask);
1482  ret = AVERROR_PATCHWELCOME;
1483  goto header_fail;
1484  }
1485  } else {
1486  avpriv_request_sample(avctx, "bpp=%d", c->bpp);
1487  ret = AVERROR_PATCHWELCOME;
1488  goto header_fail;
1489  }
1490  if (g2m_init_buffers(c)) {
1491  ret = AVERROR(ENOMEM);
1492  goto header_fail;
1493  }
1494  got_header = 1;
1495  break;
1496  case TILE_DATA:
1497  if (!c->tiles_x || !c->tiles_y) {
1498  av_log(avctx, AV_LOG_WARNING,
1499  "No display info - skipping tile\n");
1500  break;
1501  }
1502  if (chunk_size < 2) {
1503  av_log(avctx, AV_LOG_ERROR, "Invalid tile data size %"PRIu32"\n",
1504  chunk_size);
1505  break;
1506  }
1507  c->tile_x = bytestream2_get_byte(&bc);
1508  c->tile_y = bytestream2_get_byte(&bc);
1509  if (c->tile_x >= c->tiles_x || c->tile_y >= c->tiles_y) {
1510  av_log(avctx, AV_LOG_ERROR,
1511  "Invalid tile pos %d,%d (in %dx%d grid)\n",
1512  c->tile_x, c->tile_y, c->tiles_x, c->tiles_y);
1513  break;
1514  }
1515  ret = 0;
1516  switch (c->compression) {
1517  case COMPR_EPIC_J_B:
1518  ret = epic_jb_decode_tile(c, c->tile_x, c->tile_y,
1519  buf + bytestream2_tell(&bc),
1520  chunk_size - 2, avctx);
1521  break;
1522  case COMPR_KEMPF_J_B:
1523  ret = kempf_decode_tile(c, c->tile_x, c->tile_y,
1524  buf + bytestream2_tell(&bc),
1525  chunk_size - 2);
1526  break;
1527  }
1528  if (ret && c->framebuf)
1529  av_log(avctx, AV_LOG_ERROR, "Error decoding tile %d,%d\n",
1530  c->tile_x, c->tile_y);
1531  break;
1532  case CURSOR_POS:
1533  if (chunk_size < 5) {
1534  av_log(avctx, AV_LOG_ERROR, "Invalid cursor pos size %"PRIu32"\n",
1535  chunk_size);
1536  break;
1537  }
1538  c->cursor_x = bytestream2_get_be16(&bc);
1539  c->cursor_y = bytestream2_get_be16(&bc);
1540  break;
1541  case CURSOR_SHAPE:
1542  if (chunk_size < 8) {
1543  av_log(avctx, AV_LOG_ERROR, "Invalid cursor data size %"PRIu32"\n",
1544  chunk_size);
1545  break;
1546  }
1547  bytestream2_init(&tbc, buf + bytestream2_tell(&bc),
1548  chunk_size - 4);
1549  g2m_load_cursor(avctx, c, &tbc);
1550  break;
1551  case CHUNK_CC:
1552  case CHUNK_CD:
1553  break;
1554  default:
1555  av_log(avctx, AV_LOG_WARNING, "Skipping chunk type %02d\n",
1556  chunk_type);
1557  }
1558 
1559  /* navigate to next chunk */
1560  bytestream2_skip(&bc, chunk_start + chunk_size - bytestream2_tell(&bc));
1561  }
1562  if (got_header)
1563  c->got_header = 1;
1564 
1565  if (c->width && c->height && c->framebuf) {
1566  if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
1567  return ret;
1568 
1569  pic->key_frame = got_header;
1570  pic->pict_type = got_header ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1571 
1572  for (i = 0; i < avctx->height; i++)
1573  memcpy(pic->data[0] + i * pic->linesize[0],
1574  c->framebuf + i * c->framebuf_stride,
1575  c->width * 3);
1576  g2m_paint_cursor(c, pic->data[0], pic->linesize[0]);
1577 
1578  *got_picture_ptr = 1;
1579  }
1580 
1581  return buf_size;
1582 
1583 header_fail:
1584  c->width =
1585  c->height = 0;
1586  c->tiles_x =
1587  c->tiles_y = 0;
1588  c->tile_width =
1589  c->tile_height = 0;
1590  return ret;
1591 }
1592 
1594 {
1595  G2MContext *const c = avctx->priv_data;
1596  int ret;
1597 
1598  if ((ret = jpg_init(avctx, &c->jc)) != 0) {
1599  av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
1600  jpg_free_context(&c->jc);
1601  return AVERROR(ENOMEM);
1602  }
1603 
1604  avctx->pix_fmt = AV_PIX_FMT_RGB24;
1605 
1606  // store original sizes and check against those if resize happens
1607  c->orig_width = avctx->width;
1608  c->orig_height = avctx->height;
1609 
1610  return 0;
1611 }
1612 
1614 {
1615  G2MContext *const c = avctx->priv_data;
1616 
1617  jpg_free_context(&c->jc);
1618 
1619  av_freep(&c->epic_buf_base);
1620  c->epic_buf = NULL;
1621  av_freep(&c->kempf_buf);
1622  av_freep(&c->kempf_flags);
1623  av_freep(&c->synth_tile);
1624  av_freep(&c->jpeg_tile);
1625  av_freep(&c->cursor);
1626  av_freep(&c->framebuf);
1627 
1628  return 0;
1629 }
1630 
1632  .name = "g2m",
1633  .long_name = NULL_IF_CONFIG_SMALL("Go2Meeting"),
1634  .type = AVMEDIA_TYPE_VIDEO,
1635  .id = AV_CODEC_ID_G2M,
1636  .priv_data_size = sizeof(G2MContext),
1637  .init = g2m_decode_init,
1638  .close = g2m_decode_end,
1640  .capabilities = AV_CODEC_CAP_DR1,
1641  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1642 };
#define LOAD_NEIGHBOURS(x)
Definition: g2meet.c:367
static int epic_predict_from_NW_NE(ePICContext *dc, int x, int y, int run, int tile_width, const uint32_t *curr_row, const uint32_t *above_row, uint32_t *pPix)
Definition: g2meet.c:755
int plane
Definition: avisynth_c.h:422
int tiles_y
Definition: g2meet.c:141
int cursor_hot_y
Definition: g2meet.c:158
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int epic_handle_edges(ePICContext *dc, int x, int y, const uint32_t *curr_row, const uint32_t *above_row, uint32_t *pPix)
Definition: g2meet.c:578
static int kempf_decode_tile(G2MContext *c, int tile_x, int tile_y, const uint8_t *src, int src_size)
Definition: g2meet.c:1055
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:135
static int epic_add_pixel_to_cache(ePICPixHash *hash, uint32_t key, uint32_t pix)
Definition: g2meet.c:444
static float alpha(float a)
#define flag(name)
Definition: cbs_h2645.c:346
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
uint8_t * kempf_flags
Definition: g2meet.c:152
int height
Definition: g2meet.c:138
ChunkType
Definition: g2meet.c:47
static av_cold void jpg_free_context(JPGContext *ctx)
Definition: g2meet.c:211
uint8_t * epic_buf
Definition: g2meet.c:148
uint8_t * kempf_buf
Definition: g2meet.c:152
misc image utilities
static int chunk_start(AVFormatContext *s)
Definition: webm_chunk.c:152
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:269
int width
Definition: g2meet.c:138
uint32_t pixel
Definition: g2meet.c:85
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
static int epic_predict_pixel(ePICContext *dc, uint8_t *rung, uint32_t *pPix, uint32_t pix)
Definition: g2meet.c:567
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:212
int got_header
Definition: g2meet.c:143
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int cursor_fmt
Definition: g2meet.c:156
uint8_t nw_pred_rung[256]
Definition: g2meet.c:109
Entropy Logarithmic-Scale binary arithmetic coder.
void(* clear_block)(int16_t *block)
Definition: blockdsp.h:36
static void yuv2rgb(uint8_t *out, int ridx, int Y, int U, int V)
Definition: g2meet.c:277
static int epic_decode_tile(ePICContext *dc, uint8_t *out, int tile_height, int tile_width, int stride)
Definition: g2meet.c:812
uint32_t stack[EPIC_PIX_STACK_SIZE]
Definition: g2meet.c:115
int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Definition: bitstream.c:268
Scantable.
Definition: idctdsp.h:31
int size
Definition: avcodec.h:1431
int next_run_pos
Definition: g2meet.c:103
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
Definition: blockdsp.c:60
#define EPIC_HASH_SIZE
Definition: g2meet.c:94
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
const char * key
VLC dc_vlc[2]
Definition: g2meet.c:124
uint8_t permutated[64]
Definition: idctdsp.h:33
uint8_t run
Definition: svq3.c:206
#define R_shift
Definition: g2meet.c:387
#define src
Definition: vp8dsp.c:254
static int g2m_init_buffers(G2MContext *c)
Definition: g2meet.c:1166
int stride
Definition: mace.c:144
int swapuv
Definition: g2meet.c:150
AVCodec.
Definition: avcodec.h:3408
int16_t block[6][64]
Definition: g2meet.c:126
MJPEG encoder and decoder.
static void jpg_unescape(const uint8_t *src, int src_size, uint8_t *dst, int *dst_size)
Definition: g2meet.c:223
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
#define B_shift
Definition: g2meet.c:389
#define N
Definition: af_mcompand.c:54
int tile_width
Definition: g2meet.c:140
static int16_t block[64]
Definition: dct.c:115
uint8_t rung
Definition: g2meet.c:86
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int bpp
Definition: g2meet.c:138
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, const uint8_t *bits_table, const uint8_t *val_table)
Definition: jpegtables.c:127
uint8_t
#define av_cold
Definition: attributes.h:82
int tile_x
Definition: g2meet.c:141
static int epic_cache_entries_for_pixel(const ePICPixHash *hash, uint32_t pix)
Definition: g2meet.c:465
float delta
#define Y
Definition: vf_boxblur.c:76
static ePICPixHashElem * epic_hash_find(const ePICPixHash *hash, uint32_t key)
Definition: g2meet.c:409
int cursor_h
Definition: g2meet.c:157
static int epic_decode_component_pred(ePICContext *dc, int N, int W, int NW)
Definition: g2meet.c:508
static int is_pixel_on_stack(const ePICContext *dc, uint32_t pix)
Definition: g2meet.c:495
ePICPixHash hash
Definition: g2meet.c:116
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
int framebuf_stride
Definition: g2meet.c:146
struct ePICPixListElem * list
Definition: g2meet.c:91
const char data[16]
Definition: mxf.c:90
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:112
#define height
uint8_t N_ctx_rung[512]
Definition: g2meet.c:108
uint8_t * data
Definition: avcodec.h:1430
bitstream reader API header.
uint8_t * framebuf
Definition: g2meet.c:145
static void epic_free_pixel_cache(ePICPixHash *hash)
Definition: g2meet.c:476
int version
Definition: g2meet.c:135
ScanTable scantable
Definition: g2meet.c:122
#define G_shift
Definition: g2meet.c:388
static av_cold int g2m_decode_init(AVCodecContext *avctx)
Definition: g2meet.c:1593
uint8_t W_ctx_rung[256]
Definition: g2meet.c:107
#define FFALIGN(x, a)
Definition: macros.h:48
VLC ac_vlc[2]
Definition: g2meet.c:124
#define av_log(a,...)
static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c)
Definition: g2meet.c:182
uint8_t hash[HASH_SIZE]
Definition: movenc.c:57
#define U(x)
Definition: vp56_arith.h:37
int err
Definition: elsdec.h:40
uint8_t * buf
Definition: g2meet.c:128
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define R
Definition: huffyuvdsp.h:34
ElsUnsignedRung unsigned_rung
Definition: g2meet.c:104
static const uint16_t mask[17]
Definition: lzw.c:38
static int g2m_load_cursor(AVCodecContext *avctx, G2MContext *c, GetByteContext *gb)
Definition: g2meet.c:1210
static av_cold int g2m_decode_end(AVCodecContext *avctx)
Definition: g2meet.c:1613
ePICContext ec
Definition: g2meet.c:132
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: jpegtables.c:70
void(* clear_blocks)(int16_t *blocks)
Definition: blockdsp.h:37
uint8_t runlen_zeroes[14]
Definition: g2meet.c:112
#define B
Definition: huffyuvdsp.h:32
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int old_tile_h
Definition: g2meet.c:149
int cursor_stride
Definition: g2meet.c:155
int orig_height
Definition: g2meet.c:139
static int epic_jb_decode_tile(G2MContext *c, int tile_x, int tile_y, const uint8_t *src, size_t src_size, AVCodecContext *avctx)
Definition: g2meet.c:875
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
uint16_t width
Definition: gdv.c:47
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
void ff_els_decoder_init(ElsDecCtx *ctx, const uint8_t *in, size_t data_size)
Definition: elsdec.c:247
static ePICPixHashElem * epic_hash_add(ePICPixHash *hash, uint32_t key)
Definition: g2meet.c:421
int64_t max_pixels
The number of pixels per image to maximally accept.
Definition: avcodec.h:3227
int orig_width
Definition: g2meet.c:139
Definition: vlc.h:26
static int jpg_decode_block(JPGContext *c, GetBitContext *gb, int plane, int16_t *block)
Definition: g2meet.c:240
uint8_t prev_row_rung[14]
Definition: g2meet.c:111
uint8_t * cursor
Definition: g2meet.c:154
int prev_dc[3]
Definition: g2meet.c:125
static FFFrameBucket * bucket(FFFrameQueue *fq, size_t idx)
Definition: framequeue.c:25
Compression
Definition: g2meet.c:56
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:301
static int jpg_decode_data(JPGContext *c, int width, int height, const uint8_t *src, int src_size, uint8_t *dst, int dst_stride, const uint8_t *mask, int mask_stride, int num_mbs, int swapuv)
Definition: g2meet.c:284
#define FFMIN(a, b)
Definition: common.h:96
static int g2m_decode_frame(AVCodecContext *avctx, void *data, int *got_picture_ptr, AVPacket *avpkt)
Definition: g2meet.c:1379
uint8_t * synth_tile
Definition: g2meet.c:148
int width
picture width / height.
Definition: avcodec.h:1690
uint8_t w
Definition: llviddspenc.c:38
AVFormatContext * ctx
Definition: movenc.c:48
int cursor_y
Definition: g2meet.c:157
static int epic_predict_pixel2(ePICContext *dc, uint8_t *rung, uint32_t *pPix, uint32_t pix)
Definition: g2meet.c:744
static void g2m_paint_cursor(G2MContext *c, uint8_t *dst, int stride)
Definition: g2meet.c:1332
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:563
int tile_y
Definition: g2meet.c:141
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:96
if(ret< 0)
Definition: vf_mcdeint.c:279
int old_tile_w
Definition: g2meet.c:149
uint8_t ne_pred_rung[256]
Definition: g2meet.c:110
static const float pred[4]
Definition: siprdata.h:259
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
ElsDecCtx els_ctx
Definition: g2meet.c:102
static uint32_t epic_decode_pixel_pred(ePICContext *dc, int x, int y, const uint32_t *curr_row, const uint32_t *above_row)
Definition: g2meet.c:515
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
#define TOSIGNED(val)
Definition: g2meet.c:506
#define EPIC_PIX_STACK_MAX
Definition: g2meet.c:45
AVCodec ff_g2m_decoder
Definition: g2meet.c:1631
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:163
Libavcodec external API header.
static int kempf_restore_buf(const uint8_t *src, int len, uint8_t *dst, int stride, const uint8_t *jpeg_tile, int tile_stride, int width, int height, const uint8_t *pal, int npal, int tidx)
Definition: g2meet.c:1020
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
#define APPLY_ALPHA(src, new, alpha)
Definition: g2meet.c:1329
int tile_stride
Definition: g2meet.c:149
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:464
int bucket_size[EPIC_HASH_SIZE]
Definition: g2meet.c:97
main external API structure.
Definition: avcodec.h:1518
static av_cold int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, int nb_codes, int is_ac)
Definition: g2meet.c:161
static int get_xbits(GetBitContext *s, int n)
Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
Definition: get_bits.h:226
JPGContext jc
Definition: g2meet.c:133
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1891
void * buf
Definition: avisynth_c.h:690
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
#define W(a, i, v)
Definition: jpegls.h:124
int compression
Definition: g2meet.c:137
static int djb2_hash(uint32_t key)
Definition: g2meet.c:392
int epic_buf_stride
Definition: g2meet.c:149
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
#define mid_pred
Definition: mathops.h:97
int cursor_w
Definition: g2meet.c:157
ePICPixHashElem * bucket[EPIC_HASH_SIZE]
Definition: g2meet.c:96
uint8_t runlen_one
Definition: g2meet.c:113
int cursor_hot_x
Definition: g2meet.c:158
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: jpegtables.c:99
unsigned ff_els_decode_unsigned(ElsDecCtx *ctx, ElsUnsignedRung *ur)
Definition: elsdec.c:350
static const uint8_t chroma_quant[64]
Definition: g2meet.c:72
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> dc
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: jpegtables.c:102
static int epic_decode_from_cache(ePICContext *dc, uint32_t W, uint32_t *pPix)
Definition: g2meet.c:783
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define SIZE_SPECIFIER
Definition: internal.h:262
struct ePICPixListElem * next
Definition: g2meet.c:84
int bucket_fill[EPIC_HASH_SIZE]
Definition: g2meet.c:98
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
int tiles_x
Definition: g2meet.c:141
uint8_t W_flag_rung
Definition: g2meet.c:105
uint8_t * epic_buf_base
Definition: g2meet.c:148
static void epic_hash_init(ePICPixHash *hash)
Definition: g2meet.c:404
common internal api header.
uint32_t pix_id
Definition: g2meet.c:90
uint8_t N_flag_rung
Definition: g2meet.c:106
#define G
Definition: huffyuvdsp.h:33
static double c[64]
BlockDSPContext bdsp
Definition: g2meet.c:120
int cursor_x
Definition: g2meet.c:157
int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of a plane of an image with...
Definition: imgutils.c:253
IDCTDSPContext idsp
Definition: g2meet.c:121
static const uint8_t luma_quant[64]
Definition: g2meet.c:61
#define MKBETAG(a, b, c, d)
Definition: common.h:367
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:773
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: jpegtables.c:73
void * priv_data
Definition: avcodec.h:1545
int tile_height
Definition: g2meet.c:140
#define av_free(p)
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: idctdsp.c:29
int stack_pos
Definition: g2meet.c:114
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:238
int len
#define UPDATE_NEIGHBOURS(x)
Definition: g2meet.c:378
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: jpegtables.c:75
#define EPIC_PIX_STACK_SIZE
Definition: g2meet.c:44
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
void ff_els_decoder_uninit(ElsUnsignedRung *rung)
Definition: elsdec.c:272
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:296
int ff_els_decode_bit(ElsDecCtx *ctx, uint8_t *rung)
Definition: elsdec.c:291
int old_width
Definition: g2meet.c:146
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:2204
FILE * out
Definition: movenc.c:54
static int epic_decode_run_length(ePICContext *dc, int x, int y, int tile_width, const uint32_t *curr_row, const uint32_t *above_row, const uint32_t *above2_row, uint32_t *pPix, int *pRun)
Definition: g2meet.c:609
#define av_freep(p)
void(* idct)(int16_t *block)
Definition: idctdsp.h:65
uint8_t * jpeg_tile
Definition: g2meet.c:148
int old_height
Definition: g2meet.c:146
This structure stores compressed data.
Definition: avcodec.h:1407
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:354
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
for(j=16;j >0;--j)
Predicted.
Definition: avutil.h:275
#define V
Definition: avdct.c:30
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:191
static uint8_t tmp[11]
Definition: aes_ctr.c:26