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