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