From 6b4effc1c07c946c8a636c1f29c576fb8417d1b6 Mon Sep 17 00:00:00 2001 From: Alyssa Milburn Date: Tue, 30 Jul 2013 17:10:27 +0200 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 | 417 +++++++++++++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 2 +- libavformat/isom.c | 1 + libavformat/riff.c | 1 + 9 files changed, 426 insertions(+), 1 deletion(-) create mode 100644 libavcodec/cdtoons.c diff --git a/Changelog b/Changelog index faf0393..77f66b1 100644 --- a/Changelog +++ b/Changelog @@ -25,6 +25,7 @@ version 10: - support for WavPack muxing (raw and in Matroska) - Go2Webinar decoder - WavPack encoding through libwavpack +- CDToons decoder version 9: diff --git a/doc/general.texi b/doc/general.texi index 1d2cb09..8b02b20 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -479,6 +479,8 @@ following image formats are supported: @tab Codec used in Delphine Software International games. @item Discworld II BMV Video @tab @tab X @item Canopus Lossless Codec @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 X @tab X @tab fourcc: CLJR diff --git a/libavcodec/Makefile b/libavcodec/Makefile index d9c6e33..2373473 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -123,6 +123,7 @@ OBJS-$(CONFIG_C93_DECODER) += c93.o OBJS-$(CONFIG_CAVS_DECODER) += cavs.o cavsdec.o cavsdsp.o \ cavsdata.o mpeg12data.o OBJS-$(CONFIG_CDGRAPHICS_DECODER) += cdgraphics.o +OBJS-$(CONFIG_CDTOONS_DECODER) += cdtoons.o OBJS-$(CONFIG_CDXL_DECODER) += cdxl.o OBJS-$(CONFIG_CINEPAK_DECODER) += cinepak.o OBJS-$(CONFIG_CLJR_DECODER) += cljr.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 6bfc042..b6a1b0e 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -113,6 +113,7 @@ void avcodec_register_all(void) REGISTER_DECODER(C93, c93); REGISTER_DECODER(CAVS, cavs); REGISTER_DECODER(CDGRAPHICS, cdgraphics); + REGISTER_DECODER(CDTOONS, cdtoons); REGISTER_DECODER(CDXL, cdxl); REGISTER_DECODER(CINEPAK, cinepak); REGISTER_ENCDEC (CLJR, cljr); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index bd9a80c..15fbed1 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -272,6 +272,7 @@ enum AVCodecID { AV_CODEC_ID_AIC, AV_CODEC_ID_ESCAPE130, AV_CODEC_ID_G2M, + AV_CODEC_ID_CDTOONS, /* various PCM "codecs" */ AV_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..fdc2448 --- /dev/null +++ b/libavcodec/cdtoons.c @@ -0,0 +1,417 @@ +/* + * 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 + */ + +/** + * @file + * CDToons video decoder + * @author Alyssa Milburn + */ + +#include + +#include "libavutil/attributes.h" +#include "libavutil/internal.h" +#include "avcodec.h" +#include "bytestream.h" +#include "internal.h" + +#define CDTOONS_HEADER_SIZE 44 +#define CDTOONS_MAX_SPRITES 1200 + +typedef struct CDToonsSprite { + uint16_t flags; + uint16_t owner_frame; + uint16_t start_frame; + uint16_t end_frame; + uint32_t size; + uint8_t *data; +} CDToonsSprite; + +typedef struct CDToonsContext { + AVCodecContext *avctx; + AVFrame frame; + + uint16_t last_pal_id; // The index of the active palette sprite. + uint32_t pal[256]; // The currently-used palette data. + CDToonsSprite sprites[CDTOONS_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; + const uint8_t *next_line = data; + uint16_t line_size; + uint8_t *dest; + int skip = 0, to_skip, 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 color */ + if (raw) { + memcpy(dest + x, data, size); + data += size; + } else { + uint8_t color = bytestream_get_byte(&data); + /* ignore transparent runs */ + if (color) + memset(dest + x, color, 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 *got_frame, AVPacket *avpkt) +{ + CDToonsContext *c = avctx->priv_data; + const uint8_t *buf = avpkt->data; + const uint8_t *eod = avpkt->data + avpkt->size; + int buf_size = avpkt->size; + uint16_t frame_id; + uint8_t background_color; + uint16_t sprite_count, sprite_offset; + uint8_t referenced_count; + uint16_t palette_id; + uint8_t palette_set; + int ret, i; + int saw_embedded_sprites = 0; + + if (buf_size < CDTOONS_HEADER_SIZE) + return AVERROR_INVALIDDATA; + + if ((ret = ff_reget_buffer(avctx, &c->frame))) { + av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); + return ret; + } + + /* 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_color = 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; + while (sprite_count--) { + uint32_t size; + uint16_t sprite_id; + + if (buf + 14 > eod) + return AVERROR_INVALIDDATA; + + sprite_id = bytestream_get_be16(&buf); + if (sprite_id >= CDTOONS_MAX_SPRITES) { + av_log(avctx, AV_LOG_ERROR, + "Sprite ID %d is too high.\n", sprite_id); + return AVERROR_INVALIDDATA; + } + if (c->sprites[sprite_id].data) { + av_log(avctx, AV_LOG_ERROR, + "Sprite ID %d is a duplicate.\n", sprite_id); + return AVERROR_INVALIDDATA; + } + + 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 AVERROR_INVALIDDATA; + } + 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; + + if (size > buf_size || buf + size > eod) + return AVERROR_INVALIDDATA; + + c->sprites[sprite_id].data = av_malloc(size); + if (!c->sprites[sprite_id].data) + return AVERROR(ENOMEM); + + bytestream_get_buffer(&buf, c->sprites[sprite_id].data, size); + } + + /* render any embedded sprites */ + while (buf < eod) { + uint32_t tag, size; + if (buf + 8 > eod) + return AVERROR_INVALIDDATA; + tag = bytestream_get_be32(&buf); + size = bytestream_get_be32(&buf); + if (tag == MKBETAG('D', 'i', 'f', 'f')) { + uint16_t diff_count; + if (buf + 10 > eod) + return AVERROR_INVALIDDATA; + 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; + + if (buf + 16 > eod) + return AVERROR_INVALIDDATA; + + 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); + if (diff_size < 4 || diff_size - 4 > eod - buf) + return AVERROR_INVALIDDATA; + 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 */ + if (size < 8 || size - 8 > eod - buf) + return AVERROR_INVALIDDATA; + buf += (size - 8); + } + } + + /* was an intra frame? */ + if (saw_embedded_sprites) + goto done; + + /* render any referenced sprites */ + buf = avpkt->data + CDTOONS_HEADER_SIZE; + eod = avpkt->data + sprite_offset; + for (i = 0; i < referenced_count; i++) { + const uint8_t *block_data; + uint16_t sprite_id, width, height; + int16_t top, left, right; + + if (buf + 10 > eod) + return AVERROR_INVALIDDATA; + + 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_color, + 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 >= CDTOONS_MAX_SPRITES) { + av_log(avctx, AV_LOG_ERROR, + "Palette ID %d is too high.\n", palette_id); + return AVERROR_INVALIDDATA; + } + 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 AVERROR_INVALIDDATA; + } + 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 < CDTOONS_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); + + if ((ret = av_frame_ref(data, &c->frame)) < 0) + return ret; + + *got_frame = 1; + + /* 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->last_pal_id = 0; + avcodec_get_frame_defaults(&c->frame); + memset(c->sprites, 0, sizeof(c->sprites)); + + avctx->pix_fmt = AV_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 < CDTOONS_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 < CDTOONS_MAX_SPRITES; i++) + av_free(c->sprites[i].data); + + av_frame_unref(&c->frame); + + return 0; +} + +AVCodec ff_cdtoons_decoder = { + .name = "cdtoons", + .long_name = NULL_IF_CONFIG_SMALL("CDToons"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_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, +}; diff --git a/libavcodec/version.h b/libavcodec/version.h index 7c82519..575fa26 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -27,7 +27,7 @@ */ #define LIBAVCODEC_VERSION_MAJOR 55 -#define LIBAVCODEC_VERSION_MINOR 10 +#define LIBAVCODEC_VERSION_MINOR 11 #define LIBAVCODEC_VERSION_MICRO 0 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ diff --git a/libavformat/isom.c b/libavformat/isom.c index 014096d..7a3526d 100644 --- a/libavformat/isom.c +++ b/libavformat/isom.c @@ -133,6 +133,7 @@ const AVCodecTag ff_codec_movvideo_tags[] = { { AV_CODEC_ID_QTRLE, MKTAG('r', 'l', 'e', ' ') }, /* Apple Animation (RLE) */ { AV_CODEC_ID_MSRLE, MKTAG('W', 'R', 'L', 'E') }, { AV_CODEC_ID_QDRAW, MKTAG('q', 'd', 'r', 'w') }, /* QuickDraw */ + { AV_CODEC_ID_CDTOONS, MKTAG('Q', 'k', 'B', 'k') }, /* CDToons */ { AV_CODEC_ID_RAWVIDEO, MKTAG('W', 'R', 'A', 'W') }, diff --git a/libavformat/riff.c b/libavformat/riff.c index 3f1790e..8c051e5 100644 --- a/libavformat/riff.c +++ b/libavformat/riff.c @@ -317,6 +317,7 @@ const AVCodecTag ff_codec_bmp_tags[] = { { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '2') }, { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '3') }, { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '4') }, + { AV_CODEC_ID_CDTOONS, MKTAG('Q', 'k', 'B', 'k') }, { AV_CODEC_ID_NONE, 0 } }; -- 1.8.3.2