FFmpeg
agm.c
Go to the documentation of this file.
1 /*
2  * Amuse Graphics Movie decoder
3  *
4  * Copyright (c) 2018 Paul B Mahol
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 #include <string.h>
24 
25 #define BITSTREAM_READER_LE
26 
27 #include "libavutil/attributes.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/mem_internal.h"
30 
31 #include "avcodec.h"
32 #include "bytestream.h"
33 #include "codec_internal.h"
34 #include "copy_block.h"
35 #include "decode.h"
36 #include "get_bits.h"
37 #include "idctdsp.h"
38 #include "jpegquanttables.h"
39 
40 typedef struct MotionVector {
41  int16_t x, y;
42 } MotionVector;
43 
44 typedef struct AGMContext {
45  const AVClass *class;
49 
50  int key_frame;
53  int blocks_w;
54  int blocks_h;
55  int size[3];
56  int plus;
57  int dct;
58  int rgb;
59  unsigned flags;
60  unsigned fflags;
61 
62  uint8_t *output;
64  unsigned output_size;
65 
67  unsigned mvectors_size;
68 
70 
72 
75 
76  uint8_t permutated_scantable[64];
77  DECLARE_ALIGNED(32, int16_t, block)[64];
78 
79  int16_t *wblocks;
80  unsigned wblocks_size;
81 
82  int *map;
83  unsigned map_size;
84 
86 } AGMContext;
87 
88 static int read_code(GetBitContext *gb, int *oskip, int *level, int *map, int mode)
89 {
90  int len = 0, skip = 0, max;
91 
92  if (get_bits_left(gb) < 2)
93  return AVERROR_INVALIDDATA;
94 
95  if (show_bits(gb, 2)) {
96  switch (show_bits(gb, 4)) {
97  case 1:
98  case 9:
99  len = 1;
100  skip = 3;
101  break;
102  case 2:
103  len = 3;
104  skip = 4;
105  break;
106  case 3:
107  len = 7;
108  skip = 4;
109  break;
110  case 5:
111  case 13:
112  len = 2;
113  skip = 3;
114  break;
115  case 6:
116  len = 4;
117  skip = 4;
118  break;
119  case 7:
120  len = 8;
121  skip = 4;
122  break;
123  case 10:
124  len = 5;
125  skip = 4;
126  break;
127  case 11:
128  len = 9;
129  skip = 4;
130  break;
131  case 14:
132  len = 6;
133  skip = 4;
134  break;
135  case 15:
136  len = ((show_bits(gb, 5) & 0x10) | 0xA0) >> 4;
137  skip = 5;
138  break;
139  default:
140  return AVERROR_INVALIDDATA;
141  }
142 
143  skip_bits(gb, skip);
144  *level = get_bits(gb, len);
145  *map = 1;
146  *oskip = 0;
147  max = 1 << (len - 1);
148  if (*level < max)
149  *level = -(max + *level);
150  } else if (show_bits(gb, 3) & 4) {
151  skip_bits(gb, 3);
152  if (mode == 1) {
153  if (show_bits(gb, 4)) {
154  if (show_bits(gb, 4) == 1) {
155  skip_bits(gb, 4);
156  *oskip = get_bits(gb, 16);
157  } else {
158  *oskip = get_bits(gb, 4);
159  }
160  } else {
161  skip_bits(gb, 4);
162  *oskip = get_bits(gb, 10);
163  }
164  } else if (mode == 0) {
165  *oskip = get_bits(gb, 10);
166  }
167  *level = 0;
168  } else {
169  skip_bits(gb, 3);
170  if (mode == 0)
171  *oskip = get_bits(gb, 4);
172  else if (mode == 1)
173  *oskip = 0;
174  *level = 0;
175  }
176 
177  return 0;
178 }
179 
181  const int *quant_matrix, int *skip, int *dc_level)
182 {
183  const uint8_t *scantable = s->permutated_scantable;
184  int level, ret, map = 0;
185 
186  memset(s->wblocks, 0, s->wblocks_size);
187 
188  for (int i = 0; i < 64; i++) {
189  int16_t *block = s->wblocks + scantable[i];
190 
191  for (int j = 0; j < s->blocks_w;) {
192  if (*skip > 0) {
193  int rskip;
194 
195  rskip = FFMIN(*skip, s->blocks_w - j);
196  j += rskip;
197  if (i == 0) {
198  for (int k = 0; k < rskip; k++)
199  block[64 * k] = *dc_level * quant_matrix[0];
200  }
201  block += rskip * 64;
202  *skip -= rskip;
203  } else {
204  ret = read_code(gb, skip, &level, &map, s->flags & 1);
205  if (ret < 0)
206  return ret;
207 
208  if (i == 0)
209  *dc_level += level;
210 
211  block[0] = (i == 0 ? *dc_level : level) * quant_matrix[i];
212  block += 64;
213  j++;
214  }
215  }
216  }
217 
218  return 0;
219 }
220 
222  const int *quant_matrix, int *skip,
223  int *map)
224 {
225  const uint8_t *scantable = s->permutated_scantable;
226  int level, ret;
227 
228  memset(s->wblocks, 0, s->wblocks_size);
229  memset(s->map, 0, s->map_size);
230 
231  for (int i = 0; i < 64; i++) {
232  int16_t *block = s->wblocks + scantable[i];
233 
234  for (int j = 0; j < s->blocks_w;) {
235  if (*skip > 0) {
236  int rskip;
237 
238  rskip = FFMIN(*skip, s->blocks_w - j);
239  j += rskip;
240  block += rskip * 64;
241  *skip -= rskip;
242  } else {
243  ret = read_code(gb, skip, &level, &map[j], s->flags & 1);
244  if (ret < 0)
245  return ret;
246 
247  block[0] = level * quant_matrix[i];
248  block += 64;
249  j++;
250  }
251  }
252  }
253 
254  return 0;
255 }
256 
258  const int *quant_matrix, int *skip, int *dc_level)
259 {
260  const uint8_t *scantable = s->permutated_scantable;
261  const int offset = s->plus ? 0 : 1024;
262  int16_t *block = s->block;
263  int level, ret, map = 0;
264 
265  memset(block, 0, sizeof(s->block));
266 
267  if (*skip > 0) {
268  (*skip)--;
269  } else {
270  ret = read_code(gb, skip, &level, &map, s->flags & 1);
271  if (ret < 0)
272  return ret;
273  *dc_level += level;
274  }
275  block[scantable[0]] = offset + *dc_level * quant_matrix[0];
276 
277  for (int i = 1; i < 64;) {
278  if (*skip > 0) {
279  int rskip;
280 
281  rskip = FFMIN(*skip, 64 - i);
282  i += rskip;
283  *skip -= rskip;
284  } else {
285  ret = read_code(gb, skip, &level, &map, s->flags & 1);
286  if (ret < 0)
287  return ret;
288 
289  block[scantable[i]] = level * quant_matrix[i];
290  i++;
291  }
292  }
293 
294  return 0;
295 }
296 
298  const int *quant_matrix, AVFrame *frame,
299  int plane)
300 {
301  int ret, skip = 0, dc_level = 0;
302  const int offset = s->plus ? 0 : 1024;
303 
304  if ((ret = init_get_bits8(gb, s->gbyte.buffer, size)) < 0)
305  return ret;
306 
307  if (s->flags & 1) {
308  av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
309  64 * s->blocks_w * sizeof(*s->wblocks));
310  if (!s->wblocks)
311  return AVERROR(ENOMEM);
312 
313  for (int y = 0; y < s->blocks_h; y++) {
314  ret = decode_intra_blocks(s, gb, quant_matrix, &skip, &dc_level);
315  if (ret < 0)
316  return ret;
317 
318  for (int x = 0; x < s->blocks_w; x++) {
319  s->wblocks[64 * x] += offset;
320  s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
321  frame->linesize[plane], s->wblocks + 64 * x);
322  }
323  }
324  } else {
325  for (int y = 0; y < s->blocks_h; y++) {
326  for (int x = 0; x < s->blocks_w; x++) {
327  ret = decode_intra_block(s, gb, quant_matrix, &skip, &dc_level);
328  if (ret < 0)
329  return ret;
330 
331  s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
332  frame->linesize[plane], s->block);
333  }
334  }
335  }
336 
337  align_get_bits(gb);
338  if (get_bits_left(gb) < 0)
339  av_log(s->avctx, AV_LOG_WARNING, "overread\n");
340  if (get_bits_left(gb) > 0)
341  av_log(s->avctx, AV_LOG_WARNING, "underread: %d\n", get_bits_left(gb));
342 
343  return 0;
344 }
345 
347  const int *quant_matrix, int *skip,
348  int *map)
349 {
350  const uint8_t *scantable = s->permutated_scantable;
351  int16_t *block = s->block;
352  int level, ret;
353 
354  memset(block, 0, sizeof(s->block));
355 
356  for (int i = 0; i < 64;) {
357  if (*skip > 0) {
358  int rskip;
359 
360  rskip = FFMIN(*skip, 64 - i);
361  i += rskip;
362  *skip -= rskip;
363  } else {
364  ret = read_code(gb, skip, &level, map, s->flags & 1);
365  if (ret < 0)
366  return ret;
367 
368  block[scantable[i]] = level * quant_matrix[i];
369  i++;
370  }
371  }
372 
373  return 0;
374 }
375 
377  const int *quant_matrix, AVFrame *frame,
378  AVFrame *prev, int plane)
379 {
380  int ret, skip = 0;
381 
382  if ((ret = init_get_bits8(gb, s->gbyte.buffer, size)) < 0)
383  return ret;
384 
385  if (s->flags == 3) {
386  av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
387  64 * s->blocks_w * sizeof(*s->wblocks));
388  if (!s->wblocks)
389  return AVERROR(ENOMEM);
390 
391  av_fast_padded_malloc(&s->map, &s->map_size,
392  s->blocks_w * sizeof(*s->map));
393  if (!s->map)
394  return AVERROR(ENOMEM);
395 
396  for (int y = 0; y < s->blocks_h; y++) {
397  ret = decode_inter_blocks(s, gb, quant_matrix, &skip, s->map);
398  if (ret < 0)
399  return ret;
400 
401  for (int x = 0; x < s->blocks_w; x++) {
402  int shift = plane == 0;
403  int mvpos = (y >> shift) * (s->blocks_w >> shift) + (x >> shift);
404  int orig_mv_x = s->mvectors[mvpos].x;
405  int mv_x = s->mvectors[mvpos].x / (1 + !shift);
406  int mv_y = s->mvectors[mvpos].y / (1 + !shift);
407  int h = s->avctx->coded_height >> !shift;
408  int w = s->avctx->coded_width >> !shift;
409  int map = s->map[x];
410 
411  if (orig_mv_x >= -32) {
412  int src_y = (s->blocks_h - 1 - y) * 8 - mv_y;
413  int src_x = x * 8 + mv_x;
414  if (src_y < 0 || src_y + 8 > h ||
415  src_x < 0 || src_x + 8 > w)
416  return AVERROR_INVALIDDATA;
417 
418  copy_block8(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
419  prev->data[plane] + src_y * prev->linesize[plane] + src_x,
420  frame->linesize[plane], prev->linesize[plane], 8);
421  if (map) {
422  s->idsp.idct(s->wblocks + x * 64);
423  for (int i = 0; i < 64; i++)
424  s->wblocks[i + x * 64] = (s->wblocks[i + x * 64] + 1) & 0xFFFC;
425  s->idsp.add_pixels_clamped(&s->wblocks[x*64], frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
426  frame->linesize[plane]);
427  }
428  } else if (map) {
429  s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
430  frame->linesize[plane], s->wblocks + x * 64);
431  }
432  }
433  }
434  } else if (s->flags & 2) {
435  for (int y = 0; y < s->blocks_h; y++) {
436  for (int x = 0; x < s->blocks_w; x++) {
437  int shift = plane == 0;
438  int mvpos = (y >> shift) * (s->blocks_w >> shift) + (x >> shift);
439  int orig_mv_x = s->mvectors[mvpos].x;
440  int mv_x = s->mvectors[mvpos].x / (1 + !shift);
441  int mv_y = s->mvectors[mvpos].y / (1 + !shift);
442  int h = s->avctx->coded_height >> !shift;
443  int w = s->avctx->coded_width >> !shift;
444  int map = 0;
445 
446  ret = decode_inter_block(s, gb, quant_matrix, &skip, &map);
447  if (ret < 0)
448  return ret;
449 
450  if (orig_mv_x >= -32) {
451  int src_y = (s->blocks_h - 1 - y) * 8 - mv_y;
452  int src_x = x * 8 + mv_x;
453  if (src_y < 0 || src_y + 8 > h ||
454  src_x < 0 || src_x + 8 > w)
455  return AVERROR_INVALIDDATA;
456 
457  copy_block8(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
458  prev->data[plane] + src_y * prev->linesize[plane] + src_x,
459  frame->linesize[plane], prev->linesize[plane], 8);
460  if (map) {
461  s->idsp.idct(s->block);
462  for (int i = 0; i < 64; i++)
463  s->block[i] = (s->block[i] + 1) & 0xFFFC;
464  s->idsp.add_pixels_clamped(s->block, frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
465  frame->linesize[plane]);
466  }
467  } else if (map) {
468  s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
469  frame->linesize[plane], s->block);
470  }
471  }
472  }
473  } else if (s->flags & 1) {
474  av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
475  64 * s->blocks_w * sizeof(*s->wblocks));
476  if (!s->wblocks)
477  return AVERROR(ENOMEM);
478 
479  av_fast_padded_malloc(&s->map, &s->map_size,
480  s->blocks_w * sizeof(*s->map));
481  if (!s->map)
482  return AVERROR(ENOMEM);
483 
484  for (int y = 0; y < s->blocks_h; y++) {
485  ret = decode_inter_blocks(s, gb, quant_matrix, &skip, s->map);
486  if (ret < 0)
487  return ret;
488 
489  for (int x = 0; x < s->blocks_w; x++) {
490  if (!s->map[x])
491  continue;
492  s->idsp.idct_add(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
493  frame->linesize[plane], s->wblocks + 64 * x);
494  }
495  }
496  } else {
497  for (int y = 0; y < s->blocks_h; y++) {
498  for (int x = 0; x < s->blocks_w; x++) {
499  int map = 0;
500 
501  ret = decode_inter_block(s, gb, quant_matrix, &skip, &map);
502  if (ret < 0)
503  return ret;
504 
505  if (!map)
506  continue;
507  s->idsp.idct_add(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
508  frame->linesize[plane], s->block);
509  }
510  }
511  }
512 
513  align_get_bits(gb);
514  if (get_bits_left(gb) < 0)
515  av_log(s->avctx, AV_LOG_WARNING, "overread\n");
516  if (get_bits_left(gb) > 0)
517  av_log(s->avctx, AV_LOG_WARNING, "underread: %d\n", get_bits_left(gb));
518 
519  return 0;
520 }
521 
522 static void compute_quant_matrix(AGMContext *s, double qscale)
523 {
524  int luma[64], chroma[64];
525  double f = 1.0 - fabs(qscale);
526 
527  if (!s->key_frame && (s->flags & 2)) {
528  if (qscale >= 0.0) {
529  for (int i = 0; i < 64; i++) {
530  luma[i] = FFMAX(1, 16 * f);
531  chroma[i] = FFMAX(1, 16 * f);
532  }
533  } else {
534  for (int i = 0; i < 64; i++) {
535  luma[i] = FFMAX(1, 16 - qscale * 32);
536  chroma[i] = FFMAX(1, 16 - qscale * 32);
537  }
538  }
539  } else {
540  if (qscale >= 0.0) {
541  for (int i = 0; i < 64; i++) {
542  luma[i] = FFMAX(1, ff_mjpeg_std_luminance_quant_tbl [(i & 7) * 8 + (i >> 3)] * f);
543  chroma[i] = FFMAX(1, ff_mjpeg_std_chrominance_quant_tbl[(i & 7) * 8 + (i >> 3)] * f);
544  }
545  } else {
546  for (int i = 0; i < 64; i++) {
547  luma[i] = FFMAX(1, 255.0 - (255 - ff_mjpeg_std_luminance_quant_tbl [(i & 7) * 8 + (i >> 3)]) * f);
548  chroma[i] = FFMAX(1, 255.0 - (255 - ff_mjpeg_std_chrominance_quant_tbl[(i & 7) * 8 + (i >> 3)]) * f);
549  }
550  }
551  }
552 
553  for (int i = 0; i < 64; i++) {
554  int pos = ff_zigzag_direct[i];
555 
556  s->luma_quant_matrix[i] = luma[pos] * ((pos / 8) & 1 ? -1 : 1);
557  s->chroma_quant_matrix[i] = chroma[pos] * ((pos / 8) & 1 ? -1 : 1);
558  }
559 }
560 
562 {
563  uint8_t *dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
564  uint8_t r = 0, g = 0, b = 0;
565 
566  if (bytestream2_get_bytes_left(gbyte) < 3 * avctx->width * avctx->height)
567  return AVERROR_INVALIDDATA;
568 
569  for (int y = 0; y < avctx->height; y++) {
570  for (int x = 0; x < avctx->width; x++) {
571  dst[x*3+0] = bytestream2_get_byteu(gbyte) + r;
572  r = dst[x*3+0];
573  dst[x*3+1] = bytestream2_get_byteu(gbyte) + g;
574  g = dst[x*3+1];
575  dst[x*3+2] = bytestream2_get_byteu(gbyte) + b;
576  b = dst[x*3+2];
577  }
578  dst -= frame->linesize[0];
579  }
580 
581  return 0;
582 }
583 
584 av_always_inline static int fill_pixels(uint8_t **y0, uint8_t **y1,
585  uint8_t **u, uint8_t **v,
586  int ylinesize, int ulinesize, int vlinesize,
587  uint8_t *fill,
588  int *nx, int *ny, int *np, int w, int h)
589 {
590  uint8_t *y0dst = *y0;
591  uint8_t *y1dst = *y1;
592  uint8_t *udst = *u;
593  uint8_t *vdst = *v;
594  int x = *nx, y = *ny, pos = *np;
595 
596  if (pos == 0) {
597  y0dst[2*x+0] += fill[0];
598  y0dst[2*x+1] += fill[1];
599  y1dst[2*x+0] += fill[2];
600  y1dst[2*x+1] += fill[3];
601  pos++;
602  } else if (pos == 1) {
603  udst[x] += fill[0];
604  vdst[x] += fill[1];
605  x++;
606  if (x >= w) {
607  x = 0;
608  y++;
609  if (y >= h)
610  return 1;
611  y0dst -= 2*ylinesize;
612  y1dst -= 2*ylinesize;
613  udst -= ulinesize;
614  vdst -= vlinesize;
615  }
616  y0dst[2*x+0] += fill[2];
617  y0dst[2*x+1] += fill[3];
618  pos++;
619  } else if (pos == 2) {
620  y1dst[2*x+0] += fill[0];
621  y1dst[2*x+1] += fill[1];
622  udst[x] += fill[2];
623  vdst[x] += fill[3];
624  x++;
625  if (x >= w) {
626  x = 0;
627  y++;
628  if (y >= h)
629  return 1;
630  y0dst -= 2*ylinesize;
631  y1dst -= 2*ylinesize;
632  udst -= ulinesize;
633  vdst -= vlinesize;
634  }
635  pos = 0;
636  }
637 
638  *y0 = y0dst;
639  *y1 = y1dst;
640  *u = udst;
641  *v = vdst;
642  *np = pos;
643  *nx = x;
644  *ny = y;
645 
646  return 0;
647 }
648 
650 {
651  uint8_t *dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
652  int runlen, y = 0, x = 0;
653  uint8_t fill[4];
654  unsigned code;
655 
656  while (bytestream2_get_bytes_left(gbyte) > 0) {
657  code = bytestream2_peek_le32(gbyte);
658  runlen = code & 0xFFFFFF;
659 
660  if (code >> 24 == 0x77) {
661  bytestream2_skip(gbyte, 4);
662 
663  for (int i = 0; i < 4; i++)
664  fill[i] = bytestream2_get_byte(gbyte);
665 
666  while (runlen > 0) {
667  runlen--;
668 
669  for (int i = 0; i < 4; i++) {
670  dst[x] += fill[i];
671  x++;
672  if (x >= frame->width * 3) {
673  x = 0;
674  y++;
675  dst -= frame->linesize[0];
676  if (y >= frame->height)
677  return 0;
678  }
679  }
680  }
681  } else {
682  for (int i = 0; i < 4; i++)
683  fill[i] = bytestream2_get_byte(gbyte);
684 
685  for (int i = 0; i < 4; i++) {
686  dst[x] += fill[i];
687  x++;
688  if (x >= frame->width * 3) {
689  x = 0;
690  y++;
691  dst -= frame->linesize[0];
692  if (y >= frame->height)
693  return 0;
694  }
695  }
696  }
697  }
698 
699  return 0;
700 }
701 
703 {
704  uint8_t *y0dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
705  uint8_t *y1dst = y0dst - frame->linesize[0];
706  uint8_t *udst = frame->data[1] + ((avctx->height >> 1) - 1) * frame->linesize[1];
707  uint8_t *vdst = frame->data[2] + ((avctx->height >> 1) - 1) * frame->linesize[2];
708  int runlen, y = 0, x = 0, pos = 0;
709  uint8_t fill[4];
710  unsigned code;
711 
712  while (bytestream2_get_bytes_left(gbyte) > 0) {
713  code = bytestream2_peek_le32(gbyte);
714  runlen = code & 0xFFFFFF;
715 
716  if (code >> 24 == 0x77) {
717  bytestream2_skip(gbyte, 4);
718 
719  for (int i = 0; i < 4; i++)
720  fill[i] = bytestream2_get_byte(gbyte);
721 
722  while (runlen > 0) {
723  runlen--;
724 
725  if (fill_pixels(&y0dst, &y1dst, &udst, &vdst,
726  frame->linesize[0],
727  frame->linesize[1],
728  frame->linesize[2],
729  fill, &x, &y, &pos,
730  avctx->width / 2,
731  avctx->height / 2))
732  return 0;
733  }
734  } else {
735  for (int i = 0; i < 4; i++)
736  fill[i] = bytestream2_get_byte(gbyte);
737 
738  if (fill_pixels(&y0dst, &y1dst, &udst, &vdst,
739  frame->linesize[0],
740  frame->linesize[1],
741  frame->linesize[2],
742  fill, &x, &y, &pos,
743  avctx->width / 2,
744  avctx->height / 2))
745  return 0;
746  }
747  }
748 
749  return 0;
750 }
751 
753 {
754  uint8_t *y0dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
755  uint8_t *y1dst = y0dst - frame->linesize[0];
756  uint8_t *udst = frame->data[1] + ((avctx->height >> 1) - 1) * frame->linesize[1];
757  uint8_t *vdst = frame->data[2] + ((avctx->height >> 1) - 1) * frame->linesize[2];
758  uint8_t ly0 = 0, ly1 = 0, ly2 = 0, ly3 = 0, lu = 0, lv = 0;
759 
760  for (int y = 0; y < avctx->height / 2; y++) {
761  for (int x = 0; x < avctx->width / 2; x++) {
762  y0dst[x*2+0] = bytestream2_get_byte(gbyte) + ly0;
763  ly0 = y0dst[x*2+0];
764  y0dst[x*2+1] = bytestream2_get_byte(gbyte) + ly1;
765  ly1 = y0dst[x*2+1];
766  y1dst[x*2+0] = bytestream2_get_byte(gbyte) + ly2;
767  ly2 = y1dst[x*2+0];
768  y1dst[x*2+1] = bytestream2_get_byte(gbyte) + ly3;
769  ly3 = y1dst[x*2+1];
770  udst[x] = bytestream2_get_byte(gbyte) + lu;
771  lu = udst[x];
772  vdst[x] = bytestream2_get_byte(gbyte) + lv;
773  lv = vdst[x];
774  }
775 
776  y0dst -= 2*frame->linesize[0];
777  y1dst -= 2*frame->linesize[0];
778  udst -= frame->linesize[1];
779  vdst -= frame->linesize[2];
780  }
781 
782  return 0;
783 }
784 
786 {
787  AGMContext *s = avctx->priv_data;
788  int ret;
789 
790  compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
791 
792  s->blocks_w = avctx->coded_width >> 3;
793  s->blocks_h = avctx->coded_height >> 3;
794 
795  ret = decode_intra_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, 0);
796  if (ret < 0)
797  return ret;
798 
799  bytestream2_skip(&s->gbyte, s->size[0]);
800 
801  s->blocks_w = avctx->coded_width >> 4;
802  s->blocks_h = avctx->coded_height >> 4;
803 
804  ret = decode_intra_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, 2);
805  if (ret < 0)
806  return ret;
807 
808  bytestream2_skip(&s->gbyte, s->size[1]);
809 
810  s->blocks_w = avctx->coded_width >> 4;
811  s->blocks_h = avctx->coded_height >> 4;
812 
813  ret = decode_intra_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, 1);
814  if (ret < 0)
815  return ret;
816 
817  return 0;
818 }
819 
821 {
822  AGMContext *s = avctx->priv_data;
823  int nb_mvs = ((avctx->coded_height + 15) >> 4) * ((avctx->coded_width + 15) >> 4);
824  int ret, skip = 0, value, map;
825 
826  av_fast_padded_malloc(&s->mvectors, &s->mvectors_size,
827  nb_mvs * sizeof(*s->mvectors));
828  if (!s->mvectors)
829  return AVERROR(ENOMEM);
830 
831  if ((ret = init_get_bits8(gb, s->gbyte.buffer, bytestream2_get_bytes_left(&s->gbyte) -
832  (s->size[0] + s->size[1] + s->size[2]))) < 0)
833  return ret;
834 
835  memset(s->mvectors, 0, sizeof(*s->mvectors) * nb_mvs);
836 
837  for (int i = 0; i < nb_mvs; i++) {
838  ret = read_code(gb, &skip, &value, &map, 1);
839  if (ret < 0)
840  return ret;
841  s->mvectors[i].x = value;
842  i += skip;
843  }
844 
845  for (int i = 0; i < nb_mvs; i++) {
846  ret = read_code(gb, &skip, &value, &map, 1);
847  if (ret < 0)
848  return ret;
849  s->mvectors[i].y = value;
850  i += skip;
851  }
852 
853  if (get_bits_left(gb) <= 0)
854  return AVERROR_INVALIDDATA;
855  skip = (get_bits_count(gb) >> 3) + 1;
856  bytestream2_skip(&s->gbyte, skip);
857 
858  return 0;
859 }
860 
862  AVFrame *frame, AVFrame *prev)
863 {
864  AGMContext *s = avctx->priv_data;
865  int ret;
866 
867  compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
868 
869  if (s->flags & 2) {
870  ret = decode_motion_vectors(avctx, gb);
871  if (ret < 0)
872  return ret;
873  }
874 
875  s->blocks_w = avctx->coded_width >> 3;
876  s->blocks_h = avctx->coded_height >> 3;
877 
878  ret = decode_inter_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, prev, 0);
879  if (ret < 0)
880  return ret;
881 
882  bytestream2_skip(&s->gbyte, s->size[0]);
883 
884  s->blocks_w = avctx->coded_width >> 4;
885  s->blocks_h = avctx->coded_height >> 4;
886 
887  ret = decode_inter_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, prev, 2);
888  if (ret < 0)
889  return ret;
890 
891  bytestream2_skip(&s->gbyte, s->size[1]);
892 
893  s->blocks_w = avctx->coded_width >> 4;
894  s->blocks_h = avctx->coded_height >> 4;
895 
896  ret = decode_inter_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, prev, 1);
897  if (ret < 0)
898  return ret;
899 
900  return 0;
901 }
902 
903 typedef struct Node {
904  int parent;
905  int child[2];
906 } Node;
907 
908 static void get_tree_codes(uint32_t *codes, Node *nodes, int idx, uint32_t pfx, int bitpos)
909 {
910  if (idx < 256 && idx >= 0) {
911  codes[idx] = pfx;
912  } else if (idx >= 0) {
913  get_tree_codes(codes, nodes, nodes[idx].child[0], pfx + (0 << bitpos), bitpos + 1);
914  get_tree_codes(codes, nodes, nodes[idx].child[1], pfx + (1U << bitpos), bitpos + 1);
915  }
916 }
917 
918 static int make_new_tree(const uint8_t *bitlens, uint32_t *codes)
919 {
920  int zlcount = 0, curlen, idx, nindex, last, llast;
921  int blcounts[32] = { 0 };
922  int syms[8192];
923  Node nodes[512];
924  int node_idx[1024];
925  int old_idx[512];
926 
927  for (int i = 0; i < 256; i++) {
928  int bitlen = bitlens[i];
929  int blcount = blcounts[bitlen];
930 
931  zlcount += bitlen < 1;
932  syms[(bitlen << 8) + blcount] = i;
933  blcounts[bitlen]++;
934  }
935 
936  for (int i = 0; i < 512; i++) {
937  nodes[i].child[0] = -1;
938  nodes[i].child[1] = -1;
939  }
940 
941  for (int i = 0; i < 256; i++) {
942  node_idx[i] = 257 + i;
943  }
944 
945  curlen = 1;
946  node_idx[512] = 256;
947  last = 255;
948  nindex = 1;
949 
950  for (curlen = 1; curlen < 32; curlen++) {
951  if (blcounts[curlen] > 0) {
952  int max_zlcount = zlcount + blcounts[curlen];
953 
954  for (int i = 0; zlcount < 256 && zlcount < max_zlcount; zlcount++, i++) {
955  int p = node_idx[nindex - 1 + 512];
956  int ch = syms[256 * curlen + i];
957 
958  if (nindex <= 0)
959  return AVERROR_INVALIDDATA;
960 
961  if (nodes[p].child[0] == -1) {
962  nodes[p].child[0] = ch;
963  } else {
964  nodes[p].child[1] = ch;
965  nindex--;
966  }
967  nodes[ch].parent = p;
968  }
969  }
970  llast = last - 1;
971  idx = 0;
972  while (nindex > 0) {
973  int p, ch;
974 
975  last = llast - idx;
976  p = node_idx[nindex - 1 + 512];
977  ch = node_idx[last];
978  if (nodes[p].child[0] == -1) {
979  nodes[p].child[0] = ch;
980  } else {
981  nodes[p].child[1] = ch;
982  nindex--;
983  }
984  old_idx[idx] = ch;
985  nodes[ch].parent = p;
986  if (idx == llast)
987  goto next;
988  idx++;
989  if (nindex <= 0) {
990  for (int i = 0; i < idx; i++)
991  node_idx[512 + i] = old_idx[i];
992  }
993  }
994  nindex = idx;
995  }
996 
997 next:
998 
999  get_tree_codes(codes, nodes, 256, 0, 0);
1000  return 0;
1001 }
1002 
1003 static int build_huff(const uint8_t *bitlen, VLC *vlc)
1004 {
1005  uint32_t new_codes[256];
1006  uint8_t bits[256];
1007  uint8_t symbols[256];
1008  uint32_t codes[256];
1009  int nb_codes = 0;
1010 
1011  int ret = make_new_tree(bitlen, new_codes);
1012  if (ret < 0)
1013  return ret;
1014 
1015  for (int i = 0; i < 256; i++) {
1016  if (bitlen[i]) {
1017  bits[nb_codes] = bitlen[i];
1018  codes[nb_codes] = new_codes[i];
1019  symbols[nb_codes] = i;
1020  nb_codes++;
1021  }
1022  }
1023 
1024  ff_vlc_free(vlc);
1025  return ff_vlc_init_sparse(vlc, 13, nb_codes,
1026  bits, 1, 1,
1027  codes, 4, 4,
1028  symbols, 1, 1,
1029  VLC_INIT_LE);
1030 }
1031 
1032 static int decode_huffman2(AVCodecContext *avctx, int header, int size)
1033 {
1034  AGMContext *s = avctx->priv_data;
1035  GetBitContext *gb = &s->gb;
1036  uint8_t lens[256];
1037  int ret, x, len;
1038 
1039  if ((ret = init_get_bits8(gb, s->gbyte.buffer,
1040  bytestream2_get_bytes_left(&s->gbyte))) < 0)
1041  return ret;
1042 
1043  s->output_size = get_bits_long(gb, 32);
1044 
1045  if (s->output_size > avctx->width * avctx->height * 9LL + 10000)
1046  return AVERROR_INVALIDDATA;
1047 
1048  av_fast_padded_malloc(&s->output, &s->padded_output_size, s->output_size);
1049  if (!s->output)
1050  return AVERROR(ENOMEM);
1051 
1052  x = get_bits(gb, 1);
1053  len = 4 + get_bits(gb, 1);
1054  if (x) {
1055  int cb[8] = { 0 };
1056  int count = get_bits(gb, 3) + 1;
1057 
1058  for (int i = 0; i < count; i++)
1059  cb[i] = get_bits(gb, len);
1060 
1061  for (int i = 0; i < 256; i++) {
1062  int idx = get_bits(gb, 3);
1063  lens[i] = cb[idx];
1064  }
1065  } else {
1066  for (int i = 0; i < 256; i++)
1067  lens[i] = get_bits(gb, len);
1068  }
1069 
1070  if ((ret = build_huff(lens, &s->vlc)) < 0)
1071  return ret;
1072 
1073  x = 0;
1074  while (get_bits_left(gb) > 0 && x < s->output_size) {
1075  int val = get_vlc2(gb, s->vlc.table, s->vlc.bits, 3);
1076  if (val < 0)
1077  return AVERROR_INVALIDDATA;
1078  s->output[x++] = val;
1079  }
1080 
1081  return 0;
1082 }
1083 
1085  int *got_frame, AVPacket *avpkt)
1086 {
1087  AGMContext *s = avctx->priv_data;
1088  GetBitContext *gb = &s->gb;
1089  GetByteContext *gbyte = &s->gbyte;
1090  int w, h, width, height, header;
1091  unsigned compressed_size;
1092  long skip;
1093  int ret;
1094 
1095  if (!avpkt->size)
1096  return 0;
1097 
1098  bytestream2_init(gbyte, avpkt->data, avpkt->size);
1099 
1100  header = bytestream2_get_le32(gbyte);
1101  s->fflags = bytestream2_get_le32(gbyte);
1102  s->bitstream_size = s->fflags & 0x1FFFFFFF;
1103  s->fflags >>= 29;
1104  av_log(avctx, AV_LOG_DEBUG, "fflags: %X\n", s->fflags);
1105  if (avpkt->size < s->bitstream_size + 8)
1106  return AVERROR_INVALIDDATA;
1107 
1108  s->key_frame = (avpkt->flags & AV_PKT_FLAG_KEY);
1109  if (s->key_frame)
1110  frame->flags |= AV_FRAME_FLAG_KEY;
1111  else
1112  frame->flags &= ~AV_FRAME_FLAG_KEY;
1113  frame->pict_type = s->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1114 
1115  if (!s->key_frame) {
1116  if (!s->prev_frame->data[0]) {
1117  av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1118  return AVERROR_INVALIDDATA;
1119  }
1120  }
1121 
1122  if (header) {
1123  if (avctx->codec_tag == MKTAG('A', 'G', 'M', '0') ||
1124  avctx->codec_tag == MKTAG('A', 'G', 'M', '1'))
1125  return AVERROR_PATCHWELCOME;
1126  else
1127  ret = decode_huffman2(avctx, header, (avpkt->size - s->bitstream_size) - 8);
1128  if (ret < 0)
1129  return ret;
1130  bytestream2_init(gbyte, s->output, s->output_size);
1131  } else if (!s->dct) {
1132  bytestream2_skip(gbyte, 4);
1133  }
1134 
1135  if (s->dct) {
1136  s->flags = 0;
1137  w = bytestream2_get_le32(gbyte);
1138  h = bytestream2_get_le32(gbyte);
1139  if (w == INT32_MIN || h == INT32_MIN)
1140  return AVERROR_INVALIDDATA;
1141  if (w < 0) {
1142  w = -w;
1143  s->flags |= 2;
1144  }
1145  if (h < 0) {
1146  h = -h;
1147  s->flags |= 1;
1148  }
1149 
1150  width = avctx->width;
1151  height = avctx->height;
1152  if (w < width || h < height || w & 7 || h & 7)
1153  return AVERROR_INVALIDDATA;
1154 
1155  ret = ff_set_dimensions(avctx, w, h);
1156  if (ret < 0)
1157  return ret;
1158  avctx->width = width;
1159  avctx->height = height;
1160 
1161  s->compression = bytestream2_get_le32(gbyte);
1162  if (s->compression < 0 || s->compression > 100)
1163  return AVERROR_INVALIDDATA;
1164 
1165  for (int i = 0; i < 3; i++)
1166  s->size[i] = bytestream2_get_le32(gbyte);
1167  if (header) {
1168  compressed_size = s->output_size;
1169  skip = 8LL;
1170  } else {
1171  compressed_size = avpkt->size;
1172  skip = 32LL;
1173  }
1174  if (s->size[0] < 0 || s->size[1] < 0 || s->size[2] < 0 ||
1175  skip + s->size[0] + s->size[1] + s->size[2] > compressed_size) {
1176  return AVERROR_INVALIDDATA;
1177  }
1178  }
1179 
1180  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1181  return ret;
1182 
1183  if (frame->flags & AV_FRAME_FLAG_KEY) {
1184  if (!s->dct && !s->rgb)
1185  ret = decode_raw_intra(avctx, gbyte, frame);
1186  else if (!s->dct && s->rgb)
1187  ret = decode_raw_intra_rgb(avctx, gbyte, frame);
1188  else
1189  ret = decode_intra(avctx, gb, frame);
1190  } else {
1191  if (s->prev_frame-> width != frame->width ||
1192  s->prev_frame->height != frame->height)
1193  return AVERROR_INVALIDDATA;
1194 
1195  if (!(s->flags & 2)) {
1196  ret = av_frame_copy(frame, s->prev_frame);
1197  if (ret < 0)
1198  return ret;
1199  }
1200 
1201  if (s->dct) {
1202  ret = decode_inter(avctx, gb, frame, s->prev_frame);
1203  } else if (!s->dct && !s->rgb) {
1204  ret = decode_runlen(avctx, gbyte, frame);
1205  } else {
1206  ret = decode_runlen_rgb(avctx, gbyte, frame);
1207  }
1208  }
1209  if (ret < 0)
1210  return ret;
1211 
1212  if ((ret = av_frame_replace(s->prev_frame, frame)) < 0)
1213  return ret;
1214 
1215  frame->crop_top = avctx->coded_height - avctx->height;
1216  frame->crop_left = avctx->coded_width - avctx->width;
1217 
1218  *got_frame = 1;
1219 
1220  return avpkt->size;
1221 }
1222 
1224 {
1225  AGMContext *s = avctx->priv_data;
1226 
1227  s->rgb = avctx->codec_tag == MKTAG('A', 'G', 'M', '4');
1228  avctx->pix_fmt = s->rgb ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUV420P;
1229  s->avctx = avctx;
1230  s->plus = avctx->codec_tag == MKTAG('A', 'G', 'M', '3') ||
1231  avctx->codec_tag == MKTAG('A', 'G', 'M', '7');
1232 
1233  s->dct = avctx->codec_tag != MKTAG('A', 'G', 'M', '4') &&
1234  avctx->codec_tag != MKTAG('A', 'G', 'M', '5');
1235 
1236  if (!s->rgb && !s->dct) {
1237  if ((avctx->width & 1) || (avctx->height & 1))
1238  return AVERROR_INVALIDDATA;
1239  }
1240 
1241  avctx->idct_algo = FF_IDCT_SIMPLE;
1242  ff_idctdsp_init(&s->idsp, avctx);
1243  ff_permute_scantable(s->permutated_scantable, ff_zigzag_direct,
1244  s->idsp.idct_permutation);
1245 
1246  s->prev_frame = av_frame_alloc();
1247  if (!s->prev_frame)
1248  return AVERROR(ENOMEM);
1249 
1250  return 0;
1251 }
1252 
1254 {
1255  AGMContext *s = avctx->priv_data;
1256 
1257  av_frame_unref(s->prev_frame);
1258 }
1259 
1261 {
1262  AGMContext *s = avctx->priv_data;
1263 
1264  ff_vlc_free(&s->vlc);
1265  av_frame_free(&s->prev_frame);
1266  av_freep(&s->mvectors);
1267  s->mvectors_size = 0;
1268  av_freep(&s->wblocks);
1269  s->wblocks_size = 0;
1270  av_freep(&s->output);
1271  s->padded_output_size = 0;
1272  av_freep(&s->map);
1273  s->map_size = 0;
1274 
1275  return 0;
1276 }
1277 
1279  .p.name = "agm",
1280  CODEC_LONG_NAME("Amuse Graphics Movie"),
1281  .p.type = AVMEDIA_TYPE_VIDEO,
1282  .p.id = AV_CODEC_ID_AGM,
1283  .p.capabilities = AV_CODEC_CAP_DR1,
1284  .priv_data_size = sizeof(AGMContext),
1285  .init = decode_init,
1286  .close = decode_close,
1288  .flush = decode_flush,
1289  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1291 };
fill_pixels
static av_always_inline int fill_pixels(uint8_t **y0, uint8_t **y1, uint8_t **u, uint8_t **v, int ylinesize, int ulinesize, int vlinesize, uint8_t *fill, int *nx, int *ny, int *np, int w, int h)
Definition: agm.c:584
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
level
uint8_t level
Definition: svq3.c:208
AGMContext::map_size
unsigned map_size
Definition: agm.c:83
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
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:688
r
const char * r
Definition: vf_curves.c:127
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
AGMContext::wblocks_size
unsigned wblocks_size
Definition: agm.c:80
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition: bytestream.h:158
mem_internal.h
MotionVector::y
int16_t y
Definition: agm.c:41
Node
Definition: agm.c:903
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:247
GetByteContext
Definition: bytestream.h:33
AGMContext::blocks_h
int blocks_h
Definition: agm.c:54
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: agm.c:1223
AGMContext::compression
int compression
Definition: agm.c:52
decode_inter
static int decode_inter(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame, AVFrame *prev)
Definition: agm.c:861
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:424
av_cold
#define av_cold
Definition: attributes.h:119
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:254
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
mode
Definition: swscale.c:71
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:466
u
#define u(width, name, range_min, range_max)
Definition: cbs_apv.c:68
AVPacket::data
uint8_t * data
Definition: packet.h:603
b
#define b
Definition: input.c:43
chroma
static av_always_inline void chroma(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, int column, int mirror, int jobnr, int nb_jobs)
Definition: vf_waveform.c:1639
AGMContext::padded_output_size
unsigned padded_output_size
Definition: agm.c:63
FFCodec
Definition: codec_internal.h:127
AGMContext::chroma_quant_matrix
int chroma_quant_matrix[64]
Definition: agm.c:74
AGMContext::block
int16_t block[64]
Definition: agm.c:77
copy_block8
static void copy_block8(uint8_t *dst, const uint8_t *src, ptrdiff_t dstStride, ptrdiff_t srcStride, int h)
Definition: copy_block.h:47
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:76
max
#define max(a, b)
Definition: cuda_runtime.h:33
AGMContext::luma_quant_matrix
int luma_quant_matrix[64]
Definition: agm.c:73
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
decode_intra_blocks
static int decode_intra_blocks(AGMContext *s, GetBitContext *gb, const int *quant_matrix, int *skip, int *dc_level)
Definition: agm.c:180
AGMContext::vlc
VLC vlc
Definition: agm.c:69
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:91
decode_inter_block
static int decode_inter_block(AGMContext *s, GetBitContext *gb, const int *quant_matrix, int *skip, int *map)
Definition: agm.c:346
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:658
ff_idctdsp_init
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:228
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:487
av_always_inline
#define av_always_inline
Definition: attributes.h:76
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:383
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
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
AGMContext
Definition: agm.c:44
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
FF_IDCT_SIMPLE
#define FF_IDCT_SIMPLE
Definition: avcodec.h:1547
MotionVector::x
int16_t x
Definition: agm.c:41
AGMContext::prev_frame
AVFrame * prev_frame
Definition: agm.c:71
decode_huffman2
static int decode_huffman2(AVCodecContext *avctx, int header, int size)
Definition: agm.c:1032
GetBitContext
Definition: get_bits.h:109
val
static double val(void *priv, double ch)
Definition: aeval.c:77
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:619
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
read_code
static int read_code(GetBitContext *gb, int *oskip, int *level, int *map, int mode)
Definition: agm.c:88
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:544
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:681
AGMContext::bitstream_size
int bitstream_size
Definition: agm.c:51
AGMContext::idsp
IDCTDSPContext idsp
Definition: agm.c:85
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:347
s
#define s(width, name)
Definition: cbs_vp9.c:198
VLC_INIT_LE
#define VLC_INIT_LE
Definition: vlc.h:197
AGMContext::output
uint8_t * output
Definition: agm.c:62
g
const char * g
Definition: vf_curves.c:128
AGMContext::avctx
AVCodecContext * avctx
Definition: agm.c:46
AV_GET_BUFFER_FLAG_REF
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:415
bits
uint8_t bits
Definition: vp3data.h:128
Node::parent
int parent
Definition: agm.c:904
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
decode.h
get_bits.h
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
AGMContext::flags
unsigned flags
Definition: agm.c:59
decode_close
static av_cold int decode_close(AVCodecContext *avctx)
Definition: agm.c:1260
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:332
decode_inter_plane
static int decode_inter_plane(AGMContext *s, GetBitContext *gb, int size, const int *quant_matrix, AVFrame *frame, AVFrame *prev, int plane)
Definition: agm.c:376
if
if(ret)
Definition: filter_design.txt:179
ff_agm_decoder
const FFCodec ff_agm_decoder
Definition: agm.c:1278
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: agm.c:1084
decode_runlen_rgb
static int decode_runlen_rgb(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
Definition: agm.c:649
AGMContext::gb
GetBitContext gb
Definition: agm.c:47
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
AGMContext::mvectors_size
unsigned mvectors_size
Definition: agm.c:67
attributes.h
decode_motion_vectors
static int decode_motion_vectors(AVCodecContext *avctx, GetBitContext *gb)
Definition: agm.c:820
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
jpegquanttables.h
f
f
Definition: af_crystalizer.c:122
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1769
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:579
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
decode_intra_block
static int decode_intra_block(AGMContext *s, GetBitContext *gb, const int *quant_matrix, int *skip, int *dc_level)
Definition: agm.c:257
AVPacket::size
int size
Definition: packet.h:604
height
#define height
Definition: dsp.h:89
codec_internal.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:104
shift
static int shift(int a, int b)
Definition: bonk.c:261
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
av_frame_copy
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:711
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
Node::child
int child[2]
Definition: agm.c:905
FF_CODEC_CAP_EXPORTS_CROPPING
#define FF_CODEC_CAP_EXPORTS_CROPPING
The decoder sets the cropping fields in the output frames manually.
Definition: codec_internal.h:61
size
int size
Definition: twinvq_data.h:10344
decode_flush
static av_cold void decode_flush(AVCodecContext *avctx)
Definition: agm.c:1253
decode_runlen
static int decode_runlen(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
Definition: agm.c:702
AGMContext::plus
int plus
Definition: agm.c:56
header
static const uint8_t header[24]
Definition: sdr2.c:68
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:609
ff_vlc_init_sparse
int ff_vlc_init_sparse(VLC *vlc, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Build VLC decoding tables suitable for use with get_vlc2().
Definition: vlc.c:250
MotionVector
Definition: agm.c:40
AGMContext::fflags
unsigned fflags
Definition: agm.c:60
AGMContext::output_size
unsigned output_size
Definition: agm.c:64
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
copy_block.h
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:373
av_fast_padded_malloc
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:53
ff_mjpeg_std_chrominance_quant_tbl
const uint8_t ff_mjpeg_std_chrominance_quant_tbl[64]
Definition: jpegquanttables.c:45
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:496
AGMContext::rgb
int rgb
Definition: agm.c:58
AVCodecContext::idct_algo
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:1544
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::height
int height
Definition: avcodec.h:604
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:643
idctdsp.h
avcodec.h
AGMContext::gbyte
GetByteContext gbyte
Definition: agm.c:48
ff_zigzag_direct
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:137
AGMContext::size
int size[3]
Definition: agm.c:55
ff_vlc_free
void ff_vlc_free(VLC *vlc)
Definition: vlc.c:580
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
AGMContext::dct
int dct
Definition: agm.c:57
AGMContext::map
int * map
Definition: agm.c:82
decode_intra_plane
static int decode_intra_plane(AGMContext *s, GetBitContext *gb, int size, const int *quant_matrix, AVFrame *frame, int plane)
Definition: agm.c:297
align_get_bits
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:560
pos
unsigned int pos
Definition: spdifenc.c:414
IDCTDSPContext
Definition: idctdsp.h:43
AV_CODEC_ID_AGM
@ AV_CODEC_ID_AGM
Definition: codec_id.h:298
U
#define U(x)
Definition: vpx_arith.h:37
ff_mjpeg_std_luminance_quant_tbl
const uint8_t ff_mjpeg_std_luminance_quant_tbl[64]
Definition: jpegquanttables.c:35
av_frame_replace
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition: frame.c:376
AVCodecContext
main external API structure.
Definition: avcodec.h:443
decode_inter_blocks
static int decode_inter_blocks(AGMContext *s, GetBitContext *gb, const int *quant_matrix, int *skip, int *map)
Definition: agm.c:221
VLC
Definition: vlc.h:50
decode_raw_intra_rgb
static int decode_raw_intra_rgb(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
Definition: agm.c:561
AGMContext::blocks_w
int blocks_w
Definition: agm.c:53
build_huff
static int build_huff(const uint8_t *bitlen, VLC *vlc)
Definition: agm.c:1003
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:619
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
AGMContext::key_frame
int key_frame
Definition: agm.c:50
w
uint8_t w
Definition: llvidencdsp.c:39
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:468
AGMContext::wblocks
int16_t * wblocks
Definition: agm.c:79
AVPacket
This structure stores compressed data.
Definition: packet.h:580
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:470
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:604
AGMContext::permutated_scantable
uint8_t permutated_scantable[64]
Definition: agm.c:76
decode_raw_intra
static int decode_raw_intra(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
Definition: agm.c:752
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
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:511
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
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
h
h
Definition: vp9dsp_template.c:2070
AGMContext::mvectors
MotionVector * mvectors
Definition: agm.c:66
width
#define width
Definition: dsp.h:89
make_new_tree
static int make_new_tree(const uint8_t *bitlens, uint32_t *codes)
Definition: agm.c:918
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:383
get_tree_codes
static void get_tree_codes(uint32_t *codes, Node *nodes, int idx, uint32_t pfx, int bitpos)
Definition: agm.c:908
decode_intra
static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
Definition: agm.c:785
compute_quant_matrix
static void compute_quant_matrix(AGMContext *s, double qscale)
Definition: agm.c:522