2 * Delphine Software International CIN video decoder
3 * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
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 * Delphine Software International CIN video decoder
28 #include "bytestream.h"
31 typedef enum CinVideoBitmapIndex {
32 CIN_CUR_BMP = 0, /* current */
33 CIN_PRE_BMP = 1, /* previous */
34 CIN_INT_BMP = 2 /* intermediate */
35 } CinVideoBitmapIndex;
37 typedef struct CinVideoContext {
38 AVCodecContext *avctx;
40 unsigned int bitmap_size;
41 uint32_t palette[256];
42 uint8_t *bitmap_table[3];
45 static av_cold void destroy_buffers(CinVideoContext *cin)
49 for (i = 0; i < 3; ++i)
50 av_freep(&cin->bitmap_table[i]);
53 static av_cold int allocate_buffers(CinVideoContext *cin)
57 for (i = 0; i < 3; ++i) {
58 cin->bitmap_table[i] = av_mallocz(cin->bitmap_size);
59 if (!cin->bitmap_table[i]) {
60 av_log(cin->avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n");
62 return AVERROR(ENOMEM);
69 static av_cold int cinvideo_decode_init(AVCodecContext *avctx)
71 CinVideoContext *cin = avctx->priv_data;
74 avctx->pix_fmt = AV_PIX_FMT_PAL8;
76 cin->frame = av_frame_alloc();
78 return AVERROR(ENOMEM);
80 cin->bitmap_size = avctx->width * avctx->height;
81 if (allocate_buffers(cin))
82 return AVERROR(ENOMEM);
87 static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
94 static int cin_decode_huffman(const unsigned char *src, int src_size,
95 unsigned char *dst, int dst_size)
98 unsigned char huff_code_table[15];
99 unsigned char *dst_cur = dst;
100 unsigned char *dst_end = dst + dst_size;
101 const unsigned char *src_end = src + src_size;
103 memcpy(huff_code_table, src, 15);
106 while (src < src_end) {
108 if ((huff_code >> 4) == 15) {
111 *dst_cur++ = b | (huff_code >> 4);
113 *dst_cur++ = huff_code_table[huff_code >> 4];
114 if (dst_cur >= dst_end)
118 if (huff_code == 15) {
121 *dst_cur++ = huff_code_table[huff_code];
122 if (dst_cur >= dst_end)
126 return dst_cur - dst;
129 static int cin_decode_lzss(const unsigned char *src, int src_size,
130 unsigned char *dst, int dst_size)
133 int i, sz, offset, code;
134 unsigned char *dst_end = dst + dst_size, *dst_start = dst;
135 const unsigned char *src_end = src + src_size;
137 while (src < src_end && dst < dst_end) {
139 for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
140 if (code & (1 << i)) {
146 if ((int)(dst - dst_start) < offset + 1)
147 return AVERROR_INVALIDDATA;
148 sz = (cmd & 0xF) + 2;
149 /* don't use memcpy/memmove here as the decoding routine
150 * (ab)uses buffer overlappings to repeat bytes in the
152 sz = FFMIN(sz, dst_end - dst);
154 *dst = *(dst - offset - 1);
161 if (dst_end - dst > dst_size - dst_size/10)
162 return AVERROR_INVALIDDATA;
167 static int cin_decode_rle(const unsigned char *src, int src_size,
168 unsigned char *dst, int dst_size)
171 unsigned char *dst_end = dst + dst_size;
172 const unsigned char *src_end = src + src_size;
174 while (src + 1 < src_end && dst < dst_end) {
178 memset(dst, *src++, FFMIN(len, dst_end - dst));
181 if (len > src_end-src) {
182 av_log(NULL, AV_LOG_ERROR, "RLE overread\n");
183 return AVERROR_INVALIDDATA;
185 memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
191 if (dst_end - dst > dst_size - dst_size/10)
192 return AVERROR_INVALIDDATA;
197 static int cinvideo_decode_frame(AVCodecContext *avctx,
198 void *data, int *got_frame,
201 const uint8_t *buf = avpkt->data;
202 int buf_size = avpkt->size;
203 CinVideoContext *cin = avctx->priv_data;
204 int i, y, palette_type, palette_colors_count,
205 bitmap_frame_type, bitmap_frame_size, res = 0;
207 palette_type = buf[0];
208 palette_colors_count = AV_RL16(buf + 1);
209 bitmap_frame_type = buf[3];
212 bitmap_frame_size = buf_size - 4;
215 if (bitmap_frame_size < palette_colors_count * (3 + (palette_type != 0)))
216 return AVERROR_INVALIDDATA;
217 if (palette_type == 0) {
218 if (palette_colors_count > 256)
219 return AVERROR_INVALIDDATA;
220 for (i = 0; i < palette_colors_count; ++i) {
221 cin->palette[i] = 0xFFU << 24 | bytestream_get_le24(&buf);
222 bitmap_frame_size -= 3;
225 for (i = 0; i < palette_colors_count; ++i) {
226 cin->palette[buf[0]] = 0xFFU << 24 | AV_RL24(buf + 1);
228 bitmap_frame_size -= 4;
232 /* note: the decoding routines below assumes that
233 * surface.width = surface.pitch */
234 switch (bitmap_frame_type) {
236 res = cin_decode_rle(buf, bitmap_frame_size,
237 cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
242 res = cin_decode_rle(buf, bitmap_frame_size,
243 cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
246 cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
247 cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
250 bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
251 cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
252 res = cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
253 cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
258 bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
259 cin->bitmap_table[CIN_INT_BMP],
261 res = cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
262 cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
265 cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
266 cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
269 cin_decode_huffman(buf, bitmap_frame_size,
270 cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
273 res = cin_decode_lzss(buf, bitmap_frame_size,
274 cin->bitmap_table[CIN_CUR_BMP],
280 res = cin_decode_lzss(buf, bitmap_frame_size,
281 cin->bitmap_table[CIN_CUR_BMP],
285 cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
286 cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
290 if ((res = ff_reget_buffer(avctx, cin->frame)) < 0)
293 memcpy(cin->frame->data[1], cin->palette, sizeof(cin->palette));
294 cin->frame->palette_has_changed = 1;
295 for (y = 0; y < cin->avctx->height; ++y)
296 memcpy(cin->frame->data[0] + (cin->avctx->height - 1 - y) * cin->frame->linesize[0],
297 cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
300 FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP],
301 cin->bitmap_table[CIN_PRE_BMP]);
303 if ((res = av_frame_ref(data, cin->frame)) < 0)
311 static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
313 CinVideoContext *cin = avctx->priv_data;
315 av_frame_free(&cin->frame);
317 destroy_buffers(cin);
322 AVCodec ff_dsicinvideo_decoder = {
323 .name = "dsicinvideo",
324 .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
325 .type = AVMEDIA_TYPE_VIDEO,
326 .id = AV_CODEC_ID_DSICINVIDEO,
327 .priv_data_size = sizeof(CinVideoContext),
328 .init = cinvideo_decode_init,
329 .close = cinvideo_decode_end,
330 .decode = cinvideo_decode_frame,
331 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
332 .capabilities = AV_CODEC_CAP_DR1,