From db9fced85da900bf083e1d2242298e1a460862b2 Mon Sep 17 00:00:00 2001 From: Alyssa Milburn Date: Wed, 16 Nov 2011 10:24:04 +0100 Subject: [PATCH] CDToons decoder This adds a decoder for Broderbund's sprite-based QuickTime CDToons codec, based on the decoder I wrote for ScummVM. --- Changelog | 1 + doc/general.texi | 2 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/cdtoons.c | 370 ++++++++++++++++++++++++++++++++++++++++++++++++ libavformat/isom.c | 1 + libavformat/version.h | 2 +- 8 files changed, 378 insertions(+), 1 deletions(-) create mode 100644 libavcodec/cdtoons.c diff --git a/Changelog b/Changelog index b9b5f26..1f8727e 100644 --- a/Changelog +++ b/Changelog @@ -102,6 +102,7 @@ easier to use. The changes are: - Discworld II BMV decoding support - VBLE Decoder - OS X Video Decoder Acceleration (VDA) support +- CDToons decoder version 0.7: diff --git a/doc/general.texi b/doc/general.texi index d2747c0..aa58669 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -378,6 +378,8 @@ following image formats are supported: @item Delphine Software International CIN video @tab @tab X @tab Codec used in Delphine Software International games. @item Discworld II BMV Video @tab @tab X +@item CDToons @tab @tab X + @tab Codec used in various Broderbund games. @item Cinepak @tab @tab X @item Cirrus Logic AccuPak @tab @tab X @tab fourcc: CLJR diff --git a/libavcodec/Makefile b/libavcodec/Makefile index b9ed8db..acba3a0 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -101,6 +101,7 @@ OBJS-$(CONFIG_C93_DECODER) += c93.o OBJS-$(CONFIG_CAVS_DECODER) += cavs.o cavsdec.o cavsdsp.o \ mpeg12data.o mpegvideo.o OBJS-$(CONFIG_CDGRAPHICS_DECODER) += cdgraphics.o +OBJS-$(CONFIG_CDTOONS_DECODER) += cdtoons.o OBJS-$(CONFIG_CINEPAK_DECODER) += cinepak.o OBJS-$(CONFIG_CLJR_DECODER) += cljr.o OBJS-$(CONFIG_CLJR_ENCODER) += cljr.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index db213a1..7cb14a2 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -86,6 +86,7 @@ void avcodec_register_all(void) REGISTER_DECODER (C93, c93); REGISTER_DECODER (CAVS, cavs); REGISTER_DECODER (CDGRAPHICS, cdgraphics); + REGISTER_DECODER (CDTOONS, cdtoons); REGISTER_DECODER (CINEPAK, cinepak); REGISTER_DECODER (CLJR, cljr); REGISTER_DECODER (CSCD, cscd); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 7d506a1..9269fad 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -221,6 +221,7 @@ enum CodecID { CODEC_ID_UTVIDEO, CODEC_ID_BMV_VIDEO, CODEC_ID_VBLE, + CODEC_ID_CDTOONS, /* various PCM "codecs" */ CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff --git a/libavcodec/cdtoons.c b/libavcodec/cdtoons.c new file mode 100644 index 0000000..005e125 --- /dev/null +++ b/libavcodec/cdtoons.c @@ -0,0 +1,370 @@ +/* + * CDToons video decoder + * Copyright (c) 2011 Alyssa Milburn + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * 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. + * + * Libav is distributed in the hope that it will be useful, + * 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 + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avcodec.h" +#include "bytestream.h" + +#define CDTOONS_HEADER_SIZE 44 +#define MAX_SPRITES 1200 + +typedef struct { + uint16_t flags; + uint32_t size; + uint16_t owner_frame; + uint16_t start_frame, end_frame; + uint8_t *data; +} cdtoons_sprite; + +typedef struct CDToonsContext { + AVCodecContext *avctx; + AVFrame frame; + + uint16_t last_pal_id; + uint32_t pal[256]; + cdtoons_sprite sprites[MAX_SPRITES]; +} CDToonsContext; + +static int cdtoons_render_sprite(AVCodecContext *avctx, const uint8_t *data, uint32_t data_size, + int dst_x, int dst_y, int width, int height) +{ + CDToonsContext *c = avctx->priv_data; + int skip = 0; + const uint8_t *next_line = data; + uint16_t line_size; + uint8_t *dest; + int to_skip; + int x; + + if (dst_x + width > avctx->width) + width = avctx->width - dst_x; + if (dst_y + height > avctx->height) + height = avctx->height - dst_y; + + if (dst_x < 0) { + /* we need to skip the start of the scanlines */ + skip = -dst_x; + if (width <= skip) + return 0; + dst_x = 0; + } + + for (int y = 0; y < height; y++) { + /* one scanline at a time, size is provided */ + data = next_line; + line_size = bytestream_get_be16(&data); + next_line = data + line_size; + if (dst_y + y < 0) + continue; + + dest = c->frame.data[0] + (dst_y + y) * c->frame.linesize[0] + dst_x; + + to_skip = skip; + x = 0; + while (x < width - skip) { + uint8_t val = bytestream_get_byte(&data); + int raw = !(val & 0x80); + int size = (int)(val & 0x7f) + 1; + + /* skip the start of a scanline if it is off-screen */ + if (to_skip >= size) { + to_skip -= size; + if (raw) + data += size; + else + data += 1; + continue; + } else if (to_skip) { + size -= to_skip; + if (raw) + data += to_skip; + to_skip = 0; + } + + if (x + size >= width - skip) + size = width - skip - x; + + /* either raw data, or a run of a single colour */ + if (raw) { + memcpy(dest + x, data, size); + data += size; + } else { + uint8_t colour = bytestream_get_byte(&data); + /* ignore transparent runs */ + if (colour) + memset(dest + x, colour, size); + } + x += size; + } + + if (data > next_line) + av_log(avctx, AV_LOG_ERROR, "walked off end of sprite scanline\n"); + } + + return 0; +} + +static int cdtoons_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt) +{ + CDToonsContext *c = avctx->priv_data; + const uint8_t *buf = avpkt->data; + int buf_size = avpkt->size; + uint16_t frame_id; + uint8_t background_colour; + uint16_t sprite_count, sprite_offset; + uint8_t referenced_count; + uint16_t palette_id; + uint8_t palette_set; + int curr_offset; + int i; + int saw_embedded_sprites = 0; + + if (buf_size < CDTOONS_HEADER_SIZE) + return -1; + + c->frame.reference = 1; + c->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | + FF_BUFFER_HINTS_REUSABLE; + if (avctx->reget_buffer(avctx, &c->frame)) { + av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); + return -1; + } + + /* a lot of the header is useless junk in the absence of dirty rectangling etc */ + buf += 2; /* version? (always 9?) */ + frame_id = bytestream_get_be16(&buf); + buf += 2; /* blocks_valid_until */ + buf += 1; + background_colour = bytestream_get_byte(&buf); + buf += 16; /* clip rect, dirty rect */ + buf += 4; /* flags */ + sprite_count = bytestream_get_be16(&buf); + sprite_offset = bytestream_get_be16(&buf); + buf += 2; /* max block id? */ + referenced_count = bytestream_get_byte(&buf); + buf += 1; + palette_id = bytestream_get_be16(&buf); + palette_set = bytestream_get_byte(&buf); + buf += 5; + + /* read new sprites introduced in this frame */ + buf = avpkt->data + sprite_offset; + curr_offset = sprite_offset; + while (sprite_count--) { + uint32_t size; + uint16_t sprite_id = bytestream_get_be16(&buf); + if (sprite_id >= MAX_SPRITES) { + av_log(avctx, AV_LOG_ERROR, "sprite id %d is too high\n", sprite_id); + return -1; + } + if (c->sprites[sprite_id].data) { + av_log(avctx, AV_LOG_ERROR, "sprite id %d is a duplicate\n", sprite_id); + return -1; + } + + c->sprites[sprite_id].flags = bytestream_get_be16(&buf); + size = bytestream_get_be32(&buf); + if (size < 14) { + av_log(avctx, AV_LOG_ERROR, "sprite only has %d bytes of data\n", size); + return -1; + } + curr_offset += size; + size -= 14; + c->sprites[sprite_id].size = size; + c->sprites[sprite_id].owner_frame = frame_id; + c->sprites[sprite_id].start_frame = bytestream_get_be16(&buf); + c->sprites[sprite_id].end_frame = bytestream_get_be16(&buf); + buf += 2; + c->sprites[sprite_id].data = av_malloc(size); + bytestream_get_buffer(&buf, c->sprites[sprite_id].data, size); + } + + /* render any embedded sprites */ + while (curr_offset < buf_size) { + uint32_t tag = bytestream_get_be32(&buf); + uint32_t size = bytestream_get_be32(&buf); + curr_offset += size; + if (tag == MKBETAG('D', 'i', 'f', 'f')) { + uint16_t diff_count = bytestream_get_be16(&buf); + buf += 8; /* clip rect? */ + for (i = 0; i < diff_count; i++) { + int16_t top, left; + uint16_t diff_size, width, height; + + top = bytestream_get_be16(&buf); + left = bytestream_get_be16(&buf); + buf += 4; /* bottom, right */ + diff_size = bytestream_get_be32(&buf); + width = bytestream_get_be16(&buf); + height = bytestream_get_be16(&buf); + cdtoons_render_sprite(avctx, buf + 4, diff_size - 8, left, top, width, height); + buf += (diff_size - 4); + } + saw_embedded_sprites = 1; + } else { + /* we don't care about any other entries */ + buf += (size - 8); + } + } + + /* was an intra frame? */ + if (saw_embedded_sprites) + goto done; + + /* render any referenced sprites */ + buf = avpkt->data + CDTOONS_HEADER_SIZE; + for (i = 0; i < referenced_count; i++) { + const uint8_t *block_data; + uint16_t sprite_id, width, height; + int16_t top, left, right; + + sprite_id = bytestream_get_be16(&buf); + top = bytestream_get_be16(&buf); + left = bytestream_get_be16(&buf); + buf += 2; /* bottom */ + right = bytestream_get_be16(&buf); + + if ((i == 0) && (sprite_id == 0)) { + /* clear background */ + memset(c->frame.data[0], background_colour, c->frame.linesize[0] * avctx->height); + } + + if (!right) + continue; + block_data = c->sprites[sprite_id].data; + if (!block_data) { + /* this can happen when seeking around */ + av_log(avctx, AV_LOG_WARNING, "sprite %d is missing\n", sprite_id); + continue; + } + if (c->sprites[sprite_id].size < 14) { + av_log(avctx, AV_LOG_ERROR, "sprite %d is too small\n", sprite_id); + continue; + } + + height = bytestream_get_be16(&block_data); + width = bytestream_get_be16(&block_data); + block_data += 10; + cdtoons_render_sprite(avctx, block_data, c->sprites[sprite_id].size - 14, left, top, width, height); + } + + if (palette_id && (palette_id != c->last_pal_id)) { + if (palette_id >= MAX_SPRITES) { + av_log(avctx, AV_LOG_ERROR, "palette id %d is too high\n", palette_id); + return -1; + } + if (!c->sprites[palette_id].data) { + /* this can happen when seeking around */ + av_log(avctx, AV_LOG_WARNING, "palette id %d is missing\n", palette_id); + goto done; + } + if (c->sprites[palette_id].size != 256 * 2 * 3) { + av_log(avctx, AV_LOG_ERROR, "palette id %d is wrong size (%d)\n", palette_id, c->sprites[palette_id].size); + return -1; + } + c->last_pal_id = palette_id; + if (!palette_set) { + uint8_t *palette_data = c->sprites[palette_id].data; + for (i = 0; i < 256; i++) { + /* QuickTime-ish palette: 16-bit RGB components */ + uint8_t r, g, b; + r = *palette_data; + g = *(palette_data + 2); + b = *(palette_data + 4); + c->pal[i] = (r << 16) | (g << 8) | (b); + palette_data += 6; + } + /* first palette entry indicates transparency */ + c->pal[0] = 0; + c->frame.palette_has_changed = 1; + } + } + +done: + /* discard outdated blocks */ + for (i = 0; i < MAX_SPRITES; i++) { + if (c->sprites[i].end_frame > frame_id) + continue; + av_free(c->sprites[i].data); + c->sprites[i].data = NULL; + } + + memcpy(c->frame.data[1], c->pal, AVPALETTE_SIZE); + + *data_size = sizeof(AVFrame); + *(AVFrame *)data = c->frame; + + /* always report that the buffer was completely consumed */ + return buf_size; +} + +static av_cold int cdtoons_decode_init(AVCodecContext *avctx) +{ + CDToonsContext *c = avctx->priv_data; + + c->avctx = avctx; + c->frame.data[0] = NULL; + c->last_pal_id = 0; + memset(c->sprites, 0, sizeof(c->sprites)); + + avctx->pix_fmt = PIX_FMT_PAL8; + + return 0; +} + +static void cdtoons_flush(AVCodecContext *avctx) +{ + CDToonsContext *c = avctx->priv_data; + int i; + + c->last_pal_id = 0; + for (i = 0; i < MAX_SPRITES; i++) { + av_free(c->sprites[i].data); + c->sprites[i].data = NULL; + } +} + +static av_cold int cdtoons_decode_end(AVCodecContext *avctx) +{ + CDToonsContext *c = avctx->priv_data; + int i; + + for (i = 0; i < MAX_SPRITES; i++) + av_free(c->sprites[i].data); + + if (c->frame.data[0]) + avctx->release_buffer(avctx, &c->frame); + + return 0; +} + +AVCodec ff_cdtoons_decoder = { + .name = "cdtoons", + .type = AVMEDIA_TYPE_VIDEO, + .id = CODEC_ID_CDTOONS, + .priv_data_size = sizeof(CDToonsContext), + .init = cdtoons_decode_init, + .close = cdtoons_decode_end, + .decode = cdtoons_decode_frame, + .capabilities = CODEC_CAP_DR1, + .flush = cdtoons_flush, + .long_name = NULL_IF_CONFIG_SMALL("CDToons"), +}; + diff --git a/libavformat/isom.c b/libavformat/isom.c index b0eef37..91cda08 100644 --- a/libavformat/isom.c +++ b/libavformat/isom.c @@ -135,6 +135,7 @@ const AVCodecTag codec_movvideo_tags[] = { { CODEC_ID_QTRLE, MKTAG('r', 'l', 'e', ' ') }, /* Apple Animation (RLE) */ { CODEC_ID_MSRLE, MKTAG('W', 'R', 'L', 'E') }, { CODEC_ID_QDRAW, MKTAG('q', 'd', 'r', 'w') }, /* QuickDraw */ + { CODEC_ID_CDTOONS, MKTAG('Q', 'k', 'B', 'k') }, /* CDToons */ { CODEC_ID_RAWVIDEO, MKTAG('W', 'R', 'A', 'W') }, diff --git a/libavformat/version.h b/libavformat/version.h index ec5d1fd..6c1db42 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -24,7 +24,7 @@ #include "libavutil/avutil.h" #define LIBAVFORMAT_VERSION_MAJOR 53 -#define LIBAVFORMAT_VERSION_MINOR 14 +#define LIBAVFORMAT_VERSION_MINOR 15 #define LIBAVFORMAT_VERSION_MICRO 0 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ -- 1.7.6.3