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
29 #include "libavutil/mem_internal.h"
32 #include "bytestream.h"
33 #include "copy_block.h"
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,
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,
56 typedef struct MotionVector {
60 typedef struct AGMContext {
62 AVCodecContext *avctx;
79 unsigned padded_output_size;
82 MotionVector *mvectors;
83 unsigned mvectors_size;
89 int luma_quant_matrix[64];
90 int chroma_quant_matrix[64];
93 DECLARE_ALIGNED(32, int16_t, block)[64];
96 unsigned wblocks_size;
104 static int read_code(GetBitContext *gb, int *oskip, int *level, int *map, int mode)
106 int len = 0, skip = 0, max;
108 if (get_bits_left(gb) < 2)
109 return AVERROR_INVALIDDATA;
111 if (show_bits(gb, 2)) {
112 switch (show_bits(gb, 4)) {
152 len = ((show_bits(gb, 5) & 0x10) | 0xA0) >> 4;
156 return AVERROR_INVALIDDATA;
160 *level = get_bits(gb, len);
163 max = 1 << (len - 1);
165 *level = -(max + *level);
166 } else if (show_bits(gb, 3) & 4) {
169 if (show_bits(gb, 4)) {
170 if (show_bits(gb, 4) == 1) {
172 *oskip = get_bits(gb, 16);
174 *oskip = get_bits(gb, 4);
178 *oskip = get_bits(gb, 10);
180 } else if (mode == 0) {
181 *oskip = get_bits(gb, 10);
187 *oskip = get_bits(gb, 4);
196 static int decode_intra_blocks(AGMContext *s, GetBitContext *gb,
197 const int *quant_matrix, int *skip, int *dc_level)
199 const uint8_t *scantable = s->scantable.permutated;
200 int level, ret, map = 0;
202 memset(s->wblocks, 0, s->wblocks_size);
204 for (int i = 0; i < 64; i++) {
205 int16_t *block = s->wblocks + scantable[i];
207 for (int j = 0; j < s->blocks_w;) {
211 rskip = FFMIN(*skip, s->blocks_w - j);
214 for (int k = 0; k < rskip; k++)
215 block[64 * k] = *dc_level * quant_matrix[0];
220 ret = read_code(gb, skip, &level, &map, s->flags & 1);
227 block[0] = (i == 0 ? *dc_level : level) * quant_matrix[i];
237 static int decode_inter_blocks(AGMContext *s, GetBitContext *gb,
238 const int *quant_matrix, int *skip,
241 const uint8_t *scantable = s->scantable.permutated;
244 memset(s->wblocks, 0, s->wblocks_size);
245 memset(s->map, 0, s->map_size);
247 for (int i = 0; i < 64; i++) {
248 int16_t *block = s->wblocks + scantable[i];
250 for (int j = 0; j < s->blocks_w;) {
254 rskip = FFMIN(*skip, s->blocks_w - j);
259 ret = read_code(gb, skip, &level, &map[j], s->flags & 1);
263 block[0] = level * quant_matrix[i];
273 static int decode_intra_block(AGMContext *s, GetBitContext *gb,
274 const int *quant_matrix, int *skip, int *dc_level)
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;
281 memset(block, 0, sizeof(s->block));
286 ret = read_code(gb, skip, &level, &map, s->flags & 1);
291 block[scantable[0]] = offset + *dc_level * quant_matrix[0];
293 for (int i = 1; i < 64;) {
297 rskip = FFMIN(*skip, 64 - i);
301 ret = read_code(gb, skip, &level, &map, s->flags & 1);
305 block[scantable[i]] = level * quant_matrix[i];
313 static int decode_intra_plane(AGMContext *s, GetBitContext *gb, int size,
314 const int *quant_matrix, AVFrame *frame,
317 int ret, skip = 0, dc_level = 0;
318 const int offset = s->plus ? 0 : 1024;
320 if ((ret = init_get_bits8(gb, s->gbyte.buffer, size)) < 0)
324 av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
325 64 * s->blocks_w * sizeof(*s->wblocks));
327 return AVERROR(ENOMEM);
329 for (int y = 0; y < s->blocks_h; y++) {
330 ret = decode_intra_blocks(s, gb, quant_matrix, &skip, &dc_level);
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);
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);
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);
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));
362 static int decode_inter_block(AGMContext *s, GetBitContext *gb,
363 const int *quant_matrix, int *skip,
366 const uint8_t *scantable = s->scantable.permutated;
367 int16_t *block = s->block;
370 memset(block, 0, sizeof(s->block));
372 for (int i = 0; i < 64;) {
376 rskip = FFMIN(*skip, 64 - i);
380 ret = read_code(gb, skip, &level, map, s->flags & 1);
384 block[scantable[i]] = level * quant_matrix[i];
392 static int decode_inter_plane(AGMContext *s, GetBitContext *gb, int size,
393 const int *quant_matrix, AVFrame *frame,
394 AVFrame *prev, int plane)
398 if ((ret = init_get_bits8(gb, s->gbyte.buffer, size)) < 0)
402 av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
403 64 * s->blocks_w * sizeof(*s->wblocks));
405 return AVERROR(ENOMEM);
407 av_fast_padded_malloc(&s->map, &s->map_size,
408 s->blocks_w * sizeof(*s->map));
410 return AVERROR(ENOMEM);
412 for (int y = 0; y < s->blocks_h; y++) {
413 ret = decode_inter_blocks(s, gb, quant_matrix, &skip, s->map);
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;
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;
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);
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]);
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);
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;
460 ret = decode_inter_block(s, gb, quant_matrix, &skip, &map);
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;
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);
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]);
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);
485 } else if (s->flags & 1) {
486 av_fast_padded_malloc(&s->wblocks, &s->wblocks_size,
487 64 * s->blocks_w * sizeof(*s->wblocks));
489 return AVERROR(ENOMEM);
491 av_fast_padded_malloc(&s->map, &s->map_size,
492 s->blocks_w * sizeof(*s->map));
494 return AVERROR(ENOMEM);
496 for (int y = 0; y < s->blocks_h; y++) {
497 ret = decode_inter_blocks(s, gb, quant_matrix, &skip, s->map);
501 for (int x = 0; x < s->blocks_w; x++) {
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);
509 for (int y = 0; y < s->blocks_h; y++) {
510 for (int x = 0; x < s->blocks_w; x++) {
513 ret = decode_inter_block(s, gb, quant_matrix, &skip, &map);
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);
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));
534 static void compute_quant_matrix(AGMContext *s, double qscale)
536 int luma[64], chroma[64];
537 double f = 1.0 - fabs(qscale);
539 if (!s->key_frame && (s->flags & 2)) {
541 for (int i = 0; i < 64; i++) {
542 luma[i] = FFMAX(1, 16 * f);
543 chroma[i] = FFMAX(1, 16 * f);
546 for (int i = 0; i < 64; i++) {
547 luma[i] = FFMAX(1, 16 - qscale * 32);
548 chroma[i] = FFMAX(1, 16 - qscale * 32);
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);
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);
565 for (int i = 0; i < 64; i++) {
566 int pos = ff_zigzag_direct[i];
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);
573 static int decode_raw_intra_rgb(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
575 uint8_t *dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
576 uint8_t r = 0, g = 0, b = 0;
578 if (bytestream2_get_bytes_left(gbyte) < 3 * avctx->width * avctx->height)
579 return AVERROR_INVALIDDATA;
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;
585 dst[x*3+1] = bytestream2_get_byteu(gbyte) + g;
587 dst[x*3+2] = bytestream2_get_byteu(gbyte) + b;
590 dst -= frame->linesize[0];
596 av_always_inline static int fill_pixels(uint8_t **y0, uint8_t **y1,
597 uint8_t **u, uint8_t **v,
598 int ylinesize, int ulinesize, int vlinesize,
600 int *nx, int *ny, int *np, int w, int h)
602 uint8_t *y0dst = *y0;
603 uint8_t *y1dst = *y1;
606 int x = *nx, y = *ny, pos = *np;
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];
614 } else if (pos == 1) {
623 y0dst -= 2*ylinesize;
624 y1dst -= 2*ylinesize;
628 y0dst[2*x+0] += fill[2];
629 y0dst[2*x+1] += fill[3];
631 } else if (pos == 2) {
632 y1dst[2*x+0] += fill[0];
633 y1dst[2*x+1] += fill[1];
642 y0dst -= 2*ylinesize;
643 y1dst -= 2*ylinesize;
661 static int decode_runlen_rgb(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
663 uint8_t *dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0];
664 int runlen, y = 0, x = 0;
668 while (bytestream2_get_bytes_left(gbyte) > 0) {
669 code = bytestream2_peek_le32(gbyte);
670 runlen = code & 0xFFFFFF;
672 if (code >> 24 == 0x77) {
673 bytestream2_skip(gbyte, 4);
675 for (int i = 0; i < 4; i++)
676 fill[i] = bytestream2_get_byte(gbyte);
681 for (int i = 0; i < 4; i++) {
684 if (x >= frame->width * 3) {
687 dst -= frame->linesize[0];
688 if (y >= frame->height)
694 for (int i = 0; i < 4; i++)
695 fill[i] = bytestream2_get_byte(gbyte);
697 for (int i = 0; i < 4; i++) {
700 if (x >= frame->width * 3) {
703 dst -= frame->linesize[0];
704 if (y >= frame->height)
714 static int decode_runlen(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
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;
724 while (bytestream2_get_bytes_left(gbyte) > 0) {
725 code = bytestream2_peek_le32(gbyte);
726 runlen = code & 0xFFFFFF;
728 if (code >> 24 == 0x77) {
729 bytestream2_skip(gbyte, 4);
731 for (int i = 0; i < 4; i++)
732 fill[i] = bytestream2_get_byte(gbyte);
737 if (fill_pixels(&y0dst, &y1dst, &udst, &vdst,
747 for (int i = 0; i < 4; i++)
748 fill[i] = bytestream2_get_byte(gbyte);
750 if (fill_pixels(&y0dst, &y1dst, &udst, &vdst,
764 static int decode_raw_intra(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
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;
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;
776 y0dst[x*2+1] = bytestream2_get_byte(gbyte) + ly1;
778 y1dst[x*2+0] = bytestream2_get_byte(gbyte) + ly2;
780 y1dst[x*2+1] = bytestream2_get_byte(gbyte) + ly3;
782 udst[x] = bytestream2_get_byte(gbyte) + lu;
784 vdst[x] = bytestream2_get_byte(gbyte) + lv;
788 y0dst -= 2*frame->linesize[0];
789 y1dst -= 2*frame->linesize[0];
790 udst -= frame->linesize[1];
791 vdst -= frame->linesize[2];
797 static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
799 AGMContext *s = avctx->priv_data;
802 compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
804 s->blocks_w = avctx->coded_width >> 3;
805 s->blocks_h = avctx->coded_height >> 3;
807 ret = decode_intra_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, 0);
811 bytestream2_skip(&s->gbyte, s->size[0]);
813 s->blocks_w = avctx->coded_width >> 4;
814 s->blocks_h = avctx->coded_height >> 4;
816 ret = decode_intra_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, 2);
820 bytestream2_skip(&s->gbyte, s->size[1]);
822 s->blocks_w = avctx->coded_width >> 4;
823 s->blocks_h = avctx->coded_height >> 4;
825 ret = decode_intra_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, 1);
832 static int decode_motion_vectors(AVCodecContext *avctx, GetBitContext *gb)
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;
838 av_fast_padded_malloc(&s->mvectors, &s->mvectors_size,
839 nb_mvs * sizeof(*s->mvectors));
841 return AVERROR(ENOMEM);
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)
847 memset(s->mvectors, 0, sizeof(*s->mvectors) * nb_mvs);
849 for (int i = 0; i < nb_mvs; i++) {
850 ret = read_code(gb, &skip, &value, &map, 1);
853 s->mvectors[i].x = value;
857 for (int i = 0; i < nb_mvs; i++) {
858 ret = read_code(gb, &skip, &value, &map, 1);
861 s->mvectors[i].y = value;
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);
873 static int decode_inter(AVCodecContext *avctx, GetBitContext *gb,
874 AVFrame *frame, AVFrame *prev)
876 AGMContext *s = avctx->priv_data;
879 compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
882 ret = decode_motion_vectors(avctx, gb);
887 s->blocks_w = avctx->coded_width >> 3;
888 s->blocks_h = avctx->coded_height >> 3;
890 ret = decode_inter_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, prev, 0);
894 bytestream2_skip(&s->gbyte, s->size[0]);
896 s->blocks_w = avctx->coded_width >> 4;
897 s->blocks_h = avctx->coded_height >> 4;
899 ret = decode_inter_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, prev, 2);
903 bytestream2_skip(&s->gbyte, s->size[1]);
905 s->blocks_w = avctx->coded_width >> 4;
906 s->blocks_h = avctx->coded_height >> 4;
908 ret = decode_inter_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, prev, 1);
915 typedef struct Node {
920 static void get_tree_codes(uint32_t *codes, Node *nodes, int idx, uint32_t pfx, int bitpos)
922 if (idx < 256 && idx >= 0) {
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);
930 static int make_new_tree(const uint8_t *bitlens, uint32_t *codes)
932 int zlcount = 0, curlen, idx, nindex, last, llast;
933 int blcounts[32] = { 0 };
939 for (int i = 0; i < 256; i++) {
940 int bitlen = bitlens[i];
941 int blcount = blcounts[bitlen];
943 zlcount += bitlen < 1;
944 syms[(bitlen << 8) + blcount] = i;
948 for (int i = 0; i < 512; i++) {
949 nodes[i].child[0] = -1;
950 nodes[i].child[1] = -1;
953 for (int i = 0; i < 256; i++) {
954 node_idx[i] = 257 + i;
962 for (curlen = 1; curlen < 32; curlen++) {
963 if (blcounts[curlen] > 0) {
964 int max_zlcount = zlcount + blcounts[curlen];
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];
971 return AVERROR_INVALIDDATA;
973 if (nodes[p].child[0] == -1) {
974 nodes[p].child[0] = ch;
976 nodes[p].child[1] = ch;
979 nodes[ch].parent = p;
988 p = node_idx[nindex - 1 + 512];
990 if (nodes[p].child[0] == -1) {
991 nodes[p].child[0] = ch;
993 nodes[p].child[1] = ch;
997 nodes[ch].parent = p;
1002 for (int i = 0; i < idx; i++)
1003 node_idx[512 + i] = old_idx[i];
1011 get_tree_codes(codes, nodes, 256, 0, 0);
1015 static int build_huff(const uint8_t *bitlen, VLC *vlc)
1017 uint32_t new_codes[256];
1019 uint8_t symbols[256];
1020 uint32_t codes[256];
1023 int ret = make_new_tree(bitlen, new_codes);
1027 for (int i = 0; i < 256; i++) {
1029 bits[nb_codes] = bitlen[i];
1030 codes[nb_codes] = new_codes[i];
1031 symbols[nb_codes] = i;
1037 return ff_init_vlc_sparse(vlc, 13, nb_codes,
1044 static int decode_huffman2(AVCodecContext *avctx, int header, int size)
1046 AGMContext *s = avctx->priv_data;
1047 GetBitContext *gb = &s->gb;
1051 if ((ret = init_get_bits8(gb, s->gbyte.buffer,
1052 bytestream2_get_bytes_left(&s->gbyte))) < 0)
1055 s->output_size = get_bits_long(gb, 32);
1057 if (s->output_size > avctx->width * avctx->height * 9LL + 10000)
1058 return AVERROR_INVALIDDATA;
1060 av_fast_padded_malloc(&s->output, &s->padded_output_size, s->output_size);
1062 return AVERROR(ENOMEM);
1064 x = get_bits(gb, 1);
1065 len = 4 + get_bits(gb, 1);
1068 int count = get_bits(gb, 3) + 1;
1070 for (int i = 0; i < count; i++)
1071 cb[i] = get_bits(gb, len);
1073 for (int i = 0; i < 256; i++) {
1074 int idx = get_bits(gb, 3);
1078 for (int i = 0; i < 256; i++)
1079 lens[i] = get_bits(gb, len);
1082 if ((ret = build_huff(lens, &s->vlc)) < 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);
1089 return AVERROR_INVALIDDATA;
1090 s->output[x++] = val;
1096 static int decode_frame(AVCodecContext *avctx, void *data,
1097 int *got_frame, AVPacket *avpkt)
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;
1111 bytestream2_init(gbyte, avpkt->data, avpkt->size);
1113 header = bytestream2_get_le32(gbyte);
1114 s->fflags = bytestream2_get_le32(gbyte);
1115 s->bitstream_size = s->fflags & 0x1FFFFFFF;
1117 av_log(avctx, AV_LOG_DEBUG, "fflags: %X\n", s->fflags);
1118 if (avpkt->size < s->bitstream_size + 8)
1119 return AVERROR_INVALIDDATA;
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;
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;
1133 if (avctx->codec_tag == MKTAG('A', 'G', 'M', '0') ||
1134 avctx->codec_tag == MKTAG('A', 'G', 'M', '1'))
1135 return AVERROR_PATCHWELCOME;
1137 ret = decode_huffman2(avctx, header, (avpkt->size - s->bitstream_size) - 8);
1140 bytestream2_init(gbyte, s->output, s->output_size);
1141 } else if (!s->dct) {
1142 bytestream2_skip(gbyte, 4);
1147 w = bytestream2_get_le32(gbyte);
1148 h = bytestream2_get_le32(gbyte);
1149 if (w == INT32_MIN || h == INT32_MIN)
1150 return AVERROR_INVALIDDATA;
1160 width = avctx->width;
1161 height = avctx->height;
1162 if (w < width || h < height || w & 7 || h & 7)
1163 return AVERROR_INVALIDDATA;
1165 ret = ff_set_dimensions(avctx, w, h);
1168 avctx->width = width;
1169 avctx->height = height;
1171 s->compression = bytestream2_get_le32(gbyte);
1172 if (s->compression < 0 || s->compression > 100)
1173 return AVERROR_INVALIDDATA;
1175 for (int i = 0; i < 3; i++)
1176 s->size[i] = bytestream2_get_le32(gbyte);
1178 compressed_size = s->output_size;
1181 compressed_size = avpkt->size;
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;
1190 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
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);
1199 ret = decode_intra(avctx, gb, frame);
1201 if (s->prev_frame-> width != frame->width ||
1202 s->prev_frame->height != frame->height)
1203 return AVERROR_INVALIDDATA;
1205 if (!(s->flags & 2)) {
1206 ret = av_frame_copy(frame, s->prev_frame);
1212 ret = decode_inter(avctx, gb, frame, s->prev_frame);
1213 } else if (!s->dct && !s->rgb) {
1214 ret = decode_runlen(avctx, gbyte, frame);
1216 ret = decode_runlen_rgb(avctx, gbyte, frame);
1222 av_frame_unref(s->prev_frame);
1223 if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
1226 frame->crop_top = avctx->coded_height - avctx->height;
1227 frame->crop_left = avctx->coded_width - avctx->width;
1234 static av_cold int decode_init(AVCodecContext *avctx)
1236 AGMContext *s = avctx->priv_data;
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;
1241 s->plus = avctx->codec_tag == MKTAG('A', 'G', 'M', '3') ||
1242 avctx->codec_tag == MKTAG('A', 'G', 'M', '7');
1244 s->dct = avctx->codec_tag != MKTAG('A', 'G', 'M', '4') &&
1245 avctx->codec_tag != MKTAG('A', 'G', 'M', '5');
1247 if (!s->rgb && !s->dct) {
1248 if ((avctx->width & 1) || (avctx->height & 1))
1249 return AVERROR_INVALIDDATA;
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);
1256 s->prev_frame = av_frame_alloc();
1258 return AVERROR(ENOMEM);
1263 static void decode_flush(AVCodecContext *avctx)
1265 AGMContext *s = avctx->priv_data;
1267 av_frame_unref(s->prev_frame);
1270 static av_cold int decode_close(AVCodecContext *avctx)
1272 AGMContext *s = avctx->priv_data;
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;
1288 const AVCodec ff_agm_decoder = {
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 |
1300 FF_CODEC_CAP_INIT_CLEANUP |
1301 FF_CODEC_CAP_EXPORTS_CROPPING,