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.

454 lines
15KB

  1. /*
  2. * CDToons video decoder
  3. * Copyright (C) 2020 Alyssa Milburn <amilburn@zall.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. /**
  22. * @file
  23. * CDToons video decoder
  24. * @author Alyssa Milburn <amilburn@zall.org>
  25. */
  26. #include <stdint.h>
  27. #include "libavutil/attributes.h"
  28. #include "libavutil/internal.h"
  29. #include "avcodec.h"
  30. #include "bytestream.h"
  31. #include "internal.h"
  32. #define CDTOONS_HEADER_SIZE 44
  33. #define CDTOONS_MAX_SPRITES 1200
  34. typedef struct CDToonsSprite {
  35. uint16_t flags;
  36. uint16_t owner_frame;
  37. uint16_t start_frame;
  38. uint16_t end_frame;
  39. unsigned int alloc_size;
  40. uint32_t size;
  41. uint8_t *data;
  42. int active;
  43. } CDToonsSprite;
  44. typedef struct CDToonsContext {
  45. AVFrame *frame;
  46. uint16_t last_pal_id; ///< The index of the active palette sprite.
  47. uint32_t pal[256]; ///< The currently-used palette data.
  48. CDToonsSprite sprites[CDTOONS_MAX_SPRITES];
  49. } CDToonsContext;
  50. static int cdtoons_render_sprite(AVCodecContext *avctx, const uint8_t *data,
  51. uint32_t data_size,
  52. int dst_x, int dst_y, int width, int height)
  53. {
  54. CDToonsContext *c = avctx->priv_data;
  55. const uint8_t *next_line = data;
  56. const uint8_t *end = data + data_size;;
  57. uint16_t line_size;
  58. uint8_t *dest;
  59. int skip = 0, to_skip, x;
  60. if (dst_x + width > avctx->width)
  61. width = avctx->width - dst_x;
  62. if (dst_y + height > avctx->height)
  63. height = avctx->height - dst_y;
  64. if (dst_x < 0) {
  65. /* we need to skip the start of the scanlines */
  66. skip = -dst_x;
  67. if (width <= skip)
  68. return 0;
  69. dst_x = 0;
  70. }
  71. for (int y = 0; y < height; y++) {
  72. /* one scanline at a time, size is provided */
  73. data = next_line;
  74. if (end - data < 2)
  75. return 1;
  76. line_size = bytestream_get_be16(&data);
  77. if (end - data < line_size)
  78. return 1;
  79. next_line = data + line_size;
  80. if (dst_y + y < 0)
  81. continue;
  82. dest = c->frame->data[0] + (dst_y + y) * c->frame->linesize[0] + dst_x;
  83. to_skip = skip;
  84. x = 0;
  85. while (x < width - skip) {
  86. int raw, size, step;
  87. uint8_t val;
  88. if (data >= end)
  89. return 1;
  90. val = bytestream_get_byte(&data);
  91. raw = !(val & 0x80);
  92. size = (int)(val & 0x7F) + 1;
  93. /* skip the start of a scanline if it is off-screen */
  94. if (to_skip >= size) {
  95. to_skip -= size;
  96. if (raw) {
  97. step = size;
  98. } else {
  99. step = 1;
  100. }
  101. if (next_line - data < step)
  102. return 1;
  103. data += step;
  104. continue;
  105. } else if (to_skip) {
  106. size -= to_skip;
  107. if (raw) {
  108. if (next_line - data < to_skip)
  109. return 1;
  110. data += to_skip;
  111. }
  112. to_skip = 0;
  113. }
  114. if (x + size >= width - skip)
  115. size = width - skip - x;
  116. /* either raw data, or a run of a single color */
  117. if (raw) {
  118. if (next_line - data < size)
  119. return 1;
  120. memcpy(dest + x, data, size);
  121. data += size;
  122. } else {
  123. uint8_t color = bytestream_get_byte(&data);
  124. /* ignore transparent runs */
  125. if (color)
  126. memset(dest + x, color, size);
  127. }
  128. x += size;
  129. }
  130. }
  131. return 0;
  132. }
  133. static int cdtoons_decode_frame(AVCodecContext *avctx, void *data,
  134. int *got_frame, AVPacket *avpkt)
  135. {
  136. CDToonsContext *c = avctx->priv_data;
  137. const uint8_t *buf = avpkt->data;
  138. const uint8_t *eod = avpkt->data + avpkt->size;
  139. const int buf_size = avpkt->size;
  140. uint16_t frame_id;
  141. uint8_t background_color;
  142. uint16_t sprite_count, sprite_offset;
  143. uint8_t referenced_count;
  144. uint16_t palette_id;
  145. uint8_t palette_set;
  146. int ret;
  147. int saw_embedded_sprites = 0;
  148. if (buf_size < CDTOONS_HEADER_SIZE)
  149. return AVERROR_INVALIDDATA;
  150. if ((ret = ff_reget_buffer(avctx, c->frame, 0)) < 0)
  151. return ret;
  152. /* a lot of the header is useless junk in the absence of
  153. * dirty rectangling etc */
  154. buf += 2; /* version? (always 9?) */
  155. frame_id = bytestream_get_be16(&buf);
  156. buf += 2; /* blocks_valid_until */
  157. buf += 1;
  158. background_color = bytestream_get_byte(&buf);
  159. buf += 16; /* clip rect, dirty rect */
  160. buf += 4; /* flags */
  161. sprite_count = bytestream_get_be16(&buf);
  162. sprite_offset = bytestream_get_be16(&buf);
  163. buf += 2; /* max block id? */
  164. referenced_count = bytestream_get_byte(&buf);
  165. buf += 1;
  166. palette_id = bytestream_get_be16(&buf);
  167. palette_set = bytestream_get_byte(&buf);
  168. buf += 5;
  169. /* read new sprites introduced in this frame */
  170. buf = avpkt->data + sprite_offset;
  171. while (sprite_count--) {
  172. uint32_t size;
  173. uint16_t sprite_id;
  174. if (buf + 14 > eod)
  175. return AVERROR_INVALIDDATA;
  176. sprite_id = bytestream_get_be16(&buf);
  177. if (sprite_id >= CDTOONS_MAX_SPRITES) {
  178. av_log(avctx, AV_LOG_ERROR,
  179. "Sprite ID %d is too high.\n", sprite_id);
  180. return AVERROR_INVALIDDATA;
  181. }
  182. if (c->sprites[sprite_id].active) {
  183. av_log(avctx, AV_LOG_ERROR,
  184. "Sprite ID %d is a duplicate.\n", sprite_id);
  185. return AVERROR_INVALIDDATA;
  186. }
  187. c->sprites[sprite_id].flags = bytestream_get_be16(&buf);
  188. size = bytestream_get_be32(&buf);
  189. if (size < 14) {
  190. av_log(avctx, AV_LOG_ERROR,
  191. "Sprite only has %d bytes of data.\n", size);
  192. return AVERROR_INVALIDDATA;
  193. }
  194. size -= 14;
  195. c->sprites[sprite_id].size = size;
  196. c->sprites[sprite_id].owner_frame = frame_id;
  197. c->sprites[sprite_id].start_frame = bytestream_get_be16(&buf);
  198. c->sprites[sprite_id].end_frame = bytestream_get_be16(&buf);
  199. buf += 2;
  200. if (size > buf_size || buf + size > eod)
  201. return AVERROR_INVALIDDATA;
  202. av_fast_padded_malloc(&c->sprites[sprite_id].data, &c->sprites[sprite_id].alloc_size, size);
  203. if (!c->sprites[sprite_id].data)
  204. return AVERROR(ENOMEM);
  205. c->sprites[sprite_id].active = 1;
  206. bytestream_get_buffer(&buf, c->sprites[sprite_id].data, size);
  207. }
  208. /* render any embedded sprites */
  209. while (buf < eod) {
  210. uint32_t tag, size;
  211. if (buf + 8 > eod) {
  212. av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data for embedded sprites.\n");
  213. return AVERROR_INVALIDDATA;
  214. }
  215. tag = bytestream_get_be32(&buf);
  216. size = bytestream_get_be32(&buf);
  217. if (tag == MKBETAG('D', 'i', 'f', 'f')) {
  218. uint16_t diff_count;
  219. if (buf + 10 > eod) {
  220. av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data for Diff frame.\n");
  221. return AVERROR_INVALIDDATA;
  222. }
  223. diff_count = bytestream_get_be16(&buf);
  224. buf += 8; /* clip rect? */
  225. for (int i = 0; i < diff_count; i++) {
  226. int16_t top, left;
  227. uint16_t diff_size, width, height;
  228. if (buf + 16 > eod) {
  229. av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data for Diff frame header.\n");
  230. return AVERROR_INVALIDDATA;
  231. }
  232. top = bytestream_get_be16(&buf);
  233. left = bytestream_get_be16(&buf);
  234. buf += 4; /* bottom, right */
  235. diff_size = bytestream_get_be32(&buf);
  236. width = bytestream_get_be16(&buf);
  237. height = bytestream_get_be16(&buf);
  238. if (diff_size < 8 || diff_size - 4 > eod - buf) {
  239. av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data for Diff frame data.\n");
  240. return AVERROR_INVALIDDATA;
  241. }
  242. if (cdtoons_render_sprite(avctx, buf + 4, diff_size - 8,
  243. left, top, width, height)) {
  244. av_log(avctx, AV_LOG_WARNING, "Ran beyond end of sprite while rendering.\n");
  245. }
  246. buf += diff_size - 4;
  247. }
  248. saw_embedded_sprites = 1;
  249. } else {
  250. /* we don't care about any other entries */
  251. if (size < 8 || size - 8 > eod - buf) {
  252. av_log(avctx, AV_LOG_WARNING, "Ran out of data for ignored entry (size %X, %d left).\n", size, (int)(eod - buf));
  253. return AVERROR_INVALIDDATA;
  254. }
  255. buf += (size - 8);
  256. }
  257. }
  258. /* was an intra frame? */
  259. if (saw_embedded_sprites)
  260. goto done;
  261. /* render any referenced sprites */
  262. buf = avpkt->data + CDTOONS_HEADER_SIZE;
  263. eod = avpkt->data + sprite_offset;
  264. for (int i = 0; i < referenced_count; i++) {
  265. const uint8_t *block_data;
  266. uint16_t sprite_id, width, height;
  267. int16_t top, left, right;
  268. if (buf + 10 > eod) {
  269. av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data when rendering.\n");
  270. return AVERROR_INVALIDDATA;
  271. }
  272. sprite_id = bytestream_get_be16(&buf);
  273. top = bytestream_get_be16(&buf);
  274. left = bytestream_get_be16(&buf);
  275. buf += 2; /* bottom */
  276. right = bytestream_get_be16(&buf);
  277. if ((i == 0) && (sprite_id == 0)) {
  278. /* clear background */
  279. memset(c->frame->data[0], background_color,
  280. c->frame->linesize[0] * avctx->height);
  281. }
  282. if (!right)
  283. continue;
  284. if (sprite_id >= CDTOONS_MAX_SPRITES) {
  285. av_log(avctx, AV_LOG_ERROR,
  286. "Sprite ID %d is too high.\n", sprite_id);
  287. return AVERROR_INVALIDDATA;
  288. }
  289. block_data = c->sprites[sprite_id].data;
  290. if (!c->sprites[sprite_id].active) {
  291. /* this can happen when seeking around */
  292. av_log(avctx, AV_LOG_WARNING, "Sprite %d is missing.\n", sprite_id);
  293. continue;
  294. }
  295. if (c->sprites[sprite_id].size < 14) {
  296. av_log(avctx, AV_LOG_ERROR, "Sprite %d is too small.\n", sprite_id);
  297. continue;
  298. }
  299. height = bytestream_get_be16(&block_data);
  300. width = bytestream_get_be16(&block_data);
  301. block_data += 10;
  302. if (cdtoons_render_sprite(avctx, block_data,
  303. c->sprites[sprite_id].size - 14,
  304. left, top, width, height)) {
  305. av_log(avctx, AV_LOG_WARNING, "Ran beyond end of sprite while rendering.\n");
  306. }
  307. }
  308. if (palette_id && (palette_id != c->last_pal_id)) {
  309. if (palette_id >= CDTOONS_MAX_SPRITES) {
  310. av_log(avctx, AV_LOG_ERROR,
  311. "Palette ID %d is too high.\n", palette_id);
  312. return AVERROR_INVALIDDATA;
  313. }
  314. if (!c->sprites[palette_id].active) {
  315. /* this can happen when seeking around */
  316. av_log(avctx, AV_LOG_WARNING,
  317. "Palette ID %d is missing.\n", palette_id);
  318. goto done;
  319. }
  320. if (c->sprites[palette_id].size != 256 * 2 * 3) {
  321. av_log(avctx, AV_LOG_ERROR,
  322. "Palette ID %d is wrong size (%d).\n",
  323. palette_id, c->sprites[palette_id].size);
  324. return AVERROR_INVALIDDATA;
  325. }
  326. c->last_pal_id = palette_id;
  327. if (!palette_set) {
  328. uint8_t *palette_data = c->sprites[palette_id].data;
  329. for (int i = 0; i < 256; i++) {
  330. /* QuickTime-ish palette: 16-bit RGB components */
  331. unsigned r, g, b;
  332. r = *palette_data;
  333. g = *(palette_data + 2);
  334. b = *(palette_data + 4);
  335. c->pal[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
  336. palette_data += 6;
  337. }
  338. /* first palette entry indicates transparency */
  339. c->pal[0] = 0;
  340. c->frame->palette_has_changed = 1;
  341. }
  342. }
  343. done:
  344. /* discard outdated blocks */
  345. for (int i = 0; i < CDTOONS_MAX_SPRITES; i++) {
  346. if (c->sprites[i].end_frame > frame_id)
  347. continue;
  348. c->sprites[i].active = 0;
  349. }
  350. memcpy(c->frame->data[1], c->pal, AVPALETTE_SIZE);
  351. if ((ret = av_frame_ref(data, c->frame)) < 0)
  352. return ret;
  353. *got_frame = 1;
  354. /* always report that the buffer was completely consumed */
  355. return buf_size;
  356. }
  357. static av_cold int cdtoons_decode_init(AVCodecContext *avctx)
  358. {
  359. CDToonsContext *c = avctx->priv_data;
  360. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  361. c->last_pal_id = 0;
  362. c->frame = av_frame_alloc();
  363. if (!c->frame)
  364. return AVERROR(ENOMEM);
  365. return 0;
  366. }
  367. static void cdtoons_flush(AVCodecContext *avctx)
  368. {
  369. CDToonsContext *c = avctx->priv_data;
  370. c->last_pal_id = 0;
  371. for (int i = 0; i < CDTOONS_MAX_SPRITES; i++)
  372. c->sprites[i].active = 0;
  373. }
  374. static av_cold int cdtoons_decode_end(AVCodecContext *avctx)
  375. {
  376. CDToonsContext *c = avctx->priv_data;
  377. for (int i = 0; i < CDTOONS_MAX_SPRITES; i++) {
  378. av_freep(&c->sprites[i].data);
  379. c->sprites[i].active = 0;
  380. }
  381. av_frame_free(&c->frame);
  382. return 0;
  383. }
  384. AVCodec ff_cdtoons_decoder = {
  385. .name = "cdtoons",
  386. .long_name = NULL_IF_CONFIG_SMALL("CDToons video"),
  387. .type = AVMEDIA_TYPE_VIDEO,
  388. .id = AV_CODEC_ID_CDTOONS,
  389. .priv_data_size = sizeof(CDToonsContext),
  390. .init = cdtoons_decode_init,
  391. .close = cdtoons_decode_end,
  392. .decode = cdtoons_decode_frame,
  393. .capabilities = AV_CODEC_CAP_DR1,
  394. .flush = cdtoons_flush,
  395. };