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.

302 lines
9.7KB

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