2 * Deluxe Paint Animation decoder
3 * Copyright (c) 2009 Peter Ross
5 * This file is part of FFmpeg.
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.
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.
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
24 * Deluxe Paint Animation decoder
28 #include "bytestream.h"
31 typedef struct AnmContext {
33 int palette[AVPALETTE_COUNT];
36 static av_cold int decode_init(AVCodecContext *avctx)
38 AnmContext *s = avctx->priv_data;
42 if (avctx->extradata_size < 16 * 8 + 4 * 256)
43 return AVERROR_INVALIDDATA;
45 avctx->pix_fmt = AV_PIX_FMT_PAL8;
47 s->frame = av_frame_alloc();
49 return AVERROR(ENOMEM);
51 bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
52 bytestream2_skipu(&gb, 16 * 8);
53 for (i = 0; i < 256; i++)
54 s->palette[i] = (0xFFU << 24) | bytestream2_get_le32u(&gb);
60 * Perform decode operation
61 * @param dst pointer to destination image buffer
62 * @param dst_end pointer to end of destination image buffer
63 * @param gb GetByteContext (optional, see below)
64 * @param pixel Fill color (optional, see below)
65 * @param count Pixel count
66 * @param x Pointer to x-axis counter
67 * @param width Image width
68 * @param linesize Destination image buffer linesize
69 * @return non-zero if destination buffer is exhausted
71 * a copy operation is achieved when 'gb' is set
72 * a fill operation is achieved when 'gb' is null and pixel is >= 0
73 * a skip operation is achieved when 'gb' is null and pixel is < 0
75 static inline int op(uint8_t **dst, const uint8_t *dst_end,
78 int *x, int width, int linesize)
80 int remaining = width - *x;
82 int striplen = FFMIN(count, remaining);
84 if (bytestream2_get_bytes_left(gb) < striplen)
86 bytestream2_get_bufferu(gb, *dst, striplen);
87 } else if (pixel >= 0)
88 memset(*dst, pixel, striplen);
90 remaining -= striplen;
93 *dst += linesize - width;
97 if (*dst >= dst_end) goto exhausted;
99 if (*dst <= dst_end) goto exhausted;
102 *x = width - remaining;
106 *x = width - remaining;
110 static int decode_frame(AVCodecContext *avctx,
111 void *data, int *got_frame,
114 AnmContext *s = avctx->priv_data;
115 const int buf_size = avpkt->size;
116 uint8_t *dst, *dst_end;
118 int count, ret, x = 0;
121 return AVERROR_INVALIDDATA;
123 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
125 dst = s->frame->data[0];
126 dst_end = s->frame->data[0] + s->frame->linesize[0]*avctx->height;
128 bytestream2_init(&gb, avpkt->data, buf_size);
130 if (bytestream2_get_byte(&gb) != 0x42) {
131 avpriv_request_sample(avctx, "Unknown record type");
132 return AVERROR_INVALIDDATA;
134 if (bytestream2_get_byte(&gb)) {
135 avpriv_request_sample(avctx, "Padding bytes");
136 return AVERROR_PATCHWELCOME;
138 bytestream2_skip(&gb, 2);
141 /* if statements are ordered by probability */
142 #define OP(gb, pixel, count) \
143 op(&dst, dst_end, (gb), (pixel), (count), &x, avctx->width, s->frame->linesize[0])
145 int type = bytestream2_get_byte(&gb);
149 if (OP(type ? NULL : &gb, -1, count)) break;
152 count = bytestream2_get_byte(&gb); /* count==0 gives nop */
153 pixel = bytestream2_get_byte(&gb);
154 if (OP(NULL, pixel, count)) break;
157 type = bytestream2_get_le16(&gb);
158 count = type & 0x3FFF;
164 avpriv_request_sample(avctx, "Unknown opcode");
165 return AVERROR_PATCHWELCOME;
169 pixel = type == 3 ? bytestream2_get_byte(&gb) : -1;
170 if (type == 1) count += 0x4000;
171 if (OP(type == 2 ? &gb : NULL, pixel, count)) break;
173 } while (bytestream2_get_bytes_left(&gb) > 0);
175 memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
178 if ((ret = av_frame_ref(data, s->frame)) < 0)
184 static av_cold int decode_end(AVCodecContext *avctx)
186 AnmContext *s = avctx->priv_data;
188 av_frame_free(&s->frame);
192 const AVCodec ff_anm_decoder = {
194 .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
195 .type = AVMEDIA_TYPE_VIDEO,
196 .id = AV_CODEC_ID_ANM,
197 .priv_data_size = sizeof(AnmContext),
200 .decode = decode_frame,
201 .capabilities = AV_CODEC_CAP_DR1,
202 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,