2 * Amuse Graphics Movie decoder
4 * Copyright (c) 2018 Paul B Mahol
6 * This file is part of FFmpeg.
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.
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.
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
27 #define BITSTREAM_READER_LE
30 #include "bytestream.h"
31 #include "copy_block.h"
36 static const uint8_t unscaled_luma[64] = {
37 16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19,
38 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56,
39 14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56,
40 68,109,103, 77, 24, 35, 55, 64, 81,104,113, 92,
41 49, 64, 78, 87,103,121,120,101, 72, 92, 95, 98,
45 static const uint8_t unscaled_chroma[64] = {
46 17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66,
47 99, 99, 99, 99, 24, 26, 56, 99, 99, 99, 99, 99,
48 47, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
49 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
50 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
54 typedef struct MotionVector {
58 typedef struct AGMContext {
60 AVCodecContext *avctx;
77 MotionVector *mvectors;
78 unsigned mvectors_size;
84 int luma_quant_matrix[64];
85 int chroma_quant_matrix[64];
88 DECLARE_ALIGNED(32, int16_t, block)[64];
91 unsigned wblocks_size;
99 static int read_code(GetBitContext *gb, int *oskip, int *level, int *map, int mode)
101 int len = 0, skip = 0, max;
103 if (show_bits(gb, 2)) {
104 switch (show_bits(gb, 4)) {
144 len = ((show_bits(gb, 5) & 0x10) | 0xA0) >> 4;
148 return AVERROR_INVALIDDATA;
152 *level = get_bits(gb, len);
155 max = 1 << (len - 1);
157 *level = -(max + *level);
158 } else if (show_bits(gb, 3) & 4) {
161 if (show_bits(gb, 4)) {
162 if (show_bits(gb, 4) == 1) {
164 *oskip = get_bits(gb, 16);
166 *oskip = get_bits(gb, 4);
170 *oskip = get_bits(gb, 10);
172 } else if (mode == 0) {
173 *oskip = get_bits(gb, 10);
179 *oskip = get_bits(gb, 4);
188 static int decode_intra_blocks(AGMContext *s, GetBitContext *gb,
189 const int *quant_matrix, int *skip, int *dc_level)
191 const uint8_t *scantable = s->scantable.permutated;
192 int level, ret, map = 0;
194 memset(s->wblocks, 0, s->wblocks_size);
196 for (int i = 0; i < 64; i++) {
197 int16_t *block = s->wblocks + scantable[i];
199 for (int j = 0; j < s->blocks_w;) {
203 rskip = FFMIN(*skip, s->blocks_w - j);
206 for (int k = 0; k < rskip; k++)
207 block[64 * k] = *dc_level * quant_matrix[0];
212 ret = read_code(gb, skip, &level, &map, s->flags & 1);
219 block[0] = (i == 0 ? *dc_level : level) * quant_matrix[i];
229 static int decode_inter_blocks(AGMContext *s, GetBitContext *gb,
230 const int *quant_matrix, int *skip,
233 const uint8_t *scantable = s->scantable.permutated;
236 memset(s->wblocks, 0, s->wblocks_size);
237 memset(s->map, 0, s->map_size);
239 for (int i = 0; i < 64; i++) {
240 int16_t *block = s->wblocks + scantable[i];
242 for (int j = 0; j < s->blocks_w;) {
246 rskip = FFMIN(*skip, s->blocks_w - j);
251 ret = read_code(gb, skip, &level, &map[j], s->flags & 1);
255 block[0] = level * quant_matrix[i];
265 static int decode_intra_block(AGMContext *s, GetBitContext *gb,
266 const int *quant_matrix, int *skip, int *dc_level)
268 const uint8_t *scantable = s->scantable.permutated;
269 const int offset = s->plus ? 0 : 1024;
270 int16_t *block = s->block;
271 int level, ret, map = 0;
273 memset(block, 0, sizeof(s->block));
278 ret = read_code(gb, skip, &level, &map, s->flags & 1);
283 block[scantable[0]] = offset + *dc_level * quant_matrix[0];
285 for (int i = 1; i < 64;) {
289 rskip = FFMIN(*skip, 64 - i);
293 ret = read_code(gb, skip, &level, &map, s->flags & 1);
297 block[scantable[i]] = level * quant_matrix[i];
305 static int decode_intra_plane(AGMContext *s, GetBitContext *gb, int size,
306 const int *quant_matrix, AVFrame *frame,
309 int ret, skip = 0, dc_level = 0;
310 const int offset = s->plus ? 0 : 1024;
312 if ((ret = init_get_bits8(gb, s->gbyte.buffer, size)) < 0)
316 av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
317 64 * s->blocks_w * sizeof(*s->wblocks));
319 return AVERROR(ENOMEM);
321 for (int y = 0; y < s->blocks_h; y++) {
322 ret = decode_intra_blocks(s, gb, quant_matrix, &skip, &dc_level);
326 for (int x = 0; x < s->blocks_w; x++) {
327 s->wblocks[64 * x] += offset;
328 s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
329 frame->linesize[plane], s->wblocks + 64 * x);
333 for (int y = 0; y < s->blocks_h; y++) {
334 for (int x = 0; x < s->blocks_w; x++) {
335 ret = decode_intra_block(s, gb, quant_matrix, &skip, &dc_level);
339 s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
340 frame->linesize[plane], s->block);
346 if (get_bits_left(gb) < 0)
347 av_log(s->avctx, AV_LOG_WARNING, "overread\n");
348 if (get_bits_left(gb) > 0)
349 av_log(s->avctx, AV_LOG_WARNING, "underread: %d\n", get_bits_left(gb));
354 static int decode_inter_block(AGMContext *s, GetBitContext *gb,
355 const int *quant_matrix, int *skip,
358 const uint8_t *scantable = s->scantable.permutated;
359 int16_t *block = s->block;
362 memset(block, 0, sizeof(s->block));
364 for (int i = 0; i < 64;) {
368 rskip = FFMIN(*skip, 64 - i);
372 ret = read_code(gb, skip, &level, map, s->flags & 1);
376 block[scantable[i]] = level * quant_matrix[i];
384 static int decode_inter_plane(AGMContext *s, GetBitContext *gb, int size,
385 const int *quant_matrix, AVFrame *frame,
386 AVFrame *prev, int plane)
390 if ((ret = init_get_bits8(gb, s->gbyte.buffer, size)) < 0)
394 av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
395 64 * s->blocks_w * sizeof(*s->wblocks));
397 return AVERROR(ENOMEM);
399 av_fast_padded_malloc(&s->map, &s->map_size,
400 s->blocks_w * sizeof(*s->map));
402 return AVERROR(ENOMEM);
404 for (int y = 0; y < s->blocks_h; y++) {
405 ret = decode_inter_blocks(s, gb, quant_matrix, &skip, s->map);
409 for (int x = 0; x < s->blocks_w; x++) {
410 int shift = plane == 0;
411 int mvpos = (y >> shift) * (s->blocks_w >> shift) + (x >> shift);
412 int orig_mv_x = s->mvectors[mvpos].x;
413 int mv_x = s->mvectors[mvpos].x / (1 + !shift);
414 int mv_y = s->mvectors[mvpos].y / (1 + !shift);
415 int h = s->avctx->coded_height >> !shift;
416 int w = s->avctx->coded_width >> !shift;
419 if (orig_mv_x >= -32) {
420 if (y * 8 + mv_y < 0 || y * 8 + mv_y >= h ||
421 x * 8 + mv_x < 0 || x * 8 + mv_x >= w)
422 return AVERROR_INVALIDDATA;
424 copy_block8(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
425 prev->data[plane] + ((s->blocks_h - 1 - y) * 8 - mv_y) * prev->linesize[plane] + (x * 8 + mv_x),
426 frame->linesize[plane], prev->linesize[plane], 8);
428 s->idsp.idct(s->wblocks + x * 64);
429 for (int i = 0; i < 64; i++)
430 s->wblocks[i + x * 64] = (s->wblocks[i + x * 64] + 1) & 0xFFFC;
431 s->idsp.add_pixels_clamped(&s->wblocks[x*64], frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
432 frame->linesize[plane]);
435 s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
436 frame->linesize[plane], s->wblocks + x * 64);
440 } else if (s->flags & 2) {
441 for (int y = 0; y < s->blocks_h; y++) {
442 for (int x = 0; x < s->blocks_w; x++) {
443 int shift = plane == 0;
444 int mvpos = (y >> shift) * (s->blocks_w >> shift) + (x >> shift);
445 int orig_mv_x = s->mvectors[mvpos].x;
446 int mv_x = s->mvectors[mvpos].x / (1 + !shift);
447 int mv_y = s->mvectors[mvpos].y / (1 + !shift);
448 int h = s->avctx->coded_height >> !shift;
449 int w = s->avctx->coded_width >> !shift;
452 ret = decode_inter_block(s, gb, quant_matrix, &skip, &map);
456 if (orig_mv_x >= -32) {
457 if (y * 8 + mv_y < 0 || y * 8 + mv_y >= h ||
458 x * 8 + mv_x < 0 || x * 8 + mv_x >= w)
459 return AVERROR_INVALIDDATA;
461 copy_block8(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
462 prev->data[plane] + ((s->blocks_h - 1 - y) * 8 - mv_y) * prev->linesize[plane] + (x * 8 + mv_x),
463 frame->linesize[plane], prev->linesize[plane], 8);
465 s->idsp.idct(s->block);
466 for (int i = 0; i < 64; i++)
467 s->block[i] = (s->block[i] + 1) & 0xFFFC;
468 s->idsp.add_pixels_clamped(s->block, frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
469 frame->linesize[plane]);
472 s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
473 frame->linesize[plane], s->block);
477 } else if (s->flags & 1) {
478 av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
479 64 * s->blocks_w * sizeof(*s->wblocks));
481 return AVERROR(ENOMEM);
483 av_fast_padded_malloc(&s->map, &s->map_size,
484 s->blocks_w * sizeof(*s->map));
486 return AVERROR(ENOMEM);
488 for (int y = 0; y < s->blocks_h; y++) {
489 ret = decode_inter_blocks(s, gb, quant_matrix, &skip, s->map);
493 for (int x = 0; x < s->blocks_w; x++) {
496 s->idsp.idct_add(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
497 frame->linesize[plane], s->wblocks + 64 * x);
501 for (int y = 0; y < s->blocks_h; y++) {
502 for (int x = 0; x < s->blocks_w; x++) {
505 ret = decode_inter_block(s, gb, quant_matrix, &skip, &map);
511 s->idsp.idct_add(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
512 frame->linesize[plane], s->block);
518 if (get_bits_left(gb) < 0)
519 av_log(s->avctx, AV_LOG_WARNING, "overread\n");
520 if (get_bits_left(gb) > 0)
521 av_log(s->avctx, AV_LOG_WARNING, "underread: %d\n", get_bits_left(gb));
526 static void compute_quant_matrix(AGMContext *s, double qscale)
528 int luma[64], chroma[64];
529 double f = 1.0 - fabs(qscale);
531 if (!s->key_frame && (s->flags & 2)) {
533 for (int i = 0; i < 64; i++) {
534 luma[i] = FFMAX(1, 16 * f);
535 chroma[i] = FFMAX(1, 16 * f);
538 for (int i = 0; i < 64; i++) {
539 luma[i] = FFMAX(1, 16 - qscale * 32);
540 chroma[i] = FFMAX(1, 16 - qscale * 32);
545 for (int i = 0; i < 64; i++) {
546 luma[i] = FFMAX(1, unscaled_luma [(i & 7) * 8 + (i >> 3)] * f);
547 chroma[i] = FFMAX(1, unscaled_chroma[(i & 7) * 8 + (i >> 3)] * f);
550 for (int i = 0; i < 64; i++) {
551 luma[i] = FFMAX(1, 255.0 - (255 - unscaled_luma [(i & 7) * 8 + (i >> 3)]) * f);
552 chroma[i] = FFMAX(1, 255.0 - (255 - unscaled_chroma[(i & 7) * 8 + (i >> 3)]) * f);
557 for (int i = 0; i < 64; i++) {
558 int pos = ff_zigzag_direct[i];
560 s->luma_quant_matrix[i] = luma[pos] * ((pos / 8) & 1 ? -1 : 1);
561 s->chroma_quant_matrix[i] = chroma[pos] * ((pos / 8) & 1 ? -1 : 1);
565 static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
567 AGMContext *s = avctx->priv_data;
570 compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
572 s->blocks_w = avctx->coded_width >> 3;
573 s->blocks_h = avctx->coded_height >> 3;
575 ret = decode_intra_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, 0);
579 bytestream2_skip(&s->gbyte, s->size[0]);
581 s->blocks_w = avctx->coded_width >> 4;
582 s->blocks_h = avctx->coded_height >> 4;
584 ret = decode_intra_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, 2);
588 bytestream2_skip(&s->gbyte, s->size[1]);
590 s->blocks_w = avctx->coded_width >> 4;
591 s->blocks_h = avctx->coded_height >> 4;
593 ret = decode_intra_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, 1);
600 static int decode_motion_vectors(AVCodecContext *avctx, GetBitContext *gb)
602 AGMContext *s = avctx->priv_data;
603 int nb_mvs = ((avctx->height + 15) >> 4) * ((avctx->width + 15) >> 4);
604 int ret, skip = 0, value, map;
606 av_fast_padded_malloc(&s->mvectors, &s->mvectors_size,
607 nb_mvs * sizeof(*s->mvectors));
609 return AVERROR(ENOMEM);
611 if ((ret = init_get_bits8(gb, s->gbyte.buffer, bytestream2_get_bytes_left(&s->gbyte) -
612 (s->size[0] + s->size[1] + s->size[2]))) < 0)
615 memset(s->mvectors, 0, sizeof(*s->mvectors) * nb_mvs);
617 for (int i = 0; i < nb_mvs; i++) {
618 ret = read_code(gb, &skip, &value, &map, 1);
621 s->mvectors[i].x = value;
625 for (int i = 0; i < nb_mvs; i++) {
626 ret = read_code(gb, &skip, &value, &map, 1);
629 s->mvectors[i].y = value;
633 if (get_bits_left(gb) <= 0)
634 return AVERROR_INVALIDDATA;
635 skip = (get_bits_count(gb) >> 3) + 1;
636 bytestream2_skip(&s->gbyte, skip);
641 static int decode_inter(AVCodecContext *avctx, GetBitContext *gb,
642 AVFrame *frame, AVFrame *prev)
644 AGMContext *s = avctx->priv_data;
647 compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
650 ret = decode_motion_vectors(avctx, gb);
655 s->blocks_w = avctx->coded_width >> 3;
656 s->blocks_h = avctx->coded_height >> 3;
658 ret = decode_inter_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, prev, 0);
662 bytestream2_skip(&s->gbyte, s->size[0]);
664 s->blocks_w = avctx->coded_width >> 4;
665 s->blocks_h = avctx->coded_height >> 4;
667 ret = decode_inter_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, prev, 2);
671 bytestream2_skip(&s->gbyte, s->size[1]);
673 s->blocks_w = avctx->coded_width >> 4;
674 s->blocks_h = avctx->coded_height >> 4;
676 ret = decode_inter_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, prev, 1);
683 typedef struct Node {
688 static void get_tree_codes(uint32_t *codes, Node *nodes, int idx, uint32_t pfx, int bitpos)
690 if (idx < 256 && idx >= 0) {
693 get_tree_codes(codes, nodes, nodes[idx].child[0], pfx + (0 << bitpos), bitpos + 1);
694 get_tree_codes(codes, nodes, nodes[idx].child[1], pfx + (1 << bitpos), bitpos + 1);
698 static void make_new_tree(const uint8_t *bitlens, uint32_t *codes)
700 int zlcount = 0, curlen, idx, nindex, last, llast;
701 int blcounts[32] = { 0 };
707 for (int i = 0; i < 256; i++) {
708 int bitlen = bitlens[i];
709 int blcount = blcounts[bitlen];
711 zlcount += bitlen < 1;
712 syms[(bitlen << 8) + blcount] = i;
716 for (int i = 0; i < 512; i++) {
717 nodes[i].child[0] = -1;
718 nodes[i].child[1] = -1;
721 for (int i = 0; i < 256; i++) {
722 node_idx[i] = 257 + i;;
730 for (curlen = 1; curlen < 32; curlen++) {
731 if (blcounts[curlen] > 0) {
732 int max_zlcount = zlcount + blcounts[curlen];
734 for (int i = 0; zlcount < 256 && zlcount < max_zlcount; zlcount++, i++) {
735 int p = node_idx[nindex - 1 + 512];
736 int ch = syms[256 * curlen + i];
738 if (nodes[p].child[0] == -1) {
739 nodes[p].child[0] = ch;
741 nodes[p].child[1] = ch;
744 nodes[ch].parent = p;
753 p = node_idx[nindex - 1 + 512];
755 if (nodes[p].child[0] == -1) {
756 nodes[p].child[0] = ch;
758 nodes[p].child[1] = ch;
762 nodes[ch].parent = p;
767 for (int i = 0; i < idx; i++)
768 node_idx[512 + i] = old_idx[i];
776 get_tree_codes(codes, nodes, 256, 0, 0);
779 static int build_huff(const uint8_t *bitlen, VLC *vlc)
781 uint32_t new_codes[256];
783 uint8_t symbols[256];
787 make_new_tree(bitlen, new_codes);
789 for (int i = 0; i < 256; i++) {
791 bits[nb_codes] = bitlen[i];
792 codes[nb_codes] = new_codes[i];
793 symbols[nb_codes] = i;
799 return ff_init_vlc_sparse(vlc, 13, nb_codes,
806 static int decode_huffman2(AVCodecContext *avctx, int header, int size)
808 AGMContext *s = avctx->priv_data;
809 GetBitContext *gb = &s->gb;
811 unsigned output_size;
814 if ((ret = init_get_bits8(gb, s->gbyte.buffer,
815 bytestream2_get_bytes_left(&s->gbyte))) < 0)
818 output_size = get_bits_long(gb, 32);
820 av_fast_padded_malloc(&s->output, &s->output_size, output_size);
822 return AVERROR(ENOMEM);
825 len = 4 + get_bits(gb, 1);
828 int count = get_bits(gb, 3) + 1;
830 for (int i = 0; i < count; i++)
831 cb[i] = get_bits(gb, len);
833 for (int i = 0; i < 256; i++) {
834 int idx = get_bits(gb, 3);
838 for (int i = 0; i < 256; i++)
839 lens[i] = get_bits(gb, len);
842 if ((ret = build_huff(lens, &s->vlc)) < 0)
846 while (get_bits_left(gb) > 0 && x < output_size) {
847 int val = get_vlc2(gb, s->vlc.table, s->vlc.bits, 3);
849 return AVERROR_INVALIDDATA;
850 s->output[x++] = val;
856 static int decode_frame(AVCodecContext *avctx, void *data,
857 int *got_frame, AVPacket *avpkt)
859 AGMContext *s = avctx->priv_data;
860 GetBitContext *gb = &s->gb;
861 GetByteContext *gbyte = &s->gbyte;
862 AVFrame *frame = data;
863 int w, h, width, height, header;
864 unsigned compressed_size;
870 bytestream2_init(gbyte, avpkt->data, avpkt->size);
872 header = bytestream2_get_le32(gbyte);
873 s->fflags = bytestream2_get_le32(gbyte);
874 s->bitstream_size = s->fflags & 0x1FFFFFFF;
876 av_log(avctx, AV_LOG_DEBUG, "fflags: %X\n", s->fflags);
877 if (avpkt->size < s->bitstream_size + 8)
878 return AVERROR_INVALIDDATA;
880 s->key_frame = avpkt->flags & AV_PKT_FLAG_KEY;
881 frame->key_frame = s->key_frame;
882 frame->pict_type = s->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
885 if (avctx->codec_tag == MKTAG('A', 'G', 'M', '0') ||
886 avctx->codec_tag == MKTAG('A', 'G', 'M', '1'))
887 return AVERROR_PATCHWELCOME;
889 ret = decode_huffman2(avctx, header, (avpkt->size - s->bitstream_size) - 8);
892 bytestream2_init(gbyte, s->output, s->output_size);
896 w = bytestream2_get_le32(gbyte);
897 h = bytestream2_get_le32(gbyte);
898 if (w == INT32_MIN || h == INT32_MIN)
899 return AVERROR_INVALIDDATA;
909 width = avctx->width;
910 height = avctx->height;
911 if (w < width || h < height || w & 7 || h & 7)
912 return AVERROR_INVALIDDATA;
914 ret = ff_set_dimensions(avctx, w, h);
917 avctx->width = width;
918 avctx->height = height;
920 s->compression = bytestream2_get_le32(gbyte);
921 if (s->compression < 0 || s->compression > 100)
922 return AVERROR_INVALIDDATA;
924 for (int i = 0; i < 3; i++)
925 s->size[i] = bytestream2_get_le32(gbyte);
927 compressed_size = s->output_size;
929 compressed_size = avpkt->size;
930 if (s->size[0] < 0 || s->size[1] < 0 || s->size[2] < 0 ||
931 32LL + s->size[0] + s->size[1] + s->size[2] > compressed_size) {
932 return AVERROR_INVALIDDATA;
935 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
938 if (frame->key_frame) {
939 ret = decode_intra(avctx, gb, frame);
941 if (!s->prev_frame->data[0]) {
942 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
943 return AVERROR_INVALIDDATA;
946 if (!(s->flags & 2)) {
947 ret = av_frame_copy(frame, s->prev_frame);
952 ret = decode_inter(avctx, gb, frame, s->prev_frame);
957 av_frame_unref(s->prev_frame);
958 if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
961 frame->crop_top = avctx->coded_height - avctx->height;
962 frame->crop_left = avctx->coded_width - avctx->width;
969 static av_cold int decode_init(AVCodecContext *avctx)
971 AGMContext *s = avctx->priv_data;
973 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
975 s->plus = avctx->codec_tag == MKTAG('A', 'G', 'M', '3') ||
976 avctx->codec_tag == MKTAG('A', 'G', 'M', '7');
978 avctx->idct_algo = FF_IDCT_SIMPLE;
979 ff_idctdsp_init(&s->idsp, avctx);
980 ff_init_scantable(s->idsp.idct_permutation, &s->scantable, ff_zigzag_direct);
982 s->prev_frame = av_frame_alloc();
984 return AVERROR(ENOMEM);
989 static void decode_flush(AVCodecContext *avctx)
991 AGMContext *s = avctx->priv_data;
993 av_frame_unref(s->prev_frame);
996 static av_cold int decode_close(AVCodecContext *avctx)
998 AGMContext *s = avctx->priv_data;
1000 ff_free_vlc(&s->vlc);
1001 av_frame_free(&s->prev_frame);
1002 av_freep(&s->mvectors);
1003 s->mvectors_size = 0;
1004 av_freep(&s->wblocks);
1005 s->wblocks_size = 0;
1006 av_freep(&s->output);
1014 AVCodec ff_agm_decoder = {
1016 .long_name = NULL_IF_CONFIG_SMALL("Amuse Graphics Movie"),
1017 .type = AVMEDIA_TYPE_VIDEO,
1018 .id = AV_CODEC_ID_AGM,
1019 .priv_data_size = sizeof(AGMContext),
1020 .init = decode_init,
1021 .close = decode_close,
1022 .decode = decode_frame,
1023 .flush = decode_flush,
1024 .capabilities = AV_CODEC_CAP_DR1,
1025 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1026 FF_CODEC_CAP_INIT_CLEANUP |
1027 FF_CODEC_CAP_EXPORTS_CROPPING,