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.

337 lines
10KB

  1. /*
  2. * Delphine Software International CIN video decoder
  3. * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
  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. * Delphine Software International CIN video decoder
  24. */
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "internal.h"
  28. typedef enum CinVideoBitmapIndex {
  29. CIN_CUR_BMP = 0, /* current */
  30. CIN_PRE_BMP = 1, /* previous */
  31. CIN_INT_BMP = 2 /* intermediate */
  32. } CinVideoBitmapIndex;
  33. typedef struct CinVideoContext {
  34. AVCodecContext *avctx;
  35. AVFrame *frame;
  36. unsigned int bitmap_size;
  37. uint32_t palette[256];
  38. uint8_t *bitmap_table[3];
  39. } CinVideoContext;
  40. static av_cold void destroy_buffers(CinVideoContext *cin)
  41. {
  42. int i;
  43. for (i = 0; i < 3; ++i)
  44. av_freep(&cin->bitmap_table[i]);
  45. }
  46. static av_cold int allocate_buffers(CinVideoContext *cin)
  47. {
  48. int i;
  49. for (i = 0; i < 3; ++i) {
  50. cin->bitmap_table[i] = av_mallocz(cin->bitmap_size);
  51. if (!cin->bitmap_table[i]) {
  52. av_log(cin->avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n");
  53. destroy_buffers(cin);
  54. return AVERROR(ENOMEM);
  55. }
  56. }
  57. return 0;
  58. }
  59. static av_cold int cinvideo_decode_init(AVCodecContext *avctx)
  60. {
  61. CinVideoContext *cin = avctx->priv_data;
  62. cin->avctx = avctx;
  63. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  64. cin->frame = av_frame_alloc();
  65. if (!cin->frame)
  66. return AVERROR(ENOMEM);
  67. cin->bitmap_size = avctx->width * avctx->height;
  68. if (allocate_buffers(cin))
  69. return AVERROR(ENOMEM);
  70. return 0;
  71. }
  72. static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
  73. int size)
  74. {
  75. while (size--)
  76. *dst++ += *src++;
  77. }
  78. static int cin_decode_huffman(const unsigned char *src, int src_size,
  79. unsigned char *dst, int dst_size)
  80. {
  81. int b, huff_code = 0;
  82. unsigned char huff_code_table[15];
  83. unsigned char *dst_cur = dst;
  84. unsigned char *dst_end = dst + dst_size;
  85. const unsigned char *src_end = src + src_size;
  86. memcpy(huff_code_table, src, 15);
  87. src += 15;
  88. while (src < src_end) {
  89. huff_code = *src++;
  90. if ((huff_code >> 4) == 15) {
  91. b = huff_code << 4;
  92. huff_code = *src++;
  93. *dst_cur++ = b | (huff_code >> 4);
  94. } else
  95. *dst_cur++ = huff_code_table[huff_code >> 4];
  96. if (dst_cur >= dst_end)
  97. break;
  98. huff_code &= 15;
  99. if (huff_code == 15) {
  100. *dst_cur++ = *src++;
  101. } else
  102. *dst_cur++ = huff_code_table[huff_code];
  103. if (dst_cur >= dst_end)
  104. break;
  105. }
  106. return dst_cur - dst;
  107. }
  108. static int cin_decode_lzss(const unsigned char *src, int src_size,
  109. unsigned char *dst, int dst_size)
  110. {
  111. uint16_t cmd;
  112. int i, sz, offset, code;
  113. unsigned char *dst_end = dst + dst_size, *dst_start = dst;
  114. const unsigned char *src_end = src + src_size;
  115. while (src < src_end && dst < dst_end) {
  116. code = *src++;
  117. for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
  118. if (code & (1 << i)) {
  119. *dst++ = *src++;
  120. } else {
  121. cmd = AV_RL16(src);
  122. src += 2;
  123. offset = cmd >> 4;
  124. if ((int)(dst - dst_start) < offset + 1)
  125. return AVERROR_INVALIDDATA;
  126. sz = (cmd & 0xF) + 2;
  127. /* don't use memcpy/memmove here as the decoding routine
  128. * (ab)uses buffer overlappings to repeat bytes in the
  129. * destination */
  130. sz = FFMIN(sz, dst_end - dst);
  131. while (sz--) {
  132. *dst = *(dst - offset - 1);
  133. ++dst;
  134. }
  135. }
  136. }
  137. }
  138. if (dst_end - dst > dst_size - dst_size/10)
  139. return AVERROR_INVALIDDATA;
  140. return 0;
  141. }
  142. static int cin_decode_rle(const unsigned char *src, int src_size,
  143. unsigned char *dst, int dst_size)
  144. {
  145. int len, code;
  146. unsigned char *dst_end = dst + dst_size;
  147. const unsigned char *src_end = src + src_size;
  148. while (src + 1 < src_end && dst < dst_end) {
  149. code = *src++;
  150. if (code & 0x80) {
  151. len = code - 0x7F;
  152. memset(dst, *src++, FFMIN(len, dst_end - dst));
  153. } else {
  154. len = code + 1;
  155. if (len > src_end-src) {
  156. av_log(NULL, AV_LOG_ERROR, "RLE overread\n");
  157. return AVERROR_INVALIDDATA;
  158. }
  159. memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
  160. src += len;
  161. }
  162. dst += len;
  163. }
  164. if (dst_end - dst > dst_size - dst_size/10)
  165. return AVERROR_INVALIDDATA;
  166. return 0;
  167. }
  168. static int cinvideo_decode_frame(AVCodecContext *avctx,
  169. void *data, int *got_frame,
  170. AVPacket *avpkt)
  171. {
  172. const uint8_t *buf = avpkt->data;
  173. int buf_size = avpkt->size;
  174. CinVideoContext *cin = avctx->priv_data;
  175. int i, y, palette_type, palette_colors_count,
  176. bitmap_frame_type, bitmap_frame_size, res = 0;
  177. palette_type = buf[0];
  178. palette_colors_count = AV_RL16(buf + 1);
  179. bitmap_frame_type = buf[3];
  180. buf += 4;
  181. bitmap_frame_size = buf_size - 4;
  182. /* handle palette */
  183. if (bitmap_frame_size < palette_colors_count * (3 + (palette_type != 0)))
  184. return AVERROR_INVALIDDATA;
  185. if (palette_type == 0) {
  186. if (palette_colors_count > 256)
  187. return AVERROR_INVALIDDATA;
  188. for (i = 0; i < palette_colors_count; ++i) {
  189. cin->palette[i] = 0xFFU << 24 | bytestream_get_le24(&buf);
  190. bitmap_frame_size -= 3;
  191. }
  192. } else {
  193. for (i = 0; i < palette_colors_count; ++i) {
  194. cin->palette[buf[0]] = 0xFFU << 24 | AV_RL24(buf + 1);
  195. buf += 4;
  196. bitmap_frame_size -= 4;
  197. }
  198. }
  199. /* note: the decoding routines below assumes that
  200. * surface.width = surface.pitch */
  201. switch (bitmap_frame_type) {
  202. case 9:
  203. res = cin_decode_rle(buf, bitmap_frame_size,
  204. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  205. if (res < 0)
  206. return res;
  207. break;
  208. case 34:
  209. res = cin_decode_rle(buf, bitmap_frame_size,
  210. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  211. if (res < 0)
  212. return res;
  213. cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
  214. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  215. break;
  216. case 35:
  217. bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
  218. cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
  219. res = cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
  220. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  221. if (res < 0)
  222. return res;
  223. break;
  224. case 36:
  225. bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
  226. cin->bitmap_table[CIN_INT_BMP],
  227. cin->bitmap_size);
  228. res = cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
  229. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  230. if (res < 0)
  231. return res;
  232. cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
  233. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  234. break;
  235. case 37:
  236. res = cin_decode_huffman(buf, bitmap_frame_size,
  237. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  238. if (cin->bitmap_size - avctx->discard_damaged_percentage*cin->bitmap_size/100 > res)
  239. return AVERROR_INVALIDDATA;
  240. break;
  241. case 38:
  242. res = cin_decode_lzss(buf, bitmap_frame_size,
  243. cin->bitmap_table[CIN_CUR_BMP],
  244. cin->bitmap_size);
  245. if (res < 0)
  246. return res;
  247. break;
  248. case 39:
  249. res = cin_decode_lzss(buf, bitmap_frame_size,
  250. cin->bitmap_table[CIN_CUR_BMP],
  251. cin->bitmap_size);
  252. if (res < 0)
  253. return res;
  254. cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
  255. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  256. break;
  257. }
  258. if ((res = ff_reget_buffer(avctx, cin->frame, 0)) < 0)
  259. return res;
  260. memcpy(cin->frame->data[1], cin->palette, sizeof(cin->palette));
  261. cin->frame->palette_has_changed = 1;
  262. for (y = 0; y < cin->avctx->height; ++y)
  263. memcpy(cin->frame->data[0] + (cin->avctx->height - 1 - y) * cin->frame->linesize[0],
  264. cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
  265. cin->avctx->width);
  266. FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP],
  267. cin->bitmap_table[CIN_PRE_BMP]);
  268. if ((res = av_frame_ref(data, cin->frame)) < 0)
  269. return res;
  270. *got_frame = 1;
  271. return buf_size;
  272. }
  273. static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
  274. {
  275. CinVideoContext *cin = avctx->priv_data;
  276. av_frame_free(&cin->frame);
  277. destroy_buffers(cin);
  278. return 0;
  279. }
  280. AVCodec ff_dsicinvideo_decoder = {
  281. .name = "dsicinvideo",
  282. .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
  283. .type = AVMEDIA_TYPE_VIDEO,
  284. .id = AV_CODEC_ID_DSICINVIDEO,
  285. .priv_data_size = sizeof(CinVideoContext),
  286. .init = cinvideo_decode_init,
  287. .close = cinvideo_decode_end,
  288. .decode = cinvideo_decode_frame,
  289. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  290. .capabilities = AV_CODEC_CAP_DR1,
  291. };