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;
74 MotionVector *mvectors;
79 int luma_quant_matrix[64];
80 int chroma_quant_matrix[64];
83 DECLARE_ALIGNED(32, int16_t, block)[64];
87 static int read_code(GetBitContext *gb, int *oskip, int *level, int *map, int mode)
89 int len = 0, skip = 0, max;
91 if (show_bits(gb, 2)) {
92 switch (show_bits(gb, 4)) {
132 len = ((show_bits(gb, 5) & 0x10) | 0xA0) >> 4;
136 return AVERROR_INVALIDDATA;
140 *level = get_bits(gb, len);
143 max = 1 << (len - 1);
145 *level = -(max + *level);
146 } else if (show_bits(gb, 3) & 4) {
149 if (show_bits(gb, 4)) {
150 if (show_bits(gb, 4) == 1) {
152 *oskip = get_bits(gb, 16);
154 *oskip = get_bits(gb, 4);
158 *oskip = get_bits(gb, 10);
160 } else if (mode == 0) {
161 *oskip = get_bits(gb, 10);
167 *oskip = get_bits(gb, 4);
176 static int decode_intra_block(AGMContext *s, GetBitContext *gb, int size,
177 const int *quant_matrix, int *skip, int *dc_level)
179 const uint8_t *scantable = s->scantable.permutated;
180 const int offset = s->plus ? 0 : 1024;
181 int16_t *block = s->block;
182 int level, ret, map = 0;
184 memset(block, 0, sizeof(s->block));
189 ret = read_code(gb, skip, &level, &map, s->flags & 1);
194 block[scantable[0]] = offset + *dc_level * quant_matrix[0];
196 for (int i = 1; i < 64;) {
200 rskip = FFMIN(*skip, 64 - i);
204 ret = read_code(gb, skip, &level, &map, s->flags & 1);
208 block[scantable[i]] = level * quant_matrix[i];
216 static int decode_intra_plane(AGMContext *s, GetBitContext *gb, int size,
217 const int *quant_matrix, AVFrame *frame,
220 int ret, skip = 0, dc_level = 0;
222 if ((ret = init_get_bits8(gb, s->gbyte.buffer, size)) < 0)
225 for (int y = 0; y < s->blocks_h; y++) {
226 for (int x = 0; x < s->blocks_w; x++) {
227 ret = decode_intra_block(s, gb, size, quant_matrix, &skip, &dc_level);
231 s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
232 frame->linesize[plane], s->block);
237 if (get_bits_left(gb) < 0)
238 av_log(s->avctx, AV_LOG_WARNING, "overread\n");
239 if (get_bits_left(gb) > 0)
240 av_log(s->avctx, AV_LOG_WARNING, "underread: %d\n", get_bits_left(gb));
245 static int decode_inter_block(AGMContext *s, GetBitContext *gb, int size,
246 const int *quant_matrix, int *skip,
249 const uint8_t *scantable = s->scantable.permutated;
250 int16_t *block = s->block;
253 memset(block, 0, sizeof(s->block));
255 for (int i = 0; i < 64;) {
259 rskip = FFMIN(*skip, 64 - i);
263 ret = read_code(gb, skip, &level, map, s->flags & 1);
267 block[scantable[i]] = level * quant_matrix[i];
275 static int decode_inter_plane(AGMContext *s, GetBitContext *gb, int size,
276 const int *quant_matrix, AVFrame *frame,
277 AVFrame *prev, int plane)
281 if ((ret = init_get_bits8(gb, s->gbyte.buffer, size)) < 0)
285 for (int y = 0; y < s->blocks_h; y++) {
286 for (int x = 0; x < s->blocks_w; x++) {
287 int shift = plane == 0;
288 int mvpos = (y >> shift) * (s->blocks_w >> shift) + (x >> shift);
289 int orig_mv_x = s->mvectors[mvpos].x;
290 int mv_x = s->mvectors[mvpos].x / (1 + !shift);
291 int mv_y = s->mvectors[mvpos].y / (1 + !shift);
292 int h = s->avctx->coded_height >> !shift;
293 int w = s->avctx->coded_width >> !shift;
296 ret = decode_inter_block(s, gb, size, quant_matrix, &skip, &map);
300 if (orig_mv_x >= -32) {
301 if (y * 8 + mv_y < 0 || y * 8 + mv_y >= h ||
302 x * 8 + mv_x < 0 || x * 8 + mv_x >= w)
303 return AVERROR_INVALIDDATA;
305 copy_block8(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
306 prev->data[plane] + ((s->blocks_h - 1 - y) * 8 - mv_y) * prev->linesize[plane] + (x * 8 + mv_x),
307 frame->linesize[plane], prev->linesize[plane], 8);
309 s->idsp.idct(s->block);
310 for (int i = 0; i < 64; i++)
311 s->block[i] = (s->block[i] + 1) & 0xFFFC;
312 s->idsp.add_pixels_clamped(s->block, frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
313 frame->linesize[plane]);
316 s->idsp.idct_put(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
317 frame->linesize[plane], s->block);
322 for (int y = 0; y < s->blocks_h; y++) {
323 for (int x = 0; x < s->blocks_w; x++) {
326 ret = decode_inter_block(s, gb, size, quant_matrix, &skip, &map);
332 s->idsp.idct_add(frame->data[plane] + (s->blocks_h - 1 - y) * 8 * frame->linesize[plane] + x * 8,
333 frame->linesize[plane], s->block);
339 if (get_bits_left(gb) < 0)
340 av_log(s->avctx, AV_LOG_WARNING, "overread\n");
341 if (get_bits_left(gb) > 0)
342 av_log(s->avctx, AV_LOG_WARNING, "underread: %d\n", get_bits_left(gb));
347 static void compute_quant_matrix(AGMContext *s, double qscale)
349 int luma[64], chroma[64];
350 double f = 1.0 - fabs(qscale);
352 if (!s->key_frame && (s->flags & 2)) {
354 for (int i = 0; i < 64; i++) {
355 luma[i] = FFMAX(1, 16 * f);
356 chroma[i] = FFMAX(1, 16 * f);
359 for (int i = 0; i < 64; i++) {
360 luma[i] = FFMAX(1, 16 - qscale * 32);
361 chroma[i] = FFMAX(1, 16 - qscale * 32);
366 for (int i = 0; i < 64; i++) {
367 luma[i] = FFMAX(1, unscaled_luma [(i & 7) * 8 + (i >> 3)] * f);
368 chroma[i] = FFMAX(1, unscaled_chroma[(i & 7) * 8 + (i >> 3)] * f);
371 for (int i = 0; i < 64; i++) {
372 luma[i] = FFMAX(1, 255.0 - (255 - unscaled_luma [(i & 7) * 8 + (i >> 3)]) * f);
373 chroma[i] = FFMAX(1, 255.0 - (255 - unscaled_chroma[(i & 7) * 8 + (i >> 3)]) * f);
378 for (int i = 0; i < 64; i++) {
379 int pos = ff_zigzag_direct[i];
381 s->luma_quant_matrix[i] = luma[pos] * ((pos / 8) & 1 ? -1 : 1);
382 s->chroma_quant_matrix[i] = chroma[pos] * ((pos / 8) & 1 ? -1 : 1);
386 static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
388 AGMContext *s = avctx->priv_data;
391 compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
393 s->blocks_w = avctx->coded_width >> 3;
394 s->blocks_h = avctx->coded_height >> 3;
396 ret = decode_intra_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, 0);
400 bytestream2_skip(&s->gbyte, s->size[0]);
402 s->blocks_w = avctx->coded_width >> 4;
403 s->blocks_h = avctx->coded_height >> 4;
405 ret = decode_intra_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, 2);
409 bytestream2_skip(&s->gbyte, s->size[1]);
411 s->blocks_w = avctx->coded_width >> 4;
412 s->blocks_h = avctx->coded_height >> 4;
414 ret = decode_intra_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, 1);
421 static int decode_motion_vectors(AVCodecContext *avctx, GetBitContext *gb)
423 AGMContext *s = avctx->priv_data;
424 int nb_mvs = ((avctx->height + 15) >> 4) * ((avctx->width + 15) >> 4);
425 int ret, skip = 0, value, map;
427 av_fast_padded_malloc(&s->mvectors, &s->mvectors_size,
428 nb_mvs * sizeof(*s->mvectors));
430 return AVERROR(ENOMEM);
432 if ((ret = init_get_bits8(gb, s->gbyte.buffer, bytestream2_get_bytes_left(&s->gbyte) -
433 (s->size[0] + s->size[1] + s->size[2]))) < 0)
436 memset(s->mvectors, 0, sizeof(*s->mvectors) * nb_mvs);
438 for (int i = 0; i < nb_mvs; i++) {
439 ret = read_code(gb, &skip, &value, &map, 1);
442 s->mvectors[i].x = value;
446 for (int i = 0; i < nb_mvs; i++) {
447 ret = read_code(gb, &skip, &value, &map, 1);
450 s->mvectors[i].y = value;
454 if (get_bits_left(gb) <= 0)
455 return AVERROR_INVALIDDATA;
456 skip = (get_bits_count(gb) >> 3) + 1;
457 bytestream2_skip(&s->gbyte, skip);
462 static int decode_inter(AVCodecContext *avctx, GetBitContext *gb,
463 AVFrame *frame, AVFrame *prev)
465 AGMContext *s = avctx->priv_data;
468 compute_quant_matrix(s, (2 * s->compression - 100) / 100.0);
471 ret = decode_motion_vectors(avctx, gb);
476 s->blocks_w = avctx->coded_width >> 3;
477 s->blocks_h = avctx->coded_height >> 3;
479 ret = decode_inter_plane(s, gb, s->size[0], s->luma_quant_matrix, frame, prev, 0);
483 bytestream2_skip(&s->gbyte, s->size[0]);
485 s->blocks_w = avctx->coded_width >> 4;
486 s->blocks_h = avctx->coded_height >> 4;
488 ret = decode_inter_plane(s, gb, s->size[1], s->chroma_quant_matrix, frame, prev, 2);
492 bytestream2_skip(&s->gbyte, s->size[1]);
494 s->blocks_w = avctx->coded_width >> 4;
495 s->blocks_h = avctx->coded_height >> 4;
497 ret = decode_inter_plane(s, gb, s->size[2], s->chroma_quant_matrix, frame, prev, 1);
504 static int decode_frame(AVCodecContext *avctx, void *data,
505 int *got_frame, AVPacket *avpkt)
507 AGMContext *s = avctx->priv_data;
508 GetBitContext *gb = &s->gb;
509 GetByteContext *gbyte = &s->gbyte;
510 AVFrame *frame = data;
511 int w, h, width, height, header;
517 bytestream2_init(gbyte, avpkt->data, avpkt->size);
519 header = bytestream2_get_le32(gbyte);
520 s->fflags = bytestream2_get_le32(gbyte);
521 s->bitstream_size = s->fflags & 0x1FFFFFFF;
523 av_log(avctx, AV_LOG_DEBUG, "fflags: %X\n", s->fflags);
524 if (avpkt->size < s->bitstream_size + 8)
525 return AVERROR_INVALIDDATA;
527 s->key_frame = s->fflags & 0x1;
528 frame->key_frame = s->key_frame;
529 frame->pict_type = s->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
532 av_log(avctx, AV_LOG_ERROR, "header: %X\n", header);
533 return AVERROR_PATCHWELCOME;
537 w = bytestream2_get_le32(gbyte);
542 h = bytestream2_get_le32(gbyte);
548 width = avctx->width;
549 height = avctx->height;
550 if (w < width || h < height || w & 7 || h & 7)
551 return AVERROR_INVALIDDATA;
553 ret = ff_set_dimensions(avctx, w, h);
556 avctx->width = width;
557 avctx->height = height;
559 s->compression = bytestream2_get_le32(gbyte);
560 if (s->compression < 0 || s->compression > 100)
561 return AVERROR_INVALIDDATA;
563 for (int i = 0; i < 3; i++)
564 s->size[i] = bytestream2_get_le32(gbyte);
565 if (32LL + s->size[0] + s->size[1] + s->size[2] > avpkt->size)
566 return AVERROR_INVALIDDATA;
568 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
571 if (frame->key_frame) {
572 ret = decode_intra(avctx, gb, frame);
574 if (!s->prev_frame->data[0]) {
575 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
576 return AVERROR_INVALIDDATA;
579 if (!(s->flags & 2)) {
580 ret = av_frame_copy(frame, s->prev_frame);
585 ret = decode_inter(avctx, gb, frame, s->prev_frame);
590 av_frame_unref(s->prev_frame);
591 if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
594 frame->crop_top = avctx->coded_height - avctx->height;
595 frame->crop_left = avctx->coded_width - avctx->width;
602 static av_cold int decode_init(AVCodecContext *avctx)
604 AGMContext *s = avctx->priv_data;
606 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
608 s->plus = avctx->codec_tag == MKTAG('A', 'G', 'M', '3');
610 avctx->idct_algo = FF_IDCT_SIMPLE;
611 ff_idctdsp_init(&s->idsp, avctx);
612 ff_init_scantable(s->idsp.idct_permutation, &s->scantable, ff_zigzag_direct);
614 s->prev_frame = av_frame_alloc();
616 return AVERROR(ENOMEM);
621 static void decode_flush(AVCodecContext *avctx)
623 AGMContext *s = avctx->priv_data;
625 av_frame_unref(s->prev_frame);
628 static av_cold int decode_close(AVCodecContext *avctx)
630 AGMContext *s = avctx->priv_data;
632 av_frame_free(&s->prev_frame);
633 av_freep(&s->mvectors);
634 s->mvectors_size = 0;
639 AVCodec ff_agm_decoder = {
641 .long_name = NULL_IF_CONFIG_SMALL("Amuse Graphics Movie"),
642 .type = AVMEDIA_TYPE_VIDEO,
643 .id = AV_CODEC_ID_AGM,
644 .priv_data_size = sizeof(AGMContext),
646 .close = decode_close,
647 .decode = decode_frame,
648 .flush = decode_flush,
649 .capabilities = AV_CODEC_CAP_DR1,
650 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
651 FF_CODEC_CAP_INIT_CLEANUP |
652 FF_CODEC_CAP_EXPORTS_CROPPING,