2 * Deluxe Paint Animation decoder
3 * Copyright (c) 2009 Peter Ross
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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"
30 typedef struct AnmContext {
32 int x; ///< x coordinate position
35 static av_cold int decode_init(AVCodecContext *avctx)
37 AnmContext *s = avctx->priv_data;
41 avctx->pix_fmt = PIX_FMT_PAL8;
43 if (avctx->extradata_size != 16*8 + 4*256)
46 s->frame.reference = 1;
47 if (avctx->get_buffer(avctx, &s->frame) < 0) {
48 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
52 buf = avctx->extradata + 16*8;
53 for (i = 0; i < 256; i++)
54 ((uint32_t*)s->frame.data[1])[i] = bytestream_get_le32(&buf);
60 * Perform decode operation
61 * @param dst, dst_end Destination image buffer
62 * @param buf, buf_end Source buffer (optional, see below)
63 * @param pixel Fill color (optional, see below)
64 * @param count Pixel count
65 * @param x Pointer to x-axis counter
66 * @param width Image width
67 * @param linesize Destination image buffer linesize
68 * @return non-zero if destination buffer is exhausted
70 * a copy operation is achieved when 'buf' is set
71 * a fill operation is acheived when 'buf' is null and pixel is >= 0
72 * a skip operation is acheived when 'buf' is null and pixel is < 0
74 static inline int op(uint8_t **dst, const uint8_t *dst_end,
75 const uint8_t **buf, const uint8_t *buf_end,
77 int *x, int width, int linesize)
79 int remaining = width - *x;
81 int striplen = FFMIN(count, remaining);
83 striplen = FFMIN(striplen, buf_end - *buf);
86 memcpy(*dst, *buf, striplen);
88 } else if (pixel >= 0)
89 memset(*dst, pixel, striplen);
91 remaining -= striplen;
94 *dst += linesize - width;
98 if (*dst >= dst_end) goto exhausted;
100 if (*dst <= dst_end) goto exhausted;
103 *x = width - remaining;
107 *x = width - remaining;
111 static int decode_frame(AVCodecContext *avctx,
112 void *data, int *data_size,
115 AnmContext *s = avctx->priv_data;
116 const uint8_t *buf = avpkt->data;
117 const int buf_size = avpkt->size;
118 const uint8_t *buf_end = buf + buf_size;
119 uint8_t *dst, *dst_end;
122 if(avctx->reget_buffer(avctx, &s->frame) < 0){
123 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
126 dst = s->frame.data[0];
127 dst_end = s->frame.data[0] + s->frame.linesize[0]*avctx->height;
129 if (buf[0] != 0x42) {
130 av_log_ask_for_sample(avctx, "unknown record type\n");
134 av_log_ask_for_sample(avctx, "padding bytes not supported\n");
141 /* if statements are ordered by probability */
142 #define OP(buf, pixel, count) \
143 op(&dst, dst_end, (buf), buf_end, (pixel), (count), &s->x, avctx->width, s->frame.linesize[0])
145 int type = bytestream_get_byte(&buf);
149 if (OP(type ? NULL : &buf, -1, count)) break;
152 count = bytestream_get_byte(&buf); /* count==0 gives nop */
153 pixel = bytestream_get_byte(&buf);
154 if (OP(NULL, pixel, count)) break;
157 type = bytestream_get_le16(&buf);
158 count = type & 0x3FFF;
164 av_log_ask_for_sample(avctx, "unknown opcode");
165 return AVERROR_INVALIDDATA;
169 pixel = type == 3 ? bytestream_get_byte(&buf) : -1;
170 if (type == 1) count += 0x4000;
171 if (OP(type == 2 ? &buf : NULL, pixel, count)) break;
173 } while (buf + 1 < buf_end);
175 *data_size = sizeof(AVFrame);
176 *(AVFrame*)data = s->frame;
180 static av_cold int decode_end(AVCodecContext *avctx)
182 AnmContext *s = avctx->priv_data;
183 if (s->frame.data[0])
184 avctx->release_buffer(avctx, &s->frame);
188 AVCodec ff_anm_decoder = {
190 .type = AVMEDIA_TYPE_VIDEO,
192 .priv_data_size = sizeof(AnmContext),
195 .decode = decode_frame,
196 .capabilities = CODEC_CAP_DR1,
197 .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),