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  if (y * 8 + mv_y < 0 || y * 8 + mv_y + 8 > h ||
413  x * 8 + mv_x < 0 || x * 8 + mv_x + 8 > w)
414  return AVERROR_INVALIDDATA;
415 
416  copy_block8(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
417  prev->data[plane] + ((s->blocks_h - 1 - y) * 8 - mv_y) * prev->linesize[plane] + (x * 8 + mv_x),
418  frame->linesize[plane], prev->linesize[plane], 8);
419  if (map) {
420  s->idsp.idct(s->wblocks + x * 64);
421  for (int i = 0; i < 64; i++)
422  s->wblocks[i + x * 64] = (s->wblocks[i + x * 64] + 1) & 0xFFFC;
423  s->idsp.add_pixels_clamped(&s->wblocks[x*64], frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
424  frame->linesize[plane]);
425  }
426  } else if (map) {
427  s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
428  frame->linesize[plane], s->wblocks + x * 64);
429  }
430  }
431  }
432  } else if (s->flags & 2) {
433  for (int y = 0; y < s->blocks_h; y++) {
434  for (int x = 0; x < s->blocks_w; x++) {
435  int shift = plane == 0;
436  int mvpos = (y >> shift) * (s->blocks_w >> shift) + (x >> shift);
437  int orig_mv_x = s->mvectors[mvpos].x;
438  int mv_x = s->mvectors[mvpos].x / (1 + !shift);
439  int mv_y = s->mvectors[mvpos].y / (1 + !shift);
440  int h = s->avctx->coded_height >> !shift;
441  int w = s->avctx->coded_width >> !shift;
442  int map = 0;
443 
444  ret = decode_inter_block(s, gb, quant_matrix, &skip, &map);
445  if (ret < 0)
446  return ret;
447 
448  if (orig_mv_x >= -32) {
449  if (y * 8 + mv_y < 0 || y * 8 + mv_y + 8 > h ||
450  x * 8 + mv_x < 0 || x * 8 + mv_x + 8 > w)
451  return AVERROR_INVALIDDATA;
452 
453  copy_block8(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
454  prev->data[plane] + ((s->blocks_h - 1 - y) * 8 - mv_y) * prev->linesize[plane] + (x * 8 + mv_x),
455  frame->linesize[plane], prev->linesize[plane], 8);
456  if (map) {
457  s->idsp.idct(s->block);
458  for (int i = 0; i < 64; i++)
459  s->block[i] = (s->block[i] + 1) & 0xFFFC;
460  s->idsp.add_pixels_clamped(s->block, frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
461  frame->linesize[plane]);
462  }
463  } else if (map) {
464  s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
465  frame->linesize[plane], s->block);
466  }
467  }
468  }
469  } else if (s->flags & 1) {
470  av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
471  64 * s->blocks_w * sizeof(*s->wblocks));
472  if (!s->wblocks)
473  return AVERROR(ENOMEM);
474 
475  av_fast_padded_malloc(&s->map, &s->map_size,
476  s->blocks_w * sizeof(*s->map));
477  if (!s->map)
478  return AVERROR(ENOMEM);
479 
480  for (int y = 0; y < s->blocks_h; y++) {
481  ret = decode_inter_blocks(s, gb, quant_matrix, &skip, s->map);
482  if (ret < 0)
483  return ret;
484 
485  for (int x = 0; x < s->blocks_w; x++) {
486  if (!s->map[x])
487  continue;
488  s->idsp.idct_add(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
489  frame->linesize[plane], s->wblocks + 64 * x);
490  }
491  }
492  } else {
493  for (int y = 0; y < s->blocks_h; y++) {
494  for (int x = 0; x < s->blocks_w; x++) {
495  int map = 0;
496 
497  ret = decode_inter_block(s, gb, quant_matrix, &skip, &map);
498  if (ret < 0)
499  return ret;
500 
501  if (!map)
502  continue;
503  s->idsp.idct_add(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
504  frame->linesize[plane], s->block);
505  }
506  }
507  }
508 
509  align_get_bits(gb);
510  if (get_bits_left(gb) < 0)
511  av_log(s->avctx, AV_LOG_WARNING, "overread\n");
512  if (get_bits_left(gb) > 0)
513  av_log(s->avctx, AV_LOG_WARNING, "underread: %d\n", get_bits_left(gb));
514 
515  return 0;
516 }
517 
518 static void compute_quant_matrix(AGMContext *s, double qscale)
519 {
520  int luma[64], chroma[64];
521  double f = 1.0 - fabs(qscale);
522 
523  if (!s->key_frame && (s->flags & 2)) {
524  if (qscale >= 0.0) {
525  for (int i = 0; i < 64; i++) {
526  luma[i] = FFMAX(1, 16 * f);
527  chroma[i] = FFMAX(1, 16 * f);
528  }
529  } else {
530  for (int i = 0; i < 64; i++) {
531  luma[i] = FFMAX(1, 16 - qscale * 32);
532  chroma[i] = FFMAX(1, 16 - qscale * 32);
533  }
534  }
535  } else {
536  if (qscale >= 0.0) {
537  for (int i = 0; i < 64; i++) {
538  luma[i] = FFMAX(1, ff_mjpeg_std_luminance_quant_tbl [(i & 7) * 8 + (i >> 3)] * f);
539  chroma[i] = FFMAX(1, ff_mjpeg_std_chrominance_quant_tbl[(i & 7) * 8 + (i >> 3)] * f);
540  }
541  } else {
542  for (int i = 0; i < 64; i++) {
543  luma[i] = FFMAX(1, 255.0 - (255 - ff_mjpeg_std_luminance_quant_tbl [(i & 7) * 8 + (i >> 3)]) * f);
544  chroma[i] = FFMAX(1, 255.0 - (255 - ff_mjpeg_std_chrominance_quant_tbl[(i & 7) * 8 + (i >> 3)]) * f);
545  }
546  }
547  }
548 
549  for (int i = 0; i < 64; i++) {
550  int pos = ff_zigzag_direct[i];
551 
552  s->luma_quant_matrix[i] = luma[pos] * ((pos / 8) & 1 ? -1 : 1);
553  s->chroma_quant_matrix[i] = chroma[pos] * ((pos / 8) & 1 ? -1 : 1);
554  }
555 }
556 
558 {
559  uint8_t *dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
560  uint8_t r = 0, g = 0, b = 0;
561 
562  if (bytestream2_get_bytes_left(gbyte) < 3 * avctx->width * avctx->height)
563  return AVERROR_INVALIDDATA;
564 
565  for (int y = 0; y < avctx->height; y++) {
566  for (int x = 0; x < avctx->width; x++) {
567  dst[x*3+0] = bytestream2_get_byteu(gbyte) + r;
568  r = dst[x*3+0];
569  dst[x*3+1] = bytestream2_get_byteu(gbyte) + g;
570  g = dst[x*3+1];
571  dst[x*3+2] = bytestream2_get_byteu(gbyte) + b;
572  b = dst[x*3+2];
573  }
574  dst -= frame->linesize[0];
575  }
576 
577  return 0;
578 }
579 
580 av_always_inline static int fill_pixels(uint8_t **y0, uint8_t **y1,
581  uint8_t **u, uint8_t **v,
582  int ylinesize, int ulinesize, int vlinesize,
583  uint8_t *fill,
584  int *nx, int *ny, int *np, int w, int h)
585 {
586  uint8_t *y0dst = *y0;
587  uint8_t *y1dst = *y1;
588  uint8_t *udst = *u;
589  uint8_t *vdst = *v;
590  int x = *nx, y = *ny, pos = *np;
591 
592  if (pos == 0) {
593  y0dst[2*x+0] += fill[0];
594  y0dst[2*x+1] += fill[1];
595  y1dst[2*x+0] += fill[2];
596  y1dst[2*x+1] += fill[3];
597  pos++;
598  } else if (pos == 1) {
599  udst[x] += fill[0];
600  vdst[x] += fill[1];
601  x++;
602  if (x >= w) {
603  x = 0;
604  y++;
605  if (y >= h)
606  return 1;
607  y0dst -= 2*ylinesize;
608  y1dst -= 2*ylinesize;
609  udst -= ulinesize;
610  vdst -= vlinesize;
611  }
612  y0dst[2*x+0] += fill[2];
613  y0dst[2*x+1] += fill[3];
614  pos++;
615  } else if (pos == 2) {
616  y1dst[2*x+0] += fill[0];
617  y1dst[2*x+1] += fill[1];
618  udst[x] += fill[2];
619  vdst[x] += fill[3];
620  x++;
621  if (x >= w) {
622  x = 0;
623  y++;
624  if (y >= h)
625  return 1;
626  y0dst -= 2*ylinesize;
627  y1dst -= 2*ylinesize;
628  udst -= ulinesize;
629  vdst -= vlinesize;
630  }
631  pos = 0;
632  }
633 
634  *y0 = y0dst;
635  *y1 = y1dst;
636  *u = udst;
637  *v = vdst;
638  *np = pos;
639  *nx = x;
640  *ny = y;
641 
642  return 0;
643 }
644 
646 {
647  uint8_t *dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
648  int runlen, y = 0, x = 0;
649  uint8_t fill[4];
650  unsigned code;
651 
652  while (bytestream2_get_bytes_left(gbyte) > 0) {
653  code = bytestream2_peek_le32(gbyte);
654  runlen = code & 0xFFFFFF;
655 
656  if (code >> 24 == 0x77) {
657  bytestream2_skip(gbyte, 4);
658 
659  for (int i = 0; i < 4; i++)
660  fill[i] = bytestream2_get_byte(gbyte);
661 
662  while (runlen > 0) {
663  runlen--;
664 
665  for (int i = 0; i < 4; i++) {
666  dst[x] += fill[i];
667  x++;
668  if (x >= frame->width * 3) {
669  x = 0;
670  y++;
671  dst -= frame->linesize[0];
672  if (y >= frame->height)
673  return 0;
674  }
675  }
676  }
677  } else {
678  for (int i = 0; i < 4; i++)
679  fill[i] = bytestream2_get_byte(gbyte);
680 
681  for (int i = 0; i < 4; i++) {
682  dst[x] += fill[i];
683  x++;
684  if (x >= frame->width * 3) {
685  x = 0;
686  y++;
687  dst -= frame->linesize[0];
688  if (y >= frame->height)
689  return 0;
690  }
691  }
692  }
693  }
694 
695  return 0;
696 }
697 
699 {
700  uint8_t *y0dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
701  uint8_t *y1dst = y0dst - frame->linesize[0];
702  uint8_t *udst = frame->data[1] + ((avctx->height >> 1) - 1) * frame->linesize[1];
703  uint8_t *vdst = frame->data[2] + ((avctx->height >> 1) - 1) * frame->linesize[2];
704  int runlen, y = 0, x = 0, pos = 0;
705  uint8_t fill[4];
706  unsigned code;
707 
708  while (bytestream2_get_bytes_left(gbyte) > 0) {
709  code = bytestream2_peek_le32(gbyte);
710  runlen = code & 0xFFFFFF;
711 
712  if (code >> 24 == 0x77) {
713  bytestream2_skip(gbyte, 4);
714 
715  for (int i = 0; i < 4; i++)
716  fill[i] = bytestream2_get_byte(gbyte);
717 
718  while (runlen > 0) {
719  runlen--;
720 
721  if (fill_pixels(&y0dst, &y1dst, &udst, &vdst,
722  frame->linesize[0],
723  frame->linesize[1],
724  frame->linesize[2],
725  fill, &x, &y, &pos,
726  avctx->width / 2,
727  avctx->height / 2))
728  return 0;
729  }
730  } else {
731  for (int i = 0; i < 4; i++)
732  fill[i] = bytestream2_get_byte(gbyte);
733 
734  if (fill_pixels(&y0dst, &y1dst, &udst, &vdst,
735  frame->linesize[0],
736  frame->linesize[1],
737  frame->linesize[2],
738  fill, &x, &y, &pos,
739  avctx->width / 2,
740  avctx->height / 2))
741  return 0;
742  }
743  }
744 
745  return 0;
746 }
747 
749 {
750  uint8_t *y0dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
751  uint8_t *y1dst = y0dst - frame->linesize[0];
752  uint8_t *udst = frame->data[1] + ((avctx->height >> 1) - 1) * frame->linesize[1];
753  uint8_t *vdst = frame->data[2] + ((avctx->height >> 1) - 1) * frame->linesize[2];
754  uint8_t ly0 = 0, ly1 = 0, ly2 = 0, ly3 = 0, lu = 0, lv = 0;
755 
756  for (int y = 0; y < avctx->height / 2; y++) {
757  for (int x = 0; x < avctx->width / 2; x++) {
758  y0dst[x*2+0] = bytestream2_get_byte(gbyte) + ly0;
759  ly0 = y0dst[x*2+0];
760  y0dst[x*2+1] = bytestream2_get_byte(gbyte) + ly1;
761  ly1 = y0dst[x*2+1];
762  y1dst[x*2+0] = bytestream2_get_byte(gbyte) + ly2;
763  ly2 = y1dst[x*2+0];
764  y1dst[x*2+1] = bytestream2_get_byte(gbyte) + ly3;
765  ly3 = y1dst[x*2+1];
766  udst[x] = bytestream2_get_byte(gbyte) + lu;
767  lu = udst[x];
768  vdst[x] = bytestream2_get_byte(gbyte) + lv;
769  lv = vdst[x];
770  }
771 
772  y0dst -= 2*frame->linesize[0];
773  y1dst -= 2*frame->linesize[0];
774  udst -= frame->linesize[1];
775  vdst -= frame->linesize[2];
776  }
777 
778  return 0;
779 }
780 
782 {
783  AGMContext *s = avctx->priv_data;
784  int ret;
785 
786  compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
787 
788  s->blocks_w = avctx->coded_width >> 3;
789  s->blocks_h = avctx->coded_height >> 3;
790 
791  ret = decode_intra_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, 0);
792  if (ret < 0)
793  return ret;
794 
795  bytestream2_skip(&s->gbyte, s->size[0]);
796 
797  s->blocks_w = avctx->coded_width >> 4;
798  s->blocks_h = avctx->coded_height >> 4;
799 
800  ret = decode_intra_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, 2);
801  if (ret < 0)
802  return ret;
803 
804  bytestream2_skip(&s->gbyte, s->size[1]);
805 
806  s->blocks_w = avctx->coded_width >> 4;
807  s->blocks_h = avctx->coded_height >> 4;
808 
809  ret = decode_intra_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, 1);
810  if (ret < 0)
811  return ret;
812 
813  return 0;
814 }
815 
817 {
818  AGMContext *s = avctx->priv_data;
819  int nb_mvs = ((avctx->coded_height + 15) >> 4) * ((avctx->coded_width + 15) >> 4);
820  int ret, skip = 0, value, map;
821 
822  av_fast_padded_malloc(&s->mvectors, &s->mvectors_size,
823  nb_mvs * sizeof(*s->mvectors));
824  if (!s->mvectors)
825  return AVERROR(ENOMEM);
826 
827  if ((ret = init_get_bits8(gb, s->gbyte.buffer, bytestream2_get_bytes_left(&s->gbyte) -
828  (s->size[0] + s->size[1] + s->size[2]))) < 0)
829  return ret;
830 
831  memset(s->mvectors, 0, sizeof(*s->mvectors) * nb_mvs);
832 
833  for (int i = 0; i < nb_mvs; i++) {
834  ret = read_code(gb, &skip, &value, &map, 1);
835  if (ret < 0)
836  return ret;
837  s->mvectors[i].x = value;
838  i += skip;
839  }
840 
841  for (int i = 0; i < nb_mvs; i++) {
842  ret = read_code(gb, &skip, &value, &map, 1);
843  if (ret < 0)
844  return ret;
845  s->mvectors[i].y = value;
846  i += skip;
847  }
848 
849  if (get_bits_left(gb) <= 0)
850  return AVERROR_INVALIDDATA;
851  skip = (get_bits_count(gb) >> 3) + 1;
852  bytestream2_skip(&s->gbyte, skip);
853 
854  return 0;
855 }
856 
858  AVFrame *frame, AVFrame *prev)
859 {
860  AGMContext *s = avctx->priv_data;
861  int ret;
862 
863  compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
864 
865  if (s->flags & 2) {
866  ret = decode_motion_vectors(avctx, gb);
867  if (ret < 0)
868  return ret;
869  }
870 
871  s->blocks_w = avctx->coded_width >> 3;
872  s->blocks_h = avctx->coded_height >> 3;
873 
874  ret = decode_inter_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, prev, 0);
875  if (ret < 0)
876  return ret;
877 
878  bytestream2_skip(&s->gbyte, s->size[0]);
879 
880  s->blocks_w = avctx->coded_width >> 4;
881  s->blocks_h = avctx->coded_height >> 4;
882 
883  ret = decode_inter_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, prev, 2);
884  if (ret < 0)
885  return ret;
886 
887  bytestream2_skip(&s->gbyte, s->size[1]);
888 
889  s->blocks_w = avctx->coded_width >> 4;
890  s->blocks_h = avctx->coded_height >> 4;
891 
892  ret = decode_inter_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, prev, 1);
893  if (ret < 0)
894  return ret;
895 
896  return 0;
897 }
898 
899 typedef struct Node {
900  int parent;
901  int child[2];
902 } Node;
903 
904 static void get_tree_codes(uint32_t *codes, Node *nodes, int idx, uint32_t pfx, int bitpos)
905 {
906  if (idx < 256 && idx >= 0) {
907  codes[idx] = pfx;
908  } else if (idx >= 0) {
909  get_tree_codes(codes, nodes, nodes[idx].child[0], pfx + (0 << bitpos), bitpos + 1);
910  get_tree_codes(codes, nodes, nodes[idx].child[1], pfx + (1U << bitpos), bitpos + 1);
911  }
912 }
913 
914 static int make_new_tree(const uint8_t *bitlens, uint32_t *codes)
915 {
916  int zlcount = 0, curlen, idx, nindex, last, llast;
917  int blcounts[32] = { 0 };
918  int syms[8192];
919  Node nodes[512];
920  int node_idx[1024];
921  int old_idx[512];
922 
923  for (int i = 0; i < 256; i++) {
924  int bitlen = bitlens[i];
925  int blcount = blcounts[bitlen];
926 
927  zlcount += bitlen < 1;
928  syms[(bitlen << 8) + blcount] = i;
929  blcounts[bitlen]++;
930  }
931 
932  for (int i = 0; i < 512; i++) {
933  nodes[i].child[0] = -1;
934  nodes[i].child[1] = -1;
935  }
936 
937  for (int i = 0; i < 256; i++) {
938  node_idx[i] = 257 + i;
939  }
940 
941  curlen = 1;
942  node_idx[512] = 256;
943  last = 255;
944  nindex = 1;
945 
946  for (curlen = 1; curlen < 32; curlen++) {
947  if (blcounts[curlen] > 0) {
948  int max_zlcount = zlcount + blcounts[curlen];
949 
950  for (int i = 0; zlcount < 256 && zlcount < max_zlcount; zlcount++, i++) {
951  int p = node_idx[nindex - 1 + 512];
952  int ch = syms[256 * curlen + i];
953 
954  if (nindex <= 0)
955  return AVERROR_INVALIDDATA;
956 
957  if (nodes[p].child[0] == -1) {
958  nodes[p].child[0] = ch;
959  } else {
960  nodes[p].child[1] = ch;
961  nindex--;
962  }
963  nodes[ch].parent = p;
964  }
965  }
966  llast = last - 1;
967  idx = 0;
968  while (nindex > 0) {
969  int p, ch;
970 
971  last = llast - idx;
972  p = node_idx[nindex - 1 + 512];
973  ch = node_idx[last];
974  if (nodes[p].child[0] == -1) {
975  nodes[p].child[0] = ch;
976  } else {
977  nodes[p].child[1] = ch;
978  nindex--;
979  }
980  old_idx[idx] = ch;
981  nodes[ch].parent = p;
982  if (idx == llast)
983  goto next;
984  idx++;
985  if (nindex <= 0) {
986  for (int i = 0; i < idx; i++)
987  node_idx[512 + i] = old_idx[i];
988  }
989  }
990  nindex = idx;
991  }
992 
993 next:
994 
995  get_tree_codes(codes, nodes, 256, 0, 0);
996  return 0;
997 }
998 
999 static int build_huff(const uint8_t *bitlen, VLC *vlc)
1000 {
1001  uint32_t new_codes[256];
1002  uint8_t bits[256];
1003  uint8_t symbols[256];
1004  uint32_t codes[256];
1005  int nb_codes = 0;
1006 
1007  int ret = make_new_tree(bitlen, new_codes);
1008  if (ret < 0)
1009  return ret;
1010 
1011  for (int i = 0; i < 256; i++) {
1012  if (bitlen[i]) {
1013  bits[nb_codes] = bitlen[i];
1014  codes[nb_codes] = new_codes[i];
1015  symbols[nb_codes] = i;
1016  nb_codes++;
1017  }
1018  }
1019 
1020  ff_vlc_free(vlc);
1021  return ff_vlc_init_sparse(vlc, 13, nb_codes,
1022  bits, 1, 1,
1023  codes, 4, 4,
1024  symbols, 1, 1,
1025  VLC_INIT_LE);
1026 }
1027 
1028 static int decode_huffman2(AVCodecContext *avctx, int header, int size)
1029 {
1030  AGMContext *s = avctx->priv_data;
1031  GetBitContext *gb = &s->gb;
1032  uint8_t lens[256];
1033  int ret, x, len;
1034 
1035  if ((ret = init_get_bits8(gb, s->gbyte.buffer,
1036  bytestream2_get_bytes_left(&s->gbyte))) < 0)
1037  return ret;
1038 
1039  s->output_size = get_bits_long(gb, 32);
1040 
1041  if (s->output_size > avctx->width * avctx->height * 9LL + 10000)
1042  return AVERROR_INVALIDDATA;
1043 
1044  av_fast_padded_malloc(&s->output, &s->padded_output_size, s->output_size);
1045  if (!s->output)
1046  return AVERROR(ENOMEM);
1047 
1048  x = get_bits(gb, 1);
1049  len = 4 + get_bits(gb, 1);
1050  if (x) {
1051  int cb[8] = { 0 };
1052  int count = get_bits(gb, 3) + 1;
1053 
1054  for (int i = 0; i < count; i++)
1055  cb[i] = get_bits(gb, len);
1056 
1057  for (int i = 0; i < 256; i++) {
1058  int idx = get_bits(gb, 3);
1059  lens[i] = cb[idx];
1060  }
1061  } else {
1062  for (int i = 0; i < 256; i++)
1063  lens[i] = get_bits(gb, len);
1064  }
1065 
1066  if ((ret = build_huff(lens, &s->vlc)) < 0)
1067  return ret;
1068 
1069  x = 0;
1070  while (get_bits_left(gb) > 0 && x < s->output_size) {
1071  int val = get_vlc2(gb, s->vlc.table, s->vlc.bits, 3);
1072  if (val < 0)
1073  return AVERROR_INVALIDDATA;
1074  s->output[x++] = val;
1075  }
1076 
1077  return 0;
1078 }
1079 
1081  int *got_frame, AVPacket *avpkt)
1082 {
1083  AGMContext *s = avctx->priv_data;
1084  GetBitContext *gb = &s->gb;
1085  GetByteContext *gbyte = &s->gbyte;
1086  int w, h, width, height, header;
1087  unsigned compressed_size;
1088  long skip;
1089  int ret;
1090 
1091  if (!avpkt->size)
1092  return 0;
1093 
1094  bytestream2_init(gbyte, avpkt->data, avpkt->size);
1095 
1096  header = bytestream2_get_le32(gbyte);
1097  s->fflags = bytestream2_get_le32(gbyte);
1098  s->bitstream_size = s->fflags & 0x1FFFFFFF;
1099  s->fflags >>= 29;
1100  av_log(avctx, AV_LOG_DEBUG, "fflags: %X\n", s->fflags);
1101  if (avpkt->size < s->bitstream_size + 8)
1102  return AVERROR_INVALIDDATA;
1103 
1104  s->key_frame = (avpkt->flags & AV_PKT_FLAG_KEY);
1105  if (s->key_frame)
1106  frame->flags |= AV_FRAME_FLAG_KEY;
1107  else
1108  frame->flags &= ~AV_FRAME_FLAG_KEY;
1109  frame->pict_type = s->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1110 
1111  if (!s->key_frame) {
1112  if (!s->prev_frame->data[0]) {
1113  av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1114  return AVERROR_INVALIDDATA;
1115  }
1116  }
1117 
1118  if (header) {
1119  if (avctx->codec_tag == MKTAG('A', 'G', 'M', '0') ||
1120  avctx->codec_tag == MKTAG('A', 'G', 'M', '1'))
1121  return AVERROR_PATCHWELCOME;
1122  else
1123  ret = decode_huffman2(avctx, header, (avpkt->size - s->bitstream_size) - 8);
1124  if (ret < 0)
1125  return ret;
1126  bytestream2_init(gbyte, s->output, s->output_size);
1127  } else if (!s->dct) {
1128  bytestream2_skip(gbyte, 4);
1129  }
1130 
1131  if (s->dct) {
1132  s->flags = 0;
1133  w = bytestream2_get_le32(gbyte);
1134  h = bytestream2_get_le32(gbyte);
1135  if (w == INT32_MIN || h == INT32_MIN)
1136  return AVERROR_INVALIDDATA;
1137  if (w < 0) {
1138  w = -w;
1139  s->flags |= 2;
1140  }
1141  if (h < 0) {
1142  h = -h;
1143  s->flags |= 1;
1144  }
1145 
1146  width = avctx->width;
1147  height = avctx->height;
1148  if (w < width || h < height || w & 7 || h & 7)
1149  return AVERROR_INVALIDDATA;
1150 
1151  ret = ff_set_dimensions(avctx, w, h);
1152  if (ret < 0)
1153  return ret;
1154  avctx->width = width;
1155  avctx->height = height;
1156 
1157  s->compression = bytestream2_get_le32(gbyte);
1158  if (s->compression < 0 || s->compression > 100)
1159  return AVERROR_INVALIDDATA;
1160 
1161  for (int i = 0; i < 3; i++)
1162  s->size[i] = bytestream2_get_le32(gbyte);
1163  if (header) {
1164  compressed_size = s->output_size;
1165  skip = 8LL;
1166  } else {
1167  compressed_size = avpkt->size;
1168  skip = 32LL;
1169  }
1170  if (s->size[0] < 0 || s->size[1] < 0 || s->size[2] < 0 ||
1171  skip + s->size[0] + s->size[1] + s->size[2] > compressed_size) {
1172  return AVERROR_INVALIDDATA;
1173  }
1174  }
1175 
1176  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1177  return ret;
1178 
1179  if (frame->flags & AV_FRAME_FLAG_KEY) {
1180  if (!s->dct && !s->rgb)
1181  ret = decode_raw_intra(avctx, gbyte, frame);
1182  else if (!s->dct && s->rgb)
1183  ret = decode_raw_intra_rgb(avctx, gbyte, frame);
1184  else
1185  ret = decode_intra(avctx, gb, frame);
1186  } else {
1187  if (s->prev_frame-> width != frame->width ||
1188  s->prev_frame->height != frame->height)
1189  return AVERROR_INVALIDDATA;
1190 
1191  if (!(s->flags & 2)) {
1192  ret = av_frame_copy(frame, s->prev_frame);
1193  if (ret < 0)
1194  return ret;
1195  }
1196 
1197  if (s->dct) {
1198  ret = decode_inter(avctx, gb, frame, s->prev_frame);
1199  } else if (!s->dct && !s->rgb) {
1200  ret = decode_runlen(avctx, gbyte, frame);
1201  } else {
1202  ret = decode_runlen_rgb(avctx, gbyte, frame);
1203  }
1204  }
1205  if (ret < 0)
1206  return ret;
1207 
1208  if ((ret = av_frame_replace(s->prev_frame, frame)) < 0)
1209  return ret;
1210 
1211  frame->crop_top = avctx->coded_height - avctx->height;
1212  frame->crop_left = avctx->coded_width - avctx->width;
1213 
1214  *got_frame = 1;
1215 
1216  return avpkt->size;
1217 }
1218 
1220 {
1221  AGMContext *s = avctx->priv_data;
1222 
1223  s->rgb = avctx->codec_tag == MKTAG('A', 'G', 'M', '4');
1224  avctx->pix_fmt = s->rgb ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUV420P;
1225  s->avctx = avctx;
1226  s->plus = avctx->codec_tag == MKTAG('A', 'G', 'M', '3') ||
1227  avctx->codec_tag == MKTAG('A', 'G', 'M', '7');
1228 
1229  s->dct = avctx->codec_tag != MKTAG('A', 'G', 'M', '4') &&
1230  avctx->codec_tag != MKTAG('A', 'G', 'M', '5');
1231 
1232  if (!s->rgb && !s->dct) {
1233  if ((avctx->width & 1) || (avctx->height & 1))
1234  return AVERROR_INVALIDDATA;
1235  }
1236 
1237  avctx->idct_algo = FF_IDCT_SIMPLE;
1238  ff_idctdsp_init(&s->idsp, avctx);
1239  ff_permute_scantable(s->permutated_scantable, ff_zigzag_direct,
1240  s->idsp.idct_permutation);
1241 
1242  s->prev_frame = av_frame_alloc();
1243  if (!s->prev_frame)
1244  return AVERROR(ENOMEM);
1245 
1246  return 0;
1247 }
1248 
1250 {
1251  AGMContext *s = avctx->priv_data;
1252 
1253  av_frame_unref(s->prev_frame);
1254 }
1255 
1257 {
1258  AGMContext *s = avctx->priv_data;
1259 
1260  ff_vlc_free(&s->vlc);
1261  av_frame_free(&s->prev_frame);
1262  av_freep(&s->mvectors);
1263  s->mvectors_size = 0;
1264  av_freep(&s->wblocks);
1265  s->wblocks_size = 0;
1266  av_freep(&s->output);
1267  s->padded_output_size = 0;
1268  av_freep(&s->map);
1269  s->map_size = 0;
1270 
1271  return 0;
1272 }
1273 
1275  .p.name = "agm",
1276  CODEC_LONG_NAME("Amuse Graphics Movie"),
1277  .p.type = AVMEDIA_TYPE_VIDEO,
1278  .p.id = AV_CODEC_ID_AGM,
1279  .p.capabilities = AV_CODEC_CAP_DR1,
1280  .priv_data_size = sizeof(AGMContext),
1281  .init = decode_init,
1282  .close = decode_close,
1284  .flush = decode_flush,
1285  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1287 };
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:580
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:42
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:689
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:899
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:1219
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:857
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:419
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:250
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:56
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
w
uint8_t w
Definition: llviddspenc.c:38
u
#define u(width, name, range_min, range_max)
Definition: cbs_apv.c:68
AVPacket::data
uint8_t * data
Definition: packet.h:558
b
#define b
Definition: input.c:42
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)
Check that the provided frame dimensions are valid and set them on the codec context.
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:613
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:448
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:379
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:135
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:333
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:1529
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:1028
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:607
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
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:539
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:642
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:346
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:411
bits
uint8_t bits
Definition: vp3data.h:128
Node::parent
int parent
Definition: agm.c:900
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:1256
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:331
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:1274
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:1080
decode_runlen_rgb
static int decode_runlen_rgb(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
Definition: agm.c:645
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
decode_motion_vectors
static int decode_motion_vectors(AVCodecContext *avctx, GetBitContext *gb)
Definition: agm.c:816
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:646
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:1720
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
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:559
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
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:424
Node::child
int child[2]
Definition: agm.c:901
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:60
size
int size
Definition: twinvq_data.h:10344
decode_flush
static av_cold void decode_flush(AVCodecContext *avctx)
Definition: agm.c:1249
decode_runlen
static int decode_runlen(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
Definition: agm.c:698
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
attributes.h
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:564
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
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
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:369
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
av_always_inline
#define av_always_inline
Definition: attributes.h:49
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:1526
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:592
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:631
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:555
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:431
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:557
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:999
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.h:29
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:607
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
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:456
AGMContext::wblocks
int16_t * wblocks
Definition: agm.c:79
AVPacket
This structure stores compressed data.
Definition: packet.h:535
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:592
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:748
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:472
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:914
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:904
decode_intra
static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
Definition: agm.c:781
compute_quant_matrix
static void compute_quant_matrix(AGMContext *s, double qscale)
Definition: agm.c:518