blob: e9b19d880d9e68008b08051babfb0d237a2b2957 [file] [log] [blame]
Peter Rossa1ae40f2010-01-10 05:41:361/*
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 Biurrunba87f082010-04-20 14:45:3423 * @file
Peter Rossa1ae40f2010-01-10 05:41:3624 * Deluxe Paint Animation decoder
25 */
26
27#include "avcodec.h"
28#include "bytestream.h"
Anton Khirnov759001c2012-11-21 20:34:4629#include "internal.h"
Peter Rossa1ae40f2010-01-10 05:41:3630
31typedef struct AnmContext {
Anton Khirnov759001c2012-11-21 20:34:4632 AVFrame *frame;
Peter Ross62931e12011-04-15 14:59:1933 int palette[AVPALETTE_COUNT];
Paul B Mahol5b4d0262012-03-13 18:13:3834 GetByteContext gb;
Peter Rossa1ae40f2010-01-10 05:41:3635 int x; ///< x coordinate position
36} AnmContext;
37
38static av_cold int decode_init(AVCodecContext *avctx)
39{
40 AnmContext *s = avctx->priv_data;
Peter Rossa1ae40f2010-01-10 05:41:3641 int i;
42
Andreas Rheinhardt45c35022020-05-30 03:25:0643 if (avctx->extradata_size < 16 * 8 + 4 * 256)
44 return AVERROR_INVALIDDATA;
45
Anton Khirnov716d4132012-10-06 10:10:3446 avctx->pix_fmt = AV_PIX_FMT_PAL8;
Peter Rossa1ae40f2010-01-10 05:41:3647
Anton Khirnov759001c2012-11-21 20:34:4648 s->frame = av_frame_alloc();
49 if (!s->frame)
50 return AVERROR(ENOMEM);
51
Paul B Mahol5b4d0262012-03-13 18:13:3852 bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
Paul B Mahol5b4d0262012-03-13 18:13:3853 bytestream2_skipu(&s->gb, 16 * 8);
Peter Rossa1ae40f2010-01-10 05:41:3654 for (i = 0; i < 256; i++)
Marton Balinte894d952018-04-23 18:46:2555 s->palette[i] = (0xFFU << 24) | bytestream2_get_le32u(&s->gb);
Peter Rossa1ae40f2010-01-10 05:41:3656
57 return 0;
58}
59
60/**
61 * Perform decode operation
Diego Biurrun4051be62012-07-02 18:40:2662 * @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 Rossa1ae40f2010-01-10 05:41:3665 * @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 Mahol5b4d0262012-03-13 18:13:3872 * a copy operation is achieved when 'gb' is set
Diego Biurrun09f21192012-06-13 09:41:1273 * 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 Rossa1ae40f2010-01-10 05:41:3675 */
76static inline int op(uint8_t **dst, const uint8_t *dst_end,
Paul B Mahol5b4d0262012-03-13 18:13:3877 GetByteContext *gb,
Peter Rossa1ae40f2010-01-10 05:41:3678 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 Mahol5b4d0262012-03-13 18:13:3884 if (gb) {
85 if (bytestream2_get_bytes_left(gb) < striplen)
Laurent Aimar39993862011-09-30 22:44:5886 goto exhausted;
Paul B Mahol5b4d0262012-03-13 18:13:3887 bytestream2_get_bufferu(gb, *dst, striplen);
Peter Rossa1ae40f2010-01-10 05:41:3688 } 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
106exhausted:
107 *x = width - remaining;
108 return 1;
109}
110
111static int decode_frame(AVCodecContext *avctx,
Anton Khirnovdf9b9562012-11-13 18:35:22112 void *data, int *got_frame,
Peter Rossa1ae40f2010-01-10 05:41:36113 AVPacket *avpkt)
114{
115 AnmContext *s = avctx->priv_data;
Peter Rossa1ae40f2010-01-10 05:41:36116 const int buf_size = avpkt->size;
Peter Rossa1ae40f2010-01-10 05:41:36117 uint8_t *dst, *dst_end;
Paul B Mahol0baec572012-11-11 11:00:11118 int count, ret;
Peter Rossa1ae40f2010-01-10 05:41:36119
Michael Niedermayer19651612019-08-15 19:00:54120 if (buf_size < 7)
121 return AVERROR_INVALIDDATA;
122
James Almer9ea6d212019-08-30 14:37:25123 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
Paul B Mahol0baec572012-11-11 11:00:11124 return ret;
Anton Khirnov759001c2012-11-21 20:34:46125 dst = s->frame->data[0];
126 dst_end = s->frame->data[0] + s->frame->linesize[0]*avctx->height;
Peter Rossa1ae40f2010-01-10 05:41:36127
Paul B Mahol5b4d0262012-03-13 18:13:38128 bytestream2_init(&s->gb, avpkt->data, buf_size);
129
130 if (bytestream2_get_byte(&s->gb) != 0x42) {
Diego Biurrun6d974842013-03-13 20:17:05131 avpriv_request_sample(avctx, "Unknown record type");
Paul B Mahol0baec572012-11-11 11:00:11132 return AVERROR_INVALIDDATA;
Peter Rossa1ae40f2010-01-10 05:41:36133 }
Paul B Mahol5b4d0262012-03-13 18:13:38134 if (bytestream2_get_byte(&s->gb)) {
Diego Biurrun6d974842013-03-13 20:17:05135 avpriv_request_sample(avctx, "Padding bytes");
Paul B Mahol0baec572012-11-11 11:00:11136 return AVERROR_PATCHWELCOME;
Peter Rossa1ae40f2010-01-10 05:41:36137 }
Paul B Mahol5b4d0262012-03-13 18:13:38138 bytestream2_skip(&s->gb, 2);
Peter Rossa1ae40f2010-01-10 05:41:36139
140 s->x = 0;
141 do {
142 /* if statements are ordered by probability */
Paul B Mahol5b4d0262012-03-13 18:13:38143#define OP(gb, pixel, count) \
Anton Khirnov759001c2012-11-21 20:34:46144 op(&dst, dst_end, (gb), (pixel), (count), &s->x, avctx->width, s->frame->linesize[0])
Peter Rossa1ae40f2010-01-10 05:41:36145
Paul B Mahol5b4d0262012-03-13 18:13:38146 int type = bytestream2_get_byte(&s->gb);
Peter Rossa1ae40f2010-01-10 05:41:36147 count = type & 0x7F;
148 type >>= 7;
149 if (count) {
Paul B Mahol5b4d0262012-03-13 18:13:38150 if (OP(type ? NULL : &s->gb, -1, count)) break;
Peter Rossa1ae40f2010-01-10 05:41:36151 } else if (!type) {
152 int pixel;
Paul B Mahol5b4d0262012-03-13 18:13:38153 count = bytestream2_get_byte(&s->gb); /* count==0 gives nop */
154 pixel = bytestream2_get_byte(&s->gb);
Peter Rossa1ae40f2010-01-10 05:41:36155 if (OP(NULL, pixel, count)) break;
156 } else {
157 int pixel;
Paul B Mahol5b4d0262012-03-13 18:13:38158 type = bytestream2_get_le16(&s->gb);
Peter Rossa1ae40f2010-01-10 05:41:36159 count = type & 0x3FFF;
160 type >>= 14;
161 if (!count) {
162 if (type == 0)
163 break; // stop
164 if (type == 2) {
Diego Biurrun6d974842013-03-13 20:17:05165 avpriv_request_sample(avctx, "Unknown opcode");
Diego Biurrunf3298f12012-12-23 17:10:05166 return AVERROR_PATCHWELCOME;
Peter Rossa1ae40f2010-01-10 05:41:36167 }
168 continue;
169 }
Paul B Mahol5b4d0262012-03-13 18:13:38170 pixel = type == 3 ? bytestream2_get_byte(&s->gb) : -1;
Peter Rossa1ae40f2010-01-10 05:41:36171 if (type == 1) count += 0x4000;
Paul B Mahol5b4d0262012-03-13 18:13:38172 if (OP(type == 2 ? &s->gb : NULL, pixel, count)) break;
Peter Rossa1ae40f2010-01-10 05:41:36173 }
Paul B Mahol5b4d0262012-03-13 18:13:38174 } while (bytestream2_get_bytes_left(&s->gb) > 0);
Peter Rossa1ae40f2010-01-10 05:41:36175
Anton Khirnov759001c2012-11-21 20:34:46176 memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
Peter Ross62931e12011-04-15 14:59:19177
Anton Khirnovdf9b9562012-11-13 18:35:22178 *got_frame = 1;
Anton Khirnov759001c2012-11-21 20:34:46179 if ((ret = av_frame_ref(data, s->frame)) < 0)
180 return ret;
181
Peter Rossa1ae40f2010-01-10 05:41:36182 return buf_size;
183}
184
185static av_cold int decode_end(AVCodecContext *avctx)
186{
187 AnmContext *s = avctx->priv_data;
Anton Khirnov759001c2012-11-21 20:34:46188
189 av_frame_free(&s->frame);
Peter Rossa1ae40f2010-01-10 05:41:36190 return 0;
191}
192
Diego Elio Pettenòe7e2df22011-01-25 21:40:11193AVCodec ff_anm_decoder = {
Anton Khirnovec6402b2011-07-17 10:54:31194 .name = "anm",
Diego Biurrunb2bed932013-10-03 20:57:53195 .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
Anton Khirnovec6402b2011-07-17 10:54:31196 .type = AVMEDIA_TYPE_VIDEO,
Anton Khirnov36ef5362012-08-05 09:11:04197 .id = AV_CODEC_ID_ANM,
Anton Khirnovec6402b2011-07-17 10:54:31198 .priv_data_size = sizeof(AnmContext),
199 .init = decode_init,
200 .close = decode_end,
201 .decode = decode_frame,
Vittorio Giovaradef97852015-07-07 00:41:27202 .capabilities = AV_CODEC_CAP_DR1,
Derek Buitenhuis91ed4e72017-02-07 16:36:38203 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
Peter Rossa1ae40f2010-01-10 05:41:36204};