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