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.

318 lines
9.9KB

  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. return 0;
  139. }
  140. static int cin_decode_rle(const unsigned char *src, int src_size,
  141. unsigned char *dst, int dst_size)
  142. {
  143. int len, code;
  144. unsigned char *dst_end = dst + dst_size;
  145. const unsigned char *src_end = src + src_size;
  146. while (src + 1 < src_end && dst < dst_end) {
  147. code = *src++;
  148. if (code & 0x80) {
  149. len = code - 0x7F;
  150. memset(dst, *src++, FFMIN(len, dst_end - dst));
  151. } else {
  152. len = code + 1;
  153. if (len > src_end-src) {
  154. av_log(NULL, AV_LOG_ERROR, "RLE overread\n");
  155. return AVERROR_INVALIDDATA;
  156. }
  157. memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
  158. src += len;
  159. }
  160. dst += len;
  161. }
  162. return 0;
  163. }
  164. static int cinvideo_decode_frame(AVCodecContext *avctx,
  165. void *data, int *got_frame,
  166. AVPacket *avpkt)
  167. {
  168. const uint8_t *buf = avpkt->data;
  169. int buf_size = avpkt->size;
  170. CinVideoContext *cin = avctx->priv_data;
  171. int i, y, palette_type, palette_colors_count,
  172. bitmap_frame_type, bitmap_frame_size, res = 0;
  173. palette_type = buf[0];
  174. palette_colors_count = AV_RL16(buf + 1);
  175. bitmap_frame_type = buf[3];
  176. buf += 4;
  177. bitmap_frame_size = buf_size - 4;
  178. /* handle palette */
  179. if (bitmap_frame_size < palette_colors_count * (3 + (palette_type != 0)))
  180. return AVERROR_INVALIDDATA;
  181. if (palette_type == 0) {
  182. if (palette_colors_count > 256)
  183. return AVERROR_INVALIDDATA;
  184. for (i = 0; i < palette_colors_count; ++i) {
  185. cin->palette[i] = 0xFFU << 24 | bytestream_get_le24(&buf);
  186. bitmap_frame_size -= 3;
  187. }
  188. } else {
  189. for (i = 0; i < palette_colors_count; ++i) {
  190. cin->palette[buf[0]] = 0xFFU << 24 | AV_RL24(buf + 1);
  191. buf += 4;
  192. bitmap_frame_size -= 4;
  193. }
  194. }
  195. /* note: the decoding routines below assumes that
  196. * surface.width = surface.pitch */
  197. switch (bitmap_frame_type) {
  198. case 9:
  199. cin_decode_rle(buf, bitmap_frame_size,
  200. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  201. break;
  202. case 34:
  203. cin_decode_rle(buf, bitmap_frame_size,
  204. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  205. cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
  206. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  207. break;
  208. case 35:
  209. bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
  210. cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
  211. cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
  212. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  213. break;
  214. case 36:
  215. bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
  216. cin->bitmap_table[CIN_INT_BMP],
  217. cin->bitmap_size);
  218. cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
  219. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  220. cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
  221. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  222. break;
  223. case 37:
  224. cin_decode_huffman(buf, bitmap_frame_size,
  225. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  226. break;
  227. case 38:
  228. res = cin_decode_lzss(buf, bitmap_frame_size,
  229. cin->bitmap_table[CIN_CUR_BMP],
  230. cin->bitmap_size);
  231. if (res < 0)
  232. return res;
  233. break;
  234. case 39:
  235. res = cin_decode_lzss(buf, bitmap_frame_size,
  236. cin->bitmap_table[CIN_CUR_BMP],
  237. cin->bitmap_size);
  238. if (res < 0)
  239. return res;
  240. cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
  241. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  242. break;
  243. }
  244. if ((res = ff_reget_buffer(avctx, cin->frame)) < 0)
  245. return res;
  246. memcpy(cin->frame->data[1], cin->palette, sizeof(cin->palette));
  247. cin->frame->palette_has_changed = 1;
  248. for (y = 0; y < cin->avctx->height; ++y)
  249. memcpy(cin->frame->data[0] + (cin->avctx->height - 1 - y) * cin->frame->linesize[0],
  250. cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
  251. cin->avctx->width);
  252. FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP],
  253. cin->bitmap_table[CIN_PRE_BMP]);
  254. if ((res = av_frame_ref(data, cin->frame)) < 0)
  255. return res;
  256. *got_frame = 1;
  257. return buf_size;
  258. }
  259. static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
  260. {
  261. CinVideoContext *cin = avctx->priv_data;
  262. av_frame_free(&cin->frame);
  263. destroy_buffers(cin);
  264. return 0;
  265. }
  266. AVCodec ff_dsicinvideo_decoder = {
  267. .name = "dsicinvideo",
  268. .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
  269. .type = AVMEDIA_TYPE_VIDEO,
  270. .id = AV_CODEC_ID_DSICINVIDEO,
  271. .priv_data_size = sizeof(CinVideoContext),
  272. .init = cinvideo_decode_init,
  273. .close = cinvideo_decode_end,
  274. .decode = cinvideo_decode_frame,
  275. .capabilities = CODEC_CAP_DR1,
  276. };