mirror of https://github.com/FFmpeg/FFmpeg.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
302 lines
9.7 KiB
302 lines
9.7 KiB
18 years ago
|
/*
|
||
11 years ago
|
* Delphine Software International CIN video decoder
|
||
18 years ago
|
* Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
|
||
|
*
|
||
14 years ago
|
* This file is part of Libav.
|
||
18 years ago
|
*
|
||
14 years ago
|
* Libav is free software; you can redistribute it and/or
|
||
18 years ago
|
* modify it under the terms of the GNU Lesser General Public
|
||
|
* License as published by the Free Software Foundation; either
|
||
|
* version 2.1 of the License, or (at your option) any later version.
|
||
|
*
|
||
14 years ago
|
* Libav is distributed in the hope that it will be useful,
|
||
18 years ago
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* Lesser General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Lesser General Public
|
||
14 years ago
|
* License along with Libav; if not, write to the Free Software
|
||
18 years ago
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
|
*/
|
||
|
|
||
|
/**
|
||
15 years ago
|
* @file
|
||
11 years ago
|
* Delphine Software International CIN video decoder
|
||
18 years ago
|
*/
|
||
|
|
||
|
#include "avcodec.h"
|
||
18 years ago
|
#include "bytestream.h"
|
||
12 years ago
|
#include "internal.h"
|
||
18 years ago
|
|
||
|
typedef enum CinVideoBitmapIndex {
|
||
|
CIN_CUR_BMP = 0, /* current */
|
||
|
CIN_PRE_BMP = 1, /* previous */
|
||
|
CIN_INT_BMP = 2 /* intermediate */
|
||
|
} CinVideoBitmapIndex;
|
||
|
|
||
|
typedef struct CinVideoContext {
|
||
|
AVCodecContext *avctx;
|
||
11 years ago
|
AVFrame *frame;
|
||
18 years ago
|
unsigned int bitmap_size;
|
||
|
uint32_t palette[256];
|
||
|
uint8_t *bitmap_table[3];
|
||
|
} CinVideoContext;
|
||
|
|
||
17 years ago
|
static av_cold int cinvideo_decode_init(AVCodecContext *avctx)
|
||
18 years ago
|
{
|
||
18 years ago
|
CinVideoContext *cin = avctx->priv_data;
|
||
18 years ago
|
unsigned int i;
|
||
|
|
||
|
cin->avctx = avctx;
|
||
12 years ago
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
||
18 years ago
|
|
||
11 years ago
|
cin->frame = av_frame_alloc();
|
||
|
if (!cin->frame)
|
||
|
return AVERROR(ENOMEM);
|
||
18 years ago
|
|
||
|
cin->bitmap_size = avctx->width * avctx->height;
|
||
|
for (i = 0; i < 3; ++i) {
|
||
|
cin->bitmap_table[i] = av_mallocz(cin->bitmap_size);
|
||
|
if (!cin->bitmap_table[i])
|
||
|
av_log(avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n");
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
11 years ago
|
static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
|
||
|
int size)
|
||
18 years ago
|
{
|
||
|
while (size--)
|
||
|
*dst++ += *src++;
|
||
|
}
|
||
|
|
||
11 years ago
|
static int cin_decode_huffman(const unsigned char *src, int src_size,
|
||
|
unsigned char *dst, int dst_size)
|
||
18 years ago
|
{
|
||
|
int b, huff_code = 0;
|
||
|
unsigned char huff_code_table[15];
|
||
11 years ago
|
unsigned char *dst_cur = dst;
|
||
|
unsigned char *dst_end = dst + dst_size;
|
||
18 years ago
|
const unsigned char *src_end = src + src_size;
|
||
|
|
||
11 years ago
|
memcpy(huff_code_table, src, 15);
|
||
|
src += 15;
|
||
18 years ago
|
|
||
|
while (src < src_end) {
|
||
|
huff_code = *src++;
|
||
|
if ((huff_code >> 4) == 15) {
|
||
11 years ago
|
b = huff_code << 4;
|
||
|
huff_code = *src++;
|
||
18 years ago
|
*dst_cur++ = b | (huff_code >> 4);
|
||
|
} else
|
||
|
*dst_cur++ = huff_code_table[huff_code >> 4];
|
||
|
if (dst_cur >= dst_end)
|
||
|
break;
|
||
|
|
||
|
huff_code &= 15;
|
||
|
if (huff_code == 15) {
|
||
|
*dst_cur++ = *src++;
|
||
|
} else
|
||
|
*dst_cur++ = huff_code_table[huff_code];
|
||
|
if (dst_cur >= dst_end)
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
return dst_cur - dst;
|
||
|
}
|
||
|
|
||
11 years ago
|
static int cin_decode_lzss(const unsigned char *src, int src_size,
|
||
|
unsigned char *dst, int dst_size)
|
||
18 years ago
|
{
|
||
|
uint16_t cmd;
|
||
|
int i, sz, offset, code;
|
||
11 years ago
|
unsigned char *dst_end = dst + dst_size, *dst_start = dst;
|
||
18 years ago
|
const unsigned char *src_end = src + src_size;
|
||
|
|
||
|
while (src < src_end && dst < dst_end) {
|
||
|
code = *src++;
|
||
|
for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
|
||
|
if (code & (1 << i)) {
|
||
|
*dst++ = *src++;
|
||
|
} else {
|
||
11 years ago
|
cmd = AV_RL16(src);
|
||
|
src += 2;
|
||
18 years ago
|
offset = cmd >> 4;
|
||
11 years ago
|
if ((int)(dst - dst_start) < offset + 1)
|
||
13 years ago
|
return AVERROR_INVALIDDATA;
|
||
18 years ago
|
sz = (cmd & 0xF) + 2;
|
||
11 years ago
|
/* don't use memcpy/memmove here as the decoding routine
|
||
|
* (ab)uses buffer overlappings to repeat bytes in the
|
||
|
* destination */
|
||
18 years ago
|
sz = FFMIN(sz, dst_end - dst);
|
||
|
while (sz--) {
|
||
|
*dst = *(dst - offset - 1);
|
||
|
++dst;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
13 years ago
|
|
||
|
return 0;
|
||
18 years ago
|
}
|
||
|
|
||
11 years ago
|
static void cin_decode_rle(const unsigned char *src, int src_size,
|
||
|
unsigned char *dst, int dst_size)
|
||
18 years ago
|
{
|
||
|
int len, code;
|
||
11 years ago
|
unsigned char *dst_end = dst + dst_size;
|
||
18 years ago
|
const unsigned char *src_end = src + src_size;
|
||
|
|
||
|
while (src < src_end && dst < dst_end) {
|
||
|
code = *src++;
|
||
|
if (code & 0x80) {
|
||
11 years ago
|
if (src >= src_end)
|
||
|
break;
|
||
18 years ago
|
len = code - 0x7F;
|
||
|
memset(dst, *src++, FFMIN(len, dst_end - dst));
|
||
|
} else {
|
||
|
len = code + 1;
|
||
11 years ago
|
memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
|
||
18 years ago
|
src += len;
|
||
|
}
|
||
|
dst += len;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static int cinvideo_decode_frame(AVCodecContext *avctx,
|
||
12 years ago
|
void *data, int *got_frame,
|
||
16 years ago
|
AVPacket *avpkt)
|
||
18 years ago
|
{
|
||
11 years ago
|
const uint8_t *buf = avpkt->data;
|
||
|
int buf_size = avpkt->size;
|
||
18 years ago
|
CinVideoContext *cin = avctx->priv_data;
|
||
11 years ago
|
int i, y, palette_type, palette_colors_count,
|
||
|
bitmap_frame_type, bitmap_frame_size, res = 0;
|
||
18 years ago
|
|
||
11 years ago
|
palette_type = buf[0];
|
||
|
palette_colors_count = AV_RL16(buf + 1);
|
||
|
bitmap_frame_type = buf[3];
|
||
|
buf += 4;
|
||
18 years ago
|
|
||
|
bitmap_frame_size = buf_size - 4;
|
||
|
|
||
|
/* handle palette */
|
||
13 years ago
|
if (bitmap_frame_size < palette_colors_count * (3 + (palette_type != 0)))
|
||
|
return AVERROR_INVALIDDATA;
|
||
18 years ago
|
if (palette_type == 0) {
|
||
13 years ago
|
if (palette_colors_count > 256)
|
||
|
return AVERROR_INVALIDDATA;
|
||
18 years ago
|
for (i = 0; i < palette_colors_count; ++i) {
|
||
11 years ago
|
cin->palette[i] = bytestream_get_le24(&buf);
|
||
18 years ago
|
bitmap_frame_size -= 3;
|
||
|
}
|
||
|
} else {
|
||
|
for (i = 0; i < palette_colors_count; ++i) {
|
||
11 years ago
|
cin->palette[buf[0]] = AV_RL24(buf + 1);
|
||
|
buf += 4;
|
||
|
bitmap_frame_size -= 4;
|
||
18 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
bitmap_frame_size = FFMIN(cin->bitmap_size, bitmap_frame_size);
|
||
|
|
||
11 years ago
|
/* note: the decoding routines below assumes that
|
||
|
* surface.width = surface.pitch */
|
||
18 years ago
|
switch (bitmap_frame_type) {
|
||
|
case 9:
|
||
|
cin_decode_rle(buf, bitmap_frame_size,
|
||
11 years ago
|
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
|
||
18 years ago
|
break;
|
||
|
case 34:
|
||
|
cin_decode_rle(buf, bitmap_frame_size,
|
||
11 years ago
|
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
|
||
18 years ago
|
cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
|
||
11 years ago
|
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
|
||
18 years ago
|
break;
|
||
|
case 35:
|
||
|
cin_decode_huffman(buf, bitmap_frame_size,
|
||
11 years ago
|
cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
|
||
18 years ago
|
cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
|
||
11 years ago
|
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
|
||
18 years ago
|
break;
|
||
|
case 36:
|
||
|
bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
|
||
11 years ago
|
cin->bitmap_table[CIN_INT_BMP],
|
||
|
cin->bitmap_size);
|
||
18 years ago
|
cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
|
||
11 years ago
|
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
|
||
18 years ago
|
cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
|
||
11 years ago
|
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
|
||
18 years ago
|
break;
|
||
|
case 37:
|
||
|
cin_decode_huffman(buf, bitmap_frame_size,
|
||
11 years ago
|
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
|
||
18 years ago
|
break;
|
||
|
case 38:
|
||
13 years ago
|
res = cin_decode_lzss(buf, bitmap_frame_size,
|
||
|
cin->bitmap_table[CIN_CUR_BMP],
|
||
|
cin->bitmap_size);
|
||
|
if (res < 0)
|
||
|
return res;
|
||
18 years ago
|
break;
|
||
|
case 39:
|
||
13 years ago
|
res = cin_decode_lzss(buf, bitmap_frame_size,
|
||
|
cin->bitmap_table[CIN_CUR_BMP],
|
||
|
cin->bitmap_size);
|
||
|
if (res < 0)
|
||
|
return res;
|
||
18 years ago
|
cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
|
||
11 years ago
|
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
|
||
18 years ago
|
break;
|
||
|
}
|
||
|
|
||
11 years ago
|
if ((res = ff_reget_buffer(avctx, cin->frame)) < 0) {
|
||
11 years ago
|
av_log(cin->avctx, AV_LOG_ERROR,
|
||
|
"delphinecinvideo: reget_buffer() failed to allocate a frame\n");
|
||
12 years ago
|
return res;
|
||
13 years ago
|
}
|
||
|
|
||
11 years ago
|
memcpy(cin->frame->data[1], cin->palette, sizeof(cin->palette));
|
||
|
cin->frame->palette_has_changed = 1;
|
||
18 years ago
|
for (y = 0; y < cin->avctx->height; ++y)
|
||
11 years ago
|
memcpy(cin->frame->data[0] + (cin->avctx->height - 1 - y) * cin->frame->linesize[0],
|
||
11 years ago
|
cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
|
||
|
cin->avctx->width);
|
||
18 years ago
|
|
||
11 years ago
|
FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP],
|
||
|
cin->bitmap_table[CIN_PRE_BMP]);
|
||
18 years ago
|
|
||
11 years ago
|
if ((res = av_frame_ref(data, cin->frame)) < 0)
|
||
12 years ago
|
return res;
|
||
|
|
||
12 years ago
|
*got_frame = 1;
|
||
18 years ago
|
|
||
|
return buf_size;
|
||
|
}
|
||
|
|
||
17 years ago
|
static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
|
||
18 years ago
|
{
|
||
18 years ago
|
CinVideoContext *cin = avctx->priv_data;
|
||
18 years ago
|
int i;
|
||
|
|
||
11 years ago
|
av_frame_free(&cin->frame);
|
||
18 years ago
|
|
||
|
for (i = 0; i < 3; ++i)
|
||
|
av_free(cin->bitmap_table[i]);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
14 years ago
|
AVCodec ff_dsicinvideo_decoder = {
|
||
14 years ago
|
.name = "dsicinvideo",
|
||
11 years ago
|
.long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
|
||
14 years ago
|
.type = AVMEDIA_TYPE_VIDEO,
|
||
12 years ago
|
.id = AV_CODEC_ID_DSICINVIDEO,
|
||
14 years ago
|
.priv_data_size = sizeof(CinVideoContext),
|
||
|
.init = cinvideo_decode_init,
|
||
|
.close = cinvideo_decode_end,
|
||
|
.decode = cinvideo_decode_frame,
|
||
|
.capabilities = CODEC_CAP_DR1,
|
||
18 years ago
|
};
|