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.

458 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. if (sprite_offset > buf_size)
  170. return AVERROR_INVALIDDATA;
  171. /* read new sprites introduced in this frame */
  172. buf = avpkt->data + sprite_offset;
  173. while (sprite_count--) {
  174. uint32_t size;
  175. uint16_t sprite_id;
  176. if (buf + 14 > eod)
  177. return AVERROR_INVALIDDATA;
  178. sprite_id = bytestream_get_be16(&buf);
  179. if (sprite_id >= CDTOONS_MAX_SPRITES) {
  180. av_log(avctx, AV_LOG_ERROR,
  181. "Sprite ID %d is too high.\n", sprite_id);
  182. return AVERROR_INVALIDDATA;
  183. }
  184. if (c->sprites[sprite_id].active) {
  185. av_log(avctx, AV_LOG_ERROR,
  186. "Sprite ID %d is a duplicate.\n", sprite_id);
  187. return AVERROR_INVALIDDATA;
  188. }
  189. c->sprites[sprite_id].flags = bytestream_get_be16(&buf);
  190. size = bytestream_get_be32(&buf);
  191. if (size < 14) {
  192. av_log(avctx, AV_LOG_ERROR,
  193. "Sprite only has %d bytes of data.\n", size);
  194. return AVERROR_INVALIDDATA;
  195. }
  196. size -= 14;
  197. c->sprites[sprite_id].size = size;
  198. c->sprites[sprite_id].owner_frame = frame_id;
  199. c->sprites[sprite_id].start_frame = bytestream_get_be16(&buf);
  200. c->sprites[sprite_id].end_frame = bytestream_get_be16(&buf);
  201. buf += 2;
  202. if (size > buf_size || buf + size > eod)
  203. return AVERROR_INVALIDDATA;
  204. av_fast_padded_malloc(&c->sprites[sprite_id].data, &c->sprites[sprite_id].alloc_size, size);
  205. if (!c->sprites[sprite_id].data)
  206. return AVERROR(ENOMEM);
  207. c->sprites[sprite_id].active = 1;
  208. bytestream_get_buffer(&buf, c->sprites[sprite_id].data, size);
  209. }
  210. /* render any embedded sprites */
  211. while (buf < eod) {
  212. uint32_t tag, size;
  213. if (buf + 8 > eod) {
  214. av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data for embedded sprites.\n");
  215. return AVERROR_INVALIDDATA;
  216. }
  217. tag = bytestream_get_be32(&buf);
  218. size = bytestream_get_be32(&buf);
  219. if (tag == MKBETAG('D', 'i', 'f', 'f')) {
  220. uint16_t diff_count;
  221. if (buf + 10 > eod) {
  222. av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data for Diff frame.\n");
  223. return AVERROR_INVALIDDATA;
  224. }
  225. diff_count = bytestream_get_be16(&buf);
  226. buf += 8; /* clip rect? */
  227. for (int i = 0; i < diff_count; i++) {
  228. int16_t top, left;
  229. uint16_t diff_size, width, height;
  230. if (buf + 16 > eod) {
  231. av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data for Diff frame header.\n");
  232. return AVERROR_INVALIDDATA;
  233. }
  234. top = bytestream_get_be16(&buf);
  235. left = bytestream_get_be16(&buf);
  236. buf += 4; /* bottom, right */
  237. diff_size = bytestream_get_be32(&buf);
  238. width = bytestream_get_be16(&buf);
  239. height = bytestream_get_be16(&buf);
  240. if (diff_size < 8 || diff_size - 4 > eod - buf) {
  241. av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data for Diff frame data.\n");
  242. return AVERROR_INVALIDDATA;
  243. }
  244. if (cdtoons_render_sprite(avctx, buf + 4, diff_size - 8,
  245. left, top, width, height)) {
  246. av_log(avctx, AV_LOG_WARNING, "Ran beyond end of sprite while rendering.\n");
  247. }
  248. buf += diff_size - 4;
  249. }
  250. saw_embedded_sprites = 1;
  251. } else {
  252. /* we don't care about any other entries */
  253. if (size < 8 || size - 8 > eod - buf) {
  254. av_log(avctx, AV_LOG_WARNING, "Ran out of data for ignored entry (size %X, %d left).\n", size, (int)(eod - buf));
  255. return AVERROR_INVALIDDATA;
  256. }
  257. buf += (size - 8);
  258. }
  259. }
  260. /* was an intra frame? */
  261. if (saw_embedded_sprites)
  262. goto done;
  263. /* render any referenced sprites */
  264. buf = avpkt->data + CDTOONS_HEADER_SIZE;
  265. eod = avpkt->data + sprite_offset;
  266. for (int i = 0; i < referenced_count; i++) {
  267. const uint8_t *block_data;
  268. uint16_t sprite_id, width, height;
  269. int16_t top, left, right;
  270. if (buf + 10 > eod) {
  271. av_log(avctx, AV_LOG_WARNING, "Ran (seriously) out of data when rendering.\n");
  272. return AVERROR_INVALIDDATA;
  273. }
  274. sprite_id = bytestream_get_be16(&buf);
  275. top = bytestream_get_be16(&buf);
  276. left = bytestream_get_be16(&buf);
  277. buf += 2; /* bottom */
  278. right = bytestream_get_be16(&buf);
  279. if ((i == 0) && (sprite_id == 0)) {
  280. /* clear background */
  281. memset(c->frame->data[0], background_color,
  282. c->frame->linesize[0] * avctx->height);
  283. }
  284. if (!right)
  285. continue;
  286. if (sprite_id >= CDTOONS_MAX_SPRITES) {
  287. av_log(avctx, AV_LOG_ERROR,
  288. "Sprite ID %d is too high.\n", sprite_id);
  289. return AVERROR_INVALIDDATA;
  290. }
  291. block_data = c->sprites[sprite_id].data;
  292. if (!c->sprites[sprite_id].active) {
  293. /* this can happen when seeking around */
  294. av_log(avctx, AV_LOG_WARNING, "Sprite %d is missing.\n", sprite_id);
  295. continue;
  296. }
  297. if (c->sprites[sprite_id].size < 14) {
  298. av_log(avctx, AV_LOG_ERROR, "Sprite %d is too small.\n", sprite_id);
  299. continue;
  300. }
  301. height = bytestream_get_be16(&block_data);
  302. width = bytestream_get_be16(&block_data);
  303. block_data += 10;
  304. if (cdtoons_render_sprite(avctx, block_data,
  305. c->sprites[sprite_id].size - 14,
  306. left, top, width, height)) {
  307. av_log(avctx, AV_LOG_WARNING, "Ran beyond end of sprite while rendering.\n");
  308. }
  309. }
  310. if (palette_id && (palette_id != c->last_pal_id)) {
  311. if (palette_id >= CDTOONS_MAX_SPRITES) {
  312. av_log(avctx, AV_LOG_ERROR,
  313. "Palette ID %d is too high.\n", palette_id);
  314. return AVERROR_INVALIDDATA;
  315. }
  316. if (!c->sprites[palette_id].active) {
  317. /* this can happen when seeking around */
  318. av_log(avctx, AV_LOG_WARNING,
  319. "Palette ID %d is missing.\n", palette_id);
  320. goto done;
  321. }
  322. if (c->sprites[palette_id].size != 256 * 2 * 3) {
  323. av_log(avctx, AV_LOG_ERROR,
  324. "Palette ID %d is wrong size (%d).\n",
  325. palette_id, c->sprites[palette_id].size);
  326. return AVERROR_INVALIDDATA;
  327. }
  328. c->last_pal_id = palette_id;
  329. if (!palette_set) {
  330. uint8_t *palette_data = c->sprites[palette_id].data;
  331. for (int i = 0; i < 256; i++) {
  332. /* QuickTime-ish palette: 16-bit RGB components */
  333. unsigned r, g, b;
  334. r = *palette_data;
  335. g = *(palette_data + 2);
  336. b = *(palette_data + 4);
  337. c->pal[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
  338. palette_data += 6;
  339. }
  340. /* first palette entry indicates transparency */
  341. c->pal[0] = 0;
  342. c->frame->palette_has_changed = 1;
  343. }
  344. }
  345. done:
  346. /* discard outdated blocks */
  347. for (int i = 0; i < CDTOONS_MAX_SPRITES; i++) {
  348. if (c->sprites[i].end_frame > frame_id)
  349. continue;
  350. c->sprites[i].active = 0;
  351. }
  352. memcpy(c->frame->data[1], c->pal, AVPALETTE_SIZE);
  353. if ((ret = av_frame_ref(data, c->frame)) < 0)
  354. return ret;
  355. *got_frame = 1;
  356. /* always report that the buffer was completely consumed */
  357. return buf_size;
  358. }
  359. static av_cold int cdtoons_decode_init(AVCodecContext *avctx)
  360. {
  361. CDToonsContext *c = avctx->priv_data;
  362. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  363. c->last_pal_id = 0;
  364. c->frame = av_frame_alloc();
  365. if (!c->frame)
  366. return AVERROR(ENOMEM);
  367. return 0;
  368. }
  369. static void cdtoons_flush(AVCodecContext *avctx)
  370. {
  371. CDToonsContext *c = avctx->priv_data;
  372. c->last_pal_id = 0;
  373. for (int i = 0; i < CDTOONS_MAX_SPRITES; i++)
  374. c->sprites[i].active = 0;
  375. }
  376. static av_cold int cdtoons_decode_end(AVCodecContext *avctx)
  377. {
  378. CDToonsContext *c = avctx->priv_data;
  379. for (int i = 0; i < CDTOONS_MAX_SPRITES; i++) {
  380. av_freep(&c->sprites[i].data);
  381. c->sprites[i].active = 0;
  382. }
  383. av_frame_free(&c->frame);
  384. return 0;
  385. }
  386. AVCodec ff_cdtoons_decoder = {
  387. .name = "cdtoons",
  388. .long_name = NULL_IF_CONFIG_SMALL("CDToons video"),
  389. .type = AVMEDIA_TYPE_VIDEO,
  390. .id = AV_CODEC_ID_CDTOONS,
  391. .priv_data_size = sizeof(CDToonsContext),
  392. .init = cdtoons_decode_init,
  393. .close = cdtoons_decode_end,
  394. .decode = cdtoons_decode_frame,
  395. .capabilities = AV_CODEC_CAP_DR1,
  396. .flush = cdtoons_flush,
  397. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  398. };