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