2 * CDToons video decoder
3 * Copyright (C) 2020 Alyssa Milburn <amilburn@zall.org>
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 * CDToons video decoder
25 * @author Alyssa Milburn <amilburn@zall.org>
30 #include "libavutil/attributes.h"
31 #include "libavutil/internal.h"
33 #include "bytestream.h"
34 #include "codec_internal.h"
37 #define CDTOONS_HEADER_SIZE 44
38 #define CDTOONS_MAX_SPRITES 1200
40 typedef struct CDToonsSprite {
45 unsigned int alloc_size;
51 typedef struct CDToonsContext {
54 uint16_t last_pal_id; ///< The index of the active palette sprite.
55 uint32_t pal[256]; ///< The currently-used palette data.
56 CDToonsSprite sprites[CDTOONS_MAX_SPRITES];
59 static int cdtoons_render_sprite(AVCodecContext *avctx, const uint8_t *data,
61 int dst_x, int dst_y, int width, int height)
63 CDToonsContext *c = avctx->priv_data;
64 const uint8_t *next_line = data;
65 const uint8_t *end = data + data_size;
68 int skip = 0, to_skip, x;
70 if (dst_x + width > avctx->width)
71 width = avctx->width - dst_x;
72 if (dst_y + height > avctx->height)
73 height = avctx->height - dst_y;
76 /* we need to skip the start of the scanlines */
83 for (int y = 0; y < height; y++) {
84 /* one scanline at a time, size is provided */
88 line_size = bytestream_get_be16(&data);
89 if (end - data < line_size)
91 next_line = data + line_size;
95 dest = c->frame->data[0] + (dst_y + y) * c->frame->linesize[0] + dst_x;
99 while (x < width - skip) {
106 val = bytestream_get_byte(&data);
108 size = (int)(val & 0x7F) + 1;
110 /* skip the start of a scanline if it is off-screen */
111 if (to_skip >= size) {
118 if (next_line - data < step)
122 } else if (to_skip) {
125 if (next_line - data < to_skip)
132 if (x + size >= width - skip)
133 size = width - skip - x;
135 /* either raw data, or a run of a single color */
137 if (next_line - data < size)
139 memcpy(dest + x, data, size);
142 uint8_t color = bytestream_get_byte(&data);
143 /* ignore transparent runs */
145 memset(dest + x, color, size);
154 static int cdtoons_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
155 int *got_frame, AVPacket *avpkt)
157 CDToonsContext *c = avctx->priv_data;
158 const uint8_t *buf = avpkt->data;
159 const uint8_t *eod = avpkt->data + avpkt->size;
160 const int buf_size = avpkt->size;
162 uint8_t background_color;
163 uint16_t sprite_count, sprite_offset;
164 uint8_t referenced_count;
168 int saw_embedded_sprites = 0;
170 if (buf_size < CDTOONS_HEADER_SIZE)
171 return AVERROR_INVALIDDATA;
173 if ((ret = ff_reget_buffer(avctx, c->frame, 0)) < 0)
176 /* a lot of the header is useless junk in the absence of
177 * dirty rectangling etc */
178 buf += 2; /* version? (always 9?) */
179 frame_id = bytestream_get_be16(&buf);
180 buf += 2; /* blocks_valid_until */
182 background_color = bytestream_get_byte(&buf);
183 buf += 16; /* clip rect, dirty rect */
184 buf += 4; /* flags */
185 sprite_count = bytestream_get_be16(&buf);
186 sprite_offset = bytestream_get_be16(&buf);
187 buf += 2; /* max block id? */
188 referenced_count = bytestream_get_byte(&buf);
190 palette_id = bytestream_get_be16(&buf);
191 palette_set = bytestream_get_byte(&buf);
194 if (sprite_offset > buf_size)
195 return AVERROR_INVALIDDATA;
197 /* read new sprites introduced in this frame */
198 buf = avpkt->data + sprite_offset;
199 while (sprite_count--) {
204 return AVERROR_INVALIDDATA;
206 sprite_id = bytestream_get_be16(&buf);
207 if (sprite_id >= CDTOONS_MAX_SPRITES) {
208 av_log(avctx, AV_LOG_ERROR,
209 "Sprite ID %d is too high.\n", sprite_id);
210 return AVERROR_INVALIDDATA;
212 if (c->sprites[sprite_id].active) {
213 av_log(avctx, AV_LOG_ERROR,
214 "Sprite ID %d is a duplicate.\n", sprite_id);
215 return AVERROR_INVALIDDATA;
218 c->sprites[sprite_id].flags = bytestream_get_be16(&buf);
219 size = bytestream_get_be32(&buf);
221 av_log(avctx, AV_LOG_ERROR,
222 "Sprite only has %d bytes of data.\n", size);
223 return AVERROR_INVALIDDATA;
226 c->sprites[sprite_id].size = size;
227 c->sprites[sprite_id].owner_frame = frame_id;
228 c->sprites[sprite_id].start_frame = bytestream_get_be16(&buf);
229 c->sprites[sprite_id].end_frame = bytestream_get_be16(&buf);
232 if (size > buf_size || buf + size > eod)
233 return AVERROR_INVALIDDATA;
235 av_fast_padded_malloc(&c->sprites[sprite_id].data, &c->sprites[sprite_id].alloc_size, size);
236 if (!c->sprites[sprite_id].data)
237 return AVERROR(ENOMEM);
239 c->sprites[sprite_id].active = 1;
241 bytestream_get_buffer(&buf, c->sprites[sprite_id].data, size);
244 /* render any embedded sprites */
248 av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data for embedded sprites.\n");
249 return AVERROR_INVALIDDATA;
251 tag = bytestream_get_be32(&buf);
252 size = bytestream_get_be32(&buf);
253 if (tag == MKBETAG('D', 'i', 'f', 'f')) {
255 if (buf + 10 > eod) {
256 av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data for Diff frame.\n");
257 return AVERROR_INVALIDDATA;
259 diff_count = bytestream_get_be16(&buf);
260 buf += 8; /* clip rect? */
261 for (int i = 0; i < diff_count; i++) {
263 uint16_t diff_size, width, height;
265 if (buf + 16 > eod) {
266 av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data for Diff frame header.\n");
267 return AVERROR_INVALIDDATA;
270 top = bytestream_get_be16(&buf);
271 left = bytestream_get_be16(&buf);
272 buf += 4; /* bottom, right */
273 diff_size = bytestream_get_be32(&buf);
274 width = bytestream_get_be16(&buf);
275 height = bytestream_get_be16(&buf);
276 if (diff_size < 8 || diff_size - 4 > eod - buf) {
277 av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data for Diff frame data.\n");
278 return AVERROR_INVALIDDATA;
280 if (cdtoons_render_sprite(avctx, buf + 4, diff_size - 8,
281 left, top, width, height)) {
282 av_log(avctx, AV_LOG_WARNING, "Ran beyond end of sprite while rendering.\n");
284 buf += diff_size - 4;
286 saw_embedded_sprites = 1;
288 /* we don't care about any other entries */
289 if (size < 8 || size - 8 > eod - buf) {
290 av_log(avctx, AV_LOG_WARNING, "Ran out of data for ignored entry (size %X, %d left).\n", size, (int)(eod - buf));
291 return AVERROR_INVALIDDATA;
297 /* was an intra frame? */
298 if (saw_embedded_sprites)
301 /* render any referenced sprites */
302 buf = avpkt->data + CDTOONS_HEADER_SIZE;
303 eod = avpkt->data + sprite_offset;
304 for (int i = 0; i < referenced_count; i++) {
305 const uint8_t *block_data;
306 uint16_t sprite_id, width, height;
307 int16_t top, left, right;
309 if (buf + 10 > eod) {
310 av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data when rendering.\n");
311 return AVERROR_INVALIDDATA;
314 sprite_id = bytestream_get_be16(&buf);
315 top = bytestream_get_be16(&buf);
316 left = bytestream_get_be16(&buf);
317 buf += 2; /* bottom */
318 right = bytestream_get_be16(&buf);
320 if ((i == 0) && (sprite_id == 0)) {
321 /* clear background */
322 memset(c->frame->data[0], background_color,
323 c->frame->linesize[0] * avctx->height);
328 if (sprite_id >= CDTOONS_MAX_SPRITES) {
329 av_log(avctx, AV_LOG_ERROR,
330 "Sprite ID %d is too high.\n", sprite_id);
331 return AVERROR_INVALIDDATA;
334 block_data = c->sprites[sprite_id].data;
335 if (!c->sprites[sprite_id].active) {
336 /* this can happen when seeking around */
337 av_log(avctx, AV_LOG_WARNING, "Sprite %d is missing.\n", sprite_id);
340 if (c->sprites[sprite_id].size < 14) {
341 av_log(avctx, AV_LOG_ERROR, "Sprite %d is too small.\n", sprite_id);
345 height = bytestream_get_be16(&block_data);
346 width = bytestream_get_be16(&block_data);
348 if (cdtoons_render_sprite(avctx, block_data,
349 c->sprites[sprite_id].size - 14,
350 left, top, width, height)) {
351 av_log(avctx, AV_LOG_WARNING, "Ran beyond end of sprite while rendering.\n");
355 if (palette_id && (palette_id != c->last_pal_id)) {
356 if (palette_id >= CDTOONS_MAX_SPRITES) {
357 av_log(avctx, AV_LOG_ERROR,
358 "Palette ID %d is too high.\n", palette_id);
359 return AVERROR_INVALIDDATA;
361 if (!c->sprites[palette_id].active) {
362 /* this can happen when seeking around */
363 av_log(avctx, AV_LOG_WARNING,
364 "Palette ID %d is missing.\n", palette_id);
367 if (c->sprites[palette_id].size != 256 * 2 * 3) {
368 av_log(avctx, AV_LOG_ERROR,
369 "Palette ID %d is wrong size (%d).\n",
370 palette_id, c->sprites[palette_id].size);
371 return AVERROR_INVALIDDATA;
373 c->last_pal_id = palette_id;
375 uint8_t *palette_data = c->sprites[palette_id].data;
376 for (int i = 0; i < 256; i++) {
377 /* QuickTime-ish palette: 16-bit RGB components */
380 g = *(palette_data + 2);
381 b = *(palette_data + 4);
382 c->pal[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
385 /* first palette entry indicates transparency */
387 #if FF_API_PALETTE_HAS_CHANGED
388 FF_DISABLE_DEPRECATION_WARNINGS
389 c->frame->palette_has_changed = 1;
390 FF_ENABLE_DEPRECATION_WARNINGS
396 /* discard outdated blocks */
397 for (int i = 0; i < CDTOONS_MAX_SPRITES; i++) {
398 if (c->sprites[i].end_frame > frame_id)
400 c->sprites[i].active = 0;
403 memcpy(c->frame->data[1], c->pal, AVPALETTE_SIZE);
405 if ((ret = av_frame_ref(rframe, c->frame)) < 0)
410 /* always report that the buffer was completely consumed */
414 static av_cold int cdtoons_decode_init(AVCodecContext *avctx)
416 CDToonsContext *c = avctx->priv_data;
418 avctx->pix_fmt = AV_PIX_FMT_PAL8;
420 c->frame = av_frame_alloc();
422 return AVERROR(ENOMEM);
427 static void cdtoons_flush(AVCodecContext *avctx)
429 CDToonsContext *c = avctx->priv_data;
432 for (int i = 0; i < CDTOONS_MAX_SPRITES; i++)
433 c->sprites[i].active = 0;
436 static av_cold int cdtoons_decode_end(AVCodecContext *avctx)
438 CDToonsContext *c = avctx->priv_data;
440 for (int i = 0; i < CDTOONS_MAX_SPRITES; i++) {
441 av_freep(&c->sprites[i].data);
442 c->sprites[i].active = 0;
445 av_frame_free(&c->frame);
450 const FFCodec ff_cdtoons_decoder = {
452 CODEC_LONG_NAME("CDToons video"),
453 .p.type = AVMEDIA_TYPE_VIDEO,
454 .p.id = AV_CODEC_ID_CDTOONS,
455 .priv_data_size = sizeof(CDToonsContext),
456 .init = cdtoons_decode_init,
457 .close = cdtoons_decode_end,
458 FF_CODEC_DECODE_CB(cdtoons_decode_frame),
459 .p.capabilities = AV_CODEC_CAP_DR1,
460 .flush = cdtoons_flush,