Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 1 | /* |
| 2 | * Deluxe Paint Animation decoder |
| 3 | * Copyright (c) 2009 Peter Ross |
| 4 | * |
| 5 | * This file is part of FFmpeg. |
| 6 | * |
| 7 | * FFmpeg is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * FFmpeg is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with FFmpeg; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | /** |
Diego Biurrun | ba87f08 | 2010-04-20 14:45:34 | [diff] [blame] | 23 | * @file |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 24 | * Deluxe Paint Animation decoder |
| 25 | */ |
| 26 | |
| 27 | #include "avcodec.h" |
| 28 | #include "bytestream.h" |
Andreas Rheinhardt | a688f3c | 2022-03-16 17:18:28 | [diff] [blame] | 29 | #include "codec_internal.h" |
Andreas Rheinhardt | e2c24e6 | 2022-08-24 18:49:25 | [diff] [blame] | 30 | #include "decode.h" |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 31 | |
| 32 | typedef struct AnmContext { |
Anton Khirnov | 759001c | 2012-11-21 20:34:46 | [diff] [blame] | 33 | AVFrame *frame; |
Peter Ross | 62931e1 | 2011-04-15 14:59:19 | [diff] [blame] | 34 | int palette[AVPALETTE_COUNT]; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 35 | } AnmContext; |
| 36 | |
| 37 | static av_cold int decode_init(AVCodecContext *avctx) |
| 38 | { |
| 39 | AnmContext *s = avctx->priv_data; |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 40 | GetByteContext gb; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 41 | int i; |
| 42 | |
Andreas Rheinhardt | 45c3502 | 2020-05-30 03:25:06 | [diff] [blame] | 43 | if (avctx->extradata_size < 16 * 8 + 4 * 256) |
| 44 | return AVERROR_INVALIDDATA; |
| 45 | |
Anton Khirnov | 716d413 | 2012-10-06 10:10:34 | [diff] [blame] | 46 | avctx->pix_fmt = AV_PIX_FMT_PAL8; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 47 | |
Anton Khirnov | 759001c | 2012-11-21 20:34:46 | [diff] [blame] | 48 | s->frame = av_frame_alloc(); |
| 49 | if (!s->frame) |
| 50 | return AVERROR(ENOMEM); |
| 51 | |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 52 | bytestream2_init(&gb, avctx->extradata, avctx->extradata_size); |
| 53 | bytestream2_skipu(&gb, 16 * 8); |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 54 | for (i = 0; i < 256; i++) |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 55 | s->palette[i] = (0xFFU << 24) | bytestream2_get_le32u(&gb); |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Perform decode operation |
Diego Biurrun | 4051be6 | 2012-07-02 18:40:26 | [diff] [blame] | 62 | * @param dst pointer to destination image buffer |
| 63 | * @param dst_end pointer to end of destination image buffer |
| 64 | * @param gb GetByteContext (optional, see below) |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 65 | * @param pixel Fill color (optional, see below) |
| 66 | * @param count Pixel count |
| 67 | * @param x Pointer to x-axis counter |
| 68 | * @param width Image width |
| 69 | * @param linesize Destination image buffer linesize |
| 70 | * @return non-zero if destination buffer is exhausted |
| 71 | * |
Paul B Mahol | 5b4d026 | 2012-03-13 18:13:38 | [diff] [blame] | 72 | * a copy operation is achieved when 'gb' is set |
Diego Biurrun | 09f2119 | 2012-06-13 09:41:12 | [diff] [blame] | 73 | * a fill operation is achieved when 'gb' is null and pixel is >= 0 |
| 74 | * a skip operation is achieved when 'gb' is null and pixel is < 0 |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 75 | */ |
| 76 | static inline int op(uint8_t **dst, const uint8_t *dst_end, |
Paul B Mahol | 5b4d026 | 2012-03-13 18:13:38 | [diff] [blame] | 77 | GetByteContext *gb, |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 78 | int pixel, int count, |
| 79 | int *x, int width, int linesize) |
| 80 | { |
| 81 | int remaining = width - *x; |
| 82 | while(count > 0) { |
| 83 | int striplen = FFMIN(count, remaining); |
Paul B Mahol | 5b4d026 | 2012-03-13 18:13:38 | [diff] [blame] | 84 | if (gb) { |
| 85 | if (bytestream2_get_bytes_left(gb) < striplen) |
Laurent Aimar | 3999386 | 2011-09-30 22:44:58 | [diff] [blame] | 86 | goto exhausted; |
Paul B Mahol | 5b4d026 | 2012-03-13 18:13:38 | [diff] [blame] | 87 | bytestream2_get_bufferu(gb, *dst, striplen); |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 88 | } else if (pixel >= 0) |
| 89 | memset(*dst, pixel, striplen); |
| 90 | *dst += striplen; |
| 91 | remaining -= striplen; |
| 92 | count -= striplen; |
| 93 | if (remaining <= 0) { |
| 94 | *dst += linesize - width; |
| 95 | remaining = width; |
| 96 | } |
| 97 | if (linesize > 0) { |
| 98 | if (*dst >= dst_end) goto exhausted; |
| 99 | } else { |
| 100 | if (*dst <= dst_end) goto exhausted; |
| 101 | } |
| 102 | } |
| 103 | *x = width - remaining; |
| 104 | return 0; |
| 105 | |
| 106 | exhausted: |
| 107 | *x = width - remaining; |
| 108 | return 1; |
| 109 | } |
| 110 | |
Andreas Rheinhardt | ce7dbd0 | 2022-03-30 19:33:24 | [diff] [blame] | 111 | static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, |
| 112 | int *got_frame, AVPacket *avpkt) |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 113 | { |
| 114 | AnmContext *s = avctx->priv_data; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 115 | const int buf_size = avpkt->size; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 116 | uint8_t *dst, *dst_end; |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 117 | GetByteContext gb; |
| 118 | int count, ret, x = 0; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 119 | |
Michael Niedermayer | 1965161 | 2019-08-15 19:00:54 | [diff] [blame] | 120 | if (buf_size < 7) |
| 121 | return AVERROR_INVALIDDATA; |
| 122 | |
James Almer | 9ea6d21 | 2019-08-30 14:37:25 | [diff] [blame] | 123 | if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0) |
Paul B Mahol | 0baec57 | 2012-11-11 11:00:11 | [diff] [blame] | 124 | return ret; |
Anton Khirnov | 759001c | 2012-11-21 20:34:46 | [diff] [blame] | 125 | dst = s->frame->data[0]; |
| 126 | dst_end = s->frame->data[0] + s->frame->linesize[0]*avctx->height; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 127 | |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 128 | bytestream2_init(&gb, avpkt->data, buf_size); |
Paul B Mahol | 5b4d026 | 2012-03-13 18:13:38 | [diff] [blame] | 129 | |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 130 | if (bytestream2_get_byte(&gb) != 0x42) { |
Diego Biurrun | 6d97484 | 2013-03-13 20:17:05 | [diff] [blame] | 131 | avpriv_request_sample(avctx, "Unknown record type"); |
Paul B Mahol | 0baec57 | 2012-11-11 11:00:11 | [diff] [blame] | 132 | return AVERROR_INVALIDDATA; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 133 | } |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 134 | if (bytestream2_get_byte(&gb)) { |
Diego Biurrun | 6d97484 | 2013-03-13 20:17:05 | [diff] [blame] | 135 | avpriv_request_sample(avctx, "Padding bytes"); |
Paul B Mahol | 0baec57 | 2012-11-11 11:00:11 | [diff] [blame] | 136 | return AVERROR_PATCHWELCOME; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 137 | } |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 138 | bytestream2_skip(&gb, 2); |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 139 | |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 140 | do { |
| 141 | /* if statements are ordered by probability */ |
Paul B Mahol | 5b4d026 | 2012-03-13 18:13:38 | [diff] [blame] | 142 | #define OP(gb, pixel, count) \ |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 143 | op(&dst, dst_end, (gb), (pixel), (count), &x, avctx->width, s->frame->linesize[0]) |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 144 | |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 145 | int type = bytestream2_get_byte(&gb); |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 146 | count = type & 0x7F; |
| 147 | type >>= 7; |
| 148 | if (count) { |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 149 | if (OP(type ? NULL : &gb, -1, count)) break; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 150 | } else if (!type) { |
| 151 | int pixel; |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 152 | count = bytestream2_get_byte(&gb); /* count==0 gives nop */ |
| 153 | pixel = bytestream2_get_byte(&gb); |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 154 | if (OP(NULL, pixel, count)) break; |
| 155 | } else { |
| 156 | int pixel; |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 157 | type = bytestream2_get_le16(&gb); |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 158 | count = type & 0x3FFF; |
| 159 | type >>= 14; |
| 160 | if (!count) { |
| 161 | if (type == 0) |
| 162 | break; // stop |
| 163 | if (type == 2) { |
Diego Biurrun | 6d97484 | 2013-03-13 20:17:05 | [diff] [blame] | 164 | avpriv_request_sample(avctx, "Unknown opcode"); |
Diego Biurrun | f3298f1 | 2012-12-23 17:10:05 | [diff] [blame] | 165 | return AVERROR_PATCHWELCOME; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 166 | } |
| 167 | continue; |
| 168 | } |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 169 | pixel = type == 3 ? bytestream2_get_byte(&gb) : -1; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 170 | if (type == 1) count += 0x4000; |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 171 | if (OP(type == 2 ? &gb : NULL, pixel, count)) break; |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 172 | } |
Andreas Rheinhardt | a886785 | 2020-05-30 04:14:12 | [diff] [blame] | 173 | } while (bytestream2_get_bytes_left(&gb) > 0); |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 174 | |
Anton Khirnov | 759001c | 2012-11-21 20:34:46 | [diff] [blame] | 175 | memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE); |
Peter Ross | 62931e1 | 2011-04-15 14:59:19 | [diff] [blame] | 176 | |
Anton Khirnov | df9b956 | 2012-11-13 18:35:22 | [diff] [blame] | 177 | *got_frame = 1; |
Andreas Rheinhardt | ce7dbd0 | 2022-03-30 19:33:24 | [diff] [blame] | 178 | if ((ret = av_frame_ref(rframe, s->frame)) < 0) |
Anton Khirnov | 759001c | 2012-11-21 20:34:46 | [diff] [blame] | 179 | return ret; |
| 180 | |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 181 | return buf_size; |
| 182 | } |
| 183 | |
| 184 | static av_cold int decode_end(AVCodecContext *avctx) |
| 185 | { |
| 186 | AnmContext *s = avctx->priv_data; |
Anton Khirnov | 759001c | 2012-11-21 20:34:46 | [diff] [blame] | 187 | |
| 188 | av_frame_free(&s->frame); |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 192 | const FFCodec ff_anm_decoder = { |
| 193 | .p.name = "anm", |
Andreas Rheinhardt | 48286d4 | 2022-08-29 11:38:02 | [diff] [blame^] | 194 | CODEC_LONG_NAME("Deluxe Paint Animation"), |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 195 | .p.type = AVMEDIA_TYPE_VIDEO, |
| 196 | .p.id = AV_CODEC_ID_ANM, |
Anton Khirnov | ec6402b | 2011-07-17 10:54:31 | [diff] [blame] | 197 | .priv_data_size = sizeof(AnmContext), |
| 198 | .init = decode_init, |
| 199 | .close = decode_end, |
Andreas Rheinhardt | 4243da4 | 2022-03-30 21:28:24 | [diff] [blame] | 200 | FF_CODEC_DECODE_CB(decode_frame), |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 201 | .p.capabilities = AV_CODEC_CAP_DR1, |
Peter Ross | a1ae40f | 2010-01-10 05:41:36 | [diff] [blame] | 202 | }; |