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