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